2 * Android MediaCodec H.264 / H.265 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 "mediacodecdec.h"
39 #include "mediacodec_wrapper.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 static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
73 static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
75 if (!out || !out_size) {
76 return AVERROR(EINVAL);
79 p = av_malloc(sizeof(nalu_header) + src_size);
81 return AVERROR(ENOMEM);
85 *out_size = sizeof(nalu_header) + src_size;
87 memcpy(p, nalu_header, sizeof(nalu_header));
88 memcpy(p + sizeof(nalu_header), src, src_size);
90 /* Escape 0x00, 0x00, 0x0{0-3} pattern */
91 for (i = 4; i < *out_size; i++) {
92 if (i < *out_size - 3 &&
99 new = av_realloc(*out, *out_size);
101 ret = AVERROR(ENOMEM);
107 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)
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));
203 ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
204 &ps, &is_nalff, &nal_length_size, 0, avctx);
209 for (i = 0; i < MAX_VPS_COUNT; i++) {
210 if (ps.vps_list[i]) {
211 vps = (const HEVCVPS*)ps.vps_list[i]->data;
216 for (i = 0; i < 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 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
271 const char *codec_mime = NULL;
273 const char *bsf_name = NULL;
274 const AVBitStreamFilter *bsf = NULL;
276 FFAMediaFormat *format = NULL;
277 MediaCodecH264DecContext *s = avctx->priv_data;
279 format = ff_AMediaFormat_new();
281 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
282 ret = AVERROR_EXTERNAL;
286 switch (avctx->codec_id) {
287 #if CONFIG_H264_MEDIACODEC_DECODER
288 case AV_CODEC_ID_H264:
289 codec_mime = "video/avc";
290 bsf_name = "h264_mp4toannexb";
292 ret = h264_set_extradata(avctx, format);
297 #if CONFIG_HEVC_MEDIACODEC_DECODER
298 case AV_CODEC_ID_HEVC:
299 codec_mime = "video/hevc";
300 bsf_name = "hevc_mp4toannexb";
302 ret = hevc_set_extradata(avctx, format);
311 ff_AMediaFormat_setString(format, "mime", codec_mime);
312 ff_AMediaFormat_setInt32(format, "width", avctx->width);
313 ff_AMediaFormat_setInt32(format, "height", avctx->height);
315 s->ctx = av_mallocz(sizeof(*s->ctx));
317 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
318 ret = AVERROR(ENOMEM);
322 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
327 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
329 s->fifo = av_fifo_alloc(sizeof(AVPacket));
331 ret = AVERROR(ENOMEM);
335 bsf = av_bsf_get_by_name(bsf_name);
337 ret = AVERROR_BSF_NOT_FOUND;
341 if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
345 if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
346 ((ret = av_bsf_init(s->bsf)) < 0)) {
350 av_init_packet(&s->filtered_pkt);
354 ff_AMediaFormat_delete(format);
358 mediacodec_decode_close(avctx);
365 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
366 int *got_frame, AVPacket *pkt)
368 MediaCodecH264DecContext *s = avctx->priv_data;
370 return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
373 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
374 int *got_frame, AVPacket *avpkt)
376 MediaCodecH264DecContext *s = avctx->priv_data;
377 AVFrame *frame = data;
380 /* buffer the input packet */
382 AVPacket input_pkt = { 0 };
384 if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
385 ret = av_fifo_realloc2(s->fifo,
386 av_fifo_size(s->fifo) + sizeof(input_pkt));
391 ret = av_packet_ref(&input_pkt, avpkt);
394 av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
398 * MediaCodec.flush() discards both input and output buffers, thus we
399 * need to delay the call to this function until the user has released or
400 * renderered the frames he retains.
402 * After we have buffered an input packet, check if the codec is in the
403 * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
405 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
406 * the codec (because the user retains frames). The codec stays in the
409 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
410 * performed on the codec. The codec leaves the flushing state and can
411 * process again packets.
413 * ff_mediacodec_dec_flush returns a negative value if an error has
417 if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
418 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
423 /* process buffered data */
424 while (!*got_frame) {
425 /* prepare the input data -- convert to Annex B if needed */
426 if (s->filtered_pkt.size <= 0) {
427 AVPacket input_pkt = { 0 };
429 av_packet_unref(&s->filtered_pkt);
432 if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
433 return avpkt->size ? avpkt->size :
434 ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
437 av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
439 ret = av_bsf_send_packet(s->bsf, &input_pkt);
444 ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
445 if (ret == AVERROR(EAGAIN)) {
449 /* {h264,hevc}_mp4toannexb are used here and do not require flushing */
450 av_assert0(ret != AVERROR_EOF);
457 ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
461 s->filtered_pkt.size -= ret;
462 s->filtered_pkt.data += ret;
468 static void mediacodec_decode_flush(AVCodecContext *avctx)
470 MediaCodecH264DecContext *s = avctx->priv_data;
472 while (av_fifo_size(s->fifo)) {
474 av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
475 av_packet_unref(&pkt);
477 av_fifo_reset(s->fifo);
479 av_packet_unref(&s->filtered_pkt);
481 ff_mediacodec_dec_flush(avctx, s->ctx);
484 #if CONFIG_H264_MEDIACODEC_DECODER
485 AVCodec ff_h264_mediacodec_decoder = {
486 .name = "h264_mediacodec",
487 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
488 .type = AVMEDIA_TYPE_VIDEO,
489 .id = AV_CODEC_ID_H264,
490 .priv_data_size = sizeof(MediaCodecH264DecContext),
491 .init = mediacodec_decode_init,
492 .decode = mediacodec_decode_frame,
493 .flush = mediacodec_decode_flush,
494 .close = mediacodec_decode_close,
495 .capabilities = CODEC_CAP_DELAY,
496 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,
500 #if CONFIG_HEVC_MEDIACODEC_DECODER
501 AVCodec ff_hevc_mediacodec_decoder = {
502 .name = "hevc_mediacodec",
503 .long_name = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
504 .type = AVMEDIA_TYPE_VIDEO,
505 .id = AV_CODEC_ID_HEVC,
506 .priv_data_size = sizeof(MediaCodecH264DecContext),
507 .init = mediacodec_decode_init,
508 .decode = mediacodec_decode_frame,
509 .flush = mediacodec_decode_flush,
510 .close = mediacodec_decode_close,
511 .capabilities = CODEC_CAP_DELAY,
512 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,