Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

fileac3.C

Go to the documentation of this file.
00001 #include "asset.h"
00002 #include "clip.h"
00003 #include "fileac3.h"
00004 #include "language.h"
00005 #include "mwindow.inc"
00006 
00007 
00008 
00009 
00010 #include <string.h>
00011 
00012 FileAC3::FileAC3(Asset *asset, File *file)
00013  : FileBase(asset, file)
00014 {
00015         reset_parameters();
00016 }
00017 
00018 FileAC3::~FileAC3()
00019 {
00020         close_file();
00021 }
00022 
00023 int FileAC3::reset_parameters_derived()
00024 {
00025         codec = 0;
00026         codec_context = 0;
00027         fd = 0;
00028         temp_raw = 0;
00029         temp_raw_size = 0;
00030         temp_raw_allocated = 0;
00031         temp_compressed = 0;
00032         compressed_allocated = 0;
00033 }
00034 
00035 void FileAC3::get_parameters(BC_WindowBase *parent_window, 
00036                 Asset *asset, 
00037                 BC_WindowBase* &format_window,
00038                 int audio_options,
00039                 int video_options)
00040 {
00041         if(audio_options)
00042         {
00043 
00044                 AC3ConfigAudio *window = new AC3ConfigAudio(parent_window, asset);
00045                 format_window = window;
00046                 window->create_objects();
00047                 window->run_window();
00048                 delete window;
00049         }
00050 }
00051 
00052 int FileAC3::check_sig()
00053 {
00054 // Libmpeg3 does this in FileMPEG.
00055         return 0;
00056 }
00057 
00058 int FileAC3::open_file(int rd, int wr)
00059 {
00060         this->wr = wr;
00061         this->rd = rd;
00062 
00063 
00064         if(wr)
00065         {
00066                 avcodec_init();
00067                 avcodec_register_all();
00068                 codec = avcodec_find_encoder(CODEC_ID_AC3);
00069                 if(!codec)
00070                 {
00071                         fprintf(stderr, 
00072                                 "FileAC3::open_file codec not found.\n");
00073                         return 1;
00074                 }
00075                 codec_context = avcodec_alloc_context();
00076                 codec_context->bit_rate = asset->ac3_bitrate * 1000;
00077                 codec_context->sample_rate = asset->sample_rate;
00078                 codec_context->channels = asset->channels;
00079                 if(avcodec_open(codec_context, codec))
00080                 {
00081                         fprintf(stderr, 
00082                                 "FileAC3::open_file failed to open codec.\n");
00083                         return 1;
00084                 }
00085 
00086                 if(!(fd = fopen(asset->path, "w")))
00087                 {
00088                         perror("FileAC3::open_file");
00089                         return 1;
00090                 }
00091         }
00092         else
00093         {
00094                 if(!(fd = fopen(asset->path, "r")))
00095                 {
00096                         perror("FileAC3::open_file");
00097                         return 1;
00098                 }
00099         }
00100 
00101 
00102 
00103 
00104         return 0;
00105 }
00106 
00107 int FileAC3::close_file()
00108 {
00109         if(codec_context)
00110         {
00111                 avcodec_close(codec_context);
00112                 free(codec_context);
00113                 codec_context = 0;
00114                 codec = 0;
00115         }
00116         if(fd)
00117         {
00118                 fclose(fd);
00119                 fd = 0;
00120         }
00121         if(temp_raw)
00122         {
00123                 delete [] temp_raw;
00124                 temp_raw = 0;
00125         }
00126         if(temp_compressed)
00127         {
00128                 delete [] temp_compressed;
00129                 temp_compressed = 0;
00130         }
00131         reset_parameters();
00132         FileBase::close_file();
00133 }
00134 
00135 int FileAC3::write_samples(double **buffer, int64_t len)
00136 {
00137 // Convert buffer to encoder format
00138         if(temp_raw_size + len > temp_raw_allocated)
00139         {
00140                 int new_allocated = temp_raw_size + len;
00141                 int16_t *new_raw = new int16_t[new_allocated * asset->channels];
00142                 if(temp_raw)
00143                 {
00144                         memcpy(new_raw, 
00145                                 temp_raw, 
00146                                 sizeof(int16_t) * temp_raw_size * asset->channels);
00147                         delete [] temp_raw;
00148                 }
00149                 temp_raw = new_raw;
00150                 temp_raw_allocated = new_allocated;
00151         }
00152 
00153 // Allocate compressed data buffer
00154         if(temp_raw_allocated * asset->channels * 2 > compressed_allocated)
00155         {
00156                 compressed_allocated = temp_raw_allocated * asset->channels * 2;
00157                 delete [] temp_compressed;
00158                 temp_compressed = new unsigned char[compressed_allocated];
00159         }
00160 
00161 // Append buffer to temp raw
00162         int16_t *out_ptr = temp_raw + temp_raw_size * asset->channels;
00163         for(int i = 0; i < len; i++)
00164         {
00165                 for(int j = 0; j < asset->channels; j++)
00166                 {
00167                         int sample = (int)(buffer[j][i] * 32767);
00168                         CLAMP(sample, -32768, 32767);
00169                         *out_ptr++ = sample;
00170                 }
00171         }
00172         temp_raw_size += len;
00173 
00174         int frame_size = codec_context->frame_size;
00175         int output_size = 0;
00176         int current_sample = 0;
00177         for(current_sample = 0; 
00178                 current_sample + frame_size <= temp_raw_size; 
00179                 current_sample += frame_size)
00180         {
00181                 int compressed_size = avcodec_encode_audio(
00182                         codec_context, 
00183                         temp_compressed + output_size, 
00184                         compressed_allocated - output_size, 
00185             temp_raw + current_sample * asset->channels);
00186                 output_size += compressed_size;
00187         }
00188 
00189 // Shift buffer back
00190         memcpy(temp_raw,
00191                 temp_raw + current_sample * asset->channels,
00192                 (temp_raw_size - current_sample) * sizeof(int16_t) * asset->channels);
00193         temp_raw_size -= current_sample;
00194 
00195         int bytes_written = fwrite(temp_compressed, 1, output_size, fd);
00196         if(bytes_written < output_size)
00197         {
00198                 perror("FileAC3::write_samples");
00199                 return 1;
00200         }
00201         return 0;
00202 }
00203 
00204 
00205 
00206 
00207 
00208 
00209 
00210 AC3ConfigAudio::AC3ConfigAudio(BC_WindowBase *parent_window,
00211         Asset *asset)
00212  : BC_Window(PROGRAM_NAME ": Audio Compression",
00213         parent_window->get_abs_cursor_x(1),
00214         parent_window->get_abs_cursor_y(1),
00215         500,
00216         BC_OKButton::calculate_h() + 100,
00217         500,
00218         BC_OKButton::calculate_h() + 100,
00219         0,
00220         0,
00221         1)
00222 {
00223         this->parent_window = parent_window;
00224         this->asset = asset;
00225 }
00226 
00227 void AC3ConfigAudio::create_objects()
00228 {
00229         int x = 10, y = 10;
00230         int x1 = 150;
00231         add_tool(new BC_Title(x, y, "Bitrate (kbps):"));
00232         AC3ConfigAudioBitrate *bitrate;
00233         add_tool(bitrate = 
00234                 new AC3ConfigAudioBitrate(this,
00235                         x1, 
00236                         y));
00237         bitrate->create_objects();
00238 
00239         add_subwindow(new BC_OKButton(this));
00240         show_window();
00241         flush();
00242 }
00243 
00244 int AC3ConfigAudio::close_event()
00245 {
00246         set_done(0);
00247         return 1;
00248 }
00249 
00250 
00251 
00252 
00253 
00254 
00255 AC3ConfigAudioBitrate::AC3ConfigAudioBitrate(AC3ConfigAudio *gui, 
00256         int x, 
00257         int y)
00258  : BC_PopupMenu(x,
00259         y,
00260         150,
00261         AC3ConfigAudioBitrate::bitrate_to_string(gui->string, gui->asset->ac3_bitrate))
00262 {
00263         this->gui = gui;
00264 }
00265 
00266 char* AC3ConfigAudioBitrate::bitrate_to_string(char *string, int bitrate)
00267 {
00268         sprintf(string, "%d", bitrate);
00269         return string;
00270 }
00271 
00272 void AC3ConfigAudioBitrate::create_objects()
00273 {
00274         add_item(new BC_MenuItem("32"));
00275         add_item(new BC_MenuItem("40"));
00276         add_item(new BC_MenuItem("48"));
00277         add_item(new BC_MenuItem("56"));
00278         add_item(new BC_MenuItem("64"));
00279         add_item(new BC_MenuItem("80"));
00280         add_item(new BC_MenuItem("96"));
00281         add_item(new BC_MenuItem("112"));
00282         add_item(new BC_MenuItem("128"));
00283         add_item(new BC_MenuItem("160"));
00284         add_item(new BC_MenuItem("192"));
00285         add_item(new BC_MenuItem("224"));
00286         add_item(new BC_MenuItem("256"));
00287         add_item(new BC_MenuItem("320"));
00288         add_item(new BC_MenuItem("384"));
00289         add_item(new BC_MenuItem("448"));
00290         add_item(new BC_MenuItem("512"));
00291         add_item(new BC_MenuItem("576"));
00292         add_item(new BC_MenuItem("640"));
00293 }
00294 
00295 int AC3ConfigAudioBitrate::handle_event()
00296 {
00297         gui->asset->ac3_bitrate = atol(get_text());
00298         return 1;
00299 }
00300 
00301 
00302 

Generated on Sun Jan 8 13:38:54 2006 for Cinelerra-svn by  doxygen 1.4.4