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 "internal.h"
00033 #include "libavutil/avassert.h"
00034 #include "libavutil/base64.h"
00035 #include "libavutil/common.h"
00036 #include "libavutil/mathematics.h"
00037 #include "libavutil/opt.h"
00038
00043 struct FrameListData {
00044 void *buf;
00045 size_t sz;
00046 int64_t pts;
00048 unsigned long duration;
00050 uint32_t flags;
00051 struct FrameListData *next;
00052 };
00053
00054 typedef struct VP8EncoderContext {
00055 AVClass *class;
00056 struct vpx_codec_ctx encoder;
00057 struct vpx_image rawimg;
00058 struct vpx_fixed_buf twopass_stats;
00059 int deadline;
00060 struct FrameListData *coded_frame_list;
00061
00062 int cpu_used;
00066 int flags;
00067 #define VP8F_ERROR_RESILIENT 0x00000001
00068 #define VP8F_AUTO_ALT_REF 0x00000002
00069
00070 int auto_alt_ref;
00071
00072 int arnr_max_frames;
00073 int arnr_strength;
00074 int arnr_type;
00075
00076 int lag_in_frames;
00077 int error_resilient;
00078 int crf;
00079 int max_intra_rate;
00080 } VP8Context;
00081
00083 static const char *const ctlidstr[] = {
00084 [VP8E_UPD_ENTROPY] = "VP8E_UPD_ENTROPY",
00085 [VP8E_UPD_REFERENCE] = "VP8E_UPD_REFERENCE",
00086 [VP8E_USE_REFERENCE] = "VP8E_USE_REFERENCE",
00087 [VP8E_SET_ROI_MAP] = "VP8E_SET_ROI_MAP",
00088 [VP8E_SET_ACTIVEMAP] = "VP8E_SET_ACTIVEMAP",
00089 [VP8E_SET_SCALEMODE] = "VP8E_SET_SCALEMODE",
00090 [VP8E_SET_CPUUSED] = "VP8E_SET_CPUUSED",
00091 [VP8E_SET_ENABLEAUTOALTREF] = "VP8E_SET_ENABLEAUTOALTREF",
00092 [VP8E_SET_NOISE_SENSITIVITY] = "VP8E_SET_NOISE_SENSITIVITY",
00093 [VP8E_SET_SHARPNESS] = "VP8E_SET_SHARPNESS",
00094 [VP8E_SET_STATIC_THRESHOLD] = "VP8E_SET_STATIC_THRESHOLD",
00095 [VP8E_SET_TOKEN_PARTITIONS] = "VP8E_SET_TOKEN_PARTITIONS",
00096 [VP8E_GET_LAST_QUANTIZER] = "VP8E_GET_LAST_QUANTIZER",
00097 [VP8E_SET_ARNR_MAXFRAMES] = "VP8E_SET_ARNR_MAXFRAMES",
00098 [VP8E_SET_ARNR_STRENGTH] = "VP8E_SET_ARNR_STRENGTH",
00099 [VP8E_SET_ARNR_TYPE] = "VP8E_SET_ARNR_TYPE",
00100 [VP8E_SET_CQ_LEVEL] = "VP8E_SET_CQ_LEVEL",
00101 [VP8E_SET_MAX_INTRA_BITRATE_PCT] = "VP8E_SET_MAX_INTRA_BITRATE_PCT",
00102 };
00103
00104 static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
00105 {
00106 VP8Context *ctx = avctx->priv_data;
00107 const char *error = vpx_codec_error(&ctx->encoder);
00108 const char *detail = vpx_codec_error_detail(&ctx->encoder);
00109
00110 av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
00111 if (detail)
00112 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
00113 }
00114
00115 static av_cold void dump_enc_cfg(AVCodecContext *avctx,
00116 const struct vpx_codec_enc_cfg *cfg)
00117 {
00118 int width = -30;
00119 int level = AV_LOG_DEBUG;
00120
00121 av_log(avctx, level, "vpx_codec_enc_cfg\n");
00122 av_log(avctx, level, "generic settings\n"
00123 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
00124 " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
00125 width, "g_usage:", cfg->g_usage,
00126 width, "g_threads:", cfg->g_threads,
00127 width, "g_profile:", cfg->g_profile,
00128 width, "g_w:", cfg->g_w,
00129 width, "g_h:", cfg->g_h,
00130 width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
00131 width, "g_error_resilient:", cfg->g_error_resilient,
00132 width, "g_pass:", cfg->g_pass,
00133 width, "g_lag_in_frames:", cfg->g_lag_in_frames);
00134 av_log(avctx, level, "rate control settings\n"
00135 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
00136 " %*s%d\n %*s%p(%zu)\n %*s%u\n",
00137 width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
00138 width, "rc_resize_allowed:", cfg->rc_resize_allowed,
00139 width, "rc_resize_up_thresh:", cfg->rc_resize_up_thresh,
00140 width, "rc_resize_down_thresh:", cfg->rc_resize_down_thresh,
00141 width, "rc_end_usage:", cfg->rc_end_usage,
00142 width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
00143 width, "rc_target_bitrate:", cfg->rc_target_bitrate);
00144 av_log(avctx, level, "quantizer settings\n"
00145 " %*s%u\n %*s%u\n",
00146 width, "rc_min_quantizer:", cfg->rc_min_quantizer,
00147 width, "rc_max_quantizer:", cfg->rc_max_quantizer);
00148 av_log(avctx, level, "bitrate tolerance\n"
00149 " %*s%u\n %*s%u\n",
00150 width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
00151 width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
00152 av_log(avctx, level, "decoder buffer model\n"
00153 " %*s%u\n %*s%u\n %*s%u\n",
00154 width, "rc_buf_sz:", cfg->rc_buf_sz,
00155 width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
00156 width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
00157 av_log(avctx, level, "2 pass rate control settings\n"
00158 " %*s%u\n %*s%u\n %*s%u\n",
00159 width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
00160 width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
00161 width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
00162 av_log(avctx, level, "keyframing settings\n"
00163 " %*s%d\n %*s%u\n %*s%u\n",
00164 width, "kf_mode:", cfg->kf_mode,
00165 width, "kf_min_dist:", cfg->kf_min_dist,
00166 width, "kf_max_dist:", cfg->kf_max_dist);
00167 av_log(avctx, level, "\n");
00168 }
00169
00170 static void coded_frame_add(void *list, struct FrameListData *cx_frame)
00171 {
00172 struct FrameListData **p = list;
00173
00174 while (*p != NULL)
00175 p = &(*p)->next;
00176 *p = cx_frame;
00177 cx_frame->next = NULL;
00178 }
00179
00180 static av_cold void free_coded_frame(struct FrameListData *cx_frame)
00181 {
00182 av_freep(&cx_frame->buf);
00183 av_freep(&cx_frame);
00184 }
00185
00186 static av_cold void free_frame_list(struct FrameListData *list)
00187 {
00188 struct FrameListData *p = list;
00189
00190 while (p) {
00191 list = list->next;
00192 free_coded_frame(p);
00193 p = list;
00194 }
00195 }
00196
00197 static av_cold int codecctl_int(AVCodecContext *avctx,
00198 enum vp8e_enc_control_id id, int val)
00199 {
00200 VP8Context *ctx = avctx->priv_data;
00201 char buf[80];
00202 int width = -30;
00203 int res;
00204
00205 snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
00206 av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
00207
00208 res = vpx_codec_control(&ctx->encoder, id, val);
00209 if (res != VPX_CODEC_OK) {
00210 snprintf(buf, sizeof(buf), "Failed to set %s codec control",
00211 ctlidstr[id]);
00212 log_encoder_error(avctx, buf);
00213 }
00214
00215 return res == VPX_CODEC_OK ? 0 : AVERROR(EINVAL);
00216 }
00217
00218 static av_cold int vp8_free(AVCodecContext *avctx)
00219 {
00220 VP8Context *ctx = avctx->priv_data;
00221
00222 vpx_codec_destroy(&ctx->encoder);
00223 av_freep(&ctx->twopass_stats.buf);
00224 av_freep(&avctx->coded_frame);
00225 av_freep(&avctx->stats_out);
00226 free_frame_list(ctx->coded_frame_list);
00227 return 0;
00228 }
00229
00230 static av_cold int vp8_init(AVCodecContext *avctx)
00231 {
00232 VP8Context *ctx = avctx->priv_data;
00233 const struct vpx_codec_iface *iface = &vpx_codec_vp8_cx_algo;
00234 struct vpx_codec_enc_cfg enccfg;
00235 int res;
00236
00237 av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
00238 av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
00239
00240 if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) {
00241 av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
00242 vpx_codec_err_to_string(res));
00243 return AVERROR(EINVAL);
00244 }
00245
00246 if(!avctx->bit_rate)
00247 if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) {
00248 av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n");
00249 return AVERROR(EINVAL);
00250 }
00251
00252 dump_enc_cfg(avctx, &enccfg);
00253
00254 enccfg.g_w = avctx->width;
00255 enccfg.g_h = avctx->height;
00256 enccfg.g_timebase.num = avctx->time_base.num;
00257 enccfg.g_timebase.den = avctx->time_base.den;
00258 enccfg.g_threads = avctx->thread_count;
00259 enccfg.g_lag_in_frames= ctx->lag_in_frames;
00260
00261 if (avctx->flags & CODEC_FLAG_PASS1)
00262 enccfg.g_pass = VPX_RC_FIRST_PASS;
00263 else if (avctx->flags & CODEC_FLAG_PASS2)
00264 enccfg.g_pass = VPX_RC_LAST_PASS;
00265 else
00266 enccfg.g_pass = VPX_RC_ONE_PASS;
00267
00268 if (avctx->rc_min_rate == avctx->rc_max_rate &&
00269 avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate)
00270 enccfg.rc_end_usage = VPX_CBR;
00271 else if (ctx->crf)
00272 enccfg.rc_end_usage = VPX_CQ;
00273
00274 if (avctx->bit_rate) {
00275 enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
00276 AV_ROUND_NEAR_INF);
00277 } else {
00278 if (enccfg.rc_end_usage == VPX_CQ) {
00279 enccfg.rc_target_bitrate = 1000000;
00280 } else {
00281 avctx->bit_rate = enccfg.rc_target_bitrate * 1000;
00282 av_log(avctx, AV_LOG_WARNING,
00283 "Neither bitrate nor constrained quality specified, using default bitrate of %dkbit/sec\n",
00284 enccfg.rc_target_bitrate);
00285 }
00286 }
00287
00288 if (avctx->qmin > 0)
00289 enccfg.rc_min_quantizer = avctx->qmin;
00290 if (avctx->qmax > 0)
00291 enccfg.rc_max_quantizer = avctx->qmax;
00292 enccfg.rc_dropframe_thresh = avctx->frame_skip_threshold;
00293
00294
00295 enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
00296 if (avctx->bit_rate)
00297 enccfg.rc_2pass_vbr_minsection_pct =
00298 avctx->rc_min_rate * 100LL / avctx->bit_rate;
00299 if (avctx->rc_max_rate)
00300 enccfg.rc_2pass_vbr_maxsection_pct =
00301 avctx->rc_max_rate * 100LL / avctx->bit_rate;
00302
00303 if (avctx->rc_buffer_size)
00304 enccfg.rc_buf_sz =
00305 avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
00306 if (avctx->rc_initial_buffer_occupancy)
00307 enccfg.rc_buf_initial_sz =
00308 avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
00309 enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
00310 enccfg.rc_undershoot_pct = round(avctx->rc_buffer_aggressivity * 100);
00311
00312
00313 if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
00314 enccfg.kf_min_dist = avctx->keyint_min;
00315 if (avctx->gop_size >= 0)
00316 enccfg.kf_max_dist = avctx->gop_size;
00317
00318 if (enccfg.g_pass == VPX_RC_FIRST_PASS)
00319 enccfg.g_lag_in_frames = 0;
00320 else if (enccfg.g_pass == VPX_RC_LAST_PASS) {
00321 int decode_size;
00322
00323 if (!avctx->stats_in) {
00324 av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
00325 return AVERROR_INVALIDDATA;
00326 }
00327
00328 ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
00329 ctx->twopass_stats.buf = av_malloc(ctx->twopass_stats.sz);
00330 if (!ctx->twopass_stats.buf) {
00331 av_log(avctx, AV_LOG_ERROR,
00332 "Stat buffer alloc (%zu bytes) failed\n",
00333 ctx->twopass_stats.sz);
00334 return AVERROR(ENOMEM);
00335 }
00336 decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
00337 ctx->twopass_stats.sz);
00338 if (decode_size < 0) {
00339 av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
00340 return AVERROR_INVALIDDATA;
00341 }
00342
00343 ctx->twopass_stats.sz = decode_size;
00344 enccfg.rc_twopass_stats_in = ctx->twopass_stats;
00345 }
00346
00347
00348
00349
00350 if (avctx->profile != FF_PROFILE_UNKNOWN)
00351 enccfg.g_profile = avctx->profile;
00352
00353 enccfg.g_error_resilient = ctx->error_resilient || ctx->flags & VP8F_ERROR_RESILIENT;
00354
00355 dump_enc_cfg(avctx, &enccfg);
00356
00357 res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, 0);
00358 if (res != VPX_CODEC_OK) {
00359 log_encoder_error(avctx, "Failed to initialize encoder");
00360 return AVERROR(EINVAL);
00361 }
00362
00363
00364 av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n");
00365 if (ctx->cpu_used != INT_MIN)
00366 codecctl_int(avctx, VP8E_SET_CPUUSED, ctx->cpu_used);
00367 if (ctx->flags & VP8F_AUTO_ALT_REF)
00368 ctx->auto_alt_ref = 1;
00369 if (ctx->auto_alt_ref >= 0)
00370 codecctl_int(avctx, VP8E_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref);
00371 if (ctx->arnr_max_frames >= 0)
00372 codecctl_int(avctx, VP8E_SET_ARNR_MAXFRAMES, ctx->arnr_max_frames);
00373 if (ctx->arnr_strength >= 0)
00374 codecctl_int(avctx, VP8E_SET_ARNR_STRENGTH, ctx->arnr_strength);
00375 if (ctx->arnr_type >= 0)
00376 codecctl_int(avctx, VP8E_SET_ARNR_TYPE, ctx->arnr_type);
00377 codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, avctx->noise_reduction);
00378 codecctl_int(avctx, VP8E_SET_TOKEN_PARTITIONS, av_log2(avctx->slices));
00379 codecctl_int(avctx, VP8E_SET_STATIC_THRESHOLD, avctx->mb_threshold);
00380 codecctl_int(avctx, VP8E_SET_CQ_LEVEL, ctx->crf);
00381 if (ctx->max_intra_rate >= 0)
00382 codecctl_int(avctx, VP8E_SET_MAX_INTRA_BITRATE_PCT, ctx->max_intra_rate);
00383
00384 av_log(avctx, AV_LOG_DEBUG, "Using deadline: %d\n", ctx->deadline);
00385
00386
00387 vpx_img_wrap(&ctx->rawimg, VPX_IMG_FMT_I420, avctx->width, avctx->height, 1,
00388 (unsigned char*)1);
00389
00390 avctx->coded_frame = avcodec_alloc_frame();
00391 if (!avctx->coded_frame) {
00392 av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
00393 vp8_free(avctx);
00394 return AVERROR(ENOMEM);
00395 }
00396 return 0;
00397 }
00398
00399 static inline void cx_pktcpy(struct FrameListData *dst,
00400 const struct vpx_codec_cx_pkt *src)
00401 {
00402 dst->pts = src->data.frame.pts;
00403 dst->duration = src->data.frame.duration;
00404 dst->flags = src->data.frame.flags;
00405 dst->sz = src->data.frame.sz;
00406 dst->buf = src->data.frame.buf;
00407 }
00408
00416 static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
00417 AVPacket *pkt, AVFrame *coded_frame)
00418 {
00419 int ret = ff_alloc_packet2(avctx, pkt, cx_frame->sz);
00420 if (ret >= 0) {
00421 memcpy(pkt->data, cx_frame->buf, pkt->size);
00422 pkt->pts = pkt->dts = cx_frame->pts;
00423 coded_frame->pts = cx_frame->pts;
00424 coded_frame->key_frame = !!(cx_frame->flags & VPX_FRAME_IS_KEY);
00425
00426 if (coded_frame->key_frame) {
00427 coded_frame->pict_type = AV_PICTURE_TYPE_I;
00428 pkt->flags |= AV_PKT_FLAG_KEY;
00429 } else
00430 coded_frame->pict_type = AV_PICTURE_TYPE_P;
00431 } else {
00432 return ret;
00433 }
00434 return pkt->size;
00435 }
00436
00445 static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out,
00446 AVFrame *coded_frame)
00447 {
00448 VP8Context *ctx = avctx->priv_data;
00449 const struct vpx_codec_cx_pkt *pkt;
00450 const void *iter = NULL;
00451 int size = 0;
00452
00453 if (ctx->coded_frame_list) {
00454 struct FrameListData *cx_frame = ctx->coded_frame_list;
00455
00456 size = storeframe(avctx, cx_frame, pkt_out, coded_frame);
00457 if (size < 0)
00458 return size;
00459 ctx->coded_frame_list = cx_frame->next;
00460 free_coded_frame(cx_frame);
00461 }
00462
00463
00464
00465 while ((pkt = vpx_codec_get_cx_data(&ctx->encoder, &iter))) {
00466 switch (pkt->kind) {
00467 case VPX_CODEC_CX_FRAME_PKT:
00468 if (!size) {
00469 struct FrameListData cx_frame;
00470
00471
00472
00473 av_assert0(!ctx->coded_frame_list);
00474 cx_pktcpy(&cx_frame, pkt);
00475 size = storeframe(avctx, &cx_frame, pkt_out, coded_frame);
00476 if (size < 0)
00477 return size;
00478 } else {
00479 struct FrameListData *cx_frame =
00480 av_malloc(sizeof(struct FrameListData));
00481
00482 if (!cx_frame) {
00483 av_log(avctx, AV_LOG_ERROR,
00484 "Frame queue element alloc failed\n");
00485 return AVERROR(ENOMEM);
00486 }
00487 cx_pktcpy(cx_frame, pkt);
00488 cx_frame->buf = av_malloc(cx_frame->sz);
00489
00490 if (!cx_frame->buf) {
00491 av_log(avctx, AV_LOG_ERROR,
00492 "Data buffer alloc (%zu bytes) failed\n",
00493 cx_frame->sz);
00494 return AVERROR(ENOMEM);
00495 }
00496 memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
00497 coded_frame_add(&ctx->coded_frame_list, cx_frame);
00498 }
00499 break;
00500 case VPX_CODEC_STATS_PKT: {
00501 struct vpx_fixed_buf *stats = &ctx->twopass_stats;
00502 stats->buf = av_realloc_f(stats->buf, 1,
00503 stats->sz + pkt->data.twopass_stats.sz);
00504 if (!stats->buf) {
00505 av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
00506 return AVERROR(ENOMEM);
00507 }
00508 memcpy((uint8_t*)stats->buf + stats->sz,
00509 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
00510 stats->sz += pkt->data.twopass_stats.sz;
00511 break;
00512 }
00513 case VPX_CODEC_PSNR_PKT:
00514 case VPX_CODEC_CUSTOM_PKT:
00515
00516 break;
00517 }
00518 }
00519
00520 return size;
00521 }
00522
00523 static int vp8_encode(AVCodecContext *avctx, AVPacket *pkt,
00524 const AVFrame *frame, int *got_packet)
00525 {
00526 VP8Context *ctx = avctx->priv_data;
00527 struct vpx_image *rawimg = NULL;
00528 int64_t timestamp = 0;
00529 long flags = 0;
00530 int res, coded_size;
00531
00532 if (frame) {
00533 rawimg = &ctx->rawimg;
00534 rawimg->planes[VPX_PLANE_Y] = frame->data[0];
00535 rawimg->planes[VPX_PLANE_U] = frame->data[1];
00536 rawimg->planes[VPX_PLANE_V] = frame->data[2];
00537 rawimg->stride[VPX_PLANE_Y] = frame->linesize[0];
00538 rawimg->stride[VPX_PLANE_U] = frame->linesize[1];
00539 rawimg->stride[VPX_PLANE_V] = frame->linesize[2];
00540 timestamp = frame->pts;
00541 flags = frame->pict_type == AV_PICTURE_TYPE_I ? VPX_EFLAG_FORCE_KF : 0;
00542 }
00543
00544 res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp,
00545 avctx->ticks_per_frame, flags, ctx->deadline);
00546 if (res != VPX_CODEC_OK) {
00547 log_encoder_error(avctx, "Error encoding frame");
00548 return AVERROR_INVALIDDATA;
00549 }
00550 coded_size = queue_frames(avctx, pkt, avctx->coded_frame);
00551
00552 if (!frame && avctx->flags & CODEC_FLAG_PASS1) {
00553 unsigned int b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
00554
00555 avctx->stats_out = av_malloc(b64_size);
00556 if (!avctx->stats_out) {
00557 av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%d bytes) failed\n",
00558 b64_size);
00559 return AVERROR(ENOMEM);
00560 }
00561 av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
00562 ctx->twopass_stats.sz);
00563 }
00564
00565 *got_packet = !!coded_size;
00566 return 0;
00567 }
00568
00569 #define OFFSET(x) offsetof(VP8Context, x)
00570 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
00571 static const AVOption options[] = {
00572 { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = INT_MIN}, INT_MIN, INT_MAX, VE},
00573 { "auto-alt-ref", "Enable use of alternate reference "
00574 "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
00575 { "lag-in-frames", "Number of frames to look ahead for "
00576 "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
00577 { "arnr-maxframes", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
00578 { "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
00579 { "arnr-type", "altref noise reduction filter type", OFFSET(arnr_type), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "arnr_type"},
00580 { "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "arnr_type" },
00581 { "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "arnr_type" },
00582 { "centered", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "arnr_type" },
00583 { "deadline", "Time to spend encoding, in microseconds.", OFFSET(deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"},
00584 { "best", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_BEST_QUALITY}, 0, 0, VE, "quality"},
00585 { "good", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_GOOD_QUALITY}, 0, 0, VE, "quality"},
00586 { "realtime", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_REALTIME}, 0, 0, VE, "quality"},
00587 { "error-resilient", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"},
00588 { "max-intra-rate", "Maximum I-frame bitrate (pct) 0=unlimited", OFFSET(max_intra_rate), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
00589 #ifdef VPX_ERROR_RESILIENT_DEFAULT
00590 { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"},
00591 { "partitions", "The frame partitions are independently decodable "
00592 "by the bool decoder, meaning that partitions can be decoded even "
00593 "though earlier partitions have been lost. Note that intra predicition"
00594 " is still done over the partition boundary.", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_PARTITIONS}, 0, 0, VE, "er"},
00595 #endif
00596 {"speed", "", offsetof(VP8Context, cpu_used), AV_OPT_TYPE_INT, {.i64 = 3}, -16, 16, VE},
00597 {"quality", "", offsetof(VP8Context, deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"},
00598 {"vp8flags", "", offsetof(VP8Context, flags), FF_OPT_TYPE_FLAGS, {.dbl = 0}, 0, UINT_MAX, VE, "flags"},
00599 {"error_resilient", "enable error resilience", 0, FF_OPT_TYPE_CONST, {.dbl = VP8F_ERROR_RESILIENT}, INT_MIN, INT_MAX, VE, "flags"},
00600 {"altref", "enable use of alternate reference frames (VP8/2-pass only)", 0, FF_OPT_TYPE_CONST, {.dbl = VP8F_AUTO_ALT_REF}, INT_MIN, INT_MAX, VE, "flags"},
00601 {"arnr_max_frames", "altref noise reduction max frame count", offsetof(VP8Context, arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 15, VE},
00602 {"arnr_strength", "altref noise reduction filter strength", offsetof(VP8Context, arnr_strength), AV_OPT_TYPE_INT, {.i64 = 3}, 0, 6, VE},
00603 {"arnr_type", "altref noise reduction filter type", offsetof(VP8Context, arnr_type), AV_OPT_TYPE_INT, {.i64 = 3}, 1, 3, VE},
00604 {"rc_lookahead", "Number of frames to look ahead for alternate reference frame selection", offsetof(VP8Context, lag_in_frames), AV_OPT_TYPE_INT, {.i64 = 25}, 0, 25, VE},
00605 {"crf", "Select the quality for constant quality mode", offsetof(VP8Context, crf), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 63, VE},
00606 {NULL}
00607 };
00608
00609 static const AVClass class = {
00610 .class_name = "libvpx encoder",
00611 .item_name = av_default_item_name,
00612 .option = options,
00613 .version = LIBAVUTIL_VERSION_INT,
00614 };
00615
00616 static const AVCodecDefault defaults[] = {
00617 { "qmin", "-1" },
00618 { "qmax", "-1" },
00619 { "g", "-1" },
00620 { "keyint_min", "-1" },
00621 { NULL },
00622 };
00623
00624 AVCodec ff_libvpx_encoder = {
00625 .name = "libvpx",
00626 .type = AVMEDIA_TYPE_VIDEO,
00627 .id = AV_CODEC_ID_VP8,
00628 .priv_data_size = sizeof(VP8Context),
00629 .init = vp8_init,
00630 .encode2 = vp8_encode,
00631 .close = vp8_free,
00632 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
00633 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV420P, PIX_FMT_NONE },
00634 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
00635 .priv_class = &class,
00636 .defaults = defaults,
00637 };