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);
61 av_fifo_free(s->fifo);
64 av_packet_unref(&s->filtered_pkt);
69 static int h264_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
74 static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
76 if (!out || !out_size) {
77 return AVERROR(EINVAL);
80 p = av_malloc(sizeof(nalu_header) + src_size);
82 return AVERROR(ENOMEM);
86 *out_size = sizeof(nalu_header) + src_size;
88 memcpy(p, nalu_header, sizeof(nalu_header));
89 memcpy(p + sizeof(nalu_header), src, src_size);
91 /* Escape 0x00, 0x00, 0x0{0-3} pattern */
92 for (i = 4; i < *out_size; i++) {
93 if (i < *out_size - 3 &&
100 new = av_realloc(*out, *out_size);
102 ret = AVERROR(ENOMEM);
108 memmove(p + i, p + i - 1, *out_size - i);
121 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
127 const PPS *pps = NULL;
128 const SPS *sps = NULL;
130 int nal_length_size = 0;
132 FFAMediaFormat *format = NULL;
133 MediaCodecH264DecContext *s = avctx->priv_data;
135 memset(&ps, 0, sizeof(ps));
137 format = ff_AMediaFormat_new();
139 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
140 ret = AVERROR_EXTERNAL;
144 ff_AMediaFormat_setString(format, "mime", CODEC_MIME);
145 ff_AMediaFormat_setInt32(format, "width", avctx->width);
146 ff_AMediaFormat_setInt32(format, "height", avctx->height);
148 ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
149 &ps, &is_avc, &nal_length_size, 0, avctx);
154 for (i = 0; i < MAX_PPS_COUNT; i++) {
155 if (ps.pps_list[i]) {
156 pps = (const PPS*)ps.pps_list[i]->data;
162 if (ps.sps_list[pps->sps_id]) {
163 sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
168 uint8_t *data = NULL;
169 size_t data_size = 0;
171 if ((ret = h264_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
174 ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
177 if ((ret = h264_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
180 ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
183 av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
184 ret = AVERROR_INVALIDDATA;
188 s->ctx = av_mallocz(sizeof(*s->ctx));
190 av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
191 ret = AVERROR(ENOMEM);
195 if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, CODEC_MIME, format)) < 0) {
200 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
202 s->fifo = av_fifo_alloc(sizeof(AVPacket));
204 ret = AVERROR(ENOMEM);
208 const AVBitStreamFilter *bsf = av_bsf_get_by_name("h264_mp4toannexb");
210 ret = AVERROR_BSF_NOT_FOUND;
214 if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
218 if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
219 ((ret = av_bsf_init(s->bsf)) < 0)) {
223 av_init_packet(&s->filtered_pkt);
227 ff_AMediaFormat_delete(format);
231 mediacodec_decode_close(avctx);
234 ff_h264_ps_uninit(&ps);
240 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
241 int *got_frame, AVPacket *pkt)
243 MediaCodecH264DecContext *s = avctx->priv_data;
245 return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
248 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
249 int *got_frame, AVPacket *avpkt)
251 MediaCodecH264DecContext *s = avctx->priv_data;
252 AVFrame *frame = data;
255 /* buffer the input packet */
257 AVPacket input_pkt = { 0 };
259 if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
260 ret = av_fifo_realloc2(s->fifo,
261 av_fifo_size(s->fifo) + sizeof(input_pkt));
266 ret = av_packet_ref(&input_pkt, avpkt);
269 av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
273 * MediaCodec.flush() discards both input and output buffers, thus we
274 * need to delay the call to this function until the user has released or
275 * renderered the frames he retains.
277 * After we have buffered an input packet, check if the codec is in the
278 * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
280 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
281 * the codec (because the user retains frames). The codec stays in the
284 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
285 * performed on the codec. The codec leaves the flushing state and can
286 * process again packets.
288 * ff_mediacodec_dec_flush returns a negative value if an error has
292 if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
293 if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
298 /* process buffered data */
299 while (!*got_frame) {
300 /* prepare the input data -- convert to Annex B if needed */
301 if (s->filtered_pkt.size <= 0) {
302 AVPacket input_pkt = { 0 };
304 av_packet_unref(&s->filtered_pkt);
307 if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
308 return avpkt->size ? avpkt->size :
309 ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
312 av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
314 ret = av_bsf_send_packet(s->bsf, &input_pkt);
319 ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
320 if (ret == AVERROR(EAGAIN)) {
324 /* h264_mp4toannexb is used here and does not requires flushing */
325 av_assert0(ret != AVERROR_EOF);
332 ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
336 s->filtered_pkt.size -= ret;
337 s->filtered_pkt.data += ret;
343 static void mediacodec_decode_flush(AVCodecContext *avctx)
345 MediaCodecH264DecContext *s = avctx->priv_data;
347 while (av_fifo_size(s->fifo)) {
349 av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
350 av_packet_unref(&pkt);
352 av_fifo_reset(s->fifo);
354 av_packet_unref(&s->filtered_pkt);
356 ff_mediacodec_dec_flush(avctx, s->ctx);
359 AVCodec ff_h264_mediacodec_decoder = {
360 .name = "h264_mediacodec",
361 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
362 .type = AVMEDIA_TYPE_VIDEO,
363 .id = AV_CODEC_ID_H264,
364 .priv_data_size = sizeof(MediaCodecH264DecContext),
365 .init = mediacodec_decode_init,
366 .decode = mediacodec_decode_frame,
367 .flush = mediacodec_decode_flush,
368 .close = mediacodec_decode_close,
369 .capabilities = CODEC_CAP_DELAY,
370 .caps_internal = FF_CODEC_CAP_SETS_PKT_DTS,