misc/winutils/include/libavutil/rational.h
author koda
Tue, 15 Oct 2013 01:15:39 +0200
changeset 9543 1826b5315369
parent 7813 7ac83d79b897
permissions -rw-r--r--
Fix bug #656 by using the right resolution values
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7813
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
     1
/*
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
     2
 * rational numbers
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
     3
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
     4
 *
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
     5
 * This file is part of Libav.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
     6
 *
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
     7
 * Libav is free software; you can redistribute it and/or
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
     8
 * modify it under the terms of the GNU Lesser General Public
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
     9
 * License as published by the Free Software Foundation; either
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    10
 * version 2.1 of the License, or (at your option) any later version.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    11
 *
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    12
 * Libav is distributed in the hope that it will be useful,
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    15
 * Lesser General Public License for more details.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    16
 *
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    17
 * You should have received a copy of the GNU Lesser General Public
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    18
 * License along with Libav; if not, write to the Free Software
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    20
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    21
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    22
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    23
 * @file
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    24
 * rational numbers
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    25
 * @author Michael Niedermayer <michaelni@gmx.at>
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    26
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    27
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    28
#ifndef AVUTIL_RATIONAL_H
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    29
#define AVUTIL_RATIONAL_H
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    30
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    31
#include <stdint.h>
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    32
#include <limits.h>
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    33
#include "attributes.h"
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    34
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    35
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    36
 * @addtogroup lavu_math
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    37
 * @{
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    38
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    39
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    40
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    41
 * rational number numerator/denominator
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    42
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    43
typedef struct AVRational{
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    44
    int num; ///< numerator
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    45
    int den; ///< denominator
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    46
} AVRational;
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    47
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    48
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    49
 * Compare two rationals.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    50
 * @param a first rational
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    51
 * @param b second rational
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    52
 * @return 0 if a==b, 1 if a>b, -1 if a<b, and INT_MIN if one of the
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    53
 * values is of the form 0/0
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    54
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    55
static inline int av_cmp_q(AVRational a, AVRational b){
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    56
    const int64_t tmp= a.num * (int64_t)b.den - b.num * (int64_t)a.den;
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    57
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    58
    if(tmp) return ((tmp ^ a.den ^ b.den)>>63)|1;
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    59
    else if(b.den && a.den) return 0;
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    60
    else if(a.num && b.num) return (a.num>>31) - (b.num>>31);
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    61
    else                    return INT_MIN;
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    62
}
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    63
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    64
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    65
 * Convert rational to double.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    66
 * @param a rational to convert
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    67
 * @return (double) a
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    68
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    69
static inline double av_q2d(AVRational a){
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    70
    return a.num / (double) a.den;
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    71
}
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    72
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    73
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    74
 * Reduce a fraction.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    75
 * This is useful for framerate calculations.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    76
 * @param dst_num destination numerator
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    77
 * @param dst_den destination denominator
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    78
 * @param num source numerator
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    79
 * @param den source denominator
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    80
 * @param max the maximum allowed for dst_num & dst_den
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    81
 * @return 1 if exact, 0 otherwise
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    82
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    83
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max);
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    84
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    85
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    86
 * Multiply two rationals.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    87
 * @param b first rational
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    88
 * @param c second rational
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    89
 * @return b*c
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    90
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    91
AVRational av_mul_q(AVRational b, AVRational c) av_const;
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    92
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    93
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    94
 * Divide one rational by another.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    95
 * @param b first rational
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    96
 * @param c second rational
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    97
 * @return b/c
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    98
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
    99
AVRational av_div_q(AVRational b, AVRational c) av_const;
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   100
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   101
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   102
 * Add two rationals.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   103
 * @param b first rational
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   104
 * @param c second rational
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   105
 * @return b+c
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   106
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   107
AVRational av_add_q(AVRational b, AVRational c) av_const;
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   108
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   109
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   110
 * Subtract one rational from another.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   111
 * @param b first rational
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   112
 * @param c second rational
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   113
 * @return b-c
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   114
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   115
AVRational av_sub_q(AVRational b, AVRational c) av_const;
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   116
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   117
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   118
 * Invert a rational.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   119
 * @param q value
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   120
 * @return 1 / q
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   121
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   122
static av_always_inline AVRational av_inv_q(AVRational q)
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   123
{
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   124
    AVRational r = { q.den, q.num };
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   125
    return r;
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   126
}
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   127
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   128
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   129
 * Convert a double precision floating point number to a rational.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   130
 * inf is expressed as {1,0} or {-1,0} depending on the sign.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   131
 *
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   132
 * @param d double to convert
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   133
 * @param max the maximum allowed numerator and denominator
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   134
 * @return (AVRational) d
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   135
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   136
AVRational av_d2q(double d, int max) av_const;
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   137
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   138
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   139
 * @return 1 if q1 is nearer to q than q2, -1 if q2 is nearer
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   140
 * than q1, 0 if they have the same distance.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   141
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   142
int av_nearer_q(AVRational q, AVRational q1, AVRational q2);
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   143
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   144
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   145
 * Find the nearest value in q_list to q.
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   146
 * @param q_list an array of rationals terminated by {0, 0}
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   147
 * @return the index of the nearest value found in the array
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   148
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   149
int av_find_nearest_q_idx(AVRational q, const AVRational* q_list);
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   150
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   151
/**
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   152
 * @}
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   153
 */
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   154
7ac83d79b897 support video recording on windows with automation and headers
koda
parents:
diff changeset
   155
#endif /* AVUTIL_RATIONAL_H */