2 * Linux DV1394 interface
3 * Copyright (c) 2003 Max Krasnyansky <maxk@qualcomm.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sys/ioctl.h>
34 int dv1394_channel = DV1394_DEFAULT_CHANNEL;
43 void *ring; /* Ring buffer */
44 int index; /* Current frame index */
45 int avail; /* Number of frames available for reading */
46 int done; /* Number of completed frames */
49 static int dv1394_reset(struct dv1394_data *dv)
51 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_NTSC;
58 if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
65 static int dv1394_start(struct dv1394_data *dv)
67 /* Tell DV1394 driver to enable receiver */
68 if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) {
69 perror("Failed to start receiver");
75 static int dv1394_read_header(AVFormatContext * context, AVFormatParameters * ap)
77 struct dv1394_data *dv = context->priv_data;
79 const char *video_device;
81 st = av_new_stream(context, 0);
85 dv->width = DV1394_WIDTH;
86 dv->height = DV1394_HEIGHT;
87 dv->channel = ap->channel;
91 dv->frame_size = DV1394_NTSC_FRAME_SIZE;
93 /* Open and initialize DV1394 device */
94 video_device = ap->device;
96 video_device = "/dev/dv1394/0";
97 dv->fd = open(video_device, O_RDONLY);
99 perror("Failed to open DV interface");
103 if (dv1394_reset(dv) < 0) {
104 perror("Failed to initialize DV interface");
108 dv->ring = mmap(NULL, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES,
109 PROT_READ, MAP_PRIVATE, dv->fd, 0);
111 perror("Failed to mmap DV ring buffer");
115 st->codec.codec_type = CODEC_TYPE_VIDEO;
116 st->codec.codec_id = CODEC_ID_DVVIDEO;
117 st->codec.width = dv->width;
118 st->codec.height = dv->height;
119 st->codec.frame_rate = dv->frame_rate * FRAME_RATE_BASE;
121 st->codec.bit_rate = 25000000; /* Consumer DV is 25Mbps */
123 av_set_pts_info(context, 48, 1, 1000000);
125 if (dv1394_start(dv) < 0)
136 static inline int __copy_frame(struct dv1394_data *dv, void *buf)
138 char *ptr = dv->ring + (dv->index * dv->frame_size);
140 memcpy(buf, ptr, dv->frame_size);
142 dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
146 return dv->frame_size;
149 static int dv1394_read_packet(AVFormatContext * context, AVPacket * pkt)
151 struct dv1394_data *dv = context->priv_data;
155 struct dv1394_status s;
158 p.events = POLLIN | POLLERR | POLLHUP;
160 /* Wait until more frames are available */
161 if (poll(&p, 1, -1) < 0) {
162 perror("Poll failed");
166 if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
167 perror("Failed to get status");
171 fprintf(stderr, "DV1394: status\n"
172 "\tactive_frame\t%d\n"
173 "\tfirst_clear_frame\t%d\n"
174 "\tn_clear_frames\t%d\n"
175 "\tdropped_frames\t%d\n",
176 s.active_frame, s.first_clear_frame,
177 s.n_clear_frames, s.dropped_frames);
180 dv->avail = s.n_clear_frames;
181 dv->index = s.first_clear_frame;
184 if (s.dropped_frames) {
185 fprintf(stderr, "DV1394: Frame drop detected (%d). Reseting ..\n",
193 if (av_new_packet(pkt, dv->frame_size) < 0)
197 fprintf(stderr, "index %d, avail %d, done %d\n", dv->index, dv->avail,
201 len = __copy_frame(dv, pkt->data);
202 pkt->pts = av_gettime() & ((1LL << 48) - 1);
204 if (!dv->avail && dv->done) {
205 /* Request more frames */
206 if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
207 /* This usually means that ring buffer overflowed.
208 * We have to reset :(.
211 fprintf(stderr, "DV1394: Ring buffer overflow. Reseting ..\n");
221 static int dv1394_close(AVFormatContext * context)
223 struct dv1394_data *dv = context->priv_data;
225 /* Shutdown DV1394 receiver */
226 if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
227 perror("Failed to shutdown DV1394");
229 /* Unmap ring buffer */
230 if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
231 perror("Failed to munmap DV1394 ring buffer");
238 static AVInputFormat dv1394_format = {
240 .long_name = "dv1394 A/V grab",
241 .priv_data_size = sizeof(struct dv1394_data),
242 .read_header = dv1394_read_header,
243 .read_packet = dv1394_read_packet,
244 .read_close = dv1394_close,
245 .flags = AVFMT_NOFILE
248 int dv1394_init(void)
250 av_register_input_format(&dv1394_format);