2 * Android MediaCodec H.264 decoder
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"
37 #include "mediacodecdec.h"
38 #include "mediacodec_wrapper.h"
40 #define CODEC_MIME "video/avc"
42 typedef struct MediaCodecH264DecContext {
44 MediaCodecDecContext ctx;
50 AVPacket filtered_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);
60 av_fifo_free(s->fifo);
63 av_packet_unref(&s->filtered_pkt);
68 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
74 const PPS *pps = NULL;
75 const SPS *sps = NULL;
77 int nal_length_size = 0;
79 FFAMediaFormat *format = NULL;
80 MediaCodecH264DecContext *s = avctx->priv_data;
82 memset(&ps, 0, sizeof(ps));
84 format = ff_AMediaFormat_new();
86 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
87 ret = AVERROR_EXTERNAL;
91 ff_AMediaFormat_setString(format, "mime", CODEC_MIME);
92 ff_AMediaFormat_setInt32(format, "width", avctx->width);
93 ff_AMediaFormat_setInt32(format, "height", avctx->height);
95 ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
96 &ps, &is_avc, &nal_length_size, 0, avctx);
101 for (i = 0; i < MAX_PPS_COUNT; i++) {
102 if (ps.pps_list[i]) {
103 pps = (const PPS*)ps.pps_list[i]->data;
109 if (ps.sps_list[pps->sps_id]) {
110 sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
115 static const uint8_t nal_headers[] = { 0x00, 0x00, 0x00, 0x01 };
117 uint8_t *data = NULL;
118 size_t data_size = sizeof(nal_headers) + FFMAX(sps->data_size, pps->data_size);
120 data = av_mallocz(data_size);
122 ret = AVERROR(ENOMEM);
126 memcpy(data, nal_headers, sizeof(nal_headers));
127 memcpy(data + sizeof(nal_headers), sps->data, sps->data_size);
128 ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, sizeof(nal_headers) + sps->data_size);
130 memcpy(data + sizeof(nal_headers), pps->data, pps->data_size);
131 ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, sizeof(nal_headers) + pps->data_size);
135 av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
136 ret = AVERROR_INVALIDDATA;
140 if ((ret = ff_mediacodec_dec_init(avctx, &s->ctx, CODEC_MIME, format)) < 0) {
144 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
146 s->fifo = av_fifo_alloc(sizeof(AVPacket));
148 ret = AVERROR(ENOMEM);
152 const AVBitStreamFilter *bsf = av_bsf_get_by_name("h264_mp4toannexb");
154 ret = AVERROR_BSF_NOT_FOUND;
158 if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
162 if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
163 ((ret = av_bsf_init(s->bsf)) < 0)) {
167 av_init_packet(&s->filtered_pkt);
171 ff_AMediaFormat_delete(format);
175 mediacodec_decode_close(avctx);
178 ff_h264_ps_uninit(&ps);
184 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
185 int *got_frame, AVPacket *pkt)
187 MediaCodecH264DecContext *s = avctx->priv_data;
189 return ff_mediacodec_dec_decode(avctx, &s->ctx, frame, got_frame, pkt);
192 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
193 int *got_frame, AVPacket *avpkt)
195 MediaCodecH264DecContext *s = avctx->priv_data;
196 AVFrame *frame = data;
199 /* buffer the input packet */
201 AVPacket input_pkt = { 0 };
203 if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
204 ret = av_fifo_realloc2(s->fifo,
205 av_fifo_size(s->fifo) + sizeof(input_pkt));
210 ret = av_packet_ref(&input_pkt, avpkt);
213 av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
216 /* process buffered data */
217 while (!*got_frame) {
218 /* prepare the input data -- convert to Annex B if needed */
219 if (s->filtered_pkt.size <= 0) {
220 AVPacket input_pkt = { 0 };
222 av_packet_unref(&s->filtered_pkt);
225 if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
226 return avpkt->size ? avpkt->size :
227 ff_mediacodec_dec_decode(avctx, &s->ctx, frame, got_frame, avpkt);
230 av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
232 ret = av_bsf_send_packet(s->bsf, &input_pkt);
237 ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
238 if (ret == AVERROR(EAGAIN)) {
242 /* h264_mp4toannexb is used here and does not requires flushing */
243 av_assert0(ret != AVERROR_EOF);
250 ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
254 s->filtered_pkt.size -= ret;
255 s->filtered_pkt.data += ret;
261 static void mediacodec_decode_flush(AVCodecContext *avctx)
263 MediaCodecH264DecContext *s = avctx->priv_data;
265 while (av_fifo_size(s->fifo)) {
267 av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
268 av_packet_unref(&pkt);
270 av_fifo_reset(s->fifo);
272 av_packet_unref(&s->filtered_pkt);
274 ff_mediacodec_dec_flush(avctx, &s->ctx);
277 AVCodec ff_h264_mediacodec_decoder = {
278 .name = "h264_mediacodec",
279 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
280 .type = AVMEDIA_TYPE_VIDEO,
281 .id = AV_CODEC_ID_H264,
282 .priv_data_size = sizeof(MediaCodecH264DecContext),
283 .init = mediacodec_decode_init,
284 .decode = mediacodec_decode_frame,
285 .flush = mediacodec_decode_flush,
286 .close = mediacodec_decode_close,
287 .capabilities = CODEC_CAP_DELAY,
288 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,