OSDN Git Service

avcodec/vp9block: fix runtime error: signed integer overflow: 196675 * 20670 cannot...
[android-x86/external-ffmpeg.git] / 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
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_MPEG4_MEDIACODEC_DECODER
271 static int mpeg4_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_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
284 static int vpx_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 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
297 {
298     int ret;
299
300     const char *codec_mime = NULL;
301
302     const char *bsf_name = NULL;
303     const AVBitStreamFilter *bsf = NULL;
304
305     FFAMediaFormat *format = NULL;
306     MediaCodecH264DecContext *s = avctx->priv_data;
307
308     format = ff_AMediaFormat_new();
309     if (!format) {
310         av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
311         ret = AVERROR_EXTERNAL;
312         goto done;
313     }
314
315     switch (avctx->codec_id) {
316 #if CONFIG_H264_MEDIACODEC_DECODER
317     case AV_CODEC_ID_H264:
318         codec_mime = "video/avc";
319         bsf_name = "h264_mp4toannexb";
320
321         ret = h264_set_extradata(avctx, format);
322         if (ret < 0)
323             goto done;
324         break;
325 #endif
326 #if CONFIG_HEVC_MEDIACODEC_DECODER
327     case AV_CODEC_ID_HEVC:
328         codec_mime = "video/hevc";
329         bsf_name = "hevc_mp4toannexb";
330
331         ret = hevc_set_extradata(avctx, format);
332         if (ret < 0)
333             goto done;
334         break;
335 #endif
336 #if CONFIG_MPEG4_MEDIACODEC_DECODER
337     case AV_CODEC_ID_MPEG4:
338         codec_mime = "video/mp4v-es",
339
340         ret = mpeg4_set_extradata(avctx, format);
341         if (ret < 0)
342             goto done;
343         break;
344 #endif
345 #if CONFIG_VP8_MEDIACODEC_DECODER
346     case AV_CODEC_ID_VP8:
347         codec_mime = "video/x-vnd.on2.vp8";
348
349         ret = vpx_set_extradata(avctx, format);
350         if (ret < 0)
351             goto done;
352         break;
353 #endif
354 #if CONFIG_VP9_MEDIACODEC_DECODER
355     case AV_CODEC_ID_VP9:
356         codec_mime = "video/x-vnd.on2.vp9";
357
358         ret = vpx_set_extradata(avctx, format);
359         if (ret < 0)
360             goto done;
361         break;
362 #endif
363     default:
364         av_assert0(0);
365     }
366
367     ff_AMediaFormat_setString(format, "mime", codec_mime);
368     ff_AMediaFormat_setInt32(format, "width", avctx->width);
369     ff_AMediaFormat_setInt32(format, "height", avctx->height);
370
371     s->ctx = av_mallocz(sizeof(*s->ctx));
372     if (!s->ctx) {
373         av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
374         ret = AVERROR(ENOMEM);
375         goto done;
376     }
377
378     if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
379         s->ctx = NULL;
380         goto done;
381     }
382
383     av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
384
385     s->fifo = av_fifo_alloc(sizeof(AVPacket));
386     if (!s->fifo) {
387         ret = AVERROR(ENOMEM);
388         goto done;
389     }
390
391     if (bsf_name) {
392     bsf = av_bsf_get_by_name(bsf_name);
393     if(!bsf) {
394         ret = AVERROR_BSF_NOT_FOUND;
395         goto done;
396     }
397
398     if ((ret = av_bsf_alloc(bsf, &s->bsf))) {
399         goto done;
400     }
401
402     if (((ret = avcodec_parameters_from_context(s->bsf->par_in, avctx)) < 0) ||
403         ((ret = av_bsf_init(s->bsf)) < 0)) {
404           goto done;
405     }
406     }
407
408     av_init_packet(&s->filtered_pkt);
409
410 done:
411     if (format) {
412         ff_AMediaFormat_delete(format);
413     }
414
415     if (ret < 0) {
416         mediacodec_decode_close(avctx);
417     }
418
419     return ret;
420 }
421
422
423 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
424                                    int *got_frame, AVPacket *pkt)
425 {
426     MediaCodecH264DecContext *s = avctx->priv_data;
427
428     return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
429 }
430
431 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
432                                    int *got_frame, AVPacket *avpkt)
433 {
434     MediaCodecH264DecContext *s = avctx->priv_data;
435     AVFrame *frame    = data;
436     int ret;
437
438     /* buffer the input packet */
439     if (avpkt->size) {
440         AVPacket input_pkt = { 0 };
441
442         if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
443             ret = av_fifo_realloc2(s->fifo,
444                                    av_fifo_size(s->fifo) + sizeof(input_pkt));
445             if (ret < 0)
446                 return ret;
447         }
448
449         ret = av_packet_ref(&input_pkt, avpkt);
450         if (ret < 0)
451             return ret;
452         av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
453     }
454
455     /*
456      * MediaCodec.flush() discards both input and output buffers, thus we
457      * need to delay the call to this function until the user has released or
458      * renderered the frames he retains.
459      *
460      * After we have buffered an input packet, check if the codec is in the
461      * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
462      *
463      * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
464      * the codec (because the user retains frames). The codec stays in the
465      * flushing state.
466      *
467      * ff_mediacodec_dec_flush returns 1 if the flush can actually be
468      * performed on the codec. The codec leaves the flushing state and can
469      * process again packets.
470      *
471      * ff_mediacodec_dec_flush returns a negative value if an error has
472      * occurred.
473      *
474      */
475     if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
476         if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
477             return avpkt->size;
478         }
479     }
480
481     /* process buffered data */
482     while (!*got_frame) {
483         /* prepare the input data -- convert to Annex B if needed */
484         if (s->filtered_pkt.size <= 0) {
485             AVPacket input_pkt = { 0 };
486
487             av_packet_unref(&s->filtered_pkt);
488
489             /* no more data */
490             if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
491                 return avpkt->size ? avpkt->size :
492                     ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
493             }
494
495             av_fifo_generic_read(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
496
497             if (s->bsf) {
498             ret = av_bsf_send_packet(s->bsf, &input_pkt);
499             if (ret < 0) {
500                 return ret;
501             }
502
503             ret = av_bsf_receive_packet(s->bsf, &s->filtered_pkt);
504             if (ret == AVERROR(EAGAIN)) {
505                 goto done;
506             }
507             } else {
508                 av_packet_move_ref(&s->filtered_pkt, &input_pkt);
509             }
510
511             /* {h264,hevc}_mp4toannexb are used here and do not require flushing */
512             av_assert0(ret != AVERROR_EOF);
513
514             if (ret < 0) {
515                 return ret;
516             }
517         }
518
519         ret = mediacodec_process_data(avctx, frame, got_frame, &s->filtered_pkt);
520         if (ret < 0)
521             return ret;
522
523         s->filtered_pkt.size -= ret;
524         s->filtered_pkt.data += ret;
525     }
526 done:
527     return avpkt->size;
528 }
529
530 static void mediacodec_decode_flush(AVCodecContext *avctx)
531 {
532     MediaCodecH264DecContext *s = avctx->priv_data;
533
534     while (av_fifo_size(s->fifo)) {
535         AVPacket pkt;
536         av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
537         av_packet_unref(&pkt);
538     }
539     av_fifo_reset(s->fifo);
540
541     av_packet_unref(&s->filtered_pkt);
542
543     ff_mediacodec_dec_flush(avctx, s->ctx);
544 }
545
546 #if CONFIG_H264_MEDIACODEC_DECODER
547 AVCodec ff_h264_mediacodec_decoder = {
548     .name           = "h264_mediacodec",
549     .long_name      = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
550     .type           = AVMEDIA_TYPE_VIDEO,
551     .id             = AV_CODEC_ID_H264,
552     .priv_data_size = sizeof(MediaCodecH264DecContext),
553     .init           = mediacodec_decode_init,
554     .decode         = mediacodec_decode_frame,
555     .flush          = mediacodec_decode_flush,
556     .close          = mediacodec_decode_close,
557     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
558     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
559 };
560 #endif
561
562 #if CONFIG_HEVC_MEDIACODEC_DECODER
563 AVCodec ff_hevc_mediacodec_decoder = {
564     .name           = "hevc_mediacodec",
565     .long_name      = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
566     .type           = AVMEDIA_TYPE_VIDEO,
567     .id             = AV_CODEC_ID_HEVC,
568     .priv_data_size = sizeof(MediaCodecH264DecContext),
569     .init           = mediacodec_decode_init,
570     .decode         = mediacodec_decode_frame,
571     .flush          = mediacodec_decode_flush,
572     .close          = mediacodec_decode_close,
573     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
574     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
575 };
576 #endif
577
578 #if CONFIG_MPEG4_MEDIACODEC_DECODER
579 AVCodec ff_mpeg4_mediacodec_decoder = {
580     .name           = "mpeg4_mediacodec",
581     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
582     .type           = AVMEDIA_TYPE_VIDEO,
583     .id             = AV_CODEC_ID_MPEG4,
584     .priv_data_size = sizeof(MediaCodecH264DecContext),
585     .init           = mediacodec_decode_init,
586     .decode         = mediacodec_decode_frame,
587     .flush          = mediacodec_decode_flush,
588     .close          = mediacodec_decode_close,
589     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
590     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
591 };
592 #endif
593
594 #if CONFIG_VP8_MEDIACODEC_DECODER
595 AVCodec ff_vp8_mediacodec_decoder = {
596     .name           = "vp8_mediacodec",
597     .long_name      = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
598     .type           = AVMEDIA_TYPE_VIDEO,
599     .id             = AV_CODEC_ID_VP8,
600     .priv_data_size = sizeof(MediaCodecH264DecContext),
601     .init           = mediacodec_decode_init,
602     .decode         = mediacodec_decode_frame,
603     .flush          = mediacodec_decode_flush,
604     .close          = mediacodec_decode_close,
605     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
606     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
607 };
608 #endif
609
610 #if CONFIG_VP9_MEDIACODEC_DECODER
611 AVCodec ff_vp9_mediacodec_decoder = {
612     .name           = "vp9_mediacodec",
613     .long_name      = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
614     .type           = AVMEDIA_TYPE_VIDEO,
615     .id             = AV_CODEC_ID_VP9,
616     .priv_data_size = sizeof(MediaCodecH264DecContext),
617     .init           = mediacodec_decode_init,
618     .decode         = mediacodec_decode_frame,
619     .flush          = mediacodec_decode_flush,
620     .close          = mediacodec_decode_close,
621     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
622     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
623 };
624 #endif