~ubuntu-branches/debian/squeeze/gstreamer0.10-ffmpeg/squeeze

« back to all changes in this revision

Viewing changes to gst-libs/ext/ffmpeg/libavcodec/mpegaudio_parser.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2010-02-19 18:14:59 UTC
  • mfrom: (4.1.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20100219181459-mect96st3px2jfsi
Tags: 0.10.9.2-1
* New upstream pre-release:
  + debian/patches/03_restricted-caps.patch,
    debian/patches/04_ignore-vdpau.patch:
    - Dropped, merged upstream.
* debian/patches/03_too-new-codec-ids.patch:
  + Disable some ffmpeg codec IDs because Debian's
    ffmpeg is once again too old...

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
 
28
28
typedef struct MpegAudioParseContext {
29
 
    uint8_t inbuf[MPA_MAX_CODED_FRAME_SIZE];    /* input buffer */
30
 
    uint8_t *inbuf_ptr;
 
29
    ParseContext pc;
31
30
    int frame_size;
32
 
    int free_format_frame_size;
33
 
    int free_format_next_header;
34
31
    uint32_t header;
35
32
    int header_count;
36
33
} MpegAudioParseContext;
81
78
    return s->frame_size;
82
79
}
83
80
 
84
 
static av_cold int mpegaudio_parse_init(AVCodecParserContext *s1)
85
 
{
86
 
    MpegAudioParseContext *s = s1->priv_data;
87
 
    s->inbuf_ptr = s->inbuf;
88
 
    return 0;
89
 
}
90
 
 
91
81
static int mpegaudio_parse(AVCodecParserContext *s1,
92
82
                           AVCodecContext *avctx,
93
83
                           const uint8_t **poutbuf, int *poutbuf_size,
94
84
                           const uint8_t *buf, int buf_size)
95
85
{
96
86
    MpegAudioParseContext *s = s1->priv_data;
97
 
    int len, ret, sr, channels, bit_rate, frame_size;
98
 
    uint32_t header;
99
 
    const uint8_t *buf_ptr;
100
 
 
101
 
    *poutbuf = NULL;
102
 
    *poutbuf_size = 0;
103
 
    buf_ptr = buf;
104
 
    while (buf_size > 0) {
105
 
        len = s->inbuf_ptr - s->inbuf;
106
 
        if (s->frame_size == 0) {
107
 
            /* special case for next header for first frame in free
108
 
               format case (XXX: find a simpler method) */
109
 
            if (s->free_format_next_header != 0) {
110
 
                AV_WB32(s->inbuf, s->free_format_next_header);
111
 
                s->inbuf_ptr = s->inbuf + 4;
112
 
                s->free_format_next_header = 0;
113
 
                goto got_header;
114
 
            }
115
 
            /* no header seen : find one. We need at least MPA_HEADER_SIZE
116
 
               bytes to parse it */
117
 
            len = FFMIN(MPA_HEADER_SIZE - len, buf_size);
118
 
            if (len > 0) {
119
 
                memcpy(s->inbuf_ptr, buf_ptr, len);
120
 
                buf_ptr += len;
121
 
                buf_size -= len;
122
 
                s->inbuf_ptr += len;
123
 
            }
124
 
            if ((s->inbuf_ptr - s->inbuf) >= MPA_HEADER_SIZE) {
125
 
            got_header:
126
 
                header = AV_RB32(s->inbuf);
127
 
 
128
 
                ret = ff_mpa_decode_header(avctx, header, &sr, &channels, &frame_size, &bit_rate);
129
 
                if (ret < 0) {
 
87
    ParseContext *pc = &s->pc;
 
88
    uint32_t state= pc->state;
 
89
    int i;
 
90
    int next= END_NOT_FOUND;
 
91
 
 
92
    for(i=0; i<buf_size; ){
 
93
        if(s->frame_size){
 
94
            int inc= FFMIN(buf_size - i, s->frame_size);
 
95
            i += inc;
 
96
            s->frame_size -= inc;
 
97
 
 
98
            if(!s->frame_size){
 
99
                next= i;
 
100
                break;
 
101
            }
 
102
        }else{
 
103
            while(i<buf_size){
 
104
                int ret, sr, channels, bit_rate, frame_size;
 
105
 
 
106
                state= (state<<8) + buf[i++];
 
107
 
 
108
                ret = ff_mpa_decode_header(avctx, state, &sr, &channels, &frame_size, &bit_rate);
 
109
                if (ret < 4) {
130
110
                    s->header_count= -2;
131
 
                    /* no sync found : move by one byte (inefficient, but simple!) */
132
 
                    memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
133
 
                    s->inbuf_ptr--;
134
 
                    dprintf(avctx, "skip %x\n", header);
135
 
                    /* reset free format frame size to give a chance
136
 
                       to get a new bitrate */
137
 
                    s->free_format_frame_size = 0;
138
111
                } else {
139
 
                    if((header&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
 
112
                    if((state&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
140
113
                        s->header_count= -3;
141
 
                    s->header= header;
 
114
                    s->header= state;
142
115
                    s->header_count++;
143
 
                    s->frame_size = ret;
 
116
                    s->frame_size = ret-4;
144
117
 
145
 
#if 0
146
 
                    /* free format: prepare to compute frame size */
147
 
                    if (ff_mpegaudio_decode_header((MPADecodeHeader *)s, header) == 1) {
148
 
                        s->frame_size = -1;
149
 
                    }
150
 
#endif
151
118
                    if(s->header_count > 1){
152
119
                        avctx->sample_rate= sr;
153
120
                        avctx->channels   = channels;
154
121
                        avctx->frame_size = frame_size;
155
122
                        avctx->bit_rate   = bit_rate;
156
123
                    }
157
 
                }
158
 
            }
159
 
        } else
160
 
#if 0
161
 
        if (s->frame_size == -1) {
162
 
            /* free format : find next sync to compute frame size */
163
 
            len = MPA_MAX_CODED_FRAME_SIZE - len;
164
 
            if (len > buf_size)
165
 
                len = buf_size;
166
 
            if (len == 0) {
167
 
                /* frame too long: resync */
168
 
                s->frame_size = 0;
169
 
                memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
170
 
                s->inbuf_ptr--;
171
 
            } else {
172
 
                uint8_t *p, *pend;
173
 
                uint32_t header1;
174
 
                int padding;
175
 
 
176
 
                memcpy(s->inbuf_ptr, buf_ptr, len);
177
 
                /* check for header */
178
 
                p = s->inbuf_ptr - 3;
179
 
                pend = s->inbuf_ptr + len - 4;
180
 
                while (p <= pend) {
181
 
                    header = AV_RB32(p);
182
 
                    header1 = AV_RB32(s->inbuf);
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
 
                        ff_mpegaudio_decode_header((MPADecodeHeader *)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;
 
124
                    break;
 
125
                }
 
126
            }
 
127
        }
 
128
    }
 
129
 
 
130
    pc->state= state;
 
131
    if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
 
132
        *poutbuf = NULL;
 
133
        *poutbuf_size = 0;
 
134
        return buf_size;
 
135
    }
 
136
 
 
137
    *poutbuf = buf;
 
138
    *poutbuf_size = buf_size;
 
139
    return next;
249
140
}
250
141
 
251
142
 
252
143
AVCodecParser mpegaudio_parser = {
253
144
    { CODEC_ID_MP1, CODEC_ID_MP2, CODEC_ID_MP3 },
254
145
    sizeof(MpegAudioParseContext),
255
 
    mpegaudio_parse_init,
 
146
    NULL,
256
147
    mpegaudio_parse,
257
 
    NULL,
 
148
    ff_parse_close,
258
149
};