]> git.sesse.net Git - ffmpeg/blob - libavcodec/mediacodecdec_h264.c
avcodec/sheervideo: fix prediction for ybyr format
[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/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"
33
34 #include "avcodec.h"
35 #include "internal.h"
36 #include "mediacodecdec.h"
37 #include "mediacodec_wrapper.h"
38
39 #define CODEC_MIME "video/avc"
40
41 typedef struct MediaCodecH264DecContext {
42
43     MediaCodecDecContext ctx;
44
45     AVBSFContext *bsf;
46
47     AVFifoBuffer *fifo;
48
49     AVPacket filtered_pkt;
50
51 } MediaCodecH264DecContext;
52
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)
57 {
58     uint16_t unit_size;
59     uint64_t total_size = 0;
60
61     uint8_t i, j, unit_nb;
62     uint8_t sps_seen = 0;
63     uint8_t pps_seen = 0;
64
65     const uint8_t *extradata;
66     static const uint8_t nalu_header[4] = { 0x00, 0x00, 0x00, 0x01 };
67
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);
72     }
73
74     *extradata_annexb = NULL;
75     *extradata_annexb_size = 0;
76
77     *sps_offset = *sps_size = 0;
78     *pps_offset = *pps_size = 0;
79
80     extradata = avctx->extradata + 4;
81
82     /* skip length size */
83     extradata++;
84
85     for (j = 0; j < 2; j ++) {
86
87         if (j == 0) {
88             /* number of sps unit(s) */
89             unit_nb = *extradata++ & 0x1f;
90         } else {
91             /* number of pps unit(s) */
92             unit_nb = *extradata++;
93         }
94
95         for (i = 0; i < unit_nb; i++) {
96             int err;
97
98             unit_size   = AV_RB16(extradata);
99             total_size += unit_size + 4;
100
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);
106             }
107
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);
113             }
114
115             if ((err = av_reallocp(extradata_annexb, total_size)) < 0) {
116                 return err;
117             }
118
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;
122         }
123
124         if (unit_nb) {
125             if (j == 0) {
126                 sps_seen = 1;
127                 *sps_size = total_size;
128             } else {
129                 pps_seen = 1;
130                 *pps_size = total_size - *sps_size;
131                 *pps_offset = *sps_size;
132             }
133         }
134     }
135
136     *extradata_annexb_size = total_size;
137
138     if (!sps_seen)
139         av_log(avctx, AV_LOG_WARNING,
140                "Warning: SPS NALU missing or invalid. "
141                "The resulting stream may not play.\n");
142
143     if (!pps_seen)
144         av_log(avctx, AV_LOG_WARNING,
145                "Warning: PPS NALU missing or invalid. "
146                "The resulting stream may not play.\n");
147
148     return 0;
149 }
150
151 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
152 {
153     MediaCodecH264DecContext *s = avctx->priv_data;
154
155     ff_mediacodec_dec_close(avctx, &s->ctx);
156
157     av_fifo_free(s->fifo);
158
159     av_bsf_free(&s->bsf);
160     av_packet_unref(&s->filtered_pkt);
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     const AVBitStreamFilter *bsf = av_bsf_get_by_name("h264_mp4toannexb");
215     if(!bsf) {
216         ret = AVERROR_BSF_NOT_FOUND;
217         goto done;
218     }
219
220     if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
221         goto done;
222     }
223
224     if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
225         ((ret = av_bsf_init(s->bsf)) < 0)) {
226           goto done;
227     }
228
229     av_init_packet(&s->filtered_pkt);
230
231 done:
232     if (format) {
233         ff_AMediaFormat_delete(format);
234     }
235
236     if (ret < 0) {
237         mediacodec_decode_close(avctx);
238     }
239     return ret;
240 }
241
242
243 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
244                                    int *got_frame, AVPacket *pkt)
245 {
246     MediaCodecH264DecContext *s = avctx->priv_data;
247
248     return ff_mediacodec_dec_decode(avctx, &s->ctx, frame, got_frame, pkt);
249 }
250
251 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
252                                    int *got_frame, AVPacket *avpkt)
253 {
254     MediaCodecH264DecContext *s = avctx->priv_data;
255     AVFrame *frame    = data;
256     int ret;
257
258     /* buffer the input packet */
259     if (avpkt->size) {
260         AVPacket input_pkt = { 0 };
261
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));
265             if (ret < 0)
266                 return ret;
267         }
268
269         ret = av_packet_ref(&input_pkt, avpkt);
270         if (ret < 0)
271             return ret;
272         av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
273     }
274
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 };
280
281             av_packet_unref(&s->filtered_pkt);
282
283             /* no more data */
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);
287             }
288
289             av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
290
291             ret = av_bsf_send_packet(s->bsf, &input_pkt);
292             if (ret < 0) {
293                 return ret;
294             }
295
296             ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
297             if (ret == AVERROR(EAGAIN)) {
298                 goto done;
299             }
300
301             /* h264_mp4toannexb is used here and does not requires flushing */
302             av_assert0(ret != AVERROR_EOF);
303
304             if (ret < 0) {
305                 return ret;
306             }
307         }
308
309         ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
310         if (ret < 0)
311             return ret;
312
313         s->filtered_pkt.size -= ret;
314         s->filtered_pkt.data += ret;
315     }
316 done:
317     return avpkt->size;
318 }
319
320 static void mediacodec_decode_flush(AVCodecContext *avctx)
321 {
322     MediaCodecH264DecContext *s = avctx->priv_data;
323
324     while (av_fifo_size(s->fifo)) {
325         AVPacket pkt;
326         av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
327         av_packet_unref(&pkt);
328     }
329     av_fifo_reset(s->fifo);
330
331     av_packet_unref(&s->filtered_pkt);
332
333     ff_mediacodec_dec_flush(avctx, &s->ctx);
334 }
335
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,
347 };