summaryrefslogtreecommitdiff
path: root/hifi/xaf/host-apf/utest/xaf-dec-test.c
blob: b83a0f906de4bb76aa889106c30f96eae4900324 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*******************************************************************************
* Copyright (C) 2018 Cadence Design Systems, Inc.
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to use this Software with Cadence processor cores only and 
* not with any other processors and platforms, subject to
* the following conditions:
* 
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************/

#define MODULE_TAG                      UTEST

#include "xaf-utils-test.h"

#include "audio/xa_vorbis_dec_api.h"
#include "audio/xa-audio-decoder-api.h"

#include "xa_playback.h"

#define PRINT_USAGE do { fprintf(stderr, "\nUsage: %s <input-file>\n", argv[0]); \
                         fprintf(stderr, "       Only .ogg and .pcm files are supported. \n"); \
                         fprintf(stderr, "       Playback is configured @ 48kHz, 2 ch, 16 bits pcm. \n"); \
                         fprintf(stderr, "       pcm output is written to dec-out.pcm, by default. \n\n"); \
                       } while(0)

    
#define NUM_COMP_IN_GRAPH   1

void *p_input, *p_output;
pthread_t dec_thread;

FILE *fp, *ofp=NULL;
xaf_info_t comp_info;
void *pb_handle = NULL;
unsigned int card = 0;
unsigned int device = 0;
unsigned int period_size = 1024;
unsigned int period_count = 4;

/* ...playback format */    
xaf_format_t pb_format;

int g_pthread_exit_code=0x12345678;

void thread_exit_handler(int sig)
{
    /* ...unused arg */
    (void) sig;

    pthread_exit(0);
}

static int vorbis_setup(void *p_decoder)
{
    int param[2];
    
    /* 1: Raw decode, 0: Ogg decode */
    param[0] = XA_VORBISDEC_CONFIG_PARAM_RAW_VORBIS_FILE_MODE;
    param[1] = 0;

    XF_CHK_API(xaf_comp_set_config(p_decoder, 1, &param[0]));    
    
    return 0;
}

static int pcm_setup(void *p_pcm)
{
    int param[6];

    param[0] = XA_CODEC_CONFIG_PARAM_SAMPLE_RATE;
    param[1] = pb_format.sample_rate;
    param[2] = XA_CODEC_CONFIG_PARAM_CHANNELS;
    param[3] = pb_format.channels;
    param[4] = XA_CODEC_CONFIG_PARAM_PCM_WIDTH;
    param[5] = pb_format.pcm_width;

    XF_CHK_API(xaf_comp_set_config(p_pcm, 3, &param[0]));    

    return 0;
}

static int consume_output(void *p_buf, int buf_length, void *p_output)
{
	XAF_CHK_PTR(p_buf);
	XAF_CHK_PTR(p_output);

#if !defined BOARD
	FILE *fp = p_output;
	fwrite(p_buf, 1, buf_length, fp);    

	if (xa_playback_buf(pb_handle, p_buf, buf_length)) {
        TRACE(ERROR, _b("Playback Failed \n"));
		return -1;
	}	
#else 
#endif
	return 0;
}

static int read_input(void *p_buf, int buf_length, int *read_length, void *p_input)
{
    XAF_CHK_PTR(p_buf);
    XAF_CHK_PTR(read_length);
    XAF_CHK_PTR(p_input);

#if !defined BOARD
    FILE *fp = p_input;
    *read_length = fread(p_buf, 1, buf_length, fp);
#else
#endif
    return 0;
}

static int comp_process_entry(void *arg)
{
    void *p_comp;
    xaf_comp_status comp_status;
    int input_over, read_length;
    void *pg_pthread_exit_code = (void*)&g_pthread_exit_code;

    XAF_CHK_PTR(arg);
    XAF_CHK_PTR(p_input);
    XAF_CHK_PTR(p_output);

    p_comp = arg;
    input_over = 0;
    
    XF_CHK_API(xaf_comp_process(NULL, p_comp, NULL, 0, XAF_EXEC_FLAG));

    while (1)
    {
	
        XF_CHK_API(xaf_comp_get_status(NULL, p_comp, &comp_status, &comp_info));
        
        if (comp_status == XAF_EXEC_DONE) break;

        if (comp_status == XAF_NEED_INPUT && !input_over)
        {
            void *p_buf = (void *)comp_info.buf; 
            int size    = comp_info.length;
            
            XF_CHK_API(read_input(p_buf, size, &read_length, p_input));

            if (read_length) 
                XF_CHK_API(xaf_comp_process(NULL, p_comp, (void *)comp_info.buf, read_length, XAF_INPUT_READY_FLAG));
            else
            {
                XF_CHK_API(xaf_comp_process(NULL, p_comp, NULL, 0, XAF_INPUT_OVER_FLAG));
                input_over = 1;
            }
        }
        
        if (comp_status == XAF_OUTPUT_READY)
        {
            void *p_buf = (void *)comp_info.buf;
            int size    = comp_info.length;
            
            XF_CHK_API(consume_output(p_buf, size, p_output));
            XF_CHK_API(xaf_comp_process(NULL, p_comp, (void *)comp_info.buf, comp_info.length, XAF_NEED_OUTPUT_FLAG));
        }
    }
    pthread_exit(pg_pthread_exit_code);

    return 0;
}


int main(int argc, const char **argv)
{
    void *p_adev = NULL;
    void *p_decoder;
    mem_obj_t* mem_handle;
    int num_comp = NUM_COMP_IN_GRAPH;

    xaf_comp_status dec_status;
    void *dec_inbuf[2];
    int buf_length = XAF_INBUF_SIZE;
    int read_length;
    int i;    
    
    xf_id_t dec_id;
    int (*dec_setup)(void *p_comp);    
    const char *ext;
    int pthread_error;
    void *pthread_exit_code;
 
    struct sigaction actions;
    memset(&actions, 0, sizeof(actions));
    sigemptyset(&actions.sa_mask);
    actions.sa_flags = 0;
    actions.sa_handler = thread_exit_handler;
    sigaction(SIGUSR1,&actions,NULL);

    /* ...initialize playback format */
    pb_format.sample_rate = 48000;
    pb_format.channels    = 2;
    pb_format.pcm_width   = 16;

    audio_frmwk_buf_size = 0; //unused 
    audio_comp_buf_size  = 0; //unused

    print_banner("\'Audio decoder(PCM/Ogg-Vorbis)\'");

    /* ...initialize tracing facility */
    TRACE_INIT("Xtensa Audio Framework - Sample Application");

#if !defined BOARD
    /* ...check input arguments */
    if (argc != 2)
    {
        TRACE(ERROR, _b("Usage: ./xaf-test <input-file-.ogg/.pcm>\n"));
        PRINT_USAGE;
        return 0;
    }
    
        
    ext = strrchr(argv[1], '.');
    if (!ext)
    {   
        PRINT_USAGE;
        return 0;
    }
    ext++;
    if (!strcmp(ext, "pcm")) {
    	dec_id    = "audio-decoder/pcm";
        dec_setup = pcm_setup;
    }
    else if (!strcmp(ext, "ogg")) {
    	dec_id    = "audio-decoder/vorbis";
        dec_setup = vorbis_setup;
    }
    else {
    	TRACE(ERROR, _x("Unknown Decoder Extension '%s'"), ext);
        PRINT_USAGE;
        exit(-1);
    }

    /* ...open file */
    if ((fp = fopen(argv[1], "rb")) == NULL)
    {
       TRACE(ERROR, _x("Failed to open '%s': %d"), argv[1], errno);
       exit(-1);
    }
    if ((ofp = fopen("/data/dec-out.pcm", "wb")) == NULL)
    {
       TRACE(ERROR, _x("Failed to open '%s': %d"), "/data/dec-out.pcm", errno);
       exit(-1);
    }
    p_input  = fp;
    p_output = ofp;
#endif

    mem_handle = mem_init(); //initialize memory handler

    XF_CHK_API(xaf_adev_open(&p_adev, audio_frmwk_buf_size, audio_comp_buf_size, mem_malloc, mem_free));

    /* ...create decoder component */
    XF_CHK_API(xaf_comp_create(p_adev, &p_decoder, dec_id, 2, 1, &dec_inbuf[0], XAF_DECODER));
    XF_CHK_API(dec_setup(p_decoder));

    /* ...start decoder component */            
    XF_CHK_API(xaf_comp_process(p_adev, p_decoder, NULL, 0, XAF_START_FLAG));

    /* ...feed input to decoder component */
    for (i=0; i<2; i++) 
    {
        XF_CHK_API(read_input(dec_inbuf[i], buf_length, &read_length, p_input));

        if (read_length) 
            XF_CHK_API(xaf_comp_process(p_adev, p_decoder, dec_inbuf[i], read_length, XAF_INPUT_READY_FLAG));
        else 
            break;
    }

    /* ...initialization loop */    
    while (1)
    {
	    XF_CHK_API(xaf_comp_get_status(p_adev, p_decoder, &dec_status, &comp_info));

	    if (dec_status == XAF_INIT_DONE || dec_status == XAF_EXEC_DONE)
	    {
		    pb_handle = xa_playback_open(card, device, pb_format.channels, pb_format.sample_rate,
				         pb_format.pcm_width, period_size, period_count); 
		    if (!pb_handle) {
			    TRACE(ERROR, _x("Playback open error\n"));
			    return -1;
		    }

		    break;
	    }

	    if (dec_status == XAF_NEED_INPUT)
	    {
		    void *p_buf = (void *) comp_info.buf; 
		    int size    = comp_info.length;

		    XF_CHK_API(read_input(p_buf, size, &read_length, p_input));

		    if (read_length) 
			    XF_CHK_API(xaf_comp_process(p_adev, p_decoder, p_buf, read_length, XAF_INPUT_READY_FLAG));
		    else
			    break;
	    }
    }
    
    if (dec_status != XAF_INIT_DONE)
    {
        TRACE(ERROR, _x("Failed to init"));
        exit(-1);
    }

    pthread_create(&dec_thread, 0, (void *(*)(void *))&comp_process_entry, p_decoder);

    pthread_error = pthread_join(dec_thread, (void **) &pthread_exit_code);
    if(pthread_error)
    {
      TRACE(ERROR, _b("decode thread join error:%x\n"), pthread_error);
    }

    /* ...exec done, clean-up */
    XF_CHK_API(xaf_comp_delete(p_decoder));
    xa_playback_close(pb_handle);
    XF_CHK_API(xaf_adev_close(p_adev, 0 /*unused*/));
    if (fp)  fclose(fp);
    if (ofp) fclose(ofp);
    
    mem_exit();
    XF_CHK_API(print_mem_mcps_info(mem_handle, num_comp));

    return 0;
}