bytestream.h
Go to the documentation of this file.
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 
23 #ifndef AVCODEC_BYTESTREAM_H
24 #define AVCODEC_BYTESTREAM_H
25 
26 #include <stdint.h>
27 #include <string.h>
28 
29 #include "libavutil/common.h"
30 #include "libavutil/intreadwrite.h"
31 
32 typedef struct GetByteContext {
35 
36 typedef struct PutByteContext {
38  int eof;
40 
41 #define DEF(type, name, bytes, read, write) \
42 static av_always_inline type bytestream_get_ ## name(const uint8_t **b) \
43 { \
44  (*b) += bytes; \
45  return read(*b - bytes); \
46 } \
47 static av_always_inline void bytestream_put_ ## name(uint8_t **b, \
48  const type value) \
49 { \
50  write(*b, value); \
51  (*b) += bytes; \
52 } \
53 static av_always_inline void bytestream2_put_ ## name ## u(PutByteContext *p, \
54  const type value) \
55 { \
56  bytestream_put_ ## name(&p->buffer, value); \
57 } \
58 static av_always_inline void bytestream2_put_ ## name(PutByteContext *p, \
59  const type value) \
60 { \
61  if (!p->eof && (p->buffer_end - p->buffer >= bytes)) { \
62  write(p->buffer, value); \
63  p->buffer += bytes; \
64  } else \
65  p->eof = 1; \
66 } \
67 static av_always_inline type bytestream2_get_ ## name ## u(GetByteContext *g) \
68 { \
69  return bytestream_get_ ## name(&g->buffer); \
70 } \
71 static av_always_inline type bytestream2_get_ ## name(GetByteContext *g) \
72 { \
73  if (g->buffer_end - g->buffer < bytes) \
74  return 0; \
75  return bytestream2_get_ ## name ## u(g); \
76 } \
77 static av_always_inline type bytestream2_peek_ ## name(GetByteContext *g) \
78 { \
79  if (g->buffer_end - g->buffer < bytes) \
80  return 0; \
81  return read(g->buffer); \
82 }
83 
84 DEF(uint64_t, le64, 8, AV_RL64, AV_WL64)
85 DEF(unsigned int, le32, 4, AV_RL32, AV_WL32)
86 DEF(unsigned int, le24, 3, AV_RL24, AV_WL24)
87 DEF(unsigned int, le16, 2, AV_RL16, AV_WL16)
88 DEF(uint64_t, be64, 8, AV_RB64, AV_WB64)
89 DEF(unsigned int, be32, 4, AV_RB32, AV_WB32)
90 DEF(unsigned int, be24, 3, AV_RB24, AV_WB24)
91 DEF(unsigned int, be16, 2, AV_RB16, AV_WB16)
92 DEF(unsigned int, byte, 1, AV_RB8 , AV_WB8)
93 
94 #if HAVE_BIGENDIAN
95 # define bytestream2_get_ne16 bytestream2_get_be16
96 # define bytestream2_get_ne24 bytestream2_get_be24
97 # define bytestream2_get_ne32 bytestream2_get_be32
98 # define bytestream2_get_ne64 bytestream2_get_be64
99 # define bytestream2_get_ne16u bytestream2_get_be16u
100 # define bytestream2_get_ne24u bytestream2_get_be24u
101 # define bytestream2_get_ne32u bytestream2_get_be32u
102 # define bytestream2_get_ne64u bytestream2_get_be64u
103 # define bytestream2_put_ne16 bytestream2_put_be16
104 # define bytestream2_put_ne24 bytestream2_put_be24
105 # define bytestream2_put_ne32 bytestream2_put_be32
106 # define bytestream2_put_ne64 bytestream2_put_be64
107 # define bytestream2_peek_ne16 bytestream2_peek_be16
108 # define bytestream2_peek_ne24 bytestream2_peek_be24
109 # define bytestream2_peek_ne32 bytestream2_peek_be32
110 # define bytestream2_peek_ne64 bytestream2_peek_be64
111 #else
112 # define bytestream2_get_ne16 bytestream2_get_le16
113 # define bytestream2_get_ne24 bytestream2_get_le24
114 # define bytestream2_get_ne32 bytestream2_get_le32
115 # define bytestream2_get_ne64 bytestream2_get_le64
116 # define bytestream2_get_ne16u bytestream2_get_le16u
117 # define bytestream2_get_ne24u bytestream2_get_le24u
118 # define bytestream2_get_ne32u bytestream2_get_le32u
119 # define bytestream2_get_ne64u bytestream2_get_le64u
120 # define bytestream2_put_ne16 bytestream2_put_le16
121 # define bytestream2_put_ne24 bytestream2_put_le24
122 # define bytestream2_put_ne32 bytestream2_put_le32
123 # define bytestream2_put_ne64 bytestream2_put_le64
124 # define bytestream2_peek_ne16 bytestream2_peek_le16
125 # define bytestream2_peek_ne24 bytestream2_peek_le24
126 # define bytestream2_peek_ne32 bytestream2_peek_le32
127 # define bytestream2_peek_ne64 bytestream2_peek_le64
128 #endif
129 
131  const uint8_t *buf,
132  int buf_size)
133 {
134  g->buffer = buf;
135  g->buffer_start = buf;
136  g->buffer_end = buf + buf_size;
137 }
138 
140  uint8_t *buf,
141  int buf_size)
142 {
143  p->buffer = buf;
144  p->buffer_start = buf;
145  p->buffer_end = buf + buf_size;
146  p->eof = 0;
147 }
148 
150 {
151  return g->buffer_end - g->buffer;
152 }
153 
155 {
156  return p->buffer_end - p->buffer;
157 }
158 
160  unsigned int size)
161 {
162  g->buffer += FFMIN(g->buffer_end - g->buffer, size);
163 }
164 
166  unsigned int size)
167 {
168  g->buffer += size;
169 }
170 
172  unsigned int size)
173 {
174  int size2;
175  if (p->eof)
176  return;
177  size2 = FFMIN(p->buffer_end - p->buffer, size);
178  if (size2 != size)
179  p->eof = 1;
180  p->buffer += size2;
181 }
182 
184 {
185  return (int)(g->buffer - g->buffer_start);
186 }
187 
189 {
190  return (int)(p->buffer - p->buffer_start);
191 }
192 
194 {
195  return (int)(g->buffer_end - g->buffer_start);
196 }
197 
199 {
200  return (int)(p->buffer_end - p->buffer_start);
201 }
202 
204  int offset,
205  int whence)
206 {
207  switch (whence) {
208  case SEEK_CUR:
209  offset = av_clip(offset, -(g->buffer - g->buffer_start),
210  g->buffer_end - g->buffer);
211  g->buffer += offset;
212  break;
213  case SEEK_END:
214  offset = av_clip(offset, -(g->buffer_end - g->buffer_start), 0);
215  g->buffer = g->buffer_end + offset;
216  break;
217  case SEEK_SET:
218  offset = av_clip(offset, 0, g->buffer_end - g->buffer_start);
219  g->buffer = g->buffer_start + offset;
220  break;
221  default:
222  return AVERROR(EINVAL);
223  }
224  return bytestream2_tell(g);
225 }
226 
228  int offset,
229  int whence)
230 {
231  p->eof = 0;
232  switch (whence) {
233  case SEEK_CUR:
234  if (p->buffer_end - p->buffer < offset)
235  p->eof = 1;
236  offset = av_clip(offset, -(p->buffer - p->buffer_start),
237  p->buffer_end - p->buffer);
238  p->buffer += offset;
239  break;
240  case SEEK_END:
241  if (offset > 0)
242  p->eof = 1;
243  offset = av_clip(offset, -(p->buffer_end - p->buffer_start), 0);
244  p->buffer = p->buffer_end + offset;
245  break;
246  case SEEK_SET:
247  if (p->buffer_end - p->buffer_start < offset)
248  p->eof = 1;
249  offset = av_clip(offset, 0, p->buffer_end - p->buffer_start);
250  p->buffer = p->buffer_start + offset;
251  break;
252  default:
253  return AVERROR(EINVAL);
254  }
255  return bytestream2_tell_p(p);
256 }
257 
259  uint8_t *dst,
260  unsigned int size)
261 {
262  int size2 = FFMIN(g->buffer_end - g->buffer, size);
263  memcpy(dst, g->buffer, size2);
264  g->buffer += size2;
265  return size2;
266 }
267 
269  uint8_t *dst,
270  unsigned int size)
271 {
272  memcpy(dst, g->buffer, size);
273  g->buffer += size;
274  return size;
275 }
276 
278  const uint8_t *src,
279  unsigned int size)
280 {
281  int size2;
282  if (p->eof)
283  return 0;
284  size2 = FFMIN(p->buffer_end - p->buffer, size);
285  if (size2 != size)
286  p->eof = 1;
287  memcpy(p->buffer, src, size2);
288  p->buffer += size2;
289  return size2;
290 }
291 
293  const uint8_t *src,
294  unsigned int size)
295 {
296  memcpy(p->buffer, src, size);
297  p->buffer += size;
298  return size;
299 }
300 
302  const uint8_t c,
303  unsigned int size)
304 {
305  int size2;
306  if (p->eof)
307  return;
308  size2 = FFMIN(p->buffer_end - p->buffer, size);
309  if (size2 != size)
310  p->eof = 1;
311  memset(p->buffer, c, size2);
312  p->buffer += size2;
313 }
314 
316  const uint8_t c,
317  unsigned int size)
318 {
319  memset(p->buffer, c, size);
320  p->buffer += size;
321 }
322 
324 {
325  return p->eof;
326 }
327 
328 static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b,
329  uint8_t *dst,
330  unsigned int size)
331 {
332  memcpy(dst, *b, size);
333  (*b) += size;
334  return size;
335 }
336 
338  const uint8_t *src,
339  unsigned int size)
340 {
341  memcpy(*b, src, size);
342  (*b) += size;
343 }
344 
345 #endif /* AVCODEC_BYTESTREAM_H */
#define AV_RB8(x)
Definition: intreadwrite.h:387
static av_always_inline void bytestream2_set_bufferu(PutByteContext *p, const uint8_t c, unsigned int size)
Definition: bytestream.h:315
static av_always_inline void bytestream2_set_buffer(PutByteContext *p, const uint8_t c, unsigned int size)
Definition: bytestream.h:301
#define AV_RB64
static av_always_inline void bytestream2_init_writer(PutByteContext *p, uint8_t *buf, int buf_size)
Definition: bytestream.h:139
#define AV_WB8(p, d)
Definition: intreadwrite.h:388
#define AV_RB24
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
#define AV_RL16
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:268
#define AV_WB32(p, darg)
Definition: intreadwrite.h:265
#define AV_RL64
#define AV_WL32(p, darg)
Definition: intreadwrite.h:282
#define AV_WL24(p, d)
Definition: intreadwrite.h:456
uint8_t
#define DEF(type, name, bytes, read, write)
Definition: bytestream.h:41
#define AV_RB32
#define b
Definition: input.c:42
struct GetByteContext GetByteContext
const uint8_t * buffer
Definition: bytestream.h:33
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:165
static av_always_inline int bytestream2_size(GetByteContext *g)
Definition: bytestream.h:193
static av_always_inline unsigned int bytestream2_get_bytes_left_p(PutByteContext *p)
Definition: bytestream.h:154
#define AV_RB16
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:159
#define AV_WL16(p, darg)
Definition: intreadwrite.h:250
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:258
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
static const uint8_t offset[127][2]
Definition: vf_spp.c:70
static av_always_inline int bytestream2_tell_p(PutByteContext *p)
Definition: bytestream.h:188
int size
const uint8_t * buffer_end
Definition: bytestream.h:33
static av_always_inline void bytestream2_skip_p(PutByteContext *p, unsigned int size)
Definition: bytestream.h:171
FFT buffer for g
Definition: stft_peak.m:17
uint8_t * buffer_start
Definition: bytestream.h:37
#define FFMIN(a, b)
Definition: common.h:58
#define AV_WB24(p, d)
Definition: intreadwrite.h:442
#define AV_RL32
static av_always_inline unsigned int bytestream_get_buffer(const uint8_t **b, uint8_t *dst, unsigned int size)
Definition: bytestream.h:328
static av_always_inline unsigned int bytestream2_put_buffer(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition: bytestream.h:277
static av_always_inline int bytestream2_seek_p(PutByteContext *p, int offset, int whence)
Definition: bytestream.h:227
static av_always_inline int bytestream2_tell(GetByteContext *g)
Definition: bytestream.h:183
uint8_t * buffer
Definition: bytestream.h:37
AVS_Value src
Definition: avisynth_c.h:523
static av_always_inline unsigned int bytestream2_put_bufferu(PutByteContext *p, const uint8_t *src, unsigned int size)
Definition: bytestream.h:292
void * buf
Definition: avisynth_c.h:594
const uint8_t * buffer_start
Definition: bytestream.h:33
#define AV_WB16(p, darg)
Definition: intreadwrite.h:237
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
#define AV_WB64(p, darg)
Definition: intreadwrite.h:303
#define AV_RL24
#define AV_WL64(p, darg)
Definition: intreadwrite.h:328
common internal and external API header
static av_always_inline unsigned int bytestream2_get_eof(PutByteContext *p)
Definition: bytestream.h:323
static double c[64]
struct PutByteContext PutByteContext
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:337
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
static av_always_inline int bytestream2_seek(GetByteContext *g, int offset, int whence)
Definition: bytestream.h:203
#define av_always_inline
Definition: attributes.h:41
static av_always_inline int bytestream2_size_p(PutByteContext *p)
Definition: bytestream.h:198
uint8_t * buffer_end
Definition: bytestream.h:37