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