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