dv1394.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * DV input/output over IEEE 1394 on OHCI chips
  3. * Copyright (C)2001 Daniel Maas <dmaas@dcine.com>
  4. * receive, proc_fs by Dan Dennedy <dan@dennedy.org>
  5. *
  6. * based on:
  7. * video1394.h - driver for OHCI 1394 boards
  8. * Copyright (C)1999,2000 Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>
  9. * Peter Schlaile <udbz@rz.uni-karlsruhe.de>
  10. *
  11. * This file is part of FFmpeg.
  12. *
  13. * FFmpeg is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 2.1 of the License, or (at your option) any later version.
  17. *
  18. * FFmpeg is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with FFmpeg; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. #ifndef AVDEVICE_DV1394_H
  28. #define AVDEVICE_DV1394_H
  29. #define DV1394_DEFAULT_CHANNEL 63
  30. #define DV1394_DEFAULT_CARD 0
  31. #define DV1394_RING_FRAMES 20
  32. #define DV1394_WIDTH 720
  33. #define DV1394_NTSC_HEIGHT 480
  34. #define DV1394_PAL_HEIGHT 576
  35. /* This is the public user-space interface. Try not to break it. */
  36. #define DV1394_API_VERSION 0x20011127
  37. /* ********************
  38. ** **
  39. ** DV1394 API **
  40. ** **
  41. ********************
  42. There are two methods of operating the DV1394 DV output device.
  43. 1)
  44. The simplest is an interface based on write(): simply write
  45. full DV frames of data to the device, and they will be transmitted
  46. as quickly as possible. The FD may be set for non-blocking I/O,
  47. in which case you can use select() or poll() to wait for output
  48. buffer space.
  49. To set the DV output parameters (e.g. whether you want NTSC or PAL
  50. video), use the DV1394_INIT ioctl, passing in the parameters you
  51. want in a struct dv1394_init.
  52. Example 1:
  53. To play a raw .DV file: cat foo.DV > /dev/dv1394
  54. (cat will use write() internally)
  55. Example 2:
  56. static struct dv1394_init init = {
  57. 0x63, (broadcast channel)
  58. 4, (four-frame ringbuffer)
  59. DV1394_NTSC, (send NTSC video)
  60. 0, 0 (default empty packet rate)
  61. }
  62. ioctl(fd, DV1394_INIT, &init);
  63. while(1) {
  64. read( <a raw DV file>, buf, DV1394_NTSC_FRAME_SIZE );
  65. write( <the dv1394 FD>, buf, DV1394_NTSC_FRAME_SIZE );
  66. }
  67. 2)
  68. For more control over buffering, and to avoid unnecessary copies
  69. of the DV data, you can use the more sophisticated the mmap() interface.
  70. First, call the DV1394_INIT ioctl to specify your parameters,
  71. including the number of frames in the ringbuffer. Then, calling mmap()
  72. on the dv1394 device will give you direct access to the ringbuffer
  73. from which the DV card reads your frame data.
  74. The ringbuffer is simply one large, contiguous region of memory
  75. containing two or more frames of packed DV data. Each frame of DV data
  76. is 120000 bytes (NTSC) or 144000 bytes (PAL).
  77. Fill one or more frames in the ringbuffer, then use the DV1394_SUBMIT_FRAMES
  78. ioctl to begin I/O. You can use either the DV1394_WAIT_FRAMES ioctl
  79. or select()/poll() to wait until the frames are transmitted. Next, you'll
  80. need to call the DV1394_GET_STATUS ioctl to determine which ringbuffer
  81. frames are clear (ready to be filled with new DV data). Finally, use
  82. DV1394_SUBMIT_FRAMES again to send the new data to the DV output.
  83. Example: here is what a four-frame ringbuffer might look like
  84. during DV transmission:
  85. frame 0 frame 1 frame 2 frame 3
  86. *--------------------------------------*
  87. | CLEAR | DV data | DV data | CLEAR |
  88. *--------------------------------------*
  89. <ACTIVE>
  90. transmission goes in this direction --->>>
  91. The DV hardware is currently transmitting the data in frame 1.
  92. Once frame 1 is finished, it will automatically transmit frame 2.
  93. (if frame 2 finishes before frame 3 is submitted, the device
  94. will continue to transmit frame 2, and will increase the dropped_frames
  95. counter each time it repeats the transmission).
  96. If you called DV1394_GET_STATUS at this instant, you would
  97. receive the following values:
  98. n_frames = 4
  99. active_frame = 1
  100. first_clear_frame = 3
  101. n_clear_frames = 2
  102. At this point, you should write new DV data into frame 3 and optionally
  103. frame 0. Then call DV1394_SUBMIT_FRAMES to inform the device that
  104. it may transmit the new frames.
  105. ERROR HANDLING
  106. An error (buffer underflow/overflow or a break in the DV stream due
  107. to a 1394 bus reset) can be detected by checking the dropped_frames
  108. field of struct dv1394_status (obtained through the
  109. DV1394_GET_STATUS ioctl).
  110. The best way to recover from such an error is to re-initialize
  111. dv1394, either by using the DV1394_INIT ioctl call, or closing the
  112. file descriptor and opening it again. (note that you must unmap all
  113. ringbuffer mappings when closing the file descriptor, or else
  114. dv1394 will still be considered 'in use').
  115. MAIN LOOP
  116. For maximum efficiency and robustness against bus errors, you are
  117. advised to model the main loop of your application after the
  118. following pseudo-code example:
  119. (checks of system call return values omitted for brevity; always
  120. check return values in your code!)
  121. while( frames left ) {
  122. struct pollfd *pfd = ...;
  123. pfd->fd = dv1394_fd;
  124. pfd->revents = 0;
  125. pfd->events = POLLOUT | POLLIN; (OUT for transmit, IN for receive)
  126. (add other sources of I/O here)
  127. poll(pfd, 1, -1); (or select(); add a timeout if you want)
  128. if(pfd->revents) {
  129. struct dv1394_status status;
  130. ioctl(dv1394_fd, DV1394_GET_STATUS, &status);
  131. if(status.dropped_frames > 0) {
  132. reset_dv1394();
  133. } else {
  134. int i;
  135. for (i = 0; i < status.n_clear_frames; i++) {
  136. copy_DV_frame();
  137. }
  138. }
  139. }
  140. }
  141. where copy_DV_frame() reads or writes on the dv1394 file descriptor
  142. (read/write mode) or copies data to/from the mmap ringbuffer and
  143. then calls ioctl(DV1394_SUBMIT_FRAMES) to notify dv1394 that new
  144. frames are available (mmap mode).
  145. reset_dv1394() is called in the event of a buffer
  146. underflow/overflow or a halt in the DV stream (e.g. due to a 1394
  147. bus reset). To guarantee recovery from the error, this function
  148. should close the dv1394 file descriptor (and munmap() all
  149. ringbuffer mappings, if you are using them), then re-open the
  150. dv1394 device (and re-map the ringbuffer).
  151. */
  152. /* maximum number of frames in the ringbuffer */
  153. #define DV1394_MAX_FRAMES 32
  154. /* number of *full* isochronous packets per DV frame */
  155. #define DV1394_NTSC_PACKETS_PER_FRAME 250
  156. #define DV1394_PAL_PACKETS_PER_FRAME 300
  157. /* size of one frame's worth of DV data, in bytes */
  158. #define DV1394_NTSC_FRAME_SIZE (480 * DV1394_NTSC_PACKETS_PER_FRAME)
  159. #define DV1394_PAL_FRAME_SIZE (480 * DV1394_PAL_PACKETS_PER_FRAME)
  160. /* ioctl() commands */
  161. enum {
  162. /* I don't like using 0 as a valid ioctl() */
  163. DV1394_INVALID = 0,
  164. /* get the driver ready to transmit video.
  165. pass a struct dv1394_init* as the parameter (see below),
  166. or NULL to get default parameters */
  167. DV1394_INIT,
  168. /* stop transmitting video and free the ringbuffer */
  169. DV1394_SHUTDOWN,
  170. /* submit N new frames to be transmitted, where
  171. the index of the first new frame is first_clear_buffer,
  172. and the index of the last new frame is
  173. (first_clear_buffer + N) % n_frames */
  174. DV1394_SUBMIT_FRAMES,
  175. /* block until N buffers are clear (pass N as the parameter)
  176. Because we re-transmit the last frame on underrun, there
  177. will at most be n_frames - 1 clear frames at any time */
  178. DV1394_WAIT_FRAMES,
  179. /* capture new frames that have been received, where
  180. the index of the first new frame is first_clear_buffer,
  181. and the index of the last new frame is
  182. (first_clear_buffer + N) % n_frames */
  183. DV1394_RECEIVE_FRAMES,
  184. DV1394_START_RECEIVE,
  185. /* pass a struct dv1394_status* as the parameter (see below) */
  186. DV1394_GET_STATUS,
  187. };
  188. enum pal_or_ntsc {
  189. DV1394_NTSC = 0,
  190. DV1394_PAL
  191. };
  192. /* this is the argument to DV1394_INIT */
  193. struct dv1394_init {
  194. /* DV1394_API_VERSION */
  195. unsigned int api_version;
  196. /* isochronous transmission channel to use */
  197. unsigned int channel;
  198. /* number of frames in the ringbuffer. Must be at least 2
  199. and at most DV1394_MAX_FRAMES. */
  200. unsigned int n_frames;
  201. /* send/receive PAL or NTSC video format */
  202. enum pal_or_ntsc format;
  203. /* the following are used only for transmission */
  204. /* set these to zero unless you want a
  205. non-default empty packet rate (see below) */
  206. unsigned long cip_n;
  207. unsigned long cip_d;
  208. /* set this to zero unless you want a
  209. non-default SYT cycle offset (default = 3 cycles) */
  210. unsigned int syt_offset;
  211. };
  212. /* NOTE: you may only allocate the DV frame ringbuffer once each time
  213. you open the dv1394 device. DV1394_INIT will fail if you call it a
  214. second time with different 'n_frames' or 'format' arguments (which
  215. would imply a different size for the ringbuffer). If you need a
  216. different buffer size, simply close and re-open the device, then
  217. initialize it with your new settings. */
  218. /* Q: What are cip_n and cip_d? */
  219. /*
  220. A: DV video streams do not utilize 100% of the potential bandwidth offered
  221. by IEEE 1394 (FireWire). To achieve the correct rate of data transmission,
  222. DV devices must periodically insert empty packets into the 1394 data stream.
  223. Typically there is one empty packet per 14-16 data-carrying packets.
  224. Some DV devices will accept a wide range of empty packet rates, while others
  225. require a precise rate. If the dv1394 driver produces empty packets at
  226. a rate that your device does not accept, you may see ugly patterns on the
  227. DV output, or even no output at all.
  228. The default empty packet insertion rate seems to work for many people; if
  229. your DV output is stable, you can simply ignore this discussion. However,
  230. we have exposed the empty packet rate as a parameter to support devices that
  231. do not work with the default rate.
  232. The decision to insert an empty packet is made with a numerator/denominator
  233. algorithm. Empty packets are produced at an average rate of CIP_N / CIP_D.
  234. You can alter the empty packet rate by passing non-zero values for cip_n
  235. and cip_d to the INIT ioctl.
  236. */
  237. struct dv1394_status {
  238. /* this embedded init struct returns the current dv1394
  239. parameters in use */
  240. struct dv1394_init init;
  241. /* the ringbuffer frame that is currently being
  242. displayed. (-1 if the device is not transmitting anything) */
  243. int active_frame;
  244. /* index of the first buffer (ahead of active_frame) that
  245. is ready to be filled with data */
  246. unsigned int first_clear_frame;
  247. /* how many buffers, including first_clear_buffer, are
  248. ready to be filled with data */
  249. unsigned int n_clear_frames;
  250. /* how many times the DV stream has underflowed, overflowed,
  251. or otherwise encountered an error, since the previous call
  252. to DV1394_GET_STATUS */
  253. unsigned int dropped_frames;
  254. /* N.B. The dropped_frames counter is only a lower bound on the actual
  255. number of dropped frames, with the special case that if dropped_frames
  256. is zero, then it is guaranteed that NO frames have been dropped
  257. since the last call to DV1394_GET_STATUS.
  258. */
  259. };
  260. #endif /* AVDEVICE_DV1394_H */