~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/vhook/fish.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-25 15:47:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080225154712-qvr11ekcea4c9ry8
Tags: 1.1.6-0.1ubuntu1
* Merge from debian-multimedia (LP: #120003), Ubuntu Changes:
 - For ffmpeg-related build-deps, remove cvs from package names.
 - Standards-Version 3.7.3
 - Maintainer Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * Fish Detector Hook
3
3
 * Copyright (c) 2002 Philip Gladstone
4
4
 *
5
 
 * This file implements a fish detector. It is used to see when a 
 
5
 * This file implements a fish detector. It is used to see when a
6
6
 * goldfish passes in front of the camera. It does this by counting
7
7
 * the number of input pixels that fall within a particular HSV
8
8
 * range.
19
19
 * -d                turn debugging on
20
20
 * -D <directory>    where to put the fish images
21
21
 *
22
 
 * This library is free software; you can redistribute it and/or
 
22
 * This file is part of FFmpeg.
 
23
 *
 
24
 * FFmpeg is free software; you can redistribute it and/or
23
25
 * modify it under the terms of the GNU Lesser General Public
24
26
 * License as published by the Free Software Foundation; either
25
 
 * version 2 of the License, or (at your option) any later version.
 
27
 * version 2.1 of the License, or (at your option) any later version.
26
28
 *
27
 
 * This library is distributed in the hope that it will be useful,
 
29
 * FFmpeg is distributed in the hope that it will be useful,
28
30
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
31
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30
32
 * Lesser General Public License for more details.
31
33
 *
32
34
 * You should have received a copy of the GNU Lesser General Public
33
 
 * License along with this library; if not, write to the Free Software
34
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
35
 * License along with FFmpeg; if not, write to the Free Software
 
36
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
35
37
 */
36
38
#include <stdlib.h>
37
39
#include <fcntl.h>
44
46
 
45
47
#include "framehook.h"
46
48
#include "dsputil.h"
 
49
#include "avformat.h"
 
50
#include "swscale.h"
 
51
 
 
52
static int sws_flags = SWS_BICUBIC;
47
53
 
48
54
#define SCALEBITS 10
49
55
#define ONE_HALF  (1 << (SCALEBITS - 1))
68
74
}
69
75
 
70
76
 
71
 
 
72
 
  
 
77
 
 
78
 
73
79
typedef struct {
74
80
    int h;  /* 0 .. 360 */
75
81
    int s;  /* 0 .. 255 */
76
82
    int v;  /* 0 .. 255 */
77
83
} HSV;
78
 
              
 
84
 
79
85
typedef struct {
80
86
    int zapping;
81
87
    int threshold;
87
93
    int64_t next_pts;
88
94
    int inset;
89
95
    int min_width;
 
96
    struct SwsContext *toRGB_convert_ctx;
90
97
} ContextInfo;
91
98
 
92
99
static void dorange(const char *s, int *first, int *second, int maxval)
100
107
 
101
108
void Release(void *ctx)
102
109
{
103
 
    if (ctx)
 
110
    ContextInfo *ci;
 
111
    ci = (ContextInfo *) ctx;
 
112
 
 
113
    if (ctx) {
 
114
        sws_freeContext(ci->toRGB_convert_ctx);
104
115
        av_free(ctx);
 
116
    }
105
117
}
106
118
 
107
119
int Configure(void **ctxp, int argc, char *argv[])
179
191
static void get_hsv(HSV *hsv, int r, int g, int b)
180
192
{
181
193
    int i, v, x, f;
182
 
         
 
194
 
183
195
    x = (r < g) ? r : g;
184
196
    if (b < x)
185
197
        x = b;
186
198
    v = (r > g) ? r : g;
187
199
    if (b > v)
188
200
        v = b;
189
 
          
 
201
 
190
202
    if (v == x) {
191
203
        hsv->h = 0;
192
204
        hsv->s = 0;
193
205
        hsv->v = v;
194
206
        return;
195
207
    }
196
 
       
 
208
 
197
209
    if (r == v) {
198
210
        f = g - b;
199
211
        i = 0;
204
216
        f = r - g;
205
217
        i = 4 * 60;
206
218
    }
207
 
        
 
219
 
208
220
    hsv->h = i + (60 * f) / (v - x);
209
221
    if (hsv->h < 0)
210
222
        hsv->h += 360;
211
223
 
212
224
    hsv->s = (255 * (v - x)) / v;
213
225
    hsv->v = v;
214
 
         
 
226
 
215
227
    return;
216
 
}                                                                               
 
228
}
217
229
 
218
230
void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
219
231
{
220
232
    ContextInfo *ci = (ContextInfo *) ctx;
221
 
    uint8_t *cm = cropTbl + MAX_NEG_CROP;                                         
 
233
    uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
222
234
    int rowsize = picture->linesize[0];
223
235
 
224
236
#if 0
232
244
    if (width < ci->min_width)
233
245
        return;
234
246
 
235
 
    ci->next_pts = pts + 1000000;    
 
247
    ci->next_pts = pts + 1000000;
236
248
 
237
249
    if (pix_fmt == PIX_FMT_YUV420P) {
238
250
        uint8_t *y, *u, *v;
268
280
 
269
281
                get_hsv(&hsv, r, g, b);
270
282
 
271
 
                if (ci->debug > 1) 
 
283
                if (ci->debug > 1)
272
284
                    fprintf(stderr, "(%d,%d,%d) -> (%d,%d,%d)\n",
273
285
                        r,g,b,hsv.h,hsv.s,hsv.v);
274
286
 
275
287
 
276
288
                if (hsv.h >= ci->dark.h && hsv.h <= ci->bright.h &&
277
289
                    hsv.s >= ci->dark.s && hsv.s <= ci->bright.s &&
278
 
                    hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) {            
 
290
                    hsv.v >= ci->dark.v && hsv.v <= ci->bright.v) {
279
291
                    inrange++;
280
292
                } else if (ci->zapping) {
281
293
                    y[0] = y[1] = y[rowsize] = y[rowsize + 1] = 16;
293
305
            v += picture->linesize[2] - (w_start - w_end);
294
306
        }
295
307
 
296
 
        if (ci->debug) 
 
308
        if (ci->debug)
297
309
            fprintf(stderr, "Fish: Inrange=%d of %d = %d threshold\n", inrange, pixcnt, 1000 * inrange / pixcnt);
298
310
 
299
311
        if (inrange * 1000 / pixcnt >= ci->threshold) {
326
338
            }
327
339
 
328
340
            if (foundfile < ci->file_limit) {
 
341
                FILE *f;
 
342
                char fname[256];
 
343
 
329
344
                size = avpicture_get_size(PIX_FMT_RGB24, width, height);
330
345
                buf = av_malloc(size);
331
346
 
332
347
                avpicture_fill(&picture1, buf, PIX_FMT_RGB24, width, height);
333
 
                if (img_convert(&picture1, PIX_FMT_RGB24, 
334
 
                                picture, pix_fmt, width, height) >= 0) {
 
348
 
 
349
                // if we already got a SWS context, let's realloc if is not re-useable
 
350
                ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,
 
351
                                            width, height, pix_fmt,
 
352
                                            width, height, PIX_FMT_RGB24,
 
353
                                            sws_flags, NULL, NULL, NULL);
 
354
                if (ci->toRGB_convert_ctx == NULL) {
 
355
                    av_log(NULL, AV_LOG_ERROR,
 
356
                           "Cannot initialize the toRGB conversion context\n");
 
357
                    exit(1);
 
358
                }
 
359
                // img_convert parameters are          2 first destination, then 4 source
 
360
                // sws_scale   parameters are context, 4 first source,      then 2 destination
 
361
                sws_scale(ci->toRGB_convert_ctx,
 
362
                              picture->data, picture->linesize, 0, height,
 
363
                              picture1.data, picture1.linesize);
 
364
 
335
365
                    /* Write out the PPM file */
336
 
 
337
 
                    FILE *f;
338
 
                    char fname[256];
339
 
 
340
 
                    sprintf(fname, "%s/fishimg%ld_%lld.ppm", ci->dir, time(0), pts);
 
366
                    snprintf(fname, sizeof(fname), "%s/fishimg%ld_%"PRId64".ppm", ci->dir, (long)(av_gettime() / 1000000), pts);
341
367
                    f = fopen(fname, "w");
342
368
                    if (f) {
343
369
                        fprintf(f, "P6 %d %d 255\n", width, height);
344
370
                        fwrite(buf, width * height * 3, 1, f);
345
371
                        fclose(f);
346
372
                    }
347
 
                }
348
373
 
349
374
                av_free(buf);
350
 
                ci->next_pts = pts + ci->min_interval;    
 
375
                ci->next_pts = pts + ci->min_interval;
351
376
            }
352
377
        }
353
378
    }