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.

250 lines
6.3KB

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