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
40 * Encode error from regular symbol
42 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
47 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
49 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
53 if(err >= ((state->range + 1) >> 1)) {
55 val = 2 * FFABS(err) - 1 - map;
59 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
61 ff_jpegls_update_state_regular(state, Q, err);
65 * Encode error from run termination
67 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
75 temp += state->N[Q] >> 1;
76 for(k = 0; (state->N[Q] << k) < temp; k++);
78 if(!k && err && (2 * state->B[Q] < state->N[Q]))
82 val = - (2 * err) - 1 - RItype + map;
84 val = 2 * err - RItype - map;
85 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
89 state->A[Q] += (val + 1 - RItype) >> 1;
91 ff_jpegls_downscale_state(state, Q);
95 * Encode run value as specified by JPEG-LS standard
97 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
98 while(run >= (1 << ff_log2_run[state->run_index[comp]])){
100 run -= 1 << ff_log2_run[state->run_index[comp]];
101 if(state->run_index[comp] < 31)
102 state->run_index[comp]++;
104 /* if hit EOL, encode another full run, else encode aborted run */
109 if(ff_log2_run[state->run_index[comp]])
110 put_bits(pb, ff_log2_run[state->run_index[comp]], run);
115 * Encode one line of image
117 static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){
125 /* compute gradients */
126 Ra = x ? R(cur, x - stride) : R(last, x);
128 Rc = x ? R(last, x - stride) : last2;
129 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
135 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
136 int RUNval, RItype, run;
140 while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
145 ls_encode_run(state, pb, run, comp, x < w);
149 RItype = (FFABS(Ra - Rb) <= state->near);
150 pred = RItype ? Ra : Rb;
151 err = R(cur, x) - pred;
153 if(!RItype && Ra > Rb)
158 err = (state->near + err) / state->twonear;
160 err = -(state->near - err) / state->twonear;
162 if(RItype || (Rb >= Ra))
163 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
165 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
170 if(err >= ((state->range + 1) >> 1))
173 ls_encode_runterm(state, pb, RItype, err, ff_log2_run[state->run_index[comp]]);
175 if(state->run_index[comp] > 0)
176 state->run_index[comp]--;
177 } else { /* regular mode */
180 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
181 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
186 pred = av_clip(pred - state->C[context], 0, state->maxval);
187 err = pred - R(cur, x);
190 pred = av_clip(pred + state->C[context], 0, state->maxval);
191 err = R(cur, x) - pred;
196 err = (state->near + err) / state->twonear;
198 err = -(state->near - err) / state->twonear;
200 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
202 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
206 ls_encode_regular(state, pb, context, err);
212 static void ls_store_lse(JLSState *state, PutBitContext *pb){
213 /* Test if we have default params and don't need to store LSE */
214 JLSState state2 = { 0 };
215 state2.bpp = state->bpp;
216 state2.near = state->near;
217 ff_jpegls_reset_coding_parameters(&state2, 1);
218 if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
220 /* store LSE type 1 */
222 put_bits(pb, 16, 13);
224 put_bits(pb, 16, state->maxval);
225 put_bits(pb, 16, state->T1);
226 put_bits(pb, 16, state->T2);
227 put_bits(pb, 16, state->T3);
228 put_bits(pb, 16, state->reset);
231 static int encode_picture_ls(AVCodecContext *avctx, AVPacket *pkt,
232 const AVFrame *pict, int *got_packet)
234 JpeglsContext * const s = avctx->priv_data;
235 AVFrame * const p = &s->picture;
236 const int near = avctx->prediction_method;
237 PutBitContext pb, pb2;
239 uint8_t *buf2, *zero, *cur, *last;
245 p->pict_type= AV_PICTURE_TYPE_I;
248 if(avctx->pix_fmt == AV_PIX_FMT_GRAY8 || avctx->pix_fmt == AV_PIX_FMT_GRAY16)
253 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*comps*4 +
254 FF_MIN_BUFFER_SIZE)) < 0)
257 buf2 = av_malloc(pkt->size);
259 init_put_bits(&pb, pkt->data, pkt->size);
260 init_put_bits(&pb2, buf2, pkt->size);
262 /* write our own JPEG header, can't use mjpeg_picture_header */
263 put_marker(&pb, SOI);
264 put_marker(&pb, SOF48);
265 put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
266 put_bits(&pb, 8, (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8); // bpp
267 put_bits(&pb, 16, avctx->height);
268 put_bits(&pb, 16, avctx->width);
269 put_bits(&pb, 8, comps); // components
270 for(i = 1; i <= comps; i++) {
271 put_bits(&pb, 8, i); // component ID
272 put_bits(&pb, 8, 0x11); // subsampling: none
273 put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
276 put_marker(&pb, SOS);
277 put_bits(&pb, 16, 6 + comps * 2);
278 put_bits(&pb, 8, comps);
279 for(i = 1; i <= comps; i++) {
280 put_bits(&pb, 8, i); // component ID
281 put_bits(&pb, 8, 0); // mapping index: none
283 put_bits(&pb, 8, near);
284 put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
285 put_bits(&pb, 8, 0); // point transform: none
287 state = av_mallocz(sizeof(JLSState));
288 /* initialize JPEG-LS state from JPEG parameters */
290 state->bpp = (avctx->pix_fmt == AV_PIX_FMT_GRAY16) ? 16 : 8;
291 ff_jpegls_reset_coding_parameters(state, 0);
292 ff_jpegls_init_state(state);
294 ls_store_lse(state, &pb);
296 zero = av_mallocz(FFABS(p->linesize[0]));
299 return AVERROR(ENOMEM);
303 if(avctx->pix_fmt == AV_PIX_FMT_GRAY8){
306 for(i = 0; i < avctx->height; i++) {
307 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
310 cur += p->linesize[0];
312 }else if(avctx->pix_fmt == AV_PIX_FMT_GRAY16){
315 for(i = 0; i < avctx->height; i++) {
316 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
317 t = *((uint16_t*)last);
319 cur += p->linesize[0];
321 }else if(avctx->pix_fmt == AV_PIX_FMT_RGB24){
323 int Rc[3] = {0, 0, 0};
325 width = avctx->width * 3;
326 for(i = 0; i < avctx->height; i++) {
327 for(j = 0; j < 3; j++) {
328 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
332 cur += s->picture.linesize[0];
334 }else if(avctx->pix_fmt == AV_PIX_FMT_BGR24){
336 int Rc[3] = {0, 0, 0};
338 width = avctx->width * 3;
339 for(i = 0; i < avctx->height; i++) {
340 for(j = 2; j >= 0; j--) {
341 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
345 cur += s->picture.linesize[0];
352 // the specification says that after doing 0xff escaping unused bits in the
353 // last byte must be set to 0, so just append 7 "optional" zero-bits to
354 // avoid special-casing.
355 put_bits(&pb2, 7, 0);
356 size = put_bits_count(&pb2);
357 flush_put_bits(&pb2);
358 /* do escape coding */
359 init_get_bits(&gb, buf2, size);
361 while(get_bits_count(&gb) < size){
363 v = get_bits(&gb, 8);
366 v = get_bits(&gb, 7);
370 avpriv_align_put_bits(&pb);
374 put_marker(&pb, EOI);
379 pkt->size = put_bits_count(&pb) >> 3;
380 pkt->flags |= AV_PKT_FLAG_KEY;
385 static av_cold int encode_init_ls(AVCodecContext *ctx) {
386 JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
389 ctx->coded_frame = &c->picture;
391 if(ctx->pix_fmt != AV_PIX_FMT_GRAY8 && ctx->pix_fmt != AV_PIX_FMT_GRAY16 && ctx->pix_fmt != AV_PIX_FMT_RGB24 && ctx->pix_fmt != AV_PIX_FMT_BGR24){
392 av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
398 AVCodec ff_jpegls_encoder = {
400 .type = AVMEDIA_TYPE_VIDEO,
401 .id = AV_CODEC_ID_JPEGLS,
402 .priv_data_size = sizeof(JpeglsContext),
403 .init = encode_init_ls,
404 .encode2 = encode_picture_ls,
405 .pix_fmts = (const enum AVPixelFormat[]){
406 AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
409 .long_name = NULL_IF_CONFIG_SMALL("JPEG-LS"),