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
23 #include <sys/ioctl.h>
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 */
48 int stream; /* Current stream. 0 - video, 1 - audio */
49 int64_t pts; /* Current timestamp */
52 static int dv1394_reset(struct dv1394_data *dv)
54 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 = dv->format;
61 if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
64 dv->avail = dv->done = 0;
69 static int dv1394_start(struct dv1394_data *dv)
71 /* Tell DV1394 driver to enable receiver */
72 if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) {
73 perror("Failed to start receiver");
79 static int dv1394_read_header(AVFormatContext * context, AVFormatParameters * ap)
81 struct dv1394_data *dv = context->priv_data;
83 const char *video_device;
85 vst = av_new_stream(context, 0);
88 ast = av_new_stream(context, 1);
94 dv->width = DV1394_WIDTH;
95 dv->height = DV1394_HEIGHT;
98 dv->channel = ap->channel;
100 dv->channel = DV1394_DEFAULT_CHANNEL;
102 /* FIXME: Need a format change parameter */
103 dv->format = DV1394_NTSC;
105 if (dv->format == DV1394_NTSC) {
106 dv->frame_size = DV1394_NTSC_FRAME_SIZE;
109 dv->frame_size = DV1394_PAL_FRAME_SIZE;
113 /* Open and initialize DV1394 device */
114 video_device = ap->device;
116 video_device = "/dev/dv1394/0";
117 dv->fd = open(video_device, O_RDONLY);
119 perror("Failed to open DV interface");
123 if (dv1394_reset(dv) < 0) {
124 perror("Failed to initialize DV interface");
128 dv->ring = mmap(NULL, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES,
129 PROT_READ, MAP_PRIVATE, dv->fd, 0);
131 perror("Failed to mmap DV ring buffer");
137 vst->codec.codec_type = CODEC_TYPE_VIDEO;
138 vst->codec.codec_id = CODEC_ID_DVVIDEO;
139 vst->codec.width = dv->width;
140 vst->codec.height = dv->height;
141 vst->codec.frame_rate = dv->frame_rate;
142 vst->codec.frame_rate_base = 1;
143 vst->codec.bit_rate = 25000000; /* Consumer DV is 25Mbps */
145 ast->codec.codec_type = CODEC_TYPE_AUDIO;
146 ast->codec.codec_id = CODEC_ID_DVAUDIO;
147 ast->codec.channels = 2;
148 ast->codec.sample_rate= 48000;
150 av_set_pts_info(context, 48, 1, 1000000);
152 if (dv1394_start(dv) < 0)
164 static void __destruct_pkt(struct AVPacket *pkt)
166 pkt->data = NULL; pkt->size = 0;
170 static inline int __get_frame(struct dv1394_data *dv, AVPacket *pkt)
172 char *ptr = dv->ring + (dv->index * dv->frame_size);
175 dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
176 dv->done++; dv->avail--;
178 dv->pts = av_gettime() & ((1LL << 48) - 1);
182 pkt->destruct = __destruct_pkt;
184 pkt->size = dv->frame_size;
186 pkt->stream_index = dv->stream;
190 return dv->frame_size;
193 static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
195 struct dv1394_data *dv = context->priv_data;
198 struct dv1394_status s;
202 /* Request more frames */
203 if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) {
204 /* This usually means that ring buffer overflowed.
205 * We have to reset :(.
208 fprintf(stderr, "DV1394: Ring buffer overflow. Reseting ..\n");
216 /* Wait until more frames are available */
219 p.events = POLLIN | POLLERR | POLLHUP;
220 if (poll(&p, 1, -1) < 0) {
221 if (errno == EAGAIN || errno == EINTR)
223 perror("Poll failed");
227 if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
228 perror("Failed to get status");
232 fprintf(stderr, "DV1394: status\n"
233 "\tactive_frame\t%d\n"
234 "\tfirst_clear_frame\t%d\n"
235 "\tn_clear_frames\t%d\n"
236 "\tdropped_frames\t%d\n",
237 s.active_frame, s.first_clear_frame,
238 s.n_clear_frames, s.dropped_frames);
241 dv->avail = s.n_clear_frames;
242 dv->index = s.first_clear_frame;
245 if (s.dropped_frames) {
246 fprintf(stderr, "DV1394: Frame drop detected (%d). Reseting ..\n",
255 fprintf(stderr, "index %d, avail %d, done %d\n", dv->index, dv->avail,
259 return __get_frame(dv, pkt);
262 static int dv1394_close(AVFormatContext * context)
264 struct dv1394_data *dv = context->priv_data;
266 /* Shutdown DV1394 receiver */
267 if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
268 perror("Failed to shutdown DV1394");
270 /* Unmap ring buffer */
271 if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
272 perror("Failed to munmap DV1394 ring buffer");
279 static AVInputFormat dv1394_format = {
281 .long_name = "dv1394 A/V grab",
282 .priv_data_size = sizeof(struct dv1394_data),
283 .read_header = dv1394_read_header,
284 .read_packet = dv1394_read_packet,
285 .read_close = dv1394_close,
286 .flags = AVFMT_NOFILE
289 int dv1394_init(void)
291 av_register_input_format(&dv1394_format);