2 * Android MediaCodec 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_MPEG4_MEDIACODEC_DECODER
271 static int mpeg4_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_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
284 static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
288 if (avctx->extradata) {
289 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
296 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
300 const char *codec_mime = NULL;
302 const char *bsf_name = NULL;
303 const AVBitStreamFilter *bsf = NULL;
305 FFAMediaFormat *format = NULL;
306 MediaCodecH264DecContext *s = avctx->priv_data;
308 format = ff_AMediaFormat_new();
310 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
311 ret = AVERROR_EXTERNAL;
315 switch (avctx->codec_id) {
316 #if CONFIG_H264_MEDIACODEC_DECODER
317 case AV_CODEC_ID_H264:
318 codec_mime = "video/avc";
319 bsf_name = "h264_mp4toannexb";
321 ret = h264_set_extradata(avctx, format);
326 #if CONFIG_HEVC_MEDIACODEC_DECODER
327 case AV_CODEC_ID_HEVC:
328 codec_mime = "video/hevc";
329 bsf_name = "hevc_mp4toannexb";
331 ret = hevc_set_extradata(avctx, format);
336 #if CONFIG_MPEG4_MEDIACODEC_DECODER
337 case AV_CODEC_ID_MPEG4:
338 codec_mime = "video/mp4v-es",
340 ret = mpeg4_set_extradata(avctx, format);
345 #if CONFIG_VP8_MEDIACODEC_DECODER
346 case AV_CODEC_ID_VP8:
347 codec_mime = "video/x-vnd.on2.vp8";
349 ret = vpx_set_extradata(avctx, format);
354 #if CONFIG_VP9_MEDIACODEC_DECODER
355 case AV_CODEC_ID_VP9:
356 codec_mime = "video/x-vnd.on2.vp9";
358 ret = vpx_set_extradata(avctx, format);
367 ff_AMediaFormat_setString(format, "mime", codec_mime);
368 ff_AMediaFormat_setInt32(format, "width", avctx->width);
369 ff_AMediaFormat_setInt32(format, "height", avctx->height);
371 s->ctx = av_mallocz(sizeof(*s->ctx));
373 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
374 ret = AVERROR(ENOMEM);
378 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
383 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
385 s->fifo = av_fifo_alloc(sizeof(AVPacket));
387 ret = AVERROR(ENOMEM);
392 bsf = av_bsf_get_by_name(bsf_name);
394 ret = AVERROR_BSF_NOT_FOUND;
398 if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
402 if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
403 ((ret = av_bsf_init(s->bsf)) < 0)) {
408 av_init_packet(&s->filtered_pkt);
412 ff_AMediaFormat_delete(format);
416 mediacodec_decode_close(avctx);
423 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
424 int *got_frame, AVPacket *pkt)
426 MediaCodecH264DecContext *s = avctx->priv_data;
428 return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
431 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
432 int *got_frame, AVPacket *avpkt)
434 MediaCodecH264DecContext *s = avctx->priv_data;
435 AVFrame *frame = data;
438 /* buffer the input packet */
440 AVPacket input_pkt = { 0 };
442 if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
443 ret = av_fifo_realloc2(s->fifo,
444 av_fifo_size(s->fifo) + sizeof(input_pkt));
449 ret = av_packet_ref(&input_pkt, avpkt);
452 av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
456 * MediaCodec.flush() discards both input and output buffers, thus we
457 * need to delay the call to this function until the user has released or
458 * renderered the frames he retains.
460 * After we have buffered an input packet, check if the codec is in the
461 * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
463 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
464 * the codec (because the user retains frames). The codec stays in the
467 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
468 * performed on the codec. The codec leaves the flushing state and can
469 * process again packets.
471 * ff_mediacodec_dec_flush returns a negative value if an error has
475 if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
476 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
481 /* process buffered data */
482 while (!*got_frame) {
483 /* prepare the input data -- convert to Annex B if needed */
484 if (s->filtered_pkt.size <= 0) {
485 AVPacket input_pkt = { 0 };
487 av_packet_unref(&s->filtered_pkt);
490 if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
491 return avpkt->size ? avpkt->size :
492 ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
495 av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
498 ret = av_bsf_send_packet(s->bsf, &input_pkt);
503 ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
504 if (ret == AVERROR(EAGAIN)) {
508 av_packet_move_ref(&s->filtered_pkt, &input_pkt);
511 /* {h264,hevc}_mp4toannexb are used here and do not require flushing */
512 av_assert0(ret != AVERROR_EOF);
519 ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
523 s->filtered_pkt.size -= ret;
524 s->filtered_pkt.data += ret;
530 static void mediacodec_decode_flush(AVCodecContext *avctx)
532 MediaCodecH264DecContext *s = avctx->priv_data;
534 while (av_fifo_size(s->fifo)) {
536 av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
537 av_packet_unref(&pkt);
539 av_fifo_reset(s->fifo);
541 av_packet_unref(&s->filtered_pkt);
543 ff_mediacodec_dec_flush(avctx, s->ctx);
546 #if CONFIG_H264_MEDIACODEC_DECODER
547 AVCodec ff_h264_mediacodec_decoder = {
548 .name = "h264_mediacodec",
549 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
550 .type = AVMEDIA_TYPE_VIDEO,
551 .id = AV_CODEC_ID_H264,
552 .priv_data_size = sizeof(MediaCodecH264DecContext),
553 .init = mediacodec_decode_init,
554 .decode = mediacodec_decode_frame,
555 .flush = mediacodec_decode_flush,
556 .close = mediacodec_decode_close,
557 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
558 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
562 #if CONFIG_HEVC_MEDIACODEC_DECODER
563 AVCodec ff_hevc_mediacodec_decoder = {
564 .name = "hevc_mediacodec",
565 .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
566 .type = AVMEDIA_TYPE_VIDEO,
567 .id = AV_CODEC_ID_HEVC,
568 .priv_data_size = sizeof(MediaCodecH264DecContext),
569 .init = mediacodec_decode_init,
570 .decode = mediacodec_decode_frame,
571 .flush = mediacodec_decode_flush,
572 .close = mediacodec_decode_close,
573 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
574 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
578 #if CONFIG_MPEG4_MEDIACODEC_DECODER
579 AVCodec ff_mpeg4_mediacodec_decoder = {
580 .name = "mpeg4_mediacodec",
581 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
582 .type = AVMEDIA_TYPE_VIDEO,
583 .id = AV_CODEC_ID_MPEG4,
584 .priv_data_size = sizeof(MediaCodecH264DecContext),
585 .init = mediacodec_decode_init,
586 .decode = mediacodec_decode_frame,
587 .flush = mediacodec_decode_flush,
588 .close = mediacodec_decode_close,
589 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
590 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
594 #if CONFIG_VP8_MEDIACODEC_DECODER
595 AVCodec ff_vp8_mediacodec_decoder = {
596 .name = "vp8_mediacodec",
597 .long_name = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
598 .type = AVMEDIA_TYPE_VIDEO,
599 .id = AV_CODEC_ID_VP8,
600 .priv_data_size = sizeof(MediaCodecH264DecContext),
601 .init = mediacodec_decode_init,
602 .decode = mediacodec_decode_frame,
603 .flush = mediacodec_decode_flush,
604 .close = mediacodec_decode_close,
605 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
606 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
610 #if CONFIG_VP9_MEDIACODEC_DECODER
611 AVCodec ff_vp9_mediacodec_decoder = {
612 .name = "vp9_mediacodec",
613 .long_name = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
614 .type = AVMEDIA_TYPE_VIDEO,
615 .id = AV_CODEC_ID_VP9,
616 .priv_data_size = sizeof(MediaCodecH264DecContext),
617 .init = mediacodec_decode_init,
618 .decode = mediacodec_decode_frame,
619 .flush = mediacodec_decode_flush,
620 .close = mediacodec_decode_close,
621 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
622 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,