Statistics
| Branch: | Revision:

ffmpeg / libavcodec / mpegaudio_parser.c @ 4bd8e17c

History | View | Annotate | Download (8.82 KB)

1
/*
2
 * MPEG Audio parser
3
 * Copyright (c) 2003 Fabrice Bellard.
4
 * Copyright (c) 2003 Michael Niedermayer.
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22

    
23
#include "parser.h"
24
#include "mpegaudio.h"
25
#include "mpegaudiodecheader.h"
26

    
27

    
28
typedef struct MpegAudioParseContext {
29
    uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE];    /* input buffer */
30
    uint8_t *inbuf_ptr;
31
    int frame_size;
32
    int free_format_frame_size;
33
    int free_format_next_header;
34
    uint32_t header;
35
    int header_count;
36
} MpegAudioParseContext;
37

    
38
#define MPA_HEADER_SIZE 4
39

    
40
/* header + layer + bitrate + freq + lsf/mpeg25 */
41
#undef SAME_HEADER_MASK /* mpegaudio.h defines different version */
42
#define SAME_HEADER_MASK \
43
   (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
44

    
45
/* useful helper to get mpeg audio stream infos. Return -1 if error in
46
   header, otherwise the coded frame size in bytes */
47
int mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate)
48
{
49
    MPADecodeContext s1, *s = &s1;
50
    s1.avctx = avctx;
51

    
52
    if (ff_mpa_check_header(head) != 0)
53
        return -1;
54

    
55
    if (decode_header(s, head) != 0) {
56
        return -1;
57
    }
58

    
59
    switch(s->layer) {
60
    case 1:
61
        avctx->frame_size = 384;
62
        break;
63
    case 2:
64
        avctx->frame_size = 1152;
65
        break;
66
    default:
67
    case 3:
68
        if (s->lsf)
69
            avctx->frame_size = 576;
70
        else
71
            avctx->frame_size = 1152;
72
        break;
73
    }
74

    
75
    *sample_rate = s->sample_rate;
76
    avctx->channels = s->nb_channels;
77
    avctx->bit_rate = s->bit_rate;
78
    avctx->sub_id = s->layer;
79
    return s->frame_size;
80
}
81

    
82
static int mpegaudio_parse_init(AVCodecParserContext *s1)
83
{
84
    MpegAudioParseContext *s = s1->priv_data;
85
    s->inbuf_ptr = s->inbuf;
86
    return 0;
87
}
88

    
89
static int mpegaudio_parse(AVCodecParserContext *s1,
90
                           AVCodecContext *avctx,
91
                           const uint8_t **poutbuf, int *poutbuf_size,
92
                           const uint8_t *buf, int buf_size)
93
{
94
    MpegAudioParseContext *s = s1->priv_data;
95
    int len, ret, sr;
96
    uint32_t header;
97
    const uint8_t *buf_ptr;
98

    
99
    *poutbuf = NULL;
100
    *poutbuf_size = 0;
101
    buf_ptr = buf;
102
    while (buf_size > 0) {
103
        len = s->inbuf_ptr - s->inbuf;
104
        if (s->frame_size == 0) {
105
            /* special case for next header for first frame in free
106
               format case (XXX: find a simpler method) */
107
            if (s->free_format_next_header != 0) {
108
                s->inbuf[0] = s->free_format_next_header >> 24;
109
                s->inbuf[1] = s->free_format_next_header >> 16;
110
                s->inbuf[2] = s->free_format_next_header >> 8;
111
                s->inbuf[3] = s->free_format_next_header;
112
                s->inbuf_ptr = s->inbuf + 4;
113
                s->free_format_next_header = 0;
114
                goto got_header;
115
            }
116
            /* no header seen : find one. We need at least MPA_HEADER_SIZE
117
               bytes to parse it */
118
            len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
119
            if (len > 0) {
120
                memcpy(s->inbuf_ptr, buf_ptr, len);
121
                buf_ptr += len;
122
                buf_size -= len;
123
                s->inbuf_ptr += len;
124
            }
125
            if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
126
            got_header:
127
                header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
128
                    (s->inbuf[2] << 8) | s->inbuf[3];
129

    
130
                ret = mpa_decode_header(avctx, header, &sr);
131
                if (ret < 0) {
132
                    s->header_count= -2;
133
                    /* no sync found : move by one byte (inefficient, but simple!) */
134
                    memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
135
                    s->inbuf_ptr--;
136
                    dprintf(avctx, "skip %x\n", header);
137
                    /* reset free format frame size to give a chance
138
                       to get a new bitrate */
139
                    s->free_format_frame_size = 0;
140
                } else {
141
                    if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
142
                        s->header_count= -3;
143
                    s->header= header;
144
                    s->header_count++;
145
                    s->frame_size = ret;
146

    
147
#if 0
148
                    /* free format: prepare to compute frame size */
149
                    if (decode_header(s, header) == 1) {
150
                        s->frame_size = -1;
151
                    }
152
#endif
153
                }
154
                if(s->header_count > 1)
155
                    avctx->sample_rate= sr;
156
            }
157
        } else
158
#if 0
159
        if (s->frame_size == -1) {
160
            /* free format : find next sync to compute frame size */
161
            len = MPA_MAX_CODED_FRAME_SIZE - len;
162
            if (len > buf_size)
163
                len = buf_size;
164
            if (len == 0) {
165
                /* frame too long: resync */
166
                s->frame_size = 0;
167
                memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
168
                s->inbuf_ptr--;
169
            } else {
170
                uint8_t *p, *pend;
171
                uint32_t header1;
172
                int padding;
173

174
                memcpy(s->inbuf_ptr, buf_ptr, len);
175
                /* check for header */
176
                p = s->inbuf_ptr - 3;
177
                pend = s->inbuf_ptr + len - 4;
178
                while (p <= pend) {
179
                    header = (p[0] << 24) | (p[1] << 16) |
180
                        (p[2] << 8) | p[3];
181
                    header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
182
                        (s->inbuf[2] << 8) | s->inbuf[3];
183
                    /* check with high probability that we have a
184
                       valid header */
185
                    if ((header & SAME_HEADER_MASK) ==
186
                        (header1 & SAME_HEADER_MASK)) {
187
                        /* header found: update pointers */
188
                        len = (p + 4) - s->inbuf_ptr;
189
                        buf_ptr += len;
190
                        buf_size -= len;
191
                        s->inbuf_ptr = p;
192
                        /* compute frame size */
193
                        s->free_format_next_header = header;
194
                        s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
195
                        padding = (header1 >> 9) & 1;
196
                        if (s->layer == 1)
197
                            s->free_format_frame_size -= padding * 4;
198
                        else
199
                            s->free_format_frame_size -= padding;
200
                        dprintf(avctx, "free frame size=%d padding=%d\n",
201
                                s->free_format_frame_size, padding);
202
                        decode_header(s, header1);
203
                        goto next_data;
204
                    }
205
                    p++;
206
                }
207
                /* not found: simply increase pointers */
208
                buf_ptr += len;
209
                s->inbuf_ptr += len;
210
                buf_size -= len;
211
            }
212
        } else
213
#endif
214
        if (len < s->frame_size) {
215
            if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE)
216
                s->frame_size = MPA_MAX_CODED_FRAME_SIZE;
217
            len = FFMIN(s->frame_size - len, buf_size);
218
            memcpy(s->inbuf_ptr, buf_ptr, len);
219
            buf_ptr += len;
220
            s->inbuf_ptr += len;
221
            buf_size -= len;
222
        }
223

    
224
        if(s->frame_size > 0 && buf_ptr - buf == s->inbuf_ptr - s->inbuf
225
           && buf_size + buf_ptr - buf >= s->frame_size){
226
            if(s->header_count > 0){
227
                *poutbuf = buf;
228
                *poutbuf_size = s->frame_size;
229
            }
230
            buf_ptr = buf + s->frame_size;
231
            s->inbuf_ptr = s->inbuf;
232
            s->frame_size = 0;
233
            break;
234
        }
235

    
236
        //    next_data:
237
        if (s->frame_size > 0 &&
238
            (s->inbuf_ptr - s->inbuf) >= s->frame_size) {
239
            if(s->header_count > 0){
240
                *poutbuf = s->inbuf;
241
                *poutbuf_size = s->inbuf_ptr - s->inbuf;
242
            }
243
            s->inbuf_ptr = s->inbuf;
244
            s->frame_size = 0;
245
            break;
246
        }
247
    }
248
    return buf_ptr - buf;
249
}
250

    
251

    
252
AVCodecParser mpegaudio_parser = {
253
    { CODEC_ID_MP2, CODEC_ID_MP3 },
254
    sizeof(MpegAudioParseContext),
255
    mpegaudio_parse_init,
256
    mpegaudio_parse,
257
    NULL,
258
};