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.

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