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"
32 #include "libavutil/atomic.h"
35 #include "h264_parse.h"
36 #include "hevc_parse.h"
38 #include "mediacodec_wrapper.h"
39 #include "mediacodecdec_common.h"
41 typedef struct MediaCodecH264DecContext {
43 MediaCodecDecContext *ctx;
49 AVPacket filtered_pkt;
51 } MediaCodecH264DecContext;
53 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
55 MediaCodecH264DecContext *s = avctx->priv_data;
57 ff_mediacodec_dec_close(avctx, s->ctx);
60 av_fifo_free(s->fifo);
63 av_packet_unref(&s->filtered_pkt);
68 #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
69 static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
74 static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
76 if (!out || !out_size) {
77 return AVERROR(EINVAL);
80 p = av_malloc(sizeof(nalu_header) + src_size);
82 return AVERROR(ENOMEM);
86 *out_size = sizeof(nalu_header) + src_size;
88 memcpy(p, nalu_header, sizeof(nalu_header));
89 memcpy(p + sizeof(nalu_header), src, src_size);
91 /* Escape 0x00, 0x00, 0x0{0-3} pattern */
92 for (i = 4; i < *out_size; i++) {
93 if (i < *out_size - 3 &&
100 new = av_realloc(*out, *out_size);
102 ret = AVERROR(ENOMEM);
108 memmove(p + i + 1, p + i, *out_size - (i + 1));
122 #if CONFIG_H264_MEDIACODEC_DECODER
123 static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
129 const PPS *pps = NULL;
130 const SPS *sps = NULL;
132 int nal_length_size = 0;
134 memset(&ps, 0, sizeof(ps));
136 ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
137 &ps, &is_avc, &nal_length_size, 0, avctx);
142 for (i = 0; i < MAX_PPS_COUNT; i++) {
143 if (ps.pps_list[i]) {
144 pps = (const PPS*)ps.pps_list[i]->data;
150 if (ps.sps_list[pps->sps_id]) {
151 sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
156 uint8_t *data = NULL;
159 if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
162 ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
165 if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
168 ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
171 av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
172 ret = AVERROR_INVALIDDATA;
176 ff_h264_ps_uninit(&ps);
182 #if CONFIG_HEVC_MEDIACODEC_DECODER
183 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));
205 ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
206 &ps, &is_nalff, &nal_length_size, 0, avctx);
211 for (i = 0; i < MAX_VPS_COUNT; i++) {
212 if (ps.vps_list[i]) {
213 vps = (const HEVCVPS*)ps.vps_list[i]->data;
218 for (i = 0; i < MAX_PPS_COUNT; i++) {
219 if (ps.pps_list[i]) {
220 pps = (const HEVCPPS*)ps.pps_list[i]->data;
226 if (ps.sps_list[pps->sps_id]) {
227 sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
231 if (vps && pps && sps) {
235 if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
236 (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
237 (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
241 data_size = vps_data_size + sps_data_size + pps_data_size;
242 data = av_mallocz(data_size);
244 ret = AVERROR(ENOMEM);
248 memcpy(data , vps_data, vps_data_size);
249 memcpy(data + vps_data_size , sps_data, sps_data_size);
250 memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
252 ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
256 av_log(avctx, AV_LOG_ERROR, "Could not extract VPS/PPS/SPS from extradata");
257 ret = AVERROR_INVALIDDATA;
269 #if CONFIG_MPEG4_MEDIACODEC_DECODER
270 static int mpeg4_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
274 if (avctx->extradata) {
275 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
282 #if CONFIG_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
283 static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
287 if (avctx->extradata) {
288 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
295 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
299 const char *codec_mime = NULL;
301 const char *bsf_name = NULL;
302 const AVBitStreamFilter *bsf = NULL;
304 FFAMediaFormat *format = NULL;
305 MediaCodecH264DecContext *s = avctx->priv_data;
307 format = ff_AMediaFormat_new();
309 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
310 ret = AVERROR_EXTERNAL;
314 switch (avctx->codec_id) {
315 #if CONFIG_H264_MEDIACODEC_DECODER
316 case AV_CODEC_ID_H264:
317 codec_mime = "video/avc";
318 bsf_name = "h264_mp4toannexb";
320 ret = h264_set_extradata(avctx, format);
325 #if CONFIG_HEVC_MEDIACODEC_DECODER
326 case AV_CODEC_ID_HEVC:
327 codec_mime = "video/hevc";
328 bsf_name = "hevc_mp4toannexb";
330 ret = hevc_set_extradata(avctx, format);
335 #if CONFIG_MPEG4_MEDIACODEC_DECODER
336 case AV_CODEC_ID_MPEG4:
337 codec_mime = "video/mp4v-es",
339 ret = mpeg4_set_extradata(avctx, format);
344 #if CONFIG_VP8_MEDIACODEC_DECODER
345 case AV_CODEC_ID_VP8:
346 codec_mime = "video/x-vnd.on2.vp8";
348 ret = vpx_set_extradata(avctx, format);
353 #if CONFIG_VP9_MEDIACODEC_DECODER
354 case AV_CODEC_ID_VP9:
355 codec_mime = "video/x-vnd.on2.vp9";
357 ret = vpx_set_extradata(avctx, format);
366 ff_AMediaFormat_setString(format, "mime", codec_mime);
367 ff_AMediaFormat_setInt32(format, "width", avctx->width);
368 ff_AMediaFormat_setInt32(format, "height", avctx->height);
370 s->ctx = av_mallocz(sizeof(*s->ctx));
372 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
373 ret = AVERROR(ENOMEM);
377 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
382 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
384 s->fifo = av_fifo_alloc(sizeof(AVPacket));
386 ret = AVERROR(ENOMEM);
391 bsf = av_bsf_get_by_name(bsf_name);
393 ret = AVERROR_BSF_NOT_FOUND;
397 if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
401 if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
402 ((ret = av_bsf_init(s->bsf)) < 0)) {
407 av_init_packet(&s->filtered_pkt);
411 ff_AMediaFormat_delete(format);
415 mediacodec_decode_close(avctx);
422 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
423 int *got_frame, AVPacket *pkt)
425 MediaCodecH264DecContext *s = avctx->priv_data;
427 return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
430 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
431 int *got_frame, AVPacket *avpkt)
433 MediaCodecH264DecContext *s = avctx->priv_data;
434 AVFrame *frame = data;
437 /* buffer the input packet */
439 AVPacket input_pkt = { 0 };
441 if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
442 ret = av_fifo_realloc2(s->fifo,
443 av_fifo_size(s->fifo) + sizeof(input_pkt));
448 ret = av_packet_ref(&input_pkt, avpkt);
451 av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
455 * MediaCodec.flush() discards both input and output buffers, thus we
456 * need to delay the call to this function until the user has released or
457 * renderered the frames he retains.
459 * After we have buffered an input packet, check if the codec is in the
460 * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
462 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
463 * the codec (because the user retains frames). The codec stays in the
466 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
467 * performed on the codec. The codec leaves the flushing state and can
468 * process again packets.
470 * ff_mediacodec_dec_flush returns a negative value if an error has
474 if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
475 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
480 /* process buffered data */
481 while (!*got_frame) {
482 /* prepare the input data -- convert to Annex B if needed */
483 if (s->filtered_pkt.size <= 0) {
484 AVPacket input_pkt = { 0 };
486 av_packet_unref(&s->filtered_pkt);
489 if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
490 return avpkt->size ? avpkt->size :
491 ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
494 av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
497 ret = av_bsf_send_packet(s->bsf, &input_pkt);
502 ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
503 if (ret == AVERROR(EAGAIN)) {
507 av_packet_move_ref(&s->filtered_pkt, &input_pkt);
510 /* {h264,hevc}_mp4toannexb are used here and do not require flushing */
511 av_assert0(ret != AVERROR_EOF);
518 ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
522 s->filtered_pkt.size -= ret;
523 s->filtered_pkt.data += ret;
529 static void mediacodec_decode_flush(AVCodecContext *avctx)
531 MediaCodecH264DecContext *s = avctx->priv_data;
533 while (av_fifo_size(s->fifo)) {
535 av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
536 av_packet_unref(&pkt);
538 av_fifo_reset(s->fifo);
540 av_packet_unref(&s->filtered_pkt);
542 ff_mediacodec_dec_flush(avctx, s->ctx);
545 #if CONFIG_H264_MEDIACODEC_DECODER
546 AVCodec ff_h264_mediacodec_decoder = {
547 .name = "h264_mediacodec",
548 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
549 .type = AVMEDIA_TYPE_VIDEO,
550 .id = AV_CODEC_ID_H264,
551 .priv_data_size = sizeof(MediaCodecH264DecContext),
552 .init = mediacodec_decode_init,
553 .decode = mediacodec_decode_frame,
554 .flush = mediacodec_decode_flush,
555 .close = mediacodec_decode_close,
556 .capabilities = CODEC_CAP_DELAY,
557 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
561 #if CONFIG_HEVC_MEDIACODEC_DECODER
562 AVCodec ff_hevc_mediacodec_decoder = {
563 .name = "hevc_mediacodec",
564 .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
565 .type = AVMEDIA_TYPE_VIDEO,
566 .id = AV_CODEC_ID_HEVC,
567 .priv_data_size = sizeof(MediaCodecH264DecContext),
568 .init = mediacodec_decode_init,
569 .decode = mediacodec_decode_frame,
570 .flush = mediacodec_decode_flush,
571 .close = mediacodec_decode_close,
572 .capabilities = CODEC_CAP_DELAY,
573 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
577 #if CONFIG_MPEG4_MEDIACODEC_DECODER
578 AVCodec ff_mpeg4_mediacodec_decoder = {
579 .name = "mpeg4_mediacodec",
580 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
581 .type = AVMEDIA_TYPE_VIDEO,
582 .id = AV_CODEC_ID_MPEG4,
583 .priv_data_size = sizeof(MediaCodecH264DecContext),
584 .init = mediacodec_decode_init,
585 .decode = mediacodec_decode_frame,
586 .flush = mediacodec_decode_flush,
587 .close = mediacodec_decode_close,
588 .capabilities = CODEC_CAP_DELAY,
589 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
593 #if CONFIG_VP8_MEDIACODEC_DECODER
594 AVCodec ff_vp8_mediacodec_decoder = {
595 .name = "vp8_mediacodec",
596 .long_name = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
597 .type = AVMEDIA_TYPE_VIDEO,
598 .id = AV_CODEC_ID_VP8,
599 .priv_data_size = sizeof(MediaCodecH264DecContext),
600 .init = mediacodec_decode_init,
601 .decode = mediacodec_decode_frame,
602 .flush = mediacodec_decode_flush,
603 .close = mediacodec_decode_close,
604 .capabilities = CODEC_CAP_DELAY,
605 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
609 #if CONFIG_VP9_MEDIACODEC_DECODER
610 AVCodec ff_vp9_mediacodec_decoder = {
611 .name = "vp9_mediacodec",
612 .long_name = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
613 .type = AVMEDIA_TYPE_VIDEO,
614 .id = AV_CODEC_ID_VP9,
615 .priv_data_size = sizeof(MediaCodecH264DecContext),
616 .init = mediacodec_decode_init,
617 .decode = mediacodec_decode_frame,
618 .flush = mediacodec_decode_flush,
619 .close = mediacodec_decode_close,
620 .capabilities = CODEC_CAP_DELAY,
621 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,