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.

294 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;
  119. vst->codec.frame_rate_base = 1;
  120. vst->codec.bit_rate = 25000000; /* Consumer DV is 25Mbps */
  121. ast->codec.codec_type = CODEC_TYPE_AUDIO;
  122. ast->codec.codec_id = CODEC_ID_DVAUDIO;
  123. ast->codec.channels = 2;
  124. ast->codec.sample_rate= 48000;
  125. av_set_pts_info(context, 48, 1, 1000000);
  126. if (dv1394_start(dv) < 0)
  127. goto failed;
  128. return 0;
  129. failed:
  130. close(dv->fd);
  131. av_free(vst);
  132. av_free(ast);
  133. return -EIO;
  134. }
  135. static void __destruct_pkt(struct AVPacket *pkt)
  136. {
  137. pkt->data = NULL; pkt->size = 0;
  138. return;
  139. }
  140. static inline int __get_frame(struct dv1394_data *dv, AVPacket *pkt)
  141. {
  142. char *ptr = dv->ring + (dv->index * dv->frame_size);
  143. if (dv->stream) {
  144. dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
  145. dv->done++; dv->avail--;
  146. } else {
  147. dv->pts = av_gettime() & ((1LL << 48) - 1);
  148. }
  149. av_init_packet(pkt);
  150. pkt->destruct = __destruct_pkt;
  151. pkt->data = ptr;
  152. pkt->size = dv->frame_size;
  153. pkt->pts = dv->pts;
  154. pkt->stream_index = dv->stream;
  155. dv->stream ^= 1;
  156. return dv->frame_size;
  157. }
  158. static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
  159. {
  160. struct dv1394_data *dv = context->priv_data;
  161. if (!dv->avail) {
  162. struct dv1394_status s;
  163. struct pollfd p;
  164. if (dv->done) {
  165. /* Request more frames */
  166. if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
  167. /* This usually means that ring buffer overflowed.
  168. * We have to reset :(.
  169. */
  170. fprintf(stderr, "DV1394: Ring buffer overflow. Reseting ..\n");
  171. dv1394_reset(dv);
  172. dv1394_start(dv);
  173. }
  174. dv->done = 0;
  175. }
  176. /* Wait until more frames are available */
  177. restart_poll:
  178. p.fd = dv->fd;
  179. p.events = POLLIN | POLLERR | POLLHUP;
  180. if (poll(&p, 1, -1) < 0) {
  181. if (errno == EAGAIN || errno == EINTR)
  182. goto restart_poll;
  183. perror("Poll failed");
  184. return -EIO;
  185. }
  186. if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
  187. perror("Failed to get status");
  188. return -EIO;
  189. }
  190. #ifdef DV1394_DEBUG
  191. fprintf(stderr, "DV1394: status\n"
  192. "\tactive_frame\t%d\n"
  193. "\tfirst_clear_frame\t%d\n"
  194. "\tn_clear_frames\t%d\n"
  195. "\tdropped_frames\t%d\n",
  196. s.active_frame, s.first_clear_frame,
  197. s.n_clear_frames, s.dropped_frames);
  198. #endif
  199. dv->avail = s.n_clear_frames;
  200. dv->index = s.first_clear_frame;
  201. dv->done = 0;
  202. if (s.dropped_frames) {
  203. fprintf(stderr, "DV1394: Frame drop detected (%d). Reseting ..\n",
  204. s.dropped_frames);
  205. dv1394_reset(dv);
  206. dv1394_start(dv);
  207. }
  208. }
  209. #ifdef DV1394_DEBUG
  210. fprintf(stderr, "index %d, avail %d, done %d\n", dv->index, dv->avail,
  211. dv->done);
  212. #endif
  213. return __get_frame(dv, pkt);
  214. }
  215. static int dv1394_close(AVFormatContext * context)
  216. {
  217. struct dv1394_data *dv = context->priv_data;
  218. /* Shutdown DV1394 receiver */
  219. if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
  220. perror("Failed to shutdown DV1394");
  221. /* Unmap ring buffer */
  222. if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
  223. perror("Failed to munmap DV1394 ring buffer");
  224. close(dv->fd);
  225. return 0;
  226. }
  227. static AVInputFormat dv1394_format = {
  228. .name = "dv1394",
  229. .long_name = "dv1394 A/V grab",
  230. .priv_data_size = sizeof(struct dv1394_data),
  231. .read_header = dv1394_read_header,
  232. .read_packet = dv1394_read_packet,
  233. .read_close = dv1394_close,
  234. .flags = AVFMT_NOFILE
  235. };
  236. int dv1394_init(void)
  237. {
  238. av_register_input_format(&dv1394_format);
  239. return 0;
  240. }