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.

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