]> git.sesse.net Git - ffmpeg/blobdiff - vhook/fish.c
fixing missaligned memory accesses in fill_rectangle()
[ffmpeg] / vhook / fish.c
index 86ae1a474bef0d02b54a7b15e4b9de9add4fd3e1..c710526b154828899419cf8a4f9080f7ae0fd3a2 100644 (file)
 #include "framehook.h"
 #include "dsputil.h"
 
-#define SCALE_BITS 10
-#define C_Y  (76309 >> (16 - SCALE_BITS))
-#define C_RV (117504 >> (16 - SCALE_BITS))
-#define C_BU (138453 >> (16 - SCALE_BITS))
-#define C_GU (13954 >> (16 - SCALE_BITS))
-#define C_GV (34903 >> (16 - SCALE_BITS))                                       
+#define SCALEBITS 10
+#define ONE_HALF  (1 << (SCALEBITS - 1))
+#define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
+
+#define YUV_TO_RGB1_CCIR(cb1, cr1)\
+{\
+    cb = (cb1) - 128;\
+    cr = (cr1) - 128;\
+    r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
+    g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
+                    ONE_HALF;\
+    b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
+}
+
+#define YUV_TO_RGB2_CCIR(r, g, b, y1)\
+{\
+    yt = ((y1) - 16) * FIX(255.0/219.0);\
+    r = cm[(yt + r_add) >> SCALEBITS];\
+    g = cm[(yt + g_add) >> SCALEBITS];\
+    b = cm[(yt + b_add) >> SCALEBITS];\
+}
 
+
   
 typedef struct {
     int h;  /* 0 .. 360 */
@@ -68,7 +84,7 @@ typedef struct {
     int file_limit;
     int debug;
     int min_interval;
-    INT64 next_pts;
+    int64_t next_pts;
     int inset;
     int min_width;
 } ContextInfo;
@@ -82,6 +98,11 @@ static void dorange(const char *s, int *first, int *second, int maxval)
         *second = maxval;
 }
 
+void Release(void *ctx)
+{
+    if (ctx)
+        av_free(ctx);
+}
 
 int Configure(void **ctxp, int argc, char *argv[])
 {
@@ -94,7 +115,7 @@ int Configure(void **ctxp, int argc, char *argv[])
     optind = 0;
 
     ci->dir = "/tmp";
-    ci->threshold = 1000;
+    ci->threshold = 100;
     ci->file_limit = 100;
     ci->min_interval = 1000000;
     ci->inset = 10;     /* Percent */
@@ -121,6 +142,10 @@ int Configure(void **ctxp, int argc, char *argv[])
                 break;
             case 't':
                 ci->threshold = atof(optarg) * 1000;
+                if (ci->threshold > 1000 || ci->threshold < 0) {
+                    fprintf(stderr, "Invalid threshold value '%s' (range is 0-1)\n", optarg);
+                    return -1;
+                }
                 break;
             case 'w':
                 ci->min_width = atoi(optarg);
@@ -129,7 +154,7 @@ int Configure(void **ctxp, int argc, char *argv[])
                 ci->debug++;
                 break;
             case 'D':
-                ci->dir = strdup(optarg);
+                ci->dir = av_strdup(optarg);
                 break;
             default:
                 fprintf(stderr, "Unrecognized argument '%s'\n", argv[optind]);
@@ -145,6 +170,8 @@ int Configure(void **ctxp, int argc, char *argv[])
                         ci->bright.h,
                         ci->bright.s,
                         ci->bright.v);
+    fprintf(stderr, "    Threshold is %d%% pixels\n", ci->threshold / 10);
+
 
     return 0;
 }
@@ -188,12 +215,17 @@ static void get_hsv(HSV *hsv, int r, int g, int b)
     return;
 }                                                                               
 
-void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, INT64 pts)
+void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
 {
     ContextInfo *ci = (ContextInfo *) ctx;
-    UINT8 *cm = cropTbl + MAX_NEG_CROP;                                         
+    uint8_t *cm = cropTbl + MAX_NEG_CROP;                                         
     int rowsize = picture->linesize[0];
 
+#if 0
+    printf("pix_fmt = %d, width = %d, pts = %lld, ci->next_pts = %lld\n",
+        pix_fmt, width, pts, ci->next_pts);
+#endif
+
     if (pts < ci->next_pts)
         return;
 
@@ -203,7 +235,7 @@ void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width,
     ci->next_pts = pts + 1000000;    
 
     if (pix_fmt == PIX_FMT_YUV420P) {
-        UINT8 *y, *u, *v;
+        uint8_t *y, *u, *v;
         int width2 = width >> 1;
         int inrange = 0;
         int pixcnt;
@@ -219,26 +251,20 @@ void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width,
 
         pixcnt = ((h_start - h_end) >> 1) * (w_start - w_end);
 
-        y = picture->data[0];
-        u = picture->data[1];
-        v = picture->data[2];
+        y = picture->data[0] + h_end * picture->linesize[0] + w_end * 2;
+        u = picture->data[1] + h_end * picture->linesize[1] / 2 + w_end;
+        v = picture->data[2] + h_end * picture->linesize[2] / 2 + w_end;
 
         for (h = h_start; h > h_end; h -= 2) {
             int w;
 
             for (w = w_start; w > w_end; w--) {
-                int r,g,b;
-                int Y, U, V;
+                unsigned int r,g,b;
                 HSV hsv;
+                int cb, cr, yt, r_add, g_add, b_add;
 
-                U = u[0] - 128;
-                V = v[0] - 128;
-
-                Y = (y[0] - 16) * C_Y;
-
-                r = cm[(Y + C_RV * V + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
-                g = cm[(Y + - C_GU * U - C_GV * V + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
-                b = cm[(Y + C_BU * U + (1 << (SCALE_BITS - 1))) >> SCALE_BITS];
+                YUV_TO_RGB1_CCIR(u[0], v[0]);
+                YUV_TO_RGB2_CCIR(r, g, b, y[0]);
 
                 get_hsv(&hsv, r, g, b);
 
@@ -252,7 +278,9 @@ void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width,
                     hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) {            
                     inrange++;
                 } else if (ci->zapping) {
-                    y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 0;
+                    y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 16;
+                    u[0] = 128;
+                    v[0] = 128;
                 }
 
                 y+= 2;
@@ -260,11 +288,14 @@ void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width,
                 v++;
             }
 
-            y += picture->linesize[0] * 2 - width;
-            u += picture->linesize[1] - width2;
-            v += picture->linesize[2] - width2;
+            y += picture->linesize[0] * 2 - (w_start - w_end) * 2;
+            u += picture->linesize[1] - (w_start - w_end);
+            v += picture->linesize[2] - (w_start - w_end);
         }
 
+        if (ci->debug) 
+            fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
+
         if (inrange * 1000 / pixcnt >= ci->threshold) {
             /* Save to file */
             int size;
@@ -273,9 +304,6 @@ void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width,
             static int frame_counter;
             static int foundfile;
 
-            if (ci->debug) 
-                fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
-
             if ((frame_counter++ % 20) == 0) {
                 /* Check how many files we have */
                 DIR *d;
@@ -309,7 +337,7 @@ void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width,
                     FILE *f;
                     char fname[256];
 
-                    sprintf(fname, "%s/fishimg%ld_%lld.ppm", ci->dir, time(0), pts);
+                    snprintf(fname, sizeof(fname), "%s/fishimg%ld_%lld.ppm", ci->dir, time(0), pts);
                     f = fopen(fname, "w");
                     if (f) {
                         fprintf(f, "P6 %d %d 255\n", width, height);
@@ -325,6 +353,3 @@ void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width,
     }
 }
 
-/* To ensure correct typing */
-FrameHookConfigureFn ConfigureFn = Configure;
-FrameHookProcessFn ProcessFn = Process;