FFmpeg
mediacodecdec.c
Go to the documentation of this file.
1 /*
2  * Android MediaCodec MPEG-2 / H.264 / H.265 / MPEG-4 / VP8 / VP9 decoders
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 "config_components.h"
24 
25 #include <stdint.h>
26 #include <string.h>
27 
28 #include "libavutil/avassert.h"
29 #include "libavutil/common.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/pixfmt.h"
34 #include "libavutil/internal.h"
35 
36 #include "avcodec.h"
37 #include "codec_internal.h"
38 #include "decode.h"
39 #include "h264_parse.h"
40 #include "h264_ps.h"
41 #include "hevc/parse.h"
42 #include "hwconfig.h"
43 #include "internal.h"
44 #include "jni.h"
45 #include "mediacodec_wrapper.h"
46 #include "mediacodecdec_common.h"
47 
48 typedef struct MediaCodecH264DecContext {
49 
51 
53 
55 
58 
61 
63 {
65 
66  ff_mediacodec_dec_close(avctx, s->ctx);
67  s->ctx = NULL;
68 
69  av_packet_unref(&s->buffered_pkt);
70 
71  return 0;
72 }
73 
74 #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
75 static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
76 {
77  int i;
78  int ret = 0;
79  uint8_t *p = NULL;
80  static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
81 
82  if (!out || !out_size) {
83  return AVERROR(EINVAL);
84  }
85 
86  p = av_malloc(sizeof(nalu_header) + src_size);
87  if (!p) {
88  return AVERROR(ENOMEM);
89  }
90 
91  *out = p;
92  *out_size = sizeof(nalu_header) + src_size;
93 
94  memcpy(p, nalu_header, sizeof(nalu_header));
95  memcpy(p + sizeof(nalu_header), src, src_size);
96 
97  /* Escape 0x00, 0x00, 0x0{0-3} pattern */
98  for (i = 4; i < *out_size; i++) {
99  if (i < *out_size - 3 &&
100  p[i + 0] == 0 &&
101  p[i + 1] == 0 &&
102  p[i + 2] <= 3) {
103  uint8_t *new;
104 
105  *out_size += 1;
106  new = av_realloc(*out, *out_size);
107  if (!new) {
108  ret = AVERROR(ENOMEM);
109  goto done;
110  }
111  *out = p = new;
112 
113  i = i + 2;
114  memmove(p + i + 1, p + i, *out_size - (i + 1));
115  p[i] = 0x03;
116  }
117  }
118 done:
119  if (ret < 0) {
120  av_freep(out);
121  *out_size = 0;
122  }
123 
124  return ret;
125 }
126 #endif
127 
128 #if CONFIG_H264_MEDIACODEC_DECODER
129 static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
130 {
131  int i;
132  int ret;
133 
134  H264ParamSets ps;
135  const PPS *pps = NULL;
136  const SPS *sps = NULL;
137  int is_avc = 0;
138  int nal_length_size = 0;
139 
140  memset(&ps, 0, sizeof(ps));
141 
143  &ps, &is_avc, &nal_length_size, 0, avctx);
144  if (ret < 0) {
145  goto done;
146  }
147 
148  for (i = 0; i < MAX_PPS_COUNT; i++) {
149  if (ps.pps_list[i]) {
150  pps = ps.pps_list[i];
151  break;
152  }
153  }
154 
155  if (pps) {
156  if (ps.sps_list[pps->sps_id]) {
157  sps = ps.sps_list[pps->sps_id];
158  }
159  }
160 
161  if (pps && sps) {
162  uint8_t *data = NULL;
163  int data_size = 0;
164 
165  avctx->profile = ff_h264_get_profile(sps);
166  avctx->level = sps->level_idc;
167 
168  if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
169  goto done;
170  }
171  ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
172  av_freep(&data);
173 
174  if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
175  goto done;
176  }
177  ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
178  av_freep(&data);
179  } else {
180  const int warn = is_avc && (avctx->codec_tag == MKTAG('a','v','c','1') ||
181  avctx->codec_tag == MKTAG('a','v','c','2'));
182  av_log(avctx, warn ? AV_LOG_WARNING : AV_LOG_DEBUG,
183  "Could not extract PPS/SPS from extradata\n");
184  ret = 0;
185  }
186 
187 done:
188  ff_h264_ps_uninit(&ps);
189 
190  return ret;
191 }
192 #endif
193 
194 #if CONFIG_HEVC_MEDIACODEC_DECODER
195 static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
196 {
197  int i;
198  int ret;
199 
200  HEVCParamSets ps;
201  HEVCSEI sei;
202 
203  const HEVCVPS *vps = NULL;
204  const HEVCPPS *pps = NULL;
205  const HEVCSPS *sps = NULL;
206  int is_nalff = 0;
207  int nal_length_size = 0;
208 
209  uint8_t *vps_data = NULL;
210  uint8_t *sps_data = NULL;
211  uint8_t *pps_data = NULL;
212  int vps_data_size = 0;
213  int sps_data_size = 0;
214  int pps_data_size = 0;
215 
216  memset(&ps, 0, sizeof(ps));
217  memset(&sei, 0, sizeof(sei));
218 
220  &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
221  if (ret < 0) {
222  goto done;
223  }
224 
225  for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
226  if (ps.vps_list[i]) {
227  vps = ps.vps_list[i];
228  break;
229  }
230  }
231 
232  for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
233  if (ps.pps_list[i]) {
234  pps = ps.pps_list[i];
235  break;
236  }
237  }
238 
239  if (pps) {
240  if (ps.sps_list[pps->sps_id]) {
241  sps = ps.sps_list[pps->sps_id];
242  }
243  }
244 
245  if (vps && pps && sps) {
246  uint8_t *data;
247  int data_size;
248 
249  avctx->profile = sps->ptl.general_ptl.profile_idc;
250  avctx->level = sps->ptl.general_ptl.level_idc;
251 
252  if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
253  (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
254  (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
255  goto done;
256  }
257 
258  data_size = vps_data_size + sps_data_size + pps_data_size;
259  data = av_mallocz(data_size);
260  if (!data) {
261  ret = AVERROR(ENOMEM);
262  goto done;
263  }
264 
265  memcpy(data , vps_data, vps_data_size);
266  memcpy(data + vps_data_size , sps_data, sps_data_size);
267  memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
268 
269  ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
270 
271  av_freep(&data);
272  } else {
273  const int warn = is_nalff && avctx->codec_tag == MKTAG('h','v','c','1');
274  av_log(avctx, warn ? AV_LOG_WARNING : AV_LOG_DEBUG,
275  "Could not extract VPS/PPS/SPS from extradata\n");
276  ret = 0;
277  }
278 
279 done:
280  ff_hevc_ps_uninit(&ps);
281 
282  av_freep(&vps_data);
283  av_freep(&sps_data);
284  av_freep(&pps_data);
285 
286  return ret;
287 }
288 #endif
289 
290 #if CONFIG_MPEG2_MEDIACODEC_DECODER || \
291  CONFIG_MPEG4_MEDIACODEC_DECODER || \
292  CONFIG_VP8_MEDIACODEC_DECODER || \
293  CONFIG_VP9_MEDIACODEC_DECODER || \
294  CONFIG_AV1_MEDIACODEC_DECODER || \
295  CONFIG_AAC_MEDIACODEC_DECODER || \
296  CONFIG_AMRNB_MEDIACODEC_DECODER || \
297  CONFIG_AMRWB_MEDIACODEC_DECODER || \
298  CONFIG_MP3_MEDIACODEC_DECODER
299 static int common_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
300 {
301  int ret = 0;
302 
303  if (avctx->extradata) {
304  ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
305  }
306 
307  return ret;
308 }
309 #endif
310 
312 {
313  int ret;
314  int sdk_int;
315 
316  const char *codec_mime = NULL;
317 
320 
321  if (s->use_ndk_codec < 0)
322  s->use_ndk_codec = !av_jni_get_java_vm(avctx);
323 
324  format = ff_AMediaFormat_new(s->use_ndk_codec);
325  if (!format) {
326  av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
328  goto done;
329  }
330 
331  switch (avctx->codec_id) {
332 #if CONFIG_AV1_MEDIACODEC_DECODER
333  case AV_CODEC_ID_AV1:
334  codec_mime = "video/av01";
335 
336  ret = common_set_extradata(avctx, format);
337  if (ret < 0)
338  goto done;
339  break;
340 #endif
341 #if CONFIG_H264_MEDIACODEC_DECODER
342  case AV_CODEC_ID_H264:
343  codec_mime = "video/avc";
344 
345  ret = h264_set_extradata(avctx, format);
346  if (ret < 0)
347  goto done;
348  break;
349 #endif
350 #if CONFIG_HEVC_MEDIACODEC_DECODER
351  case AV_CODEC_ID_HEVC:
352  codec_mime = "video/hevc";
353 
354  ret = hevc_set_extradata(avctx, format);
355  if (ret < 0)
356  goto done;
357  break;
358 #endif
359 #if CONFIG_MPEG2_MEDIACODEC_DECODER
361  codec_mime = "video/mpeg2";
362 
363  ret = common_set_extradata(avctx, format);
364  if (ret < 0)
365  goto done;
366  break;
367 #endif
368 #if CONFIG_MPEG4_MEDIACODEC_DECODER
369  case AV_CODEC_ID_MPEG4:
370  codec_mime = "video/mp4v-es",
371 
372  ret = common_set_extradata(avctx, format);
373  if (ret < 0)
374  goto done;
375  break;
376 #endif
377 #if CONFIG_VP8_MEDIACODEC_DECODER
378  case AV_CODEC_ID_VP8:
379  codec_mime = "video/x-vnd.on2.vp8";
380 
381  ret = common_set_extradata(avctx, format);
382  if (ret < 0)
383  goto done;
384  break;
385 #endif
386 #if CONFIG_VP9_MEDIACODEC_DECODER
387  case AV_CODEC_ID_VP9:
388  codec_mime = "video/x-vnd.on2.vp9";
389 
390  ret = common_set_extradata(avctx, format);
391  if (ret < 0)
392  goto done;
393  break;
394 #endif
395 #if CONFIG_AAC_MEDIACODEC_DECODER
396  case AV_CODEC_ID_AAC:
397  codec_mime = "audio/mp4a-latm";
398 
399  ret = common_set_extradata(avctx, format);
400  if (ret < 0)
401  goto done;
402  break;
403 #endif
404 #if CONFIG_AMRNB_MEDIACODEC_DECODER
405  case AV_CODEC_ID_AMR_NB:
406  codec_mime = "audio/3gpp";
407 
408  ret = common_set_extradata(avctx, format);
409  if (ret < 0)
410  goto done;
411  break;
412 #endif
413 #if CONFIG_AMRWB_MEDIACODEC_DECODER
414  case AV_CODEC_ID_AMR_WB:
415  codec_mime = "audio/amr-wb";
416 
417  ret = common_set_extradata(avctx, format);
418  if (ret < 0)
419  goto done;
420  break;
421 #endif
422 #if CONFIG_MP3_MEDIACODEC_DECODER
423  case AV_CODEC_ID_MP3:
424  codec_mime = "audio/mpeg";
425 
426  ret = common_set_extradata(avctx, format);
427  if (ret < 0)
428  goto done;
429  break;
430 #endif
431  default:
432  av_assert0(0);
433  }
434 
435  ff_AMediaFormat_setString(format, "mime", codec_mime);
436 
437  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
438  ff_AMediaFormat_setInt32(format, "width", avctx->width);
439  ff_AMediaFormat_setInt32(format, "height", avctx->height);
440  } else {
441  ff_AMediaFormat_setInt32(format, "channel-count", avctx->ch_layout.nb_channels);
442  ff_AMediaFormat_setInt32(format, "sample-rate", avctx->sample_rate);
443  }
444 
445  s->ctx = av_mallocz(sizeof(*s->ctx));
446  if (!s->ctx) {
447  av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
448  ret = AVERROR(ENOMEM);
449  goto done;
450  }
451 
452  s->ctx->delay_flush = s->delay_flush;
453  s->ctx->use_ndk_codec = s->use_ndk_codec;
454 
455  if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
456  s->ctx = NULL;
457  goto done;
458  }
459 
460  av_log(avctx, AV_LOG_INFO,
461  "MediaCodec started successfully: codec = %s, ret = %d\n",
462  s->ctx->codec_name, ret);
463 
464  sdk_int = ff_Build_SDK_INT(avctx);
465  /* ff_Build_SDK_INT can fail when target API < 24 and JVM isn't available.
466  * If we don't check sdk_int > 0, the workaround might be enabled by
467  * mistake.
468  * JVM is required to make the workaround works reliably. On the other hand,
469  * missing a workaround should not be a serious issue, we do as best we can.
470  */
471  if (sdk_int > 0 && sdk_int <= 23 &&
472  strcmp(s->ctx->codec_name, "OMX.amlogic.mpeg2.decoder.awesome") == 0) {
473  av_log(avctx, AV_LOG_INFO, "Enabling workaround for %s on API=%d\n",
474  s->ctx->codec_name, sdk_int);
475  s->amlogic_mpeg2_api23_workaround = 1;
476  }
477 
478 done:
479  if (format) {
481  }
482 
483  if (ret < 0) {
485  }
486 
487  return ret;
488 }
489 
491 {
493  int ret;
494  ssize_t index;
495 
496  /* In delay_flush mode, wait until the user has released or rendered
497  all retained frames. */
498  if (s->delay_flush && ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
499  if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
500  return AVERROR(EAGAIN);
501  }
502  }
503 
504  /* poll for new frame */
505  ret = ff_mediacodec_dec_receive(avctx, s->ctx, frame, false);
506  if (ret != AVERROR(EAGAIN))
507  return ret;
508 
509  /* feed decoder */
510  while (1) {
511  if (s->ctx->current_input_buffer < 0 && !s->ctx->draining) {
512  /* poll for input space */
513  index = ff_AMediaCodec_dequeueInputBuffer(s->ctx->codec, 0);
514  if (index < 0) {
515  /* no space, block for an output frame to appear */
516  ret = ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
517  /* Try again if both input port and output port return EAGAIN.
518  * If no data is consumed and no frame in output, it can make
519  * both avcodec_send_packet() and avcodec_receive_frame()
520  * return EAGAIN, which violate the design.
521  */
522  if (ff_AMediaCodec_infoTryAgainLater(s->ctx->codec, index) &&
523  ret == AVERROR(EAGAIN))
524  continue;
525  return ret;
526  }
527  s->ctx->current_input_buffer = index;
528  }
529 
530  /* try to flush any buffered packet data */
531  if (s->buffered_pkt.size > 0) {
532  ret = ff_mediacodec_dec_send(avctx, s->ctx, &s->buffered_pkt, false);
533  if (ret >= 0) {
534  s->buffered_pkt.size -= ret;
535  s->buffered_pkt.data += ret;
536  if (s->buffered_pkt.size <= 0) {
537  av_packet_unref(&s->buffered_pkt);
538  } else {
539  av_log(avctx, AV_LOG_WARNING,
540  "could not send entire packet in single input buffer (%d < %d)\n",
541  ret, s->buffered_pkt.size+ret);
542  }
543  } else if (ret < 0 && ret != AVERROR(EAGAIN)) {
544  return ret;
545  }
546 
547  if (s->amlogic_mpeg2_api23_workaround && s->buffered_pkt.size <= 0) {
548  /* fallthrough to fetch next packet regardless of input buffer space */
549  } else {
550  /* poll for space again */
551  continue;
552  }
553  }
554 
555  /* fetch new packet or eof */
556  ret = ff_decode_get_packet(avctx, &s->buffered_pkt);
557  if (ret == AVERROR_EOF) {
558  AVPacket null_pkt = { 0 };
559  ret = ff_mediacodec_dec_send(avctx, s->ctx, &null_pkt, true);
560  if (ret < 0)
561  return ret;
562  return ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
563  } else if (ret == AVERROR(EAGAIN) && s->ctx->current_input_buffer < 0) {
564  return ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
565  } else if (ret < 0) {
566  return ret;
567  }
568  }
569 
570  return AVERROR(EAGAIN);
571 }
572 
574 {
576 
577  av_packet_unref(&s->buffered_pkt);
578 
579  ff_mediacodec_dec_flush(avctx, s->ctx);
580 }
581 
583  &(const AVCodecHWConfigInternal) {
584  .public = {
588  .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
589  },
590  .hwaccel = NULL,
591  },
592  NULL
593 };
594 
595 #define OFFSET(x) offsetof(MediaCodecH264DecContext, x)
596 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
598  { "delay_flush", "Delay flush until hw output buffers are returned to the decoder",
599  OFFSET(delay_flush), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VD },
600  { "ndk_codec", "Use MediaCodec from NDK",
601  OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VD },
602  { NULL }
603 };
604 
605 #define DECLARE_MEDIACODEC_VCLASS(short_name) \
606 static const AVClass ff_##short_name##_mediacodec_dec_class = { \
607  .class_name = #short_name "_mediacodec", \
608  .item_name = av_default_item_name, \
609  .option = ff_mediacodec_vdec_options, \
610  .version = LIBAVUTIL_VERSION_INT, \
611 };
612 
613 #define DECLARE_MEDIACODEC_VDEC(short_name, full_name, codec_id, bsf) \
614 DECLARE_MEDIACODEC_VCLASS(short_name) \
615 const FFCodec ff_ ## short_name ## _mediacodec_decoder = { \
616  .p.name = #short_name "_mediacodec", \
617  CODEC_LONG_NAME(full_name " Android MediaCodec decoder"), \
618  .p.type = AVMEDIA_TYPE_VIDEO, \
619  .p.id = codec_id, \
620  .p.priv_class = &ff_##short_name##_mediacodec_dec_class, \
621  .priv_data_size = sizeof(MediaCodecH264DecContext), \
622  .init = mediacodec_decode_init, \
623  FF_CODEC_RECEIVE_FRAME_CB(mediacodec_receive_frame), \
624  .flush = mediacodec_decode_flush, \
625  .close = mediacodec_decode_close, \
626  .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
627  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \
628  .bsfs = bsf, \
629  .hw_configs = mediacodec_hw_configs, \
630  .p.wrapper_name = "mediacodec", \
631 }; \
632 
633 #if CONFIG_H264_MEDIACODEC_DECODER
634 DECLARE_MEDIACODEC_VDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb")
635 #endif
636 
637 #if CONFIG_HEVC_MEDIACODEC_DECODER
638 DECLARE_MEDIACODEC_VDEC(hevc, "H.265", AV_CODEC_ID_HEVC, "hevc_mp4toannexb")
639 #endif
640 
641 #if CONFIG_MPEG2_MEDIACODEC_DECODER
643 #endif
644 
645 #if CONFIG_MPEG4_MEDIACODEC_DECODER
647 #endif
648 
649 #if CONFIG_VP8_MEDIACODEC_DECODER
651 #endif
652 
653 #if CONFIG_VP9_MEDIACODEC_DECODER
655 #endif
656 
657 #if CONFIG_AV1_MEDIACODEC_DECODER
659 #endif
660 
661 #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
663  { "ndk_codec", "Use MediaCodec from NDK",
664  OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, AD },
665  { NULL }
666 };
667 
668 #define DECLARE_MEDIACODEC_ACLASS(short_name) \
669 static const AVClass ff_##short_name##_mediacodec_dec_class = { \
670  .class_name = #short_name "_mediacodec", \
671  .item_name = av_default_item_name, \
672  .option = ff_mediacodec_adec_options, \
673  .version = LIBAVUTIL_VERSION_INT, \
674 };
675 
676 #define DECLARE_MEDIACODEC_ADEC(short_name, full_name, codec_id, bsf) \
677 DECLARE_MEDIACODEC_VCLASS(short_name) \
678 const FFCodec ff_ ## short_name ## _mediacodec_decoder = { \
679  .p.name = #short_name "_mediacodec", \
680  CODEC_LONG_NAME(full_name " Android MediaCodec decoder"), \
681  .p.type = AVMEDIA_TYPE_AUDIO, \
682  .p.id = codec_id, \
683  .p.priv_class = &ff_##short_name##_mediacodec_dec_class, \
684  .priv_data_size = sizeof(MediaCodecH264DecContext), \
685  .init = mediacodec_decode_init, \
686  FF_CODEC_RECEIVE_FRAME_CB(mediacodec_receive_frame), \
687  .flush = mediacodec_decode_flush, \
688  .close = mediacodec_decode_close, \
689  .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HARDWARE, \
690  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE, \
691  .bsfs = bsf, \
692  .p.wrapper_name = "mediacodec", \
693 }; \
694 
695 #if CONFIG_AAC_MEDIACODEC_DECODER
696 DECLARE_MEDIACODEC_ADEC(aac, "AAC", AV_CODEC_ID_AAC, "aac_adtstoasc")
697 #endif
698 
699 #if CONFIG_AMRNB_MEDIACODEC_DECODER
701 #endif
702 
703 #if CONFIG_AMRWB_MEDIACODEC_DECODER
705 #endif
706 
707 #if CONFIG_MP3_MEDIACODEC_DECODER
709 #endif
H264ParamSets::sps_list
const SPS * sps_list[MAX_SPS_COUNT]
RefStruct references.
Definition: h264_ps.h:145
ff_hevc_decode_extradata
int ff_hevc_decode_extradata(const uint8_t *data, int size, HEVCParamSets *ps, HEVCSEI *sei, int *is_nalff, int *nal_length_size, int err_recognition, int apply_defdispwin, void *logctx)
Definition: parse.c:81
hwconfig.h
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:428
ff_AMediaFormat_delete
static int ff_AMediaFormat_delete(FFAMediaFormat *format)
Definition: mediacodec_wrapper.h:92
MediaCodecDecContext
Definition: mediacodecdec_common.h:37
ff_decode_get_packet
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
Definition: decode.c:240
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
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
opt.h
ff_h264_ps_uninit
void ff_h264_ps_uninit(H264ParamSets *ps)
Uninit H264 param sets structure.
Definition: h264_ps.c:270
mediacodec_receive_frame
static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Definition: mediacodecdec.c:490
out
FILE * out
Definition: movenc.c:55
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1050
HEVCParamSets::pps_list
const HEVCPPS * pps_list[HEVC_MAX_PPS_COUNT]
RefStruct references.
Definition: ps.h:449
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
mediacodec_decode_init
static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
Definition: mediacodecdec.c:311
AD
#define AD
Definition: mediacodecdec.c:661
ff_mediacodec_dec_close
int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s)
Definition: mediacodecdec_common.c:1119
out_size
int out_size
Definition: movenc.c:56
H264ParamSets::pps_list
const PPS * pps_list[MAX_PPS_COUNT]
RefStruct references.
Definition: h264_ps.h:146
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
MediaCodecH264DecContext::buffered_pkt
AVPacket buffered_pkt
Definition: mediacodecdec.c:54
h264_parse.h
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
ff_mediacodec_dec_receive
int ff_mediacodec_dec_receive(AVCodecContext *avctx, MediaCodecDecContext *s, AVFrame *frame, bool wait)
Definition: mediacodecdec_common.c:976
internal.h
ff_AMediaFormat_setString
static void ff_AMediaFormat_setString(FFAMediaFormat *format, const char *name, const char *value)
Definition: mediacodec_wrapper.h:150
AVOption
AVOption.
Definition: opt.h:429
data
const char data[16]
Definition: mxf.c:148
AV_HWDEVICE_TYPE_MEDIACODEC
@ AV_HWDEVICE_TYPE_MEDIACODEC
Definition: hwcontext.h:38
AV_CODEC_ID_AMR_NB
@ AV_CODEC_ID_AMR_NB
Definition: codec_id.h:421
ff_mediacodec_dec_is_flushing
int ff_mediacodec_dec_is_flushing(AVCodecContext *avctx, MediaCodecDecContext *s)
Definition: mediacodecdec_common.c:1138
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
ff_AMediaFormat_setInt32
static void ff_AMediaFormat_setInt32(FFAMediaFormat *format, const char *name, int32_t value)
Definition: mediacodec_wrapper.h:135
AV_CODEC_ID_AMR_WB
@ AV_CODEC_ID_AMR_WB
Definition: codec_id.h:422
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1065
MediaCodecH264DecContext::amlogic_mpeg2_api23_workaround
int amlogic_mpeg2_api23_workaround
Definition: mediacodecdec.c:57
ff_mediacodec_dec_flush
int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s)
Definition: mediacodecdec_common.c:1101
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:441
HEVCParamSets::sps_list
const HEVCSPS * sps_list[HEVC_MAX_SPS_COUNT]
RefStruct references.
Definition: ps.h:448
ff_AMediaFormat_new
FFAMediaFormat * ff_AMediaFormat_new(int ndk)
Definition: mediacodec_wrapper.c:2401
mediacodecdec_common.h
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
ff_Build_SDK_INT
int ff_Build_SDK_INT(AVCodecContext *avctx)
Definition: mediacodec_wrapper.c:2429
AV_CODEC_HW_CONFIG_METHOD_AD_HOC
@ AV_CODEC_HW_CONFIG_METHOD_AD_HOC
The codec supports this format by some ad-hoc method.
Definition: codec.h:342
ff_AMediaFormat_setBuffer
static void ff_AMediaFormat_setBuffer(FFAMediaFormat *format, const char *name, void *data, size_t size)
Definition: mediacodec_wrapper.h:155
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
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
ff_h264_decode_extradata
int ff_h264_decode_extradata(const uint8_t *data, int size, H264ParamSets *ps, int *is_avc, int *nal_length_size, int err_recognition, void *logctx)
Definition: h264_parse.c:466
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:220
HEVCSEI
Definition: sei.h:82
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
decode.h
AVCodecHWConfig::pix_fmt
enum AVPixelFormat pix_fmt
For decoders, a hardware pixel format which that decoder may be able to decode to if suitable hardwar...
Definition: codec.h:354
OFFSET
#define OFFSET(x)
Definition: mediacodecdec.c:595
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:455
AV_PIX_FMT_MEDIACODEC
@ AV_PIX_FMT_MEDIACODEC
hardware decoding through MediaCodec
Definition: pixfmt.h:316
HEVC_MAX_PPS_COUNT
@ HEVC_MAX_PPS_COUNT
Definition: hevc.h:114
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
parse.h
HEVC_MAX_VPS_COUNT
@ HEVC_MAX_VPS_COUNT
Definition: hevc.h:110
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:280
SPS
Sequence parameter set.
Definition: h264_ps.h:44
PPS
Picture parameter set.
Definition: h264_ps.h:110
MAX_PPS_COUNT
#define MAX_PPS_COUNT
Definition: h264_ps.h:38
vps
static int FUNC() vps(CodedBitstreamContext *ctx, RWContext *rw, H265RawVPS *current)
Definition: cbs_h265_syntax_template.c:423
sei
static int FUNC() sei(CodedBitstreamContext *ctx, RWContext *rw, H264RawSEI *current)
Definition: cbs_h264_syntax_template.c:858
AVCodecContext::level
int level
Encoding level descriptor.
Definition: avcodec.h:1788
h264_ps.h
index
int index
Definition: gxfenc.c:90
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:442
MediaCodecH264DecContext::use_ndk_codec
int use_ndk_codec
Definition: mediacodecdec.c:59
codec_internal.h
ff_mediacodec_adec_options
static const AVOption ff_mediacodec_adec_options[]
Definition: mediacodecdec.c:662
VD
#define VD
Definition: mediacodecdec.c:596
MediaCodecH264DecContext
Definition: mediacodecdec.c:48
AVCodecHWConfigInternal
Definition: hwconfig.h:25
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
ff_hevc_ps_uninit
void ff_hevc_ps_uninit(HEVCParamSets *ps)
Definition: ps.c:2034
mediacodec_decode_flush
static void mediacodec_decode_flush(AVCodecContext *avctx)
Definition: mediacodecdec.c:573
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
mediacodec_wrapper.h
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
internal.h
common.h
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:226
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
AVCodecContext::height
int height
Definition: avcodec.h:618
av_jni_get_java_vm
void * av_jni_get_java_vm(void *log_ctx)
Definition: jni.c:75
MediaCodecH264DecContext::avclass
AVClass * avclass
Definition: mediacodecdec.c:50
ff_AMediaCodec_dequeueInputBuffer
static ssize_t ff_AMediaCodec_dequeueInputBuffer(FFAMediaCodec *codec, int64_t timeoutUs)
Definition: mediacodec_wrapper.h:271
avcodec.h
ret
ret
Definition: filter_design.txt:187
pixfmt.h
ff_h264_get_profile
int ff_h264_get_profile(const SPS *sps)
Compute profile from profile_idc and constraint_set?_flags.
Definition: h264_parse.c:533
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
MediaCodecH264DecContext::delay_flush
int delay_flush
Definition: mediacodecdec.c:56
DECLARE_MEDIACODEC_VDEC
#define DECLARE_MEDIACODEC_VDEC(short_name, full_name, codec_id, bsf)
Definition: mediacodecdec.c:613
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1644
H264ParamSets
Definition: h264_ps.h:144
pps
uint64_t pps
Definition: dovi_rpuenc.c:35
ff_mediacodec_vdec_options
static const AVOption ff_mediacodec_vdec_options[]
Definition: mediacodecdec.c:597
DECLARE_MEDIACODEC_ADEC
#define DECLARE_MEDIACODEC_ADEC(short_name, full_name, codec_id, bsf)
Definition: mediacodecdec.c:676
MediaCodecH264DecContext::ctx
MediaCodecDecContext * ctx
Definition: mediacodecdec.c:52
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:453
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
HEVCVPS
Definition: ps.h:156
HEVCSPS
Definition: ps.h:190
HEVCPPS
Definition: ps.h:309
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:470
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVPacket
This structure stores compressed data.
Definition: packet.h:510
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
HEVCParamSets::vps_list
const HEVCVPS * vps_list[HEVC_MAX_VPS_COUNT]
RefStruct references.
Definition: ps.h:447
ff_AMediaCodec_infoTryAgainLater
static int ff_AMediaCodec_infoTryAgainLater(FFAMediaCodec *codec, ssize_t idx)
Definition: mediacodec_wrapper.h:301
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
ff_mediacodec_dec_send
int ff_mediacodec_dec_send(AVCodecContext *avctx, MediaCodecDecContext *s, AVPacket *pkt, bool wait)
Definition: mediacodecdec_common.c:882
AV_CODEC_ID_VP8
@ AV_CODEC_ID_VP8
Definition: codec_id.h:192
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
Definition: codec.h:313
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
mediacodec_hw_configs
static const AVCodecHWConfigInternal *const mediacodec_hw_configs[]
Definition: mediacodecdec.c:582
jni.h
AVCodecHWConfigInternal::public
AVCodecHWConfig public
This is the structure which will be returned to the user by avcodec_get_hw_config().
Definition: hwconfig.h:30
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54
FFAMediaFormat
Definition: mediacodec_wrapper.h:63
mediacodec_decode_close
static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
Definition: mediacodecdec.c:62
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:155
ff_mediacodec_dec_init
int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s, const char *mime, FFAMediaFormat *format)
Definition: mediacodecdec_common.c:817
HEVCParamSets
Definition: ps.h:446