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/opt.h"
29 #include "libavutil/intreadwrite.h"
30 #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;
45 AVPacket buffered_pkt;
47 } MediaCodecH264DecContext;
49 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
51 MediaCodecH264DecContext *s = avctx->priv_data;
53 ff_mediacodec_dec_close(avctx, s->ctx);
56 av_packet_unref(&s->buffered_pkt);
61 #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
62 static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
67 static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
69 if (!out || !out_size) {
70 return AVERROR(EINVAL);
73 p = av_malloc(sizeof(nalu_header) + src_size);
75 return AVERROR(ENOMEM);
79 *out_size = sizeof(nalu_header) + src_size;
81 memcpy(p, nalu_header, sizeof(nalu_header));
82 memcpy(p + sizeof(nalu_header), src, src_size);
84 /* Escape 0x00, 0x00, 0x0{0-3} pattern */
85 for (i = 4; i < *out_size; i++) {
86 if (i < *out_size - 3 &&
93 new = av_realloc(*out, *out_size);
95 ret = AVERROR(ENOMEM);
101 memmove(p + i + 1, p + i, *out_size - (i + 1));
115 #if CONFIG_H264_MEDIACODEC_DECODER
116 static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
122 const PPS *pps = NULL;
123 const SPS *sps = NULL;
125 int nal_length_size = 0;
127 memset(&ps, 0, sizeof(ps));
129 ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
130 &ps, &is_avc, &nal_length_size, 0, avctx);
135 for (i = 0; i < MAX_PPS_COUNT; i++) {
136 if (ps.pps_list[i]) {
137 pps = (const PPS*)ps.pps_list[i]->data;
143 if (ps.sps_list[pps->sps_id]) {
144 sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
149 uint8_t *data = NULL;
152 if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
155 ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
158 if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
161 ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
164 av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
165 ret = AVERROR_INVALIDDATA;
169 ff_h264_ps_uninit(&ps);
175 #if CONFIG_HEVC_MEDIACODEC_DECODER
176 static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
184 const HEVCVPS *vps = NULL;
185 const HEVCPPS *pps = NULL;
186 const HEVCSPS *sps = NULL;
188 int nal_length_size = 0;
190 uint8_t *vps_data = NULL;
191 uint8_t *sps_data = NULL;
192 uint8_t *pps_data = NULL;
193 int vps_data_size = 0;
194 int sps_data_size = 0;
195 int pps_data_size = 0;
197 memset(&ps, 0, sizeof(ps));
198 memset(&sei, 0, sizeof(sei));
200 ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
201 &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
206 for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
207 if (ps.vps_list[i]) {
208 vps = (const HEVCVPS*)ps.vps_list[i]->data;
213 for (i = 0; i < HEVC_MAX_PPS_COUNT; i++) {
214 if (ps.pps_list[i]) {
215 pps = (const HEVCPPS*)ps.pps_list[i]->data;
221 if (ps.sps_list[pps->sps_id]) {
222 sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
226 if (vps && pps && sps) {
230 if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
231 (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
232 (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
236 data_size = vps_data_size + sps_data_size + pps_data_size;
237 data = av_mallocz(data_size);
239 ret = AVERROR(ENOMEM);
243 memcpy(data , vps_data, vps_data_size);
244 memcpy(data + vps_data_size , sps_data, sps_data_size);
245 memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
247 ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
251 av_log(avctx, AV_LOG_ERROR, "Could not extract VPS/PPS/SPS from extradata");
252 ret = AVERROR_INVALIDDATA;
256 ff_hevc_ps_uninit(&ps);
266 #if CONFIG_MPEG2_MEDIACODEC_DECODER
267 static int mpeg2_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
271 if (avctx->extradata) {
272 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
279 #if CONFIG_MPEG4_MEDIACODEC_DECODER
280 static int mpeg4_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
284 if (avctx->extradata) {
285 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
292 #if CONFIG_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
293 static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
297 if (avctx->extradata) {
298 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
305 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
309 const char *codec_mime = NULL;
311 FFAMediaFormat *format = NULL;
312 MediaCodecH264DecContext *s = avctx->priv_data;
314 format = ff_AMediaFormat_new();
316 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
317 ret = AVERROR_EXTERNAL;
321 switch (avctx->codec_id) {
322 #if CONFIG_H264_MEDIACODEC_DECODER
323 case AV_CODEC_ID_H264:
324 codec_mime = "video/avc";
326 ret = h264_set_extradata(avctx, format);
331 #if CONFIG_HEVC_MEDIACODEC_DECODER
332 case AV_CODEC_ID_HEVC:
333 codec_mime = "video/hevc";
335 ret = hevc_set_extradata(avctx, format);
340 #if CONFIG_MPEG2_MEDIACODEC_DECODER
341 case AV_CODEC_ID_MPEG2VIDEO:
342 codec_mime = "video/mpeg2";
344 ret = mpeg2_set_extradata(avctx, format);
349 #if CONFIG_MPEG4_MEDIACODEC_DECODER
350 case AV_CODEC_ID_MPEG4:
351 codec_mime = "video/mp4v-es",
353 ret = mpeg4_set_extradata(avctx, format);
358 #if CONFIG_VP8_MEDIACODEC_DECODER
359 case AV_CODEC_ID_VP8:
360 codec_mime = "video/x-vnd.on2.vp8";
362 ret = vpx_set_extradata(avctx, format);
367 #if CONFIG_VP9_MEDIACODEC_DECODER
368 case AV_CODEC_ID_VP9:
369 codec_mime = "video/x-vnd.on2.vp9";
371 ret = vpx_set_extradata(avctx, format);
380 ff_AMediaFormat_setString(format, "mime", codec_mime);
381 ff_AMediaFormat_setInt32(format, "width", avctx->width);
382 ff_AMediaFormat_setInt32(format, "height", avctx->height);
384 s->ctx = av_mallocz(sizeof(*s->ctx));
386 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
387 ret = AVERROR(ENOMEM);
391 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
396 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
400 ff_AMediaFormat_delete(format);
404 mediacodec_decode_close(avctx);
410 static int mediacodec_send_receive(AVCodecContext *avctx,
411 MediaCodecH264DecContext *s,
412 AVFrame *frame, bool wait)
416 /* send any pending data from buffered packet */
417 while (s->buffered_pkt.size) {
418 ret = ff_mediacodec_dec_send(avctx, s->ctx, &s->buffered_pkt);
419 if (ret == AVERROR(EAGAIN))
423 s->buffered_pkt.size -= ret;
424 s->buffered_pkt.data += ret;
425 if (s->buffered_pkt.size <= 0)
426 av_packet_unref(&s->buffered_pkt);
429 /* check for new frame */
430 return ff_mediacodec_dec_receive(avctx, s->ctx, frame, wait);
433 static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
435 MediaCodecH264DecContext *s = avctx->priv_data;
439 * MediaCodec.flush() discards both input and output buffers, thus we
440 * need to delay the call to this function until the user has released or
441 * renderered the frames he retains.
443 * After we have buffered an input packet, check if the codec is in the
444 * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
446 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
447 * the codec (because the user retains frames). The codec stays in the
450 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
451 * performed on the codec. The codec leaves the flushing state and can
452 * process again packets.
454 * ff_mediacodec_dec_flush returns a negative value if an error has
458 if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
459 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
460 return AVERROR(EAGAIN);
464 /* flush buffered packet and check for new frame */
465 ret = mediacodec_send_receive(avctx, s, frame, false);
466 if (ret != AVERROR(EAGAIN))
469 /* skip fetching new packet if we still have one buffered */
470 if (s->buffered_pkt.size > 0)
471 return AVERROR(EAGAIN);
473 /* fetch new packet or eof */
474 ret = ff_decode_get_packet(avctx, &s->buffered_pkt);
475 if (ret == AVERROR_EOF) {
476 AVPacket null_pkt = { 0 };
477 ret = ff_mediacodec_dec_send(avctx, s->ctx, &null_pkt);
484 /* crank decoder with new packet */
485 return mediacodec_send_receive(avctx, s, frame, true);
488 static void mediacodec_decode_flush(AVCodecContext *avctx)
490 MediaCodecH264DecContext *s = avctx->priv_data;
492 av_packet_unref(&s->buffered_pkt);
494 ff_mediacodec_dec_flush(avctx, s->ctx);
497 static const AVCodecHWConfigInternal *mediacodec_hw_configs[] = {
498 &(const AVCodecHWConfigInternal) {
500 .pix_fmt = AV_PIX_FMT_MEDIACODEC,
501 .methods = AV_CODEC_HW_CONFIG_METHOD_AD_HOC |
502 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
503 .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
510 #if CONFIG_H264_MEDIACODEC_DECODER
511 AVCodec ff_h264_mediacodec_decoder = {
512 .name = "h264_mediacodec",
513 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
514 .type = AVMEDIA_TYPE_VIDEO,
515 .id = AV_CODEC_ID_H264,
516 .priv_data_size = sizeof(MediaCodecH264DecContext),
517 .init = mediacodec_decode_init,
518 .receive_frame = mediacodec_receive_frame,
519 .flush = mediacodec_decode_flush,
520 .close = mediacodec_decode_close,
521 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
522 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
523 .bsfs = "h264_mp4toannexb",
524 .hw_configs = mediacodec_hw_configs,
525 .wrapper_name = "mediacodec",
529 #if CONFIG_HEVC_MEDIACODEC_DECODER
530 AVCodec ff_hevc_mediacodec_decoder = {
531 .name = "hevc_mediacodec",
532 .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
533 .type = AVMEDIA_TYPE_VIDEO,
534 .id = AV_CODEC_ID_HEVC,
535 .priv_data_size = sizeof(MediaCodecH264DecContext),
536 .init = mediacodec_decode_init,
537 .receive_frame = mediacodec_receive_frame,
538 .flush = mediacodec_decode_flush,
539 .close = mediacodec_decode_close,
540 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
541 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
542 .bsfs = "hevc_mp4toannexb",
543 .hw_configs = mediacodec_hw_configs,
544 .wrapper_name = "mediacodec",
548 #if CONFIG_MPEG2_MEDIACODEC_DECODER
549 AVCodec ff_mpeg2_mediacodec_decoder = {
550 .name = "mpeg2_mediacodec",
551 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 Android MediaCodec decoder"),
552 .type = AVMEDIA_TYPE_VIDEO,
553 .id = AV_CODEC_ID_MPEG2VIDEO,
554 .priv_data_size = sizeof(MediaCodecH264DecContext),
555 .init = mediacodec_decode_init,
556 .receive_frame = mediacodec_receive_frame,
557 .flush = mediacodec_decode_flush,
558 .close = mediacodec_decode_close,
559 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
560 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
561 .hw_configs = mediacodec_hw_configs,
562 .wrapper_name = "mediacodec",
566 #if CONFIG_MPEG4_MEDIACODEC_DECODER
567 AVCodec ff_mpeg4_mediacodec_decoder = {
568 .name = "mpeg4_mediacodec",
569 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
570 .type = AVMEDIA_TYPE_VIDEO,
571 .id = AV_CODEC_ID_MPEG4,
572 .priv_data_size = sizeof(MediaCodecH264DecContext),
573 .init = mediacodec_decode_init,
574 .receive_frame = mediacodec_receive_frame,
575 .flush = mediacodec_decode_flush,
576 .close = mediacodec_decode_close,
577 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
578 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
579 .hw_configs = mediacodec_hw_configs,
580 .wrapper_name = "mediacodec",
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 .receive_frame = mediacodec_receive_frame,
593 .flush = mediacodec_decode_flush,
594 .close = mediacodec_decode_close,
595 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
596 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
597 .hw_configs = mediacodec_hw_configs,
598 .wrapper_name = "mediacodec",
602 #if CONFIG_VP9_MEDIACODEC_DECODER
603 AVCodec ff_vp9_mediacodec_decoder = {
604 .name = "vp9_mediacodec",
605 .long_name = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
606 .type = AVMEDIA_TYPE_VIDEO,
607 .id = AV_CODEC_ID_VP9,
608 .priv_data_size = sizeof(MediaCodecH264DecContext),
609 .init = mediacodec_decode_init,
610 .receive_frame = mediacodec_receive_frame,
611 .flush = mediacodec_decode_flush,
612 .close = mediacodec_decode_close,
613 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE,
614 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
615 .hw_configs = mediacodec_hw_configs,
616 .wrapper_name = "mediacodec",