support for armv5tel (XScale used in iPAQs)
[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;
14bea432 34 int frame_rate_base;
0c1a9eda 35 int64_t time_frame;
4972b26f 36 int frame_size;
8949367e
FB
37 struct video_capability video_cap;
38 struct video_audio audio_saved;
0c1a9eda 39 uint8_t *video_buf;
8949367e
FB
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;
0c1a9eda
ZK
49 uint8_t *src_mem;
50 uint8_t *lum_m4_mem;
de6d9b64
FB
51} VideoData;
52
8949367e
FB
53static int aiw_init(VideoData *s);
54static int aiw_read_picture(VideoData *s, uint8_t *data);
55static int aiw_close(VideoData *s);
de6d9b64 56
4972b26f 57static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
de6d9b64 58{
c9a65ca8 59 VideoData *s = s1->priv_data;
4972b26f 60 AVStream *st;
de6d9b64 61 int width, height;
de6d9b64 62 int video_fd, frame_size;
14bea432 63 int ret, frame_rate, frame_rate_base;
4606ac8d 64 int desired_palette;
8949367e 65 struct video_audio audio;
7f172339 66 const char *video_device;
4972b26f
FB
67
68 if (!ap || ap->width <= 0 || ap->height <= 0 || ap->frame_rate <= 0)
69 return -1;
de6d9b64 70
4972b26f
FB
71 width = ap->width;
72 height = ap->height;
14bea432
MN
73 frame_rate = ap->frame_rate;
74 frame_rate_base = ap->frame_rate_base;
4972b26f 75
c9a65ca8
FB
76 st = av_new_stream(s1, 0);
77 if (!st)
4972b26f 78 return -ENOMEM;
4972b26f
FB
79
80 s->width = width;
81 s->height = height;
14bea432
MN
82 s->frame_rate = frame_rate;
83 s->frame_rate_base = frame_rate_base;
de6d9b64 84
7f172339
FB
85 video_device = ap->device;
86 if (!video_device)
87 video_device = "/dev/video";
8aa3ee32 88 video_fd = open(video_device, O_RDWR);
de6d9b64 89 if (video_fd < 0) {
8aa3ee32 90 perror(video_device);
4972b26f 91 goto fail;
de6d9b64
FB
92 }
93
8949367e 94 if (ioctl(video_fd,VIDIOCGCAP, &s->video_cap) < 0) {
de6d9b64
FB
95 perror("VIDIOCGCAP");
96 goto fail;
97 }
98
8949367e 99 if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
de6d9b64
FB
100 fprintf(stderr, "Fatal: grab device does not handle capture\n");
101 goto fail;
102 }
4606ac8d
ZK
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 }
de6d9b64
FB
112
113 /* unmute audio */
8949367e 114 audio.audio = 0;
de6d9b64 115 ioctl(video_fd, VIDIOCGAUDIO, &audio);
8949367e 116 memcpy(&s->audio_saved, &audio, sizeof(audio));
de6d9b64
FB
117 audio.flags &= ~VIDEO_AUDIO_MUTE;
118 ioctl(video_fd, VIDIOCSAUDIO, &audio);
119
8949367e 120 ret = ioctl(video_fd,VIDIOCGMBUF,&s->gb_buffers);
de6d9b64
FB
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 */
4606ac8d
ZK
146 pict.palette = desired_palette;
147 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
148 pict.palette=VIDEO_PALETTE_YUV420P;
de6d9b64
FB
149 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
150 if (ret < 0) {
4606ac8d 151 pict.palette=VIDEO_PALETTE_YUV422;
de6d9b64 152 ret = ioctl(video_fd, VIDIOCSPICT, &pict);
4606ac8d
ZK
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 }
de6d9b64
FB
159 }
160 }
161
162 s->frame_format = pict.palette;
163
164 val = 1;
165 ioctl(video_fd, VIDIOCCAPTURE, &val);
166
a11bf0bd 167 s->time_frame = av_gettime();
de6d9b64 168 s->use_mmap = 0;
8949367e
FB
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 }
de6d9b64 179 } else {
8949367e
FB
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) {
de6d9b64
FB
182 perror("mmap");
183 goto fail;
184 }
8949367e 185 s->gb_frame = 0;
a11bf0bd 186 s->time_frame = av_gettime();
de6d9b64
FB
187
188 /* start to grab the first frame */
8949367e
FB
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;
4606ac8d 193
8949367e
FB
194 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf)) < 0) {
195 s->gb_buf.format = VIDEO_PALETTE_YUV420P;
de6d9b64 196
8949367e 197 ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
4606ac8d
ZK
198 if (ret < 0 && errno != EAGAIN) {
199 /* try YUV422 */
8949367e 200 s->gb_buf.format = VIDEO_PALETTE_YUV422;
4606ac8d 201
8949367e 202 ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
4606ac8d
ZK
203 if (ret < 0 && errno != EAGAIN) {
204 /* try RGB24 */
8949367e
FB
205 s->gb_buf.format = VIDEO_PALETTE_RGB24;
206 ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
4606ac8d
ZK
207 }
208 }
de6d9b64
FB
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 }
8949367e 219 s->frame_format = s->gb_buf.format;
de6d9b64
FB
220 s->use_mmap = 1;
221 }
222
223 switch(s->frame_format) {
224 case VIDEO_PALETTE_YUV420P:
225 frame_size = (width * height * 3) / 2;
4972b26f 226 st->codec.pix_fmt = PIX_FMT_YUV420P;
de6d9b64
FB
227 break;
228 case VIDEO_PALETTE_YUV422:
229 frame_size = width * height * 2;
4972b26f 230 st->codec.pix_fmt = PIX_FMT_YUV422;
de6d9b64
FB
231 break;
232 case VIDEO_PALETTE_RGB24:
233 frame_size = width * height * 3;
4972b26f 234 st->codec.pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */
de6d9b64
FB
235 break;
236 default:
237 goto fail;
238 }
239 s->fd = video_fd;
4972b26f
FB
240 s->frame_size = frame_size;
241
28c66901 242 st->codec.codec_type = CODEC_TYPE_VIDEO;
4972b26f
FB
243 st->codec.codec_id = CODEC_ID_RAWVIDEO;
244 st->codec.width = width;
245 st->codec.height = height;
14bea432
MN
246 st->codec.frame_rate = frame_rate;
247 st->codec.frame_rate_base = frame_rate_base;
86f2b9d0 248
45dd5c69
FB
249 av_set_pts_info(s1, 48, 1, 1000000); /* 48 bits pts in us */
250
de6d9b64
FB
251 return 0;
252 fail:
4972b26f
FB
253 if (video_fd >= 0)
254 close(video_fd);
1ea4f593 255 av_free(st);
de6d9b64
FB
256 return -EIO;
257}
258
0c1a9eda 259static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
de6d9b64 260{
0c1a9eda 261 uint8_t *ptr;
de6d9b64 262
4606ac8d 263 /* Setup to capture the next frame */
8949367e
FB
264 s->gb_buf.frame = (s->gb_frame + 1) % s->gb_buffers.frames;
265 if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
28c66901
PG
266 if (errno == EAGAIN)
267 fprintf(stderr,"Cannot Sync\n");
268 else
de6d9b64 269 perror("VIDIOCMCAPTURE");
28c66901 270 return -EIO;
de6d9b64 271 }
4606ac8d 272
8949367e 273 while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
de6d9b64
FB
274 (errno == EAGAIN || errno == EINTR));
275
8949367e 276 ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
4972b26f 277 memcpy(buf, ptr, s->frame_size);
4606ac8d
ZK
278
279 /* This is now the grabbing frame */
8949367e 280 s->gb_frame = s->gb_buf.frame;
4606ac8d 281
4972b26f 282 return s->frame_size;
de6d9b64
FB
283}
284
4972b26f 285static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
de6d9b64 286{
4972b26f 287 VideoData *s = s1->priv_data;
0c1a9eda 288 int64_t curtime, delay;
4972b26f 289 struct timespec ts;
14bea432 290 int64_t per_frame = (int64_t_C(1000000) * s->frame_rate_base) / s->frame_rate;
4606ac8d
ZK
291
292 /* Calculate the time of the next frame */
293 s->time_frame += per_frame;
de6d9b64
FB
294
295 /* wait based on the frame rate */
45dd5c69 296 for(;;) {
a11bf0bd 297 curtime = av_gettime();
4972b26f 298 delay = s->time_frame - curtime;
4606ac8d
ZK
299 if (delay <= 0) {
300 if (delay < -per_frame) {
301 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
4606ac8d
ZK
302 s->time_frame += per_frame;
303 }
4972b26f 304 break;
4606ac8d 305 }
4972b26f
FB
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;
de6d9b64 313
45dd5c69
FB
314 pkt->pts = curtime & ((1LL << 48) - 1);
315
de6d9b64 316 /* read one frame */
8949367e
FB
317 if (s->aiw_enabled) {
318 return aiw_read_picture(s, pkt->data);
319 } else if (s->use_mmap) {
4972b26f 320 return v4l_mm_read_picture(s, pkt->data);
de6d9b64 321 } else {
4972b26f 322 if (read(s->fd, pkt->data, pkt->size) != pkt->size)
de6d9b64 323 return -EIO;
4972b26f 324 return s->frame_size;
de6d9b64
FB
325 }
326}
327
4972b26f 328static int grab_read_close(AVFormatContext *s1)
de6d9b64 329{
4972b26f 330 VideoData *s = s1->priv_data;
e61efa24 331
8949367e
FB
332 if (s->aiw_enabled)
333 aiw_close(s);
334
e61efa24 335 if (s->use_mmap)
8949367e 336 munmap(s->video_buf, s->gb_buffers.size);
e61efa24 337
8949367e
FB
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);
5a56c87c 342
de6d9b64 343 close(s->fd);
de6d9b64
FB
344 return 0;
345}
346
c18a2692 347static AVInputFormat video_grab_device_format = {
8aa3ee32 348 "video4linux",
4972b26f 349 "video grab",
c9a65ca8 350 sizeof(VideoData),
4972b26f 351 NULL,
4972b26f
FB
352 grab_read_header,
353 grab_read_packet,
354 grab_read_close,
bb76a117 355 .flags = AVFMT_NOFILE,
de6d9b64 356};
c9a65ca8 357
8949367e
FB
358/* All in Wonder specific stuff */
359/* XXX: remove and merge in libavcodec/imgconvert.c */
3f9bff71 360
8949367e 361static int aiw_init(VideoData *s)
3f9bff71 362{
3f9bff71 363 int width, height;
3f9bff71 364
8949367e
FB
365 width = s->width;
366 height = s->height;
3f9bff71 367
8949367e
FB
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;
3f9bff71 376 } else {
8949367e
FB
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);
3f9bff71
MN
382 goto fail;
383 }
384
3f9bff71
MN
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);
8949367e 393 if (!s->lum_m4_mem)
3f9bff71 394 goto fail;
3f9bff71
MN
395 return 0;
396 fail:
8949367e
FB
397 av_freep(&s->src_mem);
398 av_freep(&s->lum_m4_mem);
399 return -1;
3f9bff71
MN
400}
401
3f9bff71
MN
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
8949367e
FB
621/* Read two fields separately. */
622static int aiw_read_picture(VideoData *s, uint8_t *data)
3f9bff71 623{
0c1a9eda 624 uint8_t *ptr, *lum, *cb, *cr;
8949367e 625 int h;
3f9bff71 626#ifndef HAVE_MMX
8949367e 627 int sum;
3f9bff71 628#endif
0c1a9eda
ZK
629 uint8_t* src = s->src_mem;
630 uint8_t *ptrend = &src[s->width*2];
8949367e
FB
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) {
3f9bff71
MN
639 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
640 LINE_WITH_UV
8949367e 641 }
3f9bff71
MN
642 read(s->fd,src,s->width*2);
643 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16) {
644 LINE_NO_UV
8949367e
FB
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
3f9bff71 653 }
8949367e
FB
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) {
3f9bff71 666#ifdef HAVE_MMX
8949367e
FB
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);
3f9bff71 683#endif
8949367e
FB
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) {
3f9bff71
MN
689 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8, cb+=4, cr+=4) {
690 LINE_WITHUV_AVG
8949367e 691 }
3f9bff71
MN
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
8949367e
FB
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 {
0c1a9eda 722 uint8_t *lum_m1, *lum_m2, *lum_m3, *lum_m4;
3f9bff71 723#ifdef HAVE_MMX
8949367e
FB
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);
3f9bff71 731#else
0c1a9eda 732 uint8_t *cm = cropTbl + MAX_NEG_CROP;
3f9bff71
MN
733#endif
734
8949367e
FB
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) {
3f9bff71
MN
740 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
741 LINE_WITH_UV
8949367e 742 }
3f9bff71 743 read(s->fd,src,s->width*2);
8949367e
FB
744 /* skip a luminance line - will be filled in later */
745 lum += s->width;
3f9bff71
MN
746 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
747 LINE_WITH_UV
8949367e
FB
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 /*
3f9bff71
MN
766 *
767 * SECOND FIELD
768 *
769 */
8949367e
FB
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
3f9bff71
MN
791
792 DEINT_LINE_LUM(0)
793 DEINT_LINE_LUM(4)
794 DEINT_LINE_LUM(8)
795 DEINT_LINE_LUM(12)
8949367e
FB
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 /*
3f9bff71
MN
806 * Do last line
807 */
8949367e
FB
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
3f9bff71
MN
811
812 DEINT_LINE_LUM(0)
813 DEINT_LINE_LUM(4)
814 DEINT_LINE_LUM(8)
815 DEINT_LINE_LUM(12)
8949367e
FB
816 }
817 }
3f9bff71 818#ifdef HAVE_MMX
8949367e 819 emms();
3f9bff71 820#endif
3f9bff71
MN
821 return s->frame_size;
822}
823
8949367e 824static int aiw_close(VideoData *s)
3f9bff71 825{
8949367e
FB
826 av_freep(&s->lum_m4_mem);
827 av_freep(&s->src_mem);
3f9bff71
MN
828 return 0;
829}
830
c9a65ca8
FB
831int video_grab_init(void)
832{
833 av_register_input_format(&video_grab_device_format);
834 return 0;
835}