/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id$ * * Copyright (C) 2005 Dave Chapman * * All files in this archive are subject to the GNU General Public License. * See the file COPYING in the source tree root for full license agreement. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ #include #include #include "m4a.h" #if defined(DEBUG) || defined(SIMULATOR) extern struct codec_api* rb; #define DEBUGF rb->debugf #else #define DEBUGF(...) #endif /* Implementation of the stream.h functions used by libalac */ #define _Swap32(v) do { \ v = (((v) & 0x000000FF) << 0x18) | \ (((v) & 0x0000FF00) << 0x08) | \ (((v) & 0x00FF0000) >> 0x08) | \ (((v) & 0xFF000000) >> 0x18); } while(0) #define _Swap16(v) do { \ v = (((v) & 0x00FF) << 0x08) | \ (((v) & 0xFF00) >> 0x08); } while (0) /* A normal read without any byte-swapping */ void stream_read(stream_t *stream, size_t size, void *buf) { stream->ci->read_filebuf(buf,size); if (stream->ci->curpos >= stream->ci->filesize) { stream->eof=1; } } int32_t stream_read_int32(stream_t *stream) { int32_t v; stream_read(stream, 4, &v); #ifdef ROCKBOX_LITTLE_ENDIAN _Swap32(v); #endif return v; } int32_t stream_tell(stream_t *stream) { return stream->ci->curpos; } uint32_t stream_read_uint32(stream_t *stream) { uint32_t v; stream_read(stream, 4, &v); #ifdef ROCKBOX_LITTLE_ENDIAN _Swap32(v); #endif return v; } int16_t stream_read_int16(stream_t *stream) { int16_t v; stream_read(stream, 2, &v); #ifdef ROCKBOX_LITTLE_ENDIAN _Swap16(v); #endif return v; } uint16_t stream_read_uint16(stream_t *stream) { uint16_t v; stream_read(stream, 2, &v); #ifdef ROCKBOX_LITTLE_ENDIAN _Swap16(v); #endif return v; } int8_t stream_read_int8(stream_t *stream) { int8_t v; stream_read(stream, 1, &v); return v; } uint8_t stream_read_uint8(stream_t *stream) { uint8_t v; stream_read(stream, 1, &v); return v; } void stream_skip(stream_t *stream, size_t skip) { stream->ci->advance_buffer(skip); } int stream_eof(stream_t *stream) { return stream->eof; } void stream_create(stream_t *stream,struct codec_api* ci) { stream->ci=ci; stream->eof=0; } /* This function was part of the original alac decoder implementation */ int get_sample_info(demux_res_t *demux_res, uint32_t samplenum, uint32_t *sample_duration, uint32_t *sample_byte_size) { unsigned int duration_index_accum = 0; unsigned int duration_cur_index = 0; if (samplenum >= demux_res->num_sample_byte_sizes) { return 0; } if (!demux_res->num_time_to_samples) { return 0; } while ((demux_res->time_to_sample[duration_cur_index].sample_count + duration_index_accum) <= samplenum) { duration_index_accum += demux_res->time_to_sample[duration_cur_index].sample_count; duration_cur_index++; if (duration_cur_index >= demux_res->num_time_to_samples) { return 0; } } *sample_duration = demux_res->time_to_sample[duration_cur_index].sample_duration; *sample_byte_size = demux_res->sample_byte_size[samplenum]; return 1; } unsigned int get_sample_offset(demux_res_t *demux_res, uint32_t sample) { uint32_t chunk = 1; uint32_t range_samples = 0; uint32_t total_samples = 0; uint32_t chunk_sample; uint32_t prev_chunk; uint32_t prev_chunk_samples; uint32_t file_offset; uint32_t i; /* First check we have the appropriate metadata - we should always * have it. */ if (sample >= demux_res->num_sample_byte_sizes || !demux_res->num_sample_to_chunks || !demux_res->num_chunk_offsets) { return 0; } /* Locate the chunk containing the sample */ prev_chunk = demux_res->sample_to_chunk[0].first_chunk; prev_chunk_samples = demux_res->sample_to_chunk[0].num_samples; for (i = 1; i < demux_res->num_sample_to_chunks; i++) { chunk = demux_res->sample_to_chunk[i].first_chunk; range_samples = (chunk - prev_chunk) * prev_chunk_samples; if (sample < total_samples + range_samples) { break; } total_samples += range_samples; prev_chunk = demux_res->sample_to_chunk[i].first_chunk; prev_chunk_samples = demux_res->sample_to_chunk[i].num_samples; } if (sample >= demux_res->sample_to_chunk[0].num_samples) { chunk = prev_chunk + (sample - total_samples) / prev_chunk_samples; } else { chunk = 1; } /* Get sample of the first sample in the chunk */ chunk_sample = total_samples + (chunk - prev_chunk) * prev_chunk_samples; /* Get offset in file */ if (chunk > demux_res->num_chunk_offsets) { file_offset = demux_res->chunk_offset[demux_res->num_chunk_offsets - 1]; } else { file_offset = demux_res->chunk_offset[chunk - 1]; } if (chunk_sample > sample) { return 0; } for (i = chunk_sample; i < sample; i++) { file_offset += demux_res->sample_byte_size[i]; } if (file_offset > demux_res->mdat_offset + demux_res->mdat_len) { return 0; } return file_offset; } /* Seek to the sample containing sound_sample_loc. Return 1 on success * (and modify sound_samples_done and current_sample), 0 if failed. * * Seeking uses the following arrays: * * 1) the time_to_sample array contains the duration (in sound samples) * of each sample of data. * * 2) the sample_byte_size array contains the length in bytes of each * sample. * * 3) the sample_to_chunk array contains information about which chunk * of samples each sample belongs to. * * 4) the chunk_offset array contains the file offset of each chunk. * * So find the sample number we are going to seek to (using time_to_sample) * and then find the offset in the file (using sample_to_chunk, * chunk_offset sample_byte_size, in that order.). * */ unsigned int alac_seek(demux_res_t* demux_res, stream_t* stream, uint32_t sound_sample_loc, uint32_t* sound_samples_done, int* current_sample) { uint32_t i; uint32_t j; uint32_t new_sample; uint32_t new_sound_sample; uint32_t new_pos; /* First check we have the appropriate metadata - we should always * have it. */ if ((demux_res->num_time_to_samples==0) || (demux_res->num_sample_byte_sizes==0)) { return 0; } /* Find the destination block from time_to_sample array */ i = 0; new_sample = 0; new_sound_sample = 0; while ((i < demux_res->num_time_to_samples) && (new_sound_sample < sound_sample_loc)) { j = (sound_sample_loc - new_sound_sample) / demux_res->time_to_sample[i].sample_duration; if (j <= demux_res->time_to_sample[i].sample_count) { new_sample += j; new_sound_sample += j * demux_res->time_to_sample[i].sample_duration; break; } else { new_sound_sample += (demux_res->time_to_sample[i].sample_duration * demux_res->time_to_sample[i].sample_count); new_sample += demux_res->time_to_sample[i].sample_count; i++; } } /* We know the new block, now calculate the file position. */ new_pos = get_sample_offset(demux_res, new_sample); /* We know the new file position, so let's try to seek to it */ if (stream->ci->seek_buffer(new_pos)) { *sound_samples_done = new_sound_sample; *current_sample = new_sample; return 1; } return 0; } /* Seek to the sample containing file_loc. Return 1 on success (and modify * sound_samples_done and current_sample), 0 if failed. * * Seeking uses the following arrays: * * 1) the chunk_offset array contains the file offset of each chunk. * * 2) the sample_to_chunk array contains information about which chunk * of samples each sample belongs to. * * 3) the sample_byte_size array contains the length in bytes of each * sample. * * 4) the time_to_sample array contains the duration (in sound samples) * of each sample of data. * * Locate the chunk containing location (using chunk_offset), find the * sample of that chunk (using sample_to_chunk) and finally the location * of that sample (using sample_byte_size). Then use time_to_sample to * calculate the sound_samples_done value. */ unsigned int alac_seek_raw(demux_res_t* demux_res, stream_t* stream, uint32_t file_loc, uint32_t* sound_samples_done, int* current_sample) { uint32_t chunk_sample = 0; uint32_t total_samples = 0; uint32_t new_sound_sample = 0; uint32_t new_pos; uint32_t chunk; uint32_t i; if (!demux_res->num_chunk_offsets || !demux_res->num_sample_to_chunks) { return 0; } /* Locate the chunk containing file_loc. */ for (i = 0; i < demux_res->num_chunk_offsets && file_loc < demux_res->chunk_offset[i]; i++) { } chunk = i + 1; new_pos = demux_res->chunk_offset[chunk - 1]; /* Get the first sample of the chunk. */ for (i = 1; i < demux_res->num_sample_to_chunks && chunk < demux_res->sample_to_chunk[i - 1].first_chunk; i++) { chunk_sample += demux_res->sample_to_chunk[i - 1].num_samples * (demux_res->sample_to_chunk[i].first_chunk - demux_res->sample_to_chunk[i - 1].first_chunk); } chunk_sample += (chunk - demux_res->sample_to_chunk[i - 1].first_chunk) * demux_res->sample_to_chunk[i - 1].num_samples; /* Get the position within the chunk. */ for (; chunk_sample < demux_res->num_sample_byte_sizes; chunk_sample++) { if (file_loc < new_pos + demux_res->sample_byte_size[chunk_sample]) { break; } new_pos += demux_res->sample_byte_size[chunk_sample]; } /* Get sound sample offset. */ for (i = 0; i < demux_res->num_time_to_samples; i++) { if (chunk_sample < total_samples + demux_res->time_to_sample[i].sample_count) { break; } total_samples += demux_res->time_to_sample[i].sample_count; new_sound_sample += demux_res->time_to_sample[i].sample_count * demux_res->time_to_sample[i].sample_duration; } new_sound_sample += (chunk_sample - total_samples) * demux_res->time_to_sample[i].sample_duration; /* Go to the new file position. */ if (stream->ci->seek_buffer(new_pos)) { *sound_samples_done = new_sound_sample; *current_sample = chunk_sample; return 1; } return 0; } #n257'>257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 Dave Chapman
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"

PLUGIN_HEADER

/* All swcodec targets have BUTTON_SELECT apart from the H10 */

#if CONFIG_KEYPAD == IRIVER_H10_PAD
#define TESTCODEC_EXITBUTTON BUTTON_RIGHT
#else
#define TESTCODEC_EXITBUTTON BUTTON_SELECT
#endif

static struct plugin_api* rb;

CACHE_FUNCTION_WRAPPERS(rb)

/* Log functions copied from test_disk.c */
static int line = 0;
static int max_line = 0;
static int log_fd = -1;
static char logfilename[MAX_PATH];

static bool log_init(bool use_logfile)
{
    int h;

    rb->lcd_setmargins(0, 0);
    rb->lcd_getstringsize("A", NULL, &h);
    max_line = LCD_HEIGHT / h;
    line = 0;
    rb->lcd_clear_display();
    rb->lcd_update();

    if (use_logfile) {
        rb->create_numbered_filename(logfilename, "/", "test_codec_log_", ".txt",
                                     2 IF_CNFN_NUM_(, NULL));
        log_fd = rb->open(logfilename, O_RDWR|O_CREAT|O_TRUNC);
        return log_fd >= 0;
    }

    return true;
}

static void log_text(char *text, bool advance)
{
    rb->lcd_puts(0, line, text);
    rb->lcd_update();
    if (advance)
    {
        if (++line >= max_line)
            line = 0;
        if (log_fd >= 0)
            rb->fdprintf(log_fd, "%s\n", text);
    }
}

static void log_close(void)
{
    if (log_fd >= 0)
        rb->close(log_fd);
}

struct wavinfo_t
{
  int fd;
  int samplerate;
  int channels;
  int sampledepth;
  int stereomode;
  int totalsamples;
};

static void* audiobuf;
static void* codec_mallocbuf;
static size_t audiosize;
static char str[MAX_PATH];

/* Our local implementation of the codec API */
static struct codec_api ci;

struct test_track_info {
    struct mp3entry id3;       /* TAG metadata */
    size_t filesize;           /* File total length */
};

static struct test_track_info track;
static bool taginfo_ready = true;

static volatile unsigned int elapsed;
static volatile bool codec_playing;
struct wavinfo_t wavinfo;

static unsigned char wav_header[44] =
{
    'R','I','F','F',     //  0 - ChunkID
     0,0,0,0,            //  4 - ChunkSize (filesize-8)
     'W','A','V','E',    //  8 - Format
     'f','m','t',' ',    // 12 - SubChunkID
     16,0,0,0,           // 16 - SubChunk1ID  // 16 for PCM
     1,0,                // 20 - AudioFormat (1=16-bit)
     0,0,                // 22 - NumChannels
     0,0,0,0,            // 24 - SampleRate in Hz
     0,0,0,0,            // 28 - Byte Rate (SampleRate*NumChannels*(BitsPerSample/8)
     0,0,                // 32 - BlockAlign (== NumChannels * BitsPerSample/8)
     16,0,               // 34 - BitsPerSample
     'd','a','t','a',    // 36 - Subchunk2ID
     0,0,0,0             // 40 - Subchunk2Size
};

static inline void int2le32(unsigned char* buf, int32_t x)
{
  buf[0] = (x & 0xff);
  buf[1] = (x & 0xff00) >> 8;
  buf[2] = (x & 0xff0000) >> 16;
  buf[3] = (x & 0xff000000) >>24;
}

static inline void int2le24(unsigned char* buf, int32_t x)
{
  buf[0] = (x & 0xff);
  buf[1] = (x & 0xff00) >> 8;
  buf[2] = (x & 0xff0000) >> 16;
}

static inline void int2le16(unsigned char* buf, int16_t x)
{
  buf[0] = (x & 0xff);
  buf[1] = (x & 0xff00) >> 8;
}

void init_wav(char* filename)
{
    wavinfo.totalsamples = 0;

    wavinfo.fd = rb->creat(filename);

    if (wavinfo.fd >= 0)
    {
        /* Write WAV header - we go back and fill in the details at the end */
        rb->write(wavinfo.fd, wav_header, sizeof(wav_header));
    }
}


void close_wav(void) {
  int filesize = rb->filesize(wavinfo.fd);
  int channels = (wavinfo.stereomode == STEREO_MONO) ? 1 : 2;
  int bps = 16; /* TODO */

  /* We assume 16-bit, Stereo */

  rb->lseek(wavinfo.fd,0,SEEK_SET);

  int2le32(wav_header+4, filesize-8); /* ChunkSize */

  int2le16(wav_header+22, channels);

  int2le32(wav_header+24, wavinfo.samplerate);

  int2le32(wav_header+28, wavinfo.samplerate * channels * (bps / 8)); /* ByteRate */

  int2le16(wav_header+32, channels * (bps / 8));

  int2le32(wav_header+40, filesize - 44);  /* Subchunk2Size */

  rb->write(wavinfo.fd, wav_header, sizeof(wav_header));

  rb->close(wavinfo.fd);
}

/* Returns buffer to malloc array. Only codeclib should need this. */
static void* get_codec_memory(size_t *size)
{
   DEBUGF("get_codec_memory(%d)\n",(int)size);
   *size = 512*1024;
   return codec_mallocbuf;
}

/* Null output */
static bool pcmbuf_insert_null(const void *ch1, const void *ch2, int count)
{
    /* Always successful - just discard data */
    (void)ch1;
    (void)ch2;
    (void)count;

    /* Prevent idle poweroff */
    rb->reset_poweroff_timer();

    return true;
}

/* 64KB should be enough */
static unsigned char wavbuffer[64*1024];

static inline int32_t clip_sample(int32_t sample)
{
    if ((int16_t)sample != sample)
        sample = 0x7fff ^ (sample >> 31);

    return sample;
}


/* WAV output */
static bool pcmbuf_insert_wav(const void *ch1, const void *ch2, int count)
{
    const int16_t* data1_16;
    const int16_t* data2_16;
    const int32_t* data1_32;
    const int32_t* data2_32;
    unsigned char* p = wavbuffer;
    const int scale = wavinfo.sampledepth - 15;
    const int dc_bias = 1 << (scale - 1);

    /* Prevent idle poweroff */
    rb->reset_poweroff_timer();

    if (wavinfo.sampledepth <= 16) {
        data1_16 = ch1;
        data2_16 = ch2;

        switch(wavinfo.stereomode)
        {
            case STEREO_INTERLEAVED:
                while (count--) {
                    int2le16(p,*data1_16++);
                    p += 2;
                    int2le16(p,*data1_16++);
                    p += 2;
                }
                break;
 
            case STEREO_NONINTERLEAVED:
                while (count--) {
                    int2le16(p,*data1_16++);
                    p += 2;
                    int2le16(p,*data2_16++);
                    p += 2;
                }

                break;
     
            case STEREO_MONO:
                while (count--) {
                    int2le16(p,*data1_16++);
                    p += 2;
                }
                break;
        }
    } else {
        data1_32 = ch1;
        data2_32 = ch2;

        switch(wavinfo.stereomode)
        {
            case STEREO_INTERLEAVED:
                while (count--) {
                    int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale));
                    p += 2;
                    int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale));
                    p += 2;
                }
                break;
 
            case STEREO_NONINTERLEAVED:
                while (count--) {
                    int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale));
                    p += 2;
                    int2le16(p, clip_sample((*data2_32++ + dc_bias) >> scale));
                    p += 2;
                }

                break;
     
            case STEREO_MONO:
                while (count--) {
                    int2le16(p, clip_sample((*data1_32++ + dc_bias) >> scale));
                    p += 2;
                }
                break;
        }
    }

    wavinfo.totalsamples += count;
    rb->write(wavinfo.fd, wavbuffer, p - wavbuffer);

    return true;
}