encode_accessors.c 14.4 KB
Newer Older
1
#include <config.h>
2 3
#include <inttypes.h>
#include <string.h>
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
4 5 6
#include "libaec.h"
#include "encode.h"
#include "encode_accessors.h"
7

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
8
uint32_t get_8(aec_streamp strm)
9 10 11 12 13 14
{
    strm->avail_in--;
    strm->total_in++;
    return *strm->next_in++;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
15
uint32_t get_lsb_24(aec_streamp strm)
16 17 18 19 20 21 22 23 24 25 26 27 28
{
    uint32_t data;

    data = ((uint32_t)strm->next_in[2] << 16)
        | ((uint32_t)strm->next_in[1] << 8)
        | (uint32_t)strm->next_in[0];

    strm->next_in += 3;
    strm->total_in += 3;
    strm->avail_in -= 3;
    return data;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
29
uint32_t get_msb_24(aec_streamp strm)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
{
    uint32_t data;

    data = ((uint32_t)strm->next_in[0] << 16)
        | ((uint32_t)strm->next_in[1] << 8)
        | (uint32_t)strm->next_in[2];

    strm->next_in += 3;
    strm->total_in += 3;
    strm->avail_in -= 3;
    return data;
}

#ifdef WORDS_BIGENDIAN

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
45
uint32_t get_lsb_16(aec_streamp strm)
46 47 48 49 50 51 52 53 54 55 56 57
{
    uint32_t data;

    data = ((uint32_t)strm->next_in[1] << 8)
        | (uint32_t)strm->next_in[0];

    strm->next_in += 2;
    strm->total_in += 2;
    strm->avail_in -= 2;
    return data;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
58
uint32_t get_msb_16(aec_streamp strm)
59 60 61 62 63 64 65 66 67 68
{
    uint32_t data;

    data = *(uint16_t *)strm->next_in;
    strm->next_in += 2;
    strm->total_in += 2;
    strm->avail_in -= 2;
    return data;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
69
uint32_t get_lsb_32(aec_streamp strm)
70 71 72 73 74 75 76 77 78 79 80 81 82 83
{
    uint32_t data;

    data = ((uint32_t)strm->next_in[3] << 24)
        | ((uint32_t)strm->next_in[2] << 16)
        | ((uint32_t)strm->next_in[1] << 8)
        | (uint32_t)strm->next_in[0];

    strm->next_in += 4;
    strm->total_in += 4;
    strm->avail_in -= 4;
    return data;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
84
uint32_t get_msb_32(aec_streamp strm)
85 86 87 88 89 90 91 92 93
{
    uint32_t data;

    data = *(uint32_t *)strm->next_in;
    strm->next_in += 4;
    strm->total_in += 4;
    strm->avail_in -= 4;
    return data;
}
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
94
#else /* not WORDS_BIGENDIAN */
95

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
96
uint32_t get_lsb_16(aec_streamp strm)
97 98 99 100 101 102 103 104 105 106
{
    uint32_t data;

    data = *(uint16_t *)strm->next_in;
    strm->next_in += 2;
    strm->total_in += 2;
    strm->avail_in -= 2;
    return data;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
107
uint32_t get_msb_16(aec_streamp strm)
108 109 110 111 112 113 114 115 116 117 118 119
{
    uint32_t data;

    data = ((uint32_t)strm->next_in[0] << 8)
        | (uint32_t)strm->next_in[1];

    strm->next_in += 2;
    strm->total_in += 2;
    strm->avail_in -= 2;
    return data;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
120
uint32_t get_lsb_32(aec_streamp strm)
121 122 123 124 125 126 127 128 129 130
{
    uint32_t data;

    data = *(uint32_t *)strm->next_in;
    strm->next_in += 4;
    strm->total_in += 4;
    strm->avail_in -= 4;
    return data;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
131
uint32_t get_msb_32(aec_streamp strm)
132 133 134 135 136 137 138 139 140 141 142 143 144
{
    uint32_t data;

    data = ((uint32_t)strm->next_in[0] << 24)
        | ((uint32_t)strm->next_in[1] << 16)
        | ((uint32_t)strm->next_in[2] << 8)
        | (uint32_t)strm->next_in[3];

    strm->next_in += 4;
    strm->total_in += 4;
    strm->avail_in -= 4;
    return data;
}
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
145
#endif /* not WORDS_BIGENDIAN */
146 147

#define GET_BLOCK_8(BS)                                              \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
148
    static void get_block_8_bs_##BS(aec_streamp strm)                \
149 150 151 152 153 154 155 156 157 158 159 160 161 162
    {                                                                \
        int i, j;                                                    \
        uint32_t *block = strm->state->block_buf;                    \
                                                                     \
        for (i = 0; i < strm->rsi; i++)                              \
            for (j = 0; j < BS; j++)                                 \
                block[i * BS + j] = strm->next_in[i * BS + j];       \
                                                                     \
        strm->next_in += BS * strm->rsi;                             \
        strm->total_in += BS * strm->rsi;                            \
        strm->avail_in -= BS * strm->rsi;                            \
    }

#define GET_BLOCK_NATIVE_16(BS)                                      \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
163
    static void get_block_native_16_bs_##BS(aec_streamp strm)        \
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    {                                                                \
        int i, j;                                                    \
        uint32_t *block = strm->state->block_buf;                    \
        uint16_t *next_in = (uint16_t *)strm->next_in;               \
                                                                     \
        for (i = 0; i < strm->rsi; i++)                              \
            for (j = 0; j < BS; j++)                                 \
                block[i * BS + j] = (uint32_t)next_in[i * BS + j];   \
                                                                     \
        strm->next_in += 2 * BS * strm->rsi;                         \
        strm->total_in += 2 * BS * strm->rsi;                        \
        strm->avail_in -= 2 * BS * strm->rsi;                        \
    }

#define GET_BLOCK_LSB_16(BS)                                         \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
179
    static void get_block_lsb_16_bs_##BS(aec_streamp strm)           \
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    {                                                                \
        int i, j;                                                    \
        uint32_t *block = strm->state->block_buf;                    \
                                                                     \
        for (i = 0; i < strm->rsi; i++)                              \
            for (j = 0; j < BS; j++)                                 \
                block[i * BS + j] =                                  \
                    (uint32_t)strm->next_in[2 * (i * BS + j)]        \
                    | ((uint32_t)strm->next_in[2 * (i * BS + j) + 1] \
                       << 8);                                        \
                                                                     \
        strm->next_in += 2 * BS * strm->rsi;                         \
        strm->total_in += 2 * BS * strm->rsi;                        \
        strm->avail_in -= 2 * BS * strm->rsi;                        \
    }

#define GET_BLOCK_MSB_16(BS)                                         \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
197
    static void get_block_msb_16_bs_##BS(aec_streamp strm)           \
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    {                                                                \
        int i, j;                                                    \
        uint32_t *block = strm->state->block_buf;                    \
                                                                     \
        for (i = 0; i < strm->rsi; i++)                              \
            for (j = 0; j < BS; j++)                                 \
                block[i * BS + j] =                                  \
                    ((uint32_t)strm->next_in[2 * (i * BS + j)] << 8) \
                    | (uint32_t)strm->next_in[2 * (i * BS + j) + 1]; \
                                                                     \
        strm->next_in += 2 * BS * strm->rsi;                         \
        strm->total_in += 2 * BS * strm->rsi;                        \
        strm->avail_in -= 2 * BS * strm->rsi;                        \
    }

#define GET_BLOCK_LSB_24(BS)                                         \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
214
    static void get_block_lsb_24_bs_##BS(aec_streamp strm)           \
215 216 217 218 219 220 221
    {                                                                \
        int i, j;                                                    \
        uint32_t *block = strm->state->block_buf;                    \
                                                                     \
        for (i = 0; i < strm->rsi; i++)                              \
            for (j = 0; j < BS; j++)                                 \
                block[i * BS + j] =                                  \
222 223
                    (uint32_t)strm->next_in[3 * (i * BS + j)]        \
                    | ((uint32_t)strm->next_in[3 * (i * BS + j) + 1] \
224
                       << 8)                                         \
225
                    | ((uint32_t)strm->next_in[3 * (i * BS + j) + 2] \
226 227 228 229 230 231 232 233
                       << 16);                                       \
                                                                     \
        strm->next_in += 3 * BS * strm->rsi;                         \
        strm->total_in += 3 * BS * strm->rsi;                        \
        strm->avail_in -= 3 * BS * strm->rsi;                        \
    }

#define GET_BLOCK_MSB_24(BS)                                         \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
234
    static void get_block_msb_24_bs_##BS(aec_streamp strm)           \
235 236 237 238 239 240 241
    {                                                                \
        int i, j;                                                    \
        uint32_t *block = strm->state->block_buf;                    \
                                                                     \
        for (i = 0; i < strm->rsi; i++)                              \
            for (j = 0; j < BS; j++)                                 \
                block[i * BS + j] =                                  \
242
                    ((uint32_t)strm->next_in[3 * (i * BS + j)]       \
243
                       << 16)                                        \
244
                    | ((uint32_t)strm->next_in[3 * (i * BS + j) + 1] \
245
                       << 8)                                         \
246
                    | (uint32_t)strm->next_in[3 * (i * BS + j) + 2]; \
247 248 249 250 251 252 253
                                                                     \
        strm->next_in += 3 * BS * strm->rsi;                         \
        strm->total_in += 3 * BS * strm->rsi;                        \
        strm->avail_in -= 3 * BS * strm->rsi;                        \
    }

#define GET_BLOCK_NATIVE_32(BS)                                      \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
254
    static void get_block_native_32_bs_##BS(aec_streamp strm)        \
255 256 257 258 259 260 261 262 263 264 265
    {                                                                \
        memcpy(strm->state->block_buf,                               \
               strm->next_in,                                        \
               4 * BS * strm->rsi);                                  \
                                                                     \
        strm->next_in += 4 * BS * strm->rsi;                         \
        strm->total_in += 4 * BS * strm->rsi;                        \
        strm->avail_in -= 4 * BS * strm->rsi;                        \
    }

#define GET_BLOCK_LSB_32(BS)                                         \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
266
    static void get_block_lsb_32_bs_##BS(aec_streamp strm)           \
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    {                                                                \
        int i, j;                                                    \
        uint32_t *block = strm->state->block_buf;                    \
                                                                     \
        for (i = 0; i < strm->rsi; i++)                              \
            for (j = 0; j < BS; j++)                                 \
                block[i * BS + j] =                                  \
                    (uint32_t)strm->next_in[4 * (i * BS + j)]        \
                    | ((uint32_t)strm->next_in[4 * (i * BS + j) + 1] \
                       << 8)                                         \
                    | ((uint32_t)strm->next_in[4 * (i * BS + j) + 2] \
                       << 16)                                        \
                    | ((uint32_t)strm->next_in[4 * (i * BS + j) + 3] \
                       << 24);                                       \
                                                                     \
        strm->next_in += 4 * BS * strm->rsi;                         \
        strm->total_in += 4 * BS * strm->rsi;                        \
        strm->avail_in -= 4 * BS * strm->rsi;                        \
    }

#define GET_BLOCK_MSB_32(BS)                                         \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
288
    static void get_block_msb_32_bs_##BS(aec_streamp strm)           \
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    {                                                                \
        int i, j;                                                    \
        uint32_t *block = strm->state->block_buf;                    \
                                                                     \
        for (i = 0; i < strm->rsi; i++)                              \
            for (j = 0; j < BS; j++)                                 \
                block[i * BS + j] =                                  \
                    ((uint32_t)strm->next_in[4 * (i * BS + j)]       \
                     << 24)                                          \
                    | ((uint32_t)strm->next_in[4 * (i * BS + j) + 1] \
                       << 16)                                        \
                    | ((uint32_t)strm->next_in[4 * (i * BS + j) + 2] \
                       << 8)                                         \
                    | (uint32_t)strm->next_in[4 * (i * BS + j) + 3]; \
                                                                     \
        strm->next_in += 4 * BS * strm->rsi;                         \
        strm->total_in += 4 * BS * strm->rsi;                        \
        strm->avail_in -= 4 * BS * strm->rsi;                        \
    }

#define GET_BLOCK_FUNCS(A, B)                           \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
310
    void (*get_block_funcs_##A[])(aec_streamp) = {      \
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
        get_block_##B##_bs_8,                           \
        get_block_##B##_bs_16,                          \
        get_block_##B##_bs_32,                          \
        get_block_##B##_bs_64,                          \
    }

GET_BLOCK_8(8);
GET_BLOCK_8(16);
GET_BLOCK_8(32);
GET_BLOCK_8(64);

GET_BLOCK_FUNCS(8, 8);

GET_BLOCK_LSB_24(8);
GET_BLOCK_LSB_24(16);
GET_BLOCK_LSB_24(32);
GET_BLOCK_LSB_24(64);

GET_BLOCK_FUNCS(lsb_24, lsb_24);

GET_BLOCK_MSB_24(8);
GET_BLOCK_MSB_24(16);
GET_BLOCK_MSB_24(32);
GET_BLOCK_MSB_24(64);

GET_BLOCK_FUNCS(msb_24, msb_24);

GET_BLOCK_NATIVE_16(8);
GET_BLOCK_NATIVE_16(16);
GET_BLOCK_NATIVE_16(32);
GET_BLOCK_NATIVE_16(64);

GET_BLOCK_NATIVE_32(8);
GET_BLOCK_NATIVE_32(16);
GET_BLOCK_NATIVE_32(32);
GET_BLOCK_NATIVE_32(64);

#ifdef WORDS_BIGENDIAN

GET_BLOCK_LSB_16(8);
GET_BLOCK_LSB_16(16);
GET_BLOCK_LSB_16(32);
GET_BLOCK_LSB_16(64);

GET_BLOCK_LSB_32(8);
GET_BLOCK_LSB_32(16);
GET_BLOCK_LSB_32(32);
GET_BLOCK_LSB_32(64);

GET_BLOCK_FUNCS(lsb_16, lsb_16);
GET_BLOCK_FUNCS(msb_16, native_16);
GET_BLOCK_FUNCS(lsb_32, lsb_32);
GET_BLOCK_FUNCS(msb_32, native_32);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
365
#else /* not WORDS_BIGENDIAN */
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

GET_BLOCK_MSB_16(8);
GET_BLOCK_MSB_16(16);
GET_BLOCK_MSB_16(32);
GET_BLOCK_MSB_16(64);

GET_BLOCK_MSB_32(8);
GET_BLOCK_MSB_32(16);
GET_BLOCK_MSB_32(32);
GET_BLOCK_MSB_32(64);

GET_BLOCK_FUNCS(lsb_16, native_16);
GET_BLOCK_FUNCS(msb_16, msb_16);
GET_BLOCK_FUNCS(lsb_32, native_32);
GET_BLOCK_FUNCS(msb_32, msb_32);

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
382
#endif /* not WORDS_BIGENDIAN */