FFmpeg
Data Structures | Macros | Enumerations | Functions | Variables
mediacodecenc.c File Reference
#include "config_components.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/hwcontext_mediacodec.h"
#include "libavutil/imgutils.h"
#include "libavutil/mem.h"
#include "libavutil/opt.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_output_format (AVCodecContext *avctx)
 
static int extract_extradata_support (AVCodecContext *avctx)
 
static int mediacodec_init_bsf (AVCodecContext *avctx)
 
static int mediacodec_generate_extradata (AVCodecContext *avctx)
 
static av_cold int mediacodec_init (AVCodecContext *avctx)
 
static int mediacodec_receive (AVCodecContext *avctx, AVPacket *pkt)
 
static void copy_frame_to_buffer (AVCodecContext *avctx, const AVFrame *frame, uint8_t *dst, size_t size)
 
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 43 of file mediacodecenc.c.

◆ OUTPUT_DEQUEUE_TIMEOUT_US

#define OUTPUT_DEQUEUE_TIMEOUT_US   8000

Definition at line 44 of file mediacodecenc.c.

◆ OFFSET

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

Definition at line 737 of file mediacodecenc.c.

◆ VE

Definition at line 738 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 }, \
{ "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 }, \

Definition at line 739 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 759 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 767 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 46 of file mediacodecenc.c.

◆ anonymous enum

anonymous enum
Enumerator
COLOR_FormatYUV420Planar 
COLOR_FormatYUV420SemiPlanar 
COLOR_FormatSurface 

Definition at line 81 of file mediacodecenc.c.

Function Documentation

◆ mediacodec_output_format()

static void mediacodec_output_format ( AVCodecContext avctx)
static

Definition at line 103 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 117 of file mediacodecenc.c.

Referenced by mediacodec_init_bsf().

◆ mediacodec_init_bsf()

static int mediacodec_init_bsf ( AVCodecContext avctx)
static

Definition at line 134 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ mediacodec_generate_extradata()

static int mediacodec_generate_extradata ( AVCodecContext avctx)
static

Definition at line 644 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ mediacodec_init()

static av_cold int mediacodec_init ( AVCodecContext avctx)
static

Definition at line 188 of file mediacodecenc.c.

◆ mediacodec_receive()

static int mediacodec_receive ( AVCodecContext avctx,
AVPacket pkt 
)
static

Definition at line 394 of file mediacodecenc.c.

Referenced by mediacodec_encode(), and mediacodec_receive_dummy_pkt().

◆ 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 469 of file mediacodecenc.c.

Referenced by mediacodec_send().

◆ mediacodec_send()

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

Definition at line 496 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 545 of file mediacodecenc.c.

◆ mediacodec_send_dummy_frame()

static int mediacodec_send_dummy_frame ( AVCodecContext avctx)
static

Definition at line 591 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 622 of file mediacodecenc.c.

Referenced by mediacodec_generate_extradata().

◆ mediacodec_close()

static av_cold int mediacodec_close ( AVCodecContext avctx)
static

Definition at line 695 of file mediacodecenc.c.

◆ mediacodec_flush()

static av_cold void mediacodec_flush ( AVCodecContext avctx)
static

Definition at line 715 of file mediacodecenc.c.

Variable Documentation

◆ color_format

int color_format

Definition at line 88 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ pix_fmt

enum AVPixelFormat pix_fmt

Definition at line 89 of file mediacodecenc.c.

◆ color_formats

const { ... } color_formats[]

◆ avc_pix_fmts

enum AVPixelFormat avc_pix_fmts[]
static
Initial value:

Definition at line 96 of file mediacodecenc.c.

◆ mediacodec_hw_configs

const AVCodecHWConfigInternal* const mediacodec_hw_configs[]
static
Initial value:

Definition at line 724 of file mediacodecenc.c.

MediaCodecEncContext
Definition: mediacodecenc.c:57
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
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
BITRATE_MODE_VBR
@ BITRATE_MODE_VBR
Definition: mediacodecenc.c:50
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:696
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:545
VE
#define VE
Definition: mediacodecenc.c:738
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:715
mediacodec_close
static av_cold int mediacodec_close(AVCodecContext *avctx)
Definition: mediacodecenc.c:695
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:48
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:394
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
COLOR_FormatYUV420SemiPlanar
@ COLOR_FormatYUV420SemiPlanar
Definition: mediacodecenc.c:83
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:759
BITRATE_MODE_CBR_FD
@ BITRATE_MODE_CBR_FD
Definition: mediacodecenc.c:54
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:52
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:96
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:679
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:188
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
COLOR_FormatYUV420Planar
@ COLOR_FormatYUV420Planar
Definition: mediacodecenc.c:82
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:724
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
COLOR_FormatSurface
@ COLOR_FormatSurface
Definition: mediacodecenc.c:84