]> git.sesse.net Git - ffmpeg/blob - libavcodec/mediacodecdec_common.c
404ed282275e664d56089839dd3bc410353423d0
[ffmpeg] / libavcodec / mediacodecdec_common.c
1 /*
2  * Android MediaCodec decoder
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 <string.h>
24 #include <sys/types.h>
25
26 #include "libavutil/common.h"
27 #include "libavutil/hwcontext_mediacodec.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/log.h"
30 #include "libavutil/pixfmt.h"
31 #include "libavutil/time.h"
32 #include "libavutil/timestamp.h"
33
34 #include "avcodec.h"
35 #include "internal.h"
36
37 #include "mediacodec.h"
38 #include "mediacodec_surface.h"
39 #include "mediacodec_sw_buffer.h"
40 #include "mediacodec_wrapper.h"
41 #include "mediacodecdec_common.h"
42
43 /**
44  * OMX.k3.video.decoder.avc, OMX.NVIDIA.* OMX.SEC.avc.dec and OMX.google
45  * codec workarounds used in various place are taken from the Gstreamer
46  * project.
47  *
48  * Gstreamer references:
49  * https://cgit.freedesktop.org/gstreamer/gst-plugins-bad/tree/sys/androidmedia/
50  *
51  * Gstreamer copyright notice:
52  *
53  * Copyright (C) 2012, Collabora Ltd.
54  *   Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
55  *
56  * Copyright (C) 2012, Rafaël Carré <funman@videolanorg>
57  *
58  * Copyright (C) 2015, Sebastian Dröge <sebastian@centricular.com>
59  *
60  * Copyright (C) 2014-2015, Collabora Ltd.
61  *   Author: Matthieu Bouron <matthieu.bouron@gcollabora.com>
62  *
63  * Copyright (C) 2015, Edward Hervey
64  *   Author: Edward Hervey <bilboed@gmail.com>
65  *
66  * Copyright (C) 2015, Matthew Waters <matthew@centricular.com>
67  *
68  * This library is free software; you can redistribute it and/or
69  * modify it under the terms of the GNU Lesser General Public
70  * License as published by the Free Software Foundation
71  * version 2.1 of the License.
72  *
73  * This library is distributed in the hope that it will be useful,
74  * but WITHOUT ANY WARRANTY; without even the implied warranty of
75  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
76  * Lesser General Public License for more details.
77  *
78  * You should have received a copy of the GNU Lesser General Public
79  * License along with this library; if not, write to the Free Software
80  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
81  *
82  */
83
84 #define INPUT_DEQUEUE_TIMEOUT_US 8000
85 #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
86 #define OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US 1000000
87
88 enum {
89     COLOR_RANGE_FULL    = 0x1,
90     COLOR_RANGE_LIMITED = 0x2,
91 };
92
93 static enum AVColorRange mcdec_get_color_range(int color_range)
94 {
95     switch (color_range) {
96     case COLOR_RANGE_FULL:
97         return AVCOL_RANGE_JPEG;
98     case COLOR_RANGE_LIMITED:
99         return AVCOL_RANGE_MPEG;
100     default:
101         return AVCOL_RANGE_UNSPECIFIED;
102     }
103 }
104
105 enum {
106     COLOR_STANDARD_BT709      = 0x1,
107     COLOR_STANDARD_BT601_PAL  = 0x2,
108     COLOR_STANDARD_BT601_NTSC = 0x4,
109     COLOR_STANDARD_BT2020     = 0x6,
110 };
111
112 static enum AVColorSpace mcdec_get_color_space(int color_standard)
113 {
114     switch (color_standard) {
115     case COLOR_STANDARD_BT709:
116         return AVCOL_SPC_BT709;
117     case COLOR_STANDARD_BT601_PAL:
118         return AVCOL_SPC_BT470BG;
119     case COLOR_STANDARD_BT601_NTSC:
120         return AVCOL_SPC_SMPTE170M;
121     case COLOR_STANDARD_BT2020:
122         return AVCOL_SPC_BT2020_NCL;
123     default:
124         return AVCOL_SPC_UNSPECIFIED;
125     }
126 }
127
128 static enum AVColorPrimaries mcdec_get_color_pri(int color_standard)
129 {
130     switch (color_standard) {
131     case COLOR_STANDARD_BT709:
132         return AVCOL_PRI_BT709;
133     case COLOR_STANDARD_BT601_PAL:
134         return AVCOL_PRI_BT470BG;
135     case COLOR_STANDARD_BT601_NTSC:
136         return AVCOL_PRI_SMPTE170M;
137     case COLOR_STANDARD_BT2020:
138         return AVCOL_PRI_BT2020;
139     default:
140         return AVCOL_PRI_UNSPECIFIED;
141     }
142 }
143
144 enum {
145     COLOR_TRANSFER_LINEAR    = 0x1,
146     COLOR_TRANSFER_SDR_VIDEO = 0x3,
147     COLOR_TRANSFER_ST2084    = 0x6,
148     COLOR_TRANSFER_HLG       = 0x7,
149 };
150
151 static enum AVColorTransferCharacteristic mcdec_get_color_trc(int color_transfer)
152 {
153     switch (color_transfer) {
154     case COLOR_TRANSFER_LINEAR:
155         return AVCOL_TRC_LINEAR;
156     case COLOR_TRANSFER_SDR_VIDEO:
157         return AVCOL_TRC_SMPTE170M;
158     case COLOR_TRANSFER_ST2084:
159         return AVCOL_TRC_SMPTEST2084;
160     case COLOR_TRANSFER_HLG:
161         return AVCOL_TRC_ARIB_STD_B67;
162     default:
163         return AVCOL_TRC_UNSPECIFIED;
164     }
165 }
166
167 enum {
168     COLOR_FormatYUV420Planar                              = 0x13,
169     COLOR_FormatYUV420SemiPlanar                          = 0x15,
170     COLOR_FormatYCbYCr                                    = 0x19,
171     COLOR_FormatAndroidOpaque                             = 0x7F000789,
172     COLOR_QCOM_FormatYUV420SemiPlanar                     = 0x7fa30c00,
173     COLOR_QCOM_FormatYUV420SemiPlanar32m                  = 0x7fa30c04,
174     COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka = 0x7fa30c03,
175     COLOR_TI_FormatYUV420PackedSemiPlanar                 = 0x7f000100,
176     COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced       = 0x7f000001,
177 };
178
179 static const struct {
180
181     int color_format;
182     enum AVPixelFormat pix_fmt;
183
184 } color_formats[] = {
185
186     { COLOR_FormatYUV420Planar,                              AV_PIX_FMT_YUV420P },
187     { COLOR_FormatYUV420SemiPlanar,                          AV_PIX_FMT_NV12    },
188     { COLOR_QCOM_FormatYUV420SemiPlanar,                     AV_PIX_FMT_NV12    },
189     { COLOR_QCOM_FormatYUV420SemiPlanar32m,                  AV_PIX_FMT_NV12    },
190     { COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka, AV_PIX_FMT_NV12    },
191     { COLOR_TI_FormatYUV420PackedSemiPlanar,                 AV_PIX_FMT_NV12    },
192     { COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced,       AV_PIX_FMT_NV12    },
193     { 0 }
194 };
195
196 static enum AVPixelFormat mcdec_map_color_format(AVCodecContext *avctx,
197                                                  MediaCodecDecContext *s,
198                                                  int color_format)
199 {
200     int i;
201     enum AVPixelFormat ret = AV_PIX_FMT_NONE;
202
203     if (s->surface) {
204         return AV_PIX_FMT_MEDIACODEC;
205     }
206
207     if (!strcmp(s->codec_name, "OMX.k3.video.decoder.avc") && color_format == COLOR_FormatYCbYCr) {
208         s->color_format = color_format = COLOR_TI_FormatYUV420PackedSemiPlanar;
209     }
210
211     for (i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) {
212         if (color_formats[i].color_format == color_format) {
213             return color_formats[i].pix_fmt;
214         }
215     }
216
217     av_log(avctx, AV_LOG_ERROR, "Output color format 0x%x (value=%d) is not supported\n",
218         color_format, color_format);
219
220     return ret;
221 }
222
223 static void ff_mediacodec_dec_ref(MediaCodecDecContext *s)
224 {
225     atomic_fetch_add(&s->refcount, 1);
226 }
227
228 static void ff_mediacodec_dec_unref(MediaCodecDecContext *s)
229 {
230     if (!s)
231         return;
232
233     if (atomic_fetch_sub(&s->refcount, 1) == 1) {
234         if (s->codec) {
235             ff_AMediaCodec_delete(s->codec);
236             s->codec = NULL;
237         }
238
239         if (s->format) {
240             ff_AMediaFormat_delete(s->format);
241             s->format = NULL;
242         }
243
244         if (s->surface) {
245             ff_mediacodec_surface_unref(s->surface, NULL);
246             s->surface = NULL;
247         }
248
249         av_freep(&s->codec_name);
250         av_freep(&s);
251     }
252 }
253
254 static void mediacodec_buffer_release(void *opaque, uint8_t *data)
255 {
256     AVMediaCodecBuffer *buffer = opaque;
257     MediaCodecDecContext *ctx = buffer->ctx;
258     int released = atomic_load(&buffer->released);
259
260     if (!released && (ctx->delay_flush || buffer->serial == atomic_load(&ctx->serial))) {
261         atomic_fetch_sub(&ctx->hw_buffer_count, 1);
262         av_log(ctx->avctx, AV_LOG_DEBUG,
263                "Releasing output buffer %zd (%p) ts=%"PRId64" on free() [%d pending]\n",
264                buffer->index, buffer, buffer->pts, atomic_load(&ctx->hw_buffer_count));
265         ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, 0);
266     }
267
268     if (ctx->delay_flush)
269         ff_mediacodec_dec_unref(ctx);
270     av_freep(&buffer);
271 }
272
273 static int mediacodec_wrap_hw_buffer(AVCodecContext *avctx,
274                                   MediaCodecDecContext *s,
275                                   ssize_t index,
276                                   FFAMediaCodecBufferInfo *info,
277                                   AVFrame *frame)
278 {
279     int ret = 0;
280     int status = 0;
281     AVMediaCodecBuffer *buffer = NULL;
282
283     frame->buf[0] = NULL;
284     frame->width = avctx->width;
285     frame->height = avctx->height;
286     frame->format = avctx->pix_fmt;
287     frame->sample_aspect_ratio = avctx->sample_aspect_ratio;
288
289     if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
290         frame->pts = av_rescale_q(info->presentationTimeUs,
291                                       AV_TIME_BASE_Q,
292                                       avctx->pkt_timebase);
293     } else {
294         frame->pts = info->presentationTimeUs;
295     }
296 #if FF_API_PKT_PTS
297 FF_DISABLE_DEPRECATION_WARNINGS
298     frame->pkt_pts = frame->pts;
299 FF_ENABLE_DEPRECATION_WARNINGS
300 #endif
301     frame->pkt_dts = AV_NOPTS_VALUE;
302     frame->color_range = avctx->color_range;
303     frame->color_primaries = avctx->color_primaries;
304     frame->color_trc = avctx->color_trc;
305     frame->colorspace = avctx->colorspace;
306
307     buffer = av_mallocz(sizeof(AVMediaCodecBuffer));
308     if (!buffer) {
309         ret = AVERROR(ENOMEM);
310         goto fail;
311     }
312
313     atomic_init(&buffer->released, 0);
314
315     frame->buf[0] = av_buffer_create(NULL,
316                                      0,
317                                      mediacodec_buffer_release,
318                                      buffer,
319                                      AV_BUFFER_FLAG_READONLY);
320
321     if (!frame->buf[0]) {
322         ret = AVERROR(ENOMEM);
323         goto fail;
324
325     }
326
327     buffer->ctx = s;
328     buffer->serial = atomic_load(&s->serial);
329     if (s->delay_flush)
330         ff_mediacodec_dec_ref(s);
331
332     buffer->index = index;
333     buffer->pts = info->presentationTimeUs;
334
335     frame->data[3] = (uint8_t *)buffer;
336
337     atomic_fetch_add(&s->hw_buffer_count, 1);
338     av_log(avctx, AV_LOG_DEBUG,
339             "Wrapping output buffer %zd (%p) ts=%"PRId64" [%d pending]\n",
340             buffer->index, buffer, buffer->pts, atomic_load(&s->hw_buffer_count));
341
342     return 0;
343 fail:
344     av_freep(buffer);
345     av_buffer_unref(&frame->buf[0]);
346     status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
347     if (status < 0) {
348         av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
349         ret = AVERROR_EXTERNAL;
350     }
351
352     return ret;
353 }
354
355 static int mediacodec_wrap_sw_buffer(AVCodecContext *avctx,
356                                   MediaCodecDecContext *s,
357                                   uint8_t *data,
358                                   size_t size,
359                                   ssize_t index,
360                                   FFAMediaCodecBufferInfo *info,
361                                   AVFrame *frame)
362 {
363     int ret = 0;
364     int status = 0;
365
366     frame->width = avctx->width;
367     frame->height = avctx->height;
368     frame->format = avctx->pix_fmt;
369
370     /* MediaCodec buffers needs to be copied to our own refcounted buffers
371      * because the flush command invalidates all input and output buffers.
372      */
373     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
374         av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer\n");
375         goto done;
376     }
377
378     /* Override frame->pkt_pts as ff_get_buffer will override its value based
379      * on the last avpacket received which is not in sync with the frame:
380      *   * N avpackets can be pushed before 1 frame is actually returned
381      *   * 0-sized avpackets are pushed to flush remaining frames at EOS */
382     if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
383         frame->pts = av_rescale_q(info->presentationTimeUs,
384                                       AV_TIME_BASE_Q,
385                                       avctx->pkt_timebase);
386     } else {
387         frame->pts = info->presentationTimeUs;
388     }
389 #if FF_API_PKT_PTS
390 FF_DISABLE_DEPRECATION_WARNINGS
391     frame->pkt_pts = frame->pts;
392 FF_ENABLE_DEPRECATION_WARNINGS
393 #endif
394     frame->pkt_dts = AV_NOPTS_VALUE;
395
396     av_log(avctx, AV_LOG_TRACE,
397             "Frame: width=%d stride=%d height=%d slice-height=%d "
398             "crop-top=%d crop-bottom=%d crop-left=%d crop-right=%d encoder=%s "
399             "destination linesizes=%d,%d,%d\n" ,
400             avctx->width, s->stride, avctx->height, s->slice_height,
401             s->crop_top, s->crop_bottom, s->crop_left, s->crop_right, s->codec_name,
402             frame->linesize[0], frame->linesize[1], frame->linesize[2]);
403
404     switch (s->color_format) {
405     case COLOR_FormatYUV420Planar:
406         ff_mediacodec_sw_buffer_copy_yuv420_planar(avctx, s, data, size, info, frame);
407         break;
408     case COLOR_FormatYUV420SemiPlanar:
409     case COLOR_QCOM_FormatYUV420SemiPlanar:
410     case COLOR_QCOM_FormatYUV420SemiPlanar32m:
411         ff_mediacodec_sw_buffer_copy_yuv420_semi_planar(avctx, s, data, size, info, frame);
412         break;
413     case COLOR_TI_FormatYUV420PackedSemiPlanar:
414     case COLOR_TI_FormatYUV420PackedSemiPlanarInterlaced:
415         ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar(avctx, s, data, size, info, frame);
416         break;
417     case COLOR_QCOM_FormatYUV420PackedSemiPlanar64x32Tile2m8ka:
418         ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar_64x32Tile2m8ka(avctx, s, data, size, info, frame);
419         break;
420     default:
421         av_log(avctx, AV_LOG_ERROR, "Unsupported color format 0x%x (value=%d)\n",
422             s->color_format, s->color_format);
423         ret = AVERROR(EINVAL);
424         goto done;
425     }
426
427     ret = 0;
428 done:
429     status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
430     if (status < 0) {
431         av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
432         ret = AVERROR_EXTERNAL;
433     }
434
435     return ret;
436 }
437
438 #define AMEDIAFORMAT_GET_INT32(name, key, mandatory) do {                              \
439     int32_t value = 0;                                                                 \
440     if (ff_AMediaFormat_getInt32(s->format, key, &value)) {                            \
441         (name) = value;                                                                \
442     } else if (mandatory) {                                                            \
443         av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", key, format); \
444         ret = AVERROR_EXTERNAL;                                                        \
445         goto fail;                                                                     \
446     }                                                                                  \
447 } while (0)                                                                            \
448
449 static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecContext *s)
450 {
451     int ret = 0;
452     int width = 0;
453     int height = 0;
454     int color_range = 0;
455     int color_standard = 0;
456     int color_transfer = 0;
457     char *format = NULL;
458
459     if (!s->format) {
460         av_log(avctx, AV_LOG_ERROR, "Output MediaFormat is not set\n");
461         return AVERROR(EINVAL);
462     }
463
464     format = ff_AMediaFormat_toString(s->format);
465     if (!format) {
466         return AVERROR_EXTERNAL;
467     }
468     av_log(avctx, AV_LOG_DEBUG, "Parsing MediaFormat %s\n", format);
469
470     /* Mandatory fields */
471     AMEDIAFORMAT_GET_INT32(s->width,  "width", 1);
472     AMEDIAFORMAT_GET_INT32(s->height, "height", 1);
473
474     AMEDIAFORMAT_GET_INT32(s->stride, "stride", 0);
475     s->stride = s->stride > 0 ? s->stride : s->width;
476
477     AMEDIAFORMAT_GET_INT32(s->slice_height, "slice-height", 0);
478
479     if (strstr(s->codec_name, "OMX.Nvidia.") && s->slice_height == 0) {
480         s->slice_height = FFALIGN(s->height, 16);
481     } else if (strstr(s->codec_name, "OMX.SEC.avc.dec")) {
482         s->slice_height = avctx->height;
483         s->stride = avctx->width;
484     } else if (s->slice_height == 0) {
485         s->slice_height = s->height;
486     }
487
488     AMEDIAFORMAT_GET_INT32(s->color_format, "color-format", 1);
489     avctx->pix_fmt = mcdec_map_color_format(avctx, s, s->color_format);
490     if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
491         av_log(avctx, AV_LOG_ERROR, "Output color format is not supported\n");
492         ret = AVERROR(EINVAL);
493         goto fail;
494     }
495
496     /* Optional fields */
497     AMEDIAFORMAT_GET_INT32(s->crop_top,    "crop-top",    0);
498     AMEDIAFORMAT_GET_INT32(s->crop_bottom, "crop-bottom", 0);
499     AMEDIAFORMAT_GET_INT32(s->crop_left,   "crop-left",   0);
500     AMEDIAFORMAT_GET_INT32(s->crop_right,  "crop-right",  0);
501
502     width = s->crop_right + 1 - s->crop_left;
503     height = s->crop_bottom + 1 - s->crop_top;
504
505     AMEDIAFORMAT_GET_INT32(s->display_width,  "display-width",  0);
506     AMEDIAFORMAT_GET_INT32(s->display_height, "display-height", 0);
507
508     if (s->display_width && s->display_height) {
509         AVRational sar = av_div_q(
510             (AVRational){ s->display_width, s->display_height },
511             (AVRational){ width, height });
512         ff_set_sar(avctx, sar);
513     }
514
515     AMEDIAFORMAT_GET_INT32(color_range, "color-range", 0);
516     if (color_range)
517         avctx->color_range = mcdec_get_color_range(color_range);
518
519     AMEDIAFORMAT_GET_INT32(color_standard, "color-standard", 0);
520     if (color_standard) {
521         avctx->colorspace = mcdec_get_color_space(color_standard);
522         avctx->color_primaries = mcdec_get_color_pri(color_standard);
523     }
524
525     AMEDIAFORMAT_GET_INT32(color_transfer, "color-transfer", 0);
526     if (color_transfer)
527         avctx->color_trc = mcdec_get_color_trc(color_transfer);
528
529     av_log(avctx, AV_LOG_INFO,
530         "Output crop parameters top=%d bottom=%d left=%d right=%d, "
531         "resulting dimensions width=%d height=%d\n",
532         s->crop_top, s->crop_bottom, s->crop_left, s->crop_right,
533         width, height);
534
535     av_freep(&format);
536     return ff_set_dimensions(avctx, width, height);
537 fail:
538     av_freep(&format);
539     return ret;
540 }
541
542 static int mediacodec_dec_flush_codec(AVCodecContext *avctx, MediaCodecDecContext *s)
543 {
544     FFAMediaCodec *codec = s->codec;
545     int status;
546
547     s->output_buffer_count = 0;
548
549     s->draining = 0;
550     s->flushing = 0;
551     s->eos = 0;
552     atomic_fetch_add(&s->serial, 1);
553     atomic_init(&s->hw_buffer_count, 0);
554     s->current_input_buffer = -1;
555
556     status = ff_AMediaCodec_flush(codec);
557     if (status < 0) {
558         av_log(avctx, AV_LOG_ERROR, "Failed to flush codec\n");
559         return AVERROR_EXTERNAL;
560     }
561
562     return 0;
563 }
564
565 int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s,
566                            const char *mime, FFAMediaFormat *format)
567 {
568     int ret = 0;
569     int status;
570     int profile;
571
572     enum AVPixelFormat pix_fmt;
573     static const enum AVPixelFormat pix_fmts[] = {
574         AV_PIX_FMT_MEDIACODEC,
575         AV_PIX_FMT_NONE,
576     };
577
578     s->avctx = avctx;
579     atomic_init(&s->refcount, 1);
580     atomic_init(&s->hw_buffer_count, 0);
581     atomic_init(&s->serial, 1);
582     s->current_input_buffer = -1;
583
584     pix_fmt = ff_get_format(avctx, pix_fmts);
585     if (pix_fmt == AV_PIX_FMT_MEDIACODEC) {
586         AVMediaCodecContext *user_ctx = avctx->hwaccel_context;
587
588         if (avctx->hw_device_ctx) {
589             AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)(avctx->hw_device_ctx->data);
590             if (device_ctx->type == AV_HWDEVICE_TYPE_MEDIACODEC) {
591                 if (device_ctx->hwctx) {
592                     AVMediaCodecDeviceContext *mediacodec_ctx = (AVMediaCodecDeviceContext *)device_ctx->hwctx;
593                     s->surface = ff_mediacodec_surface_ref(mediacodec_ctx->surface, avctx);
594                     av_log(avctx, AV_LOG_INFO, "Using surface %p\n", s->surface);
595                 }
596             }
597         }
598
599         if (!s->surface && user_ctx && user_ctx->surface) {
600             s->surface = ff_mediacodec_surface_ref(user_ctx->surface, avctx);
601             av_log(avctx, AV_LOG_INFO, "Using surface %p\n", s->surface);
602         }
603     }
604
605     profile = ff_AMediaCodecProfile_getProfileFromAVCodecContext(avctx);
606     if (profile < 0) {
607         av_log(avctx, AV_LOG_WARNING, "Unsupported or unknown profile\n");
608     }
609
610     s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, profile, 0, avctx);
611     if (!s->codec_name) {
612         ret = AVERROR_EXTERNAL;
613         goto fail;
614     }
615
616     av_log(avctx, AV_LOG_DEBUG, "Found decoder %s\n", s->codec_name);
617     s->codec = ff_AMediaCodec_createCodecByName(s->codec_name);
618     if (!s->codec) {
619         av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name);
620         ret = AVERROR_EXTERNAL;
621         goto fail;
622     }
623
624     status = ff_AMediaCodec_configure(s->codec, format, s->surface, NULL, 0);
625     if (status < 0) {
626         char *desc = ff_AMediaFormat_toString(format);
627         av_log(avctx, AV_LOG_ERROR,
628             "Failed to configure codec %s (status = %d) with format %s\n",
629             s->codec_name, status, desc);
630         av_freep(&desc);
631
632         ret = AVERROR_EXTERNAL;
633         goto fail;
634     }
635
636     status = ff_AMediaCodec_start(s->codec);
637     if (status < 0) {
638         char *desc = ff_AMediaFormat_toString(format);
639         av_log(avctx, AV_LOG_ERROR,
640             "Failed to start codec %s (status = %d) with format %s\n",
641             s->codec_name, status, desc);
642         av_freep(&desc);
643         ret = AVERROR_EXTERNAL;
644         goto fail;
645     }
646
647     s->format = ff_AMediaCodec_getOutputFormat(s->codec);
648     if (s->format) {
649         if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
650             av_log(avctx, AV_LOG_ERROR,
651                 "Failed to configure context\n");
652             goto fail;
653         }
654     }
655
656     av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p started successfully\n", s->codec);
657
658     return 0;
659
660 fail:
661     av_log(avctx, AV_LOG_ERROR, "MediaCodec %p failed to start\n", s->codec);
662     ff_mediacodec_dec_close(avctx, s);
663     return ret;
664 }
665
666 int ff_mediacodec_dec_send(AVCodecContext *avctx, MediaCodecDecContext *s,
667                            AVPacket *pkt, bool wait)
668 {
669     int offset = 0;
670     int need_draining = 0;
671     uint8_t *data;
672     size_t size;
673     FFAMediaCodec *codec = s->codec;
674     int status;
675     int64_t input_dequeue_timeout_us = wait ? INPUT_DEQUEUE_TIMEOUT_US : 0;
676     int64_t pts;
677
678     if (s->flushing) {
679         av_log(avctx, AV_LOG_ERROR, "Decoder is flushing and cannot accept new buffer "
680                                     "until all output buffers have been released\n");
681         return AVERROR_EXTERNAL;
682     }
683
684     if (pkt->size == 0) {
685         need_draining = 1;
686     }
687
688     if (s->draining && s->eos) {
689         return AVERROR_EOF;
690     }
691
692     while (offset < pkt->size || (need_draining && !s->draining)) {
693         ssize_t index = s->current_input_buffer;
694         if (index < 0) {
695             index = ff_AMediaCodec_dequeueInputBuffer(codec, input_dequeue_timeout_us);
696             if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
697                 av_log(avctx, AV_LOG_TRACE, "No input buffer available, try again later\n");
698                 break;
699             }
700
701             if (index < 0) {
702                 av_log(avctx, AV_LOG_ERROR, "Failed to dequeue input buffer (status=%zd)\n", index);
703                 return AVERROR_EXTERNAL;
704             }
705         }
706         s->current_input_buffer = -1;
707
708         data = ff_AMediaCodec_getInputBuffer(codec, index, &size);
709         if (!data) {
710             av_log(avctx, AV_LOG_ERROR, "Failed to get input buffer\n");
711             return AVERROR_EXTERNAL;
712         }
713
714         pts = pkt->pts;
715         if (pts == AV_NOPTS_VALUE) {
716             av_log(avctx, AV_LOG_WARNING, "Input packet is missing PTS\n");
717             pts = 0;
718         }
719         if (pts && avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
720             pts = av_rescale_q(pts, avctx->pkt_timebase, AV_TIME_BASE_Q);
721         }
722
723         if (need_draining) {
724             uint32_t flags = ff_AMediaCodec_getBufferFlagEndOfStream(codec);
725
726             av_log(avctx, AV_LOG_DEBUG, "Sending End Of Stream signal\n");
727
728             status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, 0, pts, flags);
729             if (status < 0) {
730                 av_log(avctx, AV_LOG_ERROR, "Failed to queue input empty buffer (status = %d)\n", status);
731                 return AVERROR_EXTERNAL;
732             }
733
734             av_log(avctx, AV_LOG_TRACE,
735                    "Queued empty EOS input buffer %zd with flags=%d\n", index, flags);
736
737             s->draining = 1;
738             return 0;
739         }
740
741         size = FFMIN(pkt->size - offset, size);
742         memcpy(data, pkt->data + offset, size);
743         offset += size;
744
745         status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, size, pts, 0);
746         if (status < 0) {
747             av_log(avctx, AV_LOG_ERROR, "Failed to queue input buffer (status = %d)\n", status);
748             return AVERROR_EXTERNAL;
749         }
750
751         av_log(avctx, AV_LOG_TRACE,
752                "Queued input buffer %zd size=%zd ts=%"PRIi64"\n", index, size, pts);
753     }
754
755     if (offset == 0)
756         return AVERROR(EAGAIN);
757     return offset;
758 }
759
760 int ff_mediacodec_dec_receive(AVCodecContext *avctx, MediaCodecDecContext *s,
761                               AVFrame *frame, bool wait)
762 {
763     int ret;
764     uint8_t *data;
765     ssize_t index;
766     size_t size;
767     FFAMediaCodec *codec = s->codec;
768     FFAMediaCodecBufferInfo info = { 0 };
769     int status;
770     int64_t output_dequeue_timeout_us = OUTPUT_DEQUEUE_TIMEOUT_US;
771
772     if (s->draining && s->eos) {
773         return AVERROR_EOF;
774     }
775
776     if (s->draining) {
777         /* If the codec is flushing or need to be flushed, block for a fair
778          * amount of time to ensure we got a frame */
779         output_dequeue_timeout_us = OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US;
780     } else if (s->output_buffer_count == 0 || !wait) {
781         /* If the codec hasn't produced any frames, do not block so we
782          * can push data to it as fast as possible, and get the first
783          * frame */
784         output_dequeue_timeout_us = 0;
785     }
786
787     index = ff_AMediaCodec_dequeueOutputBuffer(codec, &info, output_dequeue_timeout_us);
788     if (index >= 0) {
789         av_log(avctx, AV_LOG_TRACE, "Got output buffer %zd"
790                 " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64
791                 " flags=%" PRIu32 "\n", index, info.offset, info.size,
792                 info.presentationTimeUs, info.flags);
793
794         if (info.flags & ff_AMediaCodec_getBufferFlagEndOfStream(codec)) {
795             s->eos = 1;
796         }
797
798         if (info.size) {
799             if (s->surface) {
800                 if ((ret = mediacodec_wrap_hw_buffer(avctx, s, index, &info, frame)) < 0) {
801                     av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
802                     return ret;
803                 }
804             } else {
805                 data = ff_AMediaCodec_getOutputBuffer(codec, index, &size);
806                 if (!data) {
807                     av_log(avctx, AV_LOG_ERROR, "Failed to get output buffer\n");
808                     return AVERROR_EXTERNAL;
809                 }
810
811                 if ((ret = mediacodec_wrap_sw_buffer(avctx, s, data, size, index, &info, frame)) < 0) {
812                     av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
813                     return ret;
814                 }
815             }
816
817             s->output_buffer_count++;
818             return 0;
819         } else {
820             status = ff_AMediaCodec_releaseOutputBuffer(codec, index, 0);
821             if (status < 0) {
822                 av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
823             }
824         }
825
826     } else if (ff_AMediaCodec_infoOutputFormatChanged(codec, index)) {
827         char *format = NULL;
828
829         if (s->format) {
830             status = ff_AMediaFormat_delete(s->format);
831             if (status < 0) {
832                 av_log(avctx, AV_LOG_ERROR, "Failed to delete MediaFormat %p\n", s->format);
833             }
834         }
835
836         s->format = ff_AMediaCodec_getOutputFormat(codec);
837         if (!s->format) {
838             av_log(avctx, AV_LOG_ERROR, "Failed to get output format\n");
839             return AVERROR_EXTERNAL;
840         }
841
842         format = ff_AMediaFormat_toString(s->format);
843         if (!format) {
844             return AVERROR_EXTERNAL;
845         }
846         av_log(avctx, AV_LOG_INFO, "Output MediaFormat changed to %s\n", format);
847         av_freep(&format);
848
849         if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
850             return ret;
851         }
852
853     } else if (ff_AMediaCodec_infoOutputBuffersChanged(codec, index)) {
854         ff_AMediaCodec_cleanOutputBuffers(codec);
855     } else if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
856         if (s->draining) {
857             av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer within %" PRIi64 "ms "
858                                         "while draining remaining frames, output will probably lack frames\n",
859                                         output_dequeue_timeout_us / 1000);
860         } else {
861             av_log(avctx, AV_LOG_TRACE, "No output buffer available, try again later\n");
862         }
863     } else {
864         av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer (status=%zd)\n", index);
865         return AVERROR_EXTERNAL;
866     }
867
868     return AVERROR(EAGAIN);
869 }
870
871 /*
872 * ff_mediacodec_dec_flush returns 0 if the flush cannot be performed on
873 * the codec (because the user retains frames). The codec stays in the
874 * flushing state.
875 *
876 * ff_mediacodec_dec_flush returns 1 if the flush can actually be
877 * performed on the codec. The codec leaves the flushing state and can
878 * process again packets.
879 *
880 * ff_mediacodec_dec_flush returns a negative value if an error has
881 * occurred.
882 */
883 int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s)
884 {
885     if (!s->surface || atomic_load(&s->refcount) == 1) {
886         int ret;
887
888         /* No frames (holding a reference to the codec) are retained by the
889          * user, thus we can flush the codec and returns accordingly */
890         if ((ret = mediacodec_dec_flush_codec(avctx, s)) < 0) {
891             return ret;
892         }
893
894         return 1;
895     }
896
897     s->flushing = 1;
898     return 0;
899 }
900
901 int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s)
902 {
903     ff_mediacodec_dec_unref(s);
904
905     return 0;
906 }
907
908 int ff_mediacodec_dec_is_flushing(AVCodecContext *avctx, MediaCodecDecContext *s)
909 {
910     return s->flushing;
911 }