+static int find_sync( const uint8_t *buf, int len )
+{
+ int i;
+
+ // since AC3 frames don't line up with MPEG ES frames scan the
+ // frame for an AC3 sync pattern.
+ for ( i = 0; i < len - 16; ++i )
+ {
+ int rate, bitrate, flags;
+ int size = a52_syncinfo( (uint8_t *)buf + i, &flags, &rate, &bitrate );
+ if( size > 0 )
+ {
+ // we have a plausible sync header - see if crc1 checks
+ int crc1size = (size >> 1) + (size >> 3);
+ if ( i + crc1size > len )
+ {
+ // don't have enough data to check crc1
+ break;
+ }
+ if ( av_crc( av_crc_get_table( AV_CRC_16_ANSI ), 0,
+ buf + i + 2, crc1size - 2 ) == 0 )
+ {
+ // crc checks - we've got sync
+ return i;
+ }
+ }
+ }
+ return -1;
+}
+
+static int deca52BSInfo( hb_work_object_t *w, const hb_buffer_t *b,
+ hb_work_info_t *info )
+{
+ memset( info, 0, sizeof(*info) );
+
+ // We don't know if the way that AC3 frames are fragmented into whatever
+ // packetization the container uses will give us enough bytes per fragment
+ // to check the CRC (we need at least 5/8 of the the frame). So we
+ // copy the fragment we got into an accumulation buffer in the audio object
+ // then look for sync over all the frags we've accumulated so far.
+ uint8_t *buf = w->audio->priv.config.a52.buf;
+ int len = w->audio->priv.config.a52.len, blen = b->size;
+ if ( len + blen > sizeof(w->audio->priv.config.a52.buf) )
+ {
+ // we don't have enough empty space in the accumulation buffer to
+ // hold the new frag - make room for it by discarding the oldest data.
+ if ( blen >= sizeof(w->audio->priv.config.a52.buf) )
+ {
+ // the frag is bigger than our accumulation buffer - copy all
+ // that will fit (the excess doesn't matter since the buffer
+ // is many times the size of a max length ac3 frame).
+ blen = sizeof(w->audio->priv.config.a52.buf);
+ len = 0;
+ }
+ else
+ {
+ // discard enough bytes from the front of the buffer to make
+ // room for the new stuff
+ int newlen = sizeof(w->audio->priv.config.a52.buf) - blen;
+ memcpy( buf, buf + len - newlen, newlen );
+ len = newlen;
+ }
+ }
+ // add the new frag to the buffer
+ memcpy( buf+len, b->data, blen );
+ len += blen;
+
+ int i;
+ if ( ( i = find_sync( buf, len ) ) < 0 )
+ {
+ // didn't find sync - wait for more data
+ w->audio->priv.config.a52.len = len;
+ return 0;
+ }
+
+ // got sync - extract and canoncalize the bitstream parameters
+ int rate = 0, bitrate = 0, flags = 0;
+ uint8_t raw = buf[i + 5];
+ a52_syncinfo( buf + i, &flags, &rate, &bitrate );
+
+ if ( rate == 0 || bitrate == 0 )
+ {
+ // invalid AC-3 parameters - toss what we have so we'll start over
+ // with the next buf otherwise we'll keep syncing on this junk.
+ w->audio->priv.config.a52.len = 0;
+ return 0;
+ }
+
+ // bsid | bsmod | acmod | cmixlv | surmixlv | dsurmod | lfeon | dialnorm | compre
+ // 5 3 3 2 2 2 1 5 1
+ // byte1 | byte2 | byte3
+
+ info->name = "AC-3";
+ info->rate = rate;
+ info->rate_base = 1;
+ info->bitrate = bitrate;
+ info->flags = flags;
+ info->version = raw >> 3; /* bsid is the first 5 bits */
+ info->mode = raw & 0x7; /* bsmod is the following 3 bits */
+
+ if ( (flags & A52_CHANNEL_MASK) == A52_DOLBY )
+ {
+ info->flags |= AUDIO_F_DOLBY;
+ }
+
+ switch( flags & A52_CHANNEL_MASK )
+ {
+ /* mono sources */
+ case A52_MONO:
+ case A52_CHANNEL1:
+ case A52_CHANNEL2:
+ info->channel_layout = HB_INPUT_CH_LAYOUT_MONO;
+ break;
+ /* stereo input */
+ case A52_CHANNEL:
+ case A52_STEREO:
+ info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
+ break;
+ /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
+ case A52_DOLBY:
+ info->channel_layout = HB_INPUT_CH_LAYOUT_DOLBY;
+ break;
+ /* 3F/2R input */
+ case A52_3F2R:
+ info->channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
+ break;
+ /* 3F/1R input */
+ case A52_3F1R:
+ info->channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
+ break;
+ /* other inputs */
+ case A52_3F:
+ info->channel_layout = HB_INPUT_CH_LAYOUT_3F;
+ break;
+ case A52_2F1R:
+ info->channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
+ break;
+ case A52_2F2R:
+ info->channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
+ break;
+ /* unknown */
+ default:
+ info->channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
+ }
+
+ if (flags & A52_LFE)
+ {
+ info->channel_layout |= HB_INPUT_CH_LAYOUT_HAS_LFE;
+ }
+
+ return 1;
+}