~ubuntu-branches/ubuntu/feisty/avidemux/feisty

« back to all changes in this revision

Viewing changes to avidemux/ADM_lavcodec/amr_float/encoder.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T Chen
  • Date: 2006-12-15 17:13:20 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20061215171320-w79pvpehxx2fr217
Tags: 1:2.3.0-0.0ubuntu1
* Merge from debian-multimedia.org, remaining Ubuntu change:
  - desktop file,
  - no support for ccache and make -j.
* Closes Ubuntu: #69614.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ===================================================================
 
3
 *  TS 26.104
 
4
 *  REL-5 V5.4.0 2004-03
 
5
 *  REL-6 V6.1.0 2004-03
 
6
 *  3GPP AMR Floating-point Speech Codec
 
7
 * ===================================================================
 
8
 *
 
9
 */
 
10
 
 
11
/*
 
12
 * encoder.c
 
13
 *
 
14
 *
 
15
 * Project:
 
16
 *    AMR Floating-Point Codec
 
17
 *
 
18
 * Contains:
 
19
 *    Speech encoder main program
 
20
 *
 
21
 */
 
22
#include <stdlib.h>
 
23
#include <stdio.h>
 
24
#include <memory.h>
 
25
#include <string.h>
 
26
#include "typedef.h"
 
27
#include "interf_enc.h"
 
28
 
 
29
#ifndef ETSI
 
30
#ifndef IF2
 
31
#define AMR_MAGIC_NUMBER "#!AMR\n"
 
32
#endif
 
33
#endif
 
34
 
 
35
static const short modeConv[]={
 
36
   475, 515, 59, 67, 74, 795, 102, 122};
 
37
 
 
38
static void Usage(char* argv[])
 
39
{
 
40
   fprintf (stderr,
 
41
      "Usage of %s:\n\n"
 
42
      "[-dtx] mode speech_file bitstream_file \n\n"
 
43
      "or \n\n"
 
44
      "[-dtx] -modefile=mode_file speech_file bitstream_file \n\n\n"
 
45
      "mode = MR475, MR515, MR59, MR67, MR74, MR795, MR102, MR122 \n\n\n",
 
46
      argv[0]);
 
47
}
 
48
void Copyright(void){
 
49
fprintf (stderr,
 
50
"===================================================================\n"
 
51
" TS 26.104                                                         \n"
 
52
" REL-5 V5.4.0 2004-03                                              \n"
 
53
" REL-6 V6.1.0 2004-03                                              \n"
 
54
" 3GPP AMR Floating-point Speech Encoder                            \n"
 
55
"===================================================================\n"
 
56
);
 
57
}
 
58
/*
 
59
 * main
 
60
 *
 
61
 *
 
62
 * Function:
 
63
 *    Speech encoder main program
 
64
 *
 
65
 *    Usage: encoder speech_file bitstream_file mode dtx mode_file
 
66
 *
 
67
 *    Format for speech_file:
 
68
 *       Speech is read from a binary file of 16 bits data.
 
69
 *
 
70
 *    Format for ETSI bitstream file:
 
71
 *       1 word (2-byte) for the TX frame type
 
72
 *       244 words (2-byte) containing 244 bits.
 
73
 *          Bit 0 = 0x0000 and Bit 1 = 0x0001
 
74
 *       1 word (2-byte) for the mode indication
 
75
 *       4 words for future use, currently written as zero
 
76
 *
 
77
 *    Format for 3GPP bitstream file:
 
78
 *       Holds mode information and bits packed to octets.
 
79
 *       Size is from 1 byte to 31 bytes.
 
80
 *
 
81
 *    ETSI bitstream file format is defined using ETSI as preprocessor
 
82
 *    definition
 
83
 *
 
84
 *    mode        : MR475, MR515, MR59, MR67, MR74, MR795, MR102, MR122
 
85
 *    mode_file   : reads mode information from a file
 
86
 * Returns:
 
87
 *    0
 
88
 */
 
89
int main (int argc, char * argv[]){
 
90
 
 
91
   /* file strucrures */
 
92
   FILE * file_speech = NULL;
 
93
   FILE * file_encoded = NULL;
 
94
   FILE * file_mode = NULL;
 
95
 
 
96
   /* input speech vector */
 
97
   short speech[160];
 
98
 
 
99
   /* counters */
 
100
   int byte_counter, frames = 0, bytes = 0;
 
101
 
 
102
   /* pointer to encoder state structure */
 
103
   int *enstate;
 
104
 
 
105
   /* requested mode */
 
106
   enum Mode req_mode = MR122;
 
107
   int dtx = 0;
 
108
 
 
109
   /* temporary variables */
 
110
   char mode_string[9];
 
111
   long mode_tmp;
 
112
 
 
113
   /* bitstream filetype */
 
114
#ifndef ETSI
 
115
   unsigned char serial_data[32];
 
116
#else
 
117
   short serial_data[250] = {0};
 
118
#endif
 
119
 
 
120
   /* Process command line options */
 
121
 
 
122
   if ((argc == 5) || (argc == 4)){
 
123
      file_encoded = fopen(argv[argc - 1], "wb");
 
124
      if (file_encoded == NULL){
 
125
         Usage(argv);
 
126
         return 1;
 
127
      }
 
128
      file_speech = fopen(argv[argc - 2], "rb");
 
129
      if (file_speech == NULL){
 
130
         fclose(file_encoded);
 
131
         Usage(argv);
 
132
         return 1;
 
133
      }
 
134
      if (strncmp(argv[argc - 3], "-modefile=", 10) == 0){
 
135
         file_mode = fopen(&argv[argc - 3][10], "rt");
 
136
         if (file_mode == NULL){
 
137
            Usage(argv);
 
138
            fclose(file_speech);
 
139
            fclose(file_encoded);
 
140
            return 1;
 
141
         }
 
142
      }
 
143
      else {
 
144
         mode_tmp = strtol(&argv[argc - 3][2], NULL, 0);
 
145
         for (req_mode = 0; req_mode < 8; req_mode++){
 
146
            if (mode_tmp == modeConv[req_mode])
 
147
               break;
 
148
         }
 
149
         if (req_mode == 8){
 
150
            Usage(argv);
 
151
            fclose(file_speech);
 
152
            fclose(file_encoded);
 
153
            if (file_mode != NULL)
 
154
               fclose(file_mode);
 
155
            return 1;
 
156
         }
 
157
      }
 
158
      if (argc == 5){
 
159
         if ((strcmp(argv[1], "-dtx") != 0)){
 
160
            Usage(argv);
 
161
            fclose(file_speech);
 
162
            fclose(file_encoded);
 
163
            if (file_mode != NULL){
 
164
               fclose(file_mode);
 
165
            }
 
166
            return 1;
 
167
         }
 
168
         else {
 
169
            dtx = 1;
 
170
         }
 
171
      }
 
172
   }
 
173
   else {
 
174
      Usage(argv);
 
175
      return 1;
 
176
   }
 
177
 
 
178
 
 
179
   enstate = Encoder_Interface_init(dtx);
 
180
 
 
181
   Copyright();
 
182
#ifndef VAD2
 
183
   fprintf( stderr, "%s\n", "Code compiled with VAD option: VAD1");
 
184
#else
 
185
   fprintf( stderr, "%s\n", "Code compiled with VAD option: VAD2");
 
186
#endif
 
187
 
 
188
#ifndef ETSI
 
189
#ifndef IF2
 
190
   /* write magic number to indicate single channel AMR file storage format */
 
191
        bytes = fwrite(AMR_MAGIC_NUMBER, sizeof(char), strlen(AMR_MAGIC_NUMBER), file_encoded);
 
192
#endif
 
193
#endif
 
194
 
 
195
   /* read file */
 
196
   while (fread( speech, sizeof (Word16), 160, file_speech ) > 0)
 
197
   {
 
198
      /* read mode */
 
199
      if (file_mode != NULL){
 
200
         req_mode = 8;
 
201
         if (fscanf(file_mode, "%9s\n", mode_string) != EOF) {
 
202
            mode_tmp = strtol(&mode_string[2], NULL, 0);
 
203
            for (req_mode = 0; req_mode < 8; req_mode++){
 
204
               if (mode_tmp == modeConv[req_mode]){
 
205
                  break;
 
206
               }
 
207
            }
 
208
         }
 
209
         if (req_mode == 8){
 
210
            break;
 
211
         }
 
212
      }
 
213
 
 
214
      frames ++;
 
215
 
 
216
      /* call encoder */
 
217
      byte_counter = Encoder_Interface_Encode(enstate, req_mode, speech, serial_data, 0);
 
218
 
 
219
      bytes += byte_counter;
 
220
      fwrite(serial_data, sizeof (UWord8), byte_counter, file_encoded );
 
221
      fflush(file_encoded);
 
222
   }
 
223
   Encoder_Interface_exit(enstate);
 
224
 
 
225
#ifndef ETSI
 
226
#ifdef IF2
 
227
   fprintf ( stderr, "\n%s%i%s%i%s\n", "Frame structure AMR IF2: ", frames, " frames, ", bytes, " bytes.");
 
228
#else
 
229
   fprintf ( stderr, "\n%s%i%s%i%s\n", "Frame structure AMR MIME file storage format: ", frames, " frames, ", bytes, " bytes.");
 
230
#endif
 
231
#else
 
232
   fprintf ( stderr, "\n%s%i%s\n", "Frame structure AMR ETSI: ", frames, " frames. ");
 
233
#endif
 
234
 
 
235
   fclose(file_speech);
 
236
   fclose(file_encoded);
 
237
   if (file_mode != NULL)
 
238
      fclose(file_mode);
 
239
 
 
240
   return 0;
 
241
}