FFmpeg
Data Structures | Macros | Functions | Variables
libvpxenc.c File Reference
#include <vpx/vpx_encoder.h>
#include <vpx/vp8cx.h>
#include "avcodec.h"
#include "internal.h"
#include "libavutil/avassert.h"
#include "libvpx.h"
#include "packet_internal.h"
#include "profiles.h"
#include "libavutil/avstring.h"
#include "libavutil/base64.h"
#include "libavutil/common.h"
#include "libavutil/internal.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"

Go to the source code of this file.

Data Structures

struct  FrameListData
 Portion of struct vpx_codec_cx_pkt from vpx_encoder.h. More...
 
struct  VPxEncoderContext
 

Macros

#define VPX_DISABLE_CTRL_TYPECHECKS   1
 
#define VPX_CODEC_DISABLE_COMPAT   1
 
#define VP8F_ERROR_RESILIENT   0x00000001
 Enable measures appropriate for streaming over lossy links. More...
 
#define VP8F_AUTO_ALT_REF   0x00000002
 Enable automatic alternate reference frame generation. More...
 
#define MAX_DELTA_Q   63
 
#define OFFSET(x)   offsetof(VPxContext, x)
 
#define VE   AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 
#define COMMON_OPTIONS
 
#define LEGACY_OPTIONS
 

Functions

static av_cold void log_encoder_error (AVCodecContext *avctx, const char *desc)
 
static av_cold void dump_enc_cfg (AVCodecContext *avctx, const struct vpx_codec_enc_cfg *cfg)
 
static void coded_frame_add (void *list, struct FrameListData *cx_frame)
 
static av_cold void free_coded_frame (struct FrameListData *cx_frame)
 
static av_cold void free_frame_list (struct FrameListData *list)
 
static av_cold int codecctl_int (AVCodecContext *avctx, enum vp8e_enc_control_id id, int val)
 
static av_cold int vpx_free (AVCodecContext *avctx)
 
static void vp8_ts_parse_int_array (int *dest, char *value, size_t value_len, int max_entries)
 
static void set_temporal_layer_pattern (int layering_mode, vpx_codec_enc_cfg_t *cfg, int *layer_flags, int *flag_periodicity)
 
static int vpx_ts_param_parse (VPxContext *ctx, struct vpx_codec_enc_cfg *enccfg, char *key, char *value, enum AVCodecID codec_id)
 
static void set_vp8_defaults (AVCodecContext *avctx, struct vpx_codec_enc_cfg *enccfg)
 Set the target bitrate to VPX library default. More...
 
static void set_vpx_defaults (AVCodecContext *avctx, struct vpx_codec_enc_cfg *enccfg)
 Called when the bitrate is not set. More...
 
static av_cold int vpx_init (AVCodecContext *avctx, const struct vpx_codec_iface *iface)
 
static void cx_pktcpy (struct FrameListData *dst, const struct vpx_codec_cx_pkt *src, const struct vpx_codec_cx_pkt *src_alpha, VPxContext *ctx)
 
static int storeframe (AVCodecContext *avctx, struct FrameListData *cx_frame, AVPacket *pkt)
 Store coded frame information in format suitable for return from encode2(). More...
 
static int queue_frames (AVCodecContext *avctx, AVPacket *pkt_out)
 Queue multiple output frames from the encoder, returning the front-most. More...
 
static int set_roi_map (AVCodecContext *avctx, const AVFrameSideData *sd, int frame_width, int frame_height, vpx_roi_map_t *roi_map, int block_size, int segment_cnt)
 
static int vp9_encode_set_roi (AVCodecContext *avctx, int frame_width, int frame_height, const AVFrameSideData *sd)
 
static int vp8_encode_set_roi (AVCodecContext *avctx, int frame_width, int frame_height, const AVFrameSideData *sd)
 
static int realloc_alpha_uv (AVCodecContext *avctx, int width, int height)
 
static int vpx_encode (AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
 

Variables

static const char *const ctlidstr []
 String mappings for enum vp8e_enc_control_id. More...
 
static const AVCodecDefault defaults []
 

Detailed Description

VP8/9 encoder support via libvpx

Definition in file libvpxenc.c.

Macro Definition Documentation

◆ VPX_DISABLE_CTRL_TYPECHECKS

#define VPX_DISABLE_CTRL_TYPECHECKS   1

Definition at line 26 of file libvpxenc.c.

◆ VPX_CODEC_DISABLE_COMPAT

#define VPX_CODEC_DISABLE_COMPAT   1

Definition at line 27 of file libvpxenc.c.

◆ VP8F_ERROR_RESILIENT

#define VP8F_ERROR_RESILIENT   0x00000001

Enable measures appropriate for streaming over lossy links.

Definition at line 86 of file libvpxenc.c.

◆ VP8F_AUTO_ALT_REF

#define VP8F_AUTO_ALT_REF   0x00000002

Enable automatic alternate reference frame generation.

Definition at line 87 of file libvpxenc.c.

◆ MAX_DELTA_Q

#define MAX_DELTA_Q   63

◆ OFFSET

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

Definition at line 1733 of file libvpxenc.c.

◆ VE

Definition at line 1734 of file libvpxenc.c.

◆ COMMON_OPTIONS

#define COMMON_OPTIONS
Value:
{ "lag-in-frames", "Number of frames to look ahead for " \
"alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
{ "arnr-maxframes", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
{ "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
{ "arnr-type", "altref noise reduction filter type", OFFSET(arnr_type), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "arnr_type"}, \
{ "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "arnr_type" }, \
{ "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "arnr_type" }, \
{ "centered", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "arnr_type" }, \
{ "tune", "Tune the encoding to a specific scenario", OFFSET(tune), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "tune"}, \
{ "psnr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VP8_TUNE_PSNR}, 0, 0, VE, "tune"}, \
{ "ssim", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VP8_TUNE_SSIM}, 0, 0, VE, "tune"}, \
{ "deadline", "Time to spend encoding, in microseconds.", OFFSET(deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \
{ "best", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_BEST_QUALITY}, 0, 0, VE, "quality"}, \
{ "good", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_GOOD_QUALITY}, 0, 0, VE, "quality"}, \
{ "realtime", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_REALTIME}, 0, 0, VE, "quality"}, \
{ "error-resilient", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"}, \
{ "max-intra-rate", "Maximum I-frame bitrate (pct) 0=unlimited", OFFSET(max_intra_rate), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \
{ "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"}, \
{ "partitions", "The frame partitions are independently decodable " \
"by the bool decoder, meaning that partitions can be decoded even " \
"though earlier partitions have been lost. Note that intra prediction" \
" is still done over the partition boundary.", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_PARTITIONS}, 0, 0, VE, "er"}, \
{ "crf", "Select the quality for constant quality mode", offsetof(VPxContext, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE }, \
{ "static-thresh", "A change threshold on blocks below which they will be skipped by the encoder", OFFSET(static_thresh), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, \
{ "drop-threshold", "Frame drop threshold", offsetof(VPxContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE }, \
{ "noise-sensitivity", "Noise sensitivity", OFFSET(noise_sensitivity), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 4, VE}, \
{ "undershoot-pct", "Datarate undershoot (min) target (%)", OFFSET(rc_undershoot_pct), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, VE }, \
{ "overshoot-pct", "Datarate overshoot (max) target (%)", OFFSET(rc_overshoot_pct), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1000, VE }, \
{ "ts-parameters", "Temporal scaling configuration using a :-separated list of key=value parameters", OFFSET(vpx_ts_parameters), AV_OPT_TYPE_DICT, {.str=NULL}, 0, 0, VE}, \

Definition at line 1736 of file libvpxenc.c.

◆ LEGACY_OPTIONS

#define LEGACY_OPTIONS
Value:
{"speed", "", offsetof(VPxContext, cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE}, \
{"quality", "", offsetof(VPxContext, deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \
{"vp8flags", "", offsetof(VPxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, UINT_MAX, VE, "flags"}, \
{"error_resilient", "enable error resilience", 0, AV_OPT_TYPE_CONST, {.i64 = VP8F_ERROR_RESILIENT}, INT_MIN, INT_MAX, VE, "flags"}, \
{"altref", "enable use of alternate reference frames (VP8/2-pass only)", 0, AV_OPT_TYPE_CONST, {.i64 = VP8F_AUTO_ALT_REF}, INT_MIN, INT_MAX, VE, "flags"}, \
{"arnr_max_frames", "altref noise reduction max frame count", offsetof(VPxContext, arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 15, VE}, \
{"arnr_strength", "altref noise reduction filter strength", offsetof(VPxContext, arnr_strength), AV_OPT_TYPE_INT, {.i64 = 3}, 0, 6, VE}, \
{"arnr_type", "altref noise reduction filter type", offsetof(VPxContext, arnr_type), AV_OPT_TYPE_INT, {.i64 = 3}, 1, 3, VE}, \
{"rc_lookahead", "Number of frames to look ahead for alternate reference frame selection", offsetof(VPxContext, lag_in_frames), AV_OPT_TYPE_INT, {.i64 = 25}, 0, 25, VE}, \
{"sharpness", "Increase sharpness at the expense of lower PSNR", offsetof(VPxContext, sharpness), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 7, VE},

Definition at line 1767 of file libvpxenc.c.

Function Documentation

◆ log_encoder_error()

static av_cold void log_encoder_error ( AVCodecContext avctx,
const char *  desc 
)
static

◆ dump_enc_cfg()

static av_cold void dump_enc_cfg ( AVCodecContext avctx,
const struct vpx_codec_enc_cfg *  cfg 
)
static

Definition at line 191 of file libvpxenc.c.

Referenced by vpx_init().

◆ coded_frame_add()

static void coded_frame_add ( void *  list,
struct FrameListData cx_frame 
)
static

Definition at line 289 of file libvpxenc.c.

Referenced by queue_frames().

◆ free_coded_frame()

static av_cold void free_coded_frame ( struct FrameListData cx_frame)
static

Definition at line 299 of file libvpxenc.c.

Referenced by free_frame_list(), and queue_frames().

◆ free_frame_list()

static av_cold void free_frame_list ( struct FrameListData list)
static

Definition at line 307 of file libvpxenc.c.

Referenced by vpx_free().

◆ codecctl_int()

static av_cold int codecctl_int ( AVCodecContext avctx,
enum vp8e_enc_control_id  id,
int  val 
)
static

Definition at line 318 of file libvpxenc.c.

Referenced by vpx_encode(), and vpx_init().

◆ vpx_free()

static av_cold int vpx_free ( AVCodecContext avctx)
static

Definition at line 362 of file libvpxenc.c.

◆ vp8_ts_parse_int_array()

static void vp8_ts_parse_int_array ( int dest,
char *  value,
size_t  value_len,
int  max_entries 
)
static

Definition at line 389 of file libvpxenc.c.

Referenced by vpx_ts_param_parse().

◆ set_temporal_layer_pattern()

static void set_temporal_layer_pattern ( int  layering_mode,
vpx_codec_enc_cfg_t *  cfg,
int layer_flags,
int flag_periodicity 
)
static

2-layers, 2-frame period.

3-layers structure with one reference frame. This works same as temporal_layering_mode 3.

3-layers, 4-frame period.

0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.

3-layers structure. added dependency between the two TL2 frames (on top of case 3). 3-layers, 4-frame period.

0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.

do not change the layer_flags or the flag_periodicity in this case; it might be that the code is using external flags to be used.

Definition at line 415 of file libvpxenc.c.

Referenced by vpx_ts_param_parse().

◆ vpx_ts_param_parse()

static int vpx_ts_param_parse ( VPxContext *  ctx,
struct vpx_codec_enc_cfg *  enccfg,
char *  key,
char *  value,
enum AVCodecID  codec_id 
)
static

Definition at line 519 of file libvpxenc.c.

Referenced by vpx_init().

◆ set_vp8_defaults()

static void set_vp8_defaults ( AVCodecContext avctx,
struct vpx_codec_enc_cfg *  enccfg 
)
static

Set the target bitrate to VPX library default.

Also set CRF to 32 if needed.

Definition at line 764 of file libvpxenc.c.

Referenced by set_vpx_defaults().

◆ set_vpx_defaults()

static void set_vpx_defaults ( AVCodecContext avctx,
struct vpx_codec_enc_cfg *  enccfg 
)
static

Called when the bitrate is not set.

It sets appropriate default values for bitrate and CRF.

Definition at line 808 of file libvpxenc.c.

Referenced by vpx_init().

◆ vpx_init()

static av_cold int vpx_init ( AVCodecContext avctx,
const struct vpx_codec_iface *  iface 
)
static

Definition at line 821 of file libvpxenc.c.

◆ cx_pktcpy()

static void cx_pktcpy ( struct FrameListData dst,
const struct vpx_codec_cx_pkt *  src,
const struct vpx_codec_cx_pkt *  src_alpha,
VPxContext *  ctx 
)
inlinestatic

Definition at line 1143 of file libvpxenc.c.

Referenced by queue_frames().

◆ storeframe()

static int storeframe ( AVCodecContext avctx,
struct FrameListData cx_frame,
AVPacket pkt 
)
static

Store coded frame information in format suitable for return from encode2().

Write information from cx_frame to pkt

Returns
packet data size on success
a negative AVERROR on error

Definition at line 1185 of file libvpxenc.c.

Referenced by queue_frames().

◆ queue_frames()

static int queue_frames ( AVCodecContext avctx,
AVPacket pkt_out 
)
static

Queue multiple output frames from the encoder, returning the front-most.

In cases where vpx_codec_get_cx_data() returns more than 1 frame append the frame queue. Return the head frame if available.

Returns
Stored frame size
AVERROR(EINVAL) on output size error
AVERROR(ENOMEM) on coded frame queue data allocation error

Definition at line 1262 of file libvpxenc.c.

Referenced by vpx_encode().

◆ set_roi_map()

static int set_roi_map ( AVCodecContext avctx,
const AVFrameSideData sd,
int  frame_width,
int  frame_height,
vpx_roi_map_t *  roi_map,
int  block_size,
int  segment_cnt 
)
static

range of vpx_roi_map_t.delta_q[i] is [-63, 63]

Definition at line 1363 of file libvpxenc.c.

Referenced by vp8_encode_set_roi(), and vp9_encode_set_roi().

◆ vp9_encode_set_roi()

static int vp9_encode_set_roi ( AVCodecContext avctx,
int  frame_width,
int  frame_height,
const AVFrameSideData sd 
)
static

Definition at line 1470 of file libvpxenc.c.

Referenced by vpx_encode().

◆ vp8_encode_set_roi()

static int vp8_encode_set_roi ( AVCodecContext avctx,
int  frame_width,
int  frame_height,
const AVFrameSideData sd 
)
static

Definition at line 1520 of file libvpxenc.c.

Referenced by vpx_encode().

◆ realloc_alpha_uv()

static int realloc_alpha_uv ( AVCodecContext avctx,
int  width,
int  height 
)
static

Definition at line 1542 of file libvpxenc.c.

Referenced by vpx_encode().

◆ vpx_encode()

static int vpx_encode ( AVCodecContext avctx,
AVPacket pkt,
const AVFrame frame,
int got_packet 
)
static

Definition at line 1570 of file libvpxenc.c.

Variable Documentation

◆ ctlidstr

const char* const ctlidstr[]
static

String mappings for enum vp8e_enc_control_id.

Definition at line 134 of file libvpxenc.c.

Referenced by codecctl_int().

◆ defaults

const AVCodecDefault defaults[]
static
Initial value:
= {
{ "b", "0" },
{ "qmin", "-1" },
{ "qmax", "-1" },
{ "g", "-1" },
{ "keyint_min", "-1" },
{ NULL },
}

Definition at line 1842 of file libvpxenc.c.

VP8F_ERROR_RESILIENT
#define VP8F_ERROR_RESILIENT
Enable measures appropriate for streaming over lossy links.
Definition: libvpxenc.c:86
VP8F_AUTO_ALT_REF
#define VP8F_AUTO_ALT_REF
Enable automatic alternate reference frame generation.
Definition: libvpxenc.c:87
NULL
#define NULL
Definition: coverity.c:32
AV_OPT_TYPE_DICT
@ AV_OPT_TYPE_DICT
Definition: opt.h:232
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
OFFSET
#define OFFSET(x)
Definition: libvpxenc.c:1733
AV_OPT_TYPE_FLAGS
@ AV_OPT_TYPE_FLAGS
Definition: opt.h:224
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
VE
#define VE
Definition: libvpxenc.c:1734
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:234