1 /*
2 * JPEG-LS encoder
3 * Copyright (c) 2003 Michael Niedermayer
4 * Copyright (c) 2006 Konstantin Shishkov
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * JPEG-LS encoder.
26 */
27
36
37 /**
38 * Encode error from regular symbol
39 */
41 int err)
42 {
43 int k;
45 int map;
46
47 for (k = 0; (state->
N[Q] << k) < state->
A[Q]; k++)
48 ;
49
50 map = !state->
near && !k && (2 * state->
B[Q] <= -state->
N[Q]);
51
52 if (err < 0)
54 if (err >= (state->
range + 1 >> 1)) {
56 val = 2 *
FFABS(err) - 1 - map;
57 } else
58 val = 2 * err + map;
59
61
63 }
64
65 /**
66 * Encode error from run termination
67 */
69 int RItype, int err, int limit_add)
70 {
71 int k;
73 int Q = 365 + RItype;
75
77 if (RItype)
78 temp += state->
N[Q] >> 1;
79 for (k = 0; (state->
N[Q] << k) < temp; k++)
80 ;
81 map = 0;
82 if (!k && err && (2 * state->
B[Q] < state->
N[Q]))
83 map = 1;
84
85 if (err < 0)
86 val = -(2 * err) - 1 - RItype + map;
87 else
88 val = 2 * err - RItype - map;
90
91 if (err < 0)
93 state->
A[Q] += (val + 1 - RItype) >> 1;
94
96 }
97
98 /**
99 * Encode run value as specified by JPEG-LS standard
100 */
103 {
109 }
110 /* if hit EOL, encode another full run, else encode aborted run */
111 if (!trail && run) {
113 } else if (trail) {
117 }
118 }
119
120 /**
121 * Encode one line of image
122 */
124 void *last, void *cur, int last2, int w,
126 {
127 int x = 0;
128 int Ra, Rb, Rc, Rd;
129 int D0, D1, D2;
130
131 while (x < w) {
133
134 /* compute gradients */
135 Ra = x ?
R(cur, x - stride) :
R(last, x);
137 Rc = x ?
R(last, x - stride) : last2;
138 Rd = (x >= w -
stride) ?
R(last, x) :
R(last, x + stride);
139 D0 = Rd - Rb;
140 D1 = Rb - Rc;
141 D2 = Rc - Ra;
142
143 /* run mode */
147 int RUNval, RItype,
run;
148
149 run = 0;
150 RUNval = Ra;
151 while (x < w && (
FFABS(
R(cur, x) - RUNval) <= state->
near)) {
152 run++;
155 }
157 if (x >= w)
158 return;
161 pred = RItype ? Ra : Rb;
162 err =
R(cur, x) -
pred;
163
164 if (!RItype && Ra > Rb)
165 err = -err;
166
168 if (err > 0)
170 else
172
173 if (RItype || (Rb >= Ra))
175 else
178 }
179 if (err < 0)
181 if (err >= state->
range + 1 >> 1)
183
186
189 } else { /* regular mode */
190 int context;
191
195 pred =
mid_pred(Ra, Ra + Rb - Rc, Rb);
196
197 if (context < 0) {
198 context = -context;
199 sign = 1;
200 pred = av_clip(pred - state->
C[context], 0, state->
maxval);
201 err = pred -
R(cur, x);
202 } else {
203 sign = 0;
204 pred = av_clip(pred + state->
C[context], 0, state->
maxval);
205 err =
R(cur, x) -
pred;
206 }
207
209 if (err > 0)
211 else
213 if (!sign)
215 else
218 }
219
221 }
223 }
224 }
225
227 {
228 /* Test if we have default params and don't need to store LSE */
233 if (state->
T1 == state2.
T1 &&
234 state->
T2 == state2.
T2 &&
235 state->
T3 == state2.
T3 &&
237 return;
238 /* store LSE type 1 */
247 }
248
250 const AVFrame *pict,
int *got_packet)
251 {
260 int comps;
261
262 *p = *pict;
265
268 comps = 1;
269 else
270 comps = 3;
271
275
277
280
281 /* write our own JPEG header, can't use mjpeg_picture_header */
284 put_bits(&pb, 16, 8 + comps * 3);
// header size depends on components
288 put_bits(&pb, 8, comps);
// components
289 for (i = 1; i <= comps; i++) {
290 put_bits(&pb, 8, i);
// component ID
291 put_bits(&pb, 8, 0x11);
// subsampling: none
292 put_bits(&pb, 8, 0);
// Tiq, used by JPEG-LS ext
293 }
294
298 for (i = 1; i <= comps; i++) {
299 put_bits(&pb, 8, i);
// component ID
300 put_bits(&pb, 8, 0);
// mapping index: none
301 }
303 put_bits(&pb, 8, (comps > 1) ? 1 : 0);
// interleaving: 0 - plane, 1 - line
304 put_bits(&pb, 8, 0);
// point transform: none
305
307 /* initialize JPEG-LS state from JPEG parameters */
312
314
316 if (!zero) {
319 }
324
325 for (i = 0; i < avctx->
height; i++) {
327 t = last[0];
328 last = cur;
330 }
333
334 for (i = 0; i < avctx->
height; i++) {
336 t = *((uint16_t *)last);
337 last = cur;
339 }
342 int Rc[3] = { 0, 0, 0 };
343
344 width = avctx->
width * 3;
345 for (i = 0; i < avctx->
height; i++) {
346 for (j = 0; j < 3; j++) {
348 width, 3, j, 8);
349 Rc[j] = last[j];
350 }
351 last = cur;
353 }
356 int Rc[3] = { 0, 0, 0 };
357
358 width = avctx->
width * 3;
359 for (i = 0; i < avctx->
height; i++) {
360 for (j = 2; j >= 0; j--) {
362 width, 3, j, 8);
363 Rc[j] = last[j];
364 }
365 last = cur;
367 }
368 }
369
372
373 /* the specification says that after doing 0xff escaping unused bits in
374 * the last byte must be set to 0, so just append 7 "optional" zero-bits
375 * to avoid special-casing. */
379 /* do escape coding */
381 size -= 7;
386 if (v == 0xFF) {
389 }
390 }
393
394 /* End of image */
397
399
402 *got_packet = 1;
403 return 0;
404 }
405
407 {
409
412
418 "Only grayscale and RGB24/BGR24 images are supported\n");
419 return -1;
420 }
421 return 0;
422 }
423
436 },
437 };