blob: 5879036509eac28f5698a0eb7e622c4ba22be3ec [file] [log] [blame]
John Koleszarc377bf02010-11-02 09:11:57 -04001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszarc377bf02010-11-02 09:11:57 -04003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * 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.
John Koleszarc377bf02010-11-02 09:11:57 -040010 */
Tom Finegan03848f52013-11-05 10:02:18 -080011#ifndef TOOLS_COMMON_H_
12#define TOOLS_COMMON_H_
13
14#include <stdio.h>
15
Tom Finegan60e653d2018-05-22 11:34:58 -070016#include "config/aom_config.h"
17
Yaowu Xuf883b422016-08-30 14:01:10 -070018#include "aom/aom_codec.h"
19#include "aom/aom_image.h"
20#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070021#include "aom_ports/msvc.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080022
Tom Fineganba02c242017-05-16 15:01:54 -070023#if CONFIG_AV1_ENCODER
Tom Finegandd3e2a52018-05-23 14:33:09 -070024#include "common/y4minput.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080025#endif
Tom Finegane6579192013-11-11 06:48:18 -080026
27#if defined(_MSC_VER)
James Zerndae4cfc2014-04-02 20:06:07 -070028/* MSVS uses _f{seek,tell}i64. */
Tom Finegane6579192013-11-11 06:48:18 -080029#define fseeko _fseeki64
30#define ftello _ftelli64
James Zernb6430362017-01-14 14:18:01 -080031typedef int64_t FileOffset;
Tom Finegane6579192013-11-11 06:48:18 -080032#elif defined(_WIN32)
James Zernb6430362017-01-14 14:18:01 -080033#include <sys/types.h> /* NOLINT*/
James Zerne3578af2014-04-17 10:47:08 -070034/* MinGW uses f{seek,tell}o64 for large files. */
Tom Finegane6579192013-11-11 06:48:18 -080035#define fseeko fseeko64
36#define ftello ftello64
James Zernb6430362017-01-14 14:18:01 -080037typedef off64_t FileOffset;
38#elif CONFIG_OS_SUPPORT
39#include <sys/types.h> /* NOLINT*/
40typedef off_t FileOffset;
41/* Use 32-bit file operations in WebM file format when building ARM
42 * executables (.axf) with RVCT. */
43#else
44#define fseeko fseek
45#define ftello ftell
Tom Finegan92ca0952017-01-19 14:39:04 -080046typedef long FileOffset; /* NOLINT */
James Zernb6430362017-01-14 14:18:01 -080047#endif /* CONFIG_OS_SUPPORT */
Tom Finegane6579192013-11-11 06:48:18 -080048
49#if CONFIG_OS_SUPPORT
50#if defined(_MSC_VER)
clang-format6c4d83e2016-08-08 19:03:30 -070051#include <io.h> /* NOLINT */
52#define isatty _isatty
53#define fileno _fileno
Tom Finegane6579192013-11-11 06:48:18 -080054#else
clang-format6c4d83e2016-08-08 19:03:30 -070055#include <unistd.h> /* NOLINT */
56#endif /* _MSC_VER */
57#endif /* CONFIG_OS_SUPPORT */
Tom Finegane6579192013-11-11 06:48:18 -080058
Tom Finegane6579192013-11-11 06:48:18 -080059#define LITERALU64(hi, lo) ((((uint64_t)hi) << 32) | lo)
60
61#ifndef PATH_MAX
62#define PATH_MAX 512
63#endif
64
clang-format6c4d83e2016-08-08 19:03:30 -070065#define IVF_FRAME_HDR_SZ (4 + 8) /* 4 byte size + 8 byte timestamp */
Tom Finegan00a35aa2013-11-14 12:37:42 -080066#define IVF_FILE_HDR_SZ 32
67
68#define RAW_FRAME_HDR_SZ sizeof(uint32_t)
69
Yaowu Xuf883b422016-08-30 14:01:10 -070070#define AV1_FOURCC 0x31305641
John Koleszarc377bf02010-11-02 09:11:57 -040071
Tom Finegan00a35aa2013-11-14 12:37:42 -080072enum VideoFileType {
Cyril Concolato6c788832017-10-30 16:30:35 -070073 FILE_TYPE_OBU,
Tom Finegan00a35aa2013-11-14 12:37:42 -080074 FILE_TYPE_RAW,
75 FILE_TYPE_IVF,
76 FILE_TYPE_Y4M,
77 FILE_TYPE_WEBM
78};
79
80struct FileTypeDetectionBuffer {
81 char buf[4];
82 size_t buf_read;
83 size_t position;
84};
85
Yaowu Xuf883b422016-08-30 14:01:10 -070086struct AvxRational {
Tom Finegan00a35aa2013-11-14 12:37:42 -080087 int numerator;
88 int denominator;
89};
90
Yaowu Xuf883b422016-08-30 14:01:10 -070091struct AvxInputContext {
Tom Finegan00a35aa2013-11-14 12:37:42 -080092 const char *filename;
93 FILE *file;
James Zerne3578af2014-04-17 10:47:08 -070094 int64_t length;
Tom Finegan00a35aa2013-11-14 12:37:42 -080095 struct FileTypeDetectionBuffer detect;
96 enum VideoFileType file_type;
Tom Finegan2abe2d42013-11-18 14:39:51 -080097 uint32_t width;
98 uint32_t height;
Yaowu Xuf883b422016-08-30 14:01:10 -070099 struct AvxRational pixel_aspect_ratio;
100 aom_img_fmt_t fmt;
101 aom_bit_depth_t bit_depth;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800102 int only_i420;
Tom Finegan2abe2d42013-11-18 14:39:51 -0800103 uint32_t fourcc;
Yaowu Xuf883b422016-08-30 14:01:10 -0700104 struct AvxRational framerate;
Tom Fineganba02c242017-05-16 15:01:54 -0700105#if CONFIG_AV1_ENCODER
Tom Finegan00a35aa2013-11-14 12:37:42 -0800106 y4m_input y4m;
107#endif
108};
109
110#ifdef __cplusplus
111extern "C" {
112#endif
113
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700114#if defined(__GNUC__)
Yaowu Xuf883b422016-08-30 14:01:10 -0700115#define AOM_NO_RETURN __attribute__((noreturn))
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700116#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700117#define AOM_NO_RETURN
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700118#endif
119
John Koleszarc377bf02010-11-02 09:11:57 -0400120/* Sets a stdio stream into binary mode */
John Koleszarc6b90392012-07-13 15:21:29 -0700121FILE *set_binary_mode(FILE *stream);
John Koleszarc377bf02010-11-02 09:11:57 -0400122
Yaowu Xuf883b422016-08-30 14:01:10 -0700123void die(const char *fmt, ...) AOM_NO_RETURN;
124void fatal(const char *fmt, ...) AOM_NO_RETURN;
Tom Finegan03848f52013-11-05 10:02:18 -0800125void warn(const char *fmt, ...);
126
Yaowu Xuf883b422016-08-30 14:01:10 -0700127void die_codec(aom_codec_ctx_t *ctx, const char *s) AOM_NO_RETURN;
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800128
Tom Finegan03848f52013-11-05 10:02:18 -0800129/* The tool including this file must define usage_exit() */
Yaowu Xuf883b422016-08-30 14:01:10 -0700130void usage_exit(void) AOM_NO_RETURN;
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700131
Yaowu Xuf883b422016-08-30 14:01:10 -0700132#undef AOM_NO_RETURN
Tom Finegan03848f52013-11-05 10:02:18 -0800133
Yaowu Xuf883b422016-08-30 14:01:10 -0700134int read_yuv_frame(struct AvxInputContext *input_ctx, aom_image_t *yuv_frame);
Tom Finegan00a35aa2013-11-14 12:37:42 -0800135
Yaowu Xuf883b422016-08-30 14:01:10 -0700136typedef struct AvxInterface {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800137 const char *const name;
138 const uint32_t fourcc;
Yaowu Xuf883b422016-08-30 14:01:10 -0700139 aom_codec_iface_t *(*const codec_interface)();
140} AvxInterface;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800141
Yaowu Xuf883b422016-08-30 14:01:10 -0700142int get_aom_encoder_count(void);
143const AvxInterface *get_aom_encoder_by_index(int i);
144const AvxInterface *get_aom_encoder_by_name(const char *name);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800145
Yaowu Xuf883b422016-08-30 14:01:10 -0700146int get_aom_decoder_count(void);
147const AvxInterface *get_aom_decoder_by_index(int i);
148const AvxInterface *get_aom_decoder_by_name(const char *name);
149const AvxInterface *get_aom_decoder_by_fourcc(uint32_t fourcc);
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800150
Yaowu Xuf883b422016-08-30 14:01:10 -0700151void aom_img_write(const aom_image_t *img, FILE *file);
152int aom_img_read(aom_image_t *img, FILE *file);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800153
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -0800154double sse_to_psnr(double samples, double peak, double mse);
Wan-Teh Chang7a864cc2018-06-27 14:19:25 -0700155void aom_img_upshift(aom_image_t *dst, const aom_image_t *src, int input_shift);
Yaowu Xubf9d6d42018-06-25 15:33:55 -0700156void aom_img_downshift(aom_image_t *dst, const aom_image_t *src,
157 int down_shift);
Wan-Teh Chang7a864cc2018-06-27 14:19:25 -0700158void aom_img_truncate_16_to_8(aom_image_t *dst, const aom_image_t *src);
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700159
Tom Finegan00a35aa2013-11-14 12:37:42 -0800160#ifdef __cplusplus
clang-format6c4d83e2016-08-08 19:03:30 -0700161} /* extern "C" */
Tom Finegan00a35aa2013-11-14 12:37:42 -0800162#endif
163
Tom Finegan03848f52013-11-05 10:02:18 -0800164#endif // TOOLS_COMMON_H_