• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017, Alliance for Open Media. All rights reserved
3  *
4  * 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.
10  */
11 
12 // This tool is a gadget for offline probability training.
13 // A binary executable aom_entropy_optimizer will be generated in tools/. It
14 // parses a binary file consisting of counts written in the format of
15 // FRAME_COUNTS in entropymode.h, and computes optimized probability tables
16 // and CDF tables, which will be written to a new c file optimized_probs.c
17 // according to format in the codebase.
18 //
19 // Command line: ./aom_entropy_optimizer [directory of the count file]
20 //
21 // The input file can either be generated by encoding a single clip by
22 // turning on entropy_stats experiment, or be collected at a larger scale at
23 // which a python script which will be provided soon can be used to aggregate
24 // multiple stats output.
25 
26 #include <assert.h>
27 #include <stdio.h>
28 
29 #include "config/aom_config.h"
30 
31 #include "av1/encoder/encoder.h"
32 
33 #define SPACES_PER_TAB 2
34 #define CDF_MAX_SIZE 16
35 
36 typedef unsigned int aom_count_type;
37 // A log file recording parsed counts
38 static FILE *logfile;  // TODO(yuec): make it a command line option
39 
counts_to_cdf(const aom_count_type * counts,aom_cdf_prob * cdf,int modes)40 static void counts_to_cdf(const aom_count_type *counts, aom_cdf_prob *cdf,
41                           int modes) {
42   int64_t csum[CDF_MAX_SIZE];
43   assert(modes <= CDF_MAX_SIZE);
44 
45   csum[0] = counts[0] + 1;
46   for (int i = 1; i < modes; ++i) csum[i] = counts[i] + 1 + csum[i - 1];
47 
48   for (int i = 0; i < modes; ++i) fprintf(logfile, "%d ", counts[i]);
49   fprintf(logfile, "\n");
50 
51   int64_t sum = csum[modes - 1];
52   const int64_t round_shift = sum >> 1;
53   for (int i = 0; i < modes; ++i) {
54     cdf[i] = (csum[i] * CDF_PROB_TOP + round_shift) / sum;
55     cdf[i] = AOMMIN(cdf[i], CDF_PROB_TOP - (modes - 1 + i) * 4);
56     cdf[i] = (i == 0) ? AOMMAX(cdf[i], 4) : AOMMAX(cdf[i], cdf[i - 1] + 4);
57   }
58 }
59 
parse_counts_for_cdf_opt(aom_count_type ** ct_ptr,FILE * const probsfile,int tabs,int dim_of_cts,int * cts_each_dim)60 static int parse_counts_for_cdf_opt(aom_count_type **ct_ptr,
61                                     FILE *const probsfile, int tabs,
62                                     int dim_of_cts, int *cts_each_dim) {
63   if (dim_of_cts < 1) {
64     fprintf(stderr, "The dimension of a counts vector should be at least 1!\n");
65     return 1;
66   }
67   const int total_modes = cts_each_dim[0];
68   if (dim_of_cts == 1) {
69     assert(total_modes <= CDF_MAX_SIZE);
70     aom_cdf_prob cdfs[CDF_MAX_SIZE];
71     aom_count_type *counts1d = *ct_ptr;
72 
73     counts_to_cdf(counts1d, cdfs, total_modes);
74     (*ct_ptr) += total_modes;
75 
76     if (tabs > 0) fprintf(probsfile, "%*c", tabs * SPACES_PER_TAB, ' ');
77     fprintf(probsfile, "AOM_CDF%d(", total_modes);
78     for (int k = 0; k < total_modes - 1; ++k) {
79       fprintf(probsfile, "%d", cdfs[k]);
80       if (k < total_modes - 2) fprintf(probsfile, ", ");
81     }
82     fprintf(probsfile, ")");
83   } else {
84     for (int k = 0; k < total_modes; ++k) {
85       int tabs_next_level;
86 
87       if (dim_of_cts == 2)
88         fprintf(probsfile, "%*c{ ", tabs * SPACES_PER_TAB, ' ');
89       else
90         fprintf(probsfile, "%*c{\n", tabs * SPACES_PER_TAB, ' ');
91       tabs_next_level = dim_of_cts == 2 ? 0 : tabs + 1;
92 
93       if (parse_counts_for_cdf_opt(ct_ptr, probsfile, tabs_next_level,
94                                    dim_of_cts - 1, cts_each_dim + 1)) {
95         return 1;
96       }
97 
98       if (dim_of_cts == 2) {
99         if (k == total_modes - 1)
100           fprintf(probsfile, " }\n");
101         else
102           fprintf(probsfile, " },\n");
103       } else {
104         if (k == total_modes - 1)
105           fprintf(probsfile, "%*c}\n", tabs * SPACES_PER_TAB, ' ');
106         else
107           fprintf(probsfile, "%*c},\n", tabs * SPACES_PER_TAB, ' ');
108       }
109     }
110   }
111   return 0;
112 }
113 
optimize_cdf_table(aom_count_type * counts,FILE * const probsfile,int dim_of_cts,int * cts_each_dim,char * prefix)114 static void optimize_cdf_table(aom_count_type *counts, FILE *const probsfile,
115                                int dim_of_cts, int *cts_each_dim,
116                                char *prefix) {
117   aom_count_type *ct_ptr = counts;
118 
119   fprintf(probsfile, "%s = {\n", prefix);
120   fprintf(logfile, "%s\n", prefix);
121   if (parse_counts_for_cdf_opt(&ct_ptr, probsfile, 1, dim_of_cts,
122                                cts_each_dim)) {
123     fprintf(probsfile, "Optimizer failed!\n");
124   }
125   fprintf(probsfile, "};\n\n");
126   fprintf(logfile, "============================\n");
127 }
128 
optimize_uv_mode(aom_count_type * counts,FILE * const probsfile,int dim_of_cts,int * cts_each_dim,char * prefix)129 static void optimize_uv_mode(aom_count_type *counts, FILE *const probsfile,
130                              int dim_of_cts, int *cts_each_dim, char *prefix) {
131   aom_count_type *ct_ptr = counts;
132 
133   fprintf(probsfile, "%s = {\n", prefix);
134   fprintf(probsfile, "%*c{\n", SPACES_PER_TAB, ' ');
135   fprintf(logfile, "%s\n", prefix);
136   cts_each_dim[2] = UV_INTRA_MODES - 1;
137   for (int k = 0; k < cts_each_dim[1]; ++k) {
138     fprintf(probsfile, "%*c{ ", 2 * SPACES_PER_TAB, ' ');
139     parse_counts_for_cdf_opt(&ct_ptr, probsfile, 0, dim_of_cts - 2,
140                              cts_each_dim + 2);
141     if (k + 1 == cts_each_dim[1]) {
142       fprintf(probsfile, " }\n");
143     } else {
144       fprintf(probsfile, " },\n");
145     }
146     ++ct_ptr;
147   }
148   fprintf(probsfile, "%*c},\n", SPACES_PER_TAB, ' ');
149   fprintf(probsfile, "%*c{\n", SPACES_PER_TAB, ' ');
150   cts_each_dim[2] = UV_INTRA_MODES;
151   parse_counts_for_cdf_opt(&ct_ptr, probsfile, 2, dim_of_cts - 1,
152                            cts_each_dim + 1);
153   fprintf(probsfile, "%*c}\n", SPACES_PER_TAB, ' ');
154   fprintf(probsfile, "};\n\n");
155   fprintf(logfile, "============================\n");
156 }
157 
optimize_cdf_table_var_modes_2d(aom_count_type * counts,FILE * const probsfile,int dim_of_cts,int * cts_each_dim,int * modes_each_ctx,char * prefix)158 static void optimize_cdf_table_var_modes_2d(aom_count_type *counts,
159                                             FILE *const probsfile,
160                                             int dim_of_cts, int *cts_each_dim,
161                                             int *modes_each_ctx, char *prefix) {
162   aom_count_type *ct_ptr = counts;
163 
164   assert(dim_of_cts == 2);
165   (void)dim_of_cts;
166 
167   fprintf(probsfile, "%s = {\n", prefix);
168   fprintf(logfile, "%s\n", prefix);
169 
170   for (int d0_idx = 0; d0_idx < cts_each_dim[0]; ++d0_idx) {
171     int num_of_modes = modes_each_ctx[d0_idx];
172 
173     if (num_of_modes > 0) {
174       fprintf(probsfile, "%*c{ ", SPACES_PER_TAB, ' ');
175       parse_counts_for_cdf_opt(&ct_ptr, probsfile, 0, 1, &num_of_modes);
176       ct_ptr += cts_each_dim[1] - num_of_modes;
177       fprintf(probsfile, " },\n");
178     } else {
179       fprintf(probsfile, "%*c{ 0 },\n", SPACES_PER_TAB, ' ');
180       fprintf(logfile, "dummy cdf, no need to optimize\n");
181       ct_ptr += cts_each_dim[1];
182     }
183   }
184   fprintf(probsfile, "};\n\n");
185   fprintf(logfile, "============================\n");
186 }
187 
optimize_cdf_table_var_modes_3d(aom_count_type * counts,FILE * const probsfile,int dim_of_cts,int * cts_each_dim,int * modes_each_ctx,char * prefix)188 static void optimize_cdf_table_var_modes_3d(aom_count_type *counts,
189                                             FILE *const probsfile,
190                                             int dim_of_cts, int *cts_each_dim,
191                                             int *modes_each_ctx, char *prefix) {
192   aom_count_type *ct_ptr = counts;
193 
194   assert(dim_of_cts == 3);
195   (void)dim_of_cts;
196 
197   fprintf(probsfile, "%s = {\n", prefix);
198   fprintf(logfile, "%s\n", prefix);
199 
200   for (int d0_idx = 0; d0_idx < cts_each_dim[0]; ++d0_idx) {
201     fprintf(probsfile, "%*c{\n", SPACES_PER_TAB, ' ');
202     for (int d1_idx = 0; d1_idx < cts_each_dim[1]; ++d1_idx) {
203       int num_of_modes = modes_each_ctx[d0_idx];
204 
205       if (num_of_modes > 0) {
206         fprintf(probsfile, "%*c{ ", 2 * SPACES_PER_TAB, ' ');
207         parse_counts_for_cdf_opt(&ct_ptr, probsfile, 0, 1, &num_of_modes);
208         ct_ptr += cts_each_dim[2] - num_of_modes;
209         fprintf(probsfile, " },\n");
210       } else {
211         fprintf(probsfile, "%*c{ 0 },\n", 2 * SPACES_PER_TAB, ' ');
212         fprintf(logfile, "dummy cdf, no need to optimize\n");
213         ct_ptr += cts_each_dim[2];
214       }
215     }
216     fprintf(probsfile, "%*c},\n", SPACES_PER_TAB, ' ');
217   }
218   fprintf(probsfile, "};\n\n");
219   fprintf(logfile, "============================\n");
220 }
221 
optimize_cdf_table_var_modes_4d(aom_count_type * counts,FILE * const probsfile,int dim_of_cts,int * cts_each_dim,int * modes_each_ctx,char * prefix)222 static void optimize_cdf_table_var_modes_4d(aom_count_type *counts,
223                                             FILE *const probsfile,
224                                             int dim_of_cts, int *cts_each_dim,
225                                             int *modes_each_ctx, char *prefix) {
226   aom_count_type *ct_ptr = counts;
227 
228   assert(dim_of_cts == 4);
229   (void)dim_of_cts;
230 
231   fprintf(probsfile, "%s = {\n", prefix);
232   fprintf(logfile, "%s\n", prefix);
233 
234   for (int d0_idx = 0; d0_idx < cts_each_dim[0]; ++d0_idx) {
235     fprintf(probsfile, "%*c{\n", SPACES_PER_TAB, ' ');
236     for (int d1_idx = 0; d1_idx < cts_each_dim[1]; ++d1_idx) {
237       fprintf(probsfile, "%*c{\n", 2 * SPACES_PER_TAB, ' ');
238       for (int d2_idx = 0; d2_idx < cts_each_dim[2]; ++d2_idx) {
239         int num_of_modes = modes_each_ctx[d0_idx];
240 
241         if (num_of_modes > 0) {
242           fprintf(probsfile, "%*c{ ", 3 * SPACES_PER_TAB, ' ');
243           parse_counts_for_cdf_opt(&ct_ptr, probsfile, 0, 1, &num_of_modes);
244           ct_ptr += cts_each_dim[3] - num_of_modes;
245           fprintf(probsfile, " },\n");
246         } else {
247           fprintf(probsfile, "%*c{ 0 },\n", 3 * SPACES_PER_TAB, ' ');
248           fprintf(logfile, "dummy cdf, no need to optimize\n");
249           ct_ptr += cts_each_dim[3];
250         }
251       }
252       fprintf(probsfile, "%*c},\n", 2 * SPACES_PER_TAB, ' ');
253     }
254     fprintf(probsfile, "%*c},\n", SPACES_PER_TAB, ' ');
255   }
256   fprintf(probsfile, "};\n\n");
257   fprintf(logfile, "============================\n");
258 }
259 
main(int argc,const char ** argv)260 int main(int argc, const char **argv) {
261   if (argc < 2) {
262     fprintf(stderr, "Please specify the input stats file!\n");
263     exit(EXIT_FAILURE);
264   }
265 
266   FILE *const statsfile = fopen(argv[1], "rb");
267   if (statsfile == NULL) {
268     fprintf(stderr, "Failed to open input file!\n");
269     exit(EXIT_FAILURE);
270   }
271 
272   FRAME_COUNTS fc;
273   const size_t bytes = fread(&fc, sizeof(FRAME_COUNTS), 1, statsfile);
274   if (!bytes) return 1;
275 
276   FILE *const probsfile = fopen("optimized_probs.c", "w");
277   if (probsfile == NULL) {
278     fprintf(stderr,
279             "Failed to create output file for optimized entropy tables!\n");
280     exit(EXIT_FAILURE);
281   }
282 
283   logfile = fopen("aom_entropy_optimizer_parsed_counts.log", "w");
284   if (logfile == NULL) {
285     fprintf(stderr, "Failed to create log file for parsed counts!\n");
286     exit(EXIT_FAILURE);
287   }
288 
289   int cts_each_dim[10];
290 
291   /* Intra mode (keyframe luma) */
292   cts_each_dim[0] = KF_MODE_CONTEXTS;
293   cts_each_dim[1] = KF_MODE_CONTEXTS;
294   cts_each_dim[2] = INTRA_MODES;
295   optimize_cdf_table(&fc.kf_y_mode[0][0][0], probsfile, 3, cts_each_dim,
296                      "const aom_cdf_prob\n"
297                      "default_kf_y_mode_cdf[KF_MODE_CONTEXTS][KF_MODE_CONTEXTS]"
298                      "[CDF_SIZE(INTRA_MODES)]");
299 
300   cts_each_dim[0] = DIRECTIONAL_MODES;
301   cts_each_dim[1] = 2 * MAX_ANGLE_DELTA + 1;
302   optimize_cdf_table(&fc.angle_delta[0][0], probsfile, 2, cts_each_dim,
303                      "static const aom_cdf_prob default_angle_delta_cdf"
304                      "[DIRECTIONAL_MODES][CDF_SIZE(2 * MAX_ANGLE_DELTA + 1)]");
305 
306   /* Intra mode (non-keyframe luma) */
307   cts_each_dim[0] = BLOCK_SIZE_GROUPS;
308   cts_each_dim[1] = INTRA_MODES;
309   optimize_cdf_table(
310       &fc.y_mode[0][0], probsfile, 2, cts_each_dim,
311       "static const aom_cdf_prob\n"
312       "default_if_y_mode_cdf[BLOCK_SIZE_GROUPS][CDF_SIZE(INTRA_MODES)]");
313 
314   /* Intra mode (chroma) */
315   cts_each_dim[0] = CFL_ALLOWED_TYPES;
316   cts_each_dim[1] = INTRA_MODES;
317   cts_each_dim[2] = UV_INTRA_MODES;
318   optimize_uv_mode(&fc.uv_mode[0][0][0], probsfile, 3, cts_each_dim,
319                    "static const aom_cdf_prob\n"
320                    "default_uv_mode_cdf[CFL_ALLOWED_TYPES][INTRA_MODES]"
321                    "[CDF_SIZE(UV_INTRA_MODES)]");
322 
323   /* block partition */
324   cts_each_dim[0] = PARTITION_CONTEXTS;
325   cts_each_dim[1] = EXT_PARTITION_TYPES;
326   int part_types_each_ctx[PARTITION_CONTEXTS] = {
327     4, 4, 4, 4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 8, 8, 8, 8
328   };
329   optimize_cdf_table_var_modes_2d(
330       &fc.partition[0][0], probsfile, 2, cts_each_dim, part_types_each_ctx,
331       "static const aom_cdf_prob default_partition_cdf[PARTITION_CONTEXTS]"
332       "[CDF_SIZE(EXT_PARTITION_TYPES)]");
333 
334   /* tx type */
335   cts_each_dim[0] = EXT_TX_SETS_INTRA;
336   cts_each_dim[1] = EXT_TX_SIZES;
337   cts_each_dim[2] = INTRA_MODES;
338   cts_each_dim[3] = TX_TYPES;
339   int intra_ext_tx_types_each_ctx[EXT_TX_SETS_INTRA] = { 0, 7, 5 };
340   optimize_cdf_table_var_modes_4d(
341       &fc.intra_ext_tx[0][0][0][0], probsfile, 4, cts_each_dim,
342       intra_ext_tx_types_each_ctx,
343       "static const aom_cdf_prob default_intra_ext_tx_cdf[EXT_TX_SETS_INTRA]"
344       "[EXT_TX_SIZES][INTRA_MODES][CDF_SIZE(TX_TYPES)]");
345 
346   cts_each_dim[0] = EXT_TX_SETS_INTER;
347   cts_each_dim[1] = EXT_TX_SIZES;
348   cts_each_dim[2] = TX_TYPES;
349   int inter_ext_tx_types_each_ctx[EXT_TX_SETS_INTER] = { 0, 16, 12, 2 };
350   optimize_cdf_table_var_modes_3d(
351       &fc.inter_ext_tx[0][0][0], probsfile, 3, cts_each_dim,
352       inter_ext_tx_types_each_ctx,
353       "static const aom_cdf_prob default_inter_ext_tx_cdf[EXT_TX_SETS_INTER]"
354       "[EXT_TX_SIZES][CDF_SIZE(TX_TYPES)]");
355 
356   /* Chroma from Luma */
357   cts_each_dim[0] = CFL_JOINT_SIGNS;
358   optimize_cdf_table(&fc.cfl_sign[0], probsfile, 1, cts_each_dim,
359                      "static const aom_cdf_prob\n"
360                      "default_cfl_sign_cdf[CDF_SIZE(CFL_JOINT_SIGNS)]");
361   cts_each_dim[0] = CFL_ALPHA_CONTEXTS;
362   cts_each_dim[1] = CFL_ALPHABET_SIZE;
363   optimize_cdf_table(&fc.cfl_alpha[0][0], probsfile, 2, cts_each_dim,
364                      "static const aom_cdf_prob\n"
365                      "default_cfl_alpha_cdf[CFL_ALPHA_CONTEXTS]"
366                      "[CDF_SIZE(CFL_ALPHABET_SIZE)]");
367 
368   /* Interpolation filter */
369   cts_each_dim[0] = SWITCHABLE_FILTER_CONTEXTS;
370   cts_each_dim[1] = SWITCHABLE_FILTERS;
371   optimize_cdf_table(&fc.switchable_interp[0][0], probsfile, 2, cts_each_dim,
372                      "static const aom_cdf_prob\n"
373                      "default_switchable_interp_cdf[SWITCHABLE_FILTER_CONTEXTS]"
374                      "[CDF_SIZE(SWITCHABLE_FILTERS)]");
375 
376   /* Motion vector referencing */
377   cts_each_dim[0] = NEWMV_MODE_CONTEXTS;
378   cts_each_dim[1] = 2;
379   optimize_cdf_table(&fc.newmv_mode[0][0], probsfile, 2, cts_each_dim,
380                      "static const aom_cdf_prob "
381                      "default_newmv_cdf[NEWMV_MODE_CONTEXTS][CDF_SIZE(2)]");
382 
383   cts_each_dim[0] = GLOBALMV_MODE_CONTEXTS;
384   cts_each_dim[1] = 2;
385   optimize_cdf_table(&fc.zeromv_mode[0][0], probsfile, 2, cts_each_dim,
386                      "static const aom_cdf_prob "
387                      "default_zeromv_cdf[GLOBALMV_MODE_CONTEXTS][CDF_SIZE(2)]");
388 
389   cts_each_dim[0] = REFMV_MODE_CONTEXTS;
390   cts_each_dim[1] = 2;
391   optimize_cdf_table(&fc.refmv_mode[0][0], probsfile, 2, cts_each_dim,
392                      "static const aom_cdf_prob "
393                      "default_refmv_cdf[REFMV_MODE_CONTEXTS][CDF_SIZE(2)]");
394 
395   cts_each_dim[0] = DRL_MODE_CONTEXTS;
396   cts_each_dim[1] = 2;
397   optimize_cdf_table(&fc.drl_mode[0][0], probsfile, 2, cts_each_dim,
398                      "static const aom_cdf_prob "
399                      "default_drl_cdf[DRL_MODE_CONTEXTS][CDF_SIZE(2)]");
400 
401   /* ext_inter experiment */
402   /* New compound mode */
403   cts_each_dim[0] = INTER_MODE_CONTEXTS;
404   cts_each_dim[1] = INTER_COMPOUND_MODES;
405   optimize_cdf_table(&fc.inter_compound_mode[0][0], probsfile, 2, cts_each_dim,
406                      "static const aom_cdf_prob\n"
407                      "default_inter_compound_mode_cdf[INTER_MODE_CONTEXTS][CDF_"
408                      "SIZE(INTER_COMPOUND_MODES)]");
409 
410   /* Interintra */
411   cts_each_dim[0] = BLOCK_SIZE_GROUPS;
412   cts_each_dim[1] = 2;
413   optimize_cdf_table(&fc.interintra[0][0], probsfile, 2, cts_each_dim,
414                      "static const aom_cdf_prob "
415                      "default_interintra_cdf[BLOCK_SIZE_GROUPS][CDF_SIZE(2)]");
416 
417   cts_each_dim[0] = BLOCK_SIZE_GROUPS;
418   cts_each_dim[1] = INTERINTRA_MODES;
419   optimize_cdf_table(&fc.interintra_mode[0][0], probsfile, 2, cts_each_dim,
420                      "static const aom_cdf_prob\n"
421                      "default_interintra_mode_cdf[BLOCK_SIZE_GROUPS][CDF_SIZE("
422                      "INTERINTRA_MODES)]");
423 
424   cts_each_dim[0] = BLOCK_SIZES_ALL;
425   cts_each_dim[1] = 2;
426   optimize_cdf_table(
427       &fc.wedge_interintra[0][0], probsfile, 2, cts_each_dim,
428       "static const aom_cdf_prob\n"
429       "default_wedge_interintra_cdf[BLOCK_SIZES_ALL][CDF_SIZE(2)]");
430 
431   /* Compound type */
432   cts_each_dim[0] = BLOCK_SIZES_ALL;
433   cts_each_dim[1] = COMPOUND_TYPES - 1;
434   optimize_cdf_table(&fc.compound_type[0][0], probsfile, 2, cts_each_dim,
435                      "static const aom_cdf_prob default_compound_type_cdf"
436                      "[BLOCK_SIZES_ALL][CDF_SIZE(COMPOUND_TYPES - 1)]");
437 
438   cts_each_dim[0] = BLOCK_SIZES_ALL;
439   cts_each_dim[1] = 16;
440   optimize_cdf_table(&fc.wedge_idx[0][0], probsfile, 2, cts_each_dim,
441                      "static const aom_cdf_prob "
442                      "default_wedge_idx_cdf[BLOCK_SIZES_ALL][CDF_SIZE(16)]");
443 
444   /* motion_var and warped_motion experiments */
445   cts_each_dim[0] = BLOCK_SIZES_ALL;
446   cts_each_dim[1] = MOTION_MODES;
447   optimize_cdf_table(
448       &fc.motion_mode[0][0], probsfile, 2, cts_each_dim,
449       "static const aom_cdf_prob\n"
450       "default_motion_mode_cdf[BLOCK_SIZES_ALL][CDF_SIZE(MOTION_MODES)]");
451   cts_each_dim[0] = BLOCK_SIZES_ALL;
452   cts_each_dim[1] = 2;
453   optimize_cdf_table(&fc.obmc[0][0], probsfile, 2, cts_each_dim,
454                      "static const aom_cdf_prob "
455                      "default_obmc_cdf[BLOCK_SIZES_ALL][CDF_SIZE(2)]");
456 
457   /* Intra/inter flag */
458   cts_each_dim[0] = INTRA_INTER_CONTEXTS;
459   cts_each_dim[1] = 2;
460   optimize_cdf_table(
461       &fc.intra_inter[0][0], probsfile, 2, cts_each_dim,
462       "static const aom_cdf_prob\n"
463       "default_intra_inter_cdf[INTRA_INTER_CONTEXTS][CDF_SIZE(2)]");
464 
465   /* Single/comp ref flag */
466   cts_each_dim[0] = COMP_INTER_CONTEXTS;
467   cts_each_dim[1] = 2;
468   optimize_cdf_table(
469       &fc.comp_inter[0][0], probsfile, 2, cts_each_dim,
470       "static const aom_cdf_prob\n"
471       "default_comp_inter_cdf[COMP_INTER_CONTEXTS][CDF_SIZE(2)]");
472 
473   /* ext_comp_refs experiment */
474   cts_each_dim[0] = COMP_REF_TYPE_CONTEXTS;
475   cts_each_dim[1] = 2;
476   optimize_cdf_table(
477       &fc.comp_ref_type[0][0], probsfile, 2, cts_each_dim,
478       "static const aom_cdf_prob\n"
479       "default_comp_ref_type_cdf[COMP_REF_TYPE_CONTEXTS][CDF_SIZE(2)]");
480 
481   cts_each_dim[0] = UNI_COMP_REF_CONTEXTS;
482   cts_each_dim[1] = UNIDIR_COMP_REFS - 1;
483   cts_each_dim[2] = 2;
484   optimize_cdf_table(&fc.uni_comp_ref[0][0][0], probsfile, 3, cts_each_dim,
485                      "static const aom_cdf_prob\n"
486                      "default_uni_comp_ref_cdf[UNI_COMP_REF_CONTEXTS][UNIDIR_"
487                      "COMP_REFS - 1][CDF_SIZE(2)]");
488 
489   /* Reference frame (single ref) */
490   cts_each_dim[0] = REF_CONTEXTS;
491   cts_each_dim[1] = SINGLE_REFS - 1;
492   cts_each_dim[2] = 2;
493   optimize_cdf_table(
494       &fc.single_ref[0][0][0], probsfile, 3, cts_each_dim,
495       "static const aom_cdf_prob\n"
496       "default_single_ref_cdf[REF_CONTEXTS][SINGLE_REFS - 1][CDF_SIZE(2)]");
497 
498   /* ext_refs experiment */
499   cts_each_dim[0] = REF_CONTEXTS;
500   cts_each_dim[1] = FWD_REFS - 1;
501   cts_each_dim[2] = 2;
502   optimize_cdf_table(
503       &fc.comp_ref[0][0][0], probsfile, 3, cts_each_dim,
504       "static const aom_cdf_prob\n"
505       "default_comp_ref_cdf[REF_CONTEXTS][FWD_REFS - 1][CDF_SIZE(2)]");
506 
507   cts_each_dim[0] = REF_CONTEXTS;
508   cts_each_dim[1] = BWD_REFS - 1;
509   cts_each_dim[2] = 2;
510   optimize_cdf_table(
511       &fc.comp_bwdref[0][0][0], probsfile, 3, cts_each_dim,
512       "static const aom_cdf_prob\n"
513       "default_comp_bwdref_cdf[REF_CONTEXTS][BWD_REFS - 1][CDF_SIZE(2)]");
514 
515   /* palette */
516   cts_each_dim[0] = PALATTE_BSIZE_CTXS;
517   cts_each_dim[1] = PALETTE_SIZES;
518   optimize_cdf_table(&fc.palette_y_size[0][0], probsfile, 2, cts_each_dim,
519                      "const aom_cdf_prob default_palette_y_size_cdf"
520                      "[PALATTE_BSIZE_CTXS][CDF_SIZE(PALETTE_SIZES)]");
521 
522   cts_each_dim[0] = PALATTE_BSIZE_CTXS;
523   cts_each_dim[1] = PALETTE_SIZES;
524   optimize_cdf_table(&fc.palette_uv_size[0][0], probsfile, 2, cts_each_dim,
525                      "const aom_cdf_prob default_palette_uv_size_cdf"
526                      "[PALATTE_BSIZE_CTXS][CDF_SIZE(PALETTE_SIZES)]");
527 
528   cts_each_dim[0] = PALATTE_BSIZE_CTXS;
529   cts_each_dim[1] = PALETTE_Y_MODE_CONTEXTS;
530   cts_each_dim[2] = 2;
531   optimize_cdf_table(&fc.palette_y_mode[0][0][0], probsfile, 3, cts_each_dim,
532                      "const aom_cdf_prob default_palette_y_mode_cdf"
533                      "[PALATTE_BSIZE_CTXS][PALETTE_Y_MODE_CONTEXTS]"
534                      "[CDF_SIZE(2)]");
535 
536   cts_each_dim[0] = PALETTE_UV_MODE_CONTEXTS;
537   cts_each_dim[1] = 2;
538   optimize_cdf_table(&fc.palette_uv_mode[0][0], probsfile, 2, cts_each_dim,
539                      "const aom_cdf_prob default_palette_uv_mode_cdf"
540                      "[PALETTE_UV_MODE_CONTEXTS][CDF_SIZE(2)]");
541 
542   cts_each_dim[0] = PALETTE_SIZES;
543   cts_each_dim[1] = PALETTE_COLOR_INDEX_CONTEXTS;
544   cts_each_dim[2] = PALETTE_COLORS;
545   int palette_color_indexes_each_ctx[PALETTE_SIZES] = { 2, 3, 4, 5, 6, 7, 8 };
546   optimize_cdf_table_var_modes_3d(
547       &fc.palette_y_color_index[0][0][0], probsfile, 3, cts_each_dim,
548       palette_color_indexes_each_ctx,
549       "const aom_cdf_prob default_palette_y_color_index_cdf[PALETTE_SIZES]"
550       "[PALETTE_COLOR_INDEX_CONTEXTS][CDF_SIZE(PALETTE_COLORS)]");
551 
552   cts_each_dim[0] = PALETTE_SIZES;
553   cts_each_dim[1] = PALETTE_COLOR_INDEX_CONTEXTS;
554   cts_each_dim[2] = PALETTE_COLORS;
555   optimize_cdf_table_var_modes_3d(
556       &fc.palette_uv_color_index[0][0][0], probsfile, 3, cts_each_dim,
557       palette_color_indexes_each_ctx,
558       "const aom_cdf_prob default_palette_uv_color_index_cdf[PALETTE_SIZES]"
559       "[PALETTE_COLOR_INDEX_CONTEXTS][CDF_SIZE(PALETTE_COLORS)]");
560 
561   /* Transform size */
562   cts_each_dim[0] = TXFM_PARTITION_CONTEXTS;
563   cts_each_dim[1] = 2;
564   optimize_cdf_table(
565       &fc.txfm_partition[0][0], probsfile, 2, cts_each_dim,
566       "static const aom_cdf_prob\n"
567       "default_txfm_partition_cdf[TXFM_PARTITION_CONTEXTS][CDF_SIZE(2)]");
568 
569   /* Skip flag */
570   cts_each_dim[0] = SKIP_CONTEXTS;
571   cts_each_dim[1] = 2;
572   optimize_cdf_table(&fc.skip[0][0], probsfile, 2, cts_each_dim,
573                      "static const aom_cdf_prob "
574                      "default_skip_cdfs[SKIP_CONTEXTS][CDF_SIZE(2)]");
575 
576   /* Skip mode flag */
577   cts_each_dim[0] = SKIP_MODE_CONTEXTS;
578   cts_each_dim[1] = 2;
579   optimize_cdf_table(&fc.skip_mode[0][0], probsfile, 2, cts_each_dim,
580                      "static const aom_cdf_prob "
581                      "default_skip_mode_cdfs[SKIP_MODE_CONTEXTS][CDF_SIZE(2)]");
582 
583   /* joint compound flag */
584   cts_each_dim[0] = COMP_INDEX_CONTEXTS;
585   cts_each_dim[1] = 2;
586   optimize_cdf_table(&fc.compound_index[0][0], probsfile, 2, cts_each_dim,
587                      "static const aom_cdf_prob default_compound_idx_cdfs"
588                      "[COMP_INDEX_CONTEXTS][CDF_SIZE(2)]");
589 
590   cts_each_dim[0] = COMP_GROUP_IDX_CONTEXTS;
591   cts_each_dim[1] = 2;
592   optimize_cdf_table(&fc.comp_group_idx[0][0], probsfile, 2, cts_each_dim,
593                      "static const aom_cdf_prob default_comp_group_idx_cdfs"
594                      "[COMP_GROUP_IDX_CONTEXTS][CDF_SIZE(2)]");
595 
596   /* intrabc */
597   cts_each_dim[0] = 2;
598   optimize_cdf_table(
599       &fc.intrabc[0], probsfile, 1, cts_each_dim,
600       "static const aom_cdf_prob default_intrabc_cdf[CDF_SIZE(2)]");
601 
602   /* filter_intra experiment */
603   cts_each_dim[0] = FILTER_INTRA_MODES;
604   optimize_cdf_table(
605       &fc.filter_intra_mode[0], probsfile, 1, cts_each_dim,
606       "static const aom_cdf_prob "
607       "default_filter_intra_mode_cdf[CDF_SIZE(FILTER_INTRA_MODES)]");
608 
609   cts_each_dim[0] = BLOCK_SIZES_ALL;
610   cts_each_dim[1] = 2;
611   optimize_cdf_table(&fc.filter_intra[0][0], probsfile, 2, cts_each_dim,
612                      "static const aom_cdf_prob "
613                      "default_filter_intra_cdfs[BLOCK_SIZES_ALL][CDF_SIZE(2)]");
614 
615   /* restoration type */
616   cts_each_dim[0] = RESTORE_SWITCHABLE_TYPES;
617   optimize_cdf_table(&fc.switchable_restore[0], probsfile, 1, cts_each_dim,
618                      "static const aom_cdf_prob default_switchable_restore_cdf"
619                      "[CDF_SIZE(RESTORE_SWITCHABLE_TYPES)]");
620 
621   cts_each_dim[0] = 2;
622   optimize_cdf_table(&fc.wiener_restore[0], probsfile, 1, cts_each_dim,
623                      "static const aom_cdf_prob default_wiener_restore_cdf"
624                      "[CDF_SIZE(2)]");
625 
626   cts_each_dim[0] = 2;
627   optimize_cdf_table(&fc.sgrproj_restore[0], probsfile, 1, cts_each_dim,
628                      "static const aom_cdf_prob default_sgrproj_restore_cdf"
629                      "[CDF_SIZE(2)]");
630 
631   /* intra tx size */
632   cts_each_dim[0] = MAX_TX_CATS;
633   cts_each_dim[1] = TX_SIZE_CONTEXTS;
634   cts_each_dim[2] = MAX_TX_DEPTH + 1;
635   int intra_tx_sizes_each_ctx[MAX_TX_CATS] = { 2, 3, 3, 3 };
636   optimize_cdf_table_var_modes_3d(
637       &fc.intra_tx_size[0][0][0], probsfile, 3, cts_each_dim,
638       intra_tx_sizes_each_ctx,
639       "static const aom_cdf_prob default_tx_size_cdf"
640       "[MAX_TX_CATS][TX_SIZE_CONTEXTS][CDF_SIZE(MAX_TX_DEPTH + 1)]");
641 
642   /* transform coding */
643   cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
644   cts_each_dim[1] = TX_SIZES;
645   cts_each_dim[2] = TXB_SKIP_CONTEXTS;
646   cts_each_dim[3] = 2;
647   optimize_cdf_table(&fc.txb_skip[0][0][0][0], probsfile, 4, cts_each_dim,
648                      "static const aom_cdf_prob "
649                      "av1_default_txb_skip_cdfs[TOKEN_CDF_Q_CTXS][TX_SIZES]"
650                      "[TXB_SKIP_CONTEXTS][CDF_SIZE(2)]");
651 
652   cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
653   cts_each_dim[1] = TX_SIZES;
654   cts_each_dim[2] = PLANE_TYPES;
655   cts_each_dim[3] = EOB_COEF_CONTEXTS;
656   cts_each_dim[4] = 2;
657   optimize_cdf_table(
658       &fc.eob_extra[0][0][0][0][0], probsfile, 5, cts_each_dim,
659       "static const aom_cdf_prob av1_default_eob_extra_cdfs "
660       "[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES][EOB_COEF_CONTEXTS]"
661       "[CDF_SIZE(2)]");
662 
663   cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
664   cts_each_dim[1] = PLANE_TYPES;
665   cts_each_dim[2] = 2;
666   cts_each_dim[3] = 5;
667   optimize_cdf_table(&fc.eob_multi16[0][0][0][0], probsfile, 4, cts_each_dim,
668                      "static const aom_cdf_prob av1_default_eob_multi16_cdfs"
669                      "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(5)]");
670 
671   cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
672   cts_each_dim[1] = PLANE_TYPES;
673   cts_each_dim[2] = 2;
674   cts_each_dim[3] = 6;
675   optimize_cdf_table(&fc.eob_multi32[0][0][0][0], probsfile, 4, cts_each_dim,
676                      "static const aom_cdf_prob av1_default_eob_multi32_cdfs"
677                      "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(6)]");
678 
679   cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
680   cts_each_dim[1] = PLANE_TYPES;
681   cts_each_dim[2] = 2;
682   cts_each_dim[3] = 7;
683   optimize_cdf_table(&fc.eob_multi64[0][0][0][0], probsfile, 4, cts_each_dim,
684                      "static const aom_cdf_prob av1_default_eob_multi64_cdfs"
685                      "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(7)]");
686 
687   cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
688   cts_each_dim[1] = PLANE_TYPES;
689   cts_each_dim[2] = 2;
690   cts_each_dim[3] = 8;
691   optimize_cdf_table(&fc.eob_multi128[0][0][0][0], probsfile, 4, cts_each_dim,
692                      "static const aom_cdf_prob av1_default_eob_multi128_cdfs"
693                      "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(8)]");
694 
695   cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
696   cts_each_dim[1] = PLANE_TYPES;
697   cts_each_dim[2] = 2;
698   cts_each_dim[3] = 9;
699   optimize_cdf_table(&fc.eob_multi256[0][0][0][0], probsfile, 4, cts_each_dim,
700                      "static const aom_cdf_prob av1_default_eob_multi256_cdfs"
701                      "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(9)]");
702 
703   cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
704   cts_each_dim[1] = PLANE_TYPES;
705   cts_each_dim[2] = 2;
706   cts_each_dim[3] = 10;
707   optimize_cdf_table(&fc.eob_multi512[0][0][0][0], probsfile, 4, cts_each_dim,
708                      "static const aom_cdf_prob av1_default_eob_multi512_cdfs"
709                      "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(10)]");
710 
711   cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
712   cts_each_dim[1] = PLANE_TYPES;
713   cts_each_dim[2] = 2;
714   cts_each_dim[3] = 11;
715   optimize_cdf_table(&fc.eob_multi1024[0][0][0][0], probsfile, 4, cts_each_dim,
716                      "static const aom_cdf_prob av1_default_eob_multi1024_cdfs"
717                      "[TOKEN_CDF_Q_CTXS][PLANE_TYPES][2][CDF_SIZE(11)]");
718 
719   cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
720   cts_each_dim[1] = TX_SIZES;
721   cts_each_dim[2] = PLANE_TYPES;
722   cts_each_dim[3] = LEVEL_CONTEXTS;
723   cts_each_dim[4] = BR_CDF_SIZE;
724   optimize_cdf_table(&fc.coeff_lps_multi[0][0][0][0][0], probsfile, 5,
725                      cts_each_dim,
726                      "static const aom_cdf_prob "
727                      "av1_default_coeff_lps_multi_cdfs[TOKEN_CDF_Q_CTXS]"
728                      "[TX_SIZES][PLANE_TYPES][LEVEL_CONTEXTS]"
729                      "[CDF_SIZE(BR_CDF_SIZE)]");
730 
731   cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
732   cts_each_dim[1] = TX_SIZES;
733   cts_each_dim[2] = PLANE_TYPES;
734   cts_each_dim[3] = SIG_COEF_CONTEXTS;
735   cts_each_dim[4] = NUM_BASE_LEVELS + 2;
736   optimize_cdf_table(
737       &fc.coeff_base_multi[0][0][0][0][0], probsfile, 5, cts_each_dim,
738       "static const aom_cdf_prob av1_default_coeff_base_multi_cdfs"
739       "[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS]"
740       "[CDF_SIZE(NUM_BASE_LEVELS + 2)]");
741 
742   cts_each_dim[0] = TOKEN_CDF_Q_CTXS;
743   cts_each_dim[1] = TX_SIZES;
744   cts_each_dim[2] = PLANE_TYPES;
745   cts_each_dim[3] = SIG_COEF_CONTEXTS_EOB;
746   cts_each_dim[4] = NUM_BASE_LEVELS + 1;
747   optimize_cdf_table(
748       &fc.coeff_base_eob_multi[0][0][0][0][0], probsfile, 5, cts_each_dim,
749       "static const aom_cdf_prob av1_default_coeff_base_eob_multi_cdfs"
750       "[TOKEN_CDF_Q_CTXS][TX_SIZES][PLANE_TYPES][SIG_COEF_CONTEXTS_EOB]"
751       "[CDF_SIZE(NUM_BASE_LEVELS + 1)]");
752 
753   fclose(statsfile);
754   fclose(logfile);
755   fclose(probsfile);
756 
757   return 0;
758 }
759