dv1394.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. 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. dv->fd = open(context->filename, O_RDONLY);
  86. if (dv->fd < 0) {
  87. perror("Failed to open DV interface");
  88. goto failed;
  89. }
  90. if (dv1394_reset(dv) < 0) {
  91. perror("Failed to initialize DV interface");
  92. goto failed;
  93. }
  94. dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES,
  95. PROT_READ, MAP_PRIVATE, dv->fd, 0);
  96. if (dv->ring == MAP_FAILED) {
  97. perror("Failed to mmap DV ring buffer");
  98. goto failed;
  99. }
  100. if (dv1394_start(dv) < 0)
  101. goto failed;
  102. return 0;
  103. failed:
  104. close(dv->fd);
  105. return AVERROR_IO;
  106. }
  107. static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
  108. {
  109. struct dv1394_data *dv = context->priv_data;
  110. int size;
  111. size = dv_get_packet(dv->dv_demux, pkt);
  112. if (size > 0)
  113. return size;
  114. if (!dv->avail) {
  115. struct dv1394_status s;
  116. struct pollfd p;
  117. if (dv->done) {
  118. /* Request more frames */
  119. if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
  120. /* This usually means that ring buffer overflowed.
  121. * We have to reset :(.
  122. */
  123. av_log(context, AV_LOG_ERROR, "DV1394: Ring buffer overflow. Reseting ..\n");
  124. dv1394_reset(dv);
  125. dv1394_start(dv);
  126. }
  127. dv->done = 0;
  128. }
  129. /* Wait until more frames are available */
  130. restart_poll:
  131. p.fd = dv->fd;
  132. p.events = POLLIN | POLLERR | POLLHUP;
  133. if (poll(&p, 1, -1) < 0) {
  134. if (errno == EAGAIN || errno == EINTR)
  135. goto restart_poll;
  136. perror("Poll failed");
  137. return AVERROR_IO;
  138. }
  139. if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
  140. perror("Failed to get status");
  141. return AVERROR_IO;
  142. }
  143. #ifdef DV1394_DEBUG
  144. av_log(context, AV_LOG_DEBUG, "DV1394: status\n"
  145. "\tactive_frame\t%d\n"
  146. "\tfirst_clear_frame\t%d\n"
  147. "\tn_clear_frames\t%d\n"
  148. "\tdropped_frames\t%d\n",
  149. s.active_frame, s.first_clear_frame,
  150. s.n_clear_frames, s.dropped_frames);
  151. #endif
  152. dv->avail = s.n_clear_frames;
  153. dv->index = s.first_clear_frame;
  154. dv->done = 0;
  155. if (s.dropped_frames) {
  156. av_log(context, AV_LOG_ERROR, "DV1394: Frame drop detected (%d). Reseting ..\n",
  157. s.dropped_frames);
  158. dv1394_reset(dv);
  159. dv1394_start(dv);
  160. }
  161. }
  162. #ifdef DV1394_DEBUG
  163. av_log(context, AV_LOG_DEBUG, "index %d, avail %d, done %d\n", dv->index, dv->avail,
  164. dv->done);
  165. #endif
  166. size = dv_produce_packet(dv->dv_demux, pkt,
  167. dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE),
  168. DV1394_PAL_FRAME_SIZE);
  169. dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
  170. dv->done++; dv->avail--;
  171. return size;
  172. }
  173. static int dv1394_close(AVFormatContext * context)
  174. {
  175. struct dv1394_data *dv = context->priv_data;
  176. /* Shutdown DV1394 receiver */
  177. if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
  178. perror("Failed to shutdown DV1394");
  179. /* Unmap ring buffer */
  180. if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
  181. perror("Failed to munmap DV1394 ring buffer");
  182. close(dv->fd);
  183. av_free(dv->dv_demux);
  184. return 0;
  185. }
  186. AVInputFormat dv1394_demuxer = {
  187. .name = "dv1394",
  188. .long_name = "dv1394 A/V grab",
  189. .priv_data_size = sizeof(struct dv1394_data),
  190. .read_header = dv1394_read_header,
  191. .read_packet = dv1394_read_packet,
  192. .read_close = dv1394_close,
  193. .flags = AVFMT_NOFILE
  194. };