blob: 5a54518533ea68db681e8c0d6867ed1797ef55b4 [file] [log] [blame]
Dmitry Kovalev50fa5852014-01-07 15:15:25 -08001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Dmitry Kovalev50fa5852014-01-07 15:15:25 -08003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080010 */
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// -----------------
Yaowu Xuf883b422016-08-30 14:01:10 -070030// For encoders, you only have to include `aom_encoder.h` and then any
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080031// header files for the specific codecs you use. In this case, we're using
Yaowu Xuf883b422016-08-30 14:01:10 -070032// aom.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080033//
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
Dmitry Kovalev194e6f22014-02-25 16:34:49 -080038// conferencing application and a best quality offline encoder. These
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080039// 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
Thomas Daede80826142017-03-20 15:44:24 -070064// unused in this example.
Dmitry Kovalevc3cd6b32014-03-03 17:48:06 -080065
66// Forced Keyframes
67// ----------------
Yaowu Xuf883b422016-08-30 14:01:10 -070068// 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
Dmitry Kovalevc3cd6b32014-03-03 17:48:06 -080070// keyframe every <keyframe-interval> frames. Note, the output stream can
71// contain additional keyframes beyond those that have been forced using the
Yaowu Xuf883b422016-08-30 14:01:10 -070072// AOM_EFLAG_FORCE_KF flag because of automatic keyframe placement by the
Dmitry Kovalevc3cd6b32014-03-03 17:48:06 -080073// encoder.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080074//
75// Processing The Encoded Data
76// ---------------------------
Yaowu Xuf883b422016-08-30 14:01:10 -070077// Each packet of type `AOM_CODEC_CX_FRAME_PKT` contains the encoded data
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080078// for this frame. We write a IVF frame header, followed by the raw data.
79//
80// Cleanup
81// -------
Yaowu Xuf883b422016-08-30 14:01:10 -070082// The `aom_codec_destroy` call frees any memory allocated by the codec.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080083//
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
Yaowu Xuf883b422016-08-30 14:01:10 -070088// few exeptions, aom_codec functions return an enumerated error status,
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080089// with the value `0` indicating success.
Dmitry Kovalev194e6f22014-02-25 16:34:49 -080090//
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.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080097
98#include <stdio.h>
99#include <stdlib.h>
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800100#include <string.h>
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800101
Yaowu Xuf883b422016-08-30 14:01:10 -0700102#include "aom/aom_encoder.h"
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800103
Tom Finegan9e96bdc2015-02-04 16:11:57 -0800104#include "../tools_common.h"
105#include "../video_writer.h"
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800106
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800107static const char *exec_name;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800108
James Zern59e7a472015-05-09 10:33:26 -0700109void usage_exit(void) {
Dmitry Kovalev194e6f22014-02-25 16:34:49 -0800110 fprintf(stderr,
111 "Usage: %s <codec> <width> <height> <infile> <outfile> "
clang-format397d9642016-08-08 18:51:16 -0700112 "<keyframe-interval> <error-resilient> <frames to encode>\n"
113 "See comments in simple_encoder.c for more information.\n",
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800114 exec_name);
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800115 exit(EXIT_FAILURE);
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800116}
117
Yaowu Xuf883b422016-08-30 14:01:10 -0700118static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img,
119 int frame_index, int flags, AvxVideoWriter *writer) {
Dmitry Kovaleva4e85a52014-08-11 14:10:33 -0700120 int got_pkts = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700121 aom_codec_iter_t iter = NULL;
122 const aom_codec_cx_pkt_t *pkt = NULL;
123 const aom_codec_err_t res =
Sean DuBois47cc2552018-01-23 07:44:16 +0000124 aom_codec_encode(codec, img, frame_index, 1, flags);
Yaowu Xuf883b422016-08-30 14:01:10 -0700125 if (res != AOM_CODEC_OK) die_codec(codec, "Failed to encode frame");
Dmitry Kovalev374eaf22014-02-12 16:26:58 -0800126
Yaowu Xuf883b422016-08-30 14:01:10 -0700127 while ((pkt = aom_codec_get_cx_data(codec, &iter)) != NULL) {
Dmitry Kovaleva4e85a52014-08-11 14:10:33 -0700128 got_pkts = 1;
129
Yaowu Xuf883b422016-08-30 14:01:10 -0700130 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,
Dmitry Kovalev374eaf22014-02-12 16:26:58 -0800133 pkt->data.frame.sz,
134 pkt->data.frame.pts)) {
135 die_codec(codec, "Failed to write compressed frame");
136 }
Dmitry Kovalev374eaf22014-02-12 16:26:58 -0800137 printf(keyframe ? "K" : ".");
138 fflush(stdout);
139 }
140 }
Dmitry Kovaleva4e85a52014-08-11 14:10:33 -0700141
142 return got_pkts;
Dmitry Kovalev374eaf22014-02-12 16:26:58 -0800143}
144
Tom Finegan7d6edc32016-05-09 15:00:44 -0700145// TODO(tomfinegan): Improve command line parsing and add args for bitrate/fps.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800146int main(int argc, char **argv) {
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800147 FILE *infile = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700148 aom_codec_ctx_t codec;
149 aom_codec_enc_cfg_t cfg;
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800150 int frame_count = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700151 aom_image_t raw;
152 aom_codec_err_t res;
Urvang Joshid71a2312016-07-14 12:33:48 -0700153 AvxVideoInfo info;
Yaowu Xuf883b422016-08-30 14:01:10 -0700154 AvxVideoWriter *writer = NULL;
155 const AvxInterface *encoder = NULL;
Tom Finegan7d6edc32016-05-09 15:00:44 -0700156 const int fps = 30;
157 const int bitrate = 200;
Dmitry Kovalevc3cd6b32014-03-03 17:48:06 -0800158 int keyframe_interval = 0;
Tom Finegan7d6edc32016-05-09 15:00:44 -0700159 int max_frames = 0;
160 int frames_encoded = 0;
Dmitry Kovalev194e6f22014-02-25 16:34:49 -0800161 const char *codec_arg = NULL;
162 const char *width_arg = NULL;
163 const char *height_arg = NULL;
164 const char *infile_arg = NULL;
165 const char *outfile_arg = NULL;
Dmitry Kovalevc3cd6b32014-03-03 17:48:06 -0800166 const char *keyframe_interval_arg = NULL;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800167
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800168 exec_name = argv[0];
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800169
Urvang Joshid71a2312016-07-14 12:33:48 -0700170 // Clear explicitly, as simply assigning "{ 0 }" generates
171 // "missing-field-initializers" warning in some compilers.
172 memset(&info, 0, sizeof(info));
173
clang-format397d9642016-08-08 18:51:16 -0700174 if (argc != 9) die("Invalid number of arguments");
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800175
Dmitry Kovalev194e6f22014-02-25 16:34:49 -0800176 codec_arg = argv[1];
177 width_arg = argv[2];
178 height_arg = argv[3];
179 infile_arg = argv[4];
180 outfile_arg = argv[5];
Dmitry Kovalevc3cd6b32014-03-03 17:48:06 -0800181 keyframe_interval_arg = argv[6];
James Zern097fef92017-03-23 00:25:55 -0700182 max_frames = (int)strtol(argv[8], NULL, 0);
Dmitry Kovalev194e6f22014-02-25 16:34:49 -0800183
Yaowu Xuf883b422016-08-30 14:01:10 -0700184 encoder = get_aom_encoder_by_name(codec_arg);
clang-format397d9642016-08-08 18:51:16 -0700185 if (!encoder) die("Unsupported codec.");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800186
187 info.codec_fourcc = encoder->fourcc;
James Zern097fef92017-03-23 00:25:55 -0700188 info.frame_width = (int)strtol(width_arg, NULL, 0);
189 info.frame_height = (int)strtol(height_arg, NULL, 0);
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800190 info.time_base.numerator = 1;
191 info.time_base.denominator = fps;
192
clang-format397d9642016-08-08 18:51:16 -0700193 if (info.frame_width <= 0 || info.frame_height <= 0 ||
194 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800195 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
196 }
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800197
Yaowu Xuf883b422016-08-30 14:01:10 -0700198 if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width,
clang-format397d9642016-08-08 18:51:16 -0700199 info.frame_height, 1)) {
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800200 die("Failed to allocate image.");
201 }
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800202
James Zern097fef92017-03-23 00:25:55 -0700203 keyframe_interval = (int)strtol(keyframe_interval_arg, NULL, 0);
clang-format397d9642016-08-08 18:51:16 -0700204 if (keyframe_interval < 0) die("Invalid keyframe interval value.");
Dmitry Kovalevc3cd6b32014-03-03 17:48:06 -0800205
Yaowu Xuf883b422016-08-30 14:01:10 -0700206 printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800207
Yaowu Xuf883b422016-08-30 14:01:10 -0700208 res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
clang-format397d9642016-08-08 18:51:16 -0700209 if (res) die_codec(&codec, "Failed to get default codec config.");
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800210
211 cfg.g_w = info.frame_width;
212 cfg.g_h = info.frame_height;
213 cfg.g_timebase.num = info.time_base.numerator;
214 cfg.g_timebase.den = info.time_base.denominator;
215 cfg.rc_target_bitrate = bitrate;
James Zern097fef92017-03-23 00:25:55 -0700216 cfg.g_error_resilient = (aom_codec_er_flags_t)strtoul(argv[7], NULL, 0);
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800217
Yaowu Xuf883b422016-08-30 14:01:10 -0700218 writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info);
clang-format397d9642016-08-08 18:51:16 -0700219 if (!writer) die("Failed to open %s for writing.", outfile_arg);
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800220
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800221 if (!(infile = fopen(infile_arg, "rb")))
222 die("Failed to open %s for reading.", infile_arg);
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800223
Yaowu Xuf883b422016-08-30 14:01:10 -0700224 if (aom_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800225 die_codec(&codec, "Failed to initialize encoder");
226
Dmitry Kovaleva4e85a52014-08-11 14:10:33 -0700227 // Encode frames.
Yaowu Xuf883b422016-08-30 14:01:10 -0700228 while (aom_img_read(&raw, infile)) {
Dmitry Kovalevc3cd6b32014-03-03 17:48:06 -0800229 int flags = 0;
230 if (keyframe_interval > 0 && frame_count % keyframe_interval == 0)
Yaowu Xuf883b422016-08-30 14:01:10 -0700231 flags |= AOM_EFLAG_FORCE_KF;
Dmitry Kovalevc3cd6b32014-03-03 17:48:06 -0800232 encode_frame(&codec, &raw, frame_count++, flags, writer);
Tom Finegan7d6edc32016-05-09 15:00:44 -0700233 frames_encoded++;
clang-format397d9642016-08-08 18:51:16 -0700234 if (max_frames > 0 && frames_encoded >= max_frames) break;
Dmitry Kovalevc3cd6b32014-03-03 17:48:06 -0800235 }
Dmitry Kovaleva4e85a52014-08-11 14:10:33 -0700236
237 // Flush encoder.
Yaowu Xu5cb0a7a2016-10-17 08:12:18 -0700238 while (encode_frame(&codec, NULL, -1, 0, writer)) continue;
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800239
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800240 printf("\n");
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800241 fclose(infile);
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800242 printf("Processed %d frames.\n", frame_count);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800243
Yaowu Xuf883b422016-08-30 14:01:10 -0700244 aom_img_free(&raw);
245 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800246
Yaowu Xuf883b422016-08-30 14:01:10 -0700247 aom_video_writer_close(writer);
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800248
Dmitry Kovalev37e6fd32014-02-05 18:34:46 -0800249 return EXIT_SUCCESS;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800250}