FFmpeg
Data Structures | Macros | Enumerations | Functions | Variables
mediacodecenc.c File Reference
#include "config_components.h"
#include "libavutil/avassert.h"
#include "libavutil/fifo.h"
#include "libavutil/avstring.h"
#include "libavutil/hwcontext_mediacodec.h"
#include "libavutil/imgutils.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavutil/thread.h"
#include "avcodec.h"
#include "bsf.h"
#include "codec_internal.h"
#include "encode.h"
#include "hwconfig.h"
#include "jni.h"
#include "mediacodec.h"
#include "mediacodec_wrapper.h"
#include "mediacodecdec_common.h"
#include "profiles.h"

Go to the source code of this file.

Data Structures

struct  MediaCodecEncContext
 

Macros

#define INPUT_DEQUEUE_TIMEOUT_US   8000
 
#define OUTPUT_DEQUEUE_TIMEOUT_US   8000
 
#define OFFSET(x)   offsetof(MediaCodecEncContext, x)
 
#define VE   AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 
#define COMMON_OPTION
 
#define MEDIACODEC_ENCODER_CLASS(name)
 
#define DECLARE_MEDIACODEC_ENCODER(short_name, long_name, codec_id)
 

Enumerations

enum  BitrateMode { BITRATE_MODE_CQ = 0, BITRATE_MODE_VBR = 1, BITRATE_MODE_CBR = 2, BITRATE_MODE_CBR_FD = 3 }
 
enum  { COLOR_FormatYUV420Planar = 0x13, COLOR_FormatYUV420SemiPlanar = 0x15, COLOR_FormatSurface = 0x7F000789 }
 

Functions

static void mediacodec_dump_format (AVCodecContext *avctx, FFAMediaFormat *out_format)
 
static void mediacodec_output_format (AVCodecContext *avctx)
 
static int extract_extradata_support (AVCodecContext *avctx)
 
static int mediacodec_init_bsf (AVCodecContext *avctx)
 
static void copy_frame_to_buffer (AVCodecContext *avctx, const AVFrame *frame, uint8_t *dst, size_t size)
 
static void on_input_available (FFAMediaCodec *codec, void *userdata, int32_t index)
 
static void on_output_available (FFAMediaCodec *codec, void *userdata, int32_t index, FFAMediaCodecBufferInfo *out_info)
 
static void on_format_changed (FFAMediaCodec *codec, void *userdata, FFAMediaFormat *format)
 
static void on_error (FFAMediaCodec *codec, void *userdata, int error, const char *detail)
 
static int mediacodec_init_async_state (AVCodecContext *avctx)
 
static void mediacodec_uninit_async_state (AVCodecContext *avctx)
 
static int mediacodec_generate_extradata (AVCodecContext *avctx)
 
static av_cold int mediacodec_init (AVCodecContext *avctx)
 
static int mediacodec_get_output_index (AVCodecContext *avctx, ssize_t *index, FFAMediaCodecBufferInfo *out_info)
 
static int mediacodec_receive (AVCodecContext *avctx, AVPacket *pkt)
 
static int mediacodec_get_input_index (AVCodecContext *avctx, ssize_t *index)
 
static int mediacodec_send (AVCodecContext *avctx, const AVFrame *frame)
 
static int mediacodec_encode (AVCodecContext *avctx, AVPacket *pkt)
 
static int mediacodec_send_dummy_frame (AVCodecContext *avctx)
 
static int mediacodec_receive_dummy_pkt (AVCodecContext *avctx, AVPacket *pkt)
 
static av_cold int mediacodec_close (AVCodecContext *avctx)
 
static av_cold void mediacodec_flush (AVCodecContext *avctx)
 

Variables

struct {
   int   color_format
 
   enum AVPixelFormat   pix_fmt
 
color_formats []
 
static enum AVPixelFormat avc_pix_fmts []
 
static const AVCodecHWConfigInternal *const mediacodec_hw_configs []
 

Macro Definition Documentation

◆ INPUT_DEQUEUE_TIMEOUT_US

#define INPUT_DEQUEUE_TIMEOUT_US   8000

Definition at line 45 of file mediacodecenc.c.

◆ OUTPUT_DEQUEUE_TIMEOUT_US

#define OUTPUT_DEQUEUE_TIMEOUT_US   8000

Definition at line 46 of file mediacodecenc.c.

◆ OFFSET

#define OFFSET (   x)    offsetof(MediaCodecEncContext, x)

Definition at line 981 of file mediacodecenc.c.

◆ VE

Definition at line 982 of file mediacodecenc.c.

◆ COMMON_OPTION

#define COMMON_OPTION
Value:
{ "ndk_codec", "Use MediaCodec from NDK", \
OFFSET(use_ndk_codec), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
{ "ndk_async", "Try NDK MediaCodec in async mode", \
OFFSET(async_mode), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, VE }, \
{ "codec_name", "Select codec by name", \
OFFSET(name), AV_OPT_TYPE_STRING, {0}, 0, 0, VE }, \
{ "bitrate_mode", "Bitrate control method", \
OFFSET(bitrate_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, .unit = "bitrate_mode" }, \
{ "cq", "Constant quality mode", \
0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CQ}, 0, 0, VE, .unit = "bitrate_mode" }, \
{ "vbr", "Variable bitrate mode", \
0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_VBR}, 0, 0, VE, .unit = "bitrate_mode" }, \
{ "cbr", "Constant bitrate mode", \
0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR}, 0, 0, VE, .unit = "bitrate_mode" }, \
{ "cbr_fd", "Constant bitrate mode with frame drops", \
0, AV_OPT_TYPE_CONST, {.i64 = BITRATE_MODE_CBR_FD}, 0, 0, VE, .unit = "bitrate_mode" }, \
{ "pts_as_dts", "Use PTS as DTS. It is enabled automatically if avctx max_b_frames <= 0, " \
"since most of Android devices don't output B frames by default.", \
OFFSET(pts_as_dts), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, \
{ "operating_rate", "The desired operating rate that the codec will need to operate at, zero for unspecified", \
OFFSET(operating_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE }, \

Definition at line 983 of file mediacodecenc.c.

◆ MEDIACODEC_ENCODER_CLASS

#define MEDIACODEC_ENCODER_CLASS (   name)
Value:
static const AVClass name ## _mediacodec_class = { \
.class_name = #name "_mediacodec", \
.item_name = av_default_item_name, \
.option = name ## _options, \
.version = LIBAVUTIL_VERSION_INT, \
}; \

Definition at line 1006 of file mediacodecenc.c.

◆ DECLARE_MEDIACODEC_ENCODER

#define DECLARE_MEDIACODEC_ENCODER (   short_name,
  long_name,
  codec_id 
)
Value:
const FFCodec ff_ ## short_name ## _mediacodec_encoder = { \
.p.name = #short_name "_mediacodec", \
CODEC_LONG_NAME(long_name " Android MediaCodec encoder"), \
.p.type = AVMEDIA_TYPE_VIDEO, \
.p.id = codec_id, \
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | \
AV_CODEC_CAP_HARDWARE | \
AV_CODEC_CAP_ENCODER_FLUSH, \
.priv_data_size = sizeof(MediaCodecEncContext), \
.p.pix_fmts = avc_pix_fmts, \
.color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG, \
.init = mediacodec_init, \
.close = mediacodec_close, \
.flush = mediacodec_flush, \
.p.priv_class = &short_name ## _mediacodec_class, \
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP, \
.p.wrapper_name = "mediacodec", \
.hw_configs = mediacodec_hw_configs, \
}; \

Definition at line 1014 of file mediacodecenc.c.

Enumeration Type Documentation

◆ BitrateMode

Enumerator
BITRATE_MODE_CQ 
BITRATE_MODE_VBR 
BITRATE_MODE_CBR 
BITRATE_MODE_CBR_FD 

Definition at line 48 of file mediacodecenc.c.

◆ anonymous enum

anonymous enum
Enumerator
COLOR_FormatYUV420Planar 
COLOR_FormatYUV420SemiPlanar 
COLOR_FormatSurface 

Definition at line 96 of file mediacodecenc.c.

Function Documentation

◆ mediacodec_dump_format()

static void mediacodec_dump_format ( AVCodecContext avctx,
FFAMediaFormat out_format 
)
static

Definition at line 118 of file mediacodecenc.c.

Referenced by mediacodec_output_format(), and on_format_changed().

◆ mediacodec_output_format()

static void mediacodec_output_format ( AVCodecContext avctx)
static

Definition at line 130 of file mediacodecenc.c.

Referenced by mediacodec_init(), and mediacodec_receive().

◆ extract_extradata_support()

static int extract_extradata_support ( AVCodecContext avctx)
static

Definition at line 141 of file mediacodecenc.c.

Referenced by mediacodec_init_bsf().

◆ mediacodec_init_bsf()

static int mediacodec_init_bsf ( AVCodecContext avctx)
static

Definition at line 158 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ copy_frame_to_buffer()

static void copy_frame_to_buffer ( AVCodecContext avctx,
const AVFrame frame,
uint8_t *  dst,
size_t  size 
)
static

Definition at line 210 of file mediacodecenc.c.

Referenced by mediacodec_send().

◆ on_input_available()

static void on_input_available ( FFAMediaCodec codec,
void *  userdata,
int32_t  index 
)
static

Definition at line 238 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ on_output_available()

static void on_output_available ( FFAMediaCodec codec,
void *  userdata,
int32_t  index,
FFAMediaCodecBufferInfo out_info 
)
static

Definition at line 252 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ on_format_changed()

static void on_format_changed ( FFAMediaCodec codec,
void *  userdata,
FFAMediaFormat format 
)
static

Definition at line 268 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ on_error()

static void on_error ( FFAMediaCodec codec,
void *  userdata,
int  error,
const char *  detail 
)
static

Definition at line 274 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ mediacodec_init_async_state()

static int mediacodec_init_async_state ( AVCodecContext avctx)
static

Definition at line 295 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ mediacodec_uninit_async_state()

static void mediacodec_uninit_async_state ( AVCodecContext avctx)
static

Definition at line 319 of file mediacodecenc.c.

Referenced by mediacodec_close(), and mediacodec_init().

◆ mediacodec_generate_extradata()

static int mediacodec_generate_extradata ( AVCodecContext avctx)
static

Definition at line 885 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ mediacodec_init()

static av_cold int mediacodec_init ( AVCodecContext avctx)
static

Definition at line 341 of file mediacodecenc.c.

◆ mediacodec_get_output_index()

static int mediacodec_get_output_index ( AVCodecContext avctx,
ssize_t *  index,
FFAMediaCodecBufferInfo out_info 
)
static

Definition at line 572 of file mediacodecenc.c.

Referenced by mediacodec_receive().

◆ mediacodec_receive()

static int mediacodec_receive ( AVCodecContext avctx,
AVPacket pkt 
)
static

Definition at line 614 of file mediacodecenc.c.

Referenced by mediacodec_encode(), and mediacodec_receive_dummy_pkt().

◆ mediacodec_get_input_index()

static int mediacodec_get_input_index ( AVCodecContext avctx,
ssize_t *  index 
)
static

Definition at line 702 of file mediacodecenc.c.

Referenced by mediacodec_send().

◆ mediacodec_send()

static int mediacodec_send ( AVCodecContext avctx,
const AVFrame frame 
)
static

Definition at line 735 of file mediacodecenc.c.

Referenced by mediacodec_encode(), and mediacodec_send_dummy_frame().

◆ mediacodec_encode()

static int mediacodec_encode ( AVCodecContext avctx,
AVPacket pkt 
)
static

Definition at line 786 of file mediacodecenc.c.

◆ mediacodec_send_dummy_frame()

static int mediacodec_send_dummy_frame ( AVCodecContext avctx)
static

Definition at line 832 of file mediacodecenc.c.

Referenced by mediacodec_generate_extradata().

◆ mediacodec_receive_dummy_pkt()

static int mediacodec_receive_dummy_pkt ( AVCodecContext avctx,
AVPacket pkt 
)
static

Definition at line 863 of file mediacodecenc.c.

Referenced by mediacodec_generate_extradata().

◆ mediacodec_close()

static av_cold int mediacodec_close ( AVCodecContext avctx)
static

Definition at line 937 of file mediacodecenc.c.

◆ mediacodec_flush()

static av_cold void mediacodec_flush ( AVCodecContext avctx)
static

Definition at line 959 of file mediacodecenc.c.

Variable Documentation

◆ color_format

int color_format

Definition at line 103 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ pix_fmt

enum AVPixelFormat pix_fmt

Definition at line 104 of file mediacodecenc.c.

◆ color_formats

const { ... } color_formats[]

◆ avc_pix_fmts

enum AVPixelFormat avc_pix_fmts[]
static
Initial value:

Definition at line 111 of file mediacodecenc.c.

◆ mediacodec_hw_configs

const AVCodecHWConfigInternal* const mediacodec_hw_configs[]
static
Initial value:

Definition at line 968 of file mediacodecenc.c.

MediaCodecEncContext
Definition: mediacodecenc.c:59
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
COLOR_FormatYUV420SemiPlanar
@ COLOR_FormatYUV420SemiPlanar
Definition: mediacodecenc.c:98
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
COLOR_FormatSurface
@ COLOR_FormatSurface
Definition: mediacodecenc.c:99
BITRATE_MODE_VBR
@ BITRATE_MODE_VBR
Definition: mediacodecenc.c:52
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:717
AV_HWDEVICE_TYPE_MEDIACODEC
@ AV_HWDEVICE_TYPE_MEDIACODEC
Definition: hwcontext.h:38
FFCodec
Definition: codec_internal.h:127
mediacodec_encode
static int mediacodec_encode(AVCodecContext *avctx, AVPacket *pkt)
Definition: mediacodecenc.c:786
VE
#define VE
Definition: mediacodecenc.c:982
COLOR_FormatYUV420Planar
@ COLOR_FormatYUV420Planar
Definition: mediacodecenc.c:97
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
mediacodec_flush
static av_cold void mediacodec_flush(AVCodecContext *avctx)
Definition: mediacodecenc.c:959
mediacodec_close
static av_cold int mediacodec_close(AVCodecContext *avctx)
Definition: mediacodecenc.c:937
AV_CODEC_HW_CONFIG_METHOD_AD_HOC
@ AV_CODEC_HW_CONFIG_METHOD_AD_HOC
The codec supports this format by some ad-hoc method.
Definition: codec.h:342
BITRATE_MODE_CQ
@ BITRATE_MODE_CQ
Definition: mediacodecenc.c:50
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:410
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
AV_PIX_FMT_MEDIACODEC
@ AV_PIX_FMT_MEDIACODEC
hardware decoding through MediaCodec
Definition: pixfmt.h:316
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
FF_CODEC_RECEIVE_PACKET_CB
#define FF_CODEC_RECEIVE_PACKET_CB(func)
Definition: codec_internal.h:326
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
MEDIACODEC_ENCODER_CLASS
#define MEDIACODEC_ENCODER_CLASS(name)
Definition: mediacodecenc.c:1006
BITRATE_MODE_CBR_FD
@ BITRATE_MODE_CBR_FD
Definition: mediacodecenc.c:56
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
@ AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
The codec supports this format via the hw_device_ctx interface.
Definition: codec.h:313
BITRATE_MODE_CBR
@ BITRATE_MODE_CBR
Definition: mediacodecenc.c:54
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
avc_pix_fmts
static enum AVPixelFormat avc_pix_fmts[]
Definition: mediacodecenc.c:111
AVCodecHWConfigInternal
Definition: hwconfig.h:25
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:700
AV_PIX_FMT_NV12
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:96
mediacodec_init
static av_cold int mediacodec_init(AVCodecContext *avctx)
Definition: mediacodecenc.c:341
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition: opt.h:327
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition: opt.h:276
mediacodec_hw_configs
static const AVCodecHWConfigInternal *const mediacodec_hw_configs[]
Definition: mediacodecenc.c:968
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299