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.

828 lines
27KB

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