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.

241 lines
6.5KB

  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 <unistd.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/mman.h>
  26. #include <sys/poll.h>
  27. #include <sys/time.h>
  28. #include <time.h>
  29. #include "avformat.h"
  30. #undef DV1394_DEBUG
  31. #include "dv1394.h"
  32. #include "dv.h"
  33. struct dv1394_data {
  34. int fd;
  35. int channel;
  36. int format;
  37. uint8_t *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. DVDemuxContext* dv_demux; /* Generic DV muxing/demuxing context */
  42. };
  43. /*
  44. * The trick here is to kludge around well known problem with kernel Ooopsing
  45. * when you try to capture PAL on a device node configure for NTSC. That's
  46. * why we have to configure the device node for PAL, and then read only NTSC
  47. * amount of data.
  48. */
  49. static int dv1394_reset(struct dv1394_data *dv)
  50. {
  51. struct dv1394_init init;
  52. init.channel = dv->channel;
  53. init.api_version = DV1394_API_VERSION;
  54. init.n_frames = DV1394_RING_FRAMES;
  55. init.format = DV1394_PAL;
  56. if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
  57. return -1;
  58. dv->avail = dv->done = 0;
  59. return 0;
  60. }
  61. static int dv1394_start(struct dv1394_data *dv)
  62. {
  63. /* Tell DV1394 driver to enable receiver */
  64. if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) {
  65. perror("Failed to start receiver");
  66. return -1;
  67. }
  68. return 0;
  69. }
  70. static int dv1394_read_header(AVFormatContext * context, AVFormatParameters * ap)
  71. {
  72. struct dv1394_data *dv = context->priv_data;
  73. const char *video_device;
  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. video_device = ap->device;
  87. if (!video_device)
  88. video_device = "/dev/dv1394/0";
  89. dv->fd = open(video_device, O_RDONLY);
  90. if (dv->fd < 0) {
  91. perror("Failed to open DV interface");
  92. goto failed;
  93. }
  94. if (dv1394_reset(dv) < 0) {
  95. perror("Failed to initialize DV interface");
  96. goto failed;
  97. }
  98. dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES,
  99. PROT_READ, MAP_PRIVATE, dv->fd, 0);
  100. if (dv->ring == MAP_FAILED) {
  101. perror("Failed to mmap DV ring buffer");
  102. goto failed;
  103. }
  104. if (dv1394_start(dv) < 0)
  105. goto failed;
  106. return 0;
  107. failed:
  108. close(dv->fd);
  109. return AVERROR_IO;
  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. return size;
  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 AVERROR_IO;
  142. }
  143. if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
  144. perror("Failed to get status");
  145. return AVERROR_IO;
  146. }
  147. #ifdef DV1394_DEBUG
  148. av_log(context, AV_LOG_DEBUG, "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. av_log(context, AV_LOG_DEBUG, "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. return size;
  176. }
  177. static int dv1394_close(AVFormatContext * context)
  178. {
  179. struct dv1394_data *dv = context->priv_data;
  180. /* Shutdown DV1394 receiver */
  181. if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
  182. perror("Failed to shutdown DV1394");
  183. /* Unmap ring buffer */
  184. if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
  185. perror("Failed to munmap DV1394 ring buffer");
  186. close(dv->fd);
  187. av_free(dv->dv_demux);
  188. return 0;
  189. }
  190. AVInputFormat dv1394_demuxer = {
  191. .name = "dv1394",
  192. .long_name = "dv1394 A/V grab",
  193. .priv_data_size = sizeof(struct dv1394_data),
  194. .read_header = dv1394_read_header,
  195. .read_packet = dv1394_read_packet,
  196. .read_close = dv1394_close,
  197. .flags = AVFMT_NOFILE
  198. };