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.

864 lines
29KB

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