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