Commit a9e849ff authored by Max Kellermann's avatar Max Kellermann

DecoderBuffer: _read() returns ConstBuffer object

parent 4c95a4d7
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "config.h" #include "config.h"
#include "DecoderBuffer.hxx" #include "DecoderBuffer.hxx"
#include "DecoderAPI.hxx" #include "DecoderAPI.hxx"
#include "util/ConstBuffer.hxx"
#include <glib.h> #include <glib.h>
...@@ -124,15 +125,13 @@ decoder_buffer_fill(DecoderBuffer *buffer) ...@@ -124,15 +125,13 @@ decoder_buffer_fill(DecoderBuffer *buffer)
return true; return true;
} }
const void * ConstBuffer<void>
decoder_buffer_read(const DecoderBuffer *buffer, size_t *length_r) decoder_buffer_read(const DecoderBuffer *buffer)
{ {
if (buffer->consumed >= buffer->length) return {
/* buffer is empty */ buffer->data + buffer->consumed,
return nullptr; buffer->length - buffer->consumed
};
*length_r = buffer->length - buffer->consumed;
return buffer->data + buffer->consumed;
} }
void void
...@@ -149,19 +148,17 @@ decoder_buffer_consume(DecoderBuffer *buffer, size_t nbytes) ...@@ -149,19 +148,17 @@ decoder_buffer_consume(DecoderBuffer *buffer, size_t nbytes)
bool bool
decoder_buffer_skip(DecoderBuffer *buffer, size_t nbytes) decoder_buffer_skip(DecoderBuffer *buffer, size_t nbytes)
{ {
size_t length;
const void *data;
bool success; bool success;
/* this could probably be optimized by seeking */ /* this could probably be optimized by seeking */
while (true) { while (true) {
data = decoder_buffer_read(buffer, &length); auto data = decoder_buffer_read(buffer);
if (data != nullptr) { if (!data.IsEmpty()) {
if (length > nbytes) if (data.size > nbytes)
length = nbytes; data.size = nbytes;
decoder_buffer_consume(buffer, length); decoder_buffer_consume(buffer, data.size);
nbytes -= length; nbytes -= data.size;
if (nbytes == 0) if (nbytes == 0)
return true; return true;
} }
......
...@@ -34,6 +34,8 @@ struct DecoderBuffer; ...@@ -34,6 +34,8 @@ struct DecoderBuffer;
struct Decoder; struct Decoder;
struct InputStream; struct InputStream;
template<typename T> struct ConstBuffer;
/** /**
* Creates a new buffer. * Creates a new buffer.
* *
...@@ -80,13 +82,10 @@ decoder_buffer_fill(DecoderBuffer *buffer); ...@@ -80,13 +82,10 @@ decoder_buffer_fill(DecoderBuffer *buffer);
* decoder_buffer_consume() call. * decoder_buffer_consume() call.
* *
* @param buffer the decoder_buffer object * @param buffer the decoder_buffer object
* @param length_r pointer to a size_t where you will receive the
* number of bytes available
* @return a pointer to the read buffer, or nullptr if there is no data
* available
*/ */
const void * gcc_pure
decoder_buffer_read(const DecoderBuffer *buffer, size_t *length_r); ConstBuffer<void>
decoder_buffer_read(const DecoderBuffer *buffer);
/** /**
* Consume (delete, invalidate) a part of the buffer. The "nbytes" * Consume (delete, invalidate) a part of the buffer. The "nbytes"
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "InputStream.hxx" #include "InputStream.hxx"
#include "CheckAudioFormat.hxx" #include "CheckAudioFormat.hxx"
#include "tag/TagHandler.hxx" #include "tag/TagHandler.hxx"
#include "util/ConstBuffer.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "Log.hxx" #include "Log.hxx"
...@@ -66,12 +67,9 @@ adts_check_frame(const unsigned char *data) ...@@ -66,12 +67,9 @@ 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;
while (true) { while (true) {
const uint8_t *data = (const uint8_t *) auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
decoder_buffer_read(buffer, &length); if (data.size < 8) {
if (data == nullptr || length < 8) {
/* not enough data yet */ /* not enough data yet */
if (!decoder_buffer_fill(buffer)) if (!decoder_buffer_fill(buffer))
/* failed */ /* failed */
...@@ -81,21 +79,22 @@ adts_find_frame(DecoderBuffer *buffer) ...@@ -81,21 +79,22 @@ adts_find_frame(DecoderBuffer *buffer)
} }
/* find the 0xff marker */ /* find the 0xff marker */
const uint8_t *p = (const uint8_t *)memchr(data, 0xff, length); const uint8_t *p = (const uint8_t *)
memchr(data.data, 0xff, data.size);
if (p == nullptr) { if (p == nullptr) {
/* no marker - discard the buffer */ /* no marker - discard the buffer */
decoder_buffer_clear(buffer); decoder_buffer_clear(buffer);
continue; continue;
} }
if (p > data) { if (p > data.data) {
/* discard data before 0xff */ /* discard data before 0xff */
decoder_buffer_consume(buffer, p - data); decoder_buffer_consume(buffer, p - data.data);
continue; continue;
} }
/* is it a frame? */ /* is it a frame? */
size_t frame_length = adts_check_frame(data); size_t frame_length = adts_check_frame(data.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 */
...@@ -103,7 +102,7 @@ adts_find_frame(DecoderBuffer *buffer) ...@@ -103,7 +102,7 @@ adts_find_frame(DecoderBuffer *buffer)
continue; continue;
} }
if (length < frame_length) { if (data.size < frame_length) {
/* 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 */
...@@ -135,13 +134,11 @@ adts_song_duration(DecoderBuffer *buffer) ...@@ -135,13 +134,11 @@ adts_song_duration(DecoderBuffer *buffer)
break; break;
if (frames == 0) { if (frames == 0) {
size_t buffer_length; auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
const uint8_t *data = (const uint8_t *) assert(!data.IsEmpty());
decoder_buffer_read(buffer, &buffer_length); assert(frame_length <= data.size);
assert(data != nullptr);
assert(frame_length <= buffer_length);
sample_rate = adts_sample_rates[(data[2] & 0x3c) >> 2]; sample_rate = adts_sample_rates[(data.data[2] & 0x3c) >> 2];
} }
decoder_buffer_consume(buffer, frame_length); decoder_buffer_consume(buffer, frame_length);
...@@ -161,18 +158,16 @@ faad_song_duration(DecoderBuffer *buffer, InputStream &is) ...@@ -161,18 +158,16 @@ faad_song_duration(DecoderBuffer *buffer, InputStream &is)
const size_t fileread = size >= 0 ? size : 0; const size_t fileread = size >= 0 ? size : 0;
decoder_buffer_fill(buffer); decoder_buffer_fill(buffer);
size_t length; auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
const uint8_t *data = (const uint8_t *) if (data.IsEmpty())
decoder_buffer_read(buffer, &length);
if (data == nullptr)
return -1; return -1;
size_t tagsize = 0; size_t tagsize = 0;
if (length >= 10 && !memcmp(data, "ID3", 3)) { if (data.size >= 10 && !memcmp(data.data, "ID3", 3)) {
/* skip the ID3 tag */ /* skip the ID3 tag */
tagsize = (data[6] << 21) | (data[7] << 14) | tagsize = (data.data[6] << 21) | (data.data[7] << 14) |
(data[8] << 7) | (data[9] << 0); (data.data[8] << 7) | (data.data[9] << 0);
tagsize += 10; tagsize += 10;
...@@ -181,13 +176,13 @@ faad_song_duration(DecoderBuffer *buffer, InputStream &is) ...@@ -181,13 +176,13 @@ faad_song_duration(DecoderBuffer *buffer, InputStream &is)
if (!success) if (!success)
return -1; return -1;
data = (const uint8_t *)decoder_buffer_read(buffer, &length); data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
if (data == nullptr) if (data.IsEmpty())
return -1; return -1;
} }
if (is.IsSeekable() && length >= 2 && if (is.IsSeekable() && data.size >= 2 &&
data[0] == 0xFF && ((data[1] & 0xF6) == 0xF0)) { data.data[0] == 0xFF && ((data.data[1] & 0xF6) == 0xF0)) {
/* obtain the duration from the ADTS header */ /* obtain the duration from the ADTS header */
float song_length = adts_song_duration(buffer); float song_length = adts_song_duration(buffer);
...@@ -197,20 +192,20 @@ faad_song_duration(DecoderBuffer *buffer, InputStream &is) ...@@ -197,20 +192,20 @@ faad_song_duration(DecoderBuffer *buffer, InputStream &is)
decoder_buffer_fill(buffer); decoder_buffer_fill(buffer);
return song_length; return song_length;
} else if (length >= 5 && memcmp(data, "ADIF", 4) == 0) { } else if (data.size >= 5 && memcmp(data.data, "ADIF", 4) == 0) {
/* obtain the duration from the ADIF header */ /* obtain the duration from the ADIF header */
unsigned bit_rate; unsigned bit_rate;
size_t skip_size = (data[4] & 0x80) ? 9 : 0; size_t skip_size = (data.data[4] & 0x80) ? 9 : 0;
if (8 + skip_size > length) if (8 + skip_size > data.size)
/* not enough data yet; skip parsing this /* not enough data yet; skip parsing this
header */ header */
return -1; return -1;
bit_rate = ((data[4 + skip_size] & 0x0F) << 19) | bit_rate = ((data.data[4 + skip_size] & 0x0F) << 19) |
(data[5 + skip_size] << 11) | (data.data[5 + skip_size] << 11) |
(data[6 + skip_size] << 3) | (data.data[6 + skip_size] << 3) |
(data[7 + skip_size] & 0xE0); (data.data[7 + skip_size] & 0xE0);
if (fileread != 0 && bit_rate != 0) if (fileread != 0 && bit_rate != 0)
return fileread * 8.0 / bit_rate; return fileread * 8.0 / bit_rate;
...@@ -238,10 +233,8 @@ faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer *buffer, ...@@ -238,10 +233,8 @@ faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer *buffer,
uint32_t *sample_rate_p = &sample_rate; uint32_t *sample_rate_p = &sample_rate;
#endif #endif
size_t length; auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
const unsigned char *data = (const unsigned char *) if (data.IsEmpty()) {
decoder_buffer_read(buffer, &length);
if (data == nullptr) {
error.Set(faad_decoder_domain, "Empty file"); error.Set(faad_decoder_domain, "Empty file");
return false; return false;
} }
...@@ -249,8 +242,8 @@ faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer *buffer, ...@@ -249,8 +242,8 @@ faad_decoder_init(NeAACDecHandle decoder, DecoderBuffer *buffer,
uint8_t channels; uint8_t channels;
int32_t nbytes = NeAACDecInit(decoder, int32_t nbytes = NeAACDecInit(decoder,
/* deconst hack, libfaad requires this */ /* deconst hack, libfaad requires this */
const_cast<unsigned char *>(data), const_cast<uint8_t *>(data.data),
length, data.size,
sample_rate_p, &channels); 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");
...@@ -271,16 +264,14 @@ static const void * ...@@ -271,16 +264,14 @@ static const void *
faad_decoder_decode(NeAACDecHandle decoder, DecoderBuffer *buffer, faad_decoder_decode(NeAACDecHandle decoder, DecoderBuffer *buffer,
NeAACDecFrameInfo *frame_info) NeAACDecFrameInfo *frame_info)
{ {
size_t length; auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
const unsigned char *data = (const unsigned char *) if (data.IsEmpty())
decoder_buffer_read(buffer, &length);
if (data == nullptr)
return nullptr; return nullptr;
return NeAACDecDecode(decoder, frame_info, return NeAACDecDecode(decoder, frame_info,
/* deconst hack, libfaad requires this */ /* deconst hack, libfaad requires this */
const_cast<unsigned char *>(data), const_cast<uint8_t *>(data.data),
length); data.size);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment