]> git.sesse.net Git - ffmpeg/blob - libavcodec/mediacodecdec_h2645.c
aacenc: use the decoder's lcg PRNG
[ffmpeg] / libavcodec / mediacodecdec_h2645.c
1 /*
2  * Android MediaCodec H.264 / H.265 decoders
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_parse.h"
36 #include "hevc_parse.h"
37 #include "internal.h"
38 #include "mediacodecdec.h"
39 #include "mediacodec_wrapper.h"
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 av_cold int mediacodec_decode_close(AVCodecContext *avctx)
54 {
55     MediaCodecH264DecContext *s = avctx->priv_data;
56
57     ff_mediacodec_dec_close(avctx, s->ctx);
58     s->ctx = NULL;
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 int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
69 {
70     int i;
71     int ret = 0;
72     uint8_t *p = NULL;
73     static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
74
75     if (!out || !out_size) {
76         return AVERROR(EINVAL);
77     }
78
79     p = av_malloc(sizeof(nalu_header) + src_size);
80     if (!p) {
81         return AVERROR(ENOMEM);
82     }
83
84     *out = p;
85     *out_size = sizeof(nalu_header) + src_size;
86
87     memcpy(p, nalu_header, sizeof(nalu_header));
88     memcpy(p + sizeof(nalu_header), src, src_size);
89
90     /* Escape 0x00, 0x00, 0x0{0-3} pattern */
91     for (i = 4; i < *out_size; i++) {
92         if (i < *out_size - 3 &&
93             p[i + 0] == 0 &&
94             p[i + 1] == 0 &&
95             p[i + 2] <= 3) {
96             uint8_t *new;
97
98             *out_size += 1;
99             new = av_realloc(*out, *out_size);
100             if (!new) {
101                 ret = AVERROR(ENOMEM);
102                 goto done;
103             }
104             *out = p = new;
105
106             i = i + 2;
107             memmove(p + i + 1, p + i, *out_size - (i + 1));
108             p[i] = 0x03;
109         }
110     }
111 done:
112     if (ret < 0) {
113         av_freep(out);
114         *out_size = 0;
115     }
116
117     return ret;
118 }
119
120 #if CONFIG_H264_MEDIACODEC_DECODER
121 static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
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     memset(&ps, 0, sizeof(ps));
133
134     ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
135                                    &ps, &is_avc, &nal_length_size, 0, avctx);
136     if (ret < 0) {
137         goto done;
138     }
139
140     for (i = 0; i < MAX_PPS_COUNT; i++) {
141         if (ps.pps_list[i]) {
142             pps = (const PPS*)ps.pps_list[i]->data;
143             break;
144         }
145     }
146
147     if (pps) {
148         if (ps.sps_list[pps->sps_id]) {
149             sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
150         }
151     }
152
153     if (pps && sps) {
154         uint8_t *data = NULL;
155         int data_size = 0;
156
157         if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
158             goto done;
159         }
160         ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
161         av_freep(&data);
162
163         if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
164             goto done;
165         }
166         ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
167         av_freep(&data);
168     } else {
169         av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
170         ret = AVERROR_INVALIDDATA;
171     }
172
173 done:
174     ff_h264_ps_uninit(&ps);
175
176     return ret;
177 }
178 #endif
179
180 #if CONFIG_HEVC_MEDIACODEC_DECODER
181 static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
182 {
183     int i;
184     int ret;
185
186     HEVCParamSets ps;
187
188     const HEVCVPS *vps = NULL;
189     const HEVCPPS *pps = NULL;
190     const HEVCSPS *sps = NULL;
191     int is_nalff = 0;
192     int nal_length_size = 0;
193
194     uint8_t *vps_data = NULL;
195     uint8_t *sps_data = NULL;
196     uint8_t *pps_data = NULL;
197     int vps_data_size = 0;
198     int sps_data_size = 0;
199     int pps_data_size = 0;
200
201     memset(&ps, 0, sizeof(ps));
202
203     ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
204                                 &ps, &is_nalff, &nal_length_size, 0, avctx);
205     if (ret < 0) {
206         goto done;
207     }
208
209     for (i = 0; i < MAX_VPS_COUNT; i++) {
210         if (ps.vps_list[i]) {
211             vps = (const HEVCVPS*)ps.vps_list[i]->data;
212             break;
213         }
214     }
215
216     for (i = 0; i < MAX_PPS_COUNT; i++) {
217         if (ps.pps_list[i]) {
218             pps = (const HEVCPPS*)ps.pps_list[i]->data;
219             break;
220         }
221     }
222
223     if (pps) {
224         if (ps.sps_list[pps->sps_id]) {
225             sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
226         }
227     }
228
229     if (vps && pps && sps) {
230         uint8_t *data;
231         int data_size;
232
233         if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
234             (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
235             (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
236             goto done;
237         }
238
239         data_size = vps_data_size + sps_data_size + pps_data_size;
240         data = av_mallocz(data_size);
241         if (!data) {
242             ret = AVERROR(ENOMEM);
243             goto done;
244         }
245
246         memcpy(data                                , vps_data, vps_data_size);
247         memcpy(data + vps_data_size                , sps_data, sps_data_size);
248         memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
249
250         ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
251
252         av_freep(&data);
253     } else {
254         av_log(avctx, AV_LOG_ERROR, "Could not extract VPS/PPS/SPS from extradata");
255         ret = AVERROR_INVALIDDATA;
256     }
257
258 done:
259     av_freep(&vps_data);
260     av_freep(&sps_data);
261     av_freep(&pps_data);
262
263     return ret;
264 }
265 #endif
266
267 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
268 {
269     int ret;
270
271     const char *codec_mime = NULL;
272
273     const char *bsf_name = NULL;
274     const AVBitStreamFilter *bsf = NULL;
275
276     FFAMediaFormat *format = NULL;
277     MediaCodecH264DecContext *s = avctx->priv_data;
278
279     format = ff_AMediaFormat_new();
280     if (!format) {
281         av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
282         ret = AVERROR_EXTERNAL;
283         goto done;
284     }
285
286     switch (avctx->codec_id) {
287 #if CONFIG_H264_MEDIACODEC_DECODER
288     case AV_CODEC_ID_H264:
289         codec_mime = "video/avc";
290         bsf_name = "h264_mp4toannexb";
291
292         ret = h264_set_extradata(avctx, format);
293         if (ret < 0)
294             goto done;
295         break;
296 #endif
297 #if CONFIG_HEVC_MEDIACODEC_DECODER
298     case AV_CODEC_ID_HEVC:
299         codec_mime = "video/hevc";
300         bsf_name = "hevc_mp4toannexb";
301
302         ret = hevc_set_extradata(avctx, format);
303         if (ret < 0)
304             goto done;
305         break;
306 #endif
307     default:
308         av_assert0(0);
309     }
310
311     ff_AMediaFormat_setString(format, "mime", codec_mime);
312     ff_AMediaFormat_setInt32(format, "width", avctx->width);
313     ff_AMediaFormat_setInt32(format, "height", avctx->height);
314
315     s->ctx = av_mallocz(sizeof(*s->ctx));
316     if (!s->ctx) {
317         av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
318         ret = AVERROR(ENOMEM);
319         goto done;
320     }
321
322     if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
323         s->ctx = NULL;
324         goto done;
325     }
326
327     av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
328
329     s->fifo = av_fifo_alloc(sizeof(AVPacket));
330     if (!s->fifo) {
331         ret = AVERROR(ENOMEM);
332         goto done;
333     }
334
335     bsf = av_bsf_get_by_name(bsf_name);
336     if(!bsf) {
337         ret = AVERROR_BSF_NOT_FOUND;
338         goto done;
339     }
340
341     if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
342         goto done;
343     }
344
345     if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
346         ((ret = av_bsf_init(s->bsf)) < 0)) {
347           goto done;
348     }
349
350     av_init_packet(&s->filtered_pkt);
351
352 done:
353     if (format) {
354         ff_AMediaFormat_delete(format);
355     }
356
357     if (ret < 0) {
358         mediacodec_decode_close(avctx);
359     }
360
361     return ret;
362 }
363
364
365 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
366                                    int *got_frame, AVPacket *pkt)
367 {
368     MediaCodecH264DecContext *s = avctx->priv_data;
369
370     return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
371 }
372
373 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
374                                    int *got_frame, AVPacket *avpkt)
375 {
376     MediaCodecH264DecContext *s = avctx->priv_data;
377     AVFrame *frame    = data;
378     int ret;
379
380     /* buffer the input packet */
381     if (avpkt->size) {
382         AVPacket input_pkt = { 0 };
383
384         if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
385             ret = av_fifo_realloc2(s->fifo,
386                                    av_fifo_size(s->fifo) + sizeof(input_pkt));
387             if (ret < 0)
388                 return ret;
389         }
390
391         ret = av_packet_ref(&input_pkt, avpkt);
392         if (ret < 0)
393             return ret;
394         av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
395     }
396
397     /*
398      * MediaCodec.flush() discards both input and output buffers, thus we
399      * need to delay the call to this function until the user has released or
400      * renderered the frames he retains.
401      *
402      * After we have buffered an input packet, check if the codec is in the
403      * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
404      *
405      * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
406      * the codec (because the user retains frames). The codec stays in the
407      * flushing state.
408      *
409      * ff_mediacodec_dec_flush returns 1 if the flush can actually be
410      * performed on the codec. The codec leaves the flushing state and can
411      * process again packets.
412      *
413      * ff_mediacodec_dec_flush returns a negative value if an error has
414      * occurred.
415      *
416      */
417     if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
418         if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
419             return avpkt->size;
420         }
421     }
422
423     /* process buffered data */
424     while (!*got_frame) {
425         /* prepare the input data -- convert to Annex B if needed */
426         if (s->filtered_pkt.size <= 0) {
427             AVPacket input_pkt = { 0 };
428
429             av_packet_unref(&s->filtered_pkt);
430
431             /* no more data */
432             if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
433                 return avpkt->size ? avpkt->size :
434                     ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
435             }
436
437             av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
438
439             ret = av_bsf_send_packet(s->bsf, &input_pkt);
440             if (ret < 0) {
441                 return ret;
442             }
443
444             ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
445             if (ret == AVERROR(EAGAIN)) {
446                 goto done;
447             }
448
449             /* {h264,hevc}_mp4toannexb are used here and do not require flushing */
450             av_assert0(ret != AVERROR_EOF);
451
452             if (ret < 0) {
453                 return ret;
454             }
455         }
456
457         ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
458         if (ret < 0)
459             return ret;
460
461         s->filtered_pkt.size -= ret;
462         s->filtered_pkt.data += ret;
463     }
464 done:
465     return avpkt->size;
466 }
467
468 static void mediacodec_decode_flush(AVCodecContext *avctx)
469 {
470     MediaCodecH264DecContext *s = avctx->priv_data;
471
472     while (av_fifo_size(s->fifo)) {
473         AVPacket pkt;
474         av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
475         av_packet_unref(&pkt);
476     }
477     av_fifo_reset(s->fifo);
478
479     av_packet_unref(&s->filtered_pkt);
480
481     ff_mediacodec_dec_flush(avctx, s->ctx);
482 }
483
484 #if CONFIG_H264_MEDIACODEC_DECODER
485 AVCodec ff_h264_mediacodec_decoder = {
486     .name           = "h264_mediacodec",
487     .long_name      = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
488     .type           = AVMEDIA_TYPE_VIDEO,
489     .id             = AV_CODEC_ID_H264,
490     .priv_data_size = sizeof(MediaCodecH264DecContext),
491     .init           = mediacodec_decode_init,
492     .decode         = mediacodec_decode_frame,
493     .flush          = mediacodec_decode_flush,
494     .close          = mediacodec_decode_close,
495     .capabilities   = CODEC_CAP_DELAY,
496     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
497 };
498 #endif
499
500 #if CONFIG_HEVC_MEDIACODEC_DECODER
501 AVCodec ff_hevc_mediacodec_decoder = {
502     .name           = "hevc_mediacodec",
503     .long_name      = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
504     .type           = AVMEDIA_TYPE_VIDEO,
505     .id             = AV_CODEC_ID_HEVC,
506     .priv_data_size = sizeof(MediaCodecH264DecContext),
507     .init           = mediacodec_decode_init,
508     .decode         = mediacodec_decode_frame,
509     .flush          = mediacodec_decode_flush,
510     .close          = mediacodec_decode_close,
511     .capabilities   = CODEC_CAP_DELAY,
512     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
513 };
514 #endif