AOMedia Codec SDK: twopass_encoder

AOMedia Codec SDK

twopass_encoder
1 /*
2 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 // Two Pass Encoder
13 // ================
14 //
15 // This is an example of a two pass encoder loop. It takes an input file in
16 // YV12 format, passes it through the encoder twice, and writes the compressed
17 // frames to disk in IVF format. It builds upon the simple_encoder example.
18 //
19 // Twopass Variables
20 // -----------------
21 // Twopass mode needs to track the current pass number and the buffer of
22 // statistics packets.
23 //
24 // Updating The Configuration
25 // ---------------------------------
26 // In two pass mode, the configuration has to be updated on each pass. The
27 // statistics buffer is passed on the last pass.
28 //
29 // Encoding A Frame
30 // ----------------
31 // Encoding a frame in two pass mode is identical to the simple encoder
32 // example.
33 //
34 // Processing Statistics Packets
35 // -----------------------------
36 // Each packet of type `AOM_CODEC_CX_FRAME_PKT` contains the encoded data
37 // for this frame. We write a IVF frame header, followed by the raw data.
38 //
39 //
40 // Pass Progress Reporting
41 // -----------------------------
42 // It's sometimes helpful to see when each pass completes.
43 //
44 //
45 // Clean-up
46 // -----------------------------
47 // Destruction of the encoder instance must be done on each pass. The
48 // raw image should be destroyed at the end as usual.
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53
54 #include "aom/aom_encoder.h"
55 #include "common/tools_common.h"
56 #include "common/video_writer.h"
57
58 static const char *exec_name;
59
60 void usage_exit(void) {
61 fprintf(stderr,
62 "Usage: %s <codec> <width> <height> <infile> <outfile> "
63 "<limit(optional)>\n",
64 exec_name);
65 exit(EXIT_FAILURE);
66 }
67
68 static int get_frame_stats(aom_codec_ctx_t *ctx, const aom_image_t *img,
69 aom_codec_pts_t pts, unsigned int duration,
70 aom_enc_frame_flags_t flags,
71 aom_fixed_buf_t *stats) {
72 int got_pkts = 0;
73 aom_codec_iter_t iter = NULL;
74 const aom_codec_cx_pkt_t *pkt = NULL;
75 const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags);
76 if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to get frame stats.");
77
78 while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) {
79 got_pkts = 1;
80
81 if (pkt->kind == AOM_CODEC_STATS_PKT) {
82 const uint8_t *const pkt_buf = pkt->data.twopass_stats.buf;
83 const size_t pkt_size = pkt->data.twopass_stats.sz;
84 stats->buf = realloc(stats->buf, stats->sz + pkt_size);
85 memcpy((uint8_t *)stats->buf + stats->sz, pkt_buf, pkt_size);
86 stats->sz += pkt_size;
87 }
88 }
89
90 return got_pkts;
91 }
92
93 static int encode_frame(aom_codec_ctx_t *ctx, const aom_image_t *img,
94 aom_codec_pts_t pts, unsigned int duration,
95 aom_enc_frame_flags_t flags, AvxVideoWriter *writer) {
96 int got_pkts = 0;
97 aom_codec_iter_t iter = NULL;
98 const aom_codec_cx_pkt_t *pkt = NULL;
99 const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags);
100 if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to encode frame.");
101
102 while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) {
103 got_pkts = 1;
104 if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
105 const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
106
107 if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
108 pkt->data.frame.sz,
109 pkt->data.frame.pts))
110 die_codec(ctx, "Failed to write compressed frame.");
111 printf(keyframe ? "K" : ".");
112 fflush(stdout);
113 }
114 }
115
116 return got_pkts;
117 }
118
119 static aom_fixed_buf_t pass0(aom_image_t *raw, FILE *infile,
120 const AvxInterface *encoder,
121 const aom_codec_enc_cfg_t *cfg, int limit) {
122 aom_codec_ctx_t codec;
123 int frame_count = 0;
124 aom_fixed_buf_t stats = { NULL, 0 };
125
126 if (aom_codec_enc_init(&codec, encoder->codec_interface(), cfg, 0))
127 die_codec(&codec, "Failed to initialize encoder");
128
129 // Calculate frame statistics.
130 while (aom_img_read(raw, infile) && frame_count < limit) {
131 ++frame_count;
132 get_frame_stats(&codec, raw, frame_count, 1, 0, &stats);
133 }
134
135 // Flush encoder.
136 while (get_frame_stats(&codec, NULL, frame_count, 1, 0, &stats)) {
137 }
138
139 printf("Pass 0 complete. Processed %d frames.\n", frame_count);
140 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
141
142 return stats;
143 }
144
145 static void pass1(aom_image_t *raw, FILE *infile, const char *outfile_name,
146 const AvxInterface *encoder, const aom_codec_enc_cfg_t *cfg,
147 int limit) {
148 AvxVideoInfo info = { encoder->fourcc,
149 cfg->g_w,
150 cfg->g_h,
151 { cfg->g_timebase.num, cfg->g_timebase.den },
152 0 };
153 AvxVideoWriter *writer = NULL;
154 aom_codec_ctx_t codec;
155 int frame_count = 0;
156
157 writer = aom_video_writer_open(outfile_name, kContainerIVF, &info);
158 if (!writer) die("Failed to open %s for writing", outfile_name);
159
160 if (aom_codec_enc_init(&codec, encoder->codec_interface(), cfg, 0))
161 die_codec(&codec, "Failed to initialize encoder");
162
163 // Encode frames.
164 while (aom_img_read(raw, infile) && frame_count < limit) {
165 ++frame_count;
166 encode_frame(&codec, raw, frame_count, 1, 0, writer);
167 }
168
169 // Flush encoder.
170 while (encode_frame(&codec, NULL, -1, 1, 0, writer)) {
171 }
172
173 printf("\n");
174
175 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
176
177 aom_video_writer_close(writer);
178
179 printf("Pass 1 complete. Processed %d frames.\n", frame_count);
180 }
181
182 int main(int argc, char **argv) {
183 FILE *infile = NULL;
184 int w, h;
185 aom_codec_ctx_t codec;
186 aom_codec_enc_cfg_t cfg;
187 aom_image_t raw;
188 aom_codec_err_t res;
189 aom_fixed_buf_t stats;
190
191 const AvxInterface *encoder = NULL;
192 const int fps = 30; // TODO(dkovalev) add command line argument
193 const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument
194 const char *const codec_arg = argv[1];
195 const char *const width_arg = argv[2];
196 const char *const height_arg = argv[3];
197 const char *const infile_arg = argv[4];
198 const char *const outfile_arg = argv[5];
199 int limit = 0;
200 exec_name = argv[0];
201
202 if (argc < 6) die("Invalid number of arguments");
203
204 if (argc > 6) limit = (int)strtol(argv[6], NULL, 0);
205
206 if (limit == 0) limit = 100;
207
208 encoder = get_aom_encoder_by_name(codec_arg);
209 if (!encoder) die("Unsupported codec.");
210
211 w = (int)strtol(width_arg, NULL, 0);
212 h = (int)strtol(height_arg, NULL, 0);
213
214 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
215 die("Invalid frame size: %dx%d", w, h);
216
217 if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, w, h, 1))
218 die("Failed to allocate image", w, h);
219
220 printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
221
222 // Configuration
223 res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
224 if (res) die_codec(&codec, "Failed to get default codec config.");
225
226 cfg.g_w = w;
227 cfg.g_h = h;
228 cfg.g_timebase.num = 1;
229 cfg.g_timebase.den = fps;
230 cfg.rc_target_bitrate = bitrate;
231
232 if (!(infile = fopen(infile_arg, "rb")))
233 die("Failed to open %s for reading", infile_arg);
234
235 // Pass 0
236 cfg.g_pass = AOM_RC_FIRST_PASS;
237 stats = pass0(&raw, infile, encoder, &cfg, limit);
238
239 // Pass 1
240 rewind(infile);
241 cfg.g_pass = AOM_RC_LAST_PASS;
242 cfg.rc_twopass_stats_in = stats;
243 pass1(&raw, infile, outfile_arg, encoder, &cfg, limit);
244 free(stats.buf);
245
246 aom_img_free(&raw);
247 fclose(infile);
248
249 return EXIT_SUCCESS;
250 }
AOM_IMG_FMT_I420
@ AOM_IMG_FMT_I420
Definition: aom_image.h:45
aom_codec_pts_t
int64_t aom_codec_pts_t
Time Stamp Type.
Definition: aom_codec.h:181
aom_codec_enc_cfg
Encoder configuration structure.
Definition: aom_encoder.h:369
aom_fixed_buf
Generic fixed size buffer structure.
Definition: aom_encoder.h:76
aom_codec_cx_pkt::data
union aom_codec_cx_pkt::@0 data
aom_codec_ctx
Codec context structure.
Definition: aom_codec.h:219
AOM_CODEC_OK
@ AOM_CODEC_OK
Operation completed without error.
Definition: aom_codec.h:103
aom_codec_cx_pkt::frame
struct aom_codec_cx_pkt::@0::@1 frame
aom_codec_enc_cfg::rc_target_bitrate
unsigned int rc_target_bitrate
Target data rate.
Definition: aom_encoder.h:622
AOM_RC_FIRST_PASS
@ AOM_RC_FIRST_PASS
Definition: aom_encoder.h:179
aom_img_free
void aom_img_free(aom_image_t *img)
Close an image descriptor.
aom_codec_iface_name
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_rational::den
int den
Definition: aom_encoder.h:173
aom_codec_destroy
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
AOM_RC_LAST_PASS
@ AOM_RC_LAST_PASS
Definition: aom_encoder.h:180
aom_img_alloc
aom_image_t * aom_img_alloc(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
aom_codec_cx_pkt::twopass_stats
aom_fixed_buf_t twopass_stats
Definition: aom_encoder.h:149
aom_codec_enc_config_default
aom_codec_err_t aom_codec_enc_config_default(aom_codec_iface_t *iface, aom_codec_enc_cfg_t *cfg, unsigned int usage)
Get the default configuration for a usage.
aom_codec_enc_cfg::g_pass
enum aom_enc_pass g_pass
Multi-pass Encoding Mode.
Definition: aom_encoder.h:481
aom_enc_frame_flags_t
long aom_enc_frame_flags_t
Encoded Frame Flags.
Definition: aom_encoder.h:360
aom_codec_enc_cfg::g_w
unsigned int g_w
Width of the frame.
Definition: aom_encoder.h:408
aom_codec_enc_cfg::rc_twopass_stats_in
aom_fixed_buf_t rc_twopass_stats_in
Two-pass stats buffer.
Definition: aom_encoder.h:609
aom_codec_err_t
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:101
aom_encoder.h
Describes the encoder algorithm interface to applications.
aom_codec_get_cx_data
const aom_codec_cx_pkt_t * aom_codec_get_cx_data(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Encoded data iterator.
aom_codec_encode
aom_codec_err_t aom_codec_encode(aom_codec_ctx_t *ctx, const aom_image_t *img, aom_codec_pts_t pts, unsigned long duration, aom_enc_frame_flags_t flags)
Encode a frame.
aom_codec_cx_pkt
Encoder output packet.
Definition: aom_encoder.h:131
aom_codec_iter_t
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:209
AOM_CODEC_STATS_PKT
@ AOM_CODEC_STATS_PKT
Definition: aom_encoder.h:120
aom_codec_enc_cfg::g_h
unsigned int g_h
Height of the frame.
Definition: aom_encoder.h:417
AOM_CODEC_CX_FRAME_PKT
@ AOM_CODEC_CX_FRAME_PKT
Definition: aom_encoder.h:119
aom_codec_enc_init
#define aom_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_enc_init_ver()
Definition: aom_encoder.h:941
aom_image
Image Descriptor.
Definition: aom_image.h:171
aom_fixed_buf::sz
size_t sz
Definition: aom_encoder.h:78
aom_codec_cx_pkt::kind
enum aom_codec_cx_pkt_kind kind
Definition: aom_encoder.h:132
aom_codec_enc_cfg::g_timebase
struct aom_rational g_timebase
Stream timebase units.
Definition: aom_encoder.h:466
AOM_FRAME_IS_KEY
#define AOM_FRAME_IS_KEY
Definition: aom_encoder.h:89
aom_fixed_buf::buf
void * buf
Definition: aom_encoder.h:77
aom_rational::num
int num
Definition: aom_encoder.h:172