You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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