Mercurial > libavformat.hg
annotate aviobuf.c @ 2840:f51675f78402 libavformat
Make recently added and still unused read_seek functions return offset_t.
author | michael |
---|---|
date | Wed, 19 Dec 2007 21:11:08 +0000 |
parents | b51319dd86e5 |
children | 7ad207078047 |
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" |
2683
153d6efc05b8
rename av_crc04C11DB7_update to ff_crc04C11DB7_update and move it to aviobuf.c so it can be reused by other (de)muxers
bcoudurier
parents:
2598
diff
changeset
|
23 #include "crc.h" |
0 | 24 #include <stdarg.h> |
25 | |
26 #define IO_BUFFER_SIZE 32768 | |
27 | |
1326 | 28 static void fill_buffer(ByteIOContext *s); |
29 | |
0 | 30 int init_put_byte(ByteIOContext *s, |
31 unsigned char *buffer, | |
32 int buffer_size, | |
33 int write_flag, | |
34 void *opaque, | |
65 | 35 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size), |
554 | 36 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
|
37 offset_t (*seek)(void *opaque, offset_t offset, int whence)) |
0 | 38 { |
39 s->buffer = buffer; | |
40 s->buffer_size = buffer_size; | |
41 s->buf_ptr = buffer; | |
2598
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
42 url_resetbuf(s, write_flag ? URL_WRONLY : URL_RDONLY); |
0 | 43 s->opaque = opaque; |
44 s->write_packet = write_packet; | |
45 s->read_packet = read_packet; | |
46 s->seek = seek; | |
47 s->pos = 0; | |
48 s->must_flush = 0; | |
49 s->eof_reached = 0; | |
554 | 50 s->error = 0; |
0 | 51 s->is_streamed = 0; |
52 s->max_packet_size = 0; | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
53 s->update_checksum= NULL; |
1403
1973528c6975
move memory reading ByteIOContext init from mov.c to avobuf.c
michael
parents:
1358
diff
changeset
|
54 if(!read_packet && !write_flag){ |
1973528c6975
move memory reading ByteIOContext init from mov.c to avobuf.c
michael
parents:
1358
diff
changeset
|
55 s->pos = buffer_size; |
1973528c6975
move memory reading ByteIOContext init from mov.c to avobuf.c
michael
parents:
1358
diff
changeset
|
56 s->buf_end = s->buffer + buffer_size; |
1973528c6975
move memory reading ByteIOContext init from mov.c to avobuf.c
michael
parents:
1358
diff
changeset
|
57 } |
2783
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
58 s->read_pause = NULL; |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
59 s->read_seek = NULL; |
0 | 60 return 0; |
61 } | |
885 | 62 |
0 | 63 static void flush_buffer(ByteIOContext *s) |
64 { | |
65 if (s->buf_ptr > s->buffer) { | |
554 | 66 if (s->write_packet && !s->error){ |
67 int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer); | |
68 if(ret < 0){ | |
69 s->error = ret; | |
70 } | |
71 } | |
421 | 72 if(s->update_checksum){ |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
73 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
|
74 s->checksum_ptr= s->buffer; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
75 } |
0 | 76 s->pos += s->buf_ptr - s->buffer; |
77 } | |
78 s->buf_ptr = s->buffer; | |
79 } | |
80 | |
81 void put_byte(ByteIOContext *s, int b) | |
82 { | |
83 *(s->buf_ptr)++ = b; | |
885 | 84 if (s->buf_ptr >= s->buf_end) |
0 | 85 flush_buffer(s); |
86 } | |
87 | |
88 void put_buffer(ByteIOContext *s, const unsigned char *buf, int size) | |
89 { | |
90 int len; | |
91 | |
92 while (size > 0) { | |
93 len = (s->buf_end - s->buf_ptr); | |
94 if (len > size) | |
95 len = size; | |
96 memcpy(s->buf_ptr, buf, len); | |
97 s->buf_ptr += len; | |
98 | |
885 | 99 if (s->buf_ptr >= s->buf_end) |
0 | 100 flush_buffer(s); |
101 | |
102 buf += len; | |
103 size -= len; | |
104 } | |
105 } | |
106 | |
107 void put_flush_packet(ByteIOContext *s) | |
108 { | |
109 flush_buffer(s); | |
110 s->must_flush = 0; | |
111 } | |
112 | |
113 offset_t url_fseek(ByteIOContext *s, offset_t offset, int whence) | |
114 { | |
115 offset_t offset1; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
116 offset_t pos; |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
117 |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
118 if(!s) |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
119 return AVERROR(EINVAL); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
120 |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
121 pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer)); |
0 | 122 |
123 if (whence != SEEK_CUR && whence != SEEK_SET) | |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1747
diff
changeset
|
124 return AVERROR(EINVAL); |
885 | 125 |
1323 | 126 if (whence == SEEK_CUR) { |
127 offset1 = pos + (s->buf_ptr - s->buffer); | |
128 if (offset == 0) | |
129 return offset1; | |
130 offset += offset1; | |
131 } | |
132 offset1 = offset - pos; | |
133 if (!s->must_flush && | |
134 offset1 >= 0 && offset1 < (s->buf_end - s->buffer)) { | |
135 /* can do the seek inside the buffer */ | |
136 s->buf_ptr = s->buffer + offset1; | |
1326 | 137 } else if(s->is_streamed && !s->write_flag && |
138 offset1 >= 0 && offset1 < (s->buf_end - s->buffer) + (1<<16)){ | |
139 while(s->pos < offset && !s->eof_reached) | |
140 fill_buffer(s); | |
2816
1523342b58b3
return error when url_fseek could not read until desired offset in streamed mode
bcoudurier
parents:
2783
diff
changeset
|
141 if (s->eof_reached) |
1523342b58b3
return error when url_fseek could not read until desired offset in streamed mode
bcoudurier
parents:
2783
diff
changeset
|
142 return AVERROR(EPIPE); |
1326 | 143 s->buf_ptr = s->buf_end + offset - s->pos; |
1323 | 144 } else { |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1747
diff
changeset
|
145 offset_t res = AVERROR(EPIPE); |
1747
fa70e732d2db
Fix misbehaviour in url_fseek() when seeking fails.
gpoirier
parents:
1741
diff
changeset
|
146 |
1546
41356096b2d0
Fix compile with --disable-muxers, patch by Lo«Ác Le Loarer, lll+ffmpeg m4x org.
diego
parents:
1403
diff
changeset
|
147 #if defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK) |
1323 | 148 if (s->write_flag) { |
0 | 149 flush_buffer(s); |
150 s->must_flush = 1; | |
1323 | 151 } else |
1546
41356096b2d0
Fix compile with --disable-muxers, patch by Lo«Ác Le Loarer, lll+ffmpeg m4x org.
diego
parents:
1403
diff
changeset
|
152 #endif /* defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK) */ |
1323 | 153 { |
154 s->buf_end = s->buffer; | |
0 | 155 } |
1323 | 156 s->buf_ptr = s->buffer; |
1747
fa70e732d2db
Fix misbehaviour in url_fseek() when seeking fails.
gpoirier
parents:
1741
diff
changeset
|
157 if (!s->seek || (res = s->seek(s->opaque, offset, SEEK_SET)) < 0) |
fa70e732d2db
Fix misbehaviour in url_fseek() when seeking fails.
gpoirier
parents:
1741
diff
changeset
|
158 return res; |
1323 | 159 s->pos = offset; |
0 | 160 } |
1323 | 161 s->eof_reached = 0; |
0 | 162 return offset; |
163 } | |
164 | |
165 void url_fskip(ByteIOContext *s, offset_t offset) | |
166 { | |
167 url_fseek(s, offset, SEEK_CUR); | |
168 } | |
169 | |
170 offset_t url_ftell(ByteIOContext *s) | |
171 { | |
172 return url_fseek(s, 0, SEEK_CUR); | |
173 } | |
174 | |
764
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
175 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
|
176 { |
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
177 offset_t size; |
885 | 178 |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
179 if(!s) |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
180 return AVERROR(EINVAL); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
181 |
764
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
182 if (!s->seek) |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1747
diff
changeset
|
183 return AVERROR(EPIPE); |
1612
a6eaa0762191
seekless filesize retrieving support in 7 lines of code, also doesnt break compatibility
michael
parents:
1592
diff
changeset
|
184 size = s->seek(s->opaque, 0, AVSEEK_SIZE); |
a6eaa0762191
seekless filesize retrieving support in 7 lines of code, also doesnt break compatibility
michael
parents:
1592
diff
changeset
|
185 if(size<0){ |
1741
e9c7714b1c34
proper error handling in file size retrieval, patch by Ronald S. Bultje rbultje at ronald bitfreak net
bcoudurier
parents:
1613
diff
changeset
|
186 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0) |
e9c7714b1c34
proper error handling in file size retrieval, patch by Ronald S. Bultje rbultje at ronald bitfreak net
bcoudurier
parents:
1613
diff
changeset
|
187 return size; |
e9c7714b1c34
proper error handling in file size retrieval, patch by Ronald S. Bultje rbultje at ronald bitfreak net
bcoudurier
parents:
1613
diff
changeset
|
188 size++; |
1613 | 189 s->seek(s->opaque, s->pos, SEEK_SET); |
1612
a6eaa0762191
seekless filesize retrieving support in 7 lines of code, also doesnt break compatibility
michael
parents:
1592
diff
changeset
|
190 } |
764
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
191 return size; |
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
192 } |
cdb845a57ae4
drop most url_fileno() calls (allows to use ByteIOContext directly in caller apps instead of URLProtocol)
aurel
parents:
719
diff
changeset
|
193 |
0 | 194 int url_feof(ByteIOContext *s) |
195 { | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
196 if(!s) |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
197 return 0; |
0 | 198 return s->eof_reached; |
199 } | |
200 | |
554 | 201 int url_ferror(ByteIOContext *s) |
202 { | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
203 if(!s) |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
204 return 0; |
554 | 205 return s->error; |
206 } | |
207 | |
0 | 208 void put_le32(ByteIOContext *s, unsigned int val) |
209 { | |
210 put_byte(s, val); | |
211 put_byte(s, val >> 8); | |
212 put_byte(s, val >> 16); | |
213 put_byte(s, val >> 24); | |
214 } | |
215 | |
216 void put_be32(ByteIOContext *s, unsigned int val) | |
217 { | |
218 put_byte(s, val >> 24); | |
219 put_byte(s, val >> 16); | |
220 put_byte(s, val >> 8); | |
221 put_byte(s, val); | |
222 } | |
223 | |
224 void put_strz(ByteIOContext *s, const char *str) | |
225 { | |
226 if (str) | |
227 put_buffer(s, (const unsigned char *) str, strlen(str) + 1); | |
228 else | |
229 put_byte(s, 0); | |
230 } | |
231 | |
65 | 232 void put_le64(ByteIOContext *s, uint64_t val) |
0 | 233 { |
65 | 234 put_le32(s, (uint32_t)(val & 0xffffffff)); |
235 put_le32(s, (uint32_t)(val >> 32)); | |
0 | 236 } |
237 | |
65 | 238 void put_be64(ByteIOContext *s, uint64_t val) |
0 | 239 { |
65 | 240 put_be32(s, (uint32_t)(val >> 32)); |
241 put_be32(s, (uint32_t)(val & 0xffffffff)); | |
0 | 242 } |
243 | |
244 void put_le16(ByteIOContext *s, unsigned int val) | |
245 { | |
246 put_byte(s, val); | |
247 put_byte(s, val >> 8); | |
248 } | |
249 | |
250 void put_be16(ByteIOContext *s, unsigned int val) | |
251 { | |
252 put_byte(s, val >> 8); | |
253 put_byte(s, val); | |
254 } | |
255 | |
937 | 256 void put_le24(ByteIOContext *s, unsigned int val) |
257 { | |
258 put_le16(s, val & 0xffff); | |
259 put_byte(s, val >> 16); | |
260 } | |
261 | |
822 | 262 void put_be24(ByteIOContext *s, unsigned int val) |
263 { | |
264 put_be16(s, val >> 8); | |
265 put_byte(s, val); | |
266 } | |
267 | |
0 | 268 void put_tag(ByteIOContext *s, const char *tag) |
269 { | |
270 while (*tag) { | |
271 put_byte(s, *tag++); | |
272 } | |
273 } | |
274 | |
275 /* Input stream */ | |
276 | |
277 static void fill_buffer(ByteIOContext *s) | |
278 { | |
2576 | 279 int len=0; |
0 | 280 |
281 /* no need to do anything if EOF already reached */ | |
282 if (s->eof_reached) | |
283 return; | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
284 |
421 | 285 if(s->update_checksum){ |
780 | 286 if(s->buf_end > s->checksum_ptr) |
287 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
|
288 s->checksum_ptr= s->buffer; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
289 } |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
290 |
2576 | 291 if(s->read_packet) |
2577 | 292 len = s->read_packet(s->opaque, s->buffer, s->buffer_size); |
0 | 293 if (len <= 0) { |
294 /* do not modify buffer if EOF reached so that a seek back can | |
295 be done without rereading data */ | |
296 s->eof_reached = 1; | |
2082 | 297 if(len<0) |
298 s->error= len; | |
0 | 299 } else { |
300 s->pos += len; | |
301 s->buf_ptr = s->buffer; | |
302 s->buf_end = s->buffer + len; | |
303 } | |
304 } | |
305 | |
2683
153d6efc05b8
rename av_crc04C11DB7_update to ff_crc04C11DB7_update and move it to aviobuf.c so it can be reused by other (de)muxers
bcoudurier
parents:
2598
diff
changeset
|
306 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf, unsigned int len){ |
153d6efc05b8
rename av_crc04C11DB7_update to ff_crc04C11DB7_update and move it to aviobuf.c so it can be reused by other (de)muxers
bcoudurier
parents:
2598
diff
changeset
|
307 return av_crc(av_crc04C11DB7, checksum, buf, len); |
153d6efc05b8
rename av_crc04C11DB7_update to ff_crc04C11DB7_update and move it to aviobuf.c so it can be reused by other (de)muxers
bcoudurier
parents:
2598
diff
changeset
|
308 } |
153d6efc05b8
rename av_crc04C11DB7_update to ff_crc04C11DB7_update and move it to aviobuf.c so it can be reused by other (de)muxers
bcoudurier
parents:
2598
diff
changeset
|
309 |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
310 unsigned long get_checksum(ByteIOContext *s){ |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
311 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr); |
421 | 312 s->update_checksum= NULL; |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
313 return s->checksum; |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
314 } |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
315 |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
316 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
|
317 s->update_checksum= update_checksum; |
421 | 318 if(s->update_checksum){ |
1184 | 319 s->checksum= checksum; |
421 | 320 s->checksum_ptr= s->buf_ptr; |
321 } | |
418
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
322 } |
41da3366d341
checksuming for nut & nice checksum API for libavformat
michael
parents:
389
diff
changeset
|
323 |
0 | 324 /* XXX: put an inline version */ |
325 int get_byte(ByteIOContext *s) | |
326 { | |
327 if (s->buf_ptr < s->buf_end) { | |
328 return *s->buf_ptr++; | |
329 } else { | |
330 fill_buffer(s); | |
331 if (s->buf_ptr < s->buf_end) | |
332 return *s->buf_ptr++; | |
333 else | |
334 return 0; | |
335 } | |
336 } | |
337 | |
338 int url_fgetc(ByteIOContext *s) | |
339 { | |
340 if (s->buf_ptr < s->buf_end) { | |
341 return *s->buf_ptr++; | |
342 } else { | |
343 fill_buffer(s); | |
344 if (s->buf_ptr < s->buf_end) | |
345 return *s->buf_ptr++; | |
346 else | |
347 return URL_EOF; | |
348 } | |
349 } | |
350 | |
351 int get_buffer(ByteIOContext *s, unsigned char *buf, int size) | |
352 { | |
353 int len, size1; | |
354 | |
355 size1 = size; | |
356 while (size > 0) { | |
357 len = s->buf_end - s->buf_ptr; | |
358 if (len > size) | |
359 len = size; | |
360 if (len == 0) { | |
719 | 361 if(size > s->buffer_size && !s->update_checksum){ |
2576 | 362 if(s->read_packet) |
2577 | 363 len = s->read_packet(s->opaque, buf, size); |
719 | 364 if (len <= 0) { |
365 /* do not modify buffer if EOF reached so that a seek back can | |
366 be done without rereading data */ | |
367 s->eof_reached = 1; | |
368 if(len<0) | |
369 s->error= len; | |
370 break; | |
371 } else { | |
372 s->pos += len; | |
373 size -= len; | |
374 buf += len; | |
375 s->buf_ptr = s->buffer; | |
376 s->buf_end = s->buffer/* + len*/; | |
377 } | |
378 }else{ | |
379 fill_buffer(s); | |
380 len = s->buf_end - s->buf_ptr; | |
381 if (len == 0) | |
382 break; | |
383 } | |
0 | 384 } else { |
385 memcpy(buf, s->buf_ptr, len); | |
386 buf += len; | |
387 s->buf_ptr += len; | |
388 size -= len; | |
389 } | |
390 } | |
391 return size1 - size; | |
392 } | |
393 | |
389
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
394 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
|
395 { |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
396 int len; |
885 | 397 |
643 | 398 if(size<0) |
399 return -1; | |
389
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
400 |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
401 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
|
402 if (len == 0) { |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
403 fill_buffer(s); |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
404 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
|
405 } |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
406 if (len > size) |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
407 len = size; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
408 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
|
409 s->buf_ptr += len; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
410 return len; |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
411 } |
e14fcd57ad2f
decode latency patch by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
364
diff
changeset
|
412 |
0 | 413 unsigned int get_le16(ByteIOContext *s) |
414 { | |
415 unsigned int val; | |
416 val = get_byte(s); | |
417 val |= get_byte(s) << 8; | |
418 return val; | |
419 } | |
420 | |
937 | 421 unsigned int get_le24(ByteIOContext *s) |
422 { | |
423 unsigned int val; | |
424 val = get_le16(s); | |
425 val |= get_byte(s) << 16; | |
426 return val; | |
427 } | |
428 | |
0 | 429 unsigned int get_le32(ByteIOContext *s) |
430 { | |
431 unsigned int val; | |
822 | 432 val = get_le16(s); |
433 val |= get_le16(s) << 16; | |
0 | 434 return val; |
435 } | |
436 | |
65 | 437 uint64_t get_le64(ByteIOContext *s) |
0 | 438 { |
65 | 439 uint64_t val; |
440 val = (uint64_t)get_le32(s); | |
441 val |= (uint64_t)get_le32(s) << 32; | |
0 | 442 return val; |
443 } | |
444 | |
445 unsigned int get_be16(ByteIOContext *s) | |
446 { | |
447 unsigned int val; | |
448 val = get_byte(s) << 8; | |
449 val |= get_byte(s); | |
450 return val; | |
451 } | |
452 | |
822 | 453 unsigned int get_be24(ByteIOContext *s) |
454 { | |
455 unsigned int val; | |
456 val = get_be16(s) << 8; | |
457 val |= get_byte(s); | |
458 return val; | |
459 } | |
0 | 460 unsigned int get_be32(ByteIOContext *s) |
461 { | |
462 unsigned int val; | |
822 | 463 val = get_be16(s) << 16; |
464 val |= get_be16(s); | |
0 | 465 return val; |
466 } | |
467 | |
468 char *get_strz(ByteIOContext *s, char *buf, int maxlen) | |
469 { | |
470 int i = 0; | |
471 char c; | |
472 | |
473 while ((c = get_byte(s))) { | |
474 if (i < maxlen-1) | |
475 buf[i++] = c; | |
476 } | |
885 | 477 |
0 | 478 buf[i] = 0; /* Ensure null terminated, but may be truncated */ |
479 | |
480 return buf; | |
481 } | |
482 | |
65 | 483 uint64_t get_be64(ByteIOContext *s) |
0 | 484 { |
65 | 485 uint64_t val; |
486 val = (uint64_t)get_be32(s) << 32; | |
487 val |= (uint64_t)get_be32(s); | |
0 | 488 return val; |
489 } | |
490 | |
2700 | 491 uint64_t ff_get_v(ByteIOContext *bc){ |
2699 | 492 uint64_t val = 0; |
493 int tmp; | |
494 | |
495 do{ | |
496 tmp = get_byte(bc); | |
497 val= (val<<7) + (tmp&127); | |
498 }while(tmp&128); | |
499 return val; | |
500 } | |
501 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
502 int url_fdopen(ByteIOContext **s, URLContext *h) |
0 | 503 { |
65 | 504 uint8_t *buffer; |
0 | 505 int buffer_size, max_packet_size; |
506 | |
885 | 507 |
0 | 508 max_packet_size = url_get_max_packet_size(h); |
509 if (max_packet_size) { | |
510 buffer_size = max_packet_size; /* no need to bufferize more than one packet */ | |
511 } else { | |
512 buffer_size = IO_BUFFER_SIZE; | |
513 } | |
514 buffer = av_malloc(buffer_size); | |
515 if (!buffer) | |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1747
diff
changeset
|
516 return AVERROR(ENOMEM); |
0 | 517 |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
518 *s = av_mallocz(sizeof(ByteIOContext)); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
519 if(!*s) { |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
520 av_free(buffer); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
521 return AVERROR(ENOMEM); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
522 } |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
523 |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
524 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
|
525 (h->flags & URL_WRONLY || h->flags & URL_RDWR), h, |
2831 | 526 url_read, url_write, url_seek) < 0) { |
0 | 527 av_free(buffer); |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
528 av_freep(s); |
2274
b21c2af60bc9
Replace all occurrences of AVERROR_IO with AVERROR(EIO).
takis
parents:
2082
diff
changeset
|
529 return AVERROR(EIO); |
0 | 530 } |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
531 (*s)->is_streamed = h->is_streamed; |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
532 (*s)->max_packet_size = max_packet_size; |
2783
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
533 if(h->prot) { |
2839
b51319dd86e5
Merge recently added and still unused play and pause functions.
michael
parents:
2831
diff
changeset
|
534 (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause; |
2840
f51675f78402
Make recently added and still unused read_seek functions return offset_t.
michael
parents:
2839
diff
changeset
|
535 (*s)->read_seek = (offset_t (*)(void *, int, int64_t, int))h->prot->url_read_seek; |
2783
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
536 } |
0 | 537 return 0; |
538 } | |
539 | |
540 int url_setbufsize(ByteIOContext *s, int buf_size) | |
541 { | |
65 | 542 uint8_t *buffer; |
0 | 543 buffer = av_malloc(buf_size); |
544 if (!buffer) | |
1787
eb16c64144ee
This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents:
1747
diff
changeset
|
545 return AVERROR(ENOMEM); |
0 | 546 |
547 av_free(s->buffer); | |
548 s->buffer = buffer; | |
549 s->buffer_size = buf_size; | |
550 s->buf_ptr = buffer; | |
2598
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
551 url_resetbuf(s, s->write_flag ? URL_WRONLY : URL_RDONLY); |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
552 return 0; |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
553 } |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
554 |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
555 int url_resetbuf(ByteIOContext *s, int flags) |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
556 { |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
557 URLContext *h = s->opaque; |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
558 if ((flags & URL_RDWR) || (h && h->flags != flags && !h->flags & URL_RDWR)) |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
559 return AVERROR(EINVAL); |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
560 |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
561 if (flags & URL_WRONLY) { |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
562 s->buf_end = s->buffer + s->buffer_size; |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
563 s->write_flag = 1; |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
564 } else { |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
565 s->buf_end = s->buffer; |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
566 s->write_flag = 0; |
fc7f8ee4700b
Add functionality to set the direction of a ByteIOContext buffer.
benoit
parents:
2577
diff
changeset
|
567 } |
0 | 568 return 0; |
569 } | |
570 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
571 int url_fopen(ByteIOContext **s, const char *filename, int flags) |
0 | 572 { |
573 URLContext *h; | |
574 int err; | |
575 | |
576 err = url_open(&h, filename, flags); | |
577 if (err < 0) | |
578 return err; | |
579 err = url_fdopen(s, h); | |
580 if (err < 0) { | |
581 url_close(h); | |
582 return err; | |
583 } | |
584 return 0; | |
585 } | |
586 | |
587 int url_fclose(ByteIOContext *s) | |
588 { | |
589 URLContext *h = s->opaque; | |
885 | 590 |
0 | 591 av_free(s->buffer); |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
592 av_free(s); |
0 | 593 return url_close(h); |
594 } | |
595 | |
596 URLContext *url_fileno(ByteIOContext *s) | |
597 { | |
598 return s->opaque; | |
599 } | |
600 | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
823
diff
changeset
|
601 #ifdef CONFIG_MUXERS |
0 | 602 int url_fprintf(ByteIOContext *s, const char *fmt, ...) |
603 { | |
604 va_list ap; | |
605 char buf[4096]; | |
606 int ret; | |
607 | |
608 va_start(ap, fmt); | |
609 ret = vsnprintf(buf, sizeof(buf), fmt, ap); | |
610 va_end(ap); | |
611 put_buffer(s, buf, strlen(buf)); | |
612 return ret; | |
613 } | |
858
66cc656ea404
Replace CONFIG_ENCODERS/CONFIG_DECODERS with CONFIG_MUXERS/CONFIG_DEMUXERS
diego
parents:
823
diff
changeset
|
614 #endif //CONFIG_MUXERS |
0 | 615 |
616 char *url_fgets(ByteIOContext *s, char *buf, int buf_size) | |
617 { | |
618 int c; | |
619 char *q; | |
620 | |
621 c = url_fgetc(s); | |
622 if (c == EOF) | |
623 return NULL; | |
624 q = buf; | |
625 for(;;) { | |
626 if (c == EOF || c == '\n') | |
627 break; | |
628 if ((q - buf) < buf_size - 1) | |
629 *q++ = c; | |
630 c = url_fgetc(s); | |
631 } | |
632 if (buf_size > 0) | |
633 *q = '\0'; | |
634 return buf; | |
635 } | |
636 | |
637 int url_fget_max_packet_size(ByteIOContext *s) | |
638 { | |
639 return s->max_packet_size; | |
640 } | |
641 | |
2839
b51319dd86e5
Merge recently added and still unused play and pause functions.
michael
parents:
2831
diff
changeset
|
642 int av_url_read_fpause(ByteIOContext *s, int pause) |
2783
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
643 { |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
644 if (!s->read_pause) |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
645 return AVERROR(ENOSYS); |
2839
b51319dd86e5
Merge recently added and still unused play and pause functions.
michael
parents:
2831
diff
changeset
|
646 return s->read_pause(s->opaque, pause); |
2783
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
647 } |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
648 |
2840
f51675f78402
Make recently added and still unused read_seek functions return offset_t.
michael
parents:
2839
diff
changeset
|
649 offset_t av_url_read_fseek(ByteIOContext *s, |
2783
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
650 int stream_index, int64_t timestamp, int flags) |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
651 { |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
652 URLContext *h = s->opaque; |
2840
f51675f78402
Make recently added and still unused read_seek functions return offset_t.
michael
parents:
2839
diff
changeset
|
653 offset_t ret; |
2783
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
654 if (!s->read_seek) |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
655 return AVERROR(ENOSYS); |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
656 ret = s->read_seek(h, stream_index, timestamp, flags); |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
657 if(ret >= 0) { |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
658 s->buf_ptr = s->buf_end; // Flush buffer |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
659 s->pos = s->seek(h, 0, SEEK_CUR); |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
660 } |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
661 return ret; |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
662 } |
1a9db30c1d1c
Extend ByteIOContext and add the buffered IO functions:
andoma
parents:
2771
diff
changeset
|
663 |
1546
41356096b2d0
Fix compile with --disable-muxers, patch by Lo«Ác Le Loarer, lll+ffmpeg m4x org.
diego
parents:
1403
diff
changeset
|
664 /* url_open_dyn_buf and url_close_dyn_buf are used in rtp.c to send a response |
41356096b2d0
Fix compile with --disable-muxers, patch by Lo«Ác Le Loarer, lll+ffmpeg m4x org.
diego
parents:
1403
diff
changeset
|
665 * back to the server even if CONFIG_MUXERS is not set. */ |
41356096b2d0
Fix compile with --disable-muxers, patch by Lo«Ác Le Loarer, lll+ffmpeg m4x org.
diego
parents:
1403
diff
changeset
|
666 #if defined(CONFIG_MUXERS) || defined(CONFIG_NETWORK) |
0 | 667 /* buffer handling */ |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
668 int url_open_buf(ByteIOContext **s, uint8_t *buf, int buf_size, int flags) |
0 | 669 { |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
670 int ret; |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
671 *s = av_mallocz(sizeof(ByteIOContext)); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
672 if(!*s) |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
673 return AVERROR(ENOMEM); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
674 ret = init_put_byte(*s, buf, buf_size, |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
675 (flags & URL_WRONLY || flags & URL_RDWR), |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
676 NULL, NULL, NULL, NULL); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
677 if(ret != 0) |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
678 av_freep(s); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
679 return ret; |
0 | 680 } |
681 | |
682 int url_close_buf(ByteIOContext *s) | |
683 { | |
684 put_flush_packet(s); | |
685 return s->buf_ptr - s->buffer; | |
686 } | |
687 | |
688 /* output in a dynamic buffer */ | |
689 | |
690 typedef struct DynBuffer { | |
691 int pos, size, allocated_size; | |
65 | 692 uint8_t *buffer; |
0 | 693 int io_buffer_size; |
65 | 694 uint8_t io_buffer[1]; |
0 | 695 } DynBuffer; |
696 | |
554 | 697 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size) |
0 | 698 { |
699 DynBuffer *d = opaque; | |
700 int new_size, new_allocated_size; | |
885 | 701 |
0 | 702 /* reallocate buffer if needed */ |
703 new_size = d->pos + buf_size; | |
704 new_allocated_size = d->allocated_size; | |
639 | 705 if(new_size < d->pos || new_size > INT_MAX/2) |
706 return -1; | |
0 | 707 while (new_size > new_allocated_size) { |
708 if (!new_allocated_size) | |
709 new_allocated_size = new_size; | |
710 else | |
885 | 711 new_allocated_size += new_allocated_size / 2 + 1; |
0 | 712 } |
885 | 713 |
0 | 714 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
|
715 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
|
716 if(d->buffer == NULL) |
554 | 717 return -1234; |
0 | 718 d->allocated_size = new_allocated_size; |
719 } | |
720 memcpy(d->buffer + d->pos, buf, buf_size); | |
721 d->pos = new_size; | |
722 if (d->pos > d->size) | |
723 d->size = d->pos; | |
554 | 724 return buf_size; |
0 | 725 } |
726 | |
554 | 727 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size) |
0 | 728 { |
729 unsigned char buf1[4]; | |
554 | 730 int ret; |
0 | 731 |
732 /* packetized write: output the header */ | |
733 buf1[0] = (buf_size >> 24); | |
734 buf1[1] = (buf_size >> 16); | |
735 buf1[2] = (buf_size >> 8); | |
736 buf1[3] = (buf_size); | |
554 | 737 ret= dyn_buf_write(opaque, buf1, 4); |
738 if(ret < 0) | |
739 return ret; | |
0 | 740 |
741 /* then the data */ | |
554 | 742 return dyn_buf_write(opaque, buf, buf_size); |
0 | 743 } |
744 | |
778
4fbe04f998bf
Fix url_fsize for large files patch by (Wolfram Gloger: wmglo, dent med uni-muenchen de)
michael
parents:
764
diff
changeset
|
745 static offset_t dyn_buf_seek(void *opaque, offset_t offset, int whence) |
0 | 746 { |
747 DynBuffer *d = opaque; | |
748 | |
749 if (whence == SEEK_CUR) | |
750 offset += d->pos; | |
751 else if (whence == SEEK_END) | |
752 offset += d->size; | |
753 if (offset < 0 || offset > 0x7fffffffLL) | |
754 return -1; | |
755 d->pos = offset; | |
756 return 0; | |
757 } | |
758 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
759 static int url_open_dyn_buf_internal(ByteIOContext **s, int max_packet_size) |
0 | 760 { |
761 DynBuffer *d; | |
762 int io_buffer_size, ret; | |
885 | 763 |
764 if (max_packet_size) | |
0 | 765 io_buffer_size = max_packet_size; |
766 else | |
767 io_buffer_size = 1024; | |
885 | 768 |
639 | 769 if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size) |
770 return -1; | |
0 | 771 d = av_malloc(sizeof(DynBuffer) + io_buffer_size); |
772 if (!d) | |
773 return -1; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
774 *s = av_mallocz(sizeof(ByteIOContext)); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
775 if(!*s) { |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
776 av_free(d); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
777 return AVERROR(ENOMEM); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
778 } |
0 | 779 d->io_buffer_size = io_buffer_size; |
780 d->buffer = NULL; | |
781 d->pos = 0; | |
782 d->size = 0; | |
783 d->allocated_size = 0; | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
784 ret = init_put_byte(*s, d->io_buffer, io_buffer_size, |
885 | 785 1, d, NULL, |
786 max_packet_size ? dyn_packet_buf_write : dyn_buf_write, | |
0 | 787 max_packet_size ? NULL : dyn_buf_seek); |
788 if (ret == 0) { | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
789 (*s)->max_packet_size = max_packet_size; |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
790 } else { |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
791 av_free(d); |
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
792 av_freep(s); |
0 | 793 } |
794 return ret; | |
795 } | |
796 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
797 int url_open_dyn_buf(ByteIOContext **s) |
0 | 798 { |
799 return url_open_dyn_buf_internal(s, 0); | |
800 } | |
801 | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
802 int url_open_dyn_packet_buf(ByteIOContext **s, int max_packet_size) |
0 | 803 { |
804 if (max_packet_size <= 0) | |
805 return -1; | |
806 return url_open_dyn_buf_internal(s, max_packet_size); | |
807 } | |
808 | |
65 | 809 int url_close_dyn_buf(ByteIOContext *s, uint8_t **pbuffer) |
0 | 810 { |
811 DynBuffer *d = s->opaque; | |
812 int size; | |
813 | |
814 put_flush_packet(s); | |
815 | |
816 *pbuffer = d->buffer; | |
817 size = d->size; | |
818 av_free(d); | |
2771
d52c718e83f9
Use dynamically allocated ByteIOContext in AVFormatContext
andoma
parents:
2700
diff
changeset
|
819 av_free(s); |
0 | 820 return size; |
821 } | |
1546
41356096b2d0
Fix compile with --disable-muxers, patch by Lo«Ác Le Loarer, lll+ffmpeg m4x org.
diego
parents:
1403
diff
changeset
|
822 #endif /* CONFIG_MUXERS || CONFIG_NETWORK */ |