Libav
|
00001 /* 00002 * Copyright (c) 2010, Google, Inc. 00003 * 00004 * This file is part of FFmpeg. 00005 * 00006 * FFmpeg is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * FFmpeg is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with FFmpeg; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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; //i.e., RT/GOOD/BEST 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 //convert [1,51] -> [0,63] 00241 enccfg.rc_min_quantizer = ((avctx->qmin * 5 + 1) >> 2) - 1; 00242 enccfg.rc_max_quantizer = ((avctx->qmax * 5 + 1) >> 2) - 1; 00243 00244 if (avctx->keyint_min == avctx->gop_size) 00245 enccfg.kf_mode = VPX_KF_FIXED; 00246 //_enc_init() will balk if kf_min_dist is set in this case 00247 if (enccfg.kf_mode != VPX_KF_AUTO) 00248 enccfg.kf_min_dist = avctx->keyint_min; 00249 enccfg.kf_max_dist = avctx->gop_size; 00250 00251 if (enccfg.g_pass == VPX_RC_FIRST_PASS) 00252 enccfg.g_lag_in_frames = 0; 00253 else if (enccfg.g_pass == VPX_RC_LAST_PASS) { 00254 int decode_size; 00255 00256 if (!avctx->stats_in) { 00257 av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n"); 00258 return AVERROR_INVALIDDATA; 00259 } 00260 00261 ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4; 00262 ctx->twopass_stats.buf = av_malloc(ctx->twopass_stats.sz); 00263 if (!ctx->twopass_stats.buf) { 00264 av_log(avctx, AV_LOG_ERROR, 00265 "Stat buffer alloc (%zu bytes) failed\n", 00266 ctx->twopass_stats.sz); 00267 return AVERROR(ENOMEM); 00268 } 00269 decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in, 00270 ctx->twopass_stats.sz); 00271 if (decode_size < 0) { 00272 av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n"); 00273 return AVERROR_INVALIDDATA; 00274 } 00275 00276 ctx->twopass_stats.sz = decode_size; 00277 enccfg.rc_twopass_stats_in = ctx->twopass_stats; 00278 } 00279 00280 ctx->deadline = VPX_DL_GOOD_QUALITY; 00281 00282 dump_enc_cfg(avctx, &enccfg); 00283 /* Construct Encoder Context */ 00284 res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, 0); 00285 if (res != VPX_CODEC_OK) { 00286 log_encoder_error(avctx, "Failed to initialize encoder"); 00287 return AVERROR(EINVAL); 00288 } 00289 00290 //codec control failures are currently treated only as warnings 00291 av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n"); 00292 codecctl_int(avctx, VP8E_SET_CPUUSED, cpuused); 00293 codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, avctx->noise_reduction); 00294 00295 //provide dummy value to initialize wrapper, values will be updated each _encode() 00296 vpx_img_wrap(&ctx->rawimg, VPX_IMG_FMT_I420, avctx->width, avctx->height, 1, 00297 (unsigned char*)1); 00298 00299 avctx->coded_frame = avcodec_alloc_frame(); 00300 if (!avctx->coded_frame) { 00301 av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n"); 00302 vp8_free(avctx); 00303 return AVERROR(ENOMEM); 00304 } 00305 return 0; 00306 } 00307 00308 static inline void cx_pktcpy(struct FrameListData *dst, 00309 const struct vpx_codec_cx_pkt *src) 00310 { 00311 dst->pts = src->data.frame.pts; 00312 dst->duration = src->data.frame.duration; 00313 dst->flags = src->data.frame.flags; 00314 dst->sz = src->data.frame.sz; 00315 dst->buf = src->data.frame.buf; 00316 } 00317 00326 static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame, 00327 uint8_t *buf, int buf_size, AVFrame *coded_frame) 00328 { 00329 if ((int) cx_frame->sz <= buf_size) { 00330 buf_size = cx_frame->sz; 00331 memcpy(buf, cx_frame->buf, buf_size); 00332 coded_frame->pts = cx_frame->pts; 00333 coded_frame->key_frame = !!(cx_frame->flags & VPX_FRAME_IS_KEY); 00334 00335 if (coded_frame->key_frame) 00336 coded_frame->pict_type = FF_I_TYPE; 00337 else 00338 coded_frame->pict_type = FF_P_TYPE; 00339 } else { 00340 av_log(avctx, AV_LOG_ERROR, 00341 "Compressed frame larger than storage provided! (%zu/%d)\n", 00342 cx_frame->sz, buf_size); 00343 return AVERROR(EINVAL); 00344 } 00345 return buf_size; 00346 } 00347 00356 static int queue_frames(AVCodecContext *avctx, uint8_t *buf, int buf_size, 00357 AVFrame *coded_frame) 00358 { 00359 VP8Context *ctx = avctx->priv_data; 00360 const struct vpx_codec_cx_pkt *pkt; 00361 const void *iter = NULL; 00362 int size = 0; 00363 00364 if (ctx->coded_frame_list) { 00365 struct FrameListData *cx_frame = ctx->coded_frame_list; 00366 /* return the leading frame if we've already begun queueing */ 00367 size = storeframe(avctx, cx_frame, buf, buf_size, coded_frame); 00368 if (size < 0) 00369 return AVERROR(EINVAL); 00370 ctx->coded_frame_list = cx_frame->next; 00371 free_coded_frame(cx_frame); 00372 } 00373 00374 /* consume all available output from the encoder before returning. buffers 00375 are only good through the next vpx_codec call */ 00376 while ((pkt = vpx_codec_get_cx_data(&ctx->encoder, &iter))) { 00377 switch (pkt->kind) { 00378 case VPX_CODEC_CX_FRAME_PKT: 00379 if (!size) { 00380 struct FrameListData cx_frame; 00381 00382 /* avoid storing the frame when the list is empty and we haven't yet 00383 provided a frame for output */ 00384 assert(!ctx->coded_frame_list); 00385 cx_pktcpy(&cx_frame, pkt); 00386 size = storeframe(avctx, &cx_frame, buf, buf_size, coded_frame); 00387 if (size < 0) 00388 return AVERROR(EINVAL); 00389 } else { 00390 struct FrameListData *cx_frame = 00391 av_malloc(sizeof(struct FrameListData)); 00392 00393 if (!cx_frame) { 00394 av_log(avctx, AV_LOG_ERROR, 00395 "Frame queue element alloc failed\n"); 00396 return AVERROR(ENOMEM); 00397 } 00398 cx_pktcpy(cx_frame, pkt); 00399 cx_frame->buf = av_malloc(cx_frame->sz); 00400 00401 if (!cx_frame->buf) { 00402 av_log(avctx, AV_LOG_ERROR, 00403 "Data buffer alloc (%zu bytes) failed\n", 00404 cx_frame->sz); 00405 return AVERROR(ENOMEM); 00406 } 00407 memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz); 00408 coded_frame_add(&ctx->coded_frame_list, cx_frame); 00409 } 00410 break; 00411 case VPX_CODEC_STATS_PKT: { 00412 struct vpx_fixed_buf *stats = &ctx->twopass_stats; 00413 stats->buf = av_realloc(stats->buf, 00414 stats->sz + pkt->data.twopass_stats.sz); 00415 if (!stats->buf) { 00416 av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n"); 00417 return AVERROR(ENOMEM); 00418 } 00419 memcpy((uint8_t*)stats->buf + stats->sz, 00420 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz); 00421 stats->sz += pkt->data.twopass_stats.sz; 00422 break; 00423 } 00424 case VPX_CODEC_PSNR_PKT: //FIXME add support for CODEC_FLAG_PSNR 00425 case VPX_CODEC_CUSTOM_PKT: 00426 //ignore unsupported/unrecognized packet types 00427 break; 00428 } 00429 } 00430 00431 return size; 00432 } 00433 00434 static int vp8_encode(AVCodecContext *avctx, uint8_t *buf, int buf_size, 00435 void *data) 00436 { 00437 VP8Context *ctx = avctx->priv_data; 00438 AVFrame *frame = data; 00439 struct vpx_image *rawimg = NULL; 00440 int64_t timestamp = 0; 00441 int res, coded_size; 00442 00443 if (frame) { 00444 rawimg = &ctx->rawimg; 00445 rawimg->planes[VPX_PLANE_Y] = frame->data[0]; 00446 rawimg->planes[VPX_PLANE_U] = frame->data[1]; 00447 rawimg->planes[VPX_PLANE_V] = frame->data[2]; 00448 rawimg->stride[VPX_PLANE_Y] = frame->linesize[0]; 00449 rawimg->stride[VPX_PLANE_U] = frame->linesize[1]; 00450 rawimg->stride[VPX_PLANE_V] = frame->linesize[2]; 00451 timestamp = frame->pts; 00452 } 00453 00454 res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp, 00455 avctx->ticks_per_frame, 0, ctx->deadline); 00456 if (res != VPX_CODEC_OK) { 00457 log_encoder_error(avctx, "Error encoding frame"); 00458 return AVERROR_INVALIDDATA; 00459 } 00460 coded_size = queue_frames(avctx, buf, buf_size, avctx->coded_frame); 00461 00462 if (!frame && avctx->flags & CODEC_FLAG_PASS1) { 00463 unsigned int b64_size = ((ctx->twopass_stats.sz + 2) / 3) * 4 + 1; 00464 00465 avctx->stats_out = av_malloc(b64_size); 00466 if (!avctx->stats_out) { 00467 av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%d bytes) failed\n", 00468 b64_size); 00469 return AVERROR(ENOMEM); 00470 } 00471 av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf, 00472 ctx->twopass_stats.sz); 00473 } 00474 return coded_size; 00475 } 00476 00477 AVCodec libvpx_encoder = { 00478 "libvpx", 00479 AVMEDIA_TYPE_VIDEO, 00480 CODEC_ID_VP8, 00481 sizeof(VP8Context), 00482 vp8_init, 00483 vp8_encode, 00484 vp8_free, 00485 NULL, 00486 CODEC_CAP_DELAY, 00487 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE}, 00488 .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"), 00489 };