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.

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