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"
31 #include "libavutil/internal.h"
35 #include "h264_parse.h"
36 #include "hevc_parse.h"
39 #include "mediacodec_wrapper.h"
40 #include "mediacodecdec_common.h"
42 typedef struct MediaCodecH264DecContext {
46 MediaCodecDecContext *ctx;
48 AVPacket buffered_pkt;
52 } MediaCodecH264DecContext;
54 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
56 MediaCodecH264DecContext *s = avctx->priv_data;
58 ff_mediacodec_dec_close(avctx, s->ctx);
61 av_packet_unref(&s->buffered_pkt);
66 #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
67 static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
72 static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
74 if (!out || !out_size) {
75 return AVERROR(EINVAL);
78 p = av_malloc(sizeof(nalu_header) + src_size);
80 return AVERROR(ENOMEM);
84 *out_size = sizeof(nalu_header) + src_size;
86 memcpy(p, nalu_header, sizeof(nalu_header));
87 memcpy(p + sizeof(nalu_header), src, src_size);
89 /* Escape 0x00, 0x00, 0x0{0-3} pattern */
90 for (i = 4; i < *out_size; i++) {
91 if (i < *out_size - 3 &&
98 new = av_realloc(*out, *out_size);
100 ret = AVERROR(ENOMEM);
106 memmove(p + i + 1, p + i, *out_size - (i + 1));
120 #if CONFIG_H264_MEDIACODEC_DECODER
121 static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
127 const PPS *pps = NULL;
128 const SPS *sps = NULL;
130 int nal_length_size = 0;
132 memset(&ps, 0, sizeof(ps));
134 ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
135 &ps, &is_avc, &nal_length_size, 0, avctx);
140 for (i = 0; i < MAX_PPS_COUNT; i++) {
141 if (ps.pps_list[i]) {
142 pps = (const PPS*)ps.pps_list[i]->data;
148 if (ps.sps_list[pps->sps_id]) {
149 sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
154 uint8_t *data = NULL;
157 if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
160 ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
163 if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
166 ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
169 av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
170 ret = AVERROR_INVALIDDATA;
174 ff_h264_ps_uninit(&ps);
180 #if CONFIG_HEVC_MEDIACODEC_DECODER
181 static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
189 const HEVCVPS *vps = NULL;
190 const HEVCPPS *pps = NULL;
191 const HEVCSPS *sps = NULL;
193 int nal_length_size = 0;
195 uint8_t *vps_data = NULL;
196 uint8_t *sps_data = NULL;
197 uint8_t *pps_data = NULL;
198 int vps_data_size = 0;
199 int sps_data_size = 0;
200 int pps_data_size = 0;
202 memset(&ps, 0, sizeof(ps));
203 memset(&sei, 0, sizeof(sei));
205 ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
206 &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
211 for (i = 0; i < HEVC_MAX_VPS_COUNT; i++) {
212 if (ps.vps_list[i]) {
213 vps = (const HEVCVPS*)ps.vps_list[i]->data;
218 for (i = 0; i < HEVC_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;
261 ff_hevc_ps_uninit(&ps);
271 #if CONFIG_MPEG2_MEDIACODEC_DECODER || \
272 CONFIG_MPEG4_MEDIACODEC_DECODER || \
273 CONFIG_VP8_MEDIACODEC_DECODER || \
274 CONFIG_VP9_MEDIACODEC_DECODER
275 static int common_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
279 if (avctx->extradata) {
280 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
287 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
291 const char *codec_mime = NULL;
293 FFAMediaFormat *format = NULL;
294 MediaCodecH264DecContext *s = avctx->priv_data;
296 format = ff_AMediaFormat_new();
298 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
299 ret = AVERROR_EXTERNAL;
303 switch (avctx->codec_id) {
304 #if CONFIG_H264_MEDIACODEC_DECODER
305 case AV_CODEC_ID_H264:
306 codec_mime = "video/avc";
308 ret = h264_set_extradata(avctx, format);
313 #if CONFIG_HEVC_MEDIACODEC_DECODER
314 case AV_CODEC_ID_HEVC:
315 codec_mime = "video/hevc";
317 ret = hevc_set_extradata(avctx, format);
322 #if CONFIG_MPEG2_MEDIACODEC_DECODER
323 case AV_CODEC_ID_MPEG2VIDEO:
324 codec_mime = "video/mpeg2";
326 ret = common_set_extradata(avctx, format);
331 #if CONFIG_MPEG4_MEDIACODEC_DECODER
332 case AV_CODEC_ID_MPEG4:
333 codec_mime = "video/mp4v-es",
335 ret = common_set_extradata(avctx, format);
340 #if CONFIG_VP8_MEDIACODEC_DECODER
341 case AV_CODEC_ID_VP8:
342 codec_mime = "video/x-vnd.on2.vp8";
344 ret = common_set_extradata(avctx, format);
349 #if CONFIG_VP9_MEDIACODEC_DECODER
350 case AV_CODEC_ID_VP9:
351 codec_mime = "video/x-vnd.on2.vp9";
353 ret = common_set_extradata(avctx, format);
362 ff_AMediaFormat_setString(format, "mime", codec_mime);
363 ff_AMediaFormat_setInt32(format, "width", avctx->width);
364 ff_AMediaFormat_setInt32(format, "height", avctx->height);
366 s->ctx = av_mallocz(sizeof(*s->ctx));
368 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
369 ret = AVERROR(ENOMEM);
373 s->ctx->delay_flush = s->delay_flush;
375 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
380 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
384 ff_AMediaFormat_delete(format);
388 mediacodec_decode_close(avctx);
394 static int mediacodec_send_receive(AVCodecContext *avctx,
395 MediaCodecH264DecContext *s,
396 AVFrame *frame, bool wait)
400 /* send any pending data from buffered packet */
401 while (s->buffered_pkt.size) {
402 ret = ff_mediacodec_dec_send(avctx, s->ctx, &s->buffered_pkt);
403 if (ret == AVERROR(EAGAIN))
407 s->buffered_pkt.size -= ret;
408 s->buffered_pkt.data += ret;
409 if (s->buffered_pkt.size <= 0)
410 av_packet_unref(&s->buffered_pkt);
413 /* check for new frame */
414 return ff_mediacodec_dec_receive(avctx, s->ctx, frame, wait);
417 static int mediacodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
419 MediaCodecH264DecContext *s = avctx->priv_data;
423 * MediaCodec.flush() discards both input and output buffers, thus we
424 * need to delay the call to this function until the user has released or
425 * renderered the frames he retains.
427 * After we have buffered an input packet, check if the codec is in the
428 * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
430 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
431 * the codec (because the user retains frames). The codec stays in the
434 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
435 * performed on the codec. The codec leaves the flushing state and can
436 * process again packets.
438 * ff_mediacodec_dec_flush returns a negative value if an error has
442 if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
443 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
444 return AVERROR(EAGAIN);
448 /* flush buffered packet and check for new frame */
449 ret = mediacodec_send_receive(avctx, s, frame, false);
450 if (ret != AVERROR(EAGAIN))
453 /* skip fetching new packet if we still have one buffered */
454 if (s->buffered_pkt.size > 0)
455 return mediacodec_send_receive(avctx, s, frame, true);
457 /* fetch new packet or eof */
458 ret = ff_decode_get_packet(avctx, &s->buffered_pkt);
459 if (ret == AVERROR_EOF) {
460 AVPacket null_pkt = { 0 };
461 ret = ff_mediacodec_dec_send(avctx, s->ctx, &null_pkt);
468 /* crank decoder with new packet */
469 return mediacodec_send_receive(avctx, s, frame, true);
472 static void mediacodec_decode_flush(AVCodecContext *avctx)
474 MediaCodecH264DecContext *s = avctx->priv_data;
476 av_packet_unref(&s->buffered_pkt);
478 ff_mediacodec_dec_flush(avctx, s->ctx);
481 static const AVCodecHWConfigInternal *mediacodec_hw_configs[] = {
482 &(const AVCodecHWConfigInternal) {
484 .pix_fmt = AV_PIX_FMT_MEDIACODEC,
485 .methods = AV_CODEC_HW_CONFIG_METHOD_AD_HOC |
486 AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
487 .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
494 #define OFFSET(x) offsetof(MediaCodecH264DecContext, x)
495 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
496 static const AVOption ff_mediacodec_vdec_options[] = {
497 { "delay_flush", "Delay flush until hw output buffers are returned to the decoder",
498 OFFSET(delay_flush), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VD },
502 #define DECLARE_MEDIACODEC_VCLASS(short_name) \
503 static const AVClass ff_##short_name##_mediacodec_dec_class = { \
504 .class_name = #short_name "_mediacodec", \
505 .item_name = av_default_item_name, \
506 .option = ff_mediacodec_vdec_options, \
507 .version = LIBAVUTIL_VERSION_INT, \
510 #define DECLARE_MEDIACODEC_VDEC(short_name, full_name, codec_id, bsf) \
511 DECLARE_MEDIACODEC_VCLASS(short_name) \
512 AVCodec ff_##short_name##_mediacodec_decoder = { \
513 .name = #short_name "_mediacodec", \
514 .long_name = NULL_IF_CONFIG_SMALL(full_name " Android MediaCodec decoder"), \
515 .type = AVMEDIA_TYPE_VIDEO, \
517 .priv_class = &ff_##short_name##_mediacodec_dec_class, \
518 .priv_data_size = sizeof(MediaCodecH264DecContext), \
519 .init = mediacodec_decode_init, \
520 .receive_frame = mediacodec_receive_frame, \
521 .flush = mediacodec_decode_flush, \
522 .close = mediacodec_decode_close, \
523 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
524 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS, \
526 .hw_configs = mediacodec_hw_configs, \
527 .wrapper_name = "mediacodec", \
530 #if CONFIG_H264_MEDIACODEC_DECODER
531 DECLARE_MEDIACODEC_VDEC(h264, "H.264", AV_CODEC_ID_H264, "h264_mp4toannexb")
534 #if CONFIG_HEVC_MEDIACODEC_DECODER
535 DECLARE_MEDIACODEC_VDEC(hevc, "H.265", AV_CODEC_ID_HEVC, "hevc_mp4toannexb")
538 #if CONFIG_MPEG2_MEDIACODEC_DECODER
539 DECLARE_MEDIACODEC_VDEC(mpeg2, "MPEG-2", AV_CODEC_ID_MPEG2VIDEO, NULL)
542 #if CONFIG_MPEG4_MEDIACODEC_DECODER
543 DECLARE_MEDIACODEC_VDEC(mpeg4, "MPEG-4", AV_CODEC_ID_MPEG4, NULL)
546 #if CONFIG_VP8_MEDIACODEC_DECODER
547 DECLARE_MEDIACODEC_VDEC(vp8, "VP8", AV_CODEC_ID_VP8, NULL)
550 #if CONFIG_VP9_MEDIACODEC_DECODER
551 DECLARE_MEDIACODEC_VDEC(vp9, "VP9", AV_CODEC_ID_VP9, NULL)