AOMedia AV1 Codec: scalable_encoder

| AOMedia AV1 Codec

scalable_encoder
1/*
2 * Copyright (c) 2018, 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// Scalable Encoder
13// ==============
14//
15// This is an example of a scalable encoder loop. It takes two input files in
16// YV12 format, passes it through the encoder, and writes the compressed
17// frames to disk in OBU format.
18//
19// Getting The Default Configuration
20// ---------------------------------
21// Encoders have the notion of "usage profiles." For example, an encoder
22// may want to publish default configurations for both a video
23// conferencing application and a best quality offline encoder. These
24// obviously have very different default settings. Consult the
25// documentation for your codec to see if it provides any default
26// configurations. All codecs provide a default configuration, number 0,
27// which is valid for material in the vacinity of QCIF/QVGA.
28//
29// Updating The Configuration
30// ---------------------------------
31// Almost all applications will want to update the default configuration
32// with settings specific to their usage. Here we set the width and height
33// of the video file to that specified on the command line. We also scale
34// the default bitrate based on the ratio between the default resolution
35// and the resolution specified on the command line.
36//
37// Encoding A Frame
38// ----------------
39// The frame is read as a continuous block (size = width * height * 3 / 2)
40// from the input file. If a frame was read (the input file has not hit
41// EOF) then the frame is passed to the encoder. Otherwise, a NULL
42// is passed, indicating the End-Of-Stream condition to the encoder. The
43// `frame_cnt` is reused as the presentation time stamp (PTS) and each
44// frame is shown for one frame-time in duration. The flags parameter is
45// unused in this example.
46
47// Forced Keyframes
48// ----------------
49// Keyframes can be forced by setting the AOM_EFLAG_FORCE_KF bit of the
50// flags passed to `aom_codec_control()`. In this example, we force a
51// keyframe every <keyframe-interval> frames. Note, the output stream can
52// contain additional keyframes beyond those that have been forced using the
53// AOM_EFLAG_FORCE_KF flag because of automatic keyframe placement by the
54// encoder.
55//
56// Processing The Encoded Data
57// ---------------------------
58// Each packet of type `AOM_CODEC_CX_FRAME_PKT` contains the encoded data
59// for this frame. We write a IVF frame header, followed by the raw data.
60//
61// Cleanup
62// -------
63// The `aom_codec_destroy` call frees any memory allocated by the codec.
64//
65// Error Handling
66// --------------
67// This example does not special case any error return codes. If there was
68// an error, a descriptive message is printed and the program exits. With
69// few exeptions, aom_codec functions return an enumerated error status,
70// with the value `0` indicating success.
71
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75
76#include "aom/aom_encoder.h"
77#include "aom/aomcx.h"
78#include "av1/common/enums.h"
79#include "common/tools_common.h"
80#include "common/video_writer.h"
81
82static const char *exec_name;
83
84void usage_exit(void) {
85 fprintf(stderr,
86 "Usage: %s <codec> <width> <height> <infile0> <infile1> "
87 "<outfile> <frames to encode>\n"
88 "See comments in scalable_encoder.c for more information.\n",
89 exec_name);
90 exit(EXIT_FAILURE);
91}
92
93static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img,
94 int frame_index, int flags, FILE *outfile) {
95 int got_pkts = 0;
96 aom_codec_iter_t iter = NULL;
97 const aom_codec_cx_pkt_t *pkt = NULL;
98 const aom_codec_err_t res =
99 aom_codec_encode(codec, img, frame_index, 1, flags);
100 if (res != AOM_CODEC_OK) die_codec(codec, "Failed to encode frame");
101
102 while ((pkt = aom_codec_get_cx_data(codec, &iter)) != NULL) {
103 got_pkts = 1;
104
105 if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
106 const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
107 if (fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz, outfile) !=
108 pkt->data.frame.sz) {
109 die_codec(codec, "Failed to write compressed frame");
110 }
111 printf(keyframe ? "K" : ".");
112 printf(" %6d\n", (int)pkt->data.frame.sz);
113 fflush(stdout);
114 }
115 }
116
117 return got_pkts;
118}
119
120int main(int argc, char **argv) {
121 FILE *infile0 = NULL;
122 FILE *infile1 = NULL;
123 aom_codec_enc_cfg_t cfg;
124 int frame_count = 0;
125 aom_image_t raw0, raw1;
126 aom_codec_err_t res;
127 AvxVideoInfo info;
128 const int fps = 30;
129 const int bitrate = 200;
130 int keyframe_interval = 0;
131 int max_frames = 0;
132 int frames_encoded = 0;
133 const char *codec_arg = NULL;
134 const char *width_arg = NULL;
135 const char *height_arg = NULL;
136 const char *infile0_arg = NULL;
137 const char *infile1_arg = NULL;
138 const char *outfile_arg = NULL;
139 // const char *keyframe_interval_arg = NULL;
140 FILE *outfile = NULL;
141
142 exec_name = argv[0];
143
144 // Clear explicitly, as simply assigning "{ 0 }" generates
145 // "missing-field-initializers" warning in some compilers.
146 memset(&info, 0, sizeof(info));
147
148 if (argc != 8) die("Invalid number of arguments");
149
150 codec_arg = argv[1];
151 width_arg = argv[2];
152 height_arg = argv[3];
153 infile0_arg = argv[4];
154 infile1_arg = argv[5];
155 outfile_arg = argv[6];
156 max_frames = (int)strtol(argv[7], NULL, 0);
157
158 aom_codec_iface_t *encoder = get_aom_encoder_by_short_name(codec_arg);
159 if (!encoder) die("Unsupported codec.");
160
161 info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
162 info.frame_width = (int)strtol(width_arg, NULL, 0);
163 info.frame_height = (int)strtol(height_arg, NULL, 0);
164 info.time_base.numerator = 1;
165 info.time_base.denominator = fps;
166
167 if (info.frame_width <= 0 || info.frame_height <= 0 ||
168 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
169 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
170 }
171
172 if (!aom_img_alloc(&raw0, AOM_IMG_FMT_I420, info.frame_width,
173 info.frame_height, 1)) {
174 die("Failed to allocate image for layer 0.");
175 }
176 if (!aom_img_alloc(&raw1, AOM_IMG_FMT_I420, info.frame_width,
177 info.frame_height, 1)) {
178 die("Failed to allocate image for layer 1.");
179 }
180
181 // keyframe_interval = (int)strtol(keyframe_interval_arg, NULL, 0);
182 keyframe_interval = 100;
183 if (keyframe_interval < 0) die("Invalid keyframe interval value.");
184
185 printf("Using %s\n", aom_codec_iface_name(encoder));
186
187 aom_codec_ctx_t codec;
188 res = aom_codec_enc_config_default(encoder, &cfg, 0);
189 if (res)
190 die("Failed to get default codec config: %s", aom_codec_err_to_string(res));
191
192 cfg.g_w = info.frame_width;
193 cfg.g_h = info.frame_height;
194 cfg.g_timebase.num = info.time_base.numerator;
195 cfg.g_timebase.den = info.time_base.denominator;
196 cfg.rc_target_bitrate = bitrate;
197 cfg.g_error_resilient = 0;
198 cfg.g_lag_in_frames = 0;
199 cfg.rc_end_usage = AOM_Q;
200 cfg.save_as_annexb = 0;
201
202 outfile = fopen(outfile_arg, "wb");
203 if (!outfile) die("Failed to open %s for writing.", outfile_arg);
204
205 if (!(infile0 = fopen(infile0_arg, "rb")))
206 die("Failed to open %s for reading.", infile0_arg);
207 if (!(infile1 = fopen(infile1_arg, "rb")))
208 die("Failed to open %s for reading.", infile0_arg);
209
210 if (aom_codec_enc_init(&codec, encoder, &cfg, 0))
211 die("Failed to initialize encoder");
212 if (aom_codec_control(&codec, AOME_SET_CPUUSED, 8))
213 die_codec(&codec, "Failed to set cpu to 8");
214
215 if (aom_codec_control(&codec, AV1E_SET_TILE_COLUMNS, 2))
216 die_codec(&codec, "Failed to set tile columns to 2");
217 if (aom_codec_control(&codec, AV1E_SET_NUM_TG, 3))
218 die_codec(&codec, "Failed to set num of tile groups to 3");
219
220 if (aom_codec_control(&codec, AOME_SET_NUMBER_SPATIAL_LAYERS, 2))
221 die_codec(&codec, "Failed to set number of spatial layers to 2");
222
223 // Encode frames.
224 while (aom_img_read(&raw0, infile0)) {
225 int flags = 0;
226
227 // configure and encode base layer
228
229 if (keyframe_interval > 0 && frames_encoded % keyframe_interval == 0)
230 flags |= AOM_EFLAG_FORCE_KF;
231 else
232 // use previous base layer (LAST) as sole reference
233 // save this frame as LAST to be used as reference by enhanmcent layer
234 // and next base layer
235 flags |= AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
236 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF |
237 AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 |
238 AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
239 AOM_EFLAG_NO_UPD_ENTROPY;
240 cfg.g_w = info.frame_width;
241 cfg.g_h = info.frame_height;
242 if (aom_codec_enc_config_set(&codec, &cfg))
243 die_codec(&codec, "Failed to set enc cfg for layer 0");
244 if (aom_codec_control(&codec, AOME_SET_SPATIAL_LAYER_ID, 0))
245 die_codec(&codec, "Failed to set layer id to 0");
246 if (aom_codec_control(&codec, AOME_SET_CQ_LEVEL, 62))
247 die_codec(&codec, "Failed to set cq level");
248 encode_frame(&codec, &raw0, frame_count++, flags, outfile);
249
250 // configure and encode enhancement layer
251
252 // use LAST (base layer) as sole reference
253 flags = AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
254 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
255 AOM_EFLAG_NO_REF_ARF2 | AOM_EFLAG_NO_UPD_LAST |
256 AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
257 AOM_EFLAG_NO_UPD_ENTROPY;
258 cfg.g_w = info.frame_width;
259 cfg.g_h = info.frame_height;
260 aom_img_read(&raw1, infile1);
261 if (aom_codec_enc_config_set(&codec, &cfg))
262 die_codec(&codec, "Failed to set enc cfg for layer 1");
263 if (aom_codec_control(&codec, AOME_SET_SPATIAL_LAYER_ID, 1))
264 die_codec(&codec, "Failed to set layer id to 1");
265 if (aom_codec_control(&codec, AOME_SET_CQ_LEVEL, 10))
266 die_codec(&codec, "Failed to set cq level");
267 encode_frame(&codec, &raw1, frame_count++, flags, outfile);
268
269 frames_encoded++;
270
271 if (max_frames > 0 && frames_encoded >= max_frames) break;
272 }
273
274 // Flush encoder.
275 while (encode_frame(&codec, NULL, -1, 0, outfile)) continue;
276
277 printf("\n");
278 fclose(infile0);
279 fclose(infile1);
280 printf("Processed %d frames.\n", frame_count / 2);
281
282 aom_img_free(&raw0);
283 aom_img_free(&raw1);
284 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
285
286 fclose(outfile);
287
288 return EXIT_SUCCESS;
289}
aom_encoder.h
Describes the encoder algorithm interface to applications.
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_IMG_FMT_I420
@ AOM_IMG_FMT_I420
Definition aom_image.h:45
aom_image_t
struct aom_image aom_image_t
Image Descriptor.
aom_img_free
void aom_img_free(aom_image_t *img)
Close an image descriptor.
aomcx.h
Provides definitions for using AOM or AV1 encoder algorithm within the aom Codec Interface.
enums.h
AOM_EFLAG_NO_UPD_ARF
#define AOM_EFLAG_NO_UPD_ARF
Don't update the alternate reference frame.
Definition aomcx.h:137
AOM_EFLAG_NO_REF_LAST2
#define AOM_EFLAG_NO_REF_LAST2
Don't reference the last2 frame.
Definition aomcx.h:80
AOM_EFLAG_NO_REF_BWD
#define AOM_EFLAG_NO_REF_BWD
Don't reference the bwd reference frame.
Definition aomcx.h:109
AOM_EFLAG_NO_UPD_LAST
#define AOM_EFLAG_NO_UPD_LAST
Don't update the last frame.
Definition aomcx.h:123
AOM_EFLAG_NO_REF_ARF
#define AOM_EFLAG_NO_REF_ARF
Don't reference the alternate reference frame.
Definition aomcx.h:102
AOM_EFLAG_NO_REF_LAST3
#define AOM_EFLAG_NO_REF_LAST3
Don't reference the last3 frame.
Definition aomcx.h:87
AOM_EFLAG_NO_UPD_GF
#define AOM_EFLAG_NO_UPD_GF
Don't update the golden frame.
Definition aomcx.h:130
AOM_EFLAG_NO_REF_GF
#define AOM_EFLAG_NO_REF_GF
Don't reference the golden frame.
Definition aomcx.h:94
AOM_EFLAG_NO_UPD_ENTROPY
#define AOM_EFLAG_NO_UPD_ENTROPY
Disable entropy update.
Definition aomcx.h:143
AOM_EFLAG_NO_REF_ARF2
#define AOM_EFLAG_NO_REF_ARF2
Don't reference the alt2 reference frame.
Definition aomcx.h:116
AV1E_SET_NUM_TG
@ AV1E_SET_NUM_TG
Codec control function to set a maximum number of tile groups, unsigned int parameter.
Definition aomcx.h:803
AOME_SET_SPATIAL_LAYER_ID
@ AOME_SET_SPATIAL_LAYER_ID
Codec control function to set encoder spatial layer id, int parameter. Spatial layer id must be withi...
Definition aomcx.h:205
AV1E_SET_TILE_COLUMNS
@ AV1E_SET_TILE_COLUMNS
Codec control function to set number of tile columns. unsigned int parameter.
Definition aomcx.h:393
AOME_SET_CPUUSED
@ AOME_SET_CPUUSED
Codec control function to set encoder internal speed settings, int parameter.
Definition aomcx.h:223
AOME_SET_NUMBER_SPATIAL_LAYERS
@ AOME_SET_NUMBER_SPATIAL_LAYERS
Codec control function to set number of spatial layers, int parameter.
Definition aomcx.h:324
AOME_SET_CQ_LEVEL
@ AOME_SET_CQ_LEVEL
Codec control function to set constrained / constant quality level, unsigned int parameter.
Definition aomcx.h:301
aom_codec_iface_name
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_codec_control
aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id,...)
Algorithm Control.
aom_codec_ctx_t
struct aom_codec_ctx aom_codec_ctx_t
Codec context structure.
aom_codec_iface_t
const struct aom_codec_iface aom_codec_iface_t
Codec interface structure.
Definition aom_codec.h:271
aom_codec_destroy
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
aom_codec_err_to_string
const char * aom_codec_err_to_string(aom_codec_err_t err)
Convert error number to printable string.
aom_codec_err_t
aom_codec_err_t
Algorithm return codes.
Definition aom_codec.h:155
aom_codec_iter_t
const void * aom_codec_iter_t
Iterator.
Definition aom_codec.h:305
AOM_FRAME_IS_KEY
#define AOM_FRAME_IS_KEY
Definition aom_codec.h:288
AOM_CODEC_OK
@ AOM_CODEC_OK
Operation completed without error.
Definition aom_codec.h:157
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_cx_pkt_t
struct aom_codec_cx_pkt aom_codec_cx_pkt_t
Encoder output packet.
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_EFLAG_FORCE_KF
#define AOM_EFLAG_FORCE_KF
Force this frame to be a keyframe.
Definition aom_encoder.h:379
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:956
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_t
struct aom_codec_enc_cfg aom_codec_enc_cfg_t
Encoder configuration structure.
aom_codec_enc_config_set
aom_codec_err_t aom_codec_enc_config_set(aom_codec_ctx_t *ctx, const aom_codec_enc_cfg_t *cfg)
Set or change configuration.
AOM_Q
@ AOM_Q
Definition aom_encoder.h:189
AOM_CODEC_CX_FRAME_PKT
@ AOM_CODEC_CX_FRAME_PKT
Definition aom_encoder.h:110
aom_codec_cx_pkt::kind
enum aom_codec_cx_pkt_kind kind
Definition aom_encoder.h:123
aom_codec_cx_pkt::data
union aom_codec_cx_pkt::@202210014045072156205127107315337341215221351166 data
aom_codec_enc_cfg::g_timebase
struct aom_rational g_timebase
Stream timebase units.
Definition aom_encoder.h:498
aom_codec_enc_cfg::g_h
unsigned int g_h
Height of the frame.
Definition aom_encoder.h:440
aom_codec_enc_cfg::rc_end_usage
enum aom_rc_mode rc_end_usage
Rate control algorithm to use.
Definition aom_encoder.h:632
aom_codec_enc_cfg::g_lag_in_frames
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition aom_encoder.h:527
aom_codec_enc_cfg::g_w
unsigned int g_w
Width of the frame.
Definition aom_encoder.h:431
aom_codec_enc_cfg::g_error_resilient
aom_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition aom_encoder.h:506
aom_codec_enc_cfg::rc_target_bitrate
unsigned int rc_target_bitrate
Target data rate.
Definition aom_encoder.h:653
aom_codec_enc_cfg::save_as_annexb
unsigned int save_as_annexb
Bitstream syntax mode.
Definition aom_encoder.h:853
aom_rational::num
int num
Definition aom_encoder.h:165
aom_rational::den
int den
Definition aom_encoder.h:166