blob: 242db7b75cf5b1f343b80664d7003e2c310e02c1 [file] [log] [blame]
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -07001/*
Yaowu Xu6e0d64c2016-10-10 16:21:45 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -07003 *
Yaowu Xu6e0d64c2016-10-10 16:21:45 -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 Kovalevcb1f97e2014-09-09 18:51:41 -070010 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "aom/aom_encoder.h"
17#include "aom/aomcx.h"
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070018
Tom Finegan9e96bdc2015-02-04 16:11:57 -080019#include "../tools_common.h"
20#include "../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_ctx_t codec;
62 aom_codec_enc_cfg_t cfg;
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070063 int frame_count = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -070064 aom_image_t raw;
65 aom_codec_err_t res;
Urvang Joshid71a2312016-07-14 12:33:48 -070066 AvxVideoInfo info;
Yaowu Xuf883b422016-08-30 14:01:10 -070067 AvxVideoWriter *writer = NULL;
68 const AvxInterface *encoder = NULL;
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070069 const int fps = 30;
70
71 exec_name = argv[0];
72
Urvang Joshid71a2312016-07-14 12:33:48 -070073 // Clear explicitly, as simply assigning "{ 0 }" generates
74 // "missing-field-initializers" warning in some compilers.
75 memset(&info, 0, sizeof(info));
76
clang-format397d9642016-08-08 18:51:16 -070077 if (argc < 5) die("Invalid number of arguments");
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070078
Yaowu Xuf883b422016-08-30 14:01:10 -070079 encoder = get_aom_encoder_by_name("av1");
clang-format397d9642016-08-08 18:51:16 -070080 if (!encoder) die("Unsupported codec.");
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070081
82 info.codec_fourcc = encoder->fourcc;
James Zern097fef92017-03-23 00:25:55 -070083 info.frame_width = (int)strtol(argv[1], NULL, 0);
84 info.frame_height = (int)strtol(argv[2], NULL, 0);
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070085 info.time_base.numerator = 1;
86 info.time_base.denominator = fps;
87
clang-format397d9642016-08-08 18:51:16 -070088 if (info.frame_width <= 0 || info.frame_height <= 0 ||
89 (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070090 die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
91 }
92
Yaowu Xuf883b422016-08-30 14:01:10 -070093 if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width,
clang-format397d9642016-08-08 18:51:16 -070094 info.frame_height, 1)) {
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070095 die("Failed to allocate image.");
96 }
97
Yaowu Xuf883b422016-08-30 14:01:10 -070098 printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -070099
Yaowu Xuf883b422016-08-30 14:01:10 -0700100 res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
clang-format397d9642016-08-08 18:51:16 -0700101 if (res) die_codec(&codec, "Failed to get default codec config.");
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700102
103 cfg.g_w = info.frame_width;
104 cfg.g_h = info.frame_height;
105 cfg.g_timebase.num = info.time_base.numerator;
106 cfg.g_timebase.den = info.time_base.denominator;
107
Yaowu Xuf883b422016-08-30 14:01:10 -0700108 writer = aom_video_writer_open(argv[4], kContainerIVF, &info);
clang-format397d9642016-08-08 18:51:16 -0700109 if (!writer) die("Failed to open %s for writing.", argv[4]);
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700110
111 if (!(infile = fopen(argv[3], "rb")))
112 die("Failed to open %s for reading.", argv[3]);
113
Yaowu Xuf883b422016-08-30 14:01:10 -0700114 if (aom_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700115 die_codec(&codec, "Failed to initialize encoder");
116
Yaowu Xuf883b422016-08-30 14:01:10 -0700117 if (aom_codec_control_(&codec, AV1E_SET_LOSSLESS, 1))
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700118 die_codec(&codec, "Failed to use lossless mode");
119
120 // Encode frames.
Yaowu Xuf883b422016-08-30 14:01:10 -0700121 while (aom_img_read(&raw, infile)) {
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700122 encode_frame(&codec, &raw, frame_count++, 0, writer);
123 }
124
125 // Flush encoder.
clang-format397d9642016-08-08 18:51:16 -0700126 while (encode_frame(&codec, NULL, -1, 0, writer)) {
127 }
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700128
129 printf("\n");
130 fclose(infile);
131 printf("Processed %d frames.\n", frame_count);
132
Yaowu Xuf883b422016-08-30 14:01:10 -0700133 aom_img_free(&raw);
134 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700135
Yaowu Xuf883b422016-08-30 14:01:10 -0700136 aom_video_writer_close(writer);
Dmitry Kovalevcb1f97e2014-09-09 18:51:41 -0700137
138 return EXIT_SUCCESS;
139}