40#include "aom_scale/yv12config.h"
42#include "av1/encoder/encoder_utils.h"
43#include "common/tools_common.h"
44#include "common/video_writer.h"
46static const char *exec_name;
48void usage_exit(
void) {
50 "Usage: %s <img_width> <img_height> <infile> <outfile> "
51 "<lf_width> <lf_height> <lf_blocksize>\n",
57 int image_size_bytes = 0;
59 for (plane = 0; plane < 3; ++plane) {
63 image_size_bytes += w * h;
65 return image_size_bytes;
76 if (res !=
AOM_CODEC_OK) die_codec(ctx,
"Failed to get frame stats.");
84 stats->
buf = realloc(stats->
buf, stats->
sz + pkt_size);
85 if (!stats->
buf) die(
"Failed to allocate frame stats buffer.");
86 memcpy((uint8_t *)stats->
buf + stats->
sz, pkt_buf, pkt_size);
87 stats->
sz += pkt_size;
101 if (res !=
AOM_CODEC_OK) die_codec(ctx,
"Failed to encode frame.");
108 if (!aom_video_writer_write_frame(writer, pkt->
data.
frame.
buf,
111 die_codec(ctx,
"Failed to write compressed frame.");
112 printf(keyframe ?
"K" :
".");
122 if (FORCE_HIGHBITDEPTH_DECODING) {
125 aom_img_upshift(raw_shift, raw, input_shift);
126 *frame_to_encode = raw_shift;
128 *frame_to_encode = raw;
135 int lf_height,
int lf_blocksize,
int flags,
139 int image_size_bytes = img_size_bytes(raw);
140 int u_blocks, v_blocks;
146 die(
"Failed to initialize encoder");
148 die_codec(&codec,
"Failed to turn off auto altref");
150 die_codec(&codec,
"Failed to set frame parallel decoding");
153 u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize;
154 v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize;
156 printf(
"\n First pass: ");
158 for (bv = 0; bv < v_blocks; ++bv) {
159 for (bu = 0; bu < u_blocks; ++bu) {
160 const int block_u_min = bu * lf_blocksize;
161 const int block_v_min = bv * lf_blocksize;
162 int block_u_end = (bu + 1) * lf_blocksize;
163 int block_v_end = (bv + 1) * lf_blocksize;
164 int u_block_size, v_block_size;
165 int block_ref_u, block_ref_v;
167 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
168 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
169 u_block_size = block_u_end - block_u_min;
170 v_block_size = block_v_end - block_v_min;
171 block_ref_u = block_u_min + u_block_size / 2;
172 block_ref_v = block_v_min + v_block_size / 2;
174 printf(
"A%d, ", (block_ref_u + block_ref_v * lf_width));
175 fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes,
177 aom_img_read(raw, infile);
178 get_raw_image(&frame_to_encode, raw, raw_shift);
182 get_frame_stats(&codec, frame_to_encode, frame_count, 1,
193 die_codec(&codec,
"Failed to set frame parallel decoding");
195 for (bv = 0; bv < v_blocks; ++bv) {
196 for (bu = 0; bu < u_blocks; ++bu) {
197 const int block_u_min = bu * lf_blocksize;
198 const int block_v_min = bv * lf_blocksize;
199 int block_u_end = (bu + 1) * lf_blocksize;
200 int block_v_end = (bv + 1) * lf_blocksize;
202 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
203 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
204 for (v = block_v_min; v < block_v_end; ++v) {
205 for (u = block_u_min; u < block_u_end; ++u) {
206 printf(
"C%d, ", (u + v * lf_width));
207 fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET);
208 aom_img_read(raw, infile);
209 get_raw_image(&frame_to_encode, raw, raw_shift);
212 get_frame_stats(&codec, frame_to_encode, frame_count, 1,
225 while (get_frame_stats(&codec, NULL, frame_count, 1, 0, &stats)) {
230 printf(
"\nFirst pass complete. Processed %d frames.\n", frame_count);
235static void pass1(
aom_image_t *raw, FILE *infile,
const char *outfile_name,
237 int lf_width,
int lf_height,
int lf_blocksize,
int flags,
239 AvxVideoInfo info = { get_fourcc_by_aom_encoder(encoder),
244 AvxVideoWriter *writer = NULL;
247 int image_size_bytes = 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(
"Failed to initialize encoder");
261 die_codec(&codec,
"Failed to turn off auto altref");
263 die_codec(&codec,
"Failed to set frame parallel decoding");
265 die_codec(&codec,
"Failed to enable encoder ext_tile debug");
267 die_codec(&codec,
"Failed to set cpu-used");
276 die_codec(&codec,
"Failed to set SB size");
278 u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize;
279 v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize;
281 reference_image_num = u_blocks * v_blocks;
290 reference_image_num - 1))
291 die_codec(&codec,
"Failed to set max gf interval");
297 const bool all_intra = reference_image_num - 1 == 0;
298 int border_in_pixels =
299 av1_get_enc_border_size(resize, all_intra, BLOCK_64X64);
301 for (i = 0; i < reference_image_num; i++) {
303 cfg->
g_h, 32, 8, border_in_pixels)) {
304 die(
"Failed to allocate image.");
308 printf(
"\n Second pass: ");
311 printf(
"Encoding Reference Images\n");
312 for (bv = 0; bv < v_blocks; ++bv) {
313 for (bu = 0; bu < u_blocks; ++bu) {
314 const int block_u_min = bu * lf_blocksize;
315 const int block_v_min = bv * lf_blocksize;
316 int block_u_end = (bu + 1) * lf_blocksize;
317 int block_v_end = (bv + 1) * lf_blocksize;
318 int u_block_size, v_block_size;
319 int block_ref_u, block_ref_v;
321 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
322 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
323 u_block_size = block_u_end - block_u_min;
324 v_block_size = block_v_end - block_v_min;
325 block_ref_u = block_u_min + u_block_size / 2;
326 block_ref_v = block_v_min + v_block_size / 2;
328 printf(
"A%d, ", (block_ref_u + block_ref_v * lf_width));
329 fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes,
331 aom_img_read(raw, infile);
333 get_raw_image(&frame_to_encode, raw, raw_shift);
337 printf(
"Encoding reference image %d of %d\n", bv * u_blocks + bu,
338 u_blocks * v_blocks);
339 encode_frame(&codec, frame_to_encode, frame_count, 1,
348 &reference_images[frame_count - 1]))
349 die_codec(&codec,
"Failed to copy decoder reference frame");
357 die_codec(&codec,
"Failed to configure encoder");
361 die_codec(&codec,
"Failed to set cq level");
363 die_codec(&codec,
"Failed to set frame parallel decoding");
365 die_codec(&codec,
"Failed to turn on single tile decoding");
369 die_codec(&codec,
"Failed to set tile width");
371 die_codec(&codec,
"Failed to set tile height");
373 for (bv = 0; bv < v_blocks; ++bv) {
374 for (bu = 0; bu < u_blocks; ++bu) {
375 const int block_u_min = bu * lf_blocksize;
376 const int block_v_min = bv * lf_blocksize;
377 int block_u_end = (bu + 1) * lf_blocksize;
378 int block_v_end = (bv + 1) * lf_blocksize;
380 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
381 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
382 for (v = block_v_min; v < block_v_end; ++v) {
383 for (u = block_u_min; u < block_u_end; ++u) {
387 ref.
img = reference_images[bv * u_blocks + bu];
389 die_codec(&codec,
"Failed to set reference frame");
391 printf(
"C%d, ", (u + v * lf_width));
392 fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET);
393 aom_img_read(raw, infile);
394 get_raw_image(&frame_to_encode, raw, raw_shift);
397 printf(
"Encoding image %d of %d\n",
398 frame_count - (u_blocks * v_blocks), lf_width * lf_height);
399 encode_frame(&codec, frame_to_encode, frame_count, 1,
413 while (encode_frame(&codec, NULL, -1, 1, 0, writer)) {
416 for (i = 0; i < reference_image_num; i++)
aom_img_free(&reference_images[i]);
422 aom_video_writer_set_fourcc(writer, LST_FOURCC);
423 aom_video_writer_close(writer);
425 printf(
"\nSecond pass complete. Processed %d frames.\n", frame_count);
428int main(
int argc,
char **argv) {
432 int lf_width, lf_height;
446 const int bitrate = 200;
447 const char *
const width_arg = argv[1];
448 const char *
const height_arg = argv[2];
449 const char *
const infile_arg = argv[3];
450 const char *
const outfile_arg = argv[4];
451 const char *
const lf_width_arg = argv[5];
452 const char *
const lf_height_arg = argv[6];
453 const char *lf_blocksize_arg = argv[7];
456 if (argc < 8) die(
"Invalid number of arguments");
459 if (!encoder) die(
"Unsupported codec.");
461 w = (int)strtol(width_arg, NULL, 0);
462 h = (int)strtol(height_arg, NULL, 0);
463 lf_width = (int)strtol(lf_width_arg, NULL, 0);
464 lf_height = (int)strtol(lf_height_arg, NULL, 0);
465 lf_blocksize = (int)strtol(lf_blocksize_arg, NULL, 0);
466 lf_blocksize = lf_blocksize < lf_width ? lf_blocksize : lf_width;
467 lf_blocksize = lf_blocksize < lf_height ? lf_blocksize : lf_height;
469 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
470 die(
"Invalid frame size: %dx%d", w, h);
471 if (lf_width <= 0 || lf_height <= 0)
472 die(
"Invalid lf_width and/or lf_height: %dx%d", lf_width, lf_height);
473 if (lf_blocksize <= 0) die(
"Invalid lf_blocksize: %d", lf_blocksize);
476 die(
"Failed to allocate image.");
478 if (FORCE_HIGHBITDEPTH_DECODING) {
488 if (res) die_codec(&codec,
"Failed to get default codec config.");
504 if (!(infile = fopen(infile_arg,
"rb")))
505 die(
"Failed to open %s for reading", infile_arg);
509 stats = pass0(&raw, infile, encoder, &cfg, lf_width, lf_height, lf_blocksize,
516 pass1(&raw, infile, outfile_arg, encoder, &cfg, lf_width, lf_height,
517 lf_blocksize, flags, &raw_shift);
520 if (FORCE_HIGHBITDEPTH_DECODING)
aom_img_free(&raw_shift);
Describes the encoder algorithm interface to applications.
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.
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition aom_image.h:38
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
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.
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:136
#define AOM_EFLAG_NO_REF_LAST2
Don't reference the last2 frame.
Definition aomcx.h:79
#define AOM_EFLAG_NO_REF_BWD
Don't reference the bwd reference frame.
Definition aomcx.h:108
#define AOM_EFLAG_NO_UPD_LAST
Don't update the last frame.
Definition aomcx.h:122
#define AOM_EFLAG_NO_REF_ARF
Don't reference the alternate reference frame.
Definition aomcx.h:101
#define AOM_EFLAG_NO_REF_LAST3
Don't reference the last3 frame.
Definition aomcx.h:86
#define AOM_EFLAG_NO_UPD_GF
Don't update the golden frame.
Definition aomcx.h:129
#define AOM_EFLAG_NO_REF_GF
Don't reference the golden frame.
Definition aomcx.h:93
#define AOM_EFLAG_NO_UPD_ENTROPY
Disable entropy update.
Definition aomcx.h:142
#define AOM_EFLAG_NO_REF_ARF2
Don't reference the alt2 reference frame.
Definition aomcx.h:115
@ AV1E_SET_MAX_GF_INTERVAL
Codec control function to set minimum interval between GF/ARF frames, unsigned int parameter.
Definition aomcx.h:594
@ AV1E_SET_FRAME_PARALLEL_DECODING
Codec control function to enable frame parallel decoding feature, unsigned int parameter.
Definition aomcx.h:431
@ AV1E_SET_TILE_ROWS
Codec control function to set number of tile rows, unsigned int parameter.
Definition aomcx.h:398
@ AV1E_SET_SUPERBLOCK_SIZE
Codec control function to set intended superblock size, unsigned int parameter.
Definition aomcx.h:651
@ AOME_SET_ENABLEAUTOALTREF
Codec control function to enable automatic set and use alf frames, unsigned int parameter.
Definition aomcx.h:228
@ AV1E_SET_TILE_COLUMNS
Codec control function to set number of tile columns. unsigned int parameter.
Definition aomcx.h:380
@ AOME_SET_CPUUSED
Codec control function to set encoder internal speed settings, int parameter.
Definition aomcx.h:220
@ AV1E_SET_SINGLE_TILE_DECODING
Codec control function to set the single tile decoding mode, unsigned int parameter.
Definition aomcx.h:1149
@ AOME_SET_CQ_LEVEL
Codec control function to set constrained / constant quality level, unsigned int parameter.
Definition aomcx.h:292
@ AV1E_ENABLE_EXT_TILE_DEBUG
Codec control function to enable EXT_TILE_DEBUG in AV1 encoder, unsigned int parameter.
Definition aomcx.h:1301
@ AV1_SET_REFERENCE
Codec control function to write a frame into a reference buffer.
Definition aom.h:57
@ AV1_COPY_NEW_FRAME_IMAGE
Codec control function to copy the new frame to an external buffer.
Definition aom.h:76
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.
const struct aom_codec_iface aom_codec_iface_t
Codec interface structure.
Definition aom_codec.h:271
int64_t aom_codec_pts_t
Time Stamp Type.
Definition aom_codec.h:252
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:155
const void * aom_codec_iter_t
Iterator.
Definition aom_codec.h:305
#define AOM_FRAME_IS_KEY
Definition aom_codec.h:288
@ AOM_BITS_8
Definition aom_codec.h:336
@ AOM_CODEC_OK
Operation completed without error.
Definition aom_codec.h:157
@ AOM_SUPERBLOCK_SIZE_64X64
Definition aom_codec.h:348
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_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_enc_init_ver()
Definition aom_encoder.h:943
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:377
#define AOM_CODEC_USE_HIGHBITDEPTH
Definition aom_encoder.h:80
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
Definition aom_encoder.h:189
@ AOM_RC_LAST_PASS
Definition aom_encoder.h:181
@ AOM_RC_FIRST_PASS
Definition aom_encoder.h:178
@ AOM_KF_DISABLED
Definition aom_encoder.h:203
@ AOM_CODEC_CX_FRAME_PKT
Definition aom_encoder.h:110
@ AOM_CODEC_STATS_PKT
Definition aom_encoder.h:111
Codec context structure.
Definition aom_codec.h:315
union aom_codec_ctx::@0 config
Encoder output packet.
Definition aom_encoder.h:122
size_t sz
Definition aom_encoder.h:127
enum aom_codec_cx_pkt_kind kind
Definition aom_encoder.h:123
aom_fixed_buf_t twopass_stats
Definition aom_encoder.h:140
union aom_codec_cx_pkt::@1 data
aom_codec_pts_t pts
time stamp to show frame (in timebase units)
Definition aom_encoder.h:129
struct aom_codec_cx_pkt::@1::@2 frame
aom_codec_frame_flags_t flags
Definition aom_encoder.h:132
void * buf
Definition aom_encoder.h:126
Encoder configuration structure.
Definition aom_encoder.h:387
struct aom_rational g_timebase
Stream timebase units.
Definition aom_encoder.h:489
unsigned int g_h
Height of the frame.
Definition aom_encoder.h:435
aom_superres_mode rc_superres_mode
Frame super-resolution scaling mode.
Definition aom_encoder.h:573
enum aom_kf_mode kf_mode
Keyframe placement mode.
Definition aom_encoder.h:768
enum aom_rc_mode rc_end_usage
Rate control algorithm to use.
Definition aom_encoder.h:623
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition aom_encoder.h:518
aom_bit_depth_t g_bit_depth
Bit-depth of the codec.
Definition aom_encoder.h:467
unsigned int g_w
Width of the frame.
Definition aom_encoder.h:426
aom_codec_er_flags_t g_error_resilient
Enable error resilient modes.
Definition aom_encoder.h:497
enum aom_enc_pass g_pass
Multi-pass Encoding Mode.
Definition aom_encoder.h:504
unsigned int rc_target_bitrate
Target data rate.
Definition aom_encoder.h:644
unsigned int rc_resize_mode
Mode for spatial resampling, if supported by the codec.
Definition aom_encoder.h:549
aom_fixed_buf_t rc_twopass_stats_in
Two-pass stats buffer.
Definition aom_encoder.h:630
unsigned int large_scale_tile
Tile coding mode.
Definition aom_encoder.h:818
Generic fixed size buffer structure.
Definition aom_encoder.h:88
size_t sz
Definition aom_encoder.h:90
void * buf
Definition aom_encoder.h:89
Image Descriptor.
Definition aom_image.h:182
aom_img_fmt_t fmt
Definition aom_image.h:183
int num
Definition aom_encoder.h:165
int den
Definition aom_encoder.h:166
AV1 specific reference frame data struct.
Definition aom.h:89
int use_external_ref
Definition aom.h:91
aom_image_t img
Definition aom.h:92
int idx
Definition aom.h:90