summaryrefslogtreecommitdiff
path: root/libaac/aacDecode.cpp
blob: 44296b653e402a4f6de00bed1f7e1a7d2cab0af3 (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
/*
 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *     * Neither the name of The Linux Foundation nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * */

#include "aacDecode.h"
#include "aacdecoder_lib.h"
#include <utils/Log.h>
#include <string.h>

aacDecode::aacDecode() {
    p_aacHandle = NULL;
    p_aacInfo = NULL;
    memset(&s_aacConfig, 0, sizeof(s_aacConfig));
}

aacDecode::~aacDecode() {
    if(!p_aacHandle) {
        return;
    }

    aacDecoder_Close((HANDLE_AACDECODER)p_aacHandle);
    p_aacHandle = NULL;
}

bool aacDecode::aacConfigure(aacConfigType * p_aacConfig) {
    if(!p_aacConfig) {
        return false;
    }

    memcpy(&s_aacConfig, p_aacConfig, sizeof(s_aacConfig));

    p_aacHandle = aacDecoder_Open(TT_MP4_ADTS, 1);

    if(!p_aacHandle) {
        ALOGE("Failed to open AAC decoder");
        return false;
    }

    p_aacInfo = (void*)aacDecoder_GetStreamInfo((HANDLE_AACDECODER)p_aacHandle);

    if(!p_aacInfo) {
        ALOGE("Failed to get stream info");
        return false;
    }

    /* Configure AAC decoder */

    aacDecoder_SetParam((HANDLE_AACDECODER)p_aacHandle,
                        AAC_DRC_REFERENCE_LEVEL,
                        64);

    aacDecoder_SetParam((HANDLE_AACDECODER)p_aacHandle,
                        AAC_DRC_ATTENUATION_FACTOR,
                        127);

    aacDecoder_SetParam((HANDLE_AACDECODER)p_aacHandle,
                        AAC_PCM_MAX_OUTPUT_CHANNELS,
                        s_aacConfig.n_channels > 6 ? -1 : s_aacConfig.n_channels);
    return true;
}

bool aacDecode::aacDecodeFrame(unsigned char* p_buffer, unsigned int n_size) {
    if(!p_buffer || n_size < 7) {
        ALOGE("No/Incorrect buffer provided for AAC decoder");
        return false;
    }

    if(!p_aacHandle) {
        ALOGE("Decoder handle not available");
        return false;
    }

    AAC_DECODER_ERROR err = AAC_DEC_UNKNOWN;

    UINT nSize[] = {(UINT)n_size};
    UINT nOcc[] = {(UINT)n_size};
    UCHAR* pBuf[] = {p_buffer};

    err = aacDecoder_Fill((HANDLE_AACDECODER)p_aacHandle, pBuf, nSize, nOcc);

    if(err != AAC_DEC_OK || nOcc[0] != 0) {
        ALOGE("Error in aacDecoder_Fill");
        return false;
    }

    /* Fix the alloc length being passed to AAC Decoder since it expects INT_PCM
       as input buffer but we have it as UINT8. INT_PCM is of type SHORT */

    int factor = sizeof(INT_PCM)/sizeof(unsigned char);
    int allocLen = (factor == 0) ? n_size : n_size/factor;

    err = aacDecoder_DecodeFrame((HANDLE_AACDECODER)p_aacHandle,
                                  (INT_PCM*)p_buffer, allocLen, 0);

    if(err != AAC_DEC_OK) {
        ALOGE("Failed to decode frame");
        return false;
    }
    return true;
}