00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/crc.h"
00023 #include "libavutil/intreadwrite.h"
00024 #include "avformat.h"
00025 #include "avio.h"
00026 #include "avio_internal.h"
00027 #include "internal.h"
00028 #include "url.h"
00029 #include <stdarg.h>
00030
00031 #define IO_BUFFER_SIZE 32768
00032
00038 #define SHORT_SEEK_THRESHOLD 4096
00039
00040 static void fill_buffer(AVIOContext *s);
00041 static int url_resetbuf(AVIOContext *s, int flags);
00042
00043 int ffio_init_context(AVIOContext *s,
00044 unsigned char *buffer,
00045 int buffer_size,
00046 int write_flag,
00047 void *opaque,
00048 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
00049 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
00050 int64_t (*seek)(void *opaque, int64_t offset, int whence))
00051 {
00052 s->buffer = buffer;
00053 s->buffer_size = buffer_size;
00054 s->buf_ptr = buffer;
00055 s->opaque = opaque;
00056 url_resetbuf(s, write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
00057 s->write_packet = write_packet;
00058 s->read_packet = read_packet;
00059 s->seek = seek;
00060 s->pos = 0;
00061 s->must_flush = 0;
00062 s->eof_reached = 0;
00063 s->error = 0;
00064 #if FF_API_OLD_AVIO
00065 s->is_streamed = 0;
00066 #endif
00067 s->seekable = AVIO_SEEKABLE_NORMAL;
00068 s->max_packet_size = 0;
00069 s->update_checksum= NULL;
00070 if(!read_packet && !write_flag){
00071 s->pos = buffer_size;
00072 s->buf_end = s->buffer + buffer_size;
00073 }
00074 s->read_pause = NULL;
00075 s->read_seek = NULL;
00076 return 0;
00077 }
00078
00079 #if FF_API_OLD_AVIO
00080 int init_put_byte(AVIOContext *s,
00081 unsigned char *buffer,
00082 int buffer_size,
00083 int write_flag,
00084 void *opaque,
00085 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
00086 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
00087 int64_t (*seek)(void *opaque, int64_t offset, int whence))
00088 {
00089 return ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
00090 read_packet, write_packet, seek);
00091 }
00092 AVIOContext *av_alloc_put_byte(
00093 unsigned char *buffer,
00094 int buffer_size,
00095 int write_flag,
00096 void *opaque,
00097 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
00098 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
00099 int64_t (*seek)(void *opaque, int64_t offset, int whence))
00100 {
00101 return avio_alloc_context(buffer, buffer_size, write_flag, opaque,
00102 read_packet, write_packet, seek);
00103 }
00104 #endif
00105
00106 AVIOContext *avio_alloc_context(
00107 unsigned char *buffer,
00108 int buffer_size,
00109 int write_flag,
00110 void *opaque,
00111 int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
00112 int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
00113 int64_t (*seek)(void *opaque, int64_t offset, int whence))
00114 {
00115 AVIOContext *s = av_mallocz(sizeof(AVIOContext));
00116 if (!s)
00117 return NULL;
00118 ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
00119 read_packet, write_packet, seek);
00120 return s;
00121 }
00122
00123 static void flush_buffer(AVIOContext *s)
00124 {
00125 if (s->buf_ptr > s->buffer) {
00126 if (s->write_packet && !s->error){
00127 int ret= s->write_packet(s->opaque, s->buffer, s->buf_ptr - s->buffer);
00128 if(ret < 0){
00129 s->error = ret;
00130 }
00131 }
00132 if(s->update_checksum){
00133 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
00134 s->checksum_ptr= s->buffer;
00135 }
00136 s->pos += s->buf_ptr - s->buffer;
00137 }
00138 s->buf_ptr = s->buffer;
00139 }
00140
00141 void avio_w8(AVIOContext *s, int b)
00142 {
00143 *(s->buf_ptr)++ = b;
00144 if (s->buf_ptr >= s->buf_end)
00145 flush_buffer(s);
00146 }
00147
00148 void ffio_fill(AVIOContext *s, int b, int count)
00149 {
00150 while (count > 0) {
00151 int len = FFMIN(s->buf_end - s->buf_ptr, count);
00152 memset(s->buf_ptr, b, len);
00153 s->buf_ptr += len;
00154
00155 if (s->buf_ptr >= s->buf_end)
00156 flush_buffer(s);
00157
00158 count -= len;
00159 }
00160 }
00161
00162 void avio_write(AVIOContext *s, const unsigned char *buf, int size)
00163 {
00164 while (size > 0) {
00165 int len = FFMIN(s->buf_end - s->buf_ptr, size);
00166 memcpy(s->buf_ptr, buf, len);
00167 s->buf_ptr += len;
00168
00169 if (s->buf_ptr >= s->buf_end)
00170 flush_buffer(s);
00171
00172 buf += len;
00173 size -= len;
00174 }
00175 }
00176
00177 void avio_flush(AVIOContext *s)
00178 {
00179 flush_buffer(s);
00180 s->must_flush = 0;
00181 }
00182
00183 int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
00184 {
00185 int64_t offset1;
00186 int64_t pos;
00187 int force = whence & AVSEEK_FORCE;
00188 whence &= ~AVSEEK_FORCE;
00189
00190 if(!s)
00191 return AVERROR(EINVAL);
00192
00193 pos = s->pos - (s->write_flag ? 0 : (s->buf_end - s->buffer));
00194
00195 if (whence != SEEK_CUR && whence != SEEK_SET)
00196 return AVERROR(EINVAL);
00197
00198 if (whence == SEEK_CUR) {
00199 offset1 = pos + (s->buf_ptr - s->buffer);
00200 if (offset == 0)
00201 return offset1;
00202 offset += offset1;
00203 }
00204 offset1 = offset - pos;
00205 if (!s->must_flush &&
00206 offset1 >= 0 && offset1 <= (s->buf_end - s->buffer)) {
00207
00208 s->buf_ptr = s->buffer + offset1;
00209 } else if ((!s->seekable ||
00210 offset1 <= s->buf_end + SHORT_SEEK_THRESHOLD - s->buffer) &&
00211 !s->write_flag && offset1 >= 0 &&
00212 (whence != SEEK_END || force)) {
00213 while(s->pos < offset && !s->eof_reached)
00214 fill_buffer(s);
00215 if (s->eof_reached)
00216 return AVERROR_EOF;
00217 s->buf_ptr = s->buf_end + offset - s->pos;
00218 } else {
00219 int64_t res;
00220
00221 #if CONFIG_MUXERS || CONFIG_NETWORK
00222 if (s->write_flag) {
00223 flush_buffer(s);
00224 s->must_flush = 1;
00225 }
00226 #endif
00227 if (!s->seek)
00228 return AVERROR(EPIPE);
00229 if ((res = s->seek(s->opaque, offset, SEEK_SET)) < 0)
00230 return res;
00231 if (!s->write_flag)
00232 s->buf_end = s->buffer;
00233 s->buf_ptr = s->buffer;
00234 s->pos = offset;
00235 }
00236 s->eof_reached = 0;
00237 return offset;
00238 }
00239
00240 int64_t avio_skip(AVIOContext *s, int64_t offset)
00241 {
00242 return avio_seek(s, offset, SEEK_CUR);
00243 }
00244
00245 #if FF_API_OLD_AVIO
00246 int url_fskip(AVIOContext *s, int64_t offset)
00247 {
00248 int64_t ret = avio_seek(s, offset, SEEK_CUR);
00249 return ret < 0 ? ret : 0;
00250 }
00251
00252 int64_t url_ftell(AVIOContext *s)
00253 {
00254 return avio_seek(s, 0, SEEK_CUR);
00255 }
00256 #endif
00257
00258 int64_t avio_size(AVIOContext *s)
00259 {
00260 int64_t size;
00261
00262 if(!s)
00263 return AVERROR(EINVAL);
00264
00265 if (!s->seek)
00266 return AVERROR(ENOSYS);
00267 size = s->seek(s->opaque, 0, AVSEEK_SIZE);
00268 if(size<0){
00269 if ((size = s->seek(s->opaque, -1, SEEK_END)) < 0)
00270 return size;
00271 size++;
00272 s->seek(s->opaque, s->pos, SEEK_SET);
00273 }
00274 return size;
00275 }
00276
00277 int url_feof(AVIOContext *s)
00278 {
00279 if(!s)
00280 return 0;
00281 if(s->eof_reached){
00282 s->eof_reached=0;
00283 fill_buffer(s);
00284 }
00285 return s->eof_reached;
00286 }
00287
00288 #if FF_API_OLD_AVIO
00289 int url_ferror(AVIOContext *s)
00290 {
00291 if(!s)
00292 return 0;
00293 return s->error;
00294 }
00295 #endif
00296
00297 void avio_wl32(AVIOContext *s, unsigned int val)
00298 {
00299 avio_w8(s, val);
00300 avio_w8(s, val >> 8);
00301 avio_w8(s, val >> 16);
00302 avio_w8(s, val >> 24);
00303 }
00304
00305 void avio_wb32(AVIOContext *s, unsigned int val)
00306 {
00307 avio_w8(s, val >> 24);
00308 avio_w8(s, val >> 16);
00309 avio_w8(s, val >> 8);
00310 avio_w8(s, val);
00311 }
00312
00313 #if FF_API_OLD_AVIO
00314 void put_strz(AVIOContext *s, const char *str)
00315 {
00316 avio_put_str(s, str);
00317 }
00318
00319 #define GET(name, type) \
00320 type get_be ##name(AVIOContext *s) \
00321 {\
00322 return avio_rb ##name(s);\
00323 }\
00324 type get_le ##name(AVIOContext *s) \
00325 {\
00326 return avio_rl ##name(s);\
00327 }
00328
00329 GET(16, unsigned int)
00330 GET(24, unsigned int)
00331 GET(32, unsigned int)
00332 GET(64, uint64_t)
00333
00334 #undef GET
00335
00336 #define PUT(name, type ) \
00337 void put_le ##name(AVIOContext *s, type val)\
00338 {\
00339 avio_wl ##name(s, val);\
00340 }\
00341 void put_be ##name(AVIOContext *s, type val)\
00342 {\
00343 avio_wb ##name(s, val);\
00344 }
00345
00346 PUT(16, unsigned int)
00347 PUT(24, unsigned int)
00348 PUT(32, unsigned int)
00349 PUT(64, uint64_t)
00350 #undef PUT
00351
00352 int get_byte(AVIOContext *s)
00353 {
00354 return avio_r8(s);
00355 }
00356 int get_buffer(AVIOContext *s, unsigned char *buf, int size)
00357 {
00358 return avio_read(s, buf, size);
00359 }
00360 int get_partial_buffer(AVIOContext *s, unsigned char *buf, int size)
00361 {
00362 return ffio_read_partial(s, buf, size);
00363 }
00364 void put_byte(AVIOContext *s, int val)
00365 {
00366 avio_w8(s, val);
00367 }
00368 void put_buffer(AVIOContext *s, const unsigned char *buf, int size)
00369 {
00370 avio_write(s, buf, size);
00371 }
00372 void put_nbyte(AVIOContext *s, int b, int count)
00373 {
00374 ffio_fill(s, b, count);
00375 }
00376
00377 int url_fopen(AVIOContext **s, const char *filename, int flags)
00378 {
00379 return avio_open(s, filename, flags);
00380 }
00381 int url_fclose(AVIOContext *s)
00382 {
00383 return avio_close(s);
00384 }
00385 int64_t url_fseek(AVIOContext *s, int64_t offset, int whence)
00386 {
00387 return avio_seek(s, offset, whence);
00388 }
00389 int64_t url_fsize(AVIOContext *s)
00390 {
00391 return avio_size(s);
00392 }
00393 int url_setbufsize(AVIOContext *s, int buf_size)
00394 {
00395 return ffio_set_buf_size(s, buf_size);
00396 }
00397 int url_fprintf(AVIOContext *s, const char *fmt, ...)
00398 {
00399 va_list ap;
00400 char buf[4096];
00401 int ret;
00402
00403 va_start(ap, fmt);
00404 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
00405 va_end(ap);
00406 avio_write(s, buf, strlen(buf));
00407 return ret;
00408 }
00409 void put_flush_packet(AVIOContext *s)
00410 {
00411 avio_flush(s);
00412 }
00413 int av_url_read_fpause(AVIOContext *s, int pause)
00414 {
00415 return avio_pause(s, pause);
00416 }
00417 int64_t av_url_read_fseek(AVIOContext *s, int stream_index,
00418 int64_t timestamp, int flags)
00419 {
00420 return avio_seek_time(s, stream_index, timestamp, flags);
00421 }
00422 void init_checksum(AVIOContext *s,
00423 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
00424 unsigned long checksum)
00425 {
00426 ffio_init_checksum(s, update_checksum, checksum);
00427 }
00428 unsigned long get_checksum(AVIOContext *s)
00429 {
00430 return ffio_get_checksum(s);
00431 }
00432 int url_open_dyn_buf(AVIOContext **s)
00433 {
00434 return avio_open_dyn_buf(s);
00435 }
00436 int url_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
00437 {
00438 return ffio_open_dyn_packet_buf(s, max_packet_size);
00439 }
00440 int url_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
00441 {
00442 return avio_close_dyn_buf(s, pbuffer);
00443 }
00444 int url_fdopen(AVIOContext **s, URLContext *h)
00445 {
00446 return ffio_fdopen(s, h);
00447 }
00448 #endif
00449
00450 int avio_put_str(AVIOContext *s, const char *str)
00451 {
00452 int len = 1;
00453 if (str) {
00454 len += strlen(str);
00455 avio_write(s, (const unsigned char *) str, len);
00456 } else
00457 avio_w8(s, 0);
00458 return len;
00459 }
00460
00461 int avio_put_str16le(AVIOContext *s, const char *str)
00462 {
00463 const uint8_t *q = str;
00464 int ret = 0;
00465
00466 while (*q) {
00467 uint32_t ch;
00468 uint16_t tmp;
00469
00470 GET_UTF8(ch, *q++, break;)
00471 PUT_UTF16(ch, tmp, avio_wl16(s, tmp);ret += 2;)
00472 }
00473 avio_wl16(s, 0);
00474 ret += 2;
00475 return ret;
00476 }
00477
00478 int ff_get_v_length(uint64_t val){
00479 int i=1;
00480
00481 while(val>>=7)
00482 i++;
00483
00484 return i;
00485 }
00486
00487 void ff_put_v(AVIOContext *bc, uint64_t val){
00488 int i= ff_get_v_length(val);
00489
00490 while(--i>0)
00491 avio_w8(bc, 128 | (val>>(7*i)));
00492
00493 avio_w8(bc, val&127);
00494 }
00495
00496 void avio_wl64(AVIOContext *s, uint64_t val)
00497 {
00498 avio_wl32(s, (uint32_t)(val & 0xffffffff));
00499 avio_wl32(s, (uint32_t)(val >> 32));
00500 }
00501
00502 void avio_wb64(AVIOContext *s, uint64_t val)
00503 {
00504 avio_wb32(s, (uint32_t)(val >> 32));
00505 avio_wb32(s, (uint32_t)(val & 0xffffffff));
00506 }
00507
00508 void avio_wl16(AVIOContext *s, unsigned int val)
00509 {
00510 avio_w8(s, val);
00511 avio_w8(s, val >> 8);
00512 }
00513
00514 void avio_wb16(AVIOContext *s, unsigned int val)
00515 {
00516 avio_w8(s, val >> 8);
00517 avio_w8(s, val);
00518 }
00519
00520 void avio_wl24(AVIOContext *s, unsigned int val)
00521 {
00522 avio_wl16(s, val & 0xffff);
00523 avio_w8(s, val >> 16);
00524 }
00525
00526 void avio_wb24(AVIOContext *s, unsigned int val)
00527 {
00528 avio_wb16(s, val >> 8);
00529 avio_w8(s, val);
00530 }
00531
00532 #if FF_API_OLD_AVIO
00533 void put_tag(AVIOContext *s, const char *tag)
00534 {
00535 while (*tag) {
00536 avio_w8(s, *tag++);
00537 }
00538 }
00539 #endif
00540
00541
00542
00543 static void fill_buffer(AVIOContext *s)
00544 {
00545 uint8_t *dst= !s->max_packet_size && s->buf_end - s->buffer < s->buffer_size ? s->buf_end : s->buffer;
00546 int len= s->buffer_size - (dst - s->buffer);
00547 int max_buffer_size = s->max_packet_size ? s->max_packet_size : IO_BUFFER_SIZE;
00548
00549
00550 if (s->eof_reached)
00551 return;
00552
00553 if(s->update_checksum && dst == s->buffer){
00554 if(s->buf_end > s->checksum_ptr)
00555 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_end - s->checksum_ptr);
00556 s->checksum_ptr= s->buffer;
00557 }
00558
00559
00560 if (s->read_packet && s->buffer_size > max_buffer_size) {
00561 ffio_set_buf_size(s, max_buffer_size);
00562
00563 s->checksum_ptr = dst = s->buffer;
00564 len = s->buffer_size;
00565 }
00566
00567 if(s->read_packet)
00568 len = s->read_packet(s->opaque, dst, len);
00569 else
00570 len = 0;
00571 if (len <= 0) {
00572
00573
00574 s->eof_reached = 1;
00575 if(len<0)
00576 s->error= len;
00577 } else {
00578 s->pos += len;
00579 s->buf_ptr = dst;
00580 s->buf_end = dst + len;
00581 }
00582 }
00583
00584 unsigned long ff_crc04C11DB7_update(unsigned long checksum, const uint8_t *buf,
00585 unsigned int len)
00586 {
00587 return av_crc(av_crc_get_table(AV_CRC_32_IEEE), checksum, buf, len);
00588 }
00589
00590 unsigned long ffio_get_checksum(AVIOContext *s)
00591 {
00592 s->checksum= s->update_checksum(s->checksum, s->checksum_ptr, s->buf_ptr - s->checksum_ptr);
00593 s->update_checksum= NULL;
00594 return s->checksum;
00595 }
00596
00597 void ffio_init_checksum(AVIOContext *s,
00598 unsigned long (*update_checksum)(unsigned long c, const uint8_t *p, unsigned int len),
00599 unsigned long checksum)
00600 {
00601 s->update_checksum= update_checksum;
00602 if(s->update_checksum){
00603 s->checksum= checksum;
00604 s->checksum_ptr= s->buf_ptr;
00605 }
00606 }
00607
00608
00609 int avio_r8(AVIOContext *s)
00610 {
00611 if (s->buf_ptr >= s->buf_end)
00612 fill_buffer(s);
00613 if (s->buf_ptr < s->buf_end)
00614 return *s->buf_ptr++;
00615 return 0;
00616 }
00617
00618 #if FF_API_OLD_AVIO
00619 int url_fgetc(AVIOContext *s)
00620 {
00621 if (s->buf_ptr >= s->buf_end)
00622 fill_buffer(s);
00623 if (s->buf_ptr < s->buf_end)
00624 return *s->buf_ptr++;
00625 return URL_EOF;
00626 }
00627 #endif
00628
00629 int avio_read(AVIOContext *s, unsigned char *buf, int size)
00630 {
00631 int len, size1;
00632
00633 size1 = size;
00634 while (size > 0) {
00635 len = s->buf_end - s->buf_ptr;
00636 if (len > size)
00637 len = size;
00638 if (len == 0) {
00639 if(size > s->buffer_size && !s->update_checksum){
00640 if(s->read_packet)
00641 len = s->read_packet(s->opaque, buf, size);
00642 if (len <= 0) {
00643
00644
00645 s->eof_reached = 1;
00646 if(len<0)
00647 s->error= len;
00648 break;
00649 } else {
00650 s->pos += len;
00651 size -= len;
00652 buf += len;
00653 s->buf_ptr = s->buffer;
00654 s->buf_end = s->buffer;
00655 }
00656 }else{
00657 fill_buffer(s);
00658 len = s->buf_end - s->buf_ptr;
00659 if (len == 0)
00660 break;
00661 }
00662 } else {
00663 memcpy(buf, s->buf_ptr, len);
00664 buf += len;
00665 s->buf_ptr += len;
00666 size -= len;
00667 }
00668 }
00669 if (size1 == size) {
00670 if (s->error) return s->error;
00671 if (url_feof(s)) return AVERROR_EOF;
00672 }
00673 return size1 - size;
00674 }
00675
00676 int ffio_read_partial(AVIOContext *s, unsigned char *buf, int size)
00677 {
00678 int len;
00679
00680 if(size<0)
00681 return -1;
00682
00683 len = s->buf_end - s->buf_ptr;
00684 if (len == 0) {
00685 fill_buffer(s);
00686 len = s->buf_end - s->buf_ptr;
00687 }
00688 if (len > size)
00689 len = size;
00690 memcpy(buf, s->buf_ptr, len);
00691 s->buf_ptr += len;
00692 if (!len) {
00693 if (s->error) return s->error;
00694 if (url_feof(s)) return AVERROR_EOF;
00695 }
00696 return len;
00697 }
00698
00699 unsigned int avio_rl16(AVIOContext *s)
00700 {
00701 unsigned int val;
00702 val = avio_r8(s);
00703 val |= avio_r8(s) << 8;
00704 return val;
00705 }
00706
00707 unsigned int avio_rl24(AVIOContext *s)
00708 {
00709 unsigned int val;
00710 val = avio_rl16(s);
00711 val |= avio_r8(s) << 16;
00712 return val;
00713 }
00714
00715 unsigned int avio_rl32(AVIOContext *s)
00716 {
00717 unsigned int val;
00718 val = avio_rl16(s);
00719 val |= avio_rl16(s) << 16;
00720 return val;
00721 }
00722
00723 uint64_t avio_rl64(AVIOContext *s)
00724 {
00725 uint64_t val;
00726 val = (uint64_t)avio_rl32(s);
00727 val |= (uint64_t)avio_rl32(s) << 32;
00728 return val;
00729 }
00730
00731 unsigned int avio_rb16(AVIOContext *s)
00732 {
00733 unsigned int val;
00734 val = avio_r8(s) << 8;
00735 val |= avio_r8(s);
00736 return val;
00737 }
00738
00739 unsigned int avio_rb24(AVIOContext *s)
00740 {
00741 unsigned int val;
00742 val = avio_rb16(s) << 8;
00743 val |= avio_r8(s);
00744 return val;
00745 }
00746 unsigned int avio_rb32(AVIOContext *s)
00747 {
00748 unsigned int val;
00749 val = avio_rb16(s) << 16;
00750 val |= avio_rb16(s);
00751 return val;
00752 }
00753
00754 #if FF_API_OLD_AVIO
00755 char *get_strz(AVIOContext *s, char *buf, int maxlen)
00756 {
00757 avio_get_str(s, INT_MAX, buf, maxlen);
00758 return buf;
00759 }
00760 #endif
00761
00762 int ff_get_line(AVIOContext *s, char *buf, int maxlen)
00763 {
00764 int i = 0;
00765 char c;
00766
00767 do {
00768 c = avio_r8(s);
00769 if (c && i < maxlen-1)
00770 buf[i++] = c;
00771 } while (c != '\n' && c);
00772
00773 buf[i] = 0;
00774 return i;
00775 }
00776
00777 int avio_get_str(AVIOContext *s, int maxlen, char *buf, int buflen)
00778 {
00779 int i;
00780
00781 if (buflen <= 0)
00782 return AVERROR(EINVAL);
00783
00784 buflen = FFMIN(buflen - 1, maxlen);
00785 for (i = 0; i < buflen; i++)
00786 if (!(buf[i] = avio_r8(s)))
00787 return i + 1;
00788 buf[i] = 0;
00789 for (; i < maxlen; i++)
00790 if (!avio_r8(s))
00791 return i + 1;
00792 return maxlen;
00793 }
00794
00795 #define GET_STR16(type, read) \
00796 int avio_get_str16 ##type(AVIOContext *pb, int maxlen, char *buf, int buflen)\
00797 {\
00798 char* q = buf;\
00799 int ret = 0;\
00800 if (buflen <= 0) \
00801 return AVERROR(EINVAL); \
00802 while (ret + 1 < maxlen) {\
00803 uint8_t tmp;\
00804 uint32_t ch;\
00805 GET_UTF16(ch, (ret += 2) <= maxlen ? read(pb) : 0, break;)\
00806 if (!ch)\
00807 break;\
00808 PUT_UTF8(ch, tmp, if (q - buf < buflen - 1) *q++ = tmp;)\
00809 }\
00810 *q = 0;\
00811 return ret;\
00812 }\
00813
00814 GET_STR16(le, avio_rl16)
00815 GET_STR16(be, avio_rb16)
00816
00817 #undef GET_STR16
00818
00819 uint64_t avio_rb64(AVIOContext *s)
00820 {
00821 uint64_t val;
00822 val = (uint64_t)avio_rb32(s) << 32;
00823 val |= (uint64_t)avio_rb32(s);
00824 return val;
00825 }
00826
00827 uint64_t ffio_read_varlen(AVIOContext *bc){
00828 uint64_t val = 0;
00829 int tmp;
00830
00831 do{
00832 tmp = avio_r8(bc);
00833 val= (val<<7) + (tmp&127);
00834 }while(tmp&128);
00835 return val;
00836 }
00837
00838 int ffio_fdopen(AVIOContext **s, URLContext *h)
00839 {
00840 uint8_t *buffer;
00841 int buffer_size, max_packet_size;
00842
00843 max_packet_size = h->max_packet_size;
00844 if (max_packet_size) {
00845 buffer_size = max_packet_size;
00846 } else {
00847 buffer_size = IO_BUFFER_SIZE;
00848 }
00849 buffer = av_malloc(buffer_size);
00850 if (!buffer)
00851 return AVERROR(ENOMEM);
00852
00853 *s = av_mallocz(sizeof(AVIOContext));
00854 if(!*s) {
00855 av_free(buffer);
00856 return AVERROR(ENOMEM);
00857 }
00858
00859 if (ffio_init_context(*s, buffer, buffer_size,
00860 h->flags & AVIO_FLAG_WRITE, h,
00861 (void*)ffurl_read, (void*)ffurl_write, (void*)ffurl_seek) < 0) {
00862 av_free(buffer);
00863 av_freep(s);
00864 return AVERROR(EIO);
00865 }
00866 #if FF_API_OLD_AVIO
00867 (*s)->is_streamed = h->is_streamed;
00868 #endif
00869 (*s)->seekable = h->is_streamed ? 0 : AVIO_SEEKABLE_NORMAL;
00870 (*s)->max_packet_size = max_packet_size;
00871 if(h->prot) {
00872 (*s)->read_pause = (int (*)(void *, int))h->prot->url_read_pause;
00873 (*s)->read_seek = (int64_t (*)(void *, int, int64_t, int))h->prot->url_read_seek;
00874 }
00875 return 0;
00876 }
00877
00878 int ffio_set_buf_size(AVIOContext *s, int buf_size)
00879 {
00880 uint8_t *buffer;
00881 buffer = av_malloc(buf_size);
00882 if (!buffer)
00883 return AVERROR(ENOMEM);
00884
00885 av_free(s->buffer);
00886 s->buffer = buffer;
00887 s->buffer_size = buf_size;
00888 s->buf_ptr = buffer;
00889 url_resetbuf(s, s->write_flag ? AVIO_FLAG_WRITE : AVIO_FLAG_READ);
00890 return 0;
00891 }
00892
00893 static int url_resetbuf(AVIOContext *s, int flags)
00894 {
00895 assert(flags == AVIO_FLAG_WRITE || flags == AVIO_FLAG_READ);
00896
00897 if (flags & AVIO_FLAG_WRITE) {
00898 s->buf_end = s->buffer + s->buffer_size;
00899 s->write_flag = 1;
00900 } else {
00901 s->buf_end = s->buffer;
00902 s->write_flag = 0;
00903 }
00904 return 0;
00905 }
00906
00907 int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
00908 {
00909 int64_t buffer_start;
00910 int buffer_size;
00911 int overlap, new_size, alloc_size;
00912
00913 if (s->write_flag)
00914 return AVERROR(EINVAL);
00915
00916 buffer_size = s->buf_end - s->buffer;
00917
00918
00919 if ((buffer_start = s->pos - buffer_size) > buf_size)
00920 return AVERROR(EINVAL);
00921
00922 overlap = buf_size - buffer_start;
00923 new_size = buf_size + buffer_size - overlap;
00924
00925 alloc_size = FFMAX(s->buffer_size, new_size);
00926 if (alloc_size > buf_size)
00927 if (!(buf = av_realloc_f(buf, 1, alloc_size)))
00928 return AVERROR(ENOMEM);
00929
00930 if (new_size > buf_size) {
00931 memcpy(buf + buf_size, s->buffer + overlap, buffer_size - overlap);
00932 buf_size = new_size;
00933 }
00934
00935 av_free(s->buffer);
00936 s->buf_ptr = s->buffer = buf;
00937 s->buffer_size = alloc_size;
00938 s->pos = buf_size;
00939 s->buf_end = s->buf_ptr + buf_size;
00940 s->eof_reached = 0;
00941 s->must_flush = 0;
00942
00943 return 0;
00944 }
00945
00946 int avio_open(AVIOContext **s, const char *filename, int flags)
00947 {
00948 URLContext *h;
00949 int err;
00950
00951 err = ffurl_open(&h, filename, flags);
00952 if (err < 0)
00953 return err;
00954 err = ffio_fdopen(s, h);
00955 if (err < 0) {
00956 ffurl_close(h);
00957 return err;
00958 }
00959 return 0;
00960 }
00961
00962 int avio_close(AVIOContext *s)
00963 {
00964 URLContext *h = s->opaque;
00965
00966 av_free(s->buffer);
00967 av_free(s);
00968 return ffurl_close(h);
00969 }
00970
00971 #if FF_API_OLD_AVIO
00972 URLContext *url_fileno(AVIOContext *s)
00973 {
00974 return s->opaque;
00975 }
00976 #endif
00977
00978 int avio_printf(AVIOContext *s, const char *fmt, ...)
00979 {
00980 va_list ap;
00981 char buf[4096];
00982 int ret;
00983
00984 va_start(ap, fmt);
00985 ret = vsnprintf(buf, sizeof(buf), fmt, ap);
00986 va_end(ap);
00987 avio_write(s, buf, strlen(buf));
00988 return ret;
00989 }
00990
00991 #if FF_API_OLD_AVIO
00992 char *url_fgets(AVIOContext *s, char *buf, int buf_size)
00993 {
00994 int c;
00995 char *q;
00996
00997 c = avio_r8(s);
00998 if (url_feof(s))
00999 return NULL;
01000 q = buf;
01001 for(;;) {
01002 if (url_feof(s) || c == '\n')
01003 break;
01004 if ((q - buf) < buf_size - 1)
01005 *q++ = c;
01006 c = avio_r8(s);
01007 }
01008 if (buf_size > 0)
01009 *q = '\0';
01010 return buf;
01011 }
01012
01013 int url_fget_max_packet_size(AVIOContext *s)
01014 {
01015 return s->max_packet_size;
01016 }
01017 #endif
01018
01019 int avio_pause(AVIOContext *s, int pause)
01020 {
01021 if (!s->read_pause)
01022 return AVERROR(ENOSYS);
01023 return s->read_pause(s->opaque, pause);
01024 }
01025
01026 int64_t avio_seek_time(AVIOContext *s, int stream_index,
01027 int64_t timestamp, int flags)
01028 {
01029 URLContext *h = s->opaque;
01030 int64_t ret;
01031 if (!s->read_seek)
01032 return AVERROR(ENOSYS);
01033 ret = s->read_seek(h, stream_index, timestamp, flags);
01034 if(ret >= 0) {
01035 int64_t pos;
01036 s->buf_ptr = s->buf_end;
01037 pos = s->seek(h, 0, SEEK_CUR);
01038 if (pos >= 0)
01039 s->pos = pos;
01040 else if (pos != AVERROR(ENOSYS))
01041 ret = pos;
01042 }
01043 return ret;
01044 }
01045
01046
01047 #if FF_API_OLD_AVIO
01048 int url_open_buf(AVIOContext **s, uint8_t *buf, int buf_size, int flags)
01049 {
01050 int ret;
01051 *s = av_mallocz(sizeof(AVIOContext));
01052 if(!*s)
01053 return AVERROR(ENOMEM);
01054 ret = ffio_init_context(*s, buf, buf_size,
01055 flags & AVIO_FLAG_WRITE,
01056 NULL, NULL, NULL, NULL);
01057 if(ret != 0)
01058 av_freep(s);
01059 return ret;
01060 }
01061
01062 int url_close_buf(AVIOContext *s)
01063 {
01064 avio_flush(s);
01065 return s->buf_ptr - s->buffer;
01066 }
01067 #endif
01068
01069
01070
01071 typedef struct DynBuffer {
01072 int pos, size, allocated_size;
01073 uint8_t *buffer;
01074 int io_buffer_size;
01075 uint8_t io_buffer[1];
01076 } DynBuffer;
01077
01078 static int dyn_buf_write(void *opaque, uint8_t *buf, int buf_size)
01079 {
01080 DynBuffer *d = opaque;
01081 unsigned new_size, new_allocated_size;
01082
01083
01084 new_size = d->pos + buf_size;
01085 new_allocated_size = d->allocated_size;
01086 if(new_size < d->pos || new_size > INT_MAX/2)
01087 return -1;
01088 while (new_size > new_allocated_size) {
01089 if (!new_allocated_size)
01090 new_allocated_size = new_size;
01091 else
01092 new_allocated_size += new_allocated_size / 2 + 1;
01093 }
01094
01095 if (new_allocated_size > d->allocated_size) {
01096 d->buffer = av_realloc_f(d->buffer, 1, new_allocated_size);
01097 if(d->buffer == NULL)
01098 return AVERROR(ENOMEM);
01099 d->allocated_size = new_allocated_size;
01100 }
01101 memcpy(d->buffer + d->pos, buf, buf_size);
01102 d->pos = new_size;
01103 if (d->pos > d->size)
01104 d->size = d->pos;
01105 return buf_size;
01106 }
01107
01108 static int dyn_packet_buf_write(void *opaque, uint8_t *buf, int buf_size)
01109 {
01110 unsigned char buf1[4];
01111 int ret;
01112
01113
01114 AV_WB32(buf1, buf_size);
01115 ret= dyn_buf_write(opaque, buf1, 4);
01116 if(ret < 0)
01117 return ret;
01118
01119
01120 return dyn_buf_write(opaque, buf, buf_size);
01121 }
01122
01123 static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
01124 {
01125 DynBuffer *d = opaque;
01126
01127 if (whence == SEEK_CUR)
01128 offset += d->pos;
01129 else if (whence == SEEK_END)
01130 offset += d->size;
01131 if (offset < 0 || offset > 0x7fffffffLL)
01132 return -1;
01133 d->pos = offset;
01134 return 0;
01135 }
01136
01137 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
01138 {
01139 DynBuffer *d;
01140 unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
01141
01142 if(sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
01143 return -1;
01144 d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
01145 if (!d)
01146 return AVERROR(ENOMEM);
01147 d->io_buffer_size = io_buffer_size;
01148 *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
01149 max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
01150 max_packet_size ? NULL : dyn_buf_seek);
01151 if(!*s) {
01152 av_free(d);
01153 return AVERROR(ENOMEM);
01154 }
01155 (*s)->max_packet_size = max_packet_size;
01156 return 0;
01157 }
01158
01159 int avio_open_dyn_buf(AVIOContext **s)
01160 {
01161 return url_open_dyn_buf_internal(s, 0);
01162 }
01163
01164 int ffio_open_dyn_packet_buf(AVIOContext **s, int max_packet_size)
01165 {
01166 if (max_packet_size <= 0)
01167 return -1;
01168 return url_open_dyn_buf_internal(s, max_packet_size);
01169 }
01170
01171 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
01172 {
01173 DynBuffer *d = s->opaque;
01174 int size;
01175 static const char padbuf[FF_INPUT_BUFFER_PADDING_SIZE] = {0};
01176 int padding = 0;
01177
01178
01179 if (!s->max_packet_size) {
01180 avio_write(s, padbuf, sizeof(padbuf));
01181 padding = FF_INPUT_BUFFER_PADDING_SIZE;
01182 }
01183
01184 avio_flush(s);
01185
01186 *pbuffer = d->buffer;
01187 size = d->size;
01188 av_free(d);
01189 av_free(s);
01190 return size - padding;
01191 }