2 * JPEG-LS encoder and decoder
3 * Copyright (c) 2003 Michael Niedermayer
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * JPEG-LS encoder and decoder.
28 static inline int quantize(MJpegDecodeContext *s, int v){ //FIXME optimize
31 if (v >-s->t1) return -1;
32 else if(v >-s->t2) return -2;
33 else if(v >-s->t3) return -3;
36 if (v < s->t1) return 1;
37 else if(v < s->t2) return 2;
38 else if(v < s->t3) return 3;
43 static inline int predict8(uint8_t *src, uint8_t *last){ //FIXME perhaps its better to suppress these 2
44 const int LT= last[-1];
45 const int T= last[ 0];
46 const int L = src[-1];
48 return mid_pred(L, L + T - LT, T);
51 static inline int predict16(uint16_t *src, uint16_t *last){
52 const int LT= last[-1];
53 const int T= last[ 0];
54 const int L = src[-1];
56 return mid_pred(L, L + T - LT, T);
59 static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
63 static int iso_clip(int v, int vmin, int vmax){
64 if(v > vmax || v < vmin) return vmin;
68 static void reset_ls_coding_parameters(MJpegDecodeContext *s, int reset_all){
69 const int basic_t1= 3;
70 const int basic_t2= 7;
71 const int basic_t3= 21;
74 if(s->maxval==0 || reset_all) s->maxval= (1<<s->bits) - 1;
77 factor= (FFMIN(s->maxval, 4096) + 128)>>8;
79 if(s->t1==0 || reset_all)
80 s->t1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
81 if(s->t2==0 || reset_all)
82 s->t2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->t1, s->maxval);
83 if(s->t3==0 || reset_all)
84 s->t3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->t2, s->maxval);
86 factor= 256 / (s->maxval + 1);
88 if(s->t1==0 || reset_all)
89 s->t1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
90 if(s->t2==0 || reset_all)
91 s->t2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->t1, s->maxval);
92 if(s->t3==0 || reset_all)
93 s->t3= iso_clip(FFMAX(4, basic_t3/factor + 6*s->near), s->t2, s->maxval);
96 if(s->reset==0 || reset_all) s->reset= 64;
99 static int decode_lse(MJpegDecodeContext *s)
103 /* XXX: verify len field validity */
104 len = get_bits(&s->gb, 16);
105 id = get_bits(&s->gb, 8);
109 s->maxval= get_bits(&s->gb, 16);
110 s->t1= get_bits(&s->gb, 16);
111 s->t2= get_bits(&s->gb, 16);
112 s->t3= get_bits(&s->gb, 16);
113 s->reset= get_bits(&s->gb, 16);
115 reset_ls_coding_parameters(s, 0);
120 printf("palette not supported\n");
123 printf("oversize image not supported\n");
126 printf("invalid id %d\n", id);
133 static inline void update_vlc_state(VlcState * const state, const int v, int half_count){
134 int drift= state->drift;
135 int count= state->count;
136 state->error_sum += ABS(v);
139 if(count == half_count){
142 state->error_sum >>= 1;
147 if(state->bias > -128) state->bias--;
153 if(state->bias < 127) state->bias++;
164 #define R(p, i) (is_uint8 ? (((uint8_t*)p)[i] : ((uint16_t*)p)[i])
166 static inline int ls_decode_line(MJpegDecodeContext *s, void *lastv, void *dstv, int last2,
167 int w, int point_transform, int is_uint8){
170 for(x=0; x < w; x++){
182 if(x<w-1) rt= R(last, x+1);
189 context= quantize(s, v_gradient) + 9*(quantize(s, hl_gradient) + 9*quantize(s, hr_gradient));
192 int pred= mid_pred(l, l + t - lt, t);
197 pred= clip(0, pred - state->bias, maxval);
200 pred= clip(0, pred + state->bias, maxval);
205 while(i < state->error_sum){ //FIXME optimize
210 v= get_ur_golomb_jpegls(gb, k, LIMIT-qbpp, qbpp);
216 if(k==0 && 2*state->drift <= - state->count) v ^= (-1);
218 v ^= (k==0 && 2*state->drift <= - state->count);
224 update_vlc_state(state, v, half_count);
228 if(is_uint8) ((uint8_t *)dst)[x]= (pred + v) & maxval;
229 else ((uint16_t*)dst)[x]= (pred + v) & maxval;
233 while(get_bits1(&s->gb)){
234 run_count = 1<<log2_run[run_index];
235 if(x + run_count > w) run_count= w - x;
238 for(; run_count; run_count--){
239 if(is_uint8) ((uint8_t *)dst)[x++]= l;
240 else ((uint16_t*)dst)[x++]= l;
246 run_count= get_bits(&s->gb, log2_run[run_index]);
248 for(; run_count; run_count--){
249 if(is_uint8) ((uint8_t *)dst)[x++]= l;
250 else ((uint16_t*)dst)[x++]= l;
253 if(run_index) run_index--;
262 temp= state->error_sum + (state->count>>1);
265 temp= state->error_sum;
273 while(i < temp){ //FIXME optimize
279 map = (k==0 && 2*Nn < state->count) == (Errval>0);
282 if(run_count==0 && run_mode==1){
283 if(get_bits1(&s->gb)){
284 run_count = 1<<log2_run[run_index];
285 if(x + run_count <= w) run_index++;
287 if(log2_run[run_index]) run_count = get_bits(&s->gb, log2_run[run_index]);
289 if(run_index) run_index--;
297 diff= get_vlc_symbol(&s->gb, &p->vlc_state[context]);
305 /* if (s->restart_interval && !s->restart_count)
306 s->restart_count = s->restart_interval;*/
308 if(mb_x==0 || mb_y==0 || s->interlaced){
309 for(i=0;i<nb_components;i++) {
311 int n, h, v, x, y, c, j, linesize;
313 c = s->comp_index[i];
318 linesize= s->linesize[c];
323 ptr = s->current_picture[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
326 pred= 128 << point_transform;
332 pred= ptr[-linesize];
334 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
338 if (s->interlaced && s->bottom_field)
339 ptr += linesize >> 1;
340 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
349 for(i=0;i<nb_components;i++) {
351 int n, h, v, x, y, c, j, linesize;
353 c = s->comp_index[i];
358 linesize= s->linesize[c];
363 ptr = s->current_picture[c] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
364 PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
365 *ptr= pred + (mjpeg_decode_dc(s, s->dc_index[i]) << point_transform);
373 if (s->restart_interval && !--s->restart_count) {
374 align_get_bits(&s->gb);
375 skip_bits(&s->gb, 16); /* skip RSTn */
381 #ifdef CONFIG_ENCODERS
382 AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless jpeg shouldnt need them
386 sizeof(MpegEncContext),