comparison wavpack.c @ 9549:7a51c0815b28 libavcodec

Merge decoding functions for all bitdepths in WavPack decoder
author kostya
date Fri, 24 Apr 2009 05:50:19 +0000
parents e5afd314bd14
children 9a3fddd31092
comparison
equal deleted inserted replaced
9548:c005889b5389 9549:7a51c0815b28
335 if(ctx->hybrid_bitrate) 335 if(ctx->hybrid_bitrate)
336 c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level); 336 c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
337 return sign ? ~ret : ret; 337 return sign ? ~ret : ret;
338 } 338 }
339 339
340 static int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, int16_t *dst) 340 static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *dst, const int hires)
341 { 341 {
342 int i, j, count = 0; 342 int i, j, count = 0;
343 int last, t; 343 int last, t;
344 int A, B, L, L2, R, R2, bit; 344 int A, B, L, L2, R, R2, bit;
345 int pos = 0; 345 int pos = 0;
346 uint32_t crc = 0xFFFFFFFF; 346 uint32_t crc = 0xFFFFFFFF;
347 int16_t *dst16 = dst;
348 int32_t *dst32 = dst;
347 349
348 s->one = s->zero = s->zeroes = 0; 350 s->one = s->zero = s->zeroes = 0;
349 do{ 351 do{
350 L = wv_get_value(s, gb, 0, &last); 352 L = wv_get_value(s, gb, 0, &last);
351 if(last) break; 353 if(last) break;
369 }else{ 371 }else{
370 A = s->decorr[i].samplesA[pos]; 372 A = s->decorr[i].samplesA[pos];
371 B = s->decorr[i].samplesB[pos]; 373 B = s->decorr[i].samplesB[pos];
372 j = (pos + t) & 7; 374 j = (pos + t) & 7;
373 } 375 }
374 L2 = L + ((s->decorr[i].weightA * A + 512) >> 10); 376 if(hires){
375 R2 = R + ((s->decorr[i].weightB * B + 512) >> 10); 377 L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
378 R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
379 }else{
380 L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
381 R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
382 }
376 if(A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta; 383 if(A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
377 if(B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta; 384 if(B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
378 s->decorr[i].samplesA[j] = L = L2; 385 s->decorr[i].samplesA[j] = L = L2;
379 s->decorr[i].samplesB[j] = R = R2; 386 s->decorr[i].samplesB[j] = R = R2;
380 }else if(t == -1){ 387 }else if(t == -1){
381 L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10); 388 if(hires)
389 L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
390 else
391 L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
382 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L); 392 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
383 L = L2; 393 L = L2;
384 R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10); 394 if(hires)
395 R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
396 else
397 R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
385 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R); 398 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
386 R = R2; 399 R = R2;
387 s->decorr[i].samplesA[0] = R; 400 s->decorr[i].samplesA[0] = R;
388 }else{ 401 }else{
389 R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10); 402 if(hires)
403 R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
404 else
405 R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
390 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R); 406 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
391 R = R2; 407 R = R2;
392 408
393 if(t == -3){ 409 if(t == -3){
394 R2 = s->decorr[i].samplesA[0]; 410 R2 = s->decorr[i].samplesA[0];
395 s->decorr[i].samplesA[0] = R; 411 s->decorr[i].samplesA[0] = R;
396 } 412 }
397 413
398 L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10); 414 if(hires)
415 L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
416 else
417 L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
399 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L); 418 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
400 L = L2; 419 L = L2;
401 s->decorr[i].samplesB[0] = L; 420 s->decorr[i].samplesB[0] = L;
402 } 421 }
403 } 422 }
404 pos = (pos + 1) & 7; 423 pos = (pos + 1) & 7;
405 if(s->joint) 424 if(s->joint)
406 L += (R -= (L >> 1)); 425 L += (R -= (L >> 1));
407 crc = (crc * 3 + L) * 3 + R; 426 crc = (crc * 3 + L) * 3 + R;
408 bit = (L & s->and) | s->or; 427 bit = (L & s->and) | s->or;
409 *dst++ = (((L + bit) << s->shift) - bit) << s->post_shift; 428 if(hires)
429 *dst32++ = (((L + bit) << s->shift) - bit) << s->post_shift;
430 else
431 *dst16++ = (((L + bit) << s->shift) - bit) << s->post_shift;
410 bit = (R & s->and) | s->or; 432 bit = (R & s->and) | s->or;
411 *dst++ = (((R + bit) << s->shift) - bit) << s->post_shift; 433 if(hires)
434 *dst32++ = (((R + bit) << s->shift) - bit) << s->post_shift;
435 else
436 *dst16++ = (((R + bit) << s->shift) - bit) << s->post_shift;
412 count++; 437 count++;
413 }while(!last && count < s->samples); 438 }while(!last && count < s->samples);
414 439
415 if(crc != s->CRC){ 440 if(crc != s->CRC){
416 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n"); 441 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
417 return -1; 442 return -1;
418 } 443 }
419 return count * 2; 444 return count * 2;
420 } 445 }
421 446
422 static int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, int16_t *dst) 447 static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst, const int hires)
423 { 448 {
424 int i, j, count = 0; 449 int i, j, count = 0;
425 int last, t; 450 int last, t;
426 int A, S, T, bit; 451 int A, S, T, bit;
427 int pos = 0; 452 int pos = 0;
428 uint32_t crc = 0xFFFFFFFF; 453 uint32_t crc = 0xFFFFFFFF;
454 int16_t *dst16 = dst;
455 int32_t *dst32 = dst;
429 456
430 s->one = s->zero = s->zeroes = 0; 457 s->one = s->zero = s->zeroes = 0;
431 do{ 458 do{
432 T = wv_get_value(s, gb, 0, &last); 459 T = wv_get_value(s, gb, 0, &last);
433 S = 0; 460 S = 0;
443 j = 0; 470 j = 0;
444 }else{ 471 }else{
445 A = s->decorr[i].samplesA[pos]; 472 A = s->decorr[i].samplesA[pos];
446 j = (pos + t) & 7; 473 j = (pos + t) & 7;
447 } 474 }
448 S = T + ((s->decorr[i].weightA * A + 512) >> 10); 475 if(hires)
476 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
477 else
478 S = T + ((s->decorr[i].weightA * A + 512) >> 10);
449 if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta; 479 if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
450 s->decorr[i].samplesA[j] = T = S; 480 s->decorr[i].samplesA[j] = T = S;
451 } 481 }
452 pos = (pos + 1) & 7; 482 pos = (pos + 1) & 7;
453 crc = crc * 3 + S; 483 crc = crc * 3 + S;
454 bit = (S & s->and) | s->or; 484 bit = (S & s->and) | s->or;
455 *dst++ = (((S + bit) << s->shift) - bit) << s->post_shift; 485 if(hires)
456 count++; 486 *dst32++ = (((S + bit) << s->shift) - bit) << s->post_shift;
457 }while(!last && count < s->samples); 487 else
458 488 *dst16++ = (((S + bit) << s->shift) - bit) << s->post_shift;
459 if(crc != s->CRC){
460 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
461 return -1;
462 }
463 return count;
464 }
465
466 static int wv_unpack_stereo_hires(WavpackContext *s, GetBitContext *gb, int32_t *dst)
467 {
468 int i, j, count = 0;
469 int last, t;
470 int A, B, L, L2, R, R2, bit;
471 int pos = 0;
472 uint32_t crc = 0xFFFFFFFF;
473
474 s->one = s->zero = s->zeroes = 0;
475 do{
476 L = wv_get_value(s, gb, 0, &last);
477 if(last) break;
478 R = wv_get_value(s, gb, 1, &last);
479 if(last) break;
480 for(i = 0; i < s->terms; i++){
481 t = s->decorr[i].value;
482 j = 0;
483 if(t > 0){
484 if(t > 8){
485 if(t & 1){
486 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
487 B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
488 }else{
489 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
490 B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
491 }
492 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
493 s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
494 j = 0;
495 }else{
496 A = s->decorr[i].samplesA[pos];
497 B = s->decorr[i].samplesB[pos];
498 j = (pos + t) & 7;
499 }
500 L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
501 R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
502 if(A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
503 if(B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
504 s->decorr[i].samplesA[j] = L = L2;
505 s->decorr[i].samplesB[j] = R = R2;
506 }else if(t == -1){
507 L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
508 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
509 L = L2;
510 R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
511 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
512 R = R2;
513 s->decorr[i].samplesA[0] = R;
514 }else{
515 R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
516 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
517 R = R2;
518
519 if(t == -3){
520 R2 = s->decorr[i].samplesA[0];
521 s->decorr[i].samplesA[0] = R;
522 }
523
524 L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
525 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
526 L = L2;
527 s->decorr[i].samplesB[0] = L;
528 }
529 }
530 pos = (pos + 1) & 7;
531 if(s->joint)
532 L += (R -= (L >> 1));
533 crc = (crc * 3 + L) * 3 + R;
534 bit = (L & s->and) | s->or;
535 *dst++ = (((L + bit) << s->shift) - bit) << s->post_shift;
536 bit = (R & s->and) | s->or;
537 *dst++ = (((R + bit) << s->shift) - bit) << s->post_shift;
538 count++;
539 }while(!last && count < s->samples);
540
541 if(crc != s->CRC){
542 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
543 return -1;
544 }
545 return count * 2;
546 }
547
548 static int wv_unpack_mono_hires(WavpackContext *s, GetBitContext *gb, int32_t *dst)
549 {
550 int i, j, count = 0;
551 int last, t;
552 int A, S, T, bit;
553 int pos = 0;
554 uint32_t crc = 0xFFFFFFFF;
555
556 s->one = s->zero = s->zeroes = 0;
557 do{
558 T = wv_get_value(s, gb, 0, &last);
559 S = 0;
560 if(last) break;
561 for(i = 0; i < s->terms; i++){
562 t = s->decorr[i].value;
563 if(t > 8){
564 if(t & 1)
565 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
566 else
567 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
568 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
569 j = 0;
570 }else{
571 A = s->decorr[i].samplesA[pos];
572 j = (pos + t) & 7;
573 }
574 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
575 if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
576 s->decorr[i].samplesA[j] = T = S;
577 }
578 pos = (pos + 1) & 7;
579 crc = crc * 3 + S;
580 bit = (S & s->and) | s->or;
581 *dst++ = (((S + bit) << s->shift) - bit) << s->post_shift;
582 count++; 489 count++;
583 }while(!last && count < s->samples); 490 }while(!last && count < s->samples);
584 491
585 if(crc != s->CRC){ 492 if(crc != s->CRC){
586 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n"); 493 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
609 AVPacket *avpkt) 516 AVPacket *avpkt)
610 { 517 {
611 const uint8_t *buf = avpkt->data; 518 const uint8_t *buf = avpkt->data;
612 int buf_size = avpkt->size; 519 int buf_size = avpkt->size;
613 WavpackContext *s = avctx->priv_data; 520 WavpackContext *s = avctx->priv_data;
614 int16_t *samples = data; 521 void *samples = data;
615 int32_t *samples32 = data;
616 int samplecount; 522 int samplecount;
617 int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0; 523 int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0;
618 int got_hybrid = 0; 524 int got_hybrid = 0;
619 const uint8_t* buf_end = buf + buf_size; 525 const uint8_t* buf_end = buf + buf_size;
620 int i, j, id, size, ssize, weights, t; 526 int i, j, id, size, ssize, weights, t;
834 return -1; 740 return -1;
835 } 741 }
836 742
837 if(s->stereo_in){ 743 if(s->stereo_in){
838 if(bpp == 2) 744 if(bpp == 2)
839 samplecount = wv_unpack_stereo(s, &s->gb, samples); 745 samplecount = wv_unpack_stereo(s, &s->gb, samples, 0);
840 else 746 else
841 samplecount = wv_unpack_stereo_hires(s, &s->gb, samples32); 747 samplecount = wv_unpack_stereo(s, &s->gb, samples, 1);
842 }else{ 748 }else{
843 if(bpp == 2) 749 if(bpp == 2)
844 samplecount = wv_unpack_mono(s, &s->gb, samples); 750 samplecount = wv_unpack_mono(s, &s->gb, samples, 0);
845 else 751 else
846 samplecount = wv_unpack_mono_hires(s, &s->gb, samples32); 752 samplecount = wv_unpack_mono(s, &s->gb, samples, 1);
847 if(s->stereo && bpp == 2){ 753 if(s->stereo && bpp == 2){
848 int16_t *dst = samples + samplecount * 2; 754 int16_t *dst = (int16_t*)samples + samplecount * 2;
849 int16_t *src = samples + samplecount; 755 int16_t *src = (int16_t*)samples + samplecount;
850 int cnt = samplecount; 756 int cnt = samplecount;
851 while(cnt--){ 757 while(cnt--){
852 *--dst = *--src; 758 *--dst = *--src;
853 *--dst = *src; 759 *--dst = *src;
854 } 760 }
855 samplecount *= 2; 761 samplecount *= 2;
856 }else if(s->stereo){ //32-bit output 762 }else if(s->stereo){ //32-bit output
857 int32_t *dst = samples32 + samplecount * 2; 763 int32_t *dst = (int32_t*)samples + samplecount * 2;
858 int32_t *src = samples32 + samplecount; 764 int32_t *src = (int32_t*)samples + samplecount;
859 int cnt = samplecount; 765 int cnt = samplecount;
860 while(cnt--){ 766 while(cnt--){
861 *--dst = *--src; 767 *--dst = *--src;
862 *--dst = *src; 768 *--dst = *src;
863 } 769 }