summaryrefslogtreecommitdiffstats
path: root/libhb/enclame.c
blob: 1329836e55567f9d4a3fa2e519b130c386ec043e (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* enclame.c

   Copyright (c) 2003-2013 HandBrake Team
   This file is part of the HandBrake source code
   Homepage: <http://handbrake.fr/>.
   It may be used under the terms of the GNU General Public License v2.
   For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
 */
#include "hb.h"

#include "lame/lame.h"

int  enclameInit( hb_work_object_t *, hb_job_t * );
int  enclameWork( hb_work_object_t *, hb_buffer_t **, hb_buffer_t ** );
void enclameClose( hb_work_object_t * );

hb_work_object_t hb_enclame =
{
    WORK_ENCLAME,
    "MP3 encoder (libmp3lame)",
    enclameInit,
    enclameWork,
    enclameClose
};

struct hb_work_private_s
{
    hb_job_t   * job;

    /* LAME handle */
    lame_global_flags * lame;

    int             out_discrete_channels;
    unsigned long   input_samples;
    unsigned long   output_bytes;
    uint8_t       * buf;

    hb_list_t     * list;
    int64_t         pts;
};

int enclameInit( hb_work_object_t * w, hb_job_t * job )
{
    hb_work_private_t * pv = calloc( 1, sizeof( hb_work_private_t ) );
    hb_audio_t * audio = w->audio;

    w->private_data = pv;

    pv->job   = job;

    hb_log( "enclame: opening libmp3lame" );

    pv->lame = lame_init();
    // use ABR
    lame_set_scale( pv->lame, 32768.0 );
    if( audio->config.out.compression_level >= 0 )
    {
        lame_set_quality( pv->lame, audio->config.out.compression_level );
    }
    if( audio->config.out.bitrate > 0 )
    {
        lame_set_VBR( pv->lame, vbr_abr );
        lame_set_VBR_mean_bitrate_kbps( pv->lame, audio->config.out.bitrate );
    }
    else if( audio->config.out.quality >= 0 )
    {
        lame_set_brate( pv->lame, 0 );
        lame_set_VBR( pv->lame, vbr_default );
        lame_set_VBR_quality( pv->lame, audio->config.out.quality );
    }
    lame_set_in_samplerate( pv->lame, audio->config.out.samplerate );
    lame_set_out_samplerate( pv->lame, audio->config.out.samplerate );

    pv->out_discrete_channels = hb_mixdown_get_discrete_channel_count( audio->config.out.mixdown );
    // Lame's default encoding mode is JOINT_STEREO.  This subtracts signal
    // that is "common" to left and right (within some threshold) and encodes
    // it separately.  This improves quality at low bitrates, but hurts 
    // imaging (channel separation) at higher bitrates.  So if the bitrate
    // is suffeciently high, use regular STEREO mode.
    if ( pv->out_discrete_channels == 1 )
    {
        lame_set_mode( pv->lame, MONO );
        lame_set_num_channels( pv->lame, 1 );
    }
    else if ( audio->config.out.bitrate >= 128 )
    {
        lame_set_mode( pv->lame, STEREO );
    }
    lame_init_params( pv->lame );

    pv->input_samples = 1152 * pv->out_discrete_channels;
    pv->output_bytes = LAME_MAXMP3BUFFER;
    pv->buf  = malloc( pv->input_samples * sizeof( float ) );
    audio->config.out.samples_per_frame = 1152;

    pv->list = hb_list_init();
    pv->pts  = AV_NOPTS_VALUE;

    return 0;
}

/***********************************************************************
 * Close
 ***********************************************************************
 *
 **********************************************************************/
void enclameClose( hb_work_object_t * w )
{
    hb_work_private_t * pv = w->private_data;

    lame_close( pv->lame );
    hb_list_empty( &pv->list );
    free( pv->buf );
    free( pv );
    w->private_data = NULL;
}

/***********************************************************************
 * Encode
 ***********************************************************************
 *
 **********************************************************************/
static hb_buffer_t * Encode( hb_work_object_t * w )
{
    hb_work_private_t * pv = w->private_data;
    hb_audio_t * audio = w->audio;
    hb_buffer_t * buf;
    float samples[2][1152];
    uint64_t pts, pos;
    int      i, j;

    if( hb_list_bytes( pv->list ) < pv->input_samples * sizeof( float ) )
    {
        return NULL;
    }

    hb_list_getbytes( pv->list, pv->buf, pv->input_samples * sizeof( float ),
                      &pts, &pos);

    for( i = 0; i < 1152; i++ )
    {
        for( j = 0; j < pv->out_discrete_channels; j++ )
        {
            samples[j][i] = ((float *) pv->buf)[(pv->out_discrete_channels * i + j)];
        }
    }

    buf             = hb_buffer_init( pv->output_bytes );
    buf->s.start    = pts + 90000 * pos / pv->out_discrete_channels / sizeof( float ) / audio->config.out.samplerate;
    buf->s.duration = (double)90000 * 1152 / audio->config.out.samplerate;
    buf->s.stop     = buf->s.start + buf->s.duration;
    pv->pts = buf->s.stop;
    buf->size  = lame_encode_buffer_float( 
            pv->lame, samples[0], samples[1],
            1152, buf->data, LAME_MAXMP3BUFFER );

    buf->s.type = AUDIO_BUF;
    buf->s.frametype = HB_FRAME_AUDIO;

    if( !buf->size )
    {
        /* Encoding was successful but we got no data. Try to encode
           more */
        hb_buffer_close( &buf );
        return Encode( w );
    }
    else if( buf->size < 0 )
    {
        hb_log( "enclame: lame_encode_buffer failed" );
        hb_buffer_close( &buf );
        return NULL;
    }
    return buf;
}

/***********************************************************************
 * Work
 ***********************************************************************
 *
 **********************************************************************/
int enclameWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                 hb_buffer_t ** buf_out )
{
    hb_work_private_t * pv = w->private_data;
    hb_audio_t * audio = w->audio;
    hb_buffer_t * in = *buf_in;
    hb_buffer_t * buf;

    if ( (*buf_in)->size <= 0 )
    {
        /* EOF on input - send it downstream & say we're done */

        buf = hb_buffer_init( pv->output_bytes );
        buf->size = lame_encode_flush( pv->lame, buf->data, LAME_MAXMP3BUFFER );
        buf->s.start = pv->pts;
        buf->s.stop  = buf->s.start + 90000 * 1152 / audio->config.out.samplerate;

        buf->s.type = AUDIO_BUF;
        buf->s.frametype = HB_FRAME_AUDIO;

        if( buf->size <= 0 )
        {
            hb_buffer_close( &buf );
        }

        // Add the flushed data
        *buf_out = buf;

        // Add the eof
        if ( buf )
        {
            buf->next = in;
        }
        else
        {
            *buf_out = in;
        }

        *buf_in = NULL;
        return HB_WORK_DONE;
    }

    hb_list_add( pv->list, *buf_in );
    *buf_in = NULL;

    *buf_out = buf = Encode( w );

    while( buf )
    {
        buf->next = Encode( w );
        buf       = buf->next;
    }

    return HB_WORK_OK;
}