temp_raw = 0;
temp_raw_size = 0;
temp_raw_allocated = 0;
- temp_compressed = 0;
- compressed_allocated = 0;
return 0;
}
void FileAC3::get_parameters(BC_WindowBase *parent_window,
- Asset *asset,
- BC_WindowBase* &format_window,
- int audio_options,
- int video_options)
+ Asset *asset, BC_WindowBase* &format_window,
+ int audio_options, int video_options, EDL *edl)
{
if(audio_options)
{
mpg_file = new FileMPEG(file->asset, file);
result = mpg_file->open_file(1, 0);
if( result ) {
- eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
+ eprintf(_("Error while opening \"%s\" for reading. \n%m\n"), asset->path);
}
}
if( !result && wr )
{
- //avcodec_init();
+ //avcodec_init();
avcodec_register_all();
codec = avcodec_find_encoder(AV_CODEC_ID_AC3);
if(!codec)
{
- eprintf("FileAC3::open_file codec not found.\n");
+ eprintf(_("FileAC3::open_file codec not found.\n"));
result = 1;
}
if( !result && !(fd = fopen(asset->path, "w")))
{
- eprintf("Error while opening \"%s\" for writing. \n%m\n", asset->path);
+ eprintf(_("Error while opening \"%s\" for writing. \n%m\n"), asset->path);
result = 1;
}
if( !result ) {
int sample_rate = asset->sample_rate;
int64_t layout = get_channel_layout(channels);
int bitrate = asset->ac3_bitrate * 1000;
+ av_init_packet(&avpkt);
codec_context = avcodec_alloc_context3(codec);
codec_context->bit_rate = bitrate;
codec_context->sample_rate = sample_rate;
swr_init(resample_context);
if(avcodec_open2(codec_context, codec, 0))
{
- eprintf("FileAC3::open_file failed to open codec.\n");
+ eprintf(_("FileAC3::open_file failed to open codec.\n"));
result = 1;
}
}
}
if(codec_context)
{
+ encode_flush();
avcodec_close(codec_context);
avcodec_free_context(&codec_context);
codec = 0;
delete [] temp_raw;
temp_raw = 0;
}
- if(temp_compressed)
- {
- delete [] temp_compressed;
- temp_compressed = 0;
- }
reset_parameters();
FileBase::close_file();
return 0;
// { }
// };
+int FileAC3::write_packet()
+{
+ AVCodecContext *&avctx = codec_context;
+ int ret = avcodec_receive_packet(avctx, &avpkt);
+ if( ret >= 0 ) {
+ ret = 0;
+ if( avpkt.data && avpkt.size > 0 ) {
+ int sz = fwrite(avpkt.data, 1, avpkt.size, fd);
+ if( sz == avpkt.size ) ret = 1;
+ }
+ if( !ret )
+ eprintf(_("Error while writing samples. \n%m\n"));
+ av_packet_unref(&avpkt);
+ }
+ else if( ret == AVERROR_EOF )
+ ret = 0;
+ return ret;
+}
+
+int FileAC3::encode_frame(AVFrame *frame)
+{
+ AVCodecContext *&avctx = codec_context;
+ int ret = 0, pkts = 0;
+ for( int retry=100; --retry>=0; ) {
+ ret = avcodec_send_frame(avctx, frame);
+ if( ret >= 0 ) return pkts;
+ if( ret != AVERROR(EAGAIN) ) break;
+ if( (ret=write_packet()) < 0 ) break;
+ if( !ret ) return pkts;
+ ++pkts;
+ }
+ if( ret < 0 ) {
+ char errmsg[BCTEXTLEN];
+ av_strerror(ret, errmsg, sizeof(errmsg));
+ fprintf(stderr, "FileAC3::encode_frame: encode failed: %s\n", errmsg);
+ }
+ return ret;
+}
+
+int FileAC3::encode_flush()
+{
+ AVCodecContext *&avctx = codec_context;
+ int ret = avcodec_send_frame(avctx, 0);
+ while( (ret=write_packet()) > 0 );
+ if( ret < 0 ) {
+ char errmsg[BCTEXTLEN];
+ av_strerror(ret, errmsg, sizeof(errmsg));
+ fprintf(stderr, "FileAC3::encode_flush: encode failed: %s\n", errmsg);
+ }
+ return ret;
+}
int FileAC3::write_samples(double **buffer, int64_t len)
{
temp_raw_allocated = new_allocated;
}
-// Allocate compressed data buffer
- if(temp_raw_allocated * asset->channels * 2 > compressed_allocated)
- {
- compressed_allocated = temp_raw_allocated * asset->channels * 2;
- delete [] temp_compressed;
- temp_compressed = new unsigned char[compressed_allocated];
- }
-
// Append buffer to temp raw
int16_t *out_ptr = temp_raw + temp_raw_size * asset->channels;
for(int i = 0; i < len; i++)
AVCodecContext *&avctx = codec_context;
int frame_size = avctx->frame_size;
- int output_size = 0, cur_sample = 0, ret = 0;
- for(cur_sample = 0; !ret &&
+ int cur_sample = 0, ret = 0;
+ for(cur_sample = 0; ret >= 0 &&
cur_sample + frame_size <= temp_raw_size;
cur_sample += frame_size)
{
- AVPacket avpkt;
- av_init_packet(&avpkt);
- avpkt.data = temp_compressed + output_size;
- avpkt.size = compressed_allocated - output_size;
AVFrame *frame = av_frame_alloc();
frame->nb_samples = frame_size;
frame->format = avctx->sample_fmt;
if( ret >= 0 ) {
frame->pts = avctx->sample_rate && avctx->time_base.num ?
file->get_audio_position() : AV_NOPTS_VALUE ;
- int got_packet = 0;
- ret = avcodec_encode_audio2(avctx, &avpkt, frame, &got_packet);
- if( ret < 0 ) {
- char errmsg[BCSTRLEN];
- av_strerror(ret, errmsg, sizeof(errmsg));
- fprintf(stderr, "avcodec_encode_audio2 failed. \n%s\n", errmsg);
- }
+ ret = encode_frame(frame);
}
- av_packet_free_side_data(&avpkt);
- output_size += avpkt.size;
- if(frame->extended_data != frame->data)
- av_freep(&frame->extended_data);
av_frame_free(&frame);
}
(temp_raw_size - cur_sample) * sizeof(int16_t) * asset->channels);
temp_raw_size -= cur_sample;
- int bytes_written = fwrite(temp_compressed, 1, output_size, fd);
- if(bytes_written < output_size)
- {
- eprintf("Error while writing samples. \n%m\n");
- return 1;
- }
return 0;
}
AC3ConfigAudio::AC3ConfigAudio(BC_WindowBase *parent_window,
Asset *asset)
- : BC_Window(PROGRAM_NAME ": Audio Compression",
+ : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
parent_window->get_abs_cursor_x(1),
parent_window->get_abs_cursor_y(1),
500,
int x = 10, y = 10;
int x1 = 150;
lock_window("AC3ConfigAudio::create_objects");
- add_tool(new BC_Title(x, y, "Bitrate (kbps):"));
+ add_tool(new BC_Title(x, y, _("Bitrate (kbps):")));
AC3ConfigAudioBitrate *bitrate;
add_tool(bitrate =
new AC3ConfigAudioBitrate(this,