xref: /aosp_15_r20/external/libaom/examples/set_maps.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
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 // AOM Set Active and ROI Maps
13*77c1e3ccSAndroid Build Coastguard Worker // ===========================
14*77c1e3ccSAndroid Build Coastguard Worker //
15*77c1e3ccSAndroid Build Coastguard Worker // This is an example demonstrating how to control the AOM encoder's
16*77c1e3ccSAndroid Build Coastguard Worker // ROI and Active maps.
17*77c1e3ccSAndroid Build Coastguard Worker //
18*77c1e3ccSAndroid Build Coastguard Worker // ROI (Region of Interest) maps are a way for the application to assign
19*77c1e3ccSAndroid Build Coastguard Worker // each macroblock in the image to a region, and then set quantizer and
20*77c1e3ccSAndroid Build Coastguard Worker // filtering parameters on that image.
21*77c1e3ccSAndroid Build Coastguard Worker //
22*77c1e3ccSAndroid Build Coastguard Worker // Active maps are a way for the application to specify on a
23*77c1e3ccSAndroid Build Coastguard Worker // macroblock-by-macroblock basis whether there is any activity in that
24*77c1e3ccSAndroid Build Coastguard Worker // macroblock.
25*77c1e3ccSAndroid Build Coastguard Worker //
26*77c1e3ccSAndroid Build Coastguard Worker //
27*77c1e3ccSAndroid Build Coastguard Worker // Configuration
28*77c1e3ccSAndroid Build Coastguard Worker // -------------
29*77c1e3ccSAndroid Build Coastguard Worker // An ROI map is set on frame 22. If the width of the image in macroblocks
30*77c1e3ccSAndroid Build Coastguard Worker // is evenly divisible by 4, then the output will appear to have distinct
31*77c1e3ccSAndroid Build Coastguard Worker // columns, where the quantizer, loopfilter, and static threshold differ
32*77c1e3ccSAndroid Build Coastguard Worker // from column to column.
33*77c1e3ccSAndroid Build Coastguard Worker //
34*77c1e3ccSAndroid Build Coastguard Worker // An active map is set on frame 33. If the width of the image in macroblocks
35*77c1e3ccSAndroid Build Coastguard Worker // is evenly divisible by 4, then the output will appear to have distinct
36*77c1e3ccSAndroid Build Coastguard Worker // columns, where one column will have motion and the next will not.
37*77c1e3ccSAndroid Build Coastguard Worker //
38*77c1e3ccSAndroid Build Coastguard Worker // The active map is cleared on frame 44.
39*77c1e3ccSAndroid Build Coastguard Worker //
40*77c1e3ccSAndroid Build Coastguard Worker // Observing The Effects
41*77c1e3ccSAndroid Build Coastguard Worker // ---------------------
42*77c1e3ccSAndroid Build Coastguard Worker // Use the `simple_decoder` example to decode this sample, and observe
43*77c1e3ccSAndroid Build Coastguard Worker // the change in the image at frames 22, 33, and 44.
44*77c1e3ccSAndroid Build Coastguard Worker 
45*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
46*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
47*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
48*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
49*77c1e3ccSAndroid Build Coastguard Worker 
50*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_encoder.h"
51*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aomcx.h"
52*77c1e3ccSAndroid Build Coastguard Worker #include "common/tools_common.h"
53*77c1e3ccSAndroid Build Coastguard Worker #include "common/video_writer.h"
54*77c1e3ccSAndroid Build Coastguard Worker 
55*77c1e3ccSAndroid Build Coastguard Worker static const char *exec_name;
56*77c1e3ccSAndroid Build Coastguard Worker 
usage_exit(void)57*77c1e3ccSAndroid Build Coastguard Worker void usage_exit(void) {
58*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr, "Usage: %s <codec> <width> <height> <infile> <outfile>\n",
59*77c1e3ccSAndroid Build Coastguard Worker           exec_name);
60*77c1e3ccSAndroid Build Coastguard Worker   exit(EXIT_FAILURE);
61*77c1e3ccSAndroid Build Coastguard Worker }
62*77c1e3ccSAndroid Build Coastguard Worker 
set_active_map(const aom_codec_enc_cfg_t * cfg,aom_codec_ctx_t * codec)63*77c1e3ccSAndroid Build Coastguard Worker static void set_active_map(const aom_codec_enc_cfg_t *cfg,
64*77c1e3ccSAndroid Build Coastguard Worker                            aom_codec_ctx_t *codec) {
65*77c1e3ccSAndroid Build Coastguard Worker   unsigned int i;
66*77c1e3ccSAndroid Build Coastguard Worker   aom_active_map_t map = { 0, 0, 0 };
67*77c1e3ccSAndroid Build Coastguard Worker 
68*77c1e3ccSAndroid Build Coastguard Worker   map.rows = (cfg->g_h + 15) / 16;
69*77c1e3ccSAndroid Build Coastguard Worker   map.cols = (cfg->g_w + 15) / 16;
70*77c1e3ccSAndroid Build Coastguard Worker 
71*77c1e3ccSAndroid Build Coastguard Worker   map.active_map = (uint8_t *)malloc(map.rows * map.cols);
72*77c1e3ccSAndroid Build Coastguard Worker   if (!map.active_map) die("Failed to allocate active map");
73*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < map.rows * map.cols; ++i) map.active_map[i] = i % 2;
74*77c1e3ccSAndroid Build Coastguard Worker 
75*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(codec, AOME_SET_ACTIVEMAP, &map))
76*77c1e3ccSAndroid Build Coastguard Worker     die_codec(codec, "Failed to set active map");
77*77c1e3ccSAndroid Build Coastguard Worker 
78*77c1e3ccSAndroid Build Coastguard Worker   free(map.active_map);
79*77c1e3ccSAndroid Build Coastguard Worker }
80*77c1e3ccSAndroid Build Coastguard Worker 
unset_active_map(const aom_codec_enc_cfg_t * cfg,aom_codec_ctx_t * codec)81*77c1e3ccSAndroid Build Coastguard Worker static void unset_active_map(const aom_codec_enc_cfg_t *cfg,
82*77c1e3ccSAndroid Build Coastguard Worker                              aom_codec_ctx_t *codec) {
83*77c1e3ccSAndroid Build Coastguard Worker   aom_active_map_t map = { 0, 0, 0 };
84*77c1e3ccSAndroid Build Coastguard Worker 
85*77c1e3ccSAndroid Build Coastguard Worker   map.rows = (cfg->g_h + 15) / 16;
86*77c1e3ccSAndroid Build Coastguard Worker   map.cols = (cfg->g_w + 15) / 16;
87*77c1e3ccSAndroid Build Coastguard Worker   map.active_map = NULL;
88*77c1e3ccSAndroid Build Coastguard Worker 
89*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(codec, AOME_SET_ACTIVEMAP, &map))
90*77c1e3ccSAndroid Build Coastguard Worker     die_codec(codec, "Failed to set active map");
91*77c1e3ccSAndroid Build Coastguard Worker }
92*77c1e3ccSAndroid Build Coastguard Worker 
encode_frame(aom_codec_ctx_t * codec,aom_image_t * img,int frame_index,AvxVideoWriter * writer)93*77c1e3ccSAndroid Build Coastguard Worker static int encode_frame(aom_codec_ctx_t *codec, aom_image_t *img,
94*77c1e3ccSAndroid Build Coastguard Worker                         int frame_index, AvxVideoWriter *writer) {
95*77c1e3ccSAndroid Build Coastguard Worker   int got_pkts = 0;
96*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_iter_t iter = NULL;
97*77c1e3ccSAndroid Build Coastguard Worker   const aom_codec_cx_pkt_t *pkt = NULL;
98*77c1e3ccSAndroid Build Coastguard Worker   const aom_codec_err_t res = aom_codec_encode(codec, img, frame_index, 1, 0);
99*77c1e3ccSAndroid Build Coastguard Worker   if (res != AOM_CODEC_OK) die_codec(codec, "Failed to encode frame");
100*77c1e3ccSAndroid Build Coastguard Worker 
101*77c1e3ccSAndroid Build Coastguard Worker   while ((pkt = aom_codec_get_cx_data(codec, &iter)) != NULL) {
102*77c1e3ccSAndroid Build Coastguard Worker     got_pkts = 1;
103*77c1e3ccSAndroid Build Coastguard Worker 
104*77c1e3ccSAndroid Build Coastguard Worker     if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
105*77c1e3ccSAndroid Build Coastguard Worker       const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
106*77c1e3ccSAndroid Build Coastguard Worker       if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
107*77c1e3ccSAndroid Build Coastguard Worker                                         pkt->data.frame.sz,
108*77c1e3ccSAndroid Build Coastguard Worker                                         pkt->data.frame.pts)) {
109*77c1e3ccSAndroid Build Coastguard Worker         die_codec(codec, "Failed to write compressed frame");
110*77c1e3ccSAndroid Build Coastguard Worker       }
111*77c1e3ccSAndroid Build Coastguard Worker 
112*77c1e3ccSAndroid Build Coastguard Worker       printf(keyframe ? "K" : ".");
113*77c1e3ccSAndroid Build Coastguard Worker       fflush(stdout);
114*77c1e3ccSAndroid Build Coastguard Worker     }
115*77c1e3ccSAndroid Build Coastguard Worker   }
116*77c1e3ccSAndroid Build Coastguard Worker 
117*77c1e3ccSAndroid Build Coastguard Worker   return got_pkts;
118*77c1e3ccSAndroid Build Coastguard Worker }
119*77c1e3ccSAndroid Build Coastguard Worker 
main(int argc,char ** argv)120*77c1e3ccSAndroid Build Coastguard Worker int main(int argc, char **argv) {
121*77c1e3ccSAndroid Build Coastguard Worker   FILE *infile = NULL;
122*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_ctx_t codec;
123*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_enc_cfg_t cfg;
124*77c1e3ccSAndroid Build Coastguard Worker   int frame_count = 0;
125*77c1e3ccSAndroid Build Coastguard Worker   const int limit = 10;
126*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t raw;
127*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t res;
128*77c1e3ccSAndroid Build Coastguard Worker   AvxVideoInfo info;
129*77c1e3ccSAndroid Build Coastguard Worker   AvxVideoWriter *writer = NULL;
130*77c1e3ccSAndroid Build Coastguard Worker   const int fps = 2;  // TODO(dkovalev) add command line argument
131*77c1e3ccSAndroid Build Coastguard Worker   const double bits_per_pixel_per_frame = 0.067;
132*77c1e3ccSAndroid Build Coastguard Worker 
133*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_REALTIME_ONLY
134*77c1e3ccSAndroid Build Coastguard Worker   const int usage = 1;
135*77c1e3ccSAndroid Build Coastguard Worker   const int speed = 7;
136*77c1e3ccSAndroid Build Coastguard Worker #else
137*77c1e3ccSAndroid Build Coastguard Worker   const int usage = 0;
138*77c1e3ccSAndroid Build Coastguard Worker   const int speed = 2;
139*77c1e3ccSAndroid Build Coastguard Worker #endif
140*77c1e3ccSAndroid Build Coastguard Worker 
141*77c1e3ccSAndroid Build Coastguard Worker   exec_name = argv[0];
142*77c1e3ccSAndroid Build Coastguard Worker   if (argc != 6) die("Invalid number of arguments");
143*77c1e3ccSAndroid Build Coastguard Worker 
144*77c1e3ccSAndroid Build Coastguard Worker   memset(&info, 0, sizeof(info));
145*77c1e3ccSAndroid Build Coastguard Worker 
146*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_iface_t *encoder = get_aom_encoder_by_short_name(argv[1]);
147*77c1e3ccSAndroid Build Coastguard Worker   if (encoder == NULL) {
148*77c1e3ccSAndroid Build Coastguard Worker     die("Unsupported codec.");
149*77c1e3ccSAndroid Build Coastguard Worker   }
150*77c1e3ccSAndroid Build Coastguard Worker   assert(encoder != NULL);
151*77c1e3ccSAndroid Build Coastguard Worker   info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
152*77c1e3ccSAndroid Build Coastguard Worker   info.frame_width = (int)strtol(argv[2], NULL, 0);
153*77c1e3ccSAndroid Build Coastguard Worker   info.frame_height = (int)strtol(argv[3], NULL, 0);
154*77c1e3ccSAndroid Build Coastguard Worker   info.time_base.numerator = 1;
155*77c1e3ccSAndroid Build Coastguard Worker   info.time_base.denominator = fps;
156*77c1e3ccSAndroid Build Coastguard Worker 
157*77c1e3ccSAndroid Build Coastguard Worker   if (info.frame_width <= 0 || info.frame_height <= 0 ||
158*77c1e3ccSAndroid Build Coastguard Worker       (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
159*77c1e3ccSAndroid Build Coastguard Worker     die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
160*77c1e3ccSAndroid Build Coastguard Worker   }
161*77c1e3ccSAndroid Build Coastguard Worker 
162*77c1e3ccSAndroid Build Coastguard Worker   if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width,
163*77c1e3ccSAndroid Build Coastguard Worker                      info.frame_height, 1)) {
164*77c1e3ccSAndroid Build Coastguard Worker     die("Failed to allocate image.");
165*77c1e3ccSAndroid Build Coastguard Worker   }
166*77c1e3ccSAndroid Build Coastguard Worker 
167*77c1e3ccSAndroid Build Coastguard Worker   printf("Using %s\n", aom_codec_iface_name(encoder));
168*77c1e3ccSAndroid Build Coastguard Worker 
169*77c1e3ccSAndroid Build Coastguard Worker   res = aom_codec_enc_config_default(encoder, &cfg, usage);
170*77c1e3ccSAndroid Build Coastguard Worker   if (res) die_codec(&codec, "Failed to get default codec config.");
171*77c1e3ccSAndroid Build Coastguard Worker 
172*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_w = info.frame_width;
173*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_h = info.frame_height;
174*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_timebase.num = info.time_base.numerator;
175*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_timebase.den = info.time_base.denominator;
176*77c1e3ccSAndroid Build Coastguard Worker   cfg.rc_target_bitrate =
177*77c1e3ccSAndroid Build Coastguard Worker       (unsigned int)(bits_per_pixel_per_frame * cfg.g_w * cfg.g_h * fps / 1000);
178*77c1e3ccSAndroid Build Coastguard Worker   cfg.g_lag_in_frames = 0;
179*77c1e3ccSAndroid Build Coastguard Worker 
180*77c1e3ccSAndroid Build Coastguard Worker   writer = aom_video_writer_open(argv[5], kContainerIVF, &info);
181*77c1e3ccSAndroid Build Coastguard Worker   if (!writer) die("Failed to open %s for writing.", argv[5]);
182*77c1e3ccSAndroid Build Coastguard Worker 
183*77c1e3ccSAndroid Build Coastguard Worker   if (!(infile = fopen(argv[4], "rb")))
184*77c1e3ccSAndroid Build Coastguard Worker     die("Failed to open %s for reading.", argv[4]);
185*77c1e3ccSAndroid Build Coastguard Worker 
186*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_enc_init(&codec, encoder, &cfg, 0))
187*77c1e3ccSAndroid Build Coastguard Worker     die("Failed to initialize encoder");
188*77c1e3ccSAndroid Build Coastguard Worker 
189*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_control(&codec, AOME_SET_CPUUSED, speed))
190*77c1e3ccSAndroid Build Coastguard Worker     die_codec(&codec, "Failed to set cpu-used");
191*77c1e3ccSAndroid Build Coastguard Worker 
192*77c1e3ccSAndroid Build Coastguard Worker   // Encode frames.
193*77c1e3ccSAndroid Build Coastguard Worker   while (aom_img_read(&raw, infile) && frame_count < limit) {
194*77c1e3ccSAndroid Build Coastguard Worker     ++frame_count;
195*77c1e3ccSAndroid Build Coastguard Worker 
196*77c1e3ccSAndroid Build Coastguard Worker     if (frame_count == 5) {
197*77c1e3ccSAndroid Build Coastguard Worker       set_active_map(&cfg, &codec);
198*77c1e3ccSAndroid Build Coastguard Worker     } else if (frame_count == 9) {
199*77c1e3ccSAndroid Build Coastguard Worker       unset_active_map(&cfg, &codec);
200*77c1e3ccSAndroid Build Coastguard Worker     }
201*77c1e3ccSAndroid Build Coastguard Worker 
202*77c1e3ccSAndroid Build Coastguard Worker     encode_frame(&codec, &raw, frame_count, writer);
203*77c1e3ccSAndroid Build Coastguard Worker   }
204*77c1e3ccSAndroid Build Coastguard Worker 
205*77c1e3ccSAndroid Build Coastguard Worker   // Flush encoder.
206*77c1e3ccSAndroid Build Coastguard Worker   while (encode_frame(&codec, NULL, -1, writer)) {
207*77c1e3ccSAndroid Build Coastguard Worker   }
208*77c1e3ccSAndroid Build Coastguard Worker 
209*77c1e3ccSAndroid Build Coastguard Worker   printf("\n");
210*77c1e3ccSAndroid Build Coastguard Worker   fclose(infile);
211*77c1e3ccSAndroid Build Coastguard Worker   printf("Processed %d frames.\n", frame_count);
212*77c1e3ccSAndroid Build Coastguard Worker 
213*77c1e3ccSAndroid Build Coastguard Worker   aom_img_free(&raw);
214*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
215*77c1e3ccSAndroid Build Coastguard Worker 
216*77c1e3ccSAndroid Build Coastguard Worker   aom_video_writer_close(writer);
217*77c1e3ccSAndroid Build Coastguard Worker 
218*77c1e3ccSAndroid Build Coastguard Worker   return EXIT_SUCCESS;
219*77c1e3ccSAndroid Build Coastguard Worker }
220