dv1394.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 "libavutil/log.h"
  29. #include "libavutil/opt.h"
  30. #include "avdevice.h"
  31. #include "libavformat/dv.h"
  32. #include "dv1394.h"
  33. struct dv1394_data {
  34. AVClass *class;
  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)
  72. {
  73. struct dv1394_data *dv = context->priv_data;
  74. dv->dv_demux = avpriv_dv_init_demux(context);
  75. if (!dv->dv_demux)
  76. goto failed;
  77. /* Open and initialize DV1394 device */
  78. dv->fd = open(context->filename, O_RDONLY);
  79. if (dv->fd < 0) {
  80. av_log(context, AV_LOG_ERROR, "Failed to open DV interface: %s\n", strerror(errno));
  81. goto failed;
  82. }
  83. if (dv1394_reset(dv) < 0) {
  84. av_log(context, AV_LOG_ERROR, "Failed to initialize DV interface: %s\n", strerror(errno));
  85. goto failed;
  86. }
  87. dv->ring = mmap(NULL, DV1394_PAL_FRAME_SIZE * DV1394_RING_FRAMES,
  88. PROT_READ, MAP_PRIVATE, dv->fd, 0);
  89. if (dv->ring == MAP_FAILED) {
  90. av_log(context, AV_LOG_ERROR, "Failed to mmap DV ring buffer: %s\n", strerror(errno));
  91. goto failed;
  92. }
  93. if (dv1394_start(dv) < 0)
  94. goto failed;
  95. return 0;
  96. failed:
  97. close(dv->fd);
  98. return AVERROR(EIO);
  99. }
  100. static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
  101. {
  102. struct dv1394_data *dv = context->priv_data;
  103. int size;
  104. size = avpriv_dv_get_packet(dv->dv_demux, pkt);
  105. if (size > 0)
  106. return size;
  107. if (!dv->avail) {
  108. struct dv1394_status s;
  109. struct pollfd p;
  110. if (dv->done) {
  111. /* Request more frames */
  112. if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
  113. /* This usually means that ring buffer overflowed.
  114. * We have to reset :(.
  115. */
  116. av_log(context, AV_LOG_ERROR, "DV1394: Ring buffer overflow. Reseting ..\n");
  117. dv1394_reset(dv);
  118. dv1394_start(dv);
  119. }
  120. dv->done = 0;
  121. }
  122. /* Wait until more frames are available */
  123. restart_poll:
  124. p.fd = dv->fd;
  125. p.events = POLLIN | POLLERR | POLLHUP;
  126. if (poll(&p, 1, -1) < 0) {
  127. if (errno == EAGAIN || errno == EINTR)
  128. goto restart_poll;
  129. av_log(context, AV_LOG_ERROR, "Poll failed: %s\n", strerror(errno));
  130. return AVERROR(EIO);
  131. }
  132. if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
  133. av_log(context, AV_LOG_ERROR, "Failed to get status: %s\n", strerror(errno));
  134. return AVERROR(EIO);
  135. }
  136. av_dlog(context, "DV1394: status\n"
  137. "\tactive_frame\t%d\n"
  138. "\tfirst_clear_frame\t%d\n"
  139. "\tn_clear_frames\t%d\n"
  140. "\tdropped_frames\t%d\n",
  141. s.active_frame, s.first_clear_frame,
  142. s.n_clear_frames, s.dropped_frames);
  143. dv->avail = s.n_clear_frames;
  144. dv->index = s.first_clear_frame;
  145. dv->done = 0;
  146. if (s.dropped_frames) {
  147. av_log(context, AV_LOG_ERROR, "DV1394: Frame drop detected (%d). Reseting ..\n",
  148. s.dropped_frames);
  149. dv1394_reset(dv);
  150. dv1394_start(dv);
  151. }
  152. }
  153. av_dlog(context, "index %d, avail %d, done %d\n", dv->index, dv->avail,
  154. dv->done);
  155. size = avpriv_dv_produce_packet(dv->dv_demux, pkt,
  156. dv->ring + (dv->index * DV1394_PAL_FRAME_SIZE),
  157. DV1394_PAL_FRAME_SIZE, -1);
  158. dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
  159. dv->done++; dv->avail--;
  160. return size;
  161. }
  162. static int dv1394_close(AVFormatContext * context)
  163. {
  164. struct dv1394_data *dv = context->priv_data;
  165. /* Shutdown DV1394 receiver */
  166. if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
  167. av_log(context, AV_LOG_ERROR, "Failed to shutdown DV1394: %s\n", strerror(errno));
  168. /* Unmap ring buffer */
  169. if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
  170. av_log(context, AV_LOG_ERROR, "Failed to munmap DV1394 ring buffer: %s\n", strerror(errno));
  171. close(dv->fd);
  172. av_free(dv->dv_demux);
  173. return 0;
  174. }
  175. static const AVOption options[] = {
  176. { "standard", "", offsetof(struct dv1394_data, format), AV_OPT_TYPE_INT, {.i64 = DV1394_NTSC}, DV1394_PAL, DV1394_NTSC, AV_OPT_FLAG_DECODING_PARAM, "standard" },
  177. { "PAL", "", 0, AV_OPT_TYPE_CONST, {.i64 = DV1394_PAL}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
  178. { "NTSC", "", 0, AV_OPT_TYPE_CONST, {.i64 = DV1394_NTSC}, 0, 0, AV_OPT_FLAG_DECODING_PARAM, "standard" },
  179. { "channel", "", offsetof(struct dv1394_data, channel), AV_OPT_TYPE_INT, {.i64 = DV1394_DEFAULT_CHANNEL}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
  180. { NULL },
  181. };
  182. static const AVClass dv1394_class = {
  183. .class_name = "DV1394 indev",
  184. .item_name = av_default_item_name,
  185. .option = options,
  186. .version = LIBAVUTIL_VERSION_INT,
  187. };
  188. AVInputFormat ff_dv1394_demuxer = {
  189. .name = "dv1394",
  190. .long_name = NULL_IF_CONFIG_SMALL("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. .priv_class = &dv1394_class,
  197. };