Revision 4bd8e17c

View differences:

configure
726 726
gxf_muxer_deps="gpl"
727 727
libnut_demuxer_deps="libnut"
728 728
libnut_muxer_deps="libnut"
729
mp3_demuxer_deps="mpegaudio_parser"
729 730
ogg_muxer_deps="libogg"
730 731
redir_demuxer_deps="network"
731 732
rtp_muxer_deps="network mpegts_demuxer"
libavcodec/Makefile
19 19
      motion_est.o \
20 20
      imgconvert.o \
21 21
      mpeg12.o \
22
      mpegaudiodec.o mpegaudiodata.o \
22
      mpegaudiodec.o mpegaudiodecheader.o mpegaudiodata.o \
23 23
      simple_idct.o \
24 24
      ratecontrol.o \
25 25
      eval.o \
......
300 300
OBJS-$(CONFIG_H264_PARSER)             += h264_parser.o
301 301
OBJS-$(CONFIG_MJPEG_PARSER)            += mjpeg_parser.o
302 302
OBJS-$(CONFIG_MPEG4VIDEO_PARSER)       += mpeg4video_parser.o
303
OBJS-$(CONFIG_MPEGAUDIO_PARSER)        += mpegaudio_parser.o
303
OBJS-$(CONFIG_MPEGAUDIO_PARSER)        += mpegaudio_parser.o mpegaudiodecheader.o
304 304
OBJS-$(CONFIG_MPEGVIDEO_PARSER)        += mpegvideo_parser.o
305 305
OBJS-$(CONFIG_PNM_PARSER)              += pnm_parser.o pnm.o
306 306
OBJS-$(CONFIG_VC1_PARSER)              += vc1_parser.o
libavcodec/mpegaudio.h
23 23
 * mpeg audio declarations for both encoder and decoder.
24 24
 */
25 25

  
26
#ifndef MPEGAUDIO_H
27
#define MPEGAUDIO_H
28

  
29
#include "bitstream.h"
30
#include "dsputil.h"
31

  
26 32
/* max frame size, in samples */
27 33
#define MPA_FRAME_SIZE 1152
28 34

  
......
73 79
typedef int32_t MPA_INT;
74 80
#endif
75 81

  
82
#define BACKSTEP_SIZE 512
83
#define EXTRABYTES 24
84

  
85
struct GranuleDef;
86

  
87
typedef struct MPADecodeContext {
88
    DECLARE_ALIGNED_8(uint8_t, last_buf[2*BACKSTEP_SIZE + EXTRABYTES]);
89
    int last_buf_size;
90
    int frame_size;
91
    /* next header (used in free format parsing) */
92
    uint32_t free_format_next_header;
93
    int error_protection;
94
    int layer;
95
    int sample_rate;
96
    int sample_rate_index; /* between 0 and 8 */
97
    int bit_rate;
98
    GetBitContext gb;
99
    GetBitContext in_gb;
100
    int nb_channels;
101
    int mode;
102
    int mode_ext;
103
    int lsf;
104
    DECLARE_ALIGNED_16(MPA_INT, synth_buf[MPA_MAX_CHANNELS][512 * 2]);
105
    int synth_buf_offset[MPA_MAX_CHANNELS];
106
    DECLARE_ALIGNED_16(int32_t, sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT]);
107
    int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
108
#ifdef DEBUG
109
    int frame_count;
110
#endif
111
    void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
112
    int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
113
    int dither_state;
114
    int error_resilience;
115
    AVCodecContext* avctx;
116
} MPADecodeContext;
117

  
76 118
int l2_select_table(int bitrate, int nb_channels, int freq, int lsf);
77 119
int mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate);
78 120
void ff_mpa_synth_init(MPA_INT *window);
......
97 139
        return -1;
98 140
    return 0;
99 141
}
142

  
143
#endif /* MPEGAUDIO_H */
libavcodec/mpegaudio_parser.c
22 22

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

  
26 27

  
27 28
typedef struct MpegAudioParseContext {
......
41 42
#define SAME_HEADER_MASK \
42 43
   (0xffe00000 | (3 << 17) | (3 << 10) | (3 << 19))
43 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

  
44 82
static int mpegaudio_parse_init(AVCodecParserContext *s1)
45 83
{
46 84
    MpegAudioParseContext *s = s1->priv_data;
libavcodec/mpegaudiodec.c
42 42
#endif
43 43

  
44 44
#include "mpegaudio.h"
45
#include "mpegaudiodecheader.h"
45 46

  
46 47
#include "mathops.h"
47 48

  
......
57 58
/****************/
58 59

  
59 60
#define HEADER_SIZE 4
60
#define BACKSTEP_SIZE 512
61
#define EXTRABYTES 24
62

  
63
struct GranuleDef;
64

  
65
typedef struct MPADecodeContext {
66
    DECLARE_ALIGNED_8(uint8_t, last_buf[2*BACKSTEP_SIZE + EXTRABYTES]);
67
    int last_buf_size;
68
    int frame_size;
69
    /* next header (used in free format parsing) */
70
    uint32_t free_format_next_header;
71
    int error_protection;
72
    int layer;
73
    int sample_rate;
74
    int sample_rate_index; /* between 0 and 8 */
75
    int bit_rate;
76
    GetBitContext gb;
77
    GetBitContext in_gb;
78
    int nb_channels;
79
    int mode;
80
    int mode_ext;
81
    int lsf;
82
    DECLARE_ALIGNED_16(MPA_INT, synth_buf[MPA_MAX_CHANNELS][512 * 2]);
83
    int synth_buf_offset[MPA_MAX_CHANNELS];
84
    DECLARE_ALIGNED_16(int32_t, sb_samples[MPA_MAX_CHANNELS][36][SBLIMIT]);
85
    int32_t mdct_buf[MPA_MAX_CHANNELS][SBLIMIT * 18]; /* previous samples, for layer 3 MDCT */
86
#ifdef DEBUG
87
    int frame_count;
88
#endif
89
    void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g);
90
    int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3
91
    int dither_state;
92
    int error_resilience;
93
    AVCodecContext* avctx;
94
} MPADecodeContext;
95 61

  
96 62
/**
97 63
 * Context for MP3On4 decoder
......
1107 1073
    buf[8 - 4] = MULH(t0, win[18 + 8 - 4]);
1108 1074
}
1109 1075

  
1110
/* header decoding. MUST check the header before because no
1111
   consistency check is done there. Return 1 if free format found and
1112
   that the frame size must be computed externally */
1113
static int decode_header(MPADecodeContext *s, uint32_t header)
1114
{
1115
    int sample_rate, frame_size, mpeg25, padding;
1116
    int sample_rate_index, bitrate_index;
1117
    if (header & (1<<20)) {
1118
        s->lsf = (header & (1<<19)) ? 0 : 1;
1119
        mpeg25 = 0;
1120
    } else {
1121
        s->lsf = 1;
1122
        mpeg25 = 1;
1123
    }
1124

  
1125
    s->layer = 4 - ((header >> 17) & 3);
1126
    /* extract frequency */
1127
    sample_rate_index = (header >> 10) & 3;
1128
    sample_rate = ff_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
1129
    sample_rate_index += 3 * (s->lsf + mpeg25);
1130
    s->sample_rate_index = sample_rate_index;
1131
    s->error_protection = ((header >> 16) & 1) ^ 1;
1132
    s->sample_rate = sample_rate;
1133

  
1134
    bitrate_index = (header >> 12) & 0xf;
1135
    padding = (header >> 9) & 1;
1136
    //extension = (header >> 8) & 1;
1137
    s->mode = (header >> 6) & 3;
1138
    s->mode_ext = (header >> 4) & 3;
1139
    //copyright = (header >> 3) & 1;
1140
    //original = (header >> 2) & 1;
1141
    //emphasis = header & 3;
1142

  
1143
    if (s->mode == MPA_MONO)
1144
        s->nb_channels = 1;
1145
    else
1146
        s->nb_channels = 2;
1147

  
1148
    if (bitrate_index != 0) {
1149
        frame_size = ff_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
1150
        s->bit_rate = frame_size * 1000;
1151
        switch(s->layer) {
1152
        case 1:
1153
            frame_size = (frame_size * 12000) / sample_rate;
1154
            frame_size = (frame_size + padding) * 4;
1155
            break;
1156
        case 2:
1157
            frame_size = (frame_size * 144000) / sample_rate;
1158
            frame_size += padding;
1159
            break;
1160
        default:
1161
        case 3:
1162
            frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
1163
            frame_size += padding;
1164
            break;
1165
        }
1166
        s->frame_size = frame_size;
1167
    } else {
1168
        /* if no frame size computed, signal it */
1169
        return 1;
1170
    }
1171

  
1172
#if defined(DEBUG)
1173
    dprintf(s->avctx, "layer%d, %d Hz, %d kbits/s, ",
1174
           s->layer, s->sample_rate, s->bit_rate);
1175
    if (s->nb_channels == 2) {
1176
        if (s->layer == 3) {
1177
            if (s->mode_ext & MODE_EXT_MS_STEREO)
1178
                dprintf(s->avctx, "ms-");
1179
            if (s->mode_ext & MODE_EXT_I_STEREO)
1180
                dprintf(s->avctx, "i-");
1181
        }
1182
        dprintf(s->avctx, "stereo");
1183
    } else {
1184
        dprintf(s->avctx, "mono");
1185
    }
1186
    dprintf(s->avctx, "\n");
1187
#endif
1188
    return 0;
1189
}
1190

  
1191
/* useful helper to get mpeg audio stream infos. Return -1 if error in
1192
   header, otherwise the coded frame size in bytes */
1193
int mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate)
1194
{
1195
    MPADecodeContext s1, *s = &s1;
1196
    s1.avctx = avctx;
1197

  
1198
    if (ff_mpa_check_header(head) != 0)
1199
        return -1;
1200

  
1201
    if (decode_header(s, head) != 0) {
1202
        return -1;
1203
    }
1204

  
1205
    switch(s->layer) {
1206
    case 1:
1207
        avctx->frame_size = 384;
1208
        break;
1209
    case 2:
1210
        avctx->frame_size = 1152;
1211
        break;
1212
    default:
1213
    case 3:
1214
        if (s->lsf)
1215
            avctx->frame_size = 576;
1216
        else
1217
            avctx->frame_size = 1152;
1218
        break;
1219
    }
1220

  
1221
    *sample_rate = s->sample_rate;
1222
    avctx->channels = s->nb_channels;
1223
    avctx->bit_rate = s->bit_rate;
1224
    avctx->sub_id = s->layer;
1225
    return s->frame_size;
1226
}
1227

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

  
22
/**
23
 * @file mpegaudiodecheader.c
24
 * MPEG Audio header decoder.
25
 */
26

  
27
//#define DEBUG
28
#include "avcodec.h"
29
#include "mpegaudio.h"
30
#include "mpegaudiodata.h"
31

  
32

  
33
int decode_header(MPADecodeContext *s, uint32_t header)
34
{
35
    int sample_rate, frame_size, mpeg25, padding;
36
    int sample_rate_index, bitrate_index;
37
    if (header & (1<<20)) {
38
        s->lsf = (header & (1<<19)) ? 0 : 1;
39
        mpeg25 = 0;
40
    } else {
41
        s->lsf = 1;
42
        mpeg25 = 1;
43
    }
44

  
45
    s->layer = 4 - ((header >> 17) & 3);
46
    /* extract frequency */
47
    sample_rate_index = (header >> 10) & 3;
48
    sample_rate = ff_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
49
    sample_rate_index += 3 * (s->lsf + mpeg25);
50
    s->sample_rate_index = sample_rate_index;
51
    s->error_protection = ((header >> 16) & 1) ^ 1;
52
    s->sample_rate = sample_rate;
53

  
54
    bitrate_index = (header >> 12) & 0xf;
55
    padding = (header >> 9) & 1;
56
    //extension = (header >> 8) & 1;
57
    s->mode = (header >> 6) & 3;
58
    s->mode_ext = (header >> 4) & 3;
59
    //copyright = (header >> 3) & 1;
60
    //original = (header >> 2) & 1;
61
    //emphasis = header & 3;
62

  
63
    if (s->mode == MPA_MONO)
64
        s->nb_channels = 1;
65
    else
66
        s->nb_channels = 2;
67

  
68
    if (bitrate_index != 0) {
69
        frame_size = ff_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
70
        s->bit_rate = frame_size * 1000;
71
        switch(s->layer) {
72
        case 1:
73
            frame_size = (frame_size * 12000) / sample_rate;
74
            frame_size = (frame_size + padding) * 4;
75
            break;
76
        case 2:
77
            frame_size = (frame_size * 144000) / sample_rate;
78
            frame_size += padding;
79
            break;
80
        default:
81
        case 3:
82
            frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
83
            frame_size += padding;
84
            break;
85
        }
86
        s->frame_size = frame_size;
87
    } else {
88
        /* if no frame size computed, signal it */
89
        return 1;
90
    }
91

  
92
#if defined(DEBUG)
93
    dprintf(s->avctx, "layer%d, %d Hz, %d kbits/s, ",
94
           s->layer, s->sample_rate, s->bit_rate);
95
    if (s->nb_channels == 2) {
96
        if (s->layer == 3) {
97
            if (s->mode_ext & MODE_EXT_MS_STEREO)
98
                dprintf(s->avctx, "ms-");
99
            if (s->mode_ext & MODE_EXT_I_STEREO)
100
                dprintf(s->avctx, "i-");
101
        }
102
        dprintf(s->avctx, "stereo");
103
    } else {
104
        dprintf(s->avctx, "mono");
105
    }
106
    dprintf(s->avctx, "\n");
107
#endif
108
    return 0;
109
}
libavcodec/mpegaudiodecheader.h
1
/*
2
 * MPEG Audio header decoder
3
 * Copyright (c) 2001, 2002 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

  
22
/**
23
 * @file mpegaudiodecheader.c
24
 * MPEG Audio header decoder.
25
 */
26

  
27
#ifndef MPEGAUDIODECHEADER_H
28
#define MPEGAUDIODECHEADER_H
29

  
30
#include "common.h"
31
#include "mpegaudio.h"
32

  
33

  
34
/* header decoding. MUST check the header before because no
35
   consistency check is done there. Return 1 if free format found and
36
   that the frame size must be computed externally */
37
int decode_header(MPADecodeContext *s, uint32_t header);
38

  
39
#endif /* MPEGAUDIODECHEADER_H */

Also available in: Unified diff