Add support for Sony OMA file format. Currently only supports ATRAC3 (without DRM...
[kugel-rb.git] / apps / codecs / atrac3_oma.c
blobb080b71524c747dc5c199e696822b09e93b7525e
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 /* The codec has nothing to do with RM, it just uses an RMContext struct to *
35 * store the data needs to be passed to the decoder initializing function. */
36 RMContext rmctx;
37 ATRAC3Context q IBSS_ATTR;
39 static void init_rm(RMContext *rmctx, struct mp3entry *id3)
41 rmctx->sample_rate = id3->frequency;
42 rmctx->nb_channels = 2;
43 rmctx->bit_rate = id3->bitrate;
44 rmctx->block_align = id3->bytesperframe;
46 /* 14-byte extra-data was faked in the metadata parser so that *
47 * the ATRAC3 decoder would parse it as WAV format extra-data. */
48 rmctx->extradata_size = 14;
49 memcpy(rmctx->codec_extradata, &id3->id3v1buf[0][0], 14);
52 /* this is the codec entry point */
53 enum codec_status codec_main(void)
55 static size_t buff_size;
56 int datasize, res, frame_counter, total_frames, seek_frame_offset;
57 uint8_t *bit_buffer;
58 int elapsed = 0;
59 size_t resume_offset = ci->id3->offset;
61 next_track:
62 if (codec_init()) {
63 DEBUGF("codec init failed\n");
64 return CODEC_ERROR;
66 while (!*ci->taginfo_ready && !ci->stop_codec)
67 ci->sleep(1);
69 codec_set_replaygain(ci->id3);
70 ci->memset(&rmctx,0,sizeof(RMContext));
71 ci->memset(&q,0,sizeof(ATRAC3Context));
73 init_rm(&rmctx, ci->id3);
75 ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
76 ci->configure(DSP_SET_SAMPLE_DEPTH, 17); /* Remark: atrac3 uses s15.0 by default, s15.2 was hacked. */
77 ci->configure(DSP_SET_STEREO_MODE, rmctx.nb_channels == 1 ?
78 STEREO_MONO : STEREO_NONINTERLEAVED);
80 res =atrac3_decode_init(&q, &rmctx);
81 if(res < 0) {
82 DEBUGF("failed to initialize atrac decoder\n");
83 return CODEC_ERROR;
86 /* check for a mid-track resume and force a seek time accordingly */
87 if(resume_offset > ci->id3->first_frame_offset) {
88 resume_offset -= ci->id3->first_frame_offset;
89 /* calculate resume_offset in frames */
90 resume_offset = (int)resume_offset / FRAMESIZE;
91 ci->seek_time = (int)resume_offset * ((FRAMESIZE * 8)/BITRATE);
93 total_frames = (ci->id3->filesize - ci->id3->first_frame_offset) / FRAMESIZE;
94 frame_counter = 0;
96 ci->set_elapsed(0);
97 ci->advance_buffer(ci->id3->first_frame_offset);
99 /* The main decoder loop */
100 seek_start :
101 while(frame_counter < total_frames)
103 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, FRAMESIZE);
105 ci->yield();
106 if (ci->stop_codec || ci->new_track)
107 goto done;
109 if (ci->seek_time) {
110 ci->set_elapsed(ci->seek_time);
112 /* Do not allow seeking beyond the file's length */
113 if ((unsigned) ci->seek_time > ci->id3->length) {
114 ci->seek_complete();
115 goto done;
118 /* Seek to the start of the track */
119 if (ci->seek_time == 1) {
120 ci->set_elapsed(0);
121 ci->seek_complete();
122 ci->seek_buffer(ci->id3->first_frame_offset);
123 elapsed = 0;
124 goto seek_start;
126 seek_frame_offset = (ci->seek_time * BITRATE) / (8 * FRAMESIZE);
127 frame_counter = seek_frame_offset;
128 ci->seek_buffer(ci->id3->first_frame_offset + seek_frame_offset* FRAMESIZE);
129 bit_buffer = (uint8_t *) ci->request_buffer(&buff_size, FRAMESIZE);
130 elapsed = ci->seek_time;
132 ci->set_elapsed(elapsed);
133 ci->seek_complete();
136 res = atrac3_decode_frame(&rmctx, &q, &datasize, bit_buffer, FRAMESIZE);
138 if(res != (int)FRAMESIZE) {
139 DEBUGF("codec error\n");
140 return CODEC_ERROR;
143 if(datasize)
144 ci->pcmbuf_insert(q.outSamples, q.outSamples + 1024, q.samples_per_frame / rmctx.nb_channels);
146 elapsed += (FRAMESIZE * 8) / BITRATE;
147 ci->set_elapsed(elapsed);
149 ci->advance_buffer(FRAMESIZE);
150 frame_counter++;
153 done:
154 if (ci->request_next_track())
155 goto next_track;
157 return CODEC_OK;