6fb390de61af00d545d4d937e6668ccaf7a1a398
[processor-sdk/performance-audio-sr.git] / processor_audio_sdk_1_00_00_00 / pasdk / test_arm / framework / aspDecOpCircBuf_slave.c
2 /*
3 Copyright (c) 2016, Texas Instruments Incorporated - http://www.ti.com/
4 All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
36 #include <string.h> // for memset
37 #include <xdc/std.h>
38 #include <ti/sysbios/hal/Cache.h>
39 #include <xdc/runtime/Log.h>
41 #include "common.h"
42 #include "paftyp.h"
43 //#include "pafdec.h"
44 //#include "pafsp.h"
45 #include "aspDecOpCircBuf_slave.h"
47 // Start writes to circular buffer
48 Int cbWriteStart(
49 PAF_AST_DecOpCircBuf *pCb // decoder output circular buffer
50 )
51 {
52 PAF_AudioFrame *pAfCb;
53 Int8 n;
54 //Int8 i;
56 // (***) FL: revisit
57 // Invalidate circular buffer configuration.
58 // NOTE: Probably only a subset of this information needs to be updated.
59 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
60 Cache_wait();
62 // Invalidate AF circular buffer
63 Cache_inv(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
64 for (n=0; n<pCb->maxNumAfCb; n++)
65 {
66 pAfCb = &pCb->afCb[n];
67 Cache_inv(pAfCb->data.sample, ASP_DECOP_CB_MAX_NUM_PCM_CH*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0);
68 // FL: unnecessary since part of AF
69 //for (i=0; i<PAF_MAX_NUM_PRIVATE_MD; i++) //QIN
70 //{
71 // Cache_inv(&pAfCb->pafPrivateMetadata[i], sizeof(PAF_PrivateMetadata), Cache_Type_ALLD, 0);
72 //}
73 }
74 Cache_wait();
76 // update flags
77 pCb->writerActiveFlag = 1;
78 pCb->emptyFlag = 0;
80 // (***) FL: revisit
81 // Write back circular buffer configuration
82 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
83 Cache_wait();
85 return ASP_DECOP_CB_SOK;
86 };
88 // Stop writes to circular buffer
89 Int cbWriteStop(
90 PAF_AST_DecOpCircBuf *pCb // decoder output circular buffer
91 )
92 {
93 // Invalidate circular buffer configuration
94 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
95 Cache_wait();
97 // update flags
98 pCb->writerActiveFlag = 0;
99 pCb->emptyFlag = 1;
101 // (***) FL: revisit
102 // Write back circular buffer configuration
103 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
104 Cache_wait();
106 return ASP_DECOP_CB_SOK;
107 }
109 // Write audio frame to circular buffer
110 Int cbWriteAf(
111 PAF_AST_DecOpCircBuf *pCb, // decoder output circular buffer
112 PAF_AudioFrame *pAfWrt // audio frame from which to write
113 )
114 {
115 PAF_AudioFrame *pAfCb;
116 PAF_ChannelMask_HD streamMask;
117 Int8 i;
118 Int16 j;
120 // (***) FL: revisit
121 // Invalidate circular buffer configuration.
122 // NOTE: Probably only a subset of this information nexeds to be updated.
123 Cache_inv(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
124 Cache_wait();
126 if (pCb->readerActiveFlag == 1)
127 {
128 //
129 // Normal case, reader active.
130 // If reader not active, don't write to circular buffer or check OVRflow.
132 #if 0
133 if (pCb->cbWriteAfInit == 0)
134 {
135 // Invalidate AF circular buffer
136 Cache_inv(pCb->afCb, pCb->maxNumAfCb*sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
137 for (n=0; n<pCb->maxNumAfCb; n++)
138 {
139 pAfCb = &pCb->afCb[n];
140 Cache_inv(pAfCb->data.sample, ASP_DECOP_CB_MAX_NUM_PCM_CH*sizeof(PAF_AudioData *), Cache_Type_ALLD, 0);
141 }
142 Cache_wait();
144 pCb->cbWriteAfInit = 1;
145 }
146 #endif
148 // check overflow
149 if (pCb->numAfCb >= pCb->maxNumAfCb)
150 {
151 pCb->errOvrCnt++;
152 //SW_BREAKPOINT;
153 Log_info1("cbWriteAf: ERROR: overflow, numAfCb=%d", pCb->numAfCb);
154 return ASP_DECOP_CB_WRITE_OVERFLOW;
155 }
157 // get pointer to current audio frame in circular buffer
158 pAfCb = &pCb->afCb[pCb->afWrtIdx];
160 // write audio frame information updated by decoder
161 pAfCb->sampleDecode = pAfWrt->sampleDecode;
162 PAF_PROCESS_COPY(pAfCb->sampleProcess, pAfWrt->sampleProcess);
163 pAfCb->sampleRate = pAfWrt->sampleRate;
164 pAfCb->sampleCount = pAfWrt->sampleCount;
165 pAfCb->channelConfigurationRequest = pAfWrt->channelConfigurationRequest;
166 pAfCb->channelConfigurationStream = pAfWrt->channelConfigurationStream;
167 // write metadata information updated by decoder //QIN
168 pAfCb->bsMetadata_type = pAfWrt->bsMetadata_type; /* non zero if metadata is attached. */
169 pAfCb->pafBsMetadataUpdate = pAfWrt->pafBsMetadataUpdate; /* indicates whether bit-stream metadata update */
170 pAfCb->numPrivateMetadata = pAfWrt->numPrivateMetadata; /* number of valid private metadata (0 or 1 if metadata filtering enabled) */
171 pAfCb->bsMetadata_offset = pAfWrt->bsMetadata_offset; /* offset into audio frame for change in bsMetadata_type field */
172 // write PCM samples
173 streamMask = pAfWrt->fxns->channelMask(pAfWrt, pAfCb->channelConfigurationStream);
174 for (i = 0; i < ASP_DECOP_CB_MAX_NUM_PCM_CH; i++)
175 {
176 if ((streamMask >> i) & 0x1)
177 {
178 for (j = 0; j < pCb->decOpFrameLen; j++)
179 {
180 pAfCb->data.sample[i][j] = pAfWrt->data.sample[i][j];
181 }
183 pAfCb->data.samsiz[i] = pAfWrt->data.samsiz[i];
184 }
185 }
187 // Write metadata to circular buffer
188 for (i = 0; i < pAfCb->numPrivateMetadata; i++) // FL: only copy numPrivateMetadata
189 {
190 pAfCb->pafPrivateMetadata[i].offset = pAfWrt->pafPrivateMetadata[i].offset;
191 pAfCb->pafPrivateMetadata[i].size = pAfWrt->pafPrivateMetadata[i].size;
192 memcpy(pAfCb->pafPrivateMetadata[i].pMdBuf, pAfWrt->pafPrivateMetadata[i].pMdBuf, pAfWrt->pafPrivateMetadata[i].size);
193 }
195 // update audio frame write index
196 pCb->afWrtIdx++;
197 if (pCb->afWrtIdx >= pCb->maxNumAfCb)
198 {
199 pCb->afWrtIdx = 0;
200 }
202 // update number of audio frames in circular buffer
203 pCb->numAfCb++;
205 // (***) FL: revisit
206 // Write back circular buffer configuration
207 Cache_wb(pCb, sizeof(PAF_AST_DecOpCircBuf), Cache_Type_ALLD, 0);
208 // write back audio frame
209 Cache_wb(pAfCb, sizeof(PAF_AudioFrame), Cache_Type_ALLD, 0);
210 Cache_wb(pAfCb->data.samsiz, ASP_DECOP_CB_MAX_NUM_PCM_CH*sizeof(PAF_AudioSize), Cache_Type_ALLD, 0);
211 // write back PCM data
212 for (i = 0; i < ASP_DECOP_CB_MAX_NUM_PCM_CH; i++)
213 {
214 if ((streamMask >> i) & 0x1)
215 {
216 Cache_wb(pAfCb->data.sample[i], pCb->decOpFrameLen*sizeof(PAF_AudioData), Cache_Type_ALLD, 0);
217 }
218 }
220 // write back private metadata // QIN
221 for (i=0; i<pAfCb->numPrivateMetadata; i++) // FL: only write back numPrivateMetadata
222 {
223 //Cache_wb(&pAfCb->pafPrivateMetadata[i], sizeof(PAF_PrivateMetadata), Cache_Type_ALLD, 0); // FL: unnecessary since part of AF
224 Cache_wb(pAfCb->pafPrivateMetadata[i].pMdBuf, pAfCb->pafPrivateMetadata[i].size, Cache_Type_ALLD, 0);
225 }
226 Cache_wait();
227 }
229 return ASP_DECOP_CB_SOK;
230 }
232 // Get next audio frame to write in circular buffer
233 Int cbGetNextWriteAf(
234 PAF_AST_DecOpCircBuf *pCb, // decoder output circular buffer
235 PAF_AudioFrame **ppAfWrt // audio frame next to be written
236 )
237 {
238 // get pointer to current audio frame in circular buffer
239 *ppAfWrt = &pCb->afCb[pCb->afWrtIdx];
241 // update audio frame write index
242 pCb->afWrtIdx++;
243 if (pCb->afWrtIdx > pCb->maxNumAfCb)
244 {
245 pCb->afWrtIdx = 0;
246 }
248 return ASP_DECOP_CB_SOK;
249 }