• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/libvpxenc.c

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

Generated on Wed Apr 11 2012 07:31:33 for FFmpeg by  doxygen 1.7.1