annotate jpeglsenc.c @ 7610:c15e728fd413 libavcodec

Mark init_vlcs function as av_cold.
author diego
date Tue, 19 Aug 2008 08:55:40 +0000
parents e943e1409077
children d6bab465b82c
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 encoder
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 jpeglsenc.c
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
25 * JPEG-LS encoder.
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 "avcodec.h"
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
29 #include "bitstream.h"
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
30 #include "golomb.h"
5045
f0e079daad30 move MJpegDecodeContext declaration from mjpeg.h to mjpegdec.h
aurel
parents: 5003
diff changeset
31 #include "dsputil.h"
5003
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
32 #include "mjpeg.h"
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
33 #include "jpegls.h"
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
34
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 * Encode error from regular symbol
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
38 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
39 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
40 int k;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
41 int val;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
42 int map;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
43
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
44 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
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 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
47
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
48 if(err < 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
49 err += state->range;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
50 if(err >= ((state->range + 1) >> 1)) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
51 err -= state->range;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
52 val = 2 * FFABS(err) - 1 - map;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
53 } else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
54 val = 2 * err + map;
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 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
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 ff_jpegls_update_state_regular(state, Q, err);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
59 }
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 /**
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
62 * Encode error from run termination
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
63 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
64 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
65 int k;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
66 int val, map;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
67 int Q = 365 + RItype;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
68 int temp;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
69
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
70 temp = state->A[Q];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
71 if(RItype)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
72 temp += state->N[Q] >> 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
73 for(k = 0; (state->N[Q] << k) < temp; k++);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
74 map = 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
75 if(!k && err && (2 * state->B[Q] < state->N[Q]))
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
76 map = 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
77
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
78 if(err < 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
79 val = - (2 * err) - 1 - RItype + map;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
80 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
81 val = 2 * err - RItype - map;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
82 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
83
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
84 if(err < 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
85 state->B[Q]++;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
86 state->A[Q] += (val + 1 - RItype) >> 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
87
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
88 ff_jpegls_downscale_state(state, Q);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
89 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
90
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
91 /**
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
92 * Encode run value as specified by JPEG-LS standard
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 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
95 while(run >= (1 << ff_log2_run[state->run_index[comp]])){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
96 put_bits(pb, 1, 1);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
97 run -= 1 << ff_log2_run[state->run_index[comp]];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
98 if(state->run_index[comp] < 31)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
99 state->run_index[comp]++;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
100 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
101 /* if hit EOL, encode another full run, else encode aborted run */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
102 if(!trail && run) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
103 put_bits(pb, 1, 1);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
104 }else if(trail){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
105 put_bits(pb, 1, 0);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
106 if(ff_log2_run[state->run_index[comp]])
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
107 put_bits(pb, ff_log2_run[state->run_index[comp]], run);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
108 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
109 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
110
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
111 /**
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
112 * Encode one line of image
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
113 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
114 static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
115 int x = 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
116 int Ra, Rb, Rc, Rd;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
117 int D0, D1, D2;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
118
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
119 while(x < w) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
120 int err, pred, sign;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
121
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
122 /* compute gradients */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
123 Ra = x ? R(cur, x - stride) : R(last, x);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
124 Rb = R(last, x);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
125 Rc = x ? R(last, x - stride) : last2;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
126 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
127 D0 = Rd - Rb;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
128 D1 = Rb - Rc;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
129 D2 = Rc - Ra;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
130
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
131 /* run mode */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
132 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
133 int RUNval, RItype, run;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
134
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
135 run = 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
136 RUNval = Ra;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
137 while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
138 run++;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
139 W(cur, x, Ra);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
140 x += stride;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
141 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
142 ls_encode_run(state, pb, run, comp, x < w);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
143 if(x >= w)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
144 return;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
145 Rb = R(last, x);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
146 RItype = (FFABS(Ra - Rb) <= state->near);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
147 pred = RItype ? Ra : Rb;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
148 err = R(cur, x) - pred;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
149
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
150 if(!RItype && Ra > Rb)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
151 err = -err;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
152
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
153 if(state->near){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
154 if(err > 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
155 err = (state->near + err) / state->twonear;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
156 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
157 err = -(state->near - err) / state->twonear;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
158
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
159 if(RItype || (Rb >= Ra))
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
160 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
161 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
162 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
163 W(cur, x, Ra);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
164 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
165 if(err < 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
166 err += state->range;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
167 if(err >= ((state->range + 1) >> 1))
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
168 err -= state->range;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
169
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
170 ls_encode_runterm(state, pb, RItype, err, ff_log2_run[state->run_index[comp]]);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
171
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
172 if(state->run_index[comp] > 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
173 state->run_index[comp]--;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
174 } else { /* regular mode */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
175 int context;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
176
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
177 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
178 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
179
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
180 if(context < 0){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
181 context = -context;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
182 sign = 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
183 pred = av_clip(pred - state->C[context], 0, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
184 err = pred - R(cur, x);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
185 }else{
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
186 sign = 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
187 pred = av_clip(pred + state->C[context], 0, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
188 err = R(cur, x) - pred;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
189 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
190
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
191 if(state->near){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
192 if(err > 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
193 err = (state->near + err) / state->twonear;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
194 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
195 err = -(state->near - err) / state->twonear;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
196 if(!sign)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
197 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
198 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
199 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
200 W(cur, x, Ra);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
201 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
202
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
203 ls_encode_regular(state, pb, context, err);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
204 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
205 x += stride;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
206 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
207 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
208
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
209 static void ls_store_lse(JLSState *state, PutBitContext *pb){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
210 /* Test if we have default params and don't need to store LSE */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
211 JLSState state2;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
212 memset(&state2, 0, sizeof(JLSState));
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
213 state2.bpp = state->bpp;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
214 state2.near = state->near;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
215 ff_jpegls_reset_coding_parameters(&state2, 1);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
216 if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
217 return;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
218 /* store LSE type 1 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
219 put_marker(pb, LSE);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
220 put_bits(pb, 16, 13);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
221 put_bits(pb, 8, 1);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
222 put_bits(pb, 16, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
223 put_bits(pb, 16, state->T1);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
224 put_bits(pb, 16, state->T2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
225 put_bits(pb, 16, state->T3);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
226 put_bits(pb, 16, state->reset);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
227 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
228
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
229 static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
230 JpeglsContext * const s = avctx->priv_data;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
231 AVFrame *pict = data;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
232 AVFrame * const p= (AVFrame*)&s->picture;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
233 const int near = avctx->prediction_method;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
234 PutBitContext pb, pb2;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
235 GetBitContext gb;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
236 uint8_t *buf2, *zero, *cur, *last;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
237 JLSState *state;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
238 int i, size;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
239 int comps;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
240
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
241 buf2 = av_malloc(buf_size);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
242
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
243 init_put_bits(&pb, buf, buf_size);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
244 init_put_bits(&pb2, buf2, buf_size);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
245
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
246 *p = *pict;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
247 p->pict_type= FF_I_TYPE;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
248 p->key_frame= 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
249
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
250 if(avctx->pix_fmt == PIX_FMT_GRAY8 || avctx->pix_fmt == PIX_FMT_GRAY16)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
251 comps = 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
252 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
253 comps = 3;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
254
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
255 /* write our own JPEG header, can't use mjpeg_picture_header */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
256 put_marker(&pb, SOI);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
257 put_marker(&pb, SOF48);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
258 put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
259 put_bits(&pb, 8, (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8); // bpp
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
260 put_bits(&pb, 16, avctx->height);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
261 put_bits(&pb, 16, avctx->width);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
262 put_bits(&pb, 8, comps); // components
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
263 for(i = 1; i <= comps; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
264 put_bits(&pb, 8, i); // component ID
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
265 put_bits(&pb, 8, 0x11); // subsampling: none
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
266 put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
267 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
268
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
269 put_marker(&pb, SOS);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
270 put_bits(&pb, 16, 6 + comps * 2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
271 put_bits(&pb, 8, comps);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
272 for(i = 1; i <= comps; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
273 put_bits(&pb, 8, i); // component ID
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
274 put_bits(&pb, 8, 0); // mapping index: none
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
275 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
276 put_bits(&pb, 8, near);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
277 put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
278 put_bits(&pb, 8, 0); // point transform: none
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
279
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
280 state = av_mallocz(sizeof(JLSState));
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
281 /* initialize JPEG-LS state from JPEG parameters */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
282 state->near = near;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
283 state->bpp = (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
284 ff_jpegls_reset_coding_parameters(state, 0);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
285 ff_jpegls_init_state(state);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
286
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
287 ls_store_lse(state, &pb);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
288
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
289 zero = av_mallocz(p->linesize[0]);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
290 last = zero;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
291 cur = p->data[0];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
292 if(avctx->pix_fmt == PIX_FMT_GRAY8){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
293 int t = 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
294
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
295 for(i = 0; i < avctx->height; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
296 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
297 t = last[0];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
298 last = cur;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
299 cur += p->linesize[0];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
300 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
301 }else if(avctx->pix_fmt == PIX_FMT_GRAY16){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
302 int t = 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
303
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
304 for(i = 0; i < avctx->height; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
305 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
306 t = *((uint16_t*)last);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
307 last = cur;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
308 cur += p->linesize[0];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
309 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
310 }else if(avctx->pix_fmt == PIX_FMT_RGB24){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
311 int j, width;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
312 int Rc[3] = {0, 0, 0};
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
313
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
314 width = avctx->width * 3;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
315 for(i = 0; i < avctx->height; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
316 for(j = 0; j < 3; j++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
317 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
318 Rc[j] = last[j];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
319 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
320 last = cur;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
321 cur += s->picture.linesize[0];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
322 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
323 }else if(avctx->pix_fmt == PIX_FMT_BGR24){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
324 int j, width;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
325 int Rc[3] = {0, 0, 0};
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
326
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
327 width = avctx->width * 3;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
328 for(i = 0; i < avctx->height; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
329 for(j = 2; j >= 0; j--) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
330 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
331 Rc[j] = last[j];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
332 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
333 last = cur;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
334 cur += s->picture.linesize[0];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
335 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
336 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
337
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
338 av_free(zero);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
339 av_free(state);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
340
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
341 // the specification says that after doing 0xff escaping unused bits in the
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
342 // last byte must be set to 0, so just append 7 "optional" zero-bits to
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
343 // avoid special-casing.
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
344 put_bits(&pb2, 7, 0);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
345 size = put_bits_count(&pb2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
346 flush_put_bits(&pb2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
347 /* do escape coding */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
348 init_get_bits(&gb, buf2, size);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
349 size -= 7;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
350 while(get_bits_count(&gb) < size){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
351 int v;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
352 v = get_bits(&gb, 8);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
353 put_bits(&pb, 8, v);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
354 if(v == 0xFF){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
355 v = get_bits(&gb, 7);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
356 put_bits(&pb, 8, v);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
357 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
358 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
359 align_put_bits(&pb);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
360 av_free(buf2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
361
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
362 /* End of image */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
363 put_marker(&pb, EOI);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
364 flush_put_bits(&pb);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
365
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
366 emms_c();
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
367
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
368 return put_bits_count(&pb) >> 3;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
369 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
370
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 5127
diff changeset
371 static av_cold int encode_init_ls(AVCodecContext *ctx) {
5003
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
372 JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
373
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
374 c->avctx = ctx;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
375 ctx->coded_frame = &c->picture;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
376
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
377 if(ctx->pix_fmt != PIX_FMT_GRAY8 && ctx->pix_fmt != PIX_FMT_GRAY16 && ctx->pix_fmt != PIX_FMT_RGB24 && ctx->pix_fmt != PIX_FMT_BGR24){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
378 av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
379 return -1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
380 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
381 return 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
382 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
383
5127
4dbe6578f811 misc spelling fixes
diego
parents: 5045
diff changeset
384 AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
5003
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
385 "jpegls",
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
386 CODEC_TYPE_VIDEO,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
387 CODEC_ID_JPEGLS,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
388 sizeof(JpeglsContext),
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
389 encode_init_ls,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
390 encode_picture_ls,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
391 NULL,
6788
e1302edb0f69 Replace some occurrences of -1 with PIX_FMT_NONE.
cehoyos
parents: 6712
diff changeset
392 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, PIX_FMT_NONE},
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6788
diff changeset
393 .long_name= NULL_IF_CONFIG_SMALL("JPEG-LS"),
5003
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
394 };