]> git.sesse.net Git - ffmpeg/blob - libavformat/dv1394.c
merging a small amount of the changes from BroadQ, the rest is either not clean ...
[ffmpeg] / libavformat / dv1394.c
1 /*
2  * Linux DV1394 interface
3  * Copyright (c) 2003 Max Krasnyansky <maxk@qualcomm.com>
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <sys/ioctl.h>
24 #include <sys/mman.h>
25 #include <sys/poll.h>
26 #include <sys/time.h>
27 #include <time.h>
28
29 #include "avformat.h"
30
31 #undef DV1394_DEBUG
32
33 #include "dv1394.h"
34
35 struct dv1394_data {
36     int fd;
37     int channel;
38     int width, height;
39     int frame_rate;
40     int frame_size;
41     int format;
42
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 */
47
48     int stream; /* Current stream. 0 - video, 1 - audio */
49     int64_t pts;  /* Current timestamp */
50 };
51
52 static int dv1394_reset(struct dv1394_data *dv)
53 {
54     struct dv1394_init init;
55
56     init.channel     = dv->channel;
57     init.api_version = DV1394_API_VERSION;
58     init.n_frames    = DV1394_RING_FRAMES;
59     init.format      = dv->format;
60
61     if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
62         return -1;
63
64     dv->avail  = dv->done = 0;
65     dv->stream = 0;
66     return 0;
67 }
68
69 static int dv1394_start(struct dv1394_data *dv)
70 {
71     /* Tell DV1394 driver to enable receiver */
72     if (ioctl(dv->fd, DV1394_START_RECEIVE, 0) < 0) {
73         perror("Failed to start receiver");
74         return -1;
75     }
76     return 0;
77 }
78
79 static int dv1394_read_header(AVFormatContext * context, AVFormatParameters * ap)
80 {
81     struct dv1394_data *dv = context->priv_data;
82     AVStream *vst, *ast;
83     const char *video_device;
84
85     vst = av_new_stream(context, 0);
86     if (!vst)
87         return -ENOMEM;
88     ast = av_new_stream(context, 1);
89     if (!ast) {
90         av_free(vst);
91         return -ENOMEM;
92     }
93
94     dv->width   = DV1394_WIDTH;
95     dv->height  = DV1394_HEIGHT;
96
97     if (ap->channel)
98         dv->channel = ap->channel;
99     else
100         dv->channel = DV1394_DEFAULT_CHANNEL;
101
102     /* FIXME: Need a format change parameter */
103     dv->format = DV1394_NTSC;
104
105     if (dv->format == DV1394_NTSC) {
106         dv->frame_size = DV1394_NTSC_FRAME_SIZE;
107         dv->frame_rate = 30;
108     } else {
109         dv->frame_size = DV1394_PAL_FRAME_SIZE;
110         dv->frame_rate = 25;
111     }
112
113     /* Open and initialize DV1394 device */
114     video_device = ap->device;
115     if (!video_device)
116         video_device = "/dev/dv1394/0";
117     dv->fd = open(video_device, O_RDONLY);
118     if (dv->fd < 0) {
119         perror("Failed to open DV interface");
120         goto failed;
121     }
122
123     if (dv1394_reset(dv) < 0) {
124         perror("Failed to initialize DV interface");
125         goto failed;
126     }
127
128     dv->ring = mmap(NULL, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES,
129                     PROT_READ, MAP_PRIVATE, dv->fd, 0);
130     if (!dv->ring) {
131         perror("Failed to mmap DV ring buffer");
132         goto failed;
133     }
134
135     dv->stream = 0;
136
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 */
144
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;
149
150     av_set_pts_info(context, 48, 1, 1000000);
151
152     if (dv1394_start(dv) < 0)
153         goto failed;
154
155     return 0;
156
157 failed:
158     close(dv->fd);
159     av_free(vst);
160     av_free(ast);
161     return -EIO;
162 }
163
164 static void __destruct_pkt(struct AVPacket *pkt)
165 {
166     pkt->data = NULL; pkt->size = 0;
167     return;
168 }
169
170 static inline int __get_frame(struct dv1394_data *dv, AVPacket *pkt)
171 {
172     char *ptr = dv->ring + (dv->index * dv->frame_size);
173
174     if (dv->stream) {
175         dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
176         dv->done++; dv->avail--;
177     } else {
178         dv->pts = av_gettime() & ((1LL << 48) - 1);
179     }
180
181     av_init_packet(pkt);
182     pkt->destruct = __destruct_pkt;
183     pkt->data     = ptr;
184     pkt->size     = dv->frame_size;
185     pkt->pts      = dv->pts;
186     pkt->stream_index = dv->stream;
187
188     dv->stream ^= 1;
189
190     return dv->frame_size;
191 }
192
193 static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt)
194 {
195     struct dv1394_data *dv = context->priv_data;
196
197     if (!dv->avail) {
198         struct dv1394_status s;
199         struct pollfd p;
200
201         if (dv->done) {
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 :(.
206                  */
207   
208                 fprintf(stderr, "DV1394: Ring buffer overflow. Reseting ..\n");
209  
210                 dv1394_reset(dv);
211                 dv1394_start(dv);
212             }
213             dv->done = 0;
214         }
215
216         /* Wait until more frames are available */
217 restart_poll:
218         p.fd = dv->fd;
219         p.events = POLLIN | POLLERR | POLLHUP;
220         if (poll(&p, 1, -1) < 0) {
221             if (errno == EAGAIN || errno == EINTR)
222                 goto restart_poll;
223             perror("Poll failed");
224             return -EIO;
225         }
226
227         if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
228             perror("Failed to get status");
229             return -EIO;
230         }
231 #ifdef DV1394_DEBUG
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);
239 #endif
240
241         dv->avail = s.n_clear_frames;
242         dv->index = s.first_clear_frame;
243         dv->done  = 0;
244
245         if (s.dropped_frames) {
246             fprintf(stderr, "DV1394: Frame drop detected (%d). Reseting ..\n",
247                     s.dropped_frames);
248
249             dv1394_reset(dv);
250             dv1394_start(dv);
251         }
252     }
253
254 #ifdef DV1394_DEBUG
255     fprintf(stderr, "index %d, avail %d, done %d\n", dv->index, dv->avail,
256             dv->done);
257 #endif
258
259     return __get_frame(dv, pkt);
260 }
261
262 static int dv1394_close(AVFormatContext * context)
263 {
264     struct dv1394_data *dv = context->priv_data;
265
266     /* Shutdown DV1394 receiver */
267     if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
268         perror("Failed to shutdown DV1394");
269
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");
273
274     close(dv->fd);
275
276     return 0;
277 }
278
279 static AVInputFormat dv1394_format = {
280     .name           = "dv1394",
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
287 };
288
289 int dv1394_init(void)
290 {
291     av_register_input_format(&dv1394_format);
292     return 0;
293 }