encode_accessors.c 14.5 KB
Newer Older
1
#include <config.h>
2 3 4 5 6

#if HAVE_STDINT_H
# include <stdint.h>
#endif

7
#include <string.h>
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
8 9 10
#include "libaec.h"
#include "encode.h"
#include "encode_accessors.h"
11

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
12
uint32_t get_8(struct aec_stream *strm)
13 14 15 16 17 18
{
    strm->avail_in--;
    strm->total_in++;
    return *strm->next_in++;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
19
uint32_t get_lsb_24(struct aec_stream *strm)
20 21 22 23 24 25 26 27 28 29 30 31 32
{
    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
33
uint32_t get_msb_24(struct aec_stream *strm)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
    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
49
uint32_t get_lsb_16(struct aec_stream *strm)
50 51 52 53 54 55 56 57 58 59 60 61
{
    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
62
uint32_t get_msb_16(struct aec_stream *strm)
63 64 65 66 67 68 69 70 71 72
{
    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
73
uint32_t get_lsb_32(struct aec_stream *strm)
74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
    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
88
uint32_t get_msb_32(struct aec_stream *strm)
89 90 91 92 93 94 95 96 97
{
    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
98
#else /* not WORDS_BIGENDIAN */
99

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
100
uint32_t get_lsb_16(struct aec_stream *strm)
101 102 103 104 105 106 107 108 109 110
{
    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
111
uint32_t get_msb_16(struct aec_stream *strm)
112 113 114 115 116 117 118 119 120 121 122 123
{
    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
124
uint32_t get_lsb_32(struct aec_stream *strm)
125 126 127 128 129 130 131 132 133 134
{
    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
135
uint32_t get_msb_32(struct aec_stream *strm)
136 137 138 139 140 141 142 143 144 145 146 147 148
{
    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
149
#endif /* not WORDS_BIGENDIAN */
150 151

#define GET_BLOCK_8(BS)                                              \
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
152
    static void get_block_8_bs_##BS(struct aec_stream *strm)         \
153 154 155 156 157 158 159 160 161 162 163 164 165 166
    {                                                                \
        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
167
    static void get_block_native_16_bs_##BS(struct aec_stream *strm) \
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    {                                                                \
        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
183
    static void get_block_lsb_16_bs_##BS(struct aec_stream *strm)    \
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    {                                                                \
        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
201
    static void get_block_msb_16_bs_##BS(struct aec_stream *strm)    \
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    {                                                                \
        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
218
    static void get_block_lsb_24_bs_##BS(struct aec_stream *strm)    \
219 220 221 222 223 224 225
    {                                                                \
        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] =                                  \
226 227
                    (uint32_t)strm->next_in[3 * (i * BS + j)]        \
                    | ((uint32_t)strm->next_in[3 * (i * BS + j) + 1] \
228
                       << 8)                                         \
229
                    | ((uint32_t)strm->next_in[3 * (i * BS + j) + 2] \
230 231 232 233 234 235 236 237
                       << 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
238
    static void get_block_msb_24_bs_##BS(struct aec_stream *strm)    \
239 240 241 242 243 244 245
    {                                                                \
        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] =                                  \
246
                    ((uint32_t)strm->next_in[3 * (i * BS + j)]       \
247
                       << 16)                                        \
248
                    | ((uint32_t)strm->next_in[3 * (i * BS + j) + 1] \
249
                       << 8)                                         \
250
                    | (uint32_t)strm->next_in[3 * (i * BS + j) + 2]; \
251 252 253 254 255 256 257
                                                                     \
        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
258
    static void get_block_native_32_bs_##BS(struct aec_stream *strm) \
259 260 261 262 263 264 265 266 267 268 269
    {                                                                \
        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
270
    static void get_block_lsb_32_bs_##BS(struct aec_stream *strm)    \
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    {                                                                \
        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
292
    static void get_block_msb_32_bs_##BS(struct aec_stream *strm)    \
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    {                                                                \
        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;                        \
    }

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
313 314 315 316 317 318
#define GET_BLOCK_FUNCS(A, B)                               \
    void (*get_block_funcs_##A[])(struct aec_stream *) = {  \
        get_block_##B##_bs_8,                               \
        get_block_##B##_bs_16,                              \
        get_block_##B##_bs_32,                              \
        get_block_##B##_bs_64,                              \
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 365 366 367 368
    }

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
369
#else /* not WORDS_BIGENDIAN */
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

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
386
#endif /* not WORDS_BIGENDIAN */