blob: 6fa9b6afaf363e81fe0510ca995880a47e418c9c [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
Tom Fineganba02c242017-05-16 15:01:54 -070022#if CONFIG_AV1_ENCODER
Tom Finegan00a35aa2013-11-14 12:37:42 -080023#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 {
Cyril Concolato6c788832017-10-30 16:30:35 -070072#if CONFIG_OBU_NO_IVF
73 FILE_TYPE_OBU,
74#endif
Tom Finegan00a35aa2013-11-14 12:37:42 -080075 FILE_TYPE_RAW,
76 FILE_TYPE_IVF,
77 FILE_TYPE_Y4M,
78 FILE_TYPE_WEBM
79};
80
81struct FileTypeDetectionBuffer {
82 char buf[4];
83 size_t buf_read;
84 size_t position;
85};
86
Yaowu Xuf883b422016-08-30 14:01:10 -070087struct AvxRational {
Tom Finegan00a35aa2013-11-14 12:37:42 -080088 int numerator;
89 int denominator;
90};
91
Yaowu Xuf883b422016-08-30 14:01:10 -070092struct AvxInputContext {
Tom Finegan00a35aa2013-11-14 12:37:42 -080093 const char *filename;
94 FILE *file;
James Zerne3578af2014-04-17 10:47:08 -070095 int64_t length;
Tom Finegan00a35aa2013-11-14 12:37:42 -080096 struct FileTypeDetectionBuffer detect;
97 enum VideoFileType file_type;
Tom Finegan2abe2d42013-11-18 14:39:51 -080098 uint32_t width;
99 uint32_t height;
Yaowu Xuf883b422016-08-30 14:01:10 -0700100 struct AvxRational pixel_aspect_ratio;
101 aom_img_fmt_t fmt;
102 aom_bit_depth_t bit_depth;
Tom Finegan00a35aa2013-11-14 12:37:42 -0800103 int only_i420;
Tom Finegan2abe2d42013-11-18 14:39:51 -0800104 uint32_t fourcc;
Yaowu Xuf883b422016-08-30 14:01:10 -0700105 struct AvxRational framerate;
Tom Fineganba02c242017-05-16 15:01:54 -0700106#if CONFIG_AV1_ENCODER
Tom Finegan00a35aa2013-11-14 12:37:42 -0800107 y4m_input y4m;
108#endif
109};
110
111#ifdef __cplusplus
112extern "C" {
113#endif
114
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700115#if defined(__GNUC__)
Yaowu Xuf883b422016-08-30 14:01:10 -0700116#define AOM_NO_RETURN __attribute__((noreturn))
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700117#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700118#define AOM_NO_RETURN
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700119#endif
120
John Koleszarc377bf02010-11-02 09:11:57 -0400121/* Sets a stdio stream into binary mode */
John Koleszarc6b90392012-07-13 15:21:29 -0700122FILE *set_binary_mode(FILE *stream);
John Koleszarc377bf02010-11-02 09:11:57 -0400123
Yaowu Xuf883b422016-08-30 14:01:10 -0700124void die(const char *fmt, ...) AOM_NO_RETURN;
125void fatal(const char *fmt, ...) AOM_NO_RETURN;
Tom Finegan03848f52013-11-05 10:02:18 -0800126void warn(const char *fmt, ...);
127
Yaowu Xuf883b422016-08-30 14:01:10 -0700128void die_codec(aom_codec_ctx_t *ctx, const char *s) AOM_NO_RETURN;
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800129
Tom Finegan03848f52013-11-05 10:02:18 -0800130/* The tool including this file must define usage_exit() */
Yaowu Xuf883b422016-08-30 14:01:10 -0700131void usage_exit(void) AOM_NO_RETURN;
Jim Bankoskie9b878c2014-08-21 11:43:36 -0700132
Yaowu Xuf883b422016-08-30 14:01:10 -0700133#undef AOM_NO_RETURN
Tom Finegan03848f52013-11-05 10:02:18 -0800134
Yaowu Xuf883b422016-08-30 14:01:10 -0700135int read_yuv_frame(struct AvxInputContext *input_ctx, aom_image_t *yuv_frame);
Tom Finegan00a35aa2013-11-14 12:37:42 -0800136
Yaowu Xuf883b422016-08-30 14:01:10 -0700137typedef struct AvxInterface {
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800138 const char *const name;
139 const uint32_t fourcc;
Yaowu Xuf883b422016-08-30 14:01:10 -0700140 aom_codec_iface_t *(*const codec_interface)();
141} AvxInterface;
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800142
Yaowu Xuf883b422016-08-30 14:01:10 -0700143int get_aom_encoder_count(void);
144const AvxInterface *get_aom_encoder_by_index(int i);
145const AvxInterface *get_aom_encoder_by_name(const char *name);
Dmitry Kovalev70d96642014-02-11 21:12:23 -0800146
Yaowu Xuf883b422016-08-30 14:01:10 -0700147int get_aom_decoder_count(void);
148const AvxInterface *get_aom_decoder_by_index(int i);
149const AvxInterface *get_aom_decoder_by_name(const char *name);
150const AvxInterface *get_aom_decoder_by_fourcc(uint32_t fourcc);
Dmitry Kovalev7ec27692014-01-27 13:40:29 -0800151
Yaowu Xuf883b422016-08-30 14:01:10 -0700152void aom_img_write(const aom_image_t *img, FILE *file);
153int aom_img_read(aom_image_t *img, FILE *file);
Dmitry Kovalev592936b2014-02-07 11:37:39 -0800154
Dmitry Kovalev2dad0e12014-02-27 14:00:41 -0800155double sse_to_psnr(double samples, double peak, double mse);
Yaowu Xuf883b422016-08-30 14:01:10 -0700156void aom_img_upshift(aom_image_t *dst, aom_image_t *src, int input_shift);
157void aom_img_downshift(aom_image_t *dst, aom_image_t *src, int down_shift);
158void aom_img_truncate_16_to_8(aom_image_t *dst, 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_