dv patch by Max Krasnyansky (maxk at qualcomm dot com)
[libav.git] / 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
80 st = av_new_stream(context, 0);
81 if (!st)
82 return -ENOMEM;
83
84 dv->width = DV1394_WIDTH;
85 dv->height = DV1394_HEIGHT;
86 dv->channel = dv1394_channel;
87
88 dv->frame_rate = 30;
89
90 dv->frame_size = DV1394_NTSC_FRAME_SIZE;
91
92 /* Open and initialize DV1394 device */
93
94 dv->fd = open(video_device, O_RDONLY);
95 if (dv->fd < 0) {
96 perror("Failed to open DV interface");
97 goto failed;
98 }
99
100 if (dv1394_reset(dv) < 0) {
101 perror("Failed to initialize DV interface");
102 goto failed;
103 }
104
105 dv->ring = mmap(NULL, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES,
106 PROT_READ, MAP_PRIVATE, dv->fd, 0);
107 if (!dv->ring) {
108 perror("Failed to mmap DV ring buffer");
109 goto failed;
110 }
111
112 st->codec.codec_type = CODEC_TYPE_VIDEO;
113 st->codec.codec_id = CODEC_ID_DVVIDEO;
114 st->codec.width = dv->width;
115 st->codec.height = dv->height;
116 st->codec.frame_rate = dv->frame_rate * FRAME_RATE_BASE;
117
118 st->codec.bit_rate = 25000000; /* Consumer DV is 25Mbps */
119
120 av_set_pts_info(context, 48, 1, 1000000);
121
122 if (dv1394_start(dv) < 0)
123 goto failed;
124
125 return 0;
126
127 failed:
128 close(dv->fd);
129 av_free(st);
130 return -EIO;
131 }
132
133 static inline int __copy_frame(struct dv1394_data *dv, void *buf)
134 {
135 char *ptr = dv->ring + (dv->index * dv->frame_size);
136
137 memcpy(buf, ptr, dv->frame_size);
138
139 dv->index = (dv->index + 1) % DV1394_RING_FRAMES;
140 dv->avail--;
141 dv->done++;
142
143 return dv->frame_size;
144 }
145
146 static int dv1394_read_packet(AVFormatContext * context, AVPacket * pkt)
147 {
148 struct dv1394_data *dv = context->priv_data;
149 int len;
150
151 if (!dv->avail) {
152 struct dv1394_status s;
153 struct pollfd p;
154 p.fd = dv->fd;
155 p.events = POLLIN | POLLERR | POLLHUP;
156
157 /* Wait until more frames are available */
158 if (poll(&p, 1, -1) < 0) {
159 perror("Poll failed");
160 return -EIO;
161 }
162
163 if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) {
164 perror("Failed to get status");
165 return -EIO;
166 }
167 #ifdef DV1394_DEBUG
168 fprintf(stderr, "DV1394: status\n"
169 "\tactive_frame\t%d\n"
170 "\tfirst_clear_frame\t%d\n"
171 "\tn_clear_frames\t%d\n"
172 "\tdropped_frames\t%d\n",
173 s.active_frame, s.first_clear_frame,
174 s.n_clear_frames, s.dropped_frames);
175 #endif
176
177 dv->avail = s.n_clear_frames;
178 dv->index = s.first_clear_frame;
179 dv->done = 0;
180
181 if (s.dropped_frames) {
182 fprintf(stderr, "DV1394: Frame drop detected (%d). Reseting ..\n",
183 s.dropped_frames);
184
185 dv1394_reset(dv);
186 dv1394_start(dv);
187 }
188 }
189
190 if (av_new_packet(pkt, dv->frame_size) < 0)
191 return -EIO;
192
193 #ifdef DV1394_DEBUG
194 fprintf(stderr, "index %d, avail %d, done %d\n", dv->index, dv->avail,
195 dv->done);
196 #endif
197
198 len = __copy_frame(dv, pkt->data);
199 pkt->pts = av_gettime() & ((1LL << 48) - 1);
200
201 if (!dv->avail && 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 }
214
215 return len;
216 }
217
218 static int dv1394_close(AVFormatContext * context)
219 {
220 struct dv1394_data *dv = context->priv_data;
221
222 /* Shutdown DV1394 receiver */
223 if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0)
224 perror("Failed to shutdown DV1394");
225
226 /* Unmap ring buffer */
227 if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0)
228 perror("Failed to munmap DV1394 ring buffer");
229
230 close(dv->fd);
231
232 return 0;
233 }
234
235 static AVInputFormat dv1394_format = {
236 .name = "dv1394",
237 .long_name = "dv1394 A/V grab",
238 .priv_data_size = sizeof(struct dv1394_data),
239 .read_header = dv1394_read_header,
240 .read_packet = dv1394_read_packet,
241 .read_close = dv1394_close,
242 .flags = AVFMT_NOFILE
243 };
244
245 int dv1394_init(void)
246 {
247 av_register_input_format(&dv1394_format);
248 return 0;
249 }