blob: 8acb3709f340c608b971767a73ae345a1db71be1 [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
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "./aom_config.h"
17#include "aom/aom_codec.h"
18#include "aom/aom_image.h"
19#include "aom/aom_integer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020#include "aom_ports/msvc.h"
Tom Finegan00a35aa2013-11-14 12:37:42 -080021
22#if CONFIG_ENCODERS
23#include "./y4minput.h"
24#endif
Tom Finegane6579192013-11-11 06:48:18 -080025
26#if defined(_MSC_VER)
James Zerndae4cfc2014-04-02 20:06:07 -070027/* MSVS uses _f{seek,tell}i64. */
Tom Finegane6579192013-11-11 06:48:18 -080028#define fseeko _fseeki64
29#define ftello _ftelli64
James Zernb6430362017-01-14 14:18:01 -080030typedef int64_t FileOffset;
Tom Finegane6579192013-11-11 06:48:18 -080031#elif defined(_WIN32)
James Zernb6430362017-01-14 14:18:01 -080032#include <sys/types.h> /* NOLINT*/
James Zerne3578af2014-04-17 10:47:08 -070033/* MinGW uses f{seek,tell}o64 for large files. */
Tom Finegane6579192013-11-11 06:48:18 -080034#define fseeko fseeko64
35#define ftello ftello64
James Zernb6430362017-01-14 14:18:01 -080036typedef off64_t FileOffset;
37#elif CONFIG_OS_SUPPORT
38#include <sys/types.h> /* NOLINT*/
39typedef off_t FileOffset;
40/* Use 32-bit file operations in WebM file format when building ARM
41 * executables (.axf) with RVCT. */
42#else
43#define fseeko fseek
44#define ftello ftell
Tom Finegan92ca0952017-01-19 14:39:04 -080045typedef long FileOffset; /* NOLINT */
James Zernb6430362017-01-14 14:18:01 -080046#endif /* CONFIG_OS_SUPPORT */
Tom Finegane6579192013-11-11 06:48:18 -080047
48#if CONFIG_OS_SUPPORT
49#if defined(_MSC_VER)
clang-format6c4d83e2016-08-08 19:03:30 -070050#include <io.h> /* NOLINT */
51#define isatty _isatty
52#define fileno _fileno
Tom Finegane6579192013-11-11 06:48:18 -080053#else
clang-format6c4d83e2016-08-08 19:03:30 -070054#include <unistd.h> /* NOLINT */
55#endif /* _MSC_VER */
56#endif /* CONFIG_OS_SUPPORT */
Tom Finegane6579192013-11-11 06:48:18 -080057
Tom Finegane6579192013-11-11 06:48:18 -080058#define LITERALU64(hi, lo) ((((uint64_t)hi) << 32) | lo)
59
60#ifndef PATH_MAX
61#define PATH_MAX 512
62#endif
63
clang-format6c4d83e2016-08-08 19:03:30 -070064#define IVF_FRAME_HDR_SZ (4 + 8) /* 4 byte size + 8 byte timestamp */
Tom Finegan00a35aa2013-11-14 12:37:42 -080065#define IVF_FILE_HDR_SZ 32
66
67#define RAW_FRAME_HDR_SZ sizeof(uint32_t)
68
Yaowu Xuf883b422016-08-30 14:01:10 -070069#define AV1_FOURCC 0x31305641
John Koleszarc377bf02010-11-02 09:11:57 -040070
Tom Finegan00a35aa2013-11-14 12:37:42 -080071enum VideoFileType {
72 FILE_TYPE_RAW,
73 FILE_TYPE_IVF,
74 FILE_TYPE_Y4M,
75 FILE_TYPE_WEBM
76};
77
78struct FileTypeDetectionBuffer {
79 char buf[4];
80 size_t buf_read;
81 size_t position;
82};
83
Yaowu Xuf883b422016-08-30 14:01:10 -070084struct AvxRational {
Tom Finegan00a35aa2013-11-14 12:37:42 -080085 int numerator;
86 int denominator;
87};
88
Yaowu Xuf883b422016-08-30 14:01:10 -070089struct AvxInputContext {
Tom Finegan00a35aa2013-11-14 12:37:42 -080090 const char *filename;
91 FILE *file;
James Zerne3578af2014-04-17 10:47:08 -070092 int64_t length;
Tom Finegan00a35aa2013-11-14 12:37:42 -080093 struct FileTypeDetectionBuffer detect;
94 enum VideoFileType file_type;
Tom Finegan2abe2d42013-11-18 14:39:51 -080095 uint32_t width;
96 uint32_t height;
Yaowu Xuf883b422016-08-30 14:01:10 -070097 struct AvxRational pixel_aspect_ratio;
98 aom_img_fmt_t fmt;
99 aom_bit_depth_t bit_depth;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800100 int only_i420;
Tom Finegan2abe2d42013-11-18 14:39:51 -0800101 uint32_t fourcc;
Yaowu Xuf883b422016-08-30 14:01:10 -0700102 struct AvxRational framerate;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800103#if CONFIG_ENCODERS
104 y4m_input y4m;
105#endif
106};
107
108#ifdef __cplusplus
109extern "C" {
110#endif
111
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700112#if defined(__GNUC__)
Yaowu Xuf883b422016-08-30 14:01:10 -0700113#define AOM_NO_RETURN __attribute__((noreturn))
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700114#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700115#define AOM_NO_RETURN
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700116#endif
117
John Koleszarc377bf02010-11-02 09:11:57 -0400118/* Sets a stdio stream into binary mode */
John Koleszarc6b90392012-07-13 15:21:29 -0700119FILE *set_binary_mode(FILE *stream);
John Koleszarc377bf02010-11-02 09:11:57 -0400120
Yaowu Xuf883b422016-08-30 14:01:10 -0700121void die(const char *fmt, ...) AOM_NO_RETURN;
122void fatal(const char *fmt, ...) AOM_NO_RETURN;
Tom Finegan03848f52013-11-05 10:02:18 -0800123void warn(const char *fmt, ...);
124
Yaowu Xuf883b422016-08-30 14:01:10 -0700125void die_codec(aom_codec_ctx_t *ctx, const char *s) AOM_NO_RETURN;
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800126
Tom Finegan03848f52013-11-05 10:02:18 -0800127/* The tool including this file must define usage_exit() */
Yaowu Xuf883b422016-08-30 14:01:10 -0700128void usage_exit(void) AOM_NO_RETURN;
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700129
Yaowu Xuf883b422016-08-30 14:01:10 -0700130#undef AOM_NO_RETURN
Tom Finegan03848f52013-11-05 10:02:18 -0800131
Yaowu Xuf883b422016-08-30 14:01:10 -0700132int read_yuv_frame(struct AvxInputContext *input_ctx, aom_image_t *yuv_frame);
Tom Finegan00a35aa2013-11-14 12:37:42 -0800133
Yaowu Xuf883b422016-08-30 14:01:10 -0700134typedef struct AvxInterface {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800135 const char *const name;
136 const uint32_t fourcc;
Yaowu Xuf883b422016-08-30 14:01:10 -0700137 aom_codec_iface_t *(*const codec_interface)();
138} AvxInterface;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800139
Yaowu Xuf883b422016-08-30 14:01:10 -0700140int get_aom_encoder_count(void);
141const AvxInterface *get_aom_encoder_by_index(int i);
142const AvxInterface *get_aom_encoder_by_name(const char *name);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800143
Yaowu Xuf883b422016-08-30 14:01:10 -0700144int get_aom_decoder_count(void);
145const AvxInterface *get_aom_decoder_by_index(int i);
146const AvxInterface *get_aom_decoder_by_name(const char *name);
147const AvxInterface *get_aom_decoder_by_fourcc(uint32_t fourcc);
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800148
Yaowu Xuf883b422016-08-30 14:01:10 -0700149// TODO(dkovalev): move this function to aom_image.{c, h}, so it will be part
150// of aom_image_t support
151int aom_img_plane_width(const aom_image_t *img, int plane);
152int aom_img_plane_height(const aom_image_t *img, int plane);
153void aom_img_write(const aom_image_t *img, FILE *file);
154int aom_img_read(aom_image_t *img, FILE *file);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800155
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -0800156double sse_to_psnr(double samples, double peak, double mse);
157
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200158#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700159void aom_img_upshift(aom_image_t *dst, aom_image_t *src, int input_shift);
160void aom_img_downshift(aom_image_t *dst, aom_image_t *src, int down_shift);
161void aom_img_truncate_16_to_8(aom_image_t *dst, aom_image_t *src);
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700162#endif
163
Tom Finegan00a35aa2013-11-14 12:37:42 -0800164#ifdef __cplusplus
clang-format6c4d83e2016-08-08 19:03:30 -0700165} /* extern "C" */
Tom Finegan00a35aa2013-11-14 12:37:42 -0800166#endif
167
Tom Finegan03848f52013-11-05 10:02:18 -0800168#endif // TOOLS_COMMON_H_