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.

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