lagarith.c 23.3 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
/*
 * Lagarith lossless decoder
 * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24 25 26 27
 * Lagarith lossless decoder
 * @author Nathan Caldwell
 */

28 29
#include <inttypes.h>

30 31 32 33
#include "avcodec.h"
#include "get_bits.h"
#include "mathops.h"
#include "lagarithrac.h"
34
#include "lossless_videodsp.h"
35
#include "thread.h"
36 37

enum LagarithFrameType {
38 39 40 41 42 43 44 45 46 47 48
    FRAME_RAW           = 1,    /**< uncompressed */
    FRAME_U_RGB24       = 2,    /**< unaligned RGB24 */
    FRAME_ARITH_YUY2    = 3,    /**< arithmetic coded YUY2 */
    FRAME_ARITH_RGB24   = 4,    /**< arithmetic coded RGB24 */
    FRAME_SOLID_GRAY    = 5,    /**< solid grayscale color frame */
    FRAME_SOLID_COLOR   = 6,    /**< solid non-grayscale color frame */
    FRAME_OLD_ARITH_RGB = 7,    /**< obsolete arithmetic coded RGB (no longer encoded by upstream since version 1.1.0) */
    FRAME_ARITH_RGBA    = 8,    /**< arithmetic coded RGBA */
    FRAME_SOLID_RGBA    = 9,    /**< solid RGBA color frame */
    FRAME_ARITH_YV12    = 10,   /**< arithmetic coded YV12 */
    FRAME_REDUCED_RES   = 11,   /**< reduced resolution YV12 frame */
49 50 51 52
};

typedef struct LagarithContext {
    AVCodecContext *avctx;
53
    LLVidDSPContext llviddsp;
54 55
    int zeros;                  /**< number of consecutive zero bytes encountered */
    int zeros_rem;              /**< number of zero bytes remaining to output */
56
    uint8_t *rgb_planes;
57
    int      rgb_planes_allocated;
58
    int rgb_stride;
59 60 61
} LagarithContext;

/**
62
 * Compute the 52-bit mantissa of 1/(double)denom.
63 64 65
 * This crazy format uses floats in an entropy coder and we have to match x86
 * rounding exactly, thus ordinary floats aren't portable enough.
 * @param denom denominator
66
 * @return 52-bit mantissa
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
 * @see softfloat_mul
 */
static uint64_t softfloat_reciprocal(uint32_t denom)
{
    int shift = av_log2(denom - 1) + 1;
    uint64_t ret = (1ULL << 52) / denom;
    uint64_t err = (1ULL << 52) - ret * denom;
    ret <<= shift;
    err <<= shift;
    err +=  denom / 2;
    return ret + err / denom;
}

/**
 * (uint32_t)(x*f), where f has the given mantissa, and exponent 0
 * Used in combination with softfloat_reciprocal computes x/(double)denom.
83
 * @param x 32-bit integer factor
84
 * @param mantissa mantissa of f with exponent 0
85
 * @return 32-bit integer value (x*f)
86 87 88 89 90 91 92 93
 * @see softfloat_reciprocal
 */
static uint32_t softfloat_mul(uint32_t x, uint64_t mantissa)
{
    uint64_t l = x * (mantissa & 0xffffffff);
    uint64_t h = x * (mantissa >> 32);
    h += l >> 32;
    l &= 0xffffffff;
94
    l += 1LL << av_log2(h >> 21);
95 96 97 98 99 100
    h += l >> 32;
    return h >> 20;
}

static uint8_t lag_calc_zero_run(int8_t x)
{
101
    return (x * 2) ^ (x >> 7);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
}

static int lag_decode_prob(GetBitContext *gb, uint32_t *value)
{
    static const uint8_t series[] = { 1, 2, 3, 5, 8, 13, 21 };
    int i;
    int bit     = 0;
    int bits    = 0;
    int prevbit = 0;
    unsigned val;

    for (i = 0; i < 7; i++) {
        if (prevbit && bit)
            break;
        prevbit = bit;
        bit = get_bits1(gb);
        if (bit && !prevbit)
            bits += series[i];
    }
    bits--;
    if (bits < 0 || bits > 31) {
        *value = 0;
        return -1;
    } else if (bits == 0) {
        *value = 0;
        return 0;
    }

    val  = get_bits_long(gb, bits);
131
    val |= 1U << bits;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

    *value = val - 1;

    return 0;
}

static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb)
{
    int i, j, scale_factor;
    unsigned prob, cumulative_target;
    unsigned cumul_prob = 0;
    unsigned scaled_cumul_prob = 0;

    rac->prob[0] = 0;
    rac->prob[257] = UINT_MAX;
    /* Read probabilities from bitstream */
    for (i = 1; i < 257; i++) {
        if (lag_decode_prob(gb, &rac->prob[i]) < 0) {
            av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability encountered.\n");
            return -1;
        }
        if ((uint64_t)cumul_prob + rac->prob[i] > UINT_MAX) {
            av_log(rac->avctx, AV_LOG_ERROR, "Integer overflow encountered in cumulative probability calculation.\n");
            return -1;
        }
        cumul_prob += rac->prob[i];
        if (!rac->prob[i]) {
            if (lag_decode_prob(gb, &prob)) {
                av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability run encountered.\n");
                return -1;
            }
163 164
            if (prob > 256 - i)
                prob = 256 - i;
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
            for (j = 0; j < prob; j++)
                rac->prob[++i] = 0;
        }
    }

    if (!cumul_prob) {
        av_log(rac->avctx, AV_LOG_ERROR, "All probabilities are 0!\n");
        return -1;
    }

    /* Scale probabilities so cumulative probability is an even power of 2. */
    scale_factor = av_log2(cumul_prob);

    if (cumul_prob & (cumul_prob - 1)) {
        uint64_t mul = softfloat_reciprocal(cumul_prob);
180 181 182 183 184 185 186 187 188
        for (i = 1; i <= 128; i++) {
            rac->prob[i] = softfloat_mul(rac->prob[i], mul);
            scaled_cumul_prob += rac->prob[i];
        }
        if (scaled_cumul_prob <= 0) {
            av_log(rac->avctx, AV_LOG_ERROR, "Scaled probabilities invalid\n");
            return AVERROR_INVALIDDATA;
        }
        for (; i < 257; i++) {
189 190 191 192 193
            rac->prob[i] = softfloat_mul(rac->prob[i], mul);
            scaled_cumul_prob += rac->prob[i];
        }

        scale_factor++;
194 195 196
        if (scale_factor >= 32U)
            return AVERROR_INVALIDDATA;
        cumulative_target = 1U << scale_factor;
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

        if (scaled_cumul_prob > cumulative_target) {
            av_log(rac->avctx, AV_LOG_ERROR,
                   "Scaled probabilities are larger than target!\n");
            return -1;
        }

        scaled_cumul_prob = cumulative_target - scaled_cumul_prob;

        for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) {
            if (rac->prob[i]) {
                rac->prob[i]++;
                scaled_cumul_prob--;
            }
            /* Comment from reference source:
             * if (b & 0x80 == 0) {     // order of operations is 'wrong'; it has been left this way
Diego Biurrun's avatar
Diego Biurrun committed
213
             *                          // since the compression change is negligible and fixing it
214
             *                          // breaks backwards compatibility
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
             *      b =- (signed int)b;
             *      b &= 0xFF;
             * } else {
             *      b++;
             *      b &= 0x7f;
             * }
             */
        }
    }

    rac->scale = scale_factor;

    /* Fill probability array with cumulative probability for each symbol. */
    for (i = 1; i < 257; i++)
        rac->prob[i] += rac->prob[i - 1];

    return 0;
}

static void add_lag_median_prediction(uint8_t *dst, uint8_t *src1,
                                      uint8_t *diff, int w, int *left,
                                      int *left_top)
{
238
    /* This is almost identical to add_hfyu_median_pred in huffyuvdsp.h.
239
     * However the &0xFF on the gradient predictor yields incorrect output
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
     * for lagarith.
     */
    int i;
    uint8_t l, lt;

    l  = *left;
    lt = *left_top;

    for (i = 0; i < w; i++) {
        l = mid_pred(l, src1[i], l + src1[i] - lt) + diff[i];
        lt = src1[i];
        dst[i] = l;
    }

    *left     = l;
    *left_top = lt;
}

static void lag_pred_line(LagarithContext *l, uint8_t *buf,
                          int width, int stride, int line)
{
    int L, TL;

    if (!line) {
        /* Left prediction only for first line */
265
        L = l->llviddsp.add_left_pred(buf, buf, width, 0);
266
    } else {
267 268 269 270 271 272
        /* Left pixel is actually prev_row[width] */
        L = buf[width - stride - 1];

        if (line == 1) {
            /* Second line, left predict first pixel, the rest of the line is median predicted
             * NOTE: In the case of RGB this pixel is top predicted */
273
            TL = l->avctx->pix_fmt == AV_PIX_FMT_YUV420P ? buf[-stride] : L;
274 275 276 277
        } else {
            /* Top left is 2 rows back, last pixel */
            TL = buf[width - (2 * stride) - 1];
        }
278

279 280 281
        add_lag_median_prediction(buf, buf - stride, buf,
                                  width, &L, &TL);
    }
282 283
}

284 285 286 287 288 289 290
static void lag_pred_line_yuy2(LagarithContext *l, uint8_t *buf,
                               int width, int stride, int line,
                               int is_luma)
{
    int L, TL;

    if (!line) {
291 292 293
        L= buf[0];
        if (is_luma)
            buf[0] = 0;
294
        l->llviddsp.add_left_pred(buf, buf, width, 0);
295 296
        if (is_luma)
            buf[0] = L;
297 298 299 300 301 302 303 304 305 306 307 308
        return;
    }
    if (line == 1) {
        const int HEAD = is_luma ? 4 : 2;
        int i;

        L  = buf[width - stride - 1];
        TL = buf[HEAD  - stride - 1];
        for (i = 0; i < HEAD; i++) {
            L += buf[i];
            buf[i] = L;
        }
309 310 311 312
        for (; i < width; i++) {
            L      = mid_pred(L & 0xFF, buf[i - stride], (L + buf[i - stride] - TL) & 0xFF) + buf[i];
            TL     = buf[i - stride];
            buf[i] = L;
313
        }
314 315 316
    } else {
        TL = buf[width - (2 * stride) - 1];
        L  = buf[width - stride - 1];
317
        l->llviddsp.add_median_pred(buf, buf - stride, buf, width, &L, &TL);
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
static int lag_decode_line(LagarithContext *l, lag_rac *rac,
                           uint8_t *dst, int width, int stride,
                           int esc_count)
{
    int i = 0;
    int ret = 0;

    if (!esc_count)
        esc_count = -1;

    /* Output any zeros remaining from the previous run */
handle_zeros:
    if (l->zeros_rem) {
        int count = FFMIN(l->zeros_rem, width - i);
        memset(dst + i, 0, count);
        i += count;
        l->zeros_rem -= count;
    }

    while (i < width) {
        dst[i] = lag_get_rac(rac);
        ret++;

        if (dst[i])
            l->zeros = 0;
        else
            l->zeros++;

        i++;
        if (l->zeros == esc_count) {
            int index = lag_get_rac(rac);
            ret++;

            l->zeros = 0;

            l->zeros_rem = lag_calc_zero_run(index);
            goto handle_zeros;
        }
    }
    return ret;
}

static int lag_decode_zero_run_line(LagarithContext *l, uint8_t *dst,
364 365
                                    const uint8_t *src, const uint8_t *src_end,
                                    int width, int esc_count)
366 367 368 369
{
    int i = 0;
    int count;
    uint8_t zero_run = 0;
370
    const uint8_t *src_start = src;
371 372 373 374
    uint8_t mask1 = -(esc_count < 2);
    uint8_t mask2 = -(esc_count < 3);
    uint8_t *end = dst + (width - 2);

375
    avpriv_request_sample(l->avctx, "zero_run_line");
376 377

    memset(dst, 0, width);
378

379 380 381
output_zeros:
    if (l->zeros_rem) {
        count = FFMIN(l->zeros_rem, width - i);
382 383
        if (end - dst < count) {
            av_log(l->avctx, AV_LOG_ERROR, "Too many zeros remaining.\n");
384 385
            return AVERROR_INVALIDDATA;
        }
386

387 388 389 390 391 392 393 394 395
        memset(dst, 0, count);
        l->zeros_rem -= count;
        dst += count;
    }

    while (dst < end) {
        i = 0;
        while (!zero_run && dst + i < end) {
            i++;
396
            if (i+2 >= src_end - src)
397
                return AVERROR_INVALIDDATA;
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
            zero_run =
                !(src[i] | (src[i + 1] & mask1) | (src[i + 2] & mask2));
        }
        if (zero_run) {
            zero_run = 0;
            i += esc_count;
            memcpy(dst, src, i);
            dst += i;
            l->zeros_rem = lag_calc_zero_run(src[i]);

            src += i + 1;
            goto output_zeros;
        } else {
            memcpy(dst, src, i);
            src += i;
413
            dst += i;
414 415
        }
    }
416
    return  src - src_start;
417 418 419 420 421 422 423 424 425 426 427 428
}



static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
                                  int width, int height, int stride,
                                  const uint8_t *src, int src_size)
{
    int i = 0;
    int read = 0;
    uint32_t length;
    uint32_t offset = 1;
429
    int esc_count;
430 431
    GetBitContext gb;
    lag_rac rac;
432
    const uint8_t *src_end = src + src_size;
433
    int ret;
434 435 436 437

    rac.avctx = l->avctx;
    l->zeros = 0;

438 439 440 441
    if(src_size < 2)
        return AVERROR_INVALIDDATA;

    esc_count = src[0];
442 443
    if (esc_count < 4) {
        length = width * height;
444 445
        if(src_size < 5)
            return AVERROR_INVALIDDATA;
446 447 448 449 450
        if (esc_count && AV_RL32(src + 1) < length) {
            length = AV_RL32(src + 1);
            offset += 4;
        }

451 452
        if ((ret = init_get_bits8(&gb, src + offset, src_size - offset)) < 0)
            return ret;
453 454 455 456

        if (lag_read_prob_header(&rac, &gb) < 0)
            return -1;

457
        ff_lag_rac_init(&rac, &gb, length - stride);
458 459 460
        for (i = 0; i < height; i++) {
            if (rac.overread > MAX_OVERREAD)
                return AVERROR_INVALIDDATA;
461 462
            read += lag_decode_line(l, &rac, dst + (i * stride), width,
                                    stride, esc_count);
463
        }
464 465 466

        if (read > length)
            av_log(l->avctx, AV_LOG_WARNING,
467
                   "Output more bytes than length (%d of %"PRIu32")\n", read,
468 469 470
                   length);
    } else if (esc_count < 8) {
        esc_count -= 4;
471 472
        src ++;
        src_size --;
473 474
        if (esc_count > 0) {
            /* Zero run coding only, no range coding. */
475 476 477 478 479 480 481
            for (i = 0; i < height; i++) {
                int res = lag_decode_zero_run_line(l, dst + (i * stride), src,
                                                   src_end, width, esc_count);
                if (res < 0)
                    return res;
                src += res;
            }
482
        } else {
483 484
            if (src_size < width * height)
                return AVERROR_INVALIDDATA; // buffer not big enough
485 486 487 488 489 490 491
            /* Plane is stored uncompressed */
            for (i = 0; i < height; i++) {
                memcpy(dst + (i * stride), src, width);
                src += width;
            }
        }
    } else if (esc_count == 0xff) {
492
        /* Plane is a solid run of given value */
493
        for (i = 0; i < height; i++)
494 495 496 497 498
            memset(dst + i * stride, src[1], width);
        /* Do not apply prediction.
           Note: memset to 0 above, setting first value to src[1]
           and applying prediction gives the same result. */
        return 0;
499 500 501 502 503 504
    } else {
        av_log(l->avctx, AV_LOG_ERROR,
               "Invalid zero run escape code! (%#x)\n", esc_count);
        return -1;
    }

505
    if (l->avctx->pix_fmt != AV_PIX_FMT_YUV422P) {
506 507 508 509 510 511 512 513 514 515
        for (i = 0; i < height; i++) {
            lag_pred_line(l, dst, width, stride, i);
            dst += stride;
        }
    } else {
        for (i = 0; i < height; i++) {
            lag_pred_line_yuy2(l, dst, width, stride, i,
                               width == l->avctx->width);
            dst += stride;
        }
516 517 518 519 520 521 522 523 524 525 526 527 528 529
    }

    return 0;
}

/**
 * Decode a frame.
 * @param avctx codec context
 * @param data output AVFrame
 * @param data_size size of output data or 0 if no picture is returned
 * @param avpkt input packet
 * @return number of consumed bytes on success or negative if decode fails
 */
static int lag_decode_frame(AVCodecContext *avctx,
530
                            void *data, int *got_frame, AVPacket *avpkt)
531 532
{
    const uint8_t *buf = avpkt->data;
533
    unsigned int buf_size = avpkt->size;
534
    LagarithContext *l = avctx->priv_data;
535 536
    ThreadFrame frame = { .f = data };
    AVFrame *const p  = data;
537 538
    uint8_t frametype = 0;
    uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9;
539
    uint32_t offs[4];
540
    uint8_t *srcs[4], *dst;
Paul B Mahol's avatar
Paul B Mahol committed
541
    int i, j, planes = 3;
542
    int ret;
543 544 545 546 547 548 549 550 551

    p->key_frame = 1;

    frametype = buf[0];

    offset_gu = AV_RL32(buf + 1);
    offset_bv = AV_RL32(buf + 5);

    switch (frametype) {
552
    case FRAME_SOLID_RGBA:
553
        avctx->pix_fmt = AV_PIX_FMT_RGB32;
554 555 556 557 558 559 560 561
    case FRAME_SOLID_GRAY:
        if (frametype == FRAME_SOLID_GRAY)
            if (avctx->bits_per_coded_sample == 24) {
                avctx->pix_fmt = AV_PIX_FMT_RGB24;
            } else {
                avctx->pix_fmt = AV_PIX_FMT_0RGB32;
                planes = 4;
            }
562

563
        if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
564
            return ret;
565 566

        dst = p->data[0];
567
        if (frametype == FRAME_SOLID_RGBA) {
568 569 570 571 572
        for (j = 0; j < avctx->height; j++) {
            for (i = 0; i < avctx->width; i++)
                AV_WN32(dst + i * 4, offset_gu);
            dst += p->linesize[0];
        }
573 574 575 576 577 578
        } else {
            for (j = 0; j < avctx->height; j++) {
                memset(dst, buf[1], avctx->width * planes);
                dst += p->linesize[0];
            }
        }
579
        break;
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
    case FRAME_SOLID_COLOR:
        if (avctx->bits_per_coded_sample == 24) {
            avctx->pix_fmt = AV_PIX_FMT_RGB24;
        } else {
            avctx->pix_fmt = AV_PIX_FMT_RGB32;
            offset_gu |= 0xFFU << 24;
        }

        if ((ret = ff_thread_get_buffer(avctx, &frame,0)) < 0)
            return ret;

        dst = p->data[0];
        for (j = 0; j < avctx->height; j++) {
            for (i = 0; i < avctx->width; i++)
                if (avctx->bits_per_coded_sample == 24) {
                    AV_WB24(dst + i * 3, offset_gu);
                } else {
                    AV_WN32(dst + i * 4, offset_gu);
                }
            dst += p->linesize[0];
        }
        break;
602
    case FRAME_ARITH_RGBA:
603
        avctx->pix_fmt = AV_PIX_FMT_RGB32;
Paul B Mahol's avatar
Paul B Mahol committed
604 605 606 607
        planes = 4;
        offset_ry += 4;
        offs[3] = AV_RL32(buf + 9);
    case FRAME_ARITH_RGB24:
608 609
    case FRAME_U_RGB24:
        if (frametype == FRAME_ARITH_RGB24 || frametype == FRAME_U_RGB24)
610
            avctx->pix_fmt = AV_PIX_FMT_RGB24;
611

612
        if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
613
            return ret;
Paul B Mahol's avatar
Paul B Mahol committed
614

615 616
        offs[0] = offset_bv;
        offs[1] = offset_gu;
Paul B Mahol's avatar
Paul B Mahol committed
617
        offs[2] = offset_ry;
618

619 620 621
        l->rgb_stride = FFALIGN(avctx->width, 16);
        av_fast_malloc(&l->rgb_planes, &l->rgb_planes_allocated,
                       l->rgb_stride * avctx->height * planes + 1);
622
        if (!l->rgb_planes) {
623 624
            av_log(avctx, AV_LOG_ERROR, "cannot allocate temporary buffer\n");
            return AVERROR(ENOMEM);
625
        }
Paul B Mahol's avatar
Paul B Mahol committed
626
        for (i = 0; i < planes; i++)
627
            srcs[i] = l->rgb_planes + (i + 1) * l->rgb_stride * avctx->height - l->rgb_stride;
628
        for (i = 0; i < planes; i++)
629
            if (buf_size <= offs[i]) {
630 631
                av_log(avctx, AV_LOG_ERROR,
                        "Invalid frame offsets\n");
632 633
                return AVERROR_INVALIDDATA;
            }
634

635
        for (i = 0; i < planes; i++)
636 637 638
            lag_decode_arith_plane(l, srcs[i],
                                   avctx->width, avctx->height,
                                   -l->rgb_stride, buf + offs[i],
639
                                   buf_size - offs[i]);
640
        dst = p->data[0];
Paul B Mahol's avatar
Paul B Mahol committed
641
        for (i = 0; i < planes; i++)
642 643 644 645 646 647 648 649 650
            srcs[i] = l->rgb_planes + i * l->rgb_stride * avctx->height;
        for (j = 0; j < avctx->height; j++) {
            for (i = 0; i < avctx->width; i++) {
                uint8_t r, g, b, a;
                r = srcs[0][i];
                g = srcs[1][i];
                b = srcs[2][i];
                r += g;
                b += g;
Paul B Mahol's avatar
Paul B Mahol committed
651 652 653 654 655 656 657 658
                if (frametype == FRAME_ARITH_RGBA) {
                    a = srcs[3][i];
                    AV_WN32(dst + i * 4, MKBETAG(a, r, g, b));
                } else {
                    dst[i * 3 + 0] = r;
                    dst[i * 3 + 1] = g;
                    dst[i * 3 + 2] = b;
                }
659 660
            }
            dst += p->linesize[0];
Paul B Mahol's avatar
Paul B Mahol committed
661
            for (i = 0; i < planes; i++)
662 663 664
                srcs[i] += l->rgb_stride;
        }
        break;
665
    case FRAME_ARITH_YUY2:
666
        avctx->pix_fmt = AV_PIX_FMT_YUV422P;
667

668
        if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
669
            return ret;
670 671 672 673 674 675 676 677 678 679 680 681

        if (offset_ry >= buf_size ||
            offset_gu >= buf_size ||
            offset_bv >= buf_size) {
            av_log(avctx, AV_LOG_ERROR,
                   "Invalid frame offsets\n");
            return AVERROR_INVALIDDATA;
        }

        lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
                               p->linesize[0], buf + offset_ry,
                               buf_size - offset_ry);
682
        lag_decode_arith_plane(l, p->data[1], (avctx->width + 1) / 2,
683
                               avctx->height, p->linesize[1],
684
                               buf + offset_gu, buf_size - offset_gu);
685
        lag_decode_arith_plane(l, p->data[2], (avctx->width + 1) / 2,
686
                               avctx->height, p->linesize[2],
687 688
                               buf + offset_bv, buf_size - offset_bv);
        break;
689
    case FRAME_ARITH_YV12:
690
        avctx->pix_fmt = AV_PIX_FMT_YUV420P;
691

692
        if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
693
            return ret;
694 695 696
        if (buf_size <= offset_ry || buf_size <= offset_gu || buf_size <= offset_bv) {
            return AVERROR_INVALIDDATA;
        }
697

698 699 700 701 702 703 704 705
        if (offset_ry >= buf_size ||
            offset_gu >= buf_size ||
            offset_bv >= buf_size) {
            av_log(avctx, AV_LOG_ERROR,
                   "Invalid frame offsets\n");
            return AVERROR_INVALIDDATA;
        }

706 707
        lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
                               p->linesize[0], buf + offset_ry,
708
                               buf_size - offset_ry);
709 710
        lag_decode_arith_plane(l, p->data[2], (avctx->width + 1) / 2,
                               (avctx->height + 1) / 2, p->linesize[2],
711
                               buf + offset_gu, buf_size - offset_gu);
712 713
        lag_decode_arith_plane(l, p->data[1], (avctx->width + 1) / 2,
                               (avctx->height + 1) / 2, p->linesize[1],
714
                               buf + offset_bv, buf_size - offset_bv);
715 716 717
        break;
    default:
        av_log(avctx, AV_LOG_ERROR,
718
               "Unsupported Lagarith frame type: %#"PRIx8"\n", frametype);
719
        return AVERROR_PATCHWELCOME;
720 721
    }

722
    *got_frame = 1;
723 724 725 726 727 728 729 730 731

    return buf_size;
}

static av_cold int lag_decode_init(AVCodecContext *avctx)
{
    LagarithContext *l = avctx->priv_data;
    l->avctx = avctx;

732
    ff_llviddsp_init(&l->llviddsp);
733 734 735 736

    return 0;
}

737 738 739 740 741 742 743 744 745 746
#if HAVE_THREADS
static av_cold int lag_decode_init_thread_copy(AVCodecContext *avctx)
{
    LagarithContext *l = avctx->priv_data;
    l->avctx = avctx;

    return 0;
}
#endif

747 748 749 750
static av_cold int lag_decode_end(AVCodecContext *avctx)
{
    LagarithContext *l = avctx->priv_data;

751
    av_freep(&l->rgb_planes);
752 753 754 755

    return 0;
}

756
AVCodec ff_lagarith_decoder = {
757
    .name           = "lagarith",
758
    .long_name      = NULL_IF_CONFIG_SMALL("Lagarith lossless"),
759
    .type           = AVMEDIA_TYPE_VIDEO,
760
    .id             = AV_CODEC_ID_LAGARITH,
761 762
    .priv_data_size = sizeof(LagarithContext),
    .init           = lag_decode_init,
763
    .init_thread_copy = ONLY_IF_THREADS_ENABLED(lag_decode_init_thread_copy),
764 765
    .close          = lag_decode_end,
    .decode         = lag_decode_frame,
766
    .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
767
};