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 | * |
| 12 | * All files in this archive are subject to the GNU General Public License. |
| 13 | * See the file COPYING in the source tree root for full license agreement. |
| 14 | * |
| 15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 16 | * KIND, either express or implied. |
| 17 | * |
| 18 | ****************************************************************************/ |
| 19 | |
Thom Johansen | c91e0bb | 2005-10-13 11:32:52 +0000 | [diff] [blame] | 20 | #include "codeclib.h" |
Dave Chapman | 45f9e5d | 2005-10-29 17:12:52 +0000 | [diff] [blame] | 21 | #include "libm4a/m4a.h" |
| 22 | #include "libalac/decomp.h" |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 23 | |
Jens Arnold | b8749fd | 2006-01-18 00:05:14 +0000 | [diff] [blame] | 24 | CODEC_HEADER |
| 25 | |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 26 | int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE] IBSS_ATTR; |
Dave Chapman | 45f9e5d | 2005-10-29 17:12:52 +0000 | [diff] [blame] | 27 | |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 28 | /* this is the codec entry point */ |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 29 | enum codec_status codec_main(void) |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 30 | { |
| 31 | size_t n; |
| 32 | demux_res_t demux_res; |
Dave Chapman | 45f9e5d | 2005-10-29 17:12:52 +0000 | [diff] [blame] | 33 | stream_t input_stream; |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 34 | uint32_t samplesdone; |
| 35 | uint32_t elapsedtime; |
| 36 | uint32_t sample_duration; |
| 37 | uint32_t sample_byte_size; |
Dave Chapman | f1844c4 | 2005-10-28 00:11:28 +0000 | [diff] [blame] | 38 | int samplesdecoded; |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 39 | unsigned int i; |
| 40 | unsigned char* buffer; |
| 41 | alac_file alac; |
Brandon Low | 1060e44 | 2006-01-18 20:22:03 +0000 | [diff] [blame] | 42 | int retval; |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 43 | |
| 44 | /* Generic codec initialisation */ |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 45 | ci->configure(CODEC_SET_FILEBUF_WATERMARK, 1024*512); |
| 46 | ci->configure(CODEC_SET_FILEBUF_CHUNKSIZE, 1024*128); |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 47 | |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 48 | ci->configure(DSP_SET_STEREO_MODE, STEREO_NONINTERLEAVED); |
| 49 | ci->configure(DSP_SET_SAMPLE_DEPTH, ALAC_OUTPUT_DEPTH-1); |
Dave Chapman | fca6b63 | 2005-10-30 22:06:42 +0000 | [diff] [blame] | 50 | |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 51 | next_track: |
| 52 | |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 53 | if (codec_init()) { |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 54 | LOGF("ALAC: Error initialising codec\n"); |
Brandon Low | 1060e44 | 2006-01-18 20:22:03 +0000 | [diff] [blame] | 55 | retval = CODEC_ERROR; |
| 56 | goto exit; |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Magnus Holmgren | af05296 | 2006-11-23 20:05:14 +0000 | [diff] [blame] | 59 | while (!*ci->taginfo_ready && !ci->stop_codec) |
| 60 | ci->sleep(1); |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 61 | |
Michael Sevakis | 97f369a | 2007-02-10 16:34:16 +0000 | [diff] [blame] | 62 | ci->configure(DSP_SWITCH_FREQUENCY, ci->id3->frequency); |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 63 | codec_set_replaygain(ci->id3); |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 64 | |
Dave Chapman | 45f9e5d | 2005-10-29 17:12:52 +0000 | [diff] [blame] | 65 | stream_create(&input_stream,ci); |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 66 | |
| 67 | /* if qtmovie_read returns successfully, the stream is up to |
| 68 | * the movie data, which can be used directly by the decoder */ |
| 69 | if (!qtmovie_read(&input_stream, &demux_res)) { |
| 70 | LOGF("ALAC: Error initialising file\n"); |
Brandon Low | 1060e44 | 2006-01-18 20:22:03 +0000 | [diff] [blame] | 71 | retval = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 72 | goto done; |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 75 | /* initialise the sound converter */ |
Magnus Holmgren | d63d8fe | 2006-10-11 17:12:29 +0000 | [diff] [blame] | 76 | create_alac(demux_res.sound_sample_size, demux_res.num_channels,&alac); |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 77 | alac_set_info(&alac, demux_res.codecdata); |
| 78 | |
| 79 | i=0; |
| 80 | samplesdone=0; |
| 81 | /* The main decoding loop */ |
| 82 | while (i < demux_res.num_sample_byte_sizes) { |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 83 | ci->yield(); |
Brandon Low | ebadcc6 | 2006-04-15 02:03:11 +0000 | [diff] [blame] | 84 | if (ci->stop_codec || ci->new_track) { |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 85 | break; |
| 86 | } |
| 87 | |
| 88 | /* Deal with any pending seek requests */ |
| 89 | if (ci->seek_time) { |
Daniel Stenberg | 76667e2 | 2005-12-02 08:42:48 +0000 | [diff] [blame] | 90 | if (alac_seek(&demux_res, &input_stream, |
Thom Johansen | c8193b8 | 2005-11-06 19:18:04 +0000 | [diff] [blame] | 91 | ((ci->seek_time-1)/10) * (ci->id3->frequency/100), |
Daniel Stenberg | 76667e2 | 2005-12-02 08:42:48 +0000 | [diff] [blame] | 92 | &samplesdone, (int *)&i)) { |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 93 | elapsedtime=(samplesdone*10)/(ci->id3->frequency/100); |
| 94 | ci->set_elapsed(elapsedtime); |
| 95 | } |
Dave Chapman | 5006d15 | 2005-11-02 00:09:42 +0000 | [diff] [blame] | 96 | ci->seek_complete(); |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* Lookup the length (in samples and bytes) of block i */ |
| 100 | if (!get_sample_info(&demux_res, i, &sample_duration, |
| 101 | &sample_byte_size)) { |
| 102 | LOGF("ALAC: Error in get_sample_info\n"); |
Brandon Low | 1060e44 | 2006-01-18 20:22:03 +0000 | [diff] [blame] | 103 | retval = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 104 | goto done; |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* Request the required number of bytes from the input buffer */ |
| 108 | |
Brandon Low | c76904b | 2006-03-24 14:02:27 +0000 | [diff] [blame] | 109 | buffer=ci->request_buffer(&n,sample_byte_size); |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 110 | if (n!=sample_byte_size) { |
Brandon Low | 1060e44 | 2006-01-18 20:22:03 +0000 | [diff] [blame] | 111 | retval = CODEC_ERROR; |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 112 | goto done; |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* Decode one block - returned samples will be host-endian */ |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 116 | ci->yield(); |
| 117 | samplesdecoded=alac_decode_frame(&alac, buffer, outputbuffer, ci->yield); |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 118 | |
Dave Chapman | 0c91551 | 2006-04-13 17:04:53 +0000 | [diff] [blame] | 119 | /* Advance codec buffer n bytes */ |
Dave Chapman | 29c7da9 | 2005-10-30 21:04:32 +0000 | [diff] [blame] | 120 | ci->advance_buffer(n); |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 121 | |
| 122 | /* Output the audio */ |
Tomasz Malesinski | 80da8b1 | 2006-11-26 18:31:41 +0000 | [diff] [blame] | 123 | ci->yield(); |
Michael Sevakis | aba6ca0 | 2007-02-07 00:51:50 +0000 | [diff] [blame] | 124 | ci->pcmbuf_insert(outputbuffer[0], outputbuffer[1], samplesdecoded); |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 125 | |
| 126 | /* Update the elapsed-time indicator */ |
| 127 | samplesdone+=sample_duration; |
| 128 | elapsedtime=(samplesdone*10)/(ci->id3->frequency/100); |
| 129 | ci->set_elapsed(elapsedtime); |
| 130 | |
| 131 | /* Keep track of current position - for resuming */ |
| 132 | ci->set_offset(elapsedtime); |
| 133 | |
| 134 | i++; |
| 135 | } |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 136 | retval = CODEC_OK; |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 137 | |
Brandon Low | f3bc1ef | 2006-04-22 14:40:13 +0000 | [diff] [blame] | 138 | done: |
Magnus Holmgren | 01a010f | 2007-03-18 09:50:53 +0000 | [diff] [blame] | 139 | LOGF("ALAC: Decoded %ld samples\n",samplesdone); |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 140 | |
| 141 | if (ci->request_next_track()) |
| 142 | goto next_track; |
| 143 | |
Brandon Low | 1060e44 | 2006-01-18 20:22:03 +0000 | [diff] [blame] | 144 | exit: |
| 145 | return retval; |
Dave Chapman | 139c1cb | 2005-09-22 21:55:37 +0000 | [diff] [blame] | 146 | } |