annotate jpegls.c @ 6372:48a6bbc1c633 libavcodec

Reduce the number of senselessly scanned bytes.
author michael
date Tue, 19 Feb 2008 15:55:14 +0000
parents ddb28de352bb
children d3a1ac3e227b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5003
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
1 /*
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
2 * JPEG-LS common code
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
3 * Copyright (c) 2003 Michael Niedermayer
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
4 * Copyright (c) 2006 Konstantin Shishkov
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
5 *
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
6 * This file is part of FFmpeg.
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
7 *
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
12 *
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
16 * Lesser General Public License for more details.
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
17 *
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
21 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
22
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
23 /**
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
24 * @file jpegls.c
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
25 * JPEG-LS common code.
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
26 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
27
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
28 #include "jpegls.h"
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
29
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
30 const uint8_t ff_log2_run[32]={
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
31 0, 0, 0, 0, 1, 1, 1, 1,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
32 2, 2, 2, 2, 3, 3, 3, 3,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
33 4, 4, 5, 5, 6, 6, 7, 7,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
34 8, 9,10,11,12,13,14,15
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
35 };
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
36
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
37 void ff_jpegls_init_state(JLSState *state){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
38 int i;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
39
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
40 state->twonear = state->near * 2 + 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
41 state->range = ((state->maxval + state->twonear - 1) / state->twonear) + 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
42
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
43 // QBPP = ceil(log2(RANGE))
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
44 for(state->qbpp = 0; (1 << state->qbpp) < state->range; state->qbpp++);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
45
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
46 if(state->bpp < 8)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
47 state->limit = 16 + 2 * state->bpp - state->qbpp;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
48 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
49 state->limit = (4 * state->bpp) - state->qbpp;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
50
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
51 for(i = 0; i < 367; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
52 state->A[i] = FFMAX((state->range + 32) >> 6, 2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
53 state->N[i] = 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
54 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
55
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
56 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
57
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
58 /**
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
59 * Custom value clipping function used in T1, T2, T3 calculation
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
60 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
61 static inline int iso_clip(int v, int vmin, int vmax){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
62 if(v > vmax || v < vmin) return vmin;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
63 else return v;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
64 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
65
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
66 void ff_jpegls_reset_coding_parameters(JLSState *s, int reset_all){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
67 const int basic_t1= 3;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
68 const int basic_t2= 7;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
69 const int basic_t3= 21;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
70 int factor;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
71
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
72 if(s->maxval==0 || reset_all) s->maxval= (1 << s->bpp) - 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
73
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
74 if(s->maxval >=128){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
75 factor= (FFMIN(s->maxval, 4095) + 128)>>8;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
76
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
77 if(s->T1==0 || reset_all)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
78 s->T1= iso_clip(factor*(basic_t1-2) + 2 + 3*s->near, s->near+1, s->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
79 if(s->T2==0 || reset_all)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
80 s->T2= iso_clip(factor*(basic_t2-3) + 3 + 5*s->near, s->T1, s->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
81 if(s->T3==0 || reset_all)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
82 s->T3= iso_clip(factor*(basic_t3-4) + 4 + 7*s->near, s->T2, s->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
83 }else{
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
84 factor= 256 / (s->maxval + 1);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
85
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
86 if(s->T1==0 || reset_all)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
87 s->T1= iso_clip(FFMAX(2, basic_t1/factor + 3*s->near), s->near+1, s->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
88 if(s->T2==0 || reset_all)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
89 s->T2= iso_clip(FFMAX(3, basic_t2/factor + 5*s->near), s->T1, s->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
90 if(s->T3==0 || reset_all)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
91 s->T3= iso_clip(FFMAX(4, basic_t3/factor + 6*s->near), s->T2, s->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
92 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
93
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
94 if(s->reset==0 || reset_all) s->reset= 64;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
95 // av_log(NULL, AV_LOG_DEBUG, "[JPEG-LS RESET] T=%i,%i,%i\n", s->T1, s->T2, s->T3);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
96 }