blob: 330be7be6eb9ca3a6b0430bb750bd13552e3a9f1 [file] [log] [blame]
Dmitry Kovalev50fa5852014-01-07 15:15:25 -08001/*
Krishna Rapaka7319db52021-09-28 20:35:29 -07002 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
Dmitry Kovalev50fa5852014-01-07 15:15:25 -08003 *
Vibhoothi41c6dd72021-10-12 18:48:26 +00004 * This source code is subject to the terms of the BSD 3-Clause Clear License
5 * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear
6 * License was not distributed with this source code in the LICENSE file, you
7 * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the
8 * Alliance for Open Media Patent License 1.0 was not distributed with this
9 * source code in the PATENTS file, you can obtain it at
10 * aomedia.org/license/patent-license/.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080011 */
12
13// Two Pass Encoder
14// ================
15//
16// This is an example of a two pass encoder loop. It takes an input file in
17// YV12 format, passes it through the encoder twice, and writes the compressed
18// frames to disk in IVF format. It builds upon the simple_encoder example.
19//
20// Twopass Variables
21// -----------------
22// Twopass mode needs to track the current pass number and the buffer of
23// statistics packets.
24//
25// Updating The Configuration
26// ---------------------------------
27// In two pass mode, the configuration has to be updated on each pass. The
28// statistics buffer is passed on the last pass.
29//
30// Encoding A Frame
31// ----------------
32// Encoding a frame in two pass mode is identical to the simple encoder
Thomas Daede6eca8352017-03-17 14:14:12 -070033// example.
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080034//
35// Processing Statistics Packets
36// -----------------------------
Yaowu Xuf883b422016-08-30 14:01:10 -070037// Each packet of type `AOM_CODEC_CX_FRAME_PKT` contains the encoded data
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080038// for this frame. We write a IVF frame header, followed by the raw data.
39//
40//
41// Pass Progress Reporting
42// -----------------------------
43// It's sometimes helpful to see when each pass completes.
44//
45//
46// Clean-up
47// -----------------------------
48// Destruction of the encoder instance must be done on each pass. The
49// raw image should be destroyed at the end as usual.
50
51#include <stdio.h>
52#include <stdlib.h>
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080053#include <string.h>
Dmitry Kovalev592936b2014-02-07 11:37:39 -080054
Yaowu Xuf883b422016-08-30 14:01:10 -070055#include "aom/aom_encoder.h"
Yaowu Xu8d9fecb2020-04-27 15:21:02 -070056#include "aom/aomcx.h"
Tom Finegan77902132018-05-21 10:19:15 -070057#include "common/tools_common.h"
58#include "common/video_writer.h"
Dmitry Kovalev592936b2014-02-07 11:37:39 -080059
Dmitry Kovalev592936b2014-02-07 11:37:39 -080060static const char *exec_name;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080061
James Zern59e7a472015-05-09 10:33:26 -070062void usage_exit(void) {
Tom Finegan9d473412016-05-11 14:50:03 -070063 fprintf(stderr,
64 "Usage: %s <codec> <width> <height> <infile> <outfile> "
Jim Bankoskia65e7be2016-05-20 07:31:51 -070065 "<limit(optional)>\n",
Dmitry Kovalev70d96642014-02-11 21:12:23 -080066 exec_name);
Dmitry Kovalev592936b2014-02-07 11:37:39 -080067 exit(EXIT_FAILURE);
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080068}
69
Yaowu Xuf883b422016-08-30 14:01:10 -070070static int get_frame_stats(aom_codec_ctx_t *ctx, const aom_image_t *img,
71 aom_codec_pts_t pts, unsigned int duration,
Sean DuBois47cc2552018-01-23 07:44:16 +000072 aom_enc_frame_flags_t flags,
Yaowu Xuf883b422016-08-30 14:01:10 -070073 aom_fixed_buf_t *stats) {
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -070074 int got_pkts = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070075 aom_codec_iter_t iter = NULL;
76 const aom_codec_cx_pkt_t *pkt = NULL;
Sean DuBois47cc2552018-01-23 07:44:16 +000077 const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags);
Yaowu Xuf883b422016-08-30 14:01:10 -070078 if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to get frame stats.");
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080079
Yaowu Xuf883b422016-08-30 14:01:10 -070080 while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) {
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -070081 got_pkts = 1;
82
Yaowu Xuf883b422016-08-30 14:01:10 -070083 if (pkt->kind == AOM_CODEC_STATS_PKT) {
Dmitry Kovalev592936b2014-02-07 11:37:39 -080084 const uint8_t *const pkt_buf = pkt->data.twopass_stats.buf;
85 const size_t pkt_size = pkt->data.twopass_stats.sz;
86 stats->buf = realloc(stats->buf, stats->sz + pkt_size);
87 memcpy((uint8_t *)stats->buf + stats->sz, pkt_buf, pkt_size);
88 stats->sz += pkt_size;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080089 }
Dmitry Kovalev592936b2014-02-07 11:37:39 -080090 }
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -070091
92 return got_pkts;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -080093}
94
Yaowu Xuf883b422016-08-30 14:01:10 -070095static int encode_frame(aom_codec_ctx_t *ctx, const aom_image_t *img,
96 aom_codec_pts_t pts, unsigned int duration,
Sean DuBois47cc2552018-01-23 07:44:16 +000097 aom_enc_frame_flags_t flags, AvxVideoWriter *writer) {
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -070098 int got_pkts = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070099 aom_codec_iter_t iter = NULL;
100 const aom_codec_cx_pkt_t *pkt = NULL;
Sean DuBois47cc2552018-01-23 07:44:16 +0000101 const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags);
Yaowu Xuf883b422016-08-30 14:01:10 -0700102 if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to encode frame.");
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800103
Yaowu Xuf883b422016-08-30 14:01:10 -0700104 while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) {
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700105 got_pkts = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700106 if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
107 const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800108
Yaowu Xuf883b422016-08-30 14:01:10 -0700109 if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
clang-format397d9642016-08-08 18:51:16 -0700110 pkt->data.frame.sz,
111 pkt->data.frame.pts))
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800112 die_codec(ctx, "Failed to write compressed frame.");
113 printf(keyframe ? "K" : ".");
114 fflush(stdout);
115 }
116 }
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700117
118 return got_pkts;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800119}
120
Yaowu Xuf883b422016-08-30 14:01:10 -0700121static aom_fixed_buf_t pass0(aom_image_t *raw, FILE *infile,
Elliott Karpilovsky67e84d42020-04-26 16:03:39 -0700122 aom_codec_iface_t *encoder,
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700123 const aom_codec_enc_cfg_t *cfg, int limit) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700124 aom_codec_ctx_t codec;
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700125 int frame_count = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -0700126 aom_fixed_buf_t stats = { NULL, 0 };
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700127
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -0700128 if (aom_codec_enc_init(&codec, encoder, cfg, 0))
James Zerndad374f2020-05-06 19:13:19 -0700129 die("Failed to initialize encoder");
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700130
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700131 // Calculate frame statistics.
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700132 while (aom_img_read(raw, infile) && frame_count < limit) {
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700133 ++frame_count;
Sean DuBois47cc2552018-01-23 07:44:16 +0000134 get_frame_stats(&codec, raw, frame_count, 1, 0, &stats);
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700135 }
136
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700137 // Flush encoder.
Sean DuBois47cc2552018-01-23 07:44:16 +0000138 while (get_frame_stats(&codec, NULL, frame_count, 1, 0, &stats)) {
clang-format397d9642016-08-08 18:51:16 -0700139 }
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700140
141 printf("Pass 0 complete. Processed %d frames.\n", frame_count);
Yaowu Xuf883b422016-08-30 14:01:10 -0700142 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700143
144 return stats;
145}
146
Yaowu Xuf883b422016-08-30 14:01:10 -0700147static void pass1(aom_image_t *raw, FILE *infile, const char *outfile_name,
Elliott Karpilovsky67e84d42020-04-26 16:03:39 -0700148 aom_codec_iface_t *encoder, const aom_codec_enc_cfg_t *cfg,
149 int limit) {
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -0700150 AvxVideoInfo info = { get_fourcc_by_aom_encoder(encoder),
clang-format397d9642016-08-08 18:51:16 -0700151 cfg->g_w,
152 cfg->g_h,
Jonathan Matthewsa6e4f962018-07-13 16:01:28 +0100153 { cfg->g_timebase.num, cfg->g_timebase.den },
154 0 };
Yaowu Xuf883b422016-08-30 14:01:10 -0700155 AvxVideoWriter *writer = NULL;
156 aom_codec_ctx_t codec;
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700157 int frame_count = 0;
158
Yaowu Xuf883b422016-08-30 14:01:10 -0700159 writer = aom_video_writer_open(outfile_name, kContainerIVF, &info);
clang-format397d9642016-08-08 18:51:16 -0700160 if (!writer) die("Failed to open %s for writing", outfile_name);
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700161
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -0700162 if (aom_codec_enc_init(&codec, encoder, cfg, 0))
James Zerndad374f2020-05-06 19:13:19 -0700163 die("Failed to initialize encoder");
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700164
Yaowu Xu8d9fecb2020-04-27 15:21:02 -0700165 if (aom_codec_control(&codec, AOME_SET_CPUUSED, 2))
166 die_codec(&codec, "Failed to set cpu-used");
167
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700168 // Encode frames.
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700169 while (aom_img_read(raw, infile) && frame_count < limit) {
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700170 ++frame_count;
Sean DuBois47cc2552018-01-23 07:44:16 +0000171 encode_frame(&codec, raw, frame_count, 1, 0, writer);
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700172 }
173
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700174 // Flush encoder.
Sean DuBois47cc2552018-01-23 07:44:16 +0000175 while (encode_frame(&codec, NULL, -1, 1, 0, writer)) {
clang-format397d9642016-08-08 18:51:16 -0700176 }
Dmitry Kovalev1e82bde2014-08-25 15:29:20 -0700177
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700178 printf("\n");
179
Yaowu Xuf883b422016-08-30 14:01:10 -0700180 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700181
Yaowu Xuf883b422016-08-30 14:01:10 -0700182 aom_video_writer_close(writer);
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700183
184 printf("Pass 1 complete. Processed %d frames.\n", frame_count);
185}
186
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800187int main(int argc, char **argv) {
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800188 FILE *infile = NULL;
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700189 int w, h;
Yaowu Xuf883b422016-08-30 14:01:10 -0700190 aom_codec_ctx_t codec;
191 aom_codec_enc_cfg_t cfg;
192 aom_image_t raw;
193 aom_codec_err_t res;
194 aom_fixed_buf_t stats;
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700195
clang-format397d9642016-08-08 18:51:16 -0700196 const int fps = 30; // TODO(dkovalev) add command line argument
197 const int bitrate = 200; // kbit/s TODO(dkovalev) add command line argument
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800198 const char *const codec_arg = argv[1];
199 const char *const width_arg = argv[2];
200 const char *const height_arg = argv[3];
201 const char *const infile_arg = argv[4];
202 const char *const outfile_arg = argv[5];
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700203 int limit = 0;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800204 exec_name = argv[0];
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800205
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700206 if (argc < 6) die("Invalid number of arguments");
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800207
James Zern097fef92017-03-23 00:25:55 -0700208 if (argc > 6) limit = (int)strtol(argv[6], NULL, 0);
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700209
210 if (limit == 0) limit = 100;
Tom Finegan9d473412016-05-11 14:50:03 -0700211
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -0700212 aom_codec_iface_t *encoder = get_aom_encoder_by_short_name(codec_arg);
clang-format397d9642016-08-08 18:51:16 -0700213 if (!encoder) die("Unsupported codec.");
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800214
James Zern097fef92017-03-23 00:25:55 -0700215 w = (int)strtol(width_arg, NULL, 0);
216 h = (int)strtol(height_arg, NULL, 0);
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800217
clang-format397d9642016-08-08 18:51:16 -0700218 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700219 die("Invalid frame size: %dx%d", w, h);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800220
Yaowu Xuf883b422016-08-30 14:01:10 -0700221 if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, w, h, 1))
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700222 die("Failed to allocate image", w, h);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800223
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -0700224 printf("Using %s\n", aom_codec_iface_name(encoder));
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800225
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700226 // Configuration
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -0700227 res = aom_codec_enc_config_default(encoder, &cfg, 0);
clang-format397d9642016-08-08 18:51:16 -0700228 if (res) die_codec(&codec, "Failed to get default codec config.");
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800229
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700230 cfg.g_w = w;
231 cfg.g_h = h;
232 cfg.g_timebase.num = 1;
233 cfg.g_timebase.den = fps;
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800234 cfg.rc_target_bitrate = bitrate;
235
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700236 if (!(infile = fopen(infile_arg, "rb")))
237 die("Failed to open %s for reading", infile_arg);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800238
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700239 // Pass 0
Yaowu Xuf883b422016-08-30 14:01:10 -0700240 cfg.g_pass = AOM_RC_FIRST_PASS;
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700241 stats = pass0(&raw, infile, encoder, &cfg, limit);
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800242
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700243 // Pass 1
244 rewind(infile);
Yaowu Xuf883b422016-08-30 14:01:10 -0700245 cfg.g_pass = AOM_RC_LAST_PASS;
Jim Bankoskia65e7be2016-05-20 07:31:51 -0700246 pass1(&raw, infile, outfile_arg, encoder, &cfg, limit);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800247 free(stats.buf);
248
Yaowu Xuf883b422016-08-30 14:01:10 -0700249 aom_img_free(&raw);
Dmitry Kovalev393e5d92014-08-25 12:56:32 -0700250 fclose(infile);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800251
252 return EXIT_SUCCESS;
Dmitry Kovalev50fa5852014-01-07 15:15:25 -0800253}