]> git.sesse.net Git - ffmpeg/blobdiff - libpostproc/postprocess.c
Document ff_des_encdec
[ffmpeg] / libpostproc / postprocess.c
index 648d3a95f1d11ad6f935a4d5fc44ccaa4d574860..ee09a542460465bf76c669a31353859ec27f94df 100644 (file)
@@ -1,22 +1,24 @@
 /*
-    Copyright (C) 2001-2003 Michael Niedermayer (michaelni@gmx.at)
-
-    AltiVec optimizations (C) 2004 Romain Dolbeau <romain@dolbeau.org>
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-*/
+ * Copyright (C) 2001-2003 Michael Niedermayer (michaelni@gmx.at)
+ *
+ * AltiVec optimizations (C) 2004 Romain Dolbeau <romain@dolbeau.org>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
 
 /**
  * @file postprocess.c
@@ -45,8 +47,8 @@ LinBlendDeinterlace     e               E       E*
 MedianDeinterlace#      E       Ec      Ec
 TempDeNoiser#           E               e       e       Ec
 
-* i dont have a 3dnow CPU -> its untested, but noone said it doesnt work so it seems to work
-# more or less selfinvented filters so the exactness isnt too meaningfull
+* i do not have a 3DNow! CPU -> it is untested, but no one said it does not work so it seems to work
+# more or less selfinvented filters so the exactness is not too meaningful
 E = Exact implementation
 e = allmost exact implementation (slightly different rounding,...)
 a = alternative / approximate impl
@@ -85,53 +87,33 @@ try to unroll inner for(x=0 ... loop to avoid these damn if(x ... checks
 //#undef HAVE_MMX
 //#undef ARCH_X86
 //#define DEBUG_BRIGHTNESS
-#ifdef USE_FASTMEMCPY
-#include "libvo/fastmemcpy.h"
-#endif
 #include "postprocess.h"
 #include "postprocess_internal.h"
 
-#include "mangle.h" //FIXME should be supressed
-
 #ifdef HAVE_ALTIVEC_H
 #include <altivec.h>
 #endif
 
-#define MIN(a,b) ((a) > (b) ? (b) : (a))
-#define MAX(a,b) ((a) < (b) ? (b) : (a))
-#define ABS(a) ((a) > 0 ? (a) : (-(a)))
-#define SIGN(a) ((a) > 0 ? 1 : -1)
-
 #define GET_MODE_BUFFER_SIZE 500
 #define OPTIONS_ARRAY_SIZE 10
 #define BLOCK_SIZE 8
 #define TEMP_STRIDE 8
 //#define NUM_BLOCKS_AT_ONCE 16 //not used yet
 
-#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
-#    define attribute_used __attribute__((used))
-#    define always_inline __attribute__((always_inline)) inline
-#else
-#    define attribute_used
-#    define always_inline inline
-#endif
-
-#if defined(ARCH_X86) || defined(ARCH_X86_64)
-static uint64_t __attribute__((aligned(8))) attribute_used w05= 0x0005000500050005LL;
-static uint64_t __attribute__((aligned(8))) attribute_used w04= 0x0004000400040004LL;
-static uint64_t __attribute__((aligned(8))) attribute_used w20= 0x0020002000200020LL;
-static uint64_t __attribute__((aligned(8))) attribute_used b00= 0x0000000000000000LL;
-static uint64_t __attribute__((aligned(8))) attribute_used b01= 0x0101010101010101LL;
-static uint64_t __attribute__((aligned(8))) attribute_used b02= 0x0202020202020202LL;
-static uint64_t __attribute__((aligned(8))) attribute_used b08= 0x0808080808080808LL;
-static uint64_t __attribute__((aligned(8))) attribute_used b80= 0x8080808080808080LL;
+#if defined(ARCH_X86)
+static DECLARE_ALIGNED(8, uint64_t attribute_used, w05)= 0x0005000500050005LL;
+static DECLARE_ALIGNED(8, uint64_t attribute_used, w04)= 0x0004000400040004LL;
+static DECLARE_ALIGNED(8, uint64_t attribute_used, w20)= 0x0020002000200020LL;
+static DECLARE_ALIGNED(8, uint64_t attribute_used, b00)= 0x0000000000000000LL;
+static DECLARE_ALIGNED(8, uint64_t attribute_used, b01)= 0x0101010101010101LL;
+static DECLARE_ALIGNED(8, uint64_t attribute_used, b02)= 0x0202020202020202LL;
+static DECLARE_ALIGNED(8, uint64_t attribute_used, b08)= 0x0808080808080808LL;
+static DECLARE_ALIGNED(8, uint64_t attribute_used, b80)= 0x8080808080808080LL;
 #endif
 
 static uint8_t clip_table[3*256];
 static uint8_t * const clip_tab= clip_table + 256;
 
-static const int verbose= 0;
-
 static const int attribute_used deringThreshold= 20;
 
 
@@ -169,7 +151,7 @@ static const char *replaceTable[]=
 };
 
 
-#if defined(ARCH_X86) || defined(ARCH_X86_64)
+#if defined(ARCH_X86)
 static inline void prefetchnta(void *p)
 {
         asm volatile(   "prefetchnta (%0)\n\t"
@@ -343,17 +325,17 @@ static inline void doHorizDefFilter_C(uint8_t dst[], int stride, PPContext *c)
         {
                 const int middleEnergy= 5*(dst[4] - dst[3]) + 2*(dst[2] - dst[5]);
 
-                if(ABS(middleEnergy) < 8*c->QP)
+                if(FFABS(middleEnergy) < 8*c->QP)
                 {
                         const int q=(dst[3] - dst[4])/2;
                         const int leftEnergy=  5*(dst[2] - dst[1]) + 2*(dst[0] - dst[3]);
                         const int rightEnergy= 5*(dst[6] - dst[5]) + 2*(dst[4] - dst[7]);
 
-                        int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
-                        d= MAX(d, 0);
+                        int d= FFABS(middleEnergy) - FFMIN( FFABS(leftEnergy), FFABS(rightEnergy) );
+                        d= FFMAX(d, 0);
 
                         d= (5*d + 32) >> 6;
-                        d*= SIGN(-middleEnergy);
+                        d*= FFSIGN(-middleEnergy);
 
                         if(q>0)
                         {
@@ -382,8 +364,8 @@ static inline void doHorizLowPass_C(uint8_t dst[], int stride, PPContext *c)
         int y;
         for(y=0; y<BLOCK_SIZE; y++)
         {
-                const int first= ABS(dst[-1] - dst[0]) < c->QP ? dst[-1] : dst[0];
-                const int last= ABS(dst[8] - dst[7]) < c->QP ? dst[8] : dst[7];
+                const int first= FFABS(dst[-1] - dst[0]) < c->QP ? dst[-1] : dst[0];
+                const int last= FFABS(dst[8] - dst[7]) < c->QP ? dst[8] : dst[7];
 
                 int sums[10];
                 sums[0] = 4*first + dst[0] + dst[1] + dst[2] + 4;
@@ -414,8 +396,8 @@ static inline void doHorizLowPass_C(uint8_t dst[], int stride, PPContext *c)
  * Experimental Filter 1 (Horizontal)
  * will not damage linear gradients
  * Flat blocks should look like they where passed through the (1,1,2,2,4,2,2,1,1) 9-Tap filter
- * can only smooth blocks at the expected locations (it cant smooth them if they did move)
- * MMX2 version does correct clipping C version doesnt
+ * can only smooth blocks at the expected locations (it cannot smooth them if they did move)
+ * MMX2 version does correct clipping C version does not
  * not identical with the vertical one
  */
 static inline void horizX1Filter(uint8_t *src, int stride, int QP)
@@ -458,11 +440,11 @@ static inline void horizX1Filter(uint8_t *src, int stride, int QP)
                 int b= src[3] - src[4];
                 int c= src[5] - src[6];
 
-                int d= MAX(ABS(b) - (ABS(a) + ABS(c))/2, 0);
+                int d= FFMAX(FFABS(b) - (FFABS(a) + FFABS(c))/2, 0);
 
                 if(d < QP)
                 {
-                        int v = d * SIGN(-b);
+                        int v = d * FFSIGN(-b);
 
                         src[1] +=v/8;
                         src[2] +=v/4;
@@ -479,7 +461,7 @@ static inline void horizX1Filter(uint8_t *src, int stride, int QP)
 /**
  * accurate deblock filter
  */
-static always_inline void do_a_deblock_C(uint8_t *src, int step, int stride, PPContext *c){
+static av_always_inline void do_a_deblock_C(uint8_t *src, int step, int stride, PPContext *c){
         int y;
         const int QP= c->QP;
         const int dcOffset= ((c->nonBQP*c->ppMode.baseDcDiff)>>8) + 1;
@@ -518,8 +500,8 @@ static always_inline void do_a_deblock_C(uint8_t *src, int step, int stride, PPC
                                 }
                         }
                         if(max-min < 2*QP){
-                                const int first= ABS(src[-1*step] - src[0]) < QP ? src[-1*step] : src[0];
-                                const int last= ABS(src[8*step] - src[7*step]) < QP ? src[8*step] : src[7*step];
+                                const int first= FFABS(src[-1*step] - src[0]) < QP ? src[-1*step] : src[0];
+                                const int last= FFABS(src[8*step] - src[7*step]) < QP ? src[8*step] : src[7*step];
 
                                 int sums[10];
                                 sums[0] = 4*first + src[0*step] + src[1*step] + src[2*step] + 4;
@@ -545,17 +527,17 @@ static always_inline void do_a_deblock_C(uint8_t *src, int step, int stride, PPC
                 }else{
                         const int middleEnergy= 5*(src[4*step] - src[3*step]) + 2*(src[2*step] - src[5*step]);
 
-                        if(ABS(middleEnergy) < 8*QP)
+                        if(FFABS(middleEnergy) < 8*QP)
                         {
                                 const int q=(src[3*step] - src[4*step])/2;
                                 const int leftEnergy=  5*(src[2*step] - src[1*step]) + 2*(src[0*step] - src[3*step]);
                                 const int rightEnergy= 5*(src[6*step] - src[5*step]) + 2*(src[4*step] - src[7*step]);
 
-                                int d= ABS(middleEnergy) - MIN( ABS(leftEnergy), ABS(rightEnergy) );
-                                d= MAX(d, 0);
+                                int d= FFABS(middleEnergy) - FFMIN( FFABS(leftEnergy), FFABS(rightEnergy) );
+                                d= FFMAX(d, 0);
 
                                 d= (5*d + 32) >> 6;
-                                d*= SIGN(-middleEnergy);
+                                d*= FFSIGN(-middleEnergy);
 
                                 if(q>0)
                                 {
@@ -594,7 +576,7 @@ static always_inline void do_a_deblock_C(uint8_t *src, int step, int stride, PPC
 #endif //HAVE_ALTIVEC
 #endif //ARCH_POWERPC
 
-#if defined(ARCH_X86) || defined(ARCH_X86_64)
+#if defined(ARCH_X86)
 
 #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
 #define COMPILE_MMX
@@ -607,7 +589,7 @@ static always_inline void do_a_deblock_C(uint8_t *src, int step, int stride, PPC
 #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
 #define COMPILE_3DNOW
 #endif
-#endif //ARCH_X86
+#endif /* defined(ARCH_X86) */
 
 #undef HAVE_MMX
 #undef HAVE_MMX2
@@ -662,7 +644,7 @@ static always_inline void do_a_deblock_C(uint8_t *src, int step, int stride, PPC
 #include "postprocess_template.c"
 #endif
 
-// minor note: the HAVE_xyz is messed up after that line so dont use it
+// minor note: the HAVE_xyz is messed up after that line so do not use it.
 
 static inline void postProcess(uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
         QP_STORE_T QPs[], int QPStride, int isColor, pp_mode_t *vm, pp_context_t *vc)
@@ -671,11 +653,11 @@ static inline void postProcess(uint8_t src[], int srcStride, uint8_t dst[], int
         PPMode *ppMode= (PPMode *)vm;
         c->ppMode= *ppMode; //FIXME
 
-        // useing ifs here as they are faster than function pointers allthough the
-        // difference wouldnt be messureable here but its much better because
-        // someone might exchange the cpu whithout restarting mplayer ;)
+        // Using ifs here as they are faster than function pointers although the
+        // difference would not be measureable here but it is much better because
+        // someone might exchange the CPU whithout restarting MPlayer ;)
 #ifdef RUNTIME_CPUDETECT
-#if defined(ARCH_X86) || defined(ARCH_X86_64)
+#if defined(ARCH_X86)
         // ordered per speed fasterst first
         if(c->cpuCaps & PP_CPU_CAPS_MMX2)
                 postProcess_MMX2(src, srcStride, dst, dstStride, width, height, QPs, QPStride, isColor, c);
@@ -784,7 +766,7 @@ pp_mode_t *pp_get_mode_by_name_and_quality(char *name, int quality)
 
         strncpy(temp, name, GET_MODE_BUFFER_SIZE);
 
-        if(verbose>1) printf("pp: %s\n", name);
+        av_log(NULL, AV_LOG_DEBUG, "pp: %s\n", name);
 
         for(;;){
                 char *filterName;
@@ -802,7 +784,7 @@ pp_mode_t *pp_get_mode_by_name_and_quality(char *name, int quality)
                 if(filterToken == NULL) break;
                 p+= strlen(filterToken) + 1; // p points to next filterToken
                 filterName= strtok(filterToken, optionDelimiters);
-                if(verbose>1) printf("pp: %s::%s\n", filterToken, filterName);
+                av_log(NULL, AV_LOG_DEBUG, "pp: %s::%s\n", filterToken, filterName);
 
                 if(*filterName == '-')
                 {
@@ -814,7 +796,7 @@ pp_mode_t *pp_get_mode_by_name_and_quality(char *name, int quality)
                         option= strtok(NULL, optionDelimiters);
                         if(option == NULL) break;
 
-                        if(verbose>1) printf("pp: option: %s\n", option);
+                        av_log(NULL, AV_LOG_DEBUG, "pp: option: %s\n", option);
                         if(!strcmp("autoq", option) || !strcmp("a", option)) q= quality;
                         else if(!strcmp("nochrom", option) || !strcmp("y", option)) chrom=0;
                         else if(!strcmp("chrom", option) || !strcmp("c", option)) chrom=1;
@@ -855,7 +837,6 @@ pp_mode_t *pp_get_mode_by_name_and_quality(char *name, int quality)
 
                 for(i=0; filters[i].shortName!=NULL; i++)
                 {
-//                        printf("Compareing %s, %s, %s\n", filters[i].shortName,filters[i].longName, filterName);
                         if(   !strcmp(filters[i].longName, filterName)
                            || !strcmp(filters[i].shortName, filterName))
                         {
@@ -942,10 +923,10 @@ pp_mode_t *pp_get_mode_by_name_and_quality(char *name, int quality)
                 ppMode->error += numOfUnknownOptions;
         }
 
-        if(verbose>1) printf("pp: lumMode=%X, chromMode=%X\n", ppMode->lumMode, ppMode->chromMode);
+        av_log(NULL, AV_LOG_DEBUG, "pp: lumMode=%X, chromMode=%X\n", ppMode->lumMode, ppMode->chromMode);
         if(ppMode->error)
         {
-                fprintf(stderr, "%d errors in postprocess string \"%s\"\n", ppMode->error, name);
+                av_log(NULL, AV_LOG_ERROR, "%d errors in postprocess string \"%s\"\n", ppMode->error, name);
                 av_free(ppMode);
                 return NULL;
         }
@@ -978,7 +959,7 @@ static void reallocBuffers(PPContext *c, int width, int height, int stride, int
 
         for(i=0; i<3; i++)
         {
-                //Note:the +17*1024 is just there so i dont have to worry about r/w over te end
+                //Note: The +17*1024 is just there so i do not have to worry about r/w over the end.
                 reallocAlign((void **)&c->tempBlured[i], 8, stride*mbHeight*16 + 17*1024);
                 reallocAlign((void **)&c->tempBluredPast[i], 8, 256*((height+7)&(~7))/2 + 17*1024);//FIXME size
         }
@@ -997,6 +978,12 @@ static void global_init(void){
         memset(clip_table+512, 0, 256);
 }
 
+static const char * context_to_name(void * ptr) {
+    return "postproc";
+}
+
+static AVClass av_codec_context_class = { "Postproc", context_to_name, NULL };
+
 pp_context_t *pp_get_context(int width, int height, int cpuCaps){
         PPContext *c= av_malloc(sizeof(PPContext));
         int stride= (width+15)&(~15);    //assumed / will realloc if needed
@@ -1005,6 +992,7 @@ pp_context_t *pp_get_context(int width, int height, int cpuCaps){
         global_init();
 
         memset(c, 0, sizeof(PPContext));
+        c->av_class = &av_codec_context_class;
         c->cpuCaps= cpuCaps;
         if(cpuCaps&PP_FORMAT){
                 c->hChromaSubSample= cpuCaps&0x3;
@@ -1052,14 +1040,14 @@ void  pp_postprocess(uint8_t * src[3], int srcStride[3],
         int mbHeight= (height+15)>>4;
         PPMode *mode = (PPMode*)vm;
         PPContext *c = (PPContext*)vc;
-        int minStride= MAX(ABS(srcStride[0]), ABS(dstStride[0]));
-        int absQPStride = ABS(QPStride);
+        int minStride= FFMAX(FFABS(srcStride[0]), FFABS(dstStride[0]));
+        int absQPStride = FFABS(QPStride);
 
         // c->stride and c->QPStride are always positive
         if(c->stride < minStride || c->qpStride < absQPStride)
                 reallocBuffers(c, width, height,
-                                MAX(minStride, c->stride),
-                                MAX(c->qpStride, absQPStride));
+                                FFMAX(minStride, c->stride),
+                                FFMAX(c->qpStride, absQPStride));
 
         if(QP_store==NULL || (mode->lumMode & FORCE_QUANT))
         {
@@ -1071,7 +1059,6 @@ void  pp_postprocess(uint8_t * src[3], int srcStride[3],
                 else
                         for(i=0; i<mbWidth; i++) QP_store[i]= 1;
         }
-//printf("pict_type:%d\n", pict_type);
 
         if(pict_type & PP_PICT_TYPE_QP2){
                 int i;
@@ -1090,11 +1077,11 @@ if(0){
 int x,y;
 for(y=0; y<mbHeight; y++){
         for(x=0; x<mbWidth; x++){
-                printf("%2d ", QP_store[x + y*QPStride]);
+                av_log(c, AV_LOG_INFO, "%2d ", QP_store[x + y*QPStride]);
         }
-        printf("\n");
+        av_log(c, AV_LOG_INFO, "\n");
 }
-        printf("\n");
+        av_log(c, AV_LOG_INFO, "\n");
 }
 
         if((pict_type&7)!=3)
@@ -1118,10 +1105,8 @@ for(y=0; y<mbHeight; y++){
                 }
         }
 
-        if(verbose>2)
-        {
-                printf("using npp filters 0x%X/0x%X\n", mode->lumMode, mode->chromMode);
-        }
+        av_log(c, AV_LOG_DEBUG, "using npp filters 0x%X/0x%X\n",
+               mode->lumMode, mode->chromMode);
 
         postProcess(src[0], srcStride[0], dst[0], dstStride[0],
                 width, height, QP_store, QPStride, 0, mode, c);