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.

314 lines
8.4KB

  1. /*
  2. * Linux DV1394 interface
  3. * Copyright (c) 2003 Max Krasnyansky <maxk@qualcomm.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <errno.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/mman.h>
  24. #include <sys/poll.h>
  25. #include <sys/time.h>
  26. #include <time.h>
  27. #include "avformat.h"
  28. #undef DV1394_DEBUG
  29. #include "dv1394.h"
  30. struct dv1394_data {
  31. int fd;
  32. int channel;
  33. int width, height;
  34. int frame_rate;
  35. int frame_size;
  36. int format;
  37. void *ring; /* Ring buffer */
  38. int index; /* Current frame index */
  39. int avail; /* Number of frames available for reading */
  40. int done; /* Number of completed frames */
  41. int stream; /* Current stream. 0 - video, 1 - audio */
  42. int64_t pts; /* Current timestamp */
  43. AVStream *vst, *ast;
  44. };
  45. /*
  46. * The trick here is to kludge around well known problem with kernel Ooopsing
  47. * when you try to capture PAL on a device node configure for NTSC. That's
  48. * why we have to configure the device node for PAL, and then read only NTSC
  49. * amount of data.
  50. */
  51. static int dv1394_reset(struct dv1394_data *dv)
  52. {
  53. struct dv1394_init init;
  54. init.channel = dv->channel;
  55. init.api_version = DV1394_API_VERSION;
  56. init.n_frames = DV1394_RING_FRAMES;
  57. init.format = DV1394_PAL;
  58. if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
  59. return -1;
  60. dv->avail = dv->done = 0;
  61. dv->stream = 0;
  62. return 0;
  63. }
  64. static int dv1394_start(struct dv1394_data *dv)
  65. {
  66. /* Tell DV1394 driver to enable receiver */
  67. if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) {
  68. perror("Failed to start receiver");
  69. return -1;
  70. }
  71. return 0;
  72. }
  73. static int dv1394_read_header(AVFormatContext * context, AVFormatParameters * ap)
  74. {
  75. struct dv1394_data *dv = context->priv_data;
  76. const char *video_device;
  77. dv->vst = av_new_stream(context, 0);
  78. if (!dv->vst)
  79. return -ENOMEM;
  80. dv->ast = av_new_stream(context, 1);
  81. if (!dv->ast) {
  82. av_free(dv->vst);
  83. return -ENOMEM;
  84. }
  85. if (ap->standard && !strcasecmp(ap->standard, "pal"))
  86. dv->format = DV1394_PAL;
  87. else
  88. dv->format = DV1394_NTSC;
  89. if (ap->channel)
  90. dv->channel = ap->channel;
  91. else
  92. dv->channel = DV1394_DEFAULT_CHANNEL;
  93. dv->width = DV1394_WIDTH;
  94. if (dv->format == DV1394_NTSC) {
  95. dv->height = DV1394_NTSC_HEIGHT;
  96. dv->frame_size = DV1394_NTSC_FRAME_SIZE;
  97. dv->frame_rate = 30;
  98. } else {
  99. dv->height = DV1394_PAL_HEIGHT;
  100. dv->frame_size = DV1394_PAL_FRAME_SIZE;
  101. dv->frame_rate = 25;
  102. }
  103. /* Open and initialize DV1394 device */
  104. video_device = ap->device;
  105. if (!video_device)
  106. video_device = "/dev/dv1394/0";
  107. dv->fd = open(video_device, O_RDONLY);
  108. if (dv->fd < 0) {
  109. perror("Failed to open DV interface");
  110. goto failed;
  111. }
  112. if (dv1394_reset(dv) < 0) {
  113. perror("Failed to initialize DV interface");
  114. goto failed;
  115. }
  116. dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES,
  117. PROT_READ, MAP_PRIVATE, dv->fd, 0);
  118. if (dv->ring == MAP_FAILED) {
  119. perror("Failed to mmap DV ring buffer");
  120. goto failed;
  121. }
  122. dv->stream = 0;
  123. dv->vst->codec.codec_type = CODEC_TYPE_VIDEO;
  124. dv->vst->codec.codec_id = CODEC_ID_DVVIDEO;
  125. dv->vst->codec.width = dv->width;
  126. dv->vst->codec.height = dv->height;
  127. dv->vst->codec.frame_rate = dv->frame_rate;
  128. dv->vst->codec.frame_rate_base = 1;
  129. dv->vst->codec.bit_rate = 25000000; /* Consumer DV is 25Mbps */
  130. dv->ast->codec.codec_type = CODEC_TYPE_AUDIO;
  131. dv->ast->codec.codec_id = CODEC_ID_DVAUDIO;
  132. dv->ast->codec.channels = 2;
  133. dv->ast->codec.sample_rate= 48000;
  134. av_set_pts_info(context, 48, 1, 1000000);
  135. if (dv1394_start(dv) < 0)
  136. goto failed;
  137. return 0;
  138. failed:
  139. close(dv->fd);
  140. av_free(dv->vst);
  141. av_free(dv->ast);
  142. return -EIO;
  143. }
  144. static void __destruct_pkt(struct AVPacket *pkt)
  145. {
  146. pkt->data = NULL; pkt->size = 0;
  147. return;
  148. }
  149. static inline int __get_frame(struct dv1394_data *dv, AVPacket *pkt)
  150. {
  151. char *ptr = dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE);
  152. if (dv->stream) {
  153. dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
  154. dv->done++; dv->avail--;
  155. } else {
  156. dv->pts = av_gettime() & ((1LL << 48) - 1);
  157. }
  158. dv->format = ((ptr[3] & 0x80) == 0) ? DV1394_NTSC : DV1394_PAL;
  159. if (dv->format == DV1394_NTSC) {
  160. dv->frame_size = DV1394_NTSC_FRAME_SIZE;
  161. dv->vst->codec.height = dv->height = DV1394_NTSC_HEIGHT;
  162. dv->vst->codec.frame_rate = dv->frame_rate = 30;
  163. } else {
  164. dv->frame_size = DV1394_PAL_FRAME_SIZE;
  165. dv->vst->codec.height = dv->height = DV1394_PAL_HEIGHT;
  166. dv->vst->codec.frame_rate = dv->frame_rate = 25;
  167. }
  168. av_init_packet(pkt);
  169. pkt->destruct = __destruct_pkt;
  170. pkt->data = ptr;
  171. pkt->size = dv->frame_size;
  172. pkt->pts = dv->pts;
  173. pkt->stream_index = dv->stream;
  174. pkt->flags |= PKT_FLAG_KEY;
  175. dv->stream ^= 1;
  176. return dv->frame_size;
  177. }
  178. static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
  179. {
  180. struct dv1394_data *dv = context->priv_data;
  181. if (!dv->avail) {
  182. struct dv1394_status s;
  183. struct pollfd p;
  184. if (dv->done) {
  185. /* Request more frames */
  186. if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
  187. /* This usually means that ring buffer overflowed.
  188. * We have to reset :(.
  189. */
  190. fprintf(stderr, "DV1394: Ring buffer overflow. Reseting ..\n");
  191. dv1394_reset(dv);
  192. dv1394_start(dv);
  193. }
  194. dv->done = 0;
  195. }
  196. /* Wait until more frames are available */
  197. restart_poll:
  198. p.fd = dv->fd;
  199. p.events = POLLIN | POLLERR | POLLHUP;
  200. if (poll(&p, 1, -1) < 0) {
  201. if (errno == EAGAIN || errno == EINTR)
  202. goto restart_poll;
  203. perror("Poll failed");
  204. return -EIO;
  205. }
  206. if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
  207. perror("Failed to get status");
  208. return -EIO;
  209. }
  210. #ifdef DV1394_DEBUG
  211. fprintf(stderr, "DV1394: status\n"
  212. "\tactive_frame\t%d\n"
  213. "\tfirst_clear_frame\t%d\n"
  214. "\tn_clear_frames\t%d\n"
  215. "\tdropped_frames\t%d\n",
  216. s.active_frame, s.first_clear_frame,
  217. s.n_clear_frames, s.dropped_frames);
  218. #endif
  219. dv->avail = s.n_clear_frames;
  220. dv->index = s.first_clear_frame;
  221. dv->done = 0;
  222. if (s.dropped_frames) {
  223. fprintf(stderr, "DV1394: Frame drop detected (%d). Reseting ..\n",
  224. s.dropped_frames);
  225. dv1394_reset(dv);
  226. dv1394_start(dv);
  227. }
  228. }
  229. #ifdef DV1394_DEBUG
  230. fprintf(stderr, "index %d, avail %d, done %d\n", dv->index, dv->avail,
  231. dv->done);
  232. #endif
  233. return __get_frame(dv, pkt);
  234. }
  235. static int dv1394_close(AVFormatContext * context)
  236. {
  237. struct dv1394_data *dv = context->priv_data;
  238. /* Shutdown DV1394 receiver */
  239. if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
  240. perror("Failed to shutdown DV1394");
  241. /* Unmap ring buffer */
  242. if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
  243. perror("Failed to munmap DV1394 ring buffer");
  244. close(dv->fd);
  245. return 0;
  246. }
  247. static AVInputFormat dv1394_format = {
  248. .name = "dv1394",
  249. .long_name = "dv1394 A/V grab",
  250. .priv_data_size = sizeof(struct dv1394_data),
  251. .read_header = dv1394_read_header,
  252. .read_packet = dv1394_read_packet,
  253. .read_close = dv1394_close,
  254. .flags = AVFMT_NOFILE
  255. };
  256. int dv1394_init(void)
  257. {
  258. av_register_input_format(&dv1394_format);
  259. return 0;
  260. }