]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/common.h
fixed some bugs in app parser - some jfif and adobe jpgs fixed
[ffmpeg] / libavcodec / common.h
index 35c04a875858e51a6c4151a9f7e8117a28078465..1f9f8a2d5723fd423eee715edfec970382a79e9d 100644 (file)
@@ -5,7 +5,7 @@
 #define FFMPEG_VERSION     "0.4.6"
 
 #if defined(WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
-#define CONFIG_WIN32
+#    define CONFIG_WIN32
 #endif
 
 //#define ALT_BITSTREAM_WRITER
 //#define LIBMPEG2_BITSTREAM_READER
 //#define A32_BITSTREAM_READER
 
-#ifdef ARCH_ALPHA
-#define ALT_BITSTREAM_READER
-#endif
-
-//#define ALIGNED_BITSTREAM
-#define FAST_GET_FIRST_VLC
-//#define DUMP_STREAM
-
 #ifdef HAVE_AV_CONFIG_H
 /* only include the following when compiling package */
-#include "config.h"
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <errno.h>
-#include <math.h>
-
-#ifndef ENODATA
-#define ENODATA  61
-#endif
+#    include "config.h"
+
+#    include <stdlib.h>
+#    include <stdio.h>
+#    include <string.h>
+#    ifndef __BEOS__
+#        include <errno.h>
+#    else
+#        include "berrno.h"
+#    endif
+#    include <math.h>
+
+#    ifndef ENODATA
+#        define ENODATA  61
+#    endif
 
 #endif /* HAVE_AV_CONFIG_H */
 
 /* Suppress restrict if it was not defined in config.h.  */
 #ifndef restrict
-#define restrict
+#    define restrict
+#endif
+
+#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
+#    define always_inline __attribute__((always_inline)) inline
+#else
+#    define always_inline inline
 #endif
 
 #ifdef CONFIG_WIN32
@@ -66,37 +68,37 @@ typedef INT32 int32_t;
 typedef UINT64 uint64_t;
 typedef INT64 int64_t;
 
-#ifndef __MINGW32__
-#define INT64_C(c)     (c ## i64)
-#define UINT64_C(c)    (c ## i64)
+#    ifndef __MINGW32__
+#        define INT64_C(c)     (c ## i64)
+#        define UINT64_C(c)    (c ## i64)
 
-#define inline __inline
+#        define inline __inline
 
-#else
-#define INT64_C(c)     (c ## LL)
-#define UINT64_C(c)    (c ## ULL)
-#endif /* __MINGW32__ */
+#    else
+#        define INT64_C(c)     (c ## LL)
+#        define UINT64_C(c)    (c ## ULL)
+#    endif /* __MINGW32__ */
 
-#define M_PI    3.14159265358979323846
-#define M_SQRT2 1.41421356237309504880  /* sqrt(2) */
+#    define M_PI    3.14159265358979323846
+#    define M_SQRT2 1.41421356237309504880  /* sqrt(2) */
 
-#ifdef _DEBUG
-#define DEBUG
-#endif
+#    ifdef _DEBUG
+#        define DEBUG
+#    endif
 
-#define snprintf _snprintf
+#    define snprintf _snprintf
 
 #else /* CONFIG_WIN32 */
 
 /* unix */
 
-#include <inttypes.h>
+#    include <inttypes.h>
 
-#ifndef __WINE_WINDEF16_H
+#    ifndef __WINE_WINDEF16_H
 /* workaround for typedef conflict in MPlayer (wine typedefs) */
 typedef unsigned short UINT16;
 typedef signed short INT16;
-#endif
+#    endif
 
 typedef unsigned char UINT8;
 typedef unsigned int UINT32;
@@ -105,65 +107,69 @@ typedef signed char INT8;
 typedef signed int INT32;
 typedef signed long long INT64;
 
-#ifdef HAVE_AV_CONFIG_H
-
-#ifdef __FreeBSD__
-#include <sys/param.h>
-#endif
+#    ifdef HAVE_AV_CONFIG_H
 
-#ifndef INT64_C
-#define INT64_C(c)     (c ## LL)
-#define UINT64_C(c)    (c ## ULL)
-#endif
+#        ifndef INT64_C
+#            define INT64_C(c)     (c ## LL)
+#            define UINT64_C(c)    (c ## ULL)
+#        endif
 
-#ifdef USE_FASTMEMCPY
-#include "fastmemcpy.h"
-#endif
+#        ifdef USE_FASTMEMCPY
+#            include "fastmemcpy.h"
+#        endif
 
-#endif /* HAVE_AV_CONFIG_H */
+#    endif /* HAVE_AV_CONFIG_H */
 
 #endif /* !CONFIG_WIN32 */
 
 #ifdef HAVE_AV_CONFIG_H
 
-#include "bswap.h"
+#    include "bswap.h"
 
-#if defined(__MINGW32__) || defined(__CYGWIN__) || \
-    defined(__OS2__) || defined (__OpenBSD__)
-#define MANGLE(a) "_" #a
-#else
-#define MANGLE(a) #a
-#endif
+#    if defined(__MINGW32__) || defined(__CYGWIN__) || \
+        defined(__OS2__) || defined (__OpenBSD__)
+#        define MANGLE(a) "_" #a
+#    else
+#        define MANGLE(a) #a
+#    endif
 
 /* debug stuff */
 
-#ifndef DEBUG
-#define NDEBUG
-#endif
-#include <assert.h>
+#    ifndef DEBUG
+#        define NDEBUG
+#    endif
+#    include <assert.h>
 
 /* dprintf macros */
-#if defined(CONFIG_WIN32) && !defined(__MINGW32__)
+#    if defined(CONFIG_WIN32) && !defined(__MINGW32__)
 
 inline void dprintf(const char* fmt,...) {}
 
-#else
+#    else
 
-#ifdef DEBUG
-#define dprintf(fmt,args...) printf(fmt, ## args)
-#else
-#define dprintf(fmt,args...)
-#endif
+#        ifdef DEBUG
+#            define dprintf(fmt,args...) printf(fmt, ## args)
+#        else
+#            define dprintf(fmt,args...)
+#        endif
 
-#endif /* !CONFIG_WIN32 */
+#    endif /* !CONFIG_WIN32 */
 
-#define av_abort()      do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
+#    define av_abort()      do { fprintf(stderr, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
 
+//rounded divison & shift
+#define RSHIFT(a,b) ((a) > 0 ? ((a) + (1<<((b)-1)))>>(b) : ((a) + (1<<((b)-1))-1)>>(b))
 /* assume b>0 */
 #define ROUNDED_DIV(a,b) (((a)>0 ? (a) + ((b)>>1) : (a) - ((b)>>1))/(b))
 #define ABS(a) ((a) >= 0 ? (a) : (-(a)))
-#define MAX(a,b) ((a) > (b) ? (a) : (b))
-#define MIN(a,b) ((a) > (b) ? (b) : (a))
+
+#ifndef MAX
+#    define MAX(a,b) ((a) > (b) ? (a) : (b))
+#endif
+
+#ifndef MIN
+#    define MIN(a,b) ((a) > (b) ? (b) : (a))
+#endif
 
 #ifdef ARCH_X86
 // avoid +32 for shift optimization (gcc should do that ...)
@@ -182,8 +188,8 @@ static inline uint32_t NEG_USR32(uint32_t a, int8_t s){
     return a;
 }
 #else
-#define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
-#define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
+#    define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
+#    define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
 #endif
 
 /* bit output */
@@ -246,11 +252,17 @@ typedef struct VLC {
     int table_size, table_allocated;
 } VLC;
 
+typedef struct RL_VLC_ELEM {
+    int16_t level;
+    int8_t len;
+    uint8_t run;
+} RL_VLC_ELEM;
+
 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
 #ifdef ARCH_X86
-#define unaligned32(a) (*(UINT32*)(a))
+#    define unaligned32(a) (*(UINT32*)(a))
 #else
-#ifdef __GNUC__
+#    ifdef __GNUC__
 static inline uint32_t unaligned32(const void *v) {
     struct Unaligned {
        uint32_t i;
@@ -258,15 +270,15 @@ static inline uint32_t unaligned32(const void *v) {
 
     return ((const struct Unaligned *) v)->i;
 }
-#elif defined(__DECC)
+#    elif defined(__DECC)
 static inline uint32_t unaligned32(const void *v) {
     return *(const __unaligned uint32_t *) v;
 }
-#else
+#    else
 static inline uint32_t unaligned32(const void *v) {
     return *(const uint32_t *) v;
 }
-#endif
+#    endif
 #endif //!ARCH_X86
 
 #ifndef ALT_BITSTREAM_WRITER
@@ -308,8 +320,8 @@ static inline void put_bits(PutBitContext *s, int n, unsigned int value)
 #ifdef ALT_BITSTREAM_WRITER
 static inline void put_bits(PutBitContext *s, int n, unsigned int value)
 {
-#ifdef ALIGNED_BITSTREAM_WRITER
-#ifdef ARCH_X86
+#    ifdef ALIGNED_BITSTREAM_WRITER
+#        ifdef ARCH_X86
     asm volatile(
        "movl %0, %%ecx                 \n\t"
        "xorl %%eax, %%eax              \n\t"
@@ -327,7 +339,7 @@ static inline void put_bits(PutBitContext *s, int n, unsigned int value)
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value<<(-n))
        : "%eax", "%ecx"
     );
-#else
+#        else
     int index= s->index;
     uint32_t *ptr= ((uint32_t *)s->buf)+(index>>5);
     
@@ -338,9 +350,9 @@ static inline void put_bits(PutBitContext *s, int n, unsigned int value)
 //if(n>24) printf("%d %d\n", n, value);
     index+= n;
     s->index= index;
-#endif
-#else //ALIGNED_BITSTREAM_WRITER
-#ifdef ARCH_X86
+#        endif
+#    else //ALIGNED_BITSTREAM_WRITER
+#        ifdef ARCH_X86
     asm volatile(
        "movl $7, %%ecx                 \n\t"
        "andl %0, %%ecx                 \n\t"
@@ -357,7 +369,7 @@ static inline void put_bits(PutBitContext *s, int n, unsigned int value)
        : "r" (s->buf), "r" (n), "0" (s->index), "1" (value)
        : "%ecx"
     );
-#else
+#        else
     int index= s->index;
     uint32_t *ptr= (uint32_t*)(((uint8_t *)s->buf)+(index>>3));
     
@@ -366,8 +378,8 @@ static inline void put_bits(PutBitContext *s, int n, unsigned int value)
 //if(n>24) printf("%d %d\n", n, value);
     index+= n;
     s->index= index;
-#endif
-#endif //!ALIGNED_BITSTREAM_WRITER
+#        endif
+#    endif //!ALIGNED_BITSTREAM_WRITER
 }
 #endif
 
@@ -732,7 +744,6 @@ int init_vlc(VLC *vlc, int nb_bits, int nb_codes,
              const void *codes, int codes_wrap, int codes_size);
 void free_vlc(VLC *vlc);
 
-//note table will be trashed (pointer increased)
 #define GET_VLC(code, name, gb, table, bits, max_depth)\
 {\
     int n, index, nb_bits;\
@@ -746,9 +757,8 @@ void free_vlc(VLC *vlc);
         UPDATE_CACHE(name, gb)\
 \
         nb_bits = -n;\
-        table += code;\
 \
-        index= SHOW_UBITS(name, gb, nb_bits);\
+        index= SHOW_UBITS(name, gb, nb_bits) + code;\
         code = table[index][0];\
         n    = table[index][1];\
         if(max_depth > 2 && n < 0){\
@@ -756,9 +766,8 @@ void free_vlc(VLC *vlc);
             UPDATE_CACHE(name, gb)\
 \
             nb_bits = -n;\
-            table += code;\
 \
-            index= SHOW_UBITS(name, gb, nb_bits);\
+            index= SHOW_UBITS(name, gb, nb_bits) + code;\
             code = table[index][0];\
             n    = table[index][1];\
         }\
@@ -766,6 +775,29 @@ void free_vlc(VLC *vlc);
     SKIP_BITS(name, gb, n)\
 }
 
+#define GET_RL_VLC(level, run, name, gb, table, bits, max_depth)\
+{\
+    int n, index, nb_bits;\
+\
+    index= SHOW_UBITS(name, gb, bits);\
+    level = table[index].level;\
+    n     = table[index].len;\
+\
+    if(max_depth > 1 && n < 0){\
+        LAST_SKIP_BITS(name, gb, bits)\
+        UPDATE_CACHE(name, gb)\
+\
+        nb_bits = -n;\
+\
+        index= SHOW_UBITS(name, gb, nb_bits) + level;\
+        level = table[index].level;\
+        n     = table[index].len;\
+    }\
+    run= table[index].run;\
+    SKIP_BITS(name, gb, n)\
+}
+
+// deprecated, dont use get_vlc for new code, use get_vlc2 instead or use GET_VLC directly
 static inline int get_vlc(GetBitContext *s, VLC *vlc)
 {
     int code;
@@ -780,6 +812,21 @@ static inline int get_vlc(GetBitContext *s, VLC *vlc)
     return code;
 }
 
+static always_inline int get_vlc2(GetBitContext *s, VLC_TYPE (*table)[2],
+                                  int bits, int max_depth)
+{
+    int code;
+    
+    OPEN_READER(re, s)
+    UPDATE_CACHE(re, s)
+
+    GET_VLC(code, re, s, table, bits, max_depth)
+
+    CLOSE_READER(re, s)
+    return code;
+}
+
+
 /* define it to include statistics code (useful only for optimizing
    codec efficiency */
 //#define STATS
@@ -862,6 +909,8 @@ static inline int clip(int a, int amin, int amax)
 }
 
 /* math */
+extern const UINT8 ff_sqrt_tab[128];
+
 int ff_gcd(int a, int b);
 
 static inline int ff_sqrt(int a)
@@ -869,7 +918,9 @@ static inline int ff_sqrt(int a)
     int ret=0;
     int s;
     int ret_sq=0;
-
+    
+    if(a<128) return ff_sqrt_tab[a];
+    
     for(s=15; s>=0; s--){
         int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
         if(b<=a){
@@ -879,6 +930,32 @@ static inline int ff_sqrt(int a)
     }
     return ret;
 }
+
+/**
+ * converts fourcc string to int
+ */
+static inline int ff_get_fourcc(char *s){
+    assert( strlen(s)==4 );
+    
+    return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
+}
+
+
+#ifdef ARCH_X86
+#define MASK_ABS(mask, level)\
+            asm volatile(\
+               "cdq                    \n\t"\
+               "xorl %1, %0            \n\t"\
+               "subl %1, %0            \n\t"\
+               : "+a" (level), "=&d" (mask)\
+           );
+#else
+#define MASK_ABS(mask, level)\
+            mask= level>>31;\
+            level= (level^mask)-mask;
+#endif
+
+
 #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
 #define COPY3_IF_LT(x,y,a,b,c,d)\
 asm volatile (\