]> git.sesse.net Git - ffmpeg/blob - libavcodec/mediacodecdec.c
Merge commit 'a3a0230a9870b9018dc7415ae5872784d524cfe5'
[ffmpeg] / libavcodec / mediacodecdec.c
1 /*
2  * Android MediaCodec H.264 / H.265 / MPEG-4 / VP8 / VP9 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 "mediacodec_wrapper.h"
39 #include "mediacodecdec_common.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 #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
69 static int h2645_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 + 2;
108             memmove(p + i + 1, p + i, *out_size - (i + 1));
109             p[i] = 0x03;
110         }
111     }
112 done:
113     if (ret < 0) {
114         av_freep(out);
115         *out_size = 0;
116     }
117
118     return ret;
119 }
120 #endif
121
122 #if CONFIG_H264_MEDIACODEC_DECODER
123 static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
124 {
125     int i;
126     int ret;
127
128     H264ParamSets ps;
129     const PPS *pps = NULL;
130     const SPS *sps = NULL;
131     int is_avc = 0;
132     int nal_length_size = 0;
133
134     memset(&ps, 0, sizeof(ps));
135
136     ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
137                                    &ps, &is_avc, &nal_length_size, 0, avctx);
138     if (ret < 0) {
139         goto done;
140     }
141
142     for (i = 0; i < MAX_PPS_COUNT; i++) {
143         if (ps.pps_list[i]) {
144             pps = (const PPS*)ps.pps_list[i]->data;
145             break;
146         }
147     }
148
149     if (pps) {
150         if (ps.sps_list[pps->sps_id]) {
151             sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
152         }
153     }
154
155     if (pps && sps) {
156         uint8_t *data = NULL;
157         int data_size = 0;
158
159         if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
160             goto done;
161         }
162         ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
163         av_freep(&data);
164
165         if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
166             goto done;
167         }
168         ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
169         av_freep(&data);
170     } else {
171         av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
172         ret = AVERROR_INVALIDDATA;
173     }
174
175 done:
176     ff_h264_ps_uninit(&ps);
177
178     return ret;
179 }
180 #endif
181
182 #if CONFIG_HEVC_MEDIACODEC_DECODER
183 static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
184 {
185     int i;
186     int ret;
187
188     HEVCParamSets ps;
189
190     const HEVCVPS *vps = NULL;
191     const HEVCPPS *pps = NULL;
192     const HEVCSPS *sps = NULL;
193     int is_nalff = 0;
194     int nal_length_size = 0;
195
196     uint8_t *vps_data = NULL;
197     uint8_t *sps_data = NULL;
198     uint8_t *pps_data = NULL;
199     int vps_data_size = 0;
200     int sps_data_size = 0;
201     int pps_data_size = 0;
202
203     memset(&ps, 0, sizeof(ps));
204
205     ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
206                                 &ps, &is_nalff, &nal_length_size, 0, avctx);
207     if (ret < 0) {
208         goto done;
209     }
210
211     for (i = 0; i < MAX_VPS_COUNT; i++) {
212         if (ps.vps_list[i]) {
213             vps = (const HEVCVPS*)ps.vps_list[i]->data;
214             break;
215         }
216     }
217
218     for (i = 0; i < MAX_PPS_COUNT; i++) {
219         if (ps.pps_list[i]) {
220             pps = (const HEVCPPS*)ps.pps_list[i]->data;
221             break;
222         }
223     }
224
225     if (pps) {
226         if (ps.sps_list[pps->sps_id]) {
227             sps = (const HEVCSPS*)ps.sps_list[pps->sps_id]->data;
228         }
229     }
230
231     if (vps && pps && sps) {
232         uint8_t *data;
233         int data_size;
234
235         if ((ret = h2645_ps_to_nalu(vps->data, vps->data_size, &vps_data, &vps_data_size)) < 0 ||
236             (ret = h2645_ps_to_nalu(sps->data, sps->data_size, &sps_data, &sps_data_size)) < 0 ||
237             (ret = h2645_ps_to_nalu(pps->data, pps->data_size, &pps_data, &pps_data_size)) < 0) {
238             goto done;
239         }
240
241         data_size = vps_data_size + sps_data_size + pps_data_size;
242         data = av_mallocz(data_size);
243         if (!data) {
244             ret = AVERROR(ENOMEM);
245             goto done;
246         }
247
248         memcpy(data                                , vps_data, vps_data_size);
249         memcpy(data + vps_data_size                , sps_data, sps_data_size);
250         memcpy(data + vps_data_size + sps_data_size, pps_data, pps_data_size);
251
252         ff_AMediaFormat_setBuffer(format, "csd-0", data, data_size);
253
254         av_freep(&data);
255     } else {
256         av_log(avctx, AV_LOG_ERROR, "Could not extract VPS/PPS/SPS from extradata");
257         ret = AVERROR_INVALIDDATA;
258     }
259
260 done:
261     av_freep(&vps_data);
262     av_freep(&sps_data);
263     av_freep(&pps_data);
264
265     return ret;
266 }
267 #endif
268
269 #if CONFIG_MPEG4_MEDIACODEC_DECODER
270 static int mpeg4_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
271 {
272     int ret = 0;
273
274     if (avctx->extradata) {
275         ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
276     }
277
278     return ret;
279 }
280 #endif
281
282 #if CONFIG_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
283 static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
284 {
285     int ret = 0;
286
287     if (avctx->extradata) {
288         ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
289     }
290
291     return ret;
292 }
293 #endif
294
295 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
296 {
297     int ret;
298
299     const char *codec_mime = NULL;
300
301     const char *bsf_name = NULL;
302     const AVBitStreamFilter *bsf = NULL;
303
304     FFAMediaFormat *format = NULL;
305     MediaCodecH264DecContext *s = avctx->priv_data;
306
307     format = ff_AMediaFormat_new();
308     if (!format) {
309         av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
310         ret = AVERROR_EXTERNAL;
311         goto done;
312     }
313
314     switch (avctx->codec_id) {
315 #if CONFIG_H264_MEDIACODEC_DECODER
316     case AV_CODEC_ID_H264:
317         codec_mime = "video/avc";
318         bsf_name = "h264_mp4toannexb";
319
320         ret = h264_set_extradata(avctx, format);
321         if (ret < 0)
322             goto done;
323         break;
324 #endif
325 #if CONFIG_HEVC_MEDIACODEC_DECODER
326     case AV_CODEC_ID_HEVC:
327         codec_mime = "video/hevc";
328         bsf_name = "hevc_mp4toannexb";
329
330         ret = hevc_set_extradata(avctx, format);
331         if (ret < 0)
332             goto done;
333         break;
334 #endif
335 #if CONFIG_MPEG4_MEDIACODEC_DECODER
336     case AV_CODEC_ID_MPEG4:
337         codec_mime = "video/mp4v-es",
338
339         ret = mpeg4_set_extradata(avctx, format);
340         if (ret < 0)
341             goto done;
342         break;
343 #endif
344 #if CONFIG_VP8_MEDIACODEC_DECODER
345     case AV_CODEC_ID_VP8:
346         codec_mime = "video/x-vnd.on2.vp8";
347
348         ret = vpx_set_extradata(avctx, format);
349         if (ret < 0)
350             goto done;
351         break;
352 #endif
353 #if CONFIG_VP9_MEDIACODEC_DECODER
354     case AV_CODEC_ID_VP9:
355         codec_mime = "video/x-vnd.on2.vp9";
356
357         ret = vpx_set_extradata(avctx, format);
358         if (ret < 0)
359             goto done;
360         break;
361 #endif
362     default:
363         av_assert0(0);
364     }
365
366     ff_AMediaFormat_setString(format, "mime", codec_mime);
367     ff_AMediaFormat_setInt32(format, "width", avctx->width);
368     ff_AMediaFormat_setInt32(format, "height", avctx->height);
369
370     s->ctx = av_mallocz(sizeof(*s->ctx));
371     if (!s->ctx) {
372         av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
373         ret = AVERROR(ENOMEM);
374         goto done;
375     }
376
377     if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
378         s->ctx = NULL;
379         goto done;
380     }
381
382     av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
383
384     s->fifo = av_fifo_alloc(sizeof(AVPacket));
385     if (!s->fifo) {
386         ret = AVERROR(ENOMEM);
387         goto done;
388     }
389
390     if (bsf_name) {
391     bsf = av_bsf_get_by_name(bsf_name);
392     if(!bsf) {
393         ret = AVERROR_BSF_NOT_FOUND;
394         goto done;
395     }
396
397     if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
398         goto done;
399     }
400
401     if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
402         ((ret = av_bsf_init(s->bsf)) < 0)) {
403           goto done;
404     }
405     }
406
407     av_init_packet(&s->filtered_pkt);
408
409 done:
410     if (format) {
411         ff_AMediaFormat_delete(format);
412     }
413
414     if (ret < 0) {
415         mediacodec_decode_close(avctx);
416     }
417
418     return ret;
419 }
420
421
422 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
423                                    int *got_frame, AVPacket *pkt)
424 {
425     MediaCodecH264DecContext *s = avctx->priv_data;
426
427     return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
428 }
429
430 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
431                                    int *got_frame, AVPacket *avpkt)
432 {
433     MediaCodecH264DecContext *s = avctx->priv_data;
434     AVFrame *frame    = data;
435     int ret;
436
437     /* buffer the input packet */
438     if (avpkt->size) {
439         AVPacket input_pkt = { 0 };
440
441         if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
442             ret = av_fifo_realloc2(s->fifo,
443                                    av_fifo_size(s->fifo) + sizeof(input_pkt));
444             if (ret < 0)
445                 return ret;
446         }
447
448         ret = av_packet_ref(&input_pkt, avpkt);
449         if (ret < 0)
450             return ret;
451         av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
452     }
453
454     /*
455      * MediaCodec.flush() discards both input and output buffers, thus we
456      * need to delay the call to this function until the user has released or
457      * renderered the frames he retains.
458      *
459      * After we have buffered an input packet, check if the codec is in the
460      * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
461      *
462      * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
463      * the codec (because the user retains frames). The codec stays in the
464      * flushing state.
465      *
466      * ff_mediacodec_dec_flush returns 1 if the flush can actually be
467      * performed on the codec. The codec leaves the flushing state and can
468      * process again packets.
469      *
470      * ff_mediacodec_dec_flush returns a negative value if an error has
471      * occurred.
472      *
473      */
474     if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
475         if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
476             return avpkt->size;
477         }
478     }
479
480     /* process buffered data */
481     while (!*got_frame) {
482         /* prepare the input data -- convert to Annex B if needed */
483         if (s->filtered_pkt.size <= 0) {
484             AVPacket input_pkt = { 0 };
485
486             av_packet_unref(&s->filtered_pkt);
487
488             /* no more data */
489             if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
490                 return avpkt->size ? avpkt->size :
491                     ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
492             }
493
494             av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
495
496             if (s->bsf) {
497             ret = av_bsf_send_packet(s->bsf, &input_pkt);
498             if (ret < 0) {
499                 return ret;
500             }
501
502             ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
503             if (ret == AVERROR(EAGAIN)) {
504                 goto done;
505             }
506             } else {
507                 av_packet_move_ref(&s->filtered_pkt, &input_pkt);
508             }
509
510             /* {h264,hevc}_mp4toannexb are used here and do not require flushing */
511             av_assert0(ret != AVERROR_EOF);
512
513             if (ret < 0) {
514                 return ret;
515             }
516         }
517
518         ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
519         if (ret < 0)
520             return ret;
521
522         s->filtered_pkt.size -= ret;
523         s->filtered_pkt.data += ret;
524     }
525 done:
526     return avpkt->size;
527 }
528
529 static void mediacodec_decode_flush(AVCodecContext *avctx)
530 {
531     MediaCodecH264DecContext *s = avctx->priv_data;
532
533     while (av_fifo_size(s->fifo)) {
534         AVPacket pkt;
535         av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
536         av_packet_unref(&pkt);
537     }
538     av_fifo_reset(s->fifo);
539
540     av_packet_unref(&s->filtered_pkt);
541
542     ff_mediacodec_dec_flush(avctx, s->ctx);
543 }
544
545 #if CONFIG_H264_MEDIACODEC_DECODER
546 AVCodec ff_h264_mediacodec_decoder = {
547     .name           = "h264_mediacodec",
548     .long_name      = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
549     .type           = AVMEDIA_TYPE_VIDEO,
550     .id             = AV_CODEC_ID_H264,
551     .priv_data_size = sizeof(MediaCodecH264DecContext),
552     .init           = mediacodec_decode_init,
553     .decode         = mediacodec_decode_frame,
554     .flush          = mediacodec_decode_flush,
555     .close          = mediacodec_decode_close,
556     .capabilities   = CODEC_CAP_DELAY,
557     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
558 };
559 #endif
560
561 #if CONFIG_HEVC_MEDIACODEC_DECODER
562 AVCodec ff_hevc_mediacodec_decoder = {
563     .name           = "hevc_mediacodec",
564     .long_name      = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
565     .type           = AVMEDIA_TYPE_VIDEO,
566     .id             = AV_CODEC_ID_HEVC,
567     .priv_data_size = sizeof(MediaCodecH264DecContext),
568     .init           = mediacodec_decode_init,
569     .decode         = mediacodec_decode_frame,
570     .flush          = mediacodec_decode_flush,
571     .close          = mediacodec_decode_close,
572     .capabilities   = CODEC_CAP_DELAY,
573     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
574 };
575 #endif
576
577 #if CONFIG_MPEG4_MEDIACODEC_DECODER
578 AVCodec ff_mpeg4_mediacodec_decoder = {
579     .name           = "mpeg4_mediacodec",
580     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
581     .type           = AVMEDIA_TYPE_VIDEO,
582     .id             = AV_CODEC_ID_MPEG4,
583     .priv_data_size = sizeof(MediaCodecH264DecContext),
584     .init           = mediacodec_decode_init,
585     .decode         = mediacodec_decode_frame,
586     .flush          = mediacodec_decode_flush,
587     .close          = mediacodec_decode_close,
588     .capabilities   = CODEC_CAP_DELAY,
589     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
590 };
591 #endif
592
593 #if CONFIG_VP8_MEDIACODEC_DECODER
594 AVCodec ff_vp8_mediacodec_decoder = {
595     .name           = "vp8_mediacodec",
596     .long_name      = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
597     .type           = AVMEDIA_TYPE_VIDEO,
598     .id             = AV_CODEC_ID_VP8,
599     .priv_data_size = sizeof(MediaCodecH264DecContext),
600     .init           = mediacodec_decode_init,
601     .decode         = mediacodec_decode_frame,
602     .flush          = mediacodec_decode_flush,
603     .close          = mediacodec_decode_close,
604     .capabilities   = CODEC_CAP_DELAY,
605     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
606 };
607 #endif
608
609 #if CONFIG_VP9_MEDIACODEC_DECODER
610 AVCodec ff_vp9_mediacodec_decoder = {
611     .name           = "vp9_mediacodec",
612     .long_name      = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
613     .type           = AVMEDIA_TYPE_VIDEO,
614     .id             = AV_CODEC_ID_VP9,
615     .priv_data_size = sizeof(MediaCodecH264DecContext),
616     .init           = mediacodec_decode_init,
617     .decode         = mediacodec_decode_frame,
618     .flush          = mediacodec_decode_flush,
619     .close          = mediacodec_decode_close,
620     .capabilities   = CODEC_CAP_DELAY,
621     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
622 };
623 #endif