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;
46 AVPacket buffered_pkt;
48 } MediaCodecH264DecContext;
50 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
52 MediaCodecH264DecContext *s = avctx->priv_data;
54 ff_mediacodec_dec_close(avctx, s->ctx);
57 av_fifo_free(s->fifo);
59 av_packet_unref(&s->buffered_pkt);
64 #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
65 static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
70 static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
72 if (!out || !out_size) {
73 return AVERROR(EINVAL);
76 p = av_malloc(sizeof(nalu_header) + src_size);
78 return AVERROR(ENOMEM);
82 *out_size = sizeof(nalu_header) + src_size;
84 memcpy(p, nalu_header, sizeof(nalu_header));
85 memcpy(p + sizeof(nalu_header), src, src_size);
87 /* Escape 0x00, 0x00, 0x0{0-3} pattern */
88 for (i = 4; i < *out_size; i++) {
89 if (i < *out_size - 3 &&
96 new = av_realloc(*out, *out_size);
98 ret = AVERROR(ENOMEM);
104 memmove(p + i + 1, p + i, *out_size - (i + 1));
118 #if CONFIG_H264_MEDIACODEC_DECODER
119 static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
125 const PPS *pps = NULL;
126 const SPS *sps = NULL;
128 int nal_length_size = 0;
130 memset(&ps, 0, sizeof(ps));
132 ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
133 &ps, &is_avc, &nal_length_size, 0, avctx);
138 for (i = 0; i < MAX_PPS_COUNT; i++) {
139 if (ps.pps_list[i]) {
140 pps = (const PPS*)ps.pps_list[i]->data;
146 if (ps.sps_list[pps->sps_id]) {
147 sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
152 uint8_t *data = NULL;
155 if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
158 ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
161 if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
164 ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
167 av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
168 ret = AVERROR_INVALIDDATA;
172 ff_h264_ps_uninit(&ps);
178 #if CONFIG_HEVC_MEDIACODEC_DECODER
179 static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
187 const HEVCVPS *vps = NULL;
188 const HEVCPPS *pps = NULL;
189 const HEVCSPS *sps = NULL;
191 int nal_length_size = 0;
193 uint8_t *vps_data = NULL;
194 uint8_t *sps_data = NULL;
195 uint8_t *pps_data = NULL;
196 int vps_data_size = 0;
197 int sps_data_size = 0;
198 int pps_data_size = 0;
200 memset(&ps, 0, sizeof(ps));
201 memset(&sei, 0, sizeof(sei));
203 ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
204 &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
209 for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
210 if (ps.vps_list[i]) {
211 vps = (const HEVCVPS*)ps.vps_list[i]->data;
216 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
217 if (ps.pps_list[i]) {
218 pps = (const HEVCPPS*)ps.pps_list[i]->data;
224 if (ps.sps_list[pps->sps_id]) {
225 sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
229 if (vps && pps && sps) {
233 if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
234 (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
235 (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
239 data_size = vps_data_size + sps_data_size + pps_data_size;
240 data = av_mallocz(data_size);
242 ret = AVERROR(ENOMEM);
246 memcpy(data , vps_data, vps_data_size);
247 memcpy(data + vps_data_size , sps_data, sps_data_size);
248 memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
250 ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
254 av_log(avctx, AV_LOG_ERROR, "Could not extract VPS/PPS/SPS from extradata");
255 ret = AVERROR_INVALIDDATA;
267 #if CONFIG_MPEG2_MEDIACODEC_DECODER
268 static int mpeg2_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
272 if (avctx->extradata) {
273 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
280 #if CONFIG_MPEG4_MEDIACODEC_DECODER
281 static int mpeg4_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
285 if (avctx->extradata) {
286 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
293 #if CONFIG_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
294 static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
298 if (avctx->extradata) {
299 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
306 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
310 const char *codec_mime = NULL;
312 FFAMediaFormat *format = NULL;
313 MediaCodecH264DecContext *s = avctx->priv_data;
315 format = ff_AMediaFormat_new();
317 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
318 ret = AVERROR_EXTERNAL;
322 switch (avctx->codec_id) {
323 #if CONFIG_H264_MEDIACODEC_DECODER
324 case AV_CODEC_ID_H264:
325 codec_mime = "video/avc";
327 ret = h264_set_extradata(avctx, format);
332 #if CONFIG_HEVC_MEDIACODEC_DECODER
333 case AV_CODEC_ID_HEVC:
334 codec_mime = "video/hevc";
336 ret = hevc_set_extradata(avctx, format);
341 #if CONFIG_MPEG2_MEDIACODEC_DECODER
342 case AV_CODEC_ID_MPEG2VIDEO:
343 codec_mime = "video/mpeg2";
345 ret = mpeg2_set_extradata(avctx, format);
350 #if CONFIG_MPEG4_MEDIACODEC_DECODER
351 case AV_CODEC_ID_MPEG4:
352 codec_mime = "video/mp4v-es",
354 ret = mpeg4_set_extradata(avctx, format);
359 #if CONFIG_VP8_MEDIACODEC_DECODER
360 case AV_CODEC_ID_VP8:
361 codec_mime = "video/x-vnd.on2.vp8";
363 ret = vpx_set_extradata(avctx, format);
368 #if CONFIG_VP9_MEDIACODEC_DECODER
369 case AV_CODEC_ID_VP9:
370 codec_mime = "video/x-vnd.on2.vp9";
372 ret = vpx_set_extradata(avctx, format);
381 ff_AMediaFormat_setString(format, "mime", codec_mime);
382 ff_AMediaFormat_setInt32(format, "width", avctx->width);
383 ff_AMediaFormat_setInt32(format, "height", avctx->height);
385 s->ctx = av_mallocz(sizeof(*s->ctx));
387 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
388 ret = AVERROR(ENOMEM);
392 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
397 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
399 s->fifo = av_fifo_alloc(sizeof(AVPacket));
401 ret = AVERROR(ENOMEM);
407 ff_AMediaFormat_delete(format);
411 mediacodec_decode_close(avctx);
418 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
419 int *got_frame, AVPacket *pkt)
421 MediaCodecH264DecContext *s = avctx->priv_data;
423 return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
426 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
427 int *got_frame, AVPacket *avpkt)
429 MediaCodecH264DecContext *s = avctx->priv_data;
430 AVFrame *frame = data;
433 /* buffer the input packet */
435 AVPacket input_pkt = { 0 };
437 if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
438 ret = av_fifo_realloc2(s->fifo,
439 av_fifo_size(s->fifo) + sizeof(input_pkt));
444 ret = av_packet_ref(&input_pkt, avpkt);
447 av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
451 * MediaCodec.flush() discards both input and output buffers, thus we
452 * need to delay the call to this function until the user has released or
453 * renderered the frames he retains.
455 * After we have buffered an input packet, check if the codec is in the
456 * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
458 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
459 * the codec (because the user retains frames). The codec stays in the
462 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
463 * performed on the codec. The codec leaves the flushing state and can
464 * process again packets.
466 * ff_mediacodec_dec_flush returns a negative value if an error has
470 if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
471 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
476 /* process buffered data */
477 while (!*got_frame) {
478 /* prepare the input data */
479 if (s->buffered_pkt.size <= 0) {
480 av_packet_unref(&s->buffered_pkt);
483 if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
484 return avpkt->size ? avpkt->size :
485 ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
488 av_fifo_generic_read(s->fifo, &s->buffered_pkt, sizeof(s->buffered_pkt), NULL);
491 ret = mediacodec_process_data(avctx, frame, got_frame, &s->buffered_pkt);
495 s->buffered_pkt.size -= ret;
496 s->buffered_pkt.data += ret;
502 static void mediacodec_decode_flush(AVCodecContext *avctx)
504 MediaCodecH264DecContext *s = avctx->priv_data;
506 while (av_fifo_size(s->fifo)) {
508 av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
509 av_packet_unref(&pkt);
511 av_fifo_reset(s->fifo);
513 av_packet_unref(&s->buffered_pkt);
515 ff_mediacodec_dec_flush(avctx, s->ctx);
518 #if CONFIG_H264_MEDIACODEC_DECODER
519 AVCodec ff_h264_mediacodec_decoder = {
520 .name = "h264_mediacodec",
521 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
522 .type = AVMEDIA_TYPE_VIDEO,
523 .id = AV_CODEC_ID_H264,
524 .priv_data_size = sizeof(MediaCodecH264DecContext),
525 .init = mediacodec_decode_init,
526 .decode = mediacodec_decode_frame,
527 .flush = mediacodec_decode_flush,
528 .close = mediacodec_decode_close,
529 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
530 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
531 .bsfs = "h264_mp4toannexb",
535 #if CONFIG_HEVC_MEDIACODEC_DECODER
536 AVCodec ff_hevc_mediacodec_decoder = {
537 .name = "hevc_mediacodec",
538 .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
539 .type = AVMEDIA_TYPE_VIDEO,
540 .id = AV_CODEC_ID_HEVC,
541 .priv_data_size = sizeof(MediaCodecH264DecContext),
542 .init = mediacodec_decode_init,
543 .decode = mediacodec_decode_frame,
544 .flush = mediacodec_decode_flush,
545 .close = mediacodec_decode_close,
546 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
547 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
548 .bsfs = "hevc_mp4toannexb",
552 #if CONFIG_MPEG2_MEDIACODEC_DECODER
553 AVCodec ff_mpeg2_mediacodec_decoder = {
554 .name = "mpeg2_mediacodec",
555 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Android MediaCodec decoder"),
556 .type = AVMEDIA_TYPE_VIDEO,
557 .id = AV_CODEC_ID_MPEG2VIDEO,
558 .priv_data_size = sizeof(MediaCodecH264DecContext),
559 .init = mediacodec_decode_init,
560 .decode = mediacodec_decode_frame,
561 .flush = mediacodec_decode_flush,
562 .close = mediacodec_decode_close,
563 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
564 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
568 #if CONFIG_MPEG4_MEDIACODEC_DECODER
569 AVCodec ff_mpeg4_mediacodec_decoder = {
570 .name = "mpeg4_mediacodec",
571 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
572 .type = AVMEDIA_TYPE_VIDEO,
573 .id = AV_CODEC_ID_MPEG4,
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_VP8_MEDIACODEC_DECODER
585 AVCodec ff_vp8_mediacodec_decoder = {
586 .name = "vp8_mediacodec",
587 .long_name = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
588 .type = AVMEDIA_TYPE_VIDEO,
589 .id = AV_CODEC_ID_VP8,
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_VP9_MEDIACODEC_DECODER
601 AVCodec ff_vp9_mediacodec_decoder = {
602 .name = "vp9_mediacodec",
603 .long_name = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
604 .type = AVMEDIA_TYPE_VIDEO,
605 .id = AV_CODEC_ID_VP9,
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,