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.

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