tizen 2.3.1 release
[external/opencore-amr.git] / test / amrwb-dec.c
1 /* ------------------------------------------------------------------
2  * Copyright (C) 2009 Martin Storsjo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18
19 #include <stdio.h>
20 #include <stdint.h>
21 #include <string.h>
22 #include "wavwriter.h"
23 #include <dec_if.h>
24
25 /* From pvamrwbdecoder_api.h, by dividing by 8 and rounding up */
26 const int sizes[] = { 17, 23, 32, 36, 40, 46, 50, 58, 60, 5, -1, -1, -1, -1, -1, 0 };
27
28 int main(int argc, char *argv[]) {
29         FILE* in;
30         char header[9];
31         int n;
32         void *wav, *amr;
33         if (argc < 3) {
34                 fprintf(stderr, "%s in.amr out.wav\n", argv[0]);
35                 return 1;
36         }
37
38         in = fopen(argv[1], "rb");
39         if (!in) {
40                 perror(argv[1]);
41                 return 1;
42         }
43         n = fread(header, 1, 9, in);
44         if (n != 9 || memcmp(header, "#!AMR-WB\n", 9)) {
45                 fprintf(stderr, "Bad header\n");
46                 return 1;
47         }
48
49         wav = wav_write_open(argv[2], 16000, 16, 1);
50         if (!wav) {
51                 fprintf(stderr, "Unable to open %s\n", argv[2]);
52                 return 1;
53         }
54         amr = D_IF_init();
55         while (1) {
56                 uint8_t buffer[500], littleendian[640], *ptr;
57                 int size, i;
58                 int16_t outbuffer[320];
59                 /* Read the mode byte */
60                 n = fread(buffer, 1, 1, in);
61                 if (n <= 0)
62                         break;
63                 /* Find the packet size */
64                 size = sizes[(buffer[0] >> 3) & 0x0f];
65                 if (size < 0)
66                         break;
67                 n = fread(buffer + 1, 1, size, in);
68                 if (n != size)
69                         break;
70
71                 /* Decode the packet */
72                 D_IF_decode(amr, buffer, outbuffer, 0);
73
74                 /* Convert to little endian and write to wav */
75                 ptr = littleendian;
76                 for (i = 0; i < 320; i++) {
77                         *ptr++ = (outbuffer[i] >> 0) & 0xff;
78                         *ptr++ = (outbuffer[i] >> 8) & 0xff;
79                 }
80                 wav_write_data(wav, littleendian, 640);
81         }
82         fclose(in);
83         D_IF_exit(amr);
84         wav_write_close(wav);
85         return 0;
86 }
87