1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
14*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
15*77c1e3ccSAndroid Build Coastguard Worker
16*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_encoder.h"
17*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aomcx.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "common/tools_common.h"
19*77c1e3ccSAndroid Build Coastguard Worker #include "common/video_writer.h"
20*77c1e3ccSAndroid Build Coastguard Worker
21*77c1e3ccSAndroid Build Coastguard Worker static const char *exec_name;
22*77c1e3ccSAndroid Build Coastguard Worker
usage_exit(void)23*77c1e3ccSAndroid Build Coastguard Worker void usage_exit(void) {
24*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr,
25*77c1e3ccSAndroid Build Coastguard Worker "lossless_encoder: Example demonstrating lossless "
26*77c1e3ccSAndroid Build Coastguard Worker "encoding feature. Supports raw input only.\n");
27*77c1e3ccSAndroid Build Coastguard Worker fprintf(stderr, "Usage: %s <width> <height> <infile> <outfile>\n", exec_name);
28*77c1e3ccSAndroid Build Coastguard Worker exit(EXIT_FAILURE);
29*77c1e3ccSAndroid Build Coastguard Worker }
30*77c1e3ccSAndroid Build Coastguard Worker
encode_frame(aom_codec_ctx_t * codec,aom_image_t * img,int frame_index,int flags,AvxVideoWriter * writer)31*77c1e3ccSAndroid Build Coastguard Worker static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img,
32*77c1e3ccSAndroid Build Coastguard Worker int frame_index, int flags, AvxVideoWriter *writer) {
33*77c1e3ccSAndroid Build Coastguard Worker int got_pkts = 0;
34*77c1e3ccSAndroid Build Coastguard Worker aom_codec_iter_t iter = NULL;
35*77c1e3ccSAndroid Build Coastguard Worker const aom_codec_cx_pkt_t *pkt = NULL;
36*77c1e3ccSAndroid Build Coastguard Worker const aom_codec_err_t res =
37*77c1e3ccSAndroid Build Coastguard Worker aom_codec_encode(codec, img, frame_index, 1, flags);
38*77c1e3ccSAndroid Build Coastguard Worker if (res != AOM_CODEC_OK) die_codec(codec, "Failed to encode frame");
39*77c1e3ccSAndroid Build Coastguard Worker
40*77c1e3ccSAndroid Build Coastguard Worker while ((pkt = aom_codec_get_cx_data(codec, &iter)) != NULL) {
41*77c1e3ccSAndroid Build Coastguard Worker got_pkts = 1;
42*77c1e3ccSAndroid Build Coastguard Worker
43*77c1e3ccSAndroid Build Coastguard Worker if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
44*77c1e3ccSAndroid Build Coastguard Worker const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
45*77c1e3ccSAndroid Build Coastguard Worker if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
46*77c1e3ccSAndroid Build Coastguard Worker pkt->data.frame.sz,
47*77c1e3ccSAndroid Build Coastguard Worker pkt->data.frame.pts)) {
48*77c1e3ccSAndroid Build Coastguard Worker die_codec(codec, "Failed to write compressed frame");
49*77c1e3ccSAndroid Build Coastguard Worker }
50*77c1e3ccSAndroid Build Coastguard Worker printf(keyframe ? "K" : ".");
51*77c1e3ccSAndroid Build Coastguard Worker fflush(stdout);
52*77c1e3ccSAndroid Build Coastguard Worker }
53*77c1e3ccSAndroid Build Coastguard Worker }
54*77c1e3ccSAndroid Build Coastguard Worker
55*77c1e3ccSAndroid Build Coastguard Worker return got_pkts;
56*77c1e3ccSAndroid Build Coastguard Worker }
57*77c1e3ccSAndroid Build Coastguard Worker
main(int argc,char ** argv)58*77c1e3ccSAndroid Build Coastguard Worker int main(int argc, char **argv) {
59*77c1e3ccSAndroid Build Coastguard Worker FILE *infile = NULL;
60*77c1e3ccSAndroid Build Coastguard Worker aom_codec_enc_cfg_t cfg;
61*77c1e3ccSAndroid Build Coastguard Worker int frame_count = 0;
62*77c1e3ccSAndroid Build Coastguard Worker aom_image_t raw;
63*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t res;
64*77c1e3ccSAndroid Build Coastguard Worker AvxVideoInfo info;
65*77c1e3ccSAndroid Build Coastguard Worker AvxVideoWriter *writer = NULL;
66*77c1e3ccSAndroid Build Coastguard Worker const int fps = 30;
67*77c1e3ccSAndroid Build Coastguard Worker
68*77c1e3ccSAndroid Build Coastguard Worker exec_name = argv[0];
69*77c1e3ccSAndroid Build Coastguard Worker
70*77c1e3ccSAndroid Build Coastguard Worker // Clear explicitly, as simply assigning "{ 0 }" generates
71*77c1e3ccSAndroid Build Coastguard Worker // "missing-field-initializers" warning in some compilers.
72*77c1e3ccSAndroid Build Coastguard Worker memset(&info, 0, sizeof(info));
73*77c1e3ccSAndroid Build Coastguard Worker
74*77c1e3ccSAndroid Build Coastguard Worker if (argc < 5) die("Invalid number of arguments");
75*77c1e3ccSAndroid Build Coastguard Worker
76*77c1e3ccSAndroid Build Coastguard Worker aom_codec_iface_t *encoder = get_aom_encoder_by_short_name("av1");
77*77c1e3ccSAndroid Build Coastguard Worker if (!encoder) die("Unsupported codec.");
78*77c1e3ccSAndroid Build Coastguard Worker
79*77c1e3ccSAndroid Build Coastguard Worker info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
80*77c1e3ccSAndroid Build Coastguard Worker info.frame_width = (int)strtol(argv[1], NULL, 0);
81*77c1e3ccSAndroid Build Coastguard Worker info.frame_height = (int)strtol(argv[2], NULL, 0);
82*77c1e3ccSAndroid Build Coastguard Worker info.time_base.numerator = 1;
83*77c1e3ccSAndroid Build Coastguard Worker info.time_base.denominator = fps;
84*77c1e3ccSAndroid Build Coastguard Worker
85*77c1e3ccSAndroid Build Coastguard Worker if (info.frame_width <= 0 || info.frame_height <= 0 ||
86*77c1e3ccSAndroid Build Coastguard Worker (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
87*77c1e3ccSAndroid Build Coastguard Worker die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
88*77c1e3ccSAndroid Build Coastguard Worker }
89*77c1e3ccSAndroid Build Coastguard Worker
90*77c1e3ccSAndroid Build Coastguard Worker if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width,
91*77c1e3ccSAndroid Build Coastguard Worker info.frame_height, 1)) {
92*77c1e3ccSAndroid Build Coastguard Worker die("Failed to allocate image.");
93*77c1e3ccSAndroid Build Coastguard Worker }
94*77c1e3ccSAndroid Build Coastguard Worker
95*77c1e3ccSAndroid Build Coastguard Worker printf("Using %s\n", aom_codec_iface_name(encoder));
96*77c1e3ccSAndroid Build Coastguard Worker
97*77c1e3ccSAndroid Build Coastguard Worker aom_codec_ctx_t codec;
98*77c1e3ccSAndroid Build Coastguard Worker res = aom_codec_enc_config_default(encoder, &cfg, 0);
99*77c1e3ccSAndroid Build Coastguard Worker if (res) die_codec(&codec, "Failed to get default codec config.");
100*77c1e3ccSAndroid Build Coastguard Worker
101*77c1e3ccSAndroid Build Coastguard Worker cfg.g_w = info.frame_width;
102*77c1e3ccSAndroid Build Coastguard Worker cfg.g_h = info.frame_height;
103*77c1e3ccSAndroid Build Coastguard Worker cfg.g_timebase.num = info.time_base.numerator;
104*77c1e3ccSAndroid Build Coastguard Worker cfg.g_timebase.den = info.time_base.denominator;
105*77c1e3ccSAndroid Build Coastguard Worker
106*77c1e3ccSAndroid Build Coastguard Worker writer = aom_video_writer_open(argv[4], kContainerIVF, &info);
107*77c1e3ccSAndroid Build Coastguard Worker if (!writer) die("Failed to open %s for writing.", argv[4]);
108*77c1e3ccSAndroid Build Coastguard Worker
109*77c1e3ccSAndroid Build Coastguard Worker if (!(infile = fopen(argv[3], "rb")))
110*77c1e3ccSAndroid Build Coastguard Worker die("Failed to open %s for reading.", argv[3]);
111*77c1e3ccSAndroid Build Coastguard Worker
112*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_enc_init(&codec, encoder, &cfg, 0))
113*77c1e3ccSAndroid Build Coastguard Worker die("Failed to initialize encoder");
114*77c1e3ccSAndroid Build Coastguard Worker
115*77c1e3ccSAndroid Build Coastguard Worker if (AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1E_SET_LOSSLESS, 1))
116*77c1e3ccSAndroid Build Coastguard Worker die_codec(&codec, "Failed to use lossless mode");
117*77c1e3ccSAndroid Build Coastguard Worker
118*77c1e3ccSAndroid Build Coastguard Worker // Encode frames.
119*77c1e3ccSAndroid Build Coastguard Worker while (aom_img_read(&raw, infile)) {
120*77c1e3ccSAndroid Build Coastguard Worker encode_frame(&codec, &raw, frame_count++, 0, writer);
121*77c1e3ccSAndroid Build Coastguard Worker }
122*77c1e3ccSAndroid Build Coastguard Worker
123*77c1e3ccSAndroid Build Coastguard Worker // Flush encoder.
124*77c1e3ccSAndroid Build Coastguard Worker while (encode_frame(&codec, NULL, -1, 0, writer)) {
125*77c1e3ccSAndroid Build Coastguard Worker }
126*77c1e3ccSAndroid Build Coastguard Worker
127*77c1e3ccSAndroid Build Coastguard Worker printf("\n");
128*77c1e3ccSAndroid Build Coastguard Worker fclose(infile);
129*77c1e3ccSAndroid Build Coastguard Worker printf("Processed %d frames.\n", frame_count);
130*77c1e3ccSAndroid Build Coastguard Worker
131*77c1e3ccSAndroid Build Coastguard Worker aom_img_free(&raw);
132*77c1e3ccSAndroid Build Coastguard Worker if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
133*77c1e3ccSAndroid Build Coastguard Worker
134*77c1e3ccSAndroid Build Coastguard Worker aom_video_writer_close(writer);
135*77c1e3ccSAndroid Build Coastguard Worker
136*77c1e3ccSAndroid Build Coastguard Worker return EXIT_SUCCESS;
137*77c1e3ccSAndroid Build Coastguard Worker }
138