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.

293 lines
7.5KB

  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. };
  44. static int dv1394_reset(struct dv1394_data *dv)
  45. {
  46. struct dv1394_init init;
  47. init.channel = dv->channel;
  48. init.api_version = DV1394_API_VERSION;
  49. init.n_frames = DV1394_RING_FRAMES;
  50. init.format = dv->format;
  51. if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
  52. return -1;
  53. dv->avail = dv->done = 0;
  54. dv->stream = 0;
  55. return 0;
  56. }
  57. static int dv1394_start(struct dv1394_data *dv)
  58. {
  59. /* Tell DV1394 driver to enable receiver */
  60. if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) {
  61. perror("Failed to start receiver");
  62. return -1;
  63. }
  64. return 0;
  65. }
  66. static int dv1394_read_header(AVFormatContext * context, AVFormatParameters * ap)
  67. {
  68. struct dv1394_data *dv = context->priv_data;
  69. AVStream *vst, *ast;
  70. const char *video_device;
  71. vst = av_new_stream(context, 0);
  72. if (!vst)
  73. return -ENOMEM;
  74. ast = av_new_stream(context, 1);
  75. if (!ast) {
  76. av_free(vst);
  77. return -ENOMEM;
  78. }
  79. dv->width = DV1394_WIDTH;
  80. dv->height = DV1394_HEIGHT;
  81. if (ap->channel)
  82. dv->channel = ap->channel;
  83. else
  84. dv->channel = DV1394_DEFAULT_CHANNEL;
  85. /* FIXME: Need a format change parameter */
  86. dv->format = DV1394_NTSC;
  87. if (dv->format == DV1394_NTSC) {
  88. dv->frame_size = DV1394_NTSC_FRAME_SIZE;
  89. dv->frame_rate = 30;
  90. } else {
  91. dv->frame_size = DV1394_PAL_FRAME_SIZE;
  92. dv->frame_rate = 25;
  93. }
  94. /* Open and initialize DV1394 device */
  95. video_device = ap->device;
  96. if (!video_device)
  97. video_device = "/dev/dv1394/0";
  98. dv->fd = open(video_device, O_RDONLY);
  99. if (dv->fd < 0) {
  100. perror("Failed to open DV interface");
  101. goto failed;
  102. }
  103. if (dv1394_reset(dv) < 0) {
  104. perror("Failed to initialize DV interface");
  105. goto failed;
  106. }
  107. dv->ring = mmap(NULL, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES,
  108. PROT_READ, MAP_PRIVATE, dv->fd, 0);
  109. if (!dv->ring) {
  110. perror("Failed to mmap DV ring buffer");
  111. goto failed;
  112. }
  113. dv->stream = 0;
  114. vst->codec.codec_type = CODEC_TYPE_VIDEO;
  115. vst->codec.codec_id = CODEC_ID_DVVIDEO;
  116. vst->codec.width = dv->width;
  117. vst->codec.height = dv->height;
  118. vst->codec.frame_rate = dv->frame_rate * FRAME_RATE_BASE;
  119. vst->codec.bit_rate = 25000000; /* Consumer DV is 25Mbps */
  120. ast->codec.codec_type = CODEC_TYPE_AUDIO;
  121. ast->codec.codec_id = CODEC_ID_DVAUDIO;
  122. ast->codec.channels = 2;
  123. ast->codec.sample_rate= 48000;
  124. av_set_pts_info(context, 48, 1, 1000000);
  125. if (dv1394_start(dv) < 0)
  126. goto failed;
  127. return 0;
  128. failed:
  129. close(dv->fd);
  130. av_free(vst);
  131. av_free(ast);
  132. return -EIO;
  133. }
  134. static void __destruct_pkt(struct AVPacket *pkt)
  135. {
  136. pkt->data = NULL; pkt->size = 0;
  137. return;
  138. }
  139. static inline int __get_frame(struct dv1394_data *dv, AVPacket *pkt)
  140. {
  141. char *ptr = dv->ring + (dv->index * dv->frame_size);
  142. if (dv->stream) {
  143. dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
  144. dv->done++; dv->avail--;
  145. } else {
  146. dv->pts = av_gettime() & ((1LL << 48) - 1);
  147. }
  148. av_init_packet(pkt);
  149. pkt->destruct = __destruct_pkt;
  150. pkt->data = ptr;
  151. pkt->size = dv->frame_size;
  152. pkt->pts = dv->pts;
  153. pkt->stream_index = dv->stream;
  154. dv->stream ^= 1;
  155. return dv->frame_size;
  156. }
  157. static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
  158. {
  159. struct dv1394_data *dv = context->priv_data;
  160. if (!dv->avail) {
  161. struct dv1394_status s;
  162. struct pollfd p;
  163. if (dv->done) {
  164. /* Request more frames */
  165. if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
  166. /* This usually means that ring buffer overflowed.
  167. * We have to reset :(.
  168. */
  169. fprintf(stderr, "DV1394: Ring buffer overflow. Reseting ..\n");
  170. dv1394_reset(dv);
  171. dv1394_start(dv);
  172. }
  173. dv->done = 0;
  174. }
  175. /* Wait until more frames are available */
  176. restart_poll:
  177. p.fd = dv->fd;
  178. p.events = POLLIN | POLLERR | POLLHUP;
  179. if (poll(&p, 1, -1) < 0) {
  180. if (errno == EAGAIN || errno == EINTR)
  181. goto restart_poll;
  182. perror("Poll failed");
  183. return -EIO;
  184. }
  185. if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
  186. perror("Failed to get status");
  187. return -EIO;
  188. }
  189. #ifdef DV1394_DEBUG
  190. fprintf(stderr, "DV1394: status\n"
  191. "\tactive_frame\t%d\n"
  192. "\tfirst_clear_frame\t%d\n"
  193. "\tn_clear_frames\t%d\n"
  194. "\tdropped_frames\t%d\n",
  195. s.active_frame, s.first_clear_frame,
  196. s.n_clear_frames, s.dropped_frames);
  197. #endif
  198. dv->avail = s.n_clear_frames;
  199. dv->index = s.first_clear_frame;
  200. dv->done = 0;
  201. if (s.dropped_frames) {
  202. fprintf(stderr, "DV1394: Frame drop detected (%d). Reseting ..\n",
  203. s.dropped_frames);
  204. dv1394_reset(dv);
  205. dv1394_start(dv);
  206. }
  207. }
  208. #ifdef DV1394_DEBUG
  209. fprintf(stderr, "index %d, avail %d, done %d\n", dv->index, dv->avail,
  210. dv->done);
  211. #endif
  212. return __get_frame(dv, pkt);
  213. }
  214. static int dv1394_close(AVFormatContext * context)
  215. {
  216. struct dv1394_data *dv = context->priv_data;
  217. /* Shutdown DV1394 receiver */
  218. if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
  219. perror("Failed to shutdown DV1394");
  220. /* Unmap ring buffer */
  221. if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
  222. perror("Failed to munmap DV1394 ring buffer");
  223. close(dv->fd);
  224. return 0;
  225. }
  226. static AVInputFormat dv1394_format = {
  227. .name = "dv1394",
  228. .long_name = "dv1394 A/V grab",
  229. .priv_data_size = sizeof(struct dv1394_data),
  230. .read_header = dv1394_read_header,
  231. .read_packet = dv1394_read_packet,
  232. .read_close = dv1394_close,
  233. .flags = AVFMT_NOFILE
  234. };
  235. int dv1394_init(void)
  236. {
  237. av_register_input_format(&dv1394_format);
  238. return 0;
  239. }