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