Lines Matching refs:avctx
191 static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
193 VPxContext *ctx = avctx->priv_data;
197 av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
199 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
202 static av_cold void dump_enc_cfg(AVCodecContext *avctx,
209 av_log(avctx, level, "vpx_codec_enc_cfg\n");
210 av_log(avctx, level, "generic settings\n"
229 av_log(avctx, level, "rate control settings\n"
239 av_log(avctx, level, "quantizer settings\n"
243 av_log(avctx, level, "bitrate tolerance\n"
247 av_log(avctx, level, "temporal layering settings\n"
249 if (avctx->codec_id == AV_CODEC_ID_VP8) {
250 av_log(avctx, level,
253 av_log(avctx, level,
257 if (avctx->codec_id == AV_CODEC_ID_VP9) {
258 av_log(avctx, level,
261 av_log(avctx, level,
265 av_log(avctx, level, "\n");
266 av_log(avctx, level,
269 av_log(avctx, level, "%u ", cfg->ts_rate_decimator[i]);
270 av_log(avctx, level, "\n");
271 av_log(avctx, level,
273 av_log(avctx, level,
276 av_log(avctx, level, "%u ", cfg->ts_layer_id[i]);
277 av_log(avctx, level, "\n");
278 av_log(avctx, level, "decoder buffer model\n"
283 av_log(avctx, level, "2 pass rate control settings\n"
289 av_log(avctx, level, " %*s%u\n",
292 av_log(avctx, level, "keyframing settings\n"
297 av_log(avctx, level, "\n");
356 static av_cold int codecctl_int(AVCodecContext *avctx,
359 VPxContext *ctx = avctx->priv_data;
365 av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
371 log_encoder_error(avctx, buf);
380 log_encoder_error(avctx, buf);
389 static av_cold int codecctl_intp(AVCodecContext *avctx,
392 VPxContext *ctx = avctx->priv_data;
398 av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, *val);
404 log_encoder_error(avctx, buf);
413 log_encoder_error(avctx, buf);
422 static av_cold int vpx_free(AVCodecContext *avctx)
424 VPxContext *ctx = avctx->priv_data;
427 if (avctx->codec_id == AV_CODEC_ID_VP9 && ctx->level >= 0 &&
428 !(avctx->flags & AV_CODEC_FLAG_PASS1)) {
430 if (!codecctl_intp(avctx, VP9E_GET_LEVEL, &level_out))
431 av_log(avctx, AV_LOG_INFO, "Encoded level %.1f\n", level_out * 0.1);
444 av_freep(&avctx->stats_out);
706 static int set_pix_fmt(AVCodecContext *avctx, vpx_codec_caps_t codec_caps,
710 VPxContext av_unused *ctx = avctx->priv_data;
711 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
713 switch (avctx->pix_fmt) {
775 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
779 static void set_colorspace(AVCodecContext *avctx)
782 VPxContext *ctx = avctx->priv_data;
787 switch (avctx->colorspace) {
797 av_log(avctx, AV_LOG_WARNING, "Unsupported colorspace (%d)\n",
798 avctx->colorspace);
802 codecctl_int(avctx, VP9E_SET_COLOR_SPACE, vpx_cs);
806 static void set_color_range(AVCodecContext *avctx)
809 switch (avctx->color_range) {
814 av_log(avctx, AV_LOG_WARNING, "Unsupported color range (%d)\n",
815 avctx->color_range);
819 codecctl_int(avctx, VP9E_SET_COLOR_RANGE, vpx_cr);
827 static void set_vp8_defaults(AVCodecContext *avctx,
830 VPxContext *ctx = avctx->priv_data;
831 av_assert0(!avctx->bit_rate);
832 avctx->bit_rate = enccfg->rc_target_bitrate * 1000;
834 av_log(avctx, AV_LOG_WARNING,
840 av_log(avctx, AV_LOG_WARNING,
852 static void set_vp9_defaults(AVCodecContext *avctx,
855 VPxContext *ctx = avctx->priv_data;
856 av_assert0(!avctx->bit_rate);
860 av_log(avctx, AV_LOG_WARNING,
871 static void set_vpx_defaults(AVCodecContext *avctx,
874 av_assert0(!avctx->bit_rate);
876 if (avctx->codec_id == AV_CODEC_ID_VP9) {
877 set_vp9_defaults(avctx, enccfg);
881 set_vp8_defaults(avctx, enccfg);
884 static av_cold int vpx_init(AVCodecContext *avctx,
887 VPxContext *ctx = avctx->priv_data;
890 vpx_codec_flags_t flags = (avctx->flags & AV_CODEC_FLAG_PSNR) ? VPX_CODEC_USE_PSNR : 0;
900 av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
901 av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
903 if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P)
907 av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
913 if (avctx->codec_id == AV_CODEC_ID_VP9) {
914 if (set_pix_fmt(avctx, codec_caps, &enccfg, &flags, &img_fmt))
918 if (enccfg.g_bit_depth > 8 && avctx->color_trc == AVCOL_TRC_SMPTE2084) {
927 if(!avctx->bit_rate)
928 if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) {
929 av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n");
933 dump_enc_cfg(avctx, &enccfg, AV_LOG_DEBUG);
935 enccfg.g_w = avctx->width;
936 enccfg.g_h = avctx->height;
937 enccfg.g_timebase.num = avctx->time_base.num;
938 enccfg.g_timebase.den = avctx->time_base.den;
940 FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 16);
943 if (avctx->flags & AV_CODEC_FLAG_PASS1)
945 else if (avctx->flags & AV_CODEC_FLAG_PASS2)
950 if (avctx->rc_min_rate == avctx->rc_max_rate &&
951 avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) {
956 if (!avctx->bit_rate && avctx->codec_id == AV_CODEC_ID_VP9)
961 if (avctx->bit_rate) {
962 enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
969 set_vpx_defaults(avctx, &enccfg);
972 if (avctx->codec_id == AV_CODEC_ID_VP9 && ctx->lossless == 1) {
976 if (avctx->qmin >= 0)
977 enccfg.rc_min_quantizer = avctx->qmin;
978 if (avctx->qmax >= 0)
979 enccfg.rc_max_quantizer = avctx->qmax;
988 av_log(avctx, AV_LOG_ERROR,
998 enccfg.rc_2pass_vbr_bias_pct = lrint(avctx->qcompress * 100);
999 if (avctx->bit_rate)
1001 avctx->rc_min_rate * 100LL / avctx->bit_rate;
1002 if (avctx->rc_max_rate)
1004 avctx->rc_max_rate * 100LL / avctx->bit_rate;
1006 if (avctx->codec_id == AV_CODEC_ID_VP9) {
1014 if (avctx->rc_buffer_size)
1016 avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
1017 if (avctx->rc_initial_buffer_occupancy)
1019 avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
1027 if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
1028 enccfg.kf_min_dist = avctx->keyint_min;
1029 if (avctx->gop_size >= 0)
1030 enccfg.kf_max_dist = avctx->gop_size;
1037 if (!avctx->stats_in) {
1038 av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
1042 ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
1045 av_log(avctx, AV_LOG_ERROR,
1051 decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
1054 av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
1065 if (avctx->profile != FF_PROFILE_UNKNOWN)
1066 enccfg.g_profile = avctx->profile;
1071 if (vpx_ts_param_parse(ctx, &enccfg, en->key, en->value, avctx->codec_id) < 0)
1072 av_log(avctx, AV_LOG_WARNING,
1080 dump_enc_cfg(avctx, &enccfg, AV_LOG_WARNING);
1081 log_encoder_error(avctx, "Failed to initialize encoder");
1084 dump_enc_cfg(avctx, &enccfg, AV_LOG_DEBUG);
1087 if (avctx->codec_id == AV_CODEC_ID_VP9 && enccfg.ts_number_layers > 1) {
1096 codecctl_int(avctx, VP9E_SET_SVC, 1);
1097 codecctl_intp(avctx, VP9E_SET_SVC_PARAMETERS, (int *)&svc_params);
1105 log_encoder_error(avctx, "Failed to initialize alpha encoder");
1111 av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n");
1112 codecctl_int(avctx, VP8E_SET_CPUUSED, ctx->cpu_used);
1116 codecctl_int(avctx, VP8E_SET_ENABLEAUTOALTREF,
1117 avctx->codec_id == AV_CODEC_ID_VP8 ? !!ctx->auto_alt_ref : ctx->auto_alt_ref);
1119 codecctl_int(avctx, VP8E_SET_ARNR_MAXFRAMES, ctx->arnr_max_frames);
1121 codecctl_int(avctx, VP8E_SET_ARNR_STRENGTH, ctx->arnr_strength);
1123 codecctl_int(avctx, VP8E_SET_ARNR_TYPE, ctx->arnr_type);
1125 codecctl_int(avctx, VP8E_SET_TUNING, ctx->tune);
1127 if (ctx->auto_alt_ref && ctx->is_alpha && avctx->codec_id == AV_CODEC_ID_VP8) {
1128 av_log(avctx, AV_LOG_ERROR, "Transparency encoding with auto_alt_ref does not work\n");
1133 codecctl_int(avctx, VP8E_SET_SHARPNESS, ctx->sharpness);
1135 if (CONFIG_LIBVPX_VP8_ENCODER && avctx->codec_id == AV_CODEC_ID_VP8) {
1136 codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, ctx->noise_sensitivity);
1137 codecctl_int(avctx, VP8E_SET_TOKEN_PARTITIONS, av_log2(avctx->slices));
1139 codecctl_int(avctx, VP8E_SET_STATIC_THRESHOLD, ctx->static_thresh);
1141 codecctl_int(avctx, VP8E_SET_CQ_LEVEL, ctx->crf);
1143 codecctl_int(avctx, VP8E_SET_MAX_INTRA_BITRATE_PCT, ctx->max_intra_rate);
1146 if (avctx->codec_id == AV_CODEC_ID_VP9) {
1148 codecctl_int(avctx, VP9E_SET_LOSSLESS, ctx->lossless);
1150 codecctl_int(avctx, VP9E_SET_TILE_COLUMNS, ctx->tile_columns);
1152 codecctl_int(avctx, VP9E_SET_TILE_ROWS, ctx->tile_rows);
1154 codecctl_int(avctx, VP9E_SET_FRAME_PARALLEL_DECODING, ctx->frame_parallel);
1156 codecctl_int(avctx, VP9E_SET_AQ_MODE, ctx->aq_mode);
1157 set_colorspace(avctx);
1159 set_color_range(avctx);
1162 codecctl_int(avctx, VP9E_SET_TARGET_LEVEL, ctx->level < 0 ? 255 : lrint(ctx->level * 10));
1166 codecctl_int(avctx, VP9E_SET_ROW_MT, ctx->row_mt);
1170 codecctl_int(avctx, VP9E_SET_TUNE_CONTENT, ctx->tune_content);
1174 codecctl_int(avctx, VP9E_SET_TPL, ctx->tpl_model);
1179 av_log(avctx, AV_LOG_DEBUG, "Using deadline: %d\n", ctx->deadline);
1182 vpx_img_wrap(&ctx->rawimg, img_fmt, avctx->width, avctx->height, 1,
1185 if (avctx->codec_id == AV_CODEC_ID_VP9 && (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH))
1189 cpb_props = ff_add_cpb_side_data(avctx);
1195 cpb_props->max_bitrate = avctx->rc_max_rate;
1196 cpb_props->min_bitrate = avctx->rc_min_rate;
1197 cpb_props->avg_bitrate = avctx->bit_rate;
1199 cpb_props->buffer_size = avctx->rc_buffer_size;
1238 static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
1241 VPxContext *ctx = avctx->priv_data;
1242 int ret = ff_get_encode_buffer(avctx, pkt, cx_frame->sz, 0);
1269 avctx->error[i] += cx_frame->sse[i + 1];
1302 static int queue_frames(AVCodecContext *avctx, struct vpx_codec_ctx *encoder,
1305 VPxContext *ctx = avctx->priv_data;
1313 size = storeframe(avctx, cx_frame, NULL, pkt_out);
1332 size = storeframe(avctx, &cx_frame, NULL, pkt_out);
1339 av_log(avctx, AV_LOG_ERROR,
1347 av_log(avctx, AV_LOG_ERROR,
1366 av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
1393 static int set_roi_map(AVCodecContext *avctx, const AVFrameSideData *sd, int frame_width, int frame_height,
1426 av_log(avctx, AV_LOG_ERROR, "Invalid AVRegionOfInterest.self_size.\n");
1441 av_log(avctx, AV_LOG_ERROR, "AVRegionOfInterest.qoffset.den must not be zero.\n");
1451 av_log(avctx, AV_LOG_WARNING,
1467 av_log(avctx, AV_LOG_ERROR, "roi_map alloc failed.\n");
1500 static int vp9_encode_set_roi(AVCodecContext *avctx, int frame_width, int frame_height, const AVFrameSideData *sd)
1502 VPxContext *ctx = avctx->priv_data;
1519 av_log(avctx, AV_LOG_WARNING, "ROI is only enabled when aq_mode is 0, cpu_used >= 5 "
1525 ret = set_roi_map(avctx, sd, frame_width, frame_height, &roi_map, block_size, segment_cnt);
1527 log_encoder_error(avctx, "Failed to set_roi_map.\n");
1534 log_encoder_error(avctx, "Failed to set VP9E_SET_ROI_MAP codec control.\n");
1544 av_log(avctx, AV_LOG_WARNING, "ROI is not supported, please upgrade libvpx to version >= 1.8.1. "
1550 static int vp8_encode_set_roi(AVCodecContext *avctx, int frame_width, int frame_height, const AVFrameSideData *sd)
1555 VPxContext *ctx = avctx->priv_data;
1557 int ret = set_roi_map(avctx, sd, frame_width, frame_height, &roi_map, block_size, segment_cnt);
1559 log_encoder_error(avctx, "Failed to set_roi_map.\n");
1564 log_encoder_error(avctx, "Failed to set VP8E_SET_ROI_MAP codec control.\n");
1572 static int realloc_alpha_uv(AVCodecContext *avctx, int width, int height)
1574 VPxContext *ctx = avctx->priv_data;
1600 static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt,
1603 VPxContext *ctx = avctx->priv_data;
1613 if (avctx->qmax >= 0 && enccfg->rc_max_quantizer != avctx->qmax) {
1615 cfg.rc_max_quantizer = avctx->qmax;
1618 log_encoder_error(avctx, "Error reconfiguring encoder");
1634 res = realloc_alpha_uv(avctx, frame->width, frame->height);
1673 if (avctx->codec_id == AV_CODEC_ID_VP9) {
1677 av_log(avctx, AV_LOG_WARNING,
1682 codecctl_intp(avctx, VP9E_SET_SVC_REF_FRAME_CONFIG, (int *)&ctx->ref_frame_config);
1684 av_log(avctx, AV_LOG_WARNING,
1692 if (avctx->codec_id == AV_CODEC_ID_VP8) {
1693 vp8_encode_set_roi(avctx, frame->width, frame->height, sd);
1695 vp9_encode_set_roi(avctx, frame->width, frame->height, sd);
1745 if (avctx->codec_id == AV_CODEC_ID_VP8) {
1746 codecctl_int(avctx, VP8E_SET_TEMPORAL_LAYER_ID, layer_id.temporal_layer_id);
1749 else if (avctx->codec_id == AV_CODEC_ID_VP9) {
1750 codecctl_intp(avctx, VP9E_SET_SVC_LAYER_ID, (int *)&layer_id);
1756 avctx->ticks_per_frame, flags, ctx->deadline);
1758 log_encoder_error(avctx, "Error encoding frame");
1764 avctx->ticks_per_frame, flags, ctx->deadline);
1766 log_encoder_error(avctx, "Error encoding alpha frame");
1771 coded_size = queue_frames(avctx, &ctx->encoder, &ctx->coded_frame_list, pkt);
1773 queue_frames(avctx, &ctx->encoder_alpha, &ctx->alpha_coded_frame_list, NULL);
1780 coded_size = storeframe(avctx, cx_frame, alpha_cx_frame, pkt);
1790 if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) {
1793 avctx->stats_out = av_malloc(b64_size);
1794 if (!avctx->stats_out) {
1795 av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%d bytes) failed\n",
1799 av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
1928 static av_cold int vp8_init(AVCodecContext *avctx)
1930 return vpx_init(avctx, vpx_codec_vp8_cx());
1960 static av_cold int vp9_init(AVCodecContext *avctx)
1962 return vpx_init(avctx, vpx_codec_vp9_cx());