encode_accessors.c 14.9 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 get_8(struct aec_stream *strm)
65 66 67 68 69 70
{
    strm->avail_in--;
    strm->total_in++;
    return *strm->next_in++;
}

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
71
uint32_t get_lsb_24(struct aec_stream *strm)
72 73 74 75 76 77 78 79 80 81 82 83 84
{
    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
85
uint32_t get_msb_24(struct aec_stream *strm)
86 87 88 89 90 91 92 93 94 95 96 97 98
{
    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;
}

99 100 101 102 103 104 105 106 107 108 109
#define GET_NATIVE_16(BO)                       \
uint32_t get_##BO##_16(struct aec_stream *strm) \
{                                               \
    uint32_t data;                              \
                                                \
    data = *(uint16_t *)strm->next_in;          \
    strm->next_in += 2;                         \
    strm->total_in += 2;                        \
    strm->avail_in -= 2;                        \
    return data;                                \
}
110

111 112 113 114 115 116 117 118 119 120 121 122 123
#define GET_NATIVE_32(BO)                       \
uint32_t get_##BO##_32(struct aec_stream *strm) \
{                                               \
    uint32_t data;                              \
                                                \
    data = *(uint32_t *)strm->next_in;          \
    strm->next_in += 4;                         \
    strm->total_in += 4;                        \
    strm->avail_in -= 4;                        \
    return data;                                \
}

#ifdef WORDS_BIGENDIAN
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
124
uint32_t get_lsb_16(struct aec_stream *strm)
125 126 127 128 129 130 131 132 133 134 135 136
{
    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
137
uint32_t get_lsb_32(struct aec_stream *strm)
138 139 140 141 142 143 144 145 146 147 148 149 150 151
{
    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;
}

152 153
GET_NATIVE_16(msb);
GET_NATIVE_32(msb);
154

155
#else /* !WORDS_BIGENDIAN */
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
156
uint32_t get_msb_16(struct aec_stream *strm)
157 158 159 160 161 162 163 164 165 166 167 168
{
    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
169
uint32_t get_msb_32(struct aec_stream *strm)
170 171 172 173 174 175 176 177 178 179 180 181 182 183
{
    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;
}

184 185
GET_NATIVE_16(lsb);
GET_NATIVE_32(lsb);
186

187
#endif /* !WORDS_BIGENDIAN */
188

189 190
void get_rsi_8(struct aec_stream *strm)
{
191
    uint32_t *out = strm->state->data_raw;
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    unsigned const char *in = strm->next_in;
    int rsi = strm->rsi * strm->block_size;

    strm->next_in += rsi;
    strm->total_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;
211
    }
212
}
213

214 215
void get_rsi_lsb_24(struct aec_stream *strm)
{
216
    uint32_t *out = strm->state->data_raw;
217 218 219 220 221 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
    unsigned const char *in = strm->next_in;
    int rsi = strm->rsi * strm->block_size;

    strm->next_in += 3 * rsi;
    strm->total_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;
252
    }
253
}
254

255 256
void get_rsi_msb_24(struct aec_stream *strm)
{
257
    uint32_t *out = strm->state->data_raw;
258 259 260 261 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
    unsigned const char *in = strm->next_in;
    int rsi = strm->rsi * strm->block_size;

    strm->next_in += 3 * rsi;
    strm->total_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;
293
    }
294
}
295

296 297 298
#define GET_RSI_NATIVE_16(BO)                       \
    void get_rsi_##BO##_16(struct aec_stream *strm) \
    {                                               \
299
        uint32_t *out = strm->state->data_raw;      \
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
        uint16_t *in = (uint16_t *)strm->next_in;   \
        int rsi = strm->rsi * strm->block_size;     \
                                                    \
        strm->next_in += 2 * rsi;                   \
        strm->total_in += 2 * rsi;                  \
        strm->avail_in -= 2 * 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;                               \
        }                                           \
320 321
    }

322 323 324 325
#define GET_RSI_NATIVE_32(BO)                       \
    void get_rsi_##BO##_32(struct aec_stream *strm) \
    {                                               \
        int rsi = strm->rsi * strm->block_size;     \
326
        memcpy(strm->state->data_raw,               \
327 328 329 330
               strm->next_in, 4 * rsi);             \
        strm->next_in += 4 * rsi;                   \
        strm->total_in += 4 * rsi;                  \
        strm->avail_in -= 4 * rsi;                  \
331 332
    }

333 334 335
#ifdef WORDS_BIGENDIAN
void get_rsi_lsb_16(struct aec_stream *strm)
{
336
    uint32_t *out = strm->state->data_raw;
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
    unsigned const char *in = strm->next_in;
    int rsi = strm->rsi * strm->block_size;

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

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

367 368
void get_rsi_lsb_32(struct aec_stream *strm)
{
369
    uint32_t *out = strm->state->data_raw;
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 407 408 409 410 411 412
    unsigned const char *in = strm->next_in;
    int rsi = strm->rsi * strm->block_size;

    strm->next_in += 4 * rsi;
    strm->total_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;
413
    }
414
}
415

416 417
GET_RSI_NATIVE_16(msb);
GET_RSI_NATIVE_32(msb);
418

419 420 421
#else /* !WORDS_BIGENDIAN */
void get_rsi_msb_16(struct aec_stream *strm)
{
422
    uint32_t *out = strm->state->data_raw;
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
    unsigned const char *in = strm->next_in;
    int rsi = strm->rsi * strm->block_size;

    strm->next_in += 2 * rsi;
    strm->total_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;
    }
}
452

453 454
void get_rsi_msb_32(struct aec_stream *strm)
{
455
    uint32_t *out = strm->state->data_raw;
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
    unsigned const char *in = strm->next_in;
    int rsi = strm->rsi * strm->block_size;

    strm->next_in += 4 * rsi;
    strm->total_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;
    }
}
501

502 503
GET_RSI_NATIVE_16(lsb);
GET_RSI_NATIVE_32(lsb);
504

505
#endif /* !WORDS_BIGENDIAN */