Having a default weak codec_main symbol doesn't seem to be working out for compiling...
[kugel-rb.git] / apps / codecs / atrac3_oma.c
blob50f7c8f163f7201de4fa9c6084ce870206f72f47
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Mohamed Tarek
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <string.h>
24 #include "logf.h"
25 #include "codeclib.h"
26 #include "inttypes.h"
27 #include "libatrac/atrac3.h"
29 CODEC_HEADER
31 #define FRAMESIZE ci->id3->bytesperframe
32 #define BITRATE ci->id3->bitrate
34 static ATRAC3Context q IBSS_ATTR;
36 /* this is the codec entry point */
37 enum codec_status codec_main(enum codec_entry_call_reason reason)
39 /* Nothing to do */
40 return CODEC_OK;
41 (void)reason;
44 /* this is called for each file to process */
45 enum codec_status codec_run(void)
47 static size_t buff_size;
48 int datasize, res, frame_counter, total_frames, seek_frame_offset;
49 uint8_t *bit_buffer;
50 int elapsed = 0;
51 size_t resume_offset;
52 intptr_t param;
53 enum codec_command_action action = CODEC_ACTION_NULL;
55 if (codec_init()) {
56 DEBUGF("codec init failed\n");
57 return CODEC_ERROR;
60 resume_offset = ci->id3->offset;
62 codec_set_replaygain(ci->id3);
63 ci->memset(&q,0,sizeof(ATRAC3Context));
65 ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
66 ci->configure(DSP_SET_SAMPLE_DEPTH, 17); /* Remark: atrac3 uses s15.0 by default, s15.2 was hacked. */
67 ci->configure(DSP_SET_STEREO_MODE, ci->id3->channels == 1 ?
68 STEREO_MONO : STEREO_NONINTERLEAVED);
70 ci->seek_buffer(0);
72 res = atrac3_decode_init(&q, ci->id3);
73 if(res < 0) {
74 DEBUGF("failed to initialize OMA atrac decoder\n");
75 return CODEC_ERROR;
78 total_frames = (ci->id3->filesize - ci->id3->first_frame_offset) / FRAMESIZE;
79 frame_counter = 0;
81 /* check for a mid-track resume and force a seek time accordingly */
82 if(resume_offset > ci->id3->first_frame_offset) {
83 resume_offset -= ci->id3->first_frame_offset;
84 /* calculate resume_offset in frames */
85 resume_offset = (int)resume_offset / FRAMESIZE;
86 param = (int)resume_offset * ((FRAMESIZE * 8)/BITRATE);
87 action = CODEC_ACTION_SEEK_TIME;
89 else {
90 ci->set_elapsed(0);
91 ci->seek_buffer(ci->id3->first_frame_offset);
94 /* The main decoder loop */
95 while(frame_counter < total_frames)
97 if (action == CODEC_ACTION_NULL)
98 action = ci->get_command(&param);
100 if (action == CODEC_ACTION_HALT)
101 break;
103 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, FRAMESIZE);
105 if (action == CODEC_ACTION_SEEK_TIME) {
106 /* Do not allow seeking beyond the file's length */
107 if ((unsigned) param > ci->id3->length) {
108 ci->set_elapsed(ci->id3->length);
109 ci->seek_complete();
110 break;
113 /* Seek to the start of the track */
114 if (param == 0) {
115 elapsed = 0;
116 ci->set_elapsed(0);
117 ci->seek_buffer(ci->id3->first_frame_offset);
118 ci->seek_complete();
119 action = CODEC_ACTION_NULL;
120 continue;
123 seek_frame_offset = (param * BITRATE) / (8 * FRAMESIZE);
124 frame_counter = seek_frame_offset;
125 ci->seek_buffer(ci->id3->first_frame_offset + seek_frame_offset* FRAMESIZE);
126 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, FRAMESIZE);
127 elapsed = param;
128 ci->set_elapsed(elapsed);
129 ci->seek_complete();
132 action = CODEC_ACTION_NULL;
134 res = atrac3_decode_frame(FRAMESIZE, &q, &datasize, bit_buffer, FRAMESIZE);
136 if(res != (int)FRAMESIZE) {
137 DEBUGF("codec error\n");
138 return CODEC_ERROR;
141 if(datasize)
142 ci->pcmbuf_insert(q.outSamples, q.outSamples + 1024,
143 q.samples_per_frame / ci->id3->channels);
145 elapsed += (FRAMESIZE * 8) / BITRATE;
146 ci->set_elapsed(elapsed);
148 ci->advance_buffer(FRAMESIZE);
149 frame_counter++;
152 return CODEC_OK;