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