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