Mercurial > libavformat.hg
annotate aviobuf.c @ 1381:d0a6c143b246 libavformat
support for Tiertex .seq files demuxing/video decoding, by Gregory Montoir %cyx A users P sourceforge P net%
author | bcoudurier |
---|---|
date | Thu, 12 Oct 2006 12:02:58 +0000 |
parents | 0899bfe4105c |
children | 1973528c6975 |
rev | line source |
---|---|
0 | 1 /* |
2 * Buffered I/O for ffmpeg system | |
3 * Copyright (c) 2000,2001 Fabrice Bellard | |
4 * | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1326
diff
changeset
|
5 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1326
diff
changeset
|
6 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1326
diff
changeset
|
7 * FFmpeg is free software; you can redistribute it and/or |
0 | 8 * modify it under the terms of the GNU Lesser General Public |
9 * License as published by the Free Software Foundation; either | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1326
diff
changeset
|
10 * version 2.1 of the License, or (at your option) any later version. |
0 | 11 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1326
diff
changeset
|
12 * FFmpeg is distributed in the hope that it will be useful, |
0 | 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1326
diff
changeset
|
18 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
887
diff
changeset
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 20 */ |
21 #include "avformat.h" | |
64 | 22 #include "avio.h" |
0 | 23 #include <stdarg.h> |
24 | |
25 #define IO_BUFFER_SIZE 32768 | |
26 | |
1326 | 27 static void fill_buffer(ByteIOContext *s); |
28 | |
0 | 29 int init_put_byte(ByteIOContext *s, |
30 unsigned char *buffer, | |
31 int buffer_size, | |
32 int write_flag, | |
33 void *opaque, | |
65 | 34 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size), |
554 | 35 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size), |
778
4fbe04f998bf
Fix url_fsize for large files patch by (Wolfram Gloger: wmglo, dent med uni-muenchen de)
michael
parents:
764
diff
changeset
|
36 offset_t (*seek)(void *opaque, offset_t offset, int whence)) |
0 | 37 { |
38 s->buffer = buffer; | |
39 s->buffer_size = buffer_size; | |
40 s->buf_ptr = buffer; | |
41 s->write_flag = write_flag; | |
885 | 42 if (!s->write_flag) |
0 | 43 s->buf_end = buffer; |
44 else | |
45 s->buf_end = buffer + buffer_size; | |
46 s->opaque = opaque; | |
47 s->write_packet = write_packet; | |
48 s->read_packet = read_packet; | |
49 s->seek = seek; | |
50 s->pos = 0; | |
51 s->must_flush = 0; | |
52 s->eof_reached = 0; | |
554 | 53 s->error = 0; |
0 | 54 s->is_streamed = 0; |
55 s->max_packet_size = 0; | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
56 s->update_checksum= NULL; |
0 | 57 return 0; |
58 } | |
885 | 59 |
0 | 60 static void flush_buffer(ByteIOContext *s) |
61 { | |
62 if (s->buf_ptr > s->buffer) { | |
554 | 63 if (s->write_packet && !s->error){ |
64 int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer); | |
65 if(ret < 0){ | |
66 s->error = ret; | |
67 } | |
68 } | |
421 | 69 if(s->update_checksum){ |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
70 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr); |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
71 s->checksum_ptr= s->buffer; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
72 } |
0 | 73 s->pos += s->buf_ptr - s->buffer; |
74 } | |
75 s->buf_ptr = s->buffer; | |
76 } | |
77 | |
78 void put_byte(ByteIOContext *s, int b) | |
79 { | |
80 *(s->buf_ptr)++ = b; | |
885 | 81 if (s->buf_ptr >= s->buf_end) |
0 | 82 flush_buffer(s); |
83 } | |
84 | |
85 void put_buffer(ByteIOContext *s, const unsigned char *buf, int size) | |
86 { | |
87 int len; | |
88 | |
89 while (size > 0) { | |
90 len = (s->buf_end - s->buf_ptr); | |
91 if (len > size) | |
92 len = size; | |
93 memcpy(s->buf_ptr, buf, len); | |
94 s->buf_ptr += len; | |
95 | |
885 | 96 if (s->buf_ptr >= s->buf_end) |
0 | 97 flush_buffer(s); |
98 | |
99 buf += len; | |
100 size -= len; | |
101 } | |
102 } | |
103 | |
104 void put_flush_packet(ByteIOContext *s) | |
105 { | |
106 flush_buffer(s); | |
107 s->must_flush = 0; | |
108 } | |
109 | |
110 offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence) | |
111 { | |
112 offset_t offset1; | |
1323 | 113 offset_t pos= s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer)); |
0 | 114 |
115 if (whence != SEEK_CUR && whence != SEEK_SET) | |
116 return -EINVAL; | |
885 | 117 |
1323 | 118 if (whence == SEEK_CUR) { |
119 offset1 = pos + (s->buf_ptr - s->buffer); | |
120 if (offset == 0) | |
121 return offset1; | |
122 offset += offset1; | |
123 } | |
124 offset1 = offset - pos; | |
125 if (!s->must_flush && | |
126 offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) { | |
127 /* can do the seek inside the buffer */ | |
128 s->buf_ptr = s->buffer + offset1; | |
1326 | 129 } else if(s->is_streamed && !s->write_flag && |
130 offset1 >= 0 && offset1 < (s->buf_end - s->buffer) + (1<<16)){ | |
131 while(s->pos < offset && !s->eof_reached) | |
132 fill_buffer(s); | |
133 s->buf_ptr = s->buf_end + offset - s->pos; | |
1323 | 134 } else { |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
823
diff
changeset
|
135 #ifdef CONFIG_MUXERS |
1323 | 136 if (s->write_flag) { |
0 | 137 flush_buffer(s); |
138 s->must_flush = 1; | |
1323 | 139 } else |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
823
diff
changeset
|
140 #endif //CONFIG_MUXERS |
1323 | 141 { |
142 s->buf_end = s->buffer; | |
0 | 143 } |
1323 | 144 s->buf_ptr = s->buffer; |
1325 | 145 if (!s->seek || s->seek(s->opaque, offset, SEEK_SET) == (offset_t)-EPIPE) |
1323 | 146 return -EPIPE; |
147 s->pos = offset; | |
0 | 148 } |
1323 | 149 s->eof_reached = 0; |
0 | 150 return offset; |
151 } | |
152 | |
153 void url_fskip(ByteIOContext *s, offset_t offset) | |
154 { | |
155 url_fseek(s, offset, SEEK_CUR); | |
156 } | |
157 | |
158 offset_t url_ftell(ByteIOContext *s) | |
159 { | |
160 return url_fseek(s, 0, SEEK_CUR); | |
161 } | |
162 | |
764
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
163 offset_t url_fsize(ByteIOContext *s) |
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
164 { |
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
165 offset_t size; |
885 | 166 |
764
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
167 if (!s->seek) |
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
168 return -EPIPE; |
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
169 size = s->seek(s->opaque, -1, SEEK_END) + 1; |
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
170 s->seek(s->opaque, s->pos, SEEK_SET); |
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
171 return size; |
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
172 } |
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
173 |
0 | 174 int url_feof(ByteIOContext *s) |
175 { | |
176 return s->eof_reached; | |
177 } | |
178 | |
554 | 179 int url_ferror(ByteIOContext *s) |
180 { | |
181 return s->error; | |
182 } | |
183 | |
905
dbc0145bbf11
Add --disable-protocols option to configure to disable I/O protocol from
diego
parents:
896
diff
changeset
|
184 #if defined(CONFIG_MUXERS) || defined(CONFIG_PROTOCOLS) |
0 | 185 void put_le32(ByteIOContext *s, unsigned int val) |
186 { | |
187 put_byte(s, val); | |
188 put_byte(s, val >> 8); | |
189 put_byte(s, val >> 16); | |
190 put_byte(s, val >> 24); | |
191 } | |
192 | |
193 void put_be32(ByteIOContext *s, unsigned int val) | |
194 { | |
195 put_byte(s, val >> 24); | |
196 put_byte(s, val >> 16); | |
197 put_byte(s, val >> 8); | |
198 put_byte(s, val); | |
199 } | |
200 | |
201 void put_strz(ByteIOContext *s, const char *str) | |
202 { | |
203 if (str) | |
204 put_buffer(s, (const unsigned char *) str, strlen(str) + 1); | |
205 else | |
206 put_byte(s, 0); | |
207 } | |
208 | |
65 | 209 void put_le64(ByteIOContext *s, uint64_t val) |
0 | 210 { |
65 | 211 put_le32(s, (uint32_t)(val & 0xffffffff)); |
212 put_le32(s, (uint32_t)(val >> 32)); | |
0 | 213 } |
214 | |
65 | 215 void put_be64(ByteIOContext *s, uint64_t val) |
0 | 216 { |
65 | 217 put_be32(s, (uint32_t)(val >> 32)); |
218 put_be32(s, (uint32_t)(val & 0xffffffff)); | |
0 | 219 } |
220 | |
221 void put_le16(ByteIOContext *s, unsigned int val) | |
222 { | |
223 put_byte(s, val); | |
224 put_byte(s, val >> 8); | |
225 } | |
226 | |
227 void put_be16(ByteIOContext *s, unsigned int val) | |
228 { | |
229 put_byte(s, val >> 8); | |
230 put_byte(s, val); | |
231 } | |
232 | |
937 | 233 void put_le24(ByteIOContext *s, unsigned int val) |
234 { | |
235 put_le16(s, val & 0xffff); | |
236 put_byte(s, val >> 16); | |
237 } | |
238 | |
822 | 239 void put_be24(ByteIOContext *s, unsigned int val) |
240 { | |
241 put_be16(s, val >> 8); | |
242 put_byte(s, val); | |
243 } | |
244 | |
0 | 245 void put_tag(ByteIOContext *s, const char *tag) |
246 { | |
247 while (*tag) { | |
248 put_byte(s, *tag++); | |
249 } | |
250 } | |
905
dbc0145bbf11
Add --disable-protocols option to configure to disable I/O protocol from
diego
parents:
896
diff
changeset
|
251 #endif //CONFIG_MUXERS || CONFIG_PROTOCOLS |
0 | 252 |
253 /* Input stream */ | |
254 | |
255 static void fill_buffer(ByteIOContext *s) | |
256 { | |
257 int len; | |
258 | |
259 /* no need to do anything if EOF already reached */ | |
260 if (s->eof_reached) | |
261 return; | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
262 |
421 | 263 if(s->update_checksum){ |
780 | 264 if(s->buf_end > s->checksum_ptr) |
265 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr); | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
266 s->checksum_ptr= s->buffer; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
267 } |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
268 |
0 | 269 len = s->read_packet(s->opaque, s->buffer, s->buffer_size); |
270 if (len <= 0) { | |
271 /* do not modify buffer if EOF reached so that a seek back can | |
272 be done without rereading data */ | |
273 s->eof_reached = 1; | |
554 | 274 if(len<0) |
275 s->error= len; | |
0 | 276 } else { |
277 s->pos += len; | |
278 s->buf_ptr = s->buffer; | |
279 s->buf_end = s->buffer + len; | |
280 } | |
281 } | |
282 | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
283 unsigned long get_checksum(ByteIOContext *s){ |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
284 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr); |
421 | 285 s->update_checksum= NULL; |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
286 return s->checksum; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
287 } |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
288 |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
289 void init_checksum(ByteIOContext *s, unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len), unsigned long checksum){ |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
290 s->update_checksum= update_checksum; |
421 | 291 if(s->update_checksum){ |
1184 | 292 s->checksum= checksum; |
421 | 293 s->checksum_ptr= s->buf_ptr; |
294 } | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
295 } |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
296 |
0 | 297 /* NOTE: return 0 if EOF, so you cannot use it if EOF handling is |
298 necessary */ | |
299 /* XXX: put an inline version */ | |
300 int get_byte(ByteIOContext *s) | |
301 { | |
302 if (s->buf_ptr < s->buf_end) { | |
303 return *s->buf_ptr++; | |
304 } else { | |
305 fill_buffer(s); | |
306 if (s->buf_ptr < s->buf_end) | |
307 return *s->buf_ptr++; | |
308 else | |
309 return 0; | |
310 } | |
311 } | |
312 | |
313 /* NOTE: return URL_EOF (-1) if EOF */ | |
314 int url_fgetc(ByteIOContext *s) | |
315 { | |
316 if (s->buf_ptr < s->buf_end) { | |
317 return *s->buf_ptr++; | |
318 } else { | |
319 fill_buffer(s); | |
320 if (s->buf_ptr < s->buf_end) | |
321 return *s->buf_ptr++; | |
322 else | |
323 return URL_EOF; | |
324 } | |
325 } | |
326 | |
327 int get_buffer(ByteIOContext *s, unsigned char *buf, int size) | |
328 { | |
329 int len, size1; | |
330 | |
331 size1 = size; | |
332 while (size > 0) { | |
333 len = s->buf_end - s->buf_ptr; | |
334 if (len > size) | |
335 len = size; | |
336 if (len == 0) { | |
719 | 337 if(size > s->buffer_size && !s->update_checksum){ |
338 len = s->read_packet(s->opaque, buf, size); | |
339 if (len <= 0) { | |
340 /* do not modify buffer if EOF reached so that a seek back can | |
341 be done without rereading data */ | |
342 s->eof_reached = 1; | |
343 if(len<0) | |
344 s->error= len; | |
345 break; | |
346 } else { | |
347 s->pos += len; | |
348 size -= len; | |
349 buf += len; | |
350 s->buf_ptr = s->buffer; | |
351 s->buf_end = s->buffer/* + len*/; | |
352 } | |
353 }else{ | |
354 fill_buffer(s); | |
355 len = s->buf_end - s->buf_ptr; | |
356 if (len == 0) | |
357 break; | |
358 } | |
0 | 359 } else { |
360 memcpy(buf, s->buf_ptr, len); | |
361 buf += len; | |
362 s->buf_ptr += len; | |
363 size -= len; | |
364 } | |
365 } | |
366 return size1 - size; | |
367 } | |
368 | |
389
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
369 int get_partial_buffer(ByteIOContext *s, unsigned char *buf, int size) |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
370 { |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
371 int len; |
885 | 372 |
643 | 373 if(size<0) |
374 return -1; | |
389
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
375 |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
376 len = s->buf_end - s->buf_ptr; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
377 if (len == 0) { |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
378 fill_buffer(s); |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
379 len = s->buf_end - s->buf_ptr; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
380 } |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
381 if (len > size) |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
382 len = size; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
383 memcpy(buf, s->buf_ptr, len); |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
384 s->buf_ptr += len; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
385 return len; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
386 } |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
387 |
0 | 388 unsigned int get_le16(ByteIOContext *s) |
389 { | |
390 unsigned int val; | |
391 val = get_byte(s); | |
392 val |= get_byte(s) << 8; | |
393 return val; | |
394 } | |
395 | |
937 | 396 unsigned int get_le24(ByteIOContext *s) |
397 { | |
398 unsigned int val; | |
399 val = get_le16(s); | |
400 val |= get_byte(s) << 16; | |
401 return val; | |
402 } | |
403 | |
0 | 404 unsigned int get_le32(ByteIOContext *s) |
405 { | |
406 unsigned int val; | |
822 | 407 val = get_le16(s); |
408 val |= get_le16(s) << 16; | |
0 | 409 return val; |
410 } | |
411 | |
65 | 412 uint64_t get_le64(ByteIOContext *s) |
0 | 413 { |
65 | 414 uint64_t val; |
415 val = (uint64_t)get_le32(s); | |
416 val |= (uint64_t)get_le32(s) << 32; | |
0 | 417 return val; |
418 } | |
419 | |
420 unsigned int get_be16(ByteIOContext *s) | |
421 { | |
422 unsigned int val; | |
423 val = get_byte(s) << 8; | |
424 val |= get_byte(s); | |
425 return val; | |
426 } | |
427 | |
822 | 428 unsigned int get_be24(ByteIOContext *s) |
429 { | |
430 unsigned int val; | |
431 val = get_be16(s) << 8; | |
432 val |= get_byte(s); | |
433 return val; | |
434 } | |
0 | 435 unsigned int get_be32(ByteIOContext *s) |
436 { | |
437 unsigned int val; | |
822 | 438 val = get_be16(s) << 16; |
439 val |= get_be16(s); | |
0 | 440 return val; |
441 } | |
442 | |
443 char *get_strz(ByteIOContext *s, char *buf, int maxlen) | |
444 { | |
445 int i = 0; | |
446 char c; | |
447 | |
448 while ((c = get_byte(s))) { | |
449 if (i < maxlen-1) | |
450 buf[i++] = c; | |
451 } | |
885 | 452 |
0 | 453 buf[i] = 0; /* Ensure null terminated, but may be truncated */ |
454 | |
455 return buf; | |
456 } | |
457 | |
65 | 458 uint64_t get_be64(ByteIOContext *s) |
0 | 459 { |
65 | 460 uint64_t val; |
461 val = (uint64_t)get_be32(s) << 32; | |
462 val |= (uint64_t)get_be32(s); | |
0 | 463 return val; |
464 } | |
465 | |
466 /* link with avio functions */ | |
467 | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
823
diff
changeset
|
468 #ifdef CONFIG_MUXERS |
554 | 469 static int url_write_packet(void *opaque, uint8_t *buf, int buf_size) |
0 | 470 { |
471 URLContext *h = opaque; | |
554 | 472 return url_write(h, buf, buf_size); |
0 | 473 } |
277
a313e1080322
disable encoders where appropriate (patch courtesy of BERO
melanson
parents:
122
diff
changeset
|
474 #else |
887 | 475 #define url_write_packet NULL |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
823
diff
changeset
|
476 #endif //CONFIG_MUXERS |
0 | 477 |
65 | 478 static int url_read_packet(void *opaque, uint8_t *buf, int buf_size) |
0 | 479 { |
480 URLContext *h = opaque; | |
481 return url_read(h, buf, buf_size); | |
482 } | |
483 | |
778
4fbe04f998bf
Fix url_fsize for large files patch by (Wolfram Gloger: wmglo, dent med uni-muenchen de)
michael
parents:
764
diff
changeset
|
484 static offset_t url_seek_packet(void *opaque, offset_t offset, int whence) |
0 | 485 { |
486 URLContext *h = opaque; | |
502
813b0119a98e
ffserver fixes by (Koos Vriezen <koos.vriezen at xs4all dot nl>)
michael
parents:
482
diff
changeset
|
487 return url_seek(h, offset, whence); |
813b0119a98e
ffserver fixes by (Koos Vriezen <koos.vriezen at xs4all dot nl>)
michael
parents:
482
diff
changeset
|
488 //return 0; |
0 | 489 } |
490 | |
491 int url_fdopen(ByteIOContext *s, URLContext *h) | |
492 { | |
65 | 493 uint8_t *buffer; |
0 | 494 int buffer_size, max_packet_size; |
495 | |
885 | 496 |
0 | 497 max_packet_size = url_get_max_packet_size(h); |
498 if (max_packet_size) { | |
499 buffer_size = max_packet_size; /* no need to bufferize more than one packet */ | |
500 } else { | |
501 buffer_size = IO_BUFFER_SIZE; | |
502 } | |
503 buffer = av_malloc(buffer_size); | |
504 if (!buffer) | |
505 return -ENOMEM; | |
506 | |
885 | 507 if (init_put_byte(s, buffer, buffer_size, |
364
0d74e8abcb3d
avio patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
277
diff
changeset
|
508 (h->flags & URL_WRONLY || h->flags & URL_RDWR), h, |
0 | 509 url_read_packet, url_write_packet, url_seek_packet) < 0) { |
510 av_free(buffer); | |
482 | 511 return AVERROR_IO; |
0 | 512 } |
513 s->is_streamed = h->is_streamed; | |
514 s->max_packet_size = max_packet_size; | |
515 return 0; | |
516 } | |
517 | |
518 /* XXX: must be called before any I/O */ | |
519 int url_setbufsize(ByteIOContext *s, int buf_size) | |
520 { | |
65 | 521 uint8_t *buffer; |
0 | 522 buffer = av_malloc(buf_size); |
523 if (!buffer) | |
524 return -ENOMEM; | |
525 | |
526 av_free(s->buffer); | |
527 s->buffer = buffer; | |
528 s->buffer_size = buf_size; | |
529 s->buf_ptr = buffer; | |
885 | 530 if (!s->write_flag) |
0 | 531 s->buf_end = buffer; |
532 else | |
533 s->buf_end = buffer + buf_size; | |
534 return 0; | |
535 } | |
536 | |
537 /* NOTE: when opened as read/write, the buffers are only used for | |
538 reading */ | |
539 int url_fopen(ByteIOContext *s, const char *filename, int flags) | |
540 { | |
541 URLContext *h; | |
542 int err; | |
543 | |
544 err = url_open(&h, filename, flags); | |
545 if (err < 0) | |
546 return err; | |
547 err = url_fdopen(s, h); | |
548 if (err < 0) { | |
549 url_close(h); | |
550 return err; | |
551 } | |
552 return 0; | |
553 } | |
554 | |
555 int url_fclose(ByteIOContext *s) | |
556 { | |
557 URLContext *h = s->opaque; | |
885 | 558 |
0 | 559 av_free(s->buffer); |
560 memset(s, 0, sizeof(ByteIOContext)); | |
561 return url_close(h); | |
562 } | |
563 | |
564 URLContext *url_fileno(ByteIOContext *s) | |
565 { | |
566 return s->opaque; | |
567 } | |
568 | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
823
diff
changeset
|
569 #ifdef CONFIG_MUXERS |
0 | 570 /* XXX: currently size is limited */ |
571 int url_fprintf(ByteIOContext *s, const char *fmt, ...) | |
572 { | |
573 va_list ap; | |
574 char buf[4096]; | |
575 int ret; | |
576 | |
577 va_start(ap, fmt); | |
578 ret = vsnprintf(buf, sizeof(buf), fmt, ap); | |
579 va_end(ap); | |
580 put_buffer(s, buf, strlen(buf)); | |
581 return ret; | |
582 } | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
823
diff
changeset
|
583 #endif //CONFIG_MUXERS |
0 | 584 |
585 /* note: unlike fgets, the EOL character is not returned and a whole | |
586 line is parsed. return NULL if first char read was EOF */ | |
587 char *url_fgets(ByteIOContext *s, char *buf, int buf_size) | |
588 { | |
589 int c; | |
590 char *q; | |
591 | |
592 c = url_fgetc(s); | |
593 if (c == EOF) | |
594 return NULL; | |
595 q = buf; | |
596 for(;;) { | |
597 if (c == EOF || c == '\n') | |
598 break; | |
599 if ((q - buf) < buf_size - 1) | |
600 *q++ = c; | |
601 c = url_fgetc(s); | |
602 } | |
603 if (buf_size > 0) | |
604 *q = '\0'; | |
605 return buf; | |
606 } | |
607 | |
885 | 608 /* |
0 | 609 * Return the maximum packet size associated to packetized buffered file |
610 * handle. If the file is not packetized (stream like http or file on | |
611 * disk), then 0 is returned. | |
885 | 612 * |
0 | 613 * @param h buffered file handle |
614 * @return maximum packet size in bytes | |
615 */ | |
616 int url_fget_max_packet_size(ByteIOContext *s) | |
617 { | |
618 return s->max_packet_size; | |
619 } | |
620 | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
823
diff
changeset
|
621 #ifdef CONFIG_MUXERS |
0 | 622 /* buffer handling */ |
65 | 623 int url_open_buf(ByteIOContext *s, uint8_t *buf, int buf_size, int flags) |
0 | 624 { |
885 | 625 return init_put_byte(s, buf, buf_size, |
364
0d74e8abcb3d
avio patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
277
diff
changeset
|
626 (flags & URL_WRONLY || flags & URL_RDWR), |
0d74e8abcb3d
avio patch by (Gildas Bazin <gbazin at altern dot org>)
michael
parents:
277
diff
changeset
|
627 NULL, NULL, NULL, NULL); |
0 | 628 } |
629 | |
630 /* return the written or read size */ | |
631 int url_close_buf(ByteIOContext *s) | |
632 { | |
633 put_flush_packet(s); | |
634 return s->buf_ptr - s->buffer; | |
635 } | |
636 | |
637 /* output in a dynamic buffer */ | |
638 | |
639 typedef struct DynBuffer { | |
640 int pos, size, allocated_size; | |
65 | 641 uint8_t *buffer; |
0 | 642 int io_buffer_size; |
65 | 643 uint8_t io_buffer[1]; |
0 | 644 } DynBuffer; |
645 | |
554 | 646 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size) |
0 | 647 { |
648 DynBuffer *d = opaque; | |
649 int new_size, new_allocated_size; | |
885 | 650 |
0 | 651 /* reallocate buffer if needed */ |
652 new_size = d->pos + buf_size; | |
653 new_allocated_size = d->allocated_size; | |
639 | 654 if(new_size < d->pos || new_size > INT_MAX/2) |
655 return -1; | |
0 | 656 while (new_size > new_allocated_size) { |
657 if (!new_allocated_size) | |
658 new_allocated_size = new_size; | |
659 else | |
885 | 660 new_allocated_size += new_allocated_size / 2 + 1; |
0 | 661 } |
885 | 662 |
0 | 663 if (new_allocated_size > d->allocated_size) { |
93
69ed49c151bf
ffserver deallocate ctx->streams on closing patch by (Mark Hills <mark at pogo dot org dot uk>)
michaelni
parents:
65
diff
changeset
|
664 d->buffer = av_realloc(d->buffer, new_allocated_size); |
69ed49c151bf
ffserver deallocate ctx->streams on closing patch by (Mark Hills <mark at pogo dot org dot uk>)
michaelni
parents:
65
diff
changeset
|
665 if(d->buffer == NULL) |
554 | 666 return -1234; |
0 | 667 d->allocated_size = new_allocated_size; |
668 } | |
669 memcpy(d->buffer + d->pos, buf, buf_size); | |
670 d->pos = new_size; | |
671 if (d->pos > d->size) | |
672 d->size = d->pos; | |
554 | 673 return buf_size; |
0 | 674 } |
675 | |
554 | 676 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size) |
0 | 677 { |
678 unsigned char buf1[4]; | |
554 | 679 int ret; |
0 | 680 |
681 /* packetized write: output the header */ | |
682 buf1[0] = (buf_size >> 24); | |
683 buf1[1] = (buf_size >> 16); | |
684 buf1[2] = (buf_size >> 8); | |
685 buf1[3] = (buf_size); | |
554 | 686 ret= dyn_buf_write(opaque, buf1, 4); |
687 if(ret < 0) | |
688 return ret; | |
0 | 689 |
690 /* then the data */ | |
554 | 691 return dyn_buf_write(opaque, buf, buf_size); |
0 | 692 } |
693 | |
778
4fbe04f998bf
Fix url_fsize for large files patch by (Wolfram Gloger: wmglo, dent med uni-muenchen de)
michael
parents:
764
diff
changeset
|
694 static offset_t dyn_buf_seek(void *opaque, offset_t offset, int whence) |
0 | 695 { |
696 DynBuffer *d = opaque; | |
697 | |
698 if (whence == SEEK_CUR) | |
699 offset += d->pos; | |
700 else if (whence == SEEK_END) | |
701 offset += d->size; | |
702 if (offset < 0 || offset > 0x7fffffffLL) | |
703 return -1; | |
704 d->pos = offset; | |
705 return 0; | |
706 } | |
707 | |
708 static int url_open_dyn_buf_internal(ByteIOContext *s, int max_packet_size) | |
709 { | |
710 DynBuffer *d; | |
711 int io_buffer_size, ret; | |
885 | 712 |
713 if (max_packet_size) | |
0 | 714 io_buffer_size = max_packet_size; |
715 else | |
716 io_buffer_size = 1024; | |
885 | 717 |
639 | 718 if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size) |
719 return -1; | |
0 | 720 d = av_malloc(sizeof(DynBuffer) + io_buffer_size); |
721 if (!d) | |
722 return -1; | |
723 d->io_buffer_size = io_buffer_size; | |
724 d->buffer = NULL; | |
725 d->pos = 0; | |
726 d->size = 0; | |
727 d->allocated_size = 0; | |
885 | 728 ret = init_put_byte(s, d->io_buffer, io_buffer_size, |
729 1, d, NULL, | |
730 max_packet_size ? dyn_packet_buf_write : dyn_buf_write, | |
0 | 731 max_packet_size ? NULL : dyn_buf_seek); |
732 if (ret == 0) { | |
733 s->max_packet_size = max_packet_size; | |
734 } | |
735 return ret; | |
736 } | |
737 | |
738 /* | |
739 * Open a write only memory stream. | |
885 | 740 * |
0 | 741 * @param s new IO context |
742 * @return zero if no error. | |
743 */ | |
744 int url_open_dyn_buf(ByteIOContext *s) | |
745 { | |
746 return url_open_dyn_buf_internal(s, 0); | |
747 } | |
748 | |
749 /* | |
750 * Open a write only packetized memory stream with a maximum packet | |
751 * size of 'max_packet_size'. The stream is stored in a memory buffer | |
752 * with a big endian 4 byte header giving the packet size in bytes. | |
885 | 753 * |
0 | 754 * @param s new IO context |
885 | 755 * @param max_packet_size maximum packet size (must be > 0) |
0 | 756 * @return zero if no error. |
757 */ | |
758 int url_open_dyn_packet_buf(ByteIOContext *s, int max_packet_size) | |
759 { | |
760 if (max_packet_size <= 0) | |
761 return -1; | |
762 return url_open_dyn_buf_internal(s, max_packet_size); | |
763 } | |
764 | |
885 | 765 /* |
0 | 766 * Return the written size and a pointer to the buffer. The buffer |
885 | 767 * must be freed with av_free(). |
0 | 768 * @param s IO context |
769 * @param pointer to a byte buffer | |
770 * @return the length of the byte buffer | |
771 */ | |
65 | 772 int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer) |
0 | 773 { |
774 DynBuffer *d = s->opaque; | |
775 int size; | |
776 | |
777 put_flush_packet(s); | |
778 | |
779 *pbuffer = d->buffer; | |
780 size = d->size; | |
781 av_free(d); | |
782 return size; | |
783 } | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
823
diff
changeset
|
784 #endif //CONFIG_MUXERS |