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.

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