annotate jpeglsenc.c @ 9746:e8421cfcc381 libavcodec

lclenc.c: compress directly into output buffer instead of using a pointless temporary buffer and then using put_bits to copy the data over.
author reimar
date Sun, 31 May 2009 08:49:27 +0000
parents 0dce4fe6e6f3
children 38cfe222e1a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5003
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
1 /*
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
2 * JPEG-LS encoder
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
3 * Copyright (c) 2003 Michael Niedermayer
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
4 * Copyright (c) 2006 Konstantin Shishkov
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
5 *
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
6 * This file is part of FFmpeg.
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
7 *
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
12 *
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
16 * Lesser General Public License for more details.
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
17 *
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
21 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
22
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
23 /**
8718
e9d9d946f213 Use full internal pathname in doxygen @file directives.
diego
parents: 8627
diff changeset
24 * @file libavcodec/jpeglsenc.c
5003
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
25 * JPEG-LS encoder.
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
26 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
27
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
28 #include "avcodec.h"
9428
0dce4fe6e6f3 Rename bitstream.h to get_bits.h.
stefano
parents: 8718
diff changeset
29 #include "get_bits.h"
5003
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
30 #include "golomb.h"
8627
d6bab465b82c moves mid_pred() into mathops.h (with arch specific code split by directory)
aurel
parents: 7040
diff changeset
31 #include "mathops.h"
5045
f0e079daad30 move MJpegDecodeContext declaration from mjpeg.h to mjpegdec.h
aurel
parents: 5003
diff changeset
32 #include "dsputil.h"
5003
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
33 #include "mjpeg.h"
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
34 #include "jpegls.h"
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
35
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
36
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
37 /**
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
38 * Encode error from regular symbol
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
39 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
40 static inline void ls_encode_regular(JLSState *state, PutBitContext *pb, int Q, int err){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
41 int k;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
42 int val;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
43 int map;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
44
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
45 for(k = 0; (state->N[Q] << k) < state->A[Q]; k++);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
46
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
47 map = !state->near && !k && (2 * state->B[Q] <= -state->N[Q]);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
48
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
49 if(err < 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
50 err += state->range;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
51 if(err >= ((state->range + 1) >> 1)) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
52 err -= state->range;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
53 val = 2 * FFABS(err) - 1 - map;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
54 } else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
55 val = 2 * err + map;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
56
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
57 set_ur_golomb_jpegls(pb, val, k, state->limit, state->qbpp);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
58
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
59 ff_jpegls_update_state_regular(state, Q, err);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
60 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
61
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
62 /**
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
63 * Encode error from run termination
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
64 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
65 static inline void ls_encode_runterm(JLSState *state, PutBitContext *pb, int RItype, int err, int limit_add){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
66 int k;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
67 int val, map;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
68 int Q = 365 + RItype;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
69 int temp;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
70
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
71 temp = state->A[Q];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
72 if(RItype)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
73 temp += state->N[Q] >> 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
74 for(k = 0; (state->N[Q] << k) < temp; k++);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
75 map = 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
76 if(!k && err && (2 * state->B[Q] < state->N[Q]))
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
77 map = 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
78
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
79 if(err < 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
80 val = - (2 * err) - 1 - RItype + map;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
81 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
82 val = 2 * err - RItype - map;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
83 set_ur_golomb_jpegls(pb, val, k, state->limit - limit_add - 1, state->qbpp);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
84
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
85 if(err < 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
86 state->B[Q]++;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
87 state->A[Q] += (val + 1 - RItype) >> 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
88
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
89 ff_jpegls_downscale_state(state, Q);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
90 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
91
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
92 /**
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
93 * Encode run value as specified by JPEG-LS standard
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
94 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
95 static inline void ls_encode_run(JLSState *state, PutBitContext *pb, int run, int comp, int trail){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
96 while(run >= (1 << ff_log2_run[state->run_index[comp]])){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
97 put_bits(pb, 1, 1);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
98 run -= 1 << ff_log2_run[state->run_index[comp]];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
99 if(state->run_index[comp] < 31)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
100 state->run_index[comp]++;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
101 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
102 /* if hit EOL, encode another full run, else encode aborted run */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
103 if(!trail && run) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
104 put_bits(pb, 1, 1);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
105 }else if(trail){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
106 put_bits(pb, 1, 0);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
107 if(ff_log2_run[state->run_index[comp]])
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
108 put_bits(pb, ff_log2_run[state->run_index[comp]], run);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
109 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
110 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
111
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
112 /**
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
113 * Encode one line of image
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
114 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
115 static inline void ls_encode_line(JLSState *state, PutBitContext *pb, void *last, void *cur, int last2, int w, int stride, int comp, int bits){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
116 int x = 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
117 int Ra, Rb, Rc, Rd;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
118 int D0, D1, D2;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
119
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
120 while(x < w) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
121 int err, pred, sign;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
122
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
123 /* compute gradients */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
124 Ra = x ? R(cur, x - stride) : R(last, x);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
125 Rb = R(last, x);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
126 Rc = x ? R(last, x - stride) : last2;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
127 Rd = (x >= w - stride) ? R(last, x) : R(last, x + stride);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
128 D0 = Rd - Rb;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
129 D1 = Rb - Rc;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
130 D2 = Rc - Ra;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
131
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
132 /* run mode */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
133 if((FFABS(D0) <= state->near) && (FFABS(D1) <= state->near) && (FFABS(D2) <= state->near)) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
134 int RUNval, RItype, run;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
135
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
136 run = 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
137 RUNval = Ra;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
138 while(x < w && (FFABS(R(cur, x) - RUNval) <= state->near)){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
139 run++;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
140 W(cur, x, Ra);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
141 x += stride;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
142 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
143 ls_encode_run(state, pb, run, comp, x < w);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
144 if(x >= w)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
145 return;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
146 Rb = R(last, x);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
147 RItype = (FFABS(Ra - Rb) <= state->near);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
148 pred = RItype ? Ra : Rb;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
149 err = R(cur, x) - pred;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
150
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
151 if(!RItype && Ra > Rb)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
152 err = -err;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
153
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
154 if(state->near){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
155 if(err > 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
156 err = (state->near + err) / state->twonear;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
157 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
158 err = -(state->near - err) / state->twonear;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
159
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
160 if(RItype || (Rb >= Ra))
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
161 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
162 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
163 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
164 W(cur, x, Ra);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
165 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
166 if(err < 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
167 err += state->range;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
168 if(err >= ((state->range + 1) >> 1))
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
169 err -= state->range;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
170
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
171 ls_encode_runterm(state, pb, RItype, err, ff_log2_run[state->run_index[comp]]);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
172
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
173 if(state->run_index[comp] > 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
174 state->run_index[comp]--;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
175 } else { /* regular mode */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
176 int context;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
177
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
178 context = ff_jpegls_quantize(state, D0) * 81 + ff_jpegls_quantize(state, D1) * 9 + ff_jpegls_quantize(state, D2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
179 pred = mid_pred(Ra, Ra + Rb - Rc, Rb);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
180
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
181 if(context < 0){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
182 context = -context;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
183 sign = 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
184 pred = av_clip(pred - state->C[context], 0, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
185 err = pred - R(cur, x);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
186 }else{
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
187 sign = 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
188 pred = av_clip(pred + state->C[context], 0, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
189 err = R(cur, x) - pred;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
190 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
191
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
192 if(state->near){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
193 if(err > 0)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
194 err = (state->near + err) / state->twonear;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
195 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
196 err = -(state->near - err) / state->twonear;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
197 if(!sign)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
198 Ra = av_clip(pred + err * state->twonear, 0, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
199 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
200 Ra = av_clip(pred - err * state->twonear, 0, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
201 W(cur, x, Ra);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
202 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
203
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
204 ls_encode_regular(state, pb, context, err);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
205 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
206 x += stride;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
207 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
208 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
209
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
210 static void ls_store_lse(JLSState *state, PutBitContext *pb){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
211 /* Test if we have default params and don't need to store LSE */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
212 JLSState state2;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
213 memset(&state2, 0, sizeof(JLSState));
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
214 state2.bpp = state->bpp;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
215 state2.near = state->near;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
216 ff_jpegls_reset_coding_parameters(&state2, 1);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
217 if(state->T1 == state2.T1 && state->T2 == state2.T2 && state->T3 == state2.T3 && state->reset == state2.reset)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
218 return;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
219 /* store LSE type 1 */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
220 put_marker(pb, LSE);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
221 put_bits(pb, 16, 13);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
222 put_bits(pb, 8, 1);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
223 put_bits(pb, 16, state->maxval);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
224 put_bits(pb, 16, state->T1);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
225 put_bits(pb, 16, state->T2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
226 put_bits(pb, 16, state->T3);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
227 put_bits(pb, 16, state->reset);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
228 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
229
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
230 static int encode_picture_ls(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
231 JpeglsContext * const s = avctx->priv_data;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
232 AVFrame *pict = data;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
233 AVFrame * const p= (AVFrame*)&s->picture;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
234 const int near = avctx->prediction_method;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
235 PutBitContext pb, pb2;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
236 GetBitContext gb;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
237 uint8_t *buf2, *zero, *cur, *last;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
238 JLSState *state;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
239 int i, size;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
240 int comps;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
241
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
242 buf2 = av_malloc(buf_size);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
243
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
244 init_put_bits(&pb, buf, buf_size);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
245 init_put_bits(&pb2, buf2, buf_size);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
246
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
247 *p = *pict;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
248 p->pict_type= FF_I_TYPE;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
249 p->key_frame= 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
250
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
251 if(avctx->pix_fmt == PIX_FMT_GRAY8 || avctx->pix_fmt == PIX_FMT_GRAY16)
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
252 comps = 1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
253 else
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
254 comps = 3;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
255
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
256 /* write our own JPEG header, can't use mjpeg_picture_header */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
257 put_marker(&pb, SOI);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
258 put_marker(&pb, SOF48);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
259 put_bits(&pb, 16, 8 + comps * 3); // header size depends on components
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
260 put_bits(&pb, 8, (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8); // bpp
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
261 put_bits(&pb, 16, avctx->height);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
262 put_bits(&pb, 16, avctx->width);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
263 put_bits(&pb, 8, comps); // components
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
264 for(i = 1; i <= comps; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
265 put_bits(&pb, 8, i); // component ID
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
266 put_bits(&pb, 8, 0x11); // subsampling: none
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
267 put_bits(&pb, 8, 0); // Tiq, used by JPEG-LS ext
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
268 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
269
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
270 put_marker(&pb, SOS);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
271 put_bits(&pb, 16, 6 + comps * 2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
272 put_bits(&pb, 8, comps);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
273 for(i = 1; i <= comps; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
274 put_bits(&pb, 8, i); // component ID
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
275 put_bits(&pb, 8, 0); // mapping index: none
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
276 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
277 put_bits(&pb, 8, near);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
278 put_bits(&pb, 8, (comps > 1) ? 1 : 0); // interleaving: 0 - plane, 1 - line
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
279 put_bits(&pb, 8, 0); // point transform: none
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
280
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
281 state = av_mallocz(sizeof(JLSState));
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
282 /* initialize JPEG-LS state from JPEG parameters */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
283 state->near = near;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
284 state->bpp = (avctx->pix_fmt == PIX_FMT_GRAY16) ? 16 : 8;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
285 ff_jpegls_reset_coding_parameters(state, 0);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
286 ff_jpegls_init_state(state);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
287
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
288 ls_store_lse(state, &pb);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
289
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
290 zero = av_mallocz(p->linesize[0]);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
291 last = zero;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
292 cur = p->data[0];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
293 if(avctx->pix_fmt == PIX_FMT_GRAY8){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
294 int t = 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
295
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
296 for(i = 0; i < avctx->height; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
297 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 8);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
298 t = last[0];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
299 last = cur;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
300 cur += p->linesize[0];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
301 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
302 }else if(avctx->pix_fmt == PIX_FMT_GRAY16){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
303 int t = 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
304
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
305 for(i = 0; i < avctx->height; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
306 ls_encode_line(state, &pb2, last, cur, t, avctx->width, 1, 0, 16);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
307 t = *((uint16_t*)last);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
308 last = cur;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
309 cur += p->linesize[0];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
310 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
311 }else if(avctx->pix_fmt == PIX_FMT_RGB24){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
312 int j, width;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
313 int Rc[3] = {0, 0, 0};
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
314
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
315 width = avctx->width * 3;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
316 for(i = 0; i < avctx->height; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
317 for(j = 0; j < 3; j++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
318 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
319 Rc[j] = last[j];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
320 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
321 last = cur;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
322 cur += s->picture.linesize[0];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
323 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
324 }else if(avctx->pix_fmt == PIX_FMT_BGR24){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
325 int j, width;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
326 int Rc[3] = {0, 0, 0};
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
327
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
328 width = avctx->width * 3;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
329 for(i = 0; i < avctx->height; i++) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
330 for(j = 2; j >= 0; j--) {
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
331 ls_encode_line(state, &pb2, last + j, cur + j, Rc[j], width, 3, j, 8);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
332 Rc[j] = last[j];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
333 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
334 last = cur;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
335 cur += s->picture.linesize[0];
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
336 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
337 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
338
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
339 av_free(zero);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
340 av_free(state);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
341
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
342 // the specification says that after doing 0xff escaping unused bits in the
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
343 // last byte must be set to 0, so just append 7 "optional" zero-bits to
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
344 // avoid special-casing.
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
345 put_bits(&pb2, 7, 0);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
346 size = put_bits_count(&pb2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
347 flush_put_bits(&pb2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
348 /* do escape coding */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
349 init_get_bits(&gb, buf2, size);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
350 size -= 7;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
351 while(get_bits_count(&gb) < size){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
352 int v;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
353 v = get_bits(&gb, 8);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
354 put_bits(&pb, 8, v);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
355 if(v == 0xFF){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
356 v = get_bits(&gb, 7);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
357 put_bits(&pb, 8, v);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
358 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
359 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
360 align_put_bits(&pb);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
361 av_free(buf2);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
362
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
363 /* End of image */
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
364 put_marker(&pb, EOI);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
365 flush_put_bits(&pb);
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
366
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
367 emms_c();
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
368
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
369 return put_bits_count(&pb) >> 3;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
370 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
371
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 5127
diff changeset
372 static av_cold int encode_init_ls(AVCodecContext *ctx) {
5003
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
373 JpeglsContext *c = (JpeglsContext*)ctx->priv_data;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
374
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
375 c->avctx = ctx;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
376 ctx->coded_frame = &c->picture;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
377
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
378 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){
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
379 av_log(ctx, AV_LOG_ERROR, "Only grayscale and RGB24/BGR24 images are supported\n");
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
380 return -1;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
381 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
382 return 0;
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
383 }
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
384
5127
4dbe6578f811 misc spelling fixes
diego
parents: 5045
diff changeset
385 AVCodec jpegls_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
5003
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
386 "jpegls",
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
387 CODEC_TYPE_VIDEO,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
388 CODEC_ID_JPEGLS,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
389 sizeof(JpeglsContext),
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
390 encode_init_ls,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
391 encode_picture_ls,
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
392 NULL,
6788
e1302edb0f69 Replace some occurrences of -1 with PIX_FMT_NONE.
cehoyos
parents: 6712
diff changeset
393 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB24, PIX_FMT_GRAY8, PIX_FMT_GRAY16, PIX_FMT_NONE},
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6788
diff changeset
394 .long_name= NULL_IF_CONFIG_SMALL("JPEG-LS"),
5003
ddb28de352bb split jpeg_ls into jpeglsdec, jpeglsenc and jpegls
aurel
parents:
diff changeset
395 };