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.

245 lines
6.5KB

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