encode_accessors.c 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/**
 * @file encode_accessors.c
 *
 * @author Mathis Rosenhauer, Deutsches Klimarechenzentrum
 * @author Moritz Hanke, Deutsches Klimarechenzentrum
 * @author Joerg Behrens, Deutsches Klimarechenzentrum
 * @author Luis Kornblueh, Max-Planck-Institut fuer Meteorologie
 *
 * @section LICENSE
 * Copyright 2012
 *
 * Mathis Rosenhauer,                 Luis Kornblueh
 * Moritz Hanke,
 * Joerg Behrens
 *
 * Deutsches Klimarechenzentrum GmbH  Max-Planck-Institut fuer Meteorologie
 * Bundesstr. 45a                     Bundesstr. 53
 * 20146 Hamburg                      20146 Hamburg
 * Germany                            Germany
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @section DESCRIPTION
 *
 * Read various data types from input stream
 *
 */

53
#include <config.h>
54 55 56 57 58

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

59
#include <string.h>
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
60 61 62
#include "libaec.h"
#include "encode.h"
#include "encode_accessors.h"
63

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
64
uint32_t aec_get_8(struct aec_stream *strm)
65 66 67 68 69
{
    strm->avail_in--;
    return *strm->next_in++;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
70
uint32_t aec_get_lsb_16(struct aec_stream *strm)
71 72 73 74 75 76 77 78 79 80 81
{
    uint32_t data;

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
82
uint32_t aec_get_msb_16(struct aec_stream *strm)
83 84 85 86 87 88 89 90 91 92 93
{
    uint32_t data;

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

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

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
94
uint32_t aec_get_lsb_24(struct aec_stream *strm)
95 96 97 98 99 100 101 102 103 104 105 106
{
    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->avail_in -= 3;
    return data;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
107
uint32_t aec_get_msb_24(struct aec_stream *strm)
108 109 110 111 112 113 114 115 116 117 118 119
{
    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->avail_in -= 3;
    return data;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
120
uint32_t aec_get_lsb_32(struct aec_stream *strm)
121 122 123 124 125 126 127 128 129 130 131 132 133
{
    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->avail_in -= 4;
    return data;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
134
uint32_t aec_get_msb_32(struct aec_stream *strm)
135 136 137 138 139 140 141 142 143 144 145 146 147
{
    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->avail_in -= 4;
    return data;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
148
void aec_get_rsi_8(struct aec_stream *strm)
149
{
150
    uint32_t *out = strm->state->data_raw;
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    unsigned const char *in = strm->next_in;
    int rsi = strm->rsi * strm->block_size;

    strm->next_in += rsi;
    strm->avail_in -= rsi;

    while (rsi) {
        out[0] = (uint32_t)in[0];
        out[1] = (uint32_t)in[1];
        out[2] = (uint32_t)in[2];
        out[3] = (uint32_t)in[3];
        out[4] = (uint32_t)in[4];
        out[5] = (uint32_t)in[5];
        out[6] = (uint32_t)in[6];
        out[7] = (uint32_t)in[7];
        in += 8;
        out += 8;
        rsi -= 8;
169
    }
170
}
171

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
172
void aec_get_rsi_lsb_16(struct aec_stream *strm)
173
{
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
174
    int i;
175 176 177 178
    uint32_t *out = strm->state->data_raw;
    const unsigned char *in = strm->next_in;
    int rsi = strm->rsi * strm->block_size;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
179 180 181
    for (i = 0; i < rsi; i++)
        out[i] = (uint32_t)in[2 * i] | ((uint32_t)in[2 * i + 1] << 8);

182 183 184 185
    strm->next_in += 2 * rsi;
    strm->avail_in -= 2 * rsi;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
186
void aec_get_rsi_msb_16(struct aec_stream *strm)
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
{
    uint32_t *out = strm->state->data_raw;
    const unsigned char *in = strm->next_in;
    int rsi = strm->rsi * strm->block_size;

    strm->next_in += 2 * rsi;
    strm->avail_in -= 2 * rsi;

    while (rsi) {
        out[0] = ((uint32_t)in[0] << 8)
            | (uint32_t)in[1];
        out[1] = ((uint32_t)in[2] << 8)
            | (uint32_t)in[3];
        out[2] = ((uint32_t)in[4] << 8)
            | (uint32_t)in[5];
        out[3] = ((uint32_t)in[6] << 8)
            | (uint32_t)in[7];
        out[4] = ((uint32_t)in[8] << 8)
            | (uint32_t)in[9];
        out[5] = ((uint32_t)in[10] << 8)
            | (uint32_t)in[11];
        out[6] = ((uint32_t)in[12] << 8)
            | (uint32_t)in[13];
        out[7] = ((uint32_t)in[14] << 8)
            | (uint32_t)in[15];
        in += 16;
        out += 8;
        rsi -= 8;
    }
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
218
void aec_get_rsi_lsb_24(struct aec_stream *strm)
219
{
220
    uint32_t *out = strm->state->data_raw;
221
    const unsigned char *in = strm->next_in;
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    int rsi = strm->rsi * strm->block_size;

    strm->next_in += 3 * rsi;
    strm->avail_in -= 3 * rsi;

    while (rsi) {
        out[0] = (uint32_t)in[0]
            | ((uint32_t)in[1] << 8)
            | ((uint32_t)in[2] << 16);
        out[1] = (uint32_t)in[3]
            | ((uint32_t)in[4] << 8)
            | ((uint32_t)in[5] << 16);
        out[2] = (uint32_t)in[6]
            | ((uint32_t)in[7] << 8)
            | ((uint32_t)in[8] << 16);
        out[3] = (uint32_t)in[9]
            | ((uint32_t)in[10] << 8)
            | ((uint32_t)in[11] << 16);
        out[4] = (uint32_t)in[12]
            | ((uint32_t)in[13] << 8)
            | ((uint32_t)in[14] << 16);
        out[5] = (uint32_t)in[15]
            | ((uint32_t)in[16] << 8)
            | ((uint32_t)in[17] << 16);
        out[6] = (uint32_t)in[18]
            | ((uint32_t)in[19] << 8)
            | ((uint32_t)in[20] << 16);
        out[7] = (uint32_t)in[21]
            | ((uint32_t)in[22] << 8)
            | ((uint32_t)in[23] << 16);
        in += 24;
        out += 8;
        rsi -= 8;
255
    }
256
}
257

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
258
void aec_get_rsi_msb_24(struct aec_stream *strm)
259
{
260
    uint32_t *out = strm->state->data_raw;
261
    const unsigned char *in = strm->next_in;
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    int rsi = strm->rsi * strm->block_size;

    strm->next_in += 3 * rsi;
    strm->avail_in -= 3 * rsi;

    while (rsi) {
        out[0] = ((uint32_t)in[0] << 16)
            | ((uint32_t)in[1] << 8)
            | (uint32_t)in[2];
        out[1] = ((uint32_t)in[3] << 16)
            | ((uint32_t)in[4] << 8)
            | (uint32_t)in[5];
        out[2] = ((uint32_t)in[6] << 16)
            | ((uint32_t)in[7] << 8)
            | (uint32_t)in[8];
        out[3] = ((uint32_t)in[9] << 16)
            | ((uint32_t)in[10] << 8)
            | (uint32_t)in[11];
        out[4] = ((uint32_t)in[12] << 16)
            | ((uint32_t)in[13] << 8)
            | (uint32_t)in[14];
        out[5] = ((uint32_t)in[15] << 16)
            | ((uint32_t)in[16] << 8)
            | (uint32_t)in[17];
        out[6] = ((uint32_t)in[18] << 16)
            | ((uint32_t)in[19] << 8)
            | (uint32_t)in[20];
        out[7] = ((uint32_t)in[21] << 16)
            | ((uint32_t)in[22] << 8)
            | (uint32_t)in[23];
        in += 24;
        out += 8;
        rsi -= 8;
295
    }
296
}
297

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
298 299
#define AEC_GET_RSI_NATIVE_32(BO)                       \
    void aec_get_rsi_##BO##_32(struct aec_stream *strm) \
300 301
    {                                               \
        int rsi = strm->rsi * strm->block_size;     \
302
        memcpy(strm->state->data_raw,               \
303 304 305
               strm->next_in, 4 * rsi);             \
        strm->next_in += 4 * rsi;                   \
        strm->avail_in -= 4 * rsi;                  \
306 307
    }

308
#ifdef WORDS_BIGENDIAN
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
309
void aec_get_rsi_lsb_32(struct aec_stream *strm)
310
{
311
    uint32_t *out = strm->state->data_raw;
312
    const unsigned char *in = strm->next_in;
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
    int rsi = strm->rsi * strm->block_size;

    strm->next_in += 4 * rsi;
    strm->avail_in -= 4 * rsi;

    while (rsi) {
        out[0] = (uint32_t)in[0]
            | ((uint32_t)in[1] << 8)
            | ((uint32_t)in[2] << 16)
            | ((uint32_t)in[3] << 24);
        out[1] = (uint32_t)in[4]
            | ((uint32_t)in[5] << 8)
            | ((uint32_t)in[6] << 16)
            | ((uint32_t)in[7] << 24);
        out[2] = (uint32_t)in[8]
            | ((uint32_t)in[9] << 8)
            | ((uint32_t)in[10] << 16)
            | ((uint32_t)in[11] << 24);
        out[3] = (uint32_t)in[12]
            | ((uint32_t)in[13] << 8)
            | ((uint32_t)in[14] << 16)
            | ((uint32_t)in[15] << 24);
        out[4] = (uint32_t)in[16]
            | ((uint32_t)in[17] << 8)
            | ((uint32_t)in[18] << 16)
            | ((uint32_t)in[19] << 24);
        out[5] = (uint32_t)in[20]
            | ((uint32_t)in[21] << 8)
            | ((uint32_t)in[22] << 16)
            | ((uint32_t)in[23] << 24);
        out[6] = (uint32_t)in[24]
            | ((uint32_t)in[25] << 8)
            | ((uint32_t)in[26] << 16)
            | ((uint32_t)in[27] << 24);
        out[7] = (uint32_t)in[28]
            | ((uint32_t)in[29] << 8)
            | ((uint32_t)in[30] << 16)
            | ((uint32_t)in[31] << 24);
        in += 32;
        out += 8;
        rsi -= 8;
354
    }
355
}
356

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
357
AEC_GET_RSI_NATIVE_32(msb);
358

359
#else /* !WORDS_BIGENDIAN */
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
360
void aec_get_rsi_msb_32(struct aec_stream *strm)
361
{
362
    uint32_t *out = strm->state->data_raw;
363
    const unsigned char *in = strm->next_in;
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    int rsi = strm->rsi * strm->block_size;

    strm->next_in += 4 * rsi;
    strm->avail_in -= 4 * rsi;

    while (rsi) {
        out[0] = ((uint32_t)in[0] << 24)
            | ((uint32_t)in[1] << 16)
            | ((uint32_t)in[2] << 8)
            | (uint32_t)in[3];
        out[1] = ((uint32_t)in[4] << 24)
            | ((uint32_t)in[5] << 16)
            | ((uint32_t)in[6] << 8)
            | (uint32_t)in[7];
        out[2] = ((uint32_t)in[8] << 24)
            | ((uint32_t)in[9] << 16)
            | ((uint32_t)in[10] << 8)
            | (uint32_t)in[11];
        out[3] = ((uint32_t)in[12] << 24)
            | ((uint32_t)in[13] << 16)
            | ((uint32_t)in[14] << 8)
            | (uint32_t)in[15];
        out[4] = ((uint32_t)in[16] << 24)
            | ((uint32_t)in[17] << 16)
            | ((uint32_t)in[18] << 8)
            | (uint32_t)in[19];
        out[5] = ((uint32_t)in[20] << 24)
            | ((uint32_t)in[21] << 16)
            | ((uint32_t)in[22] << 8)
            | (uint32_t)in[23];
        out[6] = ((uint32_t)in[24] << 24)
            | ((uint32_t)in[25] << 16)
            | ((uint32_t)in[26] << 8)
            | (uint32_t)in[27];
        out[7] = ((uint32_t)in[28] << 24)
            | ((uint32_t)in[29] << 16)
            | ((uint32_t)in[30] << 8)
            | (uint32_t)in[31];
        in += 32;
        out += 8;
        rsi -= 8;
    }
}
407

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
408
AEC_GET_RSI_NATIVE_32(lsb);
409

410
#endif /* !WORDS_BIGENDIAN */