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/common.h"
27 #include "libavutil/fifo.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/pixfmt.h"
31 #include "libavutil/atomic.h"
35 #include "mediacodecdec.h"
36 #include "mediacodec_wrapper.h"
38 #define CODEC_MIME "video/avc"
40 typedef struct MediaCodecH264DecContext {
42 MediaCodecDecContext ctx;
44 AVBitStreamFilterContext *bsf;
49 AVPacket filtered_pkt;
50 uint8_t *filtered_data;
52 } MediaCodecH264DecContext;
54 static int h264_extradata_to_annexb_sps_pps(AVCodecContext *avctx,
55 uint8_t **extradata_annexb, int *extradata_annexb_size,
56 int *sps_offset, int *sps_size,
57 int *pps_offset, int *pps_size)
60 uint64_t total_size = 0;
62 uint8_t i, j, unit_nb;
66 const uint8_t *extradata;
67 static const uint8_t nalu_header[4] = { 0x00, 0x00, 0x00, 0x01 };
69 if (avctx->extradata_size < 8) {
70 av_log(avctx, AV_LOG_ERROR,
71 "Too small extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
72 return AVERROR(EINVAL);
75 *extradata_annexb = NULL;
76 *extradata_annexb_size = 0;
78 *sps_offset = *sps_size = 0;
79 *pps_offset = *pps_size = 0;
81 extradata = avctx->extradata + 4;
83 /* skip length size */
86 for (j = 0; j < 2; j ++) {
89 /* number of sps unit(s) */
90 unit_nb = *extradata++ & 0x1f;
92 /* number of pps unit(s) */
93 unit_nb = *extradata++;
96 for (i = 0; i < unit_nb; i++) {
99 unit_size = AV_RB16(extradata);
100 total_size += unit_size + 4;
102 if (total_size > INT_MAX) {
103 av_log(avctx, AV_LOG_ERROR,
104 "Too big extradata size, corrupted stream or invalid MP4/AVCC bitstream\n");
105 av_freep(extradata_annexb);
106 return AVERROR(EINVAL);
109 if (extradata + 2 + unit_size > avctx->extradata + avctx->extradata_size) {
110 av_log(avctx, AV_LOG_ERROR, "Packet header is not contained in global extradata, "
111 "corrupted stream or invalid MP4/AVCC bitstream\n");
112 av_freep(extradata_annexb);
113 return AVERROR(EINVAL);
116 if ((err = av_reallocp(extradata_annexb, total_size)) < 0) {
120 memcpy(*extradata_annexb + total_size - unit_size - 4, nalu_header, 4);
121 memcpy(*extradata_annexb + total_size - unit_size, extradata + 2, unit_size);
122 extradata += 2 + unit_size;
128 *sps_size = total_size;
131 *pps_size = total_size - *sps_size;
132 *pps_offset = *sps_size;
137 *extradata_annexb_size = total_size;
140 av_log(avctx, AV_LOG_WARNING,
141 "Warning: SPS NALU missing or invalid. "
142 "The resulting stream may not play.\n");
145 av_log(avctx, AV_LOG_WARNING,
146 "Warning: PPS NALU missing or invalid. "
147 "The resulting stream may not play.\n");
152 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
154 MediaCodecH264DecContext *s = avctx->priv_data;
156 ff_mediacodec_dec_close(avctx, &s->ctx);
158 av_fifo_free(s->fifo);
160 av_bitstream_filter_close(s->bsf);
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 s->bsf = av_bitstream_filter_init("h264_mp4toannexb");
216 ret = AVERROR(ENOMEM);
222 ff_AMediaFormat_delete(format);
226 mediacodec_decode_close(avctx);
232 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
233 int *got_frame, AVPacket *pkt)
235 MediaCodecH264DecContext *s = avctx->priv_data;
237 return ff_mediacodec_dec_decode(avctx, &s->ctx, frame, got_frame, pkt);
240 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
241 int *got_frame, AVPacket *avpkt)
243 MediaCodecH264DecContext *s = avctx->priv_data;
244 AVFrame *frame = data;
247 /* buffer the input packet */
249 AVPacket input_ref = { 0 };
251 if (av_fifo_space(s->fifo) < sizeof(input_ref)) {
252 ret = av_fifo_realloc2(s->fifo,
253 av_fifo_size(s->fifo) + sizeof(input_ref));
258 ret = av_packet_ref(&input_ref, avpkt);
261 av_fifo_generic_write(s->fifo, &input_ref, sizeof(input_ref), NULL);
264 /* process buffered data */
265 while (!*got_frame) {
266 /* prepare the input data -- convert to Annex B if needed */
267 if (s->filtered_pkt.size <= 0) {
271 if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
272 return avpkt->size ? avpkt->size :
273 ff_mediacodec_dec_decode(avctx, &s->ctx, frame, got_frame, avpkt);
276 if (s->filtered_data != s->input_ref.data)
277 av_freep(&s->filtered_data);
278 s->filtered_data = NULL;
279 av_packet_unref(&s->input_ref);
281 av_fifo_generic_read(s->fifo, &s->input_ref, sizeof(s->input_ref), NULL);
282 ret = av_bitstream_filter_filter(s->bsf, avctx, NULL,
283 &s->filtered_data, &size,
284 s->input_ref.data, s->input_ref.size, 0);
286 s->filtered_data = s->input_ref.data;
287 size = s->input_ref.size;
289 s->filtered_pkt = s->input_ref;
290 s->filtered_pkt.data = s->filtered_data;
291 s->filtered_pkt.size = size;
294 ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
298 s->filtered_pkt.size -= ret;
299 s->filtered_pkt.data += ret;
305 static void mediacodec_decode_flush(AVCodecContext *avctx)
307 MediaCodecH264DecContext *s = avctx->priv_data;
309 while (av_fifo_size(s->fifo)) {
311 av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
312 av_packet_unref(&pkt);
314 av_fifo_reset(s->fifo);
316 av_packet_unref(&s->input_ref);
318 av_init_packet(&s->filtered_pkt);
319 s->filtered_pkt.data = NULL;
320 s->filtered_pkt.size = 0;
322 ff_mediacodec_dec_flush(avctx, &s->ctx);
325 AVCodec ff_h264_mediacodec_decoder = {
326 .name = "h264_mediacodec",
327 .long_name = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
328 .type = AVMEDIA_TYPE_VIDEO,
329 .id = AV_CODEC_ID_H264,
330 .priv_data_size = sizeof(MediaCodecH264DecContext),
331 .init = mediacodec_decode_init,
332 .decode = mediacodec_decode_frame,
333 .flush = mediacodec_decode_flush,
334 .close = mediacodec_decode_close,
335 .capabilities = CODEC_CAP_DELAY,