00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef AVCODEC_BYTESTREAM_H
00023 #define AVCODEC_BYTESTREAM_H
00024
00025 #include <string.h>
00026 #include "libavutil/common.h"
00027 #include "libavutil/intreadwrite.h"
00028
00029 typedef struct {
00030 const uint8_t *buffer, *buffer_end;
00031 } GetByteContext;
00032
00033 #define DEF_T(type, name, bytes, read, write) \
00034 static av_always_inline type bytestream_get_ ## name(const uint8_t **b){\
00035 (*b) += bytes;\
00036 return read(*b - bytes);\
00037 }\
00038 static av_always_inline void bytestream_put_ ##name(uint8_t **b, const type value){\
00039 write(*b, value);\
00040 (*b) += bytes;\
00041 }\
00042 static av_always_inline type bytestream2_get_ ## name(GetByteContext *g)\
00043 {\
00044 if (g->buffer_end - g->buffer < bytes)\
00045 return 0;\
00046 return bytestream_get_ ## name(&g->buffer);\
00047 }\
00048 static av_always_inline type bytestream2_peek_ ## name(GetByteContext *g)\
00049 {\
00050 if (g->buffer_end - g->buffer < bytes)\
00051 return 0;\
00052 return read(g->buffer);\
00053 }
00054
00055 #define DEF(name, bytes, read, write) \
00056 DEF_T(unsigned int, name, bytes, read, write)
00057 #define DEF64(name, bytes, read, write) \
00058 DEF_T(uint64_t, name, bytes, read, write)
00059
00060 DEF64(le64, 8, AV_RL64, AV_WL64)
00061 DEF (le32, 4, AV_RL32, AV_WL32)
00062 DEF (le24, 3, AV_RL24, AV_WL24)
00063 DEF (le16, 2, AV_RL16, AV_WL16)
00064 DEF64(be64, 8, AV_RB64, AV_WB64)
00065 DEF (be32, 4, AV_RB32, AV_WB32)
00066 DEF (be24, 3, AV_RB24, AV_WB24)
00067 DEF (be16, 2, AV_RB16, AV_WB16)
00068 DEF (byte, 1, AV_RB8 , AV_WB8 )
00069
00070 #undef DEF
00071 #undef DEF64
00072 #undef DEF_T
00073
00074 static av_always_inline void bytestream2_init(GetByteContext *g,
00075 const uint8_t *buf, int buf_size)
00076 {
00077 g->buffer = buf;
00078 g->buffer_end = buf + buf_size;
00079 }
00080
00081 static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
00082 {
00083 return g->buffer_end - g->buffer;
00084 }
00085
00086 static av_always_inline void bytestream2_skip(GetByteContext *g,
00087 unsigned int size)
00088 {
00089 g->buffer += FFMIN(g->buffer_end - g->buffer, size);
00090 }
00091
00092 static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g,
00093 uint8_t *dst,
00094 unsigned int size)
00095 {
00096 int size2 = FFMIN(g->buffer_end - g->buffer, size);
00097 memcpy(dst, g->buffer, size2);
00098 g->buffer += size2;
00099 return size2;
00100 }
00101
00102 static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
00103 {
00104 memcpy(dst, *b, size);
00105 (*b) += size;
00106 return size;
00107 }
00108
00109 static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
00110 {
00111 memcpy(*b, src, size);
00112 (*b) += size;
00113 }
00114
00115 #endif