summaryrefslogtreecommitdiff
path: root/plugins/ffap
diff options
context:
space:
mode:
authorGravatar Alexey Yakovenko <wakeroid@gmail.com>2009-12-26 13:40:09 +0100
committerGravatar Alexey Yakovenko <wakeroid@gmail.com>2009-12-26 13:40:09 +0100
commit7446a0ad28dacef71cb5c62e8de5350037461a31 (patch)
treebf8a665e9417535b9b3e1a3d000446e375d348c5 /plugins/ffap
parentf59308a14fb729f668d6539d6e486f3f5e51d4fa (diff)
ffap: bitrate display; code cleanup; memory optimization
Diffstat (limited to 'plugins/ffap')
-rw-r--r--plugins/ffap/ffap.c56
1 files changed, 23 insertions, 33 deletions
diff --git a/plugins/ffap/ffap.c b/plugins/ffap/ffap.c
index 946be5e3..5a3bf1f4 100644
--- a/plugins/ffap/ffap.c
+++ b/plugins/ffap/ffap.c
@@ -48,8 +48,6 @@
static DB_decoder_t plugin;
static DB_functions_t *deadbeef;
-//static float timestart;
-//static float timeend;
static int startsample;
static int endsample;
@@ -259,7 +257,7 @@ typedef struct APEContext {
const uint8_t *ptr; ///< current position in frame data
const uint8_t *last_ptr;
- uint8_t packet_data[PACKET_BUFFER_SIZE];
+ uint8_t *packet_data; // must be PACKET_BUFFER_SIZE
int packet_remaining; // number of bytes in packet_data
int packet_sizeleft; // number of bytes left unread for current ape frame
int samplestoskip;
@@ -606,15 +604,20 @@ static int ape_read_packet(DB_FILE *fp, APEContext *ape_ctx)
else
nblocks = ape->blocksperframe;
-// if (PACKET_MAX_SIZE < ape->frames[ape->currentframe].size + extra_size) {
-// return -1;
-// }
-// packet_sizeleft = ape->frames[ape->currentframe].size + extra_size;
-
AV_WL32(ape->packet_data , nblocks);
AV_WL32(ape->packet_data + 4, ape->frames[ape->currentframe].skip);
// packet_sizeleft -= 8;
+// update bitrate
+ float sec = (float)nblocks / ape->samplerate;
+ int bitrate = -1;
+ if (sec != 0 && ape->frames[ape->currentframe].size != 0) {
+ bitrate = ape->frames[ape->currentframe].size / sec * 8;
+ }
+ if (bitrate > 0) {
+ deadbeef->streamer_set_bitrate (bitrate/1000);
+ }
+
int sz = PACKET_BUFFER_SIZE-8;
sz = min (sz, ape->frames[ape->currentframe].size);
// fprintf (stderr, "readsize: %d, packetsize: %d\n", sz, ape->frames[ape->currentframe].size);
@@ -630,6 +633,10 @@ static int ape_read_packet(DB_FILE *fp, APEContext *ape_ctx)
static void
ape_free_ctx (APEContext *ape_ctx) {
int i;
+ if (ape_ctx->packet_data) {
+ free (ape_ctx->packet_data);
+ ape_ctx->packet_data = NULL;
+ }
if (ape_ctx->frames) {
free (ape_ctx->frames);
ape_ctx->frames = NULL;
@@ -652,21 +659,6 @@ ffap_free (void)
ape_free_ctx (&ape_ctx);
}
-#if 0
-static int ape_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
-{
- AVStream *st = s->streams[stream_index];
- APEContext *ape = s->priv_data;
- int index = av_index_search_timestamp(st, timestamp, flags);
-
- if (index < 0)
- return -1;
-
- ape->currentframe = index;
- return 0;
-}
-#endif
-
static DB_FILE *fp;
static int
@@ -714,17 +706,19 @@ ffap_init(DB_playItem_t *it)
if (it->endsample > 0) {
startsample = it->startsample;
endsample = it->endsample;
-// timestart = it->timestart;
-// timeend = it->timeend;
plugin.seek_sample (0);
//trace ("start: %d/%f, end: %d/%f\n", startsample, timestart, endsample, timeend);
}
else {
- //timestart = 0;
- //timeend = it->duration;
startsample = 0;
endsample = ape_ctx.totalsamples-1;
}
+
+ ape_ctx.packet_data = malloc (PACKET_BUFFER_SIZE);
+ if (!ape_ctx.packet_data) {
+ fprintf (stderr, "ffap: failed to allocate memory for packet data\n");
+ return -1;
+ }
return 0;
}
@@ -1517,8 +1511,6 @@ ape_decode_frame(APEContext *s, void *data, int *data_size)
}
if (s->packet_remaining < PACKET_BUFFER_SIZE) {
-// assert (packet_sizeleft >= 0 && packet_remaining >= 0);
-// if (packet_sizeleft == 0 && packet_remaining == 0) {
if (s->samples == 0) {
if (s->currentframe == s->totalframes) {
return -1;
@@ -1537,6 +1529,7 @@ ape_decode_frame(APEContext *s, void *data, int *data_size)
s->ptr = s->last_ptr = s->packet_data;
nblocks = s->samples = bytestream_get_be32(&s->ptr);
+
//fprintf (stderr, "s->samples=%d (1)\n", s->samples);
n = bytestream_get_be32(&s->ptr);
if(n < 0 || n > 3){
@@ -1546,6 +1539,7 @@ ape_decode_frame(APEContext *s, void *data, int *data_size)
s->ptr += n;
s->currentframeblocks = nblocks;
+
//buf += 4;
if (s->samples <= 0) {
*data_size = 0;
@@ -1565,10 +1559,6 @@ ape_decode_frame(APEContext *s, void *data, int *data_size)
sz = sz&~3;
uint8_t *p = s->packet_data + s->packet_remaining;
int r = deadbeef->fread (p, 1, sz, fp);
- //if (r != s) {
- // fprintf (stderr, "unexpected eof while reading ape frame\n");
- // return -1;
- //}
bswap_buf((uint32_t*)p, (const uint32_t*)p, r >> 2);
s->packet_sizeleft -= r;
s->packet_remaining += r;