#include "libavutil/avassert.h"
#include "libvpx.h"
#include "profiles.h"
+#include "libavutil/avstring.h"
#include "libavutil/base64.h"
#include "libavutil/common.h"
#include "libavutil/internal.h"
struct FrameListData *coded_frame_list;
int cpu_used;
+ int sharpness;
/**
* VP8 specific flags, see VP8F_* below.
*/
int rc_undershoot_pct;
int rc_overshoot_pct;
+ char *vp8_ts_parameters;
+
// VP9-only
int lossless;
int tile_columns;
int row_mt;
int tune_content;
int corpus_complexity;
+ int tpl_model;
+ /**
+ * If the driver does not support ROI then warn the first time we
+ * encounter a frame with ROI side data.
+ */
+ int roi_warned;
} VPxContext;
/** String mappings for enum vp8e_enc_control_id */
[VP8E_SET_TUNING] = "VP8E_SET_TUNING",
[VP8E_SET_CQ_LEVEL] = "VP8E_SET_CQ_LEVEL",
[VP8E_SET_MAX_INTRA_BITRATE_PCT] = "VP8E_SET_MAX_INTRA_BITRATE_PCT",
+ [VP8E_SET_SHARPNESS] = "VP8E_SET_SHARPNESS",
#if CONFIG_LIBVPX_VP9_ENCODER
[VP9E_SET_LOSSLESS] = "VP9E_SET_LOSSLESS",
[VP9E_SET_TILE_COLUMNS] = "VP9E_SET_TILE_COLUMNS",
#ifdef VPX_CTRL_VP9E_SET_TUNE_CONTENT
[VP9E_SET_TUNE_CONTENT] = "VP9E_SET_TUNE_CONTENT",
#endif
+#ifdef VPX_CTRL_VP9E_SET_TPL
+ [VP9E_SET_TPL] = "VP9E_SET_TPL",
+#endif
#endif
};
{
int width = -30;
int level = AV_LOG_DEBUG;
+ int i;
av_log(avctx, level, "vpx_codec_enc_cfg\n");
av_log(avctx, level, "generic settings\n"
" %*s%u\n %*s%u\n",
width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
+ av_log(avctx, level, "temporal layering settings\n"
+ " %*s%u\n", width, "ts_number_layers:", cfg->ts_number_layers);
+ av_log(avctx, level,
+ "\n %*s", width, "ts_target_bitrate:");
+ for (i = 0; i < VPX_TS_MAX_LAYERS; i++)
+ av_log(avctx, level, "%u ", cfg->ts_target_bitrate[i]);
+ av_log(avctx, level, "\n");
+ av_log(avctx, level,
+ "\n %*s", width, "ts_rate_decimator:");
+ for (i = 0; i < VPX_TS_MAX_LAYERS; i++)
+ av_log(avctx, level, "%u ", cfg->ts_rate_decimator[i]);
+ av_log(avctx, level, "\n");
+ av_log(avctx, level,
+ "\n %*s%u\n", width, "ts_periodicity:", cfg->ts_periodicity);
+ av_log(avctx, level,
+ "\n %*s", width, "ts_layer_id:");
+ for (i = 0; i < VPX_TS_MAX_PERIODICITY; i++)
+ av_log(avctx, level, "%u ", cfg->ts_layer_id[i]);
+ av_log(avctx, level, "\n");
av_log(avctx, level, "decoder buffer model\n"
" %*s%u\n %*s%u\n %*s%u\n",
width, "rc_buf_sz:", cfg->rc_buf_sz,
return 0;
}
+static void vp8_ts_parse_int_array(int *dest, char *value, size_t value_len, int max_entries)
+{
+ int dest_idx = 0;
+ char *saveptr = NULL;
+ char *token = av_strtok(value, ",", &saveptr);
+
+ while (token && dest_idx < max_entries) {
+ dest[dest_idx++] = strtoul(token, NULL, 10);
+ token = av_strtok(NULL, ",", &saveptr);
+ }
+}
+
+static int vp8_ts_param_parse(struct vpx_codec_enc_cfg *enccfg, char *key, char *value)
+{
+ size_t value_len = strlen(value);
+
+ if (!value_len)
+ return -1;
+
+ if (!strcmp(key, "ts_number_layers"))
+ enccfg->ts_number_layers = strtoul(value, &value, 10);
+ else if (!strcmp(key, "ts_target_bitrate"))
+ vp8_ts_parse_int_array(enccfg->ts_target_bitrate, value, value_len, VPX_TS_MAX_LAYERS);
+ else if (!strcmp(key, "ts_rate_decimator"))
+ vp8_ts_parse_int_array(enccfg->ts_rate_decimator, value, value_len, VPX_TS_MAX_LAYERS);
+ else if (!strcmp(key, "ts_periodicity"))
+ enccfg->ts_periodicity = strtoul(value, &value, 10);
+ else if (!strcmp(key, "ts_layer_id"))
+ vp8_ts_parse_int_array(enccfg->ts_layer_id, value, value_len, VPX_TS_MAX_PERIODICITY);
+
+ return 0;
+}
+
#if CONFIG_LIBVPX_VP9_ENCODER
static int set_pix_fmt(AVCodecContext *avctx, vpx_codec_caps_t codec_caps,
struct vpx_codec_enc_cfg *enccfg, vpx_codec_flags_t *flags,
enccfg.g_h = avctx->height;
enccfg.g_timebase.num = avctx->time_base.num;
enccfg.g_timebase.den = avctx->time_base.den;
- enccfg.g_threads = avctx->thread_count ? avctx->thread_count : av_cpu_count();
+ enccfg.g_threads =
+ FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 16);
enccfg.g_lag_in_frames= ctx->lag_in_frames;
if (avctx->flags & AV_CODEC_FLAG_PASS1)
enccfg.g_error_resilient = ctx->error_resilient || ctx->flags & VP8F_ERROR_RESILIENT;
+ if (CONFIG_LIBVPX_VP8_ENCODER && avctx->codec_id == AV_CODEC_ID_VP8 && ctx->vp8_ts_parameters) {
+ AVDictionary *dict = NULL;
+ AVDictionaryEntry* en = NULL;
+
+ if (!av_dict_parse_string(&dict, ctx->vp8_ts_parameters, "=", ":", 0)) {
+ while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
+ if (vp8_ts_param_parse(&enccfg, en->key, en->value) < 0)
+ av_log(avctx, AV_LOG_WARNING,
+ "Error parsing option '%s = %s'.\n",
+ en->key, en->value);
+ }
+
+ av_dict_free(&dict);
+ }
+ }
+
dump_enc_cfg(avctx, &enccfg);
/* Construct Encoder Context */
res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, flags);
return AVERROR(EINVAL);
}
+ if (ctx->sharpness >= 0)
+ codecctl_int(avctx, VP8E_SET_SHARPNESS, ctx->sharpness);
+
if (CONFIG_LIBVPX_VP8_ENCODER && avctx->codec_id == AV_CODEC_ID_VP8) {
#if FF_API_PRIVATE_OPT
FF_DISABLE_DEPRECATION_WARNINGS
#ifdef VPX_CTRL_VP9E_SET_TUNE_CONTENT
if (ctx->tune_content >= 0)
codecctl_int(avctx, VP9E_SET_TUNE_CONTENT, ctx->tune_content);
+#endif
+#ifdef VPX_CTRL_VP9E_SET_TPL
+ if (ctx->tpl_model >= 0)
+ codecctl_int(avctx, VP9E_SET_TPL, ctx->tpl_model);
#endif
}
#endif
are only good through the next vpx_codec call */
while ((pkt = vpx_codec_get_cx_data(&ctx->encoder, &iter)) &&
(!ctx->is_alpha ||
- (ctx->is_alpha && (pkt_alpha = vpx_codec_get_cx_data(&ctx->encoder_alpha, &iter_alpha))))) {
+ (pkt_alpha = vpx_codec_get_cx_data(&ctx->encoder_alpha, &iter_alpha)))) {
switch (pkt->kind) {
case VPX_CODEC_CX_FRAME_PKT:
if (!size) {
return size;
}
+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)
+{
+ /**
+ * range of vpx_roi_map_t.delta_q[i] is [-63, 63]
+ */
+#define MAX_DELTA_Q 63
+
+ const AVRegionOfInterest *roi = NULL;
+ int nb_rois;
+ uint32_t self_size;
+ int segment_id;
+
+ /* record the mapping from delta_q to "segment id + 1" in segment_mapping[].
+ * the range of delta_q is [-MAX_DELTA_Q, MAX_DELTA_Q],
+ * and its corresponding array index is [0, 2 * MAX_DELTA_Q],
+ * and so the length of the mapping array is 2 * MAX_DELTA_Q + 1.
+ * "segment id + 1", so we can say there's no mapping if the value of array element is zero.
+ */
+ int segment_mapping[2 * MAX_DELTA_Q + 1] = { 0 };
+
+ memset(roi_map, 0, sizeof(*roi_map));
+
+ /* segment id 0 in roi_map is reserved for the areas not covered by AVRegionOfInterest.
+ * segment id 0 in roi_map is also for the areas with AVRegionOfInterest.qoffset near 0.
+ * (delta_q of segment id 0 is 0).
+ */
+ segment_mapping[MAX_DELTA_Q] = 1;
+ segment_id = 1;
+
+ roi = (const AVRegionOfInterest*)sd->data;
+ self_size = roi->self_size;
+ if (!self_size || sd->size % self_size) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid AVRegionOfInterest.self_size.\n");
+ return AVERROR(EINVAL);
+ }
+ nb_rois = sd->size / self_size;
+
+ /* This list must be iterated from zero because regions are
+ * defined in order of decreasing importance. So discard less
+ * important areas if they exceed the segment count.
+ */
+ for (int i = 0; i < nb_rois; i++) {
+ int delta_q;
+ int mapping_index;
+
+ roi = (const AVRegionOfInterest*)(sd->data + self_size * i);
+ if (!roi->qoffset.den) {
+ av_log(avctx, AV_LOG_ERROR, "AVRegionOfInterest.qoffset.den must not be zero.\n");
+ return AVERROR(EINVAL);
+ }
+
+ delta_q = (int)(roi->qoffset.num * 1.0f / roi->qoffset.den * MAX_DELTA_Q);
+ delta_q = av_clip(delta_q, -MAX_DELTA_Q, MAX_DELTA_Q);
+
+ mapping_index = delta_q + MAX_DELTA_Q;
+ if (!segment_mapping[mapping_index]) {
+ if (segment_id == segment_cnt) {
+ av_log(avctx, AV_LOG_WARNING,
+ "ROI only supports %d segments (and segment 0 is reserved for non-ROIs), skipping the left ones.\n",
+ segment_cnt);
+ break;
+ }
+
+ segment_mapping[mapping_index] = segment_id + 1;
+ roi_map->delta_q[segment_id] = delta_q;
+ segment_id++;
+ }
+ }
+
+ roi_map->rows = (frame_height + block_size - 1) / block_size;
+ roi_map->cols = (frame_width + block_size - 1) / block_size;
+ roi_map->roi_map = av_mallocz_array(roi_map->rows * roi_map->cols, sizeof(*roi_map->roi_map));
+ if (!roi_map->roi_map) {
+ av_log(avctx, AV_LOG_ERROR, "roi_map alloc failed.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ /* This list must be iterated in reverse, so for the case that
+ * two regions are overlapping, the more important area takes effect.
+ */
+ for (int i = nb_rois - 1; i >= 0; i--) {
+ int delta_q;
+ int mapping_value;
+ int starty, endy, startx, endx;
+
+ roi = (const AVRegionOfInterest*)(sd->data + self_size * i);
+
+ starty = av_clip(roi->top / block_size, 0, roi_map->rows);
+ endy = av_clip((roi->bottom + block_size - 1) / block_size, 0, roi_map->rows);
+ startx = av_clip(roi->left / block_size, 0, roi_map->cols);
+ endx = av_clip((roi->right + block_size - 1) / block_size, 0, roi_map->cols);
+
+ delta_q = (int)(roi->qoffset.num * 1.0f / roi->qoffset.den * MAX_DELTA_Q);
+ delta_q = av_clip(delta_q, -MAX_DELTA_Q, MAX_DELTA_Q);
+
+ mapping_value = segment_mapping[delta_q + MAX_DELTA_Q];
+ if (mapping_value) {
+ for (int y = starty; y < endy; y++)
+ for (int x = startx; x < endx; x++)
+ roi_map->roi_map[x + y * roi_map->cols] = mapping_value - 1;
+ }
+ }
+
+ return 0;
+}
+
+static int vp9_encode_set_roi(AVCodecContext *avctx, int frame_width, int frame_height, const AVFrameSideData *sd)
+{
+ VPxContext *ctx = avctx->priv_data;
+
+#ifdef VPX_CTRL_VP9E_SET_ROI_MAP
+ int version = vpx_codec_version();
+ int major = VPX_VERSION_MAJOR(version);
+ int minor = VPX_VERSION_MINOR(version);
+ int patch = VPX_VERSION_PATCH(version);
+
+ if (major > 1 || (major == 1 && minor > 8) || (major == 1 && minor == 8 && patch >= 1)) {
+ vpx_roi_map_t roi_map;
+ const int segment_cnt = 8;
+ const int block_size = 8;
+ int ret;
+
+ if (ctx->aq_mode > 0 || ctx->cpu_used < 5 || ctx->deadline != VPX_DL_REALTIME) {
+ if (!ctx->roi_warned) {
+ ctx->roi_warned = 1;
+ av_log(avctx, AV_LOG_WARNING, "ROI is only enabled when aq_mode is 0, cpu_used >= 5 "
+ "and deadline is REALTIME, so skipping ROI.\n");
+ return AVERROR(EINVAL);
+ }
+ }
+
+ ret = set_roi_map(avctx, sd, frame_width, frame_height, &roi_map, block_size, segment_cnt);
+ if (ret) {
+ log_encoder_error(avctx, "Failed to set_roi_map.\n");
+ return ret;
+ }
+
+ memset(roi_map.ref_frame, -1, sizeof(roi_map.ref_frame));
+
+ if (vpx_codec_control(&ctx->encoder, VP9E_SET_ROI_MAP, &roi_map)) {
+ log_encoder_error(avctx, "Failed to set VP9E_SET_ROI_MAP codec control.\n");
+ ret = AVERROR_INVALIDDATA;
+ }
+ av_freep(&roi_map.roi_map);
+ return ret;
+ }
+#endif
+
+ if (!ctx->roi_warned) {
+ ctx->roi_warned = 1;
+ av_log(avctx, AV_LOG_WARNING, "ROI is not supported, please upgrade libvpx to version >= 1.8.1. "
+ "You may need to rebuild ffmpeg.\n");
+ }
+ return 0;
+}
+
+static int vp8_encode_set_roi(AVCodecContext *avctx, int frame_width, int frame_height, const AVFrameSideData *sd)
+{
+ vpx_roi_map_t roi_map;
+ const int segment_cnt = 4;
+ const int block_size = 16;
+ VPxContext *ctx = avctx->priv_data;
+
+ int ret = set_roi_map(avctx, sd, frame_width, frame_height, &roi_map, block_size, segment_cnt);
+ if (ret) {
+ log_encoder_error(avctx, "Failed to set_roi_map.\n");
+ return ret;
+ }
+
+ if (vpx_codec_control(&ctx->encoder, VP8E_SET_ROI_MAP, &roi_map)) {
+ log_encoder_error(avctx, "Failed to set VP8E_SET_ROI_MAP codec control.\n");
+ ret = AVERROR_INVALIDDATA;
+ }
+
+ av_freep(&roi_map.roi_map);
+ return ret;
+}
+
static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *frame, int *got_packet)
{
vpx_enc_frame_flags_t flags = 0;
if (frame) {
+ const AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST);
rawimg = &ctx->rawimg;
rawimg->planes[VPX_PLANE_Y] = frame->data[0];
rawimg->planes[VPX_PLANE_U] = frame->data[1];
#endif
if (frame->pict_type == AV_PICTURE_TYPE_I)
flags |= VPX_EFLAG_FORCE_KF;
+ if (CONFIG_LIBVPX_VP8_ENCODER && avctx->codec_id == AV_CODEC_ID_VP8 && frame->metadata) {
+ AVDictionaryEntry* en = av_dict_get(frame->metadata, "vp8-flags", NULL, 0);
+ if (en) {
+ flags |= strtoul(en->value, NULL, 10);
+ }
+ }
+
+ if (sd) {
+ if (avctx->codec_id == AV_CODEC_ID_VP8) {
+ vp8_encode_set_roi(avctx, frame->width, frame->height, sd);
+ } else {
+ vp9_encode_set_roi(avctx, frame->width, frame->height, sd);
+ }
+ }
}
res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp,
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
#define COMMON_OPTIONS \
- { "auto-alt-ref", "Enable use of alternate reference " \
- "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE}, \
{ "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", 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},
#if CONFIG_LIBVPX_VP8_ENCODER
static const AVOption vp8_options[] = {
COMMON_OPTIONS
+ { "auto-alt-ref", "Enable use of alternate reference "
+ "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
{ "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE},
+ { "ts-parameters", "Temporal scaling configuration using a "
+ ":-separated list of key=value parameters", OFFSET(vp8_ts_parameters), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},
LEGACY_OPTIONS
{ NULL }
};
#if CONFIG_LIBVPX_VP9_ENCODER
static const AVOption vp9_options[] = {
COMMON_OPTIONS
+ { "auto-alt-ref", "Enable use of alternate reference "
+ "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
{ "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -8, 8, VE},
{ "lossless", "Lossless mode", OFFSET(lossless), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
{ "tile-columns", "Number of tile columns to use, log2", OFFSET(tile_columns), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
#endif
#if VPX_ENCODER_ABI_VERSION >= 14
{ "corpus-complexity", "corpus vbr complexity midpoint", OFFSET(corpus_complexity), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 10000, VE },
+#endif
+#ifdef VPX_CTRL_VP9E_SET_TPL
+ { "enable-tpl", "Enable temporal dependency model", OFFSET(tpl_model), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE },
#endif
LEGACY_OPTIONS
{ NULL }