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