xref: /aosp_15_r20/external/libaom/examples/simple_encoder.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2016, 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 // Simple Encoder
13 // ==============
14 //
15 // This is an example of a simple encoder loop. It takes an input file in
16 // YV12 format, passes it through the encoder, and writes the compressed
17 // frames to disk in IVF format. Other decoder examples build upon this
18 // one.
19 //
20 // The details of the IVF format have been elided from this example for
21 // simplicity of presentation, as IVF files will not generally be used by
22 // your application. In general, an IVF file consists of a file header,
23 // followed by a variable number of frames. Each frame consists of a frame
24 // header followed by a variable length payload. The length of the payload
25 // is specified in the first four bytes of the frame header. The payload is
26 // the raw compressed data.
27 //
28 // Standard Includes
29 // -----------------
30 // For encoders, you only have to include `aom_encoder.h` and then any
31 // header files for the specific codecs you use. In this case, we're using
32 // aom.
33 //
34 // Getting The Default Configuration
35 // ---------------------------------
36 // Encoders have the notion of "usage profiles." For example, an encoder
37 // may want to publish default configurations for both a video
38 // conferencing application and a best quality offline encoder. These
39 // obviously have very different default settings. Consult the
40 // documentation for your codec to see if it provides any default
41 // configurations. All codecs provide a default configuration, number 0,
42 // which is valid for material in the vacinity of QCIF/QVGA.
43 //
44 // Updating The Configuration
45 // ---------------------------------
46 // Almost all applications will want to update the default configuration
47 // with settings specific to their usage. Here we set the width and height
48 // of the video file to that specified on the command line. We also scale
49 // the default bitrate based on the ratio between the default resolution
50 // and the resolution specified on the command line.
51 //
52 // Initializing The Codec
53 // ----------------------
54 // The encoder is initialized by the following code.
55 //
56 // Encoding A Frame
57 // ----------------
58 // The frame is read as a continuous block (size width * height * 3 / 2)
59 // from the input file. If a frame was read (the input file has not hit
60 // EOF) then the frame is passed to the encoder. Otherwise, a NULL
61 // is passed, indicating the End-Of-Stream condition to the encoder. The
62 // `frame_cnt` is reused as the presentation time stamp (PTS) and each
63 // frame is shown for one frame-time in duration. The flags parameter is
64 // unused in this example.
65 
66 // Forced Keyframes
67 // ----------------
68 // Keyframes can be forced by setting the AOM_EFLAG_FORCE_KF bit of the
69 // flags passed to `aom_codec_control()`. In this example, we force a
70 // keyframe every <keyframe-interval> frames. Note, the output stream can
71 // contain additional keyframes beyond those that have been forced using the
72 // AOM_EFLAG_FORCE_KF flag because of automatic keyframe placement by the
73 // encoder.
74 //
75 // Processing The Encoded Data
76 // ---------------------------
77 // Each packet of type `AOM_CODEC_CX_FRAME_PKT` contains the encoded data
78 // for this frame. We write a IVF frame header, followed by the raw data.
79 //
80 // Cleanup
81 // -------
82 // The `aom_codec_destroy` call frees any memory allocated by the codec.
83 //
84 // Error Handling
85 // --------------
86 // This example does not special case any error return codes. If there was
87 // an error, a descriptive message is printed and the program exits. With
88 // few exeptions, aom_codec functions return an enumerated error status,
89 // with the value `0` indicating success.
90 //
91 // Error Resiliency Features
92 // -------------------------
93 // Error resiliency is controlled by the g_error_resilient member of the
94 // configuration structure. Use the `decode_with_drops` example to decode with
95 // frames 5-10 dropped. Compare the output for a file encoded with this example
96 // versus one encoded with the `simple_encoder` example.
97 
98 #include <stdio.h>
99 #include <stdlib.h>
100 #include <string.h>
101 
102 #include "aom/aom_encoder.h"
103 #include "aom/aomcx.h"
104 #include "common/tools_common.h"
105 #include "common/video_writer.h"
106 
107 static const char *exec_name;
108 
usage_exit(void)109 void usage_exit(void) {
110   fprintf(stderr,
111           "Usage: %s <codec> <width> <height> <infile> <outfile> "
112           "<keyframe-interval> <error-resilient> <frames to encode>\n"
113           "See comments in simple_encoder.c for more information.\n",
114           exec_name);
115   exit(EXIT_FAILURE);
116 }
117 
encode_frame(aom_codec_ctx_t * codec,aom_image_t * img,int frame_index,int flags,AvxVideoWriter * writer)118 static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img,
119                         int frame_index, int flags, AvxVideoWriter *writer) {
120   int got_pkts = 0;
121   aom_codec_iter_t iter = NULL;
122   const aom_codec_cx_pkt_t *pkt = NULL;
123   const aom_codec_err_t res =
124       aom_codec_encode(codec, img, frame_index, 1, flags);
125   if (res != AOM_CODEC_OK) die_codec(codec, "Failed to encode frame");
126 
127   while ((pkt = aom_codec_get_cx_data(codec, &iter)) != NULL) {
128     got_pkts = 1;
129 
130     if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
131       const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
132       if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
133                                         pkt->data.frame.sz,
134                                         pkt->data.frame.pts)) {
135         die_codec(codec, "Failed to write compressed frame");
136       }
137       printf(keyframe ? "K" : ".");
138       fflush(stdout);
139     }
140   }
141 
142   return got_pkts;
143 }
144 
145 // TODO(tomfinegan): Improve command line parsing and add args for bitrate/fps.
main(int argc,char ** argv)146 int main(int argc, char **argv) {
147   FILE *infile = NULL;
148   aom_codec_ctx_t codec;
149   aom_codec_enc_cfg_t cfg;
150   int frame_count = 0;
151   aom_image_t raw;
152   aom_codec_err_t res;
153   AvxVideoInfo info;
154   AvxVideoWriter *writer = NULL;
155   const int fps = 30;
156   const int bitrate = 200;
157   int keyframe_interval = 0;
158   int max_frames = 0;
159   int frames_encoded = 0;
160   const char *codec_arg = NULL;
161   const char *width_arg = NULL;
162   const char *height_arg = NULL;
163   const char *infile_arg = NULL;
164   const char *outfile_arg = NULL;
165   const char *keyframe_interval_arg = NULL;
166 #if CONFIG_REALTIME_ONLY
167   const int usage = 1;
168   const int speed = 7;
169 #else
170   const int usage = 0;
171   const int speed = 2;
172 #endif
173 
174   exec_name = argv[0];
175 
176   // Clear explicitly, as simply assigning "{ 0 }" generates
177   // "missing-field-initializers" warning in some compilers.
178   memset(&info, 0, sizeof(info));
179 
180   if (argc != 9) die("Invalid number of arguments");
181 
182   codec_arg = argv[1];
183   width_arg = argv[2];
184   height_arg = argv[3];
185   infile_arg = argv[4];
186   outfile_arg = argv[5];
187   keyframe_interval_arg = argv[6];
188   max_frames = (int)strtol(argv[8], NULL, 0);
189 
190   aom_codec_iface_t *encoder = get_aom_encoder_by_short_name(codec_arg);
191   if (!encoder) die("Unsupported codec.");
192 
193   info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
194   info.frame_width = (int)strtol(width_arg, NULL, 0);
195   info.frame_height = (int)strtol(height_arg, NULL, 0);
196   info.time_base.numerator = 1;
197   info.time_base.denominator = fps;
198 
199   if (info.frame_width <= 0 || info.frame_height <= 0 ||
200       (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
201     die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
202   }
203 
204   if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width,
205                      info.frame_height, 1)) {
206     die("Failed to allocate image.");
207   }
208 
209   keyframe_interval = (int)strtol(keyframe_interval_arg, NULL, 0);
210   if (keyframe_interval < 0) die("Invalid keyframe interval value.");
211 
212   printf("Using %s\n", aom_codec_iface_name(encoder));
213 
214   res = aom_codec_enc_config_default(encoder, &cfg, usage);
215   if (res) die_codec(&codec, "Failed to get default codec config.");
216 
217   cfg.g_w = info.frame_width;
218   cfg.g_h = info.frame_height;
219   cfg.g_timebase.num = info.time_base.numerator;
220   cfg.g_timebase.den = info.time_base.denominator;
221   cfg.rc_target_bitrate = bitrate;
222   cfg.g_error_resilient = (aom_codec_er_flags_t)strtoul(argv[7], NULL, 0);
223 
224   writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info);
225   if (!writer) die("Failed to open %s for writing.", outfile_arg);
226 
227   if (!(infile = fopen(infile_arg, "rb")))
228     die("Failed to open %s for reading.", infile_arg);
229 
230   if (aom_codec_enc_init(&codec, encoder, &cfg, 0))
231     die("Failed to initialize encoder");
232 
233   if (aom_codec_control(&codec, AOME_SET_CPUUSED, speed))
234     die_codec(&codec, "Failed to set cpu-used");
235 
236   // Encode frames.
237   while (aom_img_read(&raw, infile)) {
238     int flags = 0;
239     if (keyframe_interval > 0 && frame_count % keyframe_interval == 0)
240       flags |= AOM_EFLAG_FORCE_KF;
241     encode_frame(&codec, &raw, frame_count++, flags, writer);
242     frames_encoded++;
243     if (max_frames > 0 && frames_encoded >= max_frames) break;
244   }
245 
246   // Flush encoder.
247   while (encode_frame(&codec, NULL, -1, 0, writer)) continue;
248 
249   printf("\n");
250   fclose(infile);
251   printf("Processed %d frames.\n", frame_count);
252 
253   aom_img_free(&raw);
254   if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
255 
256   aom_video_writer_close(writer);
257 
258   return EXIT_SUCCESS;
259 }
260