encode_accessors.c 12.4 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++;
}

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

uint32_t get_msb_16(struct aec_stream *strm)
{
    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
97
uint32_t get_lsb_24(struct aec_stream *strm)
98 99 100 101 102 103 104 105 106 107 108 109 110
{
    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
111
uint32_t get_msb_24(struct aec_stream *strm)
112 113 114 115 116 117 118 119 120 121 122 123 124
{
    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;
}

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

155 156
void get_rsi_8(struct aec_stream *strm)
{
157
    uint32_t *out = strm->state->data_raw;
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    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;
177
    }
178
}
179

180 181 182 183 184 185 186 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 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
void get_rsi_lsb_16(struct aec_stream *strm)
{
    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->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;
    }
}

void get_rsi_msb_16(struct aec_stream *strm)
{
    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->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;
    }
}

246 247
void get_rsi_lsb_24(struct aec_stream *strm)
{
248
    uint32_t *out = strm->state->data_raw;
249
    const unsigned char *in = strm->next_in;
250 251 252 253 254 255 256 257 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
    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;
284
    }
285
}
286

287 288
void get_rsi_msb_24(struct aec_stream *strm)
{
289
    uint32_t *out = strm->state->data_raw;
290
    const unsigned char *in = strm->next_in;
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    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;
325
    }
326
}
327

328 329 330 331
#define GET_RSI_NATIVE_32(BO)                       \
    void get_rsi_##BO##_32(struct aec_stream *strm) \
    {                                               \
        int rsi = strm->rsi * strm->block_size;     \
332
        memcpy(strm->state->data_raw,               \
333 334 335 336
               strm->next_in, 4 * rsi);             \
        strm->next_in += 4 * rsi;                   \
        strm->total_in += 4 * rsi;                  \
        strm->avail_in -= 4 * rsi;                  \
337 338
    }

339 340 341
#ifdef WORDS_BIGENDIAN
void get_rsi_lsb_32(struct aec_stream *strm)
{
342
    uint32_t *out = strm->state->data_raw;
343
    const unsigned char *in = strm->next_in;
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    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;
386
    }
387
}
388

389
GET_RSI_NATIVE_32(msb);
390

391 392 393
#else /* !WORDS_BIGENDIAN */
void get_rsi_msb_32(struct aec_stream *strm)
{
394
    uint32_t *out = strm->state->data_raw;
395
    const unsigned char *in = strm->next_in;
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    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;
    }
}
440

441
GET_RSI_NATIVE_32(lsb);
442

443
#endif /* !WORDS_BIGENDIAN */