Go to the documentation of this file.
23 #include "config_components.h"
40 #define INPUT_DEQUEUE_TIMEOUT_US 8000
41 #define OUTPUT_DEQUEUE_TIMEOUT_US 8000
118 int crop_right =
s->width - avctx->
width;
119 int crop_bottom =
s->height - avctx->
height;
121 if (!crop_right && !crop_bottom)
126 crop_right, crop_bottom);
129 crop_right, crop_bottom);
151 const char *codec_mime =
NULL;
157 if (
s->use_ndk_codec < 0)
162 codec_mime =
"video/avc";
165 codec_mime =
"video/hevc";
196 if (
s->width % 16 ||
s->height % 16)
198 "Video size %dx%d isn't align to 16, it may have device compatibility issue\n",
199 s->width,
s->height);
214 dev_ctx = device_ctx->
hwctx;
218 if (!
s->window && user_ctx && user_ctx->
surface)
223 av_log(avctx,
AV_LOG_ERROR,
"Missing hw_device_ctx or hwaccel_context for AV_PIX_FMT_MEDIACODEC\n");
230 if (!
s->use_ndk_codec && !
s->window->surface) {
233 "Please note that Java MediaCodec doesn't work with ANativeWindow.\n");
258 if (
s->bitrate_mode >= 0)
271 "Use %d as the default MediaFormat i-frame-interval, "
272 "please set gop_size properly (>= fps)\n", gop);
292 "Enabling B frames will produce packets with no DTS. "
293 "Use -strict experimental to use it anyway.\n");
299 if (
s->pts_as_dts == -1)
341 int extradata_size = 0;
375 s->extradata_size = out_info.
size;
376 memcpy(
s->extradata, out_buf + out_info.
offset, out_info.
size);
386 if (
s->extradata_size) {
387 extradata_size =
s->extradata_size;
388 s->extradata_size = 0;
389 memcpy(
pkt->
data,
s->extradata, extradata_size);
401 " flags %d extradata %d\n",
412 uint8_t *dst_data[4] = {};
413 int dst_linesize[4] = {};
414 const uint8_t *src_data[4] = {
420 dst_data[1] = dst +
s->width *
s->height;
421 dst_data[2] = dst_data[1] +
s->width *
s->height / 4;
423 dst_linesize[0] =
s->width;
424 dst_linesize[1] = dst_linesize[2] =
s->width / 2;
427 dst_data[1] = dst +
s->width *
s->height;
429 dst_linesize[0] =
s->width;
430 dst_linesize[1] =
s->width;
444 uint8_t *input_buf =
NULL;
445 size_t input_size = 0;
519 if (!
s->frame->buf[0]) {
568 #define OFFSET(x) offsetof(MediaCodecEncContext, x)
569 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
570 #define COMMON_OPTION \
571 { "ndk_codec", "Use MediaCodec from NDK", \
572 OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
573 { "codec_name", "Select codec by name", \
574 OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, \
575 { "bitrate_mode", "Bitrate control method", \
576 OFFSET(bitrate_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "bitrate_mode" }, \
577 { "cq", "Constant quality mode", \
578 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CQ}, 0, 0, VE, "bitrate_mode" }, \
579 { "vbr", "Variable bitrate mode", \
580 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_VBR}, 0, 0, VE, "bitrate_mode" }, \
581 { "cbr", "Constant bitrate mode", \
582 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, "bitrate_mode" }, \
583 { "cbr_fd", "Constant bitrate mode with frame drops", \
584 0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, VE, "bitrate_mode" }, \
585 { "pts_as_dts", "Use PTS as DTS. It is enabled automatically if avctx max_b_frames <= 0, " \
586 "since most of Android devices don't output B frames by default.", \
587 OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
590 #define MEDIACODEC_ENCODER_CLASS(name) \
591 static const AVClass name ## _mediacodec_class = { \
592 .class_name = #name "_mediacodec", \
593 .item_name = av_default_item_name, \
594 .option = name ## _options, \
595 .version = LIBAVUTIL_VERSION_INT, \
598 #define DECLARE_MEDIACODEC_ENCODER(short_name, long_name, codec_id) \
599 MEDIACODEC_ENCODER_CLASS(short_name) \
600 const FFCodec ff_ ## short_name ## _mediacodec_encoder = { \
601 .p.name = #short_name "_mediacodec", \
602 CODEC_LONG_NAME(long_name " Android MediaCodec encoder"), \
603 .p.type = AVMEDIA_TYPE_VIDEO, \
605 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY \
606 | AV_CODEC_CAP_HARDWARE, \
607 .priv_data_size = sizeof(MediaCodecEncContext), \
608 .p.pix_fmts = avc_pix_fmts, \
609 .init = mediacodec_init, \
610 FF_CODEC_RECEIVE_PACKET_CB(mediacodec_encode), \
611 .close = mediacodec_close, \
612 .p.priv_class = &short_name ## _mediacodec_class, \
613 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
614 .p.wrapper_name = "mediacodec", \
615 .hw_configs = mediacodec_hw_configs, \
618 #if CONFIG_H264_MEDIACODEC_ENCODER
620 enum MediaCodecAvcLevel {
637 AVCLevel52 = 0x10000,
639 AVCLevel61 = 0x40000,
640 AVCLevel62 = 0x80000,
645 {
"level",
"Specify level",
672 #endif // CONFIG_H264_MEDIACODEC_ENCODER
674 #if CONFIG_HEVC_MEDIACODEC_ENCODER
676 enum MediaCodecHevcLevel {
677 HEVCMainTierLevel1 = 0x1,
678 HEVCHighTierLevel1 = 0x2,
679 HEVCMainTierLevel2 = 0x4,
680 HEVCHighTierLevel2 = 0x8,
681 HEVCMainTierLevel21 = 0x10,
682 HEVCHighTierLevel21 = 0x20,
683 HEVCMainTierLevel3 = 0x40,
684 HEVCHighTierLevel3 = 0x80,
685 HEVCMainTierLevel31 = 0x100,
686 HEVCHighTierLevel31 = 0x200,
687 HEVCMainTierLevel4 = 0x400,
688 HEVCHighTierLevel4 = 0x800,
689 HEVCMainTierLevel41 = 0x1000,
690 HEVCHighTierLevel41 = 0x2000,
691 HEVCMainTierLevel5 = 0x4000,
692 HEVCHighTierLevel5 = 0x8000,
693 HEVCMainTierLevel51 = 0x10000,
694 HEVCHighTierLevel51 = 0x20000,
695 HEVCMainTierLevel52 = 0x40000,
696 HEVCHighTierLevel52 = 0x80000,
697 HEVCMainTierLevel6 = 0x100000,
698 HEVCHighTierLevel6 = 0x200000,
699 HEVCMainTierLevel61 = 0x400000,
700 HEVCHighTierLevel61 = 0x800000,
701 HEVCMainTierLevel62 = 0x1000000,
702 HEVCHighTierLevel62 = 0x2000000,
707 {
"level",
"Specify tier and level",
709 {
"m1",
"Main tier level 1",
711 {
"h1",
"High tier level 1",
713 {
"m2",
"Main tier level 2",
715 {
"h2",
"High tier level 2",
717 {
"m2.1",
"Main tier level 2.1",
719 {
"h2.1",
"High tier level 2.1",
721 {
"m3",
"Main tier level 3",
723 {
"h3",
"High tier level 3",
725 {
"m3.1",
"Main tier level 3.1",
727 {
"h3.1",
"High tier level 3.1",
729 {
"m4",
"Main tier level 4",
731 {
"h4",
"High tier level 4",
733 {
"m4.1",
"Main tier level 4.1",
735 {
"h4.1",
"High tier level 4.1",
737 {
"m5",
"Main tier level 5",
739 {
"h5",
"High tier level 5",
741 {
"m5.1",
"Main tier level 5.1",
743 {
"h5.1",
"High tier level 5.1",
745 {
"m5.2",
"Main tier level 5.2",
747 {
"h5.2",
"High tier level 5.2",
749 {
"m6",
"Main tier level 6",
751 {
"h6",
"High tier level 6",
753 {
"m6.1",
"Main tier level 6.1",
755 {
"h6.1",
"High tier level 6.1",
757 {
"m6.2",
"Main tier level 6.2",
759 {
"h6.2",
"High tier level 6.2",
766 #endif // CONFIG_HEVC_MEDIACODEC_ENCODER
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
void * hwaccel_context
Legacy hardware accelerator context.
#define AV_LOG_WARNING
Something somehow does not look correct.
AVPixelFormat
Pixel format.
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
enum AVColorSpace colorspace
YUV colorspace type.
#define AVERROR_EOF
End of file.
uint8_t * data
The data buffer.
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
void * surface
android/view/Surface handle, to be filled by the user.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
This structure describes decoded (raw) audio or video data.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
@ AV_HWDEVICE_TYPE_MEDIACODEC
#define FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
The bitstream filter state.
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define FF_ARRAY_ELEMS(a)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
enum AVPixelFormat pix_fmt
For decoders, a hardware pixel format which that decoder may be able to decode to if suitable hardwar...
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
@ AV_PIX_FMT_MEDIACODEC
hardware decoding through MediaCodec
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Describe the class of an AVClass context structure.
enum AVColorRange color_range
MPEG vs JPEG YUV range.
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
This structure holds a reference to a android/view/Surface object that will be used as output by the ...
int64_t bit_rate
the average bitrate
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
static const AVOption h264_options[]
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
#define AVERROR_EXTERNAL
Generic error in an external library.
int flags
A combination of AV_PKT_FLAG values.
#define AV_LOG_INFO
Standard information.
#define i(width, name, range_min, range_max)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static av_always_inline av_const double round(double x)
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
AVBufferRef * hw_device_ctx
A reference to the AVHWDeviceContext describing the device which will be used by a hardware encoder/d...
void * surface
android/view/Surface object reference.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
void * av_jni_get_java_vm(void *log_ctx)
void * native_window
Pointer to ANativeWindow.
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
@ 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...
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
@ AV_CODEC_HW_CONFIG_METHOD_AD_HOC
The codec supports this format by some ad-hoc method.
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
main external API structure.
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
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...
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...
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
This structure stores compressed data.
int width
picture width / height.
#define flags(name, subs,...)
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
AVCodecHWConfig public
This is the structure which will be returned to the user by avcodec_get_hw_config().