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