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.

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