diff options
Diffstat (limited to 'apps/codecs/libmusepack/mpc_demux.c')
-rw-r--r-- | apps/codecs/libmusepack/mpc_demux.c | 190 |
1 files changed, 121 insertions, 69 deletions
diff --git a/apps/codecs/libmusepack/mpc_demux.c b/apps/codecs/libmusepack/mpc_demux.c index 499170c135..97a431e597 100644 --- a/apps/codecs/libmusepack/mpc_demux.c +++ b/apps/codecs/libmusepack/mpc_demux.c | |||
@@ -124,7 +124,7 @@ mpc_demux_fill(mpc_demux * d, mpc_uint32_t min_bytes, int flags) | |||
124 | * @param fpos position in the stream in bits from the beginning of mpc datas | 124 | * @param fpos position in the stream in bits from the beginning of mpc datas |
125 | * @param min_bytes number of bytes to load after seeking | 125 | * @param min_bytes number of bytes to load after seeking |
126 | */ | 126 | */ |
127 | static void | 127 | static mpc_status |
128 | mpc_demux_seek(mpc_demux * d, mpc_seek_t fpos, mpc_uint32_t min_bytes) { | 128 | mpc_demux_seek(mpc_demux * d, mpc_seek_t fpos, mpc_uint32_t min_bytes) { |
129 | // d->bits_reader.buff - d->buffer = current byte position within buffer | 129 | // d->bits_reader.buff - d->buffer = current byte position within buffer |
130 | // d->bytes_total = buffer is filled with bytes_total bytes | 130 | // d->bytes_total = buffer is filled with bytes_total bytes |
@@ -143,7 +143,8 @@ mpc_demux_seek(mpc_demux * d, mpc_seek_t fpos, mpc_uint32_t min_bytes) { | |||
143 | if (d->si.stream_version == 7) | 143 | if (d->si.stream_version == 7) |
144 | next_pos = ((next_pos - d->si.header_position) & (-1 << 2)) + d->si.header_position; | 144 | next_pos = ((next_pos - d->si.header_position) & (-1 << 2)) + d->si.header_position; |
145 | buf_fpos = fpos - (next_pos << 3); | 145 | buf_fpos = fpos - (next_pos << 3); |
146 | d->r->seek(d->r, (mpc_int32_t) next_pos); | 146 | if (!d->r->seek(d->r, (mpc_int32_t) next_pos)) |
147 | return MPC_STATUS_FAIL; | ||
147 | mpc_demux_clear_buff(d); | 148 | mpc_demux_clear_buff(d); |
148 | if (d->si.stream_version == 7) | 149 | if (d->si.stream_version == 7) |
149 | mpc_demux_fill(d, MAX_BUFFER_SIZE, MPC_BUFFER_FULL | MPC_BUFFER_SWAP); | 150 | mpc_demux_fill(d, MAX_BUFFER_SIZE, MPC_BUFFER_FULL | MPC_BUFFER_SWAP); |
@@ -152,6 +153,8 @@ mpc_demux_seek(mpc_demux * d, mpc_seek_t fpos, mpc_uint32_t min_bytes) { | |||
152 | d->bits_reader.buff += buf_fpos >> 3; | 153 | d->bits_reader.buff += buf_fpos >> 3; |
153 | d->bits_reader.count = 8 - (buf_fpos & 7); | 154 | d->bits_reader.count = 8 - (buf_fpos & 7); |
154 | } | 155 | } |
156 | |||
157 | return MPC_STATUS_OK; | ||
155 | } | 158 | } |
156 | 159 | ||
157 | /** | 160 | /** |
@@ -171,7 +174,7 @@ static mpc_seek_t mpc_demux_pos(mpc_demux * d) | |||
171 | * | 174 | * |
172 | * @param d demuxer context | 175 | * @param d demuxer context |
173 | * @return size of tag, in bytes | 176 | * @return size of tag, in bytes |
174 | * @return MPC_STATUS_FILE on errors of any kind | 177 | * @return MPC_STATUS_FAIL on errors of any kind |
175 | */ | 178 | */ |
176 | static mpc_int32_t mpc_demux_skip_id3v2(mpc_demux * d) | 179 | static mpc_int32_t mpc_demux_skip_id3v2(mpc_demux * d) |
177 | { | 180 | { |
@@ -194,7 +197,7 @@ static mpc_int32_t mpc_demux_skip_id3v2(mpc_demux * d) | |||
194 | tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read flags | 197 | tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read flags |
195 | footerPresent = tmp[0] & 0x10; | 198 | footerPresent = tmp[0] & 0x10; |
196 | if ( tmp[0] & 0x0F ) | 199 | if ( tmp[0] & 0x0F ) |
197 | return MPC_STATUS_FILE; // not (yet???) allowed | 200 | return MPC_STATUS_FAIL; // not (yet???) allowed |
198 | 201 | ||
199 | tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read size | 202 | tmp[0] = mpc_bits_read(&d->bits_reader, 8); // read size |
200 | tmp[1] = mpc_bits_read(&d->bits_reader, 8); // read size | 203 | tmp[1] = mpc_bits_read(&d->bits_reader, 8); // read size |
@@ -202,7 +205,7 @@ static mpc_int32_t mpc_demux_skip_id3v2(mpc_demux * d) | |||
202 | tmp[3] = mpc_bits_read(&d->bits_reader, 8); // read size | 205 | tmp[3] = mpc_bits_read(&d->bits_reader, 8); // read size |
203 | 206 | ||
204 | if ( (tmp[0] | tmp[1] | tmp[2] | tmp[3]) & 0x80 ) | 207 | if ( (tmp[0] | tmp[1] | tmp[2] | tmp[3]) & 0x80 ) |
205 | return MPC_STATUS_FILE; // not allowed | 208 | return MPC_STATUS_FAIL; // not allowed |
206 | 209 | ||
207 | // read headerSize (syncsave: 4 * $0xxxxxxx = 28 significant bits) | 210 | // read headerSize (syncsave: 4 * $0xxxxxxx = 28 significant bits) |
208 | size = tmp[0] << 21; | 211 | size = tmp[0] << 21; |
@@ -216,7 +219,8 @@ static mpc_int32_t mpc_demux_skip_id3v2(mpc_demux * d) | |||
216 | 219 | ||
217 | // This is called before file headers get read, streamversion etc isn't yet known, demuxing isn't properly initialized and we can't call mpc_demux_seek() from here. | 220 | // This is called before file headers get read, streamversion etc isn't yet known, demuxing isn't properly initialized and we can't call mpc_demux_seek() from here. |
218 | mpc_demux_clear_buff(d); | 221 | mpc_demux_clear_buff(d); |
219 | if (!d->r->seek(d->r, size)) return MPC_STATUS_FILE; | 222 | if (!d->r->seek(d->r, size)) |
223 | return MPC_STATUS_FAIL; | ||
220 | 224 | ||
221 | return size; | 225 | return size; |
222 | } | 226 | } |
@@ -237,7 +241,7 @@ static mpc_status mpc_demux_seek_init(mpc_demux * d) | |||
237 | } | 241 | } |
238 | d->seek_table = g_seek_table; | 242 | d->seek_table = g_seek_table; |
239 | if (d->seek_table == 0) | 243 | if (d->seek_table == 0) |
240 | return MPC_STATUS_FILE; | 244 | return MPC_STATUS_FAIL; |
241 | d->seek_table[0] = (mpc_seek_t)mpc_demux_pos(d); | 245 | d->seek_table[0] = (mpc_seek_t)mpc_demux_pos(d); |
242 | d->seek_table_size = 1; | 246 | d->seek_table_size = 1; |
243 | 247 | ||
@@ -245,7 +249,7 @@ static mpc_status mpc_demux_seek_init(mpc_demux * d) | |||
245 | } | 249 | } |
246 | 250 | ||
247 | /* rockbox: do not use | 251 | /* rockbox: do not use |
248 | static void mpc_demux_ST(mpc_demux * d) | 252 | static mpc_status mpc_demux_ST(mpc_demux * d) |
249 | { | 253 | { |
250 | mpc_uint64_t tmp; | 254 | mpc_uint64_t tmp; |
251 | mpc_seek_t * table, last[2]; | 255 | mpc_seek_t * table, last[2]; |
@@ -254,7 +258,7 @@ static void mpc_demux_ST(mpc_demux * d) | |||
254 | mpc_uint32_t file_table_size; | 258 | mpc_uint32_t file_table_size; |
255 | 259 | ||
256 | if (d->seek_table != 0) | 260 | if (d->seek_table != 0) |
257 | return; | 261 | return MPC_STATUS_OK; |
258 | 262 | ||
259 | mpc_bits_get_size(&r, &tmp); | 263 | mpc_bits_get_size(&r, &tmp); |
260 | file_table_size = (mpc_seek_t) tmp; | 264 | file_table_size = (mpc_seek_t) tmp; |
@@ -276,7 +280,7 @@ static void mpc_demux_ST(mpc_demux * d) | |||
276 | table[0] = last[0] = (mpc_seek_t) (tmp + d->si.header_position) * 8; | 280 | table[0] = last[0] = (mpc_seek_t) (tmp + d->si.header_position) * 8; |
277 | 281 | ||
278 | if (d->seek_table_size == 1) | 282 | if (d->seek_table_size == 1) |
279 | return; | 283 | return MPC_STATUS_OK; |
280 | 284 | ||
281 | mpc_bits_get_size(&r, &tmp); | 285 | mpc_bits_get_size(&r, &tmp); |
282 | last[1] = (mpc_seek_t) (tmp + d->si.header_position) * 8; | 286 | last[1] = (mpc_seek_t) (tmp + d->si.header_position) * 8; |
@@ -292,9 +296,10 @@ static void mpc_demux_ST(mpc_demux * d) | |||
292 | if ((i & mask) == 0) | 296 | if ((i & mask) == 0) |
293 | table[i >> diff_pwr] = last[i & 1]; | 297 | table[i >> diff_pwr] = last[i & 1]; |
294 | } | 298 | } |
299 | return MPC_STATUS_OK; | ||
295 | } | 300 | } |
296 | 301 | ||
297 | static void mpc_demux_SP(mpc_demux * d, int size, int block_size) | 302 | static mpc_status mpc_demux_SP(mpc_demux * d, int size, int block_size) |
298 | { | 303 | { |
299 | mpc_seek_t cur; | 304 | mpc_seek_t cur; |
300 | mpc_uint64_t ptr; | 305 | mpc_uint64_t ptr; |
@@ -303,19 +308,27 @@ static void mpc_demux_SP(mpc_demux * d, int size, int block_size) | |||
303 | 308 | ||
304 | cur = mpc_demux_pos(d); | 309 | cur = mpc_demux_pos(d); |
305 | mpc_bits_get_size(&d->bits_reader, &ptr); | 310 | mpc_bits_get_size(&d->bits_reader, &ptr); |
306 | mpc_demux_seek(d, (ptr - size) * 8 + cur, 11); | 311 | MPC_AUTO_FAIL( mpc_demux_seek(d, (ptr - size) * 8 + cur, 11) ); |
307 | st_head_size = mpc_bits_get_block(&d->bits_reader, &b); | 312 | st_head_size = mpc_bits_get_block(&d->bits_reader, &b); |
308 | if (memcmp(b.key, "ST", 2) == 0) { | 313 | if (memcmp(b.key, "ST", 2) == 0) { |
309 | d->chap_pos = (ptr - size + b.size + st_head_size) * 8 + cur; | 314 | d->chap_pos = (ptr - size + b.size + st_head_size) * 8 + cur; |
310 | d->chap_nb = -1; | 315 | d->chap_nb = -1; |
311 | mpc_demux_fill(d, (mpc_uint32_t) b.size, 0); | 316 | if (mpc_demux_fill(d, (mpc_uint32_t) b.size, 0) < b.size) |
312 | mpc_demux_ST(d); | 317 | return MPC_STATUS_FAIL; |
318 | MPC_AUTO_FAIL( mpc_demux_ST(d) ); | ||
313 | } | 319 | } |
314 | mpc_demux_seek(d, cur, 11 + block_size); | 320 | return mpc_demux_seek(d, cur, 11 + block_size); |
321 | } | ||
322 | */ | ||
323 | /* rockbox: not used | ||
324 | static void mpc_demux_chap_empty(mpc_demux * d) { | ||
325 | free(d->chap); d->chap = 0; | ||
326 | d->chap_nb = 0; // -1 for undefined, 0 for no chapters | ||
327 | d->chap_pos = 0; | ||
315 | } | 328 | } |
316 | */ | 329 | */ |
317 | /* rockbox: not used | 330 | /* rockbox: not used |
318 | static void mpc_demux_chap_find(mpc_demux * d) | 331 | static mpc_status mpc_demux_chap_find_inner(mpc_demux * d) |
319 | { | 332 | { |
320 | mpc_block b; | 333 | mpc_block b; |
321 | int tag_size = 0, chap_size = 0, size, i = 0; | 334 | int tag_size = 0, chap_size = 0, size, i = 0; |
@@ -323,21 +336,26 @@ static void mpc_demux_chap_find(mpc_demux * d) | |||
323 | d->chap_nb = 0; | 336 | d->chap_nb = 0; |
324 | 337 | ||
325 | if (d->si.stream_version < 8) | 338 | if (d->si.stream_version < 8) |
326 | return; | 339 | return MPC_STATUS_OK; |
327 | 340 | ||
328 | if (d->chap_pos == 0) { | 341 | if (d->chap_pos == 0) { |
329 | mpc_uint64_t cur_pos = (d->si.header_position + 4) * 8; | 342 | mpc_uint64_t cur_pos = (d->si.header_position + 4) * 8; |
330 | mpc_demux_seek(d, cur_pos, 11); // seek to the beginning of the stream | 343 | MPC_AUTO_FAIL( mpc_demux_seek(d, cur_pos, 11) ); // seek to the beginning of the stream |
331 | size = mpc_bits_get_block(&d->bits_reader, &b); | 344 | size = mpc_bits_get_block(&d->bits_reader, &b); |
332 | while (memcmp(b.key, "SE", 2) != 0) { | 345 | while (memcmp(b.key, "SE", 2) != 0) { |
333 | if (mpc_check_key(b.key) != MPC_STATUS_OK) | 346 | mpc_uint64_t new_pos = cur_pos + (size + b.size) * 8; |
334 | return; | 347 | MPC_AUTO_FAIL(mpc_check_key(b.key)); |
348 | |||
335 | if (memcmp(b.key, "CT", 2) == 0) { | 349 | if (memcmp(b.key, "CT", 2) == 0) { |
336 | if (d->chap_pos == 0) d->chap_pos = cur_pos; | 350 | if (d->chap_pos == 0) d->chap_pos = cur_pos; |
337 | } else | 351 | } else { |
338 | d->chap_pos = 0; | 352 | d->chap_pos = 0; |
339 | cur_pos += (size + b.size) * 8; | 353 | } |
340 | mpc_demux_seek(d, cur_pos, 11); | 354 | if (new_pos <= cur_pos) |
355 | return MPC_STATUS_FAIL; | ||
356 | cur_pos = new_pos; | ||
357 | |||
358 | MPC_AUTO_FAIL( mpc_demux_seek(d, cur_pos, 11) ); | ||
341 | size = mpc_bits_get_block(&d->bits_reader, &b); | 359 | size = mpc_bits_get_block(&d->bits_reader, &b); |
342 | } | 360 | } |
343 | if (d->chap_pos == 0) | 361 | if (d->chap_pos == 0) |
@@ -353,24 +371,42 @@ static void mpc_demux_chap_find(mpc_demux * d) | |||
353 | size = mpc_bits_get_size(&d->bits_reader, &chap_sample) + 4; | 371 | size = mpc_bits_get_size(&d->bits_reader, &chap_sample) + 4; |
354 | chap_size += size; | 372 | chap_size += size; |
355 | tag_size += b.size - size; | 373 | tag_size += b.size - size; |
356 | mpc_demux_seek(d, d->chap_pos + (chap_size + tag_size) * 8, 20); | 374 | MPC_AUTO_FAIL( mpc_demux_seek(d, d->chap_pos + (chap_size + tag_size) * 8, 20) ); |
357 | size = mpc_bits_get_block(&d->bits_reader, &b); | 375 | size = mpc_bits_get_block(&d->bits_reader, &b); |
358 | } | 376 | } |
359 | 377 | ||
360 | if (d->chap_nb > 0) { | 378 | if (d->chap_nb > 0) { |
361 | char * ptag; | 379 | char * ptag; |
362 | d->chap = malloc(sizeof(mpc_chap_info) * d->chap_nb + tag_size); | 380 | d->chap = malloc(sizeof(mpc_chap_info) * d->chap_nb + tag_size); |
381 | if (d->chap == 0) | ||
382 | return MPC_STATUS_FAIL; | ||
383 | |||
363 | ptag = (char*)(d->chap + d->chap_nb); | 384 | ptag = (char*)(d->chap + d->chap_nb); |
364 | 385 | ||
365 | mpc_demux_seek(d, d->chap_pos, 11); | 386 | MPC_AUTO_FAIL( mpc_demux_seek(d, d->chap_pos, 11) ); |
366 | size = mpc_bits_get_block(&d->bits_reader, &b); | 387 | size = mpc_bits_get_block(&d->bits_reader, &b); |
367 | while (memcmp(b.key, "CT", 2) == 0) { | 388 | while (memcmp(b.key, "CT", 2) == 0) { |
368 | mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0); | 389 | mpc_uint_t tmp_size; |
390 | char * tmp_ptag = ptag; | ||
391 | if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0) < b.size) | ||
392 | return MPC_STATUS_FAIL; | ||
369 | size = mpc_bits_get_size(&d->bits_reader, &d->chap[i].sample) + 4; | 393 | size = mpc_bits_get_size(&d->bits_reader, &d->chap[i].sample) + 4; |
370 | d->chap[i].gain = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16); | 394 | d->chap[i].gain = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16); |
371 | d->chap[i].peak = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16); | 395 | d->chap[i].peak = (mpc_uint16_t) mpc_bits_read(&d->bits_reader, 16); |
372 | memcpy(ptag, d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3), b.size - size); | 396 | |
373 | d->bits_reader.buff += b.size - size; | 397 | tmp_size = b.size - size; |
398 | do { | ||
399 | mpc_uint_t rd_size = tmp_size; | ||
400 | mpc_uint8_t * tmp_buff = d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3); | ||
401 | mpc_uint32_t avail_bytes = d->bytes_total + d->buffer - tmp_buff; | ||
402 | rd_size = mini(rd_size, avail_bytes); | ||
403 | memcpy(tmp_ptag, tmp_buff, rd_size); | ||
404 | tmp_size -= rd_size; | ||
405 | tmp_ptag += rd_size; | ||
406 | d->bits_reader.buff += rd_size; | ||
407 | mpc_demux_fill(d, tmp_size, 0); | ||
408 | } while (tmp_size > 0); | ||
409 | |||
374 | d->chap[i].tag_size = b.size - size; | 410 | d->chap[i].tag_size = b.size - size; |
375 | d->chap[i].tag = ptag; | 411 | d->chap[i].tag = ptag; |
376 | ptag += b.size - size; | 412 | ptag += b.size - size; |
@@ -380,9 +416,17 @@ static void mpc_demux_chap_find(mpc_demux * d) | |||
380 | } | 416 | } |
381 | 417 | ||
382 | d->bits_reader.buff -= size; | 418 | d->bits_reader.buff -= size; |
419 | return MPC_STATUS_OK; | ||
420 | } | ||
421 | */ | ||
422 | /* rockbox: not used | ||
423 | static mpc_status mpc_demux_chap_find(mpc_demux * d) { | ||
424 | mpc_status s = mpc_demux_chap_find_inner(d); | ||
425 | if (MPC_IS_FAILURE(s)) | ||
426 | mpc_demux_chap_empty(d); | ||
427 | return s; | ||
383 | } | 428 | } |
384 | */ | 429 | */ |
385 | |||
386 | /** | 430 | /** |
387 | * Gets the number of chapters in the stream | 431 | * Gets the number of chapters in the stream |
388 | * @param d pointer to a musepack demuxer | 432 | * @param d pointer to a musepack demuxer |
@@ -424,7 +468,8 @@ static mpc_status mpc_demux_header(mpc_demux * d) | |||
424 | */ | 468 | */ |
425 | // get header position | 469 | // get header position |
426 | d->si.header_position = mpc_demux_skip_id3v2(d); | 470 | d->si.header_position = mpc_demux_skip_id3v2(d); |
427 | if(d->si.header_position < 0) return MPC_STATUS_FILE; | 471 | if(d->si.header_position < 0) |
472 | return MPC_STATUS_FAIL; | ||
428 | 473 | ||
429 | d->si.tag_offset = d->si.total_file_length = d->r->get_size(d->r); | 474 | d->si.tag_offset = d->si.total_file_length = d->r->get_size(d->r); |
430 | 475 | ||
@@ -437,14 +482,11 @@ static mpc_status mpc_demux_header(mpc_demux * d) | |||
437 | if (memcmp(magic, "MP+", 3) == 0) { | 482 | if (memcmp(magic, "MP+", 3) == 0) { |
438 | d->si.stream_version = magic[3] & 15; | 483 | d->si.stream_version = magic[3] & 15; |
439 | d->si.pns = magic[3] >> 4; | 484 | d->si.pns = magic[3] >> 4; |
440 | if (d->si.stream_version == 7) { | 485 | if (d->si.stream_version != 7) |
441 | mpc_status ret; | 486 | return MPC_STATUS_FAIL; |
442 | mpc_demux_fill(d, 6 * 4, MPC_BUFFER_SWAP); // header block size + endian convertion | 487 | if (mpc_demux_fill(d, 6 * 4, MPC_BUFFER_SWAP) < 6 * 4) // header block size + endian convertion |
443 | ret = streaminfo_read_header_sv7(&d->si, &d->bits_reader); | 488 | return MPC_STATUS_FAIL; |
444 | if (ret != MPC_STATUS_OK) return ret; | 489 | MPC_AUTO_FAIL( streaminfo_read_header_sv7(&d->si, &d->bits_reader) ); |
445 | } else { | ||
446 | return MPC_STATUS_INVALIDSV; | ||
447 | } | ||
448 | } else if (memcmp(magic, "MPCK", 4) == 0) { | 490 | } else if (memcmp(magic, "MPCK", 4) == 0) { |
449 | mpc_block b; | 491 | mpc_block b; |
450 | int size; | 492 | int size; |
@@ -452,31 +494,35 @@ static mpc_status mpc_demux_header(mpc_demux * d) | |||
452 | size = mpc_bits_get_block(&d->bits_reader, &b); | 494 | size = mpc_bits_get_block(&d->bits_reader, &b); |
453 | while( memcmp(b.key, "AP", 2) != 0 ){ // scan all blocks until audio | 495 | while( memcmp(b.key, "AP", 2) != 0 ){ // scan all blocks until audio |
454 | if (mpc_check_key(b.key) != MPC_STATUS_OK) | 496 | if (mpc_check_key(b.key) != MPC_STATUS_OK) |
455 | return MPC_STATUS_INVALIDSV; | 497 | return MPC_STATUS_FAIL; |
456 | if (b.size > (mpc_uint64_t) MAX_BUFFER_SIZE - 11) | 498 | if (b.size > (mpc_uint64_t) MAX_BUFFER_SIZE - 11) |
457 | return MPC_STATUS_INVALIDSV; | 499 | return MPC_STATUS_FAIL; |
458 | mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0); | 500 | |
459 | if (memcmp(b.key, "SH", 2) == 0){ | 501 | if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0) <= b.size) |
460 | int ret = streaminfo_read_header_sv8(&d->si, &d->bits_reader, (mpc_uint32_t) b.size); | 502 | return MPC_STATUS_FAIL; |
461 | if (ret != MPC_STATUS_OK) return ret; | 503 | |
462 | } else if (memcmp(b.key, "RG", 2) == 0) | 504 | if (memcmp(b.key, "SH", 2) == 0) { |
505 | MPC_AUTO_FAIL( streaminfo_read_header_sv8(&d->si, &d->bits_reader, (mpc_uint32_t) b.size) ); | ||
506 | } else if (memcmp(b.key, "RG", 2) == 0) { | ||
463 | streaminfo_gain(&d->si, &d->bits_reader); | 507 | streaminfo_gain(&d->si, &d->bits_reader); |
464 | else if (memcmp(b.key, "EI", 2) == 0) | 508 | } else if (memcmp(b.key, "EI", 2) == 0) { |
465 | streaminfo_encoder_info(&d->si, &d->bits_reader); | 509 | streaminfo_encoder_info(&d->si, &d->bits_reader); |
466 | /* rockbox: do not use | 510 | /* rockbox: do not use |
467 | else if (memcmp(b.key, "SO", 2) == 0) | 511 | } else if (memcmp(b.key, "SO", 2) == 0) { |
468 | mpc_demux_SP(d, size, (mpc_uint32_t) b.size); | 512 | MPC_AUTO_FAIL( mpc_demux_SP(d, size, (mpc_uint32_t) b.size) ); |
469 | else if (memcmp(b.key, "ST", 2) == 0) | 513 | } else if (memcmp(b.key, "ST", 2) == 0) { |
470 | mpc_demux_ST(d); | 514 | MPC_AUTO_FAIL( mpc_demux_ST(d) ); |
471 | */ | 515 | */ |
516 | } | ||
472 | d->bits_reader.buff += b.size; | 517 | d->bits_reader.buff += b.size; |
473 | size = mpc_bits_get_block(&d->bits_reader, &b); | 518 | size = mpc_bits_get_block(&d->bits_reader, &b); |
474 | } | 519 | } |
475 | d->bits_reader.buff -= size; | 520 | d->bits_reader.buff -= size; |
476 | if (d->si.stream_version == 0) // si not initialized !!! | 521 | if (d->si.stream_version == 0) // si not initialized !!! |
477 | return MPC_STATUS_INVALIDSV; | 522 | return MPC_STATUS_FAIL; |
478 | } else | 523 | } else { |
479 | return MPC_STATUS_INVALIDSV; | 524 | return MPC_STATUS_FAIL; |
525 | } | ||
480 | 526 | ||
481 | return MPC_STATUS_OK; | 527 | return MPC_STATUS_OK; |
482 | } | 528 | } |
@@ -519,7 +565,7 @@ void mpc_demux_get_info(mpc_demux * d, mpc_streaminfo * i) | |||
519 | memcpy(i, &d->si, sizeof d->si); | 565 | memcpy(i, &d->si, sizeof d->si); |
520 | } | 566 | } |
521 | 567 | ||
522 | mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i) | 568 | static mpc_status mpc_demux_decode_inner(mpc_demux * d, mpc_frame_info * i) |
523 | { | 569 | { |
524 | mpc_bits_reader r; | 570 | mpc_bits_reader r; |
525 | if (d->si.stream_version >= 8) { | 571 | if (d->si.stream_version >= 8) { |
@@ -532,17 +578,19 @@ mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i) | |||
532 | d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d); | 578 | d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d); |
533 | d->seek_table_size ++; | 579 | d->seek_table_size ++; |
534 | } | 580 | } |
535 | mpc_demux_fill(d, 11, 0); // max header block size | 581 | mpc_demux_fill(d, 11, MPC_BUFFER_FULL); // max header block size |
536 | mpc_bits_get_block(&d->bits_reader, &b); | 582 | mpc_bits_get_block(&d->bits_reader, &b); |
537 | while( memcmp(b.key, "AP", 2) != 0 ) { // scan all blocks until audio | 583 | while( memcmp(b.key, "AP", 2) != 0 ) { // scan all blocks until audio |
538 | if (mpc_check_key(b.key) != MPC_STATUS_OK) | 584 | MPC_AUTO_FAIL( mpc_check_key(b.key) ); |
539 | goto error; | 585 | |
540 | if (memcmp(b.key, "SE", 2) == 0) { // end block | 586 | if (memcmp(b.key, "SE", 2) == 0) { // end block |
541 | i->bits = -1; | 587 | i->bits = -1; |
542 | return MPC_STATUS_OK; | 588 | return MPC_STATUS_OK; |
543 | } | 589 | } |
544 | if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0) == 0) | 590 | |
545 | goto error; | 591 | if (mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, MPC_BUFFER_FULL) < b.size) |
592 | return MPC_STATUS_FAIL; | ||
593 | |||
546 | d->bits_reader.buff += b.size; | 594 | d->bits_reader.buff += b.size; |
547 | mpc_bits_get_block(&d->bits_reader, &b); | 595 | mpc_bits_get_block(&d->bits_reader, &b); |
548 | } | 596 | } |
@@ -556,7 +604,7 @@ mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i) | |||
556 | d->block_bits -= ((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count; | 604 | d->block_bits -= ((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count; |
557 | d->block_frames--; | 605 | d->block_frames--; |
558 | if (d->block_bits < 0 || (d->block_frames == 0 && d->block_bits > 7)) | 606 | if (d->block_bits < 0 || (d->block_frames == 0 && d->block_bits > 7)) |
559 | goto error; | 607 | return MPC_STATUS_FAIL; |
560 | } else { | 608 | } else { |
561 | if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) { | 609 | if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) { |
562 | d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d); | 610 | d->seek_table[d->seek_table_size] = (mpc_seek_t) mpc_demux_pos(d); |
@@ -568,15 +616,19 @@ mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i) | |||
568 | r = d->bits_reader; | 616 | r = d->bits_reader; |
569 | mpc_decoder_decode_frame(d->d, &d->bits_reader, i); | 617 | mpc_decoder_decode_frame(d->d, &d->bits_reader, i); |
570 | if (i->bits != -1 && d->block_bits != (mpc_int32_t)(((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count)) | 618 | if (i->bits != -1 && d->block_bits != (mpc_int32_t)(((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count)) |
571 | goto error; | 619 | return MPC_STATUS_FAIL; |
572 | } | 620 | } |
573 | if (i->bits != -1 && d->buffer + d->bytes_total < d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3)) | 621 | if (i->bits != -1 && d->buffer + d->bytes_total < d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3)) |
574 | goto error; | 622 | return MPC_STATUS_FAIL; |
575 | 623 | ||
576 | return MPC_STATUS_OK; | 624 | return MPC_STATUS_OK; |
577 | error: | 625 | } |
578 | i->bits = -1; // we pretend it's end of file | 626 | |
579 | return MPC_STATUS_INVALIDSV; | 627 | mpc_status mpc_demux_decode(mpc_demux * d, mpc_frame_info * i) { |
628 | mpc_status s = mpc_demux_decode_inner(d, i); | ||
629 | if (MPC_IS_FAILURE(s)) | ||
630 | i->bits = -1; // we pretend it's end of file | ||
631 | return s; | ||
580 | } | 632 | } |
581 | 633 | ||
582 | /* rockbox: not used | 634 | /* rockbox: not used |
@@ -654,8 +706,8 @@ mpc_status mpc_demux_seek_sample(mpc_demux * d, mpc_uint64_t destsample) | |||
654 | void mpc_set_replay_level(mpc_demux * d, float level, mpc_bool_t use_gain, | 706 | void mpc_set_replay_level(mpc_demux * d, float level, mpc_bool_t use_gain, |
655 | mpc_bool_t use_title, mpc_bool_t clip_prevention) | 707 | mpc_bool_t use_title, mpc_bool_t clip_prevention) |
656 | { | 708 | { |
657 | float peak = use_title ? d->si.peak_title : d->si.peak_album; | 709 | float peak = (float) ( use_title ? d->si.peak_title : d->si.peak_album ); |
658 | float gain = use_title ? d->si.gain_title : d->si.gain_album; | 710 | float gain = (float) ( use_title ? d->si.gain_title : d->si.gain_album ); |
659 | 711 | ||
660 | if(!use_gain && !clip_prevention) | 712 | if(!use_gain && !clip_prevention) |
661 | return; | 713 | return; |
@@ -663,12 +715,12 @@ void mpc_set_replay_level(mpc_demux * d, float level, mpc_bool_t use_gain, | |||
663 | if(!peak) | 715 | if(!peak) |
664 | peak = 1.; | 716 | peak = 1.; |
665 | else | 717 | else |
666 | peak = (1 << 15) / pow(10, peak / (20 * 256)); | 718 | peak = (float) ( (1 << 15) / pow(10, peak / (20 * 256)) ); |
667 | 719 | ||
668 | if(!gain) | 720 | if(!gain) |
669 | gain = 1.; | 721 | gain = 1.; |
670 | else | 722 | else |
671 | gain = pow(10, (level - gain / 256) / 20); | 723 | gain = (float) pow(10, (level - gain / 256) / 20); |
672 | 724 | ||
673 | if(clip_prevention && (peak < gain || !use_gain)) | 725 | if(clip_prevention && (peak < gain || !use_gain)) |
674 | gain = peak; | 726 | gain = peak; |