| AOMedia AV1 Codec

low_complexity_mode_encoder
1/*
2 * Copyright (c) 2026, Alliance for Open Media. All rights reserved.
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12// Low Complexity (LC) Mode Encoder
13// ================================
14//
15// This is an example of a low complexity mode encoder. It takes an input
16// file in Y4M format, passes it through the encoder with LC mode on, and
17// writes the compressed frames to disk in IVF format.
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "aom/aom_encoder.h"
24#include "aom/aomcx.h"
25#include "common/tools_common.h"
26#include "common/video_writer.h"
27
28 static const char *exec_name;
29
30 void usage_exit(void) {
31 fprintf(stderr,
32 "Usage: %s <codec> <width> <height> <infile> <outfile> "
33 "<keyframe-interval> <frames to encode>\n",
34 exec_name);
35 exit(EXIT_FAILURE);
36}
37
38 static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img,
39 int frame_index, int flags, AvxVideoWriter *writer) {
40 int got_pkts = 0;
41 aom_codec_iter_t iter = NULL;
42 const aom_codec_cx_pkt_t *pkt = NULL;
43 const aom_codec_err_t res =
44 aom_codec_encode(codec, img, frame_index, 1, flags);
45 if (res != AOM_CODEC_OK) die_codec(codec, "Failed to encode frame");
46
47 while ((pkt = aom_codec_get_cx_data(codec, &iter)) != NULL) {
48 got_pkts = 1;
49
50 if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
51 const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
52 if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
53 pkt->data.frame.sz,
54 pkt->data.frame.pts)) {
55 die_codec(codec, "Failed to write compressed frame");
56 }
57 printf(keyframe ? "K" : ".");
58 fflush(stdout);
59 }
60 }
61
62 return got_pkts;
63}
64
65 int main(int argc, char **argv) {
66 FILE *infile = NULL;
67 aom_codec_ctx_t codec;
68 aom_codec_enc_cfg_t cfg;
69 int frame_count = 0;
70 aom_image_t raw;
71 aom_codec_err_t res;
72 AvxVideoInfo info;
73 AvxVideoWriter *writer = NULL;
74 const int fps = 30;
75 const int bitrate = 400;
76 int keyframe_interval = 0;
77 int max_frames = 0;
78 int frames_encoded = 0;
79 const char *codec_arg = NULL;
80 const char *width_arg = NULL;
81 const char *height_arg = NULL;
82 const char *infile_arg = NULL;
83 const char *outfile_arg = NULL;
84 const char *keyframe_interval_arg = NULL;
85 const int usage = 0; // AOM_USAGE_GOOD_QUALITY
86 const int speed = 2;
87
88 exec_name = argv[0];
89
90 // Clear explicitly, as simply assigning "{ 0 }" generates
91 // "missing-field-initializers" warning in some compilers.
92 memset(&info, 0, sizeof(info));
93
94 if (argc != 8) die("Invalid number of arguments");
95
96 codec_arg = argv[1];
97 width_arg = argv[2];
98 height_arg = argv[3];
99 infile_arg = argv[4];
100 outfile_arg = argv[5];
101 keyframe_interval_arg = argv[6];
102 max_frames = (int)strtol(argv[7], NULL, 0);
103
104 aom_codec_iface_t *encoder = get_aom_encoder_by_short_name(codec_arg);
105 if (!encoder) die("Unsupported codec.");
106
107 info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
108 info.frame_width = (int)strtol(width_arg, NULL, 0);
109 info.frame_height = (int)strtol(height_arg, NULL, 0);
110 info.time_base.numerator = 1;
111 info.time_base.denominator = fps;
112
113 if (info.frame_width <= 0 || info.frame_height <= 0 ||
114 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
115 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
116 }
117
118 if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width,
119 info.frame_height, 1)) {
120 die("Failed to allocate image.");
121 }
122
123 keyframe_interval = (int)strtol(keyframe_interval_arg, NULL, 0);
124 if (keyframe_interval < 0) die("Invalid keyframe interval value.");
125
126 printf("Using %s\n", aom_codec_iface_name(encoder));
127
128 res = aom_codec_enc_config_default(encoder, &cfg, usage);
129 if (res)
130 die("Failed to get default codec config: %s", aom_codec_err_to_string(res));
131
132 cfg.g_w = info.frame_width;
133 cfg.g_h = info.frame_height;
134 cfg.g_timebase.num = info.time_base.numerator;
135 cfg.g_timebase.den = info.time_base.denominator;
136 cfg.rc_target_bitrate = bitrate;
137 cfg.g_lag_in_frames = 35;
138
139 writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info);
140 if (!writer) die("Failed to open %s for writing.", outfile_arg);
141
142 if (!(infile = fopen(infile_arg, "rb")))
143 die("Failed to open %s for reading.", infile_arg);
144
145 if (aom_codec_enc_init(&codec, encoder, &cfg, 0))
146 die("Failed to initialize encoder");
147
148 if (aom_codec_control(&codec, AOME_SET_CPUUSED, speed))
149 die_codec(&codec, "Failed to set cpu-used");
150
151 if (aom_codec_control(&codec, AV1E_SET_ENABLE_LOW_COMPLEXITY_DECODE, 1))
152 die_codec(&codec, "Failed to set low-complexity mode");
153
154 // Encode frames.
155 while (aom_img_read(&raw, infile)) {
156 int flags = 0;
157 if (keyframe_interval > 0 && frame_count % keyframe_interval == 0)
158 flags |= AOM_EFLAG_FORCE_KF;
159 encode_frame(&codec, &raw, frame_count++, flags, writer);
160 frames_encoded++;
161 if (max_frames > 0 && frames_encoded >= max_frames) break;
162 }
163
164 // Flush encoder.
165 while (encode_frame(&codec, NULL, -1, 0, writer)) continue;
166
167 printf("\n");
168 fclose(infile);
169 printf("Processed %d frames.\n", frame_count);
170
171 aom_img_free(&raw);
172 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
173
174 aom_video_writer_close(writer);
175
176 return EXIT_SUCCESS;
177}
aom_encoder.h
Describes the encoder algorithm interface to applications.
aom_img_alloc
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
@ AOM_IMG_FMT_I420
Definition aom_image.h:45
aom_image_t
struct aom_image aom_image_t
Image Descriptor.
aom_img_free
void aom_img_free(aom_image_t *img)
Close an image descriptor.
aomcx.h
Provides definitions for using AOM or AV1 encoder algorithm within the aom Codec Interface.
AV1E_SET_ENABLE_LOW_COMPLEXITY_DECODE
@ AV1E_SET_ENABLE_LOW_COMPLEXITY_DECODE
Codec control to enable the low complexity decode mode, unsigned int parameter. Value of zero means t...
Definition aomcx.h:1598
AOME_SET_CPUUSED
@ AOME_SET_CPUUSED
Codec control function to set encoder internal speed settings, int parameter.
Definition aomcx.h:223
aom_codec_iface_name
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_codec_control
aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id,...)
Algorithm Control.
aom_codec_ctx_t
struct aom_codec_ctx aom_codec_ctx_t
Codec context structure.
aom_codec_iface_t
const struct aom_codec_iface aom_codec_iface_t
Codec interface structure.
Definition aom_codec.h:271
aom_codec_destroy
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
aom_codec_err_to_string
const char * aom_codec_err_to_string(aom_codec_err_t err)
Convert error number to printable string.
aom_codec_err_t
aom_codec_err_t
Algorithm return codes.
Definition aom_codec.h:155
aom_codec_iter_t
const void * aom_codec_iter_t
Iterator.
Definition aom_codec.h:305
AOM_FRAME_IS_KEY
#define AOM_FRAME_IS_KEY
Definition aom_codec.h:288
AOM_CODEC_OK
@ AOM_CODEC_OK
Operation completed without error.
Definition aom_codec.h:157
aom_codec_get_cx_data
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_cx_pkt_t
struct aom_codec_cx_pkt aom_codec_cx_pkt_t
Encoder output packet.
aom_codec_encode
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.
AOM_EFLAG_FORCE_KF
#define AOM_EFLAG_FORCE_KF
Force this frame to be a keyframe.
Definition aom_encoder.h:379
aom_codec_enc_init
#define aom_codec_enc_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_enc_init_ver().
Definition aom_encoder.h:956
aom_codec_enc_config_default
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.
aom_codec_enc_cfg_t
struct aom_codec_enc_cfg aom_codec_enc_cfg_t
Encoder configuration structure.
AOM_CODEC_CX_FRAME_PKT
@ AOM_CODEC_CX_FRAME_PKT
Definition aom_encoder.h:110
aom_codec_cx_pkt::sz
size_t sz
Definition aom_encoder.h:127
aom_codec_cx_pkt::kind
enum aom_codec_cx_pkt_kind kind
Definition aom_encoder.h:123
aom_codec_cx_pkt::data
union aom_codec_cx_pkt::@202210014045072156205127107315337341215221351166 data
aom_codec_cx_pkt::pts
aom_codec_pts_t pts
time stamp to show frame (in timebase units)
Definition aom_encoder.h:129
aom_codec_cx_pkt::flags
aom_codec_frame_flags_t flags
Definition aom_encoder.h:132
aom_codec_cx_pkt::frame
struct aom_codec_cx_pkt::@202210014045072156205127107315337341215221351166::@052232317104146204273007241322037340334334344046 frame
aom_codec_cx_pkt::buf
void * buf
Definition aom_encoder.h:126
aom_codec_enc_cfg::g_timebase
struct aom_rational g_timebase
Stream timebase units.
Definition aom_encoder.h:498
aom_codec_enc_cfg::g_h
unsigned int g_h
Height of the frame.
Definition aom_encoder.h:440
aom_codec_enc_cfg::g_lag_in_frames
unsigned int g_lag_in_frames
Allow lagged encoding.
Definition aom_encoder.h:527
aom_codec_enc_cfg::g_w
unsigned int g_w
Width of the frame.
Definition aom_encoder.h:431
aom_codec_enc_cfg::rc_target_bitrate
unsigned int rc_target_bitrate
Target data rate.
Definition aom_encoder.h:653
aom_rational::num
int num
Definition aom_encoder.h:165
aom_rational::den
int den
Definition aom_encoder.h:166