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/fifo.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/pixfmt.h"
34 #include "h264_parse.h"
35 #include "hevc_parse.h"
37 #include "mediacodec_wrapper.h"
38 #include "mediacodecdec_common.h"
40 typedef struct MediaCodecH264DecContext {
42 MediaCodecDecContext *ctx;
48 AVPacket filtered_pkt;
50 } MediaCodecH264DecContext;
52 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
54 MediaCodecH264DecContext *s = avctx->priv_data;
56 ff_mediacodec_dec_close(avctx, s->ctx);
59 av_fifo_free(s->fifo);
62 av_packet_unref(&s->filtered_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 av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
171 ret = AVERROR_INVALIDDATA;
175 ff_h264_ps_uninit(&ps);
181 #if CONFIG_HEVC_MEDIACODEC_DECODER
182 static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
190 const HEVCVPS *vps = NULL;
191 const HEVCPPS *pps = NULL;
192 const HEVCSPS *sps = NULL;
194 int nal_length_size = 0;
196 uint8_t *vps_data = NULL;
197 uint8_t *sps_data = NULL;
198 uint8_t *pps_data = NULL;
199 int vps_data_size = 0;
200 int sps_data_size = 0;
201 int pps_data_size = 0;
203 memset(&ps, 0, sizeof(ps));
204 memset(&sei, 0, sizeof(sei));
206 ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
207 &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
212 for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
213 if (ps.vps_list[i]) {
214 vps = (const HEVCVPS*)ps.vps_list[i]->data;
219 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
220 if (ps.pps_list[i]) {
221 pps = (const HEVCPPS*)ps.pps_list[i]->data;
227 if (ps.sps_list[pps->sps_id]) {
228 sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
232 if (vps && pps && sps) {
236 if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
237 (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
238 (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
242 data_size = vps_data_size + sps_data_size + pps_data_size;
243 data = av_mallocz(data_size);
245 ret = AVERROR(ENOMEM);
249 memcpy(data , vps_data, vps_data_size);
250 memcpy(data + vps_data_size , sps_data, sps_data_size);
251 memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
253 ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
257 av_log(avctx, AV_LOG_ERROR, "Could not extract VPS/PPS/SPS from extradata");
258 ret = AVERROR_INVALIDDATA;
270 #if CONFIG_MPEG2_MEDIACODEC_DECODER
271 static int mpeg2_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
275 if (avctx->extradata) {
276 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
283 #if CONFIG_MPEG4_MEDIACODEC_DECODER
284 static int mpeg4_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
288 if (avctx->extradata) {
289 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
296 #if CONFIG_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
297 static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
301 if (avctx->extradata) {
302 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
309 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
313 const char *codec_mime = NULL;
315 const char *bsf_name = NULL;
316 const AVBitStreamFilter *bsf = NULL;
318 FFAMediaFormat *format = NULL;
319 MediaCodecH264DecContext *s = avctx->priv_data;
321 format = ff_AMediaFormat_new();
323 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
324 ret = AVERROR_EXTERNAL;
328 switch (avctx->codec_id) {
329 #if CONFIG_H264_MEDIACODEC_DECODER
330 case AV_CODEC_ID_H264:
331 codec_mime = "video/avc";
332 bsf_name = "h264_mp4toannexb";
334 ret = h264_set_extradata(avctx, format);
339 #if CONFIG_HEVC_MEDIACODEC_DECODER
340 case AV_CODEC_ID_HEVC:
341 codec_mime = "video/hevc";
342 bsf_name = "hevc_mp4toannexb";
344 ret = hevc_set_extradata(avctx, format);
349 #if CONFIG_MPEG2_MEDIACODEC_DECODER
350 case AV_CODEC_ID_MPEG2VIDEO:
351 codec_mime = "video/mpeg2";
353 ret = mpeg2_set_extradata(avctx, format);
358 #if CONFIG_MPEG4_MEDIACODEC_DECODER
359 case AV_CODEC_ID_MPEG4:
360 codec_mime = "video/mp4v-es",
362 ret = mpeg4_set_extradata(avctx, format);
367 #if CONFIG_VP8_MEDIACODEC_DECODER
368 case AV_CODEC_ID_VP8:
369 codec_mime = "video/x-vnd.on2.vp8";
371 ret = vpx_set_extradata(avctx, format);
376 #if CONFIG_VP9_MEDIACODEC_DECODER
377 case AV_CODEC_ID_VP9:
378 codec_mime = "video/x-vnd.on2.vp9";
380 ret = vpx_set_extradata(avctx, format);
389 ff_AMediaFormat_setString(format, "mime", codec_mime);
390 ff_AMediaFormat_setInt32(format, "width", avctx->width);
391 ff_AMediaFormat_setInt32(format, "height", avctx->height);
393 s->ctx = av_mallocz(sizeof(*s->ctx));
395 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
396 ret = AVERROR(ENOMEM);
400 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
405 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
407 s->fifo = av_fifo_alloc(sizeof(AVPacket));
409 ret = AVERROR(ENOMEM);
414 bsf = av_bsf_get_by_name(bsf_name);
416 ret = AVERROR_BSF_NOT_FOUND;
420 if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
424 if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
425 ((ret = av_bsf_init(s->bsf)) < 0)) {
430 av_init_packet(&s->filtered_pkt);
434 ff_AMediaFormat_delete(format);
438 mediacodec_decode_close(avctx);
445 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
446 int *got_frame, AVPacket *pkt)
448 MediaCodecH264DecContext *s = avctx->priv_data;
450 return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
453 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
454 int *got_frame, AVPacket *avpkt)
456 MediaCodecH264DecContext *s = avctx->priv_data;
457 AVFrame *frame = data;
460 /* buffer the input packet */
462 AVPacket input_pkt = { 0 };
464 if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
465 ret = av_fifo_realloc2(s->fifo,
466 av_fifo_size(s->fifo) + sizeof(input_pkt));
471 ret = av_packet_ref(&input_pkt, avpkt);
474 av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
478 * MediaCodec.flush() discards both input and output buffers, thus we
479 * need to delay the call to this function until the user has released or
480 * renderered the frames he retains.
482 * After we have buffered an input packet, check if the codec is in the
483 * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
485 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
486 * the codec (because the user retains frames). The codec stays in the
489 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
490 * performed on the codec. The codec leaves the flushing state and can
491 * process again packets.
493 * ff_mediacodec_dec_flush returns a negative value if an error has
497 if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
498 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
503 /* process buffered data */
504 while (!*got_frame) {
505 /* prepare the input data -- convert to Annex B if needed */
506 if (s->filtered_pkt.size <= 0) {
507 AVPacket input_pkt = { 0 };
509 av_packet_unref(&s->filtered_pkt);
512 if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
513 return avpkt->size ? avpkt->size :
514 ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
517 av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
520 ret = av_bsf_send_packet(s->bsf, &input_pkt);
525 ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
526 if (ret == AVERROR(EAGAIN)) {
530 av_packet_move_ref(&s->filtered_pkt, &input_pkt);
533 /* {h264,hevc}_mp4toannexb are used here and do not require flushing */
534 av_assert0(ret != AVERROR_EOF);
541 ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
545 s->filtered_pkt.size -= ret;
546 s->filtered_pkt.data += ret;
552 static void mediacodec_decode_flush(AVCodecContext *avctx)
554 MediaCodecH264DecContext *s = avctx->priv_data;
556 while (av_fifo_size(s->fifo)) {
558 av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
559 av_packet_unref(&pkt);
561 av_fifo_reset(s->fifo);
563 av_packet_unref(&s->filtered_pkt);
565 ff_mediacodec_dec_flush(avctx, s->ctx);
568 #if CONFIG_H264_MEDIACODEC_DECODER
569 AVCodec ff_h264_mediacodec_decoder = {
570 .name = "h264_mediacodec",
571 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
572 .type = AVMEDIA_TYPE_VIDEO,
573 .id = AV_CODEC_ID_H264,
574 .priv_data_size = sizeof(MediaCodecH264DecContext),
575 .init = mediacodec_decode_init,
576 .decode = mediacodec_decode_frame,
577 .flush = mediacodec_decode_flush,
578 .close = mediacodec_decode_close,
579 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
580 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
584 #if CONFIG_HEVC_MEDIACODEC_DECODER
585 AVCodec ff_hevc_mediacodec_decoder = {
586 .name = "hevc_mediacodec",
587 .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
588 .type = AVMEDIA_TYPE_VIDEO,
589 .id = AV_CODEC_ID_HEVC,
590 .priv_data_size = sizeof(MediaCodecH264DecContext),
591 .init = mediacodec_decode_init,
592 .decode = mediacodec_decode_frame,
593 .flush = mediacodec_decode_flush,
594 .close = mediacodec_decode_close,
595 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
596 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
600 #if CONFIG_MPEG2_MEDIACODEC_DECODER
601 AVCodec ff_mpeg2_mediacodec_decoder = {
602 .name = "mpeg2_mediacodec",
603 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Android MediaCodec decoder"),
604 .type = AVMEDIA_TYPE_VIDEO,
605 .id = AV_CODEC_ID_MPEG2VIDEO,
606 .priv_data_size = sizeof(MediaCodecH264DecContext),
607 .init = mediacodec_decode_init,
608 .decode = mediacodec_decode_frame,
609 .flush = mediacodec_decode_flush,
610 .close = mediacodec_decode_close,
611 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
612 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
616 #if CONFIG_MPEG4_MEDIACODEC_DECODER
617 AVCodec ff_mpeg4_mediacodec_decoder = {
618 .name = "mpeg4_mediacodec",
619 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
620 .type = AVMEDIA_TYPE_VIDEO,
621 .id = AV_CODEC_ID_MPEG4,
622 .priv_data_size = sizeof(MediaCodecH264DecContext),
623 .init = mediacodec_decode_init,
624 .decode = mediacodec_decode_frame,
625 .flush = mediacodec_decode_flush,
626 .close = mediacodec_decode_close,
627 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
628 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
632 #if CONFIG_VP8_MEDIACODEC_DECODER
633 AVCodec ff_vp8_mediacodec_decoder = {
634 .name = "vp8_mediacodec",
635 .long_name = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
636 .type = AVMEDIA_TYPE_VIDEO,
637 .id = AV_CODEC_ID_VP8,
638 .priv_data_size = sizeof(MediaCodecH264DecContext),
639 .init = mediacodec_decode_init,
640 .decode = mediacodec_decode_frame,
641 .flush = mediacodec_decode_flush,
642 .close = mediacodec_decode_close,
643 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
644 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
648 #if CONFIG_VP9_MEDIACODEC_DECODER
649 AVCodec ff_vp9_mediacodec_decoder = {
650 .name = "vp9_mediacodec",
651 .long_name = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
652 .type = AVMEDIA_TYPE_VIDEO,
653 .id = AV_CODEC_ID_VP9,
654 .priv_data_size = sizeof(MediaCodecH264DecContext),
655 .init = mediacodec_decode_init,
656 .decode = mediacodec_decode_frame,
657 .flush = mediacodec_decode_flush,
658 .close = mediacodec_decode_close,
659 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
660 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,