summaryrefslogtreecommitdiffstats
path: root/core/Mp3Encoder.cpp
blob: 06d96af293e660d16ea0dfcd07967b9294b9e6c2 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* $Id: Mp3Encoder.cpp,v 1.13 2003/10/08 15:00:20 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 "Mp3Encoder.h"
#include "Manager.h"
#include "Fifo.h"

#include <lame/lame.h>

HBMp3Encoder::HBMp3Encoder( HBManager * manager, HBAudio * audio )
{
    fManager = manager;
    fAudio   = audio;

    fLock = new HBLock();
    fUsed = false;

    fRawBuffer    = NULL;
    fPosInBuffer  = 0;
    fSamplesNb    = 0;
    fLeftSamples  = NULL;
    fRightSamples = NULL;

    fPosition     = 0;
    fInitDone = false;
    fMp3Buffer = NULL;
}

bool HBMp3Encoder::Work()
{
    if( !Lock() )
    {
        return false;
    }
    
    if( !fInitDone )
    {
        /* Wait for a first buffer so we know fAudio->fInSampleRate
           is correct */
        if( !fAudio->fRawFifo->Size() )
        {
            Unlock();
            return false;
        }
        
        /* The idea is to have exactly one mp3 frame (i.e. 1152 samples) by
           output buffer. As we are resampling from fInSampleRate to
           fOutSampleRate, we will give ( 1152 * fInSampleRate ) /
           ( 2 * fOutSampleRate ) to libmp3lame so we are sure we will
           never get more than 1 frame at a time */
        fCount = ( 1152 * fAudio->fInSampleRate ) /
                    ( 2 * fAudio->fOutSampleRate );

        /* Init libmp3lame */
        fGlobalFlags = lame_init();
        lame_set_in_samplerate( fGlobalFlags, fAudio->fInSampleRate );
        lame_set_out_samplerate( fGlobalFlags, fAudio->fOutSampleRate );
        lame_set_brate( fGlobalFlags, fAudio->fOutBitrate );

        if( lame_init_params( fGlobalFlags ) == -1 )
        {
            Log( "HBMp3Encoder: lame_init_params() failed" );
            fManager->Error( HB_ERROR_MP3_INIT );
            return false;
        }

        fLeftSamples  = (float*) malloc( fCount * sizeof( float ) );
        fRightSamples = (float*) malloc( fCount * sizeof( float ) );

        fInitDone = true;
    }

    bool didSomething = false;

    for( ;; )
    {
        if( fMp3Buffer )
        {
            if( fAudio->fMp3Fifo->Push( fMp3Buffer ) )
            {
                fMp3Buffer = NULL;
            }
            else
            {
                break;
            }
        }
        
        /* Get new samples */
        if( !GetSamples() )
        {
            break;
        }

        int ret;
        fMp3Buffer = new HBBuffer( LAME_MAXMP3BUFFER );
        ret = lame_encode_buffer_float( fGlobalFlags, fLeftSamples,
                                        fRightSamples, fCount,
                                        fMp3Buffer->fData,
                                        fMp3Buffer->fSize );

        if( ret < 0 )
        {
            /* Something wrong happened */
            Log( "HBMp3Encoder : lame_encode_buffer_float() failed "
                 "(%d)", ret );
            fManager->Error( HB_ERROR_MP3_ENCODE );
            return false;
        }
        else if( ret > 0 )
        {
            /* We got something, send it to the muxer */
            fMp3Buffer->fSize     = ret;
            fMp3Buffer->fKeyFrame = true;
            fMp3Buffer->fPosition = fPosition;
        }
        else
        {
            delete fMp3Buffer;
            fMp3Buffer = NULL;
        }
        fSamplesNb = 0;

        didSomething = true;
    }

    Unlock();
    return didSomething;
}

bool HBMp3Encoder::Lock()
{
    fLock->Lock();
    if( fUsed )
    {
        fLock->Unlock();
        return false;
    }
    fUsed = true;
    fLock->Unlock();
    return true;
}

void HBMp3Encoder::Unlock()
{
    fLock->Lock();
    fUsed = false;
    fLock->Unlock();
}

bool HBMp3Encoder::GetSamples()
{
    while( fSamplesNb < fCount )
    {
        if( !fRawBuffer )
        {
            if( !( fRawBuffer = fAudio->fRawFifo->Pop() ) )
            {
                return false;
            }

            fPosInBuffer = 0;
            fPosition    = fRawBuffer->fPosition;
        }

        int willCopy = MIN( fCount - fSamplesNb, 6 * 256 - fPosInBuffer );

        memcpy( fLeftSamples + fSamplesNb,
                (float*) fRawBuffer->fData + fPosInBuffer,
                willCopy * sizeof( float ) );
        memcpy( fRightSamples + fSamplesNb,
                (float*) fRawBuffer->fData + 6 * 256 + fPosInBuffer,
                willCopy * sizeof( float ) );

        fSamplesNb    += willCopy;
        fPosInBuffer += willCopy;

        if( fPosInBuffer == 6 * 256 )
        {
            delete fRawBuffer;
            fRawBuffer = NULL;
        }
    }

    return true;
}