00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avformat.h"
00022 #include "dsputil.h"
00023 #include <unistd.h>
00024 #include <fcntl.h>
00025 #include <sys/ioctl.h>
00026 #include <sys/mman.h>
00027 #include <sys/time.h>
00028 #define _LINUX_TIME_H 1
00029 #include <linux/videodev.h>
00030 #include <time.h>
00031
00032 typedef struct {
00033 int fd;
00034 int frame_format;
00035 int use_mmap;
00036 int width, height;
00037 int frame_rate;
00038 int frame_rate_base;
00039 int64_t time_frame;
00040 int frame_size;
00041 struct video_capability video_cap;
00042 struct video_audio audio_saved;
00043 uint8_t *video_buf;
00044 struct video_mbuf gb_buffers;
00045 struct video_mmap gb_buf;
00046 int gb_frame;
00047
00048
00049
00050 int aiw_enabled;
00051 int deint;
00052 int halfw;
00053 uint8_t *src_mem;
00054 uint8_t *lum_m4_mem;
00055 } VideoData;
00056
00057 struct {
00058 int palette;
00059 int depth;
00060 enum PixelFormat pix_fmt;
00061 } video_formats [] = {
00062 {.palette = VIDEO_PALETTE_YUV420P, .depth = 12, .pix_fmt = PIX_FMT_YUV420P },
00063 {.palette = VIDEO_PALETTE_YUV422, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
00064 {.palette = VIDEO_PALETTE_UYVY, .depth = 16, .pix_fmt = PIX_FMT_UYVY422 },
00065 {.palette = VIDEO_PALETTE_YUYV, .depth = 16, .pix_fmt = PIX_FMT_YUYV422 },
00066
00067 {.palette = VIDEO_PALETTE_RGB24, .depth = 24, .pix_fmt = PIX_FMT_BGR24 },
00068 {.palette = VIDEO_PALETTE_RGB565, .depth = 16, .pix_fmt = PIX_FMT_BGR565 },
00069 {.palette = VIDEO_PALETTE_GREY, .depth = 8, .pix_fmt = PIX_FMT_GRAY8 },
00070 };
00071
00072
00073 static int aiw_init(VideoData *s);
00074 static int aiw_read_picture(VideoData *s, uint8_t *data);
00075 static int aiw_close(VideoData *s);
00076
00077 static int grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
00078 {
00079 VideoData *s = s1->priv_data;
00080 AVStream *st;
00081 int width, height;
00082 int video_fd, frame_size;
00083 int ret, frame_rate, frame_rate_base;
00084 int desired_palette, desired_depth;
00085 struct video_tuner tuner;
00086 struct video_audio audio;
00087 struct video_picture pict;
00088 int j;
00089 int vformat_num = sizeof(video_formats) / sizeof(video_formats[0]);
00090
00091 if (ap->width <= 0 || ap->height <= 0 || ap->time_base.den <= 0) {
00092 av_log(s1, AV_LOG_ERROR, "Bad capture size (%dx%d) or wrong time base (%d)\n",
00093 ap->width, ap->height, ap->time_base.den);
00094
00095 return -1;
00096 }
00097
00098 width = ap->width;
00099 height = ap->height;
00100 frame_rate = ap->time_base.den;
00101 frame_rate_base = ap->time_base.num;
00102
00103 if((unsigned)width > 32767 || (unsigned)height > 32767) {
00104 av_log(s1, AV_LOG_ERROR, "Capture size is out of range: %dx%d\n",
00105 width, height);
00106
00107 return -1;
00108 }
00109
00110 st = av_new_stream(s1, 0);
00111 if (!st)
00112 return AVERROR(ENOMEM);
00113 av_set_pts_info(st, 64, 1, 1000000);
00114
00115 s->width = width;
00116 s->height = height;
00117 s->frame_rate = frame_rate;
00118 s->frame_rate_base = frame_rate_base;
00119
00120 video_fd = open(s1->filename, O_RDWR);
00121 if (video_fd < 0) {
00122 perror(s1->filename);
00123 goto fail;
00124 }
00125
00126 if (ioctl(video_fd,VIDIOCGCAP, &s->video_cap) < 0) {
00127 perror("VIDIOCGCAP");
00128 goto fail;
00129 }
00130
00131 if (!(s->video_cap.type & VID_TYPE_CAPTURE)) {
00132 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not handle capture\n");
00133 goto fail;
00134 }
00135
00136 desired_palette = -1;
00137 desired_depth = -1;
00138 for (j = 0; j < vformat_num; j++) {
00139 if (ap->pix_fmt == video_formats[j].pix_fmt) {
00140 desired_palette = video_formats[j].palette;
00141 desired_depth = video_formats[j].depth;
00142 break;
00143 }
00144 }
00145
00146
00147 if (ap->standard && !ioctl(video_fd, VIDIOCGTUNER, &tuner)) {
00148 if (!strcasecmp(ap->standard, "pal"))
00149 tuner.mode = VIDEO_MODE_PAL;
00150 else if (!strcasecmp(ap->standard, "secam"))
00151 tuner.mode = VIDEO_MODE_SECAM;
00152 else
00153 tuner.mode = VIDEO_MODE_NTSC;
00154 ioctl(video_fd, VIDIOCSTUNER, &tuner);
00155 }
00156
00157
00158 audio.audio = 0;
00159 ioctl(video_fd, VIDIOCGAUDIO, &audio);
00160 memcpy(&s->audio_saved, &audio, sizeof(audio));
00161 audio.flags &= ~VIDEO_AUDIO_MUTE;
00162 ioctl(video_fd, VIDIOCSAUDIO, &audio);
00163
00164 ioctl(video_fd, VIDIOCGPICT, &pict);
00165 #if 0
00166 printf("v4l: colour=%d hue=%d brightness=%d constrast=%d whiteness=%d\n",
00167 pict.colour,
00168 pict.hue,
00169 pict.brightness,
00170 pict.contrast,
00171 pict.whiteness);
00172 #endif
00173
00174 pict.palette = desired_palette;
00175 pict.depth= desired_depth;
00176 if (desired_palette == -1 || (ret = ioctl(video_fd, VIDIOCSPICT, &pict)) < 0) {
00177 for (j = 0; j < vformat_num; j++) {
00178 pict.palette = video_formats[j].palette;
00179 pict.depth = video_formats[j].depth;
00180 if (-1 != ioctl(video_fd, VIDIOCSPICT, &pict))
00181 break;
00182 }
00183 if (j >= vformat_num)
00184 goto fail1;
00185 }
00186
00187 ret = ioctl(video_fd,VIDIOCGMBUF,&s->gb_buffers);
00188 if (ret < 0) {
00189
00190 struct video_window win;
00191 int val;
00192
00193 win.x = 0;
00194 win.y = 0;
00195 win.width = width;
00196 win.height = height;
00197 win.chromakey = -1;
00198 win.flags = 0;
00199
00200 ioctl(video_fd, VIDIOCSWIN, &win);
00201
00202 s->frame_format = pict.palette;
00203
00204 val = 1;
00205 ioctl(video_fd, VIDIOCCAPTURE, &val);
00206
00207 s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
00208 s->use_mmap = 0;
00209
00210
00211 if (!strcmp(s->video_cap.name, "Km")) {
00212 if (aiw_init(s) < 0)
00213 goto fail;
00214 s->aiw_enabled = 1;
00215
00216
00217 s->frame_format = VIDEO_PALETTE_YUV420P;
00218 }
00219 } else {
00220 s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_SHARED,video_fd,0);
00221 if ((unsigned char*)-1 == s->video_buf) {
00222 s->video_buf = mmap(0,s->gb_buffers.size,PROT_READ|PROT_WRITE,MAP_PRIVATE,video_fd,0);
00223 if ((unsigned char*)-1 == s->video_buf) {
00224 perror("mmap");
00225 goto fail;
00226 }
00227 }
00228 s->gb_frame = 0;
00229 s->time_frame = av_gettime() * s->frame_rate / s->frame_rate_base;
00230
00231
00232 s->gb_buf.frame = s->gb_frame % s->gb_buffers.frames;
00233 s->gb_buf.height = height;
00234 s->gb_buf.width = width;
00235 s->gb_buf.format = pict.palette;
00236
00237 ret = ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
00238 if (ret < 0) {
00239 if (errno != EAGAIN) {
00240 fail1:
00241 av_log(s1, AV_LOG_ERROR, "Fatal: grab device does not support suitable format\n");
00242 } else {
00243 av_log(s1, AV_LOG_ERROR,"Fatal: grab device does not receive any video signal\n");
00244 }
00245 goto fail;
00246 }
00247 for (j = 1; j < s->gb_buffers.frames; j++) {
00248 s->gb_buf.frame = j;
00249 ioctl(video_fd, VIDIOCMCAPTURE, &s->gb_buf);
00250 }
00251 s->frame_format = s->gb_buf.format;
00252 s->use_mmap = 1;
00253 }
00254
00255 for (j = 0; j < vformat_num; j++) {
00256 if (s->frame_format == video_formats[j].palette) {
00257 frame_size = width * height * video_formats[j].depth / 8;
00258 st->codec->pix_fmt = video_formats[j].pix_fmt;
00259 break;
00260 }
00261 }
00262
00263 if (j >= vformat_num)
00264 goto fail;
00265
00266 s->fd = video_fd;
00267 s->frame_size = frame_size;
00268
00269 st->codec->codec_type = CODEC_TYPE_VIDEO;
00270 st->codec->codec_id = CODEC_ID_RAWVIDEO;
00271 st->codec->width = width;
00272 st->codec->height = height;
00273 st->codec->time_base.den = frame_rate;
00274 st->codec->time_base.num = frame_rate_base;
00275 st->codec->bit_rate = frame_size * 1/av_q2d(st->codec->time_base) * 8;
00276
00277 return 0;
00278 fail:
00279 if (video_fd >= 0)
00280 close(video_fd);
00281 av_free(st);
00282 return AVERROR(EIO);
00283 }
00284
00285 static int v4l_mm_read_picture(VideoData *s, uint8_t *buf)
00286 {
00287 uint8_t *ptr;
00288
00289 while (ioctl(s->fd, VIDIOCSYNC, &s->gb_frame) < 0 &&
00290 (errno == EAGAIN || errno == EINTR));
00291
00292 ptr = s->video_buf + s->gb_buffers.offsets[s->gb_frame];
00293 memcpy(buf, ptr, s->frame_size);
00294
00295
00296 s->gb_buf.frame = s->gb_frame;
00297 if (ioctl(s->fd, VIDIOCMCAPTURE, &s->gb_buf) < 0) {
00298 if (errno == EAGAIN)
00299 av_log(NULL, AV_LOG_ERROR, "Cannot Sync\n");
00300 else
00301 perror("VIDIOCMCAPTURE");
00302 return AVERROR(EIO);
00303 }
00304
00305
00306 s->gb_frame = (s->gb_frame + 1) % s->gb_buffers.frames;
00307
00308 return s->frame_size;
00309 }
00310
00311 static int grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
00312 {
00313 VideoData *s = s1->priv_data;
00314 int64_t curtime, delay;
00315 struct timespec ts;
00316
00317
00318 s->time_frame += INT64_C(1000000);
00319
00320
00321 for(;;) {
00322 curtime = av_gettime();
00323 delay = s->time_frame * s->frame_rate_base / s->frame_rate - curtime;
00324 if (delay <= 0) {
00325 if (delay < INT64_C(-1000000) * s->frame_rate_base / s->frame_rate) {
00326
00327 s->time_frame += INT64_C(1000000);
00328 }
00329 break;
00330 }
00331 ts.tv_sec = delay / 1000000;
00332 ts.tv_nsec = (delay % 1000000) * 1000;
00333 nanosleep(&ts, NULL);
00334 }
00335
00336 if (av_new_packet(pkt, s->frame_size) < 0)
00337 return AVERROR(EIO);
00338
00339 pkt->pts = curtime;
00340
00341
00342 if (s->aiw_enabled) {
00343 return aiw_read_picture(s, pkt->data);
00344 } else if (s->use_mmap) {
00345 return v4l_mm_read_picture(s, pkt->data);
00346 } else {
00347 if (read(s->fd, pkt->data, pkt->size) != pkt->size)
00348 return AVERROR(EIO);
00349 return s->frame_size;
00350 }
00351 }
00352
00353 static int grab_read_close(AVFormatContext *s1)
00354 {
00355 VideoData *s = s1->priv_data;
00356
00357 if (s->aiw_enabled)
00358 aiw_close(s);
00359
00360 if (s->use_mmap)
00361 munmap(s->video_buf, s->gb_buffers.size);
00362
00363
00364
00365 s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
00366 ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
00367
00368 close(s->fd);
00369 return 0;
00370 }
00371
00372 AVInputFormat v4l_demuxer = {
00373 "video4linux",
00374 "video grab",
00375 sizeof(VideoData),
00376 NULL,
00377 grab_read_header,
00378 grab_read_packet,
00379 grab_read_close,
00380 .flags = AVFMT_NOFILE,
00381 };
00382
00383
00384
00385
00386 static int aiw_init(VideoData *s)
00387 {
00388 int width, height;
00389
00390 width = s->width;
00391 height = s->height;
00392
00393 if ((width == s->video_cap.maxwidth && height == s->video_cap.maxheight) ||
00394 (width == s->video_cap.maxwidth && height == s->video_cap.maxheight*2) ||
00395 (width == s->video_cap.maxwidth/2 && height == s->video_cap.maxheight)) {
00396
00397 s->deint=0;
00398 s->halfw=0;
00399 if (height == s->video_cap.maxheight*2) s->deint=1;
00400 if (width == s->video_cap.maxwidth/2) s->halfw=1;
00401 } else {
00402 av_log(NULL, AV_LOG_ERROR, "\nIncorrect Grab Size Supplied - Supported Sizes Are:\n");
00403 av_log(NULL, AV_LOG_ERROR, " %dx%d %dx%d %dx%d\n\n",
00404 s->video_cap.maxwidth,s->video_cap.maxheight,
00405 s->video_cap.maxwidth,s->video_cap.maxheight*2,
00406 s->video_cap.maxwidth/2,s->video_cap.maxheight);
00407 goto fail;
00408 }
00409
00410 if (s->halfw == 0) {
00411 s->src_mem = av_malloc(s->width*2);
00412 } else {
00413 s->src_mem = av_malloc(s->width*4);
00414 }
00415 if (!s->src_mem) goto fail;
00416
00417 s->lum_m4_mem = av_malloc(s->width);
00418 if (!s->lum_m4_mem)
00419 goto fail;
00420 return 0;
00421 fail:
00422 av_freep(&s->src_mem);
00423 av_freep(&s->lum_m4_mem);
00424 return -1;
00425 }
00426
00427 #ifdef HAVE_MMX
00428 #include "i386/mmx.h"
00429
00430 #define LINE_WITH_UV \
00431 movq_m2r(ptr[0],mm0); \
00432 movq_m2r(ptr[8],mm1); \
00433 movq_r2r(mm0, mm4); \
00434 punpcklbw_r2r(mm1,mm0); \
00435 punpckhbw_r2r(mm1,mm4); \
00436 movq_r2r(mm0,mm5); \
00437 punpcklbw_r2r(mm4,mm0); \
00438 punpckhbw_r2r(mm4,mm5); \
00439 movq_r2r(mm0,mm1); \
00440 punpcklbw_r2r(mm5,mm1); \
00441 movq_r2m(mm1,lum[0]); \
00442 movq_m2r(ptr[16],mm2); \
00443 movq_m2r(ptr[24],mm1); \
00444 movq_r2r(mm2,mm4); \
00445 punpcklbw_r2r(mm1,mm2); \
00446 punpckhbw_r2r(mm1,mm4); \
00447 movq_r2r(mm2,mm3); \
00448 punpcklbw_r2r(mm4,mm2); \
00449 punpckhbw_r2r(mm4,mm3); \
00450 movq_r2r(mm2,mm1); \
00451 punpcklbw_r2r(mm3,mm1); \
00452 movq_r2m(mm1,lum[8]); \
00453 punpckhdq_r2r(mm2,mm0); \
00454 punpckhdq_r2r(mm3,mm5); \
00455 movq_r2m(mm0,cb[0]); \
00456 movq_r2m(mm5,cr[0]);
00457
00458 #define LINE_NO_UV \
00459 movq_m2r(ptr[0],mm0);\
00460 movq_m2r(ptr[8],mm1);\
00461 movq_r2r(mm0, mm4);\
00462 punpcklbw_r2r(mm1,mm0); \
00463 punpckhbw_r2r(mm1,mm4);\
00464 movq_r2r(mm0,mm5);\
00465 punpcklbw_r2r(mm4,mm0);\
00466 punpckhbw_r2r(mm4,mm5);\
00467 movq_r2r(mm0,mm1);\
00468 punpcklbw_r2r(mm5,mm1);\
00469 movq_r2m(mm1,lum[0]);\
00470 movq_m2r(ptr[16],mm2);\
00471 movq_m2r(ptr[24],mm1);\
00472 movq_r2r(mm2,mm4);\
00473 punpcklbw_r2r(mm1,mm2);\
00474 punpckhbw_r2r(mm1,mm4);\
00475 movq_r2r(mm2,mm3);\
00476 punpcklbw_r2r(mm4,mm2);\
00477 punpckhbw_r2r(mm4,mm3);\
00478 movq_r2r(mm2,mm1);\
00479 punpcklbw_r2r(mm3,mm1);\
00480 movq_r2m(mm1,lum[8]);
00481
00482 #define LINE_WITHUV_AVG \
00483 movq_m2r(ptr[0], mm0);\
00484 movq_m2r(ptr[8], mm1);\
00485 movq_r2r(mm0, mm4);\
00486 punpcklbw_r2r(mm1,mm0);\
00487 punpckhbw_r2r(mm1,mm4);\
00488 movq_r2r(mm0,mm5);\
00489 punpcklbw_r2r(mm4,mm0);\
00490 punpckhbw_r2r(mm4,mm5);\
00491 movq_r2r(mm0,mm1);\
00492 movq_r2r(mm5,mm2);\
00493 punpcklbw_r2r(mm7,mm1);\
00494 punpcklbw_r2r(mm7,mm2);\
00495 paddw_r2r(mm6,mm1);\
00496 paddw_r2r(mm2,mm1);\
00497 psraw_i2r(1,mm1);\
00498 packuswb_r2r(mm7,mm1);\
00499 movd_r2m(mm1,lum[0]);\
00500 movq_m2r(ptr[16],mm2);\
00501 movq_m2r(ptr[24],mm1);\
00502 movq_r2r(mm2,mm4);\
00503 punpcklbw_r2r(mm1,mm2);\
00504 punpckhbw_r2r(mm1,mm4);\
00505 movq_r2r(mm2,mm3);\
00506 punpcklbw_r2r(mm4,mm2);\
00507 punpckhbw_r2r(mm4,mm3);\
00508 movq_r2r(mm2,mm1);\
00509 movq_r2r(mm3,mm4);\
00510 punpcklbw_r2r(mm7,mm1);\
00511 punpcklbw_r2r(mm7,mm4);\
00512 paddw_r2r(mm6,mm1);\
00513 paddw_r2r(mm4,mm1);\
00514 psraw_i2r(1,mm1);\
00515 packuswb_r2r(mm7,mm1);\
00516 movd_r2m(mm1,lum[4]);\
00517 punpckhbw_r2r(mm7,mm0);\
00518 punpckhbw_r2r(mm7,mm2);\
00519 paddw_r2r(mm6,mm0);\
00520 paddw_r2r(mm2,mm0);\
00521 psraw_i2r(1,mm0);\
00522 packuswb_r2r(mm7,mm0);\
00523 punpckhbw_r2r(mm7,mm5);\
00524 punpckhbw_r2r(mm7,mm3);\
00525 paddw_r2r(mm6,mm5);\
00526 paddw_r2r(mm3,mm5);\
00527 psraw_i2r(1,mm5);\
00528 packuswb_r2r(mm7,mm5);\
00529 movd_r2m(mm0,cb[0]);\
00530 movd_r2m(mm5,cr[0]);
00531
00532 #define LINE_NOUV_AVG \
00533 movq_m2r(ptr[0],mm0);\
00534 movq_m2r(ptr[8],mm1);\
00535 pand_r2r(mm5,mm0);\
00536 pand_r2r(mm5,mm1);\
00537 pmaddwd_r2r(mm6,mm0);\
00538 pmaddwd_r2r(mm6,mm1);\
00539 packssdw_r2r(mm1,mm0);\
00540 paddw_r2r(mm6,mm0);\
00541 psraw_i2r(1,mm0);\
00542 movq_m2r(ptr[16],mm2);\
00543 movq_m2r(ptr[24],mm3);\
00544 pand_r2r(mm5,mm2);\
00545 pand_r2r(mm5,mm3);\
00546 pmaddwd_r2r(mm6,mm2);\
00547 pmaddwd_r2r(mm6,mm3);\
00548 packssdw_r2r(mm3,mm2);\
00549 paddw_r2r(mm6,mm2);\
00550 psraw_i2r(1,mm2);\
00551 packuswb_r2r(mm2,mm0);\
00552 movq_r2m(mm0,lum[0]);
00553
00554 #define DEINT_LINE_LUM(ptroff) \
00555 movd_m2r(lum_m4[(ptroff)],mm0);\
00556 movd_m2r(lum_m3[(ptroff)],mm1);\
00557 movd_m2r(lum_m2[(ptroff)],mm2);\
00558 movd_m2r(lum_m1[(ptroff)],mm3);\
00559 movd_m2r(lum[(ptroff)],mm4);\
00560 punpcklbw_r2r(mm7,mm0);\
00561 movd_r2m(mm2,lum_m4[(ptroff)]);\
00562 punpcklbw_r2r(mm7,mm1);\
00563 punpcklbw_r2r(mm7,mm2);\
00564 punpcklbw_r2r(mm7,mm3);\
00565 punpcklbw_r2r(mm7,mm4);\
00566 psllw_i2r(2,mm1);\
00567 psllw_i2r(1,mm2);\
00568 paddw_r2r(mm6,mm1);\
00569 psllw_i2r(2,mm3);\
00570 paddw_r2r(mm2,mm1);\
00571 paddw_r2r(mm4,mm0);\
00572 paddw_r2r(mm3,mm1);\
00573 psubusw_r2r(mm0,mm1);\
00574 psrlw_i2r(3,mm1);\
00575 packuswb_r2r(mm7,mm1);\
00576 movd_r2m(mm1,lum_m2[(ptroff)]);
00577
00578 #else
00579 #include "dsputil.h"
00580
00581 #define LINE_WITH_UV \
00582 lum[0]=ptr[0];lum[1]=ptr[2];lum[2]=ptr[4];lum[3]=ptr[6];\
00583 cb[0]=ptr[1];cb[1]=ptr[5];\
00584 cr[0]=ptr[3];cr[1]=ptr[7];\
00585 lum[4]=ptr[8];lum[5]=ptr[10];lum[6]=ptr[12];lum[7]=ptr[14];\
00586 cb[2]=ptr[9];cb[3]=ptr[13];\
00587 cr[2]=ptr[11];cr[3]=ptr[15];\
00588 lum[8]=ptr[16];lum[9]=ptr[18];lum[10]=ptr[20];lum[11]=ptr[22];\
00589 cb[4]=ptr[17];cb[5]=ptr[21];\
00590 cr[4]=ptr[19];cr[5]=ptr[23];\
00591 lum[12]=ptr[24];lum[13]=ptr[26];lum[14]=ptr[28];lum[15]=ptr[30];\
00592 cb[6]=ptr[25];cb[7]=ptr[29];\
00593 cr[6]=ptr[27];cr[7]=ptr[31];
00594
00595 #define LINE_NO_UV \
00596 lum[0]=ptr[0];lum[1]=ptr[2];lum[2]=ptr[4];lum[3]=ptr[6];\
00597 lum[4]=ptr[8];lum[5]=ptr[10];lum[6]=ptr[12];lum[7]=ptr[14];\
00598 lum[8]=ptr[16];lum[9]=ptr[18];lum[10]=ptr[20];lum[11]=ptr[22];\
00599 lum[12]=ptr[24];lum[13]=ptr[26];lum[14]=ptr[28];lum[15]=ptr[30];
00600
00601 #define LINE_WITHUV_AVG \
00602 sum=(ptr[0]+ptr[2]+1) >> 1;lum[0]=sum; \
00603 sum=(ptr[4]+ptr[6]+1) >> 1;lum[1]=sum; \
00604 sum=(ptr[1]+ptr[5]+1) >> 1;cb[0]=sum; \
00605 sum=(ptr[3]+ptr[7]+1) >> 1;cr[0]=sum; \
00606 sum=(ptr[8]+ptr[10]+1) >> 1;lum[2]=sum; \
00607 sum=(ptr[12]+ptr[14]+1) >> 1;lum[3]=sum; \
00608 sum=(ptr[9]+ptr[13]+1) >> 1;cb[1]=sum; \
00609 sum=(ptr[11]+ptr[15]+1) >> 1;cr[1]=sum; \
00610 sum=(ptr[16]+ptr[18]+1) >> 1;lum[4]=sum; \
00611 sum=(ptr[20]+ptr[22]+1) >> 1;lum[5]=sum; \
00612 sum=(ptr[17]+ptr[21]+1) >> 1;cb[2]=sum; \
00613 sum=(ptr[19]+ptr[23]+1) >> 1;cr[2]=sum; \
00614 sum=(ptr[24]+ptr[26]+1) >> 1;lum[6]=sum; \
00615 sum=(ptr[28]+ptr[30]+1) >> 1;lum[7]=sum; \
00616 sum=(ptr[25]+ptr[29]+1) >> 1;cb[3]=sum; \
00617 sum=(ptr[27]+ptr[31]+1) >> 1;cr[3]=sum;
00618
00619 #define LINE_NOUV_AVG \
00620 sum=(ptr[0]+ptr[2]+1) >> 1;lum[0]=sum; \
00621 sum=(ptr[4]+ptr[6]+1) >> 1;lum[1]=sum; \
00622 sum=(ptr[8]+ptr[10]+1) >> 1;lum[2]=sum; \
00623 sum=(ptr[12]+ptr[14]+1) >> 1;lum[3]=sum; \
00624 sum=(ptr[16]+ptr[18]+1) >> 1;lum[4]=sum; \
00625 sum=(ptr[20]+ptr[22]+1) >> 1;lum[5]=sum; \
00626 sum=(ptr[24]+ptr[26]+1) >> 1;lum[6]=sum; \
00627 sum=(ptr[28]+ptr[30]+1) >> 1;lum[7]=sum;
00628
00629 #define DEINT_LINE_LUM(ptroff) \
00630 sum=(-lum_m4[(ptroff)]+(lum_m3[(ptroff)]<<2)+(lum_m2[(ptroff)]<<1)+(lum_m1[(ptroff)]<<2)-lum[(ptroff)]); \
00631 lum_m4[(ptroff)]=lum_m2[(ptroff)];\
00632 lum_m2[(ptroff)]=cm[(sum+4)>>3];\
00633 sum=(-lum_m4[(ptroff)+1]+(lum_m3[(ptroff)+1]<<2)+(lum_m2[(ptroff)+1]<<1)+(lum_m1[(ptroff)+1]<<2)-lum[(ptroff)+1]); \
00634 lum_m4[(ptroff)+1]=lum_m2[(ptroff)+1];\
00635 lum_m2[(ptroff)+1]=cm[(sum+4)>>3];\
00636 sum=(-lum_m4[(ptroff)+2]+(lum_m3[(ptroff)+2]<<2)+(lum_m2[(ptroff)+2]<<1)+(lum_m1[(ptroff)+2]<<2)-lum[(ptroff)+2]); \
00637 lum_m4[(ptroff)+2]=lum_m2[(ptroff)+2];\
00638 lum_m2[(ptroff)+2]=cm[(sum+4)>>3];\
00639 sum=(-lum_m4[(ptroff)+3]+(lum_m3[(ptroff)+3]<<2)+(lum_m2[(ptroff)+3]<<1)+(lum_m1[(ptroff)+3]<<2)-lum[(ptroff)+3]); \
00640 lum_m4[(ptroff)+3]=lum_m2[(ptroff)+3];\
00641 lum_m2[(ptroff)+3]=cm[(sum+4)>>3];
00642
00643 #endif
00644
00645
00646
00647 static int aiw_read_picture(VideoData *s, uint8_t *data)
00648 {
00649 uint8_t *ptr, *lum, *cb, *cr;
00650 int h;
00651 #ifndef HAVE_MMX
00652 int sum;
00653 #endif
00654 uint8_t* src = s->src_mem;
00655 uint8_t *ptrend = &src[s->width*2];
00656 lum=data;
00657 cb=&lum[s->width*s->height];
00658 cr=&cb[(s->width*s->height)/4];
00659 if (s->deint == 0 && s->halfw == 0) {
00660 while (read(s->fd,src,s->width*2) < 0) {
00661 usleep(100);
00662 }
00663 for (h = 0; h < s->height-2; h+=2) {
00664 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
00665 LINE_WITH_UV
00666 }
00667 read(s->fd,src,s->width*2);
00668 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16) {
00669 LINE_NO_UV
00670 }
00671 read(s->fd,src,s->width*2);
00672 }
00673
00674
00675
00676 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
00677 LINE_WITH_UV
00678 }
00679 read(s->fd,src,s->width*2);
00680 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16) {
00681 LINE_NO_UV
00682 }
00683
00684 while (read(s->fd,src,s->width*2) < 0) {
00685 usleep(100);
00686 }
00687 for (h = 0; h < s->height - 1; h++) {
00688 read(s->fd,src,s->width*2);
00689 }
00690 } else if (s->halfw == 1) {
00691 #ifdef HAVE_MMX
00692 mmx_t rounder;
00693 mmx_t masker;
00694 rounder.uw[0]=1;
00695 rounder.uw[1]=1;
00696 rounder.uw[2]=1;
00697 rounder.uw[3]=1;
00698 masker.ub[0]=0xff;
00699 masker.ub[1]=0;
00700 masker.ub[2]=0xff;
00701 masker.ub[3]=0;
00702 masker.ub[4]=0xff;
00703 masker.ub[5]=0;
00704 masker.ub[6]=0xff;
00705 masker.ub[7]=0;
00706 pxor_r2r(mm7,mm7);
00707 movq_m2r(rounder,mm6);
00708 #endif
00709 while (read(s->fd,src,s->width*4) < 0) {
00710 usleep(100);
00711 }
00712 ptrend = &src[s->width*4];
00713 for (h = 0; h < s->height-2; h+=2) {
00714 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8, cb+=4, cr+=4) {
00715 LINE_WITHUV_AVG
00716 }
00717 read(s->fd,src,s->width*4);
00718 #ifdef HAVE_MMX
00719 movq_m2r(masker,mm5);
00720 #endif
00721 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8) {
00722 LINE_NOUV_AVG
00723 }
00724 read(s->fd,src,s->width*4);
00725 }
00726
00727
00728
00729 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8, cb+=4, cr+=4) {
00730 LINE_WITHUV_AVG
00731 }
00732 read(s->fd,src,s->width*4);
00733 #ifdef HAVE_MMX
00734 movq_m2r(masker,mm5);
00735 #endif
00736 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8) {
00737 LINE_NOUV_AVG
00738 }
00739
00740 while (read(s->fd,src,s->width*4) < 0) {
00741 usleep(100);
00742 }
00743 for (h = 0; h < s->height - 1; h++) {
00744 read(s->fd,src,s->width*4);
00745 }
00746 } else {
00747 uint8_t *lum_m1, *lum_m2, *lum_m3, *lum_m4;
00748 #ifdef HAVE_MMX
00749 mmx_t rounder;
00750 rounder.uw[0]=4;
00751 rounder.uw[1]=4;
00752 rounder.uw[2]=4;
00753 rounder.uw[3]=4;
00754 movq_m2r(rounder,mm6);
00755 pxor_r2r(mm7,mm7);
00756 #else
00757 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
00758 #endif
00759
00760
00761 while (read(s->fd,src,s->width*2) < 0) {
00762 usleep(100);
00763 }
00764 for (h = 0; h < (s->height/2)-2; h+=2) {
00765 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
00766 LINE_WITH_UV
00767 }
00768 read(s->fd,src,s->width*2);
00769
00770 lum += s->width;
00771 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
00772 LINE_WITH_UV
00773 }
00774
00775 lum += s->width;
00776 read(s->fd,src,s->width*2);
00777 }
00778
00779
00780
00781 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
00782 LINE_WITH_UV
00783 }
00784
00785 lum += s->width;
00786 read(s->fd,src,s->width*2);
00787 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
00788 LINE_WITH_UV
00789 }
00790
00791
00792
00793
00794
00795 lum=&data[s->width];
00796 while (read(s->fd,src,s->width*2) < 0) {
00797 usleep(10);
00798 }
00799
00800 for (h = 0; h < 2; h++) {
00801 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16) {
00802 LINE_NO_UV
00803 }
00804 read(s->fd,src,s->width*2);
00805
00806 lum += s->width;
00807 }
00808 lum_m1=&lum[-s->width];
00809 lum_m2=&lum_m1[-s->width];
00810 lum_m3=&lum_m2[-s->width];
00811 memmove(s->lum_m4_mem,&lum_m3[-s->width],s->width);
00812 for (; h < (s->height/2)-1; h++) {
00813 lum_m4=s->lum_m4_mem;
00814 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16,lum_m1+=16,lum_m2+=16,lum_m3+=16,lum_m4+=16) {
00815 LINE_NO_UV
00816
00817 DEINT_LINE_LUM(0)
00818 DEINT_LINE_LUM(4)
00819 DEINT_LINE_LUM(8)
00820 DEINT_LINE_LUM(12)
00821 }
00822 read(s->fd,src,s->width*2);
00823
00824 lum += s->width;
00825 lum_m1 += s->width;
00826 lum_m2 += s->width;
00827 lum_m3 += s->width;
00828
00829 }
00830
00831
00832
00833 lum_m4=s->lum_m4_mem;
00834 for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, lum_m1+=16, lum_m2+=16, lum_m3+=16, lum_m4+=16) {
00835 LINE_NO_UV
00836
00837 DEINT_LINE_LUM(0)
00838 DEINT_LINE_LUM(4)
00839 DEINT_LINE_LUM(8)
00840 DEINT_LINE_LUM(12)
00841 }
00842 }
00843 emms_c();
00844 return s->frame_size;
00845 }
00846
00847 static int aiw_close(VideoData *s)
00848 {
00849 av_freep(&s->lum_m4_mem);
00850 av_freep(&s->src_mem);
00851 return 0;
00852 }