]> git.sesse.net Git - ffmpeg/blob - libavcodec/mediacodecdec.c
Merge commit '0c0b87f12d48d4e7f0d3d13f9345e828a3a5ea32'
[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     AVFifoBuffer *fifo;
45
46     AVPacket buffered_pkt;
47
48 } MediaCodecH264DecContext;
49
50 static av_cold int mediacodec_decode_close(AVCodecContext *avctx)
51 {
52     MediaCodecH264DecContext *s = avctx->priv_data;
53
54     ff_mediacodec_dec_close(avctx, s->ctx);
55     s->ctx = NULL;
56
57     av_fifo_free(s->fifo);
58
59     av_packet_unref(&s->buffered_pkt);
60
61     return 0;
62 }
63
64 #if CONFIG_H264_MEDIACODEC_DECODER || CONFIG_HEVC_MEDIACODEC_DECODER
65 static int h2645_ps_to_nalu(const uint8_t *src, int src_size, uint8_t **out, int *out_size)
66 {
67     int i;
68     int ret = 0;
69     uint8_t *p = NULL;
70     static const uint8_t nalu_header[] = { 0x00, 0x00, 0x00, 0x01 };
71
72     if (!out || !out_size) {
73         return AVERROR(EINVAL);
74     }
75
76     p = av_malloc(sizeof(nalu_header) + src_size);
77     if (!p) {
78         return AVERROR(ENOMEM);
79     }
80
81     *out = p;
82     *out_size = sizeof(nalu_header) + src_size;
83
84     memcpy(p, nalu_header, sizeof(nalu_header));
85     memcpy(p + sizeof(nalu_header), src, src_size);
86
87     /* Escape 0x00, 0x00, 0x0{0-3} pattern */
88     for (i = 4; i < *out_size; i++) {
89         if (i < *out_size - 3 &&
90             p[i + 0] == 0 &&
91             p[i + 1] == 0 &&
92             p[i + 2] <= 3) {
93             uint8_t *new;
94
95             *out_size += 1;
96             new = av_realloc(*out, *out_size);
97             if (!new) {
98                 ret = AVERROR(ENOMEM);
99                 goto done;
100             }
101             *out = p = new;
102
103             i = i + 2;
104             memmove(p + i + 1, p + i, *out_size - (i + 1));
105             p[i] = 0x03;
106         }
107     }
108 done:
109     if (ret < 0) {
110         av_freep(out);
111         *out_size = 0;
112     }
113
114     return ret;
115 }
116 #endif
117
118 #if CONFIG_H264_MEDIACODEC_DECODER
119 static int h264_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
120 {
121     int i;
122     int ret;
123
124     H264ParamSets ps;
125     const PPS *pps = NULL;
126     const SPS *sps = NULL;
127     int is_avc = 0;
128     int nal_length_size = 0;
129
130     memset(&ps, 0, sizeof(ps));
131
132     ret = ff_h264_decode_extradata(avctx->extradata, avctx->extradata_size,
133                                    &ps, &is_avc, &nal_length_size, 0, avctx);
134     if (ret < 0) {
135         goto done;
136     }
137
138     for (i = 0; i < MAX_PPS_COUNT; i++) {
139         if (ps.pps_list[i]) {
140             pps = (const PPS*)ps.pps_list[i]->data;
141             break;
142         }
143     }
144
145     if (pps) {
146         if (ps.sps_list[pps->sps_id]) {
147             sps = (const SPS*)ps.sps_list[pps->sps_id]->data;
148         }
149     }
150
151     if (pps && sps) {
152         uint8_t *data = NULL;
153         int data_size = 0;
154
155         if ((ret = h2645_ps_to_nalu(sps->data, sps->data_size, &data, &data_size)) < 0) {
156             goto done;
157         }
158         ff_AMediaFormat_setBuffer(format, "csd-0", (void*)data, data_size);
159         av_freep(&data);
160
161         if ((ret = h2645_ps_to_nalu(pps->data, pps->data_size, &data, &data_size)) < 0) {
162             goto done;
163         }
164         ff_AMediaFormat_setBuffer(format, "csd-1", (void*)data, data_size);
165         av_freep(&data);
166     } else {
167         av_log(avctx, AV_LOG_ERROR, "Could not extract PPS/SPS from extradata");
168         ret = AVERROR_INVALIDDATA;
169     }
170
171 done:
172     ff_h264_ps_uninit(&ps);
173
174     return ret;
175 }
176 #endif
177
178 #if CONFIG_HEVC_MEDIACODEC_DECODER
179 static int hevc_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
180 {
181     int i;
182     int ret;
183
184     HEVCParamSets ps;
185     HEVCSEIContext sei;
186
187     const HEVCVPS *vps = NULL;
188     const HEVCPPS *pps = NULL;
189     const HEVCSPS *sps = NULL;
190     int is_nalff = 0;
191     int nal_length_size = 0;
192
193     uint8_t *vps_data = NULL;
194     uint8_t *sps_data = NULL;
195     uint8_t *pps_data = NULL;
196     int vps_data_size = 0;
197     int sps_data_size = 0;
198     int pps_data_size = 0;
199
200     memset(&ps, 0, sizeof(ps));
201     memset(&sei, 0, sizeof(sei));
202
203     ret = ff_hevc_decode_extradata(avctx->extradata, avctx->extradata_size,
204                                    &ps, &sei, &is_nalff, &nal_length_size, 0, 1, avctx);
205     if (ret < 0) {
206         goto done;
207     }
208
209     for (i = 0; i < HEVC_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 < HEVC_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 #if CONFIG_MPEG2_MEDIACODEC_DECODER
268 static int mpeg2_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
269 {
270     int ret = 0;
271
272     if (avctx->extradata) {
273         ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
274     }
275
276     return ret;
277 }
278 #endif
279
280 #if CONFIG_MPEG4_MEDIACODEC_DECODER
281 static int mpeg4_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
282 {
283     int ret = 0;
284
285     if (avctx->extradata) {
286         ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
287     }
288
289     return ret;
290 }
291 #endif
292
293 #if CONFIG_VP8_MEDIACODEC_DECODER || CONFIG_VP9_MEDIACODEC_DECODER
294 static int vpx_set_extradata(AVCodecContext *avctx, FFAMediaFormat *format)
295 {
296     int ret = 0;
297
298     if (avctx->extradata) {
299         ff_AMediaFormat_setBuffer(format, "csd-0", avctx->extradata, avctx->extradata_size);
300     }
301
302     return ret;
303 }
304 #endif
305
306 static av_cold int mediacodec_decode_init(AVCodecContext *avctx)
307 {
308     int ret;
309
310     const char *codec_mime = NULL;
311
312     FFAMediaFormat *format = NULL;
313     MediaCodecH264DecContext *s = avctx->priv_data;
314
315     format = ff_AMediaFormat_new();
316     if (!format) {
317         av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
318         ret = AVERROR_EXTERNAL;
319         goto done;
320     }
321
322     switch (avctx->codec_id) {
323 #if CONFIG_H264_MEDIACODEC_DECODER
324     case AV_CODEC_ID_H264:
325         codec_mime = "video/avc";
326
327         ret = h264_set_extradata(avctx, format);
328         if (ret < 0)
329             goto done;
330         break;
331 #endif
332 #if CONFIG_HEVC_MEDIACODEC_DECODER
333     case AV_CODEC_ID_HEVC:
334         codec_mime = "video/hevc";
335
336         ret = hevc_set_extradata(avctx, format);
337         if (ret < 0)
338             goto done;
339         break;
340 #endif
341 #if CONFIG_MPEG2_MEDIACODEC_DECODER
342     case AV_CODEC_ID_MPEG2VIDEO:
343         codec_mime = "video/mpeg2";
344
345         ret = mpeg2_set_extradata(avctx, format);
346         if (ret < 0)
347             goto done;
348         break;
349 #endif
350 #if CONFIG_MPEG4_MEDIACODEC_DECODER
351     case AV_CODEC_ID_MPEG4:
352         codec_mime = "video/mp4v-es",
353
354         ret = mpeg4_set_extradata(avctx, format);
355         if (ret < 0)
356             goto done;
357         break;
358 #endif
359 #if CONFIG_VP8_MEDIACODEC_DECODER
360     case AV_CODEC_ID_VP8:
361         codec_mime = "video/x-vnd.on2.vp8";
362
363         ret = vpx_set_extradata(avctx, format);
364         if (ret < 0)
365             goto done;
366         break;
367 #endif
368 #if CONFIG_VP9_MEDIACODEC_DECODER
369     case AV_CODEC_ID_VP9:
370         codec_mime = "video/x-vnd.on2.vp9";
371
372         ret = vpx_set_extradata(avctx, format);
373         if (ret < 0)
374             goto done;
375         break;
376 #endif
377     default:
378         av_assert0(0);
379     }
380
381     ff_AMediaFormat_setString(format, "mime", codec_mime);
382     ff_AMediaFormat_setInt32(format, "width", avctx->width);
383     ff_AMediaFormat_setInt32(format, "height", avctx->height);
384
385     s->ctx = av_mallocz(sizeof(*s->ctx));
386     if (!s->ctx) {
387         av_log(avctx, AV_LOG_ERROR, "Failed to allocate MediaCodecDecContext\n");
388         ret = AVERROR(ENOMEM);
389         goto done;
390     }
391
392     if ((ret = ff_mediacodec_dec_init(avctx, s->ctx, codec_mime, format)) < 0) {
393         s->ctx = NULL;
394         goto done;
395     }
396
397     av_log(avctx, AV_LOG_INFO, "MediaCodec started successfully, ret = %d\n", ret);
398
399     s->fifo = av_fifo_alloc(sizeof(AVPacket));
400     if (!s->fifo) {
401         ret = AVERROR(ENOMEM);
402         goto done;
403     }
404
405 done:
406     if (format) {
407         ff_AMediaFormat_delete(format);
408     }
409
410     if (ret < 0) {
411         mediacodec_decode_close(avctx);
412     }
413
414     return ret;
415 }
416
417
418 static int mediacodec_process_data(AVCodecContext *avctx, AVFrame *frame,
419                                    int *got_frame, AVPacket *pkt)
420 {
421     MediaCodecH264DecContext *s = avctx->priv_data;
422
423     return ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, pkt);
424 }
425
426 static int mediacodec_decode_frame(AVCodecContext *avctx, void *data,
427                                    int *got_frame, AVPacket *avpkt)
428 {
429     MediaCodecH264DecContext *s = avctx->priv_data;
430     AVFrame *frame    = data;
431     int ret;
432
433     /* buffer the input packet */
434     if (avpkt->size) {
435         AVPacket input_pkt = { 0 };
436
437         if (av_fifo_space(s->fifo) < sizeof(input_pkt)) {
438             ret = av_fifo_realloc2(s->fifo,
439                                    av_fifo_size(s->fifo) + sizeof(input_pkt));
440             if (ret < 0)
441                 return ret;
442         }
443
444         ret = av_packet_ref(&input_pkt, avpkt);
445         if (ret < 0)
446             return ret;
447         av_fifo_generic_write(s->fifo, &input_pkt, sizeof(input_pkt), NULL);
448     }
449
450     /*
451      * MediaCodec.flush() discards both input and output buffers, thus we
452      * need to delay the call to this function until the user has released or
453      * renderered the frames he retains.
454      *
455      * After we have buffered an input packet, check if the codec is in the
456      * flushing state. If it is, we need to call ff_mediacodec_dec_flush.
457      *
458      * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
459      * the codec (because the user retains frames). The codec stays in the
460      * flushing state.
461      *
462      * ff_mediacodec_dec_flush returns 1 if the flush can actually be
463      * performed on the codec. The codec leaves the flushing state and can
464      * process again packets.
465      *
466      * ff_mediacodec_dec_flush returns a negative value if an error has
467      * occurred.
468      *
469      */
470     if (ff_mediacodec_dec_is_flushing(avctx, s->ctx)) {
471         if (!ff_mediacodec_dec_flush(avctx, s->ctx)) {
472             return avpkt->size;
473         }
474     }
475
476     /* process buffered data */
477     while (!*got_frame) {
478         /* prepare the input data */
479         if (s->buffered_pkt.size <= 0) {
480             av_packet_unref(&s->buffered_pkt);
481
482             /* no more data */
483             if (av_fifo_size(s->fifo) < sizeof(AVPacket)) {
484                 return avpkt->size ? avpkt->size :
485                     ff_mediacodec_dec_decode(avctx, s->ctx, frame, got_frame, avpkt);
486             }
487
488             av_fifo_generic_read(s->fifo, &s->buffered_pkt, sizeof(s->buffered_pkt), NULL);
489         }
490
491         ret = mediacodec_process_data(avctx, frame, got_frame, &s->buffered_pkt);
492         if (ret < 0)
493             return ret;
494
495         s->buffered_pkt.size -= ret;
496         s->buffered_pkt.data += ret;
497     }
498
499     return avpkt->size;
500 }
501
502 static void mediacodec_decode_flush(AVCodecContext *avctx)
503 {
504     MediaCodecH264DecContext *s = avctx->priv_data;
505
506     while (av_fifo_size(s->fifo)) {
507         AVPacket pkt;
508         av_fifo_generic_read(s->fifo, &pkt, sizeof(pkt), NULL);
509         av_packet_unref(&pkt);
510     }
511     av_fifo_reset(s->fifo);
512
513     av_packet_unref(&s->buffered_pkt);
514
515     ff_mediacodec_dec_flush(avctx, s->ctx);
516 }
517
518 #if CONFIG_H264_MEDIACODEC_DECODER
519 AVCodec ff_h264_mediacodec_decoder = {
520     .name           = "h264_mediacodec",
521     .long_name      = NULL_IF_CONFIG_SMALL("H.264 Android MediaCodec decoder"),
522     .type           = AVMEDIA_TYPE_VIDEO,
523     .id             = AV_CODEC_ID_H264,
524     .priv_data_size = sizeof(MediaCodecH264DecContext),
525     .init           = mediacodec_decode_init,
526     .decode         = mediacodec_decode_frame,
527     .flush          = mediacodec_decode_flush,
528     .close          = mediacodec_decode_close,
529     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
530     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
531     .bsfs           = "h264_mp4toannexb",
532 };
533 #endif
534
535 #if CONFIG_HEVC_MEDIACODEC_DECODER
536 AVCodec ff_hevc_mediacodec_decoder = {
537     .name           = "hevc_mediacodec",
538     .long_name      = NULL_IF_CONFIG_SMALL("H.265 Android MediaCodec decoder"),
539     .type           = AVMEDIA_TYPE_VIDEO,
540     .id             = AV_CODEC_ID_HEVC,
541     .priv_data_size = sizeof(MediaCodecH264DecContext),
542     .init           = mediacodec_decode_init,
543     .decode         = mediacodec_decode_frame,
544     .flush          = mediacodec_decode_flush,
545     .close          = mediacodec_decode_close,
546     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
547     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
548     .bsfs           = "hevc_mp4toannexb",
549 };
550 #endif
551
552 #if CONFIG_MPEG2_MEDIACODEC_DECODER
553 AVCodec ff_mpeg2_mediacodec_decoder = {
554     .name           = "mpeg2_mediacodec",
555     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-2 Android MediaCodec decoder"),
556     .type           = AVMEDIA_TYPE_VIDEO,
557     .id             = AV_CODEC_ID_MPEG2VIDEO,
558     .priv_data_size = sizeof(MediaCodecH264DecContext),
559     .init           = mediacodec_decode_init,
560     .decode         = mediacodec_decode_frame,
561     .flush          = mediacodec_decode_flush,
562     .close          = mediacodec_decode_close,
563     .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING,
564     .caps_internal  = FF_CODEC_CAP_SETS_PKT_DTS,
565 };
566 #endif
567
568 #if CONFIG_MPEG4_MEDIACODEC_DECODER
569 AVCodec ff_mpeg4_mediacodec_decoder = {
570     .name           = "mpeg4_mediacodec",
571     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-4 Android MediaCodec decoder"),
572     .type           = AVMEDIA_TYPE_VIDEO,
573     .id             = AV_CODEC_ID_MPEG4,
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_VP8_MEDIACODEC_DECODER
585 AVCodec ff_vp8_mediacodec_decoder = {
586     .name           = "vp8_mediacodec",
587     .long_name      = NULL_IF_CONFIG_SMALL("VP8 Android MediaCodec decoder"),
588     .type           = AVMEDIA_TYPE_VIDEO,
589     .id             = AV_CODEC_ID_VP8,
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_VP9_MEDIACODEC_DECODER
601 AVCodec ff_vp9_mediacodec_decoder = {
602     .name           = "vp9_mediacodec",
603     .long_name      = NULL_IF_CONFIG_SMALL("VP9 Android MediaCodec decoder"),
604     .type           = AVMEDIA_TYPE_VIDEO,
605     .id             = AV_CODEC_ID_VP9,
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