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.

853 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() * s->frame_rate / s->frame_rate_base;
  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() * s->frame_rate / s->frame_rate_base;
  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. /* Calculate the time of the next frame */
  272. s->time_frame += int64_t_C(1000000);
  273. /* wait based on the frame rate */
  274. for(;;) {
  275. curtime = av_gettime();
  276. delay = s->time_frame * s->frame_rate_base / s->frame_rate - curtime;
  277. if (delay <= 0) {
  278. if (delay < int64_t_C(-1000000) * s->frame_rate_base / s->frame_rate) {
  279. /* printf("grabbing is %d frames late (dropping)\n", (int) -(delay / 16666)); */
  280. s->time_frame += int64_t_C(1000000);
  281. }
  282. break;
  283. }
  284. ts.tv_sec = delay / 1000000;
  285. ts.tv_nsec = (delay % 1000000) * 1000;
  286. nanosleep(&ts, NULL);
  287. }
  288. if (av_new_packet(pkt, s->frame_size) < 0)
  289. return -EIO;
  290. pkt->pts = curtime & ((1LL << 48) - 1);
  291. /* read one frame */
  292. if (s->aiw_enabled) {
  293. return aiw_read_picture(s, pkt->data);
  294. } else if (s->use_mmap) {
  295. return v4l_mm_read_picture(s, pkt->data);
  296. } else {
  297. if (read(s->fd, pkt->data, pkt->size) != pkt->size)
  298. return -EIO;
  299. return s->frame_size;
  300. }
  301. }
  302. static int grab_read_close(AVFormatContext *s1)
  303. {
  304. VideoData *s = s1->priv_data;
  305. if (s->aiw_enabled)
  306. aiw_close(s);
  307. if (s->use_mmap)
  308. munmap(s->video_buf, s->gb_buffers.size);
  309. /* mute audio. we must force it because the BTTV driver does not
  310. return its state correctly */
  311. s->audio_saved.flags |= VIDEO_AUDIO_MUTE;
  312. ioctl(s->fd, VIDIOCSAUDIO, &s->audio_saved);
  313. close(s->fd);
  314. return 0;
  315. }
  316. static AVInputFormat video_grab_device_format = {
  317. "video4linux",
  318. "video grab",
  319. sizeof(VideoData),
  320. NULL,
  321. grab_read_header,
  322. grab_read_packet,
  323. grab_read_close,
  324. .flags = AVFMT_NOFILE,
  325. };
  326. /* All in Wonder specific stuff */
  327. /* XXX: remove and merge in libavcodec/imgconvert.c */
  328. static int aiw_init(VideoData *s)
  329. {
  330. int width, height;
  331. width = s->width;
  332. height = s->height;
  333. if ((width == s->video_cap.maxwidth && height == s->video_cap.maxheight) ||
  334. (width == s->video_cap.maxwidth && height == s->video_cap.maxheight*2) ||
  335. (width == s->video_cap.maxwidth/2 && height == s->video_cap.maxheight)) {
  336. s->deint=0;
  337. s->halfw=0;
  338. if (height == s->video_cap.maxheight*2) s->deint=1;
  339. if (width == s->video_cap.maxwidth/2) s->halfw=1;
  340. } else {
  341. av_log(NULL, AV_LOG_ERROR, "\nIncorrect Grab Size Supplied - Supported Sizes Are:\n");
  342. av_log(NULL, AV_LOG_ERROR, " %dx%d %dx%d %dx%d\n\n",
  343. s->video_cap.maxwidth,s->video_cap.maxheight,
  344. s->video_cap.maxwidth,s->video_cap.maxheight*2,
  345. s->video_cap.maxwidth/2,s->video_cap.maxheight);
  346. goto fail;
  347. }
  348. if (s->halfw == 0) {
  349. s->src_mem = av_malloc(s->width*2);
  350. } else {
  351. s->src_mem = av_malloc(s->width*4);
  352. }
  353. if (!s->src_mem) goto fail;
  354. s->lum_m4_mem = av_malloc(s->width);
  355. if (!s->lum_m4_mem)
  356. goto fail;
  357. return 0;
  358. fail:
  359. av_freep(&s->src_mem);
  360. av_freep(&s->lum_m4_mem);
  361. return -1;
  362. }
  363. #ifdef HAVE_MMX
  364. #include "../libavcodec/i386/mmx.h"
  365. #define LINE_WITH_UV \
  366. movq_m2r(ptr[0],mm0); \
  367. movq_m2r(ptr[8],mm1); \
  368. movq_r2r(mm0, mm4); \
  369. punpcklbw_r2r(mm1,mm0); \
  370. punpckhbw_r2r(mm1,mm4); \
  371. movq_r2r(mm0,mm5); \
  372. punpcklbw_r2r(mm4,mm0); \
  373. punpckhbw_r2r(mm4,mm5); \
  374. movq_r2r(mm0,mm1); \
  375. punpcklbw_r2r(mm5,mm1); \
  376. movq_r2m(mm1,lum[0]); \
  377. movq_m2r(ptr[16],mm2); \
  378. movq_m2r(ptr[24],mm1); \
  379. movq_r2r(mm2,mm4); \
  380. punpcklbw_r2r(mm1,mm2); \
  381. punpckhbw_r2r(mm1,mm4); \
  382. movq_r2r(mm2,mm3); \
  383. punpcklbw_r2r(mm4,mm2); \
  384. punpckhbw_r2r(mm4,mm3); \
  385. movq_r2r(mm2,mm1); \
  386. punpcklbw_r2r(mm3,mm1); \
  387. movq_r2m(mm1,lum[8]); \
  388. punpckhdq_r2r(mm2,mm0); \
  389. punpckhdq_r2r(mm3,mm5); \
  390. movq_r2m(mm0,cb[0]); \
  391. movq_r2m(mm5,cr[0]);
  392. #define LINE_NO_UV \
  393. movq_m2r(ptr[0],mm0);\
  394. movq_m2r(ptr[8],mm1);\
  395. movq_r2r(mm0, mm4);\
  396. punpcklbw_r2r(mm1,mm0); \
  397. punpckhbw_r2r(mm1,mm4);\
  398. movq_r2r(mm0,mm5);\
  399. punpcklbw_r2r(mm4,mm0);\
  400. punpckhbw_r2r(mm4,mm5);\
  401. movq_r2r(mm0,mm1);\
  402. punpcklbw_r2r(mm5,mm1);\
  403. movq_r2m(mm1,lum[0]);\
  404. movq_m2r(ptr[16],mm2);\
  405. movq_m2r(ptr[24],mm1);\
  406. movq_r2r(mm2,mm4);\
  407. punpcklbw_r2r(mm1,mm2);\
  408. punpckhbw_r2r(mm1,mm4);\
  409. movq_r2r(mm2,mm3);\
  410. punpcklbw_r2r(mm4,mm2);\
  411. punpckhbw_r2r(mm4,mm3);\
  412. movq_r2r(mm2,mm1);\
  413. punpcklbw_r2r(mm3,mm1);\
  414. movq_r2m(mm1,lum[8]);
  415. #define LINE_WITHUV_AVG \
  416. movq_m2r(ptr[0], mm0);\
  417. movq_m2r(ptr[8], mm1);\
  418. movq_r2r(mm0, mm4);\
  419. punpcklbw_r2r(mm1,mm0);\
  420. punpckhbw_r2r(mm1,mm4);\
  421. movq_r2r(mm0,mm5);\
  422. punpcklbw_r2r(mm4,mm0);\
  423. punpckhbw_r2r(mm4,mm5);\
  424. movq_r2r(mm0,mm1);\
  425. movq_r2r(mm5,mm2);\
  426. punpcklbw_r2r(mm7,mm1);\
  427. punpcklbw_r2r(mm7,mm2);\
  428. paddw_r2r(mm6,mm1);\
  429. paddw_r2r(mm2,mm1);\
  430. psraw_i2r(1,mm1);\
  431. packuswb_r2r(mm7,mm1);\
  432. movd_r2m(mm1,lum[0]);\
  433. movq_m2r(ptr[16],mm2);\
  434. movq_m2r(ptr[24],mm1);\
  435. movq_r2r(mm2,mm4);\
  436. punpcklbw_r2r(mm1,mm2);\
  437. punpckhbw_r2r(mm1,mm4);\
  438. movq_r2r(mm2,mm3);\
  439. punpcklbw_r2r(mm4,mm2);\
  440. punpckhbw_r2r(mm4,mm3);\
  441. movq_r2r(mm2,mm1);\
  442. movq_r2r(mm3,mm4);\
  443. punpcklbw_r2r(mm7,mm1);\
  444. punpcklbw_r2r(mm7,mm4);\
  445. paddw_r2r(mm6,mm1);\
  446. paddw_r2r(mm4,mm1);\
  447. psraw_i2r(1,mm1);\
  448. packuswb_r2r(mm7,mm1);\
  449. movd_r2m(mm1,lum[4]);\
  450. punpckhbw_r2r(mm7,mm0);\
  451. punpckhbw_r2r(mm7,mm2);\
  452. paddw_r2r(mm6,mm0);\
  453. paddw_r2r(mm2,mm0);\
  454. psraw_i2r(1,mm0);\
  455. packuswb_r2r(mm7,mm0);\
  456. punpckhbw_r2r(mm7,mm5);\
  457. punpckhbw_r2r(mm7,mm3);\
  458. paddw_r2r(mm6,mm5);\
  459. paddw_r2r(mm3,mm5);\
  460. psraw_i2r(1,mm5);\
  461. packuswb_r2r(mm7,mm5);\
  462. movd_r2m(mm0,cb[0]);\
  463. movd_r2m(mm5,cr[0]);
  464. #define LINE_NOUV_AVG \
  465. movq_m2r(ptr[0],mm0);\
  466. movq_m2r(ptr[8],mm1);\
  467. pand_r2r(mm5,mm0);\
  468. pand_r2r(mm5,mm1);\
  469. pmaddwd_r2r(mm6,mm0);\
  470. pmaddwd_r2r(mm6,mm1);\
  471. packssdw_r2r(mm1,mm0);\
  472. paddw_r2r(mm6,mm0);\
  473. psraw_i2r(1,mm0);\
  474. movq_m2r(ptr[16],mm2);\
  475. movq_m2r(ptr[24],mm3);\
  476. pand_r2r(mm5,mm2);\
  477. pand_r2r(mm5,mm3);\
  478. pmaddwd_r2r(mm6,mm2);\
  479. pmaddwd_r2r(mm6,mm3);\
  480. packssdw_r2r(mm3,mm2);\
  481. paddw_r2r(mm6,mm2);\
  482. psraw_i2r(1,mm2);\
  483. packuswb_r2r(mm2,mm0);\
  484. movq_r2m(mm0,lum[0]);
  485. #define DEINT_LINE_LUM(ptroff) \
  486. movd_m2r(lum_m4[(ptroff)],mm0);\
  487. movd_m2r(lum_m3[(ptroff)],mm1);\
  488. movd_m2r(lum_m2[(ptroff)],mm2);\
  489. movd_m2r(lum_m1[(ptroff)],mm3);\
  490. movd_m2r(lum[(ptroff)],mm4);\
  491. punpcklbw_r2r(mm7,mm0);\
  492. movd_r2m(mm2,lum_m4[(ptroff)]);\
  493. punpcklbw_r2r(mm7,mm1);\
  494. punpcklbw_r2r(mm7,mm2);\
  495. punpcklbw_r2r(mm7,mm3);\
  496. punpcklbw_r2r(mm7,mm4);\
  497. psllw_i2r(2,mm1);\
  498. psllw_i2r(1,mm2);\
  499. paddw_r2r(mm6,mm1);\
  500. psllw_i2r(2,mm3);\
  501. paddw_r2r(mm2,mm1);\
  502. paddw_r2r(mm4,mm0);\
  503. paddw_r2r(mm3,mm1);\
  504. psubusw_r2r(mm0,mm1);\
  505. psrlw_i2r(3,mm1);\
  506. packuswb_r2r(mm7,mm1);\
  507. movd_r2m(mm1,lum_m2[(ptroff)]);
  508. #else
  509. #include "../libavcodec/dsputil.h"
  510. #define LINE_WITH_UV \
  511. lum[0]=ptr[0];lum[1]=ptr[2];lum[2]=ptr[4];lum[3]=ptr[6];\
  512. cb[0]=ptr[1];cb[1]=ptr[5];\
  513. cr[0]=ptr[3];cr[1]=ptr[7];\
  514. lum[4]=ptr[8];lum[5]=ptr[10];lum[6]=ptr[12];lum[7]=ptr[14];\
  515. cb[2]=ptr[9];cb[3]=ptr[13];\
  516. cr[2]=ptr[11];cr[3]=ptr[15];\
  517. lum[8]=ptr[16];lum[9]=ptr[18];lum[10]=ptr[20];lum[11]=ptr[22];\
  518. cb[4]=ptr[17];cb[5]=ptr[21];\
  519. cr[4]=ptr[19];cr[5]=ptr[23];\
  520. lum[12]=ptr[24];lum[13]=ptr[26];lum[14]=ptr[28];lum[15]=ptr[30];\
  521. cb[6]=ptr[25];cb[7]=ptr[29];\
  522. cr[6]=ptr[27];cr[7]=ptr[31];
  523. #define LINE_NO_UV \
  524. lum[0]=ptr[0];lum[1]=ptr[2];lum[2]=ptr[4];lum[3]=ptr[6];\
  525. lum[4]=ptr[8];lum[5]=ptr[10];lum[6]=ptr[12];lum[7]=ptr[14];\
  526. lum[8]=ptr[16];lum[9]=ptr[18];lum[10]=ptr[20];lum[11]=ptr[22];\
  527. lum[12]=ptr[24];lum[13]=ptr[26];lum[14]=ptr[28];lum[15]=ptr[30];
  528. #define LINE_WITHUV_AVG \
  529. sum=(ptr[0]+ptr[2]+1) >> 1;lum[0]=sum; \
  530. sum=(ptr[4]+ptr[6]+1) >> 1;lum[1]=sum; \
  531. sum=(ptr[1]+ptr[5]+1) >> 1;cb[0]=sum; \
  532. sum=(ptr[3]+ptr[7]+1) >> 1;cr[0]=sum; \
  533. sum=(ptr[8]+ptr[10]+1) >> 1;lum[2]=sum; \
  534. sum=(ptr[12]+ptr[14]+1) >> 1;lum[3]=sum; \
  535. sum=(ptr[9]+ptr[13]+1) >> 1;cb[1]=sum; \
  536. sum=(ptr[11]+ptr[15]+1) >> 1;cr[1]=sum; \
  537. sum=(ptr[16]+ptr[18]+1) >> 1;lum[4]=sum; \
  538. sum=(ptr[20]+ptr[22]+1) >> 1;lum[5]=sum; \
  539. sum=(ptr[17]+ptr[21]+1) >> 1;cb[2]=sum; \
  540. sum=(ptr[19]+ptr[23]+1) >> 1;cr[2]=sum; \
  541. sum=(ptr[24]+ptr[26]+1) >> 1;lum[6]=sum; \
  542. sum=(ptr[28]+ptr[30]+1) >> 1;lum[7]=sum; \
  543. sum=(ptr[25]+ptr[29]+1) >> 1;cb[3]=sum; \
  544. sum=(ptr[27]+ptr[31]+1) >> 1;cr[3]=sum;
  545. #define LINE_NOUV_AVG \
  546. sum=(ptr[0]+ptr[2]+1) >> 1;lum[0]=sum; \
  547. sum=(ptr[4]+ptr[6]+1) >> 1;lum[1]=sum; \
  548. sum=(ptr[8]+ptr[10]+1) >> 1;lum[2]=sum; \
  549. sum=(ptr[12]+ptr[14]+1) >> 1;lum[3]=sum; \
  550. sum=(ptr[16]+ptr[18]+1) >> 1;lum[4]=sum; \
  551. sum=(ptr[20]+ptr[22]+1) >> 1;lum[5]=sum; \
  552. sum=(ptr[24]+ptr[26]+1) >> 1;lum[6]=sum; \
  553. sum=(ptr[28]+ptr[30]+1) >> 1;lum[7]=sum;
  554. #define DEINT_LINE_LUM(ptroff) \
  555. sum=(-lum_m4[(ptroff)]+(lum_m3[(ptroff)]<<2)+(lum_m2[(ptroff)]<<1)+(lum_m1[(ptroff)]<<2)-lum[(ptroff)]); \
  556. lum_m4[(ptroff)]=lum_m2[(ptroff)];\
  557. lum_m2[(ptroff)]=cm[(sum+4)>>3];\
  558. sum=(-lum_m4[(ptroff)+1]+(lum_m3[(ptroff)+1]<<2)+(lum_m2[(ptroff)+1]<<1)+(lum_m1[(ptroff)+1]<<2)-lum[(ptroff)+1]); \
  559. lum_m4[(ptroff)+1]=lum_m2[(ptroff)+1];\
  560. lum_m2[(ptroff)+1]=cm[(sum+4)>>3];\
  561. sum=(-lum_m4[(ptroff)+2]+(lum_m3[(ptroff)+2]<<2)+(lum_m2[(ptroff)+2]<<1)+(lum_m1[(ptroff)+2]<<2)-lum[(ptroff)+2]); \
  562. lum_m4[(ptroff)+2]=lum_m2[(ptroff)+2];\
  563. lum_m2[(ptroff)+2]=cm[(sum+4)>>3];\
  564. sum=(-lum_m4[(ptroff)+3]+(lum_m3[(ptroff)+3]<<2)+(lum_m2[(ptroff)+3]<<1)+(lum_m1[(ptroff)+3]<<2)-lum[(ptroff)+3]); \
  565. lum_m4[(ptroff)+3]=lum_m2[(ptroff)+3];\
  566. lum_m2[(ptroff)+3]=cm[(sum+4)>>3];
  567. #endif
  568. /* Read two fields separately. */
  569. static int aiw_read_picture(VideoData *s, uint8_t *data)
  570. {
  571. uint8_t *ptr, *lum, *cb, *cr;
  572. int h;
  573. #ifndef HAVE_MMX
  574. int sum;
  575. #endif
  576. uint8_t* src = s->src_mem;
  577. uint8_t *ptrend = &src[s->width*2];
  578. lum=data;
  579. cb=&lum[s->width*s->height];
  580. cr=&cb[(s->width*s->height)/4];
  581. if (s->deint == 0 && s->halfw == 0) {
  582. while (read(s->fd,src,s->width*2) < 0) {
  583. usleep(100);
  584. }
  585. for (h = 0; h < s->height-2; h+=2) {
  586. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
  587. LINE_WITH_UV
  588. }
  589. read(s->fd,src,s->width*2);
  590. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16) {
  591. LINE_NO_UV
  592. }
  593. read(s->fd,src,s->width*2);
  594. }
  595. /*
  596. * Do last two lines
  597. */
  598. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
  599. LINE_WITH_UV
  600. }
  601. read(s->fd,src,s->width*2);
  602. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16) {
  603. LINE_NO_UV
  604. }
  605. /* drop second field */
  606. while (read(s->fd,src,s->width*2) < 0) {
  607. usleep(100);
  608. }
  609. for (h = 0; h < s->height - 1; h++) {
  610. read(s->fd,src,s->width*2);
  611. }
  612. } else if (s->halfw == 1) {
  613. #ifdef HAVE_MMX
  614. mmx_t rounder;
  615. mmx_t masker;
  616. rounder.uw[0]=1;
  617. rounder.uw[1]=1;
  618. rounder.uw[2]=1;
  619. rounder.uw[3]=1;
  620. masker.ub[0]=0xff;
  621. masker.ub[1]=0;
  622. masker.ub[2]=0xff;
  623. masker.ub[3]=0;
  624. masker.ub[4]=0xff;
  625. masker.ub[5]=0;
  626. masker.ub[6]=0xff;
  627. masker.ub[7]=0;
  628. pxor_r2r(mm7,mm7);
  629. movq_m2r(rounder,mm6);
  630. #endif
  631. while (read(s->fd,src,s->width*4) < 0) {
  632. usleep(100);
  633. }
  634. ptrend = &src[s->width*4];
  635. for (h = 0; h < s->height-2; h+=2) {
  636. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8, cb+=4, cr+=4) {
  637. LINE_WITHUV_AVG
  638. }
  639. read(s->fd,src,s->width*4);
  640. #ifdef HAVE_MMX
  641. movq_m2r(masker,mm5);
  642. #endif
  643. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8) {
  644. LINE_NOUV_AVG
  645. }
  646. read(s->fd,src,s->width*4);
  647. }
  648. /*
  649. * Do last two lines
  650. */
  651. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8, cb+=4, cr+=4) {
  652. LINE_WITHUV_AVG
  653. }
  654. read(s->fd,src,s->width*4);
  655. #ifdef HAVE_MMX
  656. movq_m2r(masker,mm5);
  657. #endif
  658. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=8) {
  659. LINE_NOUV_AVG
  660. }
  661. /* drop second field */
  662. while (read(s->fd,src,s->width*4) < 0) {
  663. usleep(100);
  664. }
  665. for (h = 0; h < s->height - 1; h++) {
  666. read(s->fd,src,s->width*4);
  667. }
  668. } else {
  669. uint8_t *lum_m1, *lum_m2, *lum_m3, *lum_m4;
  670. #ifdef HAVE_MMX
  671. mmx_t rounder;
  672. rounder.uw[0]=4;
  673. rounder.uw[1]=4;
  674. rounder.uw[2]=4;
  675. rounder.uw[3]=4;
  676. movq_m2r(rounder,mm6);
  677. pxor_r2r(mm7,mm7);
  678. #else
  679. uint8_t *cm = cropTbl + MAX_NEG_CROP;
  680. #endif
  681. /* read two fields and deinterlace them */
  682. while (read(s->fd,src,s->width*2) < 0) {
  683. usleep(100);
  684. }
  685. for (h = 0; h < (s->height/2)-2; h+=2) {
  686. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
  687. LINE_WITH_UV
  688. }
  689. read(s->fd,src,s->width*2);
  690. /* skip a luminance line - will be filled in later */
  691. lum += s->width;
  692. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
  693. LINE_WITH_UV
  694. }
  695. /* skip a luminance line - will be filled in later */
  696. lum += s->width;
  697. read(s->fd,src,s->width*2);
  698. }
  699. /*
  700. * Do last two lines
  701. */
  702. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
  703. LINE_WITH_UV
  704. }
  705. /* skip a luminance line - will be filled in later */
  706. lum += s->width;
  707. read(s->fd,src,s->width*2);
  708. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, cb+=8, cr+=8) {
  709. LINE_WITH_UV
  710. }
  711. /*
  712. *
  713. * SECOND FIELD
  714. *
  715. */
  716. lum=&data[s->width];
  717. while (read(s->fd,src,s->width*2) < 0) {
  718. usleep(10);
  719. }
  720. /* First (and last) two lines not interlaced */
  721. for (h = 0; h < 2; h++) {
  722. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16) {
  723. LINE_NO_UV
  724. }
  725. read(s->fd,src,s->width*2);
  726. /* skip a luminance line */
  727. lum += s->width;
  728. }
  729. lum_m1=&lum[-s->width];
  730. lum_m2=&lum_m1[-s->width];
  731. lum_m3=&lum_m2[-s->width];
  732. memmove(s->lum_m4_mem,&lum_m3[-s->width],s->width);
  733. for (; h < (s->height/2)-1; h++) {
  734. lum_m4=s->lum_m4_mem;
  735. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16,lum_m1+=16,lum_m2+=16,lum_m3+=16,lum_m4+=16) {
  736. LINE_NO_UV
  737. DEINT_LINE_LUM(0)
  738. DEINT_LINE_LUM(4)
  739. DEINT_LINE_LUM(8)
  740. DEINT_LINE_LUM(12)
  741. }
  742. read(s->fd,src,s->width*2);
  743. /* skip a luminance line */
  744. lum += s->width;
  745. lum_m1 += s->width;
  746. lum_m2 += s->width;
  747. lum_m3 += s->width;
  748. // lum_m4 += s->width;
  749. }
  750. /*
  751. * Do last line
  752. */
  753. lum_m4=s->lum_m4_mem;
  754. for (ptr = &src[0]; ptr < ptrend; ptr+=32, lum+=16, lum_m1+=16, lum_m2+=16, lum_m3+=16, lum_m4+=16) {
  755. LINE_NO_UV
  756. DEINT_LINE_LUM(0)
  757. DEINT_LINE_LUM(4)
  758. DEINT_LINE_LUM(8)
  759. DEINT_LINE_LUM(12)
  760. }
  761. }
  762. #ifdef HAVE_MMX
  763. emms();
  764. #endif
  765. return s->frame_size;
  766. }
  767. static int aiw_close(VideoData *s)
  768. {
  769. av_freep(&s->lum_m4_mem);
  770. av_freep(&s->src_mem);
  771. return 0;
  772. }
  773. int video_grab_init(void)
  774. {
  775. av_register_input_format(&video_grab_device_format);
  776. return 0;
  777. }