Initialize Tizen 2.3
[external/opencore-amr.git] / test / amrnb-enc.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 <interf_enc.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include "wavreader.h"
25
26 void usage(const char* name) {
27         fprintf(stderr, "%s [-r bitrate] [-d] in.wav out.amr\n", name);
28 }
29
30 enum Mode findMode(const char* str) {
31         struct {
32                 enum Mode mode;
33                 int rate;
34         } modes[] = {
35                 { MR475,  4750 },
36                 { MR515,  5150 },
37                 { MR59,   5900 },
38                 { MR67,   6700 },
39                 { MR74,   7400 },
40                 { MR795,  7950 },
41                 { MR102, 10200 },
42                 { MR122, 12200 }
43         };
44         int rate = atoi(str);
45         int closest = -1;
46         int closestdiff = 0;
47         unsigned int i;
48         for (i = 0; i < sizeof(modes)/sizeof(modes[0]); i++) {
49                 if (modes[i].rate == rate)
50                         return modes[i].mode;
51                 if (closest < 0 || closestdiff > abs(modes[i].rate - rate)) {
52                         closest = i;
53                         closestdiff = abs(modes[i].rate - rate);
54                 }
55         }
56         fprintf(stderr, "Using bitrate %d\n", modes[closest].rate);
57         return modes[closest].mode;
58 }
59
60 int main(int argc, char *argv[]) {
61         enum Mode mode = MR122;
62         int ch, dtx = 0;
63         const char *infile, *outfile;
64         FILE *out;
65         void *wav, *amr;
66         int format, sampleRate, channels, bitsPerSample;
67         int inputSize;
68         uint8_t* inputBuf;
69         while ((ch = getopt(argc, argv, "r:d")) != -1) {
70                 switch (ch) {
71                 case 'r':
72                         mode = findMode(optarg);
73                         break;
74                 case 'd':
75                         dtx = 1;
76                         break;
77                 case '?':
78                 default:
79                         usage(argv[0]);
80                         return 1;
81                 }
82         }
83         if (argc - optind < 2) {
84                 usage(argv[0]);
85                 return 1;
86         }
87         infile = argv[optind];
88         outfile = argv[optind + 1];
89
90         wav = wav_read_open(infile);
91         if (!wav) {
92                 fprintf(stderr, "Unable to open wav file %s\n", infile);
93                 return 1;
94         }
95         if (!wav_get_header(wav, &format, &channels, &sampleRate, &bitsPerSample, NULL)) {
96                 fprintf(stderr, "Bad wav file %s\n", infile);
97                 return 1;
98         }
99         if (format != 1) {
100                 fprintf(stderr, "Unsupported WAV format %d\n", format);
101                 return 1;
102         }
103         if (bitsPerSample != 16) {
104                 fprintf(stderr, "Unsupported WAV sample depth %d\n", bitsPerSample);
105                 return 1;
106         }
107         if (channels != 1)
108                 fprintf(stderr, "Warning, only compressing one audio channel\n");
109         if (sampleRate != 8000)
110                 fprintf(stderr, "Warning, AMR-NB uses 8000 Hz sample rate (WAV file has %d Hz)\n", sampleRate);
111         inputSize = channels*2*160;
112         inputBuf = (uint8_t*) malloc(inputSize);
113
114         amr = Encoder_Interface_init(dtx);
115         out = fopen(outfile, "wb");
116         if (!out) {
117                 perror(outfile);
118                 return 1;
119         }
120
121         fwrite("#!AMR\n", 1, 6, out);
122         while (1) {
123                 short buf[160];
124                 uint8_t outbuf[500];
125                 int read, i, n;
126                 read = wav_read_data(wav, inputBuf, inputSize);
127                 read /= channels;
128                 read /= 2;
129                 if (read < 160)
130                         break;
131                 for (i = 0; i < 160; i++) {
132                         const uint8_t* in = &inputBuf[2*channels*i];
133                         buf[i] = in[0] | (in[1] << 8);
134                 }
135                 n = Encoder_Interface_Encode(amr, mode, buf, outbuf, 0);
136                 fwrite(outbuf, 1, n, out);
137         }
138         free(inputBuf);
139         fclose(out);
140         Encoder_Interface_exit(amr);
141         wav_read_close(wav);
142
143         return 0;
144 }
145