FFmpeg
Data Structures | Macros | Enumerations | Functions | Variables
mediacodecenc.c File Reference
#include "config_components.h"
#include "libavutil/avassert.h"
#include "libavutil/hwcontext_mediacodec.h"
#include "libavutil/imgutils.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 mediacodec_init_bsf (AVCodecContext *avctx)
 
static av_cold int mediacodec_init (AVCodecContext *avctx)
 
static int mediacodec_receive (AVCodecContext *avctx, AVPacket *pkt, int *got_packet)
 
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 av_cold int mediacodec_close (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 41 of file mediacodecenc.c.

◆ OUTPUT_DEQUEUE_TIMEOUT_US

#define OUTPUT_DEQUEUE_TIMEOUT_US   8000

Definition at line 42 of file mediacodecenc.c.

◆ OFFSET

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

Definition at line 585 of file mediacodecenc.c.

◆ VE

Definition at line 586 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 587 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 607 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 \
.priv_data_size = sizeof(MediaCodecEncContext), \
.p.pix_fmts = avc_pix_fmts, \
.init = mediacodec_init, \
.close = mediacodec_close, \
.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 615 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 44 of file mediacodecenc.c.

◆ anonymous enum

anonymous enum
Enumerator
COLOR_FormatYUV420Planar 
COLOR_FormatYUV420SemiPlanar 
COLOR_FormatSurface 

Definition at line 78 of file mediacodecenc.c.

Function Documentation

◆ mediacodec_output_format()

static void mediacodec_output_format ( AVCodecContext avctx)
static

Definition at line 100 of file mediacodecenc.c.

Referenced by mediacodec_init(), and mediacodec_receive().

◆ mediacodec_init_bsf()

static int mediacodec_init_bsf ( AVCodecContext avctx)
static

Definition at line 114 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ mediacodec_init()

static av_cold int mediacodec_init ( AVCodecContext avctx)
static

Definition at line 150 of file mediacodecenc.c.

◆ mediacodec_receive()

static int mediacodec_receive ( AVCodecContext avctx,
AVPacket pkt,
int got_packet 
)
static

Definition at line 351 of file mediacodecenc.c.

Referenced by mediacodec_encode().

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

Referenced by mediacodec_send().

◆ mediacodec_send()

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

Definition at line 456 of file mediacodecenc.c.

Referenced by mediacodec_encode().

◆ mediacodec_encode()

static int mediacodec_encode ( AVCodecContext avctx,
AVPacket pkt 
)
static

Definition at line 505 of file mediacodecenc.c.

◆ mediacodec_close()

static av_cold int mediacodec_close ( AVCodecContext avctx)
static

Definition at line 552 of file mediacodecenc.c.

Variable Documentation

◆ color_format

int color_format

Definition at line 85 of file mediacodecenc.c.

Referenced by mediacodec_init().

◆ pix_fmt

enum AVPixelFormat pix_fmt

Definition at line 86 of file mediacodecenc.c.

◆ color_formats

const { ... } color_formats[]

◆ avc_pix_fmts

enum AVPixelFormat avc_pix_fmts[]
static
Initial value:

Definition at line 93 of file mediacodecenc.c.

◆ mediacodec_hw_configs

const AVCodecHWConfigInternal* const mediacodec_hw_configs[]
static
Initial value:

Definition at line 572 of file mediacodecenc.c.

MediaCodecEncContext
Definition: mediacodecenc.c:55
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:42
AV_CODEC_CAP_HARDWARE
#define AV_CODEC_CAP_HARDWARE
Codec is backed by a hardware implementation.
Definition: codec.h:145
BITRATE_MODE_VBR
@ BITRATE_MODE_VBR
Definition: mediacodecenc.c:48
AV_HWDEVICE_TYPE_MEDIACODEC
@ AV_HWDEVICE_TYPE_MEDIACODEC
Definition: hwcontext.h:38
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:338
FFCodec
Definition: codec_internal.h:127
mediacodec_encode
static int mediacodec_encode(AVCodecContext *avctx, AVPacket *pkt)
Definition: mediacodecenc.c:505
VE
#define VE
Definition: mediacodecenc.c:586
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
mediacodec_close
static av_cold int mediacodec_close(AVCodecContext *avctx)
Definition: mediacodecenc.c:552
COLOR_FormatYUV420Planar
@ COLOR_FormatYUV420Planar
Definition: mediacodecenc.c:79
BITRATE_MODE_CQ
@ BITRATE_MODE_CQ
Definition: mediacodecenc.c:46
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:389
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:320
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
FF_CODEC_RECEIVE_PACKET_CB
#define FF_CODEC_RECEIVE_PACKET_CB(func)
Definition: codec_internal.h:321
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:607
BITRATE_MODE_CBR_FD
@ BITRATE_MODE_CBR_FD
Definition: mediacodecenc.c:52
BITRATE_MODE_CBR
@ BITRATE_MODE_CBR
Definition: mediacodecenc.c:50
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:309
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:93
AVCodecHWConfigInternal
Definition: hwconfig.h:25
COLOR_FormatSurface
@ COLOR_FormatSurface
Definition: mediacodecenc.c:81
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
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:150
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
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
Definition: opt.h:244
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
mediacodec_hw_configs
static const AVCodecHWConfigInternal *const mediacodec_hw_configs[]
Definition: mediacodecenc.c:572
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234
COLOR_FormatYUV420SemiPlanar
@ COLOR_FormatYUV420SemiPlanar
Definition: mediacodecenc.c:80