bytestream.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Bytestream functions
  3. * copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@free.fr>
  4. * Copyright (c) 2012 Aneesh Dogra (lionaneesh) <lionaneesh@gmail.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVCODEC_BYTESTREAM_H
  23. #define AVCODEC_BYTESTREAM_H
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include "libavutil/avassert.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/intreadwrite.h"
  29. typedef struct GetByteContext {
  30. const uint8_t *buffer, *buffer_end, *buffer_start;
  31. } GetByteContext;
  32. typedef struct PutByteContext {
  33. uint8_t *buffer, *buffer_end, *buffer_start;
  34. int eof;
  35. } PutByteContext;
  36. #define DEF(type, name, bytes, read, write) \
  37. static av_always_inline type bytestream_get_ ## name(const uint8_t **b) \
  38. { \
  39. (*b) += bytes; \
  40. return read(*b - bytes); \
  41. } \
  42. static av_always_inline void bytestream_put_ ## name(uint8_t **b, \
  43. const type value) \
  44. { \
  45. write(*b, value); \
  46. (*b) += bytes; \
  47. } \
  48. static av_always_inline void bytestream2_put_ ## name ## u(PutByteContext *p, \
  49. const type value) \
  50. { \
  51. bytestream_put_ ## name(&p->buffer, value); \
  52. } \
  53. static av_always_inline void bytestream2_put_ ## name(PutByteContext *p, \
  54. const type value) \
  55. { \
  56. if (!p->eof && (p->buffer_end - p->buffer >= bytes)) { \
  57. write(p->buffer, value); \
  58. p->buffer += bytes; \
  59. } else \
  60. p->eof = 1; \
  61. } \
  62. static av_always_inline type bytestream2_get_ ## name ## u(GetByteContext *g) \
  63. { \
  64. return bytestream_get_ ## name(&g->buffer); \
  65. } \
  66. static av_always_inline type bytestream2_get_ ## name(GetByteContext *g) \
  67. { \
  68. if (g->buffer_end - g->buffer < bytes) { \
  69. g->buffer = g->buffer_end; \
  70. return 0; \
  71. } \
  72. return bytestream2_get_ ## name ## u(g); \
  73. } \
  74. static av_always_inline type bytestream2_peek_ ## name(GetByteContext *g) \
  75. { \
  76. if (g->buffer_end - g->buffer < bytes) \
  77. return 0; \
  78. return read(g->buffer); \
  79. }
  80. DEF(uint64_t, le64, 8, AV_RL64, AV_WL64)
  81. DEF(unsigned int, le32, 4, AV_RL32, AV_WL32)
  82. DEF(unsigned int, le24, 3, AV_RL24, AV_WL24)
  83. DEF(unsigned int, le16, 2, AV_RL16, AV_WL16)
  84. DEF(uint64_t, be64, 8, AV_RB64, AV_WB64)
  85. DEF(unsigned int, be32, 4, AV_RB32, AV_WB32)
  86. DEF(unsigned int, be24, 3, AV_RB24, AV_WB24)
  87. DEF(unsigned int, be16, 2, AV_RB16, AV_WB16)
  88. DEF(unsigned int, byte, 1, AV_RB8 , AV_WB8)
  89. #if AV_HAVE_BIGENDIAN
  90. # define bytestream2_get_ne16 bytestream2_get_be16
  91. # define bytestream2_get_ne24 bytestream2_get_be24
  92. # define bytestream2_get_ne32 bytestream2_get_be32
  93. # define bytestream2_get_ne64 bytestream2_get_be64
  94. # define bytestream2_get_ne16u bytestream2_get_be16u
  95. # define bytestream2_get_ne24u bytestream2_get_be24u
  96. # define bytestream2_get_ne32u bytestream2_get_be32u
  97. # define bytestream2_get_ne64u bytestream2_get_be64u
  98. # define bytestream2_put_ne16 bytestream2_put_be16
  99. # define bytestream2_put_ne24 bytestream2_put_be24
  100. # define bytestream2_put_ne32 bytestream2_put_be32
  101. # define bytestream2_put_ne64 bytestream2_put_be64
  102. # define bytestream2_peek_ne16 bytestream2_peek_be16
  103. # define bytestream2_peek_ne24 bytestream2_peek_be24
  104. # define bytestream2_peek_ne32 bytestream2_peek_be32
  105. # define bytestream2_peek_ne64 bytestream2_peek_be64
  106. #else
  107. # define bytestream2_get_ne16 bytestream2_get_le16
  108. # define bytestream2_get_ne24 bytestream2_get_le24
  109. # define bytestream2_get_ne32 bytestream2_get_le32
  110. # define bytestream2_get_ne64 bytestream2_get_le64
  111. # define bytestream2_get_ne16u bytestream2_get_le16u
  112. # define bytestream2_get_ne24u bytestream2_get_le24u
  113. # define bytestream2_get_ne32u bytestream2_get_le32u
  114. # define bytestream2_get_ne64u bytestream2_get_le64u
  115. # define bytestream2_put_ne16 bytestream2_put_le16
  116. # define bytestream2_put_ne24 bytestream2_put_le24
  117. # define bytestream2_put_ne32 bytestream2_put_le32
  118. # define bytestream2_put_ne64 bytestream2_put_le64
  119. # define bytestream2_peek_ne16 bytestream2_peek_le16
  120. # define bytestream2_peek_ne24 bytestream2_peek_le24
  121. # define bytestream2_peek_ne32 bytestream2_peek_le32
  122. # define bytestream2_peek_ne64 bytestream2_peek_le64
  123. #endif
  124. static av_always_inline void bytestream2_init(GetByteContext *g,
  125. const uint8_t *buf,
  126. int buf_size)
  127. {
  128. av_assert0(buf_size >= 0);
  129. g->buffer = buf;
  130. g->buffer_start = buf;
  131. g->buffer_end = buf + buf_size;
  132. }
  133. static av_always_inline void bytestream2_init_writer(PutByteContext *p,
  134. uint8_t *buf,
  135. int buf_size)
  136. {
  137. av_assert0(buf_size >= 0);
  138. p->buffer = buf;
  139. p->buffer_start = buf;
  140. p->buffer_end = buf + buf_size;
  141. p->eof = 0;
  142. }
  143. static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
  144. {
  145. return g->buffer_end - g->buffer;
  146. }
  147. static av_always_inline int bytestream2_get_bytes_left_p(PutByteContext *p)
  148. {
  149. return p->buffer_end - p->buffer;
  150. }
  151. static av_always_inline void bytestream2_skip(GetByteContext *g,
  152. unsigned int size)
  153. {
  154. g->buffer += FFMIN(g->buffer_end - g->buffer, size);
  155. }
  156. static av_always_inline void bytestream2_skipu(GetByteContext *g,
  157. unsigned int size)
  158. {
  159. g->buffer += size;
  160. }
  161. static av_always_inline void bytestream2_skip_p(PutByteContext *p,
  162. unsigned int size)
  163. {
  164. int size2;
  165. if (p->eof)
  166. return;
  167. size2 = FFMIN(p->buffer_end - p->buffer, size);
  168. if (size2 != size)
  169. p->eof = 1;
  170. p->buffer += size2;
  171. }
  172. static av_always_inline int bytestream2_tell(GetByteContext *g)
  173. {
  174. return (int)(g->buffer - g->buffer_start);
  175. }
  176. static av_always_inline int bytestream2_tell_p(PutByteContext *p)
  177. {
  178. return (int)(p->buffer - p->buffer_start);
  179. }
  180. static av_always_inline int bytestream2_size(GetByteContext *g)
  181. {
  182. return (int)(g->buffer_end - g->buffer_start);
  183. }
  184. static av_always_inline int bytestream2_size_p(PutByteContext *p)
  185. {
  186. return (int)(p->buffer_end - p->buffer_start);
  187. }
  188. static av_always_inline int bytestream2_seek(GetByteContext *g,
  189. int offset,
  190. int whence)
  191. {
  192. switch (whence) {
  193. case SEEK_CUR:
  194. offset = av_clip(offset, -(g->buffer - g->buffer_start),
  195. g->buffer_end - g->buffer);
  196. g->buffer += offset;
  197. break;
  198. case SEEK_END:
  199. offset = av_clip(offset, -(g->buffer_end - g->buffer_start), 0);
  200. g->buffer = g->buffer_end + offset;
  201. break;
  202. case SEEK_SET:
  203. offset = av_clip(offset, 0, g->buffer_end - g->buffer_start);
  204. g->buffer = g->buffer_start + offset;
  205. break;
  206. default:
  207. return AVERROR(EINVAL);
  208. }
  209. return bytestream2_tell(g);
  210. }
  211. static av_always_inline int bytestream2_seek_p(PutByteContext *p,
  212. int offset,
  213. int whence)
  214. {
  215. p->eof = 0;
  216. switch (whence) {
  217. case SEEK_CUR:
  218. if (p->buffer_end - p->buffer < offset)
  219. p->eof = 1;
  220. offset = av_clip(offset, -(p->buffer - p->buffer_start),
  221. p->buffer_end - p->buffer);
  222. p->buffer += offset;
  223. break;
  224. case SEEK_END:
  225. if (offset > 0)
  226. p->eof = 1;
  227. offset = av_clip(offset, -(p->buffer_end - p->buffer_start), 0);
  228. p->buffer = p->buffer_end + offset;
  229. break;
  230. case SEEK_SET:
  231. if (p->buffer_end - p->buffer_start < offset)
  232. p->eof = 1;
  233. offset = av_clip(offset, 0, p->buffer_end - p->buffer_start);
  234. p->buffer = p->buffer_start + offset;
  235. break;
  236. default:
  237. return AVERROR(EINVAL);
  238. }
  239. return bytestream2_tell_p(p);
  240. }
  241. static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g,
  242. uint8_t *dst,
  243. unsigned int size)
  244. {
  245. int size2 = FFMIN(g->buffer_end - g->buffer, size);
  246. memcpy(dst, g->buffer, size2);
  247. g->buffer += size2;
  248. return size2;
  249. }
  250. static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g,
  251. uint8_t *dst,
  252. unsigned int size)
  253. {
  254. memcpy(dst, g->buffer, size);
  255. g->buffer += size;
  256. return size;
  257. }
  258. static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p,
  259. const uint8_t *src,
  260. unsigned int size)
  261. {
  262. int size2;
  263. if (p->eof)
  264. return 0;
  265. size2 = FFMIN(p->buffer_end - p->buffer, size);
  266. if (size2 != size)
  267. p->eof = 1;
  268. memcpy(p->buffer, src, size2);
  269. p->buffer += size2;
  270. return size2;
  271. }
  272. static av_always_inline unsigned int bytestream2_put_bufferu(PutByteContext *p,
  273. const uint8_t *src,
  274. unsigned int size)
  275. {
  276. memcpy(p->buffer, src, size);
  277. p->buffer += size;
  278. return size;
  279. }
  280. static av_always_inline void bytestream2_set_buffer(PutByteContext *p,
  281. const uint8_t c,
  282. unsigned int size)
  283. {
  284. int size2;
  285. if (p->eof)
  286. return;
  287. size2 = FFMIN(p->buffer_end - p->buffer, size);
  288. if (size2 != size)
  289. p->eof = 1;
  290. memset(p->buffer, c, size2);
  291. p->buffer += size2;
  292. }
  293. static av_always_inline void bytestream2_set_bufferu(PutByteContext *p,
  294. const uint8_t c,
  295. unsigned int size)
  296. {
  297. memset(p->buffer, c, size);
  298. p->buffer += size;
  299. }
  300. static av_always_inline unsigned int bytestream2_get_eof(PutByteContext *p)
  301. {
  302. return p->eof;
  303. }
  304. static av_always_inline unsigned int bytestream2_copy_bufferu(PutByteContext *p,
  305. GetByteContext *g,
  306. unsigned int size)
  307. {
  308. memcpy(p->buffer, g->buffer, size);
  309. p->buffer += size;
  310. g->buffer += size;
  311. return size;
  312. }
  313. static av_always_inline unsigned int bytestream2_copy_buffer(PutByteContext *p,
  314. GetByteContext *g,
  315. unsigned int size)
  316. {
  317. int size2;
  318. if (p->eof)
  319. return 0;
  320. size = FFMIN(g->buffer_end - g->buffer, size);
  321. size2 = FFMIN(p->buffer_end - p->buffer, size);
  322. if (size2 != size)
  323. p->eof = 1;
  324. return bytestream2_copy_bufferu(p, g, size2);
  325. }
  326. static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b,
  327. uint8_t *dst,
  328. unsigned int size)
  329. {
  330. memcpy(dst, *b, size);
  331. (*b) += size;
  332. return size;
  333. }
  334. static av_always_inline void bytestream_put_buffer(uint8_t **b,
  335. const uint8_t *src,
  336. unsigned int size)
  337. {
  338. memcpy(*b, src, size);
  339. (*b) += size;
  340. }
  341. #endif /* AVCODEC_BYTESTREAM_H */