Mercurial > libavformat.hg
annotate dv1394.c @ 55:bde449e6b743 libavformat
yes, PNG can be simpler :-)
author | bellard |
---|---|
date | Tue, 04 Feb 2003 23:44:26 +0000 |
parents | fb671d87824e |
children | a58a8a53eb46 |
rev | line source |
---|---|
27 | 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> | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
22 #include <errno.h> |
27 | 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; | |
38 | 41 int format; |
27 | 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 */ | |
38 | 47 |
48 int stream; /* Current stream. 0 - video, 1 - audio */ | |
49 INT64 pts; /* Current timestamp */ | |
27 | 50 }; |
51 | |
52 static int dv1394_reset(struct dv1394_data *dv) | |
53 { | |
54 struct dv1394_init init; | |
55 | |
38 | 56 init.channel = dv->channel; |
27 | 57 init.api_version = DV1394_API_VERSION; |
38 | 58 init.n_frames = DV1394_RING_FRAMES; |
59 init.format = dv->format; | |
27 | 60 |
61 if (ioctl(dv->fd, DV1394_INIT, &init) < 0) | |
62 return -1; | |
63 | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
64 dv->avail = dv->done = 0; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
65 dv->stream = 0; |
27 | 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; | |
38 | 82 AVStream *vst, *ast; |
30
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
27
diff
changeset
|
83 const char *video_device; |
27 | 84 |
38 | 85 vst = av_new_stream(context, 0); |
86 if (!vst) | |
27 | 87 return -ENOMEM; |
38 | 88 ast = av_new_stream(context, 1); |
89 if (!ast) { | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
90 av_free(vst); |
38 | 91 return -ENOMEM; |
92 } | |
27 | 93 |
94 dv->width = DV1394_WIDTH; | |
95 dv->height = DV1394_HEIGHT; | |
31
36dd902f98d2
dv1394 channel selection by Max Krasnyansky - modified channel number to be in base 10 by default
bellard
parents:
30
diff
changeset
|
96 |
36dd902f98d2
dv1394 channel selection by Max Krasnyansky - modified channel number to be in base 10 by default
bellard
parents:
30
diff
changeset
|
97 if (ap->channel) |
36dd902f98d2
dv1394 channel selection by Max Krasnyansky - modified channel number to be in base 10 by default
bellard
parents:
30
diff
changeset
|
98 dv->channel = ap->channel; |
36dd902f98d2
dv1394 channel selection by Max Krasnyansky - modified channel number to be in base 10 by default
bellard
parents:
30
diff
changeset
|
99 else |
36dd902f98d2
dv1394 channel selection by Max Krasnyansky - modified channel number to be in base 10 by default
bellard
parents:
30
diff
changeset
|
100 dv->channel = DV1394_DEFAULT_CHANNEL; |
27 | 101 |
38 | 102 /* FIXME: Need a format change parameter */ |
103 dv->format = DV1394_NTSC; | |
27 | 104 |
38 | 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 } | |
27 | 112 |
113 /* Open and initialize DV1394 device */ | |
30
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
27
diff
changeset
|
114 video_device = ap->device; |
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
27
diff
changeset
|
115 if (!video_device) |
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
27
diff
changeset
|
116 video_device = "/dev/dv1394/0"; |
27 | 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 | |
38 | 135 dv->stream = 0; |
27 | 136 |
38 | 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 * FRAME_RATE_BASE; | |
142 vst->codec.bit_rate = 25000000; /* Consumer DV is 25Mbps */ | |
143 | |
144 ast->codec.codec_type = CODEC_TYPE_AUDIO; | |
145 ast->codec.codec_id = CODEC_ID_DVAUDIO; | |
146 ast->codec.channels = 2; | |
147 ast->codec.sample_rate= 48000; | |
27 | 148 |
149 av_set_pts_info(context, 48, 1, 1000000); | |
150 | |
151 if (dv1394_start(dv) < 0) | |
152 goto failed; | |
153 | |
154 return 0; | |
155 | |
156 failed: | |
157 close(dv->fd); | |
38 | 158 av_free(vst); |
159 av_free(ast); | |
27 | 160 return -EIO; |
161 } | |
162 | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
163 static void __destruct_pkt(struct AVPacket *pkt) |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
164 { |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
165 pkt->data = NULL; pkt->size = 0; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
166 return; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
167 } |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
168 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
169 static inline int __get_frame(struct dv1394_data *dv, AVPacket *pkt) |
27 | 170 { |
171 char *ptr = dv->ring + (dv->index * dv->frame_size); | |
172 | |
38 | 173 if (dv->stream) { |
174 dv->index = (dv->index + 1) % DV1394_RING_FRAMES; | |
175 dv->done++; dv->avail--; | |
176 } else { | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
177 dv->pts = av_gettime() & ((1LL << 48) - 1); |
38 | 178 } |
27 | 179 |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
180 av_init_packet(pkt); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
181 pkt->destruct = __destruct_pkt; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
182 pkt->data = ptr; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
183 pkt->size = dv->frame_size; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
184 pkt->pts = dv->pts; |
38 | 185 pkt->stream_index = dv->stream; |
186 | |
187 dv->stream ^= 1; | |
27 | 188 |
189 return dv->frame_size; | |
190 } | |
191 | |
38 | 192 static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt) |
27 | 193 { |
194 struct dv1394_data *dv = context->priv_data; | |
195 | |
196 if (!dv->avail) { | |
197 struct dv1394_status s; | |
198 struct pollfd p; | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
199 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
200 if (dv->done) { |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
201 /* Request more frames */ |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
202 if (ioctl(dv->fd, DV1394_RECEIVE_FRAMES, dv->done) < 0) { |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
203 /* This usually means that ring buffer overflowed. |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
204 * We have to reset :(. |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
205 */ |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
206 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
207 fprintf(stderr, "DV1394: Ring buffer overflow. Reseting ..\n"); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
208 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
209 dv1394_reset(dv); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
210 dv1394_start(dv); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
211 } |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
212 dv->done = 0; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
213 } |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
214 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
215 /* Wait until more frames are available */ |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
216 restart_poll: |
27 | 217 p.fd = dv->fd; |
218 p.events = POLLIN | POLLERR | POLLHUP; | |
219 if (poll(&p, 1, -1) < 0) { | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
220 if (errno == EAGAIN || errno == EINTR) |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
221 goto restart_poll; |
27 | 222 perror("Poll failed"); |
223 return -EIO; | |
224 } | |
225 | |
226 if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) { | |
227 perror("Failed to get status"); | |
228 return -EIO; | |
229 } | |
230 #ifdef DV1394_DEBUG | |
231 fprintf(stderr, "DV1394: status\n" | |
232 "\tactive_frame\t%d\n" | |
233 "\tfirst_clear_frame\t%d\n" | |
234 "\tn_clear_frames\t%d\n" | |
235 "\tdropped_frames\t%d\n", | |
236 s.active_frame, s.first_clear_frame, | |
237 s.n_clear_frames, s.dropped_frames); | |
238 #endif | |
239 | |
240 dv->avail = s.n_clear_frames; | |
241 dv->index = s.first_clear_frame; | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
242 dv->done = 0; |
27 | 243 |
244 if (s.dropped_frames) { | |
245 fprintf(stderr, "DV1394: Frame drop detected (%d). Reseting ..\n", | |
246 s.dropped_frames); | |
247 | |
248 dv1394_reset(dv); | |
249 dv1394_start(dv); | |
250 } | |
251 } | |
252 | |
253 #ifdef DV1394_DEBUG | |
254 fprintf(stderr, "index %d, avail %d, done %d\n", dv->index, dv->avail, | |
255 dv->done); | |
256 #endif | |
257 | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
258 return __get_frame(dv, pkt); |
27 | 259 } |
260 | |
261 static int dv1394_close(AVFormatContext * context) | |
262 { | |
263 struct dv1394_data *dv = context->priv_data; | |
264 | |
265 /* Shutdown DV1394 receiver */ | |
266 if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0) | |
267 perror("Failed to shutdown DV1394"); | |
268 | |
269 /* Unmap ring buffer */ | |
270 if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0) | |
271 perror("Failed to munmap DV1394 ring buffer"); | |
272 | |
273 close(dv->fd); | |
274 | |
275 return 0; | |
276 } | |
277 | |
278 static AVInputFormat dv1394_format = { | |
279 .name = "dv1394", | |
280 .long_name = "dv1394 A/V grab", | |
281 .priv_data_size = sizeof(struct dv1394_data), | |
282 .read_header = dv1394_read_header, | |
283 .read_packet = dv1394_read_packet, | |
284 .read_close = dv1394_close, | |
285 .flags = AVFMT_NOFILE | |
286 }; | |
287 | |
288 int dv1394_init(void) | |
289 { | |
290 av_register_input_format(&dv1394_format); | |
291 return 0; | |
292 } |