libvpxenc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. /**
  21. * @file
  22. * VP8 encoder support via libvpx
  23. */
  24. #define VPX_DISABLE_CTRL_TYPECHECKS 1
  25. #define VPX_CODEC_DISABLE_COMPAT 1
  26. #include <vpx/vpx_encoder.h>
  27. #include <vpx/vp8cx.h>
  28. #include "avcodec.h"
  29. #include "libavutil/base64.h"
  30. /**
  31. * Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
  32. * One encoded frame returned from the library.
  33. */
  34. struct FrameListData {
  35. void *buf; /**< compressed data buffer */
  36. size_t sz; /**< length of compressed data */
  37. int64_t pts; /**< time stamp to show frame
  38. (in timebase units) */
  39. unsigned long duration; /**< duration to show frame
  40. (in timebase units) */
  41. uint32_t flags; /**< flags for this frame */
  42. struct FrameListData *next;
  43. };
  44. typedef struct VP8EncoderContext {
  45. struct vpx_codec_ctx encoder;
  46. struct vpx_image rawimg;
  47. struct vpx_fixed_buf twopass_stats;
  48. unsigned long deadline; //i.e., RT/GOOD/BEST
  49. struct FrameListData *coded_frame_list;
  50. } VP8Context;
  51. /** String mappings for enum vp8e_enc_control_id */
  52. static const char *ctlidstr[] = {
  53. [VP8E_UPD_ENTROPY] = "VP8E_UPD_ENTROPY",
  54. [VP8E_UPD_REFERENCE] = "VP8E_UPD_REFERENCE",
  55. [VP8E_USE_REFERENCE] = "VP8E_USE_REFERENCE",
  56. [VP8E_SET_ROI_MAP] = "VP8E_SET_ROI_MAP",
  57. [VP8E_SET_ACTIVEMAP] = "VP8E_SET_ACTIVEMAP",
  58. [VP8E_SET_SCALEMODE] = "VP8E_SET_SCALEMODE",
  59. [VP8E_SET_CPUUSED] = "VP8E_SET_CPUUSED",
  60. [VP8E_SET_ENABLEAUTOALTREF] = "VP8E_SET_ENABLEAUTOALTREF",
  61. [VP8E_SET_NOISE_SENSITIVITY] = "VP8E_SET_NOISE_SENSITIVITY",
  62. [VP8E_SET_SHARPNESS] = "VP8E_SET_SHARPNESS",
  63. [VP8E_SET_STATIC_THRESHOLD] = "VP8E_SET_STATIC_THRESHOLD",
  64. [VP8E_SET_TOKEN_PARTITIONS] = "VP8E_SET_TOKEN_PARTITIONS",
  65. [VP8E_GET_LAST_QUANTIZER] = "VP8E_GET_LAST_QUANTIZER",
  66. [VP8E_SET_ARNR_MAXFRAMES] = "VP8E_SET_ARNR_MAXFRAMES",
  67. [VP8E_SET_ARNR_STRENGTH] = "VP8E_SET_ARNR_STRENGTH",
  68. [VP8E_SET_ARNR_TYPE] = "VP8E_SET_ARNR_TYPE",
  69. };
  70. static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
  71. {
  72. VP8Context *ctx = avctx->priv_data;
  73. const char *error = vpx_codec_error(&ctx->encoder);
  74. const char *detail = vpx_codec_error_detail(&ctx->encoder);
  75. av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
  76. if (detail)
  77. av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
  78. }
  79. static av_cold void dump_enc_cfg(AVCodecContext *avctx,
  80. const struct vpx_codec_enc_cfg *cfg)
  81. {
  82. int width = -30;
  83. int level = AV_LOG_DEBUG;
  84. av_log(avctx, level, "vpx_codec_enc_cfg\n");
  85. av_log(avctx, level, "generic settings\n"
  86. " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
  87. " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
  88. width, "g_usage:", cfg->g_usage,
  89. width, "g_threads:", cfg->g_threads,
  90. width, "g_profile:", cfg->g_profile,
  91. width, "g_w:", cfg->g_w,
  92. width, "g_h:", cfg->g_h,
  93. width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
  94. width, "g_error_resilient:", cfg->g_error_resilient,
  95. width, "g_pass:", cfg->g_pass,
  96. width, "g_lag_in_frames:", cfg->g_lag_in_frames);
  97. av_log(avctx, level, "rate control settings\n"
  98. " %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
  99. " %*s%d\n %*s%p(%zu)\n %*s%u\n",
  100. width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
  101. width, "rc_resize_allowed:", cfg->rc_resize_allowed,
  102. width, "rc_resize_up_thresh:", cfg->rc_resize_up_thresh,
  103. width, "rc_resize_down_thresh:", cfg->rc_resize_down_thresh,
  104. width, "rc_end_usage:", cfg->rc_end_usage,
  105. width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
  106. width, "rc_target_bitrate:", cfg->rc_target_bitrate);
  107. av_log(avctx, level, "quantizer settings\n"
  108. " %*s%u\n %*s%u\n",
  109. width, "rc_min_quantizer:", cfg->rc_min_quantizer,
  110. width, "rc_max_quantizer:", cfg->rc_max_quantizer);
  111. av_log(avctx, level, "bitrate tolerance\n"
  112. " %*s%u\n %*s%u\n",
  113. width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
  114. width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
  115. av_log(avctx, level, "decoder buffer model\n"
  116. " %*s%u\n %*s%u\n %*s%u\n",
  117. width, "rc_buf_sz:", cfg->rc_buf_sz,
  118. width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
  119. width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
  120. av_log(avctx, level, "2 pass rate control settings\n"
  121. " %*s%u\n %*s%u\n %*s%u\n",
  122. width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
  123. width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
  124. width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
  125. av_log(avctx, level, "keyframing settings\n"
  126. " %*s%d\n %*s%u\n %*s%u\n",
  127. width, "kf_mode:", cfg->kf_mode,
  128. width, "kf_min_dist:", cfg->kf_min_dist,
  129. width, "kf_max_dist:", cfg->kf_max_dist);
  130. av_log(avctx, level, "\n");
  131. }
  132. static void coded_frame_add(void *list, struct FrameListData *cx_frame)
  133. {
  134. struct FrameListData **p = list;
  135. while (*p != NULL)
  136. p = &(*p)->next;
  137. *p = cx_frame;
  138. cx_frame->next = NULL;
  139. }
  140. static av_cold void free_coded_frame(struct FrameListData *cx_frame)
  141. {
  142. av_freep(&cx_frame->buf);
  143. av_freep(&cx_frame);
  144. }
  145. static av_cold void free_frame_list(struct FrameListData *list)
  146. {
  147. struct FrameListData *p = list;
  148. while (p) {
  149. list = list->next;
  150. free_coded_frame(p);
  151. p = list;
  152. }
  153. }
  154. static av_cold int codecctl_int(AVCodecContext *avctx,
  155. enum vp8e_enc_control_id id, int val)
  156. {
  157. VP8Context *ctx = avctx->priv_data;
  158. char buf[80];
  159. int width = -30;
  160. int res;
  161. snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
  162. av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
  163. res = vpx_codec_control(&ctx->encoder, id, val);
  164. if (res != VPX_CODEC_OK) {
  165. snprintf(buf, sizeof(buf), "Failed to set %s codec control",
  166. ctlidstr[id]);
  167. log_encoder_error(avctx, buf);
  168. }
  169. return res == VPX_CODEC_OK ? 0 : AVERROR(EINVAL);
  170. }
  171. static av_cold int vp8_free(AVCodecContext *avctx)
  172. {
  173. VP8Context *ctx = avctx->priv_data;
  174. vpx_codec_destroy(&ctx->encoder);
  175. av_freep(&ctx->twopass_stats.buf);
  176. av_freep(&avctx->coded_frame);
  177. av_freep(&avctx->stats_out);
  178. free_frame_list(ctx->coded_frame_list);
  179. return 0;
  180. }
  181. static av_cold int vp8_init(AVCodecContext *avctx)
  182. {
  183. VP8Context *ctx = avctx->priv_data;
  184. const struct vpx_codec_iface *iface = &vpx_codec_vp8_cx_algo;
  185. int cpuused = 3;
  186. struct vpx_codec_enc_cfg enccfg;
  187. int res;
  188. av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
  189. av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
  190. if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) {
  191. av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
  192. vpx_codec_err_to_string(res));
  193. return AVERROR(EINVAL);
  194. }
  195. dump_enc_cfg(avctx, &enccfg);
  196. enccfg.g_w = avctx->width;
  197. enccfg.g_h = avctx->height;
  198. enccfg.g_timebase.num = avctx->time_base.num;
  199. enccfg.g_timebase.den = avctx->time_base.den;
  200. enccfg.g_threads = avctx->thread_count;
  201. if (avctx->flags & CODEC_FLAG_PASS1)
  202. enccfg.g_pass = VPX_RC_FIRST_PASS;
  203. else if (avctx->flags & CODEC_FLAG_PASS2)
  204. enccfg.g_pass = VPX_RC_LAST_PASS;
  205. else
  206. enccfg.g_pass = VPX_RC_ONE_PASS;
  207. if (avctx->rc_min_rate == avctx->rc_max_rate &&
  208. avctx->rc_min_rate == avctx->bit_rate)
  209. enccfg.rc_end_usage = VPX_CBR;
  210. enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
  211. AV_ROUND_NEAR_INF);
  212. enccfg.rc_min_quantizer = avctx->qmin;
  213. enccfg.rc_max_quantizer = avctx->qmax;
  214. enccfg.rc_dropframe_thresh = avctx->frame_skip_threshold;
  215. //0-100 (0 => CBR, 100 => VBR)
  216. enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
  217. enccfg.rc_2pass_vbr_minsection_pct =
  218. avctx->rc_min_rate * 100LL / avctx->bit_rate;
  219. if (avctx->rc_max_rate)
  220. enccfg.rc_2pass_vbr_maxsection_pct =
  221. avctx->rc_max_rate * 100LL / avctx->bit_rate;
  222. if (avctx->rc_buffer_size)
  223. enccfg.rc_buf_sz =
  224. avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
  225. if (avctx->rc_initial_buffer_occupancy)
  226. enccfg.rc_buf_initial_sz =
  227. avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
  228. enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
  229. //_enc_init() will balk if kf_min_dist differs from max w/VPX_KF_AUTO
  230. if (avctx->keyint_min == avctx->gop_size)
  231. enccfg.kf_min_dist = avctx->keyint_min;
  232. enccfg.kf_max_dist = avctx->gop_size;
  233. if (enccfg.g_pass == VPX_RC_FIRST_PASS)
  234. enccfg.g_lag_in_frames = 0;
  235. else if (enccfg.g_pass == VPX_RC_LAST_PASS) {
  236. int decode_size;
  237. if (!avctx->stats_in) {
  238. av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
  239. return AVERROR_INVALIDDATA;
  240. }
  241. ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
  242. ctx->twopass_stats.buf = av_malloc(ctx->twopass_stats.sz);
  243. if (!ctx->twopass_stats.buf) {
  244. av_log(avctx, AV_LOG_ERROR,
  245. "Stat buffer alloc (%zu bytes) failed\n",
  246. ctx->twopass_stats.sz);
  247. return AVERROR(ENOMEM);
  248. }
  249. decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
  250. ctx->twopass_stats.sz);
  251. if (decode_size < 0) {
  252. av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
  253. return AVERROR_INVALIDDATA;
  254. }
  255. ctx->twopass_stats.sz = decode_size;
  256. enccfg.rc_twopass_stats_in = ctx->twopass_stats;
  257. }
  258. ctx->deadline = VPX_DL_GOOD_QUALITY;
  259. /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
  260. complexity playback on low powered devices at the expense of encode
  261. quality. */
  262. if (avctx->profile != FF_PROFILE_UNKNOWN)
  263. enccfg.g_profile = avctx->profile;
  264. dump_enc_cfg(avctx, &enccfg);
  265. /* Construct Encoder Context */
  266. res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, 0);
  267. if (res != VPX_CODEC_OK) {
  268. log_encoder_error(avctx, "Failed to initialize encoder");
  269. return AVERROR(EINVAL);
  270. }
  271. //codec control failures are currently treated only as warnings
  272. av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n");
  273. codecctl_int(avctx, VP8E_SET_CPUUSED, cpuused);
  274. codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, avctx->noise_reduction);
  275. codecctl_int(avctx, VP8E_SET_TOKEN_PARTITIONS, av_log2(avctx->slices));
  276. //provide dummy value to initialize wrapper, values will be updated each _encode()
  277. vpx_img_wrap(&ctx->rawimg, VPX_IMG_FMT_I420, avctx->width, avctx->height, 1,
  278. (unsigned char*)1);
  279. avctx->coded_frame = avcodec_alloc_frame();
  280. if (!avctx->coded_frame) {
  281. av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
  282. vp8_free(avctx);
  283. return AVERROR(ENOMEM);
  284. }
  285. return 0;
  286. }
  287. static inline void cx_pktcpy(struct FrameListData *dst,
  288. const struct vpx_codec_cx_pkt *src)
  289. {
  290. dst->pts = src->data.frame.pts;
  291. dst->duration = src->data.frame.duration;
  292. dst->flags = src->data.frame.flags;
  293. dst->sz = src->data.frame.sz;
  294. dst->buf = src->data.frame.buf;
  295. }
  296. /**
  297. * Store coded frame information in format suitable for return from encode().
  298. *
  299. * Write buffer information from @a cx_frame to @a buf & @a buf_size.
  300. * Timing/frame details to @a coded_frame.
  301. * @return Frame size written to @a buf on success
  302. * @return AVERROR(EINVAL) on error
  303. */
  304. static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
  305. uint8_t *buf, int buf_size, AVFrame *coded_frame)
  306. {
  307. if ((int) cx_frame->sz <= buf_size) {
  308. buf_size = cx_frame->sz;
  309. memcpy(buf, cx_frame->buf, buf_size);
  310. coded_frame->pts = cx_frame->pts;
  311. coded_frame->key_frame = !!(cx_frame->flags & VPX_FRAME_IS_KEY);
  312. if (coded_frame->key_frame)
  313. coded_frame->pict_type = FF_I_TYPE;
  314. else
  315. coded_frame->pict_type = FF_P_TYPE;
  316. } else {
  317. av_log(avctx, AV_LOG_ERROR,
  318. "Compressed frame larger than storage provided! (%zu/%d)\n",
  319. cx_frame->sz, buf_size);
  320. return AVERROR(EINVAL);
  321. }
  322. return buf_size;
  323. }
  324. /**
  325. * Queue multiple output frames from the encoder, returning the front-most.
  326. * In cases where vpx_codec_get_cx_data() returns more than 1 frame append
  327. * the frame queue. Return the head frame if available.
  328. * @return Stored frame size
  329. * @return AVERROR(EINVAL) on output size error
  330. * @return AVERROR(ENOMEM) on coded frame queue data allocation error
  331. */
  332. static int queue_frames(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  333. AVFrame *coded_frame)
  334. {
  335. VP8Context *ctx = avctx->priv_data;
  336. const struct vpx_codec_cx_pkt *pkt;
  337. const void *iter = NULL;
  338. int size = 0;
  339. if (ctx->coded_frame_list) {
  340. struct FrameListData *cx_frame = ctx->coded_frame_list;
  341. /* return the leading frame if we've already begun queueing */
  342. size = storeframe(avctx, cx_frame, buf, buf_size, coded_frame);
  343. if (size < 0)
  344. return AVERROR(EINVAL);
  345. ctx->coded_frame_list = cx_frame->next;
  346. free_coded_frame(cx_frame);
  347. }
  348. /* consume all available output from the encoder before returning. buffers
  349. are only good through the next vpx_codec call */
  350. while ((pkt = vpx_codec_get_cx_data(&ctx->encoder, &iter))) {
  351. switch (pkt->kind) {
  352. case VPX_CODEC_CX_FRAME_PKT:
  353. if (!size) {
  354. struct FrameListData cx_frame;
  355. /* avoid storing the frame when the list is empty and we haven't yet
  356. provided a frame for output */
  357. assert(!ctx->coded_frame_list);
  358. cx_pktcpy(&cx_frame, pkt);
  359. size = storeframe(avctx, &cx_frame, buf, buf_size, coded_frame);
  360. if (size < 0)
  361. return AVERROR(EINVAL);
  362. } else {
  363. struct FrameListData *cx_frame =
  364. av_malloc(sizeof(struct FrameListData));
  365. if (!cx_frame) {
  366. av_log(avctx, AV_LOG_ERROR,
  367. "Frame queue element alloc failed\n");
  368. return AVERROR(ENOMEM);
  369. }
  370. cx_pktcpy(cx_frame, pkt);
  371. cx_frame->buf = av_malloc(cx_frame->sz);
  372. if (!cx_frame->buf) {
  373. av_log(avctx, AV_LOG_ERROR,
  374. "Data buffer alloc (%zu bytes) failed\n",
  375. cx_frame->sz);
  376. return AVERROR(ENOMEM);
  377. }
  378. memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
  379. coded_frame_add(&ctx->coded_frame_list, cx_frame);
  380. }
  381. break;
  382. case VPX_CODEC_STATS_PKT: {
  383. struct vpx_fixed_buf *stats = &ctx->twopass_stats;
  384. stats->buf = av_realloc(stats->buf,
  385. stats->sz + pkt->data.twopass_stats.sz);
  386. if (!stats->buf) {
  387. av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
  388. return AVERROR(ENOMEM);
  389. }
  390. memcpy((uint8_t*)stats->buf + stats->sz,
  391. pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
  392. stats->sz += pkt->data.twopass_stats.sz;
  393. break;
  394. }
  395. case VPX_CODEC_PSNR_PKT: //FIXME add support for CODEC_FLAG_PSNR
  396. case VPX_CODEC_CUSTOM_PKT:
  397. //ignore unsupported/unrecognized packet types
  398. break;
  399. }
  400. }
  401. return size;
  402. }
  403. static int vp8_encode(AVCodecContext *avctx, uint8_t *buf, int buf_size,
  404. void *data)
  405. {
  406. VP8Context *ctx = avctx->priv_data;
  407. AVFrame *frame = data;
  408. struct vpx_image *rawimg = NULL;
  409. int64_t timestamp = 0;
  410. int res, coded_size;
  411. if (frame) {
  412. rawimg = &ctx->rawimg;
  413. rawimg->planes[VPX_PLANE_Y] = frame->data[0];
  414. rawimg->planes[VPX_PLANE_U] = frame->data[1];
  415. rawimg->planes[VPX_PLANE_V] = frame->data[2];
  416. rawimg->stride[VPX_PLANE_Y] = frame->linesize[0];
  417. rawimg->stride[VPX_PLANE_U] = frame->linesize[1];
  418. rawimg->stride[VPX_PLANE_V] = frame->linesize[2];
  419. timestamp = frame->pts;
  420. }
  421. res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp,
  422. avctx->ticks_per_frame, 0, ctx->deadline);
  423. if (res != VPX_CODEC_OK) {
  424. log_encoder_error(avctx, "Error encoding frame");
  425. return AVERROR_INVALIDDATA;
  426. }
  427. coded_size = queue_frames(avctx, buf, buf_size, avctx->coded_frame);
  428. if (!frame && avctx->flags & CODEC_FLAG_PASS1) {
  429. unsigned int b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
  430. avctx->stats_out = av_malloc(b64_size);
  431. if (!avctx->stats_out) {
  432. av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%d bytes) failed\n",
  433. b64_size);
  434. return AVERROR(ENOMEM);
  435. }
  436. av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
  437. ctx->twopass_stats.sz);
  438. }
  439. return coded_size;
  440. }
  441. AVCodec ff_libvpx_encoder = {
  442. "libvpx",
  443. AVMEDIA_TYPE_VIDEO,
  444. CODEC_ID_VP8,
  445. sizeof(VP8Context),
  446. vp8_init,
  447. vp8_encode,
  448. vp8_free,
  449. NULL,
  450. CODEC_CAP_DELAY,
  451. .pix_fmts = (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
  452. .long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"),
  453. };