forked from DeaDBeeF-Player/deadbeef
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdecodedblock.c
162 lines (130 loc) · 4.24 KB
/
decodedblock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
DeaDBeeF -- the music player
Copyright (C) 2009-2021 Oleksiy Yakovenko and other contributors
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "decodedblock.h"
#include <assert.h>
#include <stdlib.h>
#define BLOCK_COUNT 48 // FIXME: must be the same or more than streamreader block count
static decoded_block_t *_decoded_blocks; // list of all _decoded_blocks
static decoded_block_t *_decoded_blocks_queued; // first available block with data (can be NULL)
static decoded_block_t *_decoded_blocks_free; // next block available to be read into / queued
void
decoded_blocks_init (void) {
for (int i = 0; i < BLOCK_COUNT; i++) {
decoded_block_t *b = calloc (1, sizeof (decoded_block_t));
b->next = _decoded_blocks;
_decoded_blocks = b;
}
_decoded_blocks_free = _decoded_blocks;
}
void
decoded_blocks_free (void) {
while (_decoded_blocks) {
decoded_block_t *next = _decoded_blocks->next;
if (_decoded_blocks->track != NULL) {
pl_item_unref(_decoded_blocks->track);
_decoded_blocks->track = NULL;
}
free (_decoded_blocks);
_decoded_blocks = next;
}
_decoded_blocks_free = _decoded_blocks_queued = NULL;
}
void
decoded_blocks_release (decoded_block_t *b) {
b->is_silent_header = 0;
b->last = 0;
b->first = 0;
b->remaining_bytes = 0;
b->total_bytes = 0;
b->queued = 0;
b->playback_time = 0;
if (b->track != NULL) {
pl_item_unref (b->track);
}
b->track = NULL;
}
// Recycle all _decoded_blocks / empty queue.
// Should be called from streamer_reset and similar situations.
void
decoded_blocks_reset (void) {
decoded_block_t *b = _decoded_blocks;
while (b) {
decoded_blocks_release (b);
b = b->next;
}
_decoded_blocks_free = _decoded_blocks;
_decoded_blocks_queued = NULL;
}
decoded_block_t *
decoded_blocks_current (void) {
return _decoded_blocks_queued;
}
void
decoded_blocks_next (void) {
if (_decoded_blocks_queued) {
decoded_blocks_release (_decoded_blocks_queued);
_decoded_blocks_queued = _decoded_blocks_queued->next;
if (!_decoded_blocks_queued) {
_decoded_blocks_queued = _decoded_blocks;
}
}
if (_decoded_blocks_queued && !_decoded_blocks_queued->queued) {
_decoded_blocks_queued = NULL; // no available _decoded_blocks with data
}
}
decoded_block_t *
decoded_blocks_append(void) {
if (_decoded_blocks_free->queued) {
return NULL; // all buffers full
}
if (!_decoded_blocks_queued) {
_decoded_blocks_queued = _decoded_blocks_free;
}
decoded_block_t *queued_block = _decoded_blocks_free;
queued_block->queued = 1;
_decoded_blocks_free = _decoded_blocks_free->next;
if (!_decoded_blocks_free) {
_decoded_blocks_free = _decoded_blocks;
}
return queued_block;
}
int
decoded_blocks_have_free (void) {
return _decoded_blocks_free != NULL;
}
float
decoded_blocks_playback_time_total (void) {
decoded_block_t *first = _decoded_blocks_queued;
if (first == NULL) {
return 0;
}
decoded_block_t *curr = first;
float time = 0;
int wrap = 0;
while (curr->queued && !(wrap && curr == first)) {
time += curr->playback_time;
if (curr == first) {
wrap = 1;
}
curr = curr->next;
if (curr == NULL) {
curr = _decoded_blocks;
}
}
return time;
}