Statistics
| Branch: | Revision:

ffmpeg / libavutil / intreadwrite.h @ 80a07f6e

History | View | Annotate | Download (12.7 KB)

1 f5a90186 Diego Biurrun
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18
19 98790382 Stefano Sabatini
#ifndef AVUTIL_INTREADWRITE_H
20
#define AVUTIL_INTREADWRITE_H
21 cf1e119b Reimar Döffinger
22 99545457 Måns Rullgård
#include <stdint.h>
23 a087028a Diego Biurrun
#include "config.h"
24 c08be350 Reimar Döffinger
#include "bswap.h"
25 99545457 Måns Rullgård
26 a6783b89 Måns Rullgård
/*
27
 * Arch-specific headers can provide any combination of
28 f6d03906 Alexander Strange
 * AV_[RW][BLN](16|24|32|64) and AV_(COPY|SWAP|ZERO)(64|128) macros.
29
 * Preprocessor symbols must be defined, even if these are implemented
30
 * as inline functions.
31 a6783b89 Måns Rullgård
 */
32
33 3c55ce03 Måns Rullgård
#if   ARCH_ARM
34
#   include "arm/intreadwrite.h"
35 d691da95 Måns Rullgård
#elif ARCH_AVR32
36
#   include "avr32/intreadwrite.h"
37 530456bf Måns Rullgård
#elif ARCH_MIPS
38
#   include "mips/intreadwrite.h"
39 9f5ff83f Måns Rullgård
#elif ARCH_PPC
40
#   include "ppc/intreadwrite.h"
41 f6d03906 Alexander Strange
#elif ARCH_X86
42
#   include "x86/intreadwrite.h"
43 3c55ce03 Måns Rullgård
#endif
44 a6783b89 Måns Rullgård
45
/*
46 63826ceb Måns Rullgård
 * Map AV_RNXX <-> AV_R[BL]XX for all variants provided by per-arch headers.
47
 */
48
49
#if HAVE_BIGENDIAN
50
51
#   if    defined(AV_RN16) && !defined(AV_RB16)
52
#       define AV_RB16(p) AV_RN16(p)
53
#   elif !defined(AV_RN16) &&  defined(AV_RB16)
54
#       define AV_RN16(p) AV_RB16(p)
55
#   endif
56
57
#   if    defined(AV_WN16) && !defined(AV_WB16)
58
#       define AV_WB16(p, v) AV_WN16(p, v)
59
#   elif !defined(AV_WN16) &&  defined(AV_WB16)
60
#       define AV_WN16(p, v) AV_WB16(p, v)
61
#   endif
62
63
#   if    defined(AV_RN24) && !defined(AV_RB24)
64
#       define AV_RB24(p) AV_RN24(p)
65
#   elif !defined(AV_RN24) &&  defined(AV_RB24)
66
#       define AV_RN24(p) AV_RB24(p)
67
#   endif
68
69
#   if    defined(AV_WN24) && !defined(AV_WB24)
70
#       define AV_WB24(p, v) AV_WN24(p, v)
71
#   elif !defined(AV_WN24) &&  defined(AV_WB24)
72
#       define AV_WN24(p, v) AV_WB24(p, v)
73
#   endif
74
75 4a051891 Måns Rullgård
#   if    defined(AV_RN32) && !defined(AV_RB32)
76
#       define AV_RB32(p) AV_RN32(p)
77
#   elif !defined(AV_RN32) &&  defined(AV_RB32)
78
#       define AV_RN32(p) AV_RB32(p)
79
#   endif
80
81 63826ceb Måns Rullgård
#   if    defined(AV_WN32) && !defined(AV_WB32)
82
#       define AV_WB32(p, v) AV_WN32(p, v)
83
#   elif !defined(AV_WN32) &&  defined(AV_WB32)
84
#       define AV_WN32(p, v) AV_WB32(p, v)
85
#   endif
86
87
#   if    defined(AV_RN64) && !defined(AV_RB64)
88
#       define AV_RB64(p) AV_RN64(p)
89
#   elif !defined(AV_RN64) &&  defined(AV_RB64)
90
#       define AV_RN64(p) AV_RB64(p)
91
#   endif
92
93
#   if    defined(AV_WN64) && !defined(AV_WB64)
94
#       define AV_WB64(p, v) AV_WN64(p, v)
95
#   elif !defined(AV_WN64) &&  defined(AV_WB64)
96
#       define AV_WN64(p, v) AV_WB64(p, v)
97
#   endif
98
99
#else /* HAVE_BIGENDIAN */
100
101
#   if    defined(AV_RN16) && !defined(AV_RL16)
102
#       define AV_RL16(p) AV_RN16(p)
103
#   elif !defined(AV_RN16) &&  defined(AV_RL16)
104
#       define AV_RN16(p) AV_RL16(p)
105
#   endif
106
107
#   if    defined(AV_WN16) && !defined(AV_WL16)
108
#       define AV_WL16(p, v) AV_WN16(p, v)
109
#   elif !defined(AV_WN16) &&  defined(AV_WL16)
110
#       define AV_WN16(p, v) AV_WL16(p, v)
111
#   endif
112
113
#   if    defined(AV_RN24) && !defined(AV_RL24)
114
#       define AV_RL24(p) AV_RN24(p)
115
#   elif !defined(AV_RN24) &&  defined(AV_RL24)
116
#       define AV_RN24(p) AV_RL24(p)
117
#   endif
118
119
#   if    defined(AV_WN24) && !defined(AV_WL24)
120
#       define AV_WL24(p, v) AV_WN24(p, v)
121
#   elif !defined(AV_WN24) &&  defined(AV_WL24)
122
#       define AV_WN24(p, v) AV_WL24(p, v)
123
#   endif
124
125 4a051891 Måns Rullgård
#   if    defined(AV_RN32) && !defined(AV_RL32)
126
#       define AV_RL32(p) AV_RN32(p)
127
#   elif !defined(AV_RN32) &&  defined(AV_RL32)
128
#       define AV_RN32(p) AV_RL32(p)
129
#   endif
130
131 63826ceb Måns Rullgård
#   if    defined(AV_WN32) && !defined(AV_WL32)
132
#       define AV_WL32(p, v) AV_WN32(p, v)
133
#   elif !defined(AV_WN32) &&  defined(AV_WL32)
134
#       define AV_WN32(p, v) AV_WL32(p, v)
135
#   endif
136
137
#   if    defined(AV_RN64) && !defined(AV_RL64)
138
#       define AV_RL64(p) AV_RN64(p)
139
#   elif !defined(AV_RN64) &&  defined(AV_RL64)
140
#       define AV_RN64(p) AV_RL64(p)
141
#   endif
142
143
#   if    defined(AV_WN64) && !defined(AV_WL64)
144
#       define AV_WL64(p, v) AV_WN64(p, v)
145
#   elif !defined(AV_WN64) &&  defined(AV_WL64)
146
#       define AV_WN64(p, v) AV_WL64(p, v)
147
#   endif
148
149
#endif /* !HAVE_BIGENDIAN */
150
151
/*
152 a6783b89 Måns Rullgård
 * Define AV_[RW]N helper macros to simplify definitions not provided
153
 * by per-arch headers.
154
 */
155
156 e7ea5e3d Måns Rullgård
#if   HAVE_ATTRIBUTE_PACKED
157 cf1e119b Reimar Döffinger
158
struct unaligned_64 { uint64_t l; } __attribute__((packed));
159
struct unaligned_32 { uint32_t l; } __attribute__((packed));
160
struct unaligned_16 { uint16_t l; } __attribute__((packed));
161
162 a6783b89 Måns Rullgård
#   define AV_RN(s, p) (((const struct unaligned_##s *) (p))->l)
163 c2521027 Måns Rullgård
#   define AV_WN(s, p, v) ((((struct unaligned_##s *) (p))->l) = (v))
164 cf1e119b Reimar Döffinger
165 b7b38fb2 Måns Rullgård
#elif defined(__DECC)
166
167 a6783b89 Måns Rullgård
#   define AV_RN(s, p) (*((const __unaligned uint##s##_t*)(p)))
168 c2521027 Måns Rullgård
#   define AV_WN(s, p, v) (*((__unaligned uint##s##_t*)(p)) = (v))
169 fbbea48e Måns Rullgård
170 a6783b89 Måns Rullgård
#elif HAVE_FAST_UNALIGNED
171 fbbea48e Måns Rullgård
172 a6783b89 Måns Rullgård
#   define AV_RN(s, p) (*((const uint##s##_t*)(p)))
173 c2521027 Måns Rullgård
#   define AV_WN(s, p, v) (*((uint##s##_t*)(p)) = (v))
174 fbbea48e Måns Rullgård
175 a6783b89 Måns Rullgård
#else
176 fbbea48e Måns Rullgård
177 a6783b89 Måns Rullgård
#ifndef AV_RB16
178 4a051891 Måns Rullgård
#   define AV_RB16(x)                           \
179
    ((((const uint8_t*)(x))[0] << 8) |          \
180
      ((const uint8_t*)(x))[1])
181 a6783b89 Måns Rullgård
#endif
182
#ifndef AV_WB16
183 4a051891 Måns Rullgård
#   define AV_WB16(p, d) do {                   \
184
        ((uint8_t*)(p))[1] = (d);               \
185
        ((uint8_t*)(p))[0] = (d)>>8;            \
186
    } while(0)
187 a6783b89 Måns Rullgård
#endif
188 a3550abd Alex Beregszaszi
189 a6783b89 Måns Rullgård
#ifndef AV_RL16
190 4a051891 Måns Rullgård
#   define AV_RL16(x)                           \
191
    ((((const uint8_t*)(x))[1] << 8) |          \
192
      ((const uint8_t*)(x))[0])
193 a6783b89 Måns Rullgård
#endif
194
#ifndef AV_WL16
195 4a051891 Måns Rullgård
#   define AV_WL16(p, d) do {                   \
196
        ((uint8_t*)(p))[0] = (d);               \
197
        ((uint8_t*)(p))[1] = (d)>>8;            \
198
    } while(0)
199 a6783b89 Måns Rullgård
#endif
200 7b829d2a Ramiro Polla
201 a6783b89 Måns Rullgård
#ifndef AV_RB32
202 4a051891 Måns Rullgård
#   define AV_RB32(x)                           \
203
    ((((const uint8_t*)(x))[0] << 24) |         \
204
     (((const uint8_t*)(x))[1] << 16) |         \
205
     (((const uint8_t*)(x))[2] <<  8) |         \
206
      ((const uint8_t*)(x))[3])
207 a6783b89 Måns Rullgård
#endif
208
#ifndef AV_WB32
209 4a051891 Måns Rullgård
#   define AV_WB32(p, d) do {                   \
210
        ((uint8_t*)(p))[3] = (d);               \
211
        ((uint8_t*)(p))[2] = (d)>>8;            \
212
        ((uint8_t*)(p))[1] = (d)>>16;           \
213
        ((uint8_t*)(p))[0] = (d)>>24;           \
214
    } while(0)
215 a6783b89 Måns Rullgård
#endif
216 a3550abd Alex Beregszaszi
217 a6783b89 Måns Rullgård
#ifndef AV_RL32
218 4a051891 Måns Rullgård
#   define AV_RL32(x)                           \
219
    ((((const uint8_t*)(x))[3] << 24) |         \
220
     (((const uint8_t*)(x))[2] << 16) |         \
221
     (((const uint8_t*)(x))[1] <<  8) |         \
222
      ((const uint8_t*)(x))[0])
223 a6783b89 Måns Rullgård
#endif
224
#ifndef AV_WL32
225 4a051891 Måns Rullgård
#   define AV_WL32(p, d) do {                   \
226
        ((uint8_t*)(p))[0] = (d);               \
227
        ((uint8_t*)(p))[1] = (d)>>8;            \
228
        ((uint8_t*)(p))[2] = (d)>>16;           \
229
        ((uint8_t*)(p))[3] = (d)>>24;           \
230
    } while(0)
231 a6783b89 Måns Rullgård
#endif
232 9e010b41 Ivo van Poorten
233 a6783b89 Måns Rullgård
#ifndef AV_RB64
234 4a051891 Måns Rullgård
#   define AV_RB64(x)                                   \
235
    (((uint64_t)((const uint8_t*)(x))[0] << 56) |       \
236
     ((uint64_t)((const uint8_t*)(x))[1] << 48) |       \
237
     ((uint64_t)((const uint8_t*)(x))[2] << 40) |       \
238
     ((uint64_t)((const uint8_t*)(x))[3] << 32) |       \
239
     ((uint64_t)((const uint8_t*)(x))[4] << 24) |       \
240
     ((uint64_t)((const uint8_t*)(x))[5] << 16) |       \
241
     ((uint64_t)((const uint8_t*)(x))[6] <<  8) |       \
242
      (uint64_t)((const uint8_t*)(x))[7])
243 a6783b89 Måns Rullgård
#endif
244
#ifndef AV_WB64
245 4a051891 Måns Rullgård
#   define AV_WB64(p, d) do {                   \
246
        ((uint8_t*)(p))[7] = (d);               \
247
        ((uint8_t*)(p))[6] = (d)>>8;            \
248
        ((uint8_t*)(p))[5] = (d)>>16;           \
249
        ((uint8_t*)(p))[4] = (d)>>24;           \
250
        ((uint8_t*)(p))[3] = (d)>>32;           \
251
        ((uint8_t*)(p))[2] = (d)>>40;           \
252
        ((uint8_t*)(p))[1] = (d)>>48;           \
253
        ((uint8_t*)(p))[0] = (d)>>56;           \
254
    } while(0)
255 a6783b89 Måns Rullgård
#endif
256 9e010b41 Ivo van Poorten
257 a6783b89 Måns Rullgård
#ifndef AV_RL64
258 4a051891 Måns Rullgård
#   define AV_RL64(x)                                   \
259
    (((uint64_t)((const uint8_t*)(x))[7] << 56) |       \
260
     ((uint64_t)((const uint8_t*)(x))[6] << 48) |       \
261
     ((uint64_t)((const uint8_t*)(x))[5] << 40) |       \
262
     ((uint64_t)((const uint8_t*)(x))[4] << 32) |       \
263
     ((uint64_t)((const uint8_t*)(x))[3] << 24) |       \
264
     ((uint64_t)((const uint8_t*)(x))[2] << 16) |       \
265
     ((uint64_t)((const uint8_t*)(x))[1] <<  8) |       \
266
      (uint64_t)((const uint8_t*)(x))[0])
267 a6783b89 Måns Rullgård
#endif
268
#ifndef AV_WL64
269 4a051891 Måns Rullgård
#   define AV_WL64(p, d) do {                   \
270
        ((uint8_t*)(p))[0] = (d);               \
271
        ((uint8_t*)(p))[1] = (d)>>8;            \
272
        ((uint8_t*)(p))[2] = (d)>>16;           \
273
        ((uint8_t*)(p))[3] = (d)>>24;           \
274
        ((uint8_t*)(p))[4] = (d)>>32;           \
275
        ((uint8_t*)(p))[5] = (d)>>40;           \
276
        ((uint8_t*)(p))[6] = (d)>>48;           \
277
        ((uint8_t*)(p))[7] = (d)>>56;           \
278
    } while(0)
279 a6783b89 Måns Rullgård
#endif
280
281 63613fe6 Måns Rullgård
#if HAVE_BIGENDIAN
282 a6783b89 Måns Rullgård
#   define AV_RN(s, p)    AV_RB##s(p)
283
#   define AV_WN(s, p, v) AV_WB##s(p, v)
284
#else
285
#   define AV_RN(s, p)    AV_RL##s(p)
286
#   define AV_WN(s, p, v) AV_WL##s(p, v)
287
#endif
288
289
#endif /* HAVE_FAST_UNALIGNED */
290
291
#ifndef AV_RN16
292
#   define AV_RN16(p) AV_RN(16, p)
293
#endif
294
295
#ifndef AV_RN32
296
#   define AV_RN32(p) AV_RN(32, p)
297
#endif
298
299
#ifndef AV_RN64
300
#   define AV_RN64(p) AV_RN(64, p)
301
#endif
302
303
#ifndef AV_WN16
304
#   define AV_WN16(p, v) AV_WN(16, p, v)
305
#endif
306
307
#ifndef AV_WN32
308
#   define AV_WN32(p, v) AV_WN(32, p, v)
309
#endif
310
311
#ifndef AV_WN64
312
#   define AV_WN64(p, v) AV_WN(64, p, v)
313
#endif
314
315 63613fe6 Måns Rullgård
#if HAVE_BIGENDIAN
316 63826ceb Måns Rullgård
#   define AV_RB(s, p)    AV_RN##s(p)
317
#   define AV_WB(s, p, v) AV_WN##s(p, v)
318
#   define AV_RL(s, p)    bswap_##s(AV_RN##s(p))
319
#   define AV_WL(s, p, v) AV_WN##s(p, bswap_##s(v))
320 a6783b89 Måns Rullgård
#else
321 63826ceb Måns Rullgård
#   define AV_RB(s, p)    bswap_##s(AV_RN##s(p))
322
#   define AV_WB(s, p, v) AV_WN##s(p, bswap_##s(v))
323
#   define AV_RL(s, p)    AV_RN##s(p)
324
#   define AV_WL(s, p, v) AV_WN##s(p, v)
325 a6783b89 Måns Rullgård
#endif
326
327
#define AV_RB8(x)     (((const uint8_t*)(x))[0])
328
#define AV_WB8(p, d)  do { ((uint8_t*)(p))[0] = (d); } while(0)
329
330
#define AV_RL8(x)     AV_RB8(x)
331
#define AV_WL8(p, d)  AV_WB8(p, d)
332
333
#ifndef AV_RB16
334
#   define AV_RB16(p)    AV_RB(16, p)
335
#endif
336
#ifndef AV_WB16
337
#   define AV_WB16(p, v) AV_WB(16, p, v)
338
#endif
339
340
#ifndef AV_RL16
341
#   define AV_RL16(p)    AV_RL(16, p)
342
#endif
343
#ifndef AV_WL16
344
#   define AV_WL16(p, v) AV_WL(16, p, v)
345
#endif
346
347
#ifndef AV_RB32
348
#   define AV_RB32(p)    AV_RB(32, p)
349
#endif
350
#ifndef AV_WB32
351
#   define AV_WB32(p, v) AV_WB(32, p, v)
352
#endif
353
354
#ifndef AV_RL32
355
#   define AV_RL32(p)    AV_RL(32, p)
356
#endif
357
#ifndef AV_WL32
358
#   define AV_WL32(p, v) AV_WL(32, p, v)
359
#endif
360
361
#ifndef AV_RB64
362
#   define AV_RB64(p)    AV_RB(64, p)
363
#endif
364
#ifndef AV_WB64
365
#   define AV_WB64(p, v) AV_WB(64, p, v)
366
#endif
367
368
#ifndef AV_RL64
369
#   define AV_RL64(p)    AV_RL(64, p)
370
#endif
371
#ifndef AV_WL64
372
#   define AV_WL64(p, v) AV_WL(64, p, v)
373
#endif
374 fbbea48e Måns Rullgård
375 57c36bdc Måns Rullgård
#ifndef AV_RB24
376 4a051891 Måns Rullgård
#   define AV_RB24(x)                           \
377
    ((((const uint8_t*)(x))[0] << 16) |         \
378
     (((const uint8_t*)(x))[1] <<  8) |         \
379
      ((const uint8_t*)(x))[2])
380 57c36bdc Måns Rullgård
#endif
381
#ifndef AV_WB24
382 4a051891 Måns Rullgård
#   define AV_WB24(p, d) do {                   \
383
        ((uint8_t*)(p))[2] = (d);               \
384
        ((uint8_t*)(p))[1] = (d)>>8;            \
385
        ((uint8_t*)(p))[0] = (d)>>16;           \
386
    } while(0)
387 57c36bdc Måns Rullgård
#endif
388 fbbea48e Måns Rullgård
389 57c36bdc Måns Rullgård
#ifndef AV_RL24
390 4a051891 Måns Rullgård
#   define AV_RL24(x)                           \
391
    ((((const uint8_t*)(x))[2] << 16) |         \
392
     (((const uint8_t*)(x))[1] <<  8) |         \
393
      ((const uint8_t*)(x))[0])
394 57c36bdc Måns Rullgård
#endif
395
#ifndef AV_WL24
396 4a051891 Måns Rullgård
#   define AV_WL24(p, d) do {                   \
397
        ((uint8_t*)(p))[0] = (d);               \
398
        ((uint8_t*)(p))[1] = (d)>>8;            \
399
        ((uint8_t*)(p))[2] = (d)>>16;           \
400
    } while(0)
401 57c36bdc Måns Rullgård
#endif
402 9e010b41 Ivo van Poorten
403 f6d03906 Alexander Strange
/* Parameters for AV_COPY*, AV_SWAP*, AV_ZERO* must be
404
 * naturally aligned. They may be implemented using MMX,
405
 * so emms_c() must be called before using any float code
406
 * afterwards.
407
 */
408
409
#define AV_COPY(n, d, s) (*(uint##n##_t*)(d) = *(const uint##n##_t*)(s))
410
411 6c88973a Måns Rullgård
#ifndef AV_COPY32
412
#   define AV_COPY32(d, s) AV_COPY(32, d, s)
413
#endif
414
415 f6d03906 Alexander Strange
#ifndef AV_COPY64
416
#   define AV_COPY64(d, s) AV_COPY(64, d, s)
417
#endif
418
419
#ifndef AV_COPY128
420
#   define AV_COPY128(d, s)                    \
421
    do {                                       \
422
        AV_COPY64(d, s);                       \
423
        AV_COPY64((char*)(d)+8, (char*)(s)+8); \
424
    } while(0)
425
#endif
426
427
#define AV_SWAP(n, a, b) FFSWAP(uint##n##_t, *(uint##n##_t*)(a), *(uint##n##_t*)(b))
428
429
#ifndef AV_SWAP64
430
#   define AV_SWAP64(a, b) AV_SWAP(64, a, b)
431
#endif
432
433
#define AV_ZERO(n, d) (*(uint##n##_t*)(d) = 0)
434
435
#ifndef AV_ZERO64
436
#   define AV_ZERO64(d) AV_ZERO(64, d)
437
#endif
438
439
#ifndef AV_ZERO128
440
#   define AV_ZERO128(d)         \
441
    do {                         \
442
        AV_ZERO64(d);            \
443
        AV_ZERO64((char*)(d)+8); \
444
    } while(0)
445
#endif
446
447 98790382 Stefano Sabatini
#endif /* AVUTIL_INTREADWRITE_H */