Mercurial > libavformat.hg
annotate grab.c @ 79:8bdaec4e3e4b libavformat
fixing frame_rate accuracy
author | michaelni |
---|---|
date | Sat, 08 Mar 2003 14:29:12 +0000 |
parents | a58a8a53eb46 |
children | 25062c9b1f86 |
rev | line source |
---|---|
0 | 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; | |
65 | 34 int64_t time_frame; |
0 | 35 int frame_size; |
36 struct video_capability video_cap; | |
37 struct video_audio audio_saved; | |
65 | 38 uint8_t *video_buf; |
0 | 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; | |
65 | 48 uint8_t *src_mem; |
49 uint8_t *lum_m4_mem; | |
0 | 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 static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap) | |
57 { | |
58 VideoData *s = s1->priv_data; | |
59 AVStream *st; | |
60 int width, height; | |
61 int video_fd, frame_size; | |
62 int ret, frame_rate; | |
63 int desired_palette; | |
64 struct video_audio audio; | |
30
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
27
diff
changeset
|
65 const char *video_device; |
0 | 66 |
67 if (!ap || ap->width <= 0 || ap->height <= 0 || ap->frame_rate <= 0) | |
68 return -1; | |
69 | |
70 width = ap->width; | |
71 height = ap->height; | |
72 frame_rate = ap->frame_rate; | |
73 | |
74 st = av_new_stream(s1, 0); | |
75 if (!st) | |
76 return -ENOMEM; | |
77 | |
78 s->width = width; | |
79 s->height = height; | |
80 s->frame_rate = frame_rate; | |
81 | |
30
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
27
diff
changeset
|
82 video_device = ap->device; |
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
27
diff
changeset
|
83 if (!video_device) |
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
27
diff
changeset
|
84 video_device = "/dev/video"; |
27
fcdea3df94fe
dv patch by Max Krasnyansky (maxk at qualcomm dot com)
bellard
parents:
0
diff
changeset
|
85 video_fd = open(video_device, O_RDWR); |
0 | 86 if (video_fd < 0) { |
27
fcdea3df94fe
dv patch by Max Krasnyansky (maxk at qualcomm dot com)
bellard
parents:
0
diff
changeset
|
87 perror(video_device); |
0 | 88 goto fail; |
89 } | |
90 | |
91 if (ioctl(video_fd,VIDIOCGCAP, &s->video_cap) < 0) { | |
92 perror("VIDIOCGCAP"); | |
93 goto fail; | |
94 } | |
95 | |
96 if (!(s->video_cap.type & VID_TYPE_CAPTURE)) { | |
97 fprintf(stderr, "Fatal: grab device does not handle capture\n"); | |
98 goto fail; | |
99 } | |
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 } | |
109 | |
110 /* unmute audio */ | |
111 audio.audio = 0; | |
112 ioctl(video_fd, VIDIOCGAUDIO, &audio); | |
113 memcpy(&s->audio_saved, &audio, sizeof(audio)); | |
114 audio.flags &= ~VIDEO_AUDIO_MUTE; | |
115 ioctl(video_fd, VIDIOCSAUDIO, &audio); | |
116 | |
117 ret = ioctl(video_fd,VIDIOCGMBUF,&s->gb_buffers); | |
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 */ | |
143 pict.palette = desired_palette; | |
144 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) { | |
145 pict.palette=VIDEO_PALETTE_YUV420P; | |
146 ret = ioctl(video_fd, VIDIOCSPICT, &pict); | |
147 if (ret < 0) { | |
148 pict.palette=VIDEO_PALETTE_YUV422; | |
149 ret = ioctl(video_fd, VIDIOCSPICT, &pict); | |
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 } | |
156 } | |
157 } | |
158 | |
159 s->frame_format = pict.palette; | |
160 | |
161 val = 1; | |
162 ioctl(video_fd, VIDIOCCAPTURE, &val); | |
163 | |
164 s->time_frame = av_gettime(); | |
165 s->use_mmap = 0; | |
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 } | |
176 } else { | |
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) { | |
179 perror("mmap"); | |
180 goto fail; | |
181 } | |
182 s->gb_frame = 0; | |
183 s->time_frame = av_gettime(); | |
184 | |
185 /* start to grab the first frame */ | |
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; | |
190 | |
191 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf)) < 0) { | |
192 s->gb_buf.format = VIDEO_PALETTE_YUV420P; | |
193 | |
194 ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf); | |
195 if (ret < 0 && errno != EAGAIN) { | |
196 /* try YUV422 */ | |
197 s->gb_buf.format = VIDEO_PALETTE_YUV422; | |
198 | |
199 ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf); | |
200 if (ret < 0 && errno != EAGAIN) { | |
201 /* try RGB24 */ | |
202 s->gb_buf.format = VIDEO_PALETTE_RGB24; | |
203 ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf); | |
204 } | |
205 } | |
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 } | |
216 s->frame_format = s->gb_buf.format; | |
217 s->use_mmap = 1; | |
218 } | |
219 | |
220 switch(s->frame_format) { | |
221 case VIDEO_PALETTE_YUV420P: | |
222 frame_size = (width * height * 3) / 2; | |
223 st->codec.pix_fmt = PIX_FMT_YUV420P; | |
224 break; | |
225 case VIDEO_PALETTE_YUV422: | |
226 frame_size = width * height * 2; | |
227 st->codec.pix_fmt = PIX_FMT_YUV422; | |
228 break; | |
229 case VIDEO_PALETTE_RGB24: | |
230 frame_size = width * height * 3; | |
231 st->codec.pix_fmt = PIX_FMT_BGR24; /* NOTE: v4l uses BGR24, not RGB24 ! */ | |
232 break; | |
233 default: | |
234 goto fail; | |
235 } | |
236 s->fd = video_fd; | |
237 s->frame_size = frame_size; | |
238 | |
239 st->codec.codec_type = CODEC_TYPE_VIDEO; | |
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; | |
244 | |
245 av_set_pts_info(s1, 48, 1, 1000000); /* 48 bits pts in us */ | |
246 | |
247 return 0; | |
248 fail: | |
249 if (video_fd >= 0) | |
250 close(video_fd); | |
251 av_free(st); | |
252 return -EIO; | |
253 } | |
254 | |
65 | 255 static int v4l_mm_read_picture(VideoData *s, uint8_t *buf) |
0 | 256 { |
65 | 257 uint8_t *ptr; |
0 | 258 |
259 /* Setup to capture the next frame */ | |
260 s->gb_buf.frame = (s->gb_frame + 1) % s->gb_buffers.frames; | |
261 if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) { | |
262 if (errno == EAGAIN) | |
263 fprintf(stderr,"Cannot Sync\n"); | |
264 else | |
265 perror("VIDIOCMCAPTURE"); | |
266 return -EIO; | |
267 } | |
268 | |
269 while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 && | |
270 (errno == EAGAIN || errno == EINTR)); | |
271 | |
272 ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame]; | |
273 memcpy(buf, ptr, s->frame_size); | |
274 | |
275 /* This is now the grabbing frame */ | |
276 s->gb_frame = s->gb_buf.frame; | |
277 | |
278 return s->frame_size; | |
279 } | |
280 | |
281 static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt) | |
282 { | |
283 VideoData *s = s1->priv_data; | |
65 | 284 int64_t curtime, delay; |
0 | 285 struct timespec ts; |
65 | 286 int64_t per_frame = (int64_t_C(1000000) * FRAME_RATE_BASE) / s->frame_rate; |
0 | 287 |
288 /* Calculate the time of the next frame */ | |
289 s->time_frame += per_frame; | |
290 | |
291 /* wait based on the frame rate */ | |
292 for(;;) { | |
293 curtime = av_gettime(); | |
294 delay = s->time_frame - curtime; | |
295 if (delay <= 0) { | |
296 if (delay < -per_frame) { | |
297 /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */ | |
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 /* read one frame */ | |
313 if (s->aiw_enabled) { | |
314 return aiw_read_picture(s, pkt->data); | |
315 } else if (s->use_mmap) { | |
316 return v4l_mm_read_picture(s, pkt->data); | |
317 } else { | |
318 if (read(s->fd, pkt->data, pkt->size) != pkt->size) | |
319 return -EIO; | |
320 return s->frame_size; | |
321 } | |
322 } | |
323 | |
324 static int grab_read_close(AVFormatContext *s1) | |
325 { | |
326 VideoData *s = s1->priv_data; | |
327 | |
328 if (s->aiw_enabled) | |
329 aiw_close(s); | |
330 | |
331 if (s->use_mmap) | |
332 munmap(s->video_buf, s->gb_buffers.size); | |
333 | |
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); | |
338 | |
339 close(s->fd); | |
340 return 0; | |
341 } | |
342 | |
343 static AVInputFormat video_grab_device_format = { | |
27
fcdea3df94fe
dv patch by Max Krasnyansky (maxk at qualcomm dot com)
bellard
parents:
0
diff
changeset
|
344 "video4linux", |
0 | 345 "video grab", |
346 sizeof(VideoData), | |
347 NULL, | |
348 grab_read_header, | |
349 grab_read_packet, | |
350 grab_read_close, | |
351 .flags = AVFMT_NOFILE, | |
352 }; | |
353 | |
354 /* All in Wonder specific stuff */ | |
355 /* XXX: remove and merge in libavcodec/imgconvert.c */ | |
356 | |
357 static int aiw_init(VideoData *s) | |
358 { | |
359 int width, height; | |
360 | |
361 width = s->width; | |
362 height = s->height; | |
363 | |
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; | |
372 } else { | |
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); | |
378 goto fail; | |
379 } | |
380 | |
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); | |
389 if (!s->lum_m4_mem) | |
390 goto fail; | |
391 return 0; | |
392 fail: | |
393 av_freep(&s->src_mem); | |
394 av_freep(&s->lum_m4_mem); | |
395 return -1; | |
396 } | |
397 | |
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 | |
617 /* Read two fields separately. */ | |
618 static int aiw_read_picture(VideoData *s, uint8_t *data) | |
619 { | |
65 | 620 uint8_t *ptr, *lum, *cb, *cr; |
0 | 621 int h; |
622 #ifndef HAVE_MMX | |
623 int sum; | |
624 #endif | |
65 | 625 uint8_t* src = s->src_mem; |
626 uint8_t *ptrend = &src[s->width*2]; | |
0 | 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) { | |
635 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) { | |
636 LINE_WITH_UV | |
637 } | |
638 read(s->fd,src,s->width*2); | |
639 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16) { | |
640 LINE_NO_UV | |
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 | |
649 } | |
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) { | |
662 #ifdef HAVE_MMX | |
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); | |
679 #endif | |
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) { | |
685 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8, cb+=4, cr+=4) { | |
686 LINE_WITHUV_AVG | |
687 } | |
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 | |
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 { | |
65 | 718 uint8_t *lum_m1, *lum_m2, *lum_m3, *lum_m4; |
0 | 719 #ifdef HAVE_MMX |
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); | |
727 #else | |
65 | 728 uint8_t *cm = cropTbl + MAX_NEG_CROP; |
0 | 729 #endif |
730 | |
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) { | |
736 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) { | |
737 LINE_WITH_UV | |
738 } | |
739 read(s->fd,src,s->width*2); | |
740 /* skip a luminance line - will be filled in later */ | |
741 lum += s->width; | |
742 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) { | |
743 LINE_WITH_UV | |
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 /* | |
762 * | |
763 * SECOND FIELD | |
764 * | |
765 */ | |
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 | |
787 | |
788 DEINT_LINE_LUM(0) | |
789 DEINT_LINE_LUM(4) | |
790 DEINT_LINE_LUM(8) | |
791 DEINT_LINE_LUM(12) | |
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 /* | |
802 * Do last line | |
803 */ | |
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 | |
807 | |
808 DEINT_LINE_LUM(0) | |
809 DEINT_LINE_LUM(4) | |
810 DEINT_LINE_LUM(8) | |
811 DEINT_LINE_LUM(12) | |
812 } | |
813 } | |
814 #ifdef HAVE_MMX | |
815 emms(); | |
816 #endif | |
817 return s->frame_size; | |
818 } | |
819 | |
820 static int aiw_close(VideoData *s) | |
821 { | |
822 av_freep(&s->lum_m4_mem); | |
823 av_freep(&s->src_mem); | |
824 return 0; | |
825 } | |
826 | |
827 int video_grab_init(void) | |
828 { | |
829 av_register_input_format(&video_grab_device_format); | |
830 return 0; | |
831 } |