FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mediacodecdec_common.c
Go to the documentation of this file.
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/mem.h"
28 #include "libavutil/log.h"
29 #include "libavutil/pixfmt.h"
30 #include "libavutil/time.h"
31 #include "libavutil/timestamp.h"
32 
33 #include "avcodec.h"
34 #include "internal.h"
35 
36 #include "mediacodec.h"
37 #include "mediacodec_surface.h"
38 #include "mediacodec_sw_buffer.h"
39 #include "mediacodec_wrapper.h"
40 #include "mediacodecdec_common.h"
41 
42 /**
43  * OMX.k3.video.decoder.avc, OMX.NVIDIA.* OMX.SEC.avc.dec and OMX.google
44  * codec workarounds used in various place are taken from the Gstreamer
45  * project.
46  *
47  * Gstreamer references:
48  * https://cgit.freedesktop.org/gstreamer/gst-plugins-bad/tree/sys/androidmedia/
49  *
50  * Gstreamer copyright notice:
51  *
52  * Copyright (C) 2012, Collabora Ltd.
53  * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
54  *
55  * Copyright (C) 2012, Rafaël Carré <funman@videolanorg>
56  *
57  * Copyright (C) 2015, Sebastian Dröge <sebastian@centricular.com>
58  *
59  * Copyright (C) 2014-2015, Collabora Ltd.
60  * Author: Matthieu Bouron <matthieu.bouron@gcollabora.com>
61  *
62  * Copyright (C) 2015, Edward Hervey
63  * Author: Edward Hervey <bilboed@gmail.com>
64  *
65  * Copyright (C) 2015, Matthew Waters <matthew@centricular.com>
66  *
67  * This library is free software; you can redistribute it and/or
68  * modify it under the terms of the GNU Lesser General Public
69  * License as published by the Free Software Foundation
70  * version 2.1 of the License.
71  *
72  * This library is distributed in the hope that it will be useful,
73  * but WITHOUT ANY WARRANTY; without even the implied warranty of
74  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
75  * Lesser General Public License for more details.
76  *
77  * You should have received a copy of the GNU Lesser General Public
78  * License along with this library; if not, write to the Free Software
79  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
80  *
81  */
82 
83 #define INPUT_DEQUEUE_TIMEOUT_US 8000
84 #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
85 #define OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US 1000000
86 
87 enum {
97 };
98 
99 static const struct {
100 
103 
104 } color_formats[] = {
105 
113  { 0 }
114 };
115 
118  int color_format)
119 {
120  int i;
121  enum AVPixelFormat ret = AV_PIX_FMT_NONE;
122 
123  if (s->surface) {
124  return AV_PIX_FMT_MEDIACODEC;
125  }
126 
127  if (!strcmp(s->codec_name, "OMX.k3.video.decoder.avc") && color_format == COLOR_FormatYCbYCr) {
129  }
130 
131  for (i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) {
132  if (color_formats[i].color_format == color_format) {
133  return color_formats[i].pix_fmt;
134  }
135  }
136 
137  av_log(avctx, AV_LOG_ERROR, "Output color format 0x%x (value=%d) is not supported\n",
138  color_format, color_format);
139 
140  return ret;
141 }
142 
144 {
145  atomic_fetch_add(&s->refcount, 1);
146 }
147 
149 {
150  if (!s)
151  return;
152 
153  if (atomic_fetch_sub(&s->refcount, 1) == 1) {
154  if (s->codec) {
156  s->codec = NULL;
157  }
158 
159  if (s->format) {
161  s->format = NULL;
162  }
163 
164  if (s->surface) {
166  s->surface = NULL;
167  }
168 
169  av_freep(&s->codec_name);
170  av_freep(&s);
171  }
172 }
173 
174 static void mediacodec_buffer_release(void *opaque, uint8_t *data)
175 {
176  AVMediaCodecBuffer *buffer = opaque;
177  MediaCodecDecContext *ctx = buffer->ctx;
178  int released = atomic_load(&buffer->released);
179 
180  if (!released) {
181  ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, 0);
182  }
183 
185  av_freep(&buffer);
186 }
187 
190  ssize_t index,
192  AVFrame *frame)
193 {
194  int ret = 0;
195  int status = 0;
196  AVMediaCodecBuffer *buffer = NULL;
197 
198  frame->buf[0] = NULL;
199  frame->width = avctx->width;
200  frame->height = avctx->height;
201  frame->format = avctx->pix_fmt;
202 
203  if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
204  frame->pts = av_rescale_q(info->presentationTimeUs,
205  av_make_q(1, 1000000),
206  avctx->pkt_timebase);
207  } else {
208  frame->pts = info->presentationTimeUs;
209  }
210 #if FF_API_PKT_PTS
212  frame->pkt_pts = frame->pts;
214 #endif
215  frame->pkt_dts = AV_NOPTS_VALUE;
216 
217  buffer = av_mallocz(sizeof(AVMediaCodecBuffer));
218  if (!buffer) {
219  ret = AVERROR(ENOMEM);
220  goto fail;
221  }
222 
223  atomic_init(&buffer->released, 0);
224 
225  frame->buf[0] = av_buffer_create(NULL,
226  0,
228  buffer,
230 
231  if (!frame->buf[0]) {
232  ret = AVERROR(ENOMEM);
233  goto fail;
234 
235  }
236 
237  buffer->ctx = s;
239 
240  buffer->index = index;
241  buffer->pts = info->presentationTimeUs;
242 
243  frame->data[3] = (uint8_t *)buffer;
244 
245  return 0;
246 fail:
247  av_freep(buffer);
248  av_buffer_unref(&frame->buf[0]);
249  status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
250  if (status < 0) {
251  av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
252  ret = AVERROR_EXTERNAL;
253  }
254 
255  return ret;
256 }
257 
260  uint8_t *data,
261  size_t size,
262  ssize_t index,
264  AVFrame *frame)
265 {
266  int ret = 0;
267  int status = 0;
268 
269  frame->width = avctx->width;
270  frame->height = avctx->height;
271  frame->format = avctx->pix_fmt;
272 
273  /* MediaCodec buffers needs to be copied to our own refcounted buffers
274  * because the flush command invalidates all input and output buffers.
275  */
276  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
277  av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer\n");
278  goto done;
279  }
280 
281  /* Override frame->pkt_pts as ff_get_buffer will override its value based
282  * on the last avpacket received which is not in sync with the frame:
283  * * N avpackets can be pushed before 1 frame is actually returned
284  * * 0-sized avpackets are pushed to flush remaining frames at EOS */
285  frame->pts = info->presentationTimeUs;
286 #if FF_API_PKT_PTS
288  frame->pkt_pts = info->presentationTimeUs;
290 #endif
291  frame->pkt_dts = AV_NOPTS_VALUE;
292 
293  av_log(avctx, AV_LOG_DEBUG,
294  "Frame: width=%d stride=%d height=%d slice-height=%d "
295  "crop-top=%d crop-bottom=%d crop-left=%d crop-right=%d encoder=%s\n"
296  "destination linesizes=%d,%d,%d\n" ,
297  avctx->width, s->stride, avctx->height, s->slice_height,
299  frame->linesize[0], frame->linesize[1], frame->linesize[2]);
300 
301  switch (s->color_format) {
303  ff_mediacodec_sw_buffer_copy_yuv420_planar(avctx, s, data, size, info, frame);
304  break;
308  ff_mediacodec_sw_buffer_copy_yuv420_semi_planar(avctx, s, data, size, info, frame);
309  break;
312  ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar(avctx, s, data, size, info, frame);
313  break;
316  break;
317  default:
318  av_log(avctx, AV_LOG_ERROR, "Unsupported color format 0x%x (value=%d)\n",
319  s->color_format, s->color_format);
320  ret = AVERROR(EINVAL);
321  goto done;
322  }
323 
324  ret = 0;
325 done:
326  status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
327  if (status < 0) {
328  av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
329  ret = AVERROR_EXTERNAL;
330  }
331 
332  return ret;
333 }
334 
336 {
337  int width = 0;
338  int height = 0;
339  int32_t value = 0;
340  char *format = NULL;
341 
342  if (!s->format) {
343  av_log(avctx, AV_LOG_ERROR, "Output MediaFormat is not set\n");
344  return AVERROR(EINVAL);
345  }
346 
347  format = ff_AMediaFormat_toString(s->format);
348  if (!format) {
349  return AVERROR_EXTERNAL;
350  }
351  av_log(avctx, AV_LOG_DEBUG, "Parsing MediaFormat %s\n", format);
352  av_freep(&format);
353 
354  /* Mandatory fields */
355  if (!ff_AMediaFormat_getInt32(s->format, "width", &value)) {
356  format = ff_AMediaFormat_toString(s->format);
357  av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "width", format);
358  av_freep(&format);
359  return AVERROR_EXTERNAL;
360  }
361  s->width = value;
362 
363  if (!ff_AMediaFormat_getInt32(s->format, "height", &value)) {
364  format = ff_AMediaFormat_toString(s->format);
365  av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "height", format);
366  av_freep(&format);
367  return AVERROR_EXTERNAL;
368  }
369  s->height = value;
370 
371  if (!ff_AMediaFormat_getInt32(s->format, "stride", &value)) {
372  format = ff_AMediaFormat_toString(s->format);
373  av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "stride", format);
374  av_freep(&format);
375  return AVERROR_EXTERNAL;
376  }
377  s->stride = value > 0 ? value : s->width;
378 
379  if (!ff_AMediaFormat_getInt32(s->format, "slice-height", &value)) {
380  format = ff_AMediaFormat_toString(s->format);
381  av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "slice-height", format);
382  av_freep(&format);
383  return AVERROR_EXTERNAL;
384  }
385  s->slice_height = value > 0 ? value : s->height;
386 
387  if (strstr(s->codec_name, "OMX.Nvidia.")) {
388  s->slice_height = FFALIGN(s->height, 16);
389  } else if (strstr(s->codec_name, "OMX.SEC.avc.dec")) {
390  s->slice_height = avctx->height;
391  s->stride = avctx->width;
392  }
393 
394  if (!ff_AMediaFormat_getInt32(s->format, "color-format", &value)) {
395  format = ff_AMediaFormat_toString(s->format);
396  av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "color-format", format);
397  av_freep(&format);
398  return AVERROR_EXTERNAL;
399  }
400  s->color_format = value;
401 
402  s->pix_fmt = avctx->pix_fmt = mcdec_map_color_format(avctx, s, value);
403  if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
404  av_log(avctx, AV_LOG_ERROR, "Output color format is not supported\n");
405  return AVERROR(EINVAL);
406  }
407 
408  /* Optional fields */
409  if (ff_AMediaFormat_getInt32(s->format, "crop-top", &value))
410  s->crop_top = value;
411 
412  if (ff_AMediaFormat_getInt32(s->format, "crop-bottom", &value))
413  s->crop_bottom = value;
414 
415  if (ff_AMediaFormat_getInt32(s->format, "crop-left", &value))
416  s->crop_left = value;
417 
418  if (ff_AMediaFormat_getInt32(s->format, "crop-right", &value))
419  s->crop_right = value;
420 
421  width = s->crop_right + 1 - s->crop_left;
422  height = s->crop_bottom + 1 - s->crop_top;
423 
424  av_log(avctx, AV_LOG_INFO,
425  "Output crop parameters top=%d bottom=%d left=%d right=%d, "
426  "resulting dimensions width=%d height=%d\n",
427  s->crop_top, s->crop_bottom, s->crop_left, s->crop_right,
428  width, height);
429 
430  return ff_set_dimensions(avctx, width, height);
431 }
432 
433 
435 {
436  FFAMediaCodec *codec = s->codec;
437  int status;
438 
439  s->output_buffer_count = 0;
440 
441  s->draining = 0;
442  s->flushing = 0;
443  s->eos = 0;
444 
445  status = ff_AMediaCodec_flush(codec);
446  if (status < 0) {
447  av_log(avctx, AV_LOG_ERROR, "Failed to flush codec\n");
448  return AVERROR_EXTERNAL;
449  }
450 
451  return 0;
452 }
453 
455  const char *mime, FFAMediaFormat *format)
456 {
457  int ret = 0;
458  int status;
459  int profile;
460 
461  enum AVPixelFormat pix_fmt;
462  static const enum AVPixelFormat pix_fmts[] = {
465  };
466 
467  atomic_init(&s->refcount, 1);
468 
469  pix_fmt = ff_get_format(avctx, pix_fmts);
470  if (pix_fmt == AV_PIX_FMT_MEDIACODEC) {
471  AVMediaCodecContext *user_ctx = avctx->hwaccel_context;
472 
473  if (user_ctx && user_ctx->surface) {
474  s->surface = ff_mediacodec_surface_ref(user_ctx->surface, avctx);
475  av_log(avctx, AV_LOG_INFO, "Using surface %p\n", s->surface);
476  }
477  }
478 
480  if (profile < 0) {
481  av_log(avctx, AV_LOG_WARNING, "Unsupported or unknown profile");
482  }
483 
484  s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, profile, 0, avctx);
485  if (!s->codec_name) {
486  ret = AVERROR_EXTERNAL;
487  goto fail;
488  }
489 
490  av_log(avctx, AV_LOG_DEBUG, "Found decoder %s\n", s->codec_name);
492  if (!s->codec) {
493  av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name);
494  ret = AVERROR_EXTERNAL;
495  goto fail;
496  }
497 
498  status = ff_AMediaCodec_configure(s->codec, format, s->surface, NULL, 0);
499  if (status < 0) {
500  char *desc = ff_AMediaFormat_toString(format);
501  av_log(avctx, AV_LOG_ERROR,
502  "Failed to configure codec (status = %d) with format %s\n",
503  status, desc);
504  av_freep(&desc);
505 
506  ret = AVERROR_EXTERNAL;
507  goto fail;
508  }
509 
510  status = ff_AMediaCodec_start(s->codec);
511  if (status < 0) {
512  char *desc = ff_AMediaFormat_toString(format);
513  av_log(avctx, AV_LOG_ERROR,
514  "Failed to start codec (status = %d) with format %s\n",
515  status, desc);
516  av_freep(&desc);
517  ret = AVERROR_EXTERNAL;
518  goto fail;
519  }
520 
522  if (s->format) {
523  if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
524  av_log(avctx, AV_LOG_ERROR,
525  "Failed to configure context\n");
526  goto fail;
527  }
528  }
529 
530  av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p started successfully\n", s->codec);
531 
532  return 0;
533 
534 fail:
535  av_log(avctx, AV_LOG_ERROR, "MediaCodec %p failed to start\n", s->codec);
536  ff_mediacodec_dec_close(avctx, s);
537  return ret;
538 }
539 
541  AVFrame *frame, int *got_frame,
542  AVPacket *pkt)
543 {
544  int ret;
545  int offset = 0;
546  int need_draining = 0;
547  uint8_t *data;
548  ssize_t index;
549  size_t size;
550  FFAMediaCodec *codec = s->codec;
551  FFAMediaCodecBufferInfo info = { 0 };
552 
553  int status;
554 
555  int64_t input_dequeue_timeout_us = INPUT_DEQUEUE_TIMEOUT_US;
556  int64_t output_dequeue_timeout_us = OUTPUT_DEQUEUE_TIMEOUT_US;
557 
558  if (s->flushing) {
559  av_log(avctx, AV_LOG_ERROR, "Decoder is flushing and cannot accept new buffer "
560  "until all output buffers have been released\n");
561  return AVERROR_EXTERNAL;
562  }
563 
564  if (pkt->size == 0) {
565  need_draining = 1;
566  }
567 
568  if (s->draining && s->eos) {
569  return 0;
570  }
571 
572  while (offset < pkt->size || (need_draining && !s->draining)) {
573 
574  index = ff_AMediaCodec_dequeueInputBuffer(codec, input_dequeue_timeout_us);
575  if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
576  break;
577  }
578 
579  if (index < 0) {
580  av_log(avctx, AV_LOG_ERROR, "Failed to dequeue input buffer (status=%zd)\n", index);
581  return AVERROR_EXTERNAL;
582  }
583 
584  data = ff_AMediaCodec_getInputBuffer(codec, index, &size);
585  if (!data) {
586  av_log(avctx, AV_LOG_ERROR, "Failed to get input buffer\n");
587  return AVERROR_EXTERNAL;
588  }
589 
590  if (need_draining) {
591  int64_t pts = pkt->pts;
593 
594  if (s->surface) {
595  pts = av_rescale_q(pts, avctx->pkt_timebase, av_make_q(1, 1000000));
596  }
597 
598  av_log(avctx, AV_LOG_DEBUG, "Sending End Of Stream signal\n");
599 
600  status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, 0, pts, flags);
601  if (status < 0) {
602  av_log(avctx, AV_LOG_ERROR, "Failed to queue input empty buffer (status = %d)\n", status);
603  return AVERROR_EXTERNAL;
604  }
605 
606  s->draining = 1;
607  break;
608  } else {
609  int64_t pts = pkt->pts;
610 
611  size = FFMIN(pkt->size - offset, size);
612 
613  memcpy(data, pkt->data + offset, size);
614  offset += size;
615 
616  if (s->surface && avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
617  pts = av_rescale_q(pts, avctx->pkt_timebase, av_make_q(1, 1000000));
618  }
619 
620  status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, size, pts, 0);
621  if (status < 0) {
622  av_log(avctx, AV_LOG_ERROR, "Failed to queue input buffer (status = %d)\n", status);
623  return AVERROR_EXTERNAL;
624  }
625  }
626  }
627 
628  if (need_draining || s->draining) {
629  /* If the codec is flushing or need to be flushed, block for a fair
630  * amount of time to ensure we got a frame */
631  output_dequeue_timeout_us = OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US;
632  } else if (s->output_buffer_count == 0) {
633  /* If the codec hasn't produced any frames, do not block so we
634  * can push data to it as fast as possible, and get the first
635  * frame */
636  output_dequeue_timeout_us = 0;
637  }
638 
639  index = ff_AMediaCodec_dequeueOutputBuffer(codec, &info, output_dequeue_timeout_us);
640  if (index >= 0) {
641  int ret;
642 
643  av_log(avctx, AV_LOG_DEBUG, "Got output buffer %zd"
644  " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64
645  " flags=%" PRIu32 "\n", index, info.offset, info.size,
646  info.presentationTimeUs, info.flags);
647 
649  s->eos = 1;
650  }
651 
652  if (info.size) {
653  if (s->surface) {
654  if ((ret = mediacodec_wrap_hw_buffer(avctx, s, index, &info, frame)) < 0) {
655  av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
656  return ret;
657  }
658  } else {
659  data = ff_AMediaCodec_getOutputBuffer(codec, index, &size);
660  if (!data) {
661  av_log(avctx, AV_LOG_ERROR, "Failed to get output buffer\n");
662  return AVERROR_EXTERNAL;
663  }
664 
665  if ((ret = mediacodec_wrap_sw_buffer(avctx, s, data, size, index, &info, frame)) < 0) {
666  av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
667  return ret;
668  }
669  }
670 
671  *got_frame = 1;
672  s->output_buffer_count++;
673  } else {
674  status = ff_AMediaCodec_releaseOutputBuffer(codec, index, 0);
675  if (status < 0) {
676  av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
677  }
678  }
679 
680  } else if (ff_AMediaCodec_infoOutputFormatChanged(codec, index)) {
681  char *format = NULL;
682 
683  if (s->format) {
684  status = ff_AMediaFormat_delete(s->format);
685  if (status < 0) {
686  av_log(avctx, AV_LOG_ERROR, "Failed to delete MediaFormat %p\n", s->format);
687  }
688  }
689 
691  if (!s->format) {
692  av_log(avctx, AV_LOG_ERROR, "Failed to get output format\n");
693  return AVERROR_EXTERNAL;
694  }
695 
696  format = ff_AMediaFormat_toString(s->format);
697  if (!format) {
698  return AVERROR_EXTERNAL;
699  }
700  av_log(avctx, AV_LOG_INFO, "Output MediaFormat changed to %s\n", format);
701  av_freep(&format);
702 
703  if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
704  return ret;
705  }
706 
707  } else if (ff_AMediaCodec_infoOutputBuffersChanged(codec, index)) {
709  } else if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
710  if (s->draining) {
711  av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer within %" PRIi64 "ms "
712  "while draining remaining frames, output will probably lack frames\n",
713  output_dequeue_timeout_us / 1000);
714  } else {
715  av_log(avctx, AV_LOG_DEBUG, "No output buffer available, try again later\n");
716  }
717  } else {
718  av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer (status=%zd)\n", index);
719  return AVERROR_EXTERNAL;
720  }
721 
722  return offset;
723 }
724 
726 {
727  if (!s->surface || atomic_load(&s->refcount) == 1) {
728  int ret;
729 
730  /* No frames (holding a reference to the codec) are retained by the
731  * user, thus we can flush the codec and returns accordingly */
732  if ((ret = mediacodec_dec_flush_codec(avctx, s)) < 0) {
733  return ret;
734  }
735 
736  return 1;
737  }
738 
739  s->flushing = 1;
740  return 0;
741 }
742 
744 {
746 
747  return 0;
748 }
749 
751 {
752  return s->flushing;
753 }
754 
756  .name = "mediacodec",
757  .type = AVMEDIA_TYPE_VIDEO,
758  .id = AV_CODEC_ID_H264,
759  .pix_fmt = AV_PIX_FMT_MEDIACODEC,
760 };
761 
763  .name = "mediacodec",
764  .type = AVMEDIA_TYPE_VIDEO,
765  .id = AV_CODEC_ID_HEVC,
766  .pix_fmt = AV_PIX_FMT_MEDIACODEC,
767 };
768 
770  .name = "mediacodec",
771  .type = AVMEDIA_TYPE_VIDEO,
772  .id = AV_CODEC_ID_MPEG4,
773  .pix_fmt = AV_PIX_FMT_MEDIACODEC,
774 };
775 
777  .name = "mediacodec",
778  .type = AVMEDIA_TYPE_VIDEO,
779  .id = AV_CODEC_ID_VP8,
780  .pix_fmt = AV_PIX_FMT_MEDIACODEC,
781 };
782 
784  .name = "mediacodec",
785  .type = AVMEDIA_TYPE_VIDEO,
786  .id = AV_CODEC_ID_VP9,
787  .pix_fmt = AV_PIX_FMT_MEDIACODEC,
788 };
This structure holds a reference to a android/view/Surface object that will be used as output by the ...
Definition: mediacodec.h:33
#define NULL
Definition: coverity.c:32
int ff_mediacodec_dec_decode(AVCodecContext *avctx, MediaCodecDecContext *s, AVFrame *frame, int *got_frame, AVPacket *pkt)
const char * s
Definition: avisynth_c.h:768
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
AVHWAccel ff_vp8_mediacodec_hwaccel
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
char * ff_AMediaCodecList_getCodecNameByType(const char *mime, int profile, int encoder, void *log_ctx)
Memory handling functions.
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:210
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:370
const char * desc
Definition: nvenc.c:60
AVHWAccel ff_mpeg4_mediacodec_hwaccel
FFAMediaCodec * ff_AMediaCodec_createCodecByName(const char *name)
void * ff_mediacodec_surface_ref(void *surface, void *log_ctx)
int num
Numerator.
Definition: rational.h:59
int size
Definition: avcodec.h:1658
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1960
void ff_mediacodec_sw_buffer_copy_yuv420_semi_planar(AVCodecContext *avctx, MediaCodecDecContext *s, uint8_t *data, size_t size, FFAMediaCodecBufferInfo *info, AVFrame *frame)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static AVPacket pkt
void ff_mediacodec_sw_buffer_copy_yuv420_planar(AVCodecContext *avctx, MediaCodecDecContext *s, uint8_t *data, size_t size, FFAMediaCodecBufferInfo *info, AVFrame *frame)
The code handling the various YUV color formats is taken from the GStreamer project.
int ff_mediacodec_dec_close(AVCodecContext *avctx, MediaCodecDecContext *s)
int ff_mediacodec_dec_is_flushing(AVCodecContext *avctx, MediaCodecDecContext *s)
static const struct @93 color_formats[]
int ff_AMediaCodec_flush(FFAMediaCodec *codec)
int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec *codec, size_t idx, int render)
uint8_t
AVHWAccel ff_vp9_mediacodec_hwaccel
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:3064
timestamp utils, mostly useful for debugging/logging purposes
int ff_AMediaCodec_infoOutputBuffersChanged(FFAMediaCodec *codec, ssize_t idx)
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:271
static AVFrame * frame
#define height
int ff_AMediaCodec_infoOutputFormatChanged(FFAMediaCodec *codec, ssize_t idx)
uint8_t * data
Definition: avcodec.h:1657
static int flags
Definition: log.c:57
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
Definition: buffer.h:113
char * ff_AMediaFormat_toString(FFAMediaFormat *format)
ptrdiff_t size
Definition: opengl_enc.c:101
int ff_mediacodec_surface_unref(void *surface, void *log_ctx)
int color_format
static void ff_mediacodec_dec_unref(MediaCodecDecContext *s)
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
enum AVPixelFormat pix_fmt
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are.
Definition: avcodec.h:3443
int width
width and height of the video frame
Definition: frame.h:239
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define atomic_load(object)
Definition: stdatomic.h:93
#define AVERROR(e)
Definition: error.h:43
int ff_mediacodec_dec_flush(AVCodecContext *avctx, MediaCodecDecContext *s)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
uint8_t * ff_AMediaCodec_getInputBuffer(FFAMediaCodec *codec, size_t idx, size_t *out_size)
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:90
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
#define atomic_fetch_sub(object, operand)
Definition: stdatomic.h:137
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define fail()
Definition: checkasm.h:89
void ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar(AVCodecContext *avctx, MediaCodecDecContext *s, uint8_t *data, size_t size, FFAMediaCodecBufferInfo *info, AVFrame *frame)
int ff_AMediaCodec_queueInputBuffer(FFAMediaCodec *codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags)
const char * name
Name of the hardware accelerated codec.
Definition: avcodec.h:3807
#define FFMIN(a, b)
Definition: common.h:96
ssize_t ff_AMediaCodec_dequeueOutputBuffer(FFAMediaCodec *codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs)
#define width
int width
picture width / height.
Definition: avcodec.h:1919
void ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar_64x32Tile2m8ka(AVCodecContext *avctx, MediaCodecDecContext *s, uint8_t *data, size_t size, FFAMediaCodecBufferInfo *info, AVFrame *frame)
static int mediacodec_wrap_sw_buffer(AVCodecContext *avctx, MediaCodecDecContext *s, uint8_t *data, size_t size, ssize_t index, FFAMediaCodecBufferInfo *info, AVFrame *frame)
int ff_AMediaCodec_delete(FFAMediaCodec *codec)
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:109
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
int ff_AMediaCodec_getBufferFlagEndOfStream(FFAMediaCodec *codec)
#define FF_ARRAY_ELEMS(a)
ssize_t ff_AMediaCodec_dequeueInputBuffer(FFAMediaCodec *codec, int64_t timeoutUs)
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: utils.c:1112
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:251
int ff_AMediaFormat_getInt32(FFAMediaFormat *format, const char *name, int32_t *out)
#define atomic_fetch_add(object, operand)
Definition: stdatomic.h:131
#define INPUT_DEQUEUE_TIMEOUT_US
OMX.k3.video.decoder.avc, OMX.NVIDIA.
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
int ff_AMediaCodec_cleanOutputBuffers(FFAMediaCodec *codec)
int ff_AMediaCodec_start(FFAMediaCodec *codec)
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:218
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
main external API structure.
Definition: avcodec.h:1732
int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx)
The following API around MediaCodec and MediaFormat is based on the NDK one provided by Google since ...
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:953
static const char * format
Definition: movenc.c:47
int index
Definition: gxfenc.c:89
static void mediacodec_buffer_release(void *opaque, uint8_t *data)
int ff_AMediaCodec_infoTryAgainLater(FFAMediaCodec *codec, ssize_t idx)
AVHWAccel ff_h264_mediacodec_hwaccel
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
int ff_AMediaFormat_delete(FFAMediaFormat *format)
mfxU16 profile
Definition: qsvenc.c:44
#define OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US
hardware decoding through MediaCodec
Definition: pixfmt.h:307
static int64_t pts
Global timestamp for the audio frames.
uint8_t * ff_AMediaCodec_getOutputBuffer(FFAMediaCodec *codec, size_t idx, size_t *out_size)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:201
attribute_deprecated int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
Definition: frame.h:279
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:287
int ff_AMediaCodec_configure(FFAMediaCodec *codec, const FFAMediaFormat *format, void *surface, void *crypto, uint32_t flags)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
common internal api header.
common internal and external API header
static void ff_mediacodec_dec_ref(MediaCodecDecContext *s)
int den
Denominator.
Definition: rational.h:60
int ff_mediacodec_dec_init(AVCodecContext *avctx, MediaCodecDecContext *s, const char *mime, FFAMediaFormat *format)
static enum AVPixelFormat mcdec_map_color_format(AVCodecContext *avctx, MediaCodecDecContext *s, int color_format)
pixel format definitions
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
static int mediacodec_dec_parse_format(AVCodecContext *avctx, MediaCodecDecContext *s)
#define OUTPUT_DEQUEUE_TIMEOUT_US
FFAMediaFormat * ff_AMediaCodec_getOutputFormat(FFAMediaCodec *codec)
void * surface
android/view/Surface object reference.
Definition: mediacodec.h:38
AVHWAccel ff_hevc_mediacodec_hwaccel
int height
Definition: frame.h:239
#define atomic_init(obj, value)
Definition: stdatomic.h:33
#define av_freep(p)
static int mediacodec_wrap_hw_buffer(AVCodecContext *avctx, MediaCodecDecContext *s, ssize_t index, FFAMediaCodecBufferInfo *info, AVFrame *frame)
enum AVPixelFormat pix_fmt
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1634
static int mediacodec_dec_flush_codec(AVCodecContext *avctx, MediaCodecDecContext *s)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1650
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
GLuint buffer
Definition: opengl_enc.c:102