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