Libav
libvpxenc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Google, Inc.
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
26 #define VPX_DISABLE_CTRL_TYPECHECKS 1
27 #define VPX_CODEC_DISABLE_COMPAT 1
28 #include <vpx/vpx_encoder.h>
29 #include <vpx/vp8cx.h>
30 
31 #include "avcodec.h"
32 #include "internal.h"
33 #include "libvpx.h"
34 #include "libavutil/base64.h"
35 #include "libavutil/common.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/opt.h"
38 
43 struct FrameListData {
44  void *buf;
45  size_t sz;
46  int64_t pts;
48  unsigned long duration;
50  uint32_t flags;
52 };
53 
54 typedef struct VP8EncoderContext {
55  AVClass *class;
56  struct vpx_codec_ctx encoder;
57  struct vpx_image rawimg;
58  struct vpx_fixed_buf twopass_stats;
59  unsigned long deadline; //i.e., RT/GOOD/BEST
61  int cpu_used;
65  int arnr_type;
68  int crf;
69 } VP8Context;
70 
72 static const char *const ctlidstr[] = {
73  [VP8E_SET_ARNR_MAXFRAMES] = "VP8E_SET_ARNR_MAXFRAMES",
74  [VP8E_SET_ARNR_STRENGTH] = "VP8E_SET_ARNR_STRENGTH",
75  [VP8E_SET_ARNR_TYPE] = "VP8E_SET_ARNR_TYPE",
76  [VP8E_SET_CPUUSED] = "VP8E_SET_CPUUSED",
77  [VP8E_SET_CQ_LEVEL] = "VP8E_SET_CQ_LEVEL",
78  [VP8E_SET_ENABLEAUTOALTREF] = "VP8E_SET_ENABLEAUTOALTREF",
79  [VP8E_SET_NOISE_SENSITIVITY] = "VP8E_SET_NOISE_SENSITIVITY",
80  [VP8E_SET_STATIC_THRESHOLD] = "VP8E_SET_STATIC_THRESHOLD",
81  [VP8E_SET_TOKEN_PARTITIONS] = "VP8E_SET_TOKEN_PARTITIONS",
82 };
83 
84 static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
85 {
86  VP8Context *ctx = avctx->priv_data;
87  const char *error = vpx_codec_error(&ctx->encoder);
88  const char *detail = vpx_codec_error_detail(&ctx->encoder);
89 
90  av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
91  if (detail)
92  av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
93 }
94 
95 static av_cold void dump_enc_cfg(AVCodecContext *avctx,
96  const struct vpx_codec_enc_cfg *cfg)
97 {
98  int width = -30;
99  int level = AV_LOG_DEBUG;
100 
101  av_log(avctx, level, "vpx_codec_enc_cfg\n");
102  av_log(avctx, level, "generic settings\n"
103  " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
104  " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
105  width, "g_usage:", cfg->g_usage,
106  width, "g_threads:", cfg->g_threads,
107  width, "g_profile:", cfg->g_profile,
108  width, "g_w:", cfg->g_w,
109  width, "g_h:", cfg->g_h,
110  width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
111  width, "g_error_resilient:", cfg->g_error_resilient,
112  width, "g_pass:", cfg->g_pass,
113  width, "g_lag_in_frames:", cfg->g_lag_in_frames);
114  av_log(avctx, level, "rate control settings\n"
115  " %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
116  " %*s%d\n %*s%p(%zu)\n %*s%u\n",
117  width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
118  width, "rc_resize_allowed:", cfg->rc_resize_allowed,
119  width, "rc_resize_up_thresh:", cfg->rc_resize_up_thresh,
120  width, "rc_resize_down_thresh:", cfg->rc_resize_down_thresh,
121  width, "rc_end_usage:", cfg->rc_end_usage,
122  width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
123  width, "rc_target_bitrate:", cfg->rc_target_bitrate);
124  av_log(avctx, level, "quantizer settings\n"
125  " %*s%u\n %*s%u\n",
126  width, "rc_min_quantizer:", cfg->rc_min_quantizer,
127  width, "rc_max_quantizer:", cfg->rc_max_quantizer);
128  av_log(avctx, level, "bitrate tolerance\n"
129  " %*s%u\n %*s%u\n",
130  width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
131  width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
132  av_log(avctx, level, "decoder buffer model\n"
133  " %*s%u\n %*s%u\n %*s%u\n",
134  width, "rc_buf_sz:", cfg->rc_buf_sz,
135  width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
136  width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
137  av_log(avctx, level, "2 pass rate control settings\n"
138  " %*s%u\n %*s%u\n %*s%u\n",
139  width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
140  width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
141  width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
142  av_log(avctx, level, "keyframing settings\n"
143  " %*s%d\n %*s%u\n %*s%u\n",
144  width, "kf_mode:", cfg->kf_mode,
145  width, "kf_min_dist:", cfg->kf_min_dist,
146  width, "kf_max_dist:", cfg->kf_max_dist);
147  av_log(avctx, level, "\n");
148 }
149 
150 static void coded_frame_add(void *list, struct FrameListData *cx_frame)
151 {
152  struct FrameListData **p = list;
153 
154  while (*p)
155  p = &(*p)->next;
156  *p = cx_frame;
157  cx_frame->next = NULL;
158 }
159 
160 static av_cold void free_coded_frame(struct FrameListData *cx_frame)
161 {
162  av_freep(&cx_frame->buf);
163  av_freep(&cx_frame);
164 }
165 
166 static av_cold void free_frame_list(struct FrameListData *list)
167 {
168  struct FrameListData *p = list;
169 
170  while (p) {
171  list = list->next;
172  free_coded_frame(p);
173  p = list;
174  }
175 }
176 
178  enum vp8e_enc_control_id id, int val)
179 {
180  VP8Context *ctx = avctx->priv_data;
181  char buf[80];
182  int width = -30;
183  int res;
184 
185  snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
186  av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
187 
188  res = vpx_codec_control(&ctx->encoder, id, val);
189  if (res != VPX_CODEC_OK) {
190  snprintf(buf, sizeof(buf), "Failed to set %s codec control",
191  ctlidstr[id]);
192  log_encoder_error(avctx, buf);
193  }
194 
195  return res == VPX_CODEC_OK ? 0 : AVERROR(EINVAL);
196 }
197 
198 static av_cold int vp8_free(AVCodecContext *avctx)
199 {
200  VP8Context *ctx = avctx->priv_data;
201 
202  vpx_codec_destroy(&ctx->encoder);
203  av_freep(&ctx->twopass_stats.buf);
204  av_freep(&avctx->coded_frame);
205  av_freep(&avctx->stats_out);
207  return 0;
208 }
209 
210 static av_cold int vpx_init(AVCodecContext *avctx,
211  const struct vpx_codec_iface *iface)
212 {
213  VP8Context *ctx = avctx->priv_data;
214  struct vpx_codec_enc_cfg enccfg;
215  int res;
216 
217  av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
218  av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
219 
220  if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) {
221  av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
222  vpx_codec_err_to_string(res));
223  return AVERROR(EINVAL);
224  }
225  dump_enc_cfg(avctx, &enccfg);
226 
227  enccfg.g_w = avctx->width;
228  enccfg.g_h = avctx->height;
229  enccfg.g_timebase.num = avctx->time_base.num;
230  enccfg.g_timebase.den = avctx->time_base.den;
231  enccfg.g_threads = avctx->thread_count;
232 
233  if (ctx->lag_in_frames >= 0)
234  enccfg.g_lag_in_frames = ctx->lag_in_frames;
235 
236  if (avctx->flags & CODEC_FLAG_PASS1)
237  enccfg.g_pass = VPX_RC_FIRST_PASS;
238  else if (avctx->flags & CODEC_FLAG_PASS2)
239  enccfg.g_pass = VPX_RC_LAST_PASS;
240  else
241  enccfg.g_pass = VPX_RC_ONE_PASS;
242 
243  if (!avctx->bit_rate)
244  avctx->bit_rate = enccfg.rc_target_bitrate * 1000;
245  else
246  enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
248 
249  if (ctx->crf)
250  enccfg.rc_end_usage = VPX_CQ;
251  else if (avctx->rc_min_rate == avctx->rc_max_rate &&
252  avctx->rc_min_rate == avctx->bit_rate)
253  enccfg.rc_end_usage = VPX_CBR;
254 
255  if (avctx->qmin > 0)
256  enccfg.rc_min_quantizer = avctx->qmin;
257  if (avctx->qmax > 0)
258  enccfg.rc_max_quantizer = avctx->qmax;
259  enccfg.rc_dropframe_thresh = avctx->frame_skip_threshold;
260 
261  //0-100 (0 => CBR, 100 => VBR)
262  enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
263  enccfg.rc_2pass_vbr_minsection_pct =
264  avctx->rc_min_rate * 100LL / avctx->bit_rate;
265  if (avctx->rc_max_rate)
266  enccfg.rc_2pass_vbr_maxsection_pct =
267  avctx->rc_max_rate * 100LL / avctx->bit_rate;
268 
269  if (avctx->rc_buffer_size)
270  enccfg.rc_buf_sz =
271  avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
272  if (avctx->rc_initial_buffer_occupancy)
273  enccfg.rc_buf_initial_sz =
274  avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
275  enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
276 
277  //_enc_init() will balk if kf_min_dist differs from max w/VPX_KF_AUTO
278  if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
279  enccfg.kf_min_dist = avctx->keyint_min;
280  if (avctx->gop_size >= 0)
281  enccfg.kf_max_dist = avctx->gop_size;
282 
283  if (enccfg.g_pass == VPX_RC_FIRST_PASS)
284  enccfg.g_lag_in_frames = 0;
285  else if (enccfg.g_pass == VPX_RC_LAST_PASS) {
286  int decode_size, ret;
287 
288  if (!avctx->stats_in) {
289  av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
290  return AVERROR_INVALIDDATA;
291  }
292 
293  ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
294  ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz);
295  if (ret < 0) {
296  av_log(avctx, AV_LOG_ERROR,
297  "Stat buffer alloc (%zu bytes) failed\n",
298  ctx->twopass_stats.sz);
299  return ret;
300  }
301  decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
302  ctx->twopass_stats.sz);
303  if (decode_size < 0) {
304  av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
305  return AVERROR_INVALIDDATA;
306  }
307 
308  ctx->twopass_stats.sz = decode_size;
309  enccfg.rc_twopass_stats_in = ctx->twopass_stats;
310  }
311 
312  /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
313  complexity playback on low powered devices at the expense of encode
314  quality. */
315  if (avctx->profile != FF_PROFILE_UNKNOWN)
316  enccfg.g_profile = avctx->profile;
317 
318  enccfg.g_error_resilient = ctx->error_resilient;
319 
320  dump_enc_cfg(avctx, &enccfg);
321  /* Construct Encoder Context */
322  res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, 0);
323  if (res != VPX_CODEC_OK) {
324  log_encoder_error(avctx, "Failed to initialize encoder");
325  return AVERROR(EINVAL);
326  }
327 
328  //codec control failures are currently treated only as warnings
329  av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n");
330  if (ctx->cpu_used != INT_MIN)
331  codecctl_int(avctx, VP8E_SET_CPUUSED, ctx->cpu_used);
332  if (ctx->auto_alt_ref >= 0)
333  codecctl_int(avctx, VP8E_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref);
334  if (ctx->arnr_max_frames >= 0)
335  codecctl_int(avctx, VP8E_SET_ARNR_MAXFRAMES, ctx->arnr_max_frames);
336  if (ctx->arnr_strength >= 0)
337  codecctl_int(avctx, VP8E_SET_ARNR_STRENGTH, ctx->arnr_strength);
338  if (ctx->arnr_type >= 0)
339  codecctl_int(avctx, VP8E_SET_ARNR_TYPE, ctx->arnr_type);
340  codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, avctx->noise_reduction);
341  codecctl_int(avctx, VP8E_SET_TOKEN_PARTITIONS, av_log2(avctx->slices));
342  codecctl_int(avctx, VP8E_SET_STATIC_THRESHOLD, avctx->mb_threshold);
343  codecctl_int(avctx, VP8E_SET_CQ_LEVEL, ctx->crf);
344 
345  //provide dummy value to initialize wrapper, values will be updated each _encode()
346  vpx_img_wrap(&ctx->rawimg, VPX_IMG_FMT_I420, avctx->width, avctx->height, 1,
347  (unsigned char*)1);
348 
349  avctx->coded_frame = av_frame_alloc();
350  if (!avctx->coded_frame) {
351  av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
352  vp8_free(avctx);
353  return AVERROR(ENOMEM);
354  }
355  return 0;
356 }
357 
358 static inline void cx_pktcpy(struct FrameListData *dst,
359  const struct vpx_codec_cx_pkt *src)
360 {
361  dst->pts = src->data.frame.pts;
362  dst->duration = src->data.frame.duration;
363  dst->flags = src->data.frame.flags;
364  dst->sz = src->data.frame.sz;
365  dst->buf = src->data.frame.buf;
366 }
367 
375 static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
376  AVPacket *pkt, AVFrame *coded_frame)
377 {
378  int ret = ff_alloc_packet(pkt, cx_frame->sz);
379  if (ret >= 0) {
380  memcpy(pkt->data, cx_frame->buf, pkt->size);
381  pkt->pts = pkt->dts = cx_frame->pts;
382  coded_frame->pts = cx_frame->pts;
383  coded_frame->key_frame = !!(cx_frame->flags & VPX_FRAME_IS_KEY);
384 
385  if (coded_frame->key_frame) {
386  coded_frame->pict_type = AV_PICTURE_TYPE_I;
387  pkt->flags |= AV_PKT_FLAG_KEY;
388  } else
389  coded_frame->pict_type = AV_PICTURE_TYPE_P;
390  } else {
391  av_log(avctx, AV_LOG_ERROR,
392  "Error getting output packet of size %zu.\n", cx_frame->sz);
393  return ret;
394  }
395  return pkt->size;
396 }
397 
406 static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out,
407  AVFrame *coded_frame)
408 {
409  VP8Context *ctx = avctx->priv_data;
410  const struct vpx_codec_cx_pkt *pkt;
411  const void *iter = NULL;
412  int size = 0;
413 
414  if (ctx->coded_frame_list) {
415  struct FrameListData *cx_frame = ctx->coded_frame_list;
416  /* return the leading frame if we've already begun queueing */
417  size = storeframe(avctx, cx_frame, pkt_out, coded_frame);
418  if (size < 0)
419  return size;
420  ctx->coded_frame_list = cx_frame->next;
421  free_coded_frame(cx_frame);
422  }
423 
424  /* consume all available output from the encoder before returning. buffers
425  are only good through the next vpx_codec call */
426  while ((pkt = vpx_codec_get_cx_data(&ctx->encoder, &iter))) {
427  switch (pkt->kind) {
428  case VPX_CODEC_CX_FRAME_PKT:
429  if (!size) {
430  struct FrameListData cx_frame;
431 
432  /* avoid storing the frame when the list is empty and we haven't yet
433  provided a frame for output */
434  assert(!ctx->coded_frame_list);
435  cx_pktcpy(&cx_frame, pkt);
436  size = storeframe(avctx, &cx_frame, pkt_out, coded_frame);
437  if (size < 0)
438  return size;
439  } else {
440  struct FrameListData *cx_frame =
441  av_malloc(sizeof(struct FrameListData));
442 
443  if (!cx_frame) {
444  av_log(avctx, AV_LOG_ERROR,
445  "Frame queue element alloc failed\n");
446  return AVERROR(ENOMEM);
447  }
448  cx_pktcpy(cx_frame, pkt);
449  cx_frame->buf = av_malloc(cx_frame->sz);
450 
451  if (!cx_frame->buf) {
452  av_log(avctx, AV_LOG_ERROR,
453  "Data buffer alloc (%zu bytes) failed\n",
454  cx_frame->sz);
455  return AVERROR(ENOMEM);
456  }
457  memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
458  coded_frame_add(&ctx->coded_frame_list, cx_frame);
459  }
460  break;
461  case VPX_CODEC_STATS_PKT: {
462  struct vpx_fixed_buf *stats = &ctx->twopass_stats;
463  int err;
464  if ((err = av_reallocp(&stats->buf,
465  stats->sz +
466  pkt->data.twopass_stats.sz)) < 0) {
467  stats->sz = 0;
468  av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
469  return err;
470  }
471  memcpy((uint8_t*)stats->buf + stats->sz,
472  pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
473  stats->sz += pkt->data.twopass_stats.sz;
474  break;
475  }
476  case VPX_CODEC_PSNR_PKT: //FIXME add support for CODEC_FLAG_PSNR
477  case VPX_CODEC_CUSTOM_PKT:
478  //ignore unsupported/unrecognized packet types
479  break;
480  }
481  }
482 
483  return size;
484 }
485 
486 static int vp8_encode(AVCodecContext *avctx, AVPacket *pkt,
487  const AVFrame *frame, int *got_packet)
488 {
489  VP8Context *ctx = avctx->priv_data;
490  struct vpx_image *rawimg = NULL;
491  int64_t timestamp = 0;
492  int res, coded_size;
493  vpx_enc_frame_flags_t flags = 0;
494 
495  if (frame) {
496  rawimg = &ctx->rawimg;
497  rawimg->planes[VPX_PLANE_Y] = frame->data[0];
498  rawimg->planes[VPX_PLANE_U] = frame->data[1];
499  rawimg->planes[VPX_PLANE_V] = frame->data[2];
500  rawimg->stride[VPX_PLANE_Y] = frame->linesize[0];
501  rawimg->stride[VPX_PLANE_U] = frame->linesize[1];
502  rawimg->stride[VPX_PLANE_V] = frame->linesize[2];
503  timestamp = frame->pts;
504  if (frame->pict_type == AV_PICTURE_TYPE_I)
505  flags |= VPX_EFLAG_FORCE_KF;
506  }
507 
508  res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp,
509  avctx->ticks_per_frame, flags, ctx->deadline);
510  if (res != VPX_CODEC_OK) {
511  log_encoder_error(avctx, "Error encoding frame");
512  return AVERROR_INVALIDDATA;
513  }
514  coded_size = queue_frames(avctx, pkt, avctx->coded_frame);
515 
516  if (!frame && avctx->flags & CODEC_FLAG_PASS1) {
517  unsigned int b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
518 
519  avctx->stats_out = av_malloc(b64_size);
520  if (!avctx->stats_out) {
521  av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%d bytes) failed\n",
522  b64_size);
523  return AVERROR(ENOMEM);
524  }
525  av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
526  ctx->twopass_stats.sz);
527  }
528 
529  *got_packet = !!coded_size;
530  return 0;
531 }
532 
533 #define OFFSET(x) offsetof(VP8Context, x)
534 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
535 static const AVOption options[] = {
536  { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, INT_MIN, INT_MAX, VE},
537  { "auto-alt-ref", "Enable use of alternate reference "
538  "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
539  { "lag-in-frames", "Number of frames to look ahead for "
540  "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
541  { "arnr-maxframes", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
542  { "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
543  { "arnr-type", "altref noise reduction filter type", OFFSET(arnr_type), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "arnr_type"},
544  { "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "arnr_type" },
545  { "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "arnr_type" },
546  { "centered", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "arnr_type" },
547  { "deadline", "Time to spend encoding, in microseconds.", OFFSET(deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"},
548  { "best", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_BEST_QUALITY}, 0, 0, VE, "quality"},
549  { "good", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_GOOD_QUALITY}, 0, 0, VE, "quality"},
550  { "realtime", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_REALTIME}, 0, 0, VE, "quality"},
551  { "error-resilient", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"},
552 #ifdef VPX_ERROR_RESILIENT_DEFAULT
553  { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"},
554  { "partitions", "The frame partitions are independently decodable "
555  "by the bool decoder, meaning that partitions can be decoded even "
556  "though earlier partitions have been lost. Note that intra predicition"
557  " is still done over the partition boundary.", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_PARTITIONS}, 0, 0, VE, "er"},
558 #endif
559  { "crf", "Select the quality for constant quality mode", offsetof(VP8Context, crf), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 63, VE },
560  { NULL }
561 };
562 
563 static const AVCodecDefault defaults[] = {
564  { "qmin", "-1" },
565  { "qmax", "-1" },
566  { "g", "-1" },
567  { "keyint_min", "-1" },
568  { NULL },
569 };
570 
571 #if CONFIG_LIBVPX_VP8_ENCODER
572 static av_cold int vp8_init(AVCodecContext *avctx)
573 {
574  return vpx_init(avctx, &vpx_codec_vp8_cx_algo);
575 }
576 
577 static const AVClass class_vp8 = {
578  .class_name = "libvpx encoder",
579  .item_name = av_default_item_name,
580  .option = options,
581  .version = LIBAVUTIL_VERSION_INT,
582 };
583 
584 AVCodec ff_libvpx_vp8_encoder = {
585  .name = "libvpx",
586  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
587  .type = AVMEDIA_TYPE_VIDEO,
588  .id = AV_CODEC_ID_VP8,
589  .priv_data_size = sizeof(VP8Context),
590  .init = vp8_init,
591  .encode2 = vp8_encode,
592  .close = vp8_free,
593  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
594  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
595  .priv_class = &class_vp8,
596  .defaults = defaults,
597 };
598 #endif /* CONFIG_LIBVPX_VP8_ENCODER */
599 
600 #if CONFIG_LIBVPX_VP9_ENCODER
601 static av_cold int vp9_init(AVCodecContext *avctx)
602 {
603  int ret;
604  if ((ret = ff_vp9_check_experimental(avctx)))
605  return ret;
606  return vpx_init(avctx, &vpx_codec_vp9_cx_algo);
607 }
608 
609 static const AVClass class_vp9 = {
610  .class_name = "libvpx encoder",
611  .item_name = av_default_item_name,
612  .option = options,
613  .version = LIBAVUTIL_VERSION_INT,
614 };
615 
616 AVCodec ff_libvpx_vp9_encoder = {
617  .name = "libvpx-vp9",
618  .long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"),
619  .type = AVMEDIA_TYPE_VIDEO,
620  .id = AV_CODEC_ID_VP9,
621  .priv_data_size = sizeof(VP8Context),
622  .init = vp9_init,
623  .encode2 = vp8_encode,
624  .close = vp8_free,
625  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_AUTO_THREADS,
626  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
627  .priv_class = &class_vp9,
628  .defaults = defaults,
629 };
630 #endif /* CONFIG_LIBVPX_VP9_ENCODER */
int cpu_used
Definition: libvpxenc.c:61
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int lag_in_frames
Definition: libvpxenc.c:66
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVOption.
Definition: opt.h:234
struct vpx_codec_ctx encoder
Definition: libvpxenc.c:56
#define CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:636
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:61
#define OFFSET(x)
Definition: libvpxenc.c:533
#define CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:635
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2548
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition: avcodec.h:2180
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:974
size_t sz
length of compressed data
Definition: libvpxenc.c:45
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:2316
int arnr_max_frames
Definition: libvpxenc.c:63
int profile
profile
Definition: avcodec.h:2638
AVCodec.
Definition: avcodec.h:2812
int error_resilient
Definition: libvpxenc.c:67
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1175
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:38
int auto_alt_ref
Definition: libvpxenc.c:62
static void cx_pktcpy(struct FrameListData *dst, const struct vpx_codec_cx_pkt *src)
Definition: libvpxenc.c:358
uint8_t
#define av_cold
Definition: attributes.h:66
static av_cold int codecctl_int(AVCodecContext *avctx, enum vp8e_enc_control_id id, int val)
Definition: libvpxenc.c:177
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
int64_t pts
time stamp to show frame (in timebase units)
Definition: libvpxenc.c:46
AVOptions.
static void coded_frame_add(void *list, struct FrameListData *cx_frame)
Definition: libvpxenc.c:150
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
uint8_t * data
Definition: avcodec.h:973
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:140
static int flags
Definition: log.c:44
int mb_threshold
Macroblock threshold below which the user specified macroblock types will be used.
Definition: avcodec.h:1645
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:2308
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
struct vpx_image rawimg
Definition: libvpxenc.c:57
int ff_vp9_check_experimental(AVCodecContext *avctx)
Definition: libvpx.c:25
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:713
#define AVERROR(e)
Definition: error.h:43
int frame_skip_threshold
frame skip threshold
Definition: avcodec.h:2222
int qmax
maximum quantizer
Definition: avcodec.h:2096
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
Round to nearest and halfway cases away from zero.
Definition: mathematics.h:54
static const AVOption options[]
Definition: libvpxenc.c:535
int rc_max_rate
maximum bitrate
Definition: avcodec.h:2143
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
static av_always_inline av_const double round(double x)
Definition: libm.h:151
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
Definition: base64.c:72
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:2121
static av_cold void dump_enc_cfg(AVCodecContext *avctx, const struct vpx_codec_enc_cfg *cfg)
Definition: libvpxenc.c:95
int bit_rate
the average bitrate
Definition: avcodec.h:1114
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes.
Definition: base64.h:59
int width
picture width / height.
Definition: avcodec.h:1229
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:2639
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:767
static av_cold int vp8_free(AVCodecContext *avctx)
Definition: libvpxenc.c:198
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1266
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1184
static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
Definition: libvpxenc.c:84
int arnr_strength
Definition: libvpxenc.c:64
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2556
struct FrameListData * next
Definition: libvpxenc.c:51
static av_cold int vpx_init(AVCodecContext *avctx, const struct vpx_codec_iface *iface)
Definition: libvpxenc.c:210
NULL
Definition: eval.c:55
static const AVCodecDefault defaults[]
Definition: libvpxenc.c:563
static int width
Definition: utils.c:156
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
#define VE
Definition: libvpxenc.c:534
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
av_default_item_name
Definition: dnxhdenc.c:52
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int qmin
minimum quantizer
Definition: avcodec.h:2089
static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame, AVPacket *pkt, AVFrame *coded_frame)
Store coded frame information in format suitable for return from encode2().
Definition: libvpxenc.c:375
Describe the class of an AVClass context structure.
Definition: log.h:33
int arnr_type
Definition: libvpxenc.c:65
uint32_t flags
flags for this frame
Definition: libvpxenc.c:50
static av_cold void free_coded_frame(struct FrameListData *cx_frame)
Definition: libvpxenc.c:160
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition: avcodec.h:2081
static int vp8_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: libvpxenc.c:486
void * buf
compressed data buffer
Definition: libvpxenc.c:44
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
uint8_t level
Definition: svq3.c:147
int noise_reduction
noise reduction strength
Definition: avcodec.h:1629
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1255
static const char *const ctlidstr[]
String mappings for enum vp8e_enc_control_id.
Definition: libvpxenc.c:72
struct vpx_fixed_buf twopass_stats
Definition: libvpxenc.c:58
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
common internal api header.
common internal and external API header
unsigned long deadline
Definition: libvpxenc.c:59
int den
denominator
Definition: rational.h:45
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
static av_cold void free_frame_list(struct FrameListData *list)
Definition: libvpxenc.c:166
int slices
Number of slices.
Definition: avcodec.h:1798
void * priv_data
Definition: avcodec.h:1092
Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
Definition: libvpxenc.c:43
#define av_log2
Definition: intmath.h:85
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
unsigned long duration
duration to show frame (in timebase units)
Definition: libvpxenc.c:48
int av_base64_decode(uint8_t *out, const char *in, int out_size)
Decode a base64-encoded string.
Definition: base64.c:45
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out, AVFrame *coded_frame)
Queue multiple output frames from the encoder, returning the front-most.
Definition: libvpxenc.c:406
int rc_min_rate
minimum bitrate
Definition: avcodec.h:2150
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:950
struct FrameListData * coded_frame_list
Definition: libvpxenc.c:60
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
Predicted.
Definition: avutil.h:254
int keyint_min
minimum GOP size
Definition: avcodec.h:1716