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.

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