1/*
2 *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "./warnings.h"
12
13#include <assert.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18#include "vpx/vpx_encoder.h"
19
20#include "./tools_common.h"
21#include "./vpxenc.h"
22
23static const char quantizer_warning_string[] =
24    "Bad quantizer values. Quantizer values should not be equal, and should "
25    "differ by at least 8.";
26static const char lag_in_frames_with_realtime[] =
27    "Lag in frames is ignored when deadline is set to realtime for cbr mode.";
28
29struct WarningListNode {
30  const char *warning_string;
31  struct WarningListNode *next_warning;
32};
33
34struct WarningList {
35  struct WarningListNode *warning_node;
36};
37
38static void add_warning(const char *warning_string,
39                        struct WarningList *warning_list) {
40  struct WarningListNode **node = &warning_list->warning_node;
41
42  struct WarningListNode *new_node = malloc(sizeof(*new_node));
43  if (new_node == NULL) {
44    fatal("Unable to allocate warning node.");
45  }
46
47  new_node->warning_string = warning_string;
48  new_node->next_warning = NULL;
49
50  while (*node != NULL) node = &(*node)->next_warning;
51
52  *node = new_node;
53}
54
55static void free_warning_list(struct WarningList *warning_list) {
56  while (warning_list->warning_node != NULL) {
57    struct WarningListNode *const node = warning_list->warning_node;
58    warning_list->warning_node = node->next_warning;
59    free(node);
60  }
61}
62
63static int continue_prompt(int num_warnings) {
64  int c;
65  fprintf(stderr,
66          "%d encoder configuration warning(s). Continue? (y to continue) ",
67          num_warnings);
68  c = getchar();
69  return c == 'y';
70}
71
72static void check_quantizer(int min_q, int max_q,
73                            struct WarningList *warning_list) {
74  const int lossless = min_q == 0 && max_q == 0;
75  if (!lossless && (min_q == max_q || abs(max_q - min_q) < 8))
76    add_warning(quantizer_warning_string, warning_list);
77}
78
79static void check_lag_in_frames_realtime_deadline(
80    int lag_in_frames, int deadline, int rc_end_usage,
81    struct WarningList *warning_list) {
82  if (deadline == VPX_DL_REALTIME && lag_in_frames != 0 && rc_end_usage == 1)
83    add_warning(lag_in_frames_with_realtime, warning_list);
84}
85
86void check_encoder_config(int disable_prompt,
87                          const struct VpxEncoderConfig *global_config,
88                          const struct vpx_codec_enc_cfg *stream_config) {
89  int num_warnings = 0;
90  struct WarningListNode *warning = NULL;
91  struct WarningList warning_list = { 0 };
92
93  check_quantizer(stream_config->rc_min_quantizer,
94                  stream_config->rc_max_quantizer, &warning_list);
95  check_lag_in_frames_realtime_deadline(
96      stream_config->g_lag_in_frames, global_config->deadline,
97      stream_config->rc_end_usage, &warning_list);
98  /* Count and print warnings. */
99  for (warning = warning_list.warning_node; warning != NULL;
100       warning = warning->next_warning, ++num_warnings) {
101    warn(warning->warning_string);
102  }
103
104  free_warning_list(&warning_list);
105
106  if (num_warnings) {
107    if (!disable_prompt && !continue_prompt(num_warnings)) exit(EXIT_FAILURE);
108  }
109}
110