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"
38 #include "mediacodec_wrapper.h"
39 #include "mediacodecdec_common.h"
41 typedef struct MediaCodecH264DecContext {
43 MediaCodecDecContext *ctx;
47 AVPacket buffered_pkt;
49 } MediaCodecH264DecContext;
51 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
53 MediaCodecH264DecContext *s = avctx->priv_data;
55 ff_mediacodec_dec_close(avctx, s->ctx);
58 av_fifo_free(s->fifo);
60 av_packet_unref(&s->buffered_pkt);
65 #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
66 static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
71 static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
73 if (!out || !out_size) {
74 return AVERROR(EINVAL);
77 p = av_malloc(sizeof(nalu_header) + src_size);
79 return AVERROR(ENOMEM);
83 *out_size = sizeof(nalu_header) + src_size;
85 memcpy(p, nalu_header, sizeof(nalu_header));
86 memcpy(p + sizeof(nalu_header), src, src_size);
88 /* Escape 0x00, 0x00, 0x0{0-3} pattern */
89 for (i = 4; i < *out_size; i++) {
90 if (i < *out_size - 3 &&
97 new = av_realloc(*out, *out_size);
99 ret = AVERROR(ENOMEM);
105 memmove(p + i + 1, p + i, *out_size - (i + 1));
119 #if CONFIG_H264_MEDIACODEC_DECODER
120 static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
126 const PPS *pps = NULL;
127 const SPS *sps = NULL;
129 int nal_length_size = 0;
131 memset(&ps, 0, sizeof(ps));
133 ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
134 &ps, &is_avc, &nal_length_size, 0, avctx);
139 for (i = 0; i < MAX_PPS_COUNT; i++) {
140 if (ps.pps_list[i]) {
141 pps = (const PPS*)ps.pps_list[i]->data;
147 if (ps.sps_list[pps->sps_id]) {
148 sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
153 uint8_t *data = NULL;
156 if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
159 ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
162 if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
165 ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
168 av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
169 ret = AVERROR_INVALIDDATA;
173 ff_h264_ps_uninit(&ps);
179 #if CONFIG_HEVC_MEDIACODEC_DECODER
180 static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
188 const HEVCVPS *vps = NULL;
189 const HEVCPPS *pps = NULL;
190 const HEVCSPS *sps = NULL;
192 int nal_length_size = 0;
194 uint8_t *vps_data = NULL;
195 uint8_t *sps_data = NULL;
196 uint8_t *pps_data = NULL;
197 int vps_data_size = 0;
198 int sps_data_size = 0;
199 int pps_data_size = 0;
201 memset(&ps, 0, sizeof(ps));
202 memset(&sei, 0, sizeof(sei));
204 ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
205 &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
210 for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
211 if (ps.vps_list[i]) {
212 vps = (const HEVCVPS*)ps.vps_list[i]->data;
217 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
218 if (ps.pps_list[i]) {
219 pps = (const HEVCPPS*)ps.pps_list[i]->data;
225 if (ps.sps_list[pps->sps_id]) {
226 sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
230 if (vps && pps && sps) {
234 if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
235 (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
236 (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
240 data_size = vps_data_size + sps_data_size + pps_data_size;
241 data = av_mallocz(data_size);
243 ret = AVERROR(ENOMEM);
247 memcpy(data , vps_data, vps_data_size);
248 memcpy(data + vps_data_size , sps_data, sps_data_size);
249 memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
251 ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
255 av_log(avctx, AV_LOG_ERROR, "Could not extract VPS/PPS/SPS from extradata");
256 ret = AVERROR_INVALIDDATA;
268 #if CONFIG_MPEG2_MEDIACODEC_DECODER
269 static int mpeg2_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
273 if (avctx->extradata) {
274 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
281 #if CONFIG_MPEG4_MEDIACODEC_DECODER
282 static int mpeg4_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
286 if (avctx->extradata) {
287 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
294 #if CONFIG_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
295 static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
299 if (avctx->extradata) {
300 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
307 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
311 const char *codec_mime = NULL;
313 FFAMediaFormat *format = NULL;
314 MediaCodecH264DecContext *s = avctx->priv_data;
316 format = ff_AMediaFormat_new();
318 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
319 ret = AVERROR_EXTERNAL;
323 switch (avctx->codec_id) {
324 #if CONFIG_H264_MEDIACODEC_DECODER
325 case AV_CODEC_ID_H264:
326 codec_mime = "video/avc";
328 ret = h264_set_extradata(avctx, format);
333 #if CONFIG_HEVC_MEDIACODEC_DECODER
334 case AV_CODEC_ID_HEVC:
335 codec_mime = "video/hevc";
337 ret = hevc_set_extradata(avctx, format);
342 #if CONFIG_MPEG2_MEDIACODEC_DECODER
343 case AV_CODEC_ID_MPEG2VIDEO:
344 codec_mime = "video/mpeg2";
346 ret = mpeg2_set_extradata(avctx, format);
351 #if CONFIG_MPEG4_MEDIACODEC_DECODER
352 case AV_CODEC_ID_MPEG4:
353 codec_mime = "video/mp4v-es",
355 ret = mpeg4_set_extradata(avctx, format);
360 #if CONFIG_VP8_MEDIACODEC_DECODER
361 case AV_CODEC_ID_VP8:
362 codec_mime = "video/x-vnd.on2.vp8";
364 ret = vpx_set_extradata(avctx, format);
369 #if CONFIG_VP9_MEDIACODEC_DECODER
370 case AV_CODEC_ID_VP9:
371 codec_mime = "video/x-vnd.on2.vp9";
373 ret = vpx_set_extradata(avctx, format);
382 ff_AMediaFormat_setString(format, "mime", codec_mime);
383 ff_AMediaFormat_setInt32(format, "width", avctx->width);
384 ff_AMediaFormat_setInt32(format, "height", avctx->height);
386 s->ctx = av_mallocz(sizeof(*s->ctx));
388 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
389 ret = AVERROR(ENOMEM);
393 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
398 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
400 s->fifo = av_fifo_alloc(sizeof(AVPacket));
402 ret = AVERROR(ENOMEM);
408 ff_AMediaFormat_delete(format);
412 mediacodec_decode_close(avctx);
419 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
420 int *got_frame, AVPacket *pkt)
422 MediaCodecH264DecContext *s = avctx->priv_data;
424 return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
427 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
428 int *got_frame, AVPacket *avpkt)
430 MediaCodecH264DecContext *s = avctx->priv_data;
431 AVFrame *frame = data;
434 /* buffer the input packet */
436 AVPacket input_pkt = { 0 };
438 if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
439 ret = av_fifo_realloc2(s->fifo,
440 av_fifo_size(s->fifo) + sizeof(input_pkt));
445 ret = av_packet_ref(&input_pkt, avpkt);
448 av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
452 * MediaCodec.flush() discards both input and output buffers, thus we
453 * need to delay the call to this function until the user has released or
454 * renderered the frames he retains.
456 * After we have buffered an input packet, check if the codec is in the
457 * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
459 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
460 * the codec (because the user retains frames). The codec stays in the
463 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
464 * performed on the codec. The codec leaves the flushing state and can
465 * process again packets.
467 * ff_mediacodec_dec_flush returns a negative value if an error has
471 if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
472 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
477 /* process buffered data */
478 while (!*got_frame) {
479 /* prepare the input data */
480 if (s->buffered_pkt.size <= 0) {
481 av_packet_unref(&s->buffered_pkt);
484 if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
485 return avpkt->size ? avpkt->size :
486 ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
489 av_fifo_generic_read(s->fifo, &s->buffered_pkt, sizeof(s->buffered_pkt), NULL);
492 ret = mediacodec_process_data(avctx, frame, got_frame, &s->buffered_pkt);
496 s->buffered_pkt.size -= ret;
497 s->buffered_pkt.data += ret;
503 static void mediacodec_decode_flush(AVCodecContext *avctx)
505 MediaCodecH264DecContext *s = avctx->priv_data;
507 while (av_fifo_size(s->fifo)) {
509 av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
510 av_packet_unref(&pkt);
512 av_fifo_reset(s->fifo);
514 av_packet_unref(&s->buffered_pkt);
516 ff_mediacodec_dec_flush(avctx, s->ctx);
519 static const AVCodecHWConfigInternal *mediacodec_hw_configs[] = {
520 &(const AVCodecHWConfigInternal) {
522 .pix_fmt = AV_PIX_FMT_MEDIACODEC,
523 .methods = AV_CODEC_HW_CONFIG_METHOD_AD_HOC,
524 .device_type = AV_HWDEVICE_TYPE_NONE,
531 #if CONFIG_H264_MEDIACODEC_DECODER
532 AVCodec ff_h264_mediacodec_decoder = {
533 .name = "h264_mediacodec",
534 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
535 .type = AVMEDIA_TYPE_VIDEO,
536 .id = AV_CODEC_ID_H264,
537 .priv_data_size = sizeof(MediaCodecH264DecContext),
538 .init = mediacodec_decode_init,
539 .decode = mediacodec_decode_frame,
540 .flush = mediacodec_decode_flush,
541 .close = mediacodec_decode_close,
542 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
543 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
544 .bsfs = "h264_mp4toannexb",
545 .hw_configs = mediacodec_hw_configs,
549 #if CONFIG_HEVC_MEDIACODEC_DECODER
550 AVCodec ff_hevc_mediacodec_decoder = {
551 .name = "hevc_mediacodec",
552 .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
553 .type = AVMEDIA_TYPE_VIDEO,
554 .id = AV_CODEC_ID_HEVC,
555 .priv_data_size = sizeof(MediaCodecH264DecContext),
556 .init = mediacodec_decode_init,
557 .decode = mediacodec_decode_frame,
558 .flush = mediacodec_decode_flush,
559 .close = mediacodec_decode_close,
560 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
561 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
562 .bsfs = "hevc_mp4toannexb",
563 .hw_configs = mediacodec_hw_configs,
567 #if CONFIG_MPEG2_MEDIACODEC_DECODER
568 AVCodec ff_mpeg2_mediacodec_decoder = {
569 .name = "mpeg2_mediacodec",
570 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Android MediaCodec decoder"),
571 .type = AVMEDIA_TYPE_VIDEO,
572 .id = AV_CODEC_ID_MPEG2VIDEO,
573 .priv_data_size = sizeof(MediaCodecH264DecContext),
574 .init = mediacodec_decode_init,
575 .decode = mediacodec_decode_frame,
576 .flush = mediacodec_decode_flush,
577 .close = mediacodec_decode_close,
578 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
579 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
580 .hw_configs = mediacodec_hw_configs,
584 #if CONFIG_MPEG4_MEDIACODEC_DECODER
585 AVCodec ff_mpeg4_mediacodec_decoder = {
586 .name = "mpeg4_mediacodec",
587 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
588 .type = AVMEDIA_TYPE_VIDEO,
589 .id = AV_CODEC_ID_MPEG4,
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,
597 .hw_configs = mediacodec_hw_configs,
601 #if CONFIG_VP8_MEDIACODEC_DECODER
602 AVCodec ff_vp8_mediacodec_decoder = {
603 .name = "vp8_mediacodec",
604 .long_name = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
605 .type = AVMEDIA_TYPE_VIDEO,
606 .id = AV_CODEC_ID_VP8,
607 .priv_data_size = sizeof(MediaCodecH264DecContext),
608 .init = mediacodec_decode_init,
609 .decode = mediacodec_decode_frame,
610 .flush = mediacodec_decode_flush,
611 .close = mediacodec_decode_close,
612 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
613 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
614 .hw_configs = mediacodec_hw_configs,
618 #if CONFIG_VP9_MEDIACODEC_DECODER
619 AVCodec ff_vp9_mediacodec_decoder = {
620 .name = "vp9_mediacodec",
621 .long_name = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
622 .type = AVMEDIA_TYPE_VIDEO,
623 .id = AV_CODEC_ID_VP9,
624 .priv_data_size = sizeof(MediaCodecH264DecContext),
625 .init = mediacodec_decode_init,
626 .decode = mediacodec_decode_frame,
627 .flush = mediacodec_decode_flush,
628 .close = mediacodec_decode_close,
629 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
630 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
631 .hw_configs = mediacodec_hw_configs,