yading@10
|
1 /*
|
yading@10
|
2 * MPEG Audio parser
|
yading@10
|
3 * Copyright (c) 2003 Fabrice Bellard
|
yading@10
|
4 * Copyright (c) 2003 Michael Niedermayer
|
yading@10
|
5 *
|
yading@10
|
6 * This file is part of FFmpeg.
|
yading@10
|
7 *
|
yading@10
|
8 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
9 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
10 * License as published by the Free Software Foundation; either
|
yading@10
|
11 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
12 *
|
yading@10
|
13 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
16 * Lesser General Public License for more details.
|
yading@10
|
17 *
|
yading@10
|
18 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
19 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
21 */
|
yading@10
|
22
|
yading@10
|
23 #include "parser.h"
|
yading@10
|
24 #include "mpegaudiodecheader.h"
|
yading@10
|
25 #include "libavutil/common.h"
|
yading@10
|
26
|
yading@10
|
27
|
yading@10
|
28 typedef struct MpegAudioParseContext {
|
yading@10
|
29 ParseContext pc;
|
yading@10
|
30 int frame_size;
|
yading@10
|
31 uint32_t header;
|
yading@10
|
32 int header_count;
|
yading@10
|
33 int no_bitrate;
|
yading@10
|
34 } MpegAudioParseContext;
|
yading@10
|
35
|
yading@10
|
36 #define MPA_HEADER_SIZE 4
|
yading@10
|
37
|
yading@10
|
38 /* header + layer + bitrate + freq + lsf/mpeg25 */
|
yading@10
|
39 #define SAME_HEADER_MASK \
|
yading@10
|
40 (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
|
yading@10
|
41
|
yading@10
|
42 static int mpegaudio_parse(AVCodecParserContext *s1,
|
yading@10
|
43 AVCodecContext *avctx,
|
yading@10
|
44 const uint8_t **poutbuf, int *poutbuf_size,
|
yading@10
|
45 const uint8_t *buf, int buf_size)
|
yading@10
|
46 {
|
yading@10
|
47 MpegAudioParseContext *s = s1->priv_data;
|
yading@10
|
48 ParseContext *pc = &s->pc;
|
yading@10
|
49 uint32_t state= pc->state;
|
yading@10
|
50 int i;
|
yading@10
|
51 int next= END_NOT_FOUND;
|
yading@10
|
52
|
yading@10
|
53 for(i=0; i<buf_size; ){
|
yading@10
|
54 if(s->frame_size){
|
yading@10
|
55 int inc= FFMIN(buf_size - i, s->frame_size);
|
yading@10
|
56 i += inc;
|
yading@10
|
57 s->frame_size -= inc;
|
yading@10
|
58 state = 0;
|
yading@10
|
59
|
yading@10
|
60 if(!s->frame_size){
|
yading@10
|
61 next= i;
|
yading@10
|
62 break;
|
yading@10
|
63 }
|
yading@10
|
64 }else{
|
yading@10
|
65 while(i<buf_size){
|
yading@10
|
66 int ret, sr, channels, bit_rate, frame_size;
|
yading@10
|
67
|
yading@10
|
68 state= (state<<8) + buf[i++];
|
yading@10
|
69
|
yading@10
|
70 ret = avpriv_mpa_decode_header(avctx, state, &sr, &channels, &frame_size, &bit_rate);
|
yading@10
|
71 if (ret < 4) {
|
yading@10
|
72 if (i > 4)
|
yading@10
|
73 s->header_count = -2;
|
yading@10
|
74 } else {
|
yading@10
|
75 if((state&SAME_HEADER_MASK) != (s->header&SAME_HEADER_MASK) && s->header)
|
yading@10
|
76 s->header_count= -3;
|
yading@10
|
77 s->header= state;
|
yading@10
|
78 s->header_count++;
|
yading@10
|
79 s->frame_size = ret-4;
|
yading@10
|
80
|
yading@10
|
81 if (s->header_count > 0) {
|
yading@10
|
82 avctx->sample_rate= sr;
|
yading@10
|
83 avctx->channels = channels;
|
yading@10
|
84 s1->duration = frame_size;
|
yading@10
|
85 if (s->no_bitrate || !avctx->bit_rate) {
|
yading@10
|
86 s->no_bitrate = 1;
|
yading@10
|
87 avctx->bit_rate += (bit_rate - avctx->bit_rate) / s->header_count;
|
yading@10
|
88 }
|
yading@10
|
89 }
|
yading@10
|
90 break;
|
yading@10
|
91 }
|
yading@10
|
92 }
|
yading@10
|
93 }
|
yading@10
|
94 }
|
yading@10
|
95
|
yading@10
|
96 pc->state= state;
|
yading@10
|
97 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
|
yading@10
|
98 *poutbuf = NULL;
|
yading@10
|
99 *poutbuf_size = 0;
|
yading@10
|
100 return buf_size;
|
yading@10
|
101 }
|
yading@10
|
102
|
yading@10
|
103 *poutbuf = buf;
|
yading@10
|
104 *poutbuf_size = buf_size;
|
yading@10
|
105 return next;
|
yading@10
|
106 }
|
yading@10
|
107
|
yading@10
|
108
|
yading@10
|
109 AVCodecParser ff_mpegaudio_parser = {
|
yading@10
|
110 .codec_ids = { AV_CODEC_ID_MP1, AV_CODEC_ID_MP2, AV_CODEC_ID_MP3 },
|
yading@10
|
111 .priv_data_size = sizeof(MpegAudioParseContext),
|
yading@10
|
112 .parser_parse = mpegaudio_parse,
|
yading@10
|
113 .parser_close = ff_parse_close,
|
yading@10
|
114 };
|