blob: bcb4b98a6b10a66f8b5ac73354f84aae8815dabe [file] [log] [blame]
fischman@webrtc.org540acde2014-02-13 03:56:14 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
fischman@webrtc.org540acde2014-02-13 03:56:14 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
fischman@webrtc.org540acde2014-02-13 03:56:14 +00009 */
10
fischman@webrtc.org540acde2014-02-13 03:56:14 +000011package org.webrtc;
12
Patrik Höglund68876f92015-11-12 17:36:48 +010013import android.annotation.TargetApi;
sakalb5f5bdc2017-08-10 04:15:42 -070014import android.graphics.Matrix;
fischman@webrtc.org540acde2014-02-13 03:56:14 +000015import android.media.MediaCodec;
16import android.media.MediaCodecInfo;
Sami Kalliomakid3235f02016-08-02 15:44:04 +020017import android.media.MediaCodecInfo.CodecCapabilities;
fischman@webrtc.org540acde2014-02-13 03:56:14 +000018import android.media.MediaCodecList;
19import android.media.MediaFormat;
perkj30e91822015-11-20 01:31:25 -080020import android.opengl.GLES20;
fischman@webrtc.org540acde2014-02-13 03:56:14 +000021import android.os.Build;
22import android.os.Bundle;
perkj30e91822015-11-20 01:31:25 -080023import android.view.Surface;
fischman@webrtc.org540acde2014-02-13 03:56:14 +000024import java.nio.ByteBuffer;
magjed295760d2017-01-12 01:11:57 -080025import java.util.ArrayList;
Alex Glaznev0c850202015-08-04 10:26:59 -070026import java.util.Arrays;
Alex Glazneveee86a62016-01-29 14:17:07 -080027import java.util.HashSet;
Alex Glaznev0c850202015-08-04 10:26:59 -070028import java.util.List;
Alex Glazneveee86a62016-01-29 14:17:07 -080029import java.util.Set;
Alex Glaznev5c3da4b2015-10-30 15:31:07 -070030import java.util.concurrent.CountDownLatch;
perkj48477c12015-12-18 00:34:37 -080031import java.util.concurrent.TimeUnit;
Magnus Jedvert655e1962017-12-08 11:05:22 +010032import org.webrtc.EglBase14;
33import org.webrtc.VideoFrame;
fischman@webrtc.org540acde2014-02-13 03:56:14 +000034
Magnus Jedvert9060eb12017-12-12 12:52:54 +010035// Java-side of peerconnection.cc:MediaCodecVideoEncoder.
fischman@webrtc.org540acde2014-02-13 03:56:14 +000036// This class is an implementation detail of the Java PeerConnection API.
Patrik Höglund68876f92015-11-12 17:36:48 +010037@TargetApi(19)
38@SuppressWarnings("deprecation")
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010039@JNINamespace("webrtc::jni")
perkj@webrtc.org47098872014-10-24 11:38:19 +000040public class MediaCodecVideoEncoder {
fischman@webrtc.org540acde2014-02-13 03:56:14 +000041 // This class is constructed, operated, and destroyed by its C++ incarnation,
42 // so the class and its methods have non-public visibility. The API this
43 // class exposes aims to mimic the webrtc::VideoEncoder API as closely as
44 // possibly to minimize the amount of translation work necessary.
45
46 private static final String TAG = "MediaCodecVideoEncoder";
47
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000048 // Tracks webrtc::VideoCodecType.
Magnus Jedvert655e1962017-12-08 11:05:22 +010049 public enum VideoCodecType {
50 VIDEO_CODEC_VP8,
51 VIDEO_CODEC_VP9,
52 VIDEO_CODEC_H264;
53
54 @CalledByNative("VideoCodecType")
55 static VideoCodecType fromNativeIndex(int nativeIndex) {
56 return values()[nativeIndex];
57 }
58 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000059
Alex Glaznev5c3da4b2015-10-30 15:31:07 -070060 private static final int MEDIA_CODEC_RELEASE_TIMEOUT_MS = 5000; // Timeout for codec releasing.
sakalb6760f92016-09-29 04:12:44 -070061 private static final int DEQUEUE_TIMEOUT = 0; // Non-blocking, no wait.
Alex Glaznev269fe752016-05-25 16:17:33 -070062 private static final int BITRATE_ADJUSTMENT_FPS = 30;
Alex Glaznevc55c39d2016-09-02 12:16:27 -070063 private static final int MAXIMUM_INITIAL_FPS = 30;
Alex Glaznevcfaca032016-09-06 14:08:19 -070064 private static final double BITRATE_CORRECTION_SEC = 3.0;
Alex Glaznev7fa4a722017-01-17 15:32:02 -080065 // Maximum bitrate correction scale - no more than 4 times.
66 private static final double BITRATE_CORRECTION_MAX_SCALE = 4;
Alex Glaznevcfaca032016-09-06 14:08:19 -070067 // Amount of correction steps to reach correction maximum scale.
Alex Glaznev7fa4a722017-01-17 15:32:02 -080068 private static final int BITRATE_CORRECTION_STEPS = 20;
Alex Glaznevc7483a72017-01-05 15:22:24 -080069 // Forced key frame interval - used to reduce color distortions on Qualcomm platform.
alexlau84ee5c62017-06-02 17:36:32 -070070 private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_L_MS = 15000;
71 private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_M_MS = 20000;
Alex Glaznevc7483a72017-01-05 15:22:24 -080072 private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_N_MS = 15000;
Alex Glaznevcfaca032016-09-06 14:08:19 -070073
perkj9576e542015-11-12 06:43:16 -080074 // Active running encoder instance. Set in initEncode() (called from native code)
Alex Glaznevc6aec4b2015-10-19 16:39:19 -070075 // and reset to null in release() call.
76 private static MediaCodecVideoEncoder runningInstance = null;
Alex Glaznev5c3da4b2015-10-30 15:31:07 -070077 private static MediaCodecVideoEncoderErrorCallback errorCallback = null;
78 private static int codecErrors = 0;
Alex Glazneveee86a62016-01-29 14:17:07 -080079 // List of disabled codec types - can be set from application.
80 private static Set<String> hwEncoderDisabledTypes = new HashSet<String>();
Alex Glaznev5c3da4b2015-10-30 15:31:07 -070081
Alejandro Luebs69ddaef2015-10-09 15:46:09 -070082 private Thread mediaCodecThread;
fischman@webrtc.org540acde2014-02-13 03:56:14 +000083 private MediaCodec mediaCodec;
84 private ByteBuffer[] outputBuffers;
perkj48477c12015-12-18 00:34:37 -080085 private EglBase14 eglBase;
glaznev3fc23502017-06-15 16:24:37 -070086 private int profile;
Magnus Jedvert51254332015-12-15 16:22:29 +010087 private int width;
88 private int height;
perkj30e91822015-11-20 01:31:25 -080089 private Surface inputSurface;
90 private GlRectDrawer drawer;
Alex Glaznev269fe752016-05-25 16:17:33 -070091
fischman@webrtc.org540acde2014-02-13 03:56:14 +000092 private static final String VP8_MIME_TYPE = "video/x-vnd.on2.vp8";
Alex Glaznevad948c42015-11-18 13:06:42 -080093 private static final String VP9_MIME_TYPE = "video/x-vnd.on2.vp9";
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +000094 private static final String H264_MIME_TYPE = "video/avc";
Alex Glaznev269fe752016-05-25 16:17:33 -070095
glaznev3fc23502017-06-15 16:24:37 -070096 private static final int VIDEO_AVCProfileHigh = 8;
97 private static final int VIDEO_AVCLevel3 = 0x100;
98
Alex Glaznevcfaca032016-09-06 14:08:19 -070099 // Type of bitrate adjustment for video encoder.
100 public enum BitrateAdjustmentType {
101 // No adjustment - video encoder has no known bitrate problem.
102 NO_ADJUSTMENT,
103 // Framerate based bitrate adjustment is required - HW encoder does not use frame
104 // timestamps to calculate frame bitrate budget and instead is relying on initial
105 // fps configuration assuming that all frames are coming at fixed initial frame rate.
106 FRAMERATE_ADJUSTMENT,
107 // Dynamic bitrate adjustment is required - HW encoder used frame timestamps, but actual
108 // bitrate deviates too much from the target value.
109 DYNAMIC_ADJUSTMENT
110 }
111
glaznev3fc23502017-06-15 16:24:37 -0700112 // Should be in sync with webrtc::H264::Profile.
113 public static enum H264Profile {
114 CONSTRAINED_BASELINE(0),
115 BASELINE(1),
116 MAIN(2),
117 CONSTRAINED_HIGH(3),
118 HIGH(4);
119
120 private final int value;
121
122 H264Profile(int value) {
123 this.value = value;
124 }
125
126 public int getValue() {
127 return value;
128 }
129 }
130
Alex Glaznev269fe752016-05-25 16:17:33 -0700131 // Class describing supported media codec properties.
132 private static class MediaCodecProperties {
133 public final String codecPrefix;
134 // Minimum Android SDK required for this codec to be used.
135 public final int minSdk;
136 // Flag if encoder implementation does not use frame timestamps to calculate frame bitrate
137 // budget and instead is relying on initial fps configuration assuming that all frames are
138 // coming at fixed initial frame rate. Bitrate adjustment is required for this case.
Alex Glaznevcfaca032016-09-06 14:08:19 -0700139 public final BitrateAdjustmentType bitrateAdjustmentType;
Alex Glaznev269fe752016-05-25 16:17:33 -0700140
141 MediaCodecProperties(
Alex Glaznevcfaca032016-09-06 14:08:19 -0700142 String codecPrefix, int minSdk, BitrateAdjustmentType bitrateAdjustmentType) {
Alex Glaznev269fe752016-05-25 16:17:33 -0700143 this.codecPrefix = codecPrefix;
144 this.minSdk = minSdk;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700145 this.bitrateAdjustmentType = bitrateAdjustmentType;
Alex Glaznev269fe752016-05-25 16:17:33 -0700146 }
147 }
148
Alex Glaznevdcd730f2016-04-21 17:01:46 -0700149 // List of supported HW VP8 encoders.
Alex Glaznev269fe752016-05-25 16:17:33 -0700150 private static final MediaCodecProperties qcomVp8HwProperties = new MediaCodecProperties(
Alex Glaznevcfaca032016-09-06 14:08:19 -0700151 "OMX.qcom.", Build.VERSION_CODES.KITKAT, BitrateAdjustmentType.NO_ADJUSTMENT);
Alex Glaznev269fe752016-05-25 16:17:33 -0700152 private static final MediaCodecProperties exynosVp8HwProperties = new MediaCodecProperties(
Alex Glaznevcfaca032016-09-06 14:08:19 -0700153 "OMX.Exynos.", Build.VERSION_CODES.M, BitrateAdjustmentType.DYNAMIC_ADJUSTMENT);
magjed295760d2017-01-12 01:11:57 -0800154 private static final MediaCodecProperties intelVp8HwProperties = new MediaCodecProperties(
155 "OMX.Intel.", Build.VERSION_CODES.LOLLIPOP, BitrateAdjustmentType.NO_ADJUSTMENT);
156 private static MediaCodecProperties[] vp8HwList() {
157 final ArrayList<MediaCodecProperties> supported_codecs = new ArrayList<MediaCodecProperties>();
158 supported_codecs.add(qcomVp8HwProperties);
159 supported_codecs.add(exynosVp8HwProperties);
160 if (PeerConnectionFactory.fieldTrialsFindFullName("WebRTC-IntelVP8").equals("Enabled")) {
161 supported_codecs.add(intelVp8HwProperties);
162 }
163 return supported_codecs.toArray(new MediaCodecProperties[supported_codecs.size()]);
164 }
Alex Glaznev269fe752016-05-25 16:17:33 -0700165
Alex Glaznevdcd730f2016-04-21 17:01:46 -0700166 // List of supported HW VP9 encoders.
Alex Glaznev269fe752016-05-25 16:17:33 -0700167 private static final MediaCodecProperties qcomVp9HwProperties = new MediaCodecProperties(
glaznev6fac4292017-06-02 20:18:54 -0700168 "OMX.qcom.", Build.VERSION_CODES.N, BitrateAdjustmentType.NO_ADJUSTMENT);
Alex Glaznev269fe752016-05-25 16:17:33 -0700169 private static final MediaCodecProperties exynosVp9HwProperties = new MediaCodecProperties(
glaznev6fac4292017-06-02 20:18:54 -0700170 "OMX.Exynos.", Build.VERSION_CODES.N, BitrateAdjustmentType.FRAMERATE_ADJUSTMENT);
sakalb6760f92016-09-29 04:12:44 -0700171 private static final MediaCodecProperties[] vp9HwList =
172 new MediaCodecProperties[] {qcomVp9HwProperties, exynosVp9HwProperties};
Alex Glaznev269fe752016-05-25 16:17:33 -0700173
Alex Glaznevdcd730f2016-04-21 17:01:46 -0700174 // List of supported HW H.264 encoders.
Alex Glaznev269fe752016-05-25 16:17:33 -0700175 private static final MediaCodecProperties qcomH264HwProperties = new MediaCodecProperties(
Alex Glaznevcfaca032016-09-06 14:08:19 -0700176 "OMX.qcom.", Build.VERSION_CODES.KITKAT, BitrateAdjustmentType.NO_ADJUSTMENT);
Alex Glaznev269fe752016-05-25 16:17:33 -0700177 private static final MediaCodecProperties exynosH264HwProperties = new MediaCodecProperties(
Alex Glaznevcfaca032016-09-06 14:08:19 -0700178 "OMX.Exynos.", Build.VERSION_CODES.LOLLIPOP, BitrateAdjustmentType.FRAMERATE_ADJUSTMENT);
Alex Leung5b6891a2018-01-18 10:01:14 -0800179 private static final MediaCodecProperties mediatekH264HwProperties = new MediaCodecProperties(
180 "OMX.MTK.", Build.VERSION_CODES.O, BitrateAdjustmentType.FRAMERATE_ADJUSTMENT);
181 private static final MediaCodecProperties[] h264HwList() {
182 final ArrayList<MediaCodecProperties> supported_codecs = new ArrayList<MediaCodecProperties>();
183 supported_codecs.add(qcomH264HwProperties);
184 supported_codecs.add(exynosH264HwProperties);
185 if (PeerConnectionFactory.fieldTrialsFindFullName("WebRTC-MediaTekH264").equals("Enabled")) {
186 supported_codecs.add(mediatekH264HwProperties);
187 }
188 return supported_codecs.toArray(new MediaCodecProperties[supported_codecs.size()]);
189 }
Alex Glaznev269fe752016-05-25 16:17:33 -0700190
glaznev3fc23502017-06-15 16:24:37 -0700191 // List of supported HW H.264 high profile encoders.
192 private static final MediaCodecProperties exynosH264HighProfileHwProperties =
193 new MediaCodecProperties(
194 "OMX.Exynos.", Build.VERSION_CODES.M, BitrateAdjustmentType.FRAMERATE_ADJUSTMENT);
195 private static final MediaCodecProperties[] h264HighProfileHwList =
196 new MediaCodecProperties[] {exynosH264HighProfileHwProperties};
197
Alex Glaznev0c850202015-08-04 10:26:59 -0700198 // List of devices with poor H.264 encoder quality.
sakalb6760f92016-09-29 04:12:44 -0700199 // HW H.264 encoder on below devices has poor bitrate control - actual
200 // bitrates deviates a lot from the target value.
201 private static final String[] H264_HW_EXCEPTION_MODELS =
202 new String[] {"SAMSUNG-SGH-I337", "Nexus 7", "Nexus 4"};
Alex Glaznev0c850202015-08-04 10:26:59 -0700203
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000204 // Bitrate modes - should be in sync with OMX_VIDEO_CONTROLRATETYPE defined
205 // in OMX_Video.h
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000206 private static final int VIDEO_ControlRateConstant = 2;
207 // NV12 color format supported by QCOM codec, but not declared in MediaCodec -
208 // see /hardware/qcom/media/mm-core/inc/OMX_QCOMExtns.h
sakalb6760f92016-09-29 04:12:44 -0700209 private static final int COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m = 0x7FA30C04;
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000210 // Allowable color formats supported by codec - in order of preference.
sakalb6760f92016-09-29 04:12:44 -0700211 private static final int[] supportedColorList = {CodecCapabilities.COLOR_FormatYUV420Planar,
212 CodecCapabilities.COLOR_FormatYUV420SemiPlanar,
213 CodecCapabilities.COLOR_QCOM_FormatYUV420SemiPlanar,
214 COLOR_QCOM_FORMATYUV420PackedSemiPlanar32m};
215 private static final int[] supportedSurfaceColorList = {CodecCapabilities.COLOR_FormatSurface};
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000216 private VideoCodecType type;
Magnus Jedvert655e1962017-12-08 11:05:22 +0100217 private int colorFormat;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700218
219 // Variables used for dynamic bitrate adjustment.
220 private BitrateAdjustmentType bitrateAdjustmentType = BitrateAdjustmentType.NO_ADJUSTMENT;
221 private double bitrateAccumulator;
222 private double bitrateAccumulatorMax;
223 private double bitrateObservationTimeMs;
224 private int bitrateAdjustmentScaleExp;
225 private int targetBitrateBps;
226 private int targetFps;
perkj9576e542015-11-12 06:43:16 -0800227
Alex Glaznevc7483a72017-01-05 15:22:24 -0800228 // Interval in ms to force key frame generation. Used to reduce the time of color distortions
229 // happened sometime when using Qualcomm video encoder.
230 private long forcedKeyFrameMs;
231 private long lastKeyFrameMs;
232
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000233 // SPS and PPS NALs (Config frame) for H.264.
234 private ByteBuffer configData = null;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000235
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700236 // MediaCodec error handler - invoked when critical error happens which may prevent
237 // further use of media codec API. Now it means that one of media codec instances
238 // is hanging and can no longer be used in the next call.
239 public static interface MediaCodecVideoEncoderErrorCallback {
240 void onMediaCodecVideoEncoderCriticalError(int codecErrors);
241 }
242
243 public static void setErrorCallback(MediaCodecVideoEncoderErrorCallback errorCallback) {
244 Logging.d(TAG, "Set error callback");
245 MediaCodecVideoEncoder.errorCallback = errorCallback;
246 }
247
Alex Glazneveee86a62016-01-29 14:17:07 -0800248 // Functions to disable HW encoding - can be called from applications for platforms
249 // which have known HW decoding problems.
250 public static void disableVp8HwCodec() {
251 Logging.w(TAG, "VP8 encoding is disabled by application.");
252 hwEncoderDisabledTypes.add(VP8_MIME_TYPE);
253 }
254
255 public static void disableVp9HwCodec() {
256 Logging.w(TAG, "VP9 encoding is disabled by application.");
257 hwEncoderDisabledTypes.add(VP9_MIME_TYPE);
258 }
259
260 public static void disableH264HwCodec() {
261 Logging.w(TAG, "H.264 encoding is disabled by application.");
262 hwEncoderDisabledTypes.add(H264_MIME_TYPE);
263 }
264
265 // Functions to query if HW encoding is supported.
Magnus Jedvert655e1962017-12-08 11:05:22 +0100266 @CalledByNative
Alex Glazneveee86a62016-01-29 14:17:07 -0800267 public static boolean isVp8HwSupported() {
sakalb6760f92016-09-29 04:12:44 -0700268 return !hwEncoderDisabledTypes.contains(VP8_MIME_TYPE)
magjed295760d2017-01-12 01:11:57 -0800269 && (findHwEncoder(VP8_MIME_TYPE, vp8HwList(), supportedColorList) != null);
Alex Glazneveee86a62016-01-29 14:17:07 -0800270 }
271
Alex Glaznevc7483a72017-01-05 15:22:24 -0800272 public static EncoderProperties vp8HwEncoderProperties() {
273 if (hwEncoderDisabledTypes.contains(VP8_MIME_TYPE)) {
274 return null;
275 } else {
magjed295760d2017-01-12 01:11:57 -0800276 return findHwEncoder(VP8_MIME_TYPE, vp8HwList(), supportedColorList);
Alex Glaznevc7483a72017-01-05 15:22:24 -0800277 }
278 }
279
Magnus Jedvert655e1962017-12-08 11:05:22 +0100280 @CalledByNative
Alex Glazneveee86a62016-01-29 14:17:07 -0800281 public static boolean isVp9HwSupported() {
sakalb6760f92016-09-29 04:12:44 -0700282 return !hwEncoderDisabledTypes.contains(VP9_MIME_TYPE)
283 && (findHwEncoder(VP9_MIME_TYPE, vp9HwList, supportedColorList) != null);
Alex Glazneveee86a62016-01-29 14:17:07 -0800284 }
285
Magnus Jedvert655e1962017-12-08 11:05:22 +0100286 @CalledByNative
Alex Glazneveee86a62016-01-29 14:17:07 -0800287 public static boolean isH264HwSupported() {
sakalb6760f92016-09-29 04:12:44 -0700288 return !hwEncoderDisabledTypes.contains(H264_MIME_TYPE)
Alex Leung5b6891a2018-01-18 10:01:14 -0800289 && (findHwEncoder(H264_MIME_TYPE, h264HwList(), supportedColorList) != null);
Alex Glazneveee86a62016-01-29 14:17:07 -0800290 }
291
glaznev3fc23502017-06-15 16:24:37 -0700292 public static boolean isH264HighProfileHwSupported() {
293 return !hwEncoderDisabledTypes.contains(H264_MIME_TYPE)
294 && (findHwEncoder(H264_MIME_TYPE, h264HighProfileHwList, supportedColorList) != null);
295 }
296
Alex Glazneveee86a62016-01-29 14:17:07 -0800297 public static boolean isVp8HwSupportedUsingTextures() {
sakalb6760f92016-09-29 04:12:44 -0700298 return !hwEncoderDisabledTypes.contains(VP8_MIME_TYPE)
magjed295760d2017-01-12 01:11:57 -0800299 && (findHwEncoder(VP8_MIME_TYPE, vp8HwList(), supportedSurfaceColorList) != null);
Alex Glazneveee86a62016-01-29 14:17:07 -0800300 }
301
302 public static boolean isVp9HwSupportedUsingTextures() {
sakalb6760f92016-09-29 04:12:44 -0700303 return !hwEncoderDisabledTypes.contains(VP9_MIME_TYPE)
304 && (findHwEncoder(VP9_MIME_TYPE, vp9HwList, supportedSurfaceColorList) != null);
Alex Glazneveee86a62016-01-29 14:17:07 -0800305 }
306
307 public static boolean isH264HwSupportedUsingTextures() {
sakalb6760f92016-09-29 04:12:44 -0700308 return !hwEncoderDisabledTypes.contains(H264_MIME_TYPE)
Alex Leung5b6891a2018-01-18 10:01:14 -0800309 && (findHwEncoder(H264_MIME_TYPE, h264HwList(), supportedSurfaceColorList) != null);
Alex Glazneveee86a62016-01-29 14:17:07 -0800310 }
311
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000312 // Helper struct for findHwEncoder() below.
Alex Glaznevc7483a72017-01-05 15:22:24 -0800313 public static class EncoderProperties {
Alex Glaznevcfaca032016-09-06 14:08:19 -0700314 public EncoderProperties(
315 String codecName, int colorFormat, BitrateAdjustmentType bitrateAdjustmentType) {
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000316 this.codecName = codecName;
317 this.colorFormat = colorFormat;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700318 this.bitrateAdjustmentType = bitrateAdjustmentType;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000319 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000320 public final String codecName; // OpenMax component name for HW codec.
sakalb6760f92016-09-29 04:12:44 -0700321 public final int colorFormat; // Color format supported by codec.
Alex Glaznevcfaca032016-09-06 14:08:19 -0700322 public final BitrateAdjustmentType bitrateAdjustmentType; // Bitrate adjustment type
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000323 }
324
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000325 private static EncoderProperties findHwEncoder(
Alex Glaznev269fe752016-05-25 16:17:33 -0700326 String mime, MediaCodecProperties[] supportedHwCodecProperties, int[] colorList) {
Alex Glaznev0c850202015-08-04 10:26:59 -0700327 // MediaCodec.setParameters is missing for JB and below, so bitrate
328 // can not be adjusted dynamically.
329 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
330 return null;
331 }
332
333 // Check if device is in H.264 exception list.
334 if (mime.equals(H264_MIME_TYPE)) {
335 List<String> exceptionModels = Arrays.asList(H264_HW_EXCEPTION_MODELS);
336 if (exceptionModels.contains(Build.MODEL)) {
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700337 Logging.w(TAG, "Model: " + Build.MODEL + " has black listed H.264 encoder.");
Alex Glaznev0c850202015-08-04 10:26:59 -0700338 return null;
339 }
340 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000341
342 for (int i = 0; i < MediaCodecList.getCodecCount(); ++i) {
Alex Glaznev0060c562016-08-08 12:27:24 -0700343 MediaCodecInfo info = null;
344 try {
345 info = MediaCodecList.getCodecInfoAt(i);
346 } catch (IllegalArgumentException e) {
sakalb6760f92016-09-29 04:12:44 -0700347 Logging.e(TAG, "Cannot retrieve encoder codec info", e);
Alex Glaznev0060c562016-08-08 12:27:24 -0700348 }
349 if (info == null || !info.isEncoder()) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000350 continue;
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000351 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000352 String name = null;
353 for (String mimeType : info.getSupportedTypes()) {
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000354 if (mimeType.equals(mime)) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000355 name = info.getName();
356 break;
357 }
358 }
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000359 if (name == null) {
sakalb6760f92016-09-29 04:12:44 -0700360 continue; // No HW support in this codec; try the next one.
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000361 }
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700362 Logging.v(TAG, "Found candidate encoder " + name);
glaznev@webrtc.org99678452014-09-15 17:52:42 +0000363
364 // Check if this is supported HW encoder.
365 boolean supportedCodec = false;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700366 BitrateAdjustmentType bitrateAdjustmentType = BitrateAdjustmentType.NO_ADJUSTMENT;
Alex Glaznev269fe752016-05-25 16:17:33 -0700367 for (MediaCodecProperties codecProperties : supportedHwCodecProperties) {
368 if (name.startsWith(codecProperties.codecPrefix)) {
369 if (Build.VERSION.SDK_INT < codecProperties.minSdk) {
sakalb6760f92016-09-29 04:12:44 -0700370 Logging.w(
371 TAG, "Codec " + name + " is disabled due to SDK version " + Build.VERSION.SDK_INT);
Alex Glaznev269fe752016-05-25 16:17:33 -0700372 continue;
373 }
Alex Glaznevcfaca032016-09-06 14:08:19 -0700374 if (codecProperties.bitrateAdjustmentType != BitrateAdjustmentType.NO_ADJUSTMENT) {
375 bitrateAdjustmentType = codecProperties.bitrateAdjustmentType;
sakalb6760f92016-09-29 04:12:44 -0700376 Logging.w(
377 TAG, "Codec " + name + " requires bitrate adjustment: " + bitrateAdjustmentType);
Alex Glaznev269fe752016-05-25 16:17:33 -0700378 }
glaznev@webrtc.org99678452014-09-15 17:52:42 +0000379 supportedCodec = true;
380 break;
381 }
382 }
383 if (!supportedCodec) {
384 continue;
385 }
386
Alex Glaznev269fe752016-05-25 16:17:33 -0700387 // Check if HW codec supports known color format.
Alex Glaznev0060c562016-08-08 12:27:24 -0700388 CodecCapabilities capabilities;
389 try {
390 capabilities = info.getCapabilitiesForType(mime);
391 } catch (IllegalArgumentException e) {
sakalb6760f92016-09-29 04:12:44 -0700392 Logging.e(TAG, "Cannot retrieve encoder capabilities", e);
Alex Glaznev0060c562016-08-08 12:27:24 -0700393 continue;
394 }
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000395 for (int colorFormat : capabilities.colorFormats) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700396 Logging.v(TAG, " Color: 0x" + Integer.toHexString(colorFormat));
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000397 }
398
perkj30e91822015-11-20 01:31:25 -0800399 for (int supportedColorFormat : colorList) {
glaznev@webrtc.org99678452014-09-15 17:52:42 +0000400 for (int codecColorFormat : capabilities.colorFormats) {
401 if (codecColorFormat == supportedColorFormat) {
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000402 // Found supported HW encoder.
sakalb6760f92016-09-29 04:12:44 -0700403 Logging.d(TAG, "Found target encoder for mime " + mime + " : " + name + ". Color: 0x"
404 + Integer.toHexString(codecColorFormat) + ". Bitrate adjustment: "
405 + bitrateAdjustmentType);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700406 return new EncoderProperties(name, codecColorFormat, bitrateAdjustmentType);
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000407 }
408 }
409 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000410 }
sakalb6760f92016-09-29 04:12:44 -0700411 return null; // No HW encoder.
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000412 }
413
Magnus Jedvert655e1962017-12-08 11:05:22 +0100414 @CalledByNative
415 MediaCodecVideoEncoder() {}
416
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000417 private void checkOnMediaCodecThread() {
418 if (mediaCodecThread.getId() != Thread.currentThread().getId()) {
sakalb6760f92016-09-29 04:12:44 -0700419 throw new RuntimeException("MediaCodecVideoEncoder previously operated on " + mediaCodecThread
420 + " but is now called on " + Thread.currentThread());
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000421 }
422 }
423
Alex Glaznev325d4142015-10-12 14:56:02 -0700424 public static void printStackTrace() {
Alex Glaznevc6aec4b2015-10-19 16:39:19 -0700425 if (runningInstance != null && runningInstance.mediaCodecThread != null) {
426 StackTraceElement[] mediaCodecStackTraces = runningInstance.mediaCodecThread.getStackTrace();
Alex Glaznev325d4142015-10-12 14:56:02 -0700427 if (mediaCodecStackTraces.length > 0) {
428 Logging.d(TAG, "MediaCodecVideoEncoder stacks trace:");
429 for (StackTraceElement stackTrace : mediaCodecStackTraces) {
430 Logging.d(TAG, stackTrace.toString());
431 }
432 }
433 }
434 }
435
henrike@webrtc.org528fc652014-10-06 17:56:43 +0000436 static MediaCodec createByCodecName(String codecName) {
437 try {
438 // In the L-SDK this call can throw IOException so in order to work in
439 // both cases catch an exception.
440 return MediaCodec.createByCodecName(codecName);
441 } catch (Exception e) {
442 return null;
443 }
444 }
445
Magnus Jedvert655e1962017-12-08 11:05:22 +0100446 @CalledByNativeUnchecked
glaznev3fc23502017-06-15 16:24:37 -0700447 boolean initEncode(VideoCodecType type, int profile, int width, int height, int kbps, int fps,
perkj48477c12015-12-18 00:34:37 -0800448 EglBase14.Context sharedContext) {
perkj30e91822015-11-20 01:31:25 -0800449 final boolean useSurface = sharedContext != null;
glaznev3fc23502017-06-15 16:24:37 -0700450 Logging.d(TAG,
451 "Java initEncode: " + type + ". Profile: " + profile + " : " + width + " x " + height
452 + ". @ " + kbps + " kbps. Fps: " + fps + ". Encode from texture : " + useSurface);
perkj9576e542015-11-12 06:43:16 -0800453
glaznev3fc23502017-06-15 16:24:37 -0700454 this.profile = profile;
Magnus Jedvert51254332015-12-15 16:22:29 +0100455 this.width = width;
456 this.height = height;
Alejandro Luebs69ddaef2015-10-09 15:46:09 -0700457 if (mediaCodecThread != null) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000458 throw new RuntimeException("Forgot to release()?");
459 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000460 EncoderProperties properties = null;
461 String mime = null;
462 int keyFrameIntervalSec = 0;
glaznev3fc23502017-06-15 16:24:37 -0700463 boolean configureH264HighProfile = false;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000464 if (type == VideoCodecType.VIDEO_CODEC_VP8) {
465 mime = VP8_MIME_TYPE;
Alex Glaznev269fe752016-05-25 16:17:33 -0700466 properties = findHwEncoder(
magjed295760d2017-01-12 01:11:57 -0800467 VP8_MIME_TYPE, vp8HwList(), useSurface ? supportedSurfaceColorList : supportedColorList);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000468 keyFrameIntervalSec = 100;
Alex Glaznevad948c42015-11-18 13:06:42 -0800469 } else if (type == VideoCodecType.VIDEO_CODEC_VP9) {
470 mime = VP9_MIME_TYPE;
Alex Glaznev269fe752016-05-25 16:17:33 -0700471 properties = findHwEncoder(
472 VP9_MIME_TYPE, vp9HwList, useSurface ? supportedSurfaceColorList : supportedColorList);
Alex Glaznevad948c42015-11-18 13:06:42 -0800473 keyFrameIntervalSec = 100;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000474 } else if (type == VideoCodecType.VIDEO_CODEC_H264) {
475 mime = H264_MIME_TYPE;
Alex Leung5b6891a2018-01-18 10:01:14 -0800476 properties = findHwEncoder(H264_MIME_TYPE, h264HwList(),
477 useSurface ? supportedSurfaceColorList : supportedColorList);
glaznev3fc23502017-06-15 16:24:37 -0700478 if (profile == H264Profile.CONSTRAINED_HIGH.getValue()) {
479 EncoderProperties h264HighProfileProperties = findHwEncoder(H264_MIME_TYPE,
480 h264HighProfileHwList, useSurface ? supportedSurfaceColorList : supportedColorList);
481 if (h264HighProfileProperties != null) {
482 Logging.d(TAG, "High profile H.264 encoder supported.");
483 configureH264HighProfile = true;
484 } else {
485 Logging.d(TAG, "High profile H.264 encoder requested, but not supported. Use baseline.");
486 }
487 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000488 keyFrameIntervalSec = 20;
489 }
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000490 if (properties == null) {
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000491 throw new RuntimeException("Can not find HW encoder for " + type);
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000492 }
Alex Glaznevc6aec4b2015-10-19 16:39:19 -0700493 runningInstance = this; // Encoder is now running and can be queried for stack traces.
perkj9576e542015-11-12 06:43:16 -0800494 colorFormat = properties.colorFormat;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700495 bitrateAdjustmentType = properties.bitrateAdjustmentType;
496 if (bitrateAdjustmentType == BitrateAdjustmentType.FRAMERATE_ADJUSTMENT) {
Alex Glaznev269fe752016-05-25 16:17:33 -0700497 fps = BITRATE_ADJUSTMENT_FPS;
sakalb6760f92016-09-29 04:12:44 -0700498 } else {
Alex Glaznevc55c39d2016-09-02 12:16:27 -0700499 fps = Math.min(fps, MAXIMUM_INITIAL_FPS);
Alex Glaznev269fe752016-05-25 16:17:33 -0700500 }
Alex Glaznevc7483a72017-01-05 15:22:24 -0800501
502 forcedKeyFrameMs = 0;
503 lastKeyFrameMs = -1;
Alex Glaznev512f00b2017-01-05 16:40:46 -0800504 if (type == VideoCodecType.VIDEO_CODEC_VP8
505 && properties.codecName.startsWith(qcomVp8HwProperties.codecPrefix)) {
alexlau84ee5c62017-06-02 17:36:32 -0700506 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP
507 || Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1) {
508 forcedKeyFrameMs = QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_L_MS;
509 } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
Alex Glaznevc7483a72017-01-05 15:22:24 -0800510 forcedKeyFrameMs = QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_M_MS;
511 } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
512 forcedKeyFrameMs = QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_N_MS;
513 }
514 }
515
sakalb6760f92016-09-29 04:12:44 -0700516 Logging.d(TAG, "Color format: " + colorFormat + ". Bitrate adjustment: " + bitrateAdjustmentType
Alex Glaznevc7483a72017-01-05 15:22:24 -0800517 + ". Key frame interval: " + forcedKeyFrameMs + " . Initial fps: " + fps);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700518 targetBitrateBps = 1000 * kbps;
519 targetFps = fps;
520 bitrateAccumulatorMax = targetBitrateBps / 8.0;
521 bitrateAccumulator = 0;
522 bitrateObservationTimeMs = 0;
523 bitrateAdjustmentScaleExp = 0;
perkj9576e542015-11-12 06:43:16 -0800524
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000525 mediaCodecThread = Thread.currentThread();
526 try {
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000527 MediaFormat format = MediaFormat.createVideoFormat(mime, width, height);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700528 format.setInteger(MediaFormat.KEY_BIT_RATE, targetBitrateBps);
Alex Glaznev8a2cd3d2015-08-11 11:32:53 -0700529 format.setInteger("bitrate-mode", VIDEO_ControlRateConstant);
glaznev@webrtc.orga40210a2014-06-10 23:48:29 +0000530 format.setInteger(MediaFormat.KEY_COLOR_FORMAT, properties.colorFormat);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700531 format.setInteger(MediaFormat.KEY_FRAME_RATE, targetFps);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000532 format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, keyFrameIntervalSec);
glaznev3fc23502017-06-15 16:24:37 -0700533 if (configureH264HighProfile) {
534 format.setInteger("profile", VIDEO_AVCProfileHigh);
535 format.setInteger("level", VIDEO_AVCLevel3);
536 }
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700537 Logging.d(TAG, " Format: " + format);
henrike@webrtc.org528fc652014-10-06 17:56:43 +0000538 mediaCodec = createByCodecName(properties.codecName);
perkj9576e542015-11-12 06:43:16 -0800539 this.type = type;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000540 if (mediaCodec == null) {
Alex Glaznev325d4142015-10-12 14:56:02 -0700541 Logging.e(TAG, "Can not create media encoder");
sakal996a83c2017-03-15 05:53:14 -0700542 release();
perkj9576e542015-11-12 06:43:16 -0800543 return false;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000544 }
sakalb6760f92016-09-29 04:12:44 -0700545 mediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
perkj9576e542015-11-12 06:43:16 -0800546
perkj30e91822015-11-20 01:31:25 -0800547 if (useSurface) {
perkj48477c12015-12-18 00:34:37 -0800548 eglBase = new EglBase14(sharedContext, EglBase.CONFIG_RECORDABLE);
perkj30e91822015-11-20 01:31:25 -0800549 // Create an input surface and keep a reference since we must release the surface when done.
550 inputSurface = mediaCodec.createInputSurface();
551 eglBase.createSurface(inputSurface);
552 drawer = new GlRectDrawer();
553 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000554 mediaCodec.start();
555 outputBuffers = mediaCodec.getOutputBuffers();
perkj9576e542015-11-12 06:43:16 -0800556 Logging.d(TAG, "Output buffers: " + outputBuffers.length);
557
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000558 } catch (IllegalStateException e) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700559 Logging.e(TAG, "initEncode failed", e);
sakal996a83c2017-03-15 05:53:14 -0700560 release();
perkj9576e542015-11-12 06:43:16 -0800561 return false;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000562 }
perkj9576e542015-11-12 06:43:16 -0800563 return true;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000564 }
565
Magnus Jedvert655e1962017-12-08 11:05:22 +0100566 @CalledByNativeUnchecked
sakalb6760f92016-09-29 04:12:44 -0700567 ByteBuffer[] getInputBuffers() {
perkj9576e542015-11-12 06:43:16 -0800568 ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
569 Logging.d(TAG, "Input buffers: " + inputBuffers.length);
570 return inputBuffers;
571 }
572
Alex Glaznevc7483a72017-01-05 15:22:24 -0800573 void checkKeyFrameRequired(boolean requestedKeyFrame, long presentationTimestampUs) {
574 long presentationTimestampMs = (presentationTimestampUs + 500) / 1000;
575 if (lastKeyFrameMs < 0) {
576 lastKeyFrameMs = presentationTimestampMs;
577 }
578 boolean forcedKeyFrame = false;
579 if (!requestedKeyFrame && forcedKeyFrameMs > 0
580 && presentationTimestampMs > lastKeyFrameMs + forcedKeyFrameMs) {
581 forcedKeyFrame = true;
582 }
583 if (requestedKeyFrame || forcedKeyFrame) {
584 // Ideally MediaCodec would honor BUFFER_FLAG_SYNC_FRAME so we could
585 // indicate this in queueInputBuffer() below and guarantee _this_ frame
586 // be encoded as a key frame, but sadly that flag is ignored. Instead,
587 // we request a key frame "soon".
588 if (requestedKeyFrame) {
589 Logging.d(TAG, "Sync frame request");
590 } else {
591 Logging.d(TAG, "Sync frame forced");
592 }
593 Bundle b = new Bundle();
594 b.putInt(MediaCodec.PARAMETER_KEY_REQUEST_SYNC_FRAME, 0);
595 mediaCodec.setParameters(b);
596 lastKeyFrameMs = presentationTimestampMs;
597 }
598 }
599
Magnus Jedvert655e1962017-12-08 11:05:22 +0100600 @CalledByNativeUnchecked
perkj9576e542015-11-12 06:43:16 -0800601 boolean encodeBuffer(
sakalb6760f92016-09-29 04:12:44 -0700602 boolean isKeyframe, int inputBuffer, int size, long presentationTimestampUs) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000603 checkOnMediaCodecThread();
604 try {
Alex Glaznevc7483a72017-01-05 15:22:24 -0800605 checkKeyFrameRequired(isKeyframe, presentationTimestampUs);
sakalb6760f92016-09-29 04:12:44 -0700606 mediaCodec.queueInputBuffer(inputBuffer, 0, size, presentationTimestampUs, 0);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000607 return true;
sakalb6760f92016-09-29 04:12:44 -0700608 } catch (IllegalStateException e) {
perkj9576e542015-11-12 06:43:16 -0800609 Logging.e(TAG, "encodeBuffer failed", e);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000610 return false;
611 }
612 }
613
Magnus Jedvert655e1962017-12-08 11:05:22 +0100614 @CalledByNativeUnchecked
perkj30e91822015-11-20 01:31:25 -0800615 boolean encodeTexture(boolean isKeyframe, int oesTextureId, float[] transformationMatrix,
616 long presentationTimestampUs) {
617 checkOnMediaCodecThread();
618 try {
Alex Glaznevc7483a72017-01-05 15:22:24 -0800619 checkKeyFrameRequired(isKeyframe, presentationTimestampUs);
perkj30e91822015-11-20 01:31:25 -0800620 eglBase.makeCurrent();
perkj226a6022015-12-02 02:24:40 -0800621 // TODO(perkj): glClear() shouldn't be necessary since every pixel is covered anyway,
622 // but it's a workaround for bug webrtc:5147.
623 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
ivoc1aa435c2016-05-04 07:14:14 -0700624 drawer.drawOes(oesTextureId, transformationMatrix, width, height, 0, 0, width, height);
perkj48477c12015-12-18 00:34:37 -0800625 eglBase.swapBuffers(TimeUnit.MICROSECONDS.toNanos(presentationTimestampUs));
perkj30e91822015-11-20 01:31:25 -0800626 return true;
sakalb6760f92016-09-29 04:12:44 -0700627 } catch (RuntimeException e) {
perkj30e91822015-11-20 01:31:25 -0800628 Logging.e(TAG, "encodeTexture failed", e);
629 return false;
630 }
631 }
632
sakalb5f5bdc2017-08-10 04:15:42 -0700633 /**
Magnus Jedvert655e1962017-12-08 11:05:22 +0100634 * Encodes a new style VideoFrame. |bufferIndex| is -1 if we are not encoding in surface mode.
sakalb5f5bdc2017-08-10 04:15:42 -0700635 */
Magnus Jedvert655e1962017-12-08 11:05:22 +0100636 @CalledByNativeUnchecked
Sami Kalliomäkidebbc782018-02-02 10:01:46 +0100637 boolean encodeFrame(long nativeEncoder, boolean isKeyframe, VideoFrame frame, int bufferIndex,
638 long presentationTimestampUs) {
sakalb5f5bdc2017-08-10 04:15:42 -0700639 checkOnMediaCodecThread();
640 try {
sakalb5f5bdc2017-08-10 04:15:42 -0700641 checkKeyFrameRequired(isKeyframe, presentationTimestampUs);
642
643 VideoFrame.Buffer buffer = frame.getBuffer();
644 if (buffer instanceof VideoFrame.TextureBuffer) {
645 VideoFrame.TextureBuffer textureBuffer = (VideoFrame.TextureBuffer) buffer;
646 eglBase.makeCurrent();
647 // TODO(perkj): glClear() shouldn't be necessary since every pixel is covered anyway,
648 // but it's a workaround for bug webrtc:5147.
649 GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
magjed7cede372017-09-11 06:12:07 -0700650 VideoFrameDrawer.drawTexture(drawer, textureBuffer, new Matrix() /* renderMatrix */, width,
sakal9bc599f2017-09-08 04:46:33 -0700651 height, 0 /* viewportX */, 0 /* viewportY */, width, height);
Sami Kalliomäkidebbc782018-02-02 10:01:46 +0100652 eglBase.swapBuffers(TimeUnit.MICROSECONDS.toNanos(presentationTimestampUs));
sakalb5f5bdc2017-08-10 04:15:42 -0700653 } else {
654 VideoFrame.I420Buffer i420Buffer = buffer.toI420();
Sami Kalliomäkie3044fe2017-10-02 09:41:55 +0200655 final int chromaHeight = (height + 1) / 2;
656 final ByteBuffer dataY = i420Buffer.getDataY();
657 final ByteBuffer dataU = i420Buffer.getDataU();
658 final ByteBuffer dataV = i420Buffer.getDataV();
659 final int strideY = i420Buffer.getStrideY();
660 final int strideU = i420Buffer.getStrideU();
661 final int strideV = i420Buffer.getStrideV();
662 if (dataY.capacity() < strideY * height) {
663 throw new RuntimeException("Y-plane buffer size too small.");
664 }
665 if (dataU.capacity() < strideU * chromaHeight) {
666 throw new RuntimeException("U-plane buffer size too small.");
667 }
668 if (dataV.capacity() < strideV * chromaHeight) {
669 throw new RuntimeException("V-plane buffer size too small.");
670 }
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100671 nativeFillInputBuffer(
Sami Kalliomäkie3044fe2017-10-02 09:41:55 +0200672 nativeEncoder, bufferIndex, dataY, strideY, dataU, strideU, dataV, strideV);
sakalb5f5bdc2017-08-10 04:15:42 -0700673 i420Buffer.release();
674 // I420 consists of one full-resolution and two half-resolution planes.
675 // 1 + 1 / 4 + 1 / 4 = 3 / 2
676 int yuvSize = width * height * 3 / 2;
677 mediaCodec.queueInputBuffer(bufferIndex, 0, yuvSize, presentationTimestampUs, 0);
678 }
679 return true;
680 } catch (RuntimeException e) {
681 Logging.e(TAG, "encodeFrame failed", e);
682 return false;
683 }
684 }
685
Magnus Jedvert655e1962017-12-08 11:05:22 +0100686 @CalledByNativeUnchecked
perkj9576e542015-11-12 06:43:16 -0800687 void release() {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700688 Logging.d(TAG, "Java releaseEncoder");
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000689 checkOnMediaCodecThread();
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700690
sakal996a83c2017-03-15 05:53:14 -0700691 class CaughtException {
692 Exception e;
693 }
694 final CaughtException caughtException = new CaughtException();
695 boolean stopHung = false;
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700696
sakal996a83c2017-03-15 05:53:14 -0700697 if (mediaCodec != null) {
698 // Run Mediacodec stop() and release() on separate thread since sometime
699 // Mediacodec.stop() may hang.
700 final CountDownLatch releaseDone = new CountDownLatch(1);
701
702 Runnable runMediaCodecRelease = new Runnable() {
703 @Override
704 public void run() {
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700705 Logging.d(TAG, "Java releaseEncoder on release thread");
sakal996a83c2017-03-15 05:53:14 -0700706 try {
707 mediaCodec.stop();
708 } catch (Exception e) {
709 Logging.e(TAG, "Media encoder stop failed", e);
710 }
711 try {
712 mediaCodec.release();
713 } catch (Exception e) {
714 Logging.e(TAG, "Media encoder release failed", e);
715 caughtException.e = e;
716 }
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700717 Logging.d(TAG, "Java releaseEncoder on release thread done");
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700718
sakal996a83c2017-03-15 05:53:14 -0700719 releaseDone.countDown();
720 }
721 };
722 new Thread(runMediaCodecRelease).start();
723
724 if (!ThreadUtils.awaitUninterruptibly(releaseDone, MEDIA_CODEC_RELEASE_TIMEOUT_MS)) {
725 Logging.e(TAG, "Media encoder release timeout");
726 stopHung = true;
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700727 }
sakal996a83c2017-03-15 05:53:14 -0700728
729 mediaCodec = null;
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000730 }
Alex Glaznev5c3da4b2015-10-30 15:31:07 -0700731
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000732 mediaCodecThread = null;
perkj30e91822015-11-20 01:31:25 -0800733 if (drawer != null) {
734 drawer.release();
735 drawer = null;
736 }
737 if (eglBase != null) {
738 eglBase.release();
739 eglBase = null;
740 }
741 if (inputSurface != null) {
742 inputSurface.release();
743 inputSurface = null;
744 }
Alex Glaznevc6aec4b2015-10-19 16:39:19 -0700745 runningInstance = null;
sakal996a83c2017-03-15 05:53:14 -0700746
747 if (stopHung) {
748 codecErrors++;
749 if (errorCallback != null) {
750 Logging.e(TAG, "Invoke codec error callback. Errors: " + codecErrors);
751 errorCallback.onMediaCodecVideoEncoderCriticalError(codecErrors);
752 }
753 throw new RuntimeException("Media encoder release timeout.");
754 }
755
756 // Re-throw any runtime exception caught inside the other thread. Since this is an invoke, add
757 // stack trace for the waiting thread as well.
758 if (caughtException.e != null) {
759 final RuntimeException runtimeException = new RuntimeException(caughtException.e);
760 runtimeException.setStackTrace(ThreadUtils.concatStackTraces(
761 caughtException.e.getStackTrace(), runtimeException.getStackTrace()));
762 throw runtimeException;
763 }
764
Alex Glaznev325d4142015-10-12 14:56:02 -0700765 Logging.d(TAG, "Java releaseEncoder done");
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000766 }
767
Magnus Jedvert655e1962017-12-08 11:05:22 +0100768 @CalledByNativeUnchecked
Alex Glaznev269fe752016-05-25 16:17:33 -0700769 private boolean setRates(int kbps, int frameRate) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000770 checkOnMediaCodecThread();
Alex Glaznevcfaca032016-09-06 14:08:19 -0700771
772 int codecBitrateBps = 1000 * kbps;
773 if (bitrateAdjustmentType == BitrateAdjustmentType.DYNAMIC_ADJUSTMENT) {
774 bitrateAccumulatorMax = codecBitrateBps / 8.0;
775 if (targetBitrateBps > 0 && codecBitrateBps < targetBitrateBps) {
776 // Rescale the accumulator level if the accumulator max decreases
777 bitrateAccumulator = bitrateAccumulator * codecBitrateBps / targetBitrateBps;
778 }
Alex Glaznev269fe752016-05-25 16:17:33 -0700779 }
Alex Glaznevcfaca032016-09-06 14:08:19 -0700780 targetBitrateBps = codecBitrateBps;
781 targetFps = frameRate;
782
783 // Adjust actual encoder bitrate based on bitrate adjustment type.
784 if (bitrateAdjustmentType == BitrateAdjustmentType.FRAMERATE_ADJUSTMENT && targetFps > 0) {
785 codecBitrateBps = BITRATE_ADJUSTMENT_FPS * targetBitrateBps / targetFps;
sakalb6760f92016-09-29 04:12:44 -0700786 Logging.v(TAG,
787 "setRates: " + kbps + " -> " + (codecBitrateBps / 1000) + " kbps. Fps: " + targetFps);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700788 } else if (bitrateAdjustmentType == BitrateAdjustmentType.DYNAMIC_ADJUSTMENT) {
sakalb6760f92016-09-29 04:12:44 -0700789 Logging.v(TAG, "setRates: " + kbps + " kbps. Fps: " + targetFps + ". ExpScale: "
790 + bitrateAdjustmentScaleExp);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700791 if (bitrateAdjustmentScaleExp != 0) {
sakalb6760f92016-09-29 04:12:44 -0700792 codecBitrateBps = (int) (codecBitrateBps * getBitrateScale(bitrateAdjustmentScaleExp));
Alex Glaznevcfaca032016-09-06 14:08:19 -0700793 }
794 } else {
795 Logging.v(TAG, "setRates: " + kbps + " kbps. Fps: " + targetFps);
796 }
797
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000798 try {
799 Bundle params = new Bundle();
Alex Glaznevcfaca032016-09-06 14:08:19 -0700800 params.putInt(MediaCodec.PARAMETER_KEY_VIDEO_BITRATE, codecBitrateBps);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000801 mediaCodec.setParameters(params);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000802 return true;
803 } catch (IllegalStateException e) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700804 Logging.e(TAG, "setRates failed", e);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000805 return false;
806 }
807 }
808
809 // Dequeue an input buffer and return its index, -1 if no input buffer is
810 // available, or -2 if the codec is no longer operative.
Magnus Jedvert655e1962017-12-08 11:05:22 +0100811 @CalledByNativeUnchecked
perkj9576e542015-11-12 06:43:16 -0800812 int dequeueInputBuffer() {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000813 checkOnMediaCodecThread();
814 try {
815 return mediaCodec.dequeueInputBuffer(DEQUEUE_TIMEOUT);
816 } catch (IllegalStateException e) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700817 Logging.e(TAG, "dequeueIntputBuffer failed", e);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000818 return -2;
819 }
820 }
821
822 // Helper struct for dequeueOutputBuffer() below.
perkj9576e542015-11-12 06:43:16 -0800823 static class OutputBufferInfo {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000824 public OutputBufferInfo(
sakalb6760f92016-09-29 04:12:44 -0700825 int index, ByteBuffer buffer, boolean isKeyFrame, long presentationTimestampUs) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000826 this.index = index;
827 this.buffer = buffer;
828 this.isKeyFrame = isKeyFrame;
829 this.presentationTimestampUs = presentationTimestampUs;
830 }
831
perkj9576e542015-11-12 06:43:16 -0800832 public final int index;
833 public final ByteBuffer buffer;
834 public final boolean isKeyFrame;
835 public final long presentationTimestampUs;
Magnus Jedvert655e1962017-12-08 11:05:22 +0100836
837 @CalledByNative("OutputBufferInfo")
838 int getIndex() {
839 return index;
840 }
841
842 @CalledByNative("OutputBufferInfo")
843 ByteBuffer getBuffer() {
844 return buffer;
845 }
846
847 @CalledByNative("OutputBufferInfo")
848 boolean isKeyFrame() {
849 return isKeyFrame;
850 }
851
852 @CalledByNative("OutputBufferInfo")
853 long getPresentationTimestampUs() {
854 return presentationTimestampUs;
855 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000856 }
857
858 // Dequeue and return an output buffer, or null if no output is ready. Return
859 // a fake OutputBufferInfo with index -1 if the codec is no longer operable.
Magnus Jedvert655e1962017-12-08 11:05:22 +0100860 @CalledByNativeUnchecked
perkj9576e542015-11-12 06:43:16 -0800861 OutputBufferInfo dequeueOutputBuffer() {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000862 checkOnMediaCodecThread();
863 try {
864 MediaCodec.BufferInfo info = new MediaCodec.BufferInfo();
865 int result = mediaCodec.dequeueOutputBuffer(info, DEQUEUE_TIMEOUT);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000866 // Check if this is config frame and save configuration data.
867 if (result >= 0) {
sakalb6760f92016-09-29 04:12:44 -0700868 boolean isConfigFrame = (info.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) != 0;
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000869 if (isConfigFrame) {
sakalb6760f92016-09-29 04:12:44 -0700870 Logging.d(TAG, "Config frame generated. Offset: " + info.offset + ". Size: " + info.size);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000871 configData = ByteBuffer.allocateDirect(info.size);
872 outputBuffers[result].position(info.offset);
873 outputBuffers[result].limit(info.offset + info.size);
874 configData.put(outputBuffers[result]);
glaznev3fc23502017-06-15 16:24:37 -0700875 // Log few SPS header bytes to check profile and level.
876 String spsData = "";
877 for (int i = 0; i < (info.size < 8 ? info.size : 8); i++) {
878 spsData += Integer.toHexString(configData.get(i) & 0xff) + " ";
879 }
880 Logging.d(TAG, spsData);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000881 // Release buffer back.
882 mediaCodec.releaseOutputBuffer(result, false);
883 // Query next output.
884 result = mediaCodec.dequeueOutputBuffer(info, DEQUEUE_TIMEOUT);
885 }
886 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000887 if (result >= 0) {
888 // MediaCodec doesn't care about Buffer position/remaining/etc so we can
889 // mess with them to get a slice and avoid having to pass extra
890 // (BufferInfo-related) parameters back to C++.
891 ByteBuffer outputBuffer = outputBuffers[result].duplicate();
892 outputBuffer.position(info.offset);
893 outputBuffer.limit(info.offset + info.size);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700894 reportEncodedFrame(info.size);
895
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000896 // Check key frame flag.
sakalb6760f92016-09-29 04:12:44 -0700897 boolean isKeyFrame = (info.flags & MediaCodec.BUFFER_FLAG_SYNC_FRAME) != 0;
glaznev@webrtc.orgc6c1dfd2014-06-13 22:59:08 +0000898 if (isKeyFrame) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700899 Logging.d(TAG, "Sync frame generated");
glaznev@webrtc.orgc6c1dfd2014-06-13 22:59:08 +0000900 }
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000901 if (isKeyFrame && type == VideoCodecType.VIDEO_CODEC_H264) {
sakalb6760f92016-09-29 04:12:44 -0700902 Logging.d(TAG, "Appending config frame of size " + configData.capacity()
903 + " to output buffer with offset " + info.offset + ", size " + info.size);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000904 // For H.264 key frame append SPS and PPS NALs at the start
sakalb6760f92016-09-29 04:12:44 -0700905 ByteBuffer keyFrameBuffer = ByteBuffer.allocateDirect(configData.capacity() + info.size);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000906 configData.rewind();
907 keyFrameBuffer.put(configData);
908 keyFrameBuffer.put(outputBuffer);
909 keyFrameBuffer.position(0);
sakalb6760f92016-09-29 04:12:44 -0700910 return new OutputBufferInfo(result, keyFrameBuffer, isKeyFrame, info.presentationTimeUs);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000911 } else {
sakalb6760f92016-09-29 04:12:44 -0700912 return new OutputBufferInfo(
913 result, outputBuffer.slice(), isKeyFrame, info.presentationTimeUs);
glaznev@webrtc.orgb28474c2015-02-23 17:44:27 +0000914 }
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000915 } else if (result == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
916 outputBuffers = mediaCodec.getOutputBuffers();
917 return dequeueOutputBuffer();
918 } else if (result == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
919 return dequeueOutputBuffer();
920 } else if (result == MediaCodec.INFO_TRY_AGAIN_LATER) {
921 return null;
922 }
923 throw new RuntimeException("dequeueOutputBuffer: " + result);
924 } catch (IllegalStateException e) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700925 Logging.e(TAG, "dequeueOutputBuffer failed", e);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000926 return new OutputBufferInfo(-1, null, false, -1);
927 }
928 }
929
Alex Glaznevcfaca032016-09-06 14:08:19 -0700930 private double getBitrateScale(int bitrateAdjustmentScaleExp) {
931 return Math.pow(BITRATE_CORRECTION_MAX_SCALE,
sakalb6760f92016-09-29 04:12:44 -0700932 (double) bitrateAdjustmentScaleExp / BITRATE_CORRECTION_STEPS);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700933 }
934
935 private void reportEncodedFrame(int size) {
936 if (targetFps == 0 || bitrateAdjustmentType != BitrateAdjustmentType.DYNAMIC_ADJUSTMENT) {
937 return;
938 }
939
940 // Accumulate the difference between actial and expected frame sizes.
941 double expectedBytesPerFrame = targetBitrateBps / (8.0 * targetFps);
942 bitrateAccumulator += (size - expectedBytesPerFrame);
943 bitrateObservationTimeMs += 1000.0 / targetFps;
944
945 // Put a cap on the accumulator, i.e., don't let it grow beyond some level to avoid
946 // using too old data for bitrate adjustment.
947 double bitrateAccumulatorCap = BITRATE_CORRECTION_SEC * bitrateAccumulatorMax;
948 bitrateAccumulator = Math.min(bitrateAccumulator, bitrateAccumulatorCap);
949 bitrateAccumulator = Math.max(bitrateAccumulator, -bitrateAccumulatorCap);
950
951 // Do bitrate adjustment every 3 seconds if actual encoder bitrate deviates too much
952 // form the target value.
953 if (bitrateObservationTimeMs > 1000 * BITRATE_CORRECTION_SEC) {
sakalb6760f92016-09-29 04:12:44 -0700954 Logging.d(TAG, "Acc: " + (int) bitrateAccumulator + ". Max: " + (int) bitrateAccumulatorMax
955 + ". ExpScale: " + bitrateAdjustmentScaleExp);
Alex Glaznevcfaca032016-09-06 14:08:19 -0700956 boolean bitrateAdjustmentScaleChanged = false;
957 if (bitrateAccumulator > bitrateAccumulatorMax) {
958 // Encoder generates too high bitrate - need to reduce the scale.
Alex Glaznev7fa4a722017-01-17 15:32:02 -0800959 int bitrateAdjustmentInc = (int) (bitrateAccumulator / bitrateAccumulatorMax + 0.5);
960 bitrateAdjustmentScaleExp -= bitrateAdjustmentInc;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700961 bitrateAccumulator = bitrateAccumulatorMax;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700962 bitrateAdjustmentScaleChanged = true;
963 } else if (bitrateAccumulator < -bitrateAccumulatorMax) {
964 // Encoder generates too low bitrate - need to increase the scale.
Alex Glaznev7fa4a722017-01-17 15:32:02 -0800965 int bitrateAdjustmentInc = (int) (-bitrateAccumulator / bitrateAccumulatorMax + 0.5);
966 bitrateAdjustmentScaleExp += bitrateAdjustmentInc;
Alex Glaznevcfaca032016-09-06 14:08:19 -0700967 bitrateAccumulator = -bitrateAccumulatorMax;
968 bitrateAdjustmentScaleChanged = true;
969 }
970 if (bitrateAdjustmentScaleChanged) {
971 bitrateAdjustmentScaleExp = Math.min(bitrateAdjustmentScaleExp, BITRATE_CORRECTION_STEPS);
972 bitrateAdjustmentScaleExp = Math.max(bitrateAdjustmentScaleExp, -BITRATE_CORRECTION_STEPS);
sakalb6760f92016-09-29 04:12:44 -0700973 Logging.d(TAG, "Adjusting bitrate scale to " + bitrateAdjustmentScaleExp + ". Value: "
974 + getBitrateScale(bitrateAdjustmentScaleExp));
Alex Glaznevcfaca032016-09-06 14:08:19 -0700975 setRates(targetBitrateBps / 1000, targetFps);
976 }
977 bitrateObservationTimeMs = 0;
978 }
979 }
980
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000981 // Release a dequeued output buffer back to the codec for re-use. Return
982 // false if the codec is no longer operable.
Magnus Jedvert655e1962017-12-08 11:05:22 +0100983 @CalledByNativeUnchecked
perkj9576e542015-11-12 06:43:16 -0800984 boolean releaseOutputBuffer(int index) {
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000985 checkOnMediaCodecThread();
986 try {
987 mediaCodec.releaseOutputBuffer(index, false);
988 return true;
989 } catch (IllegalStateException e) {
Jiayang Liu5975b3c2015-09-16 13:40:53 -0700990 Logging.e(TAG, "releaseOutputBuffer failed", e);
fischman@webrtc.org540acde2014-02-13 03:56:14 +0000991 return false;
992 }
993 }
sakalb5f5bdc2017-08-10 04:15:42 -0700994
Magnus Jedvert655e1962017-12-08 11:05:22 +0100995 @CalledByNative
996 int getColorFormat() {
997 return colorFormat;
998 }
999
1000 @CalledByNative
1001 static boolean isTextureBuffer(VideoFrame.Buffer buffer) {
1002 return buffer instanceof VideoFrame.TextureBuffer;
1003 }
1004
sakalb5f5bdc2017-08-10 04:15:42 -07001005 /** Fills an inputBuffer with the given index with data from the byte buffers. */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +01001006 private static native void nativeFillInputBuffer(long encoder, int inputBuffer, ByteBuffer dataY,
1007 int strideY, ByteBuffer dataU, int strideU, ByteBuffer dataV, int strideV);
fischman@webrtc.org540acde2014-02-13 03:56:14 +00001008}