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 (!CONFIG_LOWBITDEPTH) {
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 = aom_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 = aom_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");
269 die_codec(&codec,
"Failed to set SB size");
271 u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize;
272 v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize;
274 reference_image_num = u_blocks * v_blocks;
278 for (i = 0; i < reference_image_num; i++) {
280 cfg->
g_h, 32, 8, AOM_BORDER_IN_PIXELS)) {
281 die(
"Failed to allocate image.");
285 printf(
"\n Second pass: ");
288 printf(
"Encoding Reference Images\n");
289 for (bv = 0; bv < v_blocks; ++bv) {
290 for (bu = 0; bu < u_blocks; ++bu) {
291 const int block_u_min = bu * lf_blocksize;
292 const int block_v_min = bv * lf_blocksize;
293 int block_u_end = (bu + 1) * lf_blocksize;
294 int block_v_end = (bv + 1) * lf_blocksize;
295 int u_block_size, v_block_size;
296 int block_ref_u, block_ref_v;
298 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
299 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
300 u_block_size = block_u_end - block_u_min;
301 v_block_size = block_v_end - block_v_min;
302 block_ref_u = block_u_min + u_block_size / 2;
303 block_ref_v = block_v_min + v_block_size / 2;
305 printf(
"A%d, ", (block_ref_u + block_ref_v * lf_width));
306 fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes,
308 aom_img_read(raw, infile);
310 get_raw_image(&frame_to_encode, raw, raw_shift);
314 printf(
"Encoding reference image %d of %d\n", bv * u_blocks + bu,
315 u_blocks * v_blocks);
316 encode_frame(&codec, frame_to_encode, frame_count, 1,
325 &reference_images[frame_count - 1]))
326 die_codec(&codec,
"Failed to copy decoder reference frame");
334 die_codec(&codec,
"Failed to configure encoder");
338 die_codec(&codec,
"Failed to set cq level");
340 die_codec(&codec,
"Failed to set frame parallel decoding");
342 die_codec(&codec,
"Failed to turn on single tile decoding");
346 die_codec(&codec,
"Failed to set tile width");
348 die_codec(&codec,
"Failed to set tile height");
350 for (bv = 0; bv < v_blocks; ++bv) {
351 for (bu = 0; bu < u_blocks; ++bu) {
352 const int block_u_min = bu * lf_blocksize;
353 const int block_v_min = bv * lf_blocksize;
354 int block_u_end = (bu + 1) * lf_blocksize;
355 int block_v_end = (bv + 1) * lf_blocksize;
357 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
358 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
359 for (v = block_v_min; v < block_v_end; ++v) {
360 for (u = block_u_min; u < block_u_end; ++u) {
364 ref.
img = reference_images[bv * u_blocks + bu];
366 die_codec(&codec,
"Failed to set reference frame");
368 printf(
"C%d, ", (u + v * lf_width));
369 fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET);
370 aom_img_read(raw, infile);
371 get_raw_image(&frame_to_encode, raw, raw_shift);
374 printf(
"Encoding image %d of %d\n",
375 frame_count - (u_blocks * v_blocks), lf_width * lf_height);
376 encode_frame(&codec, frame_to_encode, frame_count, 1,
390 while (encode_frame(&codec, NULL, -1, 1, 0, writer)) {
393 for (i = 0; i < reference_image_num; i++)
aom_img_free(&reference_images[i]);
396 aom_video_writer_close(writer);
398 printf(
"\nSecond pass complete. Processed %d frames.\n", frame_count);
401 int main(
int argc,
char **argv) {
405 int lf_width, lf_height;
418 const AvxInterface *encoder = NULL;
420 const int bitrate = 200;
421 const char *
const width_arg = argv[1];
422 const char *
const height_arg = argv[2];
423 const char *
const infile_arg = argv[3];
424 const char *
const outfile_arg = argv[4];
425 const char *
const lf_width_arg = argv[5];
426 const char *
const lf_height_arg = argv[6];
427 const char *lf_blocksize_arg = argv[7];
430 if (argc < 8) die(
"Invalid number of arguments");
432 encoder = get_aom_encoder_by_name(
"av1");
433 if (!encoder) die(
"Unsupported codec.");
435 w = (int)strtol(width_arg, NULL, 0);
436 h = (int)strtol(height_arg, NULL, 0);
437 lf_width = (int)strtol(lf_width_arg, NULL, 0);
438 lf_height = (int)strtol(lf_height_arg, NULL, 0);
439 lf_blocksize = (int)strtol(lf_blocksize_arg, NULL, 0);
440 lf_blocksize = lf_blocksize < lf_width ? lf_blocksize : lf_width;
441 lf_blocksize = lf_blocksize < lf_height ? lf_blocksize : lf_height;
443 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
444 die(
"Invalid frame size: %dx%d", w, h);
445 if (lf_width <= 0 || lf_height <= 0)
446 die(
"Invalid lf_width and/or lf_height: %dx%d", lf_width, lf_height);
447 if (lf_blocksize <= 0) die(
"Invalid lf_blocksize: %d", lf_blocksize);
450 die(
"Failed to allocate image.");
452 if (!CONFIG_LOWBITDEPTH) {
462 if (res) die_codec(&codec,
"Failed to get default codec config.");
478 if (!(infile = fopen(infile_arg,
"rb")))
479 die(
"Failed to open %s for reading", infile_arg);
483 stats = pass0(&raw, infile, encoder, &cfg, lf_width, lf_height, lf_blocksize,
490 pass1(&raw, infile, outfile_arg, encoder, &cfg, lf_width, lf_height,
491 lf_blocksize, flags, &raw_shift);