40 #include "aom_scale/yv12config.h"
41 #include "av1/common/enums.h"
42 #include "common/tools_common.h"
43 #include "common/video_writer.h"
45 static const char *exec_name;
47 void usage_exit(
void) {
49 "Usage: %s <img_width> <img_height> <infile> <outfile> "
50 "<lf_width> <lf_height> <lf_blocksize>\n",
56 int image_size_bytes = 0;
58 for (plane = 0; plane < 3; ++plane) {
62 image_size_bytes += w * h;
64 return image_size_bytes;
75 if (res !=
AOM_CODEC_OK) die_codec(ctx,
"Failed to get frame stats.");
83 stats->
buf = realloc(stats->
buf, stats->
sz + pkt_size);
84 memcpy((uint8_t *)stats->
buf + stats->
sz, pkt_buf, pkt_size);
85 stats->
sz += pkt_size;
99 if (res !=
AOM_CODEC_OK) die_codec(ctx,
"Failed to encode frame.");
106 if (!aom_video_writer_write_frame(writer, pkt->
data.
frame.buf,
109 die_codec(ctx,
"Failed to write compressed frame.");
110 printf(keyframe ?
"K" :
".");
120 if (FORCE_HIGHBITDEPTH_DECODING) {
123 aom_img_upshift(raw_shift, raw, input_shift);
124 *frame_to_encode = raw_shift;
126 *frame_to_encode = raw;
131 const AvxInterface *encoder,
133 int lf_height,
int lf_blocksize,
int flags,
137 int image_size_bytes = img_size_bytes(raw);
138 int u_blocks, v_blocks;
144 die_codec(&codec,
"Failed to initialize encoder");
146 die_codec(&codec,
"Failed to turn off auto altref");
148 die_codec(&codec,
"Failed to set frame parallel decoding");
151 u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize;
152 v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize;
154 printf(
"\n First pass: ");
156 for (bv = 0; bv < v_blocks; ++bv) {
157 for (bu = 0; bu < u_blocks; ++bu) {
158 const int block_u_min = bu * lf_blocksize;
159 const int block_v_min = bv * lf_blocksize;
160 int block_u_end = (bu + 1) * lf_blocksize;
161 int block_v_end = (bv + 1) * lf_blocksize;
162 int u_block_size, v_block_size;
163 int block_ref_u, block_ref_v;
165 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
166 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
167 u_block_size = block_u_end - block_u_min;
168 v_block_size = block_v_end - block_v_min;
169 block_ref_u = block_u_min + u_block_size / 2;
170 block_ref_v = block_v_min + v_block_size / 2;
172 printf(
"A%d, ", (block_ref_u + block_ref_v * lf_width));
173 fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes,
175 aom_img_read(raw, infile);
176 get_raw_image(&frame_to_encode, raw, raw_shift);
180 get_frame_stats(&codec, frame_to_encode, frame_count, 1,
191 die_codec(&codec,
"Failed to set frame parallel decoding");
193 for (bv = 0; bv < v_blocks; ++bv) {
194 for (bu = 0; bu < u_blocks; ++bu) {
195 const int block_u_min = bu * lf_blocksize;
196 const int block_v_min = bv * lf_blocksize;
197 int block_u_end = (bu + 1) * lf_blocksize;
198 int block_v_end = (bv + 1) * lf_blocksize;
200 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
201 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
202 for (v = block_v_min; v < block_v_end; ++v) {
203 for (u = block_u_min; u < block_u_end; ++u) {
204 printf(
"C%d, ", (u + v * lf_width));
205 fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET);
206 aom_img_read(raw, infile);
207 get_raw_image(&frame_to_encode, raw, raw_shift);
210 get_frame_stats(&codec, frame_to_encode, frame_count, 1,
223 while (get_frame_stats(&codec, NULL, frame_count, 1, 0, &stats)) {
228 printf(
"\nFirst pass complete. Processed %d frames.\n", frame_count);
233 static void pass1(
aom_image_t *raw, FILE *infile,
const char *outfile_name,
235 int lf_width,
int lf_height,
int lf_blocksize,
int flags,
237 AvxVideoInfo info = { encoder->fourcc,
242 AvxVideoWriter *writer = NULL;
245 int image_size_bytes = img_size_bytes(raw);
247 int u_blocks, v_blocks;
249 aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
250 int reference_image_num = 0;
253 writer = aom_video_writer_open(outfile_name, kContainerIVF, &info);
254 if (!writer) die(
"Failed to open %s for writing", outfile_name);
257 die_codec(&codec,
"Failed to initialize encoder");
259 die_codec(&codec,
"Failed to turn off auto altref");
261 die_codec(&codec,
"Failed to set frame parallel decoding");
263 die_codec(&codec,
"Failed to enable encoder ext_tile debug");
265 die_codec(&codec,
"Failed to set cpu-used");
274 die_codec(&codec,
"Failed to set SB size");
276 u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize;
277 v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize;
279 reference_image_num = u_blocks * v_blocks;
288 reference_image_num - 1))
289 die_codec(&codec,
"Failed to set max gf interval");
293 int border_in_pixels =
295 ? AOM_BORDER_IN_PIXELS
296 : AOM_ENC_NO_SCALE_BORDER;
297 for (i = 0; i < reference_image_num; i++) {
299 cfg->
g_h, 32, 8, border_in_pixels)) {
300 die(
"Failed to allocate image.");
304 printf(
"\n Second pass: ");
307 printf(
"Encoding Reference Images\n");
308 for (bv = 0; bv < v_blocks; ++bv) {
309 for (bu = 0; bu < u_blocks; ++bu) {
310 const int block_u_min = bu * lf_blocksize;
311 const int block_v_min = bv * lf_blocksize;
312 int block_u_end = (bu + 1) * lf_blocksize;
313 int block_v_end = (bv + 1) * lf_blocksize;
314 int u_block_size, v_block_size;
315 int block_ref_u, block_ref_v;
317 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
318 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
319 u_block_size = block_u_end - block_u_min;
320 v_block_size = block_v_end - block_v_min;
321 block_ref_u = block_u_min + u_block_size / 2;
322 block_ref_v = block_v_min + v_block_size / 2;
324 printf(
"A%d, ", (block_ref_u + block_ref_v * lf_width));
325 fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes,
327 aom_img_read(raw, infile);
329 get_raw_image(&frame_to_encode, raw, raw_shift);
333 printf(
"Encoding reference image %d of %d\n", bv * u_blocks + bu,
334 u_blocks * v_blocks);
335 encode_frame(&codec, frame_to_encode, frame_count, 1,
344 &reference_images[frame_count - 1]))
345 die_codec(&codec,
"Failed to copy decoder reference frame");
353 die_codec(&codec,
"Failed to configure encoder");
357 die_codec(&codec,
"Failed to set cq level");
359 die_codec(&codec,
"Failed to set frame parallel decoding");
361 die_codec(&codec,
"Failed to turn on single tile decoding");
365 die_codec(&codec,
"Failed to set tile width");
367 die_codec(&codec,
"Failed to set tile height");
369 for (bv = 0; bv < v_blocks; ++bv) {
370 for (bu = 0; bu < u_blocks; ++bu) {
371 const int block_u_min = bu * lf_blocksize;
372 const int block_v_min = bv * lf_blocksize;
373 int block_u_end = (bu + 1) * lf_blocksize;
374 int block_v_end = (bv + 1) * lf_blocksize;
376 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
377 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
378 for (v = block_v_min; v < block_v_end; ++v) {
379 for (u = block_u_min; u < block_u_end; ++u) {
383 ref.
img = reference_images[bv * u_blocks + bu];
385 die_codec(&codec,
"Failed to set reference frame");
387 printf(
"C%d, ", (u + v * lf_width));
388 fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET);
389 aom_img_read(raw, infile);
390 get_raw_image(&frame_to_encode, raw, raw_shift);
393 printf(
"Encoding image %d of %d\n",
394 frame_count - (u_blocks * v_blocks), lf_width * lf_height);
395 encode_frame(&codec, frame_to_encode, frame_count, 1,
409 while (encode_frame(&codec, NULL, -1, 1, 0, writer)) {
412 for (i = 0; i < reference_image_num; i++)
aom_img_free(&reference_images[i]);
418 aom_video_writer_set_fourcc(writer, LST_FOURCC);
419 aom_video_writer_close(writer);
421 printf(
"\nSecond pass complete. Processed %d frames.\n", frame_count);
424 int main(
int argc,
char **argv) {
428 int lf_width, lf_height;
441 const AvxInterface *encoder = NULL;
443 const int bitrate = 200;
444 const char *
const width_arg = argv[1];
445 const char *
const height_arg = argv[2];
446 const char *
const infile_arg = argv[3];
447 const char *
const outfile_arg = argv[4];
448 const char *
const lf_width_arg = argv[5];
449 const char *
const lf_height_arg = argv[6];
450 const char *lf_blocksize_arg = argv[7];
453 if (argc < 8) die(
"Invalid number of arguments");
455 encoder = get_aom_encoder_by_name(
"av1");
456 if (!encoder) die(
"Unsupported codec.");
458 w = (int)strtol(width_arg, NULL, 0);
459 h = (int)strtol(height_arg, NULL, 0);
460 lf_width = (int)strtol(lf_width_arg, NULL, 0);
461 lf_height = (int)strtol(lf_height_arg, NULL, 0);
462 lf_blocksize = (int)strtol(lf_blocksize_arg, NULL, 0);
463 lf_blocksize = lf_blocksize < lf_width ? lf_blocksize : lf_width;
464 lf_blocksize = lf_blocksize < lf_height ? lf_blocksize : lf_height;
466 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
467 die(
"Invalid frame size: %dx%d", w, h);
468 if (lf_width <= 0 || lf_height <= 0)
469 die(
"Invalid lf_width and/or lf_height: %dx%d", lf_width, lf_height);
470 if (lf_blocksize <= 0) die(
"Invalid lf_blocksize: %d", lf_blocksize);
473 die(
"Failed to allocate image.");
475 if (FORCE_HIGHBITDEPTH_DECODING) {
485 if (res) die_codec(&codec,
"Failed to get default codec config.");
501 if (!(infile = fopen(infile_arg,
"rb")))
502 die(
"Failed to open %s for reading", infile_arg);
506 stats = pass0(&raw, infile, encoder, &cfg, lf_width, lf_height, lf_blocksize,
513 pass1(&raw, infile, outfile_arg, encoder, &cfg, lf_width, lf_height,
514 lf_blocksize, flags, &raw_shift);
517 if (FORCE_HIGHBITDEPTH_DECODING)
aom_img_free(&raw_shift);
Describes the encoder algorithm interface to applications.
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.
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition: aom_image.h:38
@ AOM_IMG_FMT_I420
Definition: aom_image.h:45
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
int aom_img_plane_height(const aom_image_t *img, int plane)
Get the height of a plane.
int aom_img_plane_width(const aom_image_t *img, int plane)
Get the width of a plane.
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.
void aom_img_free(aom_image_t *img)
Close an image descriptor.
Provides definitions for using AOM or AV1 encoder algorithm within the aom Codec Interface.
#define AOM_EFLAG_NO_UPD_ARF
Don't update the alternate reference frame.
Definition: aomcx.h:114
#define AOM_EFLAG_NO_REF_LAST2
Don't reference the last2 frame.
Definition: aomcx.h:57
#define AOM_EFLAG_NO_REF_BWD
Don't reference the bwd reference frame.
Definition: aomcx.h:86
#define AOM_EFLAG_NO_UPD_LAST
Don't update the last frame.
Definition: aomcx.h:100
#define AOM_EFLAG_NO_REF_ARF
Don't reference the alternate reference frame.
Definition: aomcx.h:79
#define AOM_EFLAG_NO_REF_LAST3
Don't reference the last3 frame.
Definition: aomcx.h:64
#define AOM_EFLAG_NO_UPD_GF
Don't update the golden frame.
Definition: aomcx.h:107
#define AOM_EFLAG_NO_REF_GF
Don't reference the golden frame.
Definition: aomcx.h:71
#define AOM_EFLAG_NO_UPD_ENTROPY
Disable entropy update.
Definition: aomcx.h:120
#define AOM_EFLAG_NO_REF_ARF2
Don't reference the alt2 reference frame.
Definition: aomcx.h:93
@ AV1E_SET_MAX_GF_INTERVAL
Codec control function to set minimum interval between GF/ARF frames, unsigned int parameter.
Definition: aomcx.h:551
@ AV1E_SET_FRAME_PARALLEL_DECODING
Codec control function to enable frame parallel decoding feature, unsigned int parameter.
Definition: aomcx.h:391
@ AV1E_SET_TILE_ROWS
Codec control function to set number of tile rows, unsigned int parameter.
Definition: aomcx.h:361
@ AV1E_SET_SUPERBLOCK_SIZE
Codec control function to set intended superblock size, unsigned int parameter.
Definition: aomcx.h:604
@ AOME_SET_ENABLEAUTOALTREF
Codec control function to enable automatic set and use alf frames, unsigned int parameter.
Definition: aomcx.h:200
@ AV1E_SET_TILE_COLUMNS
Codec control function to set number of tile columns. unsigned int parameter.
Definition: aomcx.h:343
@ AOME_SET_CPUUSED
Codec control function to set encoder internal speed settings, int parameter.
Definition: aomcx.h:192
@ AV1E_SET_SINGLE_TILE_DECODING
Codec control function to set the single tile decoding mode, unsigned int parameter.
Definition: aomcx.h:1084
@ AOME_SET_CQ_LEVEL
Codec control function to set constrained / constant quality level, unsigned int parameter.
Definition: aomcx.h:255
@ AV1E_ENABLE_EXT_TILE_DEBUG
Codec control function to enable EXT_TILE_DEBUG in AV1 encoder, unsigned int parameter.
Definition: aomcx.h:1240
@ AV1_SET_REFERENCE
Definition: aom.h:53
@ AV1_COPY_NEW_FRAME_IMAGE
Definition: aom.h:61
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id,...)
Algorithm Control.
int64_t aom_codec_pts_t
Time Stamp Type.
Definition: aom_codec.h:181
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:101
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:209
@ AOM_BITS_8
Definition: aom_codec.h:240
@ AOM_CODEC_OK
Operation completed without error.
Definition: aom_codec.h:103
@ AOM_SUPERBLOCK_SIZE_64X64
Definition: aom_codec.h:252
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_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_enc_init_ver()
Definition: aom_encoder.h:941
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.
long aom_enc_frame_flags_t
Encoded Frame Flags.
Definition: aom_encoder.h:360
#define AOM_CODEC_USE_HIGHBITDEPTH
Make the encoder output one partition at a time.
Definition: aom_encoder.h:70
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_enc_config_set(aom_codec_ctx_t *ctx, const aom_codec_enc_cfg_t *cfg)
Set or change configuration.
#define AOM_FRAME_IS_KEY
Definition: aom_encoder.h:89
@ AOM_Q
Definition: aom_encoder.h:188
@ AOM_RC_LAST_PASS
Definition: aom_encoder.h:180
@ AOM_RC_FIRST_PASS
Definition: aom_encoder.h:179
@ AOM_KF_DISABLED
Definition: aom_encoder.h:202
@ AOM_CODEC_CX_FRAME_PKT
Definition: aom_encoder.h:119
@ AOM_CODEC_STATS_PKT
Definition: aom_encoder.h:120
Codec context structure.
Definition: aom_codec.h:219
union aom_codec_ctx::@0 config
Encoder output packet.
Definition: aom_encoder.h:131
enum aom_codec_cx_pkt_kind kind
Definition: aom_encoder.h:132
aom_fixed_buf_t twopass_stats
Definition: aom_encoder.h:149
union aom_codec_cx_pkt::@1 data
struct aom_codec_cx_pkt::@1::@2 frame
Encoder configuration structure.
Definition: aom_encoder.h:369
struct aom_rational g_timebase
Stream timebase units.
Definition: aom_encoder.h:466
unsigned int g_h
Height of the frame.
Definition: aom_encoder.h:417
enum aom_kf_mode kf_mode
Keyframe placement mode.
Definition: aom_encoder.h:752
enum aom_rc_mode rc_end_usage
Rate control algorithm to use.
Definition: aom_encoder.h:602
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition: aom_encoder.h:495
unsigned int rc_superres_mode
Frame super-resolution scaling mode.
Definition: aom_encoder.h:552
aom_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition: aom_encoder.h:444
unsigned int g_w
Width of the frame.
Definition: aom_encoder.h:408
aom_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition: aom_encoder.h:474
enum aom_enc_pass g_pass
Multi-pass Encoding Mode.
Definition: aom_encoder.h:481
unsigned int rc_target_bitrate
Target data rate.
Definition: aom_encoder.h:622
unsigned int rc_resize_mode
Mode for spatial resampling, if supported by the codec.
Definition: aom_encoder.h:526
aom_fixed_buf_t rc_twopass_stats_in
Two-pass stats buffer.
Definition: aom_encoder.h:609
unsigned int large_scale_tile
Tile coding mode.
Definition: aom_encoder.h:800
Generic fixed size buffer structure.
Definition: aom_encoder.h:76
size_t sz
Definition: aom_encoder.h:78
void * buf
Definition: aom_encoder.h:77
Image Descriptor.
Definition: aom_image.h:171
aom_img_fmt_t fmt
Definition: aom_image.h:172
int num
Definition: aom_encoder.h:172
int den
Definition: aom_encoder.h:173
AV1 specific reference frame data struct.
Definition: aom.h:71
int use_external_ref
Definition: aom.h:73
aom_image_t img
Definition: aom.h:74
int idx
Definition: aom.h:72