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/atomic.h"
27 #include "libavutil/common.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 {
98 };
99 
100 static const struct {
101 
104 
105 } color_formats[] = {
106 
114  { 0 }
115 };
116 
119  int color_format)
120 {
121  int i;
122  enum AVPixelFormat ret = AV_PIX_FMT_NONE;
123 
124  if (s->surface) {
125  return AV_PIX_FMT_MEDIACODEC;
126  }
127 
128  if (!strcmp(s->codec_name, "OMX.k3.video.decoder.avc") && color_format == COLOR_FormatYCbYCr) {
130  }
131 
132  for (i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) {
133  if (color_formats[i].color_format == color_format) {
134  return color_formats[i].pix_fmt;
135  }
136  }
137 
138  av_log(avctx, AV_LOG_ERROR, "Output color format 0x%x (value=%d) is not supported\n",
139  color_format, color_format);
140 
141  return ret;
142 }
143 
145 {
147 }
148 
150 {
151  if (!s)
152  return;
153 
155  if (s->codec) {
157  s->codec = NULL;
158  }
159 
160  if (s->format) {
162  s->format = NULL;
163  }
164 
165  if (s->surface) {
167  s->surface = NULL;
168  }
169 
170  av_freep(&s->codec_name);
171  av_freep(&s);
172  }
173 }
174 
175 static void mediacodec_buffer_release(void *opaque, uint8_t *data)
176 {
177  AVMediaCodecBuffer *buffer = opaque;
178  MediaCodecDecContext *ctx = buffer->ctx;
179  int released = avpriv_atomic_int_get(&buffer->released);
180 
181  if (!released) {
182  ff_AMediaCodec_releaseOutputBuffer(ctx->codec, buffer->index, 0);
183  }
184 
186  av_freep(&buffer);
187 }
188 
191  ssize_t index,
193  AVFrame *frame)
194 {
195  int ret = 0;
196  int status = 0;
197  AVMediaCodecBuffer *buffer = NULL;
198 
199  frame->buf[0] = NULL;
200  frame->width = avctx->width;
201  frame->height = avctx->height;
202  frame->format = avctx->pix_fmt;
203 
204  if (avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
205  frame->pts = av_rescale_q(info->presentationTimeUs,
206  av_make_q(1, 1000000),
207  avctx->pkt_timebase);
208  } else {
209  frame->pts = info->presentationTimeUs;
210  }
211 #if FF_API_PKT_PTS
213  frame->pkt_pts = frame->pts;
215 #endif
216  frame->pkt_dts = AV_NOPTS_VALUE;
217 
218  buffer = av_mallocz(sizeof(AVMediaCodecBuffer));
219  if (!buffer) {
220  ret = AVERROR(ENOMEM);
221  goto fail;
222  }
223 
224  buffer->released = 0;
225 
226  frame->buf[0] = av_buffer_create(NULL,
227  0,
229  buffer,
231 
232  if (!frame->buf[0]) {
233  ret = AVERROR(ENOMEM);
234  goto fail;
235 
236  }
237 
238  buffer->ctx = s;
240 
241  buffer->index = index;
242  buffer->pts = info->presentationTimeUs;
243 
244  frame->data[3] = (uint8_t *)buffer;
245 
246  return 0;
247 fail:
248  av_freep(buffer);
249  av_buffer_unref(&frame->buf[0]);
250  status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
251  if (status < 0) {
252  av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
253  ret = AVERROR_EXTERNAL;
254  }
255 
256  return ret;
257 }
258 
261  uint8_t *data,
262  size_t size,
263  ssize_t index,
265  AVFrame *frame)
266 {
267  int ret = 0;
268  int status = 0;
269 
270  frame->width = avctx->width;
271  frame->height = avctx->height;
272  frame->format = avctx->pix_fmt;
273 
274  /* MediaCodec buffers needs to be copied to our own refcounted buffers
275  * because the flush command invalidates all input and output buffers.
276  */
277  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
278  av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer\n");
279  goto done;
280  }
281 
282  /* Override frame->pkt_pts as ff_get_buffer will override its value based
283  * on the last avpacket received which is not in sync with the frame:
284  * * N avpackets can be pushed before 1 frame is actually returned
285  * * 0-sized avpackets are pushed to flush remaining frames at EOS */
286  frame->pts = info->presentationTimeUs;
287 #if FF_API_PKT_PTS
289  frame->pkt_pts = info->presentationTimeUs;
291 #endif
292  frame->pkt_dts = AV_NOPTS_VALUE;
293 
294  av_log(avctx, AV_LOG_DEBUG,
295  "Frame: width=%d stride=%d height=%d slice-height=%d "
296  "crop-top=%d crop-bottom=%d crop-left=%d crop-right=%d encoder=%s\n"
297  "destination linesizes=%d,%d,%d\n" ,
298  avctx->width, s->stride, avctx->height, s->slice_height,
300  frame->linesize[0], frame->linesize[1], frame->linesize[2]);
301 
302  switch (s->color_format) {
304  ff_mediacodec_sw_buffer_copy_yuv420_planar(avctx, s, data, size, info, frame);
305  break;
309  ff_mediacodec_sw_buffer_copy_yuv420_semi_planar(avctx, s, data, size, info, frame);
310  break;
313  ff_mediacodec_sw_buffer_copy_yuv420_packed_semi_planar(avctx, s, data, size, info, frame);
314  break;
317  break;
318  default:
319  av_log(avctx, AV_LOG_ERROR, "Unsupported color format 0x%x (value=%d)\n",
320  s->color_format, s->color_format);
321  ret = AVERROR(EINVAL);
322  goto done;
323  }
324 
325  ret = 0;
326 done:
327  status = ff_AMediaCodec_releaseOutputBuffer(s->codec, index, 0);
328  if (status < 0) {
329  av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
330  ret = AVERROR_EXTERNAL;
331  }
332 
333  return ret;
334 }
335 
337 {
338  int width = 0;
339  int height = 0;
340  int32_t value = 0;
341  char *format = NULL;
342 
343  if (!s->format) {
344  av_log(avctx, AV_LOG_ERROR, "Output MediaFormat is not set\n");
345  return AVERROR(EINVAL);
346  }
347 
348  format = ff_AMediaFormat_toString(s->format);
349  if (!format) {
350  return AVERROR_EXTERNAL;
351  }
352  av_log(avctx, AV_LOG_DEBUG, "Parsing MediaFormat %s\n", format);
353  av_freep(&format);
354 
355  /* Mandatory fields */
356  if (!ff_AMediaFormat_getInt32(s->format, "width", &value)) {
357  format = ff_AMediaFormat_toString(s->format);
358  av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "width", format);
359  av_freep(&format);
360  return AVERROR_EXTERNAL;
361  }
362  s->width = value;
363 
364  if (!ff_AMediaFormat_getInt32(s->format, "height", &value)) {
365  format = ff_AMediaFormat_toString(s->format);
366  av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "height", format);
367  av_freep(&format);
368  return AVERROR_EXTERNAL;
369  }
370  s->height = value;
371 
372  if (!ff_AMediaFormat_getInt32(s->format, "stride", &value)) {
373  format = ff_AMediaFormat_toString(s->format);
374  av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "stride", format);
375  av_freep(&format);
376  return AVERROR_EXTERNAL;
377  }
378  s->stride = value > 0 ? value : s->width;
379 
380  if (!ff_AMediaFormat_getInt32(s->format, "slice-height", &value)) {
381  format = ff_AMediaFormat_toString(s->format);
382  av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "slice-height", format);
383  av_freep(&format);
384  return AVERROR_EXTERNAL;
385  }
386  s->slice_height = value > 0 ? value : s->height;
387 
388  if (strstr(s->codec_name, "OMX.Nvidia.")) {
389  s->slice_height = FFALIGN(s->height, 16);
390  } else if (strstr(s->codec_name, "OMX.SEC.avc.dec")) {
391  s->slice_height = avctx->height;
392  s->stride = avctx->width;
393  }
394 
395  if (!ff_AMediaFormat_getInt32(s->format, "color-format", &value)) {
396  format = ff_AMediaFormat_toString(s->format);
397  av_log(avctx, AV_LOG_ERROR, "Could not get %s from format %s\n", "color-format", format);
398  av_freep(&format);
399  return AVERROR_EXTERNAL;
400  }
401  s->color_format = value;
402 
403  s->pix_fmt = avctx->pix_fmt = mcdec_map_color_format(avctx, s, value);
404  if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
405  av_log(avctx, AV_LOG_ERROR, "Output color format is not supported\n");
406  return AVERROR(EINVAL);
407  }
408 
409  /* Optional fields */
410  if (ff_AMediaFormat_getInt32(s->format, "crop-top", &value))
411  s->crop_top = value;
412 
413  if (ff_AMediaFormat_getInt32(s->format, "crop-bottom", &value))
414  s->crop_bottom = value;
415 
416  if (ff_AMediaFormat_getInt32(s->format, "crop-left", &value))
417  s->crop_left = value;
418 
419  if (ff_AMediaFormat_getInt32(s->format, "crop-right", &value))
420  s->crop_right = value;
421 
422  width = s->crop_right + 1 - s->crop_left;
423  height = s->crop_bottom + 1 - s->crop_top;
424 
425  av_log(avctx, AV_LOG_INFO,
426  "Output crop parameters top=%d bottom=%d left=%d right=%d, "
427  "resulting dimensions width=%d height=%d\n",
428  s->crop_top, s->crop_bottom, s->crop_left, s->crop_right,
429  width, height);
430 
431  return ff_set_dimensions(avctx, width, height);
432 }
433 
434 
436 {
437  FFAMediaCodec *codec = s->codec;
438  int status;
439 
440  s->output_buffer_count = 0;
441 
442  s->draining = 0;
443  s->flushing = 0;
444  s->eos = 0;
445 
446  status = ff_AMediaCodec_flush(codec);
447  if (status < 0) {
448  av_log(avctx, AV_LOG_ERROR, "Failed to flush codec\n");
449  return AVERROR_EXTERNAL;
450  }
451 
452  return 0;
453 }
454 
456  const char *mime, FFAMediaFormat *format)
457 {
458  int ret = 0;
459  int status;
460  int profile;
461 
462  enum AVPixelFormat pix_fmt;
463  static const enum AVPixelFormat pix_fmts[] = {
466  };
467 
468  s->refcount = 1;
469 
470  pix_fmt = ff_get_format(avctx, pix_fmts);
471  if (pix_fmt == AV_PIX_FMT_MEDIACODEC) {
472  AVMediaCodecContext *user_ctx = avctx->hwaccel_context;
473 
474  if (user_ctx && user_ctx->surface) {
475  s->surface = ff_mediacodec_surface_ref(user_ctx->surface, avctx);
476  av_log(avctx, AV_LOG_INFO, "Using surface %p\n", s->surface);
477  }
478  }
479 
481  if (profile < 0) {
482  av_log(avctx, AV_LOG_WARNING, "Unsupported or unknown profile");
483  }
484 
485  s->codec_name = ff_AMediaCodecList_getCodecNameByType(mime, profile, 0, avctx);
486  if (!s->codec_name) {
487  ret = AVERROR_EXTERNAL;
488  goto fail;
489  }
490 
491  av_log(avctx, AV_LOG_DEBUG, "Found decoder %s\n", s->codec_name);
493  if (!s->codec) {
494  av_log(avctx, AV_LOG_ERROR, "Failed to create media decoder for type %s and name %s\n", mime, s->codec_name);
495  ret = AVERROR_EXTERNAL;
496  goto fail;
497  }
498 
499  status = ff_AMediaCodec_configure(s->codec, format, s->surface, NULL, 0);
500  if (status < 0) {
501  char *desc = ff_AMediaFormat_toString(format);
502  av_log(avctx, AV_LOG_ERROR,
503  "Failed to configure codec (status = %d) with format %s\n",
504  status, desc);
505  av_freep(&desc);
506 
507  ret = AVERROR_EXTERNAL;
508  goto fail;
509  }
510 
511  status = ff_AMediaCodec_start(s->codec);
512  if (status < 0) {
513  char *desc = ff_AMediaFormat_toString(format);
514  av_log(avctx, AV_LOG_ERROR,
515  "Failed to start codec (status = %d) with format %s\n",
516  status, desc);
517  av_freep(&desc);
518  ret = AVERROR_EXTERNAL;
519  goto fail;
520  }
521 
523  if (s->format) {
524  if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
525  av_log(avctx, AV_LOG_ERROR,
526  "Failed to configure context\n");
527  goto fail;
528  }
529  }
530 
531  av_log(avctx, AV_LOG_DEBUG, "MediaCodec %p started successfully\n", s->codec);
532 
533  return 0;
534 
535 fail:
536  av_log(avctx, AV_LOG_ERROR, "MediaCodec %p failed to start\n", s->codec);
537  ff_mediacodec_dec_close(avctx, s);
538  return ret;
539 }
540 
542  AVFrame *frame, int *got_frame,
543  AVPacket *pkt)
544 {
545  int ret;
546  int offset = 0;
547  int need_draining = 0;
548  uint8_t *data;
549  ssize_t index;
550  size_t size;
551  FFAMediaCodec *codec = s->codec;
552  FFAMediaCodecBufferInfo info = { 0 };
553 
554  int status;
555 
556  int64_t input_dequeue_timeout_us = INPUT_DEQUEUE_TIMEOUT_US;
557  int64_t output_dequeue_timeout_us = OUTPUT_DEQUEUE_TIMEOUT_US;
558 
559  if (s->flushing) {
560  av_log(avctx, AV_LOG_ERROR, "Decoder is flushing and cannot accept new buffer "
561  "until all output buffers have been released\n");
562  return AVERROR_EXTERNAL;
563  }
564 
565  if (pkt->size == 0) {
566  need_draining = 1;
567  }
568 
569  if (s->draining && s->eos) {
570  return 0;
571  }
572 
573  while (offset < pkt->size || (need_draining && !s->draining)) {
574 
575  index = ff_AMediaCodec_dequeueInputBuffer(codec, input_dequeue_timeout_us);
576  if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
577  break;
578  }
579 
580  if (index < 0) {
581  av_log(avctx, AV_LOG_ERROR, "Failed to dequeue input buffer (status=%zd)\n", index);
582  return AVERROR_EXTERNAL;
583  }
584 
585  data = ff_AMediaCodec_getInputBuffer(codec, index, &size);
586  if (!data) {
587  av_log(avctx, AV_LOG_ERROR, "Failed to get input buffer\n");
588  return AVERROR_EXTERNAL;
589  }
590 
591  if (need_draining) {
592  int64_t pts = pkt->pts;
594 
595  if (s->surface) {
596  pts = av_rescale_q(pts, avctx->pkt_timebase, av_make_q(1, 1000000));
597  }
598 
599  av_log(avctx, AV_LOG_DEBUG, "Sending End Of Stream signal\n");
600 
601  status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, 0, pts, flags);
602  if (status < 0) {
603  av_log(avctx, AV_LOG_ERROR, "Failed to queue input empty buffer (status = %d)\n", status);
604  return AVERROR_EXTERNAL;
605  }
606 
607  s->draining = 1;
608  break;
609  } else {
610  int64_t pts = pkt->pts;
611 
612  size = FFMIN(pkt->size - offset, size);
613 
614  memcpy(data, pkt->data + offset, size);
615  offset += size;
616 
617  if (s->surface && avctx->pkt_timebase.num && avctx->pkt_timebase.den) {
618  pts = av_rescale_q(pts, avctx->pkt_timebase, av_make_q(1, 1000000));
619  }
620 
621  status = ff_AMediaCodec_queueInputBuffer(codec, index, 0, size, pts, 0);
622  if (status < 0) {
623  av_log(avctx, AV_LOG_ERROR, "Failed to queue input buffer (status = %d)\n", status);
624  return AVERROR_EXTERNAL;
625  }
626  }
627  }
628 
629  if (need_draining || s->draining) {
630  /* If the codec is flushing or need to be flushed, block for a fair
631  * amount of time to ensure we got a frame */
632  output_dequeue_timeout_us = OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US;
633  } else if (s->output_buffer_count == 0) {
634  /* If the codec hasn't produced any frames, do not block so we
635  * can push data to it as fast as possible, and get the first
636  * frame */
637  output_dequeue_timeout_us = 0;
638  }
639 
640  index = ff_AMediaCodec_dequeueOutputBuffer(codec, &info, output_dequeue_timeout_us);
641  if (index >= 0) {
642  int ret;
643 
644  av_log(avctx, AV_LOG_DEBUG, "Got output buffer %zd"
645  " offset=%" PRIi32 " size=%" PRIi32 " ts=%" PRIi64
646  " flags=%" PRIu32 "\n", index, info.offset, info.size,
647  info.presentationTimeUs, info.flags);
648 
650  s->eos = 1;
651  }
652 
653  if (info.size) {
654  if (s->surface) {
655  if ((ret = mediacodec_wrap_hw_buffer(avctx, s, index, &info, frame)) < 0) {
656  av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
657  return ret;
658  }
659  } else {
660  data = ff_AMediaCodec_getOutputBuffer(codec, index, &size);
661  if (!data) {
662  av_log(avctx, AV_LOG_ERROR, "Failed to get output buffer\n");
663  return AVERROR_EXTERNAL;
664  }
665 
666  if ((ret = mediacodec_wrap_sw_buffer(avctx, s, data, size, index, &info, frame)) < 0) {
667  av_log(avctx, AV_LOG_ERROR, "Failed to wrap MediaCodec buffer\n");
668  return ret;
669  }
670  }
671 
672  *got_frame = 1;
673  s->output_buffer_count++;
674  } else {
675  status = ff_AMediaCodec_releaseOutputBuffer(codec, index, 0);
676  if (status < 0) {
677  av_log(avctx, AV_LOG_ERROR, "Failed to release output buffer\n");
678  }
679  }
680 
681  } else if (ff_AMediaCodec_infoOutputFormatChanged(codec, index)) {
682  char *format = NULL;
683 
684  if (s->format) {
685  status = ff_AMediaFormat_delete(s->format);
686  if (status < 0) {
687  av_log(avctx, AV_LOG_ERROR, "Failed to delete MediaFormat %p\n", s->format);
688  }
689  }
690 
692  if (!s->format) {
693  av_log(avctx, AV_LOG_ERROR, "Failed to get output format\n");
694  return AVERROR_EXTERNAL;
695  }
696 
697  format = ff_AMediaFormat_toString(s->format);
698  if (!format) {
699  return AVERROR_EXTERNAL;
700  }
701  av_log(avctx, AV_LOG_INFO, "Output MediaFormat changed to %s\n", format);
702  av_freep(&format);
703 
704  if ((ret = mediacodec_dec_parse_format(avctx, s)) < 0) {
705  return ret;
706  }
707 
708  } else if (ff_AMediaCodec_infoOutputBuffersChanged(codec, index)) {
710  } else if (ff_AMediaCodec_infoTryAgainLater(codec, index)) {
711  if (s->draining) {
712  av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer within %" PRIi64 "ms "
713  "while draining remaining frames, output will probably lack frames\n",
714  output_dequeue_timeout_us / 1000);
715  } else {
716  av_log(avctx, AV_LOG_DEBUG, "No output buffer available, try again later\n");
717  }
718  } else {
719  av_log(avctx, AV_LOG_ERROR, "Failed to dequeue output buffer (status=%zd)\n", index);
720  return AVERROR_EXTERNAL;
721  }
722 
723  return offset;
724 }
725 
727 {
728  if (!s->surface || avpriv_atomic_int_get(&s->refcount) == 1) {
729  int ret;
730 
731  /* No frames (holding a reference to the codec) are retained by the
732  * user, thus we can flush the codec and returns accordingly */
733  if ((ret = mediacodec_dec_flush_codec(avctx, s)) < 0) {
734  return ret;
735  }
736 
737  return 1;
738  }
739 
740  s->flushing = 1;
741  return 0;
742 }
743 
745 {
747 
748  return 0;
749 }
750 
752 {
753  return s->flushing;
754 }
755 
757  .name = "mediacodec",
758  .type = AVMEDIA_TYPE_VIDEO,
759  .id = AV_CODEC_ID_H264,
760  .pix_fmt = AV_PIX_FMT_MEDIACODEC,
761 };
762 
764  .name = "mediacodec",
765  .type = AVMEDIA_TYPE_VIDEO,
766  .id = AV_CODEC_ID_HEVC,
767  .pix_fmt = AV_PIX_FMT_MEDIACODEC,
768 };
769 
771  .name = "mediacodec",
772  .type = AVMEDIA_TYPE_VIDEO,
773  .id = AV_CODEC_ID_MPEG4,
774  .pix_fmt = AV_PIX_FMT_MEDIACODEC,
775 };
776 
778  .name = "mediacodec",
779  .type = AVMEDIA_TYPE_VIDEO,
780  .id = AV_CODEC_ID_VP8,
781  .pix_fmt = AV_PIX_FMT_MEDIACODEC,
782 };
783 
785  .name = "mediacodec",
786  .type = AVMEDIA_TYPE_VIDEO,
787  .id = AV_CODEC_ID_VP9,
788  .pix_fmt = AV_PIX_FMT_MEDIACODEC,
789 };
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)
#define avpriv_atomic_int_add_and_fetch
Definition: atomic_gcc.h:50
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:124
This structure describes decoded (raw) audio or video data.
Definition: frame.h:184
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:367
const char * desc
Definition: nvenc.c:101
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:1602
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1904
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:252
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:3008
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:268
static AVFrame * frame
#define height
int ff_AMediaCodec_infoOutputFormatChanged(FFAMediaCodec *codec, ssize_t idx)
uint8_t * data
Definition: avcodec.h:1601
#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:3391
int width
width and height of the video frame
Definition: frame.h:236
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define avpriv_atomic_int_get
Definition: atomic_gcc.h:28
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
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define fail()
Definition: checkasm.h:83
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:3726
#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:1863
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:1111
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:248
int ff_AMediaFormat_getInt32(FFAMediaFormat *format, const char *name, int32_t *out)
#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:215
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition: rational.h:71
main external API structure.
Definition: avcodec.h:1676
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:947
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:42
#define OUTPUT_DEQUEUE_BLOCK_TIMEOUT_US
hardware decoding through MediaCodec
Definition: pixfmt.h:307
static int64_t pts
Global timestamp for the audio frames.
static int flags
Definition: cpu.c:47
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:198
attribute_deprecated int64_t pkt_pts
PTS copied from the AVPacket that was decoded to produce this frame.
Definition: frame.h:276
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:284
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:80
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:81
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:236
#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:1578
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:1594
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:242
GLuint buffer
Definition: opengl_enc.c:102