Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ bb71e317

History | View | Annotate | Download (5.12 KB)

1 de6d9b64 Fabrice Bellard
/*
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 30b68f33 Zdenek Kabelac
    int channels;
30 de6d9b64 Fabrice Bellard
    ac3_state_t state;
31
} AC3DecodeState;
32
33
static int ac3_decode_init(AVCodecContext *avctx)
34
{
35
    AC3DecodeState *s = avctx->priv_data;
36
37
    ac3_init ();
38
    s->inbuf_ptr = s->inbuf;
39
    s->frame_size = 0;
40
    return 0;
41
}
42
43
stream_samples_t samples;
44
45
/**** the following two functions comes from ac3dec */
46
static inline int blah (int32_t i)
47
{
48
    if (i > 0x43c07fff)
49
        return 32767;
50
    else if (i < 0x43bf8000)
51
        return -32768;
52
    else
53
        return i - 0x43c00000;
54
}
55
56 30b68f33 Zdenek Kabelac
static inline void float_to_int (float * _f, INT16 * s16, int nchannels)
57 de6d9b64 Fabrice Bellard
{
58 30b68f33 Zdenek Kabelac
    int i, j, c;
59 de6d9b64 Fabrice Bellard
    int32_t * f = (int32_t *) _f;        // XXX assumes IEEE float format
60
61 30b68f33 Zdenek Kabelac
    j = 0;
62
    nchannels *= 256;
63 de6d9b64 Fabrice Bellard
    for (i = 0; i < 256; i++) {
64 30b68f33 Zdenek Kabelac
        for (c = 0; c < nchannels; c += 256)
65
            s16[j++] = blah (f[i + c]);
66 de6d9b64 Fabrice Bellard
    }
67
}
68
69
/**** end */
70
71
#define HEADER_SIZE 7
72
73
static int ac3_decode_frame(AVCodecContext *avctx, 
74
                            void *data, int *data_size,
75
                            UINT8 *buf, int buf_size)
76
{
77
    AC3DecodeState *s = avctx->priv_data;
78
    UINT8 *buf_ptr;
79
    int flags, i, len;
80
    int sample_rate, bit_rate;
81
    short *out_samples = data;
82
    float level;
83 076b09e9 Zdenek Kabelac
    static const int ac3_channels[8] = {
84 30b68f33 Zdenek Kabelac
        2, 1, 2, 3, 3, 4, 4, 5
85
    };
86 de6d9b64 Fabrice Bellard
87
    *data_size = 0;
88
    buf_ptr = buf;
89
    while (buf_size > 0) {
90
        len = s->inbuf_ptr - s->inbuf;
91
        if (s->frame_size == 0) {
92
            /* no header seen : find one. We need at least 7 bytes to parse it */
93
            len = HEADER_SIZE - len;
94
            if (len > buf_size)
95
                len = buf_size;
96
            memcpy(s->inbuf_ptr, buf_ptr, len);
97
            buf_ptr += len;
98
            s->inbuf_ptr += len;
99
            buf_size -= len;
100
            if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
101
                len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
102
                if (len == 0) {
103
                    /* no sync found : move by one byte (inefficient, but simple!) */
104
                    memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
105
                    s->inbuf_ptr--;
106
                } else {
107 076b09e9 Zdenek Kabelac
                    s->frame_size = len;
108 de6d9b64 Fabrice Bellard
                    /* update codec info */
109
                    avctx->sample_rate = sample_rate;
110 30b68f33 Zdenek Kabelac
                    s->channels = ac3_channels[s->flags & 7];
111 e0d2714a Juanjo
                    if (s->flags & AC3_LFE)
112 076b09e9 Zdenek Kabelac
                        s->channels++;
113
                    if (avctx->channels == 0)
114
                        /* No specific number of channel requested */
115
                        avctx->channels = s->channels;
116
                    else if (s->channels < avctx->channels) {
117
                        fprintf(stderr, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
118
                        avctx->channels = s->channels;
119
                    }
120
                    avctx->bit_rate = bit_rate;
121 de6d9b64 Fabrice Bellard
                }
122
            }
123
        } else if (len < s->frame_size) {
124
            len = s->frame_size - len;
125
            if (len > buf_size)
126
                len = buf_size;
127 076b09e9 Zdenek Kabelac
128 de6d9b64 Fabrice Bellard
            memcpy(s->inbuf_ptr, buf_ptr, len);
129
            buf_ptr += len;
130
            s->inbuf_ptr += len;
131
            buf_size -= len;
132
        } else {
133 e0d2714a Juanjo
            flags = s->flags;
134 de6d9b64 Fabrice Bellard
            if (avctx->channels == 1)
135
                flags = AC3_MONO;
136 e0d2714a Juanjo
            else if (avctx->channels == 2)
137 de6d9b64 Fabrice Bellard
                flags = AC3_STEREO;
138 e0d2714a Juanjo
            else
139
                flags |= AC3_ADJUST_LEVEL;
140 de6d9b64 Fabrice Bellard
            level = 1;
141
            if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
142
            fail:
143
                s->inbuf_ptr = s->inbuf;
144
                s->frame_size = 0;
145
                continue;
146
            }
147
            for (i = 0; i < 6; i++) {
148
                if (ac3_block (&s->state))
149
                    goto fail;
150 e0d2714a Juanjo
                float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
151 de6d9b64 Fabrice Bellard
            }
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
};