2007-05-20 13:40:07 +00:00
|
|
|
/*
|
|
|
|
* MPEG Audio header decoder
|
2009-01-19 15:46:40 +00:00
|
|
|
* Copyright (c) 2001, 2002 Fabrice Bellard
|
2007-05-20 13:40:07 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2007-05-20 13:40:07 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2007-05-20 13:40:07 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2007-05-20 13:40:07 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2007-05-20 13:40:07 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2007-05-20 13:40:07 +00:00
|
|
|
* MPEG Audio header decoder.
|
|
|
|
*/
|
|
|
|
|
2014-06-22 17:11:32 +00:00
|
|
|
#include "libavutil/common.h"
|
|
|
|
|
2007-05-20 13:40:07 +00:00
|
|
|
#include "avcodec.h"
|
|
|
|
#include "mpegaudio.h"
|
|
|
|
#include "mpegaudiodata.h"
|
2010-02-28 03:27:10 +00:00
|
|
|
#include "mpegaudiodecheader.h"
|
2007-05-20 13:40:07 +00:00
|
|
|
|
|
|
|
|
2011-10-17 07:28:53 +00:00
|
|
|
int avpriv_mpegaudio_decode_header(MPADecodeHeader *s, uint32_t header)
|
2007-05-20 13:40:07 +00:00
|
|
|
{
|
|
|
|
int sample_rate, frame_size, mpeg25, padding;
|
|
|
|
int sample_rate_index, bitrate_index;
|
2015-10-09 13:16:46 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ff_mpa_check_header(header);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2007-05-20 13:40:07 +00:00
|
|
|
if (header & (1<<20)) {
|
|
|
|
s->lsf = (header & (1<<19)) ? 0 : 1;
|
|
|
|
mpeg25 = 0;
|
|
|
|
} else {
|
|
|
|
s->lsf = 1;
|
|
|
|
mpeg25 = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->layer = 4 - ((header >> 17) & 3);
|
|
|
|
/* extract frequency */
|
|
|
|
sample_rate_index = (header >> 10) & 3;
|
2014-06-22 17:11:32 +00:00
|
|
|
if (sample_rate_index >= FF_ARRAY_ELEMS(avpriv_mpa_freq_tab))
|
|
|
|
sample_rate_index = 0;
|
2011-10-17 07:28:53 +00:00
|
|
|
sample_rate = avpriv_mpa_freq_tab[sample_rate_index] >> (s->lsf + mpeg25);
|
2007-05-20 13:40:07 +00:00
|
|
|
sample_rate_index += 3 * (s->lsf + 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;
|
|
|
|
//emphasis = header & 3;
|
|
|
|
|
|
|
|
if (s->mode == MPA_MONO)
|
|
|
|
s->nb_channels = 1;
|
|
|
|
else
|
|
|
|
s->nb_channels = 2;
|
|
|
|
|
|
|
|
if (bitrate_index != 0) {
|
2011-10-17 07:28:53 +00:00
|
|
|
frame_size = avpriv_mpa_bitrate_tab[s->lsf][s->layer - 1][bitrate_index];
|
2007-05-20 13:40:07 +00:00
|
|
|
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;
|
|
|
|
} else {
|
|
|
|
/* if no frame size computed, signal it */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(DEBUG)
|
2015-03-16 08:57:36 +00:00
|
|
|
ff_dlog(NULL, "layer%d, %d Hz, %d kbits/s, ",
|
2007-05-20 13:40:07 +00:00
|
|
|
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)
|
2015-03-16 08:57:36 +00:00
|
|
|
ff_dlog(NULL, "ms-");
|
2007-05-20 13:40:07 +00:00
|
|
|
if (s->mode_ext & MODE_EXT_I_STEREO)
|
2015-03-16 08:57:36 +00:00
|
|
|
ff_dlog(NULL, "i-");
|
2007-05-20 13:40:07 +00:00
|
|
|
}
|
2015-03-16 08:57:36 +00:00
|
|
|
ff_dlog(NULL, "stereo");
|
2007-05-20 13:40:07 +00:00
|
|
|
} else {
|
2015-03-16 08:57:36 +00:00
|
|
|
ff_dlog(NULL, "mono");
|
2007-05-20 13:40:07 +00:00
|
|
|
}
|
2015-03-16 08:57:36 +00:00
|
|
|
ff_dlog(NULL, "\n");
|
2007-05-20 13:40:07 +00:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2011-05-19 12:44:11 +00:00
|
|
|
|
2011-10-17 07:28:53 +00:00
|
|
|
int avpriv_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
|
2011-05-19 12:44:11 +00:00
|
|
|
{
|
|
|
|
MPADecodeHeader s1, *s = &s1;
|
|
|
|
|
2011-10-17 07:28:53 +00:00
|
|
|
if (avpriv_mpegaudio_decode_header(s, head) != 0) {
|
2011-05-19 12:44:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(s->layer) {
|
|
|
|
case 1:
|
2012-08-05 09:11:04 +00:00
|
|
|
avctx->codec_id = AV_CODEC_ID_MP1;
|
2011-05-19 12:44:11 +00:00
|
|
|
*frame_size = 384;
|
|
|
|
break;
|
|
|
|
case 2:
|
2012-08-05 09:11:04 +00:00
|
|
|
avctx->codec_id = AV_CODEC_ID_MP2;
|
2011-05-19 12:44:11 +00:00
|
|
|
*frame_size = 1152;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case 3:
|
2015-02-13 21:51:33 +00:00
|
|
|
if (avctx->codec_id != AV_CODEC_ID_MP3ADU)
|
|
|
|
avctx->codec_id = AV_CODEC_ID_MP3;
|
2011-05-19 12:44:11 +00:00
|
|
|
if (s->lsf)
|
|
|
|
*frame_size = 576;
|
|
|
|
else
|
|
|
|
*frame_size = 1152;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*sample_rate = s->sample_rate;
|
|
|
|
*channels = s->nb_channels;
|
|
|
|
*bit_rate = s->bit_rate;
|
|
|
|
return s->frame_size;
|
|
|
|
}
|