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