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.

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