Mercurial > libavformat.hg
annotate dv1394.c @ 171:fe5fc579b4de libavformat
BeOS fix: NOT every ld likes undefined syms, include C++ objs, and link to libs needed for audio in.
author | mmu_man |
---|---|
date | Tue, 15 Jul 2003 13:35:20 +0000 |
parents | 7d698c3213a0 |
children | d98ed04d62a6 |
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 */ | |
65 | 49 int64_t 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 |
159
7d698c3213a0
tv standard selection support for dv1394 and grab (v4l)
al3x
parents:
156
diff
changeset
|
94 if (ap->standard && !strcasecmp(ap->standard, "pal")) |
7d698c3213a0
tv standard selection support for dv1394 and grab (v4l)
al3x
parents:
156
diff
changeset
|
95 dv->format = DV1394_PAL; |
7d698c3213a0
tv standard selection support for dv1394 and grab (v4l)
al3x
parents:
156
diff
changeset
|
96 else |
7d698c3213a0
tv standard selection support for dv1394 and grab (v4l)
al3x
parents:
156
diff
changeset
|
97 dv->format = DV1394_NTSC; |
31
36dd902f98d2
dv1394 channel selection by Max Krasnyansky - modified channel number to be in base 10 by default
bellard
parents:
30
diff
changeset
|
98 |
36dd902f98d2
dv1394 channel selection by Max Krasnyansky - modified channel number to be in base 10 by default
bellard
parents:
30
diff
changeset
|
99 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
|
100 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
|
101 else |
36dd902f98d2
dv1394 channel selection by Max Krasnyansky - modified channel number to be in base 10 by default
bellard
parents:
30
diff
changeset
|
102 dv->channel = DV1394_DEFAULT_CHANNEL; |
27 | 103 |
156 | 104 dv->width = DV1394_WIDTH; |
38 | 105 if (dv->format == DV1394_NTSC) { |
156 | 106 dv->height = DV1394_NTSC_HEIGHT; |
38 | 107 dv->frame_size = DV1394_NTSC_FRAME_SIZE; |
108 dv->frame_rate = 30; | |
109 } else { | |
156 | 110 dv->height = DV1394_PAL_HEIGHT; |
38 | 111 dv->frame_size = DV1394_PAL_FRAME_SIZE; |
112 dv->frame_rate = 25; | |
113 } | |
27 | 114 |
115 /* Open and initialize DV1394 device */ | |
30
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
27
diff
changeset
|
116 video_device = ap->device; |
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
27
diff
changeset
|
117 if (!video_device) |
90fd30dd68b3
grab device is in AVFormatParameter (at least better than global variable)
bellard
parents:
27
diff
changeset
|
118 video_device = "/dev/dv1394/0"; |
27 | 119 dv->fd = open(video_device, O_RDONLY); |
120 if (dv->fd < 0) { | |
121 perror("Failed to open DV interface"); | |
122 goto failed; | |
123 } | |
124 | |
125 if (dv1394_reset(dv) < 0) { | |
126 perror("Failed to initialize DV interface"); | |
127 goto failed; | |
128 } | |
129 | |
130 dv->ring = mmap(NULL, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES, | |
131 PROT_READ, MAP_PRIVATE, dv->fd, 0); | |
132 if (!dv->ring) { | |
133 perror("Failed to mmap DV ring buffer"); | |
134 goto failed; | |
135 } | |
136 | |
38 | 137 dv->stream = 0; |
27 | 138 |
38 | 139 vst->codec.codec_type = CODEC_TYPE_VIDEO; |
140 vst->codec.codec_id = CODEC_ID_DVVIDEO; | |
141 vst->codec.width = dv->width; | |
142 vst->codec.height = dv->height; | |
85
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
143 vst->codec.frame_rate = dv->frame_rate; |
25062c9b1f86
per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents:
65
diff
changeset
|
144 vst->codec.frame_rate_base = 1; |
38 | 145 vst->codec.bit_rate = 25000000; /* Consumer DV is 25Mbps */ |
146 | |
147 ast->codec.codec_type = CODEC_TYPE_AUDIO; | |
148 ast->codec.codec_id = CODEC_ID_DVAUDIO; | |
149 ast->codec.channels = 2; | |
150 ast->codec.sample_rate= 48000; | |
27 | 151 |
152 av_set_pts_info(context, 48, 1, 1000000); | |
153 | |
154 if (dv1394_start(dv) < 0) | |
155 goto failed; | |
156 | |
157 return 0; | |
158 | |
159 failed: | |
160 close(dv->fd); | |
38 | 161 av_free(vst); |
162 av_free(ast); | |
27 | 163 return -EIO; |
164 } | |
165 | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
166 static void __destruct_pkt(struct AVPacket *pkt) |
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 pkt->data = NULL; pkt->size = 0; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
169 return; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
170 } |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
171 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
172 static inline int __get_frame(struct dv1394_data *dv, AVPacket *pkt) |
27 | 173 { |
174 char *ptr = dv->ring + (dv->index * dv->frame_size); | |
175 | |
38 | 176 if (dv->stream) { |
177 dv->index = (dv->index + 1) % DV1394_RING_FRAMES; | |
178 dv->done++; dv->avail--; | |
179 } else { | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
180 dv->pts = av_gettime() & ((1LL << 48) - 1); |
38 | 181 } |
27 | 182 |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
183 av_init_packet(pkt); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
184 pkt->destruct = __destruct_pkt; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
185 pkt->data = ptr; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
186 pkt->size = dv->frame_size; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
187 pkt->pts = dv->pts; |
38 | 188 pkt->stream_index = dv->stream; |
119 | 189 pkt->flags |= PKT_FLAG_KEY; |
38 | 190 |
191 dv->stream ^= 1; | |
27 | 192 |
193 return dv->frame_size; | |
194 } | |
195 | |
38 | 196 static int dv1394_read_packet(AVFormatContext *context, AVPacket *pkt) |
27 | 197 { |
198 struct dv1394_data *dv = context->priv_data; | |
199 | |
200 if (!dv->avail) { | |
201 struct dv1394_status s; | |
202 struct pollfd p; | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
203 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
204 if (dv->done) { |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
205 /* Request more frames */ |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
206 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
|
207 /* This usually means that ring buffer overflowed. |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
208 * We have to reset :(. |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
209 */ |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
210 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
211 fprintf(stderr, "DV1394: Ring buffer overflow. Reseting ..\n"); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
212 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
213 dv1394_reset(dv); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
214 dv1394_start(dv); |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
215 } |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
216 dv->done = 0; |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
217 } |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
218 |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
219 /* Wait until more frames are available */ |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
220 restart_poll: |
27 | 221 p.fd = dv->fd; |
222 p.events = POLLIN | POLLERR | POLLHUP; | |
223 if (poll(&p, 1, -1) < 0) { | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
224 if (errno == EAGAIN || errno == EINTR) |
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
225 goto restart_poll; |
27 | 226 perror("Poll failed"); |
227 return -EIO; | |
228 } | |
229 | |
230 if (ioctl(dv->fd, DV1394_GET_STATUS, &s) < 0) { | |
231 perror("Failed to get status"); | |
232 return -EIO; | |
233 } | |
234 #ifdef DV1394_DEBUG | |
235 fprintf(stderr, "DV1394: status\n" | |
236 "\tactive_frame\t%d\n" | |
237 "\tfirst_clear_frame\t%d\n" | |
238 "\tn_clear_frames\t%d\n" | |
239 "\tdropped_frames\t%d\n", | |
240 s.active_frame, s.first_clear_frame, | |
241 s.n_clear_frames, s.dropped_frames); | |
242 #endif | |
243 | |
244 dv->avail = s.n_clear_frames; | |
245 dv->index = s.first_clear_frame; | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
246 dv->done = 0; |
27 | 247 |
248 if (s.dropped_frames) { | |
249 fprintf(stderr, "DV1394: Frame drop detected (%d). Reseting ..\n", | |
250 s.dropped_frames); | |
251 | |
252 dv1394_reset(dv); | |
253 dv1394_start(dv); | |
254 } | |
255 } | |
256 | |
257 #ifdef DV1394_DEBUG | |
258 fprintf(stderr, "index %d, avail %d, done %d\n", dv->index, dv->avail, | |
259 dv->done); | |
260 #endif | |
261 | |
53
fb671d87824e
zero copy packet handling for DV1394 by Max Krasnyansky
bellard
parents:
38
diff
changeset
|
262 return __get_frame(dv, pkt); |
27 | 263 } |
264 | |
265 static int dv1394_close(AVFormatContext * context) | |
266 { | |
267 struct dv1394_data *dv = context->priv_data; | |
268 | |
269 /* Shutdown DV1394 receiver */ | |
270 if (ioctl(dv->fd, DV1394_SHUTDOWN, 0) < 0) | |
271 perror("Failed to shutdown DV1394"); | |
272 | |
273 /* Unmap ring buffer */ | |
274 if (munmap(dv->ring, DV1394_NTSC_FRAME_SIZE * DV1394_RING_FRAMES) < 0) | |
275 perror("Failed to munmap DV1394 ring buffer"); | |
276 | |
277 close(dv->fd); | |
278 | |
279 return 0; | |
280 } | |
281 | |
282 static AVInputFormat dv1394_format = { | |
283 .name = "dv1394", | |
284 .long_name = "dv1394 A/V grab", | |
285 .priv_data_size = sizeof(struct dv1394_data), | |
286 .read_header = dv1394_read_header, | |
287 .read_packet = dv1394_read_packet, | |
288 .read_close = dv1394_close, | |
289 .flags = AVFMT_NOFILE | |
290 }; | |
291 | |
292 int dv1394_init(void) | |
293 { | |
294 av_register_input_format(&dv1394_format); | |
295 return 0; | |
296 } |