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