FFmpeg
Loading...
Searching...
No Matches
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"
26#include "libavutil/fifo.h"
27#include "libavutil/avstring.h"
29#include "libavutil/imgutils.h"
30#include "libavutil/mem.h"
31#include "libavutil/opt.h"
32#include "libavutil/thread.h"
33
34#include "avcodec.h"
35#include "bsf.h"
36#include "codec_internal.h"
37#include "dts_gen.h"
38#include "encode.h"
39#include "hwconfig.h"
40#include "jni.h"
41#include "mediacodec.h"
42#include "mediacodec_wrapper.h"
44#include "profiles.h"
45
46#define INPUT_DEQUEUE_TIMEOUT_US 8000
47#define OUTPUT_DEQUEUE_TIMEOUT_US 8000
48
50 /* Constant quality mode */
52 /* Variable bitrate mode */
54 /* Constant bitrate mode */
56 /* Constant bitrate mode with frame drops */
58};
59
64
110
111enum {
115};
116
117static const struct {
120} color_formats[] = {
125
132
134 FFAMediaFormat *out_format)
135{
137 const char *name = s->name;
138 char *str = ff_AMediaFormat_toString(out_format);
139
140 av_log(avctx, AV_LOG_DEBUG, "MediaCodec encoder %s output format %s\n",
141 name ? name : "unknown", str);
142 av_free(str);
143}
144
146{
148 FFAMediaFormat *out_format = ff_AMediaCodec_getOutputFormat(s->codec);
149
150 if (!s->name)
151 s->name = ff_AMediaCodec_getName(s->codec);
152 mediacodec_dump_format(avctx, out_format);
153 ff_AMediaFormat_delete(out_format);
154}
155
157{
158 const AVBitStreamFilter *bsf = av_bsf_get_by_name("extract_extradata");
159
160 if (!bsf) {
161 av_log(avctx, AV_LOG_WARNING, "extract_extradata bsf not found\n");
162 return 0;
163 }
164
165 for (int i = 0; bsf->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
166 if (bsf->codec_ids[i] == avctx->codec_id)
167 return 1;
168 }
169
170 return 0;
171}
172
174{
176 char str[128] = {0};
177 int ret;
178 int crop_right = s->width - avctx->width;
179 int crop_bottom = s->height - avctx->height;
180
181 /* Nothing can be done for this format now */
182 if (avctx->pix_fmt == AV_PIX_FMT_MEDIACODEC)
183 return 0;
184
185 s->extract_extradata = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) &&
187 if (!crop_right && !crop_bottom && !s->extract_extradata)
188 return 0;
189
190 ret = 0;
191 if (crop_right || crop_bottom) {
192 if (avctx->codec_id == AV_CODEC_ID_H264)
193 ret = snprintf(str, sizeof(str), "h264_metadata=crop_right=%d:crop_bottom=%d",
194 crop_right, crop_bottom);
195 else if (avctx->codec_id == AV_CODEC_ID_HEVC)
196 /* Encoder can use CTU size larger than 16x16, so the real crop
197 * margin can be larger than crop_right/crop_bottom. Let bsf figure
198 * out the real crop margin.
199 */
200 ret = snprintf(str, sizeof(str), "hevc_metadata=width=%d:height=%d",
201 avctx->width, avctx->height);
202 if (ret >= sizeof(str))
204 }
205
206 if (s->extract_extradata) {
207 ret = av_strlcatf(str, sizeof(str), "%sextract_extradata", ret ? "," : "");
208 if (ret >= sizeof(str))
210 }
211
212 ret = av_bsf_list_parse_str(str, &s->bsf);
213 if (ret < 0)
214 return ret;
215
216 ret = avcodec_parameters_from_context(s->bsf->par_in, avctx);
217 if (ret < 0)
218 return ret;
219 s->bsf->time_base_in = avctx->time_base;
220 ret = av_bsf_init(s->bsf);
221
222 return ret;
223}
224
226 uint8_t *dst, size_t size)
227{
229 uint8_t *dst_data[4] = {};
230 int dst_linesize[4] = {};
231
232 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
233 dst_data[0] = dst;
234 dst_data[1] = dst + s->width * s->height;
235 dst_data[2] = dst_data[1] + s->width * s->height / 4;
236
237 dst_linesize[0] = s->width;
238 dst_linesize[1] = dst_linesize[2] = s->width / 2;
239 } else if (avctx->pix_fmt == AV_PIX_FMT_NV12) {
240 dst_data[0] = dst;
241 dst_data[1] = dst + s->width * s->height;
242
243 dst_linesize[0] = s->width;
244 dst_linesize[1] = s->width;
245 } else {
246 av_assert0(0);
247 }
248
249 av_image_copy2(dst_data, dst_linesize, frame->data, frame->linesize,
250 avctx->pix_fmt, avctx->width, avctx->height);
251}
252
253
254static void on_error(FFAMediaCodec *codec, void *userdata, int error,
255 const char *detail)
256{
257 AVCodecContext *avctx = userdata;
259
260 if (error == AVERROR(EAGAIN))
261 return;
262
263 av_log(avctx, AV_LOG_ERROR, "On error, %s, %s\n", av_err2str(error), detail);
264
265 ff_mutex_lock(&s->input_mutex);
266 ff_mutex_lock(&s->output_mutex);
267 s->encode_status = error;
268 ff_mutex_unlock(&s->output_mutex);
269 ff_mutex_unlock(&s->input_mutex);
270
271 ff_cond_signal(&s->output_cond);
272 ff_cond_signal(&s->input_cond);
273}
274
275static void on_input_available(FFAMediaCodec *codec, void *userdata,
277{
278 AVCodecContext *avctx = userdata;
280 int ret;
281
282 ff_mutex_lock(&s->input_mutex);
283 ret = av_fifo_write(s->input_index, &index, 1);
284 if (ret >= 0)
285 ff_cond_signal(&s->input_cond);
286 ff_mutex_unlock(&s->input_mutex);
287
288 if (ret < 0)
289 on_error(codec, userdata, ret, "av_fifo_write failed");
290}
291
292static void on_output_available(FFAMediaCodec *codec, void *userdata,
294 FFAMediaCodecBufferInfo *out_info)
295{
296 AVCodecContext *avctx = userdata;
298 MediaCodecAsyncOutput output = {
299 .index = index,
300 .buf_info = *out_info,
301 };
302 int ret;
303
304 ff_mutex_lock(&s->output_mutex);
305 ret = av_fifo_write(s->async_output, &output, 1);
306 if (ret >= 0)
307 ff_cond_signal(&s->output_cond);
308 ff_mutex_unlock(&s->output_mutex);
309
310 if (ret < 0)
311 on_error(codec, userdata, ret, "av_fifo_write failed");
312}
313
314static void on_format_changed(FFAMediaCodec *codec, void *userdata,
316{
318}
319
321{
323 size_t fifo_size = 16;
324
325 if (!s->async_mode)
326 return 0;
327
328 ff_mutex_init(&s->input_mutex, NULL);
329 ff_cond_init(&s->input_cond, NULL);
330
331 ff_mutex_init(&s->output_mutex, NULL);
332 ff_cond_init(&s->output_cond, NULL);
333
334 s->input_index = av_fifo_alloc2(fifo_size, sizeof(int32_t), AV_FIFO_FLAG_AUTO_GROW);
335 s->async_output = av_fifo_alloc2(fifo_size, sizeof(MediaCodecAsyncOutput),
337
338 if (!s->input_index || !s->async_output)
339 return AVERROR(ENOMEM);
340
341 return 0;
342}
343
345{
347
348 if (!s->async_mode)
349 return;
350
351 ff_mutex_destroy(&s->input_mutex);
352 ff_cond_destroy(&s->input_cond);
353
354 ff_mutex_destroy(&s->output_mutex);
355 ff_cond_destroy(&s->output_cond);
356
357 av_fifo_freep2(&s->input_index);
358 av_fifo_freep2(&s->async_output);
359
360 s->async_mode = 0;
361}
362
364
367{
369
370 // Handle common options in AVCodecContext first.
371 if (avctx->qmin >= 0) {
372 ff_AMediaFormat_setInt32(format, "video-qp-i-min", avctx->qmin);
373 ff_AMediaFormat_setInt32(format, "video-qp-p-min", avctx->qmin);
374 ff_AMediaFormat_setInt32(format, "video-qp-b-min", avctx->qmin);
375 }
376
377 if (avctx->qmax >= 0) {
378 ff_AMediaFormat_setInt32(format, "video-qp-i-max", avctx->qmax);
379 ff_AMediaFormat_setInt32(format, "video-qp-p-max", avctx->qmax);
380 ff_AMediaFormat_setInt32(format, "video-qp-b-max", avctx->qmax);
381 }
382
383 if (s->qp_i_min >= 0)
384 ff_AMediaFormat_setInt32(format, "video-qp-i-min", s->qp_i_min);
385 if (s->qp_p_min >= 0)
386 ff_AMediaFormat_setInt32(format, "video-qp-p-min", s->qp_p_min);
387 if (s->qp_b_min >= 0)
388 ff_AMediaFormat_setInt32(format, "video-qp-b-min", s->qp_b_min);
389
390 if (s->qp_i_max >= 0)
391 ff_AMediaFormat_setInt32(format, "video-qp-i-max", s->qp_i_max);
392 if (s->qp_p_max >= 0)
393 ff_AMediaFormat_setInt32(format, "video-qp-p-max", s->qp_p_max);
394 if (s->qp_b_max >= 0)
395 ff_AMediaFormat_setInt32(format, "video-qp-b-max", s->qp_b_max);
396}
397
399{
400 const char *codec_mime = NULL;
403 int ret;
404 int gop;
405 int delay;
406
407 // Init async state first, so we can do cleanup safely on error path.
409 if (ret < 0)
410 return ret;
411
412 if (s->use_ndk_codec < 0)
413 s->use_ndk_codec = !av_jni_get_java_vm(avctx);
414
415 switch (avctx->codec_id) {
416 case AV_CODEC_ID_H264:
417 codec_mime = "video/avc";
418 break;
419 case AV_CODEC_ID_HEVC:
420 codec_mime = "video/hevc";
421 break;
422 case AV_CODEC_ID_VP8:
423 codec_mime = "video/x-vnd.on2.vp8";
424 break;
425 case AV_CODEC_ID_VP9:
426 codec_mime = "video/x-vnd.on2.vp9";
427 break;
429 codec_mime = "video/mp4v-es";
430 break;
431 case AV_CODEC_ID_AV1:
432 codec_mime = "video/av01";
433 break;
434 default:
435 av_assert0(0);
436 }
437
438 if (s->name)
439 s->codec = ff_AMediaCodec_createCodecByName(s->name, s->use_ndk_codec);
440 else
441 s->codec = ff_AMediaCodec_createEncoderByType(codec_mime, s->use_ndk_codec);
442 if (!s->codec) {
443 av_log(avctx, AV_LOG_ERROR, "Failed to create encoder for type %s\n",
444 codec_mime);
445 return AVERROR_EXTERNAL;
446 }
447
448 format = ff_AMediaFormat_new(s->use_ndk_codec);
449 if (!format) {
450 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
451 return AVERROR_EXTERNAL;
452 }
453
454 ff_AMediaFormat_setString(format, "mime", codec_mime);
455 // Workaround the alignment requirement of mediacodec. We can't do it
456 // silently for AV_PIX_FMT_MEDIACODEC.
457 if (avctx->pix_fmt != AV_PIX_FMT_MEDIACODEC &&
458 (avctx->codec_id == AV_CODEC_ID_H264 ||
459 avctx->codec_id == AV_CODEC_ID_HEVC)) {
460 s->width = FFALIGN(avctx->width, 16);
461 s->height = FFALIGN(avctx->height, 16);
462 } else {
463 s->width = avctx->width;
464 s->height = avctx->height;
465 if (s->width % 16 || s->height % 16)
466 av_log(avctx, AV_LOG_WARNING,
467 "Video size %dx%d isn't align to 16, it may have device compatibility issue\n",
468 s->width, s->height);
469 }
470 ff_AMediaFormat_setInt32(format, "width", s->width);
471 ff_AMediaFormat_setInt32(format, "height", s->height);
472
473 if (avctx->pix_fmt == AV_PIX_FMT_MEDIACODEC) {
474 AVMediaCodecContext *user_ctx = avctx->hwaccel_context;
475 if (avctx->hw_device_ctx) {
476 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)(avctx->hw_device_ctx->data);
478
479 if (device_ctx->type != AV_HWDEVICE_TYPE_MEDIACODEC || !device_ctx->hwctx) {
480 ret = AVERROR(EINVAL);
481 goto bailout;
482 }
483 dev_ctx = device_ctx->hwctx;
484 s->window = ff_mediacodec_surface_ref(dev_ctx->surface, dev_ctx->native_window, avctx);
485 }
486
487 if (!s->window && user_ctx && user_ctx->surface)
488 s->window = ff_mediacodec_surface_ref(user_ctx->surface, NULL, avctx);
489
490 if (!s->window) {
491 ret = AVERROR(EINVAL);
492 av_log(avctx, AV_LOG_ERROR, "Missing hw_device_ctx or hwaccel_context for AV_PIX_FMT_MEDIACODEC\n");
493 goto bailout;
494 }
495 /* Although there is a method ANativeWindow_toSurface() introduced in
496 * API level 26, it's easier and safe to always require a Surface for
497 * Java MediaCodec.
498 */
499 if (!s->use_ndk_codec && !s->window->surface) {
500 ret = AVERROR(EINVAL);
501 av_log(avctx, AV_LOG_ERROR, "Missing jobject Surface for AV_PIX_FMT_MEDIACODEC. "
502 "Please note that Java MediaCodec doesn't work with ANativeWindow.\n");
503 goto bailout;
504 }
505 }
506
507 for (int i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) {
508 if (avctx->pix_fmt == color_formats[i].pix_fmt) {
509 ff_AMediaFormat_setInt32(format, "color-format",
511 break;
512 }
513 }
514
517 ff_AMediaFormat_setInt32(format, "color-range", ret);
520 ff_AMediaFormat_setInt32(format, "color-standard", ret);
523 ff_AMediaFormat_setInt32(format, "color-transfer", ret);
524
525 if (avctx->bit_rate)
526 ff_AMediaFormat_setInt32(format, "bitrate", avctx->bit_rate);
527 if (s->bitrate_mode >= 0) {
528 ff_AMediaFormat_setInt32(format, "bitrate-mode", s->bitrate_mode);
529 if (s->bitrate_mode == BITRATE_MODE_CQ && avctx->global_quality > 0)
531 }
533
534 // frame-rate and i-frame-interval are required to configure codec
535 if (avctx->framerate.num >= avctx->framerate.den && avctx->framerate.den > 0) {
536 s->fps = avctx->framerate.num / avctx->framerate.den;
537 } else {
538 s->fps = 30;
539 av_log(avctx, AV_LOG_INFO, "Use %d as the default MediaFormat frame-rate\n", s->fps);
540 }
541 gop = round(avctx->gop_size / s->fps);
542 if (gop == 0) {
543 gop = 1;
544 av_log(avctx, AV_LOG_INFO,
545 "Use %d as the default MediaFormat i-frame-interval, "
546 "please set gop_size properly (>= fps)\n", gop);
547 } else {
548 av_log(avctx, AV_LOG_DEBUG, "Set i-frame-interval to %d\n", gop);
549 }
550
551 ff_AMediaFormat_setInt32(format, "frame-rate", s->fps);
552 ff_AMediaFormat_setInt32(format, "i-frame-interval", gop);
553
555 if (ret > 0) {
556 av_log(avctx, AV_LOG_DEBUG, "set profile to 0x%x\n", ret);
558 }
559 if (s->level > 0) {
560 av_log(avctx, AV_LOG_DEBUG, "set level to 0x%x\n", s->level);
561 ff_AMediaFormat_setInt32(format, "level", s->level);
562 }
563 if (avctx->max_b_frames > 0)
564 ff_AMediaFormat_setInt32(format, "max-bframes", avctx->max_b_frames);
565 if (s->pts_as_dts == -1)
566 s->pts_as_dts = avctx->max_b_frames <= 0;
567
568 /* MediaCodec provides no DTS. "max-bframes" limits the B pictures
569 * between two anchors, which bounds the reorder depth.
570 */
571 delay = s->reorder_delay;
572 if (delay == FF_DTS_GEN_DELAY_AUTO)
573 delay = s->pts_as_dts ? 0 : av_clip(avctx->max_b_frames, 0,
575 if ((ret = ff_dts_gen_init(avctx, &s->dts_gen, delay)) < 0)
576 goto bailout;
577
578 if (s->operating_rate > 0)
579 ff_AMediaFormat_setInt32(format, "operating-rate", s->operating_rate);
580
582 ret = ff_AMediaCodec_configure(s->codec, format, s->window, NULL, ret);
583 if (ret) {
584 av_log(avctx, AV_LOG_ERROR, "MediaCodec configure failed, %s\n", av_err2str(ret));
585 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P)
586 av_log(avctx, AV_LOG_ERROR, "Please try -pix_fmt nv12, some devices don't "
587 "support yuv420p as encoder input format.\n");
588 goto bailout;
589 }
590
591 if (s->async_mode) {
593 .onAsyncInputAvailable = on_input_available,
594 .onAsyncOutputAvailable = on_output_available,
595 .onAsyncFormatChanged = on_format_changed,
596 .onAsyncError = on_error,
597 };
598
600 if (ret < 0) {
601 av_log(avctx, AV_LOG_WARNING,
602 "Try MediaCodec async mode failed, %s, switch to sync mode\n",
603 av_err2str(ret));
605 }
606 }
607
608 ret = mediacodec_init_bsf(avctx);
609 if (ret)
610 goto bailout;
611
613
614 s->frame = av_frame_alloc();
615 if (!s->frame) {
616 ret = AVERROR(ENOMEM);
617 goto bailout;
618 }
619
620 ret = ff_AMediaCodec_start(s->codec);
621 if (ret) {
622 av_log(avctx, AV_LOG_ERROR, "MediaCodec failed to start, %s\n",
623 av_err2str(ret));
624 goto bailout;
625 }
626
628
629bailout:
630 if (format)
632 return ret;
633}
634
636 FFAMediaCodecBufferInfo *out_info)
637{
639 FFAMediaCodec *codec = s->codec;
640 int64_t timeout_us = s->eof_sent ? OUTPUT_DEQUEUE_TIMEOUT_US : 0;
641 MediaCodecAsyncOutput output = { .index = -1 };
642 int ret;
643
644 if (!s->async_mode) {
645 *index = ff_AMediaCodec_dequeueOutputBuffer(codec, out_info, timeout_us);
646 return 0;
647 }
648
649 ff_mutex_lock(&s->output_mutex);
650
651 while (!s->encode_status) {
652 if (av_fifo_read(s->async_output, &output, 1) >= 0)
653 break;
654
655 // Only wait after signalEndOfInputStream
656 if (s->eof_sent && !s->encode_status)
657 ff_cond_wait(&s->output_cond, &s->output_mutex);
658 else
659 break;
660 }
661
662 ret = s->encode_status;
663 ff_mutex_unlock(&s->output_mutex);
664
665 // Get output index success
666 if (output.index >= 0) {
667 *index = output.index;
668 *out_info = output.buf_info;
669 return 0;
670 }
671
672 return ret ? ret : AVERROR(EAGAIN);
673}
674
676{
678 FFAMediaCodec *codec = s->codec;
679 ssize_t index;
680 FFAMediaCodecBufferInfo out_info = {0};
681 uint8_t *out_buf;
682 size_t out_size = 0;
683 int ret;
684 int extradata_size = 0;
685
686 ret = mediacodec_get_output_index(avctx, &index, &out_info);
687 if (ret < 0)
688 return ret;
689
691 return AVERROR(EAGAIN);
692
695 return AVERROR(EAGAIN);
696 }
697
700 return AVERROR(EAGAIN);
701 }
702
703 if (index < 0)
704 return AVERROR_EXTERNAL;
705
707 return AVERROR_EOF;
708
710 if (!out_buf) {
712 goto bailout;
713 }
714
715 if (out_info.flags & ff_AMediaCodec_getBufferFlagCodecConfig(codec)) {
716 if (avctx->codec_id == AV_CODEC_ID_AV1) {
717 // Skip AV1CodecConfigurationRecord without configOBUs
718 if (out_info.size <= 4) {
720 return mediacodec_receive(avctx, pkt);
721 }
722 out_info.size -= 4;
723 out_info.offset += 4;
724 }
725
726 ret = av_reallocp(&s->extradata, out_info.size);
727 if (ret)
728 goto bailout;
729
730 s->extradata_size = out_info.size;
731 memcpy(s->extradata, out_buf + out_info.offset, out_info.size);
733 // try immediately
734 return mediacodec_receive(avctx, pkt);
735 }
736
737 ret = ff_get_encode_buffer(avctx, pkt, out_info.size + s->extradata_size, 0);
738 if (ret < 0)
739 goto bailout;
740
741 if (s->extradata_size) {
742 extradata_size = s->extradata_size;
743 s->extradata_size = 0;
744 memcpy(pkt->data, s->extradata, extradata_size);
745 }
746 memcpy(pkt->data + extradata_size, out_buf + out_info.offset, out_info.size);
748 ff_dts_gen_fill_pkt(avctx, &s->dts_gen, pkt);
749 if (out_info.flags & ff_AMediaCodec_getBufferFlagKeyFrame(codec))
750 pkt->flags |= AV_PKT_FLAG_KEY;
751 ret = 0;
752
753 av_log(avctx, AV_LOG_TRACE, "receive packet pts %" PRId64 " dts %" PRId64
754 " flags %d extradata %d\n",
755 pkt->pts, pkt->dts, pkt->flags, extradata_size);
756
757bailout:
759 return ret;
760}
761
763{
765 FFAMediaCodec *codec = s->codec;
766 int ret = 0;
767 int32_t n;
768
769 if (!s->async_mode) {
771 return 0;
772 }
773
774 ff_mutex_lock(&s->input_mutex);
775
776 n = -1;
777 while (n < 0 && !s->encode_status) {
778 if (av_fifo_can_read(s->input_index) > 0) {
779 av_fifo_read(s->input_index, &n, 1);
780 break;
781 }
782
783 if (n < 0 && !s->encode_status)
784 ff_cond_wait(&s->input_cond, &s->input_mutex);
785 }
786
787 ret = s->encode_status;
788 *index = n;
789 ff_mutex_unlock(&s->input_mutex);
790
791 return ret;
792}
793
794
796 const AVFrame *frame) {
798 FFAMediaCodec *codec = s->codec;
799 ssize_t index;
800 uint8_t *input_buf = NULL;
801 size_t input_size = 0;
802 int64_t pts = 0;
803 uint32_t flags = 0;
804 int ret;
805
806 if (s->eof_sent)
807 return 0;
808
809 if (s->window) {
810 if (!frame) {
811 s->eof_sent = 1;
813 }
814
815 ff_dts_gen_record_pts(&s->dts_gen, frame->pts);
816 if (frame->data[3])
817 av_mediacodec_release_buffer((AVMediaCodecBuffer *)frame->data[3], 1);
818 return 0;
819 }
820
822 if (ret < 0)
823 return ret;
824
826 return AVERROR(EAGAIN);
827
828 if (index < 0) {
829 av_log(avctx, AV_LOG_ERROR, "dequeue input buffer failed, %zd", index);
830 return AVERROR_EXTERNAL;
831 }
832
833 if (frame) {
834 input_buf = ff_AMediaCodec_getInputBuffer(codec, index, &input_size);
835 copy_frame_to_buffer(avctx, frame, input_buf, input_size);
836
838 ff_dts_gen_record_pts(&s->dts_gen, frame->pts);
839 } else {
841 s->eof_sent = 1;
842 }
843
844 ff_AMediaCodec_queueInputBuffer(codec, index, 0, input_size, pts, flags);
845 return 0;
846}
847
849{
851 int ret;
852
853 if (s->flush_error < 0)
854 return s->flush_error;
855
856 // Return on three case:
857 // 1. Serious error
858 // 2. Got a packet success
859 // 3. No AVFrame is available yet (don't return if get_frame return EOF)
860 while (1) {
861 if (s->bsf) {
863 if (!ret)
864 return 0;
865 if (ret != AVERROR(EAGAIN))
866 return ret;
867 }
868
869 ret = mediacodec_receive(avctx, pkt);
870 if (s->bsf) {
871 if (!ret || ret == AVERROR_EOF)
872 ret = av_bsf_send_packet(s->bsf, pkt);
873 } else {
874 if (!ret)
875 return 0;
876 }
877
878 if (ret < 0 && ret != AVERROR(EAGAIN))
879 return ret;
880
881 if (!s->frame->buf[0]) {
882 ret = ff_encode_get_frame(avctx, s->frame);
883 if (ret && ret != AVERROR_EOF)
884 return ret;
885 }
886
887 ret = mediacodec_send(avctx, s->frame->buf[0] ? s->frame : NULL);
888 if (!ret)
889 av_frame_unref(s->frame);
890 else if (ret != AVERROR(EAGAIN))
891 return ret;
892 }
893
894 return 0;
895}
896
898{
900 int ret;
901
902 s->frame->width = avctx->width;
903 s->frame->height = avctx->height;
904 s->frame->format = avctx->pix_fmt;
905 s->frame->pts = 0;
906
907 ret = av_frame_get_buffer(s->frame, 0);
908 if (ret < 0)
909 return ret;
910
911 do {
912 ret = mediacodec_send(avctx, s->frame);
913 } while (ret == AVERROR(EAGAIN));
914 av_frame_unref(s->frame);
915
916 if (ret < 0)
917 return ret;
918
919 ret = mediacodec_send(avctx, NULL);
920 if (ret < 0) {
921 av_log(avctx, AV_LOG_ERROR, "Flush failed: %s\n", av_err2str(ret));
922 return ret;
923 }
924
925 return 0;
926}
927
929{
931 int ret;
932
933 do {
934 ret = mediacodec_receive(avctx, pkt);
935 } while (ret == AVERROR(EAGAIN));
936
937 if (ret < 0)
938 return ret;
939
940 do {
941 ret = av_bsf_send_packet(s->bsf, pkt);
942 if (ret < 0)
943 return ret;
945 } while (ret == AVERROR(EAGAIN));
946
947 return ret;
948}
949
951{
953 AVPacket *pkt = NULL;
954 int ret;
955 size_t side_size;
956 uint8_t *side;
957
958 if (!(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER))
959 return 0;
960
961 // Send dummy frame and receive a packet doesn't work in async mode
962 if (s->async_mode || !s->extract_extradata) {
963 av_log(avctx, AV_LOG_WARNING,
964 "Mediacodec encoder doesn't support AV_CODEC_FLAG_GLOBAL_HEADER. "
965 "Use extract_extradata bsf when necessary.\n");
966 return 0;
967 }
968
970 if (!pkt)
971 return AVERROR(ENOMEM);
972
974 if (ret < 0)
975 goto bailout;
977 if (ret < 0)
978 goto bailout;
979
981 if (side && side_size > 0) {
983 if (!avctx->extradata) {
984 ret = AVERROR(ENOMEM);
985 goto bailout;
986 }
987
988 memcpy(avctx->extradata, side, side_size);
989 avctx->extradata_size = side_size;
990 }
991
992bailout:
993 if (s->eof_sent) {
994 s->eof_sent = 0;
995 ff_AMediaCodec_flush(s->codec);
996 ff_dts_gen_reset(&s->dts_gen);
997 }
998 av_bsf_flush(s->bsf);
1000 return ret;
1001}
1002
1004{
1006 if (s->codec) {
1007 ff_AMediaCodec_stop(s->codec);
1008 ff_AMediaCodec_delete(s->codec);
1009 s->codec = NULL;
1010 }
1011
1012 if (s->window) {
1013 ff_mediacodec_surface_unref(s->window, avctx);
1014 s->window = NULL;
1015 }
1016
1017 av_bsf_free(&s->bsf);
1018 av_frame_free(&s->frame);
1019 av_freep(&s->extradata);
1020
1022
1023 return 0;
1024}
1025
1027{
1029
1030 if (s->bsf)
1031 av_bsf_flush(s->bsf);
1032 av_frame_unref(s->frame);
1033 ff_dts_gen_reset(&s->dts_gen);
1034
1035 s->flush_error = ff_AMediaCodec_flush(s->codec);
1036 if (s->flush_error < 0) {
1037 av_log(avctx, AV_LOG_ERROR, "Failed to flush MediaCodec encoder\n");
1038 return;
1039 }
1040
1041 s->eof_sent = 0;
1042
1043 if (!s->async_mode)
1044 return;
1045
1046 ff_mutex_lock(&s->input_mutex);
1047 ff_mutex_lock(&s->output_mutex);
1048
1049 av_fifo_reset2(s->input_index);
1050 av_fifo_reset2(s->async_output);
1051 s->encode_status = 0;
1052
1053 ff_cond_signal(&s->output_cond);
1054 ff_cond_signal(&s->input_cond);
1055
1056 ff_mutex_unlock(&s->output_mutex);
1057 ff_mutex_unlock(&s->input_mutex);
1058
1059 // MediaCodec delivers input buffers only after start().
1060 s->flush_error = ff_AMediaCodec_start(s->codec);
1061 if (s->flush_error < 0)
1062 av_log(avctx, AV_LOG_ERROR,
1063 "Failed to resume MediaCodec after async flush\n");
1064}
1065
1067 &(const AVCodecHWConfigInternal) {
1068 .public = {
1069 .pix_fmt = AV_PIX_FMT_MEDIACODEC,
1072 .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
1073 },
1074 .hwaccel = NULL,
1075 },
1076 NULL
1077};
1078
1080 {"qmin", "-1"},
1081 {"qmax", "-1"},
1082 {NULL},
1083};
1084
1085#define OFFSET(x) offsetof(MediaCodecEncContext, x)
1086#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1087#define COMMON_OPTION \
1088 { "ndk_codec", "Use MediaCodec from NDK", \
1089 OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
1090 { "ndk_async", "Try NDK MediaCodec in async mode", \
1091 OFFSET(async_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VE }, \
1092 { "codec_name", "Select codec by name", \
1093 OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, \
1094 { "bitrate_mode", "Bitrate control method", \
1095 OFFSET(bitrate_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, .unit = "bitrate_mode" }, \
1096 { "cq", "Constant quality mode", \
1097 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CQ}, 0, 0, VE, .unit = "bitrate_mode" }, \
1098 { "vbr", "Variable bitrate mode", \
1099 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_VBR}, 0, 0, VE, .unit = "bitrate_mode" }, \
1100 { "cbr", "Constant bitrate mode", \
1101 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, .unit = "bitrate_mode" }, \
1102 { "cbr_fd", "Constant bitrate mode with frame drops", \
1103 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, VE, .unit = "bitrate_mode" }, \
1104 { "pts_as_dts", "Use PTS as DTS when reorder_delay is auto. It is enabled automatically if " \
1105 "avctx max_b_frames <= 0, since most of Android devices don't output B frames " \
1106 "by default.", \
1107 OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
1108 { "operating_rate", "The desired operating rate that the codec will need to operate at, zero for unspecified", \
1109 OFFSET(operating_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE }, \
1110 { "qp_i_min", "minimum quantization parameter for I frame", \
1111 OFFSET(qp_i_min), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
1112 { "qp_p_min", "minimum quantization parameter for P frame", \
1113 OFFSET(qp_p_min), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
1114 { "qp_b_min", "minimum quantization parameter for B frame", \
1115 OFFSET(qp_b_min), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
1116 { "qp_i_max", "maximum quantization parameter for I frame", \
1117 OFFSET(qp_i_max), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
1118 { "qp_p_max", "maximum quantization parameter for P frame", \
1119 OFFSET(qp_p_max), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
1120 { "qp_b_max", "maximum quantization parameter for B frame", \
1121 OFFSET(qp_b_max), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
1122 FF_DTS_GEN_REORDER_DELAY_OPTION(OFFSET(reorder_delay), VE), \
1123
1124
1125#define MEDIACODEC_ENCODER_CLASS(name) \
1126static const AVClass name ## _mediacodec_class = { \
1127 .class_name = #name "_mediacodec", \
1128 .item_name = av_default_item_name, \
1129 .option = name ## _options, \
1130 .version = LIBAVUTIL_VERSION_INT, \
1131}; \
1132
1133#define DECLARE_MEDIACODEC_ENCODER(short_name, long_name, codec_id) \
1134MEDIACODEC_ENCODER_CLASS(short_name) \
1135const FFCodec ff_ ## short_name ## _mediacodec_encoder = { \
1136 .p.name = #short_name "_mediacodec", \
1137 CODEC_LONG_NAME(long_name " Android MediaCodec encoder"), \
1138 .p.type = AVMEDIA_TYPE_VIDEO, \
1139 .p.id = codec_id, \
1140 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | \
1141 AV_CODEC_CAP_HARDWARE | \
1142 AV_CODEC_CAP_ENCODER_FLUSH, \
1143 .priv_data_size = sizeof(MediaCodecEncContext), \
1144 CODEC_PIXFMTS_ARRAY(avc_pix_fmts), \
1145 .color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG, \
1146 .defaults = mediacodec_defaults, \
1147 .init = mediacodec_init, \
1148 FF_CODEC_RECEIVE_PACKET_CB(mediacodec_encode), \
1149 .close = mediacodec_close, \
1150 .flush = mediacodec_flush, \
1151 .p.priv_class = &short_name ## _mediacodec_class, \
1152 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
1153 .p.wrapper_name = "mediacodec", \
1154 .hw_configs = mediacodec_hw_configs, \
1155}; \
1156
1157#if CONFIG_H264_MEDIACODEC_ENCODER
1158
1159enum MediaCodecAvcLevel {
1160 AVCLevel1 = 0x01,
1161 AVCLevel1b = 0x02,
1162 AVCLevel11 = 0x04,
1163 AVCLevel12 = 0x08,
1164 AVCLevel13 = 0x10,
1165 AVCLevel2 = 0x20,
1166 AVCLevel21 = 0x40,
1167 AVCLevel22 = 0x80,
1168 AVCLevel3 = 0x100,
1169 AVCLevel31 = 0x200,
1170 AVCLevel32 = 0x400,
1171 AVCLevel4 = 0x800,
1172 AVCLevel41 = 0x1000,
1173 AVCLevel42 = 0x2000,
1174 AVCLevel5 = 0x4000,
1175 AVCLevel51 = 0x8000,
1176 AVCLevel52 = 0x10000,
1177 AVCLevel6 = 0x20000,
1178 AVCLevel61 = 0x40000,
1179 AVCLevel62 = 0x80000,
1180};
1181
1182static const AVOption h264_options[] = {
1184
1193
1194 { "level", "Specify level",
1195 OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "level" },
1196 { "1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel1 }, 0, 0, VE, .unit = "level" },
1197 { "1b", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel1b }, 0, 0, VE, .unit = "level" },
1198 { "1.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel11 }, 0, 0, VE, .unit = "level" },
1199 { "1.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel12 }, 0, 0, VE, .unit = "level" },
1200 { "1.3", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel13 }, 0, 0, VE, .unit = "level" },
1201 { "2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel2 }, 0, 0, VE, .unit = "level" },
1202 { "2.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel21 }, 0, 0, VE, .unit = "level" },
1203 { "2.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel22 }, 0, 0, VE, .unit = "level" },
1204 { "3", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel3 }, 0, 0, VE, .unit = "level" },
1205 { "3.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel31 }, 0, 0, VE, .unit = "level" },
1206 { "3.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel32 }, 0, 0, VE, .unit = "level" },
1207 { "4", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel4 }, 0, 0, VE, .unit = "level" },
1208 { "4.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel41 }, 0, 0, VE, .unit = "level" },
1209 { "4.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel42 }, 0, 0, VE, .unit = "level" },
1210 { "5", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel5 }, 0, 0, VE, .unit = "level" },
1211 { "5.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel51 }, 0, 0, VE, .unit = "level" },
1212 { "5.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel52 }, 0, 0, VE, .unit = "level" },
1213 { "6.0", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel6 }, 0, 0, VE, .unit = "level" },
1214 { "6.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel61 }, 0, 0, VE, .unit = "level" },
1215 { "6.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel62 }, 0, 0, VE, .unit = "level" },
1216 { NULL, }
1217};
1218
1220
1221#endif // CONFIG_H264_MEDIACODEC_ENCODER
1222
1223#if CONFIG_HEVC_MEDIACODEC_ENCODER
1224
1225enum MediaCodecHevcLevel {
1226 HEVCMainTierLevel1 = 0x1,
1227 HEVCHighTierLevel1 = 0x2,
1228 HEVCMainTierLevel2 = 0x4,
1229 HEVCHighTierLevel2 = 0x8,
1230 HEVCMainTierLevel21 = 0x10,
1231 HEVCHighTierLevel21 = 0x20,
1232 HEVCMainTierLevel3 = 0x40,
1233 HEVCHighTierLevel3 = 0x80,
1234 HEVCMainTierLevel31 = 0x100,
1235 HEVCHighTierLevel31 = 0x200,
1236 HEVCMainTierLevel4 = 0x400,
1237 HEVCHighTierLevel4 = 0x800,
1238 HEVCMainTierLevel41 = 0x1000,
1239 HEVCHighTierLevel41 = 0x2000,
1240 HEVCMainTierLevel5 = 0x4000,
1241 HEVCHighTierLevel5 = 0x8000,
1242 HEVCMainTierLevel51 = 0x10000,
1243 HEVCHighTierLevel51 = 0x20000,
1244 HEVCMainTierLevel52 = 0x40000,
1245 HEVCHighTierLevel52 = 0x80000,
1246 HEVCMainTierLevel6 = 0x100000,
1247 HEVCHighTierLevel6 = 0x200000,
1248 HEVCMainTierLevel61 = 0x400000,
1249 HEVCHighTierLevel61 = 0x800000,
1250 HEVCMainTierLevel62 = 0x1000000,
1251 HEVCHighTierLevel62 = 0x2000000,
1252};
1253
1254static const AVOption hevc_options[] = {
1256
1259
1260 { "level", "Specify tier and level",
1261 OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "level" },
1262 { "m1", "Main tier level 1",
1263 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel1 }, 0, 0, VE, .unit = "level" },
1264 { "h1", "High tier level 1",
1265 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel1 }, 0, 0, VE, .unit = "level" },
1266 { "m2", "Main tier level 2",
1267 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel2 }, 0, 0, VE, .unit = "level" },
1268 { "h2", "High tier level 2",
1269 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel2 }, 0, 0, VE, .unit = "level" },
1270 { "m2.1", "Main tier level 2.1",
1271 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel21 }, 0, 0, VE, .unit = "level" },
1272 { "h2.1", "High tier level 2.1",
1273 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel21 }, 0, 0, VE, .unit = "level" },
1274 { "m3", "Main tier level 3",
1275 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel3 }, 0, 0, VE, .unit = "level" },
1276 { "h3", "High tier level 3",
1277 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel3 }, 0, 0, VE, .unit = "level" },
1278 { "m3.1", "Main tier level 3.1",
1279 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel31 }, 0, 0, VE, .unit = "level" },
1280 { "h3.1", "High tier level 3.1",
1281 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel31 }, 0, 0, VE, .unit = "level" },
1282 { "m4", "Main tier level 4",
1283 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel4 }, 0, 0, VE, .unit = "level" },
1284 { "h4", "High tier level 4",
1285 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel4 }, 0, 0, VE, .unit = "level" },
1286 { "m4.1", "Main tier level 4.1",
1287 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel41 }, 0, 0, VE, .unit = "level" },
1288 { "h4.1", "High tier level 4.1",
1289 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel41 }, 0, 0, VE, .unit = "level" },
1290 { "m5", "Main tier level 5",
1291 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel5 }, 0, 0, VE, .unit = "level" },
1292 { "h5", "High tier level 5",
1293 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel5 }, 0, 0, VE, .unit = "level" },
1294 { "m5.1", "Main tier level 5.1",
1295 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel51 }, 0, 0, VE, .unit = "level" },
1296 { "h5.1", "High tier level 5.1",
1297 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel51 }, 0, 0, VE, .unit = "level" },
1298 { "m5.2", "Main tier level 5.2",
1299 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel52 }, 0, 0, VE, .unit = "level" },
1300 { "h5.2", "High tier level 5.2",
1301 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel52 }, 0, 0, VE, .unit = "level" },
1302 { "m6", "Main tier level 6",
1303 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel6 }, 0, 0, VE, .unit = "level" },
1304 { "h6", "High tier level 6",
1305 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel6 }, 0, 0, VE, .unit = "level" },
1306 { "m6.1", "Main tier level 6.1",
1307 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel61 }, 0, 0, VE, .unit = "level" },
1308 { "h6.1", "High tier level 6.1",
1309 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel61 }, 0, 0, VE, .unit = "level" },
1310 { "m6.2", "Main tier level 6.2",
1311 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel62 }, 0, 0, VE, .unit = "level" },
1312 { "h6.2", "High tier level 6.2",
1313 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel62 }, 0, 0, VE, .unit = "level" },
1314 { NULL, }
1315};
1316
1318
1319#endif // CONFIG_HEVC_MEDIACODEC_ENCODER
1320
1321#if CONFIG_VP8_MEDIACODEC_ENCODER
1322
1323enum MediaCodecVP8Level {
1324 VP8Level_Version0 = 0x01,
1325 VP8Level_Version1 = 0x02,
1326 VP8Level_Version2 = 0x04,
1327 VP8Level_Version3 = 0x08,
1328};
1329
1330static const AVOption vp8_options[] = {
1332 { "level", "Specify tier and level",
1333 OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "level" },
1334 { "V0", "Level Version 0",
1335 0, AV_OPT_TYPE_CONST, { .i64 = VP8Level_Version0 }, 0, 0, VE, .unit = "level" },
1336 { "V1", "Level Version 1",
1337 0, AV_OPT_TYPE_CONST, { .i64 = VP8Level_Version1 }, 0, 0, VE, .unit = "level" },
1338 { "V2", "Level Version 2",
1339 0, AV_OPT_TYPE_CONST, { .i64 = VP8Level_Version2 }, 0, 0, VE, .unit = "level" },
1340 { "V3", "Level Version 3",
1341 0, AV_OPT_TYPE_CONST, { .i64 = VP8Level_Version3 }, 0, 0, VE, .unit = "level" },
1342 { NULL, }
1343};
1344
1346
1347#endif // CONFIG_VP8_MEDIACODEC_ENCODER
1348
1349#if CONFIG_VP9_MEDIACODEC_ENCODER
1350
1351enum MediaCodecVP9Level {
1352 VP9Level1 = 0x1,
1353 VP9Level11 = 0x2,
1354 VP9Level2 = 0x4,
1355 VP9Level21 = 0x8,
1356 VP9Level3 = 0x10,
1357 VP9Level31 = 0x20,
1358 VP9Level4 = 0x40,
1359 VP9Level41 = 0x80,
1360 VP9Level5 = 0x100,
1361 VP9Level51 = 0x200,
1362 VP9Level52 = 0x400,
1363 VP9Level6 = 0x800,
1364 VP9Level61 = 0x1000,
1365 VP9Level62 = 0x2000,
1366};
1367
1368static const AVOption vp9_options[] = {
1370
1371 FF_AVCTX_PROFILE_OPTION("profile0", NULL, VIDEO, AV_PROFILE_VP9_0)
1372 FF_AVCTX_PROFILE_OPTION("profile1", NULL, VIDEO, AV_PROFILE_VP9_1)
1373 FF_AVCTX_PROFILE_OPTION("profile2", NULL, VIDEO, AV_PROFILE_VP9_2)
1374 FF_AVCTX_PROFILE_OPTION("profile3", NULL, VIDEO, AV_PROFILE_VP9_3)
1375
1376 { "level", "Specify tier and level",
1377 OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "level" },
1378 { "1", "Level 1",
1379 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level1 }, 0, 0, VE, .unit = "level" },
1380 { "1.1", "Level 1.1",
1381 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level11 }, 0, 0, VE, .unit = "level" },
1382 { "2", "Level 2",
1383 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level2 }, 0, 0, VE, .unit = "level" },
1384 { "2.1", "Level 2.1",
1385 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level21 }, 0, 0, VE, .unit = "level" },
1386 { "3", "Level 3",
1387 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level3 }, 0, 0, VE, .unit = "level" },
1388 { "3.1", "Level 3.1",
1389 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level31 }, 0, 0, VE, .unit = "level" },
1390 { "4", "Level 4",
1391 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level4 }, 0, 0, VE, .unit = "level" },
1392 { "4.1", "Level 4.1",
1393 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level41 }, 0, 0, VE, .unit = "level" },
1394 { "5", "Level 5",
1395 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level5 }, 0, 0, VE, .unit = "level" },
1396 { "5.1", "Level 5.1",
1397 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level51 }, 0, 0, VE, .unit = "level" },
1398 { "5.2", "Level 5.2",
1399 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level52 }, 0, 0, VE, .unit = "level" },
1400 { "6", "Level 6",
1401 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level6 }, 0, 0, VE, .unit = "level" },
1402 { "6.1", "Level 6.1",
1403 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level61 }, 0, 0, VE, .unit = "level" },
1404 { "6.2", "Level 6.2",
1405 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level62 }, 0, 0, VE, .unit = "level" },
1406 { NULL, }
1407};
1408
1410
1411#endif // CONFIG_VP9_MEDIACODEC_ENCODER
1412
1413#if CONFIG_MPEG4_MEDIACODEC_ENCODER
1414
1415enum MediaCodecMpeg4Level {
1416 MPEG4Level0 = 0x01,
1417 MPEG4Level0b = 0x02,
1418 MPEG4Level1 = 0x04,
1419 MPEG4Level2 = 0x08,
1420 MPEG4Level3 = 0x10,
1421 MPEG4Level3b = 0x18,
1422 MPEG4Level4 = 0x20,
1423 MPEG4Level4a = 0x40,
1424 MPEG4Level5 = 0x80,
1425 MPEG4Level6 = 0x100,
1426};
1427
1428static const AVOption mpeg4_options[] = {
1430
1432
1433 { "level", "Specify tier and level",
1434 OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "level" },
1435 { "0", "Level 0",
1436 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level0 }, 0, 0, VE, .unit = "level" },
1437 { "0b", "Level 0b",
1438 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level0b }, 0, 0, VE, .unit = "level" },
1439 { "1", "Level 1",
1440 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level1 }, 0, 0, VE, .unit = "level" },
1441 { "2", "Level 2",
1442 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level2 }, 0, 0, VE, .unit = "level" },
1443 { "3", "Level 3",
1444 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level3 }, 0, 0, VE, .unit = "level" },
1445 { "3b", "Level 3b",
1446 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level3b }, 0, 0, VE, .unit = "level" },
1447 { "4", "Level 4",
1448 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level4 }, 0, 0, VE, .unit = "level" },
1449 { "4a", "Level 4a",
1450 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level4a }, 0, 0, VE, .unit = "level" },
1451 { "5", "Level 5",
1452 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level5 }, 0, 0, VE, .unit = "level" },
1453 { "6", "Level 6",
1454 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level6 }, 0, 0, VE, .unit = "level" },
1455 { NULL, }
1456};
1457
1459
1460#endif // CONFIG_MPEG4_MEDIACODEC_ENCODER
1461
1462#if CONFIG_AV1_MEDIACODEC_ENCODER
1463
1464enum MediaCodecAV1Level {
1465 AV1Level2 = 0x1,
1466 AV1Level21 = 0x2,
1467 AV1Level22 = 0x4,
1468 AV1Level23 = 0x8,
1469 AV1Level3 = 0x10,
1470 AV1Level31 = 0x20,
1471 AV1Level32 = 0x40,
1472 AV1Level33 = 0x80,
1473 AV1Level4 = 0x100,
1474 AV1Level41 = 0x200,
1475 AV1Level42 = 0x400,
1476 AV1Level43 = 0x800,
1477 AV1Level5 = 0x1000,
1478 AV1Level51 = 0x2000,
1479 AV1Level52 = 0x4000,
1480 AV1Level53 = 0x8000,
1481 AV1Level6 = 0x10000,
1482 AV1Level61 = 0x20000,
1483 AV1Level62 = 0x40000,
1484 AV1Level63 = 0x80000,
1485 AV1Level7 = 0x100000,
1486 AV1Level71 = 0x200000,
1487 AV1Level72 = 0x400000,
1488 AV1Level73 = 0x800000,
1489};
1490
1491static const AVOption av1_options[] = {
1493
1495
1496 { "level", "Specify tier and level",
1497 OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "level" },
1498 { "2", "Level 2",
1499 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level2 }, 0, 0, VE, .unit = "level" },
1500 { "2.1", "Level 2.1",
1501 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level21 }, 0, 0, VE, .unit = "level" },
1502 { "2.2", "Level 2.2",
1503 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level22 }, 0, 0, VE, .unit = "level" },
1504 { "2.3", "Level 2.3",
1505 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level23 }, 0, 0, VE, .unit = "level" },
1506 { "3", "Level 3",
1507 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level3 }, 0, 0, VE, .unit = "level" },
1508 { "3.1", "Level 3.1",
1509 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level31 }, 0, 0, VE, .unit = "level" },
1510 { "3.2", "Level 3.2",
1511 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level32 }, 0, 0, VE, .unit = "level" },
1512 { "3.3", "Level 3.3",
1513 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level33 }, 0, 0, VE, .unit = "level" },
1514 { "4", "Level 4",
1515 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level4 }, 0, 0, VE, .unit = "level" },
1516 { "4.1", "Level 4.1",
1517 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level41 }, 0, 0, VE, .unit = "level" },
1518 { "4.2", "Level 4.2",
1519 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level42 }, 0, 0, VE, .unit = "level" },
1520 { "4.3", "Level 4.3",
1521 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level43 }, 0, 0, VE, .unit = "level" },
1522 { "5", "Level 5",
1523 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level5 }, 0, 0, VE, .unit = "level" },
1524 { "5.1", "Level 5.1",
1525 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level51 }, 0, 0, VE, .unit = "level" },
1526 { "5.2", "Level 5.2",
1527 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level52 }, 0, 0, VE, .unit = "level" },
1528 { "5.3", "Level 5.3",
1529 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level53 }, 0, 0, VE, .unit = "level" },
1530 { "6", "Level 6",
1531 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level6 }, 0, 0, VE, .unit = "level" },
1532 { "6.1", "Level 6.1",
1533 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level61 }, 0, 0, VE, .unit = "level" },
1534 { "6.2", "Level 6.2",
1535 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level62 }, 0, 0, VE, .unit = "level" },
1536 { "6.3", "Level 6.3",
1537 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level63 }, 0, 0, VE, .unit = "level" },
1538 { "7", "Level 7",
1539 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level7 }, 0, 0, VE, .unit = "level" },
1540 { "7.1", "Level 7.1",
1541 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level71 }, 0, 0, VE, .unit = "level" },
1542 { "7.2", "Level 7.2",
1543 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level72 }, 0, 0, VE, .unit = "level" },
1544 { "7.3", "Level 7.3",
1545 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level73 }, 0, 0, VE, .unit = "level" },
1546 { NULL, }
1547};
1548
1550
1551#endif // CONFIG_AV1_MEDIACODEC_ENCODER
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static const char *const format[]
Definition af_aiir.c:445
#define VE
Definition amfenc_av1.c:30
static int out_size
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition avstring.c:103
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Definition codec_par.c:138
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define AV_PROFILE_H264_MAIN
Definition defs.h:126
#define AV_PROFILE_H264_EXTENDED
Definition defs.h:127
#define AV_PROFILE_VP9_3
Definition defs.h:171
#define AV_PROFILE_H264_HIGH_444
Definition defs.h:135
#define AV_PROFILE_HEVC_MAIN
Definition defs.h:173
#define AV_PROFILE_HEVC_MAIN_10
Definition defs.h:174
#define AV_PROFILE_VP9_1
Definition defs.h:169
#define AV_PROFILE_H264_HIGH
Definition defs.h:128
#define AV_PROFILE_H264_CONSTRAINED_BASELINE
Definition defs.h:125
#define AV_PROFILE_VP9_0
Definition defs.h:168
#define AV_PROFILE_VP9_2
Definition defs.h:170
#define AV_PROFILE_H264_HIGH_10
Definition defs.h:129
#define AV_PROFILE_H264_HIGH_422
Definition defs.h:132
#define AV_PROFILE_H264_BASELINE
Definition defs.h:124
static AVPacket * pkt
static enum AVPixelFormat pix_fmt
static AVFrame * frame
size_t fifo_size
Definition dts2pts.c:616
void ff_dts_gen_fill_pkt(AVCodecContext *avctx, FFDTSGen *g, AVPacket *pkt)
Definition dts_gen.c:79
av_cold void ff_dts_gen_reset(FFDTSGen *g)
Definition dts_gen.c:130
void ff_dts_gen_record_pts(FFDTSGen *g, int64_t pts)
Definition dts_gen.c:51
av_cold int ff_dts_gen_init(const AVCodecContext *avctx, FFDTSGen *g, int delay)
Definition dts_gen.c:28
#define FF_DTS_GEN_DELAY_MAX
Definition dts_gen.h:35
#define FF_DTS_GEN_DELAY_AUTO
Definition dts_gen.h:32
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:107
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
Definition encode.c:220
int ret
Definition ffplay.c:391
A generic FIFO API.
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
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:47
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition bsf.c:147
void av_bsf_flush(AVBSFContext *ctx)
Reset the internal bitstream filter state.
Definition bsf.c:188
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition bsf.c:228
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition bsf.c:200
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:524
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition avcodec.h:318
@ AV_CODEC_HW_CONFIG_METHOD_AD_HOC
The codec supports this format by some ad-hoc method.
Definition codec.h:311
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
Definition codec.h:282
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
@ AV_CODEC_ID_VP8
Definition codec_id.h:190
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_VP9
Definition codec_id.h:217
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:657
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition packet.c:252
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition error.h:53
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR_EOF
End of file.
Definition error.h:57
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
Definition fifo.c:47
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
Definition fifo.c:286
void av_fifo_reset2(AVFifo *f)
Definition fifo.c:280
#define AV_FIFO_FLAG_AUTO_GROW
Automatically resize the FIFO on writes, so that the data fits.
Definition fifo.h:63
size_t av_fifo_can_read(const AVFifo *f)
Definition fifo.c:87
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
Definition fifo.c:188
int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
Read data from a FIFO.
Definition fifo.c:240
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:500
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:210
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:53
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
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:289
static void av_image_copy2(uint8_t *const dst_data[4], const int dst_linesizes[4], uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Wrapper around av_image_copy() to workaround the limitation that the conversion from uint8_t * const ...
Definition imgutils.h:184
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
int index
Definition gxfenc.c:90
@ AV_HWDEVICE_TYPE_MEDIACODEC
Definition hwcontext.h:38
misc image utilities
void * av_jni_get_java_vm(void *log_ctx)
Definition jni.c:75
static const AVOption av1_options[]
Definition av1dec.c:1560
static const AVOption h264_options[]
Definition h264dec.c:1182
#define av_cold
Definition attributes.h:117
static int ff_cond_wait(AVCond *cond, AVMutex *mutex)
Definition thread.h:198
static int ff_mutex_unlock(AVMutex *mutex)
Definition thread.h:189
static int ff_cond_destroy(AVCond *cond)
Definition thread.h:195
static int ff_mutex_lock(AVMutex *mutex)
Definition thread.h:188
static int ff_mutex_destroy(AVMutex *mutex)
Definition thread.h:190
static int ff_cond_init(AVCond *cond, const void *attr)
Definition thread.h:194
#define AVCond
Definition thread.h:192
static int ff_cond_signal(AVCond *cond)
Definition thread.h:196
#define AVMutex
Definition thread.h:184
static int ff_mutex_init(AVMutex *mutex, const void *attr)
Definition thread.h:187
#define FFALIGN(x, a)
Definition macros.h:78
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:140
FFANativeWindow * ff_mediacodec_surface_ref(void *surface, void *native_window, void *log_ctx)
int ff_mediacodec_surface_unref(FFANativeWindow *window, void *log_ctx)
FFAMediaCodec * ff_AMediaCodec_createCodecByName(const char *name, int ndk)
FFAMediaFormat * ff_AMediaFormat_new(int ndk)
int ff_AMediaFormatColorTransfer_from_AVColorTransfer(enum AVColorTransferCharacteristic color_transfer)
Map AVColorTransferCharacteristic to MediaFormat color transfer.
int ff_AMediaFormatColorStandard_from_AVColorSpace(enum AVColorSpace color_space)
Map AVColorSpace to MediaFormat color standard.
int ff_AMediaCodecProfile_getProfileFromAVCodecContext(AVCodecContext *avctx)
The following API around MediaCodec and MediaFormat is based on the NDK one provided by Google since ...
FFAMediaCodec * ff_AMediaCodec_createEncoderByType(const char *mime_type, int ndk)
int ff_AMediaFormatColorRange_from_AVColorRange(enum AVColorRange color_range)
Map AVColorRange to MediaFormat color range.
static char * ff_AMediaFormat_toString(FFAMediaFormat *format)
static int ff_AMediaCodec_releaseOutputBuffer(FFAMediaCodec *codec, size_t idx, int render)
static void ff_AMediaFormat_setString(FFAMediaFormat *format, const char *name, const char *value)
static int ff_AMediaCodec_getBufferFlagKeyFrame(FFAMediaCodec *codec)
static FFAMediaFormat * ff_AMediaCodec_getOutputFormat(FFAMediaCodec *codec)
static int ff_AMediaCodec_flush(FFAMediaCodec *codec)
static void ff_AMediaFormat_setInt32(FFAMediaFormat *format, const char *name, int32_t value)
static int ff_AMediaCodec_infoOutputBuffersChanged(FFAMediaCodec *codec, ssize_t idx)
static int ff_AMediaCodec_queueInputBuffer(FFAMediaCodec *codec, size_t idx, off_t offset, size_t size, uint64_t time, uint32_t flags)
static int ff_AMediaCodec_getConfigureFlagEncode(FFAMediaCodec *codec)
static char * ff_AMediaCodec_getName(FFAMediaCodec *codec)
static int ff_AMediaCodec_getBufferFlagEndOfStream(FFAMediaCodec *codec)
static int ff_AMediaCodec_cleanOutputBuffers(FFAMediaCodec *codec)
static int ff_AMediaCodec_setAsyncNotifyCallback(FFAMediaCodec *codec, const FFAMediaCodecOnAsyncNotifyCallback *callback, void *userdata)
@ COLOR_RANGE_UNSPECIFIED
static int ff_AMediaCodec_signalEndOfInputStream(FFAMediaCodec *codec)
static int ff_AMediaFormat_delete(FFAMediaFormat *format)
static int ff_AMediaCodec_configure(FFAMediaCodec *codec, const FFAMediaFormat *format, FFANativeWindow *surface, void *crypto, uint32_t flags)
static int ff_AMediaCodec_start(FFAMediaCodec *codec)
static uint8_t * ff_AMediaCodec_getOutputBuffer(FFAMediaCodec *codec, size_t idx, size_t *out_size)
static ssize_t ff_AMediaCodec_dequeueInputBuffer(FFAMediaCodec *codec, int64_t timeoutUs)
@ COLOR_STANDARD_UNSPECIFIED
static int ff_AMediaCodec_infoOutputFormatChanged(FFAMediaCodec *codec, ssize_t idx)
static uint8_t * ff_AMediaCodec_getInputBuffer(FFAMediaCodec *codec, size_t idx, size_t *out_size)
static int ff_AMediaCodec_delete(FFAMediaCodec *codec)
static int ff_AMediaCodec_stop(FFAMediaCodec *codec)
@ COLOR_TRANSFER_UNSPECIFIED
static ssize_t ff_AMediaCodec_dequeueOutputBuffer(FFAMediaCodec *codec, FFAMediaCodecBufferInfo *info, int64_t timeoutUs)
static int ff_AMediaCodec_getBufferFlagCodecConfig(FFAMediaCodec *codec)
static int ff_AMediaCodec_infoTryAgainLater(FFAMediaCodec *codec, ssize_t idx)
static const AVCodecHWConfigInternal *const mediacodec_hw_configs[]
@ COLOR_FormatYUV420Planar
@ COLOR_FormatYUV420SemiPlanar
#define OUTPUT_DEQUEUE_TIMEOUT_US
#define INPUT_DEQUEUE_TIMEOUT_US
OMX.k3.video.decoder.avc, OMX.NVIDIA.
static const struct @000316305124326106314053112007167062251150035262 color_formats[]
int color_format
@ COLOR_FormatSurface
static void on_output_available(FFAMediaCodec *codec, void *userdata, int32_t index, FFAMediaCodecBufferInfo *out_info)
static int mediacodec_init_async_state(AVCodecContext *avctx)
static const FFCodecDefault mediacodec_defaults[]
static void mediacodec_set_qp_range(AVCodecContext *avctx, FFAMediaFormat *format)
static int mediacodec_encode(AVCodecContext *avctx, AVPacket *pkt)
static int mediacodec_receive(AVCodecContext *avctx, AVPacket *pkt)
static void mediacodec_dump_format(AVCodecContext *avctx, FFAMediaFormat *out_format)
BitrateMode
@ BITRATE_MODE_VBR
@ BITRATE_MODE_CQ
@ BITRATE_MODE_CBR
@ BITRATE_MODE_CBR_FD
static void on_format_changed(FFAMediaCodec *codec, void *userdata, FFAMediaFormat *format)
static void copy_frame_to_buffer(AVCodecContext *avctx, const AVFrame *frame, uint8_t *dst, size_t size)
static enum AVPixelFormat avc_pix_fmts[]
static int mediacodec_get_input_index(AVCodecContext *avctx, ssize_t *index)
static int mediacodec_send(AVCodecContext *avctx, const AVFrame *frame)
static int extract_extradata_support(AVCodecContext *avctx)
static av_cold int mediacodec_init(AVCodecContext *avctx)
static int mediacodec_send_dummy_frame(AVCodecContext *avctx)
static int mediacodec_receive_dummy_pkt(AVCodecContext *avctx, AVPacket *pkt)
static int mediacodec_get_output_index(AVCodecContext *avctx, ssize_t *index, FFAMediaCodecBufferInfo *out_info)
static void mediacodec_uninit_async_state(AVCodecContext *avctx)
#define COMMON_OPTION
static void mediacodec_output_format(AVCodecContext *avctx)
#define OFFSET(x)
static av_cold void mediacodec_flush(AVCodecContext *avctx)
static av_cold int mediacodec_close(AVCodecContext *avctx)
static int mediacodec_generate_extradata(AVCodecContext *avctx)
static void on_input_available(FFAMediaCodec *codec, void *userdata, int32_t index)
#define DECLARE_MEDIACODEC_ENCODER(short_name, long_name, codec_id)
static void on_error(FFAMediaCodec *codec, void *userdata, int error, const char *detail)
static int mediacodec_init_bsf(AVCodecContext *avctx)
Memory handling functions.
AVOptions.
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ 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:96
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_MEDIACODEC
hardware decoding through MediaCodec
Definition pixfmt.h:316
#define FF_AV1_PROFILE_OPTS
Definition profiles.h:56
#define FF_MPEG4_PROFILE_OPTS
Definition profiles.h:42
#define FF_AVCTX_PROFILE_OPTION(name, description, type, value)
Definition profiles.h:26
const char * name
Definition qsvenc.c:142
#define FF_ARRAY_ELEMS(a)
The bitstream filter state.
Definition bsf.h:68
enum AVCodecID * codec_ids
A list of codec ids supported by the filter, terminated by AV_CODEC_ID_NONE.
Definition bsf.h:119
uint8_t * data
The data buffer.
Definition buffer.h:90
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:644
int width
picture width / height.
Definition avcodec.h:605
int global_quality
Global quality for codecs which cannot change it per frame.
Definition avcodec.h:1240
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:685
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:785
int qmin
minimum quantizer
Definition avcodec.h:1257
AVRational framerate
Definition avcodec.h:563
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:672
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition avcodec.h:1025
int qmax
maximum quantizer
Definition avcodec.h:1264
void * hwaccel_context
Legacy hardware accelerator context.
Definition avcodec.h:1453
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:665
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avcodec.h:547
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
Definition avcodec.h:1499
enum AVCodecID codec_id
Definition avcodec.h:453
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
Definition fifo.c:35
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition hwcontext.h:63
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition hwcontext.h:88
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition hwcontext.h:75
This structure holds a reference to a android/view/Surface object that will be used as output by the ...
Definition mediacodec.h:33
void * surface
android/view/Surface object reference.
Definition mediacodec.h:38
void * native_window
Pointer to ANativeWindow.
void * surface
android/view/Surface handle, to be filled by the user.
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
FFAMediaCodecBufferInfo buf_info
FFAMediaCodec * codec
FFANativeWindow * window
AVBSFContext * bsf
uint8_t level
Definition svq3.c:208
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
static int64_t pts
int size
static const AVOption mpeg4_options[]
static double cb(void *priv, double x, double y)
Definition vf_geq.c:247
static const AVOption hevc_options[]