FFmpeg
mediacodec_wrapper.c
Go to the documentation of this file.
1 /*
2  * Android MediaCodec Wrapper
3  *
4  * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <dlfcn.h>
24 #include <jni.h>
25 #include <stdbool.h>
26 #include <media/NdkMediaFormat.h>
27 #include <media/NdkMediaCodec.h>
28 #include <android/native_window_jni.h>
29 
30 #include "libavutil/avassert.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/avstring.h"
33 
34 #include "avcodec.h"
35 #include "ffjni.h"
36 #include "mediacodec_wrapper.h"
37 
39 
41  jmethodID init_id;
43 
44  jmethodID get_codec_count_id;
46 
48  jmethodID get_name_id;
51  jmethodID is_encoder_id;
53 
55  jfieldID color_formats_id;
57 
59  jfieldID profile_id;
60  jfieldID level_id;
61 };
62 
63 #define OFFSET(x) offsetof(struct JNIAMediaCodecListFields, x)
64 static const struct FFJniField jni_amediacodeclist_mapping[] = {
65  { "android/media/MediaCodecList", NULL, NULL, FF_JNI_CLASS, OFFSET(mediacodec_list_class), 1 },
66  { "android/media/MediaCodecList", "<init>", "(I)V", FF_JNI_METHOD, OFFSET(init_id), 0 },
67  { "android/media/MediaCodecList", "findDecoderForFormat", "(Landroid/media/MediaFormat;)Ljava/lang/String;", FF_JNI_METHOD, OFFSET(find_decoder_for_format_id), 0 },
68 
69  { "android/media/MediaCodecList", "getCodecCount", "()I", FF_JNI_STATIC_METHOD, OFFSET(get_codec_count_id), 1 },
70  { "android/media/MediaCodecList", "getCodecInfoAt", "(I)Landroid/media/MediaCodecInfo;", FF_JNI_STATIC_METHOD, OFFSET(get_codec_info_at_id), 1 },
71 
72  { "android/media/MediaCodecInfo", NULL, NULL, FF_JNI_CLASS, OFFSET(mediacodec_info_class), 1 },
73  { "android/media/MediaCodecInfo", "getName", "()Ljava/lang/String;", FF_JNI_METHOD, OFFSET(get_name_id), 1 },
74  { "android/media/MediaCodecInfo", "getCapabilitiesForType", "(Ljava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;", FF_JNI_METHOD, OFFSET(get_codec_capabilities_id), 1 },
75  { "android/media/MediaCodecInfo", "getSupportedTypes", "()[Ljava/lang/String;", FF_JNI_METHOD, OFFSET(get_supported_types_id), 1 },
76  { "android/media/MediaCodecInfo", "isEncoder", "()Z", FF_JNI_METHOD, OFFSET(is_encoder_id), 1 },
77  { "android/media/MediaCodecInfo", "isSoftwareOnly", "()Z", FF_JNI_METHOD, OFFSET(is_software_only_id), 0 },
78 
79  { "android/media/MediaCodecInfo$CodecCapabilities", NULL, NULL, FF_JNI_CLASS, OFFSET(codec_capabilities_class), 1 },
80  { "android/media/MediaCodecInfo$CodecCapabilities", "colorFormats", "[I", FF_JNI_FIELD, OFFSET(color_formats_id), 1 },
81  { "android/media/MediaCodecInfo$CodecCapabilities", "profileLevels", "[Landroid/media/MediaCodecInfo$CodecProfileLevel;", FF_JNI_FIELD, OFFSET(profile_levels_id), 1 },
82 
83  { "android/media/MediaCodecInfo$CodecProfileLevel", NULL, NULL, FF_JNI_CLASS, OFFSET(codec_profile_level_class), 1 },
84  { "android/media/MediaCodecInfo$CodecProfileLevel", "profile", "I", FF_JNI_FIELD, OFFSET(profile_id), 1 },
85  { "android/media/MediaCodecInfo$CodecProfileLevel", "level", "I", FF_JNI_FIELD, OFFSET(level_id), 1 },
86 
87  { NULL }
88 };
89 #undef OFFSET
90 
92 
94 
95  jmethodID init_id;
96 
97  jmethodID contains_key_id;
98 
99  jmethodID get_integer_id;
100  jmethodID get_long_id;
101  jmethodID get_float_id;
102  jmethodID get_bytebuffer_id;
103  jmethodID get_string_id;
104 
105  jmethodID set_integer_id;
106  jmethodID set_long_id;
107  jmethodID set_float_id;
108  jmethodID set_bytebuffer_id;
109  jmethodID set_string_id;
110 
111  jmethodID to_string_id;
112 
113 };
114 
115 #define OFFSET(x) offsetof(struct JNIAMediaFormatFields, x)
116 static const struct FFJniField jni_amediaformat_mapping[] = {
117  { "android/media/MediaFormat", NULL, NULL, FF_JNI_CLASS, OFFSET(mediaformat_class), 1 },
118 
119  { "android/media/MediaFormat", "<init>", "()V", FF_JNI_METHOD, OFFSET(init_id), 1 },
120 
121  { "android/media/MediaFormat", "containsKey", "(Ljava/lang/String;)Z", FF_JNI_METHOD, OFFSET(contains_key_id), 1 },
122 
123  { "android/media/MediaFormat", "getInteger", "(Ljava/lang/String;)I", FF_JNI_METHOD, OFFSET(get_integer_id), 1 },
124  { "android/media/MediaFormat", "getLong", "(Ljava/lang/String;)J", FF_JNI_METHOD, OFFSET(get_long_id), 1 },
125  { "android/media/MediaFormat", "getFloat", "(Ljava/lang/String;)F", FF_JNI_METHOD, OFFSET(get_float_id), 1 },
126  { "android/media/MediaFormat", "getByteBuffer", "(Ljava/lang/String;)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, OFFSET(get_bytebuffer_id), 1 },
127  { "android/media/MediaFormat", "getString", "(Ljava/lang/String;)Ljava/lang/String;", FF_JNI_METHOD, OFFSET(get_string_id), 1 },
128 
129  { "android/media/MediaFormat", "setInteger", "(Ljava/lang/String;I)V", FF_JNI_METHOD, OFFSET(set_integer_id), 1 },
130  { "android/media/MediaFormat", "setLong", "(Ljava/lang/String;J)V", FF_JNI_METHOD, OFFSET(set_long_id), 1 },
131  { "android/media/MediaFormat", "setFloat", "(Ljava/lang/String;F)V", FF_JNI_METHOD, OFFSET(set_float_id), 1 },
132  { "android/media/MediaFormat", "setByteBuffer", "(Ljava/lang/String;Ljava/nio/ByteBuffer;)V", FF_JNI_METHOD, OFFSET(set_bytebuffer_id), 1 },
133  { "android/media/MediaFormat", "setString", "(Ljava/lang/String;Ljava/lang/String;)V", FF_JNI_METHOD, OFFSET(set_string_id), 1 },
134 
135  { "android/media/MediaFormat", "toString", "()Ljava/lang/String;", FF_JNI_METHOD, OFFSET(to_string_id), 1 },
136 
137  { NULL }
138 };
139 #undef OFFSET
140 
141 static const AVClass amediaformat_class = {
142  .class_name = "amediaformat",
143  .item_name = av_default_item_name,
144  .version = LIBAVUTIL_VERSION_INT,
145 };
146 
147 typedef struct FFAMediaFormatJni {
149 
151  jobject object;
153 
155 
157 
159 
163 
167 
169 
173 
174  jmethodID get_name_id;
175 
176  jmethodID configure_id;
177  jmethodID start_id;
178  jmethodID flush_id;
179  jmethodID stop_id;
180  jmethodID release_id;
181 
183 
188 
194 
197 
199 
200  jmethodID init_id;
201 
202  jfieldID flags_id;
203  jfieldID offset_id;
205  jfieldID size_id;
206 
207 };
208 
209 #define OFFSET(x) offsetof(struct JNIAMediaCodecFields, x)
210 static const struct FFJniField jni_amediacodec_mapping[] = {
211  { "android/media/MediaCodec", NULL, NULL, FF_JNI_CLASS, OFFSET(mediacodec_class), 1 },
212 
213  { "android/media/MediaCodec", "INFO_TRY_AGAIN_LATER", "I", FF_JNI_STATIC_FIELD, OFFSET(info_try_again_later_id), 1 },
214  { "android/media/MediaCodec", "INFO_OUTPUT_BUFFERS_CHANGED", "I", FF_JNI_STATIC_FIELD, OFFSET(info_output_buffers_changed_id), 1 },
215  { "android/media/MediaCodec", "INFO_OUTPUT_FORMAT_CHANGED", "I", FF_JNI_STATIC_FIELD, OFFSET(info_output_format_changed_id), 1 },
216 
217  { "android/media/MediaCodec", "BUFFER_FLAG_CODEC_CONFIG", "I", FF_JNI_STATIC_FIELD, OFFSET(buffer_flag_codec_config_id), 1 },
218  { "android/media/MediaCodec", "BUFFER_FLAG_END_OF_STREAM", "I", FF_JNI_STATIC_FIELD, OFFSET(buffer_flag_end_of_stream_id), 1 },
219  { "android/media/MediaCodec", "BUFFER_FLAG_KEY_FRAME", "I", FF_JNI_STATIC_FIELD, OFFSET(buffer_flag_key_frame_id), 0 },
220 
221  { "android/media/MediaCodec", "CONFIGURE_FLAG_ENCODE", "I", FF_JNI_STATIC_FIELD, OFFSET(configure_flag_encode_id), 1 },
222 
223  { "android/media/MediaCodec", "createByCodecName", "(Ljava/lang/String;)Landroid/media/MediaCodec;", FF_JNI_STATIC_METHOD, OFFSET(create_by_codec_name_id), 1 },
224  { "android/media/MediaCodec", "createDecoderByType", "(Ljava/lang/String;)Landroid/media/MediaCodec;", FF_JNI_STATIC_METHOD, OFFSET(create_decoder_by_type_id), 1 },
225  { "android/media/MediaCodec", "createEncoderByType", "(Ljava/lang/String;)Landroid/media/MediaCodec;", FF_JNI_STATIC_METHOD, OFFSET(create_encoder_by_type_id), 1 },
226 
227  { "android/media/MediaCodec", "getName", "()Ljava/lang/String;", FF_JNI_METHOD, OFFSET(get_name_id), 1 },
228 
229  { "android/media/MediaCodec", "configure", "(Landroid/media/MediaFormat;Landroid/view/Surface;Landroid/media/MediaCrypto;I)V", FF_JNI_METHOD, OFFSET(configure_id), 1 },
230  { "android/media/MediaCodec", "start", "()V", FF_JNI_METHOD, OFFSET(start_id), 1 },
231  { "android/media/MediaCodec", "flush", "()V", FF_JNI_METHOD, OFFSET(flush_id), 1 },
232  { "android/media/MediaCodec", "stop", "()V", FF_JNI_METHOD, OFFSET(stop_id), 1 },
233  { "android/media/MediaCodec", "release", "()V", FF_JNI_METHOD, OFFSET(release_id), 1 },
234 
235  { "android/media/MediaCodec", "getOutputFormat", "()Landroid/media/MediaFormat;", FF_JNI_METHOD, OFFSET(get_output_format_id), 1 },
236 
237  { "android/media/MediaCodec", "dequeueInputBuffer", "(J)I", FF_JNI_METHOD, OFFSET(dequeue_input_buffer_id), 1 },
238  { "android/media/MediaCodec", "queueInputBuffer", "(IIIJI)V", FF_JNI_METHOD, OFFSET(queue_input_buffer_id), 1 },
239  { "android/media/MediaCodec", "getInputBuffer", "(I)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, OFFSET(get_input_buffer_id), 0 },
240  { "android/media/MediaCodec", "getInputBuffers", "()[Ljava/nio/ByteBuffer;", FF_JNI_METHOD, OFFSET(get_input_buffers_id), 1 },
241 
242  { "android/media/MediaCodec", "dequeueOutputBuffer", "(Landroid/media/MediaCodec$BufferInfo;J)I", FF_JNI_METHOD, OFFSET(dequeue_output_buffer_id), 1 },
243  { "android/media/MediaCodec", "getOutputBuffer", "(I)Ljava/nio/ByteBuffer;", FF_JNI_METHOD, OFFSET(get_output_buffer_id), 0 },
244  { "android/media/MediaCodec", "getOutputBuffers", "()[Ljava/nio/ByteBuffer;", FF_JNI_METHOD, OFFSET(get_output_buffers_id), 1 },
245  { "android/media/MediaCodec", "releaseOutputBuffer", "(IZ)V", FF_JNI_METHOD, OFFSET(release_output_buffer_id), 1 },
246  { "android/media/MediaCodec", "releaseOutputBuffer", "(IJ)V", FF_JNI_METHOD, OFFSET(release_output_buffer_at_time_id), 0 },
247 
248  { "android/media/MediaCodec", "setInputSurface", "(Landroid/view/Surface;)V", FF_JNI_METHOD, OFFSET(set_input_surface_id), 0 },
249  { "android/media/MediaCodec", "signalEndOfInputStream", "()V", FF_JNI_METHOD, OFFSET(signal_end_of_input_stream_id), 0 },
250 
251  { "android/media/MediaCodec$BufferInfo", NULL, NULL, FF_JNI_CLASS, OFFSET(mediainfo_class), 1 },
252 
253  { "android/media/MediaCodec.BufferInfo", "<init>", "()V", FF_JNI_METHOD, OFFSET(init_id), 1 },
254  { "android/media/MediaCodec.BufferInfo", "flags", "I", FF_JNI_FIELD, OFFSET(flags_id), 1 },
255  { "android/media/MediaCodec.BufferInfo", "offset", "I", FF_JNI_FIELD, OFFSET(offset_id), 1 },
256  { "android/media/MediaCodec.BufferInfo", "presentationTimeUs", "J", FF_JNI_FIELD, OFFSET(presentation_time_us_id), 1 },
257  { "android/media/MediaCodec.BufferInfo", "size", "I", FF_JNI_FIELD, OFFSET(size_id), 1 },
258 
259  { NULL }
260 };
261 #undef OFFSET
262 
263 static const AVClass amediacodec_class = {
264  .class_name = "amediacodec",
265  .item_name = av_default_item_name,
266  .version = LIBAVUTIL_VERSION_INT,
267 };
268 
269 typedef struct FFAMediaCodecJni {
271 
273 
274  jobject object;
275  jobject buffer_info;
276 
277  jobject input_buffers;
278  jobject output_buffers;
279 
283 
287 
289 
292 
294 
295 #define JNI_GET_ENV_OR_RETURN(env, log_ctx, ret) do { \
296  (env) = ff_jni_get_env(log_ctx); \
297  if (!(env)) { \
298  return ret; \
299  } \
300 } while (0)
301 
302 #define JNI_GET_ENV_OR_RETURN_VOID(env, log_ctx) do { \
303  (env) = ff_jni_get_env(log_ctx); \
304  if (!(env)) { \
305  return; \
306  } \
307 } while (0)
308 
310 {
311  // Copy and modified from MediaCodecInfo.java
312  static const int AVCProfileBaseline = 0x01;
313  static const int AVCProfileMain = 0x02;
314  static const int AVCProfileExtended = 0x04;
315  static const int AVCProfileHigh = 0x08;
316  static const int AVCProfileHigh10 = 0x10;
317  static const int AVCProfileHigh422 = 0x20;
318  static const int AVCProfileHigh444 = 0x40;
319  static const int AVCProfileConstrainedBaseline = 0x10000;
320  static const int AVCProfileConstrainedHigh = 0x80000;
321 
322  static const int HEVCProfileMain = 0x01;
323  static const int HEVCProfileMain10 = 0x02;
324  static const int HEVCProfileMainStill = 0x04;
325  static const int HEVCProfileMain10HDR10 = 0x1000;
326  static const int HEVCProfileMain10HDR10Plus = 0x2000;
327 
328  static const int VP9Profile0 = 0x01;
329  static const int VP9Profile1 = 0x02;
330  static const int VP9Profile2 = 0x04;
331  static const int VP9Profile3 = 0x08;
332  static const int VP9Profile2HDR = 0x1000;
333  static const int VP9Profile3HDR = 0x2000;
334  static const int VP9Profile2HDR10Plus = 0x4000;
335  static const int VP9Profile3HDR10Plus = 0x8000;
336 
337  static const int MPEG4ProfileSimple = 0x01;
338  static const int MPEG4ProfileSimpleScalable = 0x02;
339  static const int MPEG4ProfileCore = 0x04;
340  static const int MPEG4ProfileMain = 0x08;
341  static const int MPEG4ProfileNbit = 0x10;
342  static const int MPEG4ProfileScalableTexture = 0x20;
343  static const int MPEG4ProfileSimpleFBA = 0x80;
344  static const int MPEG4ProfileSimpleFace = 0x40;
345  static const int MPEG4ProfileBasicAnimated = 0x100;
346  static const int MPEG4ProfileHybrid = 0x200;
347  static const int MPEG4ProfileAdvancedRealTime = 0x400;
348  static const int MPEG4ProfileCoreScalable = 0x800;
349  static const int MPEG4ProfileAdvancedCoding = 0x1000;
350  static const int MPEG4ProfileAdvancedCore = 0x2000;
351  static const int MPEG4ProfileAdvancedScalable = 0x4000;
352  static const int MPEG4ProfileAdvancedSimple = 0x8000;
353 
354 
355  static const int AV1ProfileMain8 = 0x1;
356  static const int AV1ProfileMain10 = 0x2;
357  static const int AV1ProfileMain10HDR10 = 0x1000;
358  static const int AV1ProfileMain10HDR10Plus = 0x2000;
359 
360  // Unused yet.
361  (void)AVCProfileConstrainedHigh;
362  (void)HEVCProfileMain10HDR10;
363  (void)HEVCProfileMain10HDR10Plus;
364  (void)VP9Profile2HDR;
365  (void)VP9Profile3HDR;
366  (void)VP9Profile2HDR10Plus;
367  (void)VP9Profile3HDR10Plus;
368  (void)MPEG4ProfileSimpleFace;
369  (void)AV1ProfileMain10;
370  (void)AV1ProfileMain10HDR10;
371  (void)AV1ProfileMain10HDR10Plus;
372 
373  if (avctx->codec_id == AV_CODEC_ID_H264) {
374  switch(avctx->profile) {
376  return AVCProfileBaseline;
378  return AVCProfileConstrainedBaseline;
380  return AVCProfileMain;
381  break;
383  return AVCProfileExtended;
385  return AVCProfileHigh;
388  return AVCProfileHigh10;
391  return AVCProfileHigh422;
395  return AVCProfileHigh444;
396  }
397  } else if (avctx->codec_id == AV_CODEC_ID_HEVC) {
398  switch (avctx->profile) {
400  return HEVCProfileMain;
402  return HEVCProfileMainStill;
404  return HEVCProfileMain10;
405  }
406  } else if (avctx->codec_id == AV_CODEC_ID_VP9) {
407  switch (avctx->profile) {
408  case AV_PROFILE_VP9_0:
409  return VP9Profile0;
410  case AV_PROFILE_VP9_1:
411  return VP9Profile1;
412  case AV_PROFILE_VP9_2:
413  return VP9Profile2;
414  case AV_PROFILE_VP9_3:
415  return VP9Profile3;
416  }
417  } else if(avctx->codec_id == AV_CODEC_ID_MPEG4) {
418  switch (avctx->profile)
419  {
421  return MPEG4ProfileSimple;
423  return MPEG4ProfileSimpleScalable;
425  return MPEG4ProfileCore;
427  return MPEG4ProfileMain;
429  return MPEG4ProfileNbit;
431  return MPEG4ProfileScalableTexture;
433  return MPEG4ProfileSimpleFBA;
435  return MPEG4ProfileBasicAnimated;
437  return MPEG4ProfileHybrid;
439  return MPEG4ProfileAdvancedRealTime;
441  return MPEG4ProfileCoreScalable;
443  return MPEG4ProfileAdvancedCoding;
445  return MPEG4ProfileAdvancedCore;
447  return MPEG4ProfileAdvancedScalable;
449  return MPEG4ProfileAdvancedSimple;
451  // Studio profiles are not supported by mediacodec.
452  default:
453  break;
454  }
455  } else if(avctx->codec_id == AV_CODEC_ID_AV1) {
456  switch (avctx->profile)
457  {
458  case AV_PROFILE_AV1_MAIN:
459  return AV1ProfileMain8;
460  case AV_PROFILE_AV1_HIGH:
462  default:
463  break;
464  }
465  }
466 
467  return -1;
468 }
469 
470 char *ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int encoder, void *log_ctx)
471 {
472  int ret;
473  int i;
474  int codec_count;
475  int found_codec = 0;
476  char *name = NULL;
477  char *supported_type = NULL;
478 
479  JNIEnv *env = NULL;
480  struct JNIAMediaCodecListFields jfields = { 0 };
481  struct JNIAMediaFormatFields mediaformat_jfields = { 0 };
482 
483  jobject codec_name = NULL;
484 
485  jobject info = NULL;
486  jobject type = NULL;
487  jobjectArray types = NULL;
488 
489  jobject capabilities = NULL;
490  jobject profile_level = NULL;
491  jobjectArray profile_levels = NULL;
492 
493  JNI_GET_ENV_OR_RETURN(env, log_ctx, NULL);
494 
495  if ((ret = ff_jni_init_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx)) < 0) {
496  goto done;
497  }
498 
499  if ((ret = ff_jni_init_jfields(env, &mediaformat_jfields, jni_amediaformat_mapping, 0, log_ctx)) < 0) {
500  goto done;
501  }
502 
503  codec_count = (*env)->CallStaticIntMethod(env, jfields.mediacodec_list_class, jfields.get_codec_count_id);
504  if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
505  goto done;
506  }
507 
508  for(i = 0; i < codec_count; i++) {
509  int j;
510  int type_count;
511  int is_encoder;
512 
513  info = (*env)->CallStaticObjectMethod(env, jfields.mediacodec_list_class, jfields.get_codec_info_at_id, i);
514  if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
515  goto done;
516  }
517 
518  types = (*env)->CallObjectMethod(env, info, jfields.get_supported_types_id);
519  if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
520  goto done;
521  }
522 
523  is_encoder = (*env)->CallBooleanMethod(env, info, jfields.is_encoder_id);
524  if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
525  goto done;
526  }
527 
528  if (is_encoder != encoder) {
529  goto done_with_info;
530  }
531 
532  if (jfields.is_software_only_id) {
533  int is_software_only = (*env)->CallBooleanMethod(env, info, jfields.is_software_only_id);
534  if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
535  goto done;
536  }
537 
538  if (is_software_only) {
539  goto done_with_info;
540  }
541  }
542 
543  codec_name = (*env)->CallObjectMethod(env, info, jfields.get_name_id);
544  if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
545  goto done;
546  }
547 
548  name = ff_jni_jstring_to_utf_chars(env, codec_name, log_ctx);
549  if (!name) {
550  goto done;
551  }
552 
553  (*env)->DeleteLocalRef(env, codec_name);
554  codec_name = NULL;
555 
556  /* Skip software decoders */
557  if (
558  strstr(name, "OMX.google") ||
559  strstr(name, "OMX.ffmpeg") ||
560  (strstr(name, "OMX.SEC") && strstr(name, ".sw.")) ||
561  !strcmp(name, "OMX.qcom.video.decoder.hevcswvdec")) {
562  goto done_with_info;
563  }
564 
565  type_count = (*env)->GetArrayLength(env, types);
566  for (j = 0; j < type_count; j++) {
567  int k;
568  int profile_count;
569 
570  type = (*env)->GetObjectArrayElement(env, types, j);
571  if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
572  goto done;
573  }
574 
575  supported_type = ff_jni_jstring_to_utf_chars(env, type, log_ctx);
576  if (!supported_type) {
577  goto done;
578  }
579 
580  if (av_strcasecmp(supported_type, mime)) {
581  goto done_with_type;
582  }
583 
584  capabilities = (*env)->CallObjectMethod(env, info, jfields.get_codec_capabilities_id, type);
585  if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
586  goto done;
587  }
588 
589  profile_levels = (*env)->GetObjectField(env, capabilities, jfields.profile_levels_id);
590  if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
591  goto done;
592  }
593 
594  profile_count = (*env)->GetArrayLength(env, profile_levels);
595  if (!profile_count) {
596  found_codec = 1;
597  }
598  for (k = 0; k < profile_count; k++) {
599  int supported_profile = 0;
600 
601  if (profile < 0) {
602  found_codec = 1;
603  break;
604  }
605 
606  profile_level = (*env)->GetObjectArrayElement(env, profile_levels, k);
607  if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
608  goto done;
609  }
610 
611  supported_profile = (*env)->GetIntField(env, profile_level, jfields.profile_id);
612  if (ff_jni_exception_check(env, 1, log_ctx) < 0) {
613  goto done;
614  }
615 
616  found_codec = profile == supported_profile;
617 
618  (*env)->DeleteLocalRef(env, profile_level);
619  profile_level = NULL;
620 
621  if (found_codec) {
622  break;
623  }
624  }
625 
626 done_with_type:
627  (*env)->DeleteLocalRef(env, profile_levels);
628  profile_levels = NULL;
629 
630  (*env)->DeleteLocalRef(env, capabilities);
631  capabilities = NULL;
632 
633  (*env)->DeleteLocalRef(env, type);
634  type = NULL;
635 
636  av_freep(&supported_type);
637 
638  if (found_codec) {
639  break;
640  }
641  }
642 
643 done_with_info:
644  (*env)->DeleteLocalRef(env, info);
645  info = NULL;
646 
647  (*env)->DeleteLocalRef(env, types);
648  types = NULL;
649 
650  if (found_codec) {
651  break;
652  }
653 
654  av_freep(&name);
655  }
656 
657 done:
658  (*env)->DeleteLocalRef(env, codec_name);
659  (*env)->DeleteLocalRef(env, info);
660  (*env)->DeleteLocalRef(env, type);
661  (*env)->DeleteLocalRef(env, types);
662  (*env)->DeleteLocalRef(env, capabilities);
663  (*env)->DeleteLocalRef(env, profile_level);
664  (*env)->DeleteLocalRef(env, profile_levels);
665 
666  av_freep(&supported_type);
667 
668  ff_jni_reset_jfields(env, &jfields, jni_amediacodeclist_mapping, 0, log_ctx);
669  ff_jni_reset_jfields(env, &mediaformat_jfields, jni_amediaformat_mapping, 0, log_ctx);
670 
671  if (!found_codec) {
672  av_freep(&name);
673  }
674 
675  return name;
676 }
677 
679 {
680  JNIEnv *env = NULL;
682  jobject object = NULL;
683 
684  format = av_mallocz(sizeof(*format));
685  if (!format) {
686  return NULL;
687  }
688  format->api = media_format_jni;
689 
690  env = ff_jni_get_env(format);
691  if (!env) {
692  av_freep(&format);
693  return NULL;
694  }
695 
696  if (ff_jni_init_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format) < 0) {
697  goto fail;
698  }
699 
700  object = (*env)->NewObject(env, format->jfields.mediaformat_class, format->jfields.init_id);
701  if (!object) {
702  goto fail;
703  }
704 
705  format->object = (*env)->NewGlobalRef(env, object);
706  if (!format->object) {
707  goto fail;
708  }
709 
710 fail:
711  (*env)->DeleteLocalRef(env, object);
712 
713  if (!format->object) {
715  av_freep(&format);
716  }
717 
718  return (FFAMediaFormat *)format;
719 }
720 
722 {
723  JNIEnv *env = NULL;
725 
726  format = av_mallocz(sizeof(*format));
727  if (!format) {
728  return NULL;
729  }
730  format->api = media_format_jni;
731 
732  env = ff_jni_get_env(format);
733  if (!env) {
734  av_freep(&format);
735  return NULL;
736  }
737 
738  if (ff_jni_init_jfields(env, &format->jfields, jni_amediaformat_mapping, 1, format) < 0) {
739  goto fail;
740  }
741 
742  format->object = (*env)->NewGlobalRef(env, object);
743  if (!format->object) {
744  goto fail;
745  }
746 
747  return (FFAMediaFormat *)format;
748 fail:
750 
751  av_freep(&format);
752 
753  return NULL;
754 }
755 
757 {
758  int ret = 0;
760  JNIEnv *env = NULL;
761 
762  if (!format) {
763  return 0;
764  }
765 
767 
768  (*env)->DeleteGlobalRef(env, format->object);
769  format->object = NULL;
770 
772 
773  av_freep(&format);
774 
775  return ret;
776 }
777 
779 {
780  char *ret = NULL;
782  JNIEnv *env = NULL;
783  jstring description = NULL;
784 
785  av_assert0(format != NULL);
786 
788 
789  description = (*env)->CallObjectMethod(env, format->object, format->jfields.to_string_id);
790  if (ff_jni_exception_check(env, 1, NULL) < 0) {
791  goto fail;
792  }
793 
795 fail:
796  (*env)->DeleteLocalRef(env, description);
797 
798  return ret;
799 }
800 
802 {
803  int ret = 1;
805  JNIEnv *env = NULL;
806  jstring key = NULL;
807  jboolean contains_key;
808 
809  av_assert0(format != NULL);
810 
811  JNI_GET_ENV_OR_RETURN(env, format, 0);
812 
814  if (!key) {
815  ret = 0;
816  goto fail;
817  }
818 
819  contains_key = (*env)->CallBooleanMethod(env, format->object, format->jfields.contains_key_id, key);
820  if (!contains_key || (ret = ff_jni_exception_check(env, 1, format)) < 0) {
821  ret = 0;
822  goto fail;
823  }
824 
825  *out = (*env)->CallIntMethod(env, format->object, format->jfields.get_integer_id, key);
826  if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
827  ret = 0;
828  goto fail;
829  }
830 
831  ret = 1;
832 fail:
833  (*env)->DeleteLocalRef(env, key);
834 
835  return ret;
836 }
837 
839 {
840  int ret = 1;
842  JNIEnv *env = NULL;
843  jstring key = NULL;
844  jboolean contains_key;
845 
846  av_assert0(format != NULL);
847 
848  JNI_GET_ENV_OR_RETURN(env, format, 0);
849 
851  if (!key) {
852  ret = 0;
853  goto fail;
854  }
855 
856  contains_key = (*env)->CallBooleanMethod(env, format->object, format->jfields.contains_key_id, key);
857  if (!contains_key || (ret = ff_jni_exception_check(env, 1, format)) < 0) {
858  ret = 0;
859  goto fail;
860  }
861 
862  *out = (*env)->CallLongMethod(env, format->object, format->jfields.get_long_id, key);
863  if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
864  ret = 0;
865  goto fail;
866  }
867 
868  ret = 1;
869 fail:
870  (*env)->DeleteLocalRef(env, key);
871 
872  return ret;
873 }
874 
875 static int mediaformat_jni_getFloat(FFAMediaFormat* ctx, const char *name, float *out)
876 {
877  int ret = 1;
879  JNIEnv *env = NULL;
880  jstring key = NULL;
881  jboolean contains_key;
882 
883  av_assert0(format != NULL);
884 
885  JNI_GET_ENV_OR_RETURN(env, format, 0);
886 
888  if (!key) {
889  ret = 0;
890  goto fail;
891  }
892 
893  contains_key = (*env)->CallBooleanMethod(env, format->object, format->jfields.contains_key_id, key);
894  if (!contains_key || (ret = ff_jni_exception_check(env, 1, format)) < 0) {
895  ret = 0;
896  goto fail;
897  }
898 
899  *out = (*env)->CallFloatMethod(env, format->object, format->jfields.get_float_id, key);
900  if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
901  ret = 0;
902  goto fail;
903  }
904 
905  ret = 1;
906 fail:
907  (*env)->DeleteLocalRef(env, key);
908 
909  return ret;
910 }
911 
912 static int mediaformat_jni_getBuffer(FFAMediaFormat* ctx, const char *name, void** data, size_t *size)
913 {
914  int ret = 1;
916  JNIEnv *env = NULL;
917  jstring key = NULL;
918  jboolean contains_key;
919  jobject result = NULL;
920 
921  av_assert0(format != NULL);
922 
923  JNI_GET_ENV_OR_RETURN(env, format, 0);
924 
926  if (!key) {
927  ret = 0;
928  goto fail;
929  }
930 
931  contains_key = (*env)->CallBooleanMethod(env, format->object, format->jfields.contains_key_id, key);
932  if (!contains_key || (ret = ff_jni_exception_check(env, 1, format)) < 0) {
933  ret = 0;
934  goto fail;
935  }
936 
937  result = (*env)->CallObjectMethod(env, format->object, format->jfields.get_bytebuffer_id, key);
938  if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
939  ret = 0;
940  goto fail;
941  }
942 
943  *data = (*env)->GetDirectBufferAddress(env, result);
944  *size = (*env)->GetDirectBufferCapacity(env, result);
945 
946  if (*data && *size) {
947  void *src = *data;
948  *data = av_malloc(*size);
949  if (!*data) {
950  ret = 0;
951  goto fail;
952  }
953 
954  memcpy(*data, src, *size);
955  }
956 
957  ret = 1;
958 fail:
959  (*env)->DeleteLocalRef(env, key);
960  (*env)->DeleteLocalRef(env, result);
961 
962  return ret;
963 }
964 
965 static int mediaformat_jni_getString(FFAMediaFormat* ctx, const char *name, const char **out)
966 {
967  int ret = 1;
969  JNIEnv *env = NULL;
970  jstring key = NULL;
971  jboolean contains_key;
972  jstring result = NULL;
973 
974  av_assert0(format != NULL);
975 
976  JNI_GET_ENV_OR_RETURN(env, format, 0);
977 
979  if (!key) {
980  ret = 0;
981  goto fail;
982  }
983 
984  contains_key = (*env)->CallBooleanMethod(env, format->object, format->jfields.contains_key_id, key);
985  if (!contains_key || (ret = ff_jni_exception_check(env, 1, format)) < 0) {
986  ret = 0;
987  goto fail;
988  }
989 
990  result = (*env)->CallObjectMethod(env, format->object, format->jfields.get_string_id, key);
991  if ((ret = ff_jni_exception_check(env, 1, format)) < 0) {
992  ret = 0;
993  goto fail;
994  }
995 
997  if (!*out) {
998  ret = 0;
999  goto fail;
1000  }
1001 
1002  ret = 1;
1003 fail:
1004  (*env)->DeleteLocalRef(env, key);
1005  (*env)->DeleteLocalRef(env, result);
1006 
1007  return ret;
1008 }
1009 
1011 {
1012  JNIEnv *env = NULL;
1013  jstring key = NULL;
1015 
1016  av_assert0(format != NULL);
1017 
1019 
1021  if (!key) {
1022  goto fail;
1023  }
1024 
1025  (*env)->CallVoidMethod(env, format->object, format->jfields.set_integer_id, key, value);
1026  if (ff_jni_exception_check(env, 1, format) < 0) {
1027  goto fail;
1028  }
1029 
1030 fail:
1031  (*env)->DeleteLocalRef(env, key);
1032 }
1033 
1035 {
1036  JNIEnv *env = NULL;
1037  jstring key = NULL;
1039 
1040  av_assert0(format != NULL);
1041 
1043 
1045  if (!key) {
1046  goto fail;
1047  }
1048 
1049  (*env)->CallVoidMethod(env, format->object, format->jfields.set_long_id, key, value);
1050  if (ff_jni_exception_check(env, 1, format) < 0) {
1051  goto fail;
1052  }
1053 
1054 fail:
1055  (*env)->DeleteLocalRef(env, key);
1056 }
1057 
1058 static void mediaformat_jni_setFloat(FFAMediaFormat* ctx, const char* name, float value)
1059 {
1060  JNIEnv *env = NULL;
1061  jstring key = NULL;
1063 
1064  av_assert0(format != NULL);
1065 
1067 
1069  if (!key) {
1070  goto fail;
1071  }
1072 
1073  (*env)->CallVoidMethod(env, format->object, format->jfields.set_float_id, key, value);
1074  if (ff_jni_exception_check(env, 1, format) < 0) {
1075  goto fail;
1076  }
1077 
1078 fail:
1079  (*env)->DeleteLocalRef(env, key);
1080 }
1081 
1082 static void mediaformat_jni_setString(FFAMediaFormat* ctx, const char* name, const char* value)
1083 {
1084  JNIEnv *env = NULL;
1085  jstring key = NULL;
1086  jstring string = NULL;
1088 
1089  av_assert0(format != NULL);
1090 
1092 
1094  if (!key) {
1095  goto fail;
1096  }
1097 
1098  string = ff_jni_utf_chars_to_jstring(env, value, format);
1099  if (!string) {
1100  goto fail;
1101  }
1102 
1103  (*env)->CallVoidMethod(env, format->object, format->jfields.set_string_id, key, string);
1104  if (ff_jni_exception_check(env, 1, format) < 0) {
1105  goto fail;
1106  }
1107 
1108 fail:
1109  (*env)->DeleteLocalRef(env, key);
1110  (*env)->DeleteLocalRef(env, string);
1111 }
1112 
1113 static void mediaformat_jni_setBuffer(FFAMediaFormat* ctx, const char* name, void* data, size_t size)
1114 {
1115  JNIEnv *env = NULL;
1116  jstring key = NULL;
1117  jobject buffer = NULL;
1118  void *buffer_data = NULL;
1120 
1121  av_assert0(format != NULL);
1122 
1124 
1126  if (!key) {
1127  goto fail;
1128  }
1129 
1130  if (!data || !size) {
1131  goto fail;
1132  }
1133 
1135  if (!buffer_data) {
1136  goto fail;
1137  }
1138 
1139  memcpy(buffer_data, data, size);
1140 
1141  buffer = (*env)->NewDirectByteBuffer(env, buffer_data, size);
1142  if (!buffer) {
1143  goto fail;
1144  }
1145 
1146  (*env)->CallVoidMethod(env, format->object, format->jfields.set_bytebuffer_id, key, buffer);
1147  if (ff_jni_exception_check(env, 1, format) < 0) {
1148  goto fail;
1149  }
1150 
1151 fail:
1152  (*env)->DeleteLocalRef(env, key);
1153  (*env)->DeleteLocalRef(env, buffer);
1154 }
1155 
1157 {
1158  int ret = 0;
1159  JNIEnv *env = NULL;
1160 
1162 
1163  codec->INFO_TRY_AGAIN_LATER = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_try_again_later_id);
1164  if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1165  goto fail;
1166  }
1167 
1168  codec->BUFFER_FLAG_CODEC_CONFIG = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.buffer_flag_codec_config_id);
1169  if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1170  goto fail;
1171  }
1172 
1173  codec->BUFFER_FLAG_END_OF_STREAM = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.buffer_flag_end_of_stream_id);
1174  if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1175  goto fail;
1176  }
1177 
1178  if (codec->jfields.buffer_flag_key_frame_id) {
1179  codec->BUFFER_FLAG_KEY_FRAME = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.buffer_flag_key_frame_id);
1180  if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1181  goto fail;
1182  }
1183  }
1184 
1185  codec->CONFIGURE_FLAG_ENCODE = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.configure_flag_encode_id);
1186  if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1187  goto fail;
1188  }
1189 
1190  codec->INFO_TRY_AGAIN_LATER = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_try_again_later_id);
1191  if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1192  goto fail;
1193  }
1194 
1195  codec->INFO_OUTPUT_BUFFERS_CHANGED = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_output_buffers_changed_id);
1196  if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1197  goto fail;
1198  }
1199 
1200  codec->INFO_OUTPUT_FORMAT_CHANGED = (*env)->GetStaticIntField(env, codec->jfields.mediacodec_class, codec->jfields.info_output_format_changed_id);
1201  if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1202  goto fail;
1203  }
1204 
1205 fail:
1206 
1207  return ret;
1208 }
1209 
1210 #define CREATE_CODEC_BY_NAME 0
1211 #define CREATE_DECODER_BY_TYPE 1
1212 #define CREATE_ENCODER_BY_TYPE 2
1213 
1214 static inline FFAMediaCodec *codec_create(int method, const char *arg)
1215 {
1216  int ret = -1;
1217  JNIEnv *env = NULL;
1218  FFAMediaCodecJni *codec = NULL;
1219  jstring jarg = NULL;
1220  jobject object = NULL;
1221  jobject buffer_info = NULL;
1222  jmethodID create_id = NULL;
1223 
1224  codec = av_mallocz(sizeof(*codec));
1225  if (!codec) {
1226  return NULL;
1227  }
1228  codec->api = media_codec_jni;
1229 
1230  env = ff_jni_get_env(codec);
1231  if (!env) {
1232  av_freep(&codec);
1233  return NULL;
1234  }
1235 
1236  if (ff_jni_init_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec) < 0) {
1237  goto fail;
1238  }
1239 
1240  jarg = ff_jni_utf_chars_to_jstring(env, arg, codec);
1241  if (!jarg) {
1242  goto fail;
1243  }
1244 
1245  switch (method) {
1246  case CREATE_CODEC_BY_NAME: create_id = codec->jfields.create_by_codec_name_id; break;
1247  case CREATE_DECODER_BY_TYPE: create_id = codec->jfields.create_decoder_by_type_id; break;
1248  case CREATE_ENCODER_BY_TYPE: create_id = codec->jfields.create_encoder_by_type_id; break;
1249  default:
1250  av_assert0(0);
1251  }
1252 
1253  object = (*env)->CallStaticObjectMethod(env,
1254  codec->jfields.mediacodec_class,
1255  create_id,
1256  jarg);
1257  if (ff_jni_exception_check(env, 1, codec) < 0) {
1258  goto fail;
1259  }
1260 
1261  codec->object = (*env)->NewGlobalRef(env, object);
1262  if (!codec->object) {
1263  goto fail;
1264  }
1265 
1266  if (codec_init_static_fields(codec) < 0) {
1267  goto fail;
1268  }
1269 
1271  codec->has_get_i_o_buffer = 1;
1272  }
1273 
1274  buffer_info = (*env)->NewObject(env, codec->jfields.mediainfo_class, codec->jfields.init_id);
1275  if (ff_jni_exception_check(env, 1, codec) < 0) {
1276  goto fail;
1277  }
1278 
1279  codec->buffer_info = (*env)->NewGlobalRef(env, buffer_info);
1280  if (!codec->buffer_info) {
1281  goto fail;
1282  }
1283 
1284  ret = 0;
1285 fail:
1286  (*env)->DeleteLocalRef(env, jarg);
1287  (*env)->DeleteLocalRef(env, object);
1288  (*env)->DeleteLocalRef(env, buffer_info);
1289 
1290  if (ret < 0) {
1291  (*env)->DeleteGlobalRef(env, codec->object);
1292  (*env)->DeleteGlobalRef(env, codec->buffer_info);
1293 
1294  ff_jni_reset_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec);
1295  av_freep(&codec);
1296  }
1297 
1298  return (FFAMediaCodec *)codec;
1299 }
1300 
1301 #define DECLARE_FF_AMEDIACODEC_CREATE_FUNC(name, method) \
1302 static FFAMediaCodec *mediacodec_jni_##name(const char *arg) \
1303 { \
1304  return codec_create(method, arg); \
1305 } \
1306 
1310 
1312 {
1313  int ret = 0;
1315  JNIEnv *env = NULL;
1316 
1317  if (!codec) {
1318  return 0;
1319  }
1320 
1322 
1323  (*env)->CallVoidMethod(env, codec->object, codec->jfields.release_id);
1324  if (ff_jni_exception_check(env, 1, codec) < 0) {
1326  }
1327 
1328  (*env)->DeleteGlobalRef(env, codec->input_buffers);
1329  codec->input_buffers = NULL;
1330 
1331  (*env)->DeleteGlobalRef(env, codec->output_buffers);
1332  codec->output_buffers = NULL;
1333 
1334  (*env)->DeleteGlobalRef(env, codec->object);
1335  codec->object = NULL;
1336 
1337  (*env)->DeleteGlobalRef(env, codec->buffer_info);
1338  codec->buffer_info = NULL;
1339 
1340  ff_jni_reset_jfields(env, &codec->jfields, jni_amediacodec_mapping, 1, codec);
1341 
1342  av_freep(&codec);
1343 
1344  return ret;
1345 }
1346 
1348 {
1349  char *ret = NULL;
1350  JNIEnv *env = NULL;
1351  jobject *name = NULL;
1353 
1354  JNI_GET_ENV_OR_RETURN(env, codec, NULL);
1355 
1356  name = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_name_id);
1357  if (ff_jni_exception_check(env, 1, codec) < 0) {
1358  goto fail;
1359  }
1360 
1361  ret = ff_jni_jstring_to_utf_chars(env, name, codec);
1362 
1363 fail:
1364  if (name) {
1365  (*env)->DeleteLocalRef(env, name);
1366  }
1367 
1368  return ret;
1369 }
1370 
1372  const FFAMediaFormat* format_ctx,
1374  void *crypto,
1375  uint32_t flags)
1376 {
1377  int ret = 0;
1378  JNIEnv *env = NULL;
1380  const FFAMediaFormatJni *format = (FFAMediaFormatJni *)format_ctx;
1381  jobject *surface = window ? window->surface : NULL;
1382 
1384 
1385  if (flags & codec->CONFIGURE_FLAG_ENCODE) {
1386  if (surface && !codec->jfields.set_input_surface_id) {
1387  av_log(ctx, AV_LOG_ERROR, "System doesn't support setInputSurface\n");
1388  return AVERROR_EXTERNAL;
1389  }
1390 
1391  (*env)->CallVoidMethod(env, codec->object, codec->jfields.configure_id, format->object, NULL, NULL, flags);
1392  if (ff_jni_exception_check(env, 1, codec) < 0)
1393  return AVERROR_EXTERNAL;
1394 
1395  if (!surface)
1396  return 0;
1397 
1398  (*env)->CallVoidMethod(env, codec->object, codec->jfields.set_input_surface_id, surface);
1399  if (ff_jni_exception_check(env, 1, codec) < 0)
1400  return AVERROR_EXTERNAL;
1401  return 0;
1402  } else {
1403  (*env)->CallVoidMethod(env, codec->object, codec->jfields.configure_id, format->object, surface, NULL, flags);
1404  }
1405  if (ff_jni_exception_check(env, 1, codec) < 0) {
1407  goto fail;
1408  }
1409 
1410 fail:
1411  return ret;
1412 }
1413 
1415 {
1416  int ret = 0;
1417  JNIEnv *env = NULL;
1419 
1421 
1422  (*env)->CallVoidMethod(env, codec->object, codec->jfields.start_id);
1423  if (ff_jni_exception_check(env, 1, codec) < 0) {
1425  goto fail;
1426  }
1427 
1428 fail:
1429  return ret;
1430 }
1431 
1433 {
1434  int ret = 0;
1435  JNIEnv *env = NULL;
1437 
1439 
1440  (*env)->CallVoidMethod(env, codec->object, codec->jfields.stop_id);
1441  if (ff_jni_exception_check(env, 1, codec) < 0) {
1443  goto fail;
1444  }
1445 
1446 fail:
1447  return ret;
1448 }
1449 
1451 {
1452  int ret = 0;
1453  JNIEnv *env = NULL;
1455 
1457 
1458  (*env)->CallVoidMethod(env, codec->object, codec->jfields.flush_id);
1459  if (ff_jni_exception_check(env, 1, codec) < 0) {
1461  goto fail;
1462  }
1463 
1464 fail:
1465  return ret;
1466 }
1467 
1468 static int mediacodec_jni_releaseOutputBuffer(FFAMediaCodec* ctx, size_t idx, int render)
1469 {
1470  int ret = 0;
1471  JNIEnv *env = NULL;
1473 
1475 
1476  (*env)->CallVoidMethod(env, codec->object, codec->jfields.release_output_buffer_id, (jint)idx, (jboolean)render);
1477  if (ff_jni_exception_check(env, 1, codec) < 0) {
1479  goto fail;
1480  }
1481 
1482 fail:
1483  return ret;
1484 }
1485 
1487 {
1488  int ret = 0;
1489  JNIEnv *env = NULL;
1491 
1493 
1494  (*env)->CallVoidMethod(env, codec->object, codec->jfields.release_output_buffer_at_time_id, (jint)idx, (jlong)timestampNs);
1495  if (ff_jni_exception_check(env, 1, codec) < 0) {
1497  goto fail;
1498  }
1499 
1500 fail:
1501  return ret;
1502 }
1503 
1505 {
1506  int ret = 0;
1507  JNIEnv *env = NULL;
1509 
1511 
1512  ret = (*env)->CallIntMethod(env, codec->object, codec->jfields.dequeue_input_buffer_id, timeoutUs);
1513  if (ff_jni_exception_check(env, 1, codec) < 0) {
1515  goto fail;
1516  }
1517 
1518 fail:
1519  return ret;
1520 }
1521 
1522 static int mediacodec_jni_queueInputBuffer(FFAMediaCodec* ctx, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags)
1523 {
1524  int ret = 0;
1525  JNIEnv *env = NULL;
1527 
1529 
1530  (*env)->CallVoidMethod(env, codec->object, codec->jfields.queue_input_buffer_id, (jint)idx, (jint)offset, (jint)size, time, flags);
1531  if ((ret = ff_jni_exception_check(env, 1, codec)) < 0) {
1533  goto fail;
1534  }
1535 
1536 fail:
1537  return ret;
1538 }
1539 
1541 {
1542  int ret = 0;
1543  JNIEnv *env = NULL;
1545 
1547 
1548  ret = (*env)->CallIntMethod(env, codec->object, codec->jfields.dequeue_output_buffer_id, codec->buffer_info, timeoutUs);
1549  if (ff_jni_exception_check(env, 1, codec) < 0) {
1550  return AVERROR_EXTERNAL;
1551  }
1552 
1553  info->flags = (*env)->GetIntField(env, codec->buffer_info, codec->jfields.flags_id);
1554  if (ff_jni_exception_check(env, 1, codec) < 0) {
1555  return AVERROR_EXTERNAL;
1556  }
1557 
1558  info->offset = (*env)->GetIntField(env, codec->buffer_info, codec->jfields.offset_id);
1559  if (ff_jni_exception_check(env, 1, codec) < 0) {
1560  return AVERROR_EXTERNAL;
1561  }
1562 
1563  info->presentationTimeUs = (*env)->GetLongField(env, codec->buffer_info, codec->jfields.presentation_time_us_id);
1564  if (ff_jni_exception_check(env, 1, codec) < 0) {
1565  return AVERROR_EXTERNAL;
1566  }
1567 
1568  info->size = (*env)->GetIntField(env, codec->buffer_info, codec->jfields.size_id);
1569  if (ff_jni_exception_check(env, 1, codec) < 0) {
1570  return AVERROR_EXTERNAL;
1571  }
1572 
1573  return ret;
1574 }
1575 
1576 static uint8_t* mediacodec_jni_getInputBuffer(FFAMediaCodec* ctx, size_t idx, size_t *out_size)
1577 {
1578  uint8_t *ret = NULL;
1579  JNIEnv *env = NULL;
1581  jobject buffer = NULL;
1582  jobject input_buffers = NULL;
1583 
1584  JNI_GET_ENV_OR_RETURN(env, codec, NULL);
1585 
1586  if (codec->has_get_i_o_buffer) {
1587  buffer = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_input_buffer_id, (jint)idx);
1588  if (ff_jni_exception_check(env, 1, codec) < 0) {
1589  goto fail;
1590  }
1591  } else {
1592  if (!codec->input_buffers) {
1593  input_buffers = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_input_buffers_id);
1594  if (ff_jni_exception_check(env, 1, codec) < 0) {
1595  goto fail;
1596  }
1597 
1598  codec->input_buffers = (*env)->NewGlobalRef(env, input_buffers);
1599  if (ff_jni_exception_check(env, 1, codec) < 0) {
1600  goto fail;
1601  }
1602  }
1603 
1604  buffer = (*env)->GetObjectArrayElement(env, codec->input_buffers, idx);
1605  if (ff_jni_exception_check(env, 1, codec) < 0) {
1606  goto fail;
1607  }
1608  }
1609 
1610  ret = (*env)->GetDirectBufferAddress(env, buffer);
1611  *out_size = (*env)->GetDirectBufferCapacity(env, buffer);
1612 fail:
1613  (*env)->DeleteLocalRef(env, buffer);
1614  (*env)->DeleteLocalRef(env, input_buffers);
1615 
1616  return ret;
1617 }
1618 
1619 static uint8_t* mediacodec_jni_getOutputBuffer(FFAMediaCodec* ctx, size_t idx, size_t *out_size)
1620 {
1621  uint8_t *ret = NULL;
1622  JNIEnv *env = NULL;
1624  jobject buffer = NULL;
1625  jobject output_buffers = NULL;
1626 
1627  JNI_GET_ENV_OR_RETURN(env, codec, NULL);
1628 
1629  if (codec->has_get_i_o_buffer) {
1630  buffer = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_output_buffer_id, (jint)idx);
1631  if (ff_jni_exception_check(env, 1, codec) < 0) {
1632  goto fail;
1633  }
1634  } else {
1635  if (!codec->output_buffers) {
1636  output_buffers = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_output_buffers_id);
1637  if (ff_jni_exception_check(env, 1, codec) < 0) {
1638  goto fail;
1639  }
1640 
1641  codec->output_buffers = (*env)->NewGlobalRef(env, output_buffers);
1642  if (ff_jni_exception_check(env, 1, codec) < 0) {
1643  goto fail;
1644  }
1645  }
1646 
1647  buffer = (*env)->GetObjectArrayElement(env, codec->output_buffers, idx);
1648  if (ff_jni_exception_check(env, 1, codec) < 0) {
1649  goto fail;
1650  }
1651  }
1652 
1653  ret = (*env)->GetDirectBufferAddress(env, buffer);
1654  *out_size = (*env)->GetDirectBufferCapacity(env, buffer);
1655 fail:
1656  (*env)->DeleteLocalRef(env, buffer);
1657  (*env)->DeleteLocalRef(env, output_buffers);
1658 
1659  return ret;
1660 }
1661 
1663 {
1664  FFAMediaFormat *ret = NULL;
1665  JNIEnv *env = NULL;
1667 
1668  jobject mediaformat = NULL;
1669 
1670  JNI_GET_ENV_OR_RETURN(env, codec, NULL);
1671 
1672  mediaformat = (*env)->CallObjectMethod(env, codec->object, codec->jfields.get_output_format_id);
1673  if (ff_jni_exception_check(env, 1, codec) < 0) {
1674  goto fail;
1675  }
1676 
1677  ret = mediaformat_jni_newFromObject(mediaformat);
1678 fail:
1679  (*env)->DeleteLocalRef(env, mediaformat);
1680 
1681  return ret;
1682 }
1683 
1685 {
1687  return idx == codec->INFO_TRY_AGAIN_LATER;
1688 }
1689 
1691 {
1693  return idx == codec->INFO_OUTPUT_BUFFERS_CHANGED;
1694 }
1695 
1697 {
1699  return idx == codec->INFO_OUTPUT_FORMAT_CHANGED;
1700 }
1701 
1703 {
1705  return codec->BUFFER_FLAG_CODEC_CONFIG;
1706 }
1707 
1709 {
1711  return codec->BUFFER_FLAG_END_OF_STREAM;
1712 }
1713 
1715 {
1717  return codec->BUFFER_FLAG_KEY_FRAME;
1718 }
1719 
1721 {
1723  return codec->CONFIGURE_FLAG_ENCODE;
1724 }
1725 
1727 {
1728  int ret = 0;
1730 
1731  if (!codec->has_get_i_o_buffer) {
1732  if (codec->output_buffers) {
1733  JNIEnv *env = NULL;
1734 
1735  env = ff_jni_get_env(codec);
1736  if (!env) {
1738  goto fail;
1739  }
1740 
1741  (*env)->DeleteGlobalRef(env, codec->output_buffers);
1742  codec->output_buffers = NULL;
1743  }
1744  }
1745 
1746 fail:
1747  return ret;
1748 }
1749 
1751 {
1752  JNIEnv *env = NULL;
1754 
1756 
1757  (*env)->CallVoidMethod(env, codec->object, codec->jfields.signal_end_of_input_stream_id);
1758  if (ff_jni_exception_check(env, 1, codec) < 0) {
1759  return AVERROR_EXTERNAL;
1760  }
1761 
1762  return 0;
1763 }
1764 
1765 static const FFAMediaFormat media_format_jni = {
1767 
1768  .create = mediaformat_jni_new,
1770 
1771  .toString = mediaformat_jni_toString,
1772 
1773  .getInt32 = mediaformat_jni_getInt32,
1774  .getInt64 = mediaformat_jni_getInt64,
1775  .getFloat = mediaformat_jni_getFloat,
1776  .getBuffer = mediaformat_jni_getBuffer,
1777  .getString = mediaformat_jni_getString,
1778 
1779  .setInt32 = mediaformat_jni_setInt32,
1780  .setInt64 = mediaformat_jni_setInt64,
1781  .setFloat = mediaformat_jni_setFloat,
1782  .setString = mediaformat_jni_setString,
1783  .setBuffer = mediaformat_jni_setBuffer,
1784 };
1785 
1786 static const FFAMediaCodec media_codec_jni = {
1788 
1789  .getName = mediacodec_jni_getName,
1790 
1791  .createCodecByName = mediacodec_jni_createCodecByName,
1792  .createDecoderByType = mediacodec_jni_createDecoderByType,
1793  .createEncoderByType = mediacodec_jni_createEncoderByType,
1794  .delete = mediacodec_jni_delete,
1795 
1796  .configure = mediacodec_jni_configure,
1797  .start = mediacodec_jni_start,
1798  .stop = mediacodec_jni_stop,
1799  .flush = mediacodec_jni_flush,
1800 
1801  .getInputBuffer = mediacodec_jni_getInputBuffer,
1802  .getOutputBuffer = mediacodec_jni_getOutputBuffer,
1803 
1804  .dequeueInputBuffer = mediacodec_jni_dequeueInputBuffer,
1805  .queueInputBuffer = mediacodec_jni_queueInputBuffer,
1806 
1807  .dequeueOutputBuffer = mediacodec_jni_dequeueOutputBuffer,
1808  .getOutputFormat = mediacodec_jni_getOutputFormat,
1809 
1810  .releaseOutputBuffer = mediacodec_jni_releaseOutputBuffer,
1811  .releaseOutputBufferAtTime = mediacodec_jni_releaseOutputBufferAtTime,
1812 
1813  .infoTryAgainLater = mediacodec_jni_infoTryAgainLater,
1814  .infoOutputBuffersChanged = mediacodec_jni_infoOutputBuffersChanged,
1815  .infoOutputFormatChanged = mediacodec_jni_infoOutputFormatChanged,
1816 
1817  .getBufferFlagCodecConfig = mediacodec_jni_getBufferFlagCodecConfig,
1818  .getBufferFlagEndOfStream = mediacodec_jni_getBufferFlagEndOfStream,
1819  .getBufferFlagKeyFrame = mediacodec_jni_getBufferFlagKeyFrame,
1820 
1821  .getConfigureFlagEncode = mediacodec_jni_getConfigureFlagEncode,
1822  .cleanOutputBuffers = mediacodec_jni_cleanOutputBuffers,
1823  .signalEndOfInputStream = mediacodec_jni_signalEndOfInputStream,
1824 };
1825 
1826 typedef struct FFAMediaFormatNdk {
1828 
1829  void *libmedia;
1830  AMediaFormat *impl;
1831 
1832  bool (*getRect)(AMediaFormat *, const char *name,
1833  int32_t *left, int32_t *top, int32_t *right, int32_t *bottom);
1834  void (*setRect)(AMediaFormat *, const char *name,
1835  int32_t left, int32_t top, int32_t right, int32_t bottom);
1837 
1838 typedef struct FFAMediaCodecNdk {
1840 
1841  void *libmedia;
1842  AMediaCodec *impl;
1843  ANativeWindow *window;
1844 
1845  // Available since API level 28.
1846  media_status_t (*getName)(AMediaCodec*, char** out_name);
1847  void (*releaseName)(AMediaCodec*, char* name);
1848 
1849  // Available since API level 26.
1850  media_status_t (*setInputSurface)(AMediaCodec*, ANativeWindow *);
1851  media_status_t (*signalEndOfInputStream)(AMediaCodec *);
1853 
1856 
1858  .class_name = "amediaformat_ndk",
1859  .item_name = av_default_item_name,
1860  .version = LIBAVUTIL_VERSION_INT,
1861 };
1862 
1864  .class_name = "amediacodec_ndk",
1865  .item_name = av_default_item_name,
1866  .version = LIBAVUTIL_VERSION_INT,
1867 };
1868 
1869 static FFAMediaFormat *mediaformat_ndk_create(AMediaFormat *impl)
1870 {
1872  if (!format)
1873  return NULL;
1874 
1875  format->api = media_format_ndk;
1876 
1877  format->libmedia = dlopen("libmediandk.so", RTLD_NOW);
1878  if (!format->libmedia)
1879  goto error;
1880 
1881 #define GET_OPTIONAL_SYMBOL(sym) \
1882  format->sym = dlsym(format->libmedia, "AMediaFormat_" #sym);
1883 
1884  GET_OPTIONAL_SYMBOL(getRect)
1885  GET_OPTIONAL_SYMBOL(setRect)
1886 
1887 #undef GET_OPTIONAL_SYMBOL
1888 
1889  if (impl) {
1890  format->impl = impl;
1891  } else {
1892  format->impl = AMediaFormat_new();
1893  if (!format->impl)
1894  goto error;
1895  }
1896 
1897  return (FFAMediaFormat *)format;
1898 
1899 error:
1900  if (format->libmedia)
1901  dlclose(format->libmedia);
1902  av_freep(&format);
1903  return NULL;
1904 }
1905 
1907 {
1908  return mediaformat_ndk_create(NULL);
1909 }
1910 
1912 {
1914  int ret = 0;
1915  if (!format)
1916  return 0;
1917 
1918  av_assert0(format->api.class == &amediaformat_ndk_class);
1919 
1920  if (format->impl && (AMediaFormat_delete(format->impl) != AMEDIA_OK))
1922  if (format->libmedia)
1923  dlclose(format->libmedia);
1924  av_free(format);
1925 
1926  return ret;
1927 }
1928 
1930 {
1932  const char *str = AMediaFormat_toString(format->impl);
1933  return av_strdup(str);
1934 }
1935 
1937 {
1939  return AMediaFormat_getInt32(format->impl, name, out);
1940 }
1941 
1943 {
1945  return AMediaFormat_getInt64(format->impl, name, out);
1946 }
1947 
1948 static int mediaformat_ndk_getFloat(FFAMediaFormat* ctx, const char *name, float *out)
1949 {
1951  return AMediaFormat_getFloat(format->impl, name, out);
1952 }
1953 
1954 static int mediaformat_ndk_getBuffer(FFAMediaFormat* ctx, const char *name, void** data, size_t *size)
1955 {
1957  return AMediaFormat_getBuffer(format->impl, name, data, size);
1958 }
1959 
1960 static int mediaformat_ndk_getString(FFAMediaFormat* ctx, const char *name, const char **out)
1961 {
1963  const char *tmp = NULL;
1964  int ret = AMediaFormat_getString(format->impl, name, &tmp);
1965 
1966  if (tmp)
1967  *out = av_strdup(tmp);
1968  return ret;
1969 }
1970 
1972  int32_t *left, int32_t *top, int32_t *right, int32_t *bottom)
1973 {
1975  if (!format->getRect)
1976  return AVERROR_EXTERNAL;
1977  return format->getRect(format->impl, name, left, top, right, bottom);
1978 }
1979 
1981 {
1983  AMediaFormat_setInt32(format->impl, name, value);
1984 }
1985 
1987 {
1989  AMediaFormat_setInt64(format->impl, name, value);
1990 }
1991 
1992 static void mediaformat_ndk_setFloat(FFAMediaFormat* ctx, const char* name, float value)
1993 {
1995  AMediaFormat_setFloat(format->impl, name, value);
1996 }
1997 
1998 static void mediaformat_ndk_setString(FFAMediaFormat* ctx, const char* name, const char* value)
1999 {
2001  AMediaFormat_setString(format->impl, name, value);
2002 }
2003 
2004 static void mediaformat_ndk_setBuffer(FFAMediaFormat* ctx, const char* name, void* data, size_t size)
2005 {
2007  AMediaFormat_setBuffer(format->impl, name, data, size);
2008 }
2009 
2011  int32_t left, int32_t top, int32_t right, int32_t bottom)
2012 {
2014  if (!format->setRect) {
2015  av_log(ctx, AV_LOG_WARNING, "Doesn't support setRect\n");
2016  return;
2017  }
2018  format->setRect(format->impl, name, left, top, right, bottom);
2019 }
2020 
2022 {
2024  char *ret = NULL;
2025  char *name = NULL;
2026 
2027  if (!codec->getName || !codec->releaseName) {
2028  av_log(ctx, AV_LOG_DEBUG, "getName() unavailable\n");
2029  return ret;
2030  }
2031 
2032  codec->getName(codec->impl, &name);
2033  if (name) {
2034  ret = av_strdup(name);
2035  codec->releaseName(codec->impl, name);
2036  }
2037 
2038  return ret;
2039 }
2040 
2041 static inline FFAMediaCodec *ndk_codec_create(int method, const char *arg) {
2042  FFAMediaCodecNdk *codec = av_mallocz(sizeof(*codec));
2043  const char *lib_name = "libmediandk.so";
2044 
2045  if (!codec)
2046  return NULL;
2047 
2048  codec->api = media_codec_ndk;
2049  codec->libmedia = dlopen(lib_name, RTLD_NOW);
2050  if (!codec->libmedia)
2051  goto error;
2052 
2053 #define GET_SYMBOL(sym) \
2054  codec->sym = dlsym(codec->libmedia, "AMediaCodec_" #sym); \
2055  if (!codec->sym) \
2056  av_log(codec, AV_LOG_INFO, #sym "() unavailable from %s\n", lib_name);
2057 
2058  GET_SYMBOL(getName)
2059  GET_SYMBOL(releaseName)
2060 
2061  GET_SYMBOL(setInputSurface)
2062  GET_SYMBOL(signalEndOfInputStream)
2063 
2064 #undef GET_SYMBOL
2065 
2066  switch (method) {
2067  case CREATE_CODEC_BY_NAME:
2068  codec->impl = AMediaCodec_createCodecByName(arg);
2069  break;
2071  codec->impl = AMediaCodec_createDecoderByType(arg);
2072  break;
2074  codec->impl = AMediaCodec_createEncoderByType(arg);
2075  break;
2076  default:
2077  av_assert0(0);
2078  }
2079  if (!codec->impl)
2080  goto error;
2081 
2082  return (FFAMediaCodec *)codec;
2083 
2084 error:
2085  if (codec->libmedia)
2086  dlclose(codec->libmedia);
2087  av_freep(&codec);
2088  return NULL;
2089 }
2090 
2091 #define DECLARE_NDK_AMEDIACODEC_CREATE_FUNC(name, method) \
2092 static FFAMediaCodec *mediacodec_ndk_##name(const char *arg) \
2093 { \
2094  return ndk_codec_create(method, arg); \
2095 } \
2096 
2100 
2102 {
2104  int ret = 0;
2105 
2106  if (!codec)
2107  return 0;
2108 
2110 
2111  if (codec->impl && (AMediaCodec_delete(codec->impl) != AMEDIA_OK))
2113  if (codec->window)
2114  ANativeWindow_release(codec->window);
2115  if (codec->libmedia)
2116  dlclose(codec->libmedia);
2117  av_free(codec);
2118 
2119  return ret;
2120 }
2121 
2123  const FFAMediaFormat* format_ctx,
2125  void *crypto,
2126  uint32_t flags)
2127 {
2129  FFAMediaFormatNdk *format = (FFAMediaFormatNdk *)format_ctx;
2130  media_status_t status;
2131  ANativeWindow *native_window = NULL;
2132 
2133  if (window) {
2134  if (window->surface) {
2135  JNIEnv *env = NULL;
2136  JNI_GET_ENV_OR_RETURN(env, ctx, -1);
2137  native_window = ANativeWindow_fromSurface(env, window->surface);
2138  // Save for release
2139  codec->window = native_window;
2140  } else if (window->native_window) {
2141  native_window = window->native_window;
2142  }
2143  }
2144 
2145  if (format_ctx->class != &amediaformat_ndk_class) {
2146  av_log(ctx, AV_LOG_ERROR, "invalid media format\n");
2147  return AVERROR(EINVAL);
2148  }
2149 
2150  if (flags & AMEDIACODEC_CONFIGURE_FLAG_ENCODE) {
2151  if (native_window && !codec->setInputSurface) {
2152  av_log(ctx, AV_LOG_ERROR, "System doesn't support setInputSurface\n");
2153  return AVERROR_EXTERNAL;
2154  }
2155 
2156  status = AMediaCodec_configure(codec->impl, format->impl, NULL, NULL, flags);
2157  if (status != AMEDIA_OK) {
2158  av_log(codec, AV_LOG_ERROR, "Encoder configure failed, %d\n", status);
2159  return AVERROR_EXTERNAL;
2160  }
2161 
2162  if (!native_window)
2163  return 0;
2164 
2165  status = codec->setInputSurface(codec->impl, native_window);
2166  if (status != AMEDIA_OK) {
2167  av_log(codec, AV_LOG_ERROR, "Encoder set input surface failed, %d\n", status);
2168  return AVERROR_EXTERNAL;
2169  }
2170  } else {
2171  status = AMediaCodec_configure(codec->impl, format->impl, native_window, NULL, flags);
2172  if (status != AMEDIA_OK) {
2173  av_log(codec, AV_LOG_ERROR, "Decoder configure failed, %d\n", status);
2174  return AVERROR_EXTERNAL;
2175  }
2176  }
2177 
2178  return 0;
2179 }
2180 
2181 #define MEDIACODEC_NDK_WRAPPER(method) \
2182 static int mediacodec_ndk_ ## method(FFAMediaCodec* ctx) \
2183 { \
2184  FFAMediaCodecNdk *codec = (FFAMediaCodecNdk *)ctx; \
2185  media_status_t status = AMediaCodec_ ## method (codec->impl); \
2186  \
2187  if (status != AMEDIA_OK) { \
2188  av_log(codec, AV_LOG_ERROR, #method " failed, %d\n", status); \
2189  return AVERROR_EXTERNAL; \
2190  } \
2191  \
2192  return 0; \
2193 } \
2194 
2198 
2199 static uint8_t* mediacodec_ndk_getInputBuffer(FFAMediaCodec* ctx, size_t idx, size_t *out_size)
2200 {
2202  return AMediaCodec_getInputBuffer(codec->impl, idx, out_size);
2203 }
2204 
2205 static uint8_t* mediacodec_ndk_getOutputBuffer(FFAMediaCodec* ctx, size_t idx, size_t *out_size)
2206 {
2208  return AMediaCodec_getOutputBuffer(codec->impl, idx, out_size);
2209 }
2210 
2212 {
2214  return AMediaCodec_dequeueInputBuffer(codec->impl, timeoutUs);
2215 }
2216 
2218  off_t offset, size_t size,
2219  uint64_t time, uint32_t flags)
2220 {
2222  return AMediaCodec_queueInputBuffer(codec->impl, idx, offset, size, time, flags);
2223 }
2224 
2226 {
2228  AMediaCodecBufferInfo buf_info = {0};
2229  ssize_t ret;
2230 
2231  ret = AMediaCodec_dequeueOutputBuffer(codec->impl, &buf_info, timeoutUs);
2232  info->offset = buf_info.offset;
2233  info->size = buf_info.size;
2234  info->presentationTimeUs = buf_info.presentationTimeUs;
2235  info->flags = buf_info.flags;
2236 
2237  return ret;
2238 }
2239 
2241 {
2243  AMediaFormat *format = AMediaCodec_getOutputFormat(codec->impl);
2244 
2245  if (!format)
2246  return NULL;
2248 }
2249 
2250 static int mediacodec_ndk_releaseOutputBuffer(FFAMediaCodec* ctx, size_t idx, int render)
2251 {
2253  media_status_t status;
2254 
2255  status = AMediaCodec_releaseOutputBuffer(codec->impl, idx, render);
2256  if (status != AMEDIA_OK) {
2257  av_log(codec, AV_LOG_ERROR, "release output buffer failed, %d\n", status);
2258  return AVERROR_EXTERNAL;
2259  }
2260 
2261  return 0;
2262 }
2263 
2265 {
2267  media_status_t status;
2268 
2269  status = AMediaCodec_releaseOutputBufferAtTime(codec->impl, idx, timestampNs);
2270  if (status != AMEDIA_OK) {
2271  av_log(codec, AV_LOG_ERROR, "releaseOutputBufferAtTime failed, %d\n", status);
2272  return AVERROR_EXTERNAL;
2273  }
2274 
2275  return 0;
2276 }
2277 
2279 {
2280  return idx == AMEDIACODEC_INFO_TRY_AGAIN_LATER;
2281 }
2282 
2284 {
2285  return idx == AMEDIACODEC_INFO_OUTPUT_BUFFERS_CHANGED;
2286 }
2287 
2289 {
2290  return idx == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED;
2291 }
2292 
2294 {
2295  return AMEDIACODEC_BUFFER_FLAG_CODEC_CONFIG;
2296 }
2297 
2299 {
2300  return AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM;
2301 }
2302 
2304 {
2305  return 1;
2306 }
2307 
2309 {
2310  return AMEDIACODEC_CONFIGURE_FLAG_ENCODE;
2311 }
2312 
2314 {
2315  return 0;
2316 }
2317 
2319 {
2321  media_status_t status;
2322 
2323  if (!codec->signalEndOfInputStream) {
2324  av_log(codec, AV_LOG_ERROR, "signalEndOfInputStream unavailable\n");
2325  return AVERROR_EXTERNAL;
2326  }
2327 
2328  status = codec->signalEndOfInputStream(codec->impl);
2329  if (status != AMEDIA_OK) {
2330  av_log(codec, AV_LOG_ERROR, "signalEndOfInputStream failed, %d\n", status);
2331  return AVERROR_EXTERNAL;
2332  }
2333  av_log(codec, AV_LOG_DEBUG, "signalEndOfInputStream success\n");
2334 
2335  return 0;
2336 }
2337 
2338 static const FFAMediaFormat media_format_ndk = {
2340 
2341  .create = mediaformat_ndk_new,
2343 
2344  .toString = mediaformat_ndk_toString,
2345 
2346  .getInt32 = mediaformat_ndk_getInt32,
2347  .getInt64 = mediaformat_ndk_getInt64,
2348  .getFloat = mediaformat_ndk_getFloat,
2349  .getBuffer = mediaformat_ndk_getBuffer,
2350  .getString = mediaformat_ndk_getString,
2351  .getRect = mediaformat_ndk_getRect,
2352 
2353  .setInt32 = mediaformat_ndk_setInt32,
2354  .setInt64 = mediaformat_ndk_setInt64,
2355  .setFloat = mediaformat_ndk_setFloat,
2356  .setString = mediaformat_ndk_setString,
2357  .setBuffer = mediaformat_ndk_setBuffer,
2358  .setRect = mediaformat_ndk_setRect,
2359 };
2360 
2361 static const FFAMediaCodec media_codec_ndk = {
2363 
2364  .getName = mediacodec_ndk_getName,
2365 
2366  .createCodecByName = mediacodec_ndk_createCodecByName,
2367  .createDecoderByType = mediacodec_ndk_createDecoderByType,
2368  .createEncoderByType = mediacodec_ndk_createEncoderByType,
2369  .delete = mediacodec_ndk_delete,
2370 
2371  .configure = mediacodec_ndk_configure,
2372  .start = mediacodec_ndk_start,
2373  .stop = mediacodec_ndk_stop,
2374  .flush = mediacodec_ndk_flush,
2375 
2376  .getInputBuffer = mediacodec_ndk_getInputBuffer,
2377  .getOutputBuffer = mediacodec_ndk_getOutputBuffer,
2378 
2379  .dequeueInputBuffer = mediacodec_ndk_dequeueInputBuffer,
2380  .queueInputBuffer = mediacodec_ndk_queueInputBuffer,
2381 
2382  .dequeueOutputBuffer = mediacodec_ndk_dequeueOutputBuffer,
2383  .getOutputFormat = mediacodec_ndk_getOutputFormat,
2384 
2385  .releaseOutputBuffer = mediacodec_ndk_releaseOutputBuffer,
2386  .releaseOutputBufferAtTime = mediacodec_ndk_releaseOutputBufferAtTime,
2387 
2388  .infoTryAgainLater = mediacodec_ndk_infoTryAgainLater,
2389  .infoOutputBuffersChanged = mediacodec_ndk_infoOutputBuffersChanged,
2390  .infoOutputFormatChanged = mediacodec_ndk_infoOutputFormatChanged,
2391 
2392  .getBufferFlagCodecConfig = mediacodec_ndk_getBufferFlagCodecConfig,
2393  .getBufferFlagEndOfStream = mediacodec_ndk_getBufferFlagEndOfStream,
2394  .getBufferFlagKeyFrame = mediacodec_ndk_getBufferFlagKeyFrame,
2395 
2396  .getConfigureFlagEncode = mediacodec_ndk_getConfigureFlagEncode,
2397  .cleanOutputBuffers = mediacodec_ndk_cleanOutputBuffers,
2398  .signalEndOfInputStream = mediacodec_ndk_signalEndOfInputStream,
2399 };
2400 
2402 {
2403  if (ndk)
2404  return media_format_ndk.create();
2405  return media_format_jni.create();
2406 }
2407 
2409 {
2410  if (ndk)
2413 }
2414 
2415 FFAMediaCodec* ff_AMediaCodec_createDecoderByType(const char *mime_type, int ndk)
2416 {
2417  if (ndk)
2418  return media_codec_ndk.createDecoderByType(mime_type);
2419  return media_codec_jni.createDecoderByType(mime_type);
2420 }
2421 
2422 FFAMediaCodec* ff_AMediaCodec_createEncoderByType(const char *mime_type, int ndk)
2423 {
2424  if (ndk)
2425  return media_codec_ndk.createEncoderByType(mime_type);
2426  return media_codec_jni.createEncoderByType(mime_type);
2427 }
2428 
2430 {
2431  int ret = -1;
2432 
2433 #if __ANDROID_API__ >= 24
2434  // android_get_device_api_level() is a static inline before API level 29.
2435  // dlsym() might doesn't work.
2436  //
2437  // We can implement android_get_device_api_level() by
2438  // __system_property_get(), but __system_property_get() has created a lot of
2439  // troubles and is deprecated. So avoid using __system_property_get() for
2440  // now.
2441  //
2442  // Hopy we can remove the conditional compilation finally by bumping the
2443  // required API level.
2444  //
2445  ret = android_get_device_api_level();
2446 #else
2447  JNIEnv *env = NULL;
2448  jclass versionClass;
2449  jfieldID sdkIntFieldID;
2450  JNI_GET_ENV_OR_RETURN(env, avctx, -1);
2451 
2452  versionClass = (*env)->FindClass(env, "android/os/Build$VERSION");
2453  sdkIntFieldID = (*env)->GetStaticFieldID(env, versionClass, "SDK_INT", "I");
2454  ret = (*env)->GetStaticIntField(env, versionClass, sdkIntFieldID);
2455  (*env)->DeleteLocalRef(env, versionClass);
2456 #endif
2457  av_log(avctx, AV_LOG_DEBUG, "device api level %d\n", ret);
2458 
2459  return ret;
2460 }
2461 
2462 static struct {
2465 } color_range_map[] = {
2468 };
2469 
2470 static struct {
2473 } color_space_map[] = {
2478 };
2479 
2480 static struct {
2483 } color_primaries_map[] = {
2488 };
2489 
2490 static struct {
2493 } color_transfer_map[] = {
2498 };
2499 
2501 {
2502  for (int i = 0; i < FF_ARRAY_ELEMS(color_range_map); i++)
2504  return color_range_map[i].range;
2505 
2506  return AVCOL_RANGE_UNSPECIFIED;
2507 }
2508 
2510 {
2511  for (int i = 0; i < FF_ARRAY_ELEMS(color_range_map); i++)
2513  return color_range_map[i].mf_range;
2514  return COLOR_RANGE_UNSPECIFIED;
2515 }
2516 
2518 {
2519  for (int i = 0; i < FF_ARRAY_ELEMS(color_space_map); i++)
2520  if (color_space_map[i].mf_standard == color_standard)
2521  return color_space_map[i].space;
2522 
2523  return AVCOL_SPC_UNSPECIFIED;
2524 }
2525 
2527 {
2528  for (int i = 0; i < FF_ARRAY_ELEMS(color_space_map); i++)
2529  if (color_space_map[i].space == color_space)
2530  return color_space_map[i].mf_standard;
2531 
2533 }
2534 
2536 {
2537  for (int i = 0; i < FF_ARRAY_ELEMS(color_primaries_map); i++)
2538  if (color_primaries_map[i].mf_standard == color_standard)
2539  return color_primaries_map[i].primaries;
2540 
2541  return AVCOL_PRI_UNSPECIFIED;
2542 }
2543 
2546 {
2547  for (int i = 0; i < FF_ARRAY_ELEMS(color_transfer_map); i++)
2548  if (color_transfer_map[i].mf_transfer == color_transfer)
2549  return color_transfer_map[i].transfer;
2550 
2551  return AVCOL_TRC_UNSPECIFIED;
2552 }
2553 
2555  enum AVColorTransferCharacteristic color_transfer)
2556 {
2557  for (int i = 0; i < FF_ARRAY_ELEMS(color_transfer_map); i++)
2558  if (color_transfer_map[i].transfer == color_transfer)
2559  return color_transfer_map[i].mf_transfer;
2560 
2562 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
JNIAMediaCodecFields::create_by_codec_name_id
jmethodID create_by_codec_name_id
Definition: mediacodec_wrapper.c:170
JNIAMediaCodecListFields::get_codec_info_at_id
jmethodID get_codec_info_at_id
Definition: mediacodec_wrapper.c:45
FFAMediaCodecJni::jfields
struct JNIAMediaCodecFields jfields
Definition: mediacodec_wrapper.c:272
JNIAMediaCodecFields::get_output_buffer_id
jmethodID get_output_buffer_id
Definition: mediacodec_wrapper.c:190
mf_range
enum FFAMediaFormatColorRange mf_range
Definition: mediacodec_wrapper.c:2463
JNIAMediaCodecListFields::is_software_only_id
jmethodID is_software_only_id
Definition: mediacodec_wrapper.c:52
mediaformat_ndk_setRect
static void mediaformat_ndk_setRect(FFAMediaFormat *ctx, const char *name, int32_t left, int32_t top, int32_t right, int32_t bottom)
Definition: mediacodec_wrapper.c:2010
FFAMediaCodecJni::api
FFAMediaCodec api
Definition: mediacodec_wrapper.c:270
ff_AMediaCodecList_getCodecNameByType
char * ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int encoder, void *log_ctx)
Definition: mediacodec_wrapper.c:470
JNIAMediaFormatFields::init_id
jmethodID init_id
Definition: mediacodec_wrapper.c:95
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
JNIAMediaFormatFields
Definition: mediacodec_wrapper.c:91
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
mediaformat_ndk_getString
static int mediaformat_ndk_getString(FFAMediaFormat *ctx, const char *name, const char **out)
Definition: mediacodec_wrapper.c:1960
AV_PROFILE_H264_HIGH_10_INTRA
#define AV_PROFILE_H264_HIGH_10_INTRA
Definition: defs.h:116
mediacodec_ndk_getBufferFlagKeyFrame
static int mediacodec_ndk_getBufferFlagKeyFrame(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:2303
mediacodec_ndk_releaseOutputBuffer
static int mediacodec_ndk_releaseOutputBuffer(FFAMediaCodec *ctx, size_t idx, int render)
Definition: mediacodec_wrapper.c:2250
AV_PROFILE_MPEG4_ADVANCED_SIMPLE
#define AV_PROFILE_MPEG4_ADVANCED_SIMPLE
Definition: defs.h:146
amediacodec_ndk_class
static const AVClass amediacodec_ndk_class
Definition: mediacodec_wrapper.c:1863
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
mediaformat_jni_setInt32
static void mediaformat_jni_setInt32(FFAMediaFormat *ctx, const char *name, int32_t value)
Definition: mediacodec_wrapper.c:1010
AV_PROFILE_MPEG4_N_BIT
#define AV_PROFILE_MPEG4_N_BIT
Definition: defs.h:135
JNIAMediaCodecFields::size_id
jfieldID size_id
Definition: mediacodec_wrapper.c:205
AVColorTransferCharacteristic
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:580
mediacodec_jni_cleanOutputBuffers
static int mediacodec_jni_cleanOutputBuffers(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:1726
out
FILE * out
Definition: movenc.c:55
AV_PROFILE_MPEG4_SIMPLE_SCALABLE
#define AV_PROFILE_MPEG4_SIMPLE_SCALABLE
Definition: defs.h:132
ff_jni_utf_chars_to_jstring
jstring ff_jni_utf_chars_to_jstring(JNIEnv *env, const char *utf_chars, void *log_ctx)
Definition: ffjni.c:129
FFAMediaFormatNdk::impl
AMediaFormat * impl
Definition: mediacodec_wrapper.c:1830
mediacodec_ndk_infoOutputBuffersChanged
static int mediacodec_ndk_infoOutputBuffersChanged(FFAMediaCodec *ctx, ssize_t idx)
Definition: mediacodec_wrapper.c:2283
AVCOL_TRC_LINEAR
@ AVCOL_TRC_LINEAR
"Linear transfer characteristics"
Definition: pixfmt.h:589
AV_PROFILE_H264_MAIN
#define AV_PROFILE_H264_MAIN
Definition: defs.h:112
ff_AMediaFormatColorStandard_to_AVColorSpace
enum AVColorSpace ff_AMediaFormatColorStandard_to_AVColorSpace(int color_standard)
Map MediaFormat color standard to AVColorSpace.
Definition: mediacodec_wrapper.c:2517
mediacodec_ndk_getInputBuffer
static uint8_t * mediacodec_ndk_getInputBuffer(FFAMediaCodec *ctx, size_t idx, size_t *out_size)
Definition: mediacodec_wrapper.c:2199
mediaformat_jni_delete
static int mediaformat_jni_delete(FFAMediaFormat *ctx)
Definition: mediacodec_wrapper.c:756
JNIAMediaCodecFields::flush_id
jmethodID flush_id
Definition: mediacodec_wrapper.c:178
JNIAMediaCodecFields::get_name_id
jmethodID get_name_id
Definition: mediacodec_wrapper.c:174
JNIAMediaFormatFields::get_float_id
jmethodID get_float_id
Definition: mediacodec_wrapper.c:101
mediaformat_jni_new
static FFAMediaFormat * mediaformat_jni_new(void)
Definition: mediacodec_wrapper.c:678
int64_t
long long int64_t
Definition: coverity.c:34
av_strcasecmp
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:207
AV_PROFILE_VP9_1
#define AV_PROFILE_VP9_1
Definition: defs.h:155
AV_PROFILE_HEVC_MAIN
#define AV_PROFILE_HEVC_MAIN
Definition: defs.h:159
out_size
int out_size
Definition: movenc.c:56
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
JNIAMediaCodecListFields::mediacodec_list_class
jclass mediacodec_list_class
Definition: mediacodec_wrapper.c:40
JNIAMediaCodecFields::mediainfo_class
jclass mediainfo_class
Definition: mediacodec_wrapper.c:198
jni_amediacodec_mapping
static const struct FFJniField jni_amediacodec_mapping[]
Definition: mediacodec_wrapper.c:210
mediacodec_jni_queueInputBuffer
static int mediacodec_jni_queueInputBuffer(FFAMediaCodec *ctx, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags)
Definition: mediacodec_wrapper.c:1522
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:686
mediaformat_jni_setBuffer
static void mediaformat_jni_setBuffer(FFAMediaFormat *ctx, const char *name, void *data, size_t size)
Definition: mediacodec_wrapper.c:1113
ff_jni_reset_jfields
int ff_jni_reset_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx)
Definition: ffjni.c:368
JNIAMediaCodecListFields::get_supported_types_id
jmethodID get_supported_types_id
Definition: mediacodec_wrapper.c:50
AVCOL_TRC_UNSPECIFIED
@ AVCOL_TRC_UNSPECIFIED
Definition: pixfmt.h:583
data
const char data[16]
Definition: mxf.c:148
AV_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE
#define AV_PROFILE_MPEG4_BASIC_ANIMATED_TEXTURE
Definition: defs.h:138
FFAMediaFormatNdk
Definition: mediacodec_wrapper.c:1826
amediacodec_class
static const AVClass amediacodec_class
Definition: mediacodec_wrapper.c:263
COLOR_TRANSFER_LINEAR
@ COLOR_TRANSFER_LINEAR
Definition: mediacodec_wrapper.h:364
JNIAMediaCodecFields::info_try_again_later_id
jfieldID info_try_again_later_id
Definition: mediacodec_wrapper.c:160
mediacodec_jni_infoTryAgainLater
static int mediacodec_jni_infoTryAgainLater(FFAMediaCodec *ctx, ssize_t idx)
Definition: mediacodec_wrapper.c:1684
mediacodec_jni_getBufferFlagEndOfStream
static int mediacodec_jni_getBufferFlagEndOfStream(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:1708
AVColorPrimaries
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:555
mediacodec_ndk_getConfigureFlagEncode
static int mediacodec_ndk_getConfigureFlagEncode(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:2308
mediacodec_jni_start
static int mediacodec_jni_start(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:1414
AV_PROFILE_AV1_PROFESSIONAL
#define AV_PROFILE_AV1_PROFESSIONAL
Definition: defs.h:170
mediaformat_jni_getString
static int mediaformat_jni_getString(FFAMediaFormat *ctx, const char *name, const char **out)
Definition: mediacodec_wrapper.c:965
mf_standard
enum FFAMediaFormatColorStandard mf_standard
Definition: mediacodec_wrapper.c:2471
AV_PROFILE_MPEG4_CORE_SCALABLE
#define AV_PROFILE_MPEG4_CORE_SCALABLE
Definition: defs.h:141
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
mediaformat_jni_getInt64
static int mediaformat_jni_getInt64(FFAMediaFormat *ctx, const char *name, int64_t *out)
Definition: mediacodec_wrapper.c:838
ff_jni_init_jfields
int ff_jni_init_jfields(JNIEnv *env, void *jfields, const struct FFJniField *jfields_mapping, int global, void *log_ctx)
Definition: ffjni.c:279
FFAMediaCodecNdk
Definition: mediacodec_wrapper.c:1838
JNIAMediaCodecFields::presentation_time_us_id
jfieldID presentation_time_us_id
Definition: mediacodec_wrapper.c:204
AV_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE
#define AV_PROFILE_MPEG4_ADVANCED_SCALABLE_TEXTURE
Definition: defs.h:144
JNIAMediaCodecListFields::get_name_id
jmethodID get_name_id
Definition: mediacodec_wrapper.c:48
mediacodec_ndk_cleanOutputBuffers
static int mediacodec_ndk_cleanOutputBuffers(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:2313
mediaformat_jni_getInt32
static int mediaformat_jni_getInt32(FFAMediaFormat *ctx, const char *name, int32_t *out)
Definition: mediacodec_wrapper.c:801
AV_PROFILE_H264_EXTENDED
#define AV_PROFILE_H264_EXTENDED
Definition: defs.h:113
mediacodec_jni_getOutputFormat
static FFAMediaFormat * mediacodec_jni_getOutputFormat(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:1662
primaries
enum AVColorPrimaries primaries
Definition: mediacodec_wrapper.c:2482
AVCOL_SPC_BT470BG
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition: pixfmt.h:615
JNIAMediaCodecListFields::is_encoder_id
jmethodID is_encoder_id
Definition: mediacodec_wrapper.c:51
window
static SDL_Window * window
Definition: ffplay.c:361
mediacodec_jni_delete
static int mediacodec_jni_delete(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:1311
JNIAMediaCodecListFields::get_codec_count_id
jmethodID get_codec_count_id
Definition: mediacodec_wrapper.c:44
jni_amediacodeclist_mapping
static const struct FFJniField jni_amediacodeclist_mapping[]
Definition: mediacodec_wrapper.c:64
FFAMediaCodecNdk::libmedia
void * libmedia
Definition: mediacodec_wrapper.c:1841
JNIAMediaFormatFields::set_long_id
jmethodID set_long_id
Definition: mediacodec_wrapper.c:106
mediaformat_ndk_getInt32
static int mediaformat_ndk_getInt32(FFAMediaFormat *ctx, const char *name, int32_t *out)
Definition: mediacodec_wrapper.c:1936
fail
#define fail()
Definition: checkasm.h:186
mediaformat_jni_setString
static void mediaformat_jni_setString(FFAMediaFormat *ctx, const char *name, const char *value)
Definition: mediacodec_wrapper.c:1082
JNIAMediaCodecFields::set_input_surface_id
jmethodID set_input_surface_id
Definition: mediacodec_wrapper.c:195
mediacodec_ndk_infoTryAgainLater
static int mediacodec_ndk_infoTryAgainLater(FFAMediaCodec *ctx, ssize_t idx)
Definition: mediacodec_wrapper.c:2278
AV_PROFILE_MPEG4_SCALABLE_TEXTURE
#define AV_PROFILE_MPEG4_SCALABLE_TEXTURE
Definition: defs.h:136
FFAMediaCodecJni::INFO_OUTPUT_FORMAT_CHANGED
int INFO_OUTPUT_FORMAT_CHANGED
Definition: mediacodec_wrapper.c:282
JNIAMediaFormatFields::set_float_id
jmethodID set_float_id
Definition: mediacodec_wrapper.c:107
JNIAMediaCodecListFields::mediacodec_info_class
jclass mediacodec_info_class
Definition: mediacodec_wrapper.c:47
mediacodec_jni_releaseOutputBufferAtTime
static int mediacodec_jni_releaseOutputBufferAtTime(FFAMediaCodec *ctx, size_t idx, int64_t timestampNs)
Definition: mediacodec_wrapper.c:1486
JNIAMediaFormatFields::to_string_id
jmethodID to_string_id
Definition: mediacodec_wrapper.c:111
FF_JNI_CLASS
@ FF_JNI_CLASS
Definition: ffjni.h:91
COLOR_RANGE_UNSPECIFIED
@ COLOR_RANGE_UNSPECIFIED
Definition: mediacodec_wrapper.h:349
AV_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION
#define AV_PROFILE_MPEG4_SIMPLE_FACE_ANIMATION
Definition: defs.h:137
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
JNIAMediaCodecFields::release_id
jmethodID release_id
Definition: mediacodec_wrapper.c:180
FFAMediaFormatJni::api
FFAMediaFormat api
Definition: mediacodec_wrapper.c:148
COLOR_TRANSFER_HLG
@ COLOR_TRANSFER_HLG
Definition: mediacodec_wrapper.h:367
AV_PROFILE_VP9_3
#define AV_PROFILE_VP9_3
Definition: defs.h:157
JNIAMediaCodecFields::get_output_buffers_id
jmethodID get_output_buffers_id
Definition: mediacodec_wrapper.c:191
JNIAMediaCodecFields::release_output_buffer_at_time_id
jmethodID release_output_buffer_at_time_id
Definition: mediacodec_wrapper.c:193
ff_AMediaFormat_new
FFAMediaFormat * ff_AMediaFormat_new(int ndk)
Definition: mediacodec_wrapper.c:2401
mediacodec_jni_dequeueOutputBuffer
static ssize_t mediacodec_jni_dequeueOutputBuffer(FFAMediaCodec *ctx, FFAMediaCodecBufferInfo *info, int64_t timeoutUs)
Definition: mediacodec_wrapper.c:1540
CREATE_CODEC_BY_NAME
#define CREATE_CODEC_BY_NAME
Definition: mediacodec_wrapper.c:1210
JNIAMediaCodecFields::buffer_flag_codec_config_id
jfieldID buffer_flag_codec_config_id
Definition: mediacodec_wrapper.c:164
FFAMediaFormat::delete
int(* delete)(FFAMediaFormat *)
Definition: mediacodec_wrapper.h:67
mediacodec_ndk_getName
static char * mediacodec_ndk_getName(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:2021
avassert.h
mediaformat_ndk_getInt64
static int mediaformat_ndk_getInt64(FFAMediaFormat *ctx, const char *name, int64_t *out)
Definition: mediacodec_wrapper.c:1942
description
Tag description
Definition: snow.txt:206
mediacodec_jni_infoOutputBuffersChanged
static int mediacodec_jni_infoOutputBuffersChanged(FFAMediaCodec *ctx, ssize_t idx)
Definition: mediacodec_wrapper.c:1690
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
mediacodec_ndk_queueInputBuffer
static int mediacodec_ndk_queueInputBuffer(FFAMediaCodec *ctx, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags)
Definition: mediacodec_wrapper.c:2217
FFAMediaCodecNdk::signalEndOfInputStream
media_status_t(* signalEndOfInputStream)(AMediaCodec *)
Definition: mediacodec_wrapper.c:1851
JNIAMediaCodecListFields::profile_id
jfieldID profile_id
Definition: mediacodec_wrapper.c:59
FFAMediaCodecJni::output_buffers
jobject output_buffers
Definition: mediacodec_wrapper.c:278
ff_Build_SDK_INT
int ff_Build_SDK_INT(AVCodecContext *avctx)
Definition: mediacodec_wrapper.c:2429
JNIAMediaCodecListFields::codec_profile_level_class
jclass codec_profile_level_class
Definition: mediacodec_wrapper.c:58
mediacodec_ndk_getBufferFlagCodecConfig
static int mediacodec_ndk_getBufferFlagCodecConfig(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:2293
mediaformat_jni_setInt64
static void mediaformat_jni_setInt64(FFAMediaFormat *ctx, const char *name, int64_t value)
Definition: mediacodec_wrapper.c:1034
JNIAMediaCodecFields::create_decoder_by_type_id
jmethodID create_decoder_by_type_id
Definition: mediacodec_wrapper.c:171
format
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
AV_PROFILE_MPEG4_HYBRID
#define AV_PROFILE_MPEG4_HYBRID
Definition: defs.h:139
JNIAMediaCodecFields::get_output_format_id
jmethodID get_output_format_id
Definition: mediacodec_wrapper.c:182
AVCOL_SPC_SMPTE170M
@ AVCOL_SPC_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
Definition: pixfmt.h:616
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:220
mediacodec_ndk_dequeueInputBuffer
static ssize_t mediacodec_ndk_dequeueInputBuffer(FFAMediaCodec *ctx, int64_t timeoutUs)
Definition: mediacodec_wrapper.c:2211
JNI_GET_ENV_OR_RETURN_VOID
#define JNI_GET_ENV_OR_RETURN_VOID(env, log_ctx)
Definition: mediacodec_wrapper.c:302
AV_PROFILE_H264_HIGH_10
#define AV_PROFILE_H264_HIGH_10
Definition: defs.h:115
info
MIPS optimizations info
Definition: mips.txt:2
JNIAMediaCodecFields::get_input_buffer_id
jmethodID get_input_buffer_id
Definition: mediacodec_wrapper.c:186
ff_jni_jstring_to_utf_chars
char * ff_jni_jstring_to_utf_chars(JNIEnv *env, jstring string, void *log_ctx)
Definition: ffjni.c:99
FFAMediaCodecNdk::setInputSurface
media_status_t(* setInputSurface)(AMediaCodec *, ANativeWindow *)
Definition: mediacodec_wrapper.c:1850
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
JNIAMediaCodecFields::offset_id
jfieldID offset_id
Definition: mediacodec_wrapper.c:203
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:49
AV_PROFILE_H264_HIGH_422_INTRA
#define AV_PROFILE_H264_HIGH_422_INTRA
Definition: defs.h:119
mediaformat_jni_newFromObject
static FFAMediaFormat * mediaformat_jni_newFromObject(void *object)
Definition: mediacodec_wrapper.c:721
JNIAMediaCodecListFields::find_decoder_for_format_id
jmethodID find_decoder_for_format_id
Definition: mediacodec_wrapper.c:42
mediaformat_ndk_getFloat
static int mediaformat_ndk_getFloat(FFAMediaFormat *ctx, const char *name, float *out)
Definition: mediacodec_wrapper.c:1948
key
const char * key
Definition: hwcontext_opencl.c:189
color_range
color_range
Definition: vf_selectivecolor.c:43
JNIAMediaCodecFields::dequeue_input_buffer_id
jmethodID dequeue_input_buffer_id
Definition: mediacodec_wrapper.c:184
mediacodec_jni_getBufferFlagKeyFrame
static int mediacodec_jni_getBufferFlagKeyFrame(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:1714
mf_transfer
enum FFAMediaFormatColorTransfer mf_transfer
Definition: mediacodec_wrapper.c:2491
FFAMediaCodecJni::CONFIGURE_FLAG_ENCODE
int CONFIGURE_FLAG_ENCODE
Definition: mediacodec_wrapper.c:288
AVCOL_PRI_UNSPECIFIED
@ AVCOL_PRI_UNSPECIFIED
Definition: pixfmt.h:558
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
AVCOL_PRI_BT470BG
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:562
AV_PROFILE_H264_HIGH_422
#define AV_PROFILE_H264_HIGH_422
Definition: defs.h:118
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:455
arg
const char * arg
Definition: jacosubdec.c:67
AVCOL_PRI_SMPTE170M
@ AVCOL_PRI_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:563
COLOR_RANGE_LIMITED
@ COLOR_RANGE_LIMITED
Definition: mediacodec_wrapper.h:351
mediaformat_ndk_setBuffer
static void mediaformat_ndk_setBuffer(FFAMediaFormat *ctx, const char *name, void *data, size_t size)
Definition: mediacodec_wrapper.c:2004
ff_AMediaFormatColorStandard_from_AVColorSpace
int ff_AMediaFormatColorStandard_from_AVColorSpace(enum AVColorSpace color_space)
Map AVColorSpace to MediaFormat color standard.
Definition: mediacodec_wrapper.c:2526
JNIAMediaCodecFields::queue_input_buffer_id
jmethodID queue_input_buffer_id
Definition: mediacodec_wrapper.c:185
mediaformat_ndk_setInt32
static void mediaformat_ndk_setInt32(FFAMediaFormat *ctx, const char *name, int32_t value)
Definition: mediacodec_wrapper.c:1980
JNIAMediaFormatFields::get_string_id
jmethodID get_string_id
Definition: mediacodec_wrapper.c:103
GET_SYMBOL
#define GET_SYMBOL(sym)
mediaformat_ndk_getRect
static int mediaformat_ndk_getRect(FFAMediaFormat *ctx, const char *name, int32_t *left, int32_t *top, int32_t *right, int32_t *bottom)
Definition: mediacodec_wrapper.c:1971
JNIAMediaCodecListFields
Definition: mediacodec_wrapper.c:38
jni_amediaformat_mapping
static const struct FFJniField jni_amediaformat_mapping[]
Definition: mediacodec_wrapper.c:116
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
JNIAMediaCodecFields::create_encoder_by_type_id
jmethodID create_encoder_by_type_id
Definition: mediacodec_wrapper.c:172
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
ff_AMediaCodec_createCodecByName
FFAMediaCodec * ff_AMediaCodec_createCodecByName(const char *name, int ndk)
Definition: mediacodec_wrapper.c:2408
ff_AMediaFormatColorRange_from_AVColorRange
int ff_AMediaFormatColorRange_from_AVColorRange(enum AVColorRange color_range)
Map AVColorRange to MediaFormat color range.
Definition: mediacodec_wrapper.c:2509
FF_JNI_METHOD
@ FF_JNI_METHOD
Definition: ffjni.h:94
JNIAMediaCodecFields::stop_id
jmethodID stop_id
Definition: mediacodec_wrapper.c:179
NULL
#define NULL
Definition: coverity.c:32
mediacodec_ndk_getBufferFlagEndOfStream
static int mediacodec_ndk_getBufferFlagEndOfStream(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:2298
FFAMediaCodecJni::input_buffers
jobject input_buffers
Definition: mediacodec_wrapper.c:277
JNIAMediaCodecFields::release_output_buffer_id
jmethodID release_output_buffer_id
Definition: mediacodec_wrapper.c:192
FFAMediaCodecBufferInfo
Definition: mediacodec_wrapper.h:172
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:280
FFAMediaCodecJni::has_get_i_o_buffer
int has_get_i_o_buffer
Definition: mediacodec_wrapper.c:290
FFAMediaCodecJni::BUFFER_FLAG_KEY_FRAME
int BUFFER_FLAG_KEY_FRAME
Definition: mediacodec_wrapper.c:286
FFJniField
Definition: ffjni.h:103
AV_PROFILE_HEVC_MAIN_STILL_PICTURE
#define AV_PROFILE_HEVC_MAIN_STILL_PICTURE
Definition: defs.h:161
GET_OPTIONAL_SYMBOL
#define GET_OPTIONAL_SYMBOL(sym)
media_codec_ndk
static const FFAMediaCodec media_codec_ndk
Definition: mediacodec_wrapper.c:1855
AVCOL_PRI_BT709
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition: pixfmt.h:557
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
FFAMediaCodecNdk::api
FFAMediaCodec api
Definition: mediacodec_wrapper.c:1839
mediacodec_jni_releaseOutputBuffer
static int mediacodec_jni_releaseOutputBuffer(FFAMediaCodec *ctx, size_t idx, int render)
Definition: mediacodec_wrapper.c:1468
ff_AMediaCodec_createEncoderByType
FFAMediaCodec * ff_AMediaCodec_createEncoderByType(const char *mime_type, int ndk)
Definition: mediacodec_wrapper.c:2422
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts.c:368
COLOR_STANDARD_BT709
@ COLOR_STANDARD_BT709
Definition: mediacodec_wrapper.h:356
mediacodec_jni_getName
static char * mediacodec_jni_getName(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:1347
JNIAMediaCodecFields::get_input_buffers_id
jmethodID get_input_buffers_id
Definition: mediacodec_wrapper.c:187
FFAMediaCodecJni::INFO_OUTPUT_BUFFERS_CHANGED
int INFO_OUTPUT_BUFFERS_CHANGED
Definition: mediacodec_wrapper.c:281
JNIAMediaCodecFields::buffer_flag_end_of_stream_id
jfieldID buffer_flag_end_of_stream_id
Definition: mediacodec_wrapper.c:165
JNIAMediaCodecFields::info_output_format_changed_id
jfieldID info_output_format_changed_id
Definition: mediacodec_wrapper.c:162
mediaformat_ndk_getBuffer
static int mediaformat_ndk_getBuffer(FFAMediaFormat *ctx, const char *name, void **data, size_t *size)
Definition: mediacodec_wrapper.c:1954
DECLARE_FF_AMEDIACODEC_CREATE_FUNC
#define DECLARE_FF_AMEDIACODEC_CREATE_FUNC(name, method)
Definition: mediacodec_wrapper.c:1301
JNIAMediaCodecFields::init_id
jmethodID init_id
Definition: mediacodec_wrapper.c:200
mediacodec_jni_dequeueInputBuffer
static ssize_t mediacodec_jni_dequeueInputBuffer(FFAMediaCodec *ctx, int64_t timeoutUs)
Definition: mediacodec_wrapper.c:1504
AV_PROFILE_HEVC_MAIN_10
#define AV_PROFILE_HEVC_MAIN_10
Definition: defs.h:160
AVCOL_RANGE_UNSPECIFIED
@ AVCOL_RANGE_UNSPECIFIED
Definition: pixfmt.h:652
AVCOL_PRI_BT2020
@ AVCOL_PRI_BT2020
ITU-R BT2020.
Definition: pixfmt.h:566
JNIAMediaCodecListFields::get_codec_capabilities_id
jmethodID get_codec_capabilities_id
Definition: mediacodec_wrapper.c:49
FFAMediaCodecNdk::getName
media_status_t(* getName)(AMediaCodec *, char **out_name)
Definition: mediacodec_wrapper.c:1846
FFAMediaFormatNdk::getRect
bool(* getRect)(AMediaFormat *, const char *name, int32_t *left, int32_t *top, int32_t *right, int32_t *bottom)
Definition: mediacodec_wrapper.c:1832
color_space_map
static struct @157 color_space_map[]
JNIAMediaCodecFields
Definition: mediacodec_wrapper.c:156
mediaformat_jni_getFloat
static int mediaformat_jni_getFloat(FFAMediaFormat *ctx, const char *name, float *out)
Definition: mediacodec_wrapper.c:875
AV_PROFILE_MPEG4_ADVANCED_CORE
#define AV_PROFILE_MPEG4_ADVANCED_CORE
Definition: defs.h:143
ff_AMediaFormatColorStandard_to_AVColorPrimaries
enum AVColorPrimaries ff_AMediaFormatColorStandard_to_AVColorPrimaries(int color_standard)
Map MediaFormat color standard to AVColorPrimaries.
Definition: mediacodec_wrapper.c:2535
JNIAMediaCodecFields::configure_id
jmethodID configure_id
Definition: mediacodec_wrapper.c:176
JNIAMediaCodecFields::configure_flag_encode_id
jfieldID configure_flag_encode_id
Definition: mediacodec_wrapper.c:168
JNIAMediaFormatFields::mediaformat_class
jclass mediaformat_class
Definition: mediacodec_wrapper.c:93
AVCOL_TRC_SMPTEST2084
@ AVCOL_TRC_SMPTEST2084
Definition: pixfmt.h:598
media_format_jni
static const FFAMediaFormat media_format_jni
Definition: mediacodec_wrapper.c:154
FFAMediaCodec::createDecoderByType
FFAMediaCodec *(* createDecoderByType)(const char *mime_type)
Definition: mediacodec_wrapper.h:187
COLOR_TRANSFER_SDR_VIDEO
@ COLOR_TRANSFER_SDR_VIDEO
Definition: mediacodec_wrapper.h:365
FFAMediaCodec::createCodecByName
FFAMediaCodec *(* createCodecByName)(const char *name)
Definition: mediacodec_wrapper.h:186
COLOR_TRANSFER_UNSPECIFIED
@ COLOR_TRANSFER_UNSPECIFIED
Definition: mediacodec_wrapper.h:363
size
int size
Definition: twinvq_data.h:10344
JNIAMediaFormatFields::get_long_id
jmethodID get_long_id
Definition: mediacodec_wrapper.c:100
media_format_ndk
static const FFAMediaFormat media_format_ndk
Definition: mediacodec_wrapper.c:1854
ff_AMediaFormatColorTransfer_from_AVColorTransfer
int ff_AMediaFormatColorTransfer_from_AVColorTransfer(enum AVColorTransferCharacteristic color_transfer)
Map AVColorTransferCharacteristic to MediaFormat color transfer.
Definition: mediacodec_wrapper.c:2554
CREATE_ENCODER_BY_TYPE
#define CREATE_ENCODER_BY_TYPE
Definition: mediacodec_wrapper.c:1212
JNIAMediaFormatFields::set_bytebuffer_id
jmethodID set_bytebuffer_id
Definition: mediacodec_wrapper.c:108
DECLARE_NDK_AMEDIACODEC_CREATE_FUNC
#define DECLARE_NDK_AMEDIACODEC_CREATE_FUNC(name, method)
Definition: mediacodec_wrapper.c:2091
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2464
mediacodec_ndk_dequeueOutputBuffer
static ssize_t mediacodec_ndk_dequeueOutputBuffer(FFAMediaCodec *ctx, FFAMediaCodecBufferInfo *info, int64_t timeoutUs)
Definition: mediacodec_wrapper.c:2225
AV_PROFILE_MPEG4_ADVANCED_CODING
#define AV_PROFILE_MPEG4_ADVANCED_CODING
Definition: defs.h:142
JNIAMediaCodecFields::start_id
jmethodID start_id
Definition: mediacodec_wrapper.c:177
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
ff_AMediaCodecProfile_getProfileFromAVCodecContext
int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx)
The following API around MediaCodec and MediaFormat is based on the NDK one provided by Google since ...
Definition: mediacodec_wrapper.c:309
COLOR_RANGE_FULL
@ COLOR_RANGE_FULL
Definition: mediacodec_wrapper.h:350
mediacodec_jni_infoOutputFormatChanged
static int mediacodec_jni_infoOutputFormatChanged(FFAMediaCodec *ctx, ssize_t idx)
Definition: mediacodec_wrapper.c:1696
JNIAMediaFormatFields::get_integer_id
jmethodID get_integer_id
Definition: mediacodec_wrapper.c:99
AV_PROFILE_AV1_HIGH
#define AV_PROFILE_AV1_HIGH
Definition: defs.h:169
space
enum AVColorSpace space
Definition: mediacodec_wrapper.c:2472
amediaformat_ndk_class
static const AVClass amediaformat_ndk_class
Definition: mediacodec_wrapper.c:1857
ff_AMediaCodec_createDecoderByType
FFAMediaCodec * ff_AMediaCodec_createDecoderByType(const char *mime_type, int ndk)
Definition: mediacodec_wrapper.c:2415
FFAMediaCodecNdk::impl
AMediaCodec * impl
Definition: mediacodec_wrapper.c:1842
COLOR_TRANSFER_ST2084
@ COLOR_TRANSFER_ST2084
Definition: mediacodec_wrapper.h:366
FFAMediaCodec::class
const AVClass * class
Definition: mediacodec_wrapper.h:182
mediaformat_ndk_new
static FFAMediaFormat * mediaformat_ndk_new(void)
Definition: mediacodec_wrapper.c:1906
JNIAMediaCodecListFields::color_formats_id
jfieldID color_formats_id
Definition: mediacodec_wrapper.c:55
CREATE_DECODER_BY_TYPE
#define CREATE_DECODER_BY_TYPE
Definition: mediacodec_wrapper.c:1211
JNIAMediaFormatFields::set_integer_id
jmethodID set_integer_id
Definition: mediacodec_wrapper.c:105
AV_PROFILE_MPEG4_SIMPLE
#define AV_PROFILE_MPEG4_SIMPLE
Definition: defs.h:131
FF_JNI_FIELD
@ FF_JNI_FIELD
Definition: ffjni.h:92
mediaformat_ndk_create
static FFAMediaFormat * mediaformat_ndk_create(AMediaFormat *impl)
Definition: mediacodec_wrapper.c:1869
mediacodec_jni_flush
static int mediacodec_jni_flush(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:1450
buffer_data
Definition: avio_read_callback.c:37
OFFSET
#define OFFSET(x)
Definition: mediacodec_wrapper.c:209
COLOR_STANDARD_BT2020
@ COLOR_STANDARD_BT2020
Definition: mediacodec_wrapper.h:359
FFANativeWindow
Definition: mediacodec_surface.h:28
ff_jni_exception_check
int ff_jni_exception_check(JNIEnv *env, int log, void *log_ctx)
Definition: ffjni.c:246
ff_AMediaFormatColorTransfer_to_AVColorTransfer
enum AVColorTransferCharacteristic ff_AMediaFormatColorTransfer_to_AVColorTransfer(int color_transfer)
Map MediaFormat color transfer to AVColorTransferCharacteristic.
Definition: mediacodec_wrapper.c:2545
mediacodec_ndk_delete
static int mediacodec_ndk_delete(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:2101
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
mediacodec_wrapper.h
COLOR_STANDARD_UNSPECIFIED
@ COLOR_STANDARD_UNSPECIFIED
Definition: mediacodec_wrapper.h:355
AVCOL_SPC_BT2020_NCL
@ AVCOL_SPC_BT2020_NCL
ITU-R BT2020 non-constant luminance system.
Definition: pixfmt.h:620
ffjni.h
FFAMediaCodec
Definition: mediacodec_wrapper.h:181
transfer
enum AVColorTransferCharacteristic transfer
Definition: mediacodec_wrapper.c:2492
mediacodec_ndk_getOutputFormat
static FFAMediaFormat * mediacodec_ndk_getOutputFormat(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:2240
mediacodec_ndk_signalEndOfInputStream
static int mediacodec_ndk_signalEndOfInputStream(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:2318
AVColorSpace
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:609
FFAMediaCodecNdk::window
ANativeWindow * window
Definition: mediacodec_wrapper.c:1843
FFAMediaFormatJni::jfields
struct JNIAMediaFormatFields jfields
Definition: mediacodec_wrapper.c:150
JNIAMediaCodecFields::buffer_flag_key_frame_id
jfieldID buffer_flag_key_frame_id
Definition: mediacodec_wrapper.c:166
JNIAMediaCodecFields::signal_end_of_input_stream_id
jmethodID signal_end_of_input_stream_id
Definition: mediacodec_wrapper.c:196
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:226
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
FFAMediaCodecJni::BUFFER_FLAG_END_OF_STREAM
int BUFFER_FLAG_END_OF_STREAM
Definition: mediacodec_wrapper.c:285
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
AV_PROFILE_MPEG4_MAIN
#define AV_PROFILE_MPEG4_MAIN
Definition: defs.h:134
FFAMediaCodecJni::INFO_TRY_AGAIN_LATER
int INFO_TRY_AGAIN_LATER
Definition: mediacodec_wrapper.c:280
color_transfer_map
static struct @159 color_transfer_map[]
AV_PROFILE_MPEG4_SIMPLE_STUDIO
#define AV_PROFILE_MPEG4_SIMPLE_STUDIO
Definition: defs.h:145
profile
int profile
Definition: mxfenc.c:2228
AVCOL_SPC_UNSPECIFIED
@ AVCOL_SPC_UNSPECIFIED
Definition: pixfmt.h:612
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:669
COLOR_STANDARD_BT601_PAL
@ COLOR_STANDARD_BT601_PAL
Definition: mediacodec_wrapper.h:357
FFAMediaCodec::createEncoderByType
FFAMediaCodec *(* createEncoderByType)(const char *mime_type)
Definition: mediacodec_wrapper.h:188
avcodec.h
FFAMediaFormatColorStandard
FFAMediaFormatColorStandard
Definition: mediacodec_wrapper.h:354
mediaformat_ndk_setFloat
static void mediaformat_ndk_setFloat(FFAMediaFormat *ctx, const char *name, float value)
Definition: mediacodec_wrapper.c:1992
JNIAMediaCodecFields::mediacodec_class
jclass mediacodec_class
Definition: mediacodec_wrapper.c:158
ff_AMediaFormatColorRange_to_AVColorRange
enum AVColorRange ff_AMediaFormatColorRange_to_AVColorRange(int color_range)
Map MediaFormat color range to AVColorRange.
Definition: mediacodec_wrapper.c:2500
AV_PROFILE_H264_HIGH_444_PREDICTIVE
#define AV_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: defs.h:122
color_range_map
static struct @156 color_range_map[]
AV_PROFILE_VP9_2
#define AV_PROFILE_VP9_2
Definition: defs.h:156
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
FFAMediaFormat::create
FFAMediaFormat *(* create)(void)
Definition: mediacodec_wrapper.h:66
codec_init_static_fields
static int codec_init_static_fields(FFAMediaCodecJni *codec)
Definition: mediacodec_wrapper.c:1156
mediacodec_jni_getBufferFlagCodecConfig
static int mediacodec_jni_getBufferFlagCodecConfig(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:1702
AV_PROFILE_H264_BASELINE
#define AV_PROFILE_H264_BASELINE
Definition: defs.h:110
AV_PROFILE_MPEG4_CORE
#define AV_PROFILE_MPEG4_CORE
Definition: defs.h:133
JNIAMediaFormatFields::get_bytebuffer_id
jmethodID get_bytebuffer_id
Definition: mediacodec_wrapper.c:102
FF_JNI_STATIC_FIELD
@ FF_JNI_STATIC_FIELD
Definition: ffjni.h:93
FFAMediaFormatColorRange
FFAMediaFormatColorRange
Definition: mediacodec_wrapper.h:348
FFAMediaFormatNdk::setRect
void(* setRect)(AMediaFormat *, const char *name, int32_t left, int32_t top, int32_t right, int32_t bottom)
Definition: mediacodec_wrapper.c:1834
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
mediacodec_jni_getInputBuffer
static uint8_t * mediacodec_jni_getInputBuffer(FFAMediaCodec *ctx, size_t idx, size_t *out_size)
Definition: mediacodec_wrapper.c:1576
FFAMediaCodecJni::buffer_info
jobject buffer_info
Definition: mediacodec_wrapper.c:275
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVCOL_TRC_ARIB_STD_B67
@ AVCOL_TRC_ARIB_STD_B67
ARIB STD-B67, known as "Hybrid log-gamma".
Definition: pixfmt.h:601
AV_PROFILE_H264_HIGH
#define AV_PROFILE_H264_HIGH
Definition: defs.h:114
status
ov_status_e status
Definition: dnn_backend_openvino.c:101
mediacodec_jni_getConfigureFlagEncode
static int mediacodec_jni_getConfigureFlagEncode(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:1720
JNI_GET_ENV_OR_RETURN
#define JNI_GET_ENV_OR_RETURN(env, log_ctx, ret)
Definition: mediacodec_wrapper.c:295
JNIAMediaCodecListFields::profile_levels_id
jfieldID profile_levels_id
Definition: mediacodec_wrapper.c:56
FFAMediaFormatColorTransfer
FFAMediaFormatColorTransfer
Definition: mediacodec_wrapper.h:362
FFAMediaCodecJni::object
jobject object
Definition: mediacodec_wrapper.c:274
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1640
AV_PROFILE_MPEG4_ADVANCED_REAL_TIME
#define AV_PROFILE_MPEG4_ADVANCED_REAL_TIME
Definition: defs.h:140
mediacodec_ndk_getOutputBuffer
static uint8_t * mediacodec_ndk_getOutputBuffer(FFAMediaCodec *ctx, size_t idx, size_t *out_size)
Definition: mediacodec_wrapper.c:2205
mediacodec_jni_signalEndOfInputStream
static int mediacodec_jni_signalEndOfInputStream(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:1750
FFAMediaFormatNdk::libmedia
void * libmedia
Definition: mediacodec_wrapper.c:1829
AVCOL_TRC_SMPTE170M
@ AVCOL_TRC_SMPTE170M
also ITU-R BT601-6 525 or 625 / ITU-R BT1358 525 or 625 / ITU-R BT1700 NTSC
Definition: pixfmt.h:587
AV_PROFILE_H264_CONSTRAINED_BASELINE
#define AV_PROFILE_H264_CONSTRAINED_BASELINE
Definition: defs.h:111
ff_jni_get_env
JNIEnv * ff_jni_get_env(void *log_ctx)
Definition: ffjni.c:53
amediaformat_class
static const AVClass amediaformat_class
Definition: mediacodec_wrapper.c:141
AV_PROFILE_VP9_0
#define AV_PROFILE_VP9_0
Definition: defs.h:154
JNIAMediaCodecListFields::init_id
jmethodID init_id
Definition: mediacodec_wrapper.c:41
FFAMediaCodecJni::BUFFER_FLAG_CODEC_CONFIG
int BUFFER_FLAG_CODEC_CONFIG
Definition: mediacodec_wrapper.c:284
FFAMediaFormatJni
Definition: mediacodec_wrapper.c:147
mediaformat_ndk_toString
static char * mediaformat_ndk_toString(FFAMediaFormat *ctx)
Definition: mediacodec_wrapper.c:1929
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:272
COLOR_STANDARD_BT601_NTSC
@ COLOR_STANDARD_BT601_NTSC
Definition: mediacodec_wrapper.h:358
mediacodec_jni_configure
static int mediacodec_jni_configure(FFAMediaCodec *ctx, const FFAMediaFormat *format_ctx, FFANativeWindow *window, void *crypto, uint32_t flags)
Definition: mediacodec_wrapper.c:1371
AV_PROFILE_H264_HIGH_444
#define AV_PROFILE_H264_HIGH_444
Definition: defs.h:121
mem.h
AV_PROFILE_H264_HIGH_444_INTRA
#define AV_PROFILE_H264_HIGH_444_INTRA
Definition: defs.h:123
mediaformat_jni_getBuffer
static int mediaformat_jni_getBuffer(FFAMediaFormat *ctx, const char *name, void **data, size_t *size)
Definition: mediacodec_wrapper.c:912
color_primaries_map
static struct @158 color_primaries_map[]
JNIAMediaCodecListFields::level_id
jfieldID level_id
Definition: mediacodec_wrapper.c:60
JNIAMediaCodecFields::dequeue_output_buffer_id
jmethodID dequeue_output_buffer_id
Definition: mediacodec_wrapper.c:189
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
mediaformat_ndk_setInt64
static void mediaformat_ndk_setInt64(FFAMediaFormat *ctx, const char *name, int64_t value)
Definition: mediacodec_wrapper.c:1986
mediacodec_jni_stop
static int mediacodec_jni_stop(FFAMediaCodec *ctx)
Definition: mediacodec_wrapper.c:1432
FF_JNI_STATIC_METHOD
@ FF_JNI_STATIC_METHOD
Definition: ffjni.h:95
FFAMediaFormat::class
const AVClass * class
Definition: mediacodec_wrapper.h:64
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
ndk_codec_create
static FFAMediaCodec * ndk_codec_create(int method, const char *arg)
Definition: mediacodec_wrapper.c:2041
JNIAMediaCodecFields::info_output_buffers_changed_id
jfieldID info_output_buffers_changed_id
Definition: mediacodec_wrapper.c:161
int32_t
int32_t
Definition: audioconvert.c:56
MEDIACODEC_NDK_WRAPPER
#define MEDIACODEC_NDK_WRAPPER(method)
Definition: mediacodec_wrapper.c:2181
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
FFAMediaCodecJni
Definition: mediacodec_wrapper.c:269
mediacodec_jni_getOutputBuffer
static uint8_t * mediacodec_jni_getOutputBuffer(FFAMediaCodec *ctx, size_t idx, size_t *out_size)
Definition: mediacodec_wrapper.c:1619
mediacodec_ndk_releaseOutputBufferAtTime
static int mediacodec_ndk_releaseOutputBufferAtTime(FFAMediaCodec *ctx, size_t idx, int64_t timestampNs)
Definition: mediacodec_wrapper.c:2264
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
JNIAMediaCodecFields::flags_id
jfieldID flags_id
Definition: mediacodec_wrapper.c:202
FFAMediaCodecNdk::releaseName
void(* releaseName)(AMediaCodec *, char *name)
Definition: mediacodec_wrapper.c:1847
FFAMediaFormatNdk::api
FFAMediaFormat api
Definition: mediacodec_wrapper.c:1827
avstring.h
jni.h
media_codec_jni
static const FFAMediaCodec media_codec_jni
Definition: mediacodec_wrapper.c:293
JNIAMediaFormatFields::contains_key_id
jmethodID contains_key_id
Definition: mediacodec_wrapper.c:97
AV_PROFILE_AV1_MAIN
#define AV_PROFILE_AV1_MAIN
Definition: defs.h:168
mediacodec_ndk_infoOutputFormatChanged
static int mediacodec_ndk_infoOutputFormatChanged(FFAMediaCodec *ctx, ssize_t idx)
Definition: mediacodec_wrapper.c:2288
AVCOL_SPC_BT709
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition: pixfmt.h:611
mediaformat_jni_toString
static char * mediaformat_jni_toString(FFAMediaFormat *ctx)
Definition: mediacodec_wrapper.c:778
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:651
JNIAMediaCodecListFields::codec_capabilities_class
jclass codec_capabilities_class
Definition: mediacodec_wrapper.c:54
mediaformat_ndk_setString
static void mediaformat_ndk_setString(FFAMediaFormat *ctx, const char *name, const char *value)
Definition: mediacodec_wrapper.c:1998
FFAMediaFormat
Definition: mediacodec_wrapper.h:63
JNIAMediaFormatFields::set_string_id
jmethodID set_string_id
Definition: mediacodec_wrapper.c:109
mediaformat_ndk_delete
static int mediaformat_ndk_delete(FFAMediaFormat *ctx)
Definition: mediacodec_wrapper.c:1911
codec_create
static FFAMediaCodec * codec_create(int method, const char *arg)
Definition: mediacodec_wrapper.c:1214
FFAMediaFormatJni::object
jobject object
Definition: mediacodec_wrapper.c:151
mediaformat_jni_setFloat
static void mediaformat_jni_setFloat(FFAMediaFormat *ctx, const char *name, float value)
Definition: mediacodec_wrapper.c:1058
mediacodec_ndk_configure
static int mediacodec_ndk_configure(FFAMediaCodec *ctx, const FFAMediaFormat *format_ctx, FFANativeWindow *window, void *crypto, uint32_t flags)
Definition: mediacodec_wrapper.c:2122