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.

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