AOMedia AV1 Codec
aomenc
1/*
2 * Copyright (c) 2016, 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#include "apps/aomenc.h"
13
14#include "config/aom_config.h"
15
16#include <assert.h>
17#include <limits.h>
18#include <math.h>
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#if CONFIG_AV1_DECODER
25#include "aom/aom_decoder.h"
26#include "aom/aomdx.h"
27#endif
28
29#include "aom/aom_encoder.h"
30#include "aom/aom_integer.h"
31#include "aom/aomcx.h"
32#include "aom_dsp/aom_dsp_common.h"
33#include "aom_ports/aom_timer.h"
34#include "aom_ports/mem_ops.h"
35#include "common/args.h"
36#include "common/ivfenc.h"
37#include "common/tools_common.h"
38#include "common/warnings.h"
39
40#if CONFIG_WEBM_IO
41#include "common/webmenc.h"
42#endif
43
44#include "common/y4minput.h"
45#include "examples/encoder_util.h"
46#include "stats/aomstats.h"
47#include "stats/rate_hist.h"
48
49#if CONFIG_LIBYUV
50#include <libyuv/scale.h>
51#endif
52
53/* Swallow warnings about unused results of fread/fwrite */
54static size_t wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream) {
55 return fread(ptr, size, nmemb, stream);
56}
57#define fread wrap_fread
58
59static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,
60 FILE *stream) {
61 return fwrite(ptr, size, nmemb, stream);
62}
63#define fwrite wrap_fwrite
64
65static const char *exec_name;
66
67static AOM_TOOLS_FORMAT_PRINTF(3, 0) void warn_or_exit_on_errorv(
68 aom_codec_ctx_t *ctx, int fatal, const char *s, va_list ap) {
69 if (ctx->err) {
70 const char *detail = aom_codec_error_detail(ctx);
71
72 vfprintf(stderr, s, ap);
73 fprintf(stderr, ": %s\n", aom_codec_error(ctx));
74
75 if (detail) fprintf(stderr, " %s\n", detail);
76
77 if (fatal) exit(EXIT_FAILURE);
78 }
79}
80
81static AOM_TOOLS_FORMAT_PRINTF(2,
82 3) void ctx_exit_on_error(aom_codec_ctx_t *ctx,
83 const char *s, ...) {
84 va_list ap;
85
86 va_start(ap, s);
87 warn_or_exit_on_errorv(ctx, 1, s, ap);
88 va_end(ap);
89}
90
91static AOM_TOOLS_FORMAT_PRINTF(3, 4) void warn_or_exit_on_error(
92 aom_codec_ctx_t *ctx, int fatal, const char *s, ...) {
93 va_list ap;
94
95 va_start(ap, s);
96 warn_or_exit_on_errorv(ctx, fatal, s, ap);
97 va_end(ap);
98}
99
100static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
101 FILE *f = input_ctx->file;
102 y4m_input *y4m = &input_ctx->y4m;
103 int shortread = 0;
104
105 if (input_ctx->file_type == FILE_TYPE_Y4M) {
106 if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
107 } else {
108 shortread = read_yuv_frame(input_ctx, img);
109 }
110
111 return !shortread;
112}
113
114static int file_is_y4m(const char detect[4]) {
115 if (memcmp(detect, "YUV4", 4) == 0) {
116 return 1;
117 }
118 return 0;
119}
120
121static int fourcc_is_ivf(const char detect[4]) {
122 if (memcmp(detect, "DKIF", 4) == 0) {
123 return 1;
124 }
125 return 0;
126}
127
128static const int av1_arg_ctrl_map[] = { AOME_SET_CPUUSED,
217#if CONFIG_DENOISE
220 AV1E_SET_ENABLE_DNL_DENOISING,
221#endif // CONFIG_DENOISE
231#if CONFIG_TUNE_VMAF
233#endif
240 0 };
241
242const arg_def_t *main_args[] = { &g_av1_codec_arg_defs.help,
243 &g_av1_codec_arg_defs.use_cfg,
244 &g_av1_codec_arg_defs.debugmode,
245 &g_av1_codec_arg_defs.outputfile,
246 &g_av1_codec_arg_defs.codecarg,
247 &g_av1_codec_arg_defs.passes,
248 &g_av1_codec_arg_defs.pass_arg,
249 &g_av1_codec_arg_defs.fpf_name,
250 &g_av1_codec_arg_defs.limit,
251 &g_av1_codec_arg_defs.skip,
252 &g_av1_codec_arg_defs.good_dl,
253 &g_av1_codec_arg_defs.rt_dl,
254 &g_av1_codec_arg_defs.ai_dl,
255 &g_av1_codec_arg_defs.quietarg,
256 &g_av1_codec_arg_defs.verbosearg,
257 &g_av1_codec_arg_defs.psnrarg,
258 &g_av1_codec_arg_defs.use_webm,
259 &g_av1_codec_arg_defs.use_ivf,
260 &g_av1_codec_arg_defs.use_obu,
261 &g_av1_codec_arg_defs.q_hist_n,
262 &g_av1_codec_arg_defs.rate_hist_n,
263 &g_av1_codec_arg_defs.disable_warnings,
264 &g_av1_codec_arg_defs.disable_warning_prompt,
265 &g_av1_codec_arg_defs.recontest,
266 NULL };
267
268const arg_def_t *global_args[] = {
269 &g_av1_codec_arg_defs.use_nv12,
270 &g_av1_codec_arg_defs.use_yv12,
271 &g_av1_codec_arg_defs.use_i420,
272 &g_av1_codec_arg_defs.use_i422,
273 &g_av1_codec_arg_defs.use_i444,
274 &g_av1_codec_arg_defs.usage,
275 &g_av1_codec_arg_defs.threads,
276 &g_av1_codec_arg_defs.profile,
277 &g_av1_codec_arg_defs.width,
278 &g_av1_codec_arg_defs.height,
279 &g_av1_codec_arg_defs.forced_max_frame_width,
280 &g_av1_codec_arg_defs.forced_max_frame_height,
281#if CONFIG_WEBM_IO
282 &g_av1_codec_arg_defs.stereo_mode,
283#endif
284 &g_av1_codec_arg_defs.timebase,
285 &g_av1_codec_arg_defs.framerate,
286 &g_av1_codec_arg_defs.global_error_resilient,
287 &g_av1_codec_arg_defs.bitdeptharg,
288 &g_av1_codec_arg_defs.inbitdeptharg,
289 &g_av1_codec_arg_defs.lag_in_frames,
290 &g_av1_codec_arg_defs.large_scale_tile,
291 &g_av1_codec_arg_defs.monochrome,
292 &g_av1_codec_arg_defs.full_still_picture_hdr,
293 &g_av1_codec_arg_defs.use_16bit_internal,
294 &g_av1_codec_arg_defs.save_as_annexb,
295 NULL
296};
297
298const arg_def_t *rc_args[] = { &g_av1_codec_arg_defs.dropframe_thresh,
299 &g_av1_codec_arg_defs.resize_mode,
300 &g_av1_codec_arg_defs.resize_denominator,
301 &g_av1_codec_arg_defs.resize_kf_denominator,
302 &g_av1_codec_arg_defs.superres_mode,
303 &g_av1_codec_arg_defs.superres_denominator,
304 &g_av1_codec_arg_defs.superres_kf_denominator,
305 &g_av1_codec_arg_defs.superres_qthresh,
306 &g_av1_codec_arg_defs.superres_kf_qthresh,
307 &g_av1_codec_arg_defs.end_usage,
308 &g_av1_codec_arg_defs.target_bitrate,
309 &g_av1_codec_arg_defs.min_quantizer,
310 &g_av1_codec_arg_defs.max_quantizer,
311 &g_av1_codec_arg_defs.undershoot_pct,
312 &g_av1_codec_arg_defs.overshoot_pct,
313 &g_av1_codec_arg_defs.buf_sz,
314 &g_av1_codec_arg_defs.buf_initial_sz,
315 &g_av1_codec_arg_defs.buf_optimal_sz,
316 &g_av1_codec_arg_defs.bias_pct,
317 &g_av1_codec_arg_defs.minsection_pct,
318 &g_av1_codec_arg_defs.maxsection_pct,
319 NULL };
320
321const arg_def_t *kf_args[] = { &g_av1_codec_arg_defs.fwd_kf_enabled,
322 &g_av1_codec_arg_defs.kf_min_dist,
323 &g_av1_codec_arg_defs.kf_max_dist,