Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ac3dec.c @ 186447f8

History | View | Annotate | Download (5.23 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * AC3 decoder
3 ff4ec49e Fabrice Bellard
 * Copyright (c) 2001 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 ff4ec49e Fabrice Bellard
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9 de6d9b64 Fabrice Bellard
 *
10 ff4ec49e Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ff4ec49e Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 ff4ec49e Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 de6d9b64 Fabrice Bellard
 */
19 983e3246 Michael Niedermayer
20
/**
21
 * @file ac3dec.c
22
 * AC3 decoder.
23
 */
24
25
//#define DEBUG
26
27 de6d9b64 Fabrice Bellard
#include "avcodec.h"
28
#include "libac3/ac3.h"
29
30
/* currently, I use libac3 which is Copyright (C) Aaron Holtzman and
31
   released under the GPL license. I may reimplement it someday... */
32
typedef struct AC3DecodeState {
33 0c1a9eda Zdenek Kabelac
    uint8_t inbuf[4096]; /* input buffer */
34
    uint8_t *inbuf_ptr;
35 de6d9b64 Fabrice Bellard
    int frame_size;
36
    int flags;
37 30b68f33 Zdenek Kabelac
    int channels;
38 de6d9b64 Fabrice Bellard
    ac3_state_t state;
39
} AC3DecodeState;
40
41
static int ac3_decode_init(AVCodecContext *avctx)
42
{
43
    AC3DecodeState *s = avctx->priv_data;
44
45
    ac3_init ();
46
    s->inbuf_ptr = s->inbuf;
47
    s->frame_size = 0;
48
    return 0;
49
}
50
51
stream_samples_t samples;
52
53
/**** the following two functions comes from ac3dec */
54
static inline int blah (int32_t i)
55
{
56
    if (i > 0x43c07fff)
57
        return 32767;
58
    else if (i < 0x43bf8000)
59
        return -32768;
60
    else
61
        return i - 0x43c00000;
62
}
63
64 0c1a9eda Zdenek Kabelac
static inline void float_to_int (float * _f, int16_t * s16, int nchannels)
65 de6d9b64 Fabrice Bellard
{
66 30b68f33 Zdenek Kabelac
    int i, j, c;
67 de6d9b64 Fabrice Bellard
    int32_t * f = (int32_t *) _f;        // XXX assumes IEEE float format
68
69 30b68f33 Zdenek Kabelac
    j = 0;
70
    nchannels *= 256;
71 de6d9b64 Fabrice Bellard
    for (i = 0; i < 256; i++) {
72 30b68f33 Zdenek Kabelac
        for (c = 0; c < nchannels; c += 256)
73
            s16[j++] = blah (f[i + c]);
74 de6d9b64 Fabrice Bellard
    }
75
}
76
77
/**** end */
78
79
#define HEADER_SIZE 7
80
81
static int ac3_decode_frame(AVCodecContext *avctx, 
82
                            void *data, int *data_size,
83 0c1a9eda Zdenek Kabelac
                            uint8_t *buf, int buf_size)
84 de6d9b64 Fabrice Bellard
{
85
    AC3DecodeState *s = avctx->priv_data;
86 0c1a9eda Zdenek Kabelac
    uint8_t *buf_ptr;
87 de6d9b64 Fabrice Bellard
    int flags, i, len;
88
    int sample_rate, bit_rate;
89
    short *out_samples = data;
90
    float level;
91 076b09e9 Zdenek Kabelac
    static const int ac3_channels[8] = {
92 30b68f33 Zdenek Kabelac
        2, 1, 2, 3, 3, 4, 4, 5
93
    };
94 de6d9b64 Fabrice Bellard
95
    *data_size = 0;
96
    buf_ptr = buf;
97
    while (buf_size > 0) {
98
        len = s->inbuf_ptr - s->inbuf;
99
        if (s->frame_size == 0) {
100
            /* no header seen : find one. We need at least 7 bytes to parse it */
101
            len = HEADER_SIZE - len;
102
            if (len > buf_size)
103
                len = buf_size;
104
            memcpy(s->inbuf_ptr, buf_ptr, len);
105
            buf_ptr += len;
106
            s->inbuf_ptr += len;
107
            buf_size -= len;
108
            if ((s->inbuf_ptr - s->inbuf) == HEADER_SIZE) {
109
                len = ac3_syncinfo (s->inbuf, &s->flags, &sample_rate, &bit_rate);
110
                if (len == 0) {
111
                    /* no sync found : move by one byte (inefficient, but simple!) */
112
                    memcpy(s->inbuf, s->inbuf + 1, HEADER_SIZE - 1);
113
                    s->inbuf_ptr--;
114
                } else {
115 076b09e9 Zdenek Kabelac
                    s->frame_size = len;
116 de6d9b64 Fabrice Bellard
                    /* update codec info */
117
                    avctx->sample_rate = sample_rate;
118 30b68f33 Zdenek Kabelac
                    s->channels = ac3_channels[s->flags & 7];
119 e0d2714a Juanjo
                    if (s->flags & AC3_LFE)
120 076b09e9 Zdenek Kabelac
                        s->channels++;
121
                    if (avctx->channels == 0)
122
                        /* No specific number of channel requested */
123
                        avctx->channels = s->channels;
124
                    else if (s->channels < avctx->channels) {
125
                        fprintf(stderr, "ac3dec: AC3 Source channels are less than specified: output to %d channels.. (frmsize: %d)\n", s->channels, len);
126
                        avctx->channels = s->channels;
127
                    }
128
                    avctx->bit_rate = bit_rate;
129 de6d9b64 Fabrice Bellard
                }
130
            }
131
        } else if (len < s->frame_size) {
132
            len = s->frame_size - len;
133
            if (len > buf_size)
134
                len = buf_size;
135 076b09e9 Zdenek Kabelac
136 de6d9b64 Fabrice Bellard
            memcpy(s->inbuf_ptr, buf_ptr, len);
137
            buf_ptr += len;
138
            s->inbuf_ptr += len;
139
            buf_size -= len;
140
        } else {
141 e0d2714a Juanjo
            flags = s->flags;
142 de6d9b64 Fabrice Bellard
            if (avctx->channels == 1)
143
                flags = AC3_MONO;
144 e0d2714a Juanjo
            else if (avctx->channels == 2)
145 de6d9b64 Fabrice Bellard
                flags = AC3_STEREO;
146 e0d2714a Juanjo
            else
147
                flags |= AC3_ADJUST_LEVEL;
148 de6d9b64 Fabrice Bellard
            level = 1;
149
            if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
150
            fail:
151
                s->inbuf_ptr = s->inbuf;
152
                s->frame_size = 0;
153
                continue;
154
            }
155
            for (i = 0; i < 6; i++) {
156
                if (ac3_block (&s->state))
157
                    goto fail;
158 e0d2714a Juanjo
                float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
159 de6d9b64 Fabrice Bellard
            }
160
            s->inbuf_ptr = s->inbuf;
161
            s->frame_size = 0;
162 0c1a9eda Zdenek Kabelac
            *data_size = 6 * avctx->channels * 256 * sizeof(int16_t);
163 de6d9b64 Fabrice Bellard
            break;
164
        }
165
    }
166
    return buf_ptr - buf;
167
}
168
169
static int ac3_decode_end(AVCodecContext *s)
170
{
171
    return 0;
172
}
173
174
AVCodec ac3_decoder = {
175
    "ac3",
176
    CODEC_TYPE_AUDIO,
177
    CODEC_ID_AC3,
178
    sizeof(AC3DecodeState),
179
    ac3_decode_init,
180
    NULL,
181
    ac3_decode_end,
182
    ac3_decode_frame,
183
};