00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027
00028 #include "avcodec.h"
00029 #include "mpegaudio.h"
00030 #include "mpegaudiodata.h"
00031 #include "mpegaudiodecheader.h"
00032
00033
00034 int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
00035 {
00036 int sample_rate, frame_size, mpeg25, padding;
00037 int sample_rate_index, bitrate_index;
00038 if (header & (1<<20)) {
00039 s->lsf = (header & (1<<19)) ? 0 : 1;
00040 mpeg25 = 0;
00041 } else {
00042 s->lsf = 1;
00043 mpeg25 = 1;
00044 }
00045
00046 s->layer = 4 - ((header >> 17) & 3);
00047
00048 sample_rate_index = (header >> 10) & 3;
00049 sample_rate = avpriv_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
00050 sample_rate_index += 3 * (s->lsf + mpeg25);
00051 s->sample_rate_index = sample_rate_index;
00052 s->error_protection = ((header >> 16) & 1) ^ 1;
00053 s->sample_rate = sample_rate;
00054
00055 bitrate_index = (header >> 12) & 0xf;
00056 padding = (header >> 9) & 1;
00057
00058 s->mode = (header >> 6) & 3;
00059 s->mode_ext = (header >> 4) & 3;
00060
00061
00062
00063
00064 if (s->mode == MPA_MONO)
00065 s->nb_channels = 1;
00066 else
00067 s->nb_channels = 2;
00068
00069 if (bitrate_index != 0) {
00070 frame_size = avpriv_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
00071 s->bit_rate = frame_size * 1000;
00072 switch(s->layer) {
00073 case 1:
00074 frame_size = (frame_size * 12000) / sample_rate;
00075 frame_size = (frame_size + padding) * 4;
00076 break;
00077 case 2:
00078 frame_size = (frame_size * 144000) / sample_rate;
00079 frame_size += padding;
00080 break;
00081 default:
00082 case 3:
00083 frame_size = (frame_size * 144000) / (sample_rate << s->lsf);
00084 frame_size += padding;
00085 break;
00086 }
00087 s->frame_size = frame_size;
00088 } else {
00089
00090 return 1;
00091 }
00092
00093 #if defined(DEBUG)
00094 av_dlog(NULL, "layer%d, %d Hz, %d kbits/s, ",
00095 s->layer, s->sample_rate, s->bit_rate);
00096 if (s->nb_channels == 2) {
00097 if (s->layer == 3) {
00098 if (s->mode_ext & MODE_EXT_MS_STEREO)
00099 av_dlog(NULL, "ms-");
00100 if (s->mode_ext & MODE_EXT_I_STEREO)
00101 av_dlog(NULL, "i-");
00102 }
00103 av_dlog(NULL, "stereo");
00104 } else {
00105 av_dlog(NULL, "mono");
00106 }
00107 av_dlog(NULL, "\n");
00108 #endif
00109 return 0;
00110 }
00111
00112 int avpriv_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
00113 {
00114 MPADecodeHeader s1, *s = &s1;
00115
00116 if (ff_mpa_check_header(head) != 0)
00117 return -1;
00118
00119 if (avpriv_mpegaudio_decode_header(s, head) != 0) {
00120 return -1;
00121 }
00122
00123 switch(s->layer) {
00124 case 1:
00125 avctx->codec_id = AV_CODEC_ID_MP1;
00126 *frame_size = 384;
00127 break;
00128 case 2:
00129 avctx->codec_id = AV_CODEC_ID_MP2;
00130 *frame_size = 1152;
00131 break;
00132 default:
00133 case 3:
00134 avctx->codec_id = AV_CODEC_ID_MP3;
00135 if (s->lsf)
00136 *frame_size = 576;
00137 else
00138 *frame_size = 1152;
00139 break;
00140 }
00141
00142 *sample_rate = s->sample_rate;
00143 *channels = s->nb_channels;
00144 *bit_rate = s->bit_rate;
00145 return s->frame_size;
00146 }