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"
36 * Encode error from regular symbol
38 static inline void ls_encode_regular(JLSState
*state
, PutBitContext
*pb
, int Q
, int err
){
43 for(k
= 0; (state
->N
[Q
] << k
) < state
->A
[Q
]; k
++);
45 map
= !state
->near
&& !k
&& (2 * state
->B
[Q
] <= -state
->N
[Q
]);
49 if(err
>= ((state
->range
+ 1) >> 1)) {
51 val
= 2 * FFABS(err
) - 1 - map
;
55 set_ur_golomb_jpegls(pb
, val
, k
, state
->limit
, state
->qbpp
);
57 ff_jpegls_update_state_regular(state
, Q
, err
);
61 * Encode error from run termination
63 static inline void ls_encode_runterm(JLSState
*state
, PutBitContext
*pb
, int RItype
, int err
, int limit_add
){
71 temp
+= state
->N
[Q
] >> 1;
72 for(k
= 0; (state
->N
[Q
] << k
) < temp
; k
++);
74 if(!k
&& err
&& (2 * state
->B
[Q
] < state
->N
[Q
]))
78 val
= - (2 * err
) - 1 - RItype
+ map
;
80 val
= 2 * err
- RItype
- map
;
81 set_ur_golomb_jpegls(pb
, val
, k
, state
->limit
- limit_add
- 1, state
->qbpp
);
85 state
->A
[Q
] += (val
+ 1 - RItype
) >> 1;
87 ff_jpegls_downscale_state(state
, Q
);
91 * Encode run value as specified by JPEG-LS standard
93 static inline void ls_encode_run(JLSState
*state
, PutBitContext
*pb
, int run
, int comp
, int trail
){
94 while(run
>= (1 << ff_log2_run
[state
->run_index
[comp
]])){
96 run
-= 1 << ff_log2_run
[state
->run_index
[comp
]];
97 if(state
->run_index
[comp
] < 31)
98 state
->run_index
[comp
]++;
100 /* if hit EOL, encode another full run, else encode aborted run */
105 if(ff_log2_run
[state
->run_index
[comp
]])
106 put_bits(pb
, ff_log2_run
[state
->run_index
[comp
]], run
);
111 * Encode one line of image
113 static inline void ls_encode_line(JLSState
*state
, PutBitContext
*pb
, void *last
, void *cur
, int last2
, int w
, int stride
, int comp
, int bits
){
121 /* compute gradients */
122 Ra
= x
? R(cur
, x
- stride
) : R(last
, x
);
124 Rc
= x
? R(last
, x
- stride
) : last2
;
125 Rd
= (x
>= w
- stride
) ? R(last
, x
) : R(last
, x
+ stride
);
131 if((FFABS(D0
) <= state
->near
) && (FFABS(D1
) <= state
->near
) && (FFABS(D2
) <= state
->near
)) {
132 int RUNval
, RItype
, run
;
136 while(x
< w
&& (FFABS(R(cur
, x
) - RUNval
) <= state
->near
)){
141 ls_encode_run(state
, pb
, run
, comp
, x
< w
);
145 RItype
= (FFABS(Ra
- Rb
) <= state
->near
);
146 pred
= RItype
? Ra
: Rb
;
147 err
= R(cur
, x
) - pred
;
149 if(!RItype
&& Ra
> Rb
)
154 err
= (state
->near
+ err
) / state
->twonear
;
156 err
= -(state
->near
- err
) / state
->twonear
;
158 if(RItype
|| (Rb
>= Ra
))
159 Ra
= av_clip(pred
+ err
* state
->twonear
, 0, state
->maxval
);
161 Ra
= av_clip(pred
- err
* state
->twonear
, 0, state
->maxval
);
166 if(err
>= ((state
->range
+ 1) >> 1))
169 ls_encode_runterm(state
, pb
, RItype
, err
, ff_log2_run
[state
->run_index
[comp
]]);
171 if(state
->run_index
[comp
] > 0)
172 state
->run_index
[comp
]--;
173 } else { /* regular mode */
176 context
= ff_jpegls_quantize(state
, D0
) * 81 + ff_jpegls_quantize(state
, D1
) * 9 + ff_jpegls_quantize(state
, D2
);
177 pred
= mid_pred(Ra
, Ra
+ Rb
- Rc
, Rb
);
182 pred
= av_clip(pred
- state
->C
[context
], 0, state
->maxval
);
183 err
= pred
- R(cur
, x
);
186 pred
= av_clip(pred
+ state
->C
[context
], 0, state
->maxval
);
187 err
= R(cur
, x
) - pred
;
192 err
= (state
->near
+ err
) / state
->twonear
;
194 err
= -(state
->near
- err
) / state
->twonear
;
196 Ra
= av_clip(pred
+ err
* state
->twonear
, 0, state
->maxval
);
198 Ra
= av_clip(pred
- err
* state
->twonear
, 0, state
->maxval
);
202 ls_encode_regular(state
, pb
, context
, err
);
208 static void ls_store_lse(JLSState
*state
, PutBitContext
*pb
){
209 /* Test if we have default params and don't need to store LSE */
211 memset(&state2
, 0, sizeof(JLSState
));
212 state2
.bpp
= state
->bpp
;
213 state2
.near
= state
->near
;
214 ff_jpegls_reset_coding_parameters(&state2
, 1);
215 if(state
->T1
== state2
.T1
&& state
->T2
== state2
.T2
&& state
->T3
== state2
.T3
&& state
->reset
== state2
.reset
)
217 /* store LSE type 1 */
219 put_bits(pb
, 16, 13);
221 put_bits(pb
, 16, state
->maxval
);
222 put_bits(pb
, 16, state
->T1
);
223 put_bits(pb
, 16, state
->T2
);
224 put_bits(pb
, 16, state
->T3
);
225 put_bits(pb
, 16, state
->reset
);
228 static int encode_picture_ls(AVCodecContext
*avctx
, unsigned char *buf
, int buf_size
, void *data
){
229 JpeglsContext
* const s
= avctx
->priv_data
;
230 AVFrame
*pict
= data
;
231 AVFrame
* const p
= (AVFrame
*)&s
->picture
;
232 const int near
= avctx
->prediction_method
;
233 PutBitContext pb
, pb2
;
235 uint8_t *buf2
, *zero
, *cur
, *last
;
240 buf2
= av_malloc(buf_size
);
242 init_put_bits(&pb
, buf
, buf_size
);
243 init_put_bits(&pb2
, buf2
, buf_size
);
246 p
->pict_type
= FF_I_TYPE
;
249 if(avctx
->pix_fmt
== PIX_FMT_GRAY8
|| avctx
->pix_fmt
== PIX_FMT_GRAY16
)
254 /* write our own JPEG header, can't use mjpeg_picture_header */
255 put_marker(&pb
, SOI
);
256 put_marker(&pb
, SOF48
);
257 put_bits(&pb
, 16, 8 + comps
* 3); // header size depends on components
258 put_bits(&pb
, 8, (avctx
->pix_fmt
== PIX_FMT_GRAY16
) ? 16 : 8); // bpp
259 put_bits(&pb
, 16, avctx
->height
);
260 put_bits(&pb
, 16, avctx
->width
);
261 put_bits(&pb
, 8, comps
); // components
262 for(i
= 1; i
<= comps
; i
++) {
263 put_bits(&pb
, 8, i
); // component ID
264 put_bits(&pb
, 8, 0x11); // subsampling: none
265 put_bits(&pb
, 8, 0); // Tiq, used by JPEG-LS ext
268 put_marker(&pb
, SOS
);
269 put_bits(&pb
, 16, 6 + comps
* 2);
270 put_bits(&pb
, 8, comps
);
271 for(i
= 1; i
<= comps
; i
++) {
272 put_bits(&pb
, 8, i
); // component ID
273 put_bits(&pb
, 8, 0); // mapping index: none
275 put_bits(&pb
, 8, near
);
276 put_bits(&pb
, 8, (comps
> 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
277 put_bits(&pb
, 8, 0); // point transform: none
279 state
= av_mallocz(sizeof(JLSState
));
280 /* initialize JPEG-LS state from JPEG parameters */
282 state
->bpp
= (avctx
->pix_fmt
== PIX_FMT_GRAY16
) ? 16 : 8;
283 ff_jpegls_reset_coding_parameters(state
, 0);
284 ff_jpegls_init_state(state
);
286 ls_store_lse(state
, &pb
);
288 zero
= av_mallocz(p
->linesize
[0]);
291 if(avctx
->pix_fmt
== PIX_FMT_GRAY8
){
294 for(i
= 0; i
< avctx
->height
; i
++) {
295 ls_encode_line(state
, &pb2
, last
, cur
, t
, avctx
->width
, 1, 0, 8);
298 cur
+= p
->linesize
[0];
300 }else if(avctx
->pix_fmt
== PIX_FMT_GRAY16
){
303 for(i
= 0; i
< avctx
->height
; i
++) {
304 ls_encode_line(state
, &pb2
, last
, cur
, t
, avctx
->width
, 1, 0, 16);
305 t
= *((uint16_t*)last
);
307 cur
+= p
->linesize
[0];
309 }else if(avctx
->pix_fmt
== PIX_FMT_RGB24
){
311 int Rc
[3] = {0, 0, 0};
313 width
= avctx
->width
* 3;
314 for(i
= 0; i
< avctx
->height
; i
++) {
315 for(j
= 0; j
< 3; j
++) {
316 ls_encode_line(state
, &pb2
, last
+ j
, cur
+ j
, Rc
[j
], width
, 3, j
, 8);
320 cur
+= s
->picture
.linesize
[0];
322 }else if(avctx
->pix_fmt
== PIX_FMT_BGR24
){
324 int Rc
[3] = {0, 0, 0};
326 width
= avctx
->width
* 3;
327 for(i
= 0; i
< avctx
->height
; i
++) {
328 for(j
= 2; j
>= 0; j
--) {
329 ls_encode_line(state
, &pb2
, last
+ j
, cur
+ j
, Rc
[j
], width
, 3, j
, 8);
333 cur
+= s
->picture
.linesize
[0];
340 // the specification says that after doing 0xff escaping unused bits in the
341 // last byte must be set to 0, so just append 7 "optional" zero-bits to
342 // avoid special-casing.
343 put_bits(&pb2
, 7, 0);
344 size
= put_bits_count(&pb2
);
345 flush_put_bits(&pb2
);
346 /* do escape coding */
347 init_get_bits(&gb
, buf2
, size
);
349 while(get_bits_count(&gb
) < size
){
351 v
= get_bits(&gb
, 8);
354 v
= get_bits(&gb
, 7);
362 put_marker(&pb
, EOI
);
367 return put_bits_count(&pb
) >> 3;
370 static int encode_init_ls(AVCodecContext
*ctx
) {
371 JpeglsContext
*c
= (JpeglsContext
*)ctx
->priv_data
;
374 ctx
->coded_frame
= &c
->picture
;
376 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
){
377 av_log(ctx
, AV_LOG_ERROR
, "Only grayscale and RGB24/BGR24 images are supported\n");
383 AVCodec jpegls_encoder
= { //FIXME avoid MPV_* lossless jpeg shouldnt need them
387 sizeof(JpeglsContext
),
391 .pix_fmts
= (enum PixelFormat
[]){PIX_FMT_BGR24
, PIX_FMT_RGB24
, PIX_FMT_GRAY8
, PIX_FMT_GRAY16
, -1},