dv1394.c 7.7 KB

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