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.

238 lines
6.8KB

  1. /*
  2. * Linux DV1394 interface
  3. * Copyright (c) 2003 Max Krasnyansky <maxk@qualcomm.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <errno.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/mman.h>
  27. #include <sys/poll.h>
  28. #include <sys/time.h>
  29. #include <time.h>
  30. #include "avformat.h"
  31. #undef DV1394_DEBUG
  32. #include "dv1394.h"
  33. #include "dv.h"
  34. struct dv1394_data {
  35. int fd;
  36. int channel;
  37. int format;
  38. uint8_t *ring; /* Ring buffer */
  39. int index; /* Current frame index */
  40. int avail; /* Number of frames available for reading */
  41. int done; /* Number of completed frames */
  42. DVDemuxContext* dv_demux; /* Generic DV muxing/demuxing context */
  43. };
  44. /*
  45. * The trick here is to kludge around well known problem with kernel Ooopsing
  46. * when you try to capture PAL on a device node configure for NTSC. That's
  47. * why we have to configure the device node for PAL, and then read only NTSC
  48. * amount of data.
  49. */
  50. static int dv1394_reset(struct dv1394_data *dv)
  51. {
  52. struct dv1394_init init;
  53. init.channel = dv->channel;
  54. init.api_version = DV1394_API_VERSION;
  55. init.n_frames = DV1394_RING_FRAMES;
  56. init.format = DV1394_PAL;
  57. if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
  58. return -1;
  59. dv->avail = dv->done = 0;
  60. return 0;
  61. }
  62. static int dv1394_start(struct dv1394_data *dv)
  63. {
  64. /* Tell DV1394 driver to enable receiver */
  65. if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) {
  66. av_log(NULL, AV_LOG_ERROR, "Failed to start receiver: %s\n", strerror(errno));
  67. return -1;
  68. }
  69. return 0;
  70. }
  71. static int dv1394_read_header(AVFormatContext * context, AVFormatParameters * ap)
  72. {
  73. struct dv1394_data *dv = context->priv_data;
  74. dv->dv_demux = dv_init_demux(context);
  75. if (!dv->dv_demux)
  76. goto failed;
  77. if (ap->standard && !strcasecmp(ap->standard, "pal"))
  78. dv->format = DV1394_PAL;
  79. else
  80. dv->format = DV1394_NTSC;
  81. if (ap->channel)
  82. dv->channel = ap->channel;
  83. else
  84. dv->channel = DV1394_DEFAULT_CHANNEL;
  85. /* Open and initialize DV1394 device */
  86. dv->fd = open(context->filename, O_RDONLY);
  87. if (dv->fd < 0) {
  88. av_log(context, AV_LOG_ERROR, "Failed to open DV interface: %s\n", strerror(errno));
  89. goto failed;
  90. }
  91. if (dv1394_reset(dv) < 0) {
  92. av_log(context, AV_LOG_ERROR, "Failed to initialize DV interface: %s\n", strerror(errno));
  93. goto failed;
  94. }
  95. dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES,
  96. PROT_READ, MAP_PRIVATE, dv->fd, 0);
  97. if (dv->ring == MAP_FAILED) {
  98. av_log(context, AV_LOG_ERROR, "Failed to mmap DV ring buffer: %s\n", strerror(errno));
  99. goto failed;
  100. }
  101. if (dv1394_start(dv) < 0)
  102. goto failed;
  103. return 0;
  104. failed:
  105. close(dv->fd);
  106. return AVERROR(EIO);
  107. }
  108. static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
  109. {
  110. struct dv1394_data *dv = context->priv_data;
  111. int size;
  112. size = dv_get_packet(dv->dv_demux, pkt);
  113. if (size > 0)
  114. return size;
  115. if (!dv->avail) {
  116. struct dv1394_status s;
  117. struct pollfd p;
  118. if (dv->done) {
  119. /* Request more frames */
  120. if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
  121. /* This usually means that ring buffer overflowed.
  122. * We have to reset :(.
  123. */
  124. av_log(context, AV_LOG_ERROR, "DV1394: Ring buffer overflow. Reseting ..\n");
  125. dv1394_reset(dv);
  126. dv1394_start(dv);
  127. }
  128. dv->done = 0;
  129. }
  130. /* Wait until more frames are available */
  131. restart_poll:
  132. p.fd = dv->fd;
  133. p.events = POLLIN | POLLERR | POLLHUP;
  134. if (poll(&p, 1, -1) < 0) {
  135. if (errno == EAGAIN || errno == EINTR)
  136. goto restart_poll;
  137. av_log(context, AV_LOG_ERROR, "Poll failed: %s\n", strerror(errno));
  138. return AVERROR(EIO);
  139. }
  140. if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
  141. av_log(context, AV_LOG_ERROR, "Failed to get status: %s\n", strerror(errno));
  142. return AVERROR(EIO);
  143. }
  144. #ifdef DV1394_DEBUG
  145. av_log(context, AV_LOG_DEBUG, "DV1394: status\n"
  146. "\tactive_frame\t%d\n"
  147. "\tfirst_clear_frame\t%d\n"
  148. "\tn_clear_frames\t%d\n"
  149. "\tdropped_frames\t%d\n",
  150. s.active_frame, s.first_clear_frame,
  151. s.n_clear_frames, s.dropped_frames);
  152. #endif
  153. dv->avail = s.n_clear_frames;
  154. dv->index = s.first_clear_frame;
  155. dv->done = 0;
  156. if (s.dropped_frames) {
  157. av_log(context, AV_LOG_ERROR, "DV1394: Frame drop detected (%d). Reseting ..\n",
  158. s.dropped_frames);
  159. dv1394_reset(dv);
  160. dv1394_start(dv);
  161. }
  162. }
  163. #ifdef DV1394_DEBUG
  164. av_log(context, AV_LOG_DEBUG, "index %d, avail %d, done %d\n", dv->index, dv->avail,
  165. dv->done);
  166. #endif
  167. size = dv_produce_packet(dv->dv_demux, pkt,
  168. dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE),
  169. DV1394_PAL_FRAME_SIZE);
  170. dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
  171. dv->done++; dv->avail--;
  172. return size;
  173. }
  174. static int dv1394_close(AVFormatContext * context)
  175. {
  176. struct dv1394_data *dv = context->priv_data;
  177. /* Shutdown DV1394 receiver */
  178. if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
  179. av_log(context, AV_LOG_ERROR, "Failed to shutdown DV1394: %s\n", strerror(errno));
  180. /* Unmap ring buffer */
  181. if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
  182. av_log(context, AV_LOG_ERROR, "Failed to munmap DV1394 ring buffer: %s\n", strerror(errno));
  183. close(dv->fd);
  184. av_free(dv->dv_demux);
  185. return 0;
  186. }
  187. AVInputFormat dv1394_demuxer = {
  188. .name = "dv1394",
  189. .long_name = "dv1394 A/V grab",
  190. .priv_data_size = sizeof(struct dv1394_data),
  191. .read_header = dv1394_read_header,
  192. .read_packet = dv1394_read_packet,
  193. .read_close = dv1394_close,
  194. .flags = AVFMT_NOFILE
  195. };