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