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.

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