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"
36 #include "mediacodecdec.h"
37 #include "mediacodec_wrapper.h"
39 #define CODEC_MIME "video/avc"
41 typedef struct MediaCodecH264DecContext {
43 MediaCodecDecContext ctx;
49 AVPacket filtered_pkt;
51 } MediaCodecH264DecContext;
53 static int h264_extradata_to_annexb_sps_pps(AVCodecContext *avctx,
54 uint8_t **extradata_annexb, int *extradata_annexb_size,
55 int *sps_offset, int *sps_size,
56 int *pps_offset, int *pps_size)
59 uint64_t total_size = 0;
61 uint8_t i, j, unit_nb;
65 const uint8_t *extradata;
66 static const uint8_t nalu_header[4] = { 0x00, 0x00, 0x00, 0x01 };
68 if (avctx->extradata_size < 8) {
69 av_log(avctx, AV_LOG_ERROR,
70 "Too small extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
71 return AVERROR(EINVAL);
74 *extradata_annexb = NULL;
75 *extradata_annexb_size = 0;
77 *sps_offset = *sps_size = 0;
78 *pps_offset = *pps_size = 0;
80 extradata = avctx->extradata + 4;
82 /* skip length size */
85 for (j = 0; j < 2; j ++) {
88 /* number of sps unit(s) */
89 unit_nb = *extradata++ & 0x1f;
91 /* number of pps unit(s) */
92 unit_nb = *extradata++;
95 for (i = 0; i < unit_nb; i++) {
98 unit_size = AV_RB16(extradata);
99 total_size += unit_size + 4;
101 if (total_size > INT_MAX) {
102 av_log(avctx, AV_LOG_ERROR,
103 "Too big extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
104 av_freep(extradata_annexb);
105 return AVERROR(EINVAL);
108 if (extradata + 2 + unit_size > avctx->extradata + avctx->extradata_size) {
109 av_log(avctx, AV_LOG_ERROR, "Packet header is not contained in global extradata, "
110 "corrupted stream or invalid MP4/AVCC bitstream\n");
111 av_freep(extradata_annexb);
112 return AVERROR(EINVAL);
115 if ((err = av_reallocp(extradata_annexb, total_size)) < 0) {
119 memcpy(*extradata_annexb + total_size - unit_size - 4, nalu_header, 4);
120 memcpy(*extradata_annexb + total_size - unit_size, extradata + 2, unit_size);
121 extradata += 2 + unit_size;
127 *sps_size = total_size;
130 *pps_size = total_size - *sps_size;
131 *pps_offset = *sps_size;
136 *extradata_annexb_size = total_size;
139 av_log(avctx, AV_LOG_WARNING,
140 "Warning: SPS NALU missing or invalid. "
141 "The resulting stream may not play.\n");
144 av_log(avctx, AV_LOG_WARNING,
145 "Warning: PPS NALU missing or invalid. "
146 "The resulting stream may not play.\n");
151 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
153 MediaCodecH264DecContext *s = avctx->priv_data;
155 ff_mediacodec_dec_close(avctx, &s->ctx);
157 av_fifo_free(s->fifo);
159 av_bsf_free(&s->bsf);
160 av_packet_unref(&s->filtered_pkt);
165 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
168 FFAMediaFormat *format = NULL;
169 MediaCodecH264DecContext *s = avctx->priv_data;
171 format = ff_AMediaFormat_new();
173 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
174 ret = AVERROR_EXTERNAL;
178 ff_AMediaFormat_setString(format, "mime", CODEC_MIME);
179 ff_AMediaFormat_setInt32(format, "width", avctx->width);
180 ff_AMediaFormat_setInt32(format, "height", avctx->height);
182 if (avctx->extradata[0] == 1) {
183 uint8_t *extradata = NULL;
184 int extradata_size = 0;
186 int sps_offset, sps_size;
187 int pps_offset, pps_size;
189 if ((ret = h264_extradata_to_annexb_sps_pps(avctx, &extradata, &extradata_size,
190 &sps_offset, &sps_size, &pps_offset, &pps_size)) < 0) {
194 ff_AMediaFormat_setBuffer(format, "csd-0", extradata + sps_offset, sps_size);
195 ff_AMediaFormat_setBuffer(format, "csd-1", extradata + pps_offset, pps_size);
197 av_freep(&extradata);
199 ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
202 if ((ret = ff_mediacodec_dec_init(avctx, &s->ctx, CODEC_MIME, format)) < 0) {
206 av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
208 s->fifo = av_fifo_alloc(sizeof(AVPacket));
210 ret = AVERROR(ENOMEM);
214 const AVBitStreamFilter *bsf = av_bsf_get_by_name("h264_mp4toannexb");
216 ret = AVERROR_BSF_NOT_FOUND;
220 if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
224 if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
225 ((ret = av_bsf_init(s->bsf)) < 0)) {
229 av_init_packet(&s->filtered_pkt);
233 ff_AMediaFormat_delete(format);
237 mediacodec_decode_close(avctx);
243 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
244 int *got_frame, AVPacket *pkt)
246 MediaCodecH264DecContext *s = avctx->priv_data;
248 return ff_mediacodec_dec_decode(avctx, &s->ctx, frame, got_frame, pkt);
251 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
252 int *got_frame, AVPacket *avpkt)
254 MediaCodecH264DecContext *s = avctx->priv_data;
255 AVFrame *frame = data;
258 /* buffer the input packet */
260 AVPacket input_pkt = { 0 };
262 if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
263 ret = av_fifo_realloc2(s->fifo,
264 av_fifo_size(s->fifo) + sizeof(input_pkt));
269 ret = av_packet_ref(&input_pkt, avpkt);
272 av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
275 /* process buffered data */
276 while (!*got_frame) {
277 /* prepare the input data -- convert to Annex B if needed */
278 if (s->filtered_pkt.size <= 0) {
279 AVPacket input_pkt = { 0 };
281 av_packet_unref(&s->filtered_pkt);
284 if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
285 return avpkt->size ? avpkt->size :
286 ff_mediacodec_dec_decode(avctx, &s->ctx, frame, got_frame, avpkt);
289 av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
291 ret = av_bsf_send_packet(s->bsf, &input_pkt);
296 ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
297 if (ret == AVERROR(EAGAIN)) {
301 /* h264_mp4toannexb is used here and does not requires flushing */
302 av_assert0(ret != AVERROR_EOF);
309 ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
313 s->filtered_pkt.size -= ret;
314 s->filtered_pkt.data += ret;
320 static void mediacodec_decode_flush(AVCodecContext *avctx)
322 MediaCodecH264DecContext *s = avctx->priv_data;
324 while (av_fifo_size(s->fifo)) {
326 av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
327 av_packet_unref(&pkt);
329 av_fifo_reset(s->fifo);
331 av_packet_unref(&s->filtered_pkt);
333 ff_mediacodec_dec_flush(avctx, &s->ctx);
336 AVCodec ff_h264_mediacodec_decoder = {
337 .name = "h264_mediacodec",
338 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
339 .type = AVMEDIA_TYPE_VIDEO,
340 .id = AV_CODEC_ID_H264,
341 .priv_data_size = sizeof(MediaCodecH264DecContext),
342 .init = mediacodec_decode_init,
343 .decode = mediacodec_decode_frame,
344 .flush = mediacodec_decode_flush,
345 .close = mediacodec_decode_close,
346 .capabilities = CODEC_CAP_DELAY,