]> git.sesse.net Git - ffmpeg/blob - libavcodec/mediacodecdec_h264.c
lavc/fic: Do not warn about empty cursor.
[ffmpeg] / libavcodec / mediacodecdec_h264.c
1 /*
2  * Android MediaCodec H.264 decoder
3  *
4  * Copyright (c) 2015-2016 Matthieu Bouron <matthieu.bouron stupeflix.com>
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <stdint.h>
24 #include <string.h>
25
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"
32
33 #include "avcodec.h"
34 #include "internal.h"
35 #include "mediacodecdec.h"
36 #include "mediacodec_wrapper.h"
37
38 #define CODEC_MIME "video/avc"
39
40 typedef struct MediaCodecH264DecContext {
41
42     MediaCodecDecContext ctx;
43
44     AVBitStreamFilterContext *bsf;
45
46     AVFifoBuffer *fifo;
47
48     AVPacket input_ref;
49     AVPacket filtered_pkt;
50     uint8_t *filtered_data;
51
52 } MediaCodecH264DecContext;
53
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)
58 {
59     uint16_t unit_size;
60     uint64_t total_size = 0;
61
62     uint8_t i, j, unit_nb;
63     uint8_t sps_seen = 0;
64     uint8_t pps_seen = 0;
65
66     const uint8_t *extradata;
67     static const uint8_t nalu_header[4] = { 0x00, 0x00, 0x00, 0x01 };
68
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);
73     }
74
75     *extradata_annexb = NULL;
76     *extradata_annexb_size = 0;
77
78     *sps_offset = *sps_size = 0;
79     *pps_offset = *pps_size = 0;
80
81     extradata = avctx->extradata + 4;
82
83     /* skip length size */
84     extradata++;
85
86     for (j = 0; j < 2; j ++) {
87
88         if (j == 0) {
89             /* number of sps unit(s) */
90             unit_nb = *extradata++ & 0x1f;
91         } else {
92             /* number of pps unit(s) */
93             unit_nb = *extradata++;
94         }
95
96         for (i = 0; i < unit_nb; i++) {
97             int err;
98
99             unit_size   = AV_RB16(extradata);
100             total_size += unit_size + 4;
101
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);
107             }
108
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);
114             }
115
116             if ((err = av_reallocp(extradata_annexb, total_size)) < 0) {
117                 return err;
118             }
119
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;
123         }
124
125         if (unit_nb) {
126             if (j == 0) {
127                 sps_seen = 1;
128                 *sps_size = total_size;
129             } else {
130                 pps_seen = 1;
131                 *pps_size = total_size - *sps_size;
132                 *pps_offset = *sps_size;
133             }
134         }
135     }
136
137     *extradata_annexb_size = total_size;
138
139     if (!sps_seen)
140         av_log(avctx, AV_LOG_WARNING,
141                "Warning: SPS NALU missing or invalid. "
142                "The resulting stream may not play.\n");
143
144     if (!pps_seen)
145         av_log(avctx, AV_LOG_WARNING,
146                "Warning: PPS NALU missing or invalid. "
147                "The resulting stream may not play.\n");
148
149     return 0;
150 }
151
152 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
153 {
154     MediaCodecH264DecContext *s = avctx->priv_data;
155
156     ff_mediacodec_dec_close(avctx, &s->ctx);
157
158     av_fifo_free(s->fifo);
159
160     av_bitstream_filter_close(s->bsf);
161
162     return 0;
163 }
164
165 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
166 {
167     int ret;
168     FFAMediaFormat *format = NULL;
169     MediaCodecH264DecContext *s = avctx->priv_data;
170
171     format = ff_AMediaFormat_new();
172     if (!format) {
173         av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
174         ret = AVERROR_EXTERNAL;
175         goto done;
176     }
177
178     ff_AMediaFormat_setString(format, "mime", CODEC_MIME);
179     ff_AMediaFormat_setInt32(format, "width", avctx->width);
180     ff_AMediaFormat_setInt32(format, "height", avctx->height);
181
182     if (avctx->extradata[0] == 1) {
183         uint8_t *extradata = NULL;
184         int extradata_size = 0;
185
186         int sps_offset, sps_size;
187         int pps_offset, pps_size;
188
189         if ((ret = h264_extradata_to_annexb_sps_pps(avctx, &extradata, &extradata_size,
190                 &sps_offset, &sps_size, &pps_offset, &pps_size)) < 0) {
191             goto done;
192         }
193
194         ff_AMediaFormat_setBuffer(format, "csd-0", extradata + sps_offset, sps_size);
195         ff_AMediaFormat_setBuffer(format, "csd-1", extradata + pps_offset, pps_size);
196
197         av_freep(&extradata);
198     } else {
199         ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
200     }
201
202     if ((ret = ff_mediacodec_dec_init(avctx, &s->ctx, CODEC_MIME, format)) < 0) {
203         goto done;
204     }
205
206     av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
207
208     s->fifo = av_fifo_alloc(sizeof(AVPacket));
209     if (!s->fifo) {
210         ret = AVERROR(ENOMEM);
211         goto done;
212     }
213
214     s->bsf = av_bitstream_filter_init("h264_mp4toannexb");
215     if (!s->bsf) {
216         ret = AVERROR(ENOMEM);
217         goto done;
218     }
219
220 done:
221     if (format) {
222         ff_AMediaFormat_delete(format);
223     }
224
225     if (ret < 0) {
226         mediacodec_decode_close(avctx);
227     }
228     return ret;
229 }
230
231
232 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
233                                    int *got_frame, AVPacket *pkt)
234 {
235     MediaCodecH264DecContext *s = avctx->priv_data;
236
237     return ff_mediacodec_dec_decode(avctx, &s->ctx, frame, got_frame, pkt);
238 }
239
240 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
241                                    int *got_frame, AVPacket *avpkt)
242 {
243     MediaCodecH264DecContext *s = avctx->priv_data;
244     AVFrame *frame    = data;
245     int ret;
246
247     /* buffer the input packet */
248     if (avpkt->size) {
249         AVPacket input_ref = { 0 };
250
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));
254             if (ret < 0)
255                 return ret;
256         }
257
258         ret = av_packet_ref(&input_ref, avpkt);
259         if (ret < 0)
260             return ret;
261         av_fifo_generic_write(s->fifo, &input_ref, sizeof(input_ref), NULL);
262     }
263
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) {
268             int size;
269
270             /* no more data */
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);
274             }
275
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);
280
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);
285             if (ret < 0) {
286                 s->filtered_data = s->input_ref.data;
287                 size             = s->input_ref.size;
288             }
289             s->filtered_pkt      = s->input_ref;
290             s->filtered_pkt.data = s->filtered_data;
291             s->filtered_pkt.size = size;
292         }
293
294         ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
295         if (ret < 0)
296             return ret;
297
298         s->filtered_pkt.size -= ret;
299         s->filtered_pkt.data += ret;
300     }
301
302     return avpkt->size;
303 }
304
305 static void mediacodec_decode_flush(AVCodecContext *avctx)
306 {
307     MediaCodecH264DecContext *s = avctx->priv_data;
308
309     while (av_fifo_size(s->fifo)) {
310         AVPacket pkt;
311         av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
312         av_packet_unref(&pkt);
313     }
314     av_fifo_reset(s->fifo);
315
316     av_packet_unref(&s->input_ref);
317
318     av_init_packet(&s->filtered_pkt);
319     s->filtered_pkt.data = NULL;
320     s->filtered_pkt.size = 0;
321
322     ff_mediacodec_dec_flush(avctx, &s->ctx);
323 }
324
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,
336 };