40 #include "av1/common/enums.h"
42 #include "common/tools_common.h"
43 #include "common/video_writer.h"
45 #define MAX_EXTERNAL_REFERENCES 128
46 #define AOM_BORDER_IN_PIXELS 288
48 static const char *exec_name;
50 void usage_exit(
void) {
52 "Usage: %s <img_width> <img_height> <infile> <outfile> "
53 "<lf_width> <lf_height> <lf_blocksize>\n",
59 int image_size_bytes = 0;
61 for (plane = 0; plane < 3; ++plane) {
65 image_size_bytes += w * h;
67 return image_size_bytes;
78 if (res !=
AOM_CODEC_OK) die_codec(ctx,
"Failed to get frame stats.");
86 stats->
buf = realloc(stats->
buf, stats->
sz + pkt_size);
87 memcpy((uint8_t *)stats->
buf + stats->
sz, pkt_buf, pkt_size);
88 stats->
sz += pkt_size;
102 if (res !=
AOM_CODEC_OK) die_codec(ctx,
"Failed to encode frame.");
109 if (!aom_video_writer_write_frame(writer, pkt->
data.
frame.buf,
112 die_codec(ctx,
"Failed to write compressed frame.");
113 printf(keyframe ?
"K" :
".");
123 if (!CONFIG_LOWBITDEPTH) {
126 aom_img_upshift(raw_shift, raw, input_shift);
127 *frame_to_encode = raw_shift;
129 *frame_to_encode = raw;
134 const AvxInterface *encoder,
136 int lf_height,
int lf_blocksize,
int flags,
140 int image_size_bytes = aom_img_size_bytes(raw);
141 int u_blocks, v_blocks;
147 die_codec(&codec,
"Failed to initialize encoder");
149 die_codec(&codec,
"Failed to turn off auto altref");
151 die_codec(&codec,
"Failed to set frame parallel decoding");
154 u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize;
155 v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize;
157 printf(
"\n First pass: ");
159 for (bv = 0; bv < v_blocks; ++bv) {
160 for (bu = 0; bu < u_blocks; ++bu) {
161 const int block_u_min = bu * lf_blocksize;
162 const int block_v_min = bv * lf_blocksize;
163 int block_u_end = (bu + 1) * lf_blocksize;
164 int block_v_end = (bv + 1) * lf_blocksize;
165 int u_block_size, v_block_size;
166 int block_ref_u, block_ref_v;
168 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
169 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
170 u_block_size = block_u_end - block_u_min;
171 v_block_size = block_v_end - block_v_min;
172 block_ref_u = block_u_min + u_block_size / 2;
173 block_ref_v = block_v_min + v_block_size / 2;
175 printf(
"A%d, ", (block_ref_u + block_ref_v * lf_width));
176 fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes,
178 aom_img_read(raw, infile);
179 get_raw_image(&frame_to_encode, raw, raw_shift);
183 get_frame_stats(&codec, frame_to_encode, frame_count, 1,
194 die_codec(&codec,
"Failed to set frame parallel decoding");
196 for (bv = 0; bv < v_blocks; ++bv) {
197 for (bu = 0; bu < u_blocks; ++bu) {
198 const int block_u_min = bu * lf_blocksize;
199 const int block_v_min = bv * lf_blocksize;
200 int block_u_end = (bu + 1) * lf_blocksize;
201 int block_v_end = (bv + 1) * lf_blocksize;
203 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
204 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
205 for (v = block_v_min; v < block_v_end; ++v) {
206 for (u = block_u_min; u < block_u_end; ++u) {
207 printf(
"C%d, ", (u + v * lf_width));
208 fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET);
209 aom_img_read(raw, infile);
210 get_raw_image(&frame_to_encode, raw, raw_shift);
213 get_frame_stats(&codec, frame_to_encode, frame_count, 1,
226 while (get_frame_stats(&codec, NULL, frame_count, 1, 0, &stats)) {
231 printf(
"\nFirst pass complete. Processed %d frames.\n", frame_count);
236 static void pass1(
aom_image_t *raw, FILE *infile,
const char *outfile_name,
238 int lf_width,
int lf_height,
int lf_blocksize,
int flags,
240 AvxVideoInfo info = { encoder->fourcc,
244 AvxVideoWriter *writer = NULL;
247 int image_size_bytes = aom_img_size_bytes(raw);
249 int u_blocks, v_blocks;
251 aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
252 int reference_image_num = 0;
255 writer = aom_video_writer_open(outfile_name, kContainerIVF, &info);
256 if (!writer) die(
"Failed to open %s for writing", outfile_name);
259 die_codec(&codec,
"Failed to initialize encoder");
261 die_codec(&codec,
"Failed to turn off auto altref");
263 die_codec(&codec,
"Failed to set frame parallel decoding");
271 die_codec(&codec,
"Failed to set SB size");
273 u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize;
274 v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize;
276 reference_image_num = u_blocks * v_blocks;
280 for (i = 0; i < reference_image_num; i++) {
282 cfg->
g_h, 32, 8, AOM_BORDER_IN_PIXELS)) {
283 die(
"Failed to allocate image.");
287 printf(
"\n Second pass: ");
290 printf(
"Encoding Reference Images\n");
291 for (bv = 0; bv < v_blocks; ++bv) {
292 for (bu = 0; bu < u_blocks; ++bu) {
293 const int block_u_min = bu * lf_blocksize;
294 const int block_v_min = bv * lf_blocksize;
295 int block_u_end = (bu + 1) * lf_blocksize;
296 int block_v_end = (bv + 1) * lf_blocksize;
297 int u_block_size, v_block_size;
298 int block_ref_u, block_ref_v;
300 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
301 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
302 u_block_size = block_u_end - block_u_min;
303 v_block_size = block_v_end - block_v_min;
304 block_ref_u = block_u_min + u_block_size / 2;
305 block_ref_v = block_v_min + v_block_size / 2;
307 printf(
"A%d, ", (block_ref_u + block_ref_v * lf_width));
308 fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes,
310 aom_img_read(raw, infile);
312 get_raw_image(&frame_to_encode, raw, raw_shift);
316 printf(
"Encoding reference image %d of %d\n", bv * u_blocks + bu,
317 u_blocks * v_blocks);
318 encode_frame(&codec, frame_to_encode, frame_count, 1,
327 &reference_images[frame_count - 1]))
328 die_codec(&codec,
"Failed to copy decoder reference frame");
336 die_codec(&codec,
"Failed to configure encoder");
340 die_codec(&codec,
"Failed to set cq level");
342 die_codec(&codec,
"Failed to set frame parallel decoding");
344 die_codec(&codec,
"Failed to turn on single tile decoding");
348 die_codec(&codec,
"Failed to set tile width");
350 die_codec(&codec,
"Failed to set tile height");
352 for (bv = 0; bv < v_blocks; ++bv) {
353 for (bu = 0; bu < u_blocks; ++bu) {
354 const int block_u_min = bu * lf_blocksize;
355 const int block_v_min = bv * lf_blocksize;
356 int block_u_end = (bu + 1) * lf_blocksize;
357 int block_v_end = (bv + 1) * lf_blocksize;
359 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
360 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
361 for (v = block_v_min; v < block_v_end; ++v) {
362 for (u = block_u_min; u < block_u_end; ++u) {
366 ref.
img = reference_images[bv * u_blocks + bu];
368 die_codec(&codec,
"Failed to set reference frame");
370 printf(
"C%d, ", (u + v * lf_width));
371 fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET);
372 aom_img_read(raw, infile);
373 get_raw_image(&frame_to_encode, raw, raw_shift);
376 printf(
"Encoding image %d of %d\n",
377 frame_count - (u_blocks * v_blocks), lf_width * lf_height);
378 encode_frame(&codec, frame_to_encode, frame_count, 1,
392 while (encode_frame(&codec, NULL, -1, 1, 0, writer)) {
395 for (i = 0; i < reference_image_num; i++)
aom_img_free(&reference_images[i]);
398 aom_video_writer_close(writer);
400 printf(
"\nSecond pass complete. Processed %d frames.\n", frame_count);
403 int main(
int argc,
char **argv) {
407 int lf_width, lf_height;
420 const AvxInterface *encoder = NULL;
422 const int bitrate = 200;
423 const char *
const width_arg = argv[1];
424 const char *
const height_arg = argv[2];
425 const char *
const infile_arg = argv[3];
426 const char *
const outfile_arg = argv[4];
427 const char *
const lf_width_arg = argv[5];
428 const char *
const lf_height_arg = argv[6];
429 const char *lf_blocksize_arg = argv[7];
432 if (argc < 8) die(
"Invalid number of arguments");
434 encoder = get_aom_encoder_by_name(
"av1");
435 if (!encoder) die(
"Unsupported codec.");
437 w = (int)strtol(width_arg, NULL, 0);
438 h = (int)strtol(height_arg, NULL, 0);
439 lf_width = (int)strtol(lf_width_arg, NULL, 0);
440 lf_height = (int)strtol(lf_height_arg, NULL, 0);
441 lf_blocksize = (int)strtol(lf_blocksize_arg, NULL, 0);
442 lf_blocksize = lf_blocksize < lf_width ? lf_blocksize : lf_width;
443 lf_blocksize = lf_blocksize < lf_height ? lf_blocksize : lf_height;
445 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
446 die(
"Invalid frame size: %dx%d", w, h);
447 if (lf_width <= 0 || lf_height <= 0)
448 die(
"Invalid lf_width and/or lf_height: %dx%d", lf_width, lf_height);
449 if (lf_blocksize <= 0) die(
"Invalid lf_blocksize: %d", lf_blocksize);
452 die(
"Failed to allocate image.");
454 if (!CONFIG_LOWBITDEPTH) {
464 if (res) die_codec(&codec,
"Failed to get default codec config.");
480 if (!(infile = fopen(infile_arg,
"rb")))
481 die(
"Failed to open %s for reading", infile_arg);
485 stats = pass0(&raw, infile, encoder, &cfg, lf_width, lf_height, lf_blocksize,
492 pass1(&raw, infile, outfile_arg, encoder, &cfg, lf_width, lf_height,
493 lf_blocksize, flags, &raw_shift);
void * buf
Definition: aom_encoder.h:85
Operation completed without error.
Definition: aom_codec.h:103
Definition: aom_encoder.h:196
unsigned int g_w
Width of the frame.
Definition: aom_encoder.h:276
unsigned int rc_target_bitrate
Target data rate.
Definition: aom_encoder.h:491
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:774
Encoder configuration structure.
Definition: aom_encoder.h:237
int idx
Definition: aom.h:109
enum aom_kf_mode kf_mode
Keyframe placement mode.
Definition: aom_encoder.h:621
aom_fixed_buf_t rc_twopass_stats_in
Two-pass stats buffer.
Definition: aom_encoder.h:478
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
int64_t aom_codec_pts_t
Time Stamp Type.
Definition: aom_encoder.h:94
Codec control function to set constrained quality level.
Definition: aomcx.h:231
aom_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: aom_encoder.h:342
Provides definitions for using AOM or AV1 encoder algorithm within the aom Codec Interface.
#define AOM_EFLAG_NO_UPD_LAST
Don't update the last frame.
Definition: aomcx.h:100
Codec context structure.
Definition: aom_codec.h:204
#define AOM_FRAME_IS_KEY
Definition: aom_encoder.h:104
Codec control function to enable frame parallel decoding feature.
Definition: aomcx.h:338
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition: aom_image.h:38
aom_codec_err_t aom_codec_enc_config_default(aom_codec_iface_t *iface, aom_codec_enc_cfg_t *cfg, unsigned int reserved)
Get a default configuration.
Image Descriptor.
Definition: aom_image.h:141
Codec control function to set number of tile columns.
Definition: aomcx.h:308
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.
enum aom_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: aom_encoder.h:471
Generic fixed size buffer structure.
Definition: aom_encoder.h:84
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.
#define AOM_EFLAG_NO_REF_LAST3
Don't reference the last3 frame.
Definition: aomcx.h:64
Definition: aom_codec.h:225
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:334
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
Definition: aom_encoder.h:136
aom_image_t * aom_img_alloc_with_border(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align, unsigned int size_align, unsigned int border)
Open a descriptor, allocating storage for the underlying image with a border.
enum aom_codec_cx_pkt_kind kind
Definition: aom_encoder.h:148
void aom_img_free(aom_image_t *img)
Close an image descriptor.
struct aom_codec_cx_pkt::@1::@2 frame
Definition: aom_encoder.h:135
#define AOM_EFLAG_NO_UPD_GF
Don't update the golden frame.
Definition: aomcx.h:107
#define aom_codec_control(ctx, id, data)
aom_codec_control wrapper macro
Definition: aom_codec.h:423
int aom_img_plane_width(const aom_image_t *img, int plane)
Get the width of a plane.
#define AOM_EFLAG_NO_REF_GF
Don't reference the golden frame.
Definition: aomcx.h:71
#define AOM_CODEC_USE_HIGHBITDEPTH
Definition: aom_encoder.h:78
unsigned int large_scale_tile
Tile coding mode.
Definition: aom_encoder.h:669
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.
AV1 specific reference frame data struct.
Definition: aom.h:108
int aom_img_plane_height(const aom_image_t *img, int plane)
Get the height of a plane.
#define AOM_EFLAG_NO_UPD_ARF
Don't update the alternate reference frame.
Definition: aomcx.h:114
Definition: aom_codec.h:237
Codec control function to enable automatic set and use alf frames.
Definition: aomcx.h:190
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
#define AOM_EFLAG_NO_REF_ARF2
Don't reference the alt2 reference frame.
Definition: aomcx.h:93
int use_external_ref
Definition: aom.h:110
#define AOM_EFLAG_NO_UPD_ENTROPY
Disable entropy update.
Definition: aomcx.h:120
Codec control function to set number of tile rows.
Definition: aomcx.h:326
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:101
aom_fixed_buf_t twopass_stats
Definition: aom_encoder.h:165
Definition: aom_encoder.h:204
int den
Definition: aom_encoder.h:189
aom_image_t img
Definition: aom.h:111
Encoder output packet.
Definition: aom_encoder.h:147
#define AOM_EFLAG_NO_REF_ARF
Don't reference the alternate reference frame.
Definition: aomcx.h:79
int num
Definition: aom_encoder.h:188
Definition: aom_image.h:45
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: aom_encoder.h:363
Codec control function to set the single tile decoding mode to 0 or 1.
Definition: aomcx.h:832
long aom_enc_frame_flags_t
Encoded Frame Flags.
Definition: aom_encoder.h:228
union aom_codec_cx_pkt::@1 data
aom_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition: aom_encoder.h:312
#define AOM_EFLAG_NO_REF_LAST2
Don't reference the last2 frame.
Definition: aomcx.h:57
size_t sz
Definition: aom_encoder.h:86
Codec control function to set intended superblock size.
Definition: aomcx.h:530
Definition: aom_encoder.h:195
Definition: aom_encoder.h:218
enum aom_enc_pass g_pass
Multi-pass Encoding Mode.
Definition: aom_encoder.h:349
unsigned int g_h
Height of the frame.
Definition: aom_encoder.h:285
aom_img_fmt_t fmt
Definition: aom_image.h:142
#define AOM_EFLAG_NO_REF_BWD
Don't reference the bwd reference frame.
Definition: aomcx.h:86