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