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