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