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.

295 lines
7.6KB

  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. pkt->flags |= PKT_FLAG_KEY;
  156. dv->stream ^= 1;
  157. return dv->frame_size;
  158. }
  159. static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
  160. {
  161. struct dv1394_data *dv = context->priv_data;
  162. if (!dv->avail) {
  163. struct dv1394_status s;
  164. struct pollfd p;
  165. if (dv->done) {
  166. /* Request more frames */
  167. if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
  168. /* This usually means that ring buffer overflowed.
  169. * We have to reset :(.
  170. */
  171. fprintf(stderr, "DV1394: Ring buffer overflow. Reseting ..\n");
  172. dv1394_reset(dv);
  173. dv1394_start(dv);
  174. }
  175. dv->done = 0;
  176. }
  177. /* Wait until more frames are available */
  178. restart_poll:
  179. p.fd = dv->fd;
  180. p.events = POLLIN | POLLERR | POLLHUP;
  181. if (poll(&p, 1, -1) < 0) {
  182. if (errno == EAGAIN || errno == EINTR)
  183. goto restart_poll;
  184. perror("Poll failed");
  185. return -EIO;
  186. }
  187. if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
  188. perror("Failed to get status");
  189. return -EIO;
  190. }
  191. #ifdef DV1394_DEBUG
  192. fprintf(stderr, "DV1394: status\n"
  193. "\tactive_frame\t%d\n"
  194. "\tfirst_clear_frame\t%d\n"
  195. "\tn_clear_frames\t%d\n"
  196. "\tdropped_frames\t%d\n",
  197. s.active_frame, s.first_clear_frame,
  198. s.n_clear_frames, s.dropped_frames);
  199. #endif
  200. dv->avail = s.n_clear_frames;
  201. dv->index = s.first_clear_frame;
  202. dv->done = 0;
  203. if (s.dropped_frames) {
  204. fprintf(stderr, "DV1394: Frame drop detected (%d). Reseting ..\n",
  205. s.dropped_frames);
  206. dv1394_reset(dv);
  207. dv1394_start(dv);
  208. }
  209. }
  210. #ifdef DV1394_DEBUG
  211. fprintf(stderr, "index %d, avail %d, done %d\n", dv->index, dv->avail,
  212. dv->done);
  213. #endif
  214. return __get_frame(dv, pkt);
  215. }
  216. static int dv1394_close(AVFormatContext * context)
  217. {
  218. struct dv1394_data *dv = context->priv_data;
  219. /* Shutdown DV1394 receiver */
  220. if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
  221. perror("Failed to shutdown DV1394");
  222. /* Unmap ring buffer */
  223. if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
  224. perror("Failed to munmap DV1394 ring buffer");
  225. close(dv->fd);
  226. return 0;
  227. }
  228. static AVInputFormat dv1394_format = {
  229. .name = "dv1394",
  230. .long_name = "dv1394 A/V grab",
  231. .priv_data_size = sizeof(struct dv1394_data),
  232. .read_header = dv1394_read_header,
  233. .read_packet = dv1394_read_packet,
  234. .read_close = dv1394_close,
  235. .flags = AVFMT_NOFILE
  236. };
  237. int dv1394_init(void)
  238. {
  239. av_register_input_format(&dv1394_format);
  240. return 0;
  241. }