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.

861 lines
28KB

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