dv1394.c 7.6 KB

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