blob: a03bc6cc2a83cefd2b4ae4e55c1cb7c3d4933681 [file] [log] [blame]
Dmitry Kovalev50fa5852014-01-07 15:15:25 -08001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Dmitry Kovalev50fa5852014-01-07 15:15:25 -08003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -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// Two Pass Encoder
13// ================
14//
15// This is an example of a two pass encoder loop. It takes an input file in
16// YV12 format, passes it through the encoder twice, and writes the compressed
17// frames to disk in IVF format. It builds upon the simple_encoder example.
18//
19// Twopass Variables
20// -----------------
21// Twopass mode needs to track the current pass number and the buffer of
22// statistics packets.
23//
24// Updating The Configuration
25// ---------------------------------
26// In two pass mode, the configuration has to be updated on each pass. The
27// statistics buffer is passed on the last pass.
28//
29// Encoding A Frame
30// ----------------
31// Encoding a frame in two pass mode is identical to the simple encoder
Thomas Daede6eca8352017-03-17 14:14:12 -070032// example.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080033//
34// Processing Statistics Packets
35// -----------------------------
Yaowu Xuf883b422016-08-30 14:01:10 -070036// Each packet of type `AOM_CODEC_CX_FRAME_PKT` contains the encoded data
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080037// for this frame. We write a IVF frame header, followed by the raw data.
38//
39//
40// Pass Progress Reporting
41// -----------------------------
42// It's sometimes helpful to see when each pass completes.
43//
44//
45// Clean-up
46// -----------------------------
47// Destruction of the encoder instance must be done on each pass. The
48// raw image should be destroyed at the end as usual.
49
50#include <stdio.h>
51#include <stdlib.h>
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080052#include <string.h>
Dmitry Kovalev592936b2014-02-07 11:37:39 -080053
Yaowu Xuf883b422016-08-30 14:01:10 -070054#include "aom/aom_encoder.h"
Tom Finegan77902132018-05-21 10:19:15 -070055#include "common/tools_common.h"
56#include "common/video_writer.h"
Dmitry Kovalev592936b2014-02-07 11:37:39 -080057
Dmitry Kovalev592936b2014-02-07 11:37:39 -080058static const char *exec_name;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080059
James Zern59e7a472015-05-09 10:33:26 -070060void usage_exit(void) {
Tom Finegan9d473412016-05-11 14:50:03 -070061 fprintf(stderr,
62 "Usage: %s <codec> <width> <height> <infile> <outfile> "
Jim Bankoskia65e7be2016-05-20 07:31:51 -070063 "<limit(optional)>\n",
Dmitry Kovalev70d96642014-02-11 21:12:23 -080064 exec_name);
Dmitry Kovalev592936b2014-02-07 11:37:39 -080065 exit(EXIT_FAILURE);
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080066}
67
Yaowu Xuf883b422016-08-30 14:01:10 -070068static int get_frame_stats(aom_codec_ctx_t *ctx, const aom_image_t *img,
69 aom_codec_pts_t pts, unsigned int duration,
Sean DuBois47cc2552018-01-23 07:44:16 +000070 aom_enc_frame_flags_t flags,
Yaowu Xuf883b422016-08-30 14:01:10 -070071 aom_fixed_buf_t *stats) {
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -070072 int got_pkts = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070073 aom_codec_iter_t iter = NULL;
74 const aom_codec_cx_pkt_t *pkt = NULL;
Sean DuBois47cc2552018-01-23 07:44:16 +000075 const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags);
Yaowu Xuf883b422016-08-30 14:01:10 -070076 if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to get frame stats.");
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080077
Yaowu Xuf883b422016-08-30 14:01:10 -070078 while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) {
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -070079 got_pkts = 1;
80
Yaowu Xuf883b422016-08-30 14:01:10 -070081 if (pkt->kind == AOM_CODEC_STATS_PKT) {
Dmitry Kovalev592936b2014-02-07 11:37:39 -080082 const uint8_t *const pkt_buf = pkt->data.twopass_stats.buf;
83 const size_t pkt_size = pkt->data.twopass_stats.sz;
84 stats->buf = realloc(stats->buf, stats->sz + pkt_size);
85 memcpy((uint8_t *)stats->buf + stats->sz, pkt_buf, pkt_size);
86 stats->sz += pkt_size;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080087 }
Dmitry Kovalev592936b2014-02-07 11:37:39 -080088 }
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -070089
90 return got_pkts;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080091}
92
Yaowu Xuf883b422016-08-30 14:01:10 -070093static int encode_frame(aom_codec_ctx_t *ctx, const aom_image_t *img,
94 aom_codec_pts_t pts, unsigned int duration,
Sean DuBois47cc2552018-01-23 07:44:16 +000095 aom_enc_frame_flags_t flags, AvxVideoWriter *writer) {
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -070096 int got_pkts = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070097 aom_codec_iter_t iter = NULL;
98 const aom_codec_cx_pkt_t *pkt = NULL;
Sean DuBois47cc2552018-01-23 07:44:16 +000099 const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags);
Yaowu Xuf883b422016-08-30 14:01:10 -0700100 if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to encode frame.");
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800101
Yaowu Xuf883b422016-08-30 14:01:10 -0700102 while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) {
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700103 got_pkts = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700104 if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
105 const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800106
Yaowu Xuf883b422016-08-30 14:01:10 -0700107 if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
clang-format397d9642016-08-08 18:51:16 -0700108 pkt->data.frame.sz,
109 pkt->data.frame.pts))
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800110 die_codec(ctx, "Failed to write compressed frame.");
111 printf(keyframe ? "K" : ".");
112 fflush(stdout);
113 }
114 }
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700115
116 return got_pkts;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800117}
118
Yaowu Xuf883b422016-08-30 14:01:10 -0700119static aom_fixed_buf_t pass0(aom_image_t *raw, FILE *infile,
120 const AvxInterface *encoder,
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700121 const aom_codec_enc_cfg_t *cfg, int limit) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700122 aom_codec_ctx_t codec;
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700123 int frame_count = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700124 aom_fixed_buf_t stats = { NULL, 0 };
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700125
Yaowu Xuf883b422016-08-30 14:01:10 -0700126 if (aom_codec_enc_init(&codec, encoder->codec_interface(), cfg, 0))
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700127 die_codec(&codec, "Failed to initialize encoder");
128
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700129 // Calculate frame statistics.
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700130 while (aom_img_read(raw, infile) && frame_count < limit) {
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700131 ++frame_count;
Sean DuBois47cc2552018-01-23 07:44:16 +0000132 get_frame_stats(&codec, raw, frame_count, 1, 0, &stats);
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700133 }
134
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700135 // Flush encoder.
Sean DuBois47cc2552018-01-23 07:44:16 +0000136 while (get_frame_stats(&codec, NULL, frame_count, 1, 0, &stats)) {
clang-format397d9642016-08-08 18:51:16 -0700137 }
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700138
139 printf("Pass 0 complete. Processed %d frames.\n", frame_count);
Yaowu Xuf883b422016-08-30 14:01:10 -0700140 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700141
142 return stats;
143}
144
Yaowu Xuf883b422016-08-30 14:01:10 -0700145static void pass1(aom_image_t *raw, FILE *infile, const char *outfile_name,
146 const AvxInterface *encoder, const aom_codec_enc_cfg_t *cfg,
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700147 int limit) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700148 AvxVideoInfo info = { encoder->fourcc,
clang-format397d9642016-08-08 18:51:16 -0700149 cfg->g_w,
150 cfg->g_h,
Jonathan Matthewsa6e4f962018-07-13 16:01:28 +0100151 { cfg->g_timebase.num, cfg->g_timebase.den },
152 0 };
Yaowu Xuf883b422016-08-30 14:01:10 -0700153 AvxVideoWriter *writer = NULL;
154 aom_codec_ctx_t codec;
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700155 int frame_count = 0;
156
Yaowu Xuf883b422016-08-30 14:01:10 -0700157 writer = aom_video_writer_open(outfile_name, kContainerIVF, &info);
clang-format397d9642016-08-08 18:51:16 -0700158 if (!writer) die("Failed to open %s for writing", outfile_name);
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700159
Yaowu Xuf883b422016-08-30 14:01:10 -0700160 if (aom_codec_enc_init(&codec, encoder->codec_interface(), cfg, 0))
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700161 die_codec(&codec, "Failed to initialize encoder");
162
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700163 // Encode frames.
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700164 while (aom_img_read(raw, infile) && frame_count < limit) {
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700165 ++frame_count;
Sean DuBois47cc2552018-01-23 07:44:16 +0000166 encode_frame(&codec, raw, frame_count, 1, 0, writer);
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700167 }
168
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700169 // Flush encoder.
Sean DuBois47cc2552018-01-23 07:44:16 +0000170 while (encode_frame(&codec, NULL, -1, 1, 0, writer)) {
clang-format397d9642016-08-08 18:51:16 -0700171 }
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700172
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700173 printf("\n");
174
Yaowu Xuf883b422016-08-30 14:01:10 -0700175 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700176
Yaowu Xuf883b422016-08-30 14:01:10 -0700177 aom_video_writer_close(writer);
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700178
179 printf("Pass 1 complete. Processed %d frames.\n", frame_count);
180}
181
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800182int main(int argc, char **argv) {
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800183 FILE *infile = NULL;
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700184 int w, h;
Yaowu Xuf883b422016-08-30 14:01:10 -0700185 aom_codec_ctx_t codec;
186 aom_codec_enc_cfg_t cfg;
187 aom_image_t raw;
188 aom_codec_err_t res;
189 aom_fixed_buf_t stats;
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700190
Yaowu Xuf883b422016-08-30 14:01:10 -0700191 const AvxInterface *encoder = NULL;
clang-format397d9642016-08-08 18:51:16 -0700192 const int fps = 30; // TODO(dkovalev) add command line argument
193 const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800194 const char *const codec_arg = argv[1];
195 const char *const width_arg = argv[2];
196 const char *const height_arg = argv[3];
197 const char *const infile_arg = argv[4];
198 const char *const outfile_arg = argv[5];
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700199 int limit = 0;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800200 exec_name = argv[0];
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800201
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700202 if (argc < 6) die("Invalid number of arguments");
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800203
James Zern097fef92017-03-23 00:25:55 -0700204 if (argc > 6) limit = (int)strtol(argv[6], NULL, 0);
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700205
206 if (limit == 0) limit = 100;
Tom Finegan9d473412016-05-11 14:50:03 -0700207
Yaowu Xuf883b422016-08-30 14:01:10 -0700208 encoder = get_aom_encoder_by_name(codec_arg);
clang-format397d9642016-08-08 18:51:16 -0700209 if (!encoder) die("Unsupported codec.");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800210
James Zern097fef92017-03-23 00:25:55 -0700211 w = (int)strtol(width_arg, NULL, 0);
212 h = (int)strtol(height_arg, NULL, 0);
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800213
clang-format397d9642016-08-08 18:51:16 -0700214 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700215 die("Invalid frame size: %dx%d", w, h);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800216
Yaowu Xuf883b422016-08-30 14:01:10 -0700217 if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, w, h, 1))
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700218 die("Failed to allocate image", w, h);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800219
Yaowu Xuf883b422016-08-30 14:01:10 -0700220 printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800221
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700222 // Configuration
Yaowu Xuf883b422016-08-30 14:01:10 -0700223 res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
clang-format397d9642016-08-08 18:51:16 -0700224 if (res) die_codec(&codec, "Failed to get default codec config.");
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800225
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700226 cfg.g_w = w;
227 cfg.g_h = h;
228 cfg.g_timebase.num = 1;
229 cfg.g_timebase.den = fps;
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800230 cfg.rc_target_bitrate = bitrate;
231
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700232 if (!(infile = fopen(infile_arg, "rb")))
233 die("Failed to open %s for reading", infile_arg);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800234
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700235 // Pass 0
Yaowu Xuf883b422016-08-30 14:01:10 -0700236 cfg.g_pass = AOM_RC_FIRST_PASS;
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700237 stats = pass0(&raw, infile, encoder, &cfg, limit);
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800238
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700239 // Pass 1
240 rewind(infile);
Yaowu Xuf883b422016-08-30 14:01:10 -0700241 cfg.g_pass = AOM_RC_LAST_PASS;
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700242 cfg.rc_twopass_stats_in = stats;
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700243 pass1(&raw, infile, outfile_arg, encoder, &cfg, limit);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800244 free(stats.buf);
245
Yaowu Xuf883b422016-08-30 14:01:10 -0700246 aom_img_free(&raw);
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700247 fclose(infile);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800248
249 return EXIT_SUCCESS;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800250}