/* Copyright (c) 1997-1999 Miller Puckette. * For information on usage and redistribution, and for a DISCLAIMER OF ALL * WARRANTIES, see the file, "LICENSE.txt," in this distribution. */ /* this file handles Max-style patchable objects, i.e., objects which can interconnect via inlets and outlets; also, the (terse) generic behavior for "gobjs" appears at the end of this file. */ #include "m_pd.h" #include "m_imp.h" union inletunion { t_symbol *iu_symto; t_gpointer *iu_pointerslot; t_float *iu_floatslot; t_symbol **iu_symslot; t_sample iu_floatsignalvalue; }; struct _inlet { t_pd i_pd; struct _inlet *i_next; t_object *i_owner; t_pd *i_dest; t_symbol *i_symfrom; union inletunion i_un; }; #define i_symto i_un.iu_symto #define i_pointerslot i_un.iu_pointerslot #define i_floatslot i_un.iu_floatslot #define i_symslot i_un.iu_symslot static t_class *inlet_class, *pointerinlet_class, *floatinlet_class, *symbolinlet_class; #define ISINLET(pd) ((*(pd) == inlet_class) || \ (*(pd) == pointerinlet_class) || \ (*(pd) == floatinlet_class) || \ (*(pd) == symbolinlet_class)) /* --------------------- generic inlets ala max ------------------ */ t_inlet *inlet_new(t_object *owner, t_pd *dest, t_symbol *s1, t_symbol *s2) { t_inlet *x = (t_inlet *)pd_new(inlet_class), *y, *y2; x->i_owner = owner; x->i_dest = dest; if (s1 == &s_signal) x->i_un.iu_floatsignalvalue = 0; else x->i_symto = s2; x->i_symfrom = s1; x->i_next = 0; if((y = owner->ob_inlet)) { while((y2 = y->i_next)) y = y2; y->i_next = x; } else owner->ob_inlet = x; return (x); } static void inlet_wrong(t_inlet *x, t_symbol *s) { pd_error(x->i_owner, "inlet: expected '%s' but got '%s'", x->i_symfrom->s_name, s->s_name); } /* LATER figure out how to make these efficient: */ static void inlet_bang(t_inlet *x) { if (x->i_symfrom == &s_bang) pd_vmess(x->i_dest, x->i_symto, ""); else if (!x->i_symfrom) pd_bang(x->i_dest); else inlet_wrong(x, &s_bang); } static void inlet_pointer(t_inlet *x, t_gpointer *gp) { if (x->i_symfrom == &s_pointer) pd_vmess(x->i_dest, x->i_symto, "p", gp); else if (!x->i_symfrom) pd_pointer(x->i_dest, gp); else inlet_wrong(x, &s_pointer); } static void inlet_float(t_inlet *x, t_float f) { if (x->i_symfrom == &s_float) pd_vmess(x->i_dest, x->i_symto, "f", (t_floatarg)f); else if (x->i_symfrom == &s_signal) x->i_un.iu_floatsignalvalue = ftofix(f); else if (!x->i_symfrom) pd_float(x->i_dest, f); else inlet_wrong(x, &s_float); } static void inlet_symbol(t_inlet *x, t_symbol *s) { if (x->i_symfrom == &s_symbol) pd_vmess(x->i_dest, x->i_symto, "s", s); else if (!x->i_symfrom) pd_symbol(x->i_dest, s); else inlet_wrong(x, &s_symbol); } static void inlet_list(t_inlet *x, t_symbol *s, int argc, t_atom *argv) { #ifndef ROCKBOX t_atom at; #endif if (x->i_symfrom == &s_list || x->i_symfrom == &s_float || x->i_symfrom == &s_symbol || x->i_symfrom == &s_pointer) typedmess(x->i_dest, x->i_symto, argc, argv); else if (!x->i_symfrom) pd_list(x->i_dest, s, argc, argv); else inlet_wrong(x, &s_list); } static void inlet_anything(t_inlet *x, t_symbol *s, int argc, t_atom *argv) { if (x->i_symfrom == s) typedmess(x->i_dest, x->i_symto, argc, argv); else if (!x->i_symfrom) typedmess(x->i_dest, s, argc, argv); else inlet_wrong(x, s); } void inlet_free(t_inlet *x) { t_object *y = x->i_owner; t_inlet *x2; if (y->ob_inlet == x) y->ob_inlet = x->i_next; else for (x2 = y->ob_inlet; x2; x2 = x2->i_next) if (x2->i_next == x) { x2->i_next = x->i_next; break; } t_freebytes(x, sizeof(*x)); } /* ----- pointerinlets, floatinlets, syminlets: optimized inlets ------- */ static void pointerinlet_pointer(t_inlet *x, t_gpointer *gp) { gpointer_unset(x->i_pointerslot); *(x->i_pointerslot) = *gp; if (gp->gp_stub) gp->gp_stub->gs_refcount++; } t_inlet *pointerinlet_new(t_object *owner, t_gpointer *gp) { t_inlet *x = (t_inlet *)pd_new(pointerinlet_class), *y, *y2; x->i_owner = owner; x->i_dest = 0; x->i_symfrom = &s_pointer; x->i_pointerslot = gp; x->i_next = 0; if((y = owner->ob_inlet)) { while((y2 = y->i_next)) y = y2; y->i_next = x; } else owner->ob_inlet = x; return (x); } static void floatinlet_float(t_inlet *x, t_float f) { *(x->i_floatslot) = f; } t_inlet *floatinlet_new(t_object *owner, t_float *fp) { t_inlet *x = (t_inlet *)pd_new(floatinlet_class), *y, *y2; x->i_owner = owner; x->i_dest = 0; x->i_symfrom = &s_float; x->i_floatslot = fp; x->i_next = 0; if((y = owner->ob_inlet)) { while((y2 = y->i_next)) y = y2; y->i_next = x; } else owner->ob_inlet = x; return (x); } static void symbolinlet_symbol(t_inlet *x, t_symbol *s) { *(x->i_symslot) = s; } t_inlet *symbolinlet_new(t_object *owner, t_symbol **sp) { t_inlet *x = (t_inlet *)pd_new(symbolinlet_class), *y, *y2; x->i_owner = owner; x->i_dest = 0; x->i_symfrom = &s_symbol; x->i_symslot = sp; x->i_next = 0; if((y = owner->ob_inlet)) { while((y2 = y->i_next)) y = y2; y->i_next = x; } else owner->ob_inlet = x; return (x); } /* ---------------------- routine to handle lists ---------------------- */ /* objects interpret lists by feeding them to the individual inlets. Before you call this check that the object doesn't have a more specific way to handle lists. */ void obj_list(t_object *x, t_symbol *s, int argc, t_atom *argv) { t_atom *ap; int count; t_inlet *ip = ((t_object *)x)->ob_inlet; #ifdef ROCKBOX (void) s; #endif if (!argc) return; for (count = argc-1, ap = argv+1; ip && count--; ap++, ip = ip->i_next) { if (ap->a_type == A_POINTER) pd_pointer(&ip->i_pd, ap->a_w.w_gpointer); else if (ap->a_type == A_FLOAT) pd_float(&ip->i_pd, ap->a_w.w_float); else pd_symbol(&ip->i_pd, ap->a_w.w_symbol); } if (argv->a_type == A_POINTER) pd_pointer(&x->ob_pd, argv->a_w.w_gpointer); else if (argv->a_type == A_FLOAT) pd_float(&x->ob_pd, argv->a_w.w_float); else pd_symbol(&x->ob_pd, argv->a_w.w_symbol); } void obj_init(void) { inlet_class = class_new(gensym("inlet"), 0, 0, sizeof(t_inlet), CLASS_PD, 0); class_addbang(inlet_class, inlet_bang); class_addpointer(inlet_class, inlet_pointer); class_addfloat(inlet_class, inlet_float); class_addsymbol(inlet_class, inlet_symbol); class_addlist(inlet_class, inlet_list); class_addanything(inlet_class, inlet_anything); pointerinlet_class = class_new(gensym("inlet"), 0, 0, sizeof(t_inlet), CLASS_PD, 0); class_addpointer(pointerinlet_class, pointerinlet_pointer); floatinlet_class = class_new(gensym("inlet"), 0, 0, sizeof(t_inlet), CLASS_PD, 0); class_addfloat(floatinlet_class, (t_method)floatinlet_float); symbolinlet_class = class_new(gensym("inlet"), 0, 0, sizeof(t_inlet), CLASS_PD, 0); class_addsymbol(symbolinlet_class, symbolinlet_symbol); } /* --------------------------- outlets ------------------------------ */ static char *stacklimit, *topstack; #define STACKSIZE 1000000 static int outlet_eventno; /* set a stack limit (on each incoming event that can set off messages) for the outlet functions to check to prevent stack overflow from message recursion */ void outlet_setstacklim(void) { char c; topstack = &c; stacklimit = (&c) - STACKSIZE; outlet_eventno++; } /* get a number unique to the (clock, MIDI, GUI, etc.) event we're on */ int sched_geteventno( void) { return (outlet_eventno); } struct _outconnect { struct _outconnect *oc_next; t_pd *oc_to; }; struct _outlet { t_object *o_owner; struct _outlet *o_next; t_outconnect *o_connections; t_symbol *o_sym; }; t_outlet *outlet_new(t_object *owner, t_symbol *s) { t_outlet *x = (t_outlet *)getbytes(sizeof(*x)), *y, *y2; x->o_owner = owner; x->o_next = 0; if((y = owner->ob_outlet)) { while((y2 = y->o_next)) y = y2; y->o_next = x; } else owner->ob_outlet = x; x->o_connections = 0; x->o_sym = s; return (x); } static void outlet_stackerror(t_outlet *x) { pd_error(x->o_owner, "stack overflow"); stacklimit = topstack; } void outlet_bang(t_outlet *x) { t_outconnect *oc; char c; if (&c < stacklimit) outlet_stackerror(x); else for (oc = x->o_connections; oc; oc = oc->oc_next) pd_bang(oc->oc_to); } void outlet_pointer(t_outlet *x, t_gpointer *gp) { t_outconnect *oc; t_gpointer gpointer; char c; if (&c < stacklimit) outlet_stackerror(x); else { #if 0 gpointer_copy(gp, &gpointer); for (oc = x->o_connections; oc; oc = oc->oc_next) pd_pointer(oc->oc_to, &gpointer); gpointer_unset(&gpointer); #else gpointer = *gp; for (oc = x->o_connections; oc; oc = oc->oc_next) pd_pointer(oc->oc_to, &gpointer); #endif } } void outlet_float(t_outlet *x, t_float f) { t_outconnect *oc; char c; if (&c < stacklimit) outlet_stackerror(x); else for (oc = x->o_connections; oc; oc = oc->oc_next) pd_float(oc->oc_to, f); } void outlet_symbol(t_outlet *x, t_symbol *s) { t_outconnect *oc; char c; if (&c < stacklimit) outlet_stackerror(x); else for (oc = x->o_connections; oc; oc = oc->oc_next) pd_symbol(oc->oc_to, s); } void outlet_list(t_outlet *x, t_symbol *s, int argc, t_atom *argv) { t_outconnect *oc; char c; if (&c < stacklimit) outlet_stackerror(x); else for (oc = x->o_connections; oc; oc = oc->oc_next) pd_list(oc->oc_to, s, argc, argv); } void outlet_anything(t_outlet *x, t_symbol *s, int argc, t_atom *argv) { t_outconnect *oc; char c; if (&c < stacklimit) outlet_stackerror(x); else for (oc = x->o_connections; oc; oc = oc->oc_next) typedmess(oc->oc_to, s, argc, argv); } /* get the outlet's declared symbol */ t_symbol *outlet_getsymbol(t_outlet *x) { return (x->o_sym); } void outlet_free(t_outlet *x) { t_object *y = x->o_owner; t_outlet *x2; if (y->ob_outlet == x) y->ob_outlet = x->o_next; else for (x2 = y->ob_outlet; x2; x2 = x2->o_next) if (x2->o_next == x) { x2->o_next = x->o_next; break; } t_freebytes(x, sizeof(*x)); } t_outconnect *obj_connect(t_object *source, int outno, t_object *sink, int inno) { t_inlet *i; t_outlet *o; t_pd *to; t_outconnect *oc, *oc2; for (o = source->ob_outlet; o && outno; o = o->o_next, outno--) ; if (!o) return (0); if (sink->ob_pd->c_firstin) { if (!inno) { to = &sink->ob_pd; goto doit; } else inno--; } for (i = sink->ob_inlet; i && inno; i = i->i_next, inno--) ; if (!i) return (0); to = &i->i_pd; doit: oc = (t_outconnect *)t_getbytes(sizeof(*oc)); oc->oc_next = 0; oc->oc_to = to; /* append it to the end of the list */ /* LATER we might cache the last "oc" to make this faster. */ if ((oc2 = o->o_connections)) { while (oc2->oc_next) oc2 = oc2->oc_next; oc2->oc_next = oc; } else o->o_connections = oc; if (o->o_sym == &s_signal) canvas_update_dsp(); return (oc); } void obj_disconnect(t_object *source, int outno, t_object *sink, int inno) { t_inlet *i; t_outlet *o; t_pd *to; t_outconnect *oc, *oc2; for (o = source->ob_outlet; o && outno; o = o->o_next, outno--) if (!o) return; if (sink->ob_pd->c_firstin) { if (!inno) { to = &sink->ob_pd; goto doit; } else inno--; } for (i = sink->ob_inlet; i && inno; i = i->i_next, inno--) ; if (!i) return; to = &i->i_pd; doit: if (!(oc = o->o_connections)) return; if (oc->oc_to == to) { o->o_connections = oc->oc_next; freebytes(oc, sizeof(*oc)); goto done; } while((oc2 = oc->oc_next)) { if (oc2->oc_to == to) { oc->oc_next = oc2->oc_next; freebytes(oc2, sizeof(*oc2)); goto done; } oc = oc2; } done: if (o->o_sym == &s_signal) canvas_update_dsp(); } /* ------ traversal routines for code that can't see our structures ------ */ int obj_noutlets(t_object *x) { int n; t_outlet *o; for (o = x->ob_outlet, n = 0; o; o = o->o_next) n++; return (n); } int obj_ninlets(t_object *x) { int n; t_inlet *i; for (i = x->ob_inlet, n = 0; i; i = i->i_next) n++; if (x->ob_pd->c_firstin) n++; return (n); } t_outconnect *obj_starttraverseoutlet(t_object *x, t_outlet **op, int nout) { t_outlet *o = x->ob_outlet; while (nout-- && o) o = o->o_next; *op = o; if (o) return (o->o_connections); else return (0); } t_outconnect *obj_nexttraverseoutlet(t_outconnect *lastconnect, t_object **destp, t_inlet **inletp, int *whichp) { t_pd *y; y = lastconnect->oc_to; if (ISINLET(y)) { int n; t_inlet *i = (t_inlet *)y, *i2; t_object *dest = i->i_owner; for (n = dest->ob_pd->c_firstin, i2 = dest->ob_inlet; i2 && i2 != i; i2 = i2->i_next) n++; *whichp = n; *destp = dest; *inletp = i; } else { *whichp = 0; *inletp = 0; *destp = ((t_object *)y); } return (lastconnect->oc_next); } /* this one checks that a pd is indeed a patchable object, and returns it, correctly typed, or zero if the check failed. */ t_object *pd_checkobject(t_pd *x) { if ((*x)->c_patchable) return ((t_object *)x); else return (0); } /* move an inlet or outlet to the head of the list */ void obj_moveinletfirst(t_object *x, t_inlet *i) { t_inlet *i2; if (x->ob_inlet == i) return; else for (i2 = x->ob_inlet; i2; i2 = i2->i_next) if (i2->i_next == i) { i2->i_next = i->i_next; i->i_next = x->ob_inlet; x->ob_inlet = i; return; } } void obj_moveoutletfirst(t_object *x, t_outlet *o) { t_outlet *o2; if (x->ob_outlet == o) return; else for (o2 = x->ob_outlet; o2; o2 = o2->o_next) if (o2->o_next == o) { o2->o_next = o->o_next; o->o_next = x->ob_outlet; x->ob_outlet = o; return; } } /* routines for DSP sorting, which are used in d_ugen.c and g_canvas.c */ /* LATER try to consolidate all the slightly different routines. */ int obj_nsiginlets(t_object *x) { int n; t_inlet *i; for (i = x->ob_inlet, n = 0; i; i = i->i_next) if (i->i_symfrom == &s_signal) n++; if (x->ob_pd->c_firstin && x->ob_pd->c_floatsignalin) n++; return (n); } /* get the index, among signal inlets, of the mth inlet overall */ int obj_siginletindex(t_object *x, int m) { int n = 0; t_inlet *i; if (x->ob_pd->c_firstin && x->ob_pd->c_floatsignalin) { if (!m--) return (0); n++; } for (i = x->ob_inlet; i; i = i->i_next, m--) if (i->i_symfrom == &s_signal) { if (m == 0) return (n); n++; } return (-1); } int obj_issignalinlet(t_object *x, int m) { t_inlet *i; if (x->ob_pd->c_firstin) { if (!m) return (x->ob_pd->c_firstin && x->ob_pd->c_floatsignalin); else m--; } for (i = x->ob_inlet; i && m; i = i->i_next, m--) ; return (i && (i->i_symfrom == &s_signal)); } int obj_nsigoutlets(t_object *x) { int n; t_outlet *o; for (o = x->ob_outlet, n = 0; o; o = o->o_next) if (o->o_sym == &s_signal) n++; return (n); } int obj_sigoutletindex(t_object *x, int m) { int n; t_outlet *o2; for (o2 = x->ob_outlet, n = 0; o2; o2 = o2->o_next, m--) if (o2->o_sym == &s_signal) { if (m == 0) return (n); n++; } return (-1); } int obj_issignaloutlet(t_object *x, int m) { int n; t_outlet *o2; for (o2 = x->ob_outlet, n = 0; o2 && m--; o2 = o2->o_next); return (o2 && (o2->o_sym == &s_signal)); } t_sample *obj_findsignalscalar(t_object *x, int m) { int n = 0; t_inlet *i; if (x->ob_pd->c_firstin && x->ob_pd->c_floatsignalin) { if (!m--) return (x->ob_pd->c_floatsignalin > 0 ? (t_sample *)(((char *)x) + x->ob_pd->c_floatsignalin) : 0); n++; } for (i = x->ob_inlet; i; i = i->i_next, m--) if (i->i_symfrom == &s_signal) { if (m == 0) return (&i->i_un.iu_floatsignalvalue); n++; } return (0); } /* and these are only used in g_io.c... */ int inlet_getsignalindex(t_inlet *x) { int n = 0; t_inlet *i; for (i = x->i_owner->ob_inlet, n = 0; i && i != x; i = i->i_next) if (i->i_symfrom == &s_signal) n++; return (n); } int outlet_getsignalindex(t_outlet *x) { int n = 0; t_outlet *o; for (o = x->o_owner->ob_outlet, n = 0; o && o != x; o = o->o_next) if (o->o_sym == &s_signal) n++; return (n); } 'n420' href='#n420'>420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
/*
 * parse.c
 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
 *
 * This file is part of a52dec, a free ATSC A-52 stream decoder.
 * See http://liba52.sourceforge.net/ for updates.
 *
 * a52dec is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * a52dec is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config-a52.h"

#include <string.h>
#include <inttypes.h>

#include "a52.h"
#include "a52_internal.h"
#include "bitstream.h"
#include "tables.h"

#if defined(HAVE_MEMALIGN) && !defined(__cplusplus)
/* some systems have memalign() but no declaration for it */
void * memalign (size_t align, size_t size);
#else
/* assume malloc alignment is sufficient */
#define memalign(align,size) malloc (size)
#endif

typedef struct {
    quantizer_t q1[2];
    quantizer_t q2[2];
    quantizer_t q4;
    int q1_ptr;
    int q2_ptr;
    int q4_ptr;
} quantizer_set_t;

static a52_state_t istate IBSS_ATTR;
static sample_t isamples[256*12] IBSS_ATTR;

static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};

a52_state_t * a52_init (uint32_t mm_accel)
{
    a52_state_t * state;
    int i;

    #if defined(CPU_COLDFIRE)
    coldfire_set_macsr(EMAC_FRACTIONAL | EMAC_ROUND | EMAC_SATURATE);
    #endif
    /* 
      this needs to come back if we ever want two decoder instances
      simultenously. NOTE, you also need to remove comments in a52_free.
    state = (a52_state_t *) malloc (sizeof (a52_state_t));
    if (state == NULL)
	return NULL;

    state->samples = (sample_t *) memalign (16, 256 * 12 * sizeof (sample_t));
    if (state->samples == NULL) {
	free (state);
	return NULL;
    }

    */
    state = &istate;
    state->samples = isamples;
    for (i = 0; i < 256 * 12; i++)
	state->samples[i] = 0;

    state->downmixed = 1;

    state->lfsr_state = 1;

    a52_imdct_init (mm_accel);

    return state;
}

sample_t * a52_samples (a52_state_t * state)
{
    return state->samples;
}

int a52_syncinfo (uint8_t * buf, int * flags,
		  int * sample_rate, int * bit_rate)
{
    static int rate[] = { 32,  40,  48,  56,  64,  80,  96, 112,
			 128, 160, 192, 224, 256, 320, 384, 448,
			 512, 576, 640};
    static uint8_t lfeon[8] = {0x10, 0x10, 0x04, 0x04, 0x04, 0x01, 0x04, 0x01};
    int frmsizecod;
    int bitrate;
    int half;
    int acmod;

    if ((buf[0] != 0x0b) || (buf[1] != 0x77))	/* syncword */
	return 0;

    if (buf[5] >= 0x60)		/* bsid >= 12 */
	return 0;
    half = halfrate[buf[5] >> 3];

    /* acmod, dsurmod and lfeon */
    acmod = buf[6] >> 5;
    *flags = ((((buf[6] & 0xf8) == 0x50) ? A52_DOLBY : acmod) |
	      ((buf[6] & lfeon[acmod]) ? A52_LFE : 0));

    frmsizecod = buf[4] & 63;
    if (frmsizecod >= 38)
	return 0;
    bitrate = rate [frmsizecod >> 1];
    *bit_rate = (bitrate * 1000) >> half;

    switch (buf[4] & 0xc0) {
    case 0:
	*sample_rate = 48000 >> half;
	return 4 * bitrate;
    case 0x40:
	*sample_rate = 44100 >> half;
	return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
    case 0x80:
	*sample_rate = 32000 >> half;
	return 6 * bitrate;
    default:
	return 0;
    }
}

int a52_frame (a52_state_t * state, uint8_t * buf, int * flags,
	       level_t * level, sample_t bias)
{
    static level_t clev[4] = { LEVEL (LEVEL_3DB), LEVEL (LEVEL_45DB),
			       LEVEL (LEVEL_6DB), LEVEL (LEVEL_45DB) };
    static level_t slev[4] = { LEVEL (LEVEL_3DB), LEVEL (LEVEL_6DB), 
			       0,                 LEVEL (LEVEL_6DB) };
    int chaninfo;
    int acmod;

    state->fscod = buf[4] >> 6;
    state->halfrate = halfrate[buf[5] >> 3];
    state->acmod = acmod = buf[6] >> 5;

    a52_bitstream_set_ptr (state, buf + 6);
    bitstream_get (state, 3);	/* skip acmod we already parsed */

    if ((acmod == 2) && (bitstream_get (state, 2) == 2))	/* dsurmod */
	acmod = A52_DOLBY;

    state->clev = state->slev = 0;

    if ((acmod & 1) && (acmod != 1))
	state->clev = clev[bitstream_get (state, 2)];	/* cmixlev */

    if (acmod & 4)
	state->slev = slev[bitstream_get (state, 2)];	/* surmixlev */

    state->lfeon = bitstream_get (state, 1);

    state->output = a52_downmix_init (acmod, *flags, level,
				      state->clev, state->slev);
    if (state->output < 0)
	return 1;
    if (state->lfeon && (*flags & A52_LFE))
	state->output |= A52_LFE;
    *flags = state->output;
    /* the 2* compensates for differences in imdct */
    state->dynrng = state->level = MUL_C (*level, 2);
    state->bias = bias;
    state->dynrnge = 1;
    state->dynrngcall = NULL;
    state->cplba.deltbae = DELTA_BIT_NONE;
    state->ba[0].deltbae = state->ba[1].deltbae = state->ba[2].deltbae =
	state->ba[3].deltbae = state->ba[4].deltbae = DELTA_BIT_NONE;

    chaninfo = !acmod;
    do {
	bitstream_get (state, 5);	/* dialnorm */
	if (bitstream_get (state, 1))	/* compre */
	    bitstream_get (state, 8);	/* compr */
	if (bitstream_get (state, 1))	/* langcode */
	    bitstream_get (state, 8);	/* langcod */
	if (bitstream_get (state, 1))	/* audprodie */
	    bitstream_get (state, 7);	/* mixlevel + roomtyp */
    } while (chaninfo--);

    bitstream_get (state, 2);		/* copyrightb + origbs */

    if (bitstream_get (state, 1))	/* timecod1e */
	bitstream_get (state, 14);	/* timecod1 */
    if (bitstream_get (state, 1))	/* timecod2e */
	bitstream_get (state, 14);	/* timecod2 */

    if (bitstream_get (state, 1)) {	/* addbsie */
	int addbsil;

	addbsil = bitstream_get (state, 6);
	do {
	    bitstream_get (state, 8);	/* addbsi */
	} while (addbsil--);
    }

    return 0;
}

void a52_dynrng (a52_state_t * state,
		 level_t (* call) (level_t, void *), void * data)
{
    state->dynrnge = 0;
    if (call) {
	state->dynrnge = 1;
	state->dynrngcall = call;
	state->dynrngdata = data;
    }
}

static int parse_exponents (a52_state_t * state, int expstr, int ngrps,
			    uint8_t exponent, uint8_t * dest)
{
    int exps;

    while (ngrps--) {
	exps = bitstream_get (state, 7);

	exponent += exp_1[exps];
	if (exponent > 24)
	    return 1;

	switch (expstr) {
	case EXP_D45:
	    *(dest++) = exponent;
	    *(dest++) = exponent;
	case EXP_D25:
	    *(dest++) = exponent;
	case EXP_D15:
	    *(dest++) = exponent;
	}

	exponent += exp_2[exps];
	if (exponent > 24)
	    return 1;

	switch (expstr) {
	case EXP_D45:
	    *(dest++) = exponent;
	    *(dest++) = exponent;
	case EXP_D25:
	    *(dest++) = exponent;
	case EXP_D15:
	    *(dest++) = exponent;
	}

	exponent += exp_3[exps];
	if (exponent > 24)
	    return 1;

	switch (expstr) {
	case EXP_D45:
	    *(dest++) = exponent;
	    *(dest++) = exponent;
	case EXP_D25:
	    *(dest++) = exponent;
	case EXP_D15:
	    *(dest++) = exponent;
	}
    }	

    return 0;
}

static int parse_deltba (a52_state_t * state, int8_t * deltba)
{
    int deltnseg, deltlen, delta, j;

    memset (deltba, 0, 50);

    deltnseg = bitstream_get (state, 3);
    j = 0;
    do {
	j += bitstream_get (state, 5);
	deltlen = bitstream_get (state, 4);
	delta = bitstream_get (state, 3);
	delta -= (delta >= 4) ? 3 : 4;
	if (!deltlen)
	    continue;
	if (j + deltlen >= 50)
	    return 1;
	while (deltlen--)
	    deltba[j++] = delta;
    } while (deltnseg--);

    return 0;
}

static inline int zero_snr_offsets (int nfchans, a52_state_t * state)
{
    int i;

    if ((state->csnroffst) ||
	(state->chincpl && state->cplba.bai >> 3) ||	/* cplinu, fsnroffst */
	(state->lfeon && state->lfeba.bai >> 3))	/* fsnroffst */
	return 0;
    for (i = 0; i < nfchans; i++)
	if (state->ba[i].bai >> 3)			/* fsnroffst */
	    return 0;
    return 1;
}

static inline int16_t dither_gen (a52_state_t * state)
{
    int16_t nstate;

    nstate = dither_lut[state->lfsr_state >> 8] ^ (state->lfsr_state << 8);
	
    state->lfsr_state = (uint16_t) nstate;

    return (3 * nstate) >> 2;
}

#ifndef LIBA52_FIXED
#define COEFF(c,t,l,s,e) (c) = (t) * (s)[e]
#else
#define COEFF(c,_t,_l,s,e) do {					\
    quantizer_t t = (_t);					\
    level_t l = (_l);						\
    int shift = e - 5;						\
    sample_t tmp = t * (l >> 16) + ((t * (l & 0xffff)) >> 16);	\
    if (shift >= 0)						\
	(c) = tmp >> shift;					\
    else							\
	(c) = tmp << -shift;					\
} while (0)
#endif

static void coeff_get (a52_state_t * state, sample_t * coeff,
		       expbap_t * expbap, quantizer_set_t * quant,
		       level_t level, int dither, int end)
{
    int i;
    uint8_t * exp;
    int8_t * bap;

#ifndef LIBA52_FIXED
    sample_t factor[25];

    for (i = 0; i <= 24; i++)
	factor[i] = scale_factor[i] * level;
#endif

    exp = expbap->exp;
    bap = expbap->bap;

    for (i = 0; i < end; i++) {
	int bapi;

	bapi = bap[i];
	switch (bapi) {
	case 0:
	    if (dither) {
		COEFF (coeff[i], dither_gen (state), level, factor, exp[i]);
		continue;
	    } else {
		coeff[i] = 0;
		continue;
	    }

	case -1:
	    if (quant->q1_ptr >= 0) {
		COEFF (coeff[i], quant->q1[quant->q1_ptr--], level,
		       factor, exp[i]);
		continue;
	    } else {
		int code;

		code = bitstream_get (state, 5);

		quant->q1_ptr = 1;
		quant->q1[0] = q_1_2[code];
		quant->q1[1] = q_1_1[code];
		COEFF (coeff[i], q_1_0[code], level, factor, exp[i]);
		continue;
	    }

	case -2:
	    if (quant->q2_ptr >= 0) {
		COEFF (coeff[i], quant->q2[quant->q2_ptr--], level,
		       factor, exp[i]);
		continue;
	    } else {
		int code;

		code = bitstream_get (state, 7);

		quant->q2_ptr = 1;
		quant->q2[0] = q_2_2[code];
		quant->q2[1] = q_2_1[code];
		COEFF (coeff[i], q_2_0[code], level, factor, exp[i]);
		continue;
	    }

	case 3:
	    COEFF (coeff[i], q_3[bitstream_get (state, 3)], level,
		   factor, exp[i]);
	    continue;

	case -3:
	    if (quant->q4_ptr == 0) {
		quant->q4_ptr = -1;
		COEFF (coeff[i], quant->q4, level, factor, exp[i]);
		continue;
	    } else {
		int code;

		code = bitstream_get (state, 7);

		quant->q4_ptr = 0;
		quant->q4 = q_4_1[code];
		COEFF (coeff[i], q_4_0[code], level, factor, exp[i]);
		continue;
	    }

	case 4:
	    COEFF (coeff[i], q_5[bitstream_get (state, 4)], level,
		   factor, exp[i]);
	    continue;

	default:
	    COEFF (coeff[i], bitstream_get_2 (state, bapi) << (16 - bapi),
		   level, factor, exp[i]);
	}
    }
}

static void coeff_get_coupling (a52_state_t * state, int nfchans,
				level_t * coeff, sample_t (* samples)[256],
				quantizer_set_t * quant, uint8_t dithflag[5])
{
    int cplbndstrc, bnd, i, i_end, ch;
    uint8_t * exp;
    int8_t * bap;
    level_t cplco[5];

    exp = state->cpl_expbap.exp;
    bap = state->cpl_expbap.bap;
    bnd = 0;
    cplbndstrc = state->cplbndstrc;
    i = state->cplstrtmant;
    while (i < state->cplendmant) {
	i_end = i + 12;
	while (cplbndstrc & 1) {
	    cplbndstrc >>= 1;
	    i_end += 12;
	}
	cplbndstrc >>= 1;
	for (ch = 0; ch < nfchans; ch++)
	    cplco[ch] = MUL_L (state->cplco[ch][bnd], coeff[ch]);
	bnd++;

	while (i < i_end) {
	    quantizer_t cplcoeff;
	    int bapi;

	    bapi = bap[i];
	    switch (bapi) {
	    case 0:
		for (ch = 0; ch < nfchans; ch++)
		    if ((state->chincpl >> ch) & 1) {
			if (dithflag[ch])
#ifndef LIBA52_FIXED
			    samples[ch][i] = (scale_factor[exp[i]] *
					      cplco[ch] * dither_gen (state));
#else
			    COEFF (samples[ch][i], dither_gen (state),
				   cplco[ch], scale_factor, exp[i]);
#endif
			else
			    samples[ch][i] = 0;
		    }
		i++;
		continue;

	    case -1:
		if (quant->q1_ptr >= 0) {
		    cplcoeff = quant->q1[quant->q1_ptr--];
		    break;
		} else {
		    int code;

		    code = bitstream_get (state, 5);

		    quant->q1_ptr = 1;
		    quant->q1[0] = q_1_2[code];
		    quant->q1[1] = q_1_1[code];
		    cplcoeff = q_1_0[code];
		    break;
		}

	    case -2:
		if (quant->q2_ptr >= 0) {
		    cplcoeff = quant->q2[quant->q2_ptr--];
		    break;
		} else {
		    int code;

		    code = bitstream_get (state, 7);

		    quant->q2_ptr = 1;
		    quant->q2[0] = q_2_2[code];
		    quant->q2[1] = q_2_1[code];
		    cplcoeff = q_2_0[code];
		    break;
		}

	    case 3:
		cplcoeff = q_3[bitstream_get (state, 3)];
		break;

	    case -3:
		if (quant->q4_ptr == 0) {
		    quant->q4_ptr = -1;
		    cplcoeff = quant->q4;
		    break;
		} else {
		    int code;

		    code = bitstream_get (state, 7);

		    quant->q4_ptr = 0;
		    quant->q4 = q_4_1[code];
		    cplcoeff = q_4_0[code];
		    break;
		}

	    case 4:
		cplcoeff = q_5[bitstream_get (state, 4)];
		break;

	    default:
		cplcoeff = bitstream_get_2 (state, bapi) << (16 - bapi);
	    }
#ifndef LIBA52_FIXED
	    cplcoeff *= scale_factor[exp[i]];
#endif
	    for (ch = 0; ch < nfchans; ch++)
	       if ((state->chincpl >> ch) & 1)
#ifndef LIBA52_FIXED
		    samples[ch][i] = cplcoeff * cplco[ch];
#else
		    COEFF (samples[ch][i], cplcoeff, cplco[ch],
			   scale_factor, exp[i]);
#endif
	    i++;
	}
    }