Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ 05f361f0

History | View | Annotate | Download (4.96 KB)

1
/*
2
 * AC3 decoder
3
 * Copyright (c) 2001 Gerard Lantau.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 */
19
#include "avcodec.h"
20
#include "libac3/ac3.h"
21

    
22
/* currently, I use libac3 which is Copyright (C) Aaron Holtzman and
23
   released under the GPL license. I may reimplement it someday... */
24
typedef struct AC3DecodeState {
25
    UINT8 inbuf[4096]; /* input buffer */
26
    UINT8 *inbuf_ptr;
27
    int frame_size;
28
    int flags;
29
    ac3_state_t state;
30
} AC3DecodeState;
31

    
32
static int ac3_decode_init(AVCodecContext *avctx)
33
{
34
    AC3DecodeState *s = avctx->priv_data;
35

    
36
    ac3_init ();
37
    s->inbuf_ptr = s->inbuf;
38
    s->frame_size = 0;
39
    return 0;
40
}
41

    
42
stream_samples_t samples;
43

    
44
/**** the following two functions comes from ac3dec */
45
static inline int blah (int32_t i)
46
{
47
    if (i > 0x43c07fff)
48
        return 32767;
49
    else if (i < 0x43bf8000)
50
        return -32768;
51
    else
52
        return i - 0x43c00000;
53
}
54

    
55
static inline void float_to_int (float * _f, INT16 * s16) 
56
{
57
    int i;
58
    int32_t * f = (int32_t *) _f;        // XXX assumes IEEE float format
59

    
60
    for (i = 0; i < 256; i++) {
61
        s16[2*i] = blah (f[i]);
62
        s16[2*i+1] = blah (f[i+256]);
63
    }
64
}
65

    
66
static inline void float_to_int_mono (float * _f, INT16 * s16) 
67
{
68
    int i;
69
    int32_t * f = (int32_t *) _f;        // XXX assumes IEEE float format
70

    
71
    for (i = 0; i < 256; i++) {
72
        s16[i] = blah (f[i]);
73
    }
74
}
75

    
76
/**** end */
77

    
78
#define HEADER_SIZE 7
79

    
80
static int ac3_decode_frame(AVCodecContext *avctx, 
81
                            void *data, int *data_size,
82
                            UINT8 *buf, int buf_size)
83
{
84
    AC3DecodeState *s = avctx->priv_data;
85
    UINT8 *buf_ptr;
86
    int flags, i, len;
87
    int sample_rate, bit_rate;
88
    short *out_samples = data;
89
    float level;
90

    
91
    *data_size = 0;
92
    buf_ptr = buf;
93
    while (buf_size > 0) {
94
        len = s->inbuf_ptr - s->inbuf;
95
        if (s->frame_size == 0) {
96
            /* no header seen : find one. We need at least 7 bytes to parse it */
97
            len = HEADER_SIZE - len;
98
            if (len > buf_size)
99
                len = buf_size;
100
            memcpy(s->inbuf_ptr, buf_ptr, len);
101
            buf_ptr += len;
102
            s->inbuf_ptr += len;
103
            buf_size -= len;
104
            if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
105
                len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
106
                if (len == 0) {
107
                    /* no sync found : move by one byte (inefficient, but simple!) */
108
                    memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
109
                    s->inbuf_ptr--;
110
                } else {
111
                    s->frame_size = len;
112
                    /* update codec info */
113
                    avctx->sample_rate = sample_rate;
114
                    if ((s->flags & AC3_CHANNEL_MASK) == AC3_MONO)
115
                        avctx->channels = 1;
116
                    else
117
                        avctx->channels = 2;
118
                    avctx->bit_rate = bit_rate;
119
                }
120
            }
121
        } else if (len < s->frame_size) {
122
            len = s->frame_size - len;
123
            if (len > buf_size)
124
                len = buf_size;
125
            
126
            memcpy(s->inbuf_ptr, buf_ptr, len);
127
            buf_ptr += len;
128
            s->inbuf_ptr += len;
129
            buf_size -= len;
130
        } else {
131
            if (avctx->channels == 1)
132
                flags = AC3_MONO;
133
            else
134
                flags = AC3_STEREO;
135

    
136
            flags |= AC3_ADJUST_LEVEL;
137
            level = 1;
138
            if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
139
            fail:
140
                s->inbuf_ptr = s->inbuf;
141
                s->frame_size = 0;
142
                continue;
143
            }
144
            for (i = 0; i < 6; i++) {
145
                if (ac3_block (&s->state))
146
                    goto fail;
147
                if (avctx->channels == 1)
148
                    float_to_int_mono (*samples, out_samples + i * 256);
149
                else
150
                    float_to_int (*samples, out_samples + i * 512);
151
            }
152
            s->inbuf_ptr = s->inbuf;
153
            s->frame_size = 0;
154
            *data_size = 6 * avctx->channels * 256 * sizeof(INT16);
155
            break;
156
        }
157
    }
158
    return buf_ptr - buf;
159
}
160

    
161
static int ac3_decode_end(AVCodecContext *s)
162
{
163
    return 0;
164
}
165

    
166
AVCodec ac3_decoder = {
167
    "ac3",
168
    CODEC_TYPE_AUDIO,
169
    CODEC_ID_AC3,
170
    sizeof(AC3DecodeState),
171
    ac3_decode_init,
172
    NULL,
173
    ac3_decode_end,
174
    ac3_decode_frame,
175
};