00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef AVCODEC_BYTESTREAM_H
00024 #define AVCODEC_BYTESTREAM_H
00025
00026 #include <stdint.h>
00027 #include <string.h>
00028
00029 #include "libavutil/common.h"
00030 #include "libavutil/intreadwrite.h"
00031
00032 typedef struct {
00033 const uint8_t *buffer, *buffer_end, *buffer_start;
00034 } GetByteContext;
00035
00036 typedef struct {
00037 uint8_t *buffer, *buffer_end, *buffer_start;
00038 int eof;
00039 } PutByteContext;
00040
00041 #define DEF(type, name, bytes, read, write) \
00042 static av_always_inline type bytestream_get_ ## name(const uint8_t **b) \
00043 { \
00044 (*b) += bytes; \
00045 return read(*b - bytes); \
00046 } \
00047 static av_always_inline void bytestream_put_ ## name(uint8_t **b, \
00048 const type value) \
00049 { \
00050 write(*b, value); \
00051 (*b) += bytes; \
00052 } \
00053 static av_always_inline void bytestream2_put_ ## name ## u(PutByteContext *p, \
00054 const type value) \
00055 { \
00056 bytestream_put_ ## name(&p->buffer, value); \
00057 } \
00058 static av_always_inline void bytestream2_put_ ## name(PutByteContext *p, \
00059 const type value) \
00060 { \
00061 if (!p->eof && (p->buffer_end - p->buffer >= bytes)) { \
00062 write(p->buffer, value); \
00063 p->buffer += bytes; \
00064 } else \
00065 p->eof = 1; \
00066 } \
00067 static av_always_inline type bytestream2_get_ ## name ## u(GetByteContext *g) \
00068 { \
00069 return bytestream_get_ ## name(&g->buffer); \
00070 } \
00071 static av_always_inline type bytestream2_get_ ## name(GetByteContext *g) \
00072 { \
00073 if (g->buffer_end - g->buffer < bytes) \
00074 return 0; \
00075 return bytestream2_get_ ## name ## u(g); \
00076 } \
00077 static av_always_inline type bytestream2_peek_ ## name(GetByteContext *g) \
00078 { \
00079 if (g->buffer_end - g->buffer < bytes) \
00080 return 0; \
00081 return read(g->buffer); \
00082 }
00083
00084 DEF(uint64_t, le64, 8, AV_RL64, AV_WL64)
00085 DEF(unsigned int, le32, 4, AV_RL32, AV_WL32)
00086 DEF(unsigned int, le24, 3, AV_RL24, AV_WL24)
00087 DEF(unsigned int, le16, 2, AV_RL16, AV_WL16)
00088 DEF(uint64_t, be64, 8, AV_RB64, AV_WB64)
00089 DEF(unsigned int, be32, 4, AV_RB32, AV_WB32)
00090 DEF(unsigned int, be24, 3, AV_RB24, AV_WB24)
00091 DEF(unsigned int, be16, 2, AV_RB16, AV_WB16)
00092 DEF(unsigned int, byte, 1, AV_RB8 , AV_WB8)
00093
00094 #if HAVE_BIGENDIAN
00095 # define bytestream2_get_ne16 bytestream2_get_be16
00096 # define bytestream2_get_ne24 bytestream2_get_be24
00097 # define bytestream2_get_ne32 bytestream2_get_be32
00098 # define bytestream2_get_ne64 bytestream2_get_be64
00099 # define bytestream2_get_ne16u bytestream2_get_be16u
00100 # define bytestream2_get_ne24u bytestream2_get_be24u
00101 # define bytestream2_get_ne32u bytestream2_get_be32u
00102 # define bytestream2_get_ne64u bytestream2_get_be64u
00103 # define bytestream2_put_ne16 bytestream2_put_be16
00104 # define bytestream2_put_ne24 bytestream2_put_be24
00105 # define bytestream2_put_ne32 bytestream2_put_be32
00106 # define bytestream2_put_ne64 bytestream2_put_be64
00107 # define bytestream2_peek_ne16 bytestream2_peek_be16
00108 # define bytestream2_peek_ne24 bytestream2_peek_be24
00109 # define bytestream2_peek_ne32 bytestream2_peek_be32
00110 # define bytestream2_peek_ne64 bytestream2_peek_be64
00111 #else
00112 # define bytestream2_get_ne16 bytestream2_get_le16
00113 # define bytestream2_get_ne24 bytestream2_get_le24
00114 # define bytestream2_get_ne32 bytestream2_get_le32
00115 # define bytestream2_get_ne64 bytestream2_get_le64
00116 # define bytestream2_get_ne16u bytestream2_get_le16u
00117 # define bytestream2_get_ne24u bytestream2_get_le24u
00118 # define bytestream2_get_ne32u bytestream2_get_le32u
00119 # define bytestream2_get_ne64u bytestream2_get_le64u
00120 # define bytestream2_put_ne16 bytestream2_put_le16
00121 # define bytestream2_put_ne24 bytestream2_put_le24
00122 # define bytestream2_put_ne32 bytestream2_put_le32
00123 # define bytestream2_put_ne64 bytestream2_put_le64
00124 # define bytestream2_peek_ne16 bytestream2_peek_le16
00125 # define bytestream2_peek_ne24 bytestream2_peek_le24
00126 # define bytestream2_peek_ne32 bytestream2_peek_le32
00127 # define bytestream2_peek_ne64 bytestream2_peek_le64
00128 #endif
00129
00130 static av_always_inline void bytestream2_init(GetByteContext *g,
00131 const uint8_t *buf,
00132 int buf_size)
00133 {
00134 g->buffer = buf;
00135 g->buffer_start = buf;
00136 g->buffer_end = buf + buf_size;
00137 }
00138
00139 static av_always_inline void bytestream2_init_writer(PutByteContext *p,
00140 uint8_t *buf,
00141 int buf_size)
00142 {
00143 p->buffer = buf;
00144 p->buffer_start = buf;
00145 p->buffer_end = buf + buf_size;
00146 p->eof = 0;
00147 }
00148
00149 static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
00150 {
00151 return g->buffer_end - g->buffer;
00152 }
00153
00154 static av_always_inline unsigned int bytestream2_get_bytes_left_p(PutByteContext *p)
00155 {
00156 return p->buffer_end - p->buffer;
00157 }
00158
00159 static av_always_inline void bytestream2_skip(GetByteContext *g,
00160 unsigned int size)
00161 {
00162 g->buffer += FFMIN(g->buffer_end - g->buffer, size);
00163 }
00164
00165 static av_always_inline void bytestream2_skipu(GetByteContext *g,
00166 unsigned int size)
00167 {
00168 g->buffer += size;
00169 }
00170
00171 static av_always_inline void bytestream2_skip_p(PutByteContext *p,
00172 unsigned int size)
00173 {
00174 int size2;
00175 if (p->eof)
00176 return;
00177 size2 = FFMIN(p->buffer_end - p->buffer, size);
00178 if (size2 != size)
00179 p->eof = 1;
00180 p->buffer += size2;
00181 }
00182
00183 static av_always_inline int bytestream2_tell(GetByteContext *g)
00184 {
00185 return (int)(g->buffer - g->buffer_start);
00186 }
00187
00188 static av_always_inline int bytestream2_tell_p(PutByteContext *p)
00189 {
00190 return (int)(p->buffer - p->buffer_start);
00191 }
00192
00193 static av_always_inline int bytestream2_size(GetByteContext *g)
00194 {
00195 return (int)(g->buffer_end - g->buffer_start);
00196 }
00197
00198 static av_always_inline int bytestream2_size_p(PutByteContext *p)
00199 {
00200 return (int)(p->buffer_end - p->buffer_start);
00201 }
00202
00203 static av_always_inline int bytestream2_seek(GetByteContext *g,
00204 int offset,
00205 int whence)
00206 {
00207 switch (whence) {
00208 case SEEK_CUR:
00209 offset = av_clip(offset, -(g->buffer - g->buffer_start),
00210 g->buffer_end - g->buffer);
00211 g->buffer += offset;
00212 break;
00213 case SEEK_END:
00214 offset = av_clip(offset, -(g->buffer_end - g->buffer_start), 0);
00215 g->buffer = g->buffer_end + offset;
00216 break;
00217 case SEEK_SET:
00218 offset = av_clip(offset, 0, g->buffer_end - g->buffer_start);
00219 g->buffer = g->buffer_start + offset;
00220 break;
00221 default:
00222 return AVERROR(EINVAL);
00223 }
00224 return bytestream2_tell(g);
00225 }
00226
00227 static av_always_inline int bytestream2_seek_p(PutByteContext *p,
00228 int offset,
00229 int whence)
00230 {
00231 p->eof = 0;
00232 switch (whence) {
00233 case SEEK_CUR:
00234 if (p->buffer_end - p->buffer < offset)
00235 p->eof = 1;
00236 offset = av_clip(offset, -(p->buffer - p->buffer_start),
00237 p->buffer_end - p->buffer);
00238 p->buffer += offset;
00239 break;
00240 case SEEK_END:
00241 if (offset > 0)
00242 p->eof = 1;
00243 offset = av_clip(offset, -(p->buffer_end - p->buffer_start), 0);
00244 p->buffer = p->buffer_end + offset;
00245 break;
00246 case SEEK_SET:
00247 if (p->buffer_end - p->buffer_start < offset)
00248 p->eof = 1;
00249 offset = av_clip(offset, 0, p->buffer_end - p->buffer_start);
00250 p->buffer = p->buffer_start + offset;
00251 break;
00252 default:
00253 return AVERROR(EINVAL);
00254 }
00255 return bytestream2_tell_p(p);
00256 }
00257
00258 static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g,
00259 uint8_t *dst,
00260 unsigned int size)
00261 {
00262 int size2 = FFMIN(g->buffer_end - g->buffer, size);
00263 memcpy(dst, g->buffer, size2);
00264 g->buffer += size2;
00265 return size2;
00266 }
00267
00268 static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g,
00269 uint8_t *dst,
00270 unsigned int size)
00271 {
00272 memcpy(dst, g->buffer, size);
00273 g->buffer += size;
00274 return size;
00275 }
00276
00277 static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p,
00278 const uint8_t *src,
00279 unsigned int size)
00280 {
00281 int size2;
00282 if (p->eof)
00283 return 0;
00284 size2 = FFMIN(p->buffer_end - p->buffer, size);
00285 if (size2 != size)
00286 p->eof = 1;
00287 memcpy(p->buffer, src, size2);
00288 p->buffer += size2;
00289 return size2;
00290 }
00291
00292 static av_always_inline unsigned int bytestream2_put_bufferu(PutByteContext *p,
00293 const uint8_t *src,
00294 unsigned int size)
00295 {
00296 memcpy(p->buffer, src, size);
00297 p->buffer += size;
00298 return size;
00299 }
00300
00301 static av_always_inline void bytestream2_set_buffer(PutByteContext *p,
00302 const uint8_t c,
00303 unsigned int size)
00304 {
00305 int size2;
00306 if (p->eof)
00307 return;
00308 size2 = FFMIN(p->buffer_end - p->buffer, size);
00309 if (size2 != size)
00310 p->eof = 1;
00311 memset(p->buffer, c, size2);
00312 p->buffer += size2;
00313 }
00314
00315 static av_always_inline void bytestream2_set_bufferu(PutByteContext *p,
00316 const uint8_t c,
00317 unsigned int size)
00318 {
00319 memset(p->buffer, c, size);
00320 p->buffer += size;
00321 }
00322
00323 static av_always_inline unsigned int bytestream2_get_eof(PutByteContext *p)
00324 {
00325 return p->eof;
00326 }
00327
00328 static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b,
00329 uint8_t *dst,
00330 unsigned int size)
00331 {
00332 memcpy(dst, *b, size);
00333 (*b) += size;
00334 return size;
00335 }
00336
00337 static av_always_inline void bytestream_put_buffer(uint8_t **b,
00338 const uint8_t *src,
00339 unsigned int size)
00340 {
00341 memcpy(*b, src, size);
00342 (*b) += size;
00343 }
00344
00345 #endif