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.

284 lines
7.2KB

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