r802: Remove renderframfsclient and renderfarmfsserver .h and .C from Makefile.am...
[cinelerra_cv/mob.git] / cinelerra / fileac3.C
blob1b7a7270de1f1682bc6fe7c333e0e5dbe84d619b
1 #include "asset.h"
2 #include "clip.h"
3 #include "fileac3.h"
4 #include "language.h"
5 #include "mwindow.inc"
10 #include <string.h>
12 FileAC3::FileAC3(Asset *asset, File *file)
13  : FileBase(asset, file)
15         reset_parameters();
18 FileAC3::~FileAC3()
20         close_file();
23 int FileAC3::reset_parameters_derived()
25         codec = 0;
26         codec_context = 0;
27         fd = 0;
28         temp_raw = 0;
29         temp_raw_size = 0;
30         temp_raw_allocated = 0;
31         temp_compressed = 0;
32         compressed_allocated = 0;
35 void FileAC3::get_parameters(BC_WindowBase *parent_window, 
36                 Asset *asset, 
37                 BC_WindowBase* &format_window,
38                 int audio_options,
39                 int video_options)
41         if(audio_options)
42         {
44                 AC3ConfigAudio *window = new AC3ConfigAudio(parent_window, asset);
45                 format_window = window;
46                 window->create_objects();
47                 window->run_window();
48                 delete window;
49         }
52 int FileAC3::check_sig()
54 // Libmpeg3 does this in FileMPEG.
55         return 0;
58 int FileAC3::open_file(int rd, int wr)
60         this->wr = wr;
61         this->rd = rd;
64         if(wr)
65         {
66                 avcodec_init();
67                 avcodec_register_all();
68                 codec = avcodec_find_encoder(CODEC_ID_AC3);
69                 if(!codec)
70                 {
71                         fprintf(stderr, 
72                                 "FileAC3::open_file codec not found.\n");
73                         return 1;
74                 }
75                 codec_context = avcodec_alloc_context();
76                 codec_context->bit_rate = asset->ac3_bitrate * 1000;
77                 codec_context->sample_rate = asset->sample_rate;
78                 codec_context->channels = asset->channels;
79                 if(avcodec_open(codec_context, codec))
80                 {
81                         fprintf(stderr, 
82                                 "FileAC3::open_file failed to open codec.\n");
83                         return 1;
84                 }
86                 if(!(fd = fopen(asset->path, "w")))
87                 {
88                         perror("FileAC3::open_file");
89                         return 1;
90                 }
91         }
92         else
93         {
94                 if(!(fd = fopen(asset->path, "r")))
95                 {
96                         perror("FileAC3::open_file");
97                         return 1;
98                 }
99         }
104         return 0;
107 int FileAC3::close_file()
109         if(codec_context)
110         {
111                 avcodec_close(codec_context);
112                 free(codec_context);
113                 codec_context = 0;
114                 codec = 0;
115         }
116         if(fd)
117         {
118                 fclose(fd);
119                 fd = 0;
120         }
121         if(temp_raw)
122         {
123                 delete [] temp_raw;
124                 temp_raw = 0;
125         }
126         if(temp_compressed)
127         {
128                 delete [] temp_compressed;
129                 temp_compressed = 0;
130         }
131         reset_parameters();
132         FileBase::close_file();
135 int FileAC3::write_samples(double **buffer, int64_t len)
137 // Convert buffer to encoder format
138         if(temp_raw_size + len > temp_raw_allocated)
139         {
140                 int new_allocated = temp_raw_size + len;
141                 int16_t *new_raw = new int16_t[new_allocated * asset->channels];
142                 if(temp_raw)
143                 {
144                         memcpy(new_raw, 
145                                 temp_raw, 
146                                 sizeof(int16_t) * temp_raw_size * asset->channels);
147                         delete [] temp_raw;
148                 }
149                 temp_raw = new_raw;
150                 temp_raw_allocated = new_allocated;
151         }
153 // Allocate compressed data buffer
154         if(temp_raw_allocated * asset->channels * 2 > compressed_allocated)
155         {
156                 compressed_allocated = temp_raw_allocated * asset->channels * 2;
157                 delete [] temp_compressed;
158                 temp_compressed = new unsigned char[compressed_allocated];
159         }
161 // Append buffer to temp raw
162         int16_t *out_ptr = temp_raw + temp_raw_size * asset->channels;
163         for(int i = 0; i < len; i++)
164         {
165                 for(int j = 0; j < asset->channels; j++)
166                 {
167                         int sample = (int)(buffer[j][i] * 32767);
168                         CLAMP(sample, -32768, 32767);
169                         *out_ptr++ = sample;
170                 }
171         }
172         temp_raw_size += len;
174         int frame_size = codec_context->frame_size;
175         int output_size = 0;
176         int current_sample = 0;
177         for(current_sample = 0; 
178                 current_sample + frame_size <= temp_raw_size; 
179                 current_sample += frame_size)
180         {
181                 int compressed_size = avcodec_encode_audio(
182                         codec_context, 
183                         temp_compressed + output_size, 
184                         compressed_allocated - output_size, 
185             temp_raw + current_sample * asset->channels);
186                 output_size += compressed_size;
187         }
189 // Shift buffer back
190         memcpy(temp_raw,
191                 temp_raw + current_sample * asset->channels,
192                 (temp_raw_size - current_sample) * sizeof(int16_t) * asset->channels);
193         temp_raw_size -= current_sample;
195         int bytes_written = fwrite(temp_compressed, 1, output_size, fd);
196         if(bytes_written < output_size)
197         {
198                 perror("FileAC3::write_samples");
199                 return 1;
200         }
201         return 0;
210 AC3ConfigAudio::AC3ConfigAudio(BC_WindowBase *parent_window,
211         Asset *asset)
212  : BC_Window(PROGRAM_NAME ": Audio Compression",
213         parent_window->get_abs_cursor_x(1),
214         parent_window->get_abs_cursor_y(1),
215         500,
216         BC_OKButton::calculate_h() + 100,
217         500,
218         BC_OKButton::calculate_h() + 100,
219         0,
220         0,
221         1)
223         this->parent_window = parent_window;
224         this->asset = asset;
227 void AC3ConfigAudio::create_objects()
229         int x = 10, y = 10;
230         int x1 = 150;
231         add_tool(new BC_Title(x, y, "Bitrate (kbps):"));
232         AC3ConfigAudioBitrate *bitrate;
233         add_tool(bitrate = 
234                 new AC3ConfigAudioBitrate(this,
235                         x1, 
236                         y));
237         bitrate->create_objects();
239         add_subwindow(new BC_OKButton(this));
240         show_window();
241         flush();
244 int AC3ConfigAudio::close_event()
246         set_done(0);
247         return 1;
255 AC3ConfigAudioBitrate::AC3ConfigAudioBitrate(AC3ConfigAudio *gui, 
256         int x, 
257         int y)
258  : BC_PopupMenu(x,
259         y,
260         150,
261         AC3ConfigAudioBitrate::bitrate_to_string(gui->string, gui->asset->ac3_bitrate))
263         this->gui = gui;
266 char* AC3ConfigAudioBitrate::bitrate_to_string(char *string, int bitrate)
268         sprintf(string, "%d", bitrate);
269         return string;
272 void AC3ConfigAudioBitrate::create_objects()
274         add_item(new BC_MenuItem("32"));
275         add_item(new BC_MenuItem("40"));
276         add_item(new BC_MenuItem("48"));
277         add_item(new BC_MenuItem("56"));
278         add_item(new BC_MenuItem("64"));
279         add_item(new BC_MenuItem("80"));
280         add_item(new BC_MenuItem("96"));
281         add_item(new BC_MenuItem("112"));
282         add_item(new BC_MenuItem("128"));
283         add_item(new BC_MenuItem("160"));
284         add_item(new BC_MenuItem("192"));
285         add_item(new BC_MenuItem("224"));
286         add_item(new BC_MenuItem("256"));
287         add_item(new BC_MenuItem("320"));
288         add_item(new BC_MenuItem("384"));
289         add_item(new BC_MenuItem("448"));
290         add_item(new BC_MenuItem("512"));
291         add_item(new BC_MenuItem("576"));
292         add_item(new BC_MenuItem("640"));
295 int AC3ConfigAudioBitrate::handle_event()
297         gui->asset->ac3_bitrate = atol(get_text());
298         return 1;