summaryrefslogtreecommitdiffstats
path: root/HBAc3Decoder.cpp
blob: 81753d899fa849dcedf3911ad66969c37f0cdfb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* $Id: HBAc3Decoder.cpp,v 1.6 2003/08/24 15:03:41 titer Exp $ */

#include "HBCommon.h"
#include "HBAc3Decoder.h"
#include "HBManager.h"
#include "HBFifo.h"

extern "C" {
#include <a52dec/a52.h>
}

HBAc3Decoder::HBAc3Decoder( HBManager * manager, HBAudioInfo * audioInfo )
    : HBThread( "ac3decoder" )
{
    fManager        = manager;
    fAudioInfo      = audioInfo;
    
    fAc3Buffer      = NULL;
    fAc3Frame       = NULL;
    fPosInAc3Buffer = 0;
}

void HBAc3Decoder::DoWork()
{
    /* Init liba52 */
    a52_state_t * state       = a52_init( 0 );
    int           inFlags     = 0;
    int           outFlags    = A52_STEREO;
    float         sampleLevel = 32768;       /* lame wants samples from
                                                -32768 to 32768 */
    
    /* Max size for a A52 frame is 3840 bytes */
    fAc3Frame = new HBBuffer( 3840 ); 

    int           frameSize;
    HBBuffer    * rawBuffer;
    sample_t    * samples;
    
    /* Main loop */
    while( !fDie )
    {
        fAc3Frame->fSize = 0;
        
        /* Get a frame header (7 bytes) */
        if( !( GetBytes( 7 ) ) )
        {
            continue;
        }
        
        /* Get the size of the current frame */
        frameSize = a52_syncinfo( fAc3Frame->fData, &inFlags,
                                  &fAudioInfo->fInSampleRate,
                                  &fAudioInfo->fInBitrate );
        
        if( !frameSize )
        {
            Log( "HBAc3Decoder : a52_syncinfo failed" );
            fManager->Error();
            break;
        }
        
        /* Get the whole frame */
        if( !( GetBytes( (uint32_t) frameSize ) ) )
        {
            continue;
        }

        /* Feed liba52 */
        a52_frame( state, fAc3Frame->fData, &outFlags, &sampleLevel, 0 );
    
        /* 6 blocks per frame, 256 samples per block */
        rawBuffer = new HBBuffer( 12 * 256 * sizeof( float ) );
        for( int i = 0; i < 6; i++ )
        {
            /* Decode a block */
            a52_block( state );
            
            /* Get a pointer to the raw data */
            samples = a52_samples( state );

            /* Copy left channel data */
            memcpy( (float*) rawBuffer->fData + i * 256,
                    samples,
                    256 * sizeof( float ) );
        
            /* Copy right channel data */
            memcpy( (float*) rawBuffer->fData + ( 6 + i ) * 256,
                    samples + 256,
                    256 * sizeof( float ) );
        }
        
        fAudioInfo->fRawFifo->Push( rawBuffer );
    }
    
    /* Clean up */
    delete fAc3Frame;
}

/* GetBytes() : pops buffers from the AC3 fifo until fAc3Frame
   contains <size> bytes */
bool HBAc3Decoder::GetBytes( uint32_t size )
{
    while( fAc3Frame->fSize < size )
    {
        if( !fAc3Buffer )
        {
            if( !( fAc3Buffer = fAudioInfo->fAc3Fifo->Pop() ) )
            {
                return false;
            }
            fPosInAc3Buffer = 0;
        }
        
        int willCopy = MIN( size - fAc3Frame->fSize,
                            fAc3Buffer->fSize - fPosInAc3Buffer );
        memcpy( fAc3Frame->fData + fAc3Frame->fSize,
                fAc3Buffer->fData + fPosInAc3Buffer,
                willCopy );
        fAc3Frame->fSize += willCopy;
        fPosInAc3Buffer  += willCopy;
        
        if( fAc3Buffer->fSize == fPosInAc3Buffer )
        {
            delete fAc3Buffer;
            fAc3Buffer = NULL;
        }
    }
    
    return true;
}