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
29 #include "bytestream.h"
39 typedef struct JPEGLSContext {
45 static inline void put_marker_byteu(PutByteContext *pb, enum JpegMarker code)
47 bytestream2_put_byteu(pb, 0xff);
48 bytestream2_put_byteu(pb, code);
51 static inline void put_marker_byte(PutByteContext *pb, enum JpegMarker code)
53 bytestream2_put_byte(pb, 0xff);
54 bytestream2_put_byte(pb, code);
58 * Encode error from regular symbol
60 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q,
67 for (k = 0; (state->N[Q] << k) < state->A[Q]; k++)
70 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
74 if (err >= (state->range + 1 >> 1)) {
76 val = 2 * FFABS(err) - 1 - map;
80 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
82 ff_jpegls_update_state_regular(state, Q, err);
86 * Encode error from run termination
88 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb,
89 int RItype, int err, int limit_add)
98 temp += state->N[Q] >> 1;
99 for (k = 0; (state->N[Q] << k) < temp; k++)
102 if (!k && err && (2 * state->B[Q] < state->N[Q]))
106 val = -(2 * err) - 1 - RItype + map;
108 val = 2 * err - RItype - map;
109 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
113 state->A[Q] += (val + 1 - RItype) >> 1;
115 ff_jpegls_downscale_state(state, Q);
119 * Encode run value as specified by JPEG-LS standard
121 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run,
124 while (run >= (1 << ff_log2_run[state->run_index[comp]])) {
126 run -= 1 << ff_log2_run[state->run_index[comp]];
127 if (state->run_index[comp] < 31)
128 state->run_index[comp]++;
130 /* if hit EOL, encode another full run, else encode aborted run */
135 if (ff_log2_run[state->run_index[comp]])
136 put_bits(pb, ff_log2_run[state->run_index[comp]], run);
141 * Encode one line of image
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)
148 int Ra = R(tmp, 0), Rb, Rc = last2, Rd;
154 /* compute gradients */
156 Rd = (x >= w - stride) ? R(tmp, x) : R(tmp, x + stride);
162 if ((FFABS(D0) <= state->near) &&
163 (FFABS(D1) <= state->near) &&
164 (FFABS(D2) <= state->near)) {
165 int RUNval, RItype, run;
169 while (x < w && (FFABS(R(in, x) - RUNval) <= state->near)) {
174 ls_encode_run(state, pb, run, comp, x < w);
178 RItype = FFABS(Ra - Rb) <= state->near;
179 pred = RItype ? Ra : Rb;
180 err = R(in, x) - pred;
182 if (!RItype && Ra > Rb)
187 err = (state->near + err) / state->twonear;
189 err = -(state->near - err) / state->twonear;
191 if (RItype || (Rb >= Ra))
192 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
194 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
201 if (err >= state->range + 1 >> 1)
204 ls_encode_runterm(state, pb, RItype, err,
205 ff_log2_run[state->run_index[comp]]);
207 if (state->run_index[comp] > 0)
208 state->run_index[comp]--;
209 } else { /* regular mode */
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);
220 pred = av_clip(pred - state->C[context], 0, state->maxval);
221 err = pred - R(in, x);
224 pred = av_clip(pred + state->C[context], 0, state->maxval);
225 err = R(in, x) - pred;
230 err = (state->near + err) / state->twonear;
232 err = -(state->near - err) / state->twonear;
234 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
236 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
241 ls_encode_regular(state, pb, context, err);
248 static void ls_store_lse(JLSState *state, PutByteContext *pb)
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)
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);
271 static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
272 const AVFrame *pict, int *got_packet)
274 JPEGLSContext *ctx = avctx->priv_data;
275 const AVFrame *const p = pict;
279 uint8_t *buf2 = NULL;
281 uint8_t *last = NULL;
282 JLSState *state = NULL;
286 #if FF_API_PRIVATE_OPT
287 FF_DISABLE_DEPRECATION_WARNINGS
288 if (avctx->prediction_method)
289 ctx->pred = avctx->prediction_method;
290 FF_ENABLE_DEPRECATION_WARNINGS
293 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8 ||
294 avctx->pix_fmt == AV_PIX_FMT_GRAY16)
299 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width *avctx->height * comps * 4 +
300 AV_INPUT_BUFFER_MIN_SIZE, 0)) < 0)
303 buf2 = av_malloc(pkt->size);
307 bytestream2_init_writer(&pb, pkt->data, pkt->size);
308 init_put_bits(&pb2, buf2, pkt->size);
310 /* write our own JPEG header, can't use mjpeg_picture_header */
311 put_marker_byteu(&pb, SOI);
312 put_marker_byteu(&pb, SOF48);
313 bytestream2_put_be16u(&pb, 8 + comps * 3); // header size depends on components
314 bytestream2_put_byteu(&pb, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8); // bpp
315 bytestream2_put_be16u(&pb, avctx->height);
316 bytestream2_put_be16u(&pb, avctx->width);
317 bytestream2_put_byteu(&pb, comps); // components
318 for (i = 1; i <= comps; i++) {
319 bytestream2_put_byteu(&pb, i); // component ID
320 bytestream2_put_byteu(&pb, 0x11); // subsampling: none
321 bytestream2_put_byteu(&pb, 0); // Tiq, used by JPEG-LS ext
324 put_marker_byteu(&pb, SOS);
325 bytestream2_put_be16u(&pb, 6 + comps * 2);
326 bytestream2_put_byteu(&pb, comps);
327 for (i = 1; i <= comps; i++) {
328 bytestream2_put_byteu(&pb, i); // component ID
329 bytestream2_put_byteu(&pb, 0); // mapping index: none
331 bytestream2_put_byteu(&pb, ctx->pred);
332 bytestream2_put_byteu(&pb, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
333 bytestream2_put_byteu(&pb, 0); // point transform: none
335 state = av_mallocz(sizeof(JLSState));
339 /* initialize JPEG-LS state from JPEG parameters */
340 state->near = ctx->pred;
341 state->bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8;
342 ff_jpegls_reset_coding_parameters(state, 0);
343 ff_jpegls_init_state(state);
345 ls_store_lse(state, &pb);
347 last = av_mallocz(FFABS(p->linesize[0]));
352 if (avctx->pix_fmt == AV_PIX_FMT_GRAY8) {
355 for (i = 0; i < avctx->height; i++) {
357 ls_encode_line(state, &pb2, last, in, t, avctx->width, 1, 0, 8);
359 in += p->linesize[0];
361 } else if (avctx->pix_fmt == AV_PIX_FMT_GRAY16) {
364 for (i = 0; i < avctx->height; i++) {
365 int last0 = *((uint16_t *)last);
366 ls_encode_line(state, &pb2, last, in, t, avctx->width, 1, 0, 16);
368 in += p->linesize[0];
370 } else if (avctx->pix_fmt == AV_PIX_FMT_RGB24) {
372 int Rc[3] = { 0, 0, 0 };
374 width = avctx->width * 3;
375 for (i = 0; i < avctx->height; i++) {
376 for (j = 0; j < 3; j++) {
378 ls_encode_line(state, &pb2, last + j, in + j, Rc[j],
382 in += p->linesize[0];
384 } else if (avctx->pix_fmt == AV_PIX_FMT_BGR24) {
386 int Rc[3] = { 0, 0, 0 };
388 width = avctx->width * 3;
389 for (i = 0; i < avctx->height; i++) {
390 for (j = 2; j >= 0; j--) {
392 ls_encode_line(state, &pb2, last + j, in + j, Rc[j],
396 in += p->linesize[0];
403 /* the specification says that after doing 0xff escaping unused bits in
404 * the last byte must be set to 0, so just append 7 "optional" zero bits
405 * to avoid special-casing. */
406 put_bits(&pb2, 7, 0);
407 size = put_bits_count(&pb2);
408 flush_put_bits(&pb2);
409 /* do escape coding */
410 init_get_bits(&gb, buf2, size);
412 while (get_bits_count(&gb) < size) {
414 v = get_bits(&gb, 8);
415 bytestream2_put_byte(&pb, v);
417 v = get_bits(&gb, 7);
418 bytestream2_put_byte(&pb, v);
424 put_marker_byte(&pb, EOI);
428 pkt->size = bytestream2_tell_p(&pb);
429 pkt->flags |= AV_PKT_FLAG_KEY;
437 return AVERROR(ENOMEM);
440 static av_cold int encode_init_ls(AVCodecContext *ctx)
442 #if FF_API_CODED_FRAME
443 FF_DISABLE_DEPRECATION_WARNINGS
444 ctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
445 ctx->coded_frame->key_frame = 1;
446 FF_ENABLE_DEPRECATION_WARNINGS
449 if (ctx->pix_fmt != AV_PIX_FMT_GRAY8 &&
450 ctx->pix_fmt != AV_PIX_FMT_GRAY16 &&
451 ctx->pix_fmt != AV_PIX_FMT_RGB24 &&
452 ctx->pix_fmt != AV_PIX_FMT_BGR24) {
453 av_log(ctx, AV_LOG_ERROR,
454 "Only grayscale and RGB24/BGR24 images are supported\n");
460 #define OFFSET(x) offsetof(JPEGLSContext, x)
461 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
462 static const AVOption options[] = {
463 { "pred", "Prediction method", OFFSET(pred), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, VE, "pred" },
464 { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "pred" },
465 { "plane", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "pred" },
466 { "median", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "pred" },
471 static const AVClass jpegls_class = {
472 .class_name = "jpegls",
473 .item_name = av_default_item_name,
475 .version = LIBAVUTIL_VERSION_INT,
478 AVCodec ff_jpegls_encoder = {
480 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),
481 .type = AVMEDIA_TYPE_VIDEO,
482 .id = AV_CODEC_ID_JPEGLS,
483 .priv_data_size = sizeof(JPEGLSContext),
484 .priv_class = &jpegls_class,
485 .init = encode_init_ls,
486 .capabilities = AV_CODEC_CAP_FRAME_THREADS,
487 .encode2 = encode_picture_ls,
488 .pix_fmts = (const enum AVPixelFormat[]) {
489 AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
490 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
493 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
494 FF_CODEC_CAP_INIT_CLEANUP,