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