2 * Android MediaCodec MPEG-2 / H.264 / H.265 / MPEG-4 / VP8 / VP9 decoders
4 * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
6 * This file is part of FFmpeg.
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.
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.
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
26 #include "libavutil/avassert.h"
27 #include "libavutil/common.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/pixfmt.h"
31 #include "libavutil/internal.h"
35 #include "h264_parse.h"
36 #include "hevc_parse.h"
39 #include "mediacodec_wrapper.h"
40 #include "mediacodecdec_common.h"
42 typedef struct MediaCodecH264DecContext {
46 MediaCodecDecContext *ctx;
48 AVPacket buffered_pkt;
51 int amlogic_mpeg2_api23_workaround;
53 } MediaCodecH264DecContext;
55 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
57 MediaCodecH264DecContext *s = avctx->priv_data;
59 ff_mediacodec_dec_close(avctx, s->ctx);
62 av_packet_unref(&s->buffered_pkt);
67 #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
68 static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
73 static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
75 if (!out || !out_size) {
76 return AVERROR(EINVAL);
79 p = av_malloc(sizeof(nalu_header) + src_size);
81 return AVERROR(ENOMEM);
85 *out_size = sizeof(nalu_header) + src_size;
87 memcpy(p, nalu_header, sizeof(nalu_header));
88 memcpy(p + sizeof(nalu_header), src, src_size);
90 /* Escape 0x00, 0x00, 0x0{0-3} pattern */
91 for (i = 4; i < *out_size; i++) {
92 if (i < *out_size - 3 &&
99 new = av_realloc(*out, *out_size);
101 ret = AVERROR(ENOMEM);
107 memmove(p + i + 1, p + i, *out_size - (i + 1));
121 #if CONFIG_H264_MEDIACODEC_DECODER
122 static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
128 const PPS *pps = NULL;
129 const SPS *sps = NULL;
131 int nal_length_size = 0;
133 memset(&ps, 0, sizeof(ps));
135 ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
136 &ps, &is_avc, &nal_length_size, 0, avctx);
141 for (i = 0; i < MAX_PPS_COUNT; i++) {
142 if (ps.pps_list[i]) {
143 pps = (const PPS*)ps.pps_list[i]->data;
149 if (ps.sps_list[pps->sps_id]) {
150 sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
155 uint8_t *data = NULL;
158 if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
161 ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
164 if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
167 ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
170 const int warn = is_avc && (avctx->codec_tag == MKTAG('a','v','c','1') ||
171 avctx->codec_tag == MKTAG('a','v','c','2'));
172 av_log(avctx, warn ? AV_LOG_WARNING : AV_LOG_DEBUG,
173 "Could not extract PPS/SPS from extradata\n");
178 ff_h264_ps_uninit(&ps);
184 #if CONFIG_HEVC_MEDIACODEC_DECODER
185 static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
193 const HEVCVPS *vps = NULL;
194 const HEVCPPS *pps = NULL;
195 const HEVCSPS *sps = NULL;
197 int nal_length_size = 0;
199 uint8_t *vps_data = NULL;
200 uint8_t *sps_data = NULL;
201 uint8_t *pps_data = NULL;
202 int vps_data_size = 0;
203 int sps_data_size = 0;
204 int pps_data_size = 0;
206 memset(&ps, 0, sizeof(ps));
207 memset(&sei, 0, sizeof(sei));
209 ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
210 &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
215 for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
216 if (ps.vps_list[i]) {
217 vps = (const HEVCVPS*)ps.vps_list[i]->data;
222 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
223 if (ps.pps_list[i]) {
224 pps = (const HEVCPPS*)ps.pps_list[i]->data;
230 if (ps.sps_list[pps->sps_id]) {
231 sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
235 if (vps && pps && sps) {
239 if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
240 (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
241 (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
245 data_size = vps_data_size + sps_data_size + pps_data_size;
246 data = av_mallocz(data_size);
248 ret = AVERROR(ENOMEM);
252 memcpy(data , vps_data, vps_data_size);
253 memcpy(data + vps_data_size , sps_data, sps_data_size);
254 memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
256 ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
260 const int warn = is_nalff && avctx->codec_tag == MKTAG('h','v','c','1');
261 av_log(avctx, warn ? AV_LOG_WARNING : AV_LOG_DEBUG,
262 "Could not extract VPS/PPS/SPS from extradata\n");
267 ff_hevc_ps_uninit(&ps);
277 #if CONFIG_MPEG2_MEDIACODEC_DECODER || \
278 CONFIG_MPEG4_MEDIACODEC_DECODER || \
279 CONFIG_VP8_MEDIACODEC_DECODER || \
280 CONFIG_VP9_MEDIACODEC_DECODER
281 static int common_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
285 if (avctx->extradata) {
286 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
293 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
298 const char *codec_mime = NULL;
300 FFAMediaFormat *format = NULL;
301 MediaCodecH264DecContext *s = avctx->priv_data;
303 format = ff_AMediaFormat_new();
305 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
306 ret = AVERROR_EXTERNAL;
310 switch (avctx->codec_id) {
311 #if CONFIG_H264_MEDIACODEC_DECODER
312 case AV_CODEC_ID_H264:
313 codec_mime = "video/avc";
315 ret = h264_set_extradata(avctx, format);
320 #if CONFIG_HEVC_MEDIACODEC_DECODER
321 case AV_CODEC_ID_HEVC:
322 codec_mime = "video/hevc";
324 ret = hevc_set_extradata(avctx, format);
329 #if CONFIG_MPEG2_MEDIACODEC_DECODER
330 case AV_CODEC_ID_MPEG2VIDEO:
331 codec_mime = "video/mpeg2";
333 ret = common_set_extradata(avctx, format);
338 #if CONFIG_MPEG4_MEDIACODEC_DECODER
339 case AV_CODEC_ID_MPEG4:
340 codec_mime = "video/mp4v-es",
342 ret = common_set_extradata(avctx, format);
347 #if CONFIG_VP8_MEDIACODEC_DECODER
348 case AV_CODEC_ID_VP8:
349 codec_mime = "video/x-vnd.on2.vp8";
351 ret = common_set_extradata(avctx, format);
356 #if CONFIG_VP9_MEDIACODEC_DECODER
357 case AV_CODEC_ID_VP9:
358 codec_mime = "video/x-vnd.on2.vp9";
360 ret = common_set_extradata(avctx, format);
369 ff_AMediaFormat_setString(format, "mime", codec_mime);
370 ff_AMediaFormat_setInt32(format, "width", avctx->width);
371 ff_AMediaFormat_setInt32(format, "height", avctx->height);
373 s->ctx = av_mallocz(sizeof(*s->ctx));
375 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
376 ret = AVERROR(ENOMEM);
380 s->ctx->delay_flush = s->delay_flush;
382 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
387 av_log(avctx, AV_LOG_INFO,
388 "MediaCodec started successfully: codec = %s, ret = %d\n",
389 s->ctx->codec_name, ret);
391 sdk_int = ff_Build_SDK_INT(avctx);
393 strcmp(s->ctx->codec_name, "OMX.amlogic.mpeg2.decoder.awesome") == 0) {
394 av_log(avctx, AV_LOG_INFO, "Enabling workaround for %s on API=%d\n",
395 s->ctx->codec_name, sdk_int);
396 s->amlogic_mpeg2_api23_workaround = 1;
401 ff_AMediaFormat_delete(format);
405 mediacodec_decode_close(avctx);
411 static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
413 MediaCodecH264DecContext *s = avctx->priv_data;
417 /* In delay_flush mode, wait until the user has released or rendered
418 all retained frames. */
419 if (s->delay_flush && ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
420 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
421 return AVERROR(EAGAIN);
425 /* poll for new frame */
426 ret = ff_mediacodec_dec_receive(avctx, s->ctx, frame, false);
427 if (ret != AVERROR(EAGAIN))
432 if (s->ctx->current_input_buffer < 0) {
433 /* poll for input space */
434 index = ff_AMediaCodec_dequeueInputBuffer(s->ctx->codec, 0);
436 /* no space, block for an output frame to appear */
437 return ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
439 s->ctx->current_input_buffer = index;
442 /* try to flush any buffered packet data */
443 if (s->buffered_pkt.size > 0) {
444 ret = ff_mediacodec_dec_send(avctx, s->ctx, &s->buffered_pkt, false);
446 s->buffered_pkt.size -= ret;
447 s->buffered_pkt.data += ret;
448 if (s->buffered_pkt.size <= 0) {
449 av_packet_unref(&s->buffered_pkt);
451 av_log(avctx, AV_LOG_WARNING,
452 "could not send entire packet in single input buffer (%d < %d)\n",
453 ret, s->buffered_pkt.size+ret);
455 } else if (ret < 0 && ret != AVERROR(EAGAIN)) {
459 if (s->amlogic_mpeg2_api23_workaround && s->buffered_pkt.size <= 0) {
460 /* fallthrough to fetch next packet regardless of input buffer space */
462 /* poll for space again */
467 /* fetch new packet or eof */
468 ret = ff_decode_get_packet(avctx, &s->buffered_pkt);
469 if (ret == AVERROR_EOF) {
470 AVPacket null_pkt = { 0 };
471 ret = ff_mediacodec_dec_send(avctx, s->ctx, &null_pkt, true);
474 return ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
475 } else if (ret == AVERROR(EAGAIN) && s->ctx->current_input_buffer < 0) {
476 return ff_mediacodec_dec_receive(avctx, s->ctx, frame, true);
477 } else if (ret < 0) {
482 return AVERROR(EAGAIN);
485 static void mediacodec_decode_flush(AVCodecContext *avctx)
487 MediaCodecH264DecContext *s = avctx->priv_data;
489 av_packet_unref(&s->buffered_pkt);
491 ff_mediacodec_dec_flush(avctx, s->ctx);
494 static const AVCodecHWConfigInternal *const mediacodec_hw_configs[] = {
495 &(const AVCodecHWConfigInternal) {
497 .pix_fmt = AV_PIX_FMT_MEDIACODEC,
498 .methods = AV_CODEC_HW_CONFIG_METHOD_AD_HOC |
499 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
500 .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
507 #define OFFSET(x) offsetof(MediaCodecH264DecContext, x)
508 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
509 static const AVOption ff_mediacodec_vdec_options[] = {
510 { "delay_flush", "Delay flush until hw output buffers are returned to the decoder",
511 OFFSET(delay_flush), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VD },
515 #define DECLARE_MEDIACODEC_VCLASS(short_name) \
516 static const AVClass ff_##short_name##_mediacodec_dec_class = { \
517 .class_name = #short_name "_mediacodec", \
518 .item_name = av_default_item_name, \
519 .option = ff_mediacodec_vdec_options, \
520 .version = LIBAVUTIL_VERSION_INT, \
523 #define DECLARE_MEDIACODEC_VDEC(short_name, full_name, codec_id, bsf) \
524 DECLARE_MEDIACODEC_VCLASS(short_name) \
525 const AVCodec ff_ ## short_name ## _mediacodec_decoder = { \
526 .name = #short_name "_mediacodec", \
527 .long_name = NULL_IF_CONFIG_SMALL(full_name " Android MediaCodec decoder"), \
528 .type = AVMEDIA_TYPE_VIDEO, \
530 .priv_class = &ff_##short_name##_mediacodec_dec_class, \
531 .priv_data_size = sizeof(MediaCodecH264DecContext), \
532 .init = mediacodec_decode_init, \
533 .receive_frame = mediacodec_receive_frame, \
534 .flush = mediacodec_decode_flush, \
535 .close = mediacodec_decode_close, \
536 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
537 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \
539 .hw_configs = mediacodec_hw_configs, \
540 .wrapper_name = "mediacodec", \
543 #if CONFIG_H264_MEDIACODEC_DECODER
544 DECLARE_MEDIACODEC_VDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb")
547 #if CONFIG_HEVC_MEDIACODEC_DECODER
548 DECLARE_MEDIACODEC_VDEC(hevc, "H.265", AV_CODEC_ID_HEVC, "hevc_mp4toannexb")
551 #if CONFIG_MPEG2_MEDIACODEC_DECODER
552 DECLARE_MEDIACODEC_VDEC(mpeg2, "MPEG-2", AV_CODEC_ID_MPEG2VIDEO, NULL)
555 #if CONFIG_MPEG4_MEDIACODEC_DECODER
556 DECLARE_MEDIACODEC_VDEC(mpeg4, "MPEG-4", AV_CODEC_ID_MPEG4, NULL)
559 #if CONFIG_VP8_MEDIACODEC_DECODER
560 DECLARE_MEDIACODEC_VDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL)
563 #if CONFIG_VP9_MEDIACODEC_DECODER
564 DECLARE_MEDIACODEC_VDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL)