summaryrefslogtreecommitdiffstats
path: root/core/Resample.c
blob: f3632b371a3993858cdca20ec7a5a5fb36d7f32b (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
/* $Id: Resample.c,v 1.4 2004/05/02 16:25:00 titer Exp $

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

#include "HBInternal.h"

#include "samplerate.h"

struct HBWork
{
    HB_WORK_COMMON_MEMBERS

    HBHandle  * handle;
    HBAudio   * audio;

    float     * samples;
    SRC_STATE * state;
    SRC_DATA    data;

    uint64_t    in;
    uint64_t    out;
};

/* Local prototypes */
static int ResampleWork( HBWork * );

HBWork * HBResampleInit( HBHandle * handle, HBAudio * audio )
{
    HBWork * w;
    if( !( w = calloc( sizeof( HBWork ), 1 ) ) )
    {
        HBLog( "HBResampleInit: malloc() failed, gonna crash" );
        return NULL;
    }

    w->name = strdup( "Resample" );
    w->work = ResampleWork;

    w->handle = handle;
    w->audio  = audio;

    return w;
}

void HBResampleClose( HBWork ** _w )
{
    HBWork * w = *_w;

    if( w->samples ) free( w->samples );
    if( w->state )   src_delete( w->state );

    free( w->name );
    free( w );
    *_w = NULL;
}

static int ResampleWork( HBWork * w )
{
    HBAudio  * audio = w->audio;

    HBBuffer * resampleBuffer;
    float      position;

    if( HBFifoIsHalfFull( audio->resampleFifo ) )
    {
        return 0;
    }

    /* Initialization */
    if( !w->samples )
    {
        int error;

        /* Until a first packet comes, audio->inSampleRate is
           undefined */
        if( !HBFifoSize( audio->rawFifo ) )
        {
            return 0;
        }

        /* No, the user can't choose. 44100 Hz, take it or leave it */
        audio->outSampleRate = 44100;
        HBLog( "HBResample: in = %d Hz, out = %d Hz",
                audio->inSampleRate, audio->outSampleRate );

        /* Buffer in which we'll pop the samples from the decoder */
        w->samples = malloc( audio->inSampleRate * 2 *
                             sizeof( float ) / 10 );

        /* Init libsamplerate */
        w->state = src_new( SRC_SINC_FASTEST, 2, &error );

        /* Prepare the SRC_DATA structure */
        w->data.data_in       = w->samples;
        w->data.input_frames  = audio->inSampleRate / 10;
        w->data.output_frames = audio->outSampleRate / 10;
        w->data.src_ratio     = (double) audio->outSampleRate /
                                (double) audio->inSampleRate;
        w->data.end_of_input  = 0;
    }

    /* Fix A/V synchro in case the audio track starts later than the
       video */
    if( audio->delay > 0 )
    {
        HBLog( "HBResample: adding %d ms of silence", audio->delay );

        resampleBuffer = HBBufferInit( audio->delay *
                audio->outSampleRate * 2 * sizeof( float ) / 1000 );
        memset( resampleBuffer->data, 0, resampleBuffer->size );
        if( !HBFifoPush( audio->resampleFifo, &resampleBuffer ) )
        {
            HBLog( "HBResample: HBFifoPush failed" );
        }

        audio->delay = 0;
        return 1;
    }

    /* Get samples from the decoder */
    if( !HBFifoGetBytes( audio->rawFifo, (uint8_t *) w->samples,
                         audio->inSampleRate * 2 * sizeof( float ) / 10,
                         &position ) )
    {
        return 0;
    }

    /* Init resampled buffer */
    resampleBuffer = HBBufferInit( audio->outSampleRate * 2 *
                                   sizeof( float ) / 10 );
    resampleBuffer->position = position;

    /* Resample */
    w->data.data_out = resampleBuffer->dataf;
    if( src_process( w->state, &w->data ) )
    {
        HBLog( "HBResample: src_process failed" );
    }
    resampleBuffer->size = w->data.output_frames_gen * 2 *
                           sizeof( float );

    if( w->data.input_frames_used != w->data.input_frames )
    {
        /* Here we're basically f*cked */
        HBLog( "HBResample: ohoh, %d/%d used",
               w->data.input_frames_used, w->data.input_frames );
    }

    /* Send resampled data to the encoder */
    if( !HBFifoPush( audio->resampleFifo, &resampleBuffer ) )
    {
        HBLog( "HBResample: HBFifoPush failed" );
    }

    return 1;
}