summaryrefslogtreecommitdiffstats
path: root/lib/rbcodec/codecs/tta.c
blob: 60481c9f2890ad85e20f9bee74aa426c143efb29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 Yoshihisa Uchida
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "codeclib.h"
#include "codecs/libtta/ttalib.h"

CODEC_HEADER

/*
 * TTA (True Audio) codec:
 *
 * References
 * [1] TRUE AUDIO CODEC SOFTWARE http://true-audio.com/
 */

static int32_t samples[PCM_BUFFER_LENGTH * 2] IBSS_ATTR;

/* this is the codec entry point */
enum codec_status codec_main(enum codec_entry_call_reason reason)
{
    if (reason == CODEC_LOAD) {
        /* Generic codec initialisation */
        ci->configure(DSP_SET_SAMPLE_DEPTH, TTA_OUTPUT_DEPTH - 1);
    }

    return CODEC_OK;
}

/* this is called for each file to process */
enum codec_status codec_run(void)
{
    tta_info info;
    unsigned int decodedsamples;
    int endofstream;
    int new_pos = 0;
    int sample_count;
    intptr_t param;
  
    if (codec_init())
    {
        DEBUGF("codec_init() error\n");
        return CODEC_ERROR;
    }

    ci->seek_buffer(0);

    if (set_tta_info(&info) < 0 || player_init(&info) < 0)
        return CODEC_ERROR;

    codec_set_replaygain(ci->id3);

    ci->configure(DSP_SET_FREQUENCY, ci->id3->frequency);
    if (info.NCH == 2) {
        ci->configure(DSP_SET_STEREO_MODE, STEREO_INTERLEAVED);
    } else if (info.NCH == 1) {
        ci->configure(DSP_SET_STEREO_MODE, STEREO_MONO);
    } else {
        DEBUGF("CODEC_ERROR: more than 2 channels\n");
        player_stop();
        return CODEC_ERROR;
    }

    /* The main decoder loop */
    decodedsamples = 0;
    endofstream = 0;

    if (ci->id3->offset || ci->id3->elapsed)
    {
        /* Need to save offset for later use (cleared indirectly by
           advance_buffer) */
        unsigned int pos = ci->id3->offset;
        enum tta_seek_type type = TTA_SEEK_POS;

        if (!pos) {
            pos = ci->id3->elapsed / SEEK_STEP;
            type = TTA_SEEK_TIME;
        }

        new_pos = set_position(pos, type);

        if (new_pos >= 0)
            decodedsamples = new_pos;
    }

    ci->set_elapsed((uint64_t)info.LENGTH * 1000 * decodedsamples / info.DATALENGTH);

    while (!endofstream)
    {
        long action = ci->get_command(&param);

        if (action == CODEC_ACTION_HALT)
            break;

        if (action == CODEC_ACTION_SEEK_TIME)
        {
            new_pos = set_position(param / SEEK_STEP, TTA_SEEK_TIME);
            if (new_pos >= 0)
            {
                decodedsamples = new_pos;
            }

            ci->set_elapsed((uint64_t)info.LENGTH * 1000 * decodedsamples / info.DATALENGTH);
            ci->seek_complete();
        }

        sample_count = get_samples(samples);
        if (sample_count < 0)
            break;
 
        ci->pcmbuf_insert(samples, NULL, sample_count);
        decodedsamples += sample_count;
        if (decodedsamples >= info.DATALENGTH)
            endofstream = 1;
        ci->set_elapsed((uint64_t)info.LENGTH * 1000 * decodedsamples / info.DATALENGTH);
    }

    player_stop();
    return CODEC_OK;
}