merge manifest file
[external/opencore-amr.git] / test / amrnb-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 <interf_dec.h>
24
25 /* From WmfDecBytesPerFrame in dec_input_format_tab.cpp */
26 const int sizes[] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 6, 5, 5, 0, 0, 0, 0 };
27
28
29 int main(int argc, char *argv[]) {
30         FILE* in;
31         char header[6];
32         int n;
33         void *wav, *amr;
34         if (argc < 3) {
35                 fprintf(stderr, "%s in.amr out.wav\n", argv[0]);
36                 return 1;
37         }
38
39         in = fopen(argv[1], "rb");
40         if (!in) {
41                 perror(argv[1]);
42                 return 1;
43         }
44         n = fread(header, 1, 6, in);
45         if (n != 6 || memcmp(header, "#!AMR\n", 6)) {
46                 fprintf(stderr, "Bad header\n");
47                 return 1;
48         }
49
50         wav = wav_write_open(argv[2], 8000, 16, 1);
51         if (!wav) {
52                 fprintf(stderr, "Unable to open %s\n", argv[2]);
53                 return 1;
54         }
55
56         amr = Decoder_Interface_init();
57         while (1) {
58                 uint8_t buffer[500], littleendian[320], *ptr;
59                 int size, i;
60                 int16_t outbuffer[160];
61                 /* Read the mode byte */
62                 n = fread(buffer, 1, 1, in);
63                 if (n <= 0)
64                         break;
65                 /* Find the packet size */
66                 size = sizes[(buffer[0] >> 3) & 0x0f];
67                 n = fread(buffer + 1, 1, size, in);
68                 if (n != size)
69                         break;
70
71                 /* Decode the packet */
72                 Decoder_Interface_Decode(amr, buffer, outbuffer, 0);
73
74                 /* Convert to little endian and write to wav */
75                 ptr = littleendian;
76                 for (i = 0; i < 160; i++) {
77                         *ptr++ = (outbuffer[i] >> 0) & 0xff;
78                         *ptr++ = (outbuffer[i] >> 8) & 0xff;
79                 }
80                 wav_write_data(wav, littleendian, 320);
81         }
82         fclose(in);
83         Decoder_Interface_exit(amr);
84         wav_write_close(wav);
85         return 0;
86 }
87