]> git.sesse.net Git - ffmpeg/blob - libavcodec/mediacodecdec_h264.c
Merge commit '48e2967cd50c2e1a2a539fd697d20ead2c5c4cc8'
[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
60     av_fifo_free(s->fifo);
61
62     av_bsf_free(&s->bsf);
63     av_packet_unref(&s->filtered_pkt);
64
65     return 0;
66 }
67
68 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
69 {
70     int i;
71     int ret;
72
73     H264ParamSets ps;
74     const PPS *pps = NULL;
75     const SPS *sps = NULL;
76     int is_avc = 0;
77     int nal_length_size = 0;
78
79     FFAMediaFormat *format = NULL;
80     MediaCodecH264DecContext *s = avctx->priv_data;
81
82     memset(&ps, 0, sizeof(ps));
83
84     format = ff_AMediaFormat_new();
85     if (!format) {
86         av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
87         ret = AVERROR_EXTERNAL;
88         goto done;
89     }
90
91     ff_AMediaFormat_setString(format, "mime", CODEC_MIME);
92     ff_AMediaFormat_setInt32(format, "width", avctx->width);
93     ff_AMediaFormat_setInt32(format, "height", avctx->height);
94
95     ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
96                                    &ps, &is_avc, &nal_length_size, 0, avctx);
97     if (ret < 0) {
98         goto done;
99     }
100
101     for (i = 0; i < MAX_PPS_COUNT; i++) {
102         if (ps.pps_list[i]) {
103             pps = (const PPS*)ps.pps_list[i]->data;
104             break;
105         }
106     }
107
108     if (pps) {
109         if (ps.sps_list[pps->sps_id]) {
110             sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
111         }
112     }
113
114     if (pps && sps) {
115         static const uint8_t nal_headers[] = { 0x00, 0x00, 0x00, 0x01 };
116
117         uint8_t *data = NULL;
118         size_t data_size = sizeof(nal_headers) + FFMAX(sps->data_size, pps->data_size);
119
120         data = av_mallocz(data_size);
121         if (!data) {
122             ret = AVERROR(ENOMEM);
123             goto done;
124         }
125
126         memcpy(data, nal_headers, sizeof(nal_headers));
127         memcpy(data + sizeof(nal_headers), sps->data, sps->data_size);
128         ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, sizeof(nal_headers) + sps->data_size);
129
130         memcpy(data + sizeof(nal_headers), pps->data, pps->data_size);
131         ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, sizeof(nal_headers) + pps->data_size);
132
133         av_freep(&data);
134     } else {
135         av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
136         ret = AVERROR_INVALIDDATA;
137         goto done;
138     }
139
140     if ((ret = ff_mediacodec_dec_init(avctx, &s->ctx, CODEC_MIME, format)) < 0) {
141         goto done;
142     }
143
144     av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
145
146     s->fifo = av_fifo_alloc(sizeof(AVPacket));
147     if (!s->fifo) {
148         ret = AVERROR(ENOMEM);
149         goto done;
150     }
151
152     const AVBitStreamFilter *bsf = av_bsf_get_by_name("h264_mp4toannexb");
153     if(!bsf) {
154         ret = AVERROR_BSF_NOT_FOUND;
155         goto done;
156     }
157
158     if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
159         goto done;
160     }
161
162     if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
163         ((ret = av_bsf_init(s->bsf)) < 0)) {
164           goto done;
165     }
166
167     av_init_packet(&s->filtered_pkt);
168
169 done:
170     if (format) {
171         ff_AMediaFormat_delete(format);
172     }
173
174     if (ret < 0) {
175         mediacodec_decode_close(avctx);
176     }
177
178     ff_h264_ps_uninit(&ps);
179
180     return ret;
181 }
182
183
184 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
185                                    int *got_frame, AVPacket *pkt)
186 {
187     MediaCodecH264DecContext *s = avctx->priv_data;
188
189     return ff_mediacodec_dec_decode(avctx, &s->ctx, frame, got_frame, pkt);
190 }
191
192 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
193                                    int *got_frame, AVPacket *avpkt)
194 {
195     MediaCodecH264DecContext *s = avctx->priv_data;
196     AVFrame *frame    = data;
197     int ret;
198
199     /* buffer the input packet */
200     if (avpkt->size) {
201         AVPacket input_pkt = { 0 };
202
203         if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
204             ret = av_fifo_realloc2(s->fifo,
205                                    av_fifo_size(s->fifo) + sizeof(input_pkt));
206             if (ret < 0)
207                 return ret;
208         }
209
210         ret = av_packet_ref(&input_pkt, avpkt);
211         if (ret < 0)
212             return ret;
213         av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
214     }
215
216     /* process buffered data */
217     while (!*got_frame) {
218         /* prepare the input data -- convert to Annex B if needed */
219         if (s->filtered_pkt.size <= 0) {
220             AVPacket input_pkt = { 0 };
221
222             av_packet_unref(&s->filtered_pkt);
223
224             /* no more data */
225             if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
226                 return avpkt->size ? avpkt->size :
227                     ff_mediacodec_dec_decode(avctx, &s->ctx, frame, got_frame, avpkt);
228             }
229
230             av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
231
232             ret = av_bsf_send_packet(s->bsf, &input_pkt);
233             if (ret < 0) {
234                 return ret;
235             }
236
237             ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
238             if (ret == AVERROR(EAGAIN)) {
239                 goto done;
240             }
241
242             /* h264_mp4toannexb is used here and does not requires flushing */
243             av_assert0(ret != AVERROR_EOF);
244
245             if (ret < 0) {
246                 return ret;
247             }
248         }
249
250         ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
251         if (ret < 0)
252             return ret;
253
254         s->filtered_pkt.size -= ret;
255         s->filtered_pkt.data += ret;
256     }
257 done:
258     return avpkt->size;
259 }
260
261 static void mediacodec_decode_flush(AVCodecContext *avctx)
262 {
263     MediaCodecH264DecContext *s = avctx->priv_data;
264
265     while (av_fifo_size(s->fifo)) {
266         AVPacket pkt;
267         av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
268         av_packet_unref(&pkt);
269     }
270     av_fifo_reset(s->fifo);
271
272     av_packet_unref(&s->filtered_pkt);
273
274     ff_mediacodec_dec_flush(avctx, &s->ctx);
275 }
276
277 AVCodec ff_h264_mediacodec_decoder = {
278     .name           = "h264_mediacodec",
279     .long_name      = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
280     .type           = AVMEDIA_TYPE_VIDEO,
281     .id             = AV_CODEC_ID_H264,
282     .priv_data_size = sizeof(MediaCodecH264DecContext),
283     .init           = mediacodec_decode_init,
284     .decode         = mediacodec_decode_frame,
285     .flush          = mediacodec_decode_flush,
286     .close          = mediacodec_decode_close,
287     .capabilities   = CODEC_CAP_DELAY,
288     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
289 };