summaryrefslogtreecommitdiffstats
path: root/core/Ac3Decoder.cpp
blob: 12eed05df733bd95de511dc12e1587864216f005 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* $Id: Ac3Decoder.cpp,v 1.12 2003/09/30 14:38:15 titer Exp $

   This file is part of the HandBrake source code.
   Homepage: <http://beos.titer.org/handbrake/>.
   It may be used under the terms of the GNU General Public License. */

#include "Ac3Decoder.h"
#include "Fifo.h"
#include "Manager.h"

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

HBAc3Decoder::HBAc3Decoder( HBManager * manager, HBAudio * audio )
    : HBThread( "ac3decoder" )
{
    fManager     = manager;
    fAudio       = audio;

    /* Max size for a A52 frame is 3840 bytes */
    fAc3Frame    = new HBBuffer( 3840 );
    fAc3Buffer   = NULL;
    fPosInBuffer = 0;
    fPosition    = 0;
}

HBAc3Decoder::~HBAc3Decoder()
{
    delete fAc3Frame;
}

void HBAc3Decoder::DoWork()
{
    /* Init liba52 */
    a52_state_t * state       = a52_init( 0 );
    int           inFlags     = 0;
    int           outFlags    = A52_STEREO;

    /* Lame wants samples from -32768 to 32768 */
    float         sampleLevel = 32768;

    int           frameSize;
    HBBuffer    * rawBuffer;
    sample_t    * samples;

    /* Main loop */
    for( ;; )
    {
        while( fSuspend )
        {
            Snooze( 10000 );
        }

        fAc3Frame->fSize = 0;

        /* Get a frame header (7 bytes) */
        if( !( GetBytes( 7 ) ) )
        {
            break;
        }

        /* Get the size of the current frame */
        frameSize = a52_syncinfo( fAc3Frame->fData, &inFlags,
                                  &fAudio->fInSampleRate,
                                  &fAudio->fInBitrate );

        if( !frameSize )
        {
            Log( "HBAc3Decoder : a52_syncinfo failed" );
            fManager->Error();
            break;
        }

        /* Get the whole frame */
        if( !( GetBytes( (uint32_t) frameSize ) ) )
        {
            break;
        }

        /* 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 ) );
        rawBuffer->fPosition = fPosition;
        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 ) );
        }

        if( !Push( fAudio->fRawFifo, rawBuffer ) )
        {
            break;
        }
    }
}

/* 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 = Pop( fAudio->fAc3Fifo ) ) )
            {
                return false;
            }
            fPosInBuffer = 0;
            fPosition    = fAc3Buffer->fPosition;
        }

        int willCopy = MIN( size - fAc3Frame->fSize,
                            fAc3Buffer->fSize - fPosInBuffer );
        memcpy( fAc3Frame->fData + fAc3Frame->fSize,
                fAc3Buffer->fData + fPosInBuffer,
                willCopy );
        fAc3Frame->fSize += willCopy;
        fPosInBuffer     += willCopy;

        if( fAc3Buffer->fSize == fPosInBuffer )
        {
            delete fAc3Buffer;
            fAc3Buffer = NULL;
        }
    }

    return true;
}