Browse code

Lagarith decoder by Nathan Caldwell, saintdev at gmail

Originally committed as revision 26270 to svn://svn.ffmpeg.org/ffmpeg/trunk

Carl Eugen Hoyos authored on 2011/01/09 06:22:15
Showing 9 changed files
... ...
@@ -71,6 +71,7 @@ version <next>:
71 71
 - FFmpeg metadata format muxer and demuxer
72 72
 - SubRip (srt) subtitle decoder
73 73
 - floating-point AC-3 encoder added
74
+- Lagarith decoder
74 75
 
75 76
 
76 77
 version 0.6:
... ...
@@ -424,6 +424,7 @@ following image formats are supported:
424 424
     @tab Codec used in Worms games.
425 425
 @item Kega Game Video (KGV1) @tab      @tab  X
426 426
     @tab Kega emulator screen capture codec.
427
+@item Lagarith               @tab     @tab  X
427 428
 @item LCL (LossLess Codec Library) MSZH  @tab     @tab  X
428 429
 @item LCL (LossLess Codec Library) ZLIB  @tab  E  @tab  E
429 430
 @item LOCO                   @tab     @tab  X
... ...
@@ -185,6 +185,7 @@ OBJS-$(CONFIG_JPEGLS_DECODER)          += jpeglsdec.o jpegls.o \
185 185
 OBJS-$(CONFIG_JPEGLS_ENCODER)          += jpeglsenc.o jpegls.o
186 186
 OBJS-$(CONFIG_KGV1_DECODER)            += kgv1dec.o
187 187
 OBJS-$(CONFIG_KMVC_DECODER)            += kmvc.o
188
+OBJS-$(CONFIG_LAGARITH_DECODER)        += lagarith.o lagarithrac.o
188 189
 OBJS-$(CONFIG_LJPEG_ENCODER)           += ljpegenc.o mjpegenc.o mjpeg.o \
189 190
                                           mpegvideo_enc.o motion_est.o  \
190 191
                                           ratecontrol.o mpeg12data.o    \
... ...
@@ -128,6 +128,7 @@ void avcodec_register_all(void)
128 128
     REGISTER_ENCDEC  (JPEGLS, jpegls);
129 129
     REGISTER_DECODER (KGV1, kgv1);
130 130
     REGISTER_DECODER (KMVC, kmvc);
131
+    REGISTER_DECODER (LAGARITH, lagarith);
131 132
     REGISTER_ENCODER (LJPEG, ljpeg);
132 133
     REGISTER_DECODER (LOCO, loco);
133 134
     REGISTER_DECODER (MDEC, mdec);
... ...
@@ -32,7 +32,7 @@
32 32
 #include "libavutil/cpu.h"
33 33
 
34 34
 #define LIBAVCODEC_VERSION_MAJOR 52
35
-#define LIBAVCODEC_VERSION_MINOR 107
35
+#define LIBAVCODEC_VERSION_MINOR 108
36 36
 #define LIBAVCODEC_VERSION_MICRO  0
37 37
 
38 38
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -259,6 +259,7 @@ enum CodecID {
259 259
     CODEC_ID_A64_MULTI5,
260 260
     CODEC_ID_R10K,
261 261
     CODEC_ID_MXPEG,
262
+    CODEC_ID_LAGARITH,
262 263
 
263 264
     /* various PCM "codecs" */
264 265
     CODEC_ID_PCM_S16LE= 0x10000,
265 266
new file mode 100644
... ...
@@ -0,0 +1,518 @@
0
+/*
1
+ * Lagarith lossless decoder
2
+ * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file libavcodec/lagarith.c
23
+ * Lagarith lossless decoder
24
+ * @author Nathan Caldwell
25
+ */
26
+
27
+#include "avcodec.h"
28
+#include "get_bits.h"
29
+#include "mathops.h"
30
+#include "dsputil.h"
31
+#include "lagarithrac.h"
32
+
33
+enum LagarithFrameType {
34
+    FRAME_RAW           = 1,    /*!< uncompressed */
35
+    FRAME_U_RGB24       = 2,    /*!< unaligned RGB24 */
36
+    FRAME_ARITH_YUY2    = 3,    /*!< arithmetic coded YUY2 */
37
+    FRAME_ARITH_RGB24   = 4,    /*!< arithmetic coded RGB24 */
38
+    FRAME_SOLID_GRAY    = 5,    /*!< solid grayscale color frame */
39
+    FRAME_SOLID_COLOR   = 6,    /*!< solid non-grayscale color frame */
40
+    FRAME_OLD_ARITH_RGB = 7,    /*!< obsolete arithmetic coded RGB (no longer encoded by upstream since version 1.1.0) */
41
+    FRAME_ARITH_RGBA    = 8,    /*!< arithmetic coded RGBA */
42
+    FRAME_SOLID_RGBA    = 9,    /*!< solid RGBA color frame */
43
+    FRAME_ARITH_YV12    = 10,   /*!< arithmetic coded YV12 */
44
+    FRAME_REDUCED_RES   = 11,   /*!< reduced resolution YV12 frame */
45
+};
46
+
47
+typedef struct LagarithContext {
48
+    AVCodecContext *avctx;
49
+    AVFrame picture;
50
+    DSPContext dsp;
51
+    int zeros;                  /*!< number of consecutive zero bytes encountered */
52
+    int zeros_rem;              /*!< number of zero bytes remaining to output */
53
+} LagarithContext;
54
+
55
+/**
56
+ * Compute the 52bit mantissa of 1/(double)denom.
57
+ * This crazy format uses floats in an entropy coder and we have to match x86
58
+ * rounding exactly, thus ordinary floats aren't portable enough.
59
+ * @param denom denominator
60
+ * @return 52bit mantissa
61
+ * @see softfloat_mul
62
+ */
63
+static uint64_t softfloat_reciprocal(uint32_t denom)
64
+{
65
+    int shift = av_log2(denom - 1) + 1;
66
+    uint64_t ret = (1ULL << 52) / denom;
67
+    uint64_t err = (1ULL << 52) - ret * denom;
68
+    ret <<= shift;
69
+    err <<= shift;
70
+    err +=  denom / 2;
71
+    return ret + err / denom;
72
+}
73
+
74
+/**
75
+ * (uint32_t)(x*f), where f has the given mantissa, and exponent 0
76
+ * Used in combination with softfloat_reciprocal computes x/(double)denom.
77
+ * @param x 32bit integer factor
78
+ * @param mantissa mantissa of f with exponent 0
79
+ * @return 32bit integer value (x*f)
80
+ * @see softfloat_reciprocal
81
+ */
82
+static uint32_t softfloat_mul(uint32_t x, uint64_t mantissa)
83
+{
84
+    uint64_t l = x * (mantissa & 0xffffffff);
85
+    uint64_t h = x * (mantissa >> 32);
86
+    h += l >> 32;
87
+    l &= 0xffffffff;
88
+    l += 1 << av_log2(h >> 21);
89
+    h += l >> 32;
90
+    return h >> 20;
91
+}
92
+
93
+static uint8_t lag_calc_zero_run(int8_t x)
94
+{
95
+    return (x << 1) ^ (x >> 7);
96
+}
97
+
98
+static int lag_decode_prob(GetBitContext *gb, uint32_t *value)
99
+{
100
+    static const uint8_t series[] = { 1, 2, 3, 5, 8, 13, 21 };
101
+    int i;
102
+    int bit     = 0;
103
+    int bits    = 0;
104
+    int prevbit = 0;
105
+    unsigned val;
106
+
107
+    for (i = 0; i < 7; i++) {
108
+        if (prevbit && bit)
109
+            break;
110
+        prevbit = bit;
111
+        bit = get_bits1(gb);
112
+        if (bit && !prevbit)
113
+            bits += series[i];
114
+    }
115
+    bits--;
116
+    if (bits < 0 || bits > 31) {
117
+        *value = 0;
118
+        return -1;
119
+    } else if (bits == 0) {
120
+        *value = 0;
121
+        return 0;
122
+    }
123
+
124
+    val  = get_bits_long(gb, bits);
125
+    val |= 1 << bits;
126
+
127
+    *value = val - 1;
128
+
129
+    return 0;
130
+}
131
+
132
+static int lag_read_prob_header(lag_rac *rac, GetBitContext *gb)
133
+{
134
+    int i, j, scale_factor;
135
+    unsigned prob, cumulative_target;
136
+    unsigned cumul_prob = 0;
137
+    unsigned scaled_cumul_prob = 0;
138
+
139
+    rac->prob[0] = 0;
140
+    rac->prob[257] = UINT_MAX;
141
+    /* Read probabilities from bitstream */
142
+    for (i = 1; i < 257; i++) {
143
+        if (lag_decode_prob(gb, &rac->prob[i]) < 0) {
144
+            av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability encountered.\n");
145
+            return -1;
146
+        }
147
+        if ((uint64_t)cumul_prob + rac->prob[i] > UINT_MAX) {
148
+            av_log(rac->avctx, AV_LOG_ERROR, "Integer overflow encountered in cumulative probability calculation.\n");
149
+            return -1;
150
+        }
151
+        cumul_prob += rac->prob[i];
152
+        if (!rac->prob[i]) {
153
+            if (lag_decode_prob(gb, &prob)) {
154
+                av_log(rac->avctx, AV_LOG_ERROR, "Invalid probability run encountered.\n");
155
+                return -1;
156
+            }
157
+            if (prob > 257 - i)
158
+                prob = 257 - i;
159
+            for (j = 0; j < prob; j++)
160
+                rac->prob[++i] = 0;
161
+        }
162
+    }
163
+
164
+    if (!cumul_prob) {
165
+        av_log(rac->avctx, AV_LOG_ERROR, "All probabilities are 0!\n");
166
+        return -1;
167
+    }
168
+
169
+    /* Scale probabilities so cumulative probability is an even power of 2. */
170
+    scale_factor = av_log2(cumul_prob);
171
+
172
+    if (cumul_prob & (cumul_prob - 1)) {
173
+        uint64_t mul = softfloat_reciprocal(cumul_prob);
174
+        for (i = 1; i < 257; i++) {
175
+            rac->prob[i] = softfloat_mul(rac->prob[i], mul);
176
+            scaled_cumul_prob += rac->prob[i];
177
+        }
178
+
179
+        scale_factor++;
180
+        cumulative_target = 1 << scale_factor;
181
+
182
+        if (scaled_cumul_prob > cumulative_target) {
183
+            av_log(rac->avctx, AV_LOG_ERROR,
184
+                   "Scaled probabilities are larger than target!\n");
185
+            return -1;
186
+        }
187
+
188
+        scaled_cumul_prob = cumulative_target - scaled_cumul_prob;
189
+
190
+        for (i = 1; scaled_cumul_prob; i = (i & 0x7f) + 1) {
191
+            if (rac->prob[i]) {
192
+                rac->prob[i]++;
193
+                scaled_cumul_prob--;
194
+            }
195
+            /* Comment from reference source:
196
+             * if (b & 0x80 == 0) {     // order of operations is 'wrong'; it has been left this way
197
+             *                          // since the compression change is negligable and fixing it
198
+             *                          // breaks backwards compatibilty
199
+             *      b =- (signed int)b;
200
+             *      b &= 0xFF;
201
+             * } else {
202
+             *      b++;
203
+             *      b &= 0x7f;
204
+             * }
205
+             */
206
+        }
207
+    }
208
+
209
+    rac->scale = scale_factor;
210
+
211
+    /* Fill probability array with cumulative probability for each symbol. */
212
+    for (i = 1; i < 257; i++)
213
+        rac->prob[i] += rac->prob[i - 1];
214
+
215
+    return 0;
216
+}
217
+
218
+static void add_lag_median_prediction(uint8_t *dst, uint8_t *src1,
219
+                                      uint8_t *diff, int w, int *left,
220
+                                      int *left_top)
221
+{
222
+    /* This is almost identical to add_hfyu_median_prediction in dsputil.h.
223
+     * However the &0xFF on the gradient predictor yealds incorrect output
224
+     * for lagarith.
225
+     */
226
+    int i;
227
+    uint8_t l, lt;
228
+
229
+    l  = *left;
230
+    lt = *left_top;
231
+
232
+    for (i = 0; i < w; i++) {
233
+        l = mid_pred(l, src1[i], l + src1[i] - lt) + diff[i];
234
+        lt = src1[i];
235
+        dst[i] = l;
236
+    }
237
+
238
+    *left     = l;
239
+    *left_top = lt;
240
+}
241
+
242
+static void lag_pred_line(LagarithContext *l, uint8_t *buf,
243
+                          int width, int stride, int line)
244
+{
245
+    int L, TL;
246
+
247
+    if (!line) {
248
+        /* Left prediction only for first line */
249
+        L = l->dsp.add_hfyu_left_prediction(buf + 1, buf + 1,
250
+                                            width - 1, buf[0]);
251
+        return;
252
+    } else if (line == 1) {
253
+        /* Second line, left predict first pixel, the rest of the line is median predicted */
254
+        /* FIXME: In the case of RGB this pixel is top predicted */
255
+        TL = buf[-stride];
256
+    } else {
257
+        /* Top left is 2 rows back, last pixel */
258
+        TL = buf[width - (2 * stride) - 1];
259
+    }
260
+    /* Left pixel is actually prev_row[width] */
261
+    L = buf[width - stride - 1];
262
+
263
+    add_lag_median_prediction(buf, buf - stride, buf,
264
+                              width, &L, &TL);
265
+}
266
+
267
+static int lag_decode_line(LagarithContext *l, lag_rac *rac,
268
+                           uint8_t *dst, int width, int stride,
269
+                           int esc_count)
270
+{
271
+    int i = 0;
272
+    int ret = 0;
273
+
274
+    if (!esc_count)
275
+        esc_count = -1;
276
+
277
+    /* Output any zeros remaining from the previous run */
278
+handle_zeros:
279
+    if (l->zeros_rem) {
280
+        int count = FFMIN(l->zeros_rem, width - i);
281
+        memset(dst + i, 0, count);
282
+        i += count;
283
+        l->zeros_rem -= count;
284
+    }
285
+
286
+    while (i < width) {
287
+        dst[i] = lag_get_rac(rac);
288
+        ret++;
289
+
290
+        if (dst[i])
291
+            l->zeros = 0;
292
+        else
293
+            l->zeros++;
294
+
295
+        i++;
296
+        if (l->zeros == esc_count) {
297
+            int index = lag_get_rac(rac);
298
+            ret++;
299
+
300
+            l->zeros = 0;
301
+
302
+            l->zeros_rem = lag_calc_zero_run(index);
303
+            goto handle_zeros;
304
+        }
305
+    }
306
+    return ret;
307
+}
308
+
309
+static int lag_decode_zero_run_line(LagarithContext *l, uint8_t *dst,
310
+                                    const uint8_t *src, int width,
311
+                                    int esc_count)
312
+{
313
+    int i = 0;
314
+    int count;
315
+    uint8_t zero_run = 0;
316
+    const uint8_t *start = src;
317
+    uint8_t mask1 = -(esc_count < 2);
318
+    uint8_t mask2 = -(esc_count < 3);
319
+    uint8_t *end = dst + (width - 2);
320
+
321
+output_zeros:
322
+    if (l->zeros_rem) {
323
+        count = FFMIN(l->zeros_rem, width - i);
324
+        memset(dst, 0, count);
325
+        l->zeros_rem -= count;
326
+        dst += count;
327
+    }
328
+
329
+    while (dst < end) {
330
+        i = 0;
331
+        while (!zero_run && dst + i < end) {
332
+            i++;
333
+            zero_run =
334
+                !(src[i] | (src[i + 1] & mask1) | (src[i + 2] & mask2));
335
+        }
336
+        if (zero_run) {
337
+            zero_run = 0;
338
+            i += esc_count;
339
+            memcpy(dst, src, i);
340
+            dst += i;
341
+            l->zeros_rem = lag_calc_zero_run(src[i]);
342
+
343
+            src += i + 1;
344
+            goto output_zeros;
345
+        } else {
346
+            memcpy(dst, src, i);
347
+            src += i;
348
+        }
349
+    }
350
+    return start - src;
351
+}
352
+
353
+
354
+
355
+static int lag_decode_arith_plane(LagarithContext *l, uint8_t *dst,
356
+                                  int width, int height, int stride,
357
+                                  const uint8_t *src, int src_size)
358
+{
359
+    int i = 0;
360
+    int read = 0;
361
+    uint32_t length;
362
+    uint32_t offset = 1;
363
+    int esc_count = src[0];
364
+    GetBitContext gb;
365
+    lag_rac rac;
366
+
367
+    rac.avctx = l->avctx;
368
+    l->zeros = 0;
369
+
370
+    if (esc_count < 4) {
371
+        length = width * height;
372
+        if (esc_count && AV_RL32(src + 1) < length) {
373
+            length = AV_RL32(src + 1);
374
+            offset += 4;
375
+        }
376
+
377
+        init_get_bits(&gb, src + offset, src_size * 8);
378
+
379
+        if (lag_read_prob_header(&rac, &gb) < 0)
380
+            return -1;
381
+
382
+        lag_rac_init(&rac, &gb, length - stride);
383
+
384
+        for (i = 0; i < height; i++)
385
+            read += lag_decode_line(l, &rac, dst + (i * stride), width,
386
+                                    stride, esc_count);
387
+
388
+        if (read > length)
389
+            av_log(l->avctx, AV_LOG_WARNING,
390
+                   "Output more bytes than length (%d of %d)\n", read,
391
+                   length);
392
+    } else if (esc_count < 8) {
393
+        esc_count -= 4;
394
+        if (esc_count > 0) {
395
+            /* Zero run coding only, no range coding. */
396
+            for (i = 0; i < height; i++)
397
+                src += lag_decode_zero_run_line(l, dst + (i * stride), src,
398
+                                                width, esc_count);
399
+        } else {
400
+            /* Plane is stored uncompressed */
401
+            for (i = 0; i < height; i++) {
402
+                memcpy(dst + (i * stride), src, width);
403
+                src += width;
404
+            }
405
+        }
406
+    } else if (esc_count == 0xff) {
407
+        /* Plane is a solid run of 0 bytes */
408
+        for (i = 0; i < height; i++)
409
+            memset(dst + i * stride, 0, width);
410
+    } else {
411
+        av_log(l->avctx, AV_LOG_ERROR,
412
+               "Invalid zero run escape code! (%#x)\n", esc_count);
413
+        return -1;
414
+    }
415
+
416
+    for (i = 0; i < height; i++) {
417
+        lag_pred_line(l, dst, width, stride, i);
418
+        dst += stride;
419
+    }
420
+
421
+    return 0;
422
+}
423
+
424
+/**
425
+ * Decode a frame.
426
+ * @param avctx codec context
427
+ * @param data output AVFrame
428
+ * @param data_size size of output data or 0 if no picture is returned
429
+ * @param avpkt input packet
430
+ * @return number of consumed bytes on success or negative if decode fails
431
+ */
432
+static int lag_decode_frame(AVCodecContext *avctx,
433
+                            void *data, int *data_size, AVPacket *avpkt)
434
+{
435
+    const uint8_t *buf = avpkt->data;
436
+    int buf_size = avpkt->size;
437
+    LagarithContext *l = avctx->priv_data;
438
+    AVFrame *const p = &l->picture;
439
+    uint8_t frametype = 0;
440
+    uint32_t offset_gu = 0, offset_bv = 0, offset_ry = 9;
441
+
442
+    AVFrame *picture = data;
443
+
444
+    if (p->data[0])
445
+        avctx->release_buffer(avctx, p);
446
+
447
+    p->reference = 0;
448
+    p->key_frame = 1;
449
+
450
+    frametype = buf[0];
451
+
452
+    offset_gu = AV_RL32(buf + 1);
453
+    offset_bv = AV_RL32(buf + 5);
454
+
455
+    switch (frametype) {
456
+    case FRAME_ARITH_YV12:
457
+        avctx->pix_fmt = PIX_FMT_YUV420P;
458
+
459
+        if (avctx->get_buffer(avctx, p) < 0) {
460
+            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
461
+            return -1;
462
+        }
463
+
464
+        lag_decode_arith_plane(l, p->data[0], avctx->width, avctx->height,
465
+                               p->linesize[0], buf + offset_ry,
466
+                               buf_size);
467
+        lag_decode_arith_plane(l, p->data[2], avctx->width / 2,
468
+                               avctx->height / 2, p->linesize[2],
469
+                               buf + offset_gu, buf_size);
470
+        lag_decode_arith_plane(l, p->data[1], avctx->width / 2,
471
+                               avctx->height / 2, p->linesize[1],
472
+                               buf + offset_bv, buf_size);
473
+        break;
474
+    default:
475
+        av_log(avctx, AV_LOG_ERROR,
476
+               "Unsupported Lagarith frame type: %#x\n", frametype);
477
+        return -1;
478
+    }
479
+
480
+    *picture = *p;
481
+    *data_size = sizeof(AVFrame);
482
+
483
+    return buf_size;
484
+}
485
+
486
+static av_cold int lag_decode_init(AVCodecContext *avctx)
487
+{
488
+    LagarithContext *l = avctx->priv_data;
489
+    l->avctx = avctx;
490
+
491
+    dsputil_init(&l->dsp, avctx);
492
+
493
+    return 0;
494
+}
495
+
496
+static av_cold int lag_decode_end(AVCodecContext *avctx)
497
+{
498
+    LagarithContext *l = avctx->priv_data;
499
+
500
+    if (l->picture.data[0])
501
+        avctx->release_buffer(avctx, &l->picture);
502
+
503
+    return 0;
504
+}
505
+
506
+AVCodec lagarith_decoder = {
507
+    "lagarith",
508
+    CODEC_TYPE_VIDEO,
509
+    CODEC_ID_LAGARITH,
510
+    sizeof(LagarithContext),
511
+    lag_decode_init,
512
+    NULL,
513
+    lag_decode_end,
514
+    lag_decode_frame,
515
+    CODEC_CAP_DR1,
516
+    .long_name = NULL_IF_CONFIG_SMALL("Lagarith lossless"),
517
+};
0 518
new file mode 100644
... ...
@@ -0,0 +1,58 @@
0
+/*
1
+ * Lagarith range decoder
2
+ * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
3
+ * Copyright (c) 2009 David Conrad
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+/**
23
+ * @file libavcodec/lagarithrac.c
24
+ * Lagarith range decoder
25
+ * @author Nathan Caldwell
26
+ * @author David Conrad
27
+ */
28
+
29
+#include "get_bits.h"
30
+#include "lagarithrac.h"
31
+
32
+void lag_rac_init(lag_rac *l, GetBitContext *gb, int length)
33
+{
34
+    int i, j;
35
+
36
+    /* According to reference decoder "1st byte is garbage",
37
+     * however, it gets skipped by the call to align_get_bits()
38
+     */
39
+    align_get_bits(gb);
40
+    l->bytestream_start =
41
+    l->bytestream       = gb->buffer + get_bits_count(gb) / 8;
42
+    l->bytestream_end   = l->bytestream_start + length;
43
+
44
+    l->range        = 0x80;
45
+    l->low          = *l->bytestream >> 1;
46
+    l->hash_shift   = FFMAX(l->scale - 8, 0);
47
+
48
+    for (i = j = 0; i < 256; i++) {
49
+        unsigned r = i << l->hash_shift;
50
+        while (l->prob[j + 1] <= r)
51
+            j++;
52
+        l->range_hash[i] = j;
53
+    }
54
+
55
+    /* Add conversion factor to hash_shift so we don't have to in lag_get_rac. */
56
+    l->hash_shift += 23;
57
+}
0 58
new file mode 100644
... ...
@@ -0,0 +1,116 @@
0
+/*
1
+ * Lagarith range decoder
2
+ * Copyright (c) 2009 Nathan Caldwell <saintdev (at) gmail.com>
3
+ * Copyright (c) 2009 David Conrad
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+/**
23
+ * @file libavcodec/lagarithrac.h
24
+ * Lagarith range decoder
25
+ * @author Nathan Caldwell
26
+ * @author David Conrad
27
+ */
28
+
29
+#ifndef AVCODEC_LAGARITHRAC_H
30
+#define AVCODEC_LAGARITHRAC_H
31
+
32
+#include <stdint.h>
33
+#include "libavutil/common.h"
34
+#include "libavutil/intreadwrite.h"
35
+#include "avcodec.h"
36
+#include "get_bits.h"
37
+
38
+typedef struct lag_rac {
39
+    AVCodecContext *avctx;
40
+    unsigned low;
41
+    unsigned range;
42
+    unsigned scale;             /*!< Number of bits of precision in range. */
43
+    unsigned hash_shift;        /*!< Number of bits to shift to calculate hash for radix search. */
44
+
45
+    const uint8_t *bytestream_start;  /*!< Start of input bytestream. */
46
+    const uint8_t *bytestream;        /*!< Current position in input bytestream. */
47
+    const uint8_t *bytestream_end;    /*!< End position of input bytestream. */
48
+
49
+    uint32_t prob[258];         /*!< Table of cumulative probability for each symbol. */
50
+    uint8_t  range_hash[256];   /*!< Hash table mapping upper byte to approximate symbol. */
51
+} lag_rac;
52
+
53
+void lag_rac_init(lag_rac *l, GetBitContext *gb, int length);
54
+
55
+/* TODO: Optimize */
56
+static inline void lag_rac_refill(lag_rac *l)
57
+{
58
+    while (l->range <= 0x800000) {
59
+        l->low   <<= 8;
60
+        l->range <<= 8;
61
+        l->low |= 0xff & (AV_RB16(l->bytestream) >> 1);
62
+        if (l->bytestream < l->bytestream_end)
63
+            l->bytestream++;
64
+    }
65
+}
66
+
67
+/**
68
+ * Decode a single byte from the compressed plane described by *l.
69
+ * @param l pointer to lag_rac for the current plane
70
+ * @return next byte of decoded data
71
+ */
72
+static inline uint8_t lag_get_rac(lag_rac *l)
73
+{
74
+    unsigned range_scaled, low_scaled, div;
75
+    int val;
76
+    uint8_t shift;
77
+
78
+    lag_rac_refill(l);
79
+
80
+    range_scaled = l->range >> l->scale;
81
+
82
+    if (l->low < range_scaled * l->prob[255]) {
83
+        /* val = 0 is frequent enough to deserve a shortcut */
84
+        if (l->low < range_scaled * l->prob[1]) {
85
+            val = 0;
86
+        } else {
87
+            /* FIXME __builtin_clz is ~20% faster here, but not allowed in generic code. */
88
+            shift = 30 - av_log2(range_scaled);
89
+            div = ((range_scaled << shift) + (1 << 23) - 1) >> 23;
90
+            /* low>>24 ensures that any cases too big for exact FASTDIV are
91
+             * under- rather than over-estimated
92
+             */
93
+            low_scaled = FASTDIV(l->low - (l->low >> 24), div);
94
+            shift -= l->hash_shift;
95
+            shift &= 31;
96
+            low_scaled = (low_scaled << shift) | (low_scaled >> (32 - shift));
97
+            /* low_scaled is now a lower bound of low/range_scaled */
98
+            val = l->range_hash[(uint8_t) low_scaled];
99
+            while (l->low >= range_scaled * l->prob[val + 1])
100
+                val++;
101
+        }
102
+
103
+        l->range = range_scaled * (l->prob[val + 1] - l->prob[val]);
104
+    } else {
105
+        val = 255;
106
+        l->range -= range_scaled * l->prob[255];
107
+    }
108
+
109
+    l->low -= range_scaled * l->prob[val];
110
+
111
+    return val;
112
+}
113
+
114
+
115
+#endif /* AVCODEC_LAGARITHRAC_H */
... ...
@@ -257,6 +257,7 @@ const AVCodecTag ff_codec_bmp_tags[] = {
257 257
     { CODEC_ID_AURA2,        MKTAG('A', 'U', 'R', '2') },
258 258
     { CODEC_ID_DPX,          MKTAG('d', 'p', 'x', ' ') },
259 259
     { CODEC_ID_KGV1,         MKTAG('K', 'G', 'V', '1') },
260
+    { CODEC_ID_LAGARITH,     MKTAG('L', 'A', 'G', 'S') },
260 261
     { CODEC_ID_NONE,         0 }
261 262
 };
262 263