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