DecoderBuffer: add method _need()
Move code from the FAAD decoder plugin.
This commit is contained in:
parent
eabec967ec
commit
b2b95cad20
@ -117,21 +117,40 @@ decoder_buffer_fill(DecoderBuffer *buffer)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const void *
|
||||||
|
decoder_buffer_head(const DecoderBuffer *buffer)
|
||||||
|
{
|
||||||
|
return buffer->data + buffer->consumed;
|
||||||
|
}
|
||||||
|
|
||||||
size_t
|
size_t
|
||||||
decoder_buffer_available(const DecoderBuffer *buffer)
|
decoder_buffer_available(const DecoderBuffer *buffer)
|
||||||
{
|
{
|
||||||
return buffer->length - buffer->consumed;;
|
return buffer->length - buffer->consumed;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConstBuffer<void>
|
ConstBuffer<void>
|
||||||
decoder_buffer_read(const DecoderBuffer *buffer)
|
decoder_buffer_read(const DecoderBuffer *buffer)
|
||||||
{
|
{
|
||||||
return {
|
return {
|
||||||
buffer->data + buffer->consumed,
|
decoder_buffer_head(buffer),
|
||||||
buffer->length - buffer->consumed
|
decoder_buffer_available(buffer),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ConstBuffer<void>
|
||||||
|
decoder_buffer_need(DecoderBuffer *buffer, size_t min_size)
|
||||||
|
{
|
||||||
|
while (true) {
|
||||||
|
const auto available = decoder_buffer_available(buffer);
|
||||||
|
if (available >= min_size)
|
||||||
|
return { decoder_buffer_head(buffer), available };
|
||||||
|
|
||||||
|
if (!decoder_buffer_fill(buffer))
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
decoder_buffer_consume(DecoderBuffer *buffer, size_t nbytes)
|
decoder_buffer_consume(DecoderBuffer *buffer, size_t nbytes)
|
||||||
{
|
{
|
||||||
|
@ -90,6 +90,13 @@ gcc_pure
|
|||||||
ConstBuffer<void>
|
ConstBuffer<void>
|
||||||
decoder_buffer_read(const DecoderBuffer *buffer);
|
decoder_buffer_read(const DecoderBuffer *buffer);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wait until this number of bytes are available. Returns nullptr on
|
||||||
|
* error.
|
||||||
|
*/
|
||||||
|
ConstBuffer<void>
|
||||||
|
decoder_buffer_need(DecoderBuffer *buffer, size_t min_size);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Consume (delete, invalidate) a part of the buffer. The "nbytes"
|
* Consume (delete, invalidate) a part of the buffer. The "nbytes"
|
||||||
* parameter must not be larger than the length returned by
|
* parameter must not be larger than the length returned by
|
||||||
|
@ -66,15 +66,10 @@ static size_t
|
|||||||
adts_find_frame(DecoderBuffer *buffer)
|
adts_find_frame(DecoderBuffer *buffer)
|
||||||
{
|
{
|
||||||
while (true) {
|
while (true) {
|
||||||
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
|
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_need(buffer, 8));
|
||||||
if (data.size < 8) {
|
if (data.IsNull())
|
||||||
/* not enough data yet */
|
/* failed */
|
||||||
if (!decoder_buffer_fill(buffer))
|
return 0;
|
||||||
/* failed */
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* find the 0xff marker */
|
/* find the 0xff marker */
|
||||||
const uint8_t *p = (const uint8_t *)
|
const uint8_t *p = (const uint8_t *)
|
||||||
@ -100,17 +95,10 @@ adts_find_frame(DecoderBuffer *buffer)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data.size < frame_length) {
|
if (decoder_buffer_need(buffer, frame_length).IsNull()) {
|
||||||
/* available buffer size is smaller than the
|
/* not enough data; discard this frame to
|
||||||
frame will be - attempt to read more
|
prevent a possible buffer overflow */
|
||||||
data */
|
decoder_buffer_clear(buffer);
|
||||||
if (!decoder_buffer_fill(buffer)) {
|
|
||||||
/* not enough data; discard this frame
|
|
||||||
to prevent a possible buffer
|
|
||||||
overflow */
|
|
||||||
decoder_buffer_clear(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,9 +169,8 @@ faad_song_duration(DecoderBuffer *buffer, InputStream &is)
|
|||||||
const auto size = is.GetSize();
|
const auto size = is.GetSize();
|
||||||
const size_t fileread = size >= 0 ? size : 0;
|
const size_t fileread = size >= 0 ? size : 0;
|
||||||
|
|
||||||
decoder_buffer_fill(buffer);
|
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_need(buffer, 5));
|
||||||
auto data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
|
if (data.IsNull())
|
||||||
if (data.IsEmpty())
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
size_t tagsize = 0;
|
size_t tagsize = 0;
|
||||||
@ -195,13 +182,11 @@ faad_song_duration(DecoderBuffer *buffer, InputStream &is)
|
|||||||
|
|
||||||
tagsize += 10;
|
tagsize += 10;
|
||||||
|
|
||||||
const bool success = decoder_buffer_skip(buffer, tagsize) &&
|
if (!decoder_buffer_skip(buffer, tagsize))
|
||||||
decoder_buffer_fill(buffer);
|
|
||||||
if (!success)
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_read(buffer));
|
data = ConstBuffer<uint8_t>::FromVoid(decoder_buffer_need(buffer, 5));
|
||||||
if (data.IsEmpty())
|
if (data.IsNull())
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user