FFmpeg
mediacodecenc.c
Go to the documentation of this file.
1 /*
2  * Android MediaCodec encoders
3  *
4  * Copyright (c) 2022 Zhao Zhili <zhilizhao@tencent.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 "config_components.h"
24 
25 #include "libavutil/avassert.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/opt.h"
29 
30 #include "avcodec.h"
31 #include "bsf.h"
32 #include "codec_internal.h"
33 #include "encode.h"
34 #include "hwconfig.h"
35 #include "jni.h"
36 #include "mediacodec.h"
37 #include "mediacodec_wrapper.h"
38 #include "mediacodecdec_common.h"
39 
40 #define INPUT_DEQUEUE_TIMEOUT_US 8000
41 #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
42 
44  /* Constant quality mode */
46  /* Variable bitrate mode */
48  /* Constant bitrate mode */
50  /* Constant bitrate mode with frame drops */
52 };
53 
54 typedef struct MediaCodecEncContext {
58  const char *name;
60 
61  int fps;
62  int width;
63  int height;
64 
65  uint8_t *extradata;
67  int eof_sent;
68 
71 
73  int level;
76 
77 enum {
80  COLOR_FormatSurface = 0x7F000789,
81 };
82 
83 static const struct {
86 } color_formats[] = {
90 };
91 
92 static const enum AVPixelFormat avc_pix_fmts[] = {
97 };
98 
100 {
101  MediaCodecEncContext *s = avctx->priv_data;
102  char *name = ff_AMediaCodec_getName(s->codec);
103  FFAMediaFormat *out_format = ff_AMediaCodec_getOutputFormat(s->codec);
104  char *str = ff_AMediaFormat_toString(out_format);
105 
106  av_log(avctx, AV_LOG_DEBUG, "MediaCodec encoder %s output format %s\n",
107  name ? name : "unknown", str);
108  av_free(name);
109  av_free(str);
110  ff_AMediaFormat_delete(out_format);
111 }
112 
114 {
115  MediaCodecEncContext *s = avctx->priv_data;
116  char str[128];
117  int ret;
118  int crop_right = s->width - avctx->width;
119  int crop_bottom = s->height - avctx->height;
120 
121  if (!crop_right && !crop_bottom)
122  return 0;
123 
124  if (avctx->codec_id == AV_CODEC_ID_H264)
125  ret = snprintf(str, sizeof(str), "h264_metadata=crop_right=%d:crop_bottom=%d",
126  crop_right, crop_bottom);
127  else if (avctx->codec_id == AV_CODEC_ID_HEVC)
128  ret = snprintf(str, sizeof(str), "hevc_metadata=crop_right=%d:crop_bottom=%d",
129  crop_right, crop_bottom);
130  else
131  return 0;
132 
133  if (ret >= sizeof(str))
135 
136  ret = av_bsf_list_parse_str(str, &s->bsf);
137  if (ret < 0)
138  return ret;
139 
140  ret = avcodec_parameters_from_context(s->bsf->par_in, avctx);
141  if (ret < 0)
142  return ret;
143  s->bsf->time_base_in = avctx->time_base;
144  ret = av_bsf_init(s->bsf);
145 
146  return ret;
147 }
148 
150 {
151  const char *codec_mime = NULL;
152  MediaCodecEncContext *s = avctx->priv_data;
154  int ret;
155  int gop;
156 
157  if (s->use_ndk_codec < 0)
158  s->use_ndk_codec = !av_jni_get_java_vm(avctx);
159 
160  switch (avctx->codec_id) {
161  case AV_CODEC_ID_H264:
162  codec_mime = "video/avc";
163  break;
164  case AV_CODEC_ID_HEVC:
165  codec_mime = "video/hevc";
166  break;
167  case AV_CODEC_ID_VP8:
168  codec_mime = "video/x-vnd.on2.vp8";
169  break;
170  case AV_CODEC_ID_VP9:
171  codec_mime = "video/x-vnd.on2.vp9";
172  break;
173  case AV_CODEC_ID_MPEG4:
174  codec_mime = "video/mp4v-es";
175  break;
176  case AV_CODEC_ID_AV1:
177  codec_mime = "video/av01";
178  break;
179  default:
180  av_assert0(0);
181  }
182 
183  if (s->name)
184  s->codec = ff_AMediaCodec_createCodecByName(s->name, s->use_ndk_codec);
185  else
186  s->codec = ff_AMediaCodec_createEncoderByType(codec_mime, s->use_ndk_codec);
187  if (!s->codec) {
188  av_log(avctx, AV_LOG_ERROR, "Failed to create encoder for type %s\n",
189  codec_mime);
190  return AVERROR_EXTERNAL;
191  }
192 
193  format = ff_AMediaFormat_new(s->use_ndk_codec);
194  if (!format) {
195  av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
196  return AVERROR_EXTERNAL;
197  }
198 
199  ff_AMediaFormat_setString(format, "mime", codec_mime);
200  // Workaround the alignment requirement of mediacodec. We can't do it
201  // silently for AV_PIX_FMT_MEDIACODEC.
202  if (avctx->pix_fmt != AV_PIX_FMT_MEDIACODEC) {
203  s->width = FFALIGN(avctx->width, 16);
204  s->height = FFALIGN(avctx->height, 16);
205  } else {
206  s->width = avctx->width;
207  s->height = avctx->height;
208  if (s->width % 16 || s->height % 16)
209  av_log(avctx, AV_LOG_WARNING,
210  "Video size %dx%d isn't align to 16, it may have device compatibility issue\n",
211  s->width, s->height);
212  }
213  ff_AMediaFormat_setInt32(format, "width", s->width);
214  ff_AMediaFormat_setInt32(format, "height", s->height);
215 
216  if (avctx->pix_fmt == AV_PIX_FMT_MEDIACODEC) {
217  AVMediaCodecContext *user_ctx = avctx->hwaccel_context;
218  if (avctx->hw_device_ctx) {
219  AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)(avctx->hw_device_ctx->data);
220  AVMediaCodecDeviceContext *dev_ctx;
221 
222  if (device_ctx->type != AV_HWDEVICE_TYPE_MEDIACODEC || !device_ctx->hwctx) {
223  ret = AVERROR(EINVAL);
224  goto bailout;
225  }
226  dev_ctx = device_ctx->hwctx;
227  s->window = ff_mediacodec_surface_ref(dev_ctx->surface, dev_ctx->native_window, avctx);
228  }
229 
230  if (!s->window && user_ctx && user_ctx->surface)
231  s->window = ff_mediacodec_surface_ref(user_ctx->surface, NULL, avctx);
232 
233  if (!s->window) {
234  ret = AVERROR(EINVAL);
235  av_log(avctx, AV_LOG_ERROR, "Missing hw_device_ctx or hwaccel_context for AV_PIX_FMT_MEDIACODEC\n");
236  goto bailout;
237  }
238  /* Although there is a method ANativeWindow_toSurface() introduced in
239  * API level 26, it's easier and safe to always require a Surface for
240  * Java MediaCodec.
241  */
242  if (!s->use_ndk_codec && !s->window->surface) {
243  ret = AVERROR(EINVAL);
244  av_log(avctx, AV_LOG_ERROR, "Missing jobject Surface for AV_PIX_FMT_MEDIACODEC. "
245  "Please note that Java MediaCodec doesn't work with ANativeWindow.\n");
246  goto bailout;
247  }
248  }
249 
250  for (int i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) {
251  if (avctx->pix_fmt == color_formats[i].pix_fmt) {
252  ff_AMediaFormat_setInt32(format, "color-format",
254  break;
255  }
256  }
257 
260  ff_AMediaFormat_setInt32(format, "color-range", ret);
263  ff_AMediaFormat_setInt32(format, "color-standard", ret);
266  ff_AMediaFormat_setInt32(format, "color-transfer", ret);
267 
268  if (avctx->bit_rate)
269  ff_AMediaFormat_setInt32(format, "bitrate", avctx->bit_rate);
270  if (s->bitrate_mode >= 0)
271  ff_AMediaFormat_setInt32(format, "bitrate-mode", s->bitrate_mode);
272  // frame-rate and i-frame-interval are required to configure codec
273  if (avctx->framerate.num >= avctx->framerate.den && avctx->framerate.den > 0) {
274  s->fps = avctx->framerate.num / avctx->framerate.den;
275  } else {
276  s->fps = 30;
277  av_log(avctx, AV_LOG_INFO, "Use %d as the default MediaFormat frame-rate\n", s->fps);
278  }
279  gop = round(avctx->gop_size / s->fps);
280  if (gop == 0) {
281  gop = 1;
282  av_log(avctx, AV_LOG_INFO,
283  "Use %d as the default MediaFormat i-frame-interval, "
284  "please set gop_size properly (>= fps)\n", gop);
285  } else {
286  av_log(avctx, AV_LOG_DEBUG, "Set i-frame-interval to %d\n", gop);
287  }
288 
289  ff_AMediaFormat_setInt32(format, "frame-rate", s->fps);
290  ff_AMediaFormat_setInt32(format, "i-frame-interval", gop);
291 
293  if (ret > 0) {
294  av_log(avctx, AV_LOG_DEBUG, "set profile to 0x%x\n", ret);
295  ff_AMediaFormat_setInt32(format, "profile", ret);
296  }
297  if (s->level > 0) {
298  av_log(avctx, AV_LOG_DEBUG, "set level to 0x%x\n", s->level);
299  ff_AMediaFormat_setInt32(format, "level", s->level);
300  }
301  if (avctx->max_b_frames > 0) {
303  av_log(avctx, AV_LOG_ERROR,
304  "Enabling B frames will produce packets with no DTS. "
305  "Use -strict experimental to use it anyway.\n");
306  ret = AVERROR(EINVAL);
307  goto bailout;
308  }
309  ff_AMediaFormat_setInt32(format, "max-bframes", avctx->max_b_frames);
310  }
311  if (s->pts_as_dts == -1)
312  s->pts_as_dts = avctx->max_b_frames <= 0;
313 
315  ret = ff_AMediaCodec_configure(s->codec, format, s->window, NULL, ret);
316  if (ret) {
317  av_log(avctx, AV_LOG_ERROR, "MediaCodec configure failed, %s\n", av_err2str(ret));
318  goto bailout;
319  }
320 
321  ret = ff_AMediaCodec_start(s->codec);
322  if (ret) {
323  av_log(avctx, AV_LOG_ERROR, "MediaCodec failed to start, %s\n", av_err2str(ret));
324  goto bailout;
325  }
326 
327  ret = mediacodec_init_bsf(avctx);
328  if (ret)
329  goto bailout;
330 
332  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)
333  av_log(avctx, AV_LOG_WARNING,
334  "Mediacodec encoder doesn't support AV_CODEC_FLAG_GLOBAL_HEADER. "
335  "Use extract_extradata bsf when necessary.\n");
336 
337  s->frame = av_frame_alloc();
338  if (!s->frame)
339  ret = AVERROR(ENOMEM);
340 
341 bailout:
342  if (format)
344  return ret;
345 }
346 
348  AVPacket *pkt,
349  int *got_packet)
350 {
351  MediaCodecEncContext *s = avctx->priv_data;
352  FFAMediaCodec *codec = s->codec;
353  FFAMediaCodecBufferInfo out_info = {0};
354  uint8_t *out_buf;
355  size_t out_size = 0;
356  int ret;
357  int extradata_size = 0;
358  int64_t timeout_us = s->eof_sent ? OUTPUT_DEQUEUE_TIMEOUT_US : 0;
359  ssize_t index = ff_AMediaCodec_dequeueOutputBuffer(codec, &out_info, timeout_us);
360 
362  return AVERROR(EAGAIN);
363 
366  return AVERROR(EAGAIN);
367  }
368 
371  return AVERROR(EAGAIN);
372  }
373 
374  if (index < 0)
375  return AVERROR_EXTERNAL;
376 
377  if (out_info.flags & ff_AMediaCodec_getBufferFlagEndOfStream(codec))
378  return AVERROR_EOF;
379 
380  out_buf = ff_AMediaCodec_getOutputBuffer(codec, index, &out_size);
381  if (!out_buf) {
383  goto bailout;
384  }
385 
386  if (out_info.flags & ff_AMediaCodec_getBufferFlagCodecConfig(codec)) {
387  ret = av_reallocp(&s->extradata, out_info.size);
388  if (ret)
389  goto bailout;
390 
391  s->extradata_size = out_info.size;
392  memcpy(s->extradata, out_buf + out_info.offset, out_info.size);
394  // try immediately
395  return mediacodec_receive(avctx, pkt, got_packet);
396  }
397 
398  ret = ff_get_encode_buffer(avctx, pkt, out_info.size + s->extradata_size, 0);
399  if (ret < 0)
400  goto bailout;
401 
402  if (s->extradata_size) {
403  extradata_size = s->extradata_size;
404  s->extradata_size = 0;
405  memcpy(pkt->data, s->extradata, extradata_size);
406  }
407  memcpy(pkt->data + extradata_size, out_buf + out_info.offset, out_info.size);
409  if (s->pts_as_dts)
410  pkt->dts = pkt->pts;
411  if (out_info.flags & ff_AMediaCodec_getBufferFlagKeyFrame(codec))
413  ret = 0;
414  *got_packet = 1;
415 
416  av_log(avctx, AV_LOG_TRACE, "receive packet pts %" PRId64 " dts %" PRId64
417  " flags %d extradata %d\n",
418  pkt->pts, pkt->dts, pkt->flags, extradata_size);
419 
420 bailout:
422  return ret;
423 }
424 
425 static void copy_frame_to_buffer(AVCodecContext *avctx, const AVFrame *frame, uint8_t *dst, size_t size)
426 {
427  MediaCodecEncContext *s = avctx->priv_data;
428  uint8_t *dst_data[4] = {};
429  int dst_linesize[4] = {};
430  const uint8_t *src_data[4] = {
431  frame->data[0], frame->data[1], frame->data[2], frame->data[3]
432  };
433 
434  if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
435  dst_data[0] = dst;
436  dst_data[1] = dst + s->width * s->height;
437  dst_data[2] = dst_data[1] + s->width * s->height / 4;
438 
439  dst_linesize[0] = s->width;
440  dst_linesize[1] = dst_linesize[2] = s->width / 2;
441  } else if (avctx->pix_fmt == AV_PIX_FMT_NV12) {
442  dst_data[0] = dst;
443  dst_data[1] = dst + s->width * s->height;
444 
445  dst_linesize[0] = s->width;
446  dst_linesize[1] = s->width;
447  } else {
448  av_assert0(0);
449  }
450 
451  av_image_copy(dst_data, dst_linesize, src_data, frame->linesize,
452  avctx->pix_fmt, avctx->width, avctx->height);
453 }
454 
455 static int mediacodec_send(AVCodecContext *avctx,
456  const AVFrame *frame) {
457  MediaCodecEncContext *s = avctx->priv_data;
458  FFAMediaCodec *codec = s->codec;
459  ssize_t index;
460  uint8_t *input_buf = NULL;
461  size_t input_size = 0;
462  int64_t pts = 0;
463  uint32_t flags = 0;
464  int64_t timeout_us;
465 
466  if (s->eof_sent)
467  return 0;
468 
469  if (s->window) {
470  if (!frame) {
471  s->eof_sent = 1;
473  }
474 
475  if (frame->data[3])
476  av_mediacodec_release_buffer((AVMediaCodecBuffer *)frame->data[3], 1);
477  return 0;
478  }
479 
480  timeout_us = INPUT_DEQUEUE_TIMEOUT_US;
481  index = ff_AMediaCodec_dequeueInputBuffer(codec, timeout_us);
483  return AVERROR(EAGAIN);
484 
485  if (index < 0) {
486  av_log(avctx, AV_LOG_ERROR, "dequeue input buffer failed, %zd", index);
487  return AVERROR_EXTERNAL;
488  }
489 
490  if (frame) {
491  input_buf = ff_AMediaCodec_getInputBuffer(codec, index, &input_size);
492  copy_frame_to_buffer(avctx, frame, input_buf, input_size);
493 
494  pts = av_rescale_q(frame->pts, avctx->time_base, AV_TIME_BASE_Q);
495  } else {
497  s->eof_sent = 1;
498  }
499 
500  ff_AMediaCodec_queueInputBuffer(codec, index, 0, input_size, pts, flags);
501  return 0;
502 }
503 
505 {
506  MediaCodecEncContext *s = avctx->priv_data;
507  int ret;
508  int got_packet = 0;
509 
510  // Return on three case:
511  // 1. Serious error
512  // 2. Got a packet success
513  // 3. No AVFrame is available yet (don't return if get_frame return EOF)
514  while (1) {
515  if (s->bsf) {
516  ret = av_bsf_receive_packet(s->bsf, pkt);
517  if (!ret)
518  return 0;
519  if (ret != AVERROR(EAGAIN))
520  return ret;
521  }
522 
523  ret = mediacodec_receive(avctx, pkt, &got_packet);
524  if (s->bsf) {
525  if (!ret || ret == AVERROR_EOF)
526  ret = av_bsf_send_packet(s->bsf, pkt);
527  } else {
528  if (!ret)
529  return 0;
530  }
531 
532  if (ret != AVERROR(EAGAIN))
533  return ret;
534 
535  if (!s->frame->buf[0]) {
536  ret = ff_encode_get_frame(avctx, s->frame);
537  if (ret && ret != AVERROR_EOF)
538  return ret;
539  }
540 
541  ret = mediacodec_send(avctx, s->frame->buf[0] ? s->frame : NULL);
542  if (!ret)
543  av_frame_unref(s->frame);
544  else if (ret != AVERROR(EAGAIN))
545  return ret;
546  }
547 
548  return 0;
549 }
550 
552 {
553  MediaCodecEncContext *s = avctx->priv_data;
554  if (s->codec) {
555  ff_AMediaCodec_stop(s->codec);
556  ff_AMediaCodec_delete(s->codec);
557  s->codec = NULL;
558  }
559 
560  if (s->window) {
561  ff_mediacodec_surface_unref(s->window, avctx);
562  s->window = NULL;
563  }
564 
565  av_bsf_free(&s->bsf);
566  av_frame_free(&s->frame);
567 
568  return 0;
569 }
570 
572  &(const AVCodecHWConfigInternal) {
573  .public = {
577  .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
578  },
579  .hwaccel = NULL,
580  },
581  NULL
582 };
583 
584 #define OFFSET(x) offsetof(MediaCodecEncContext, x)
585 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
586 #define COMMON_OPTION \
587  { "ndk_codec", "Use MediaCodec from NDK", \
588  OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
589  { "codec_name", "Select codec by name", \
590  OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, \
591  { "bitrate_mode", "Bitrate control method", \
592  OFFSET(bitrate_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "bitrate_mode" }, \
593  { "cq", "Constant quality mode", \
594  0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CQ}, 0, 0, VE, "bitrate_mode" }, \
595  { "vbr", "Variable bitrate mode", \
596  0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_VBR}, 0, 0, VE, "bitrate_mode" }, \
597  { "cbr", "Constant bitrate mode", \
598  0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, "bitrate_mode" }, \
599  { "cbr_fd", "Constant bitrate mode with frame drops", \
600  0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, VE, "bitrate_mode" }, \
601  { "pts_as_dts", "Use PTS as DTS. It is enabled automatically if avctx max_b_frames <= 0, " \
602  "since most of Android devices don't output B frames by default.", \
603  OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
604 
605 
606 #define MEDIACODEC_ENCODER_CLASS(name) \
607 static const AVClass name ## _mediacodec_class = { \
608  .class_name = #name "_mediacodec", \
609  .item_name = av_default_item_name, \
610  .option = name ## _options, \
611  .version = LIBAVUTIL_VERSION_INT, \
612 }; \
613 
614 #define DECLARE_MEDIACODEC_ENCODER(short_name, long_name, codec_id) \
615 MEDIACODEC_ENCODER_CLASS(short_name) \
616 const FFCodec ff_ ## short_name ## _mediacodec_encoder = { \
617  .p.name = #short_name "_mediacodec", \
618  CODEC_LONG_NAME(long_name " Android MediaCodec encoder"), \
619  .p.type = AVMEDIA_TYPE_VIDEO, \
620  .p.id = codec_id, \
621  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY \
622  | AV_CODEC_CAP_HARDWARE, \
623  .priv_data_size = sizeof(MediaCodecEncContext), \
624  .p.pix_fmts = avc_pix_fmts, \
625  .init = mediacodec_init, \
626  FF_CODEC_RECEIVE_PACKET_CB(mediacodec_encode), \
627  .close = mediacodec_close, \
628  .p.priv_class = &short_name ## _mediacodec_class, \
629  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
630  .p.wrapper_name = "mediacodec", \
631  .hw_configs = mediacodec_hw_configs, \
632 }; \
633 
634 #if CONFIG_H264_MEDIACODEC_ENCODER
635 
636 enum MediaCodecAvcLevel {
637  AVCLevel1 = 0x01,
638  AVCLevel1b = 0x02,
639  AVCLevel11 = 0x04,
640  AVCLevel12 = 0x08,
641  AVCLevel13 = 0x10,
642  AVCLevel2 = 0x20,
643  AVCLevel21 = 0x40,
644  AVCLevel22 = 0x80,
645  AVCLevel3 = 0x100,
646  AVCLevel31 = 0x200,
647  AVCLevel32 = 0x400,
648  AVCLevel4 = 0x800,
649  AVCLevel41 = 0x1000,
650  AVCLevel42 = 0x2000,
651  AVCLevel5 = 0x4000,
652  AVCLevel51 = 0x8000,
653  AVCLevel52 = 0x10000,
654  AVCLevel6 = 0x20000,
655  AVCLevel61 = 0x40000,
656  AVCLevel62 = 0x80000,
657 };
658 
659 static const AVOption h264_options[] = {
661  { "level", "Specify level",
662  OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, "level" },
663  { "1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel1 }, 0, 0, VE, "level" },
664  { "1b", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel1b }, 0, 0, VE, "level" },
665  { "1.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel11 }, 0, 0, VE, "level" },
666  { "1.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel12 }, 0, 0, VE, "level" },
667  { "1.3", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel13 }, 0, 0, VE, "level" },
668  { "2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel2 }, 0, 0, VE, "level" },
669  { "2.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel21 }, 0, 0, VE, "level" },
670  { "2.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel22 }, 0, 0, VE, "level" },
671  { "3", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel3 }, 0, 0, VE, "level" },
672  { "3.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel31 }, 0, 0, VE, "level" },
673  { "3.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel32 }, 0, 0, VE, "level" },
674  { "4", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel4 }, 0, 0, VE, "level" },
675  { "4.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel41 }, 0, 0, VE, "level" },
676  { "4.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel42 }, 0, 0, VE, "level" },
677  { "5", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel5 }, 0, 0, VE, "level" },
678  { "5.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel51 }, 0, 0, VE, "level" },
679  { "5.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel52 }, 0, 0, VE, "level" },
680  { "6.0", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel6 }, 0, 0, VE, "level" },
681  { "6.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel61 }, 0, 0, VE, "level" },
682  { "6.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel62 }, 0, 0, VE, "level" },
683  { NULL, }
684 };
685 
687 
688 #endif // CONFIG_H264_MEDIACODEC_ENCODER
689 
690 #if CONFIG_HEVC_MEDIACODEC_ENCODER
691 
692 enum MediaCodecHevcLevel {
693  HEVCMainTierLevel1 = 0x1,
694  HEVCHighTierLevel1 = 0x2,
695  HEVCMainTierLevel2 = 0x4,
696  HEVCHighTierLevel2 = 0x8,
697  HEVCMainTierLevel21 = 0x10,
698  HEVCHighTierLevel21 = 0x20,
699  HEVCMainTierLevel3 = 0x40,
700  HEVCHighTierLevel3 = 0x80,
701  HEVCMainTierLevel31 = 0x100,
702  HEVCHighTierLevel31 = 0x200,
703  HEVCMainTierLevel4 = 0x400,
704  HEVCHighTierLevel4 = 0x800,
705  HEVCMainTierLevel41 = 0x1000,
706  HEVCHighTierLevel41 = 0x2000,
707  HEVCMainTierLevel5 = 0x4000,
708  HEVCHighTierLevel5 = 0x8000,
709  HEVCMainTierLevel51 = 0x10000,
710  HEVCHighTierLevel51 = 0x20000,
711  HEVCMainTierLevel52 = 0x40000,
712  HEVCHighTierLevel52 = 0x80000,
713  HEVCMainTierLevel6 = 0x100000,
714  HEVCHighTierLevel6 = 0x200000,
715  HEVCMainTierLevel61 = 0x400000,
716  HEVCHighTierLevel61 = 0x800000,
717  HEVCMainTierLevel62 = 0x1000000,
718  HEVCHighTierLevel62 = 0x2000000,
719 };
720 
721 static const AVOption hevc_options[] = {
723  { "level", "Specify tier and level",
724  OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, "level" },
725  { "m1", "Main tier level 1",
726  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel1 }, 0, 0, VE, "level" },
727  { "h1", "High tier level 1",
728  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel1 }, 0, 0, VE, "level" },
729  { "m2", "Main tier level 2",
730  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel2 }, 0, 0, VE, "level" },
731  { "h2", "High tier level 2",
732  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel2 }, 0, 0, VE, "level" },
733  { "m2.1", "Main tier level 2.1",
734  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel21 }, 0, 0, VE, "level" },
735  { "h2.1", "High tier level 2.1",
736  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel21 }, 0, 0, VE, "level" },
737  { "m3", "Main tier level 3",
738  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel3 }, 0, 0, VE, "level" },
739  { "h3", "High tier level 3",
740  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel3 }, 0, 0, VE, "level" },
741  { "m3.1", "Main tier level 3.1",
742  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel31 }, 0, 0, VE, "level" },
743  { "h3.1", "High tier level 3.1",
744  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel31 }, 0, 0, VE, "level" },
745  { "m4", "Main tier level 4",
746  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel4 }, 0, 0, VE, "level" },
747  { "h4", "High tier level 4",
748  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel4 }, 0, 0, VE, "level" },
749  { "m4.1", "Main tier level 4.1",
750  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel41 }, 0, 0, VE, "level" },
751  { "h4.1", "High tier level 4.1",
752  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel41 }, 0, 0, VE, "level" },
753  { "m5", "Main tier level 5",
754  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel5 }, 0, 0, VE, "level" },
755  { "h5", "High tier level 5",
756  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel5 }, 0, 0, VE, "level" },
757  { "m5.1", "Main tier level 5.1",
758  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel51 }, 0, 0, VE, "level" },
759  { "h5.1", "High tier level 5.1",
760  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel51 }, 0, 0, VE, "level" },
761  { "m5.2", "Main tier level 5.2",
762  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel52 }, 0, 0, VE, "level" },
763  { "h5.2", "High tier level 5.2",
764  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel52 }, 0, 0, VE, "level" },
765  { "m6", "Main tier level 6",
766  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel6 }, 0, 0, VE, "level" },
767  { "h6", "High tier level 6",
768  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel6 }, 0, 0, VE, "level" },
769  { "m6.1", "Main tier level 6.1",
770  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel61 }, 0, 0, VE, "level" },
771  { "h6.1", "High tier level 6.1",
772  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel61 }, 0, 0, VE, "level" },
773  { "m6.2", "Main tier level 6.2",
774  0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel62 }, 0, 0, VE, "level" },
775  { "h6.2", "High tier level 6.2",
776  0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel62 }, 0, 0, VE, "level" },
777  { NULL, }
778 };
779 
781 
782 #endif // CONFIG_HEVC_MEDIACODEC_ENCODER
783 
784 #if CONFIG_VP8_MEDIACODEC_ENCODER
785 
786 enum MediaCodecVP8Level {
787  VP8Level_Version0 = 0x01,
788  VP8Level_Version1 = 0x02,
789  VP8Level_Version2 = 0x04,
790  VP8Level_Version3 = 0x08,
791 };
792 
793 static const AVOption vp8_options[] = {
795  { "level", "Specify tier and level",
796  OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, "level" },
797  { "V0", "Level Version 0",
798  0, AV_OPT_TYPE_CONST, { .i64 = VP8Level_Version0 }, 0, 0, VE, "level" },
799  { "V1", "Level Version 1",
800  0, AV_OPT_TYPE_CONST, { .i64 = VP8Level_Version1 }, 0, 0, VE, "level" },
801  { "V2", "Level Version 2",
802  0, AV_OPT_TYPE_CONST, { .i64 = VP8Level_Version2 }, 0, 0, VE, "level" },
803  { "V3", "Level Version 3",
804  0, AV_OPT_TYPE_CONST, { .i64 = VP8Level_Version3 }, 0, 0, VE, "level" },
805  { NULL, }
806 };
807 
809 
810 #endif // CONFIG_VP8_MEDIACODEC_ENCODER
811 
812 #if CONFIG_VP9_MEDIACODEC_ENCODER
813 
814 enum MediaCodecVP9Level {
815  VP9Level1 = 0x1,
816  VP9Level11 = 0x2,
817  VP9Level2 = 0x4,
818  VP9Level21 = 0x8,
819  VP9Level3 = 0x10,
820  VP9Level31 = 0x20,
821  VP9Level4 = 0x40,
822  VP9Level41 = 0x80,
823  VP9Level5 = 0x100,
824  VP9Level51 = 0x200,
825  VP9Level52 = 0x400,
826  VP9Level6 = 0x800,
827  VP9Level61 = 0x1000,
828  VP9Level62 = 0x2000,
829 };
830 
831 static const AVOption vp9_options[] = {
833  { "level", "Specify tier and level",
834  OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, "level" },
835  { "1", "Level 1",
836  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level1 }, 0, 0, VE, "level" },
837  { "1.1", "Level 1.1",
838  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level11 }, 0, 0, VE, "level" },
839  { "2", "Level 2",
840  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level2 }, 0, 0, VE, "level" },
841  { "2.1", "Level 2.1",
842  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level21 }, 0, 0, VE, "level" },
843  { "3", "Level 3",
844  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level3 }, 0, 0, VE, "level" },
845  { "3.1", "Level 3.1",
846  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level31 }, 0, 0, VE, "level" },
847  { "4", "Level 4",
848  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level4 }, 0, 0, VE, "level" },
849  { "4.1", "Level 4.1",
850  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level41 }, 0, 0, VE, "level" },
851  { "5", "Level 5",
852  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level5 }, 0, 0, VE, "level" },
853  { "5.1", "Level 5.1",
854  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level51 }, 0, 0, VE, "level" },
855  { "5.2", "Level 5.2",
856  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level52 }, 0, 0, VE, "level" },
857  { "6", "Level 6",
858  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level6 }, 0, 0, VE, "level" },
859  { "6.1", "Level 4.1",
860  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level61 }, 0, 0, VE, "level" },
861  { "6.2", "Level 6.2",
862  0, AV_OPT_TYPE_CONST, { .i64 = VP9Level62 }, 0, 0, VE, "level" },
863  { NULL, }
864 };
865 
867 
868 #endif // CONFIG_VP9_MEDIACODEC_ENCODER
869 
870 #if CONFIG_MPEG4_MEDIACODEC_ENCODER
871 
872 enum MediaCodecMpeg4Level {
873  MPEG4Level0 = 0x01,
874  MPEG4Level0b = 0x02,
875  MPEG4Level1 = 0x04,
876  MPEG4Level2 = 0x08,
877  MPEG4Level3 = 0x10,
878  MPEG4Level3b = 0x18,
879  MPEG4Level4 = 0x20,
880  MPEG4Level4a = 0x40,
881  MPEG4Level5 = 0x80,
882  MPEG4Level6 = 0x100,
883 };
884 
885 static const AVOption mpeg4_options[] = {
887  { "level", "Specify tier and level",
888  OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, "level" },
889  { "0", "Level 0",
890  0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level0 }, 0, 0, VE, "level" },
891  { "0b", "Level 0b",
892  0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level0b }, 0, 0, VE, "level" },
893  { "1", "Level 1",
894  0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level1 }, 0, 0, VE, "level" },
895  { "2", "Level 2",
896  0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level2 }, 0, 0, VE, "level" },
897  { "3", "Level 3",
898  0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level3 }, 0, 0, VE, "level" },
899  { "3b", "Level 3b",
900  0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level3b }, 0, 0, VE, "level" },
901  { "4", "Level 4",
902  0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level4 }, 0, 0, VE, "level" },
903  { "4a", "Level 4a",
904  0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level4a }, 0, 0, VE, "level" },
905  { "5", "Level 5",
906  0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level5 }, 0, 0, VE, "level" },
907  { "6", "Level 6",
908  0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level6 }, 0, 0, VE, "level" },
909  { NULL, }
910 };
911 
913 
914 #endif // CONFIG_MPEG4_MEDIACODEC_ENCODER
915 
916 #if CONFIG_AV1_MEDIACODEC_ENCODER
917 
918 enum MediaCodecAV1Level {
919  AV1Level2 = 0x1,
920  AV1Level21 = 0x2,
921  AV1Level22 = 0x4,
922  AV1Level23 = 0x8,
923  AV1Level3 = 0x10,
924  AV1Level31 = 0x20,
925  AV1Level32 = 0x40,
926  AV1Level33 = 0x80,
927  AV1Level4 = 0x100,
928  AV1Level41 = 0x200,
929  AV1Level42 = 0x400,
930  AV1Level43 = 0x800,
931  AV1Level5 = 0x1000,
932  AV1Level51 = 0x2000,
933  AV1Level52 = 0x4000,
934  AV1Level53 = 0x8000,
935  AV1Level6 = 0x10000,
936  AV1Level61 = 0x20000,
937  AV1Level62 = 0x40000,
938  AV1Level63 = 0x80000,
939  AV1Level7 = 0x100000,
940  AV1Level71 = 0x200000,
941  AV1Level72 = 0x400000,
942  AV1Level73 = 0x800000,
943 };
944 
945 static const AVOption av1_options[] = {
947  { "level", "Specify tier and level",
948  OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, "level" },
949  { "2", "Level 2",
950  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level2 }, 0, 0, VE, "level" },
951  { "2.1", "Level 2.1",
952  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level21 }, 0, 0, VE, "level" },
953  { "2.2", "Level 2.2",
954  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level22 }, 0, 0, VE, "level" },
955  { "2.3", "Level 2.3",
956  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level23 }, 0, 0, VE, "level" },
957  { "3", "Level 3",
958  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level3 }, 0, 0, VE, "level" },
959  { "3.1", "Level 3.1",
960  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level31 }, 0, 0, VE, "level" },
961  { "3.2", "Level 3.2",
962  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level32 }, 0, 0, VE, "level" },
963  { "3.3", "Level 3.3",
964  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level33 }, 0, 0, VE, "level" },
965  { "4", "Level 4",
966  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level4 }, 0, 0, VE, "level" },
967  { "4.1", "Level 4.1",
968  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level41 }, 0, 0, VE, "level" },
969  { "4.2", "Level 4.2",
970  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level42 }, 0, 0, VE, "level" },
971  { "4.3", "Level 4.3",
972  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level43 }, 0, 0, VE, "level" },
973  { "5", "Level 5",
974  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level5 }, 0, 0, VE, "level" },
975  { "5.1", "Level 5.1",
976  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level51 }, 0, 0, VE, "level" },
977  { "5.2", "Level 5.2",
978  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level52 }, 0, 0, VE, "level" },
979  { "5.3", "Level 5.3",
980  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level53 }, 0, 0, VE, "level" },
981  { "6", "Level 6",
982  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level6 }, 0, 0, VE, "level" },
983  { "6.1", "Level 6.1",
984  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level61 }, 0, 0, VE, "level" },
985  { "6.2", "Level 6.2",
986  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level62 }, 0, 0, VE, "level" },
987  { "6.3", "Level 6.3",
988  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level63 }, 0, 0, VE, "level" },
989  { "7", "Level 7",
990  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level7 }, 0, 0, VE, "level" },
991  { "7.1", "Level 7.1",
992  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level71 }, 0, 0, VE, "level" },
993  { "7.2", "Level 7.2",
994  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level72 }, 0, 0, VE, "level" },
995  { "7.3", "Level 7.3",
996  0, AV_OPT_TYPE_CONST, { .i64 = AV1Level73 }, 0, 0, VE, "level" },
997  { NULL, }
998 };
999 
1001 
1002 #endif // CONFIG_AV1_MEDIACODEC_ENCODER
MediaCodecEncContext
Definition: mediacodecenc.c:54
ff_AMediaCodec_getInputBuffer
static uint8_t * ff_AMediaCodec_getInputBuffer(FFAMediaCodec *codec, size_t idx, size_t *out_size)
Definition: mediacodec_wrapper.h:261
hwconfig.h
AVHWDeviceContext::hwctx
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:92
AVCodecContext::hwaccel_context
void * hwaccel_context
Legacy hardware accelerator context.
Definition: avcodec.h:1453
ff_AMediaFormat_delete
static int ff_AMediaFormat_delete(FFAMediaFormat *format)
Definition: mediacodec_wrapper.h:92
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
ff_AMediaCodec_delete
static int ff_AMediaCodec_delete(FFAMediaCodec *codec)
Definition: mediacodec_wrapper.h:256
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
level
uint8_t level
Definition: svq3.c:204
MediaCodecEncContext::pts_as_dts
int pts_as_dts
Definition: mediacodecenc.c:74
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1023
DECLARE_MEDIACODEC_ENCODER
#define DECLARE_MEDIACODEC_ENCODER(short_name, long_name, codec_id)
Definition: mediacodecenc.c:614
ff_AMediaCodec_start
static int ff_AMediaCodec_start(FFAMediaCodec *codec)
Definition: mediacodec_wrapper.h:241
FFAMediaCodecBufferInfo::offset
int32_t offset
Definition: mediacodec_wrapper.h:173
MediaCodecEncContext::avclass
AVClass * avclass
Definition: mediacodecenc.c:55
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
avcodec_parameters_from_context
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: codec_par.c:100
AV_CODEC_HW_CONFIG_METHOD_AD_HOC
@ AV_CODEC_HW_CONFIG_METHOD_AD_HOC
The codec supports this format by some ad-hoc method.
Definition: codec.h:338
color_formats
static const struct @113 color_formats[]
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
color_format
int color_format
Definition: mediacodecenc.c:84
BITRATE_MODE_VBR
@ BITRATE_MODE_VBR
Definition: mediacodecenc.c:47
AVMediaCodecDeviceContext::surface
void * surface
android/view/Surface handle, to be filled by the user.
Definition: hwcontext_mediacodec.h:33
MediaCodecEncContext::fps
int fps
Definition: mediacodecenc.c:61
out_size
int out_size
Definition: movenc.c:55
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
COLOR_FormatYUV420SemiPlanar
@ COLOR_FormatYUV420SemiPlanar
Definition: mediacodecenc.c:79
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
ff_AMediaCodec_signalEndOfInputStream
static int ff_AMediaCodec_signalEndOfInputStream(FFAMediaCodec *codec)
Definition: mediacodec_wrapper.h:341
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1016
MediaCodecEncContext::window
FFANativeWindow * window
Definition: mediacodecenc.c:59
AVPacket::data
uint8_t * data
Definition: packet.h:374
ff_AMediaFormat_setString
static void ff_AMediaFormat_setString(FFAMediaFormat *format, const char *name, const char *value)
Definition: mediacodec_wrapper.h:150
BitrateMode
BitrateMode
Definition: mediacodecenc.c:43
ff_AMediaCodec_infoOutputFormatChanged
static int ff_AMediaCodec_infoOutputFormatChanged(FFAMediaCodec *codec, ssize_t idx)
Definition: mediacodec_wrapper.h:311
AVOption
AVOption.
Definition: opt.h:251
encode.h
ff_AMediaCodec_infoOutputBuffersChanged
static int ff_AMediaCodec_infoOutputBuffersChanged(FFAMediaCodec *codec, ssize_t idx)
Definition: mediacodec_wrapper.h:306
AV_HWDEVICE_TYPE_MEDIACODEC
@ AV_HWDEVICE_TYPE_MEDIACODEC
Definition: hwcontext.h:38
ff_AMediaCodec_queueInputBuffer
static int ff_AMediaCodec_queueInputBuffer(FFAMediaCodec *codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags)
Definition: mediacodec_wrapper.h:276
MediaCodecEncContext::eof_sent
int eof_sent
Definition: mediacodecenc.c:67
COMMON_OPTION
#define COMMON_OPTION
Definition: mediacodecenc.c:586
mediacodec_encode
static int mediacodec_encode(AVCodecContext *avctx, AVPacket *pkt)
Definition: mediacodecenc.c:504
FF_COMPLIANCE_EXPERIMENTAL
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: defs.h:62
hwcontext_mediacodec.h
av_bsf_free
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:53
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:429
ff_AMediaFormat_setInt32
static void ff_AMediaFormat_setInt32(FFAMediaFormat *format, const char *name, int32_t value)
Definition: mediacodec_wrapper.h:135
AVBSFContext
The bitstream filter state.
Definition: bsf.h:68
VE
#define VE
Definition: mediacodecenc.c:585
AV_CODEC_FLAG_GLOBAL_HEADER
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:326
mediacodec_output_format
static void mediacodec_output_format(AVCodecContext *avctx)
Definition: mediacodecenc.c:99
ff_AMediaCodec_configure
static int ff_AMediaCodec_configure(FFAMediaCodec *codec, const FFAMediaFormat *format, FFANativeWindow *surface, void *crypto, uint32_t flags)
Definition: mediacodec_wrapper.h:233
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1785
bsf.h
av1_options
static const AVOption av1_options[]
Definition: av1dec.c:1506
mediacodec_close
static av_cold int mediacodec_close(AVCodecContext *avctx)
Definition: mediacodecenc.c:551
MediaCodecEncContext::extradata
uint8_t * extradata
Definition: mediacodecenc.c:65
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:515
pix_fmt
enum AVPixelFormat pix_fmt
Definition: mediacodecenc.c:85
COLOR_RANGE_UNSPECIFIED
@ COLOR_RANGE_UNSPECIFIED
Definition: mediacodec_wrapper.h:349
AVERROR_BUFFER_TOO_SMALL
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition: error.h:53
pts
static int64_t pts
Definition: transcode_aac.c:643
ff_AMediaFormat_new
FFAMediaFormat * ff_AMediaFormat_new(int ndk)
Definition: mediacodec_wrapper.c:2583
AVRational::num
int num
Numerator.
Definition: rational.h:59
mediacodecdec_common.h
mediacodec_receive
static int mediacodec_receive(AVCodecContext *avctx, AVPacket *pkt, int *got_packet)
Definition: mediacodecenc.c:347
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:61
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:88
mediacodec_send
static int mediacodec_send(AVCodecContext *avctx, const AVFrame *frame)
Definition: mediacodecenc.c:455
avassert.h
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
MediaCodecEncContext::bitrate_mode
int bitrate_mode
Definition: mediacodecenc.c:72
ff_AMediaCodec_getName
static char * ff_AMediaCodec_getName(FFAMediaCodec *codec)
Definition: mediacodec_wrapper.h:224
ff_AMediaCodec_getBufferFlagEndOfStream
static int ff_AMediaCodec_getBufferFlagEndOfStream(FFAMediaCodec *codec)
Definition: mediacodec_wrapper.h:321
s
#define s(width, name)
Definition: cbs_vp9.c:256
hevc_options
static const AVOption hevc_options[]
Definition: videotoolboxenc.c:2813
BITRATE_MODE_CQ
@ BITRATE_MODE_CQ
Definition: mediacodecenc.c:45
format
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
AV_CODEC_ID_VP9
@ AV_CODEC_ID_VP9
Definition: codec_id.h:220
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
OFFSET
#define OFFSET(x)
Definition: mediacodecenc.c:584
av_rescale_q
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
AVCodecHWConfig::pix_fmt
enum AVPixelFormat pix_fmt
For decoders, a hardware pixel format which that decoder may be able to decode to if suitable hardwar...
Definition: codec.h:350
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:445
MediaCodecEncContext::use_ndk_codec
int use_ndk_codec
Definition: mediacodecenc.c:57
AV_PIX_FMT_MEDIACODEC
@ AV_PIX_FMT_MEDIACODEC
hardware decoding through MediaCodec
Definition: pixfmt.h:313
ff_AMediaFormatColorStandard_from_AVColorSpace
int ff_AMediaFormatColorStandard_from_AVColorSpace(enum AVColorSpace color_space)
Map AVColorSpace to MediaFormat color standard.
Definition: mediacodec_wrapper.c:2708
OUTPUT_DEQUEUE_TIMEOUT_US
#define OUTPUT_DEQUEUE_TIMEOUT_US
Definition: mediacodecenc.c:41
ff_AMediaCodec_getOutputFormat
static FFAMediaFormat * ff_AMediaCodec_getOutputFormat(FFAMediaCodec *codec)
Definition: mediacodec_wrapper.h:286
MediaCodecEncContext::level
int level
Definition: mediacodecenc.c:73
av_bsf_init
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:150
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ff_AMediaCodec_createCodecByName
FFAMediaCodec * ff_AMediaCodec_createCodecByName(const char *name, int ndk)
Definition: mediacodec_wrapper.c:2590
ff_AMediaFormatColorRange_from_AVColorRange
int ff_AMediaFormatColorRange_from_AVColorRange(enum AVColorRange color_range)
Map AVColorRange to MediaFormat color range.
Definition: mediacodec_wrapper.c:2691
NULL
#define NULL
Definition: coverity.c:32
FFAMediaCodecBufferInfo
Definition: mediacodec_wrapper.h:172
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1033
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:283
av_bsf_receive_packet
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:231
FFAMediaCodecBufferInfo::size
int32_t size
Definition: mediacodec_wrapper.h:174
AVMediaCodecContext
This structure holds a reference to a android/view/Surface object that will be used as output by the ...
Definition: mediacodec.h:33
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:485
ff_AMediaCodec_createEncoderByType
FFAMediaCodec * ff_AMediaCodec_createEncoderByType(const char *mime_type, int ndk)
Definition: mediacodec_wrapper.c:2604
ff_AMediaCodec_stop
static int ff_AMediaCodec_stop(FFAMediaCodec *codec)
Definition: mediacodec_wrapper.h:246
ff_mediacodec_surface_unref
int ff_mediacodec_surface_unref(FFANativeWindow *window, void *log_ctx)
Definition: mediacodec_surface.c:59
BITRATE_MODE_CBR_FD
@ BITRATE_MODE_CBR_FD
Definition: mediacodecenc.c:51
ff_AMediaCodec_getBufferFlagKeyFrame
static int ff_AMediaCodec_getBufferFlagKeyFrame(FFAMediaCodec *codec)
Definition: mediacodec_wrapper.h:326
mpeg4_options
static const AVOption mpeg4_options[]
Definition: v4l2_m2m_enc.c:397
BITRATE_MODE_CBR
@ BITRATE_MODE_CBR
Definition: mediacodecenc.c:49
index
int index
Definition: gxfenc.c:89
AVMediaCodecDeviceContext
MediaCodec details.
Definition: hwcontext_mediacodec.h:27
ff_AMediaFormat_toString
static char * ff_AMediaFormat_toString(FFAMediaFormat *format)
Definition: mediacodec_wrapper.h:97
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:557
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
Definition: codec.h:309
AVCodecContext::gop_size
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:637
h264_options
static const AVOption h264_options[]
Definition: h264dec.c:1055
codec_internal.h
av_bsf_send_packet
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:203
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:121
COLOR_TRANSFER_UNSPECIFIED
@ COLOR_TRANSFER_UNSPECIFIED
Definition: mediacodec_wrapper.h:363
avc_pix_fmts
static enum AVPixelFormat avc_pix_fmts[]
Definition: mediacodecenc.c:92
size
int size
Definition: twinvq_data.h:10344
av_reallocp
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:186
ff_AMediaFormatColorTransfer_from_AVColorTransfer
int ff_AMediaFormatColorTransfer_from_AVColorTransfer(enum AVColorTransferCharacteristic color_transfer)
Map AVColorTransferCharacteristic to MediaFormat color transfer.
Definition: mediacodec_wrapper.c:2736
AVCodecHWConfigInternal
Definition: hwconfig.h:30
MediaCodecEncContext::bsf
AVBSFContext * bsf
Definition: mediacodecenc.c:70
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:373
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
ff_AMediaCodecProfile_getProfileFromAVCodecContext
int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx)
The following API around MediaCodec and MediaFormat is based on the NDK one provided by Google since ...
Definition: mediacodec_wrapper.c:303
COLOR_FormatSurface
@ COLOR_FormatSurface
Definition: mediacodecenc.c:80
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:380
MediaCodecEncContext::name
const char * name
Definition: mediacodecenc.c:58
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
MediaCodecEncContext::width
int width
Definition: mediacodecenc.c:62
ff_AMediaCodec_getConfigureFlagEncode
static int ff_AMediaCodec_getConfigureFlagEncode(FFAMediaCodec *codec)
Definition: mediacodec_wrapper.h:331
MediaCodecEncContext::codec
FFAMediaCodec * codec
Definition: mediacodecenc.c:56
FFANativeWindow
Definition: mediacodec_surface.h:28
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
mediacodec_wrapper.h
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
COLOR_STANDARD_UNSPECIFIED
@ COLOR_STANDARD_UNSPECIFIED
Definition: mediacodec_wrapper.h:355
round
static av_always_inline av_const double round(double x)
Definition: libm.h:444
FFAMediaCodec
Definition: mediacodec_wrapper.h:181
MediaCodecEncContext::height
int height
Definition: mediacodecenc.c:63
MediaCodecEncContext::extradata_size
int extradata_size
Definition: mediacodecenc.c:66
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:226
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:622
COLOR_FormatYUV420Planar
@ COLOR_FormatYUV420Planar
Definition: mediacodecenc.c:78
FFAMediaCodecBufferInfo::flags
uint32_t flags
Definition: mediacodec_wrapper.h:176
AVCodecContext::hw_device_ctx
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition: avcodec.h:1963
AVMediaCodecContext::surface
void * surface
android/view/Surface object reference.
Definition: mediacodec.h:38
ff_mediacodec_surface_ref
FFANativeWindow * ff_mediacodec_surface_ref(void *surface, void *native_window, void *log_ctx)
Definition: mediacodec_surface.c:30
MediaCodecEncContext::frame
AVFrame * frame
Definition: mediacodecenc.c:69
INPUT_DEQUEUE_TIMEOUT_US
#define INPUT_DEQUEUE_TIMEOUT_US
Definition: mediacodecenc.c:40
AVCodecContext::height
int height
Definition: avcodec.h:615
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:652
av_jni_get_java_vm
void * av_jni_get_java_vm(void *log_ctx)
Definition: jni.c:74
AVMediaCodecDeviceContext::native_window
void * native_window
Pointer to ANativeWindow.
Definition: hwcontext_mediacodec.h:45
FFAMediaCodecBufferInfo::presentationTimeUs
int64_t presentationTimeUs
Definition: mediacodec_wrapper.h:175
ff_AMediaCodec_dequeueInputBuffer
static ssize_t ff_AMediaCodec_dequeueInputBuffer(FFAMediaCodec *codec, int64_t timeoutUs)
Definition: mediacodec_wrapper.h:271
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVHWDeviceContext::type
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition: hwcontext.h:79
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:89
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext::strict_std_compliance
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:1365
av_mediacodec_release_buffer
int av_mediacodec_release_buffer(AVMediaCodecBuffer *buffer, int render)
Release a MediaCodec buffer and render it to the surface that is associated with the decoder.
Definition: mediacodec.c:138
AVCodecContext
main external API structure.
Definition: avcodec.h:435
av_image_copy
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:422
mediacodec_init
static av_cold int mediacodec_init(AVCodecContext *avctx)
Definition: mediacodecenc.c:149
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:79
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
ff_AMediaCodec_getBufferFlagCodecConfig
static int ff_AMediaCodec_getBufferFlagCodecConfig(FFAMediaCodec *codec)
Definition: mediacodec_wrapper.h:316
av_bsf_list_parse_str
int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst)
Parse string describing list of bitstream filters and create single AVBSFContext describing the whole...
Definition: bsf.c:527
AVCodecContext::max_b_frames
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:714
ff_encode_get_frame
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
Definition: encode.c:183
ff_AMediaCodec_getOutputBuffer
static uint8_t * ff_AMediaCodec_getOutputBuffer(FFAMediaCodec *codec, size_t idx, size_t *out_size)
Definition: mediacodec_wrapper.h:266
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
ff_AMediaCodec_cleanOutputBuffers
static int ff_AMediaCodec_cleanOutputBuffers(FFAMediaCodec *codec)
Definition: mediacodec_wrapper.h:336
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:462
ff_AMediaCodec_infoTryAgainLater
static int ff_AMediaCodec_infoTryAgainLater(FFAMediaCodec *codec, ssize_t idx)
Definition: mediacodec_wrapper.h:301
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:615
imgutils.h
AV_CODEC_ID_VP8
@ AV_CODEC_ID_VP8
Definition: codec_id.h:192
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
ff_AMediaCodec_releaseOutputBuffer
static int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec *codec, size_t idx, int render)
Definition: mediacodec_wrapper.h:291
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
jni.h
mediacodec_hw_configs
static const AVCodecHWConfigInternal *const mediacodec_hw_configs[]
Definition: mediacodecenc.c:571
AVCodecHWConfigInternal::public
AVCodecHWConfig public
This is the structure which will be returned to the user by avcodec_get_hw_config().
Definition: hwconfig.h:35
FFAMediaFormat
Definition: mediacodec_wrapper.h:63
ff_AMediaCodec_dequeueOutputBuffer
static ssize_t ff_AMediaCodec_dequeueOutputBuffer(FFAMediaCodec *codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs)
Definition: mediacodec_wrapper.h:281
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
snprintf
#define snprintf
Definition: snprintf.h:34
copy_frame_to_buffer
static void copy_frame_to_buffer(AVCodecContext *avctx, const AVFrame *frame, uint8_t *dst, size_t size)
Definition: mediacodecenc.c:425
mediacodec_init_bsf
static int mediacodec_init_bsf(AVCodecContext *avctx)
Definition: mediacodecenc.c:113
mediacodec.h