Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 Dave Chapman |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
Thom Johansen | c91e0bb | 2005-10-13 11:32:52 +0000 | [diff] [blame] | 22 | #include "codeclib.h" |
Dave Chapman | 45f9e5d | 2005-10-29 17:12:52 +0000 | [diff] [blame] | 23 | #include "libm4a/m4a.h" |
| 24 | #include "libalac/decomp.h" |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 25 | |
Jens Arnold | b8749fd | 2006-01-18 00:05:14 +0000 | [diff] [blame] | 26 | CODEC_HEADER |
| 27 | |
Andree Buschmann | 68199cc | 2011-04-18 19:12:51 +0000 | [diff] [blame] | 28 | /* The maximum buffer size handled. This amount of bytes is buffered for each |
| 29 | * frame. */ |
| 30 | #define ALAC_BYTE_BUFFER_SIZE 32768 |
| 31 | |
Nils Wallménius | 6325ef9 | 2010-07-25 22:24:53 +0000 | [diff] [blame] | 32 | static int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE] IBSS_ATTR; |
Dave Chapman | 45f9e5d | 2005-10-29 17:12:52 +0000 | [diff] [blame] | 33 | |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 34 | /* this is the codec entry point */ |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 35 | enum codec_status codec_main(enum codec_entry_call_reason reason) |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 36 | { |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 37 | if (reason == CODEC_LOAD) { |
| 38 | /* Generic codec initialisation */ |
| 39 | ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED); |
| 40 | ci->configure(DSP_SET_SAMPLE_DEPTH, ALAC_OUTPUT_DEPTH-1); |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 43 | return CODEC_OK; |
| 44 | } |
| 45 | |
| 46 | /* this is called for each file to process */ |
| 47 | enum codec_status codec_run(void) |
| 48 | { |
| 49 | size_t n; |
| 50 | demux_res_t demux_res; |
| 51 | stream_t input_stream; |
| 52 | uint32_t samplesdone; |
| 53 | uint32_t elapsedtime = 0; |
| 54 | int samplesdecoded; |
| 55 | unsigned int i; |
| 56 | unsigned char* buffer; |
| 57 | alac_file alac; |
| 58 | intptr_t param; |
| 59 | |
| 60 | /* Clean and initialize decoder structures */ |
| 61 | memset(&demux_res , 0, sizeof(demux_res)); |
| 62 | if (codec_init()) { |
| 63 | LOGF("ALAC: Error initialising codec\n"); |
| 64 | return CODEC_ERROR; |
| 65 | } |
| 66 | |
| 67 | ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); |
| 68 | codec_set_replaygain(ci->id3); |
| 69 | |
| 70 | ci->seek_buffer(0); |
| 71 | |
| 72 | stream_create(&input_stream,ci); |
| 73 | |
| 74 | /* Read from ci->id3->offset before calling qtmovie_read. */ |
| 75 | samplesdone = (uint32_t)(((uint64_t)(ci->id3->offset) * ci->id3->frequency) / |
| 76 | (ci->id3->bitrate*128)); |
| 77 | |
| 78 | /* if qtmovie_read returns successfully, the stream is up to |
| 79 | * the movie data, which can be used directly by the decoder */ |
| 80 | if (!qtmovie_read(&input_stream, &demux_res)) { |
| 81 | LOGF("ALAC: Error initialising file\n"); |
| 82 | return CODEC_ERROR; |
| 83 | } |
| 84 | |
| 85 | /* initialise the sound converter */ |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 86 | alac_set_info(&alac, demux_res.codecdata); |
| 87 | |
| 88 | /* Set i for first frame, seek to desired sample position for resuming. */ |
| 89 | i=0; |
| 90 | if (samplesdone > 0) { |
| 91 | if (m4a_seek(&demux_res, &input_stream, samplesdone, |
| 92 | &samplesdone, (int*) &i)) { |
| 93 | elapsedtime = (samplesdone * 10) / (ci->id3->frequency / 100); |
| 94 | ci->set_elapsed(elapsedtime); |
| 95 | } else { |
| 96 | samplesdone = 0; |
| 97 | } |
| 98 | } |
| 99 | |
Michael Sevakis | 7ad2cad | 2011-08-28 07:45:35 +0000 | [diff] [blame^] | 100 | ci->set_elapsed(elapsedtime); |
| 101 | |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 102 | /* The main decoding loop */ |
| 103 | while (i < demux_res.num_sample_byte_sizes) { |
| 104 | enum codec_command_action action = ci->get_command(¶m); |
| 105 | |
| 106 | if (action == CODEC_ACTION_HALT) |
| 107 | break; |
| 108 | |
| 109 | /* Request the required number of bytes from the input buffer */ |
| 110 | buffer=ci->request_buffer(&n, ALAC_BYTE_BUFFER_SIZE); |
| 111 | |
| 112 | /* Deal with any pending seek requests */ |
| 113 | if (action == CODEC_ACTION_SEEK_TIME) { |
| 114 | if (m4a_seek(&demux_res, &input_stream, |
| 115 | (param/10) * (ci->id3->frequency/100), |
| 116 | &samplesdone, (int *)&i)) { |
| 117 | elapsedtime=(samplesdone*10)/(ci->id3->frequency/100); |
| 118 | } |
| 119 | ci->set_elapsed(elapsedtime); |
| 120 | ci->seek_complete(); |
| 121 | } |
| 122 | |
| 123 | /* Request the required number of bytes from the input buffer */ |
| 124 | buffer=ci->request_buffer(&n, ALAC_BYTE_BUFFER_SIZE); |
| 125 | |
| 126 | /* Decode one block - returned samples will be host-endian */ |
| 127 | samplesdecoded=alac_decode_frame(&alac, buffer, outputbuffer, ci->yield); |
| 128 | ci->yield(); |
| 129 | |
| 130 | /* Advance codec buffer by amount of consumed bytes */ |
| 131 | ci->advance_buffer(alac.bytes_consumed); |
| 132 | |
| 133 | /* Output the audio */ |
| 134 | ci->pcmbuf_insert(outputbuffer[0], outputbuffer[1], samplesdecoded); |
| 135 | |
| 136 | /* Update the elapsed-time indicator */ |
| 137 | samplesdone+=samplesdecoded; |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 138 | elapsedtime=(samplesdone*10)/(ci->id3->frequency/100); |
| 139 | ci->set_elapsed(elapsedtime); |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 140 | |
| 141 | i++; |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Michael Sevakis | c537d59 | 2011-04-27 03:08:23 +0000 | [diff] [blame] | 144 | LOGF("ALAC: Decoded %lu samples\n",(unsigned long)samplesdone); |
| 145 | return CODEC_OK; |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 146 | } |