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
28 #define UNCHECKED_BITSTREAM_READER 1
29 #include "libavutil/opt.h"
30 #include "avcodec.h"
31 #include "bytestream.h"
32 #include "codec_internal.h"
33 #include "encode.h"
34 #include "get_bits.h"
35 #include "put_bits.h"
36 #include "put_golomb.h"
37 #include "mathops.h"
38 #include "mjpeg.h"
39 #include "jpegls.h"
40
41 typedef struct JPEGLSContext {
42 AVClass *class;
43
44 int pred;
45 int comps;
46
47 size_t size;
48 uint8_t *buf;
49 } JPEGLSContext;
50
put_marker_byteu(PutByteContext *pb, enum JpegMarker code)51 static inline void put_marker_byteu(PutByteContext *pb, enum JpegMarker code)
52 {
53 bytestream2_put_byteu(pb, 0xff);
54 bytestream2_put_byteu(pb, code);
55 }
56
57 /**
58 * Encode error from regular symbol
59 */
ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err)60 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q,
61 int err)
62 {
63 int k;
64 int val;
65 int map;
66
67 for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
68 ;
69
70 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
71
72 if (err < 0)
73 err += state->range;
74 if (err >= (state->range + 1 >> 1)) {
75 err -= state->range;
76 val = 2 * FFABS(err) - 1 - map;
77 } else
78 val = 2 * err + map;
79
80 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
81
82 ff_jpegls_update_state_regular(state, Q, err);
83 }
84
85 /**
86 * Encode error from run termination
87 */
ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add)88 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb,
89 int RItype, int err, int limit_add)
90 {
91 int k;
92 int val, map;
93 int Q = 365 + RItype;
94 int temp;
95
96 temp = state->A[Q];
97 if (RItype)
98 temp += state->N[Q] >> 1;
99 for (k = 0; (state->N[Q] << k) < temp; k++)
100 ;
101 map = 0;
102 if (!k && err && (2 * state->B[Q] < state->N[Q]))
103 map = 1;
104
105 if (err < 0)
106 val = -(2 * err) - 1 - RItype + map;
107 else
108 val = 2 * err - RItype - map;
109 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
110
111 if (err < 0)
112 state->B[Q]++;
113 state->A[Q] += (val + 1 - RItype) >> 1;
114
115 ff_jpegls_downscale_state(state, Q);
116 }
117
118 /**
119 * Encode run value as specified by JPEG-LS standard
120 */
ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail)121 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run,
122 int comp, int trail)
123 {
124 while (run >= (1 << ff_log2_run[state->run_index[comp]])) {
125 put_bits(pb, 1, 1);
126 run -= 1 << ff_log2_run[state->run_index[comp]];
127 if (state->run_index[comp] < 31)
128 state->run_index[comp]++;
129 }
130 /* if hit EOL, encode another full run, else encode aborted run */
131 if (!trail && run) {
132 put_bits(pb, 1, 1);
133 } else if (trail) {
134 put_bits(pb, 1, 0);
135 if (ff_log2_run[state->run_index[comp]])
136 put_bits(pb, ff_log2_run[state->run_index[comp]], run);
137 }
138 }
139
140 /**
141 * Encode one line of image
142 */
ls_encode_line(JLSState *state, PutBitContext *pb, void *tmp, const void *in, int last2, int w, int stride, int comp, int bits)143 static inline void ls_encode_line(JLSState *state, PutBitContext *pb,
144 void *tmp, const void *in, int last2, int w,
145 int stride, int comp, int bits)
146 {
147 int x = 0;
148 int Ra = R(tmp, 0), Rb, Rc = last2, Rd;
149 int D0, D1, D2;
150
151 while (x < w) {
152 int err, pred, sign;
153
154 /* compute gradients */
155 Rb = R(tmp, x);
156 Rd = (x >= w - stride) ? R(tmp, x) : R(tmp, x + stride);
157 D0 = Rd - Rb;
158 D1 = Rb - Rc;
159 D2 = Rc - Ra;
160
161 /* run mode */
162 if ((FFABS(D0) <= state->near) &&
163 (FFABS(D1) <= state->near) &&
164 (FFABS(D2) <= state->near)) {
165 int RUNval, RItype, run;
166
167 run = 0;
168 RUNval = Ra;
169 while (x < w && (FFABS(R(in, x) - RUNval) <= state->near)) {
170 run++;
171 W(tmp, x, Ra);
172 x += stride;
173 }
174 ls_encode_run(state, pb, run, comp, x < w);
175 if (x >= w)
176 return;
177 Rb = R(tmp, x);
178 RItype = FFABS(Ra - Rb) <= state->near;
179 pred = RItype ? Ra : Rb;
180 err = R(in, x) - pred;
181
182 if (!RItype && Ra > Rb)
183 err = -err;
184
185 if (state->near) {
186 if (err > 0)
187 err = (state->near + err) / state->twonear;
188 else
189 err = -(state->near - err) / state->twonear;
190
191 if (RItype || (Rb >= Ra))
192 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
193 else
194 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
195 } else
196 Ra = R(in, x);
197 W(tmp, x, Ra);
198
199 if (err < 0)
200 err += state->range;
201 if (err >= state->range + 1 >> 1)
202 err -= state->range;
203
204 ls_encode_runterm(state, pb, RItype, err,
205 ff_log2_run[state->run_index[comp]]);
206
207 if (state->run_index[comp] > 0)
208 state->run_index[comp]--;
209 } else { /* regular mode */
210 int context;
211
212 context = ff_jpegls_quantize(state, D0) * 81 +
213 ff_jpegls_quantize(state, D1) * 9 +
214 ff_jpegls_quantize(state, D2);
215 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
216
217 if (context < 0) {
218 context = -context;
219 sign = 1;
220 pred = av_clip(pred - state->C[context], 0, state->maxval);
221 err = pred - R(in, x);
222 } else {
223 sign = 0;
224 pred = av_clip(pred + state->C[context], 0, state->maxval);
225 err = R(in, x) - pred;
226 }
227
228 if (state->near) {
229 if (err > 0)
230 err = (state->near + err) / state->twonear;
231 else
232 err = -(state->near - err) / state->twonear;
233 if (!sign)
234 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
235 else
236 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
237 } else
238 Ra = R(in, x);
239 W(tmp, x, Ra);
240
241 ls_encode_regular(state, pb, context, err);
242 }
243 Rc = Rb;
244 x += stride;
245 }
246 }
247
ls_store_lse(JLSState *state, PutByteContext *pb)248 static void ls_store_lse(JLSState *state, PutByteContext *pb)
249 {
250 /* Test if we have default params and don't need to store LSE */
251 JLSState state2 = { 0 };
252 state2.bpp = state->bpp;
253 state2.near = state->near;
254 ff_jpegls_reset_coding_parameters(&state2, 1);
255 if (state->T1 == state2.T1 &&
256 state->T2 == state2.T2 &&
257 state->T3 == state2.T3 &&
258 state->reset == state2.reset)
259 return;
260 /* store LSE type 1 */
261 put_marker_byteu(pb, LSE);
262 bytestream2_put_be16u(pb, 13);
263 bytestream2_put_byteu(pb, 1);
264 bytestream2_put_be16u(pb, state->maxval);
265 bytestream2_put_be16u(pb, state->T1);
266 bytestream2_put_be16u(pb, state->T2);
267 bytestream2_put_be16u(pb, state->T3);
268 bytestream2_put_be16u(pb, state->reset);
269 }
270
encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)271 static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
272 const AVFrame *pict, int *got_packet)
273 {
274 JPEGLSContext *ctx = avctx->priv_data;
275 const AVFrame *const p = pict;
276 PutByteContext pb;
277 PutBitContext pb2;
278 GetBitContext gb;
279 const uint8_t *in;
280 uint8_t *last = NULL;
281 JLSState state = { 0 };
282 size_t size;
283 int i, ret, size_in_bits;
284 int comps;
285
286 last = av_mallocz(FFABS(p->linesize[0]));
287 if (!last)
288 return AVERROR(ENOMEM);
289
290 init_put_bits(&pb2, ctx->buf, ctx->size);
291
292 comps = ctx->comps;
293 /* initialize JPEG-LS state from JPEG parameters */
294 state.near = ctx->pred;
295 state.bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8;
296 ff_jpegls_reset_coding_parameters(&state, 0);
297 ff_jpegls_init_state(&state);
298
299 in = p->data[0];
300 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
301 int t = 0;
302
303 for (i = 0; i < avctx->height; i++) {
304 int last0 = last[0];
305 ls_encode_line(&state, &pb2, last, in, t, avctx->width, 1, 0, 8);
306 t = last0;
307 in += p->linesize[0];
308 }
309 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY16) {
310 int t = 0;
311
312 for (i = 0; i < avctx->height; i++) {
313 int last0 = *((uint16_t *)last);
314 ls_encode_line(&state, &pb2, last, in, t, avctx->width, 1, 0, 16);
315 t = last0;
316 in += p->linesize[0];
317 }
318 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
319 int j, width;
320 int Rc[3] = { 0, 0, 0 };
321
322 width = avctx->width * 3;
323 for (i = 0; i < avctx->height; i++) {
324 for (j = 0; j < 3; j++) {
325 int last0 = last[j];
326 ls_encode_line(&state, &pb2, last + j, in + j, Rc[j],
327 width, 3, j, 8);
328 Rc[j] = last0;
329 }
330 in += p->linesize[0];
331 }
332 } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
333 int j, width;
334 int Rc[3] = { 0, 0, 0 };
335
336 width = avctx->width * 3;
337 for (i = 0; i < avctx->height; i++) {
338 for (j = 2; j >= 0; j--) {
339 int last0 = last[j];
340 ls_encode_line(&state, &pb2, last + j, in + j, Rc[j],
341 width, 3, j, 8);
342 Rc[j] = last0;
343 }
344 in += p->linesize[0];
345 }
346 }
347 av_free(last);
348 /* Now the actual image data has been written, which enables us to estimate
349 * the needed packet size: For every 15 input bits, an escape bit might be
350 * added below; and if put_bits_count % 15 is >= 8, then another bit might
351 * be added.
352 * Furthermore the specification says that after doing 0xff escaping unused
353 * bits in the last byte must be set to 0, so just append 7 "optional" zero
354 * bits to avoid special-casing. This also simplifies the size calculation:
355 * Properly rounding up is now automatically baked-in. */
356 put_bits(&pb2, 7, 0);
357 /* Make sure that the bit count + padding is representable in an int;
358 necessary for put_bits_count() as well as for using a GetBitContext. */
359 if (put_bytes_count(&pb2, 0) > INT_MAX / 8 - AV_INPUT_BUFFER_PADDING_SIZE)
360 return AVERROR(ERANGE);
361 size_in_bits = put_bits_count(&pb2);
362 flush_put_bits(&pb2);
363 size = size_in_bits * 2U / 15;
364 size += 2 + 2 + 2 + 1 + 2 + 2 + 1 + comps * (1 + 1 + 1) + 2 + 2 + 1
365 + comps * (1 + 1) + 1 + 1 + 1; /* Header */
366 size += 2 + 2 + 1 + 2 + 2 + 2 + 2 + 2; /* LSE */
367 size += 2; /* EOI */
368 if ((ret = ff_get_encode_buffer(avctx, pkt, size, 0)) < 0)
369 return ret;
370
371 bytestream2_init_writer(&pb, pkt->data, pkt->size);
372
373 /* write our own JPEG header, can't use mjpeg_picture_header */
374 put_marker_byteu(&pb, SOI);
375 put_marker_byteu(&pb, SOF48);
376 bytestream2_put_be16u(&pb, 8 + comps * 3); // header size depends on components
377 bytestream2_put_byteu(&pb, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8); // bpp
378 bytestream2_put_be16u(&pb, avctx->height);
379 bytestream2_put_be16u(&pb, avctx->width);
380 bytestream2_put_byteu(&pb, comps); // components
381 for (i = 1; i <= comps; i++) {
382 bytestream2_put_byteu(&pb, i); // component ID
383 bytestream2_put_byteu(&pb, 0x11); // subsampling: none
384 bytestream2_put_byteu(&pb, 0); // Tiq, used by JPEG-LS ext
385 }
386
387 put_marker_byteu(&pb, SOS);
388 bytestream2_put_be16u(&pb, 6 + comps * 2);
389 bytestream2_put_byteu(&pb, comps);
390 for (i = 1; i <= comps; i++) {
391 bytestream2_put_byteu(&pb, i); // component ID
392 bytestream2_put_byteu(&pb, 0); // mapping index: none
393 }
394 bytestream2_put_byteu(&pb, ctx->pred);
395 bytestream2_put_byteu(&pb, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
396 bytestream2_put_byteu(&pb, 0); // point transform: none
397
398 ls_store_lse(&state, &pb);
399
400 /* do escape coding */
401 init_get_bits(&gb, pb2.buf, size_in_bits);
402 size_in_bits -= 7;
403 while (get_bits_count(&gb) < size_in_bits) {
404 int v;
405 v = get_bits(&gb, 8);
406 bytestream2_put_byteu(&pb, v);
407 if (v == 0xFF) {
408 v = get_bits(&gb, 7);
409 bytestream2_put_byteu(&pb, v);
410 }
411 }
412
413 /* End of image */
414 put_marker_byteu(&pb, EOI);
415
416 emms_c();
417
418 av_shrink_packet(pkt, bytestream2_tell_p(&pb));
419 *got_packet = 1;
420 return 0;
421 }
422
encode_jpegls_init(AVCodecContext *avctx)423 static av_cold int encode_jpegls_init(AVCodecContext *avctx)
424 {
425 JPEGLSContext *ctx = avctx->priv_data;
426 size_t size;
427
428 if ((avctx->width | avctx->height) > UINT16_MAX) {
429 av_log(avctx, AV_LOG_ERROR, "Dimensions exceeding 65535x65535\n");
430 return AVERROR(EINVAL);
431 }
432 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
433 avctx->pix_fmt == AV_PIX_FMT_GRAY16)
434 ctx->comps = 1;
435 else
436 ctx->comps = 3;
437 size = AV_INPUT_BUFFER_MIN_SIZE;
438 /* INT_MAX due to PutBit-API. */
439 if (avctx->width * (unsigned)avctx->height > (INT_MAX - size) / 4 / ctx->comps)
440 return AVERROR(ERANGE);
441 size += 4 * ctx->comps * avctx->width * avctx->height;
442 ctx->size = size;
443 ctx->buf = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
444 if (!ctx->buf)
445 return AVERROR(ENOMEM);
446
447 return 0;
448 }
449
encode_jpegls_close(AVCodecContext *avctx)450 static av_cold int encode_jpegls_close(AVCodecContext *avctx)
451 {
452 JPEGLSContext *ctx = avctx->priv_data;
453
454 av_freep(&ctx->buf);
455 return 0;
456 }
457
458 #define OFFSET(x) offsetof(JPEGLSContext, x)
459 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
460 static const AVOption options[] = {
461 { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, VE, "pred" },
462 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
463 { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
464 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
465
466 { NULL},
467 };
468
469 static const AVClass jpegls_class = {
470 .class_name = "jpegls",
471 .item_name = av_default_item_name,
472 .option = options,
473 .version = LIBAVUTIL_VERSION_INT,
474 };
475
476 const FFCodec ff_jpegls_encoder = {
477 .p.name = "jpegls",
478 .p.long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
479 .p.type = AVMEDIA_TYPE_VIDEO,
480 .p.id = AV_CODEC_ID_JPEGLS,
481 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
482 .priv_data_size = sizeof(JPEGLSContext),
483 .p.priv_class = &jpegls_class,
484 .init = encode_jpegls_init,
485 FF_CODEC_ENCODE_CB(encode_picture_ls),
486 .close = encode_jpegls_close,
487 .p.pix_fmts = (const enum AVPixelFormat[]) {
488 AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
489 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
490 AV_PIX_FMT_NONE
491 },
492 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
493 FF_CODEC_CAP_INIT_CLEANUP,
494 };
495