56#include "common/tools_common.h"
57#include "common/video_writer.h"
59static const char *exec_name;
61void usage_exit(
void) {
63 "Usage: %s <codec> <width> <height> <infile> <outfile> "
64 "<limit(optional)>\n",
77 if (res !=
AOM_CODEC_OK) die_codec(ctx,
"Failed to get frame stats.");
85 stats->
buf = realloc(stats->
buf, stats->
sz + pkt_size);
86 if (!stats->
buf) die(
"Failed to allocate frame stats buffer.");
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" :
".");
129 die(
"Failed to initialize encoder");
132 while (aom_img_read(raw, infile) && frame_count < limit) {
134 get_frame_stats(&codec, raw, frame_count, 1, 0, &stats);
138 while (get_frame_stats(&codec, NULL, frame_count, 1, 0, &stats)) {
141 printf(
"Pass 0 complete. Processed %d frames.\n", frame_count);
147static void pass1(
aom_image_t *raw, FILE *infile,
const char *outfile_name,
150 AvxVideoInfo info = { get_fourcc_by_aom_encoder(encoder),
155 AvxVideoWriter *writer = NULL;
159 writer = aom_video_writer_open(outfile_name, kContainerIVF, &info);
160 if (!writer) die(
"Failed to open %s for writing", outfile_name);
163 die(
"Failed to initialize encoder");
166 die_codec(&codec,
"Failed to set cpu-used");
169 while (aom_img_read(raw, infile) && frame_count < limit) {
171 encode_frame(&codec, raw, frame_count, 1, 0, writer);
175 while (encode_frame(&codec, NULL, -1, 1, 0, writer)) {
182 aom_video_writer_close(writer);
184 printf(
"Pass 1 complete. Processed %d frames.\n", frame_count);
187int main(
int argc,
char **argv) {
197 const int bitrate = 200;
198 const char *
const codec_arg = argv[1];
199 const char *
const width_arg = argv[2];
200 const char *
const height_arg = argv[3];
201 const char *
const infile_arg = argv[4];
202 const char *
const outfile_arg = argv[5];
206 if (argc < 6) die(
"Invalid number of arguments");
208 if (argc > 6) limit = (int)strtol(argv[6], NULL, 0);
210 if (limit == 0) limit = 100;
213 if (!encoder) die(
"Unsupported codec.");
215 w = (int)strtol(width_arg, NULL, 0);
216 h = (int)strtol(height_arg, NULL, 0);
218 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
219 die(
"Invalid frame size: %dx%d", w, h);
222 die(
"Failed to allocate image (%dx%d)", w, h);
228 if (res) die_codec(&codec,
"Failed to get default codec config.");
236 if (!(infile = fopen(infile_arg,
"rb")))
237 die(
"Failed to open %s for reading", infile_arg);
241 stats = pass0(&raw, infile, encoder, &cfg, limit);
247 pass1(&raw, infile, outfile_arg, encoder, &cfg, limit);
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.
@ AOM_IMG_FMT_I420
Definition aom_image.h:45
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.
@ AOME_SET_CPUUSED
Codec control function to set encoder internal speed settings, int parameter.
Definition aomcx.h:220
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_CODEC_OK
Operation completed without error.
Definition aom_codec.h:157
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
@ AOM_RC_LAST_PASS
Definition aom_encoder.h:181
@ AOM_RC_FIRST_PASS
Definition aom_encoder.h:178
@ 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
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
unsigned int g_w
Width of the frame.
Definition aom_encoder.h:426
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
aom_fixed_buf_t rc_twopass_stats_in
Two-pass stats buffer.
Definition aom_encoder.h:630
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
int num
Definition aom_encoder.h:165
int den
Definition aom_encoder.h:166