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