]> git.sesse.net Git - ffmpeg/blob - libavcodec/mediacodecdec_h264.c
vp9: add mxext versions of the single-block (w=4,npx=8) h/v loopfilters.
[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 "h264.h"
36 #include "internal.h"
37 #include "mediacodecdec.h"
38 #include "mediacodec_wrapper.h"
39
40 #define CODEC_MIME "video/avc"
41
42 typedef struct MediaCodecH264DecContext {
43
44     MediaCodecDecContext *ctx;
45
46     AVBSFContext *bsf;
47
48     AVFifoBuffer *fifo;
49
50     AVPacket filtered_pkt;
51
52 } MediaCodecH264DecContext;
53
54 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
55 {
56     MediaCodecH264DecContext *s = avctx->priv_data;
57
58     ff_mediacodec_dec_close(avctx, s->ctx);
59     s->ctx = NULL;
60
61     av_fifo_free(s->fifo);
62
63     av_bsf_free(&s->bsf);
64     av_packet_unref(&s->filtered_pkt);
65
66     return 0;
67 }
68
69 static int h264_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
70 {
71     int i;
72     int ret = 0;
73     uint8_t *p = NULL;
74     static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
75
76     if (!out || !out_size) {
77         return AVERROR(EINVAL);
78     }
79
80     p = av_malloc(sizeof(nalu_header) + src_size);
81     if (!p) {
82         return AVERROR(ENOMEM);
83     }
84
85     *out = p;
86     *out_size = sizeof(nalu_header) + src_size;
87
88     memcpy(p, nalu_header, sizeof(nalu_header));
89     memcpy(p + sizeof(nalu_header), src, src_size);
90
91     /* Escape 0x00, 0x00, 0x0{0-3} pattern */
92     for (i = 4; i < *out_size; i++) {
93         if (i < *out_size - 3 &&
94             p[i + 0] == 0 &&
95             p[i + 1] == 0 &&
96             p[i + 2] <= 3) {
97             uint8_t *new;
98
99             *out_size += 1;
100             new = av_realloc(*out, *out_size);
101             if (!new) {
102                 ret = AVERROR(ENOMEM);
103                 goto done;
104             }
105             *out = p = new;
106
107             i = i + 3;
108             memmove(p + i, p + i - 1, *out_size - i);
109             p[i - 1] = 0x03;
110         }
111     }
112 done:
113     if (ret < 0) {
114         av_freep(out);
115         *out_size = 0;
116     }
117
118     return ret;
119 }
120
121 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
122 {
123     int i;
124     int ret;
125
126     H264ParamSets ps;
127     const PPS *pps = NULL;
128     const SPS *sps = NULL;
129     int is_avc = 0;
130     int nal_length_size = 0;
131
132     FFAMediaFormat *format = NULL;
133     MediaCodecH264DecContext *s = avctx->priv_data;
134
135     memset(&ps, 0, sizeof(ps));
136
137     format = ff_AMediaFormat_new();
138     if (!format) {
139         av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
140         ret = AVERROR_EXTERNAL;
141         goto done;
142     }
143
144     ff_AMediaFormat_setString(format, "mime", CODEC_MIME);
145     ff_AMediaFormat_setInt32(format, "width", avctx->width);
146     ff_AMediaFormat_setInt32(format, "height", avctx->height);
147
148     ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
149                                    &ps, &is_avc, &nal_length_size, 0, avctx);
150     if (ret < 0) {
151         goto done;
152     }
153
154     for (i = 0; i < MAX_PPS_COUNT; i++) {
155         if (ps.pps_list[i]) {
156             pps = (const PPS*)ps.pps_list[i]->data;
157             break;
158         }
159     }
160
161     if (pps) {
162         if (ps.sps_list[pps->sps_id]) {
163             sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
164         }
165     }
166
167     if (pps && sps) {
168         uint8_t *data = NULL;
169         size_t data_size = 0;
170
171         if ((ret = h264_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
172             goto done;
173         }
174         ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
175         av_freep(&data);
176
177         if ((ret = h264_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
178             goto done;
179         }
180         ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
181         av_freep(&data);
182     } else {
183         av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
184         ret = AVERROR_INVALIDDATA;
185         goto done;
186     }
187
188     s->ctx = av_mallocz(sizeof(*s->ctx));
189     if (!s->ctx) {
190         av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
191         ret = AVERROR(ENOMEM);
192         goto done;
193     }
194
195     if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, CODEC_MIME, format)) < 0) {
196         s->ctx = NULL;
197         goto done;
198     }
199
200     av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
201
202     s->fifo = av_fifo_alloc(sizeof(AVPacket));
203     if (!s->fifo) {
204         ret = AVERROR(ENOMEM);
205         goto done;
206     }
207
208     const AVBitStreamFilter *bsf = av_bsf_get_by_name("h264_mp4toannexb");
209     if(!bsf) {
210         ret = AVERROR_BSF_NOT_FOUND;
211         goto done;
212     }
213
214     if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
215         goto done;
216     }
217
218     if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
219         ((ret = av_bsf_init(s->bsf)) < 0)) {
220           goto done;
221     }
222
223     av_init_packet(&s->filtered_pkt);
224
225 done:
226     if (format) {
227         ff_AMediaFormat_delete(format);
228     }
229
230     if (ret < 0) {
231         mediacodec_decode_close(avctx);
232     }
233
234     ff_h264_ps_uninit(&ps);
235
236     return ret;
237 }
238
239
240 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
241                                    int *got_frame, AVPacket *pkt)
242 {
243     MediaCodecH264DecContext *s = avctx->priv_data;
244
245     return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
246 }
247
248 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
249                                    int *got_frame, AVPacket *avpkt)
250 {
251     MediaCodecH264DecContext *s = avctx->priv_data;
252     AVFrame *frame    = data;
253     int ret;
254
255     /* buffer the input packet */
256     if (avpkt->size) {
257         AVPacket input_pkt = { 0 };
258
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));
262             if (ret < 0)
263                 return ret;
264         }
265
266         ret = av_packet_ref(&input_pkt, avpkt);
267         if (ret < 0)
268             return ret;
269         av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
270     }
271
272     /*
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.
276      *
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.
279      *
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
282      * flushing state.
283      *
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.
287      *
288      * ff_mediacodec_dec_flush returns a negative value if an error has
289      * occurred.
290      *
291      */
292     if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
293         if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
294             return avpkt->size;
295         }
296     }
297
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 };
303
304             av_packet_unref(&s->filtered_pkt);
305
306             /* no more data */
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);
310             }
311
312             av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
313
314             ret = av_bsf_send_packet(s->bsf, &input_pkt);
315             if (ret < 0) {
316                 return ret;
317             }
318
319             ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
320             if (ret == AVERROR(EAGAIN)) {
321                 goto done;
322             }
323
324             /* h264_mp4toannexb is used here and does not requires flushing */
325             av_assert0(ret != AVERROR_EOF);
326
327             if (ret < 0) {
328                 return ret;
329             }
330         }
331
332         ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
333         if (ret < 0)
334             return ret;
335
336         s->filtered_pkt.size -= ret;
337         s->filtered_pkt.data += ret;
338     }
339 done:
340     return avpkt->size;
341 }
342
343 static void mediacodec_decode_flush(AVCodecContext *avctx)
344 {
345     MediaCodecH264DecContext *s = avctx->priv_data;
346
347     while (av_fifo_size(s->fifo)) {
348         AVPacket pkt;
349         av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
350         av_packet_unref(&pkt);
351     }
352     av_fifo_reset(s->fifo);
353
354     av_packet_unref(&s->filtered_pkt);
355
356     ff_mediacodec_dec_flush(avctx, s->ctx);
357 }
358
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,
371 };