]> git.sesse.net Git - ffmpeg/blob - libavformat/dv1394.c
grab device is in AVFormatParameter (at least better than global variable)
[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 <sys/ioctl.h>
23 #include <sys/mman.h>
24 #include <sys/poll.h>
25 #include <sys/time.h>
26 #include <time.h>
27
28 #include "avformat.h"
29
30 #undef DV1394_DEBUG
31
32 #include "dv1394.h"
33
34 int dv1394_channel = DV1394_DEFAULT_CHANNEL;
35
36 struct dv1394_data {
37     int fd;
38     int channel;
39     int width, height;
40     int frame_rate;
41     int frame_size;
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
49 static int dv1394_reset(struct dv1394_data *dv)
50 {
51     struct dv1394_init init;
52
53     init.channel = dv->channel;
54     init.api_version = DV1394_API_VERSION;
55     init.n_frames = DV1394_RING_FRAMES;
56     init.format = DV1394_NTSC;
57
58     if (ioctl(dv->fd, DV1394_INIT, &init) < 0)
59         return -1;
60
61     dv->avail = 0;
62     return 0;
63 }
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         perror("Failed to start receiver");
70         return -1;
71     }
72     return 0;
73 }
74
75 static int dv1394_read_header(AVFormatContext * context, AVFormatParameters * ap)
76 {
77     struct dv1394_data *dv = context->priv_data;
78     AVStream *st;
79     const char *video_device;
80
81     st = av_new_stream(context, 0);
82     if (!st)
83         return -ENOMEM;
84
85     dv->width   = DV1394_WIDTH;
86     dv->height  = DV1394_HEIGHT;
87     dv->channel = ap->channel;
88
89     dv->frame_rate = 30;
90
91     dv->frame_size = DV1394_NTSC_FRAME_SIZE;
92
93     /* Open and initialize DV1394 device */
94     video_device = ap->device;
95     if (!video_device)
96         video_device = "/dev/dv1394/0";
97     dv->fd = open(video_device, O_RDONLY);
98     if (dv->fd < 0) {
99         perror("Failed to open DV interface");
100         goto failed;
101     }
102
103     if (dv1394_reset(dv) < 0) {
104         perror("Failed to initialize DV interface");
105         goto failed;
106     }
107
108     dv->ring = mmap(NULL, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES,
109                     PROT_READ, MAP_PRIVATE, dv->fd, 0);
110     if (!dv->ring) {
111         perror("Failed to mmap DV ring buffer");
112         goto failed;
113     }
114
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;
120
121     st->codec.bit_rate   = 25000000;  /* Consumer DV is 25Mbps */
122
123     av_set_pts_info(context, 48, 1, 1000000);
124
125     if (dv1394_start(dv) < 0)
126         goto failed;
127
128     return 0;
129
130 failed:
131     close(dv->fd);
132     av_free(st);
133     return -EIO;
134 }
135
136 static inline int __copy_frame(struct dv1394_data *dv, void *buf)
137 {
138     char *ptr = dv->ring + (dv->index * dv->frame_size);
139
140     memcpy(buf, ptr, dv->frame_size);
141
142     dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
143     dv->avail--;
144     dv->done++;
145
146     return dv->frame_size;
147 }
148
149 static int dv1394_read_packet(AVFormatContext * context, AVPacket * pkt)
150 {
151     struct dv1394_data *dv = context->priv_data;
152     int len;
153
154     if (!dv->avail) {
155         struct dv1394_status s;
156         struct pollfd p;
157         p.fd = dv->fd;
158         p.events = POLLIN | POLLERR | POLLHUP;
159
160         /* Wait until more frames are available */
161         if (poll(&p, 1, -1) < 0) {
162             perror("Poll failed");
163             return -EIO;
164         }
165
166         if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
167             perror("Failed to get status");
168             return -EIO;
169         }
170 #ifdef DV1394_DEBUG
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);
178 #endif
179
180         dv->avail = s.n_clear_frames;
181         dv->index = s.first_clear_frame;
182         dv->done = 0;
183
184         if (s.dropped_frames) {
185             fprintf(stderr, "DV1394: Frame drop detected (%d). Reseting ..\n",
186                     s.dropped_frames);
187
188             dv1394_reset(dv);
189             dv1394_start(dv);
190         }
191     }
192
193     if (av_new_packet(pkt, dv->frame_size) < 0)
194         return -EIO;
195
196 #ifdef DV1394_DEBUG
197     fprintf(stderr, "index %d, avail %d, done %d\n", dv->index, dv->avail,
198             dv->done);
199 #endif
200
201     len = __copy_frame(dv, pkt->data);
202     pkt->pts = av_gettime() & ((1LL << 48) - 1);
203
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 :(.
209              */
210
211             fprintf(stderr, "DV1394: Ring buffer overflow. Reseting ..\n");
212
213             dv1394_reset(dv);
214             dv1394_start(dv);
215         }
216     }
217
218     return len;
219 }
220
221 static int dv1394_close(AVFormatContext * context)
222 {
223     struct dv1394_data *dv = context->priv_data;
224
225     /* Shutdown DV1394 receiver */
226     if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
227         perror("Failed to shutdown DV1394");
228
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");
232
233     close(dv->fd);
234
235     return 0;
236 }
237
238 static AVInputFormat dv1394_format = {
239     .name           = "dv1394",
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
246 };
247
248 int dv1394_init(void)
249 {
250     av_register_input_format(&dv1394_format);
251     return 0;
252 }