blob: 6d3688a79b27da5799e2bb5fc6326ddfad924252 [file] [log] [blame]
John Koleszarc377bf02010-11-02 09:11:57 -04001/*
Adrian Grangeee9843d2016-03-24 12:08:51 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszarc377bf02010-11-02 09:11:57 -04003 *
Adrian Grangeee9843d2016-03-24 12:08:51 -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
Adrian Grangecebe6f02016-03-25 12:11:05 -070016#include "./aom_config.h"
17#include "aom/aom_codec.h"
18#include "aom/aom_image.h"
19#include "aom/aom_integer.h"
Yaowu Xubf4202e2016-03-21 15:15:19 -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
30#elif defined(_WIN32)
James Zerne3578af2014-04-17 10:47:08 -070031/* MinGW uses f{seek,tell}o64 for large files. */
Tom Finegane6579192013-11-11 06:48:18 -080032#define fseeko fseeko64
33#define ftello ftello64
clang-format99e28b82016-01-27 12:42:45 -080034#endif /* _WIN32 */
Tom Finegane6579192013-11-11 06:48:18 -080035
36#if CONFIG_OS_SUPPORT
37#if defined(_MSC_VER)
clang-format99e28b82016-01-27 12:42:45 -080038#include <io.h> /* NOLINT */
39#define isatty _isatty
40#define fileno _fileno
Tom Finegane6579192013-11-11 06:48:18 -080041#else
clang-format99e28b82016-01-27 12:42:45 -080042#include <unistd.h> /* NOLINT */
43#endif /* _MSC_VER */
44#endif /* CONFIG_OS_SUPPORT */
Tom Finegane6579192013-11-11 06:48:18 -080045
46/* Use 32-bit file operations in WebM file format when building ARM
47 * executables (.axf) with RVCT. */
48#if !CONFIG_OS_SUPPORT
Tom Finegane6579192013-11-11 06:48:18 -080049#define fseeko fseek
50#define ftello ftell
clang-format99e28b82016-01-27 12:42:45 -080051#endif /* CONFIG_OS_SUPPORT */
Tom Finegane6579192013-11-11 06:48:18 -080052
53#define LITERALU64(hi, lo) ((((uint64_t)hi) << 32) | lo)
54
55#ifndef PATH_MAX
56#define PATH_MAX 512
57#endif
58
clang-format99e28b82016-01-27 12:42:45 -080059#define IVF_FRAME_HDR_SZ (4 + 8) /* 4 byte size + 8 byte timestamp */
Tom Finegan00a35aa2013-11-14 12:37:42 -080060#define IVF_FILE_HDR_SZ 32
61
62#define RAW_FRAME_HDR_SZ sizeof(uint32_t)
63
Adrian Grange80edfa02016-03-28 10:04:30 -070064#define AV1_FOURCC 0x31305641
John Koleszarc377bf02010-11-02 09:11:57 -040065
Tom Finegan00a35aa2013-11-14 12:37:42 -080066enum VideoFileType {
67 FILE_TYPE_RAW,
68 FILE_TYPE_IVF,
69 FILE_TYPE_Y4M,
70 FILE_TYPE_WEBM
71};
72
73struct FileTypeDetectionBuffer {
74 char buf[4];
75 size_t buf_read;
76 size_t position;
77};
78
Adrian Grange5082a362016-03-29 14:27:56 -070079struct AvxRational {
Tom Finegan00a35aa2013-11-14 12:37:42 -080080 int numerator;
81 int denominator;
82};
83
Adrian Grange5082a362016-03-29 14:27:56 -070084struct AvxInputContext {
Tom Finegan00a35aa2013-11-14 12:37:42 -080085 const char *filename;
86 FILE *file;
James Zerne3578af2014-04-17 10:47:08 -070087 int64_t length;
Tom Finegan00a35aa2013-11-14 12:37:42 -080088 struct FileTypeDetectionBuffer detect;
89 enum VideoFileType file_type;
Tom Finegan2abe2d42013-11-18 14:39:51 -080090 uint32_t width;
91 uint32_t height;
Adrian Grange5082a362016-03-29 14:27:56 -070092 struct AvxRational pixel_aspect_ratio;
Adrian Grangecebe6f02016-03-25 12:11:05 -070093 aom_img_fmt_t fmt;
94 aom_bit_depth_t bit_depth;
Tom Finegan00a35aa2013-11-14 12:37:42 -080095 int only_i420;
Tom Finegan2abe2d42013-11-18 14:39:51 -080096 uint32_t fourcc;
Adrian Grange5082a362016-03-29 14:27:56 -070097 struct AvxRational framerate;
Tom Finegan00a35aa2013-11-14 12:37:42 -080098#if CONFIG_ENCODERS
99 y4m_input y4m;
100#endif
101};
102
103#ifdef __cplusplus
104extern "C" {
105#endif
106
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700107#if defined(__GNUC__)
Adrian Grangeff00fc02016-03-25 12:57:08 -0700108#define AOM_NO_RETURN __attribute__((noreturn))
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700109#else
Adrian Grangeff00fc02016-03-25 12:57:08 -0700110#define AOM_NO_RETURN
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700111#endif
112
John Koleszarc377bf02010-11-02 09:11:57 -0400113/* Sets a stdio stream into binary mode */
John Koleszarc6b90392012-07-13 15:21:29 -0700114FILE *set_binary_mode(FILE *stream);
John Koleszarc377bf02010-11-02 09:11:57 -0400115
Adrian Grangeff00fc02016-03-25 12:57:08 -0700116void die(const char *fmt, ...) AOM_NO_RETURN;
117void fatal(const char *fmt, ...) AOM_NO_RETURN;
Tom Finegan03848f52013-11-05 10:02:18 -0800118void warn(const char *fmt, ...);
119
Adrian Grangeff00fc02016-03-25 12:57:08 -0700120void die_codec(aom_codec_ctx_t *ctx, const char *s) AOM_NO_RETURN;
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800121
Tom Finegan03848f52013-11-05 10:02:18 -0800122/* The tool including this file must define usage_exit() */
Adrian Grangeff00fc02016-03-25 12:57:08 -0700123void usage_exit(void) AOM_NO_RETURN;
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700124
Adrian Grangeff00fc02016-03-25 12:57:08 -0700125#undef AOM_NO_RETURN
Tom Finegan03848f52013-11-05 10:02:18 -0800126
Adrian Grange5082a362016-03-29 14:27:56 -0700127int read_yuv_frame(struct AvxInputContext *input_ctx, aom_image_t *yuv_frame);
Tom Finegan00a35aa2013-11-14 12:37:42 -0800128
Adrian Grange5082a362016-03-29 14:27:56 -0700129typedef struct AvxInterface {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800130 const char *const name;
131 const uint32_t fourcc;
Adrian Grangecebe6f02016-03-25 12:11:05 -0700132 aom_codec_iface_t *(*const codec_interface)();
Adrian Grange5082a362016-03-29 14:27:56 -0700133} AvxInterface;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800134
Adrian Grangecebe6f02016-03-25 12:11:05 -0700135int get_aom_encoder_count(void);
Adrian Grange5082a362016-03-29 14:27:56 -0700136const AvxInterface *get_aom_encoder_by_index(int i);
137const AvxInterface *get_aom_encoder_by_name(const char *name);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800138
Adrian Grangecebe6f02016-03-25 12:11:05 -0700139int get_aom_decoder_count(void);
Adrian Grange5082a362016-03-29 14:27:56 -0700140const AvxInterface *get_aom_decoder_by_index(int i);
141const AvxInterface *get_aom_decoder_by_name(const char *name);
142const AvxInterface *get_aom_decoder_by_fourcc(uint32_t fourcc);
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800143
Adrian Grangecebe6f02016-03-25 12:11:05 -0700144// TODO(dkovalev): move this function to aom_image.{c, h}, so it will be part
145// of aom_image_t support
146int aom_img_plane_width(const aom_image_t *img, int plane);
147int aom_img_plane_height(const aom_image_t *img, int plane);
148void aom_img_write(const aom_image_t *img, FILE *file);
149int aom_img_read(aom_image_t *img, FILE *file);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800150
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -0800151double sse_to_psnr(double samples, double peak, double mse);
152
Yaowu Xu01dee0b2016-03-25 12:43:01 -0700153#if CONFIG_AOM_HIGHBITDEPTH
Adrian Grangecebe6f02016-03-25 12:11:05 -0700154void aom_img_upshift(aom_image_t *dst, aom_image_t *src, int input_shift);
155void aom_img_downshift(aom_image_t *dst, aom_image_t *src, int down_shift);
156void aom_img_truncate_16_to_8(aom_image_t *dst, aom_image_t *src);
Deb Mukherjee7a2a6112014-10-06 20:46:11 -0700157#endif
158
Tom Finegan00a35aa2013-11-14 12:37:42 -0800159#ifdef __cplusplus
clang-format99e28b82016-01-27 12:42:45 -0800160} /* extern "C" */
Tom Finegan00a35aa2013-11-14 12:37:42 -0800161#endif
162
Tom Finegan03848f52013-11-05 10:02:18 -0800163#endif // TOOLS_COMMON_H_