Statistics
| Branch: | Revision:

ffmpeg / libavformat / grab.c @ 34f633df

History | View | Annotate | Download (28.2 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * Linux video grab interface
3 19720f15 Fabrice Bellard
 * Copyright (c) 2000,2001 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 19720f15 Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 *
10 19720f15 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 19720f15 Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 */
19 8be1c656 Fabrice Bellard
#include "avformat.h"
20 de6d9b64 Fabrice Bellard
#include <unistd.h>
21
#include <fcntl.h>
22
#include <sys/ioctl.h>
23
#include <sys/mman.h>
24
#include <sys/time.h>
25 72a476c1 Michael Niedermayer
#define _LINUX_TIME_H 1
26
#include <linux/videodev.h>
27 608d0dee Zdenek Kabelac
#include <time.h>
28 de6d9b64 Fabrice Bellard
29
typedef struct {
30
    int fd;
31
    int frame_format; /* see VIDEO_PALETTE_xxx */
32
    int use_mmap;
33
    int width, height;
34 4972b26f Fabrice Bellard
    int frame_rate;
35 14bea432 Michael Niedermayer
    int frame_rate_base;
36 0c1a9eda Zdenek Kabelac
    int64_t time_frame;
37 4972b26f Fabrice Bellard
    int frame_size;
38 8949367e Fabrice Bellard
    struct video_capability video_cap;
39
    struct video_audio audio_saved;
40 0c1a9eda Zdenek Kabelac
    uint8_t *video_buf;
41 8949367e Fabrice Bellard
    struct video_mbuf gb_buffers;
42
    struct video_mmap gb_buf;
43
    int gb_frame;
44
45
    /* ATI All In Wonder specific stuff */
46
    /* XXX: remove and merge in libavcodec/imgconvert.c */
47
    int aiw_enabled;
48
    int deint;
49
    int halfw;
50 0c1a9eda Zdenek Kabelac
    uint8_t *src_mem;
51
    uint8_t *lum_m4_mem;
52 de6d9b64 Fabrice Bellard
} VideoData;
53
54 8949367e Fabrice Bellard
static int aiw_init(VideoData *s);
55
static int aiw_read_picture(VideoData *s, uint8_t *data);
56
static int aiw_close(VideoData *s);
57 de6d9b64 Fabrice Bellard
58 4972b26f Fabrice Bellard
static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
59 de6d9b64 Fabrice Bellard
{
60 c9a65ca8 Fabrice Bellard
    VideoData *s = s1->priv_data;
61 4972b26f Fabrice Bellard
    AVStream *st;
62 de6d9b64 Fabrice Bellard
    int width, height;
63
    int video_fd, frame_size;
64 14bea432 Michael Niedermayer
    int ret, frame_rate, frame_rate_base;
65 4606ac8d Zdenek Kabelac
    int desired_palette;
66 e3ee3283 Alex Beregszaszi
    struct video_tuner tuner;
67 8949367e Fabrice Bellard
    struct video_audio audio;
68 7f172339 Fabrice Bellard
    const char *video_device;
69 7aae3168 Michael Niedermayer
    int j;
70 4972b26f Fabrice Bellard
71 c0df9d75 Michael Niedermayer
    if (!ap || ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0)
72 4972b26f Fabrice Bellard
        return -1;
73 de6d9b64 Fabrice Bellard
    
74 4972b26f Fabrice Bellard
    width = ap->width;
75
    height = ap->height;
76 c0df9d75 Michael Niedermayer
    frame_rate      = ap->time_base.den;
77
    frame_rate_base = ap->time_base.num;
78 4972b26f Fabrice Bellard
79 568e18b1 Michael Niedermayer
    if((unsigned)width > 32767 || (unsigned)height > 32767)
80
        return -1;
81
    
82 c9a65ca8 Fabrice Bellard
    st = av_new_stream(s1, 0);
83
    if (!st)
84 4972b26f Fabrice Bellard
        return -ENOMEM;
85 9ee91c2f Michael Niedermayer
    av_set_pts_info(st, 48, 1, 1000000); /* 48 bits pts in us */
86 4972b26f Fabrice Bellard
87
    s->width = width;
88
    s->height = height;
89 14bea432 Michael Niedermayer
    s->frame_rate      = frame_rate;
90
    s->frame_rate_base = frame_rate_base;
91 de6d9b64 Fabrice Bellard
92 7f172339 Fabrice Bellard
    video_device = ap->device;
93
    if (!video_device)
94
        video_device = "/dev/video";
95 8aa3ee32 Max Krasnyansky
    video_fd = open(video_device, O_RDWR);
96 de6d9b64 Fabrice Bellard
    if (video_fd < 0) {
97 8aa3ee32 Max Krasnyansky
        perror(video_device);
98 4972b26f Fabrice Bellard
        goto fail;
99 de6d9b64 Fabrice Bellard
    }
100
    
101 8949367e Fabrice Bellard
    if (ioctl(video_fd,VIDIOCGCAP, &s->video_cap) < 0) {
102 de6d9b64 Fabrice Bellard
        perror("VIDIOCGCAP");
103
        goto fail;
104
    }
105
106 8949367e Fabrice Bellard
    if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
107 bc874dae Michel Bardiaux
        av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
108 de6d9b64 Fabrice Bellard
        goto fail;
109
    }
110 4606ac8d Zdenek Kabelac
111
    desired_palette = -1;
112
    if (st->codec.pix_fmt == PIX_FMT_YUV420P) {
113
        desired_palette = VIDEO_PALETTE_YUV420P;
114
    } else if (st->codec.pix_fmt == PIX_FMT_YUV422) {
115
        desired_palette = VIDEO_PALETTE_YUV422;
116
    } else if (st->codec.pix_fmt == PIX_FMT_BGR24) {
117
        desired_palette = VIDEO_PALETTE_RGB24;
118
    }    
119 e3ee3283 Alex Beregszaszi
120
    /* set tv standard */
121
    if (ap->standard && !ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
122
        if (!strcasecmp(ap->standard, "pal"))
123
            tuner.mode = VIDEO_MODE_PAL;
124
        else if (!strcasecmp(ap->standard, "secam"))
125
            tuner.mode = VIDEO_MODE_SECAM;
126
        else
127
            tuner.mode = VIDEO_MODE_NTSC;
128
        ioctl(video_fd, VIDIOCSTUNER, &tuner);
129
    }
130 de6d9b64 Fabrice Bellard
    
131
    /* unmute audio */
132 8949367e Fabrice Bellard
    audio.audio = 0;
133 de6d9b64 Fabrice Bellard
    ioctl(video_fd, VIDIOCGAUDIO, &audio);
134 8949367e Fabrice Bellard
    memcpy(&s->audio_saved, &audio, sizeof(audio));
135 de6d9b64 Fabrice Bellard
    audio.flags &= ~VIDEO_AUDIO_MUTE;
136
    ioctl(video_fd, VIDIOCSAUDIO, &audio);
137
138 8949367e Fabrice Bellard
    ret = ioctl(video_fd,VIDIOCGMBUF,&s->gb_buffers);
139 de6d9b64 Fabrice Bellard
    if (ret < 0) {
140
        /* try to use read based access */
141
        struct video_window win;
142
        struct video_picture pict;
143
        int val;
144
145
        win.x = 0;
146
        win.y = 0;
147
        win.width = width;
148
        win.height = height;
149
        win.chromakey = -1;
150
        win.flags = 0;
151
152
        ioctl(video_fd, VIDIOCSWIN, &win);
153
154
        ioctl(video_fd, VIDIOCGPICT, &pict);
155
#if 0
156
        printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
157
               pict.colour,
158
               pict.hue,
159
               pict.brightness,
160
               pict.contrast,
161
               pict.whiteness);
162
#endif        
163
        /* try to choose a suitable video format */
164 4606ac8d Zdenek Kabelac
        pict.palette = desired_palette;
165
        if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
166
            pict.palette=VIDEO_PALETTE_YUV420P;
167 de6d9b64 Fabrice Bellard
            ret = ioctl(video_fd, VIDIOCSPICT, &pict);
168
            if (ret < 0) {
169 4606ac8d Zdenek Kabelac
                pict.palette=VIDEO_PALETTE_YUV422;
170 de6d9b64 Fabrice Bellard
                ret = ioctl(video_fd, VIDIOCSPICT, &pict);
171 4606ac8d Zdenek Kabelac
                if (ret < 0) {
172
                    pict.palette=VIDEO_PALETTE_RGB24;
173
                    ret = ioctl(video_fd, VIDIOCSPICT, &pict);
174
                    if (ret < 0) 
175
                        goto fail1;
176
                }
177 de6d9b64 Fabrice Bellard
            }
178
        }
179
180
        s->frame_format = pict.palette;
181
182
        val = 1;
183
        ioctl(video_fd, VIDIOCCAPTURE, &val);
184
185 3834be58 Luca Abeni
        s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
186 de6d9b64 Fabrice Bellard
        s->use_mmap = 0;
187 8949367e Fabrice Bellard
        
188
        /* ATI All In Wonder automatic activation */
189
        if (!strcmp(s->video_cap.name, "Km")) {
190
            if (aiw_init(s) < 0)
191
                goto fail;
192
            s->aiw_enabled = 1;
193
            /* force 420P format because convertion from YUV422 to YUV420P
194
               is done in this driver (ugly) */
195
            s->frame_format = VIDEO_PALETTE_YUV420P;
196
        }
197 de6d9b64 Fabrice Bellard
    } else {
198 8949367e Fabrice Bellard
        s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
199
        if ((unsigned char*)-1 == s->video_buf) {
200 de6d9b64 Fabrice Bellard
            perror("mmap");
201
            goto fail;
202
        }
203 8949367e Fabrice Bellard
        s->gb_frame = 0;
204 3834be58 Luca Abeni
        s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
205 de6d9b64 Fabrice Bellard
        
206
        /* start to grab the first frame */
207 8949367e Fabrice Bellard
        s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
208
        s->gb_buf.height = height;
209
        s->gb_buf.width = width;
210
        s->gb_buf.format = desired_palette;
211 4606ac8d Zdenek Kabelac
212 8949367e Fabrice Bellard
        if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf)) < 0) {
213
            s->gb_buf.format = VIDEO_PALETTE_YUV420P;
214 de6d9b64 Fabrice Bellard
            
215 8949367e Fabrice Bellard
            ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
216 4606ac8d Zdenek Kabelac
            if (ret < 0 && errno != EAGAIN) {
217
                /* try YUV422 */
218 8949367e Fabrice Bellard
                s->gb_buf.format = VIDEO_PALETTE_YUV422;
219 4606ac8d Zdenek Kabelac
                
220 8949367e Fabrice Bellard
                ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
221 4606ac8d Zdenek Kabelac
                if (ret < 0 && errno != EAGAIN) {
222
                    /* try RGB24 */
223 8949367e Fabrice Bellard
                    s->gb_buf.format = VIDEO_PALETTE_RGB24;
224
                    ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
225 4606ac8d Zdenek Kabelac
                }
226
            }
227 de6d9b64 Fabrice Bellard
        }
228
        if (ret < 0) {
229
            if (errno != EAGAIN) {
230
            fail1:
231 bc874dae Michel Bardiaux
                av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not support suitable format\n");
232 de6d9b64 Fabrice Bellard
            } else {
233 bc874dae Michel Bardiaux
                av_log(s1, AV_LOG_ERROR,"Fatal: grab device does not receive any video signal\n");
234 de6d9b64 Fabrice Bellard
            }
235
            goto fail;
236
        }
237 7aae3168 Michael Niedermayer
        for (j = 1; j < s->gb_buffers.frames; j++) {
238
          s->gb_buf.frame = j;
239
          ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
240
        }
241 8949367e Fabrice Bellard
        s->frame_format = s->gb_buf.format;
242 de6d9b64 Fabrice Bellard
        s->use_mmap = 1;
243
    }
244
245
    switch(s->frame_format) {
246
    case VIDEO_PALETTE_YUV420P:
247
        frame_size = (width * height * 3) / 2;
248 4972b26f Fabrice Bellard
        st->codec.pix_fmt = PIX_FMT_YUV420P;
249 de6d9b64 Fabrice Bellard
        break;
250
    case VIDEO_PALETTE_YUV422:
251
        frame_size = width * height * 2;
252 4972b26f Fabrice Bellard
        st->codec.pix_fmt = PIX_FMT_YUV422;
253 de6d9b64 Fabrice Bellard
        break;
254
    case VIDEO_PALETTE_RGB24:
255
        frame_size = width * height * 3;
256 4972b26f Fabrice Bellard
        st->codec.pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
257 de6d9b64 Fabrice Bellard
        break;
258
    default:
259
        goto fail;
260
    }
261
    s->fd = video_fd;
262 4972b26f Fabrice Bellard
    s->frame_size = frame_size;
263
    
264 28c66901 Philip Gladstone
    st->codec.codec_type = CODEC_TYPE_VIDEO;
265 4972b26f Fabrice Bellard
    st->codec.codec_id = CODEC_ID_RAWVIDEO;
266
    st->codec.width = width;
267
    st->codec.height = height;
268 c0df9d75 Michael Niedermayer
    st->codec.time_base.den      = frame_rate;
269
    st->codec.time_base.num = frame_rate_base;
270 45dd5c69 Fabrice Bellard
271 de6d9b64 Fabrice Bellard
    return 0;
272
 fail:
273 4972b26f Fabrice Bellard
    if (video_fd >= 0)
274
        close(video_fd);
275 1ea4f593 Fabrice Bellard
    av_free(st);
276 0bd586c5 Mike Melanson
    return AVERROR_IO;
277 de6d9b64 Fabrice Bellard
}
278
279 0c1a9eda Zdenek Kabelac
static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
280 de6d9b64 Fabrice Bellard
{
281 0c1a9eda Zdenek Kabelac
    uint8_t *ptr;
282 de6d9b64 Fabrice Bellard
283 7aae3168 Michael Niedermayer
    while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
284
           (errno == EAGAIN || errno == EINTR));
285
286
    ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
287
    memcpy(buf, ptr, s->frame_size);
288
289 4606ac8d Zdenek Kabelac
    /* Setup to capture the next frame */
290 7aae3168 Michael Niedermayer
    s->gb_buf.frame = s->gb_frame;
291 8949367e Fabrice Bellard
    if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
292 28c66901 Philip Gladstone
        if (errno == EAGAIN)
293 bc874dae Michel Bardiaux
            av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
294 28c66901 Philip Gladstone
        else
295 de6d9b64 Fabrice Bellard
            perror("VIDIOCMCAPTURE");
296 0bd586c5 Mike Melanson
        return AVERROR_IO;
297 de6d9b64 Fabrice Bellard
    }
298 4606ac8d Zdenek Kabelac
299
    /* This is now the grabbing frame */
300 7aae3168 Michael Niedermayer
    s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
301 4606ac8d Zdenek Kabelac
302 4972b26f Fabrice Bellard
    return s->frame_size;
303 de6d9b64 Fabrice Bellard
}
304
305 4972b26f Fabrice Bellard
static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
306 de6d9b64 Fabrice Bellard
{
307 4972b26f Fabrice Bellard
    VideoData *s = s1->priv_data;
308 0c1a9eda Zdenek Kabelac
    int64_t curtime, delay;
309 4972b26f Fabrice Bellard
    struct timespec ts;
310 4606ac8d Zdenek Kabelac
311
    /* Calculate the time of the next frame */
312 3834be58 Luca Abeni
    s->time_frame += int64_t_C(1000000);
313 de6d9b64 Fabrice Bellard
314
    /* wait based on the frame rate */
315 45dd5c69 Fabrice Bellard
    for(;;) {
316 a11bf0bd Fabrice Bellard
        curtime = av_gettime();
317 3834be58 Luca Abeni
        delay = s->time_frame  * s->frame_rate_base / s->frame_rate - curtime;
318 4606ac8d Zdenek Kabelac
        if (delay <= 0) {
319 3834be58 Luca Abeni
            if (delay < int64_t_C(-1000000) * s->frame_rate_base / s->frame_rate) {
320 4606ac8d Zdenek Kabelac
                /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
321 3834be58 Luca Abeni
                s->time_frame += int64_t_C(1000000);
322 4606ac8d Zdenek Kabelac
            }
323 4972b26f Fabrice Bellard
            break;
324 4606ac8d Zdenek Kabelac
        }    
325 4972b26f Fabrice Bellard
        ts.tv_sec = delay / 1000000;
326
        ts.tv_nsec = (delay % 1000000) * 1000;
327
        nanosleep(&ts, NULL);
328
    }
329
330
    if (av_new_packet(pkt, s->frame_size) < 0)
331 0bd586c5 Mike Melanson
        return AVERROR_IO;
332 de6d9b64 Fabrice Bellard
333 45dd5c69 Fabrice Bellard
    pkt->pts = curtime & ((1LL << 48) - 1);
334
335 de6d9b64 Fabrice Bellard
    /* read one frame */
336 8949367e Fabrice Bellard
    if (s->aiw_enabled) {
337
        return aiw_read_picture(s, pkt->data);
338
    } else if (s->use_mmap) {
339 4972b26f Fabrice Bellard
        return v4l_mm_read_picture(s, pkt->data);
340 de6d9b64 Fabrice Bellard
    } else {
341 4972b26f Fabrice Bellard
        if (read(s->fd, pkt->data, pkt->size) != pkt->size)
342 0bd586c5 Mike Melanson
            return AVERROR_IO;
343 4972b26f Fabrice Bellard
        return s->frame_size;
344 de6d9b64 Fabrice Bellard
    }
345
}
346
347 4972b26f Fabrice Bellard
static int grab_read_close(AVFormatContext *s1)
348 de6d9b64 Fabrice Bellard
{
349 4972b26f Fabrice Bellard
    VideoData *s = s1->priv_data;
350 e61efa24 Lennert Buytenhek
351 8949367e Fabrice Bellard
    if (s->aiw_enabled)
352
        aiw_close(s);
353
354 e61efa24 Lennert Buytenhek
    if (s->use_mmap)
355 8949367e Fabrice Bellard
        munmap(s->video_buf, s->gb_buffers.size);
356 e61efa24 Lennert Buytenhek
357 8949367e Fabrice Bellard
    /* mute audio. we must force it because the BTTV driver does not
358
       return its state correctly */
359
    s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
360
    ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
361 5a56c87c Fabrice Bellard
362 de6d9b64 Fabrice Bellard
    close(s->fd);
363
    return 0;
364
}
365
366 c18a2692 Zdenek Kabelac
static AVInputFormat video_grab_device_format = {
367 8aa3ee32 Max Krasnyansky
    "video4linux",
368 4972b26f Fabrice Bellard
    "video grab",
369 c9a65ca8 Fabrice Bellard
    sizeof(VideoData),
370 4972b26f Fabrice Bellard
    NULL,
371
    grab_read_header,
372
    grab_read_packet,
373
    grab_read_close,
374 bb76a117 Måns Rullgård
    .flags = AVFMT_NOFILE,
375 de6d9b64 Fabrice Bellard
};
376 c9a65ca8 Fabrice Bellard
377 8949367e Fabrice Bellard
/* All in Wonder specific stuff */
378
/* XXX: remove and merge in libavcodec/imgconvert.c */
379 3f9bff71 Michael Niedermayer
380 8949367e Fabrice Bellard
static int aiw_init(VideoData *s)
381 3f9bff71 Michael Niedermayer
{
382
    int width, height;
383
384 8949367e Fabrice Bellard
    width = s->width;
385
    height = s->height;
386 3f9bff71 Michael Niedermayer
387 8949367e Fabrice Bellard
    if ((width == s->video_cap.maxwidth && height == s->video_cap.maxheight) ||
388
        (width == s->video_cap.maxwidth && height == s->video_cap.maxheight*2) ||
389
        (width == s->video_cap.maxwidth/2 && height == s->video_cap.maxheight)) {
390
        
391
        s->deint=0;
392
        s->halfw=0;
393
        if (height == s->video_cap.maxheight*2) s->deint=1;
394
        if (width == s->video_cap.maxwidth/2) s->halfw=1;
395 3f9bff71 Michael Niedermayer
    } else {
396 bc874dae Michel Bardiaux
        av_log(NULL, AV_LOG_ERROR, "\nIncorrect Grab Size Supplied - Supported Sizes Are:\n");
397
        av_log(NULL, AV_LOG_ERROR, " %dx%d  %dx%d %dx%d\n\n",
398 8949367e Fabrice Bellard
                s->video_cap.maxwidth,s->video_cap.maxheight,
399
                s->video_cap.maxwidth,s->video_cap.maxheight*2,
400
                s->video_cap.maxwidth/2,s->video_cap.maxheight);
401 3f9bff71 Michael Niedermayer
        goto fail;
402
    }
403
404
    if (s->halfw == 0) {
405
        s->src_mem = av_malloc(s->width*2);
406
    } else {
407
        s->src_mem = av_malloc(s->width*4);
408
    }
409
    if (!s->src_mem) goto fail;
410
411
    s->lum_m4_mem = av_malloc(s->width);
412 8949367e Fabrice Bellard
    if (!s->lum_m4_mem)
413 3f9bff71 Michael Niedermayer
        goto fail;
414
    return 0;
415
 fail:
416 8949367e Fabrice Bellard
    av_freep(&s->src_mem);
417
    av_freep(&s->lum_m4_mem);
418
    return -1;
419 3f9bff71 Michael Niedermayer
}
420
421
#ifdef HAVE_MMX
422
#include "../libavcodec/i386/mmx.h"
423
424
#define LINE_WITH_UV \
425
                    movq_m2r(ptr[0],mm0); \
426
                    movq_m2r(ptr[8],mm1);  \
427
                    movq_r2r(mm0, mm4); \
428
                    punpcklbw_r2r(mm1,mm0); \
429
                    punpckhbw_r2r(mm1,mm4); \
430
                    movq_r2r(mm0,mm5); \
431
                    punpcklbw_r2r(mm4,mm0); \
432
                    punpckhbw_r2r(mm4,mm5); \
433
                    movq_r2r(mm0,mm1); \
434
                    punpcklbw_r2r(mm5,mm1); \
435
                    movq_r2m(mm1,lum[0]); \
436
                    movq_m2r(ptr[16],mm2); \
437
                    movq_m2r(ptr[24],mm1); \
438
                    movq_r2r(mm2,mm4); \
439
                    punpcklbw_r2r(mm1,mm2); \
440
                    punpckhbw_r2r(mm1,mm4); \
441
                    movq_r2r(mm2,mm3); \
442
                    punpcklbw_r2r(mm4,mm2); \
443
                    punpckhbw_r2r(mm4,mm3); \
444
                    movq_r2r(mm2,mm1); \
445
                    punpcklbw_r2r(mm3,mm1); \
446
                    movq_r2m(mm1,lum[8]); \
447
                    punpckhdq_r2r(mm2,mm0); \
448
                    punpckhdq_r2r(mm3,mm5); \
449
                    movq_r2m(mm0,cb[0]); \
450
                    movq_r2m(mm5,cr[0]);
451
452
#define LINE_NO_UV \
453
                    movq_m2r(ptr[0],mm0);\
454
                    movq_m2r(ptr[8],mm1);\
455
                    movq_r2r(mm0, mm4);\
456
                    punpcklbw_r2r(mm1,mm0); \
457
                    punpckhbw_r2r(mm1,mm4);\
458
                    movq_r2r(mm0,mm5);\
459
                    punpcklbw_r2r(mm4,mm0);\
460
                    punpckhbw_r2r(mm4,mm5);\
461
                    movq_r2r(mm0,mm1);\
462
                    punpcklbw_r2r(mm5,mm1);\
463
                    movq_r2m(mm1,lum[0]);\
464
                    movq_m2r(ptr[16],mm2);\
465
                    movq_m2r(ptr[24],mm1);\
466
                    movq_r2r(mm2,mm4);\
467
                    punpcklbw_r2r(mm1,mm2);\
468
                    punpckhbw_r2r(mm1,mm4);\
469
                    movq_r2r(mm2,mm3);\
470
                    punpcklbw_r2r(mm4,mm2);\
471
                    punpckhbw_r2r(mm4,mm3);\
472
                    movq_r2r(mm2,mm1);\
473
                    punpcklbw_r2r(mm3,mm1);\
474
                    movq_r2m(mm1,lum[8]);
475
476
#define LINE_WITHUV_AVG \
477
                    movq_m2r(ptr[0], mm0);\
478
                    movq_m2r(ptr[8], mm1);\
479
                    movq_r2r(mm0, mm4);\
480
                    punpcklbw_r2r(mm1,mm0);\
481
                    punpckhbw_r2r(mm1,mm4);\
482
                    movq_r2r(mm0,mm5);\
483
                    punpcklbw_r2r(mm4,mm0);\
484
                    punpckhbw_r2r(mm4,mm5);\
485
                    movq_r2r(mm0,mm1);\
486
                    movq_r2r(mm5,mm2);\
487
                    punpcklbw_r2r(mm7,mm1);\
488
                    punpcklbw_r2r(mm7,mm2);\
489
                    paddw_r2r(mm6,mm1);\
490
                    paddw_r2r(mm2,mm1);\
491
                    psraw_i2r(1,mm1);\
492
                    packuswb_r2r(mm7,mm1);\
493
                    movd_r2m(mm1,lum[0]);\
494
                    movq_m2r(ptr[16],mm2);\
495
                    movq_m2r(ptr[24],mm1);\
496
                    movq_r2r(mm2,mm4);\
497
                    punpcklbw_r2r(mm1,mm2);\
498
                    punpckhbw_r2r(mm1,mm4);\
499
                    movq_r2r(mm2,mm3);\
500
                    punpcklbw_r2r(mm4,mm2);\
501
                    punpckhbw_r2r(mm4,mm3);\
502
                    movq_r2r(mm2,mm1);\
503
                    movq_r2r(mm3,mm4);\
504
                    punpcklbw_r2r(mm7,mm1);\
505
                    punpcklbw_r2r(mm7,mm4);\
506
                    paddw_r2r(mm6,mm1);\
507
                    paddw_r2r(mm4,mm1);\
508
                    psraw_i2r(1,mm1);\
509
                    packuswb_r2r(mm7,mm1);\
510
                    movd_r2m(mm1,lum[4]);\
511
                    punpckhbw_r2r(mm7,mm0);\
512
                    punpckhbw_r2r(mm7,mm2);\
513
                    paddw_r2r(mm6,mm0);\
514
                    paddw_r2r(mm2,mm0);\
515
                    psraw_i2r(1,mm0);\
516
                    packuswb_r2r(mm7,mm0);\
517
                    punpckhbw_r2r(mm7,mm5);\
518
                    punpckhbw_r2r(mm7,mm3);\
519
                    paddw_r2r(mm6,mm5);\
520
                    paddw_r2r(mm3,mm5);\
521
                    psraw_i2r(1,mm5);\
522
                    packuswb_r2r(mm7,mm5);\
523
                    movd_r2m(mm0,cb[0]);\
524
                    movd_r2m(mm5,cr[0]);
525
526
#define LINE_NOUV_AVG \
527
                    movq_m2r(ptr[0],mm0);\
528
                    movq_m2r(ptr[8],mm1);\
529
                    pand_r2r(mm5,mm0);\
530
                    pand_r2r(mm5,mm1);\
531
                    pmaddwd_r2r(mm6,mm0);\
532
                    pmaddwd_r2r(mm6,mm1);\
533
                    packssdw_r2r(mm1,mm0);\
534
                    paddw_r2r(mm6,mm0);\
535
                    psraw_i2r(1,mm0);\
536
                    movq_m2r(ptr[16],mm2);\
537
                    movq_m2r(ptr[24],mm3);\
538
                    pand_r2r(mm5,mm2);\
539
                    pand_r2r(mm5,mm3);\
540
                    pmaddwd_r2r(mm6,mm2);\
541
                    pmaddwd_r2r(mm6,mm3);\
542
                    packssdw_r2r(mm3,mm2);\
543
                    paddw_r2r(mm6,mm2);\
544
                    psraw_i2r(1,mm2);\
545
                    packuswb_r2r(mm2,mm0);\
546
                    movq_r2m(mm0,lum[0]);
547
548
#define DEINT_LINE_LUM(ptroff) \
549
                    movd_m2r(lum_m4[(ptroff)],mm0);\
550
                    movd_m2r(lum_m3[(ptroff)],mm1);\
551
                    movd_m2r(lum_m2[(ptroff)],mm2);\
552
                    movd_m2r(lum_m1[(ptroff)],mm3);\
553
                    movd_m2r(lum[(ptroff)],mm4);\
554
                    punpcklbw_r2r(mm7,mm0);\
555
                    movd_r2m(mm2,lum_m4[(ptroff)]);\
556
                    punpcklbw_r2r(mm7,mm1);\
557
                    punpcklbw_r2r(mm7,mm2);\
558
                    punpcklbw_r2r(mm7,mm3);\
559
                    punpcklbw_r2r(mm7,mm4);\
560
                    psllw_i2r(2,mm1);\
561
                    psllw_i2r(1,mm2);\
562
                    paddw_r2r(mm6,mm1);\
563
                    psllw_i2r(2,mm3);\
564
                    paddw_r2r(mm2,mm1);\
565
                    paddw_r2r(mm4,mm0);\
566
                    paddw_r2r(mm3,mm1);\
567
                    psubusw_r2r(mm0,mm1);\
568
                    psrlw_i2r(3,mm1);\
569
                    packuswb_r2r(mm7,mm1);\
570
                    movd_r2m(mm1,lum_m2[(ptroff)]);
571
572
#else
573
#include "../libavcodec/dsputil.h"
574
575
#define LINE_WITH_UV \
576
                    lum[0]=ptr[0];lum[1]=ptr[2];lum[2]=ptr[4];lum[3]=ptr[6];\
577
                    cb[0]=ptr[1];cb[1]=ptr[5];\
578
                    cr[0]=ptr[3];cr[1]=ptr[7];\
579
                    lum[4]=ptr[8];lum[5]=ptr[10];lum[6]=ptr[12];lum[7]=ptr[14];\
580
                    cb[2]=ptr[9];cb[3]=ptr[13];\
581
                    cr[2]=ptr[11];cr[3]=ptr[15];\
582
                    lum[8]=ptr[16];lum[9]=ptr[18];lum[10]=ptr[20];lum[11]=ptr[22];\
583
                    cb[4]=ptr[17];cb[5]=ptr[21];\
584
                    cr[4]=ptr[19];cr[5]=ptr[23];\
585
                    lum[12]=ptr[24];lum[13]=ptr[26];lum[14]=ptr[28];lum[15]=ptr[30];\
586
                    cb[6]=ptr[25];cb[7]=ptr[29];\
587
                    cr[6]=ptr[27];cr[7]=ptr[31];
588
589
#define LINE_NO_UV \
590
                    lum[0]=ptr[0];lum[1]=ptr[2];lum[2]=ptr[4];lum[3]=ptr[6];\
591
                    lum[4]=ptr[8];lum[5]=ptr[10];lum[6]=ptr[12];lum[7]=ptr[14];\
592
                    lum[8]=ptr[16];lum[9]=ptr[18];lum[10]=ptr[20];lum[11]=ptr[22];\
593
                    lum[12]=ptr[24];lum[13]=ptr[26];lum[14]=ptr[28];lum[15]=ptr[30];
594
595
#define LINE_WITHUV_AVG \
596
                    sum=(ptr[0]+ptr[2]+1) >> 1;lum[0]=sum; \
597
                    sum=(ptr[4]+ptr[6]+1) >> 1;lum[1]=sum; \
598
                    sum=(ptr[1]+ptr[5]+1) >> 1;cb[0]=sum; \
599
                    sum=(ptr[3]+ptr[7]+1) >> 1;cr[0]=sum; \
600
                    sum=(ptr[8]+ptr[10]+1) >> 1;lum[2]=sum; \
601
                    sum=(ptr[12]+ptr[14]+1) >> 1;lum[3]=sum; \
602
                    sum=(ptr[9]+ptr[13]+1) >> 1;cb[1]=sum; \
603
                    sum=(ptr[11]+ptr[15]+1) >> 1;cr[1]=sum; \
604
                    sum=(ptr[16]+ptr[18]+1) >> 1;lum[4]=sum; \
605
                    sum=(ptr[20]+ptr[22]+1) >> 1;lum[5]=sum; \
606
                    sum=(ptr[17]+ptr[21]+1) >> 1;cb[2]=sum; \
607
                    sum=(ptr[19]+ptr[23]+1) >> 1;cr[2]=sum; \
608
                    sum=(ptr[24]+ptr[26]+1) >> 1;lum[6]=sum; \
609
                    sum=(ptr[28]+ptr[30]+1) >> 1;lum[7]=sum; \
610
                    sum=(ptr[25]+ptr[29]+1) >> 1;cb[3]=sum; \
611
                    sum=(ptr[27]+ptr[31]+1) >> 1;cr[3]=sum; 
612
613
#define LINE_NOUV_AVG \
614
                    sum=(ptr[0]+ptr[2]+1) >> 1;lum[0]=sum; \
615
                    sum=(ptr[4]+ptr[6]+1) >> 1;lum[1]=sum; \
616
                    sum=(ptr[8]+ptr[10]+1) >> 1;lum[2]=sum; \
617
                    sum=(ptr[12]+ptr[14]+1) >> 1;lum[3]=sum; \
618
                    sum=(ptr[16]+ptr[18]+1) >> 1;lum[4]=sum; \
619
                    sum=(ptr[20]+ptr[22]+1) >> 1;lum[5]=sum; \
620
                    sum=(ptr[24]+ptr[26]+1) >> 1;lum[6]=sum; \
621
                    sum=(ptr[28]+ptr[30]+1) >> 1;lum[7]=sum; 
622
623
#define DEINT_LINE_LUM(ptroff) \
624
                    sum=(-lum_m4[(ptroff)]+(lum_m3[(ptroff)]<<2)+(lum_m2[(ptroff)]<<1)+(lum_m1[(ptroff)]<<2)-lum[(ptroff)]); \
625
                    lum_m4[(ptroff)]=lum_m2[(ptroff)];\
626
                    lum_m2[(ptroff)]=cm[(sum+4)>>3];\
627
                    sum=(-lum_m4[(ptroff)+1]+(lum_m3[(ptroff)+1]<<2)+(lum_m2[(ptroff)+1]<<1)+(lum_m1[(ptroff)+1]<<2)-lum[(ptroff)+1]); \
628
                    lum_m4[(ptroff)+1]=lum_m2[(ptroff)+1];\
629
                    lum_m2[(ptroff)+1]=cm[(sum+4)>>3];\
630
                    sum=(-lum_m4[(ptroff)+2]+(lum_m3[(ptroff)+2]<<2)+(lum_m2[(ptroff)+2]<<1)+(lum_m1[(ptroff)+2]<<2)-lum[(ptroff)+2]); \
631
                    lum_m4[(ptroff)+2]=lum_m2[(ptroff)+2];\
632
                    lum_m2[(ptroff)+2]=cm[(sum+4)>>3];\
633
                    sum=(-lum_m4[(ptroff)+3]+(lum_m3[(ptroff)+3]<<2)+(lum_m2[(ptroff)+3]<<1)+(lum_m1[(ptroff)+3]<<2)-lum[(ptroff)+3]); \
634
                    lum_m4[(ptroff)+3]=lum_m2[(ptroff)+3];\
635
                    lum_m2[(ptroff)+3]=cm[(sum+4)>>3];
636
637
#endif
638
639
640 8949367e Fabrice Bellard
/* Read two fields separately. */
641
static int aiw_read_picture(VideoData *s, uint8_t *data)
642 3f9bff71 Michael Niedermayer
{
643 0c1a9eda Zdenek Kabelac
    uint8_t *ptr, *lum, *cb, *cr;
644 8949367e Fabrice Bellard
    int h;
645 3f9bff71 Michael Niedermayer
#ifndef HAVE_MMX
646 8949367e Fabrice Bellard
    int sum;
647 3f9bff71 Michael Niedermayer
#endif
648 0c1a9eda Zdenek Kabelac
    uint8_t* src = s->src_mem;
649
    uint8_t *ptrend = &src[s->width*2];
650 8949367e Fabrice Bellard
    lum=data;
651
    cb=&lum[s->width*s->height];
652
    cr=&cb[(s->width*s->height)/4];
653
    if (s->deint == 0 && s->halfw == 0) {
654
        while (read(s->fd,src,s->width*2) < 0) {
655
            usleep(100);
656
        }
657
        for (h = 0; h < s->height-2; h+=2) {
658 3f9bff71 Michael Niedermayer
            for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
659
                LINE_WITH_UV
660 8949367e Fabrice Bellard
                    }
661 3f9bff71 Michael Niedermayer
            read(s->fd,src,s->width*2);
662
            for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16) {
663
                LINE_NO_UV
664 8949367e Fabrice Bellard
                    }
665
            read(s->fd,src,s->width*2);
666
        }
667
        /*
668
         * Do last two lines
669
         */
670
        for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
671
            LINE_WITH_UV
672 3f9bff71 Michael Niedermayer
                }
673 8949367e Fabrice Bellard
        read(s->fd,src,s->width*2);
674
        for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16) {
675
            LINE_NO_UV
676
                }
677
        /* drop second field */
678
        while (read(s->fd,src,s->width*2) < 0) {
679
            usleep(100);
680
        }
681
        for (h = 0; h < s->height - 1; h++) {
682
            read(s->fd,src,s->width*2);
683
        }
684
    } else if (s->halfw == 1) {
685 3f9bff71 Michael Niedermayer
#ifdef HAVE_MMX
686 8949367e Fabrice Bellard
        mmx_t rounder;
687
        mmx_t masker;
688
        rounder.uw[0]=1;
689
        rounder.uw[1]=1;
690
        rounder.uw[2]=1;
691
        rounder.uw[3]=1;
692
        masker.ub[0]=0xff;
693
        masker.ub[1]=0;
694
        masker.ub[2]=0xff;
695
        masker.ub[3]=0;
696
        masker.ub[4]=0xff;
697
        masker.ub[5]=0;
698
        masker.ub[6]=0xff;
699
        masker.ub[7]=0;
700
        pxor_r2r(mm7,mm7);
701
        movq_m2r(rounder,mm6);
702 3f9bff71 Michael Niedermayer
#endif
703 8949367e Fabrice Bellard
        while (read(s->fd,src,s->width*4) < 0) {
704
            usleep(100);
705
        }
706
        ptrend = &src[s->width*4];
707
        for (h = 0; h < s->height-2; h+=2) {
708 3f9bff71 Michael Niedermayer
            for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8, cb+=4, cr+=4) {
709
                LINE_WITHUV_AVG
710 8949367e Fabrice Bellard
                    }
711 3f9bff71 Michael Niedermayer
            read(s->fd,src,s->width*4);
712
#ifdef HAVE_MMX
713
            movq_m2r(masker,mm5);
714
#endif
715
            for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8) {
716
                LINE_NOUV_AVG
717 8949367e Fabrice Bellard
                    }
718
            read(s->fd,src,s->width*4);
719
        }
720
        /*
721
 * Do last two lines
722
 */
723
        for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8, cb+=4, cr+=4) {
724
            LINE_WITHUV_AVG
725
                }
726
        read(s->fd,src,s->width*4);
727
#ifdef HAVE_MMX
728
        movq_m2r(masker,mm5);
729
#endif
730
        for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8) {
731
            LINE_NOUV_AVG
732
                }
733
        /* drop second field */
734
        while (read(s->fd,src,s->width*4) < 0) {
735
            usleep(100);
736
        }
737
        for (h = 0; h < s->height - 1; h++) {
738
            read(s->fd,src,s->width*4);
739
        }
740
    } else {
741 0c1a9eda Zdenek Kabelac
        uint8_t *lum_m1, *lum_m2, *lum_m3, *lum_m4;
742 3f9bff71 Michael Niedermayer
#ifdef HAVE_MMX
743 8949367e Fabrice Bellard
        mmx_t rounder;
744
        rounder.uw[0]=4;
745
        rounder.uw[1]=4;
746
        rounder.uw[2]=4;
747
        rounder.uw[3]=4;
748
        movq_m2r(rounder,mm6);
749
        pxor_r2r(mm7,mm7);
750 3f9bff71 Michael Niedermayer
#else
751 0c1a9eda Zdenek Kabelac
        uint8_t *cm = cropTbl + MAX_NEG_CROP;
752 3f9bff71 Michael Niedermayer
#endif
753
754 8949367e Fabrice Bellard
        /* read two fields and deinterlace them */
755
        while (read(s->fd,src,s->width*2) < 0) {
756
            usleep(100);
757
        }
758
        for (h = 0; h < (s->height/2)-2; h+=2) {
759 3f9bff71 Michael Niedermayer
            for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
760
                LINE_WITH_UV
761 8949367e Fabrice Bellard
                    }
762 3f9bff71 Michael Niedermayer
            read(s->fd,src,s->width*2);
763 8949367e Fabrice Bellard
            /* skip a luminance line - will be filled in later */
764
            lum += s->width;
765 3f9bff71 Michael Niedermayer
            for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
766
                LINE_WITH_UV
767 8949367e Fabrice Bellard
                    }
768
            /* skip a luminance line - will be filled in later */
769
            lum += s->width;
770
            read(s->fd,src,s->width*2);
771
        }
772
        /*
773
 * Do last two lines
774
 */
775
        for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
776
            LINE_WITH_UV
777
                }
778
        /* skip a luminance line - will be filled in later */
779
        lum += s->width;
780
        read(s->fd,src,s->width*2);
781
        for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
782
            LINE_WITH_UV
783
                }
784
        /*
785 3f9bff71 Michael Niedermayer
 *
786
 * SECOND FIELD
787
 *
788
 */
789 8949367e Fabrice Bellard
        lum=&data[s->width];
790
        while (read(s->fd,src,s->width*2) < 0) {
791
            usleep(10);
792
        }
793
        /* First (and last) two lines not interlaced */
794
        for (h = 0; h < 2; h++) {
795
            for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16) {
796
                LINE_NO_UV
797
                    }
798
            read(s->fd,src,s->width*2);
799
            /* skip a luminance line */
800
            lum += s->width;
801
        }
802
        lum_m1=&lum[-s->width];
803
        lum_m2=&lum_m1[-s->width];
804
        lum_m3=&lum_m2[-s->width];
805
        memmove(s->lum_m4_mem,&lum_m3[-s->width],s->width);
806
        for (; h < (s->height/2)-1; h++) {
807
            lum_m4=s->lum_m4_mem;
808
            for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16,lum_m1+=16,lum_m2+=16,lum_m3+=16,lum_m4+=16) {
809
                LINE_NO_UV
810 3f9bff71 Michael Niedermayer
811
                    DEINT_LINE_LUM(0)
812
                    DEINT_LINE_LUM(4)
813
                    DEINT_LINE_LUM(8)
814
                    DEINT_LINE_LUM(12)
815 8949367e Fabrice Bellard
                    }
816
            read(s->fd,src,s->width*2);
817
            /* skip a luminance line */
818
            lum += s->width;
819
            lum_m1 += s->width;
820
            lum_m2 += s->width;
821
            lum_m3 += s->width;
822
            //                lum_m4 += s->width;
823
        }
824
        /*
825 3f9bff71 Michael Niedermayer
 * Do last line
826
 */
827 8949367e Fabrice Bellard
        lum_m4=s->lum_m4_mem;
828
        for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, lum_m1+=16, lum_m2+=16, lum_m3+=16, lum_m4+=16) {
829
            LINE_NO_UV
830 3f9bff71 Michael Niedermayer
831
                DEINT_LINE_LUM(0)
832
                DEINT_LINE_LUM(4)
833
                DEINT_LINE_LUM(8)
834
                DEINT_LINE_LUM(12)
835 8949367e Fabrice Bellard
                }
836
    }
837 3f9bff71 Michael Niedermayer
#ifdef HAVE_MMX
838 8949367e Fabrice Bellard
    emms();
839 3f9bff71 Michael Niedermayer
#endif
840
    return s->frame_size;
841
}
842
843 8949367e Fabrice Bellard
static int aiw_close(VideoData *s)
844 3f9bff71 Michael Niedermayer
{
845 8949367e Fabrice Bellard
    av_freep(&s->lum_m4_mem);
846
    av_freep(&s->src_mem);
847 3f9bff71 Michael Niedermayer
    return 0;
848
}
849
850 c9a65ca8 Fabrice Bellard
int video_grab_init(void)
851
{
852
    av_register_input_format(&video_grab_device_format);
853
    return 0;
854
}