summaryrefslogtreecommitdiffstats
path: root/core/MpgaDec.c
blob: e397a7234ca6f6ab140f48d30b131e81f6f42b33 (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
/* $Id: MpgaDec.c,v 1.3 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 "ffmpeg/avcodec.h"

struct HBWork
{
    HB_WORK_COMMON_MEMBERS

    HBHandle    * handle;
    HBAudio     * audio;

    AVCodecContext * context;
};

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

HBWork * HBMpgaDecInit( HBHandle * handle, HBAudio * audio )
{
    HBWork  * w;
    AVCodec * codec;

    if( !( w = calloc( sizeof( HBWork ), 1 ) ) )
    {
        HBLog( "HBMpgaDecInit: malloc() failed, gonna crash" );
        return NULL;
    }

    w->name = strdup( "MpgaDec" );
    w->work = MpgaDecWork;

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

    codec = avcodec_find_decoder( CODEC_ID_MP2 );
    if( !codec )
    {
        HBLog( "HBMpgaDec: avcodec_find_decoder failed" );
    }

    w->context = avcodec_alloc_context();
    if( !w->context )
    {
        HBLog( "HBMpgaDec: avcodec_alloc_context failed" );
    }

    if( avcodec_open( w->context, codec ) < 0 )
    {
        HBLog( "HBMpgaDec: avcodec_open failed" );
    }

    return w;
}

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

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

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

    HBBuffer  * mpgaBuffer;
    HBBuffer  * rawBuffer;

    int out_size, len, pos;
    short buffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];

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

    /* Get a new mpeg buffer */
    mpgaBuffer = HBFifoPop( audio->inFifo );
    if( !mpgaBuffer )
    {
        return 0;
    }

    pos = 0;
    while( pos < mpgaBuffer->size )
    {
        len = avcodec_decode_audio( w->context, buffer, &out_size,
                                    mpgaBuffer->data + pos,
                                    mpgaBuffer->size - pos );
        pos += len;

        if( !audio->inSampleRate )
        {
            audio->inSampleRate = w->context->sample_rate;
            HBLog( "HBMpgaDec: samplerate = %d", audio->inSampleRate );
        }

        if( out_size )
        {
            int i;
            rawBuffer = HBBufferInit( 2 * AVCODEC_MAX_AUDIO_FRAME_SIZE );
            rawBuffer->position = mpgaBuffer->position;

            /* s16 -> float */
            for( i = 0; i < out_size / 2; i++ )
            {
                rawBuffer->dataf[i] = buffer[i];
            }

            rawBuffer->size = out_size * 2;

            if( !HBFifoPush( audio->rawFifo, &rawBuffer ) )
            {
                HBLog( "HBMpgaDec: HBFifoPush failed" );
            }
        }
    }

    HBBufferClose( &mpgaBuffer );

    return 1;
}