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.

856 lines
28KB

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