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