2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
14 // This is an example of a two pass encoder loop. It takes an input file in
15 // YV12 format, passes it through the encoder twice, and writes the compressed
16 // frames to disk in IVF format. It builds upon the simple_encoder example.
20 // Twopass mode needs to track the current pass number and the buffer of
21 // statistics packets.
23 // Updating The Configuration
24 // ---------------------------------
25 // In two pass mode, the configuration has to be updated on each pass. The
26 // statistics buffer is passed on the last pass.
30 // Encoding a frame in two pass mode is identical to the simple encoder
31 // example, except the deadline is set to VPX_DL_BEST_QUALITY to get the
32 // best quality possible. VPX_DL_GOOD_QUALITY could also be used.
35 // Processing Statistics Packets
36 // -----------------------------
37 // Each packet of type `VPX_CODEC_CX_FRAME_PKT` contains the encoded data
38 // for this frame. We write a IVF frame header, followed by the raw data.
41 // Pass Progress Reporting
42 // -----------------------------
43 // It's sometimes helpful to see when each pass completes.
47 // -----------------------------
48 // Destruction of the encoder instance must be done on each pass. The
49 // raw image should be destroyed at the end as usual.
55 #define VPX_CODEC_DISABLE_COMPAT 1
56 #include "vpx/vpx_encoder.h"
58 #include "./tools_common.h"
59 #include "./video_writer.h"
61 static const char *exec_name;
64 fprintf(stderr, "Usage: %s <codec> <width> <height> <infile> <outfile>\n",
69 static void get_frame_stats(vpx_codec_ctx_t *ctx,
70 const vpx_image_t *img,
73 vpx_enc_frame_flags_t flags,
75 vpx_fixed_buf_t *stats) {
76 vpx_codec_iter_t iter = NULL;
77 const vpx_codec_cx_pkt_t *pkt = NULL;
78 const vpx_codec_err_t res = vpx_codec_encode(ctx, img, pts, duration, flags,
80 if (res != VPX_CODEC_OK)
81 die_codec(ctx, "Failed to get frame stats.");
83 while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL) {
84 if (pkt->kind == VPX_CODEC_STATS_PKT) {
85 const uint8_t *const pkt_buf = pkt->data.twopass_stats.buf;
86 const size_t pkt_size = pkt->data.twopass_stats.sz;
87 stats->buf = realloc(stats->buf, stats->sz + pkt_size);
88 memcpy((uint8_t *)stats->buf + stats->sz, pkt_buf, pkt_size);
89 stats->sz += pkt_size;
94 static void encode_frame(vpx_codec_ctx_t *ctx,
95 const vpx_image_t *img,
98 vpx_enc_frame_flags_t flags,
100 VpxVideoWriter *writer) {
101 vpx_codec_iter_t iter = NULL;
102 const vpx_codec_cx_pkt_t *pkt = NULL;
103 const vpx_codec_err_t res = vpx_codec_encode(ctx, img, pts, duration, flags,
105 if (res != VPX_CODEC_OK)
106 die_codec(ctx, "Failed to encode frame.");
108 while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL) {
109 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
110 const int keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
112 if (!vpx_video_writer_write_frame(writer, pkt->data.frame.buf,
114 pkt->data.frame.pts))
115 die_codec(ctx, "Failed to write compressed frame.");
116 printf(keyframe ? "K" : ".");
122 int main(int argc, char **argv) {
124 VpxVideoWriter *writer = NULL;
125 vpx_codec_ctx_t codec;
126 vpx_codec_enc_cfg_t cfg;
129 vpx_fixed_buf_t stats = {0};
130 VpxVideoInfo info = {0};
131 const VpxInterface *encoder = NULL;
133 const int fps = 30; // TODO(dkovalev) add command line argument
134 const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument
135 const char *const codec_arg = argv[1];
136 const char *const width_arg = argv[2];
137 const char *const height_arg = argv[3];
138 const char *const infile_arg = argv[4];
139 const char *const outfile_arg = argv[5];
143 die("Invalid number of arguments.");
145 encoder = get_vpx_encoder_by_name(codec_arg);
147 die("Unsupported codec.");
149 info.codec_fourcc = encoder->fourcc;
150 info.time_base.numerator = 1;
151 info.time_base.denominator = fps;
152 info.frame_width = strtol(width_arg, NULL, 0);
153 info.frame_height = strtol(height_arg, NULL, 0);
155 if (info.frame_width <= 0 ||
156 info.frame_height <= 0 ||
157 (info.frame_width % 2) != 0 ||
158 (info.frame_height % 2) != 0) {
159 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
162 if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, info.frame_width,
163 info.frame_height, 1)) {
164 die("Failed to allocate image", info.frame_width, info.frame_height);
167 writer = vpx_video_writer_open(outfile_arg, kContainerIVF, &info);
169 die("Failed to open %s for writing", outfile_arg);
171 printf("Using %s\n", vpx_codec_iface_name(encoder->interface()));
173 res = vpx_codec_enc_config_default(encoder->interface(), &cfg, 0);
175 die_codec(&codec, "Failed to get default codec config.");
177 cfg.g_w = info.frame_width;
178 cfg.g_h = info.frame_height;
179 cfg.g_timebase.num = info.time_base.numerator;
180 cfg.g_timebase.den = info.time_base.denominator;
181 cfg.rc_target_bitrate = bitrate;
183 for (pass = 0; pass < 2; ++pass) {
187 cfg.g_pass = VPX_RC_FIRST_PASS;
189 cfg.g_pass = VPX_RC_LAST_PASS;
190 cfg.rc_twopass_stats_in = stats;
193 if (!(infile = fopen(infile_arg, "rb")))
194 die("Failed to open %s for reading", infile_arg);
196 if (vpx_codec_enc_init(&codec, encoder->interface(), &cfg, 0))
197 die_codec(&codec, "Failed to initialize encoder");
199 while (vpx_img_read(&raw, infile)) {
203 get_frame_stats(&codec, &raw, frame_count, 1, 0, VPX_DL_BEST_QUALITY,
206 encode_frame(&codec, &raw, frame_count, 1, 0, VPX_DL_BEST_QUALITY,
212 get_frame_stats(&codec, NULL, frame_count, 1, 0, VPX_DL_BEST_QUALITY,
219 printf("Pass %d complete. Processed %d frames.\n", pass + 1, frame_count);
220 if (vpx_codec_destroy(&codec))
221 die_codec(&codec, "Failed to destroy codec.");
227 vpx_video_writer_close(writer);