3 * Copyright (c) 2003 Michael Niedermayer
4 * Copyright (c) 2006 Konstantin Shishkov
6 * This file is part of FFmpeg.
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.
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.
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
38 typedef struct JPEGLSContext {
45 * Encode error from regular symbol
47 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q,
54 for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
57 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
61 if (err >= (state->range + 1 >> 1)) {
63 val = 2 * FFABS(err) - 1 - map;
67 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
69 ff_jpegls_update_state_regular(state, Q, err);
73 * Encode error from run termination
75 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb,
76 int RItype, int err, int limit_add)
85 temp += state->N[Q] >> 1;
86 for (k = 0; (state->N[Q] << k) < temp; k++)
89 if (!k && err && (2 * state->B[Q] < state->N[Q]))
93 val = -(2 * err) - 1 - RItype + map;
95 val = 2 * err - RItype - map;
96 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
100 state->A[Q] += (val + 1 - RItype) >> 1;
102 ff_jpegls_downscale_state(state, Q);
106 * Encode run value as specified by JPEG-LS standard
108 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run,
111 while (run >= (1 << ff_log2_run[state->run_index[comp]])) {
113 run -= 1 << ff_log2_run[state->run_index[comp]];
114 if (state->run_index[comp] < 31)
115 state->run_index[comp]++;
117 /* if hit EOL, encode another full run, else encode aborted run */
122 if (ff_log2_run[state->run_index[comp]])
123 put_bits(pb, ff_log2_run[state->run_index[comp]], run);
128 * Encode one line of image
130 static inline void ls_encode_line(JLSState *state, PutBitContext *pb,
131 void *last, void *cur, int last2, int w,
132 int stride, int comp, int bits)
141 /* compute gradients */
142 Ra = x ? R(cur, x - stride) : R(last, x);
144 Rc = x ? R(last, x - stride) : last2;
145 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
151 if ((FFABS(D0) <= state->near) &&
152 (FFABS(D1) <= state->near) &&
153 (FFABS(D2) <= state->near)) {
154 int RUNval, RItype, run;
158 while (x < w && (FFABS(R(cur, x) - RUNval) <= state->near)) {
163 ls_encode_run(state, pb, run, comp, x < w);
167 RItype = FFABS(Ra - Rb) <= state->near;
168 pred = RItype ? Ra : Rb;
169 err = R(cur, x) - pred;
171 if (!RItype && Ra > Rb)
176 err = (state->near + err) / state->twonear;
178 err = -(state->near - err) / state->twonear;
180 if (RItype || (Rb >= Ra))
181 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
183 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
188 if (err >= state->range + 1 >> 1)
191 ls_encode_runterm(state, pb, RItype, err,
192 ff_log2_run[state->run_index[comp]]);
194 if (state->run_index[comp] > 0)
195 state->run_index[comp]--;
196 } else { /* regular mode */
199 context = ff_jpegls_quantize(state, D0) * 81 +
200 ff_jpegls_quantize(state, D1) * 9 +
201 ff_jpegls_quantize(state, D2);
202 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
207 pred = av_clip(pred - state->C[context], 0, state->maxval);
208 err = pred - R(cur, x);
211 pred = av_clip(pred + state->C[context], 0, state->maxval);
212 err = R(cur, x) - pred;
217 err = (state->near + err) / state->twonear;
219 err = -(state->near - err) / state->twonear;
221 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
223 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
227 ls_encode_regular(state, pb, context, err);
233 static void ls_store_lse(JLSState *state, PutBitContext *pb)
235 /* Test if we have default params and don't need to store LSE */
236 JLSState state2 = { 0 };
237 state2.bpp = state->bpp;
238 state2.near = state->near;
239 ff_jpegls_reset_coding_parameters(&state2, 1);
240 if (state->T1 == state2.T1 &&
241 state->T2 == state2.T2 &&
242 state->T3 == state2.T3 &&
243 state->reset == state2.reset)
245 /* store LSE type 1 */
247 put_bits(pb, 16, 13);
249 put_bits(pb, 16, state->maxval);
250 put_bits(pb, 16, state->T1);
251 put_bits(pb, 16, state->T2);
252 put_bits(pb, 16, state->T3);
253 put_bits(pb, 16, state->reset);
256 static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
257 const AVFrame *pict, int *got_packet)
259 JPEGLSContext *ctx = avctx->priv_data;
260 const AVFrame *const p = pict;
261 PutBitContext pb, pb2;
263 uint8_t *buf2 = NULL;
264 uint8_t *zero = NULL;
266 uint8_t *last = NULL;
267 JLSState *state = NULL;
271 #if FF_API_PRIVATE_OPT
272 FF_DISABLE_DEPRECATION_WARNINGS
273 if (avctx->prediction_method)
274 ctx->pred = avctx->prediction_method;
275 FF_ENABLE_DEPRECATION_WARNINGS
278 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
279 avctx->pix_fmt == AV_PIX_FMT_GRAY16)
284 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width *avctx->height * comps * 4 +
285 AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
288 buf2 = av_malloc(pkt->size);
292 init_put_bits(&pb, pkt->data, pkt->size);
293 init_put_bits(&pb2, buf2, pkt->size);
295 /* write our own JPEG header, can't use mjpeg_picture_header */
296 put_marker(&pb, SOI);
297 put_marker(&pb, SOF48);
298 put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
299 put_bits(&pb, 8, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8); // bpp
300 put_bits(&pb, 16, avctx->height);
301 put_bits(&pb, 16, avctx->width);
302 put_bits(&pb, 8, comps); // components
303 for (i = 1; i <= comps; i++) {
304 put_bits(&pb, 8, i); // component ID
305 put_bits(&pb, 8, 0x11); // subsampling: none
306 put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
309 put_marker(&pb, SOS);
310 put_bits(&pb, 16, 6 + comps * 2);
311 put_bits(&pb, 8, comps);
312 for (i = 1; i <= comps; i++) {
313 put_bits(&pb, 8, i); // component ID
314 put_bits(&pb, 8, 0); // mapping index: none
316 put_bits(&pb, 8, ctx->pred);
317 put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
318 put_bits(&pb, 8, 0); // point transform: none
320 state = av_mallocz(sizeof(JLSState));
324 /* initialize JPEG-LS state from JPEG parameters */
325 state->near = ctx->pred;
326 state->bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8;
327 ff_jpegls_reset_coding_parameters(state, 0);
328 ff_jpegls_init_state(state);
330 ls_store_lse(state, &pb);
332 zero = last = av_mallocz(FFABS(p->linesize[0]));
337 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
340 for (i = 0; i < avctx->height; i++) {
341 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
344 cur += p->linesize[0];
346 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY16) {
349 for (i = 0; i < avctx->height; i++) {
350 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
351 t = *((uint16_t *)last);
353 cur += p->linesize[0];
355 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
357 int Rc[3] = { 0, 0, 0 };
359 width = avctx->width * 3;
360 for (i = 0; i < avctx->height; i++) {
361 for (j = 0; j < 3; j++) {
362 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j],
367 cur += p->linesize[0];
369 } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
371 int Rc[3] = { 0, 0, 0 };
373 width = avctx->width * 3;
374 for (i = 0; i < avctx->height; i++) {
375 for (j = 2; j >= 0; j--) {
376 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j],
381 cur += p->linesize[0];
388 /* the specification says that after doing 0xff escaping unused bits in
389 * the last byte must be set to 0, so just append 7 "optional" zero bits
390 * to avoid special-casing. */
391 put_bits(&pb2, 7, 0);
392 size = put_bits_count(&pb2);
393 flush_put_bits(&pb2);
394 /* do escape coding */
395 init_get_bits(&gb, buf2, size);
397 while (get_bits_count(&gb) < size) {
399 v = get_bits(&gb, 8);
402 v = get_bits(&gb, 7);
406 avpriv_align_put_bits(&pb);
410 put_marker(&pb, EOI);
415 pkt->size = put_bits_count(&pb) >> 3;
416 pkt->flags |= AV_PKT_FLAG_KEY;
421 av_packet_unref(pkt);
425 return AVERROR(ENOMEM);
428 static av_cold int encode_init_ls(AVCodecContext *ctx)
430 #if FF_API_CODED_FRAME
431 FF_DISABLE_DEPRECATION_WARNINGS
432 ctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
433 ctx->coded_frame->key_frame = 1;
434 FF_ENABLE_DEPRECATION_WARNINGS
437 if (ctx->pix_fmt != AV_PIX_FMT_GRAY8 &&
438 ctx->pix_fmt != AV_PIX_FMT_GRAY16 &&
439 ctx->pix_fmt != AV_PIX_FMT_RGB24 &&
440 ctx->pix_fmt != AV_PIX_FMT_BGR24) {
441 av_log(ctx, AV_LOG_ERROR,
442 "Only grayscale and RGB24/BGR24 images are supported\n");
448 #define OFFSET(x) offsetof(JPEGLSContext, x)
449 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
450 static const AVOption options[] = {
451 { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, VE, "pred" },
452 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
453 { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
454 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
459 static const AVClass jpegls_class = {
460 .class_name = "jpegls",
461 .item_name = av_default_item_name,
463 .version = LIBAVUTIL_VERSION_INT,
466 AVCodec ff_jpegls_encoder = {
468 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
469 .type = AVMEDIA_TYPE_VIDEO,
470 .id = AV_CODEC_ID_JPEGLS,
471 .priv_data_size = sizeof(JPEGLSContext),
472 .priv_class = &jpegls_class,
473 .init = encode_init_ls,
474 .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
475 .encode2 = encode_picture_ls,
476 .pix_fmts = (const enum AVPixelFormat[]) {
477 AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
478 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
481 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
482 FF_CODEC_CAP_INIT_CLEANUP,