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