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.

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