00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026 #define VPX_DISABLE_CTRL_TYPECHECKS 1
00027 #define VPX_CODEC_DISABLE_COMPAT 1
00028 #include <vpx/vpx_encoder.h>
00029 #include <vpx/vp8cx.h>
00030
00031 #include "avcodec.h"
00032 #include "libavutil/base64.h"
00033
00038 struct FrameListData {
00039 void *buf;
00040 size_t sz;
00041 int64_t pts;
00043 unsigned long duration;
00045 uint32_t flags;
00046 struct FrameListData *next;
00047 };
00048
00049 typedef struct VP8EncoderContext {
00050 struct vpx_codec_ctx encoder;
00051 struct vpx_image rawimg;
00052 struct vpx_fixed_buf twopass_stats;
00053 unsigned long deadline;
00054 struct FrameListData *coded_frame_list;
00055 } VP8Context;
00056
00058 static const char *ctlidstr[] = {
00059 [VP8E_UPD_ENTROPY] = "VP8E_UPD_ENTROPY",
00060 [VP8E_UPD_REFERENCE] = "VP8E_UPD_REFERENCE",
00061 [VP8E_USE_REFERENCE] = "VP8E_USE_REFERENCE",
00062 [VP8E_SET_ROI_MAP] = "VP8E_SET_ROI_MAP",
00063 [VP8E_SET_ACTIVEMAP] = "VP8E_SET_ACTIVEMAP",
00064 [VP8E_SET_SCALEMODE] = "VP8E_SET_SCALEMODE",
00065 [VP8E_SET_CPUUSED] = "VP8E_SET_CPUUSED",
00066 [VP8E_SET_ENABLEAUTOALTREF] = "VP8E_SET_ENABLEAUTOALTREF",
00067 [VP8E_SET_NOISE_SENSITIVITY] = "VP8E_SET_NOISE_SENSITIVITY",
00068 [VP8E_SET_SHARPNESS] = "VP8E_SET_SHARPNESS",
00069 [VP8E_SET_STATIC_THRESHOLD] = "VP8E_SET_STATIC_THRESHOLD",
00070 [VP8E_SET_TOKEN_PARTITIONS] = "VP8E_SET_TOKEN_PARTITIONS",
00071 [VP8E_GET_LAST_QUANTIZER] = "VP8E_GET_LAST_QUANTIZER",
00072 [VP8E_SET_ARNR_MAXFRAMES] = "VP8E_SET_ARNR_MAXFRAMES",
00073 [VP8E_SET_ARNR_STRENGTH] = "VP8E_SET_ARNR_STRENGTH",
00074 [VP8E_SET_ARNR_TYPE] = "VP8E_SET_ARNR_TYPE",
00075 };
00076
00077 static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
00078 {
00079 VP8Context *ctx = avctx->priv_data;
00080 const char *error = vpx_codec_error(&ctx->encoder);
00081 const char *detail = vpx_codec_error_detail(&ctx->encoder);
00082
00083 av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
00084 if (detail)
00085 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
00086 }
00087
00088 static av_cold void dump_enc_cfg(AVCodecContext *avctx,
00089 const struct vpx_codec_enc_cfg *cfg)
00090 {
00091 int width = -30;
00092 int level = AV_LOG_DEBUG;
00093
00094 av_log(avctx, level, "vpx_codec_enc_cfg\n");
00095 av_log(avctx, level, "generic settings\n"
00096 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
00097 " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
00098 width, "g_usage:", cfg->g_usage,
00099 width, "g_threads:", cfg->g_threads,
00100 width, "g_profile:", cfg->g_profile,
00101 width, "g_w:", cfg->g_w,
00102 width, "g_h:", cfg->g_h,
00103 width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
00104 width, "g_error_resilient:", cfg->g_error_resilient,
00105 width, "g_pass:", cfg->g_pass,
00106 width, "g_lag_in_frames:", cfg->g_lag_in_frames);
00107 av_log(avctx, level, "rate control settings\n"
00108 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
00109 " %*s%d\n %*s%p(%zu)\n %*s%u\n",
00110 width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
00111 width, "rc_resize_allowed:", cfg->rc_resize_allowed,
00112 width, "rc_resize_up_thresh:", cfg->rc_resize_up_thresh,
00113 width, "rc_resize_down_thresh:", cfg->rc_resize_down_thresh,
00114 width, "rc_end_usage:", cfg->rc_end_usage,
00115 width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
00116 width, "rc_target_bitrate:", cfg->rc_target_bitrate);
00117 av_log(avctx, level, "quantizer settings\n"
00118 " %*s%u\n %*s%u\n",
00119 width, "rc_min_quantizer:", cfg->rc_min_quantizer,
00120 width, "rc_max_quantizer:", cfg->rc_max_quantizer);
00121 av_log(avctx, level, "bitrate tolerance\n"
00122 " %*s%u\n %*s%u\n",
00123 width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
00124 width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
00125 av_log(avctx, level, "decoder buffer model\n"
00126 " %*s%u\n %*s%u\n %*s%u\n",
00127 width, "rc_buf_sz:", cfg->rc_buf_sz,
00128 width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
00129 width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
00130 av_log(avctx, level, "2 pass rate control settings\n"
00131 " %*s%u\n %*s%u\n %*s%u\n",
00132 width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
00133 width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
00134 width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
00135 av_log(avctx, level, "keyframing settings\n"
00136 " %*s%d\n %*s%u\n %*s%u\n",
00137 width, "kf_mode:", cfg->kf_mode,
00138 width, "kf_min_dist:", cfg->kf_min_dist,
00139 width, "kf_max_dist:", cfg->kf_max_dist);
00140 av_log(avctx, level, "\n");
00141 }
00142
00143 static void coded_frame_add(void *list, struct FrameListData *cx_frame)
00144 {
00145 struct FrameListData **p = list;
00146
00147 while (*p != NULL)
00148 p = &(*p)->next;
00149 *p = cx_frame;
00150 cx_frame->next = NULL;
00151 }
00152
00153 static av_cold void free_coded_frame(struct FrameListData *cx_frame)
00154 {
00155 av_freep(&cx_frame->buf);
00156 av_freep(&cx_frame);
00157 }
00158
00159 static av_cold void free_frame_list(struct FrameListData *list)
00160 {
00161 struct FrameListData *p = list;
00162
00163 while (p) {
00164 list = list->next;
00165 free_coded_frame(p);
00166 p = list;
00167 }
00168 }
00169
00170 static av_cold int codecctl_int(AVCodecContext *avctx,
00171 enum vp8e_enc_control_id id, int val)
00172 {
00173 VP8Context *ctx = avctx->priv_data;
00174 char buf[80];
00175 int width = -30;
00176 int res;
00177
00178 snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
00179 av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
00180
00181 res = vpx_codec_control(&ctx->encoder, id, val);
00182 if (res != VPX_CODEC_OK) {
00183 snprintf(buf, sizeof(buf), "Failed to set %s codec control",
00184 ctlidstr[id]);
00185 log_encoder_error(avctx, buf);
00186 }
00187
00188 return res == VPX_CODEC_OK ? 0 : AVERROR(EINVAL);
00189 }
00190
00191 static av_cold int vp8_free(AVCodecContext *avctx)
00192 {
00193 VP8Context *ctx = avctx->priv_data;
00194
00195 vpx_codec_destroy(&ctx->encoder);
00196 av_freep(&ctx->twopass_stats.buf);
00197 av_freep(&avctx->coded_frame);
00198 av_freep(&avctx->stats_out);
00199 free_frame_list(ctx->coded_frame_list);
00200 return 0;
00201 }
00202
00203 static av_cold int vp8_init(AVCodecContext *avctx)
00204 {
00205 VP8Context *ctx = avctx->priv_data;
00206 const struct vpx_codec_iface *iface = &vpx_codec_vp8_cx_algo;
00207 int cpuused = 3;
00208 struct vpx_codec_enc_cfg enccfg;
00209 int res;
00210
00211 av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
00212 av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
00213
00214 if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) {
00215 av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
00216 vpx_codec_err_to_string(res));
00217 return AVERROR(EINVAL);
00218 }
00219 dump_enc_cfg(avctx, &enccfg);
00220
00221 enccfg.g_w = avctx->width;
00222 enccfg.g_h = avctx->height;
00223 enccfg.g_timebase.num = avctx->time_base.num;
00224 enccfg.g_timebase.den = avctx->time_base.den;
00225 enccfg.g_threads = avctx->thread_count;
00226
00227 if (avctx->flags & CODEC_FLAG_PASS1)
00228 enccfg.g_pass = VPX_RC_FIRST_PASS;
00229 else if (avctx->flags & CODEC_FLAG_PASS2)
00230 enccfg.g_pass = VPX_RC_LAST_PASS;
00231 else
00232 enccfg.g_pass = VPX_RC_ONE_PASS;
00233
00234 if (avctx->rc_min_rate == avctx->rc_max_rate &&
00235 avctx->rc_min_rate == avctx->bit_rate)
00236 enccfg.rc_end_usage = VPX_CBR;
00237 enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
00238 AV_ROUND_NEAR_INF);
00239
00240 enccfg.rc_min_quantizer = avctx->qmin;
00241 enccfg.rc_max_quantizer = avctx->qmax;
00242 enccfg.rc_dropframe_thresh = avctx->frame_skip_threshold;
00243
00244
00245 enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
00246 enccfg.rc_2pass_vbr_minsection_pct =
00247 avctx->rc_min_rate * 100LL / avctx->bit_rate;
00248 if (avctx->rc_max_rate)
00249 enccfg.rc_2pass_vbr_maxsection_pct =
00250 avctx->rc_max_rate * 100LL / avctx->bit_rate;
00251
00252 if (avctx->rc_buffer_size)
00253 enccfg.rc_buf_sz =
00254 avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
00255 if (avctx->rc_initial_buffer_occupancy)
00256 enccfg.rc_buf_initial_sz =
00257 avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
00258 enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
00259
00260
00261 if (avctx->keyint_min == avctx->gop_size)
00262 enccfg.kf_min_dist = avctx->keyint_min;
00263 enccfg.kf_max_dist = avctx->gop_size;
00264
00265 if (enccfg.g_pass == VPX_RC_FIRST_PASS)
00266 enccfg.g_lag_in_frames = 0;
00267 else if (enccfg.g_pass == VPX_RC_LAST_PASS) {
00268 int decode_size;
00269
00270 if (!avctx->stats_in) {
00271 av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
00272 return AVERROR_INVALIDDATA;
00273 }
00274
00275 ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
00276 ctx->twopass_stats.buf = av_malloc(ctx->twopass_stats.sz);
00277 if (!ctx->twopass_stats.buf) {
00278 av_log(avctx, AV_LOG_ERROR,
00279 "Stat buffer alloc (%zu bytes) failed\n",
00280 ctx->twopass_stats.sz);
00281 return AVERROR(ENOMEM);
00282 }
00283 decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
00284 ctx->twopass_stats.sz);
00285 if (decode_size < 0) {
00286 av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
00287 return AVERROR_INVALIDDATA;
00288 }
00289
00290 ctx->twopass_stats.sz = decode_size;
00291 enccfg.rc_twopass_stats_in = ctx->twopass_stats;
00292 }
00293
00294 ctx->deadline = VPX_DL_GOOD_QUALITY;
00295
00296
00297
00298 if (avctx->profile != FF_PROFILE_UNKNOWN)
00299 enccfg.g_profile = avctx->profile;
00300
00301 dump_enc_cfg(avctx, &enccfg);
00302
00303 res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, 0);
00304 if (res != VPX_CODEC_OK) {
00305 log_encoder_error(avctx, "Failed to initialize encoder");
00306 return AVERROR(EINVAL);
00307 }
00308
00309
00310 av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n");
00311 codecctl_int(avctx, VP8E_SET_CPUUSED, cpuused);
00312 codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, avctx->noise_reduction);
00313 codecctl_int(avctx, VP8E_SET_TOKEN_PARTITIONS, av_log2(avctx->slices));
00314
00315
00316 vpx_img_wrap(&ctx->rawimg, VPX_IMG_FMT_I420, avctx->width, avctx->height, 1,
00317 (unsigned char*)1);
00318
00319 avctx->coded_frame = avcodec_alloc_frame();
00320 if (!avctx->coded_frame) {
00321 av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
00322 vp8_free(avctx);
00323 return AVERROR(ENOMEM);
00324 }
00325 return 0;
00326 }
00327
00328 static inline void cx_pktcpy(struct FrameListData *dst,
00329 const struct vpx_codec_cx_pkt *src)
00330 {
00331 dst->pts = src->data.frame.pts;
00332 dst->duration = src->data.frame.duration;
00333 dst->flags = src->data.frame.flags;
00334 dst->sz = src->data.frame.sz;
00335 dst->buf = src->data.frame.buf;
00336 }
00337
00346 static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
00347 uint8_t *buf, int buf_size, AVFrame *coded_frame)
00348 {
00349 if ((int) cx_frame->sz <= buf_size) {
00350 buf_size = cx_frame->sz;
00351 memcpy(buf, cx_frame->buf, buf_size);
00352 coded_frame->pts = cx_frame->pts;
00353 coded_frame->key_frame = !!(cx_frame->flags & VPX_FRAME_IS_KEY);
00354
00355 if (coded_frame->key_frame)
00356 coded_frame->pict_type = FF_I_TYPE;
00357 else
00358 coded_frame->pict_type = FF_P_TYPE;
00359 } else {
00360 av_log(avctx, AV_LOG_ERROR,
00361 "Compressed frame larger than storage provided! (%zu/%d)\n",
00362 cx_frame->sz, buf_size);
00363 return AVERROR(EINVAL);
00364 }
00365 return buf_size;
00366 }
00367
00376 static int queue_frames(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00377 AVFrame *coded_frame)
00378 {
00379 VP8Context *ctx = avctx->priv_data;
00380 const struct vpx_codec_cx_pkt *pkt;
00381 const void *iter = NULL;
00382 int size = 0;
00383
00384 if (ctx->coded_frame_list) {
00385 struct FrameListData *cx_frame = ctx->coded_frame_list;
00386
00387 size = storeframe(avctx, cx_frame, buf, buf_size, coded_frame);
00388 if (size < 0)
00389 return AVERROR(EINVAL);
00390 ctx->coded_frame_list = cx_frame->next;
00391 free_coded_frame(cx_frame);
00392 }
00393
00394
00395
00396 while ((pkt = vpx_codec_get_cx_data(&ctx->encoder, &iter))) {
00397 switch (pkt->kind) {
00398 case VPX_CODEC_CX_FRAME_PKT:
00399 if (!size) {
00400 struct FrameListData cx_frame;
00401
00402
00403
00404 assert(!ctx->coded_frame_list);
00405 cx_pktcpy(&cx_frame, pkt);
00406 size = storeframe(avctx, &cx_frame, buf, buf_size, coded_frame);
00407 if (size < 0)
00408 return AVERROR(EINVAL);
00409 } else {
00410 struct FrameListData *cx_frame =
00411 av_malloc(sizeof(struct FrameListData));
00412
00413 if (!cx_frame) {
00414 av_log(avctx, AV_LOG_ERROR,
00415 "Frame queue element alloc failed\n");
00416 return AVERROR(ENOMEM);
00417 }
00418 cx_pktcpy(cx_frame, pkt);
00419 cx_frame->buf = av_malloc(cx_frame->sz);
00420
00421 if (!cx_frame->buf) {
00422 av_log(avctx, AV_LOG_ERROR,
00423 "Data buffer alloc (%zu bytes) failed\n",
00424 cx_frame->sz);
00425 return AVERROR(ENOMEM);
00426 }
00427 memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
00428 coded_frame_add(&ctx->coded_frame_list, cx_frame);
00429 }
00430 break;
00431 case VPX_CODEC_STATS_PKT: {
00432 struct vpx_fixed_buf *stats = &ctx->twopass_stats;
00433 stats->buf = av_realloc(stats->buf,
00434 stats->sz + pkt->data.twopass_stats.sz);
00435 if (!stats->buf) {
00436 av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
00437 return AVERROR(ENOMEM);
00438 }
00439 memcpy((uint8_t*)stats->buf + stats->sz,
00440 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
00441 stats->sz += pkt->data.twopass_stats.sz;
00442 break;
00443 }
00444 case VPX_CODEC_PSNR_PKT:
00445 case VPX_CODEC_CUSTOM_PKT:
00446
00447 break;
00448 }
00449 }
00450
00451 return size;
00452 }
00453
00454 static int vp8_encode(AVCodecContext *avctx, uint8_t *buf, int buf_size,
00455 void *data)
00456 {
00457 VP8Context *ctx = avctx->priv_data;
00458 AVFrame *frame = data;
00459 struct vpx_image *rawimg = NULL;
00460 int64_t timestamp = 0;
00461 int res, coded_size;
00462
00463 if (frame) {
00464 rawimg = &ctx->rawimg;
00465 rawimg->planes[VPX_PLANE_Y] = frame->data[0];
00466 rawimg->planes[VPX_PLANE_U] = frame->data[1];
00467 rawimg->planes[VPX_PLANE_V] = frame->data[2];
00468 rawimg->stride[VPX_PLANE_Y] = frame->linesize[0];
00469 rawimg->stride[VPX_PLANE_U] = frame->linesize[1];
00470 rawimg->stride[VPX_PLANE_V] = frame->linesize[2];
00471 timestamp = frame->pts;
00472 }
00473
00474 res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp,
00475 avctx->ticks_per_frame, 0, ctx->deadline);
00476 if (res != VPX_CODEC_OK) {
00477 log_encoder_error(avctx, "Error encoding frame");
00478 return AVERROR_INVALIDDATA;
00479 }
00480 coded_size = queue_frames(avctx, buf, buf_size, avctx->coded_frame);
00481
00482 if (!frame && avctx->flags & CODEC_FLAG_PASS1) {
00483 unsigned int b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
00484
00485 avctx->stats_out = av_malloc(b64_size);
00486 if (!avctx->stats_out) {
00487 av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%d bytes) failed\n",
00488 b64_size);
00489 return AVERROR(ENOMEM);
00490 }
00491 av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
00492 ctx->twopass_stats.sz);
00493 }
00494 return coded_size;
00495 }
00496
00497 AVCodec ff_libvpx_encoder = {
00498 "libvpx",
00499 AVMEDIA_TYPE_VIDEO,
00500 CODEC_ID_VP8,
00501 sizeof(VP8Context),
00502 vp8_init,
00503 vp8_encode,
00504 vp8_free,
00505 NULL,
00506 CODEC_CAP_DELAY,
00507 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
00508 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
00509 };