dv1394.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 <poll.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/mman.h>
  28. #include <sys/time.h>
  29. #include <time.h>
  30. #include "libavutil/log.h"
  31. #include "libavutil/opt.h"
  32. #include "avdevice.h"
  33. #include "libavformat/dv.h"
  34. #include "dv1394.h"
  35. struct dv1394_data {
  36. AVClass *class;
  37. int fd;
  38. int channel;
  39. int format;
  40. uint8_t *ring; /* Ring buffer */
  41. int index; /* Current frame index */
  42. int avail; /* Number of frames available for reading */
  43. int done; /* Number of completed frames */
  44. DVDemuxContext* dv_demux; /* Generic DV muxing/demuxing context */
  45. };
  46. /*
  47. * The trick here is to kludge around well known problem with kernel Ooopsing
  48. * when you try to capture PAL on a device node configure for NTSC. That's
  49. * why we have to configure the device node for PAL, and then read only NTSC
  50. * amount of data.
  51. */
  52. static int dv1394_reset(struct dv1394_data *dv)
  53. {
  54. struct dv1394_init init;
  55. init.channel = dv->channel;
  56. init.api_version = DV1394_API_VERSION;
  57. init.n_frames = DV1394_RING_FRAMES;
  58. init.format = DV1394_PAL;
  59. if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
  60. return -1;
  61. dv->avail = dv->done = 0;
  62. return 0;
  63. }
  64. static int dv1394_start(struct dv1394_data *dv)
  65. {
  66. /* Tell DV1394 driver to enable receiver */
  67. if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) {
  68. av_log(NULL, AV_LOG_ERROR, "Failed to start receiver: %s\n", strerror(errno));
  69. return -1;
  70. }
  71. return 0;
  72. }
  73. static int dv1394_read_header(AVFormatContext * context)
  74. {
  75. struct dv1394_data *dv = context->priv_data;
  76. dv->dv_demux = avpriv_dv_init_demux(context);
  77. if (!dv->dv_demux)
  78. goto failed;
  79. /* Open and initialize DV1394 device */
  80. dv->fd = open(context->filename, O_RDONLY);
  81. if (dv->fd < 0) {
  82. av_log(context, AV_LOG_ERROR, "Failed to open DV interface: %s\n", strerror(errno));
  83. goto failed;
  84. }
  85. if (dv1394_reset(dv) < 0) {
  86. av_log(context, AV_LOG_ERROR, "Failed to initialize DV interface: %s\n", strerror(errno));
  87. goto failed;
  88. }
  89. dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES,
  90. PROT_READ, MAP_PRIVATE, dv->fd, 0);
  91. if (dv->ring == MAP_FAILED) {
  92. av_log(context, AV_LOG_ERROR, "Failed to mmap DV ring buffer: %s\n", strerror(errno));
  93. goto failed;
  94. }
  95. if (dv1394_start(dv) < 0)
  96. goto failed;
  97. return 0;
  98. failed:
  99. close(dv->fd);
  100. return AVERROR(EIO);
  101. }
  102. static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
  103. {
  104. struct dv1394_data *dv = context->priv_data;
  105. int size;
  106. size = avpriv_dv_get_packet(dv->dv_demux, pkt);
  107. if (size > 0)
  108. return size;
  109. if (!dv->avail) {
  110. struct dv1394_status s;
  111. struct pollfd p;
  112. if (dv->done) {
  113. /* Request more frames */
  114. if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
  115. /* This usually means that ring buffer overflowed.
  116. * We have to reset :(.
  117. */
  118. av_log(context, AV_LOG_ERROR, "DV1394: Ring buffer overflow. Reseting ..\n");
  119. dv1394_reset(dv);
  120. dv1394_start(dv);
  121. }
  122. dv->done = 0;
  123. }
  124. /* Wait until more frames are available */
  125. restart_poll:
  126. p.fd = dv->fd;
  127. p.events = POLLIN | POLLERR | POLLHUP;
  128. if (poll(&p, 1, -1) < 0) {
  129. if (errno == EAGAIN || errno == EINTR)
  130. goto restart_poll;
  131. av_log(context, AV_LOG_ERROR, "Poll failed: %s\n", strerror(errno));
  132. return AVERROR(EIO);
  133. }
  134. if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
  135. av_log(context, AV_LOG_ERROR, "Failed to get status: %s\n", strerror(errno));
  136. return AVERROR(EIO);
  137. }
  138. av_dlog(context, "DV1394: status\n"
  139. "\tactive_frame\t%d\n"
  140. "\tfirst_clear_frame\t%d\n"
  141. "\tn_clear_frames\t%d\n"
  142. "\tdropped_frames\t%d\n",
  143. s.active_frame, s.first_clear_frame,
  144. s.n_clear_frames, s.dropped_frames);
  145. dv->avail = s.n_clear_frames;
  146. dv->index = s.first_clear_frame;
  147. dv->done = 0;
  148. if (s.dropped_frames) {
  149. av_log(context, AV_LOG_ERROR, "DV1394: Frame drop detected (%d). Reseting ..\n",
  150. s.dropped_frames);
  151. dv1394_reset(dv);
  152. dv1394_start(dv);
  153. }
  154. }
  155. av_dlog(context, "index %d, avail %d, done %d\n", dv->index, dv->avail,
  156. dv->done);
  157. size = avpriv_dv_produce_packet(dv->dv_demux, pkt,
  158. dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE),
  159. DV1394_PAL_FRAME_SIZE, -1);
  160. dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
  161. dv->done++; dv->avail--;
  162. return size;
  163. }
  164. static int dv1394_close(AVFormatContext * context)
  165. {
  166. struct dv1394_data *dv = context->priv_data;
  167. /* Shutdown DV1394 receiver */
  168. if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
  169. av_log(context, AV_LOG_ERROR, "Failed to shutdown DV1394: %s\n", strerror(errno));
  170. /* Unmap ring buffer */
  171. if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
  172. av_log(context, AV_LOG_ERROR, "Failed to munmap DV1394 ring buffer: %s\n", strerror(errno));
  173. close(dv->fd);
  174. av_free(dv->dv_demux);
  175. return 0;
  176. }
  177. static const AVOption options[] = {
  178. { "standard", "", offsetof(struct dv1394_data, format), AV_OPT_TYPE_INT, {.dbl = DV1394_NTSC}, DV1394_PAL, DV1394_NTSC, AV_OPT_FLAG_DECODING_PARAM, "standard" },
  179. { "PAL", "", 0, AV_OPT_TYPE_CONST, {.dbl = DV1394_PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
  180. { "NTSC", "", 0, AV_OPT_TYPE_CONST, {.dbl = DV1394_NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
  181. { "channel", "", offsetof(struct dv1394_data, channel), AV_OPT_TYPE_INT, {.dbl = DV1394_DEFAULT_CHANNEL}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  182. { NULL },
  183. };
  184. static const AVClass dv1394_class = {
  185. .class_name = "DV1394 indev",
  186. .item_name = av_default_item_name,
  187. .option = options,
  188. .version = LIBAVUTIL_VERSION_INT,
  189. };
  190. AVInputFormat ff_dv1394_demuxer = {
  191. .name = "dv1394",
  192. .long_name = NULL_IF_CONFIG_SMALL("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. .priv_class = &dv1394_class,
  199. };