📄 mpegaudiodec.c
字号:
// TODO// Implement de-emphasis, maybe.// First few milliseconds of MPEG 2.5 files sometimes have a glitch./* * MPEG Audio decoder * Copyright (c) 2001, 2002 Fabrice Bellard. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * *//** * @file mpegaudiodec.c * MPEG Audio decoder. * from FFMPEG_VERSION "0.4.8" */#include <stdio.h>#include <string.h>#include "mpegaudiodec.h"#ifdef USE_MAI /* for compatibility with standalone */ #include "mai_osal.h" //debug messages#endif#define DPRINTF(_args_) /* MAIOSDebugPrint _args_ */#define INFO_PRINTF(_args_) /* MAIOSDebugPrint _args_ */#define HEADER_PRINTF(_args_) /* MAIOSDebugPrint _args_ */#ifdef DEBUG #undef DEBUG#endif//#define DEBUGFILE *mpgaudofp = NULL;void debug_dump(int *stuff, int length, char *comment){ int ii; if (mpgaudofp == NULL) mpgaudofp = fopen("dump_mp3.txt", "w"); fprintf(mpgaudofp, comment); if (mpgaudofp != NULL) { for (ii = 0; ii < length; ii++) fprintf(mpgaudofp, "%13d ", stuff[ii]); fprintf(mpgaudofp, "\n"); }}void c_memcpy(unsigned char *out, unsigned char *in, unsigned int len){ unsigned int ii; for (ii = 0; ii < len; ii++) out[ii] = in[ii];}void i_memcpy(int *out, int *in, unsigned int len){ unsigned int ii; for (ii = 0; ii < len; ii++) out[ii] = in[ii];}int frame_number;MPEG_codec mpeg_decoder;/* fast header check for resync */static int check_header(uint32_t header){ /* header */ if ((header & 0xffe00000) != 0xffe00000) return -1; /* layer check */ if (((header >> 17)& 3) == 0) return -1; /* bit rate */ if (((header >> 12)& 0xf) == 0xf) return -1; /* frequency */ if (((header >> 10)& 3) == 3) return -1; return 0;}/* header + layer + bitrate + freq + lsf/mpeg25 */#define SAME_HEADER_MASK (0xffe00000 | (3 << 17) | (0xf << 12) | (3 << 10) | (3 << 19))/* header decoding. MUST check the header before because no consistency check is done there. Return 1 if free format found and that the frame size must be computed externally */static int decode_header(MPADecodeContext *s, uint32_t header){ int sample_rate, frame_size, padding; int sample_rate_index, bitrate_index; static int old_bitrate_index, old_mpeg25; if (header & (1 << 20)) { s->lsf = (header & (1 << 19)) ? 0 : 1; s->mpeg25 = 0; } else { s->lsf = 1; s->mpeg25 = 1; } s->layer = 4 - ((header >> 17)& 3); /* extract frequency */ sample_rate_index = (header >> 10)& 3; sample_rate = mpa_freq_tab[sample_rate_index] >> (s->lsf + s->mpeg25); sample_rate_index += 3 *(s->lsf + s->mpeg25); s->sample_rate_index = sample_rate_index; s->error_protection = ((header >> 16)& 1) ^ 1; s->sample_rate = sample_rate; bitrate_index = (header >> 12)& 0xf; padding = (header >> 9)& 1; //extension = (header >> 8) & 1; s->mode = (header >> 6) & 3; s->mode_ext = (header >> 4) & 3; //copyright = (header >> 3) & 1; //original = (header >> 2) & 1; s->emphasis = header & 3; if (s->mode == MPA_MONO) s->nb_channels = 1; else s->nb_channels = 2; if (bitrate_index != 0) { frame_size = mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index]; dprintf("FS %x p%x %x, ", frame_size, padding, header); s->bit_rate = frame_size *1000; switch (s->layer) { case 1: frame_size = (frame_size *12000) / sample_rate; frame_size = (frame_size + padding) *4; break; case 2: frame_size = (frame_size *144000) / sample_rate; frame_size += padding; break; default: case 3: frame_size = (frame_size *144000) / (sample_rate << s->lsf); frame_size += padding; break; } s->frame_size = frame_size; dprintf("now %x, ", frame_size); } else { /* if no frame size computed, signal it */ if (!s->free_format_frame_size) return 1; /* free format: compute bitrate and real frame size from the frame size we extracted by reading the bitstream */ s->frame_size = s->free_format_frame_size; switch (s->layer) { case 1: s->frame_size += padding *4; s->bit_rate = (s->frame_size *sample_rate) / 48000; break; case 2: s->frame_size += padding; s->bit_rate = (s->frame_size *sample_rate) / 144000; break; default: case 3: s->frame_size += padding; s->bit_rate = (s->frame_size *(sample_rate << s->lsf)) / 144000; break; } dprintf("free format ride %x, ", s->frame_size); } { static int changed = 1; if (old_mpeg25 != s->mpeg25) changed = 1; if (old_bitrate_index != bitrate_index) { if (s->bit_rate_counter++ > 10) s->vbr = 1; else s->vbr = 0; changed = 1; } if (s->frame_size) s->keep_frame_size = s->frame_size;#if defined(DEBUG) if ((frame_number & 0x1ff) == 0x1ff) changed = 1; if (changed) { printf("frame %x, ", frame_number); if (s->emphasis) printf("emphasis %x ", s->emphasis); if (s->nb_channels == 2) { if (s->layer == 3) { if (s->mode_ext & MODE_EXT_MS_STEREO) printf("ms-"); if (s->mode_ext & MODE_EXT_I_STEREO) printf("i-"); } printf("stereo "); } else { printf("mono "); } printf("layer%d, %d hz, %d bits/sec, framesize %d, ", s->layer, s->sample_rate, s->bit_rate, s->frame_size); if (s->mpeg25) printf("MPEG 2.5\n"); else printf("MPEG %x\n", s->lsf + 1); changed = 0; } old_mpeg25 = s->mpeg25;#endif old_bitrate_index = bitrate_index; }#if defined(DEBUG) printf("layer%d, %d Hz, %d bits/sec, ", s->layer, s->sample_rate, s->bit_rate); if (s->nb_channels == 2) { if (s->layer == 3) { if (s->mode_ext & MODE_EXT_MS_STEREO) printf("ms-"); if (s->mode_ext & MODE_EXT_I_STEREO) printf("i-"); } printf("stereo"); } else { printf("mono"); } printf("\n");#endif return 0;} /* return the number of decoded frames */static int mp_decode_layer1(MPADecodeContext *s){ int bound, i, v, n, ch, j, mant; uint8_t allocation[MPA_MAX_CHANNELS][SBLIMIT]; uint8_t scale_factors[MPA_MAX_CHANNELS][SBLIMIT]; if (s->mode == MPA_JSTEREO) bound = (s->mode_ext + 1) *4; else bound = SBLIMIT; /* allocation bits */ for (i = 0; i < bound; i++) { for (ch = 0; ch < s->nb_channels; ch++) { allocation[ch][i] = get_bits(&s->gb, 4); } } for (i = bound; i < SBLIMIT; i++) { allocation[0][i] = get_bits(&s->gb, 4); } /* scale factors */ for (i = 0; i < bound; i++) { for (ch = 0; ch < s->nb_channels; ch++) { if (allocation[ch][i]) scale_factors[ch][i] = get_bits(&s->gb, 6); } } for (i = bound; i < SBLIMIT; i++) { if (allocation[0][i]) { scale_factors[0][i] = get_bits(&s->gb, 6); scale_factors[1][i] = get_bits(&s->gb, 6); } } /* compute samples */ for (j = 0; j < 12; j++) { for (i = 0; i < bound; i++) { for (ch = 0; ch < s->nb_channels; ch++) { n = allocation[ch][i]; if (n) { mant = get_bits(&s->gb, n + 1); v = l1_unscale(n, mant, scale_factors[ch][i]); } else { v = 0; } s->sb_samples[ch][j][i] = v; } } for (i = bound; i < SBLIMIT; i++) { n = allocation[0][i]; if (n) { mant = get_bits(&s->gb, n + 1); v = l1_unscale(n, mant, scale_factors[0][i]); s->sb_samples[0][j][i] = v; v = l1_unscale(n, mant, scale_factors[1][i]); s->sb_samples[1][j][i] = v; } else { s->sb_samples[0][j][i] = 0; s->sb_samples[1][j][i] = 0; } } } return 12;}/* bitrate is in kb/s */int l2_select_table(int bitrate, int nb_channels, int freq, int lsf){ int ch_bitrate, table; ch_bitrate = bitrate / nb_channels; if (!lsf) { if ((freq == 48000 && ch_bitrate >= 56) || (ch_bitrate >= 56 && ch_bitrate <= 80)) table = 0; else if (freq != 48000 && ch_bitrate >= 96) table = 1; else if (freq != 32000 && ch_bitrate <= 48) table = 2; else table = 3; } else { table = 4; } return table;}static int mp_decode_layer2(MPADecodeContext *s){ int sblimit; /* number of used subbands */ const unsigned char *alloc_table; int table, bit_alloc_bits, i, j, ch, bound, v; unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT]; unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]; unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3], *sf; int scale, qindex, bits, steps, k, l, m, b; /* select decoding table */ table = l2_select_table(s->bit_rate / 1000, s->nb_channels, s->sample_rate, s->lsf); sblimit = sblimit_table[table]; alloc_table = alloc_tables[table]; if (s->mode == MPA_JSTEREO) bound = (s->mode_ext + 1) *4; else bound = sblimit; dprintf("bound=%d sblimit=%d\n", bound, sblimit); /* parse bit allocation */ j = 0; for (i = 0; i < bound; i++) { bit_alloc_bits = alloc_table[j]; for (ch = 0; ch < s->nb_channels; ch++) { bit_alloc[ch][i] = get_bits(&s->gb, bit_alloc_bits); } j += 1 << bit_alloc_bits; } for (i = bound; i < sblimit; i++) { bit_alloc_bits = alloc_table[j]; v = get_bits(&s->gb, bit_alloc_bits); bit_alloc[0][i] = v; bit_alloc[1][i] = v; j += 1 << bit_alloc_bits; }#ifdef DEBUG { for (ch = 0; ch < s->nb_channels; ch++) { for (i = 0; i < sblimit; i++) printf(" %d", bit_alloc[ch][i]); printf("\n"); } }#endif /* scale codes */ for (i = 0; i < sblimit; i++) { for (ch = 0; ch < s->nb_channels; ch++) { if (bit_alloc[ch][i]) scale_code[ch][i] = get_bits(&s->gb, 2); } } /* scale factors */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -