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.

252 lines
6.7KB

  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. #include "dv.h"
  31. struct dv1394_data {
  32. int fd;
  33. int channel;
  34. int format;
  35. void *ring; /* Ring buffer */
  36. int index; /* Current frame index */
  37. int avail; /* Number of frames available for reading */
  38. int done; /* Number of completed frames */
  39. int64_t pts; /* Current timestamp */
  40. DVDemuxContext* dv_demux; /* Generic DV muxing/demuxing context */
  41. };
  42. /*
  43. * The trick here is to kludge around well known problem with kernel Ooopsing
  44. * when you try to capture PAL on a device node configure for NTSC. That's
  45. * why we have to configure the device node for PAL, and then read only NTSC
  46. * amount of data.
  47. */
  48. static int dv1394_reset(struct dv1394_data *dv)
  49. {
  50. struct dv1394_init init;
  51. init.channel = dv->channel;
  52. init.api_version = DV1394_API_VERSION;
  53. init.n_frames = DV1394_RING_FRAMES;
  54. init.format = DV1394_PAL;
  55. if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
  56. return -1;
  57. dv->avail = dv->done = 0;
  58. return 0;
  59. }
  60. static int dv1394_start(struct dv1394_data *dv)
  61. {
  62. /* Tell DV1394 driver to enable receiver */
  63. if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) {
  64. perror("Failed to start receiver");
  65. return -1;
  66. }
  67. return 0;
  68. }
  69. static int dv1394_read_header(AVFormatContext * context, AVFormatParameters * ap)
  70. {
  71. struct dv1394_data *dv = context->priv_data;
  72. const char *video_device;
  73. dv->dv_demux = dv_init_demux(context);
  74. if (!dv->dv_demux)
  75. goto failed;
  76. if (ap->standard && !strcasecmp(ap->standard, "pal"))
  77. dv->format = DV1394_PAL;
  78. else
  79. dv->format = DV1394_NTSC;
  80. if (ap->channel)
  81. dv->channel = ap->channel;
  82. else
  83. dv->channel = DV1394_DEFAULT_CHANNEL;
  84. /* Open and initialize DV1394 device */
  85. video_device = ap->device;
  86. if (!video_device)
  87. video_device = "/dev/dv1394/0";
  88. dv->fd = open(video_device, O_RDONLY);
  89. if (dv->fd < 0) {
  90. perror("Failed to open DV interface");
  91. goto failed;
  92. }
  93. if (dv1394_reset(dv) < 0) {
  94. perror("Failed to initialize DV interface");
  95. goto failed;
  96. }
  97. dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES,
  98. PROT_READ, MAP_PRIVATE, dv->fd, 0);
  99. if (dv->ring == MAP_FAILED) {
  100. perror("Failed to mmap DV ring buffer");
  101. goto failed;
  102. }
  103. av_set_pts_info(context, 48, 1, 1000000);
  104. if (dv1394_start(dv) < 0)
  105. goto failed;
  106. return 0;
  107. failed:
  108. close(dv->fd);
  109. return -EIO;
  110. }
  111. static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
  112. {
  113. struct dv1394_data *dv = context->priv_data;
  114. int size;
  115. size = dv_get_packet(dv->dv_demux, pkt);
  116. if (size > 0)
  117. goto out;
  118. if (!dv->avail) {
  119. struct dv1394_status s;
  120. struct pollfd p;
  121. if (dv->done) {
  122. /* Request more frames */
  123. if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
  124. /* This usually means that ring buffer overflowed.
  125. * We have to reset :(.
  126. */
  127. av_log(context, AV_LOG_ERROR, "DV1394: Ring buffer overflow. Reseting ..\n");
  128. dv1394_reset(dv);
  129. dv1394_start(dv);
  130. }
  131. dv->done = 0;
  132. }
  133. /* Wait until more frames are available */
  134. restart_poll:
  135. p.fd = dv->fd;
  136. p.events = POLLIN | POLLERR | POLLHUP;
  137. if (poll(&p, 1, -1) < 0) {
  138. if (errno == EAGAIN || errno == EINTR)
  139. goto restart_poll;
  140. perror("Poll failed");
  141. return -EIO;
  142. }
  143. if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
  144. perror("Failed to get status");
  145. return -EIO;
  146. }
  147. #ifdef DV1394_DEBUG
  148. fprintf(stderr, "DV1394: status\n"
  149. "\tactive_frame\t%d\n"
  150. "\tfirst_clear_frame\t%d\n"
  151. "\tn_clear_frames\t%d\n"
  152. "\tdropped_frames\t%d\n",
  153. s.active_frame, s.first_clear_frame,
  154. s.n_clear_frames, s.dropped_frames);
  155. #endif
  156. dv->avail = s.n_clear_frames;
  157. dv->index = s.first_clear_frame;
  158. dv->done = 0;
  159. if (s.dropped_frames) {
  160. av_log(context, AV_LOG_ERROR, "DV1394: Frame drop detected (%d). Reseting ..\n",
  161. s.dropped_frames);
  162. dv1394_reset(dv);
  163. dv1394_start(dv);
  164. }
  165. }
  166. #ifdef DV1394_DEBUG
  167. fprintf(stderr, "index %d, avail %d, done %d\n", dv->index, dv->avail,
  168. dv->done);
  169. #endif
  170. size = dv_produce_packet(dv->dv_demux, pkt,
  171. dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE),
  172. DV1394_PAL_FRAME_SIZE);
  173. dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
  174. dv->done++; dv->avail--;
  175. dv->pts = av_gettime() & ((1LL << 48) - 1);
  176. out:
  177. pkt->pts = dv->pts;
  178. return size;
  179. }
  180. static int dv1394_close(AVFormatContext * context)
  181. {
  182. struct dv1394_data *dv = context->priv_data;
  183. /* Shutdown DV1394 receiver */
  184. if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
  185. perror("Failed to shutdown DV1394");
  186. /* Unmap ring buffer */
  187. if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
  188. perror("Failed to munmap DV1394 ring buffer");
  189. close(dv->fd);
  190. av_free(dv->dv_demux);
  191. return 0;
  192. }
  193. static AVInputFormat dv1394_format = {
  194. .name = "dv1394",
  195. .long_name = "dv1394 A/V grab",
  196. .priv_data_size = sizeof(struct dv1394_data),
  197. .read_header = dv1394_read_header,
  198. .read_packet = dv1394_read_packet,
  199. .read_close = dv1394_close,
  200. .flags = AVFMT_NOFILE
  201. };
  202. int dv1394_init(void)
  203. {
  204. av_register_input_format(&dv1394_format);
  205. return 0;
  206. }