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 "encode.h"
38#include "hwconfig.h"
39#include "jni.h"
40#include "mediacodec.h"
41#include "mediacodec_wrapper.h"
43#include "profiles.h"
44
45#define INPUT_DEQUEUE_TIMEOUT_US 8000
46#define OUTPUT_DEQUEUE_TIMEOUT_US 8000
47
49 /* Constant quality mode */
51 /* Variable bitrate mode */
53 /* Constant bitrate mode */
55 /* Constant bitrate mode with frame drops */
57};
58
63
106
107enum {
111};
112
113static const struct {
116} color_formats[] = {
121
128
130 FFAMediaFormat *out_format)
131{
133 const char *name = s->name;
134 char *str = ff_AMediaFormat_toString(out_format);
135
136 av_log(avctx, AV_LOG_DEBUG, "MediaCodec encoder %s output format %s\n",
137 name ? name : "unknown", str);
138 av_free(str);
139}
140
142{
144 FFAMediaFormat *out_format = ff_AMediaCodec_getOutputFormat(s->codec);
145
146 if (!s->name)
147 s->name = ff_AMediaCodec_getName(s->codec);
148 mediacodec_dump_format(avctx, out_format);
149 ff_AMediaFormat_delete(out_format);
150}
151
153{
154 const AVBitStreamFilter *bsf = av_bsf_get_by_name("extract_extradata");
155
156 if (!bsf) {
157 av_log(avctx, AV_LOG_WARNING, "extract_extradata bsf not found\n");
158 return 0;
159 }
160
161 for (int i = 0; bsf->codec_ids[i] != AV_CODEC_ID_NONE; i++) {
162 if (bsf->codec_ids[i] == avctx->codec_id)
163 return 1;
164 }
165
166 return 0;
167}
168
170{
172 char str[128] = {0};
173 int ret;
174 int crop_right = s->width - avctx->width;
175 int crop_bottom = s->height - avctx->height;
176
177 /* Nothing can be done for this format now */
178 if (avctx->pix_fmt == AV_PIX_FMT_MEDIACODEC)
179 return 0;
180
181 s->extract_extradata = (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) &&
183 if (!crop_right && !crop_bottom && !s->extract_extradata)
184 return 0;
185
186 ret = 0;
187 if (crop_right || crop_bottom) {
188 if (avctx->codec_id == AV_CODEC_ID_H264)
189 ret = snprintf(str, sizeof(str), "h264_metadata=crop_right=%d:crop_bottom=%d",
190 crop_right, crop_bottom);
191 else if (avctx->codec_id == AV_CODEC_ID_HEVC)
192 /* Encoder can use CTU size larger than 16x16, so the real crop
193 * margin can be larger than crop_right/crop_bottom. Let bsf figure
194 * out the real crop margin.
195 */
196 ret = snprintf(str, sizeof(str), "hevc_metadata=width=%d:height=%d",
197 avctx->width, avctx->height);
198 if (ret >= sizeof(str))
200 }
201
202 if (s->extract_extradata) {
203 ret = av_strlcatf(str, sizeof(str), "%sextract_extradata", ret ? "," : "");
204 if (ret >= sizeof(str))
206 }
207
208 ret = av_bsf_list_parse_str(str, &s->bsf);
209 if (ret < 0)
210 return ret;
211
212 ret = avcodec_parameters_from_context(s->bsf->par_in, avctx);
213 if (ret < 0)
214 return ret;
215 s->bsf->time_base_in = avctx->time_base;
216 ret = av_bsf_init(s->bsf);
217
218 return ret;
219}
220
222 uint8_t *dst, size_t size)
223{
225 uint8_t *dst_data[4] = {};
226 int dst_linesize[4] = {};
227
228 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P) {
229 dst_data[0] = dst;
230 dst_data[1] = dst + s->width * s->height;
231 dst_data[2] = dst_data[1] + s->width * s->height / 4;
232
233 dst_linesize[0] = s->width;
234 dst_linesize[1] = dst_linesize[2] = s->width / 2;
235 } else if (avctx->pix_fmt == AV_PIX_FMT_NV12) {
236 dst_data[0] = dst;
237 dst_data[1] = dst + s->width * s->height;
238
239 dst_linesize[0] = s->width;
240 dst_linesize[1] = s->width;
241 } else {
242 av_assert0(0);
243 }
244
245 av_image_copy2(dst_data, dst_linesize, frame->data, frame->linesize,
246 avctx->pix_fmt, avctx->width, avctx->height);
247}
248
249
250static void on_error(FFAMediaCodec *codec, void *userdata, int error,
251 const char *detail)
252{
253 AVCodecContext *avctx = userdata;
255
256 if (error == AVERROR(EAGAIN))
257 return;
258
259 av_log(avctx, AV_LOG_ERROR, "On error, %s, %s\n", av_err2str(error), detail);
260
261 ff_mutex_lock(&s->input_mutex);
262 ff_mutex_lock(&s->output_mutex);
263 s->encode_status = error;
264 ff_mutex_unlock(&s->output_mutex);
265 ff_mutex_unlock(&s->input_mutex);
266
267 ff_cond_signal(&s->output_cond);
268 ff_cond_signal(&s->input_cond);
269}
270
271static void on_input_available(FFAMediaCodec *codec, void *userdata,
273{
274 AVCodecContext *avctx = userdata;
276 int ret;
277
278 ff_mutex_lock(&s->input_mutex);
279 ret = av_fifo_write(s->input_index, &index, 1);
280 if (ret >= 0)
281 ff_cond_signal(&s->input_cond);
282 ff_mutex_unlock(&s->input_mutex);
283
284 if (ret < 0)
285 on_error(codec, userdata, ret, "av_fifo_write failed");
286}
287
288static void on_output_available(FFAMediaCodec *codec, void *userdata,
290 FFAMediaCodecBufferInfo *out_info)
291{
292 AVCodecContext *avctx = userdata;
294 MediaCodecAsyncOutput output = {
295 .index = index,
296 .buf_info = *out_info,
297 };
298 int ret;
299
300 ff_mutex_lock(&s->output_mutex);
301 ret = av_fifo_write(s->async_output, &output, 1);
302 if (ret >= 0)
303 ff_cond_signal(&s->output_cond);
304 ff_mutex_unlock(&s->output_mutex);
305
306 if (ret < 0)
307 on_error(codec, userdata, ret, "av_fifo_write failed");
308}
309
310static void on_format_changed(FFAMediaCodec *codec, void *userdata,
312{
314}
315
317{
319 size_t fifo_size = 16;
320
321 if (!s->async_mode)
322 return 0;
323
324 ff_mutex_init(&s->input_mutex, NULL);
325 ff_cond_init(&s->input_cond, NULL);
326
327 ff_mutex_init(&s->output_mutex, NULL);
328 ff_cond_init(&s->output_cond, NULL);
329
330 s->input_index = av_fifo_alloc2(fifo_size, sizeof(int32_t), AV_FIFO_FLAG_AUTO_GROW);
331 s->async_output = av_fifo_alloc2(fifo_size, sizeof(MediaCodecAsyncOutput),
333
334 if (!s->input_index || !s->async_output)
335 return AVERROR(ENOMEM);
336
337 return 0;
338}
339
341{
343
344 if (!s->async_mode)
345 return;
346
347 ff_mutex_destroy(&s->input_mutex);
348 ff_cond_destroy(&s->input_cond);
349
350 ff_mutex_destroy(&s->output_mutex);
351 ff_cond_destroy(&s->output_cond);
352
353 av_fifo_freep2(&s->input_index);
354 av_fifo_freep2(&s->async_output);
355
356 s->async_mode = 0;
357}
358
360
363{
365
366 // Handle common options in AVCodecContext first.
367 if (avctx->qmin >= 0) {
368 ff_AMediaFormat_setInt32(format, "video-qp-i-min", avctx->qmin);
369 ff_AMediaFormat_setInt32(format, "video-qp-p-min", avctx->qmin);
370 ff_AMediaFormat_setInt32(format, "video-qp-b-min", avctx->qmin);
371 }
372
373 if (avctx->qmax >= 0) {
374 ff_AMediaFormat_setInt32(format, "video-qp-i-max", avctx->qmax);
375 ff_AMediaFormat_setInt32(format, "video-qp-p-max", avctx->qmax);
376 ff_AMediaFormat_setInt32(format, "video-qp-b-max", avctx->qmax);
377 }
378
379 if (s->qp_i_min >= 0)
380 ff_AMediaFormat_setInt32(format, "video-qp-i-min", s->qp_i_min);
381 if (s->qp_p_min >= 0)
382 ff_AMediaFormat_setInt32(format, "video-qp-p-min", s->qp_p_min);
383 if (s->qp_b_min >= 0)
384 ff_AMediaFormat_setInt32(format, "video-qp-b-min", s->qp_b_min);
385
386 if (s->qp_i_max >= 0)
387 ff_AMediaFormat_setInt32(format, "video-qp-i-max", s->qp_i_max);
388 if (s->qp_p_max >= 0)
389 ff_AMediaFormat_setInt32(format, "video-qp-p-max", s->qp_p_max);
390 if (s->qp_b_max >= 0)
391 ff_AMediaFormat_setInt32(format, "video-qp-b-max", s->qp_b_max);
392}
393
395{
396 const char *codec_mime = NULL;
399 int ret;
400 int gop;
401
402 // Init async state first, so we can do cleanup safely on error path.
403 ret = mediacodec_init_async_state(avctx);
404 if (ret < 0)
405 return ret;
406
407 if (s->use_ndk_codec < 0)
408 s->use_ndk_codec = !av_jni_get_java_vm(avctx);
409
410 switch (avctx->codec_id) {
411 case AV_CODEC_ID_H264:
412 codec_mime = "video/avc";
413 break;
414 case AV_CODEC_ID_HEVC:
415 codec_mime = "video/hevc";
416 break;
417 case AV_CODEC_ID_VP8:
418 codec_mime = "video/x-vnd.on2.vp8";
419 break;
420 case AV_CODEC_ID_VP9:
421 codec_mime = "video/x-vnd.on2.vp9";
422 break;
424 codec_mime = "video/mp4v-es";
425 break;
426 case AV_CODEC_ID_AV1:
427 codec_mime = "video/av01";
428 break;
429 default:
430 av_assert0(0);
431 }
432
433 if (s->name)
434 s->codec = ff_AMediaCodec_createCodecByName(s->name, s->use_ndk_codec);
435 else
436 s->codec = ff_AMediaCodec_createEncoderByType(codec_mime, s->use_ndk_codec);
437 if (!s->codec) {
438 av_log(avctx, AV_LOG_ERROR, "Failed to create encoder for type %s\n",
439 codec_mime);
440 return AVERROR_EXTERNAL;
441 }
442
443 format = ff_AMediaFormat_new(s->use_ndk_codec);
444 if (!format) {
445 av_log(avctx, AV_LOG_ERROR, "Failed to create media format\n");
446 return AVERROR_EXTERNAL;
447 }
448
449 ff_AMediaFormat_setString(format, "mime", codec_mime);
450 // Workaround the alignment requirement of mediacodec. We can't do it
451 // silently for AV_PIX_FMT_MEDIACODEC.
452 if (avctx->pix_fmt != AV_PIX_FMT_MEDIACODEC &&
453 (avctx->codec_id == AV_CODEC_ID_H264 ||
454 avctx->codec_id == AV_CODEC_ID_HEVC)) {
455 s->width = FFALIGN(avctx->width, 16);
456 s->height = FFALIGN(avctx->height, 16);
457 } else {
458 s->width = avctx->width;
459 s->height = avctx->height;
460 if (s->width % 16 || s->height % 16)
461 av_log(avctx, AV_LOG_WARNING,
462 "Video size %dx%d isn't align to 16, it may have device compatibility issue\n",
463 s->width, s->height);
464 }
465 ff_AMediaFormat_setInt32(format, "width", s->width);
466 ff_AMediaFormat_setInt32(format, "height", s->height);
467
468 if (avctx->pix_fmt == AV_PIX_FMT_MEDIACODEC) {
469 AVMediaCodecContext *user_ctx = avctx->hwaccel_context;
470 if (avctx->hw_device_ctx) {
471 AVHWDeviceContext *device_ctx = (AVHWDeviceContext*)(avctx->hw_device_ctx->data);
473
474 if (device_ctx->type != AV_HWDEVICE_TYPE_MEDIACODEC || !device_ctx->hwctx) {
475 ret = AVERROR(EINVAL);
476 goto bailout;
477 }
478 dev_ctx = device_ctx->hwctx;
479 s->window = ff_mediacodec_surface_ref(dev_ctx->surface, dev_ctx->native_window, avctx);
480 }
481
482 if (!s->window && user_ctx && user_ctx->surface)
483 s->window = ff_mediacodec_surface_ref(user_ctx->surface, NULL, avctx);
484
485 if (!s->window) {
486 ret = AVERROR(EINVAL);
487 av_log(avctx, AV_LOG_ERROR, "Missing hw_device_ctx or hwaccel_context for AV_PIX_FMT_MEDIACODEC\n");
488 goto bailout;
489 }
490 /* Although there is a method ANativeWindow_toSurface() introduced in
491 * API level 26, it's easier and safe to always require a Surface for
492 * Java MediaCodec.
493 */
494 if (!s->use_ndk_codec && !s->window->surface) {
495 ret = AVERROR(EINVAL);
496 av_log(avctx, AV_LOG_ERROR, "Missing jobject Surface for AV_PIX_FMT_MEDIACODEC. "
497 "Please note that Java MediaCodec doesn't work with ANativeWindow.\n");
498 goto bailout;
499 }
500 }
501
502 for (int i = 0; i < FF_ARRAY_ELEMS(color_formats); i++) {
503 if (avctx->pix_fmt == color_formats[i].pix_fmt) {
504 ff_AMediaFormat_setInt32(format, "color-format",
506 break;
507 }
508 }
509
511 if (ret != COLOR_RANGE_UNSPECIFIED)
512 ff_AMediaFormat_setInt32(format, "color-range", ret);
515 ff_AMediaFormat_setInt32(format, "color-standard", ret);
518 ff_AMediaFormat_setInt32(format, "color-transfer", ret);
519
520 if (avctx->bit_rate)
521 ff_AMediaFormat_setInt32(format, "bitrate", avctx->bit_rate);
522 if (s->bitrate_mode >= 0) {
523 ff_AMediaFormat_setInt32(format, "bitrate-mode", s->bitrate_mode);
524 if (s->bitrate_mode == BITRATE_MODE_CQ && avctx->global_quality > 0)
526 }
528
529 // frame-rate and i-frame-interval are required to configure codec
530 if (avctx->framerate.num >= avctx->framerate.den && avctx->framerate.den > 0) {
531 s->fps = avctx->framerate.num / avctx->framerate.den;
532 } else {
533 s->fps = 30;
534 av_log(avctx, AV_LOG_INFO, "Use %d as the default MediaFormat frame-rate\n", s->fps);
535 }
536 gop = round(avctx->gop_size / s->fps);
537 if (gop == 0) {
538 gop = 1;
539 av_log(avctx, AV_LOG_INFO,
540 "Use %d as the default MediaFormat i-frame-interval, "
541 "please set gop_size properly (>= fps)\n", gop);
542 } else {
543 av_log(avctx, AV_LOG_DEBUG, "Set i-frame-interval to %d\n", gop);
544 }
545
546 ff_AMediaFormat_setInt32(format, "frame-rate", s->fps);
547 ff_AMediaFormat_setInt32(format, "i-frame-interval", gop);
548
550 if (ret > 0) {
551 av_log(avctx, AV_LOG_DEBUG, "set profile to 0x%x\n", ret);
552 ff_AMediaFormat_setInt32(format, "profile", ret);
553 }
554 if (s->level > 0) {
555 av_log(avctx, AV_LOG_DEBUG, "set level to 0x%x\n", s->level);
556 ff_AMediaFormat_setInt32(format, "level", s->level);
557 }
558 if (avctx->max_b_frames > 0) {
560 av_log(avctx, AV_LOG_ERROR,
561 "Enabling B frames will produce packets with no DTS. "
562 "Use -strict experimental to use it anyway.\n");
563 ret = AVERROR(EINVAL);
564 goto bailout;
565 }
566 ff_AMediaFormat_setInt32(format, "max-bframes", avctx->max_b_frames);
567 }
568 if (s->pts_as_dts == -1)
569 s->pts_as_dts = avctx->max_b_frames <= 0;
570 if (s->operating_rate > 0)
571 ff_AMediaFormat_setInt32(format, "operating-rate", s->operating_rate);
572
574 ret = ff_AMediaCodec_configure(s->codec, format, s->window, NULL, ret);
575 if (ret) {
576 av_log(avctx, AV_LOG_ERROR, "MediaCodec configure failed, %s\n", av_err2str(ret));
577 if (avctx->pix_fmt == AV_PIX_FMT_YUV420P)
578 av_log(avctx, AV_LOG_ERROR, "Please try -pix_fmt nv12, some devices don't "
579 "support yuv420p as encoder input format.\n");
580 goto bailout;
581 }
582
583 if (s->async_mode) {
585 .onAsyncInputAvailable = on_input_available,
586 .onAsyncOutputAvailable = on_output_available,
587 .onAsyncFormatChanged = on_format_changed,
588 .onAsyncError = on_error,
589 };
590
591 ret = ff_AMediaCodec_setAsyncNotifyCallback(s->codec, &cb, avctx);
592 if (ret < 0) {
593 av_log(avctx, AV_LOG_WARNING,
594 "Try MediaCodec async mode failed, %s, switch to sync mode\n",
595 av_err2str(ret));
597 }
598 }
599
600 ret = mediacodec_init_bsf(avctx);
601 if (ret)
602 goto bailout;
603
605
606 s->frame = av_frame_alloc();
607 if (!s->frame) {
608 ret = AVERROR(ENOMEM);
609 goto bailout;
610 }
611
612 ret = ff_AMediaCodec_start(s->codec);
613 if (ret) {
614 av_log(avctx, AV_LOG_ERROR, "MediaCodec failed to start, %s\n",
615 av_err2str(ret));
616 goto bailout;
617 }
618
620
621bailout:
622 if (format)
624 return ret;
625}
626
628 FFAMediaCodecBufferInfo *out_info)
629{
631 FFAMediaCodec *codec = s->codec;
632 int64_t timeout_us = s->eof_sent ? OUTPUT_DEQUEUE_TIMEOUT_US : 0;
633 MediaCodecAsyncOutput output = { .index = -1 };
634 int ret;
635
636 if (!s->async_mode) {
637 *index = ff_AMediaCodec_dequeueOutputBuffer(codec, out_info, timeout_us);
638 return 0;
639 }
640
641 ff_mutex_lock(&s->output_mutex);
642
643 while (!s->encode_status) {
644 if (av_fifo_read(s->async_output, &output, 1) >= 0)
645 break;
646
647 // Only wait after signalEndOfInputStream
648 if (s->eof_sent && !s->encode_status)
649 ff_cond_wait(&s->output_cond, &s->output_mutex);
650 else
651 break;
652 }
653
654 ret = s->encode_status;
655 ff_mutex_unlock(&s->output_mutex);
656
657 // Get output index success
658 if (output.index >= 0) {
659 *index = output.index;
660 *out_info = output.buf_info;
661 return 0;
662 }
663
664 return ret ? ret : AVERROR(EAGAIN);
665}
666
668{
670 FFAMediaCodec *codec = s->codec;
671 ssize_t index;
672 FFAMediaCodecBufferInfo out_info = {0};
673 uint8_t *out_buf;
674 size_t out_size = 0;
675 int ret;
676 int extradata_size = 0;
677
678 ret = mediacodec_get_output_index(avctx, &index, &out_info);
679 if (ret < 0)
680 return ret;
681
683 return AVERROR(EAGAIN);
684
687 return AVERROR(EAGAIN);
688 }
689
692 return AVERROR(EAGAIN);
693 }
694
695 if (index < 0)
696 return AVERROR_EXTERNAL;
697
699 return AVERROR_EOF;
700
702 if (!out_buf) {
703 ret = AVERROR_EXTERNAL;
704 goto bailout;
705 }
706
707 if (out_info.flags & ff_AMediaCodec_getBufferFlagCodecConfig(codec)) {
708 if (avctx->codec_id == AV_CODEC_ID_AV1) {
709 // Skip AV1CodecConfigurationRecord without configOBUs
710 if (out_info.size <= 4) {
712 return mediacodec_receive(avctx, pkt);
713 }
714 out_info.size -= 4;
715 out_info.offset += 4;
716 }
717
718 ret = av_reallocp(&s->extradata, out_info.size);
719 if (ret)
720 goto bailout;
721
722 s->extradata_size = out_info.size;
723 memcpy(s->extradata, out_buf + out_info.offset, out_info.size);
725 // try immediately
726 return mediacodec_receive(avctx, pkt);
727 }
728
729 ret = ff_get_encode_buffer(avctx, pkt, out_info.size + s->extradata_size, 0);
730 if (ret < 0)
731 goto bailout;
732
733 if (s->extradata_size) {
734 extradata_size = s->extradata_size;
735 s->extradata_size = 0;
736 memcpy(pkt->data, s->extradata, extradata_size);
737 }
738 memcpy(pkt->data + extradata_size, out_buf + out_info.offset, out_info.size);
740 if (s->pts_as_dts)
741 pkt->dts = pkt->pts;
742 if (out_info.flags & ff_AMediaCodec_getBufferFlagKeyFrame(codec))
743 pkt->flags |= AV_PKT_FLAG_KEY;
744 ret = 0;
745
746 av_log(avctx, AV_LOG_TRACE, "receive packet pts %" PRId64 " dts %" PRId64
747 " flags %d extradata %d\n",
748 pkt->pts, pkt->dts, pkt->flags, extradata_size);
749
750bailout:
752 return ret;
753}
754
756{
758 FFAMediaCodec *codec = s->codec;
759 int ret = 0;
760 int32_t n;
761
762 if (!s->async_mode) {
764 return 0;
765 }
766
767 ff_mutex_lock(&s->input_mutex);
768
769 n = -1;
770 while (n < 0 && !s->encode_status) {
771 if (av_fifo_can_read(s->input_index) > 0) {
772 av_fifo_read(s->input_index, &n, 1);
773 break;
774 }
775
776 if (n < 0 && !s->encode_status)
777 ff_cond_wait(&s->input_cond, &s->input_mutex);
778 }
779
780 ret = s->encode_status;
781 *index = n;
782 ff_mutex_unlock(&s->input_mutex);
783
784 return ret;
785}
786
787
789 const AVFrame *frame) {
791 FFAMediaCodec *codec = s->codec;
792 ssize_t index;
793 uint8_t *input_buf = NULL;
794 size_t input_size = 0;
795 int64_t pts = 0;
796 uint32_t flags = 0;
797 int ret;
798
799 if (s->eof_sent)
800 return 0;
801
802 if (s->window) {
803 if (!frame) {
804 s->eof_sent = 1;
806 }
807
808 if (frame->data[3])
809 av_mediacodec_release_buffer((AVMediaCodecBuffer *)frame->data[3], 1);
810 return 0;
811 }
812
813 ret = mediacodec_get_input_index(avctx, &index);
814 if (ret < 0)
815 return ret;
816
818 return AVERROR(EAGAIN);
819
820 if (index < 0) {
821 av_log(avctx, AV_LOG_ERROR, "dequeue input buffer failed, %zd", index);
822 return AVERROR_EXTERNAL;
823 }
824
825 if (frame) {
826 input_buf = ff_AMediaCodec_getInputBuffer(codec, index, &input_size);
827 copy_frame_to_buffer(avctx, frame, input_buf, input_size);
828
830 } else {
832 s->eof_sent = 1;
833 }
834
835 ff_AMediaCodec_queueInputBuffer(codec, index, 0, input_size, pts, flags);
836 return 0;
837}
838
840{
842 int ret;
843
844 // Return on three case:
845 // 1. Serious error
846 // 2. Got a packet success
847 // 3. No AVFrame is available yet (don't return if get_frame return EOF)
848 while (1) {
849 if (s->bsf) {
850 ret = av_bsf_receive_packet(s->bsf, pkt);
851 if (!ret)
852 return 0;
853 if (ret != AVERROR(EAGAIN))
854 return ret;
855 }
856
857 ret = mediacodec_receive(avctx, pkt);
858 if (s->bsf) {
859 if (!ret || ret == AVERROR_EOF)
860 ret = av_bsf_send_packet(s->bsf, pkt);
861 } else {
862 if (!ret)
863 return 0;
864 }
865
866 if (ret < 0 && ret != AVERROR(EAGAIN))
867 return ret;
868
869 if (!s->frame->buf[0]) {
870 ret = ff_encode_get_frame(avctx, s->frame);
871 if (ret && ret != AVERROR_EOF)
872 return ret;
873 }
874
875 ret = mediacodec_send(avctx, s->frame->buf[0] ? s->frame : NULL);
876 if (!ret)
877 av_frame_unref(s->frame);
878 else if (ret != AVERROR(EAGAIN))
879 return ret;
880 }
881
882 return 0;
883}
884
886{
888 int ret;
889
890 s->frame->width = avctx->width;
891 s->frame->height = avctx->height;
892 s->frame->format = avctx->pix_fmt;
893 s->frame->pts = 0;
894
895 ret = av_frame_get_buffer(s->frame, 0);
896 if (ret < 0)
897 return ret;
898
899 do {
900 ret = mediacodec_send(avctx, s->frame);
901 } while (ret == AVERROR(EAGAIN));
902 av_frame_unref(s->frame);
903
904 if (ret < 0)
905 return ret;
906
907 ret = mediacodec_send(avctx, NULL);
908 if (ret < 0) {
909 av_log(avctx, AV_LOG_ERROR, "Flush failed: %s\n", av_err2str(ret));
910 return ret;
911 }
912
913 return 0;
914}
915
917{
919 int ret;
920
921 do {
922 ret = mediacodec_receive(avctx, pkt);
923 } while (ret == AVERROR(EAGAIN));
924
925 if (ret < 0)
926 return ret;
927
928 do {
929 ret = av_bsf_send_packet(s->bsf, pkt);
930 if (ret < 0)
931 return ret;
932 ret = av_bsf_receive_packet(s->bsf, pkt);
933 } while (ret == AVERROR(EAGAIN));
934
935 return ret;
936}
937
939{
941 AVPacket *pkt = NULL;
942 int ret;
943 size_t side_size;
944 uint8_t *side;
945
946 if (!(avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER))
947 return 0;
948
949 // Send dummy frame and receive a packet doesn't work in async mode
950 if (s->async_mode || !s->extract_extradata) {
951 av_log(avctx, AV_LOG_WARNING,
952 "Mediacodec encoder doesn't support AV_CODEC_FLAG_GLOBAL_HEADER. "
953 "Use extract_extradata bsf when necessary.\n");
954 return 0;
955 }
956
958 if (!pkt)
959 return AVERROR(ENOMEM);
960
961 ret = mediacodec_send_dummy_frame(avctx);
962 if (ret < 0)
963 goto bailout;
964 ret = mediacodec_receive_dummy_pkt(avctx, pkt);
965 if (ret < 0)
966 goto bailout;
967
969 if (side && side_size > 0) {
971 if (!avctx->extradata) {
972 ret = AVERROR(ENOMEM);
973 goto bailout;
974 }
975
976 memcpy(avctx->extradata, side, side_size);
977 avctx->extradata_size = side_size;
978 }
979
980bailout:
981 if (s->eof_sent) {
982 s->eof_sent = 0;
983 ff_AMediaCodec_flush(s->codec);
984 }
985 av_bsf_flush(s->bsf);
987 return ret;
988}
989
991{
993 if (s->codec) {
994 ff_AMediaCodec_stop(s->codec);
995 ff_AMediaCodec_delete(s->codec);
996 s->codec = NULL;
997 }
998
999 if (s->window) {
1000 ff_mediacodec_surface_unref(s->window, avctx);
1001 s->window = NULL;
1002 }
1003
1004 av_bsf_free(&s->bsf);
1005 av_frame_free(&s->frame);
1006
1008
1009 return 0;
1010}
1011
1013{
1015 if (s->bsf)
1016 av_bsf_flush(s->bsf);
1017 av_frame_unref(s->frame);
1018 ff_AMediaCodec_flush(s->codec);
1019}
1020
1022 &(const AVCodecHWConfigInternal) {
1023 .public = {
1024 .pix_fmt = AV_PIX_FMT_MEDIACODEC,
1027 .device_type = AV_HWDEVICE_TYPE_MEDIACODEC,
1028 },
1029 .hwaccel = NULL,
1030 },
1031 NULL
1032};
1033
1035 {"qmin", "-1"},
1036 {"qmax", "-1"},
1037 {NULL},
1038};
1039
1040#define OFFSET(x) offsetof(MediaCodecEncContext, x)
1041#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1042#define COMMON_OPTION \
1043 { "ndk_codec", "Use MediaCodec from NDK", \
1044 OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
1045 { "ndk_async", "Try NDK MediaCodec in async mode", \
1046 OFFSET(async_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VE }, \
1047 { "codec_name", "Select codec by name", \
1048 OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, \
1049 { "bitrate_mode", "Bitrate control method", \
1050 OFFSET(bitrate_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, .unit = "bitrate_mode" }, \
1051 { "cq", "Constant quality mode", \
1052 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CQ}, 0, 0, VE, .unit = "bitrate_mode" }, \
1053 { "vbr", "Variable bitrate mode", \
1054 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_VBR}, 0, 0, VE, .unit = "bitrate_mode" }, \
1055 { "cbr", "Constant bitrate mode", \
1056 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, .unit = "bitrate_mode" }, \
1057 { "cbr_fd", "Constant bitrate mode with frame drops", \
1058 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, VE, .unit = "bitrate_mode" }, \
1059 { "pts_as_dts", "Use PTS as DTS. It is enabled automatically if avctx max_b_frames <= 0, " \
1060 "since most of Android devices don't output B frames by default.", \
1061 OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
1062 { "operating_rate", "The desired operating rate that the codec will need to operate at, zero for unspecified", \
1063 OFFSET(operating_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE }, \
1064 { "qp_i_min", "minimum quantization parameter for I frame", \
1065 OFFSET(qp_i_min), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
1066 { "qp_p_min", "minimum quantization parameter for P frame", \
1067 OFFSET(qp_p_min), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
1068 { "qp_b_min", "minimum quantization parameter for B frame", \
1069 OFFSET(qp_b_min), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
1070 { "qp_i_max", "maximum quantization parameter for I frame", \
1071 OFFSET(qp_i_max), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
1072 { "qp_p_max", "maximum quantization parameter for P frame", \
1073 OFFSET(qp_p_max), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
1074 { "qp_b_max", "maximum quantization parameter for B frame", \
1075 OFFSET(qp_b_max), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE }, \
1076
1077
1078#define MEDIACODEC_ENCODER_CLASS(name) \
1079static const AVClass name ## _mediacodec_class = { \
1080 .class_name = #name "_mediacodec", \
1081 .item_name = av_default_item_name, \
1082 .option = name ## _options, \
1083 .version = LIBAVUTIL_VERSION_INT, \
1084}; \
1085
1086#define DECLARE_MEDIACODEC_ENCODER(short_name, long_name, codec_id) \
1087MEDIACODEC_ENCODER_CLASS(short_name) \
1088const FFCodec ff_ ## short_name ## _mediacodec_encoder = { \
1089 .p.name = #short_name "_mediacodec", \
1090 CODEC_LONG_NAME(long_name " Android MediaCodec encoder"), \
1091 .p.type = AVMEDIA_TYPE_VIDEO, \
1092 .p.id = codec_id, \
1093 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | \
1094 AV_CODEC_CAP_HARDWARE | \
1095 AV_CODEC_CAP_ENCODER_FLUSH, \
1096 .priv_data_size = sizeof(MediaCodecEncContext), \
1097 CODEC_PIXFMTS_ARRAY(avc_pix_fmts), \
1098 .color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG, \
1099 .defaults = mediacodec_defaults, \
1100 .init = mediacodec_init, \
1101 FF_CODEC_RECEIVE_PACKET_CB(mediacodec_encode), \
1102 .close = mediacodec_close, \
1103 .flush = mediacodec_flush, \
1104 .p.priv_class = &short_name ## _mediacodec_class, \
1105 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
1106 .p.wrapper_name = "mediacodec", \
1107 .hw_configs = mediacodec_hw_configs, \
1108}; \
1109
1110#if CONFIG_H264_MEDIACODEC_ENCODER
1111
1112enum MediaCodecAvcLevel {
1113 AVCLevel1 = 0x01,
1114 AVCLevel1b = 0x02,
1115 AVCLevel11 = 0x04,
1116 AVCLevel12 = 0x08,
1117 AVCLevel13 = 0x10,
1118 AVCLevel2 = 0x20,
1119 AVCLevel21 = 0x40,
1120 AVCLevel22 = 0x80,
1121 AVCLevel3 = 0x100,
1122 AVCLevel31 = 0x200,
1123 AVCLevel32 = 0x400,
1124 AVCLevel4 = 0x800,
1125 AVCLevel41 = 0x1000,
1126 AVCLevel42 = 0x2000,
1127 AVCLevel5 = 0x4000,
1128 AVCLevel51 = 0x8000,
1129 AVCLevel52 = 0x10000,
1130 AVCLevel6 = 0x20000,
1131 AVCLevel61 = 0x40000,
1132 AVCLevel62 = 0x80000,
1133};
1134
1135static const AVOption h264_options[] = {
1137
1146
1147 { "level", "Specify level",
1148 OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "level" },
1149 { "1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel1 }, 0, 0, VE, .unit = "level" },
1150 { "1b", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel1b }, 0, 0, VE, .unit = "level" },
1151 { "1.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel11 }, 0, 0, VE, .unit = "level" },
1152 { "1.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel12 }, 0, 0, VE, .unit = "level" },
1153 { "1.3", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel13 }, 0, 0, VE, .unit = "level" },
1154 { "2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel2 }, 0, 0, VE, .unit = "level" },
1155 { "2.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel21 }, 0, 0, VE, .unit = "level" },
1156 { "2.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel22 }, 0, 0, VE, .unit = "level" },
1157 { "3", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel3 }, 0, 0, VE, .unit = "level" },
1158 { "3.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel31 }, 0, 0, VE, .unit = "level" },
1159 { "3.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel32 }, 0, 0, VE, .unit = "level" },
1160 { "4", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel4 }, 0, 0, VE, .unit = "level" },
1161 { "4.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel41 }, 0, 0, VE, .unit = "level" },
1162 { "4.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel42 }, 0, 0, VE, .unit = "level" },
1163 { "5", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel5 }, 0, 0, VE, .unit = "level" },
1164 { "5.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel51 }, 0, 0, VE, .unit = "level" },
1165 { "5.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel52 }, 0, 0, VE, .unit = "level" },
1166 { "6.0", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel6 }, 0, 0, VE, .unit = "level" },
1167 { "6.1", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel61 }, 0, 0, VE, .unit = "level" },
1168 { "6.2", "", 0, AV_OPT_TYPE_CONST, { .i64 = AVCLevel62 }, 0, 0, VE, .unit = "level" },
1169 { NULL, }
1170};
1171
1173
1174#endif // CONFIG_H264_MEDIACODEC_ENCODER
1175
1176#if CONFIG_HEVC_MEDIACODEC_ENCODER
1177
1178enum MediaCodecHevcLevel {
1179 HEVCMainTierLevel1 = 0x1,
1180 HEVCHighTierLevel1 = 0x2,
1181 HEVCMainTierLevel2 = 0x4,
1182 HEVCHighTierLevel2 = 0x8,
1183 HEVCMainTierLevel21 = 0x10,
1184 HEVCHighTierLevel21 = 0x20,
1185 HEVCMainTierLevel3 = 0x40,
1186 HEVCHighTierLevel3 = 0x80,
1187 HEVCMainTierLevel31 = 0x100,
1188 HEVCHighTierLevel31 = 0x200,
1189 HEVCMainTierLevel4 = 0x400,
1190 HEVCHighTierLevel4 = 0x800,
1191 HEVCMainTierLevel41 = 0x1000,
1192 HEVCHighTierLevel41 = 0x2000,
1193 HEVCMainTierLevel5 = 0x4000,
1194 HEVCHighTierLevel5 = 0x8000,
1195 HEVCMainTierLevel51 = 0x10000,
1196 HEVCHighTierLevel51 = 0x20000,
1197 HEVCMainTierLevel52 = 0x40000,
1198 HEVCHighTierLevel52 = 0x80000,
1199 HEVCMainTierLevel6 = 0x100000,
1200 HEVCHighTierLevel6 = 0x200000,
1201 HEVCMainTierLevel61 = 0x400000,
1202 HEVCHighTierLevel61 = 0x800000,
1203 HEVCMainTierLevel62 = 0x1000000,
1204 HEVCHighTierLevel62 = 0x2000000,
1205};
1206
1207static const AVOption hevc_options[] = {
1209
1212
1213 { "level", "Specify tier and level",
1214 OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "level" },
1215 { "m1", "Main tier level 1",
1216 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel1 }, 0, 0, VE, .unit = "level" },
1217 { "h1", "High tier level 1",
1218 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel1 }, 0, 0, VE, .unit = "level" },
1219 { "m2", "Main tier level 2",
1220 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel2 }, 0, 0, VE, .unit = "level" },
1221 { "h2", "High tier level 2",
1222 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel2 }, 0, 0, VE, .unit = "level" },
1223 { "m2.1", "Main tier level 2.1",
1224 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel21 }, 0, 0, VE, .unit = "level" },
1225 { "h2.1", "High tier level 2.1",
1226 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel21 }, 0, 0, VE, .unit = "level" },
1227 { "m3", "Main tier level 3",
1228 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel3 }, 0, 0, VE, .unit = "level" },
1229 { "h3", "High tier level 3",
1230 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel3 }, 0, 0, VE, .unit = "level" },
1231 { "m3.1", "Main tier level 3.1",
1232 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel31 }, 0, 0, VE, .unit = "level" },
1233 { "h3.1", "High tier level 3.1",
1234 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel31 }, 0, 0, VE, .unit = "level" },
1235 { "m4", "Main tier level 4",
1236 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel4 }, 0, 0, VE, .unit = "level" },
1237 { "h4", "High tier level 4",
1238 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel4 }, 0, 0, VE, .unit = "level" },
1239 { "m4.1", "Main tier level 4.1",
1240 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel41 }, 0, 0, VE, .unit = "level" },
1241 { "h4.1", "High tier level 4.1",
1242 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel41 }, 0, 0, VE, .unit = "level" },
1243 { "m5", "Main tier level 5",
1244 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel5 }, 0, 0, VE, .unit = "level" },
1245 { "h5", "High tier level 5",
1246 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel5 }, 0, 0, VE, .unit = "level" },
1247 { "m5.1", "Main tier level 5.1",
1248 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel51 }, 0, 0, VE, .unit = "level" },
1249 { "h5.1", "High tier level 5.1",
1250 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel51 }, 0, 0, VE, .unit = "level" },
1251 { "m5.2", "Main tier level 5.2",
1252 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel52 }, 0, 0, VE, .unit = "level" },
1253 { "h5.2", "High tier level 5.2",
1254 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel52 }, 0, 0, VE, .unit = "level" },
1255 { "m6", "Main tier level 6",
1256 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel6 }, 0, 0, VE, .unit = "level" },
1257 { "h6", "High tier level 6",
1258 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel6 }, 0, 0, VE, .unit = "level" },
1259 { "m6.1", "Main tier level 6.1",
1260 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel61 }, 0, 0, VE, .unit = "level" },
1261 { "h6.1", "High tier level 6.1",
1262 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel61 }, 0, 0, VE, .unit = "level" },
1263 { "m6.2", "Main tier level 6.2",
1264 0, AV_OPT_TYPE_CONST, { .i64 = HEVCMainTierLevel62 }, 0, 0, VE, .unit = "level" },
1265 { "h6.2", "High tier level 6.2",
1266 0, AV_OPT_TYPE_CONST, { .i64 = HEVCHighTierLevel62 }, 0, 0, VE, .unit = "level" },
1267 { NULL, }
1268};
1269
1271
1272#endif // CONFIG_HEVC_MEDIACODEC_ENCODER
1273
1274#if CONFIG_VP8_MEDIACODEC_ENCODER
1275
1276enum MediaCodecVP8Level {
1277 VP8Level_Version0 = 0x01,
1278 VP8Level_Version1 = 0x02,
1279 VP8Level_Version2 = 0x04,
1280 VP8Level_Version3 = 0x08,
1281};
1282
1283static const AVOption vp8_options[] = {
1285 { "level", "Specify tier and level",
1286 OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "level" },
1287 { "V0", "Level Version 0",
1288 0, AV_OPT_TYPE_CONST, { .i64 = VP8Level_Version0 }, 0, 0, VE, .unit = "level" },
1289 { "V1", "Level Version 1",
1290 0, AV_OPT_TYPE_CONST, { .i64 = VP8Level_Version1 }, 0, 0, VE, .unit = "level" },
1291 { "V2", "Level Version 2",
1292 0, AV_OPT_TYPE_CONST, { .i64 = VP8Level_Version2 }, 0, 0, VE, .unit = "level" },
1293 { "V3", "Level Version 3",
1294 0, AV_OPT_TYPE_CONST, { .i64 = VP8Level_Version3 }, 0, 0, VE, .unit = "level" },
1295 { NULL, }
1296};
1297
1299
1300#endif // CONFIG_VP8_MEDIACODEC_ENCODER
1301
1302#if CONFIG_VP9_MEDIACODEC_ENCODER
1303
1304enum MediaCodecVP9Level {
1305 VP9Level1 = 0x1,
1306 VP9Level11 = 0x2,
1307 VP9Level2 = 0x4,
1308 VP9Level21 = 0x8,
1309 VP9Level3 = 0x10,
1310 VP9Level31 = 0x20,
1311 VP9Level4 = 0x40,
1312 VP9Level41 = 0x80,
1313 VP9Level5 = 0x100,
1314 VP9Level51 = 0x200,
1315 VP9Level52 = 0x400,
1316 VP9Level6 = 0x800,
1317 VP9Level61 = 0x1000,
1318 VP9Level62 = 0x2000,
1319};
1320
1321static const AVOption vp9_options[] = {
1323
1324 FF_AVCTX_PROFILE_OPTION("profile0", NULL, VIDEO, AV_PROFILE_VP9_0)
1325 FF_AVCTX_PROFILE_OPTION("profile1", NULL, VIDEO, AV_PROFILE_VP9_1)
1326 FF_AVCTX_PROFILE_OPTION("profile2", NULL, VIDEO, AV_PROFILE_VP9_2)
1327 FF_AVCTX_PROFILE_OPTION("profile3", NULL, VIDEO, AV_PROFILE_VP9_3)
1328
1329 { "level", "Specify tier and level",
1330 OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "level" },
1331 { "1", "Level 1",
1332 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level1 }, 0, 0, VE, .unit = "level" },
1333 { "1.1", "Level 1.1",
1334 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level11 }, 0, 0, VE, .unit = "level" },
1335 { "2", "Level 2",
1336 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level2 }, 0, 0, VE, .unit = "level" },
1337 { "2.1", "Level 2.1",
1338 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level21 }, 0, 0, VE, .unit = "level" },
1339 { "3", "Level 3",
1340 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level3 }, 0, 0, VE, .unit = "level" },
1341 { "3.1", "Level 3.1",
1342 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level31 }, 0, 0, VE, .unit = "level" },
1343 { "4", "Level 4",
1344 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level4 }, 0, 0, VE, .unit = "level" },
1345 { "4.1", "Level 4.1",
1346 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level41 }, 0, 0, VE, .unit = "level" },
1347 { "5", "Level 5",
1348 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level5 }, 0, 0, VE, .unit = "level" },
1349 { "5.1", "Level 5.1",
1350 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level51 }, 0, 0, VE, .unit = "level" },
1351 { "5.2", "Level 5.2",
1352 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level52 }, 0, 0, VE, .unit = "level" },
1353 { "6", "Level 6",
1354 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level6 }, 0, 0, VE, .unit = "level" },
1355 { "6.1", "Level 6.1",
1356 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level61 }, 0, 0, VE, .unit = "level" },
1357 { "6.2", "Level 6.2",
1358 0, AV_OPT_TYPE_CONST, { .i64 = VP9Level62 }, 0, 0, VE, .unit = "level" },
1359 { NULL, }
1360};
1361
1363
1364#endif // CONFIG_VP9_MEDIACODEC_ENCODER
1365
1366#if CONFIG_MPEG4_MEDIACODEC_ENCODER
1367
1368enum MediaCodecMpeg4Level {
1369 MPEG4Level0 = 0x01,
1370 MPEG4Level0b = 0x02,
1371 MPEG4Level1 = 0x04,
1372 MPEG4Level2 = 0x08,
1373 MPEG4Level3 = 0x10,
1374 MPEG4Level3b = 0x18,
1375 MPEG4Level4 = 0x20,
1376 MPEG4Level4a = 0x40,
1377 MPEG4Level5 = 0x80,
1378 MPEG4Level6 = 0x100,
1379};
1380
1381static const AVOption mpeg4_options[] = {
1383
1385
1386 { "level", "Specify tier and level",
1387 OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "level" },
1388 { "0", "Level 0",
1389 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level0 }, 0, 0, VE, .unit = "level" },
1390 { "0b", "Level 0b",
1391 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level0b }, 0, 0, VE, .unit = "level" },
1392 { "1", "Level 1",
1393 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level1 }, 0, 0, VE, .unit = "level" },
1394 { "2", "Level 2",
1395 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level2 }, 0, 0, VE, .unit = "level" },
1396 { "3", "Level 3",
1397 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level3 }, 0, 0, VE, .unit = "level" },
1398 { "3b", "Level 3b",
1399 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level3b }, 0, 0, VE, .unit = "level" },
1400 { "4", "Level 4",
1401 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level4 }, 0, 0, VE, .unit = "level" },
1402 { "4a", "Level 4a",
1403 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level4a }, 0, 0, VE, .unit = "level" },
1404 { "5", "Level 5",
1405 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level5 }, 0, 0, VE, .unit = "level" },
1406 { "6", "Level 6",
1407 0, AV_OPT_TYPE_CONST, { .i64 = MPEG4Level6 }, 0, 0, VE, .unit = "level" },
1408 { NULL, }
1409};
1410
1412
1413#endif // CONFIG_MPEG4_MEDIACODEC_ENCODER
1414
1415#if CONFIG_AV1_MEDIACODEC_ENCODER
1416
1417enum MediaCodecAV1Level {
1418 AV1Level2 = 0x1,
1419 AV1Level21 = 0x2,
1420 AV1Level22 = 0x4,
1421 AV1Level23 = 0x8,
1422 AV1Level3 = 0x10,
1423 AV1Level31 = 0x20,
1424 AV1Level32 = 0x40,
1425 AV1Level33 = 0x80,
1426 AV1Level4 = 0x100,
1427 AV1Level41 = 0x200,
1428 AV1Level42 = 0x400,
1429 AV1Level43 = 0x800,
1430 AV1Level5 = 0x1000,
1431 AV1Level51 = 0x2000,
1432 AV1Level52 = 0x4000,
1433 AV1Level53 = 0x8000,
1434 AV1Level6 = 0x10000,
1435 AV1Level61 = 0x20000,
1436 AV1Level62 = 0x40000,
1437 AV1Level63 = 0x80000,
1438 AV1Level7 = 0x100000,
1439 AV1Level71 = 0x200000,
1440 AV1Level72 = 0x400000,
1441 AV1Level73 = 0x800000,
1442};
1443
1444static const AVOption av1_options[] = {
1446
1448
1449 { "level", "Specify tier and level",
1450 OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "level" },
1451 { "2", "Level 2",
1452 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level2 }, 0, 0, VE, .unit = "level" },
1453 { "2.1", "Level 2.1",
1454 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level21 }, 0, 0, VE, .unit = "level" },
1455 { "2.2", "Level 2.2",
1456 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level22 }, 0, 0, VE, .unit = "level" },
1457 { "2.3", "Level 2.3",
1458 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level23 }, 0, 0, VE, .unit = "level" },
1459 { "3", "Level 3",
1460 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level3 }, 0, 0, VE, .unit = "level" },
1461 { "3.1", "Level 3.1",
1462 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level31 }, 0, 0, VE, .unit = "level" },
1463 { "3.2", "Level 3.2",
1464 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level32 }, 0, 0, VE, .unit = "level" },
1465 { "3.3", "Level 3.3",
1466 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level33 }, 0, 0, VE, .unit = "level" },
1467 { "4", "Level 4",
1468 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level4 }, 0, 0, VE, .unit = "level" },
1469 { "4.1", "Level 4.1",
1470 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level41 }, 0, 0, VE, .unit = "level" },
1471 { "4.2", "Level 4.2",
1472 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level42 }, 0, 0, VE, .unit = "level" },
1473 { "4.3", "Level 4.3",
1474 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level43 }, 0, 0, VE, .unit = "level" },
1475 { "5", "Level 5",
1476 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level5 }, 0, 0, VE, .unit = "level" },
1477 { "5.1", "Level 5.1",
1478 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level51 }, 0, 0, VE, .unit = "level" },
1479 { "5.2", "Level 5.2",
1480 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level52 }, 0, 0, VE, .unit = "level" },
1481 { "5.3", "Level 5.3",
1482 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level53 }, 0, 0, VE, .unit = "level" },
1483 { "6", "Level 6",
1484 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level6 }, 0, 0, VE, .unit = "level" },
1485 { "6.1", "Level 6.1",
1486 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level61 }, 0, 0, VE, .unit = "level" },
1487 { "6.2", "Level 6.2",
1488 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level62 }, 0, 0, VE, .unit = "level" },
1489 { "6.3", "Level 6.3",
1490 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level63 }, 0, 0, VE, .unit = "level" },
1491 { "7", "Level 7",
1492 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level7 }, 0, 0, VE, .unit = "level" },
1493 { "7.1", "Level 7.1",
1494 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level71 }, 0, 0, VE, .unit = "level" },
1495 { "7.2", "Level 7.2",
1496 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level72 }, 0, 0, VE, .unit = "level" },
1497 { "7.3", "Level 7.3",
1498 0, AV_OPT_TYPE_CONST, { .i64 = AV1Level73 }, 0, 0, VE, .unit = "level" },
1499 { NULL, }
1500};
1501
1503
1504#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:87
static const char *const format[]
Definition af_aiir.c:444
#define VE
Definition amfenc_av1.c:30
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 NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition defs.h:62
#define AV_PROFILE_H264_MAIN
Definition defs.h:112
#define AV_PROFILE_H264_EXTENDED
Definition defs.h:113
#define AV_PROFILE_VP9_3
Definition defs.h:157
#define AV_PROFILE_H264_HIGH_444
Definition defs.h:121
#define AV_PROFILE_HEVC_MAIN
Definition defs.h:159
#define AV_PROFILE_HEVC_MAIN_10
Definition defs.h:160
#define AV_PROFILE_VP9_1
Definition defs.h:155
#define AV_PROFILE_H264_HIGH
Definition defs.h:114
#define AV_PROFILE_H264_CONSTRAINED_BASELINE
Definition defs.h:111
#define AV_PROFILE_VP9_0
Definition defs.h:154
#define AV_PROFILE_VP9_2
Definition defs.h:156
#define AV_PROFILE_H264_HIGH_10
Definition defs.h:115
#define AV_PROFILE_H264_HIGH_422
Definition defs.h:118
#define AV_PROFILE_H264_BASELINE
Definition defs.h:110
static AVPacket * pkt
static enum AVPixelFormat pix_fmt
static AVFrame * frame
size_t fifo_size
Definition dts2pts.c:611
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
Definition encode.c:218
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:650
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
#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:496
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:206
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#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:188
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:1533
static const AVOption h264_options[]
Definition h264dec.c:1092
#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
static av_always_inline av_const double round(double x)
Definition libm.h:446
#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:138
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)
#define snprintf
Definition snprintf.h:34
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:643
int width
picture width / height.
Definition avcodec.h:604
int global_quality
Global quality for codecs which cannot change it per frame.
Definition avcodec.h:1235
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:681
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition avcodec.h:1375
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:781
int qmin
minimum quantizer
Definition avcodec.h:1252
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:671
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition avcodec.h:1021
int qmax
maximum quantizer
Definition avcodec.h:1259
void * hwaccel_context
Legacy hardware accelerator context.
Definition avcodec.h:1447
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:664
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:1493
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:472
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:580
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_log(a,...)
static void error(const char *err)
static int out_size
Definition movenc.c:56
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[]