Revision 4bd8e17c libavcodec/mpegaudiodec.c

View differences:

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
{

Also available in: Unified diff