Revision c11c2bc2

View differences:

Makefile
6 6

  
7 7
VPATH=$(SRC_PATH)
8 8

  
9
CFLAGS=$(OPTFLAGS) -I. -I$(SRC_PATH) -I$(SRC_PATH)/libavcodec -I$(SRC_PATH)/libavformat -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE
9
CFLAGS=$(OPTFLAGS) -I. -I$(SRC_PATH) -I$(SRC_PATH)/libavutil -I$(SRC_PATH)/libavcodec -I$(SRC_PATH)/libavformat -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE
10 10
LDFLAGS+= -g 
11 11

  
12 12
ifeq ($(TARGET_GPROF),yes)
......
91 91

  
92 92
OBJS = ffmpeg.o ffserver.o cmdutils.o $(FFPLAY_O)
93 93
SRCS = $(OBJS:.o=.c) $(ASM_OBJS:.o=.s)
94
FFLIBS = -L./libavformat -lavformat$(BUILDSUF) -L./libavcodec -lavcodec$(BUILDSUF)
94
FFLIBS = -L./libavformat -lavformat$(BUILDSUF) -L./libavcodec -lavcodec$(BUILDSUF) -L./libavutil -lavutil$(BUILDSUF)
95 95

  
96 96
all: lib $(PROG) $(PROGTEST) $(VHOOK) $(QTFASTSTART) $(DOC)
97 97

  
98 98
lib:
99
	$(MAKE) -C libavutil all
99 100
	$(MAKE) -C libavcodec all
100 101
	$(MAKE) -C libavformat all
101 102

  
......
140 141
.PHONY: install
141 142

  
142 143
install: all install-man $(INSTALLVHOOK)
144
	$(MAKE) -C libavutil install
143 145
	$(MAKE) -C libavcodec install
144 146
	$(MAKE) -C libavformat install
145 147
	install -d "$(bindir)"
......
162 164
	$(MAKE) -C vhook install
163 165

  
164 166
installlib:
167
	$(MAKE) -C libavutil installlib
165 168
	$(MAKE) -C libavcodec installlib
166 169
	$(MAKE) -C libavformat installlib
167 170

  
......
182 185
	@for i in $(DEP_LIBS) ; do if $(TEST) $$i -nt .libs ; then touch .libs; fi ; done
183 186

  
184 187
clean: $(CLEANVHOOK)
188
	$(MAKE) -C libavutil clean
185 189
	$(MAKE) -C libavcodec clean
186 190
	$(MAKE) -C libavformat clean
187 191
	$(MAKE) -C tests clean
libavcodec/Makefile
7 7
VPATH=$(SRC_PATH)/libavcodec
8 8

  
9 9
# NOTE: -I.. is needed to include config.h
10
CFLAGS=$(OPTFLAGS) -DHAVE_AV_CONFIG_H -I.. -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE $(AMR_CFLAGS)
10
CFLAGS=$(OPTFLAGS) -DHAVE_AV_CONFIG_H -I.. -I$(SRC_PATH)/libavutil -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_GNU_SOURCE $(AMR_CFLAGS)
11 11

  
12 12
OBJS= bitstream.o utils.o mem.o allcodecs.o \
13 13
      mpegvideo.o jrevdct.o jfdctfst.o jfdctint.o\
......
16 16
      mpeg12.o mpegaudiodec.o pcm.o simple_idct.o \
17 17
      ratecontrol.o adpcm.o eval.o error_resilience.o \
18 18
      fft.o mdct.o raw.o golomb.o cabac.o\
19
      dpcm.o adx.o rational.o faandct.o parser.o g726.o \
20
      vp3dsp.o integer.o h264idct.o rangecoder.o pnm.o h263.o msmpeg4.o h263dec.o dvdsub.o dvbsub.o dvbsubdec.o
19
      dpcm.o adx.o faandct.o parser.o g726.o \
20
      vp3dsp.o h264idct.o rangecoder.o pnm.o h263.o msmpeg4.o h263dec.o dvdsub.o dvbsub.o dvbsubdec.o
21 21

  
22 22
ifeq ($(CONFIG_AASC_DECODER),yes)
23 23
    OBJS+= aasc.o
......
234 234
endif
235 235
endif
236 236

  
237
EXTRALIBS += -L$(SRC_PATH)/libavutil -lavutil$(BUILDSUF)
238

  
237 239
# currently using libdts for dts decoding
238 240
ifeq ($(CONFIG_DTS),yes)
239 241
OBJS+= dtsdec.o
......
375 377
OBJS := $(OBJS) $(ASM_OBJS)
376 378

  
377 379
LIB= $(LIBPREF)avcodec$(LIBSUF)
380
LIBAVUTIL= $(SRC_PATH)/libavutil/$(LIBPREF)avutil$(LIBSUF)
378 381
ifeq ($(BUILD_SHARED),yes)
379 382
SLIB= $(SLIBPREF)avcodec$(SLIBSUF)
380 383
endif
......
446 449

  
447 450
# api example program
448 451
apiexample: apiexample.c $(LIB)
449
	$(CC) $(CFLAGS) -o $@ $< $(LIB) $(EXTRALIBS) -lm
452
	$(CC) $(CFLAGS) -o $@ $< $(LIB) $(LIBAVUTIL) $(EXTRALIBS) -lm
450 453

  
451 454
# cpuid test
452 455
cpuid_test: i386/cputest.c
......
464 467
	$(CC) -o $@ $^ -lm
465 468

  
466 469
fft-test: fft-test.o $(LIB)
467
	$(CC) -o $@ $^ -lm
470
	$(CC) -o $@ $^ $(LIBAVUTIL) -lm
468 471

  
469 472
ifeq ($(BUILD_SHARED),yes)
470 473
install: all install-headers
......
489 492
install-headers:
490 493
	mkdir -p "$(prefix)/include/ffmpeg"
491 494
	install -m 644 $(SRC_PATH)/libavcodec/avcodec.h \
492
	               $(SRC_PATH)/libavcodec/common.h \
493
	               $(SRC_PATH)/libavcodec/rational.h \
494 495
                "$(prefix)/include/ffmpeg"
495 496
	install -d $(libdir)/pkgconfig
496 497
	install -m 644 ../libavcodec.pc $(libdir)/pkgconfig
libavcodec/avcodec.h
11 11
extern "C" {
12 12
#endif
13 13

  
14
#include "common.h"
15
#include "rational.h"
14
#include "avutil.h"
16 15
#include <sys/types.h> /* size_t */
17 16

  
18 17
//FIXME the following 2 really dont belong in here
......
22 21
#define AV_STRINGIFY(s)	AV_TOSTRING(s)
23 22
#define AV_TOSTRING(s) #s
24 23

  
25
#define LIBAVCODEC_VERSION_INT ((49<<16)+(0<<8)+0)
26
#define LIBAVCODEC_VERSION     49.0.0
24
#define LIBAVCODEC_VERSION_INT ((49<<16)+(0<<8)+1)
25
#define LIBAVCODEC_VERSION     49.0.1
27 26
#define LIBAVCODEC_BUILD       LIBAVCODEC_VERSION_INT
28 27

  
29 28
#define LIBAVCODEC_IDENT       "Lavc" AV_STRINGIFY(LIBAVCODEC_VERSION)
......
279 278
    ME_X1
280 279
};
281 280

  
282
enum AVRounding {
283
    AV_ROUND_ZERO     = 0, ///< round toward zero
284
    AV_ROUND_INF      = 1, ///< round away from zero
285
    AV_ROUND_DOWN     = 2, ///< round toward -infinity
286
    AV_ROUND_UP       = 3, ///< round toward +infinity
287
    AV_ROUND_NEAR_INF = 5, ///< round to nearest and halfway cases away from zero
288
};
289

  
290 281
enum AVDiscard{
291 282
//we leave some space between them for extensions (drop some keyframes for intra only or drop just some bidir frames)
292 283
    AVDISCARD_NONE   =-16, ///< discard nothing
......
2308 2299
 */
2309 2300
char av_get_pict_type_char(int pict_type);
2310 2301

  
2311
/**
2312
 * reduce a fraction.
2313
 * this is usefull for framerate calculations
2314
 * @param max the maximum allowed for dst_nom & dst_den
2315
 * @return 1 if exact, 0 otherwise
2316
 */
2317
int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max);
2318

  
2319
/**
2320
 * rescale a 64bit integer with rounding to nearest.
2321
 * a simple a*b/c isn't possible as it can overflow
2322
 */
2323
int64_t av_rescale(int64_t a, int64_t b, int64_t c);
2324

  
2325
/**
2326
 * rescale a 64bit integer with specified rounding.
2327
 * a simple a*b/c isn't possible as it can overflow
2328
 */
2329
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding);
2330

  
2331
/**
2332
 * rescale a 64bit integer by 2 rational numbers.
2333
 */
2334
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq);
2335

  
2336
double av_int2dbl(int64_t v);
2337
float av_int2flt(int32_t v);
2338
int64_t av_dbl2int(double d);
2339
int32_t av_flt2int(float d);
2340

  
2341 2302

  
2342 2303
/* frame parsing */
2343 2304
typedef struct AVCodecParserContext {
libavcodec/bswap.h
1
/**
2
 * @file bswap.h
3
 * byte swap.
4
 */
5

  
6
#ifndef __BSWAP_H__
7
#define __BSWAP_H__
8

  
9
#ifdef HAVE_BYTESWAP_H
10
#include <byteswap.h>
11
#else
12

  
13
#ifdef ARCH_X86_64
14
#  define LEGACY_REGS "=Q"
15
#else
16
#  define LEGACY_REGS "=q"
17
#endif
18

  
19
#if defined(ARCH_X86) || defined(ARCH_X86_64)
20
static always_inline uint16_t bswap_16(uint16_t x)
21
{
22
  __asm("rorw $8, %0"	:
23
        LEGACY_REGS (x)	:
24
        "0" (x));
25
    return x;
26
}
27

  
28
static always_inline uint32_t bswap_32(uint32_t x)
29
{
30
#if __CPU__ > 386
31
 __asm("bswap	%0":
32
      "=r" (x)     :
33
#else
34
 __asm("xchgb	%b0,%h0\n"
35
      "	rorl	$16,%0\n"
36
      "	xchgb	%b0,%h0":
37
      LEGACY_REGS (x)		:
38
#endif
39
      "0" (x));
40
  return x;
41
}
42

  
43
static inline uint64_t bswap_64(uint64_t x)
44
{
45
#ifdef ARCH_X86_64
46
  __asm("bswap	%0":
47
        "=r" (x)     :
48
        "0" (x));
49
  return x;
50
#else
51
    union { 
52
        uint64_t ll;
53
        struct {
54
           uint32_t l,h;
55
        } l;
56
    } r;
57
    r.l.l = bswap_32 (x);
58
    r.l.h = bswap_32 (x>>32);
59
    return r.ll;
60
#endif
61
}
62

  
63
#elif defined(ARCH_SH4)
64

  
65
static always_inline uint16_t bswap_16(uint16_t x) {
66
	__asm__("swap.b %0,%0":"=r"(x):"0"(x));
67
	return x;
68
}
69

  
70
static always_inline uint32_t bswap_32(uint32_t x) {
71
	__asm__(
72
	"swap.b %0,%0\n"
73
	"swap.w %0,%0\n"
74
	"swap.b %0,%0\n"
75
	:"=r"(x):"0"(x));
76
	return x;
77
}
78

  
79
static inline uint64_t bswap_64(uint64_t x)
80
{
81
    union { 
82
        uint64_t ll;
83
        struct {
84
           uint32_t l,h;
85
        } l;
86
    } r;
87
    r.l.l = bswap_32 (x);
88
    r.l.h = bswap_32 (x>>32);
89
    return r.ll;
90
}
91
#else
92

  
93
static always_inline uint16_t bswap_16(uint16_t x){
94
    return (x>>8) | (x<<8);
95
}
96

  
97
#ifdef ARCH_ARM
98
static always_inline uint32_t bswap_32(uint32_t x){
99
    uint32_t t;
100
    __asm__ (
101
      "eor %1, %0, %0, ror #16 \n\t"
102
      "bic %1, %1, #0xFF0000   \n\t"
103
      "mov %0, %0, ror #8      \n\t"
104
      "eor %0, %0, %1, lsr #8  \n\t"
105
      : "+r"(x), "+r"(t));
106
    return x;
107
}
108
#else
109
static always_inline uint32_t bswap_32(uint32_t x){
110
    x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
111
    return (x>>16) | (x<<16);
112
}
113
#endif
114

  
115
static inline uint64_t bswap_64(uint64_t x)
116
{
117
#if 0
118
    x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
119
    x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
120
    return (x>>32) | (x<<32);
121
#else
122
    union { 
123
        uint64_t ll;
124
        uint32_t l[2]; 
125
    } w, r;
126
    w.ll = x;
127
    r.l[0] = bswap_32 (w.l[1]);
128
    r.l[1] = bswap_32 (w.l[0]);
129
    return r.ll;
130
#endif
131
}
132
#endif	/* !ARCH_X86 */
133

  
134
#endif	/* !HAVE_BYTESWAP_H */
135

  
136
// be2me ... BigEndian to MachineEndian
137
// le2me ... LittleEndian to MachineEndian
138

  
139
#ifdef WORDS_BIGENDIAN
140
#define be2me_16(x) (x)
141
#define be2me_32(x) (x)
142
#define be2me_64(x) (x)
143
#define le2me_16(x) bswap_16(x)
144
#define le2me_32(x) bswap_32(x)
145
#define le2me_64(x) bswap_64(x)
146
#else
147
#define be2me_16(x) bswap_16(x)
148
#define be2me_32(x) bswap_32(x)
149
#define be2me_64(x) bswap_64(x)
150
#define le2me_16(x) (x)
151
#define le2me_32(x) (x)
152
#define le2me_64(x) (x)
153
#endif
154

  
155
#endif /* __BSWAP_H__ */
libavcodec/common.h
1
/**
2
 * @file common.h
3
 * common internal api header.
4
 */
5

  
6
#ifndef COMMON_H
7
#define COMMON_H
8

  
9
#if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
10
#    define CONFIG_WIN32
11
#endif
12

  
13
#ifndef M_PI
14
#define M_PI    3.14159265358979323846
15
#endif
16

  
17
#ifdef HAVE_AV_CONFIG_H
18
/* only include the following when compiling package */
19
#    include "config.h"
20

  
21
#    include <stdlib.h>
22
#    include <stdio.h>
23
#    include <string.h>
24
#    include <ctype.h>
25
#    include <limits.h>
26
#    ifndef __BEOS__
27
#        include <errno.h>
28
#    else
29
#        include "berrno.h"
30
#    endif
31
#    include <math.h>
32

  
33
#    ifndef ENODATA
34
#        define ENODATA  61
35
#    endif
36

  
37
#include <stddef.h>
38
#ifndef offsetof
39
# define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
40
#endif
41

  
42
#define AVOPTION_CODEC_BOOL(name, help, field) \
43
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_BOOL }
44
#define AVOPTION_CODEC_DOUBLE(name, help, field, minv, maxv, defval) \
45
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_DOUBLE, minv, maxv, defval }
46
#define AVOPTION_CODEC_FLAG(name, help, field, flag, defval) \
47
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_FLAG, flag, 0, defval }
48
#define AVOPTION_CODEC_INT(name, help, field, minv, maxv, defval) \
49
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_INT, minv, maxv, defval }
50
#define AVOPTION_CODEC_STRING(name, help, field, str, val) \
51
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_STRING, .defval = val, .defstr = str }
52
#define AVOPTION_CODEC_RCOVERRIDE(name, help, field) \
53
    { name, help, offsetof(AVCodecContext, field), FF_OPT_TYPE_RCOVERRIDE, .defval = 0, .defstr = NULL }
54
#define AVOPTION_SUB(ptr) { .name = NULL, .help = (const char*)ptr }
55
#define AVOPTION_END() AVOPTION_SUB(NULL)
56

  
57
#endif /* HAVE_AV_CONFIG_H */
58

  
59
/* Suppress restrict if it was not defined in config.h.  */
60
#ifndef restrict
61
#    define restrict
62
#endif
63

  
64
#ifndef always_inline
65
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
66
#    define always_inline __attribute__((always_inline)) inline
67
#else
68
#    define always_inline inline
69
#endif
70
#endif
71

  
72
#ifndef attribute_used
73
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
74
#    define attribute_used __attribute__((used))
75
#else
76
#    define attribute_used
77
#endif
78
#endif
79

  
80
#ifndef attribute_unused
81
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
82
#    define attribute_unused __attribute__((unused))
83
#else
84
#    define attribute_unused
85
#endif
86
#endif
87

  
88
#ifndef EMULATE_INTTYPES
89
#   include <inttypes.h>
90
#else
91
    typedef signed char  int8_t;
92
    typedef signed short int16_t;
93
    typedef signed int   int32_t;
94
    typedef unsigned char  uint8_t;
95
    typedef unsigned short uint16_t;
96
    typedef unsigned int   uint32_t;
97

  
98
#   ifdef CONFIG_WIN32
99
        typedef signed __int64   int64_t;
100
        typedef unsigned __int64 uint64_t;
101
#   else /* other OS */
102
        typedef signed long long   int64_t;
103
        typedef unsigned long long uint64_t;
104
#   endif /* other OS */
105
#endif /* HAVE_INTTYPES_H */
106

  
107
#ifndef INT16_MIN
108
#define INT16_MIN       (-0x7fff-1)
109
#endif
110

  
111
#ifndef INT16_MAX
112
#define INT16_MAX       0x7fff
113
#endif
114

  
115
#ifndef INT64_MIN
116
#define INT64_MIN       (-0x7fffffffffffffffLL-1)
117
#endif
118

  
119
#ifndef INT64_MAX
120
#define INT64_MAX int64_t_C(9223372036854775807)
121
#endif
122

  
123
#ifndef UINT64_MAX
124
#define UINT64_MAX uint64_t_C(0xFFFFFFFFFFFFFFFF)
125
#endif
126

  
127
#ifdef EMULATE_FAST_INT
128
typedef signed char int_fast8_t;
129
typedef signed int  int_fast16_t;
130
typedef signed int  int_fast32_t;
131
typedef unsigned char uint_fast8_t;
132
typedef unsigned int  uint_fast16_t;
133
typedef unsigned int  uint_fast32_t;
134
typedef uint64_t      uint_fast64_t;
135
#endif
136

  
137
#ifndef INT_BIT
138
#    if INT_MAX != 2147483647
139
#        define INT_BIT 64
140
#    else
141
#        define INT_BIT 32
142
#    endif
143
#endif
144

  
145
#if defined(CONFIG_OS2) || defined(CONFIG_SUNOS)
146
static inline float floorf(float f) { 
147
    return floor(f); 
148
}
149
#endif
150

  
151
#ifdef CONFIG_WIN32
152

  
153
/* windows */
154

  
155
#    if !defined(__MINGW32__) && !defined(__CYGWIN__)
156
#        define int64_t_C(c)     (c ## i64)
157
#        define uint64_t_C(c)    (c ## i64)
158

  
159
#    ifdef HAVE_AV_CONFIG_H
160
#            define inline __inline
161
#    endif
162

  
163
#    else
164
#        define int64_t_C(c)     (c ## LL)
165
#        define uint64_t_C(c)    (c ## ULL)
166
#    endif /* __MINGW32__ */
167

  
168
#    ifdef HAVE_AV_CONFIG_H
169
#        ifdef _DEBUG
170
#            define DEBUG
171
#        endif
172

  
173
#        define snprintf _snprintf
174
#        define vsnprintf _vsnprintf
175
#    endif
176

  
177
/* CONFIG_WIN32 end */
178
#elif defined (CONFIG_OS2)
179
/* OS/2 EMX */
180

  
181
#ifndef int64_t_C
182
#define int64_t_C(c)     (c ## LL)
183
#define uint64_t_C(c)    (c ## ULL)
184
#endif
185

  
186
#ifdef HAVE_AV_CONFIG_H
187

  
188
#ifdef USE_FASTMEMCPY
189
#include "fastmemcpy.h"
190
#endif
191

  
192
#include <float.h>
193

  
194
#endif /* HAVE_AV_CONFIG_H */
195

  
196
/* CONFIG_OS2 end */
197
#else
198

  
199
/* unix */
200

  
201
#ifndef int64_t_C
202
#define int64_t_C(c)     (c ## LL)
203
#define uint64_t_C(c)    (c ## ULL)
204
#endif
205

  
206
#ifdef HAVE_AV_CONFIG_H
207

  
208
#        ifdef USE_FASTMEMCPY
209
#            include "fastmemcpy.h"
210
#        endif
211
#    endif /* HAVE_AV_CONFIG_H */
212

  
213
#endif /* !CONFIG_WIN32 && !CONFIG_OS2 */
214

  
215
#ifdef HAVE_AV_CONFIG_H
216

  
217
#    include "bswap.h"
218

  
219
// Use rip-relative addressing if compiling PIC code on x86-64.
220
#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
221
        defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
222
#        if defined(ARCH_X86_64) && defined(PIC)
223
#            define MANGLE(a) "_" #a"(%%rip)"
224
#        else
225
#            define MANGLE(a) "_" #a
226
#        endif
227
#    else
228
#        if defined(ARCH_X86_64) && defined(PIC)
229
#            define MANGLE(a) #a"(%%rip)"
230
#        else
231
#            define MANGLE(a) #a
232
#        endif
233
#    endif
234

  
235
/* debug stuff */
236

  
237
#    ifndef DEBUG
238
#        define NDEBUG
239
#    endif
240
#    include <assert.h>
241

  
242
/* dprintf macros */
243
#    if defined(CONFIG_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
244

  
245
inline void dprintf(const char* fmt,...) {}
246

  
247
#    else
248

  
249
#        ifdef DEBUG
250
#            define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
251
#        else
252
#            define dprintf(fmt,...)
253
#        endif
254

  
255
#    endif /* !CONFIG_WIN32 */
256

  
257
#    define av_abort()      do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
258

  
259
//rounded divison & shift
260
#define RSHIFT(a,b) ((a) > 0 ? ((a) + ((1<<(b))>>1))>>(b) : ((a) + ((1<<(b))>>1)-1)>>(b))
261
/* assume b>0 */
262
#define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
263
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
264

  
265
#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
266
#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
267

  
268
extern const uint32_t inverse[256];
269

  
270
#if defined(ARCH_X86) || defined(ARCH_X86_64)
271
#    define FASTDIV(a,b) \
272
    ({\
273
        int ret,dmy;\
274
        asm volatile(\
275
            "mull %3"\
276
            :"=d"(ret),"=a"(dmy)\
277
            :"1"(a),"g"(inverse[b])\
278
            );\
279
        ret;\
280
    })
281
#elif defined(CONFIG_FASTDIV)
282
#    define FASTDIV(a,b)   ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
283
#else
284
#    define FASTDIV(a,b)   ((a)/(b))
285
#endif
286
 
287
/* define it to include statistics code (useful only for optimizing
288
   codec efficiency */
289
//#define STATS
290

  
291
#ifdef STATS
292

  
293
enum {
294
    ST_UNKNOWN,
295
    ST_DC,
296
    ST_INTRA_AC,
297
    ST_INTER_AC,
298
    ST_INTRA_MB,
299
    ST_INTER_MB,
300
    ST_MV,
301
    ST_NB,
302
};
303

  
304
extern int st_current_index;
305
extern unsigned int st_bit_counts[ST_NB];
306
extern unsigned int st_out_bit_counts[ST_NB];
307

  
308
void print_stats(void);
309
#endif
310

  
311
/* misc math functions */
312
extern const uint8_t ff_log2_tab[256];
313

  
314
static inline int av_log2(unsigned int v)
315
{
316
    int n;
317

  
318
    n = 0;
319
    if (v & 0xffff0000) {
320
        v >>= 16;
321
        n += 16;
322
    }
323
    if (v & 0xff00) {
324
        v >>= 8;
325
        n += 8;
326
    }
327
    n += ff_log2_tab[v];
328

  
329
    return n;
330
}
331

  
332
static inline int av_log2_16bit(unsigned int v)
333
{
334
    int n;
335

  
336
    n = 0;
337
    if (v & 0xff00) {
338
        v >>= 8;
339
        n += 8;
340
    }
341
    n += ff_log2_tab[v];
342

  
343
    return n;
344
}
345

  
346
/* median of 3 */
347
static inline int mid_pred(int a, int b, int c)
348
{
349
#if 0
350
    int t= (a-b)&((a-b)>>31);
351
    a-=t;
352
    b+=t;
353
    b-= (b-c)&((b-c)>>31);
354
    b+= (a-b)&((a-b)>>31);
355

  
356
    return b;
357
#else
358
    if(a>b){
359
        if(c>b){
360
            if(c>a) b=a;
361
            else    b=c;
362
        }
363
    }else{
364
        if(b>c){
365
            if(c>a) b=c;
366
            else    b=a;
367
        }
368
    }
369
    return b;
370
#endif
371
}
372

  
373
static inline int clip(int a, int amin, int amax)
374
{
375
    if (a < amin)
376
        return amin;
377
    else if (a > amax)
378
        return amax;
379
    else
380
        return a;
381
}
382

  
383
static inline int clip_uint8(int a)
384
{
385
    if (a&(~255)) return (-a)>>31;
386
    else          return a;
387
}
388

  
389
/* math */
390
extern const uint8_t ff_sqrt_tab[128];
391

  
392
int64_t ff_gcd(int64_t a, int64_t b);
393

  
394
static inline int ff_sqrt(int a)
395
{
396
    int ret=0;
397
    int s;
398
    int ret_sq=0;
399
    
400
    if(a<128) return ff_sqrt_tab[a];
401
    
402
    for(s=15; s>=0; s--){
403
        int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
404
        if(b<=a){
405
            ret_sq=b;
406
            ret+= 1<<s;
407
        }
408
    }
409
    return ret;
410
}
411

  
412
/**
413
 * converts fourcc string to int
414
 */
415
static inline int ff_get_fourcc(const char *s){
416
    assert( strlen(s)==4 );
417

  
418
    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
419
}
420

  
421
#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
422
#define MKBETAG(a,b,c,d) (d | (c << 8) | (b << 16) | (a << 24))
423

  
424

  
425
#if defined(ARCH_X86) || defined(ARCH_X86_64)
426
#define MASK_ABS(mask, level)\
427
            asm volatile(\
428
		"cdq			\n\t"\
429
		"xorl %1, %0		\n\t"\
430
		"subl %1, %0		\n\t"\
431
		: "+a" (level), "=&d" (mask)\
432
	    );
433
#else
434
#define MASK_ABS(mask, level)\
435
            mask= level>>31;\
436
            level= (level^mask)-mask;
437
#endif
438

  
439

  
440
#if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
441
#define COPY3_IF_LT(x,y,a,b,c,d)\
442
asm volatile (\
443
    "cmpl %0, %3	\n\t"\
444
    "cmovl %3, %0	\n\t"\
445
    "cmovl %4, %1	\n\t"\
446
    "cmovl %5, %2	\n\t"\
447
    : "+r" (x), "+r" (a), "+r" (c)\
448
    : "r" (y), "r" (b), "r" (d)\
449
);
450
#else
451
#define COPY3_IF_LT(x,y,a,b,c,d)\
452
if((y)<(x)){\
453
     (x)=(y);\
454
     (a)=(b);\
455
     (c)=(d);\
456
}
457
#endif
458

  
459
#if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(ARCH_POWERPC)
460
#if defined(ARCH_X86_64)
461
static inline uint64_t read_time(void)
462
{
463
	uint64_t a, d;
464
	asm volatile(	"rdtsc\n\t"
465
		: "=a" (a), "=d" (d)
466
	);
467
	return (d << 32) | (a & 0xffffffff);
468
}
469
#elif defined(ARCH_X86)
470
static inline long long read_time(void)
471
{
472
	long long l;
473
	asm volatile(	"rdtsc\n\t"
474
		: "=A" (l)
475
	);
476
	return l;
477
}
478
#else //FIXME check ppc64
479
static inline uint64_t read_time(void)
480
{
481
    uint32_t tbu, tbl, temp;
482

  
483
     /* from section 2.2.1 of the 32-bit PowerPC PEM */
484
     __asm__ __volatile__(
485
         "1:\n"
486
         "mftbu  %2\n"
487
         "mftb   %0\n"
488
         "mftbu  %1\n"
489
         "cmpw   %2,%1\n"
490
         "bne    1b\n"
491
     : "=r"(tbl), "=r"(tbu), "=r"(temp)
492
     :
493
     : "cc");
494

  
495
     return (((uint64_t)tbu)<<32) | (uint64_t)tbl;
496
}
497
#endif
498

  
499
#define START_TIMER \
500
uint64_t tend;\
501
uint64_t tstart= read_time();\
502

  
503
#define STOP_TIMER(id) \
504
tend= read_time();\
505
{\
506
  static uint64_t tsum=0;\
507
  static int tcount=0;\
508
  static int tskip_count=0;\
509
  if(tcount<2 || tend - tstart < 8*tsum/tcount){\
510
      tsum+= tend - tstart;\
511
      tcount++;\
512
  }else\
513
      tskip_count++;\
514
  if(256*256*256*64%(tcount+tskip_count)==0){\
515
      av_log(NULL, AV_LOG_DEBUG, "%Ld dezicycles in %s, %d runs, %d skips\n", tsum*10/tcount, id, tcount, tskip_count);\
516
  }\
517
}
518
#else
519
#define START_TIMER 
520
#define STOP_TIMER(id) {}
521
#endif
522

  
523
/* avoid usage of various functions */
524
#define malloc please_use_av_malloc
525
#define free please_use_av_free
526
#define realloc please_use_av_realloc
527
#define time time_is_forbidden_due_to_security_issues
528
#define rand rand_is_forbidden_due_to_state_trashing
529
#define srand srand_is_forbidden_due_to_state_trashing
530
#define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
531
#define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
532
#if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
533
#define printf please_use_av_log
534
#define fprintf please_use_av_log
535
#endif
536

  
537
#define CHECKED_ALLOCZ(p, size)\
538
{\
539
    p= av_mallocz(size);\
540
    if(p==NULL && (size)!=0){\
541
        perror("malloc");\
542
        goto fail;\
543
    }\
544
}
545

  
546
#endif /* HAVE_AV_CONFIG_H */
547

  
548
#endif /* COMMON_H */
libavcodec/i386/fdct_mmx.c
13 13
 * a page about fdct at http://www.geocities.com/ssavekar/dct.htm
14 14
 * Skal's fdct at http://skal.planet-d.net/coding/dct.html
15 15
 */
16
#include "../common.h"
16
#include "common.h"
17 17
#include "../dsputil.h"
18 18
#include "mmx.h"
19 19

  
libavcodec/i386/idct_mmx.c
22 22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 23
 */
24 24

  
25
#include "../common.h"
25
#include "common.h"
26 26
#include "../dsputil.h"
27 27

  
28 28
#include "mmx.h"
libavcodec/integer.c
1
/*
2
 * arbitrary precision integers
3
 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 */
20
 
21
/**
22
 * @file integer.c
23
 * arbitrary precision integers.
24
 * @author Michael Niedermayer <michaelni@gmx.at>
25
 */
26

  
27
#include "common.h"
28
#include "integer.h"
29

  
30
AVInteger av_add_i(AVInteger a, AVInteger b){
31
    int i, carry=0;
32
    
33
    for(i=0; i<AV_INTEGER_SIZE; i++){
34
        carry= (carry>>16) + a.v[i] + b.v[i];
35
        a.v[i]= carry;
36
    }
37
    return a;
38
}
39

  
40
AVInteger av_sub_i(AVInteger a, AVInteger b){
41
    int i, carry=0;
42
    
43
    for(i=0; i<AV_INTEGER_SIZE; i++){
44
        carry= (carry>>16) + a.v[i] - b.v[i];
45
        a.v[i]= carry;
46
    }
47
    return a;
48
}
49

  
50
/**
51
 * returns the rounded down value of the logarithm of base 2 of the given AVInteger.
52
 * this is simply the index of the most significant bit which is 1. Or 0 of all bits are 0
53
 */
54
int av_log2_i(AVInteger a){
55
    int i;
56

  
57
    for(i=AV_INTEGER_SIZE-1; i>=0; i--){
58
        if(a.v[i])
59
            return av_log2_16bit(a.v[i]) + 16*i;
60
    }
61
    return -1;
62
}
63

  
64
AVInteger av_mul_i(AVInteger a, AVInteger b){
65
    AVInteger out;
66
    int i, j;
67
    int na= (av_log2_i(a)+16) >> 4;
68
    int nb= (av_log2_i(b)+16) >> 4;
69
    
70
    memset(&out, 0, sizeof(out));
71
    
72
    for(i=0; i<na; i++){
73
        unsigned int carry=0;
74
        
75
        if(a.v[i])
76
            for(j=i; j<AV_INTEGER_SIZE && j-i<=nb; j++){
77
                carry= (carry>>16) + out.v[j] + a.v[i]*b.v[j-i];
78
                out.v[j]= carry;
79
            }
80
    }
81

  
82
    return out;
83
}
84

  
85
/**
86
 * returns 0 if a==b, 1 if a>b and -1 if a<b.
87
 */
88
int av_cmp_i(AVInteger a, AVInteger b){
89
    int i; 
90
    int v= (int16_t)a.v[AV_INTEGER_SIZE-1] - (int16_t)b.v[AV_INTEGER_SIZE-1];
91
    if(v) return (v>>16)|1;
92
    
93
    for(i=AV_INTEGER_SIZE-2; i>=0; i--){
94
        int v= a.v[i] - b.v[i];
95
        if(v) return (v>>16)|1;
96
    }
97
    return 0;
98
}
99

  
100
/**
101
 * bitwise shift.
102
 * @param s the number of bits by which the value should be shifted right, may be negative for shifting left
103
 */
104
AVInteger av_shr_i(AVInteger a, int s){
105
    AVInteger out;
106
    int i;
107

  
108
    for(i=0; i<AV_INTEGER_SIZE; i++){
109
        int index= i + (s>>4);
110
        unsigned int v=0;
111
        if(index+1<AV_INTEGER_SIZE && index+1>=0) v = a.v[index+1]<<16;
112
        if(index  <AV_INTEGER_SIZE && index  >=0) v+= a.v[index  ];
113
        out.v[i]= v >> (s&15);
114
    }
115
    return out;
116
}
117

  
118
/**
119
 * returns a % b.
120
 * @param quot a/b will be stored here
121
 */
122
AVInteger av_mod_i(AVInteger *quot, AVInteger a, AVInteger b){
123
    int i= av_log2_i(a) - av_log2_i(b);
124
    AVInteger quot_temp;
125
    if(!quot) quot = &quot_temp;
126
    
127
    assert((int16_t)a[AV_INTEGER_SIZE-1] >= 0 && (int16_t)b[AV_INTEGER_SIZE-1] >= 0);
128
    assert(av_log2(b)>=0);
129
    
130
    if(i > 0)
131
        b= av_shr_i(b, -i);
132
        
133
    memset(quot, 0, sizeof(AVInteger));
134

  
135
    while(i-- >= 0){
136
        *quot= av_shr_i(*quot, -1);
137
        if(av_cmp_i(a, b) >= 0){
138
            a= av_sub_i(a, b);
139
            quot->v[0] += 1;
140
        }
141
        b= av_shr_i(b, 1);
142
    }
143
    return a;
144
}
145

  
146
/**
147
 * returns a/b.
148
 */
149
AVInteger av_div_i(AVInteger a, AVInteger b){
150
    AVInteger quot;
151
    av_mod_i(&quot, a, b);
152
    return quot;
153
}
154

  
155
/**
156
 * converts the given int64_t to an AVInteger.
157
 */
158
AVInteger av_int2i(int64_t a){
159
    AVInteger out;
160
    int i;
161
    
162
    for(i=0; i<AV_INTEGER_SIZE; i++){
163
        out.v[i]= a;
164
        a>>=16;
165
    }
166
    return out;
167
}
168

  
169
/**
170
 * converts the given AVInteger to an int64_t.
171
 * if the AVInteger is too large to fit into an int64_t, 
172
 * then only the least significant 64bit will be used
173
 */
174
int64_t av_i2int(AVInteger a){
175
    int i;
176
    int64_t out=(int8_t)a.v[AV_INTEGER_SIZE-1];
177
    
178
    for(i= AV_INTEGER_SIZE-2; i>=0; i--){
179
        out = (out<<16) + a.v[i];
180
    }
181
    return out;
182
}
183

  
184
#if 0
185
#undef NDEBUG
186
#include <assert.h>
187

  
188
const uint8_t ff_log2_tab[256]={
189
        0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
190
        5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
191
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
192
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
193
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
194
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
195
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
196
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
197
};
198

  
199
main(){
200
    int64_t a,b;
201

  
202
    for(a=7; a<256*256*256; a+=13215){
203
        for(b=3; b<256*256*256; b+=27118){
204
            AVInteger ai= av_int2i(a);
205
            AVInteger bi= av_int2i(b);
206
            
207
            assert(av_i2int(ai) == a);
208
            assert(av_i2int(bi) == b);
209
            assert(av_i2int(av_add_i(ai,bi)) == a+b);
210
            assert(av_i2int(av_sub_i(ai,bi)) == a-b);
211
            assert(av_i2int(av_mul_i(ai,bi)) == a*b);
212
            assert(av_i2int(av_shr_i(ai, 9)) == a>>9);
213
            assert(av_i2int(av_shr_i(ai,-9)) == a<<9);
214
            assert(av_i2int(av_shr_i(ai, 17)) == a>>17);
215
            assert(av_i2int(av_shr_i(ai,-17)) == a<<17);
216
            assert(av_log2_i(ai) == av_log2(a));
217
            assert(av_i2int(av_div_i(ai,bi)) == a/b);
218
        }
219
    }
220
}
221
#endif
libavcodec/integer.h
1
/*
2
 * arbitrary precision integers
3
 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 */
20
 
21
/**
22
 * @file integer.h
23
 * arbitrary precision integers
24
 * @author Michael Niedermayer <michaelni@gmx.at>
25
 */
26
 
27
#ifndef INTEGER_H
28
#define INTEGER_H
29

  
30
#define AV_INTEGER_SIZE 8
31

  
32
typedef struct AVInteger{
33
    uint16_t v[AV_INTEGER_SIZE]; 
34
} AVInteger;
35

  
36
AVInteger av_add_i(AVInteger a, AVInteger b);
37
AVInteger av_sub_i(AVInteger a, AVInteger b);
38
int av_log2_i(AVInteger a);
39
AVInteger av_mul_i(AVInteger a, AVInteger b);
40
int av_cmp_i(AVInteger a, AVInteger b);
41
AVInteger av_shr_i(AVInteger a, int s);
42
AVInteger av_mod_i(AVInteger *quot, AVInteger a, AVInteger b);
43
AVInteger av_div_i(AVInteger a, AVInteger b);
44
AVInteger av_int2i(int64_t a);
45
int64_t av_i2int(AVInteger a);
46

  
47
#endif // INTEGER_H
libavcodec/rational.c
1
/*
2
 * Rational numbers
3
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 */
20
 
21
/**
22
 * @file rational.c
23
 * Rational numbers
24
 * @author Michael Niedermayer <michaelni@gmx.at>
25
 */
26

  
27
//#include <math.h>
28
#include <limits.h>
29
 
30
#include "common.h"
31
#include "avcodec.h"
32
#include "rational.h"
33

  
34
/**
35
 * returns b*c.
36
 */
37
AVRational av_mul_q(AVRational b, AVRational c){
38
    av_reduce(&b.num, &b.den, b.num * (int64_t)c.num, b.den * (int64_t)c.den, INT_MAX);
39
    return b;
40
}
41

  
42
/**
43
 * returns b/c.
44
 */
45
AVRational av_div_q(AVRational b, AVRational c){
46
    av_reduce(&b.num, &b.den, b.num * (int64_t)c.den, b.den * (int64_t)c.num, INT_MAX);
47
    return b;
48
}
49

  
50
/**
51
 * returns b+c.
52
 */
53
AVRational av_add_q(AVRational b, AVRational c){
54
    av_reduce(&b.num, &b.den, b.num * (int64_t)c.den + c.num * (int64_t)b.den, b.den * (int64_t)c.den, INT_MAX);
55
    return b;
56
}
57

  
58
/**
59
 * returns b-c.
60
 */
61
AVRational av_sub_q(AVRational b, AVRational c){
62
    av_reduce(&b.num, &b.den, b.num * (int64_t)c.den - c.num * (int64_t)b.den, b.den * (int64_t)c.den, INT_MAX);
63
    return b;
64
}
65

  
66
/**
67
 * Converts a double precission floating point number to a AVRational.
68
 * @param max the maximum allowed numerator and denominator
69
 */
70
AVRational av_d2q(double d, int max){
71
    AVRational a;
72
    int exponent= FFMAX( (int)(log(ABS(d) + 1e-20)/log(2)), 0);
73
    int64_t den= 1LL << (61 - exponent);
74
    av_reduce(&a.num, &a.den, (int64_t)(d * den + 0.5), den, max);
75

  
76
    return a;
77
}
libavcodec/rational.h
1
/*
2
 * Rational numbers
3
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 *
19
 */
20
 
21
/**
22
 * @file rational.h
23
 * Rational numbers.
24
 * @author Michael Niedermayer <michaelni@gmx.at>
25
 */
26

  
27
#ifndef RATIONAL_H
28
#define RATIONAL_H
29

  
30
/**
31
 * Rational number num/den.
32
 */
33
typedef struct AVRational{
34
    int num; ///< numerator
35
    int den; ///< denominator
36
} AVRational;
37

  
38
/**
39
 * returns 0 if a==b, 1 if a>b and -1 if a<b.
40
 */
41
static inline int av_cmp_q(AVRational a, AVRational b){
42
    const int64_t tmp= a.num * (int64_t)b.den - b.num * (int64_t)a.den;
43

  
44
    if(tmp) return (tmp>>63)|1;
45
    else    return 0;
46
}
47

  
48
/**
49
 * converts the given AVRational to a double.
50
 */
51
static inline double av_q2d(AVRational a){
52
    return a.num / (double) a.den;
53
}
54

  
55
AVRational av_mul_q(AVRational b, AVRational c);
56
AVRational av_div_q(AVRational b, AVRational c);
57
AVRational av_add_q(AVRational b, AVRational c);
58
AVRational av_sub_q(AVRational b, AVRational c);
59
AVRational av_d2q(double d, int max);
60

  
61
#endif // RATIONAL_H
libavcodec/utils.c
31 31
#include <stdarg.h>
32 32
#include <limits.h>
33 33

  
34
const uint8_t ff_sqrt_tab[128]={
35
        0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5,
36
        5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
37
        8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
38
        9, 9, 9, 9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11
39
};
40

  
41
const uint8_t ff_log2_tab[256]={
42
        0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
43
        5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
44
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
45
        6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
46
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
47
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
48
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
49
        7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
50
};
51

  
52 34
const uint8_t ff_reverse[256]={
53 35
0x00,0x80,0x40,0xC0,0x20,0xA0,0x60,0xE0,0x10,0x90,0x50,0xD0,0x30,0xB0,0x70,0xF0,
54 36
0x08,0x88,0x48,0xC8,0x28,0xA8,0x68,0xE8,0x18,0x98,0x58,0xD8,0x38,0xB8,0x78,0xF8,
......
938 920
    }
939 921
}
940 922

  
941
int av_reduce(int *dst_nom, int *dst_den, int64_t nom, int64_t den, int64_t max){
942
    AVRational a0={0,1}, a1={1,0};
943
    int sign= (nom<0) ^ (den<0);
944
    int64_t gcd= ff_gcd(ABS(nom), ABS(den));
945

  
946
    nom = ABS(nom)/gcd;
947
    den = ABS(den)/gcd;
948
    if(nom<=max && den<=max){
949
        a1= (AVRational){nom, den};
950
        den=0;
951
    }
952
    
953
    while(den){
954
        int64_t x       = nom / den;
955
        int64_t next_den= nom - den*x;
956
        int64_t a2n= x*a1.num + a0.num;
957
        int64_t a2d= x*a1.den + a0.den;
958

  
959
        if(a2n > max || a2d > max) break;
960

  
961
        a0= a1;
962
        a1= (AVRational){a2n, a2d};
963
        nom= den;
964
        den= next_den;
965
    }
966
    assert(ff_gcd(a1.num, a1.den) == 1);
967
    
968
    *dst_nom = sign ? -a1.num : a1.num;
969
    *dst_den = a1.den;
970
    
971
    return den==0;
972
}
973

  
974
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd){
975
    AVInteger ai;
976
    int64_t r=0;
977
    assert(c > 0);
978
    assert(b >=0);
979
    assert(rnd >=0 && rnd<=5 && rnd!=4);
980
    
981
    if(a<0 && a != INT64_MIN) return -av_rescale_rnd(-a, b, c, rnd ^ ((rnd>>1)&1)); 
982
    
983
    if(rnd==AV_ROUND_NEAR_INF) r= c/2;
984
    else if(rnd&1)             r= c-1;
985

  
986
    if(b<=INT_MAX && c<=INT_MAX){
987
        if(a<=INT_MAX)
988
            return (a * b + r)/c;
989
        else
990
            return a/c*b + (a%c*b + r)/c;
991
    }
992
    
993
    ai= av_mul_i(av_int2i(a), av_int2i(b));
994
    ai= av_add_i(ai, av_int2i(r));
995
    
996
    return av_i2int(av_div_i(ai, av_int2i(c)));
997
}
998

  
999
int64_t av_rescale(int64_t a, int64_t b, int64_t c){
1000
    return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
1001
}
1002

  
1003
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq){
1004
    int64_t b= bq.num * (int64_t)cq.den;
1005
    int64_t c= cq.num * (int64_t)bq.den;
1006
    return av_rescale_rnd(a, b, c, AV_ROUND_NEAR_INF);
1007
}
1008

  
1009
int64_t ff_gcd(int64_t a, int64_t b){
1010
    if(b) return ff_gcd(b, a%b);
1011
    else  return a;
1012
}
1013

  
1014
double av_int2dbl(int64_t v){
1015
    if(v+v > 0xFFELLU<<52)
1016
        return 0.0/0.0;
1017
    return ldexp(((v&(1LL<<52)-1) + (1LL<<52)) * (v>>63|1), (v>>52&0x7FF)-1075);
1018
}
1019

  
1020
float av_int2flt(int32_t v){
1021
    if(v+v > 0xFF000000U)
1022
        return 0.0/0.0;
1023
    return ldexp(((v&0x7FFFFF) + (1<<23)) * (v>>31|1), (v>>23&0xFF)-150);
1024
}
1025

  
1026
int64_t av_dbl2int(double d){
1027
    int e;
1028
    if     ( !d) return 0;
1029
    else if(d-d) return 0x7FF0000000000000LL + ((int64_t)(d<0)<<63) + (d!=d);
1030
    d= frexp(d, &e);
1031
    return (int64_t)(d<0)<<63 | (e+1022LL)<<52 | (int64_t)((fabs(d)-0.5)*(1LL<<53));
1032
}
1033

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff