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 "bitstream.h"
37 * Encode error from regular symbol
39 static inline void ls_encode_regular(JLSState
*state
, PutBitContext
*pb
, int Q
, int err
){
44 for(k
= 0; (state
->N
[Q
] << k
) < state
->A
[Q
]; k
++);
46 map
= !state
->near
&& !k
&& (2 * state
->B
[Q
] <= -state
->N
[Q
]);
50 if(err
>= ((state
->range
+ 1) >> 1)) {
52 val
= 2 * FFABS(err
) - 1 - map
;
56 set_ur_golomb_jpegls(pb
, val
, k
, state
->limit
, state
->qbpp
);
58 ff_jpegls_update_state_regular(state
, Q
, err
);
62 * Encode error from run termination
64 static inline void ls_encode_runterm(JLSState
*state
, PutBitContext
*pb
, int RItype
, int err
, int limit_add
){
72 temp
+= state
->N
[Q
] >> 1;
73 for(k
= 0; (state
->N
[Q
] << k
) < temp
; k
++);
75 if(!k
&& err
&& (2 * state
->B
[Q
] < state
->N
[Q
]))
79 val
= - (2 * err
) - 1 - RItype
+ map
;
81 val
= 2 * err
- RItype
- map
;
82 set_ur_golomb_jpegls(pb
, val
, k
, state
->limit
- limit_add
- 1, state
->qbpp
);
86 state
->A
[Q
] += (val
+ 1 - RItype
) >> 1;
88 ff_jpegls_downscale_state(state
, Q
);
92 * Encode run value as specified by JPEG-LS standard
94 static inline void ls_encode_run(JLSState
*state
, PutBitContext
*pb
, int run
, int comp
, int trail
){
95 while(run
>= (1 << ff_log2_run
[state
->run_index
[comp
]])){
97 run
-= 1 << ff_log2_run
[state
->run_index
[comp
]];
98 if(state
->run_index
[comp
] < 31)
99 state
->run_index
[comp
]++;
101 /* if hit EOL, encode another full run, else encode aborted run */
106 if(ff_log2_run
[state
->run_index
[comp
]])
107 put_bits(pb
, ff_log2_run
[state
->run_index
[comp
]], run
);
112 * Encode one line of image
114 static inline void ls_encode_line(JLSState
*state
, PutBitContext
*pb
, void *last
, void *cur
, int last2
, int w
, int stride
, int comp
, int bits
){
122 /* compute gradients */
123 Ra
= x
? R(cur
, x
- stride
) : R(last
, x
);
125 Rc
= x
? R(last
, x
- stride
) : last2
;
126 Rd
= (x
>= w
- stride
) ? R(last
, x
) : R(last
, x
+ stride
);
132 if((FFABS(D0
) <= state
->near
) && (FFABS(D1
) <= state
->near
) && (FFABS(D2
) <= state
->near
)) {
133 int RUNval
, RItype
, run
;
137 while(x
< w
&& (FFABS(R(cur
, x
) - RUNval
) <= state
->near
)){
142 ls_encode_run(state
, pb
, run
, comp
, x
< w
);
146 RItype
= (FFABS(Ra
- Rb
) <= state
->near
);
147 pred
= RItype
? Ra
: Rb
;
148 err
= R(cur
, x
) - pred
;
150 if(!RItype
&& Ra
> Rb
)
155 err
= (state
->near
+ err
) / state
->twonear
;
157 err
= -(state
->near
- err
) / state
->twonear
;
159 if(RItype
|| (Rb
>= Ra
))
160 Ra
= av_clip(pred
+ err
* state
->twonear
, 0, state
->maxval
);
162 Ra
= av_clip(pred
- err
* state
->twonear
, 0, state
->maxval
);
167 if(err
>= ((state
->range
+ 1) >> 1))
170 ls_encode_runterm(state
, pb
, RItype
, err
, ff_log2_run
[state
->run_index
[comp
]]);
172 if(state
->run_index
[comp
] > 0)
173 state
->run_index
[comp
]--;
174 } else { /* regular mode */
177 context
= ff_jpegls_quantize(state
, D0
) * 81 + ff_jpegls_quantize(state
, D1
) * 9 + ff_jpegls_quantize(state
, D2
);
178 pred
= mid_pred(Ra
, Ra
+ Rb
- Rc
, Rb
);
183 pred
= av_clip(pred
- state
->C
[context
], 0, state
->maxval
);
184 err
= pred
- R(cur
, x
);
187 pred
= av_clip(pred
+ state
->C
[context
], 0, state
->maxval
);
188 err
= R(cur
, x
) - pred
;
193 err
= (state
->near
+ err
) / state
->twonear
;
195 err
= -(state
->near
- err
) / state
->twonear
;
197 Ra
= av_clip(pred
+ err
* state
->twonear
, 0, state
->maxval
);
199 Ra
= av_clip(pred
- err
* state
->twonear
, 0, state
->maxval
);
203 ls_encode_regular(state
, pb
, context
, err
);
209 static void ls_store_lse(JLSState
*state
, PutBitContext
*pb
){
210 /* Test if we have default params and don't need to store LSE */
212 memset(&state2
, 0, sizeof(JLSState
));
213 state2
.bpp
= state
->bpp
;
214 state2
.near
= state
->near
;
215 ff_jpegls_reset_coding_parameters(&state2
, 1);
216 if(state
->T1
== state2
.T1
&& state
->T2
== state2
.T2
&& state
->T3
== state2
.T3
&& state
->reset
== state2
.reset
)
218 /* store LSE type 1 */
220 put_bits(pb
, 16, 13);
222 put_bits(pb
, 16, state
->maxval
);
223 put_bits(pb
, 16, state
->T1
);
224 put_bits(pb
, 16, state
->T2
);
225 put_bits(pb
, 16, state
->T3
);
226 put_bits(pb
, 16, state
->reset
);
229 static int encode_picture_ls(AVCodecContext
*avctx
, unsigned char *buf
, int buf_size
, void *data
){
230 JpeglsContext
* const s
= avctx
->priv_data
;
231 AVFrame
*pict
= data
;
232 AVFrame
* const p
= (AVFrame
*)&s
->picture
;
233 const int near
= avctx
->prediction_method
;
234 PutBitContext pb
, pb2
;
236 uint8_t *buf2
, *zero
, *cur
, *last
;
241 buf2
= av_malloc(buf_size
);
243 init_put_bits(&pb
, buf
, buf_size
);
244 init_put_bits(&pb2
, buf2
, buf_size
);
247 p
->pict_type
= FF_I_TYPE
;
250 if(avctx
->pix_fmt
== PIX_FMT_GRAY8
|| avctx
->pix_fmt
== PIX_FMT_GRAY16
)
255 /* write our own JPEG header, can't use mjpeg_picture_header */
256 put_marker(&pb
, SOI
);
257 put_marker(&pb
, SOF48
);
258 put_bits(&pb
, 16, 8 + comps
* 3); // header size depends on components
259 put_bits(&pb
, 8, (avctx
->pix_fmt
== PIX_FMT_GRAY16
) ? 16 : 8); // bpp
260 put_bits(&pb
, 16, avctx
->height
);
261 put_bits(&pb
, 16, avctx
->width
);
262 put_bits(&pb
, 8, comps
); // components
263 for(i
= 1; i
<= comps
; i
++) {
264 put_bits(&pb
, 8, i
); // component ID
265 put_bits(&pb
, 8, 0x11); // subsampling: none
266 put_bits(&pb
, 8, 0); // Tiq, used by JPEG-LS ext
269 put_marker(&pb
, SOS
);
270 put_bits(&pb
, 16, 6 + comps
* 2);
271 put_bits(&pb
, 8, comps
);
272 for(i
= 1; i
<= comps
; i
++) {
273 put_bits(&pb
, 8, i
); // component ID
274 put_bits(&pb
, 8, 0); // mapping index: none
276 put_bits(&pb
, 8, near
);
277 put_bits(&pb
, 8, (comps
> 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
278 put_bits(&pb
, 8, 0); // point transform: none
280 state
= av_mallocz(sizeof(JLSState
));
281 /* initialize JPEG-LS state from JPEG parameters */
283 state
->bpp
= (avctx
->pix_fmt
== PIX_FMT_GRAY16
) ? 16 : 8;
284 ff_jpegls_reset_coding_parameters(state
, 0);
285 ff_jpegls_init_state(state
);
287 ls_store_lse(state
, &pb
);
289 zero
= av_mallocz(p
->linesize
[0]);
292 if(avctx
->pix_fmt
== PIX_FMT_GRAY8
){
295 for(i
= 0; i
< avctx
->height
; i
++) {
296 ls_encode_line(state
, &pb2
, last
, cur
, t
, avctx
->width
, 1, 0, 8);
299 cur
+= p
->linesize
[0];
301 }else if(avctx
->pix_fmt
== PIX_FMT_GRAY16
){
304 for(i
= 0; i
< avctx
->height
; i
++) {
305 ls_encode_line(state
, &pb2
, last
, cur
, t
, avctx
->width
, 1, 0, 16);
306 t
= *((uint16_t*)last
);
308 cur
+= p
->linesize
[0];
310 }else if(avctx
->pix_fmt
== PIX_FMT_RGB24
){
312 int Rc
[3] = {0, 0, 0};
314 width
= avctx
->width
* 3;
315 for(i
= 0; i
< avctx
->height
; i
++) {
316 for(j
= 0; j
< 3; j
++) {
317 ls_encode_line(state
, &pb2
, last
+ j
, cur
+ j
, Rc
[j
], width
, 3, j
, 8);
321 cur
+= s
->picture
.linesize
[0];
323 }else if(avctx
->pix_fmt
== PIX_FMT_BGR24
){
325 int Rc
[3] = {0, 0, 0};
327 width
= avctx
->width
* 3;
328 for(i
= 0; i
< avctx
->height
; i
++) {
329 for(j
= 2; j
>= 0; j
--) {
330 ls_encode_line(state
, &pb2
, last
+ j
, cur
+ j
, Rc
[j
], width
, 3, j
, 8);
334 cur
+= s
->picture
.linesize
[0];
341 // the specification says that after doing 0xff escaping unused bits in the
342 // last byte must be set to 0, so just append 7 "optional" zero-bits to
343 // avoid special-casing.
344 put_bits(&pb2
, 7, 0);
345 size
= put_bits_count(&pb2
);
346 flush_put_bits(&pb2
);
347 /* do escape coding */
348 init_get_bits(&gb
, buf2
, size
);
350 while(get_bits_count(&gb
) < size
){
352 v
= get_bits(&gb
, 8);
355 v
= get_bits(&gb
, 7);
363 put_marker(&pb
, EOI
);
368 return put_bits_count(&pb
) >> 3;
371 static av_cold
int encode_init_ls(AVCodecContext
*ctx
) {
372 JpeglsContext
*c
= (JpeglsContext
*)ctx
->priv_data
;
375 ctx
->coded_frame
= &c
->picture
;
377 if(ctx
->pix_fmt
!= PIX_FMT_GRAY8
&& ctx
->pix_fmt
!= PIX_FMT_GRAY16
&& ctx
->pix_fmt
!= PIX_FMT_RGB24
&& ctx
->pix_fmt
!= PIX_FMT_BGR24
){
378 av_log(ctx
, AV_LOG_ERROR
, "Only grayscale and RGB24/BGR24 images are supported\n");
384 AVCodec jpegls_encoder
= { //FIXME avoid MPV_* lossless JPEG should not need them
388 sizeof(JpeglsContext
),
392 .pix_fmts
= (enum PixelFormat
[]){PIX_FMT_BGR24
, PIX_FMT_RGB24
, PIX_FMT_GRAY8
, PIX_FMT_GRAY16
, PIX_FMT_NONE
},
393 .long_name
= NULL_IF_CONFIG_SMALL("JPEG-LS"),