decoder/faad: make variables more local
This commit is contained in:
parent
ef9ef03b1f
commit
6f1b4292f0
@ -64,16 +64,13 @@ adts_check_frame(const unsigned char *data)
|
||||
static size_t
|
||||
adts_find_frame(DecoderBuffer *buffer)
|
||||
{
|
||||
size_t length, frame_length;
|
||||
bool ret;
|
||||
|
||||
while (true) {
|
||||
size_t length;
|
||||
const uint8_t *data = (const uint8_t *)
|
||||
decoder_buffer_read(buffer, &length);
|
||||
if (data == nullptr || length < 8) {
|
||||
/* not enough data yet */
|
||||
ret = decoder_buffer_fill(buffer);
|
||||
if (!ret)
|
||||
if (!decoder_buffer_fill(buffer))
|
||||
/* failed */
|
||||
return 0;
|
||||
|
||||
@ -95,7 +92,7 @@ adts_find_frame(DecoderBuffer *buffer)
|
||||
}
|
||||
|
||||
/* is it a frame? */
|
||||
frame_length = adts_check_frame(data);
|
||||
const size_t frame_length = adts_check_frame(data);
|
||||
if (frame_length == 0) {
|
||||
/* it's just some random 0xff byte; discard it
|
||||
and continue searching */
|
||||
@ -107,8 +104,7 @@ adts_find_frame(DecoderBuffer *buffer)
|
||||
/* available buffer size is smaller than the
|
||||
frame will be - attempt to read more
|
||||
data */
|
||||
ret = decoder_buffer_fill(buffer);
|
||||
if (!ret) {
|
||||
if (!decoder_buffer_fill(buffer)) {
|
||||
/* not enough data; discard this frame
|
||||
to prevent a possible buffer
|
||||
overflow */
|
||||
@ -129,13 +125,12 @@ adts_find_frame(DecoderBuffer *buffer)
|
||||
static float
|
||||
adts_song_duration(DecoderBuffer *buffer)
|
||||
{
|
||||
unsigned int frames, frame_length;
|
||||
unsigned sample_rate = 0;
|
||||
float frames_per_second;
|
||||
|
||||
/* Read all frames to ensure correct time and bitrate */
|
||||
unsigned frames;
|
||||
for (frames = 0;; frames++) {
|
||||
frame_length = adts_find_frame(buffer);
|
||||
const unsigned frame_length = adts_find_frame(buffer);
|
||||
if (frame_length == 0)
|
||||
break;
|
||||
|
||||
@ -153,7 +148,7 @@ adts_song_duration(DecoderBuffer *buffer)
|
||||
decoder_buffer_consume(buffer, frame_length);
|
||||
}
|
||||
|
||||
frames_per_second = (float)sample_rate / 1024.0;
|
||||
float frames_per_second = (float)sample_rate / 1024.0;
|
||||
if (frames_per_second <= 0)
|
||||
return -1;
|
||||
|
||||
@ -163,21 +158,17 @@ adts_song_duration(DecoderBuffer *buffer)
|
||||
static float
|
||||
faad_song_duration(DecoderBuffer *buffer, InputStream &is)
|
||||
{
|
||||
size_t fileread;
|
||||
size_t tagsize;
|
||||
size_t length;
|
||||
bool success;
|
||||
|
||||
const auto size = is.GetSize();
|
||||
fileread = size >= 0 ? size : 0;
|
||||
const size_t fileread = size >= 0 ? size : 0;
|
||||
|
||||
decoder_buffer_fill(buffer);
|
||||
size_t length;
|
||||
const uint8_t *data = (const uint8_t *)
|
||||
decoder_buffer_read(buffer, &length);
|
||||
if (data == nullptr)
|
||||
return -1;
|
||||
|
||||
tagsize = 0;
|
||||
size_t tagsize = 0;
|
||||
if (length >= 10 && !memcmp(data, "ID3", 3)) {
|
||||
/* skip the ID3 tag */
|
||||
|
||||
@ -186,7 +177,7 @@ faad_song_duration(DecoderBuffer *buffer, InputStream &is)
|
||||
|
||||
tagsize += 10;
|
||||
|
||||
success = decoder_buffer_skip(buffer, tagsize) &&
|
||||
const bool success = decoder_buffer_skip(buffer, tagsize) &&
|
||||
decoder_buffer_fill(buffer);
|
||||
if (!success)
|
||||
return -1;
|
||||
@ -211,7 +202,6 @@ faad_song_duration(DecoderBuffer *buffer, InputStream &is)
|
||||
return song_length;
|
||||
} else if (length >= 5 && memcmp(data, "ADIF", 4) == 0) {
|
||||
/* obtain the duration from the ADIF header */
|
||||
unsigned bit_rate;
|
||||
size_t skip_size = (data[4] & 0x80) ? 9 : 0;
|
||||
|
||||
if (8 + skip_size > length)
|
||||
@ -219,7 +209,7 @@ faad_song_duration(DecoderBuffer *buffer, InputStream &is)
|
||||
header */
|
||||
return -1;
|
||||
|
||||
bit_rate = ((data[4 + skip_size] & 0x0F) << 19) |
|
||||
unsigned bit_rate = ((data[4 + skip_size] & 0x0F) << 19) |
|
||||
(data[5 + skip_size] << 11) |
|
||||
(data[6 + skip_size] << 3) |
|
||||
(data[7 + skip_size] & 0xE0);
|
||||
@ -240,17 +230,6 @@ static bool
|
||||
faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer *buffer,
|
||||
AudioFormat &audio_format, Error &error)
|
||||
{
|
||||
int32_t nbytes;
|
||||
uint32_t sample_rate;
|
||||
uint8_t channels;
|
||||
#ifdef HAVE_FAAD_LONG
|
||||
/* neaacdec.h declares all arguments as "unsigned long", but
|
||||
internally expects uint32_t pointers. To avoid gcc
|
||||
warnings, use this workaround. */
|
||||
unsigned long *sample_rate_p = (unsigned long *)(void *)&sample_rate;
|
||||
#else
|
||||
uint32_t *sample_rate_p = &sample_rate;
|
||||
#endif
|
||||
|
||||
size_t length;
|
||||
const unsigned char *data = (const unsigned char *)
|
||||
@ -260,11 +239,21 @@ faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer *buffer,
|
||||
return false;
|
||||
}
|
||||
|
||||
nbytes = NeAACDecInit(decoder,
|
||||
/* deconst hack, libfaad requires this */
|
||||
const_cast<unsigned char *>(data),
|
||||
length,
|
||||
sample_rate_p, &channels);
|
||||
uint8_t channels;
|
||||
uint32_t sample_rate;
|
||||
#ifdef HAVE_FAAD_LONG
|
||||
/* neaacdec.h declares all arguments as "unsigned long", but
|
||||
internally expects uint32_t pointers. To avoid gcc
|
||||
warnings, use this workaround. */
|
||||
unsigned long *sample_rate_p = (unsigned long *)(void *)&sample_rate;
|
||||
#else
|
||||
uint32_t *sample_rate_p = &sample_rate;
|
||||
#endif
|
||||
long nbytes = NeAACDecInit(decoder,
|
||||
/* deconst hack, libfaad requires this */
|
||||
const_cast<unsigned char *>(data),
|
||||
length,
|
||||
sample_rate_p, &channels);
|
||||
if (nbytes < 0) {
|
||||
error.Set(faad_decoder_domain, "Not an AAC stream");
|
||||
return false;
|
||||
@ -304,15 +293,13 @@ faad_decoder_decode(NeAACDecHandle decoder, DecoderBuffer *buffer,
|
||||
static float
|
||||
faad_get_file_time_float(InputStream &is)
|
||||
{
|
||||
DecoderBuffer *buffer;
|
||||
float length;
|
||||
DecoderBuffer *const buffer =
|
||||
decoder_buffer_new(nullptr, is,
|
||||
FAAD_MIN_STREAMSIZE * MAX_CHANNELS);
|
||||
|
||||
buffer = decoder_buffer_new(nullptr, is,
|
||||
FAAD_MIN_STREAMSIZE * MAX_CHANNELS);
|
||||
length = faad_song_duration(buffer, is);
|
||||
float length = faad_song_duration(buffer, is);
|
||||
|
||||
if (length < 0) {
|
||||
bool ret;
|
||||
AudioFormat audio_format;
|
||||
|
||||
NeAACDecHandle decoder = NeAACDecOpen();
|
||||
@ -324,9 +311,8 @@ faad_get_file_time_float(InputStream &is)
|
||||
|
||||
decoder_buffer_fill(buffer);
|
||||
|
||||
ret = faad_decoder_init(decoder, buffer, audio_format,
|
||||
IgnoreError());
|
||||
if (ret)
|
||||
if (faad_decoder_init(decoder, buffer, audio_format,
|
||||
IgnoreError()))
|
||||
length = 0;
|
||||
|
||||
NeAACDecClose(decoder);
|
||||
@ -345,31 +331,25 @@ faad_get_file_time_float(InputStream &is)
|
||||
static int
|
||||
faad_get_file_time(InputStream &is)
|
||||
{
|
||||
int file_time = -1;
|
||||
float length;
|
||||
float length = faad_get_file_time_float(is);
|
||||
if (length < 0)
|
||||
return -1;
|
||||
|
||||
if ((length = faad_get_file_time_float(is)) >= 0)
|
||||
file_time = length + 0.5;
|
||||
|
||||
return file_time;
|
||||
return int(length + 0.5);
|
||||
}
|
||||
|
||||
static void
|
||||
faad_stream_decode(Decoder &mpd_decoder, InputStream &is)
|
||||
{
|
||||
float total_time = 0;
|
||||
AudioFormat audio_format;
|
||||
bool ret;
|
||||
uint16_t bit_rate = 0;
|
||||
DecoderBuffer *buffer;
|
||||
DecoderBuffer *const buffer =
|
||||
decoder_buffer_new(&mpd_decoder, is,
|
||||
FAAD_MIN_STREAMSIZE * MAX_CHANNELS);
|
||||
|
||||
buffer = decoder_buffer_new(&mpd_decoder, is,
|
||||
FAAD_MIN_STREAMSIZE * MAX_CHANNELS);
|
||||
total_time = faad_song_duration(buffer, is);
|
||||
const float total_time = faad_song_duration(buffer, is);
|
||||
|
||||
/* create the libfaad decoder */
|
||||
|
||||
NeAACDecHandle decoder = NeAACDecOpen();
|
||||
const NeAACDecHandle decoder = NeAACDecOpen();
|
||||
|
||||
NeAACDecConfigurationPtr config =
|
||||
NeAACDecGetCurrentConfiguration(decoder);
|
||||
@ -387,8 +367,8 @@ faad_stream_decode(Decoder &mpd_decoder, InputStream &is)
|
||||
/* initialize it */
|
||||
|
||||
Error error;
|
||||
ret = faad_decoder_init(decoder, buffer, audio_format, error);
|
||||
if (!ret) {
|
||||
AudioFormat audio_format;
|
||||
if (!faad_decoder_init(decoder, buffer, audio_format, error)) {
|
||||
LogError(error);
|
||||
NeAACDecClose(decoder);
|
||||
decoder_buffer_free(buffer);
|
||||
@ -402,21 +382,20 @@ faad_stream_decode(Decoder &mpd_decoder, InputStream &is)
|
||||
/* the decoder loop */
|
||||
|
||||
DecoderCommand cmd;
|
||||
uint16_t bit_rate = 0;
|
||||
do {
|
||||
size_t frame_size;
|
||||
const void *decoded;
|
||||
NeAACDecFrameInfo frame_info;
|
||||
|
||||
/* find the next frame */
|
||||
|
||||
frame_size = adts_find_frame(buffer);
|
||||
const size_t frame_size = adts_find_frame(buffer);
|
||||
if (frame_size == 0)
|
||||
/* end of file */
|
||||
break;
|
||||
|
||||
/* decode it */
|
||||
|
||||
decoded = faad_decoder_decode(decoder, buffer, &frame_info);
|
||||
NeAACDecFrameInfo frame_info;
|
||||
const void *const decoded =
|
||||
faad_decoder_decode(decoder, buffer, &frame_info);
|
||||
|
||||
if (frame_info.error > 0) {
|
||||
FormatWarning(faad_decoder_domain,
|
||||
@ -468,7 +447,6 @@ faad_scan_stream(InputStream &is,
|
||||
const struct tag_handler *handler, void *handler_ctx)
|
||||
{
|
||||
int file_time = faad_get_file_time(is);
|
||||
|
||||
if (file_time < 0)
|
||||
return false;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user