summaryrefslogtreecommitdiffstats
path: root/libavcodec/jpeg_ls.c
blob: 4f059361741f6159f2974d153422b0a528e0eda4 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
97
98
99
100
101
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
131
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
246
247
248
249
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
284
285
286
287
288
289
290
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
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
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
/*
 * JPEG-LS encoder and decoder
 * Copyright (c) 2003 Michael Niedermayer
 *
 * This library 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 of the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file jpeg_ls.c
 * JPEG-LS encoder and decoder.
 */

#undef printf
#undef fprintf

static inline int quantize(MJpegDecodeContext *s, int v){ //FIXME optimize
    if(v==0) return 0;
    if(v < 0){
        if     (v >-s->t1) return -1;
        else if(v >-s->t2) return -2;
        else if(v >-s->t3) return -3;
        else               return -4;
    }else{
        if     (v < s->t1) return 1;
        else if(v < s->t2) return 2;
        else if(v < s->t3) return 3;
        else               return 4;
    }
}

static inline int predict8(uint8_t *src, uint8_t *last){ //FIXME perhaps its better to suppress these 2
    const int LT= last[-1];
    const int  T= last[ 0];
    const int L =  src[-1];

    return mid_pred(L, L + T - LT, T);
}

static inline int predict16(uint16_t *src, uint16_t *last){
    const int LT= last[-1];
    const int  T= last[ 0];
    const int L =  src[-1];

    return mid_pred(L, L + T - LT, T);
}

static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
    return 0;
}

static int iso_clip(int v, int vmin, int vmax){
    if(v > vmax || v < vmin) return vmin;
    else                     return v;
}

static void reset_ls_coding_parameters(MJpegDecodeContext *s, int reset_all){
    const int basic_t1= 3;
    const int basic_t2= 7;
    const int basic_t3= 21;
    int factor;

    if(s->maxval==0 || reset_all) s->maxval= (1<<s->bits) - 1;
    
    if(s->maxval >=128){
        factor= (FFMIN(s->maxval, 4096) + 128)>>8;

        if(s->t1==0     || reset_all)
            s->t1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
        if(s->t2==0     || reset_all)
            s->t2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->t1, s->maxval);
        if(s->t3==0     || reset_all)
            s->t3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->t2, s->maxval);
    }else{
        factor= 256 / (s->maxval + 1);

        if(s->t1==0     || reset_all)
            s->t1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
        if(s->t2==0     || reset_all)
            s->t2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->t1, s->maxval);
        if(s->t3==0     || reset_all)
            s->t3= iso_clip(FFMAX(4, basic_t3/factor + 6*s->near), s->t2, s->maxval);
    }

    if(s->reset==0  || reset_all) s->reset= 64;
}

static int decode_lse(MJpegDecodeContext *s)
{
    int len, id;

    /* XXX: verify len field validity */
    len = get_bits(&s->gb, 16);
    id = get_bits(&s->gb, 8);
    
    switch(id){
    case 1:
        s->maxval= get_bits(&s->gb, 16);
        s->t1= get_bits(&s->gb, 16);
        s->t2= get_bits(&s->gb, 16);
        s->t3= get_bits(&s->gb, 16);
        s->reset= get_bits(&s->gb, 16);
        
        reset_ls_coding_parameters(s, 0);
        //FIXME quant table?
    break;
    case 2:
    case 3:
        printf("palette not supported\n");
        return -1;
    case 4:
        printf("oversize image not supported\n");
        return -1;
    default:
        printf("invalid id %d\n", id);
        return -1;
    }

    return 0;
}
#if 0
static inline void update_vlc_state(VlcState * const state, const int v, int half_count){
    int drift= state->drift;
    int count= state->count;
    state->error_sum += ABS(v);
    drift += v;

    if(count == half_count){
        count >>= 1;
        drift >>= 1;
        state->error_sum >>= 1;
    }
    count++;

    if(drift <= -count){
        if(state->bias > -128) state->bias--;
        
        drift += count;
        if(drift <= -count)
            drift= -count + 1;
    }else if(drift > 0){
        if(state->bias <  127) state->bias++;
        
        drift -= count;
        if(drift > 0) 
            drift= 0;
    }

    state->drift= drift;
    state->count= count;
}

#define R(p, i) (is_uint8 ? (((uint8_t*)p)[i] : ((uint16_t*)p)[i])

static inline int ls_decode_line(MJpegDecodeContext *s, void *lastv, void *dstv, int last2,
                                 int w, int point_transform, int is_uint8){
    int i, x, y;

    for(x=0; x < w; x++){
        int l, t, lt, rt;
    
        t= R(last, 0);
        if(x){
            l = t;
            lt= last2;
        }else{
            l = R(dst, x-1);
            lt= R(last, x-1);
        }

        if(x<w-1) rt= R(last, x+1);
        else      rt= t;
        
        hr_gradient= rt - t;
        hl_gradient= t - lt;
         v_gradient= lt - l;
            
        context= quantize(s, v_gradient) + 9*(quantize(s, hl_gradient) + 9*quantize(s, hr_gradient));

        if(context){
            int pred= mid_pred(l, l + t - lt, t);

            if(context < 0){
                context= -context;
                sign= 1;
                pred= clip(0, pred - state->bias, maxval);
            }else{
                sign= 0;
                pred= clip(0, pred + state->bias, maxval);
            }

            i= state->count;
            k=0;
            while(i < state->error_sum){ //FIXME optimize
                k++;
                i += i;
            }
            
            v= get_ur_golomb_jpegls(gb, k, LIMIT-qbpp, qbpp);
#if 1
    v++;
    if(v&1) v=  (v>>1);
    else    v= -(v>>1);

    if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
#else
    v ^= (k==0 && 2*state->drift <= - state->count);
    v++;
    if(v&1) v=  (v>>1);
    else    v= -(v>>1);

#endif
            update_vlc_state(state, v, half_count);
            
            if(sign) v= -v;
            
            if(is_uint8) ((uint8_t *)dst)[x]= (pred + v) & maxval;
            else         ((uint16_t*)dst)[x]= (pred + v) & maxval;
        }else{
            int run_count;

            while(get_bits1(&s->gb)){
                run_count = 1<<log2_run[run_index];
                if(x + run_count > w) run_count= w - x;
                else                  run_index++;
                
                for(; run_count; run_count--){
                    if(is_uint8) ((uint8_t *)dst)[x++]= l;
                    else         ((uint16_t*)dst)[x++]= l;
                }
                
                if(x >= w) return 0; 
            }
            
            run_count= get_bits(&s->gb, log2_run[run_index]);

            for(; run_count; run_count--){
                if(is_uint8) ((uint8_t *)dst)[x++]= l;
                else         ((uint16_t*)dst)[x++]= l;
            }
            
            if(run_index) run_index--;
            
            if(x >= w) return 0;

            t= R(last, 0);
            
            RItype= (l==t);
            if(l==t){
                state= 366;
                temp= state->error_sum + (state->count>>1);
            }else{
                state= 365;
                temp= state->error_sum;
            }
            
            pred= t;
            sign= l > t;
            
            i= state->count;
            k=0;
            while(i < temp){ //FIXME optimize
                k++;
                i += i;
            }
            
            assert(Errval != 0);
            map = (k==0 && 2*Nn < state->count) == (Errval>0); 
            
            
                    if(run_count==0 && run_mode==1){
                        if(get_bits1(&s->gb)){
                            run_count = 1<<log2_run[run_index];
                            if(x + run_count <= w) run_index++;
                        }else{
                            if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
                            else run_count=0;
                            if(run_index) run_index--;
                            run_mode=2;
                        }
                    }
                    run_count--;
                    if(run_count < 0){
                        run_mode=0;
                        run_count=0;
                        diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]);
                        if(diff>=0) diff++;
                    }else
                        diff=0;
        
        }
    }

/*                if (s->restart_interval && !s->restart_count)
                    s->restart_count = s->restart_interval;*/

            if(mb_x==0 || mb_y==0 || s->interlaced){
                for(i=0;i<nb_components;i++) {
                    uint8_t *ptr;
                    int n, h, v, x, y, c, j, linesize;
                    n = s->nb_blocks[i];
                    c = s->comp_index[i];
                    h = s->h_scount[i];
                    v = s->v_scount[i];
                    x = 0;
                    y = 0;
                    linesize= s->linesize[c];
                    
                    for(j=0; j<n; j++) {
                        int pred;

                        ptr = s->current_picture[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
                        if(y==0 && mb_y==0){
                            if(x==0 && mb_x==0){
                                pred= 128 << point_transform;
                            }else{
                                pred= ptr[-1];
                            }
                        }else{
                            if(x==0 && mb_x==0){
                                pred= ptr[-linesize];
                            }else{
                                PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
                            }
                        }
                        
                        if (s->interlaced && s->bottom_field)
                            ptr += linesize >> 1;
                        *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);

                        if (++x == h) {
                            x = 0;
                            y++;
                        }
                    }
                }
            }else{
                for(i=0;i<nb_components;i++) {
                    uint8_t *ptr;
                    int n, h, v, x, y, c, j, linesize;
                    n = s->nb_blocks[i];
                    c = s->comp_index[i];
                    h = s->h_scount[i];
                    v = s->v_scount[i];
                    x = 0;
                    y = 0;
                    linesize= s->linesize[c];
                    
                    for(j=0; j<n; j++) {
                        int pred;

                        ptr = s->current_picture[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
                        PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
                        *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
                        if (++x == h) {
                            x = 0;
                            y++;
                        }
                    }
                }
            }
            if (s->restart_interval && !--s->restart_count) {
                align_get_bits(&s->gb);
                skip_bits(&s->gb, 16); /* skip RSTn */
            }
    return 0;
}
#endif

#ifdef CONFIG_ENCODERS
AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
    "jpegls",
    CODEC_TYPE_VIDEO,
    CODEC_ID_JPEGLS,
    sizeof(MpegEncContext),
    MPV_encode_init,
    encode_picture_ls,
    MPV_encode_end,
};
#endif
OpenPOWER on IntegriCloud