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,
245 AvxVideoWriter *writer = NULL;
248 int image_size_bytes = aom_img_size_bytes(raw);
250 int u_blocks, v_blocks;
252 aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
253 int reference_image_num = 0;
256 writer = aom_video_writer_open(outfile_name, kContainerIVF, &info);
257 if (!writer) die(
"Failed to open %s for writing", outfile_name);
260 die_codec(&codec,
"Failed to initialize encoder");
262 die_codec(&codec,
"Failed to turn off auto altref");
264 die_codec(&codec,
"Failed to set frame parallel decoding");
272 die_codec(&codec,
"Failed to set SB size");
274 u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize;
275 v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize;
277 reference_image_num = u_blocks * v_blocks;
281 for (i = 0; i < reference_image_num; i++) {
283 cfg->
g_h, 32, 8, AOM_BORDER_IN_PIXELS)) {
284 die(
"Failed to allocate image.");
288 printf(
"\n Second pass: ");
291 printf(
"Encoding Reference Images\n");
292 for (bv = 0; bv < v_blocks; ++bv) {
293 for (bu = 0; bu < u_blocks; ++bu) {
294 const int block_u_min = bu * lf_blocksize;
295 const int block_v_min = bv * lf_blocksize;
296 int block_u_end = (bu + 1) * lf_blocksize;
297 int block_v_end = (bv + 1) * lf_blocksize;
298 int u_block_size, v_block_size;
299 int block_ref_u, block_ref_v;
301 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
302 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
303 u_block_size = block_u_end - block_u_min;
304 v_block_size = block_v_end - block_v_min;
305 block_ref_u = block_u_min + u_block_size / 2;
306 block_ref_v = block_v_min + v_block_size / 2;
308 printf(
"A%d, ", (block_ref_u + block_ref_v * lf_width));
309 fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes,
311 aom_img_read(raw, infile);
313 get_raw_image(&frame_to_encode, raw, raw_shift);
317 printf(
"Encoding reference image %d of %d\n", bv * u_blocks + bu,
318 u_blocks * v_blocks);
319 encode_frame(&codec, frame_to_encode, frame_count, 1,
328 &reference_images[frame_count - 1]))
329 die_codec(&codec,
"Failed to copy decoder reference frame");
337 die_codec(&codec,
"Failed to configure encoder");
341 die_codec(&codec,
"Failed to set cq level");
343 die_codec(&codec,
"Failed to set frame parallel decoding");
345 die_codec(&codec,
"Failed to turn on single tile decoding");
349 die_codec(&codec,
"Failed to set tile width");
351 die_codec(&codec,
"Failed to set tile height");
353 for (bv = 0; bv < v_blocks; ++bv) {
354 for (bu = 0; bu < u_blocks; ++bu) {
355 const int block_u_min = bu * lf_blocksize;
356 const int block_v_min = bv * lf_blocksize;
357 int block_u_end = (bu + 1) * lf_blocksize;
358 int block_v_end = (bv + 1) * lf_blocksize;
360 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
361 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
362 for (v = block_v_min; v < block_v_end; ++v) {
363 for (u = block_u_min; u < block_u_end; ++u) {
367 ref.
img = reference_images[bv * u_blocks + bu];
369 die_codec(&codec,
"Failed to set reference frame");
371 printf(
"C%d, ", (u + v * lf_width));
372 fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET);
373 aom_img_read(raw, infile);
374 get_raw_image(&frame_to_encode, raw, raw_shift);
377 printf(
"Encoding image %d of %d\n",
378 frame_count - (u_blocks * v_blocks), lf_width * lf_height);
379 encode_frame(&codec, frame_to_encode, frame_count, 1,
393 while (encode_frame(&codec, NULL, -1, 1, 0, writer)) {
396 for (i = 0; i < reference_image_num; i++)
aom_img_free(&reference_images[i]);
399 aom_video_writer_close(writer);
401 printf(
"\nSecond pass complete. Processed %d frames.\n", frame_count);
404 int main(
int argc,
char **argv) {
408 int lf_width, lf_height;
421 const AvxInterface *encoder = NULL;
423 const int bitrate = 200;
424 const char *
const width_arg = argv[1];
425 const char *
const height_arg = argv[2];
426 const char *
const infile_arg = argv[3];
427 const char *
const outfile_arg = argv[4];
428 const char *
const lf_width_arg = argv[5];
429 const char *
const lf_height_arg = argv[6];
430 const char *lf_blocksize_arg = argv[7];
433 if (argc < 8) die(
"Invalid number of arguments");
435 encoder = get_aom_encoder_by_name(
"av1");
436 if (!encoder) die(
"Unsupported codec.");
438 w = (int)strtol(width_arg, NULL, 0);
439 h = (int)strtol(height_arg, NULL, 0);
440 lf_width = (int)strtol(lf_width_arg, NULL, 0);
441 lf_height = (int)strtol(lf_height_arg, NULL, 0);
442 lf_blocksize = (int)strtol(lf_blocksize_arg, NULL, 0);
443 lf_blocksize = lf_blocksize < lf_width ? lf_blocksize : lf_width;
444 lf_blocksize = lf_blocksize < lf_height ? lf_blocksize : lf_height;
446 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
447 die(
"Invalid frame size: %dx%d", w, h);
448 if (lf_width <= 0 || lf_height <= 0)
449 die(
"Invalid lf_width and/or lf_height: %dx%d", lf_width, lf_height);
450 if (lf_blocksize <= 0) die(
"Invalid lf_blocksize: %d", lf_blocksize);
453 die(
"Failed to allocate image.");
455 if (!CONFIG_LOWBITDEPTH) {
465 if (res) die_codec(&codec,
"Failed to get default codec config.");
481 if (!(infile = fopen(infile_arg,
"rb")))
482 die(
"Failed to open %s for reading", infile_arg);
486 stats = pass0(&raw, infile, encoder, &cfg, lf_width, lf_height, lf_blocksize,
493 pass1(&raw, infile, outfile_arg, encoder, &cfg, lf_width, lf_height,
494 lf_blocksize, flags, &raw_shift);