summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/rbcodec/codecs/libffmpegFLAC/decoder.c248
-rw-r--r--lib/rbcodec/codecs/libffmpegFLAC/decoder.h11
-rw-r--r--lib/rbcodec/codecs/libffmpegFLAC/golomb.h77
3 files changed, 198 insertions, 138 deletions
diff --git a/lib/rbcodec/codecs/libffmpegFLAC/decoder.c b/lib/rbcodec/codecs/libffmpegFLAC/decoder.c
index 2e92c4b90d..107315c2f7 100644
--- a/lib/rbcodec/codecs/libffmpegFLAC/decoder.c
+++ b/lib/rbcodec/codecs/libffmpegFLAC/decoder.c
@@ -135,64 +135,73 @@ static int get_crc8(const uint8_t *buf, int count)
135} 135}
136 136
137static int decode_residuals(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC; 137static int decode_residuals(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC;
138static int decode_residuals(FLACContext *s, int32_t* decoded, int pred_order) 138static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
139{ 139{
140 GetBitContext gb = s->gb;
140 int i, tmp, partition, method_type, rice_order; 141 int i, tmp, partition, method_type, rice_order;
141 int sample = 0, samples; 142 int rice_bits, rice_esc;
143 int samples;
142 144
143 method_type = get_bits(&s->gb, 2); 145 method_type = get_bits(&gb, 2);
144 if (method_type > 1){ 146 rice_order = get_bits(&gb, 4);
145 //fprintf(stderr,"illegal residual coding method %d\n", method_type); 147
146 return -3; 148 samples = s->blocksize >> rice_order;
147 } 149 rice_bits = 4 + method_type;
148 150 rice_esc = (1 << rice_bits) - 1;
149 rice_order = get_bits(&s->gb, 4);
150 151
151 samples= s->blocksize >> rice_order; 152 decoded += pred_order;
153 i = pred_order;
152 154
153 sample= 155 if (method_type > 1 || (samples << rice_order != s->blocksize) || pred_order > samples)
154 i= pred_order;
155 for (partition = 0; partition < (1 << rice_order); partition++)
156 { 156 {
157 tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5); 157 return -3;
158 if (tmp == (method_type == 0 ? 15 : 31)) 158 }
159 { 159
160 //fprintf(stderr,"fixed len partition\n"); 160 for (partition = 0; partition < (1 << rice_order); partition++) {
161 tmp = get_bits(&s->gb, 5); 161 tmp = get_bits(&gb, rice_bits);
162 for (; i < samples; i++, sample++) 162 if (tmp == rice_esc) {
163 decoded[sample] = get_sbits(&s->gb, tmp); 163 tmp = get_bits(&gb, 5);
164 } 164 for (; i < samples; i++)
165 else 165 *decoded++ = get_sbits(&gb, tmp);
166 { 166 } else {
167 for (; i < samples; i++, sample++){ 167 int real_limit = tmp ? (INT_MAX >> tmp) + 2 : INT_MAX;
168 decoded[sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0); 168 for (; i < samples; i++) {
169 int v = get_sr_golomb_flac(&gb, tmp, real_limit, 0);
170 if ((unsigned) v == 0x80000000){
171 return -3;
172 }
173
174 *decoded++ = v;
169 } 175 }
170 } 176 }
171 i= 0; 177 i= 0;
172 } 178 }
173 179
180 s->gb = gb;
181
174 return 0; 182 return 0;
175} 183}
176 184
177static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC; 185static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order, int bps) ICODE_ATTR_FLAC;
178static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order) 186static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order, int bps)
179{ 187{
180 const int blocksize = s->blocksize; 188 const int blocksize = s->blocksize;
181 int a, b, c, d, i; 189 unsigned a, b, c, d;
182 190 int i;
191
183 /* warm up samples */ 192 /* warm up samples */
184 for (i = 0; i < pred_order; i++) 193 for (i = 0; i < pred_order; i++)
185 { 194 {
186 decoded[i] = get_sbits(&s->gb, s->curr_bps); 195 decoded[i] = get_sbits(&s->gb, bps);
187 } 196 }
188 197
189 if (decode_residuals(s, decoded, pred_order) < 0) 198 if (decode_residuals(s, decoded, pred_order) < 0)
190 return -4; 199 return -4;
191 200
192 a = decoded[pred_order-1]; 201 a = decoded[pred_order-1];
193 b = a - decoded[pred_order-2]; 202 b = a - decoded[pred_order-2];
194 c = b - decoded[pred_order-2] + decoded[pred_order-3]; 203 c = b - decoded[pred_order-2] + decoded[pred_order-3];
195 d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4]; 204 d = c - decoded[pred_order-2] + 2U*decoded[pred_order-3] - decoded[pred_order-4];
196 205
197 switch(pred_order) 206 switch(pred_order)
198 { 207 {
@@ -221,20 +230,63 @@ static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_orde
221 return 0; 230 return 0;
222} 231}
223 232
224static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC; 233static void flac_lpc_32_c(int32_t *decoded, int coeffs[],
225static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order) 234 int pred_order, int qlevel, int len) ICODE_ATTR_FLAC;
235static void flac_lpc_32_c(int32_t *decoded, int coeffs[],
236 int pred_order, int qlevel, int len)
237{
238 int i, j;
239 /*NOTE coeffs[] is in reverse order compared to upstream*/
240 for (i = pred_order; i < len; i++, decoded++) {
241 int64_t sum = 0;
242 for (j = 0; j < pred_order; j++)
243 sum += (int64_t)coeffs[pred_order-j-1] * decoded[j];
244 decoded[j] += sum >> qlevel;
245 }
246
247}
248
249static void lpc_analyze_remodulate(int32_t *decoded, int coeffs[],
250 int order, int qlevel, int len, int bps)
251{
252 /*NOTE coeffs[] is in reverse order compared to upstream*/
253 int i, j;
254 int ebps = 1 << (bps-1);
255 unsigned sigma = 0;
256
257 for (i = order; i < len; i++)
258 sigma |= decoded[i] + ebps;
259
260 if (sigma < 2U*ebps)
261 return;
262
263 for (i = len - 1; i >= order; i--) {
264 int64_t p = 0;
265 for (j = 0; j < order; j++)
266 p += coeffs[order-j-1] * (int64_t)(int32_t)decoded[i-order+j];
267 decoded[i] -= p >> qlevel;
268 }
269 for (i = order; i < len; i++, decoded++) {
270 int32_t p = 0;
271 for (j = 0; j < order; j++)
272 p += coeffs[order-j-1] * (uint32_t)decoded[j];
273 decoded[j] += p >> qlevel;
274 }
275}
276
277static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order, int bps) ICODE_ATTR_FLAC;
278static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order, int bps)
226{ 279{
227 int sum, i, j; 280 int sum, i, j;
228 int64_t wsum;
229 int coeff_prec, qlevel; 281 int coeff_prec, qlevel;
230 int coeffs[pred_order]; 282 int coeffs[pred_order];
231 283
232 /* warm up samples */ 284 /* warm up samples */
233 for (i = 0; i < pred_order; i++) 285 for (i = 0; i < pred_order; i++)
234 { 286 {
235 decoded[i] = get_sbits(&s->gb, s->curr_bps); 287 decoded[i] = get_sbits(&s->gb, bps);
236 } 288 }
237 289
238 coeff_prec = get_bits(&s->gb, 4) + 1; 290 coeff_prec = get_bits(&s->gb, 4) + 1;
239 if (coeff_prec == 16) 291 if (coeff_prec == 16)
240 { 292 {
@@ -276,21 +328,18 @@ static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order)
276 #endif 328 #endif
277 } else { 329 } else {
278 #if defined(CPU_COLDFIRE) 330 #if defined(CPU_COLDFIRE)
279 (void)wsum;
280 (void)j; 331 (void)j;
281 lpc_decode_emac_wide(s->blocksize - pred_order, qlevel, pred_order, 332 lpc_decode_emac_wide(s->blocksize - pred_order, qlevel, pred_order,
282 decoded + pred_order, coeffs); 333 decoded + pred_order, coeffs);
283 #else 334 #else
284 for (i = pred_order; i < s->blocksize; i++) 335
285 { 336 flac_lpc_32_c(decoded, coeffs, pred_order, qlevel, s->blocksize);
286 wsum = 0; 337
287 for (j = 0; j < pred_order; j++) 338 if (bps <= 16)
288 wsum += (int64_t)coeffs[j] * (int64_t)decoded[i-j-1]; 339 lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps);
289 decoded[i] += wsum >> qlevel;
290 }
291 #endif 340 #endif
292 } 341 }
293 342
294 return 0; 343 return 0;
295} 344}
296 345
@@ -298,14 +347,13 @@ static inline int decode_subframe(FLACContext *s, int channel, int32_t* decoded)
298{ 347{
299 int type, wasted = 0; 348 int type, wasted = 0;
300 int i, tmp; 349 int i, tmp;
301 350 int bps = s->bps;
302 s->curr_bps = s->bps;
303 if(channel == 0){ 351 if(channel == 0){
304 if(s->decorrelation == RIGHT_SIDE) 352 if(s->ch_mode == RIGHT_SIDE)
305 s->curr_bps++; 353 bps++;
306 }else{ 354 }else{
307 if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE) 355 if(s->ch_mode == LEFT_SIDE || s->ch_mode == MID_SIDE)
308 s->curr_bps++; 356 bps++;
309 } 357 }
310 358
311 if (get_bits1(&s->gb)) 359 if (get_bits1(&s->gb))
@@ -314,35 +362,21 @@ static inline int decode_subframe(FLACContext *s, int channel, int32_t* decoded)
314 return -9; 362 return -9;
315 } 363 }
316 type = get_bits(&s->gb, 6); 364 type = get_bits(&s->gb, 6);
317// wasted = get_bits1(&s->gb); 365
318
319// if (wasted)
320// {
321// while (!get_bits1(&s->gb))
322// wasted++;
323// if (wasted)
324// wasted++;
325// s->curr_bps -= wasted;
326// }
327#if 0
328 wasted= 16 - av_log2(show_bits(&s->gb, 17));
329 skip_bits(&s->gb, wasted+1);
330 s->curr_bps -= wasted;
331#else
332 if (get_bits1(&s->gb)) 366 if (get_bits1(&s->gb))
333 { 367 {
334 wasted = 1; 368 wasted = 1;
335 while (!get_bits1(&s->gb)) 369 while (!get_bits1(&s->gb))
336 wasted++; 370 wasted++;
337 s->curr_bps -= wasted; 371 bps -= wasted;
338 //fprintf(stderr,"%d wasted bits\n", wasted); 372 //fprintf(stderr,"%d wasted bits\n", wasted);
339 } 373 }
340#endif 374
341//FIXME use av_log2 for types 375//FIXME use av_log2 for types
342 if (type == 0) 376 if (type == 0)
343 { 377 {
344 //fprintf(stderr,"coding type: constant\n"); 378 //fprintf(stderr,"coding type: constant\n");
345 tmp = get_sbits(&s->gb, s->curr_bps); 379 tmp = get_sbits(&s->gb, bps);
346 for (i = 0; i < s->blocksize; i++) 380 for (i = 0; i < s->blocksize; i++)
347 decoded[i] = tmp; 381 decoded[i] = tmp;
348 } 382 }
@@ -350,18 +384,18 @@ static inline int decode_subframe(FLACContext *s, int channel, int32_t* decoded)
350 { 384 {
351 //fprintf(stderr,"coding type: verbatim\n"); 385 //fprintf(stderr,"coding type: verbatim\n");
352 for (i = 0; i < s->blocksize; i++) 386 for (i = 0; i < s->blocksize; i++)
353 decoded[i] = get_sbits(&s->gb, s->curr_bps); 387 decoded[i] = get_sbits(&s->gb, bps);
354 } 388 }
355 else if ((type >= 8) && (type <= 12)) 389 else if ((type >= 8) && (type <= 12))
356 { 390 {
357 //fprintf(stderr,"coding type: fixed\n"); 391 //fprintf(stderr,"coding type: fixed\n");
358 if (decode_subframe_fixed(s, decoded, type & ~0x8) < 0) 392 if (decode_subframe_fixed(s, decoded, type & ~0x8, bps) < 0)
359 return -10; 393 return -10;
360 } 394 }
361 else if (type >= 32) 395 else if (type >= 32)
362 { 396 {
363 //fprintf(stderr,"coding type: lpc\n"); 397 //fprintf(stderr,"coding type: lpc\n");
364 if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1) < 0) 398 if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps) < 0)
365 return -11; 399 return -11;
366 } 400 }
367 else 401 else
@@ -369,12 +403,12 @@ static inline int decode_subframe(FLACContext *s, int channel, int32_t* decoded)
369 //fprintf(stderr,"Unknown coding type: %d\n",type); 403 //fprintf(stderr,"Unknown coding type: %d\n",type);
370 return -12; 404 return -12;
371 } 405 }
372 406
373 if (wasted) 407 if (wasted && wasted < 32)
374 { 408 {
375 int i; 409 int i;
376 for (i = 0; i < s->blocksize; i++) 410 for (i = 0; i < s->blocksize; i++)
377 decoded[i] <<= wasted; 411 decoded[i] = (unsigned)decoded[i] << wasted;
378 } 412 }
379 413
380 return 0; 414 return 0;
@@ -385,25 +419,26 @@ static int decode_frame(FLACContext *s,
385static int decode_frame(FLACContext *s, 419static int decode_frame(FLACContext *s,
386 void (*yield)(void)) 420 void (*yield)(void))
387{ 421{
388 int blocksize_code, sample_rate_code, sample_size_code, assignment, crc8; 422 int blocksize_code, sample_rate_code, sample_size_code, crc8;
389 int decorrelation, bps, blocksize, samplerate; 423 int ch_mode, bps, blocksize, samplerate;
390 int res, ch; 424 int res, ch;
391 425 GetBitContext *gb = &s->gb;
392 blocksize_code = get_bits(&s->gb, 4);
393 426
394 sample_rate_code = get_bits(&s->gb, 4); 427 blocksize_code = get_bits(gb, 4);
395 428
396 assignment = get_bits(&s->gb, 4); /* channel assignment */ 429 sample_rate_code = get_bits(gb, 4);
397 if (assignment < 8 && s->channels == assignment+1) 430
398 decorrelation = INDEPENDENT; 431 ch_mode = get_bits(gb, 4); /* channel assignment */
399 else if (assignment >=8 && assignment < 11 && s->channels == 2) 432 if (ch_mode < 8 && s->channels == ch_mode+1)
400 decorrelation = LEFT_SIDE + assignment - 8; 433 ch_mode = INDEPENDENT;
434 else if (ch_mode >=8 && ch_mode < 11 && s->channels == 2)
435 ch_mode = LEFT_SIDE + ch_mode - 8;
401 else 436 else
402 { 437 {
403 return -13; 438 return -13;
404 } 439 }
405 440
406 sample_size_code = get_bits(&s->gb, 3); 441 sample_size_code = get_bits(gb, 3);
407 if(sample_size_code == 0) 442 if(sample_size_code == 0)
408 bps= s->bps; 443 bps= s->bps;
409 else if((sample_size_code != 3) && (sample_size_code != 7)) 444 else if((sample_size_code != 3) && (sample_size_code != 7))
@@ -413,13 +448,13 @@ static int decode_frame(FLACContext *s,
413 return -14; 448 return -14;
414 } 449 }
415 450
416 if (get_bits1(&s->gb)) 451 if (get_bits1(gb))
417 { 452 {
418 return -15; 453 return -15;
419 } 454 }
420 455
421 /* Get the samplenumber of the first sample in this block */ 456 /* Get the samplenumber of the first sample in this block */
422 s->samplenumber=get_utf8(&s->gb); 457 s->samplenumber=get_utf8(gb);
423 458
424 /* samplenumber actually contains the frame number for streams 459 /* samplenumber actually contains the frame number for streams
425 with a constant block size - so we multiply by blocksize to 460 with a constant block size - so we multiply by blocksize to
@@ -438,9 +473,9 @@ static int decode_frame(FLACContext *s,
438 if (blocksize_code == 0) 473 if (blocksize_code == 0)
439 blocksize = s->min_blocksize; 474 blocksize = s->min_blocksize;
440 else if (blocksize_code == 6) 475 else if (blocksize_code == 6)
441 blocksize = get_bits(&s->gb, 8)+1; 476 blocksize = get_bits(gb, 8)+1;
442 else if (blocksize_code == 7) 477 else if (blocksize_code == 7)
443 blocksize = get_bits(&s->gb, 16)+1; 478 blocksize = get_bits(gb, 16)+1;
444 else 479 else
445 blocksize = blocksize_table[blocksize_code]; 480 blocksize = blocksize_table[blocksize_code];
446 481
@@ -453,17 +488,17 @@ static int decode_frame(FLACContext *s,
453 }else if ((sample_rate_code < 12)) 488 }else if ((sample_rate_code < 12))
454 samplerate = sample_rate_table[sample_rate_code]; 489 samplerate = sample_rate_table[sample_rate_code];
455 else if (sample_rate_code == 12) 490 else if (sample_rate_code == 12)
456 samplerate = get_bits(&s->gb, 8) * 1000; 491 samplerate = get_bits(gb, 8) * 1000;
457 else if (sample_rate_code == 13) 492 else if (sample_rate_code == 13)
458 samplerate = get_bits(&s->gb, 16); 493 samplerate = get_bits(gb, 16);
459 else if (sample_rate_code == 14) 494 else if (sample_rate_code == 14)
460 samplerate = get_bits(&s->gb, 16) * 10; 495 samplerate = get_bits(gb, 16) * 10;
461 else{ 496 else{
462 return -17; 497 return -17;
463 } 498 }
464 499
465 skip_bits(&s->gb, 8); 500 skip_bits(gb, 8);
466 crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8); 501 crc8= get_crc8(s->gb.buffer, get_bits_count(gb)/8);
467 if(crc8){ 502 if(crc8){
468 return -18; 503 return -18;
469 } 504 }
@@ -471,7 +506,7 @@ static int decode_frame(FLACContext *s,
471 s->blocksize = blocksize; 506 s->blocksize = blocksize;
472 s->samplerate = samplerate; 507 s->samplerate = samplerate;
473 s->bps = bps; 508 s->bps = bps;
474 s->decorrelation= decorrelation; 509 s->ch_mode = ch_mode;
475 510
476 for (ch=0; ch<s->channels; ++ch) { 511 for (ch=0; ch<s->channels; ++ch) {
477 yield(); 512 yield();
@@ -480,10 +515,10 @@ static int decode_frame(FLACContext *s,
480 } 515 }
481 516
482 yield(); 517 yield();
483 align_get_bits(&s->gb); 518 align_get_bits(gb);
484 519
485 /* frame footer */ 520 /* frame footer */
486 skip_bits(&s->gb, 16); /* data crc */ 521 skip_bits(gb, 16); /* data crc */
487 522
488 return 0; 523 return 0;
489} 524}
@@ -576,6 +611,13 @@ int flac_decode_frame(FLACContext *s,
576 int framesize; 611 int framesize;
577 int scale; 612 int scale;
578 613
614 /* check that there is at least the smallest decodable amount of data.
615 this amount corresponds to the smallest valid FLAC frame possible.
616 this amount corresponds to the smallest valid FLAC frame possible.
617 FF F8 69 02 00 00 9A 00 00 34 46 */
618 if (buf_size < MIN_FRAME_SIZE)
619 return buf_size;
620
579 init_get_bits(&s->gb, buf, buf_size*8); 621 init_get_bits(&s->gb, buf, buf_size*8);
580 622
581 tmp = get_bits(&s->gb, 16); 623 tmp = get_bits(&s->gb, 16);
@@ -600,7 +642,7 @@ int flac_decode_frame(FLACContext *s,
600 }\ 642 }\
601 643
602 scale=FLAC_OUTPUT_DEPTH-s->bps; 644 scale=FLAC_OUTPUT_DEPTH-s->bps;
603 switch(s->decorrelation) 645 switch(s->ch_mode)
604 { 646 {
605 case INDEPENDENT: 647 case INDEPENDENT:
606 if (s->channels <= 2) { 648 if (s->channels <= 2) {
diff --git a/lib/rbcodec/codecs/libffmpegFLAC/decoder.h b/lib/rbcodec/codecs/libffmpegFLAC/decoder.h
index 677a21ac98..7bd021dce6 100644
--- a/lib/rbcodec/codecs/libffmpegFLAC/decoder.h
+++ b/lib/rbcodec/codecs/libffmpegFLAC/decoder.h
@@ -3,9 +3,10 @@
3 3
4#include "bitstream.h" 4#include "bitstream.h"
5 5
6#define MAX_CHANNELS 6 /* Maximum supported channels, only left/right will be played back */ 6#define MAX_CHANNELS 6 /* Maximum supported channels, only left/right will be played back */
7#define MAX_BLOCKSIZE 4608 /* Maxsize in samples of one uncompressed frame */ 7#define MAX_BLOCKSIZE 4608 /* Maxsize in samples of one uncompressed frame */
8#define MAX_FRAMESIZE 65536 /* Maxsize in bytes of one compressed frame */ 8#define MAX_FRAMESIZE 65536 /* Maxsize in bytes of one compressed frame */
9#define MIN_FRAME_SIZE 11 /* smallest valid FLAC frame possible */
9 10
10#define FLAC_OUTPUT_DEPTH 29 /* Provide samples left-shifted to 28 bits+sign */ 11#define FLAC_OUTPUT_DEPTH 29 /* Provide samples left-shifted to 28 bits+sign */
11 12
@@ -23,10 +24,10 @@ typedef struct FLACContext {
23 int min_framesize, max_framesize; 24 int min_framesize, max_framesize;
24 int samplerate, channels; 25 int samplerate, channels;
25 int blocksize/*, last_blocksize*/; 26 int blocksize/*, last_blocksize*/;
26 int bps, curr_bps; 27 int bps;
27 unsigned long samplenumber; 28 unsigned long samplenumber;
28 unsigned long totalsamples; 29 unsigned long totalsamples;
29 enum decorrelation_type decorrelation; 30 enum decorrelation_type ch_mode;
30 31
31 int filesize; 32 int filesize;
32 int length; 33 int length;
diff --git a/lib/rbcodec/codecs/libffmpegFLAC/golomb.h b/lib/rbcodec/codecs/libffmpegFLAC/golomb.h
index 197b78ee1c..68d2558e1d 100644
--- a/lib/rbcodec/codecs/libffmpegFLAC/golomb.h
+++ b/lib/rbcodec/codecs/libffmpegFLAC/golomb.h
@@ -33,49 +33,69 @@
33/** 33/**
34 * read unsigned golomb rice code (jpegls). 34 * read unsigned golomb rice code (jpegls).
35 */ 35 */
36static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit, int esc_len){ 36static inline int get_ur_golomb_jpegls(GetBitContext *gb, int k, int limit,
37 int esc_len)
38{
37 unsigned int buf; 39 unsigned int buf;
38 int log; 40 int log;
39 41
40 OPEN_READER(re, gb); 42 OPEN_READER(re, gb);
41 UPDATE_CACHE(re, gb); 43 UPDATE_CACHE(re, gb);
42 buf=GET_CACHE(re, gb); 44 buf = GET_CACHE(re, gb);
45
46 log = av_log2(buf);
43 47
44 log= av_log2(buf); 48 if (log - k >= 32 - MIN_CACHE_BITS + (MIN_CACHE_BITS == 32) &&
45 49 32 - log < limit) {
46 if(log - k >= 32-MIN_CACHE_BITS+(MIN_CACHE_BITS==32) && 32-log < limit){
47 buf >>= log - k; 50 buf >>= log - k;
48 buf += (30-log)<<k; 51 buf += (30U - log) << k;
49 LAST_SKIP_BITS(re, gb, 32 + k - log); 52 LAST_SKIP_BITS(re, gb, 32 + k - log);
50 CLOSE_READER(re, gb); 53 CLOSE_READER(re, gb);
51 54
52 return buf; 55 return buf;
53 }else{ 56 } else {
54 int i; 57 int i;
55 for(i=0; SHOW_UBITS(re, gb, 1) == 0; i++){ 58 for (i = 0; i + MIN_CACHE_BITS <= limit && SHOW_UBITS(re, gb, MIN_CACHE_BITS) == 0; i += MIN_CACHE_BITS) {
56 LAST_SKIP_BITS(re, gb, 1); 59 if (gb->size_in_bits <= (signed) re_index) {
60 CLOSE_READER(re, gb);
61 return -1;
62 }
63 LAST_SKIP_BITS(re, gb, MIN_CACHE_BITS);
57 UPDATE_CACHE(re, gb); 64 UPDATE_CACHE(re, gb);
58 } 65 }
59 SKIP_BITS(re, gb, 1); 66 for (; i < limit && SHOW_UBITS(re, gb, 1) == 0; i++) {
67 SKIP_BITS(re, gb, 1);
68 }
69 LAST_SKIP_BITS(re, gb, 1);
70 UPDATE_CACHE(re, gb);
60 71
61 if(i < limit - 1){ 72 if (i < limit - 1) {
62 if(k){ 73 if (k) {
63 buf = SHOW_UBITS(re, gb, k); 74 if (k > MIN_CACHE_BITS - 1) {
64 LAST_SKIP_BITS(re, gb, k); 75 buf = SHOW_UBITS(re, gb, 16) << (k-16);
65 }else{ 76 LAST_SKIP_BITS(re, gb, 16);
66 buf=0; 77 UPDATE_CACHE(re, gb);
78 buf |= SHOW_UBITS(re, gb, k-16);
79 LAST_SKIP_BITS(re, gb, k-16);
80 } else {
81 buf = SHOW_UBITS(re, gb, k);
82 LAST_SKIP_BITS(re, gb, k);
83 }
84 } else {
85 buf = 0;
67 } 86 }
68 87
69 CLOSE_READER(re, gb); 88 buf += ((int32_t)i << k);
70 return buf + (i<<k); 89 } else if (i == limit - 1) {
71 }else if(i == limit - 1){
72 buf = SHOW_UBITS(re, gb, esc_len); 90 buf = SHOW_UBITS(re, gb, esc_len);
73 LAST_SKIP_BITS(re, gb, esc_len); 91 LAST_SKIP_BITS(re, gb, esc_len);
74 CLOSE_READER(re, gb); 92
75 93 buf ++;
76 return buf + 1; 94 } else {
77 }else 95 buf = -1;
78 return -1; 96 }
97 CLOSE_READER(re, gb);
98 return buf;
79 } 99 }
80} 100}
81 101
@@ -103,8 +123,5 @@ static inline unsigned int get_ur_golomb_shorten(GetBitContext *gb, int k){
103static inline int get_sr_golomb_shorten(GetBitContext* gb, int k) 123static inline int get_sr_golomb_shorten(GetBitContext* gb, int k)
104{ 124{
105 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0); 125 int uvar = get_ur_golomb_jpegls(gb, k + 1, INT_MAX, 0);
106 if (uvar & 1) 126 return (uvar >> 1) ^ -(uvar & 1);
107 return ~(uvar >> 1);
108 else
109 return uvar >> 1;
110} 127}