blob: 4c2b16c3da365234614b2a8028090d459e74cc3b [file] [log] [blame]
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -07001/*
Lester Lu6bc30d62021-12-16 19:13:21 +00002 * Copyright (c) 2021, Alliance for Open Media. All rights reserved
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -07003 *
Lester Lu6bc30d62021-12-16 19:13:21 +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 Kovalevcb1f97e2014-09-09 18:51:41 -070011 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
Yaowu Xuf883b422016-08-30 14:01:10 -070017#include "aom/aom_encoder.h"
18#include "aom/aomcx.h"
Tom Finegan77902132018-05-21 10:19:15 -070019#include "common/tools_common.h"
20#include "common/video_writer.h"
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070021
22static const char *exec_name;
23
James Zern59e7a472015-05-09 10:33:26 -070024void usage_exit(void) {
clang-format397d9642016-08-08 18:51:16 -070025 fprintf(stderr,
26 "lossless_encoder: Example demonstrating lossless "
27 "encoding feature. Supports raw input only.\n");
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070028 fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile>\n", exec_name);
29 exit(EXIT_FAILURE);
30}
31
Yaowu Xuf883b422016-08-30 14:01:10 -070032static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img,
33 int frame_index, int flags, AvxVideoWriter *writer) {
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070034 int got_pkts = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070035 aom_codec_iter_t iter = NULL;
36 const aom_codec_cx_pkt_t *pkt = NULL;
37 const aom_codec_err_t res =
Sean DuBois47cc2552018-01-23 07:44:16 +000038 aom_codec_encode(codec, img, frame_index, 1, flags);
Yaowu Xuf883b422016-08-30 14:01:10 -070039 if (res != AOM_CODEC_OK) die_codec(codec, "Failed to encode frame");
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070040
Yaowu Xuf883b422016-08-30 14:01:10 -070041 while ((pkt = aom_codec_get_cx_data(codec, &iter)) != NULL) {
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070042 got_pkts = 1;
43
Yaowu Xuf883b422016-08-30 14:01:10 -070044 if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
45 const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
46 if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070047 pkt->data.frame.sz,
48 pkt->data.frame.pts)) {
49 die_codec(codec, "Failed to write compressed frame");
50 }
51 printf(keyframe ? "K" : ".");
52 fflush(stdout);
53 }
54 }
55
56 return got_pkts;
57}
58
59int main(int argc, char **argv) {
60 FILE *infile = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -070061 aom_codec_enc_cfg_t cfg;
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070062 int frame_count = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070063 aom_image_t raw;
64 aom_codec_err_t res;
Urvang Joshid71a2312016-07-14 12:33:48 -070065 AvxVideoInfo info;
Yaowu Xuf883b422016-08-30 14:01:10 -070066 AvxVideoWriter *writer = NULL;
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070067 const int fps = 30;
68
69 exec_name = argv[0];
70
Urvang Joshid71a2312016-07-14 12:33:48 -070071 // Clear explicitly, as simply assigning "{ 0 }" generates
72 // "missing-field-initializers" warning in some compilers.
73 memset(&info, 0, sizeof(info));
74
clang-format397d9642016-08-08 18:51:16 -070075 if (argc < 5) die("Invalid number of arguments");
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070076
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -070077 aom_codec_iface_t *encoder = get_aom_encoder_by_short_name("av1");
clang-format397d9642016-08-08 18:51:16 -070078 if (!encoder) die("Unsupported codec.");
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070079
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -070080 info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
James Zern097fef92017-03-23 00:25:55 -070081 info.frame_width = (int)strtol(argv[1], NULL, 0);
82 info.frame_height = (int)strtol(argv[2], NULL, 0);
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070083 info.time_base.numerator = 1;
84 info.time_base.denominator = fps;
85
clang-format397d9642016-08-08 18:51:16 -070086 if (info.frame_width <= 0 || info.frame_height <= 0 ||
87 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070088 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
89 }
90
Yaowu Xuf883b422016-08-30 14:01:10 -070091 if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width,
clang-format397d9642016-08-08 18:51:16 -070092 info.frame_height, 1)) {
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070093 die("Failed to allocate image.");
94 }
95
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -070096 printf("Using %s\n", aom_codec_iface_name(encoder));
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070097
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -070098 aom_codec_ctx_t codec;
99 res = aom_codec_enc_config_default(encoder, &cfg, 0);
clang-format397d9642016-08-08 18:51:16 -0700100 if (res) die_codec(&codec, "Failed to get default codec config.");
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700101
102 cfg.g_w = info.frame_width;
103 cfg.g_h = info.frame_height;
104 cfg.g_timebase.num = info.time_base.numerator;
105 cfg.g_timebase.den = info.time_base.denominator;
106
Yaowu Xuf883b422016-08-30 14:01:10 -0700107 writer = aom_video_writer_open(argv[4], kContainerIVF, &info);
clang-format397d9642016-08-08 18:51:16 -0700108 if (!writer) die("Failed to open %s for writing.", argv[4]);
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700109
110 if (!(infile = fopen(argv[3], "rb")))
111 die("Failed to open %s for reading.", argv[3]);
112
Elliott Karpilovskycbe219b2020-04-22 16:21:06 -0700113 if (aom_codec_enc_init(&codec, encoder, &cfg, 0))
James Zerndad374f2020-05-06 19:13:19 -0700114 die("Failed to initialize encoder");
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700115
Elliott Karpilovsky60dd0f92020-04-30 19:19:27 -0700116 if (AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1E_SET_LOSSLESS, 1))
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700117 die_codec(&codec, "Failed to use lossless mode");
118
119 // Encode frames.
Yaowu Xuf883b422016-08-30 14:01:10 -0700120 while (aom_img_read(&raw, infile)) {
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700121 encode_frame(&codec, &raw, frame_count++, 0, writer);
122 }
123
124 // Flush encoder.
clang-format397d9642016-08-08 18:51:16 -0700125 while (encode_frame(&codec, NULL, -1, 0, writer)) {
126 }
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700127
128 printf("\n");
129 fclose(infile);
130 printf("Processed %d frames.\n", frame_count);
131
Yaowu Xuf883b422016-08-30 14:01:10 -0700132 aom_img_free(&raw);
133 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700134
Yaowu Xuf883b422016-08-30 14:01:10 -0700135 aom_video_writer_close(writer);
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700136
137 return EXIT_SUCCESS;
138}