AOMedia AV1 Codec
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 "aom/aomcx.h"
56 #include "common/tools_common.h"
57 #include "common/video_writer.h"
58 
59 static const char *exec_name;
60 
61 void usage_exit(void) {
62  fprintf(stderr,
63  "Usage: %s <codec> <width> <height> <infile> <outfile> "
64  "<limit(optional)>\n",
65  exec_name);
66  exit(EXIT_FAILURE);
67 }
68 
69 static int get_frame_stats(aom_codec_ctx_t *ctx, const aom_image_t *img,
70  aom_codec_pts_t pts, unsigned int duration,
72  aom_fixed_buf_t *stats) {
73  int got_pkts = 0;
74  aom_codec_iter_t iter = NULL;
75  const aom_codec_cx_pkt_t *pkt = NULL;
76  const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags);
77  if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to get frame stats.");
78 
79  while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) {
80  got_pkts = 1;
81 
82  if (pkt->kind == AOM_CODEC_STATS_PKT) {
83  const uint8_t *const pkt_buf = pkt->data.twopass_stats.buf;
84  const size_t pkt_size = pkt->data.twopass_stats.sz;
85  stats->buf = realloc(stats->buf, stats->sz + pkt_size);
86  memcpy((uint8_t *)stats->buf + stats->sz, pkt_buf, pkt_size);
87  stats->sz += pkt_size;
88  }
89  }
90 
91  return got_pkts;
92 }
93 
94 static int encode_frame(aom_codec_ctx_t *ctx, const aom_image_t *img,
95  aom_codec_pts_t pts, unsigned int duration,
96  aom_enc_frame_flags_t flags, AvxVideoWriter *writer) {
97  int got_pkts = 0;
98  aom_codec_iter_t iter = NULL;
99  const aom_codec_cx_pkt_t *pkt = NULL;
100  const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags);
101  if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to encode frame.");
102 
103  while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) {
104  got_pkts = 1;
105  if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
106  const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
107 
108  if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
109  pkt->data.frame.sz,
110  pkt->data.frame.pts))
111  die_codec(ctx, "Failed to write compressed frame.");
112  printf(keyframe ? "K" : ".");
113  fflush(stdout);
114  }
115  }
116 
117  return got_pkts;
118 }
119 
120 static aom_fixed_buf_t pass0(aom_image_t *raw, FILE *infile,
121  aom_codec_iface_t *encoder,
122  const aom_codec_enc_cfg_t *cfg, int limit) {
123  aom_codec_ctx_t codec;
124  int frame_count = 0;
125  aom_fixed_buf_t stats = { NULL, 0 };
126 
127  if (aom_codec_enc_init(&codec, encoder, cfg, 0))
128  die("Failed to initialize encoder");
129 
130  // Calculate frame statistics.
131  while (aom_img_read(raw, infile) && frame_count < limit) {
132  ++frame_count;
133  get_frame_stats(&codec, raw, frame_count, 1, 0, &stats);
134  }
135 
136  // Flush encoder.
137  while (get_frame_stats(&codec, NULL, frame_count, 1, 0, &stats)) {
138  }
139 
140  printf("Pass 0 complete. Processed %d frames.\n", frame_count);
141  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
142 
143  return stats;
144 }
145 
146 static void pass1(aom_image_t *raw, FILE *infile, const char *outfile_name,
147  aom_codec_iface_t *encoder, const aom_codec_enc_cfg_t *cfg,
148  int limit) {
149  AvxVideoInfo info = { get_fourcc_by_aom_encoder(encoder),
150  cfg->g_w,
151  cfg->g_h,
152  { cfg->g_timebase.num, cfg->g_timebase.den },
153  0 };
154  AvxVideoWriter *writer = NULL;
155  aom_codec_ctx_t codec;
156  int frame_count = 0;
157 
158  writer = aom_video_writer_open(outfile_name, kContainerIVF, &info);
159  if (!writer) die("Failed to open %s for writing", outfile_name);
160 
161  if (aom_codec_enc_init(&codec, encoder, cfg, 0))
162  die("Failed to initialize encoder");
163 
164  if (aom_codec_control(&codec, AOME_SET_CPUUSED, 2))
165  die_codec(&codec, "Failed to set cpu-used");
166 
167  // Encode frames.
168  while (aom_img_read(raw, infile) && frame_count < limit) {
169  ++frame_count;
170  encode_frame(&codec, raw, frame_count, 1, 0, writer);
171  }
172 
173  // Flush encoder.
174  while (encode_frame(&codec, NULL, -1, 1, 0, writer)) {
175  }
176 
177  printf("\n");
178 
179  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
180 
181  aom_video_writer_close(writer);
182 
183  printf("Pass 1 complete. Processed %d frames.\n", frame_count);
184 }
185 
186 int main(int argc, char **argv) {
187  FILE *infile = NULL;
188  int w, h;
189  aom_codec_ctx_t codec;
191  aom_image_t raw;
192  aom_codec_err_t res;
193  aom_fixed_buf_t stats;
194 
195  const int fps = 30; // TODO(dkovalev) add command line argument
196  const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument
197  const char *const codec_arg = argv[1];
198  const char *const width_arg = argv[2];
199  const char *const height_arg = argv[3];
200  const char *const infile_arg = argv[4];
201  const char *const outfile_arg = argv[5];
202  int limit = 0;
203  exec_name = argv[0];
204 
205  if (argc < 6) die("Invalid number of arguments");
206 
207  if (argc > 6) limit = (int)strtol(argv[6], NULL, 0);
208 
209  if (limit == 0) limit = 100;
210 
211  aom_codec_iface_t *encoder = get_aom_encoder_by_short_name(codec_arg);
212  if (!encoder) die("Unsupported codec.");
213 
214  w = (int)strtol(width_arg, NULL, 0);
215  h = (int)strtol(height_arg, NULL, 0);
216 
217  if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
218  die("Invalid frame size: %dx%d", w, h);
219 
220  if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, w, h, 1))
221  die("Failed to allocate image", w, h);
222 
223  printf("Using %s\n", aom_codec_iface_name(encoder));
224 
225  // Configuration
226  res = aom_codec_enc_config_default(encoder, &cfg, 0);
227  if (res) die_codec(&codec, "Failed to get default codec config.");
228 
229  cfg.g_w = w;
230  cfg.g_h = h;
231  cfg.g_timebase.num = 1;
232  cfg.g_timebase.den = fps;
233  cfg.rc_target_bitrate = bitrate;
234 
235  if (!(infile = fopen(infile_arg, "rb")))
236  die("Failed to open %s for reading", infile_arg);
237 
238  // Pass 0
240  stats = pass0(&raw, infile, encoder, &cfg, limit);
241 
242  // Pass 1
243  rewind(infile);
244  cfg.g_pass = AOM_RC_LAST_PASS;
245  cfg.rc_twopass_stats_in = stats;
246  pass1(&raw, infile, outfile_arg, encoder, &cfg, limit);
247  free(stats.buf);
248 
249  aom_img_free(&raw);
250  fclose(infile);
251 
252  return EXIT_SUCCESS;
253 }
void * buf
Definition: aom_encoder.h:77
Operation completed without error.
Definition: aom_codec.h:157
Definition: aom_encoder.h:161
unsigned int g_w
Width of the frame.
Definition: aom_encoder.h:406
unsigned int rc_target_bitrate
Target data rate.
Definition: aom_encoder.h:618
int64_t aom_codec_pts_t
Time Stamp Type.
Definition: aom_codec.h:235
Describes the encoder algorithm interface to applications.
#define aom_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_enc_init_ver()
Definition: aom_encoder.h:931
Encoder configuration structure.
Definition: aom_encoder.h:367
aom_fixed_buf_t rc_twopass_stats_in
Two-pass stats buffer.
Definition: aom_encoder.h:605
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.
Provides definitions for using AOM or AV1 encoder algorithm within the aom Codec Interface.
Codec context structure.
Definition: aom_codec.h:298
Image Descriptor.
Definition: aom_image.h:171
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.
Generic fixed size buffer structure.
Definition: aom_encoder.h:76
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_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.
const struct aom_codec_iface aom_codec_iface_t
Codec interface structure.
Definition: aom_codec.h:254
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
struct aom_rational g_timebase
Stream timebase units.
Definition: aom_encoder.h:464
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
Definition: aom_encoder.h:99
enum aom_codec_cx_pkt_kind kind
Definition: aom_encoder.h:111
void aom_img_free(aom_image_t *img)
Close an image descriptor.
struct aom_codec_cx_pkt::@1::@2 frame
Definition: aom_encoder.h:98
Codec control function to set encoder internal speed settings, int parameter.
Definition: aomcx.h:213
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:288
#define AOM_FRAME_IS_KEY
Definition: aom_codec.h:271
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:155
aom_fixed_buf_t twopass_stats
Definition: aom_encoder.h:128
int den
Definition: aom_encoder.h:154
Encoder output packet.
Definition: aom_encoder.h:110
int num
Definition: aom_encoder.h:153
Definition: aom_image.h:45
long aom_enc_frame_flags_t
Encoded Frame Flags.
Definition: aom_encoder.h:357
union aom_codec_cx_pkt::@1 data
size_t sz
Definition: aom_encoder.h:78
Definition: aom_encoder.h:160
enum aom_enc_pass g_pass
Multi-pass Encoding Mode.
Definition: aom_encoder.h:479
aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id,...)
Algorithm Control.
unsigned int g_h
Height of the frame.
Definition: aom_encoder.h:415