You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

848 lines
28KB

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