X-Git-Url: http://git.cinelerra-gg.org/git/?a=blobdiff_plain;f=cinelerra-5.1%2Fcinelerra%2Ffileac3.C;h=3b9810e99114b796ce1bf47e93b2aad1b5d14f80;hb=c6f91f271145ad9cd637f6b8f1560146178d3d41;hp=5d14e7139242b225fbdec8db5a25dc4564f6afe4;hpb=190b4810ec184c74ceab3fcd6faf09a91c92c53c;p=goodguy%2Fhistory.git diff --git a/cinelerra-5.1/cinelerra/fileac3.C b/cinelerra-5.1/cinelerra/fileac3.C index 5d14e713..3b9810e9 100644 --- a/cinelerra-5.1/cinelerra/fileac3.C +++ b/cinelerra-5.1/cinelerra/fileac3.C @@ -72,16 +72,12 @@ int FileAC3::reset_parameters_derived() 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) { @@ -124,23 +120,21 @@ int FileAC3::open_file(int rd, int wr) 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_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 ) { @@ -148,6 +142,7 @@ int FileAC3::open_file(int rd, int wr) 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; @@ -161,7 +156,7 @@ int FileAC3::open_file(int rd, int wr) 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; } } @@ -179,6 +174,7 @@ int FileAC3::close_file() } if(codec_context) { + encode_flush(); avcodec_close(codec_context); avcodec_free_context(&codec_context); codec = 0; @@ -195,11 +191,6 @@ int FileAC3::close_file() delete [] temp_raw; temp_raw = 0; } - if(temp_compressed) - { - delete [] temp_compressed; - temp_compressed = 0; - } reset_parameters(); FileBase::close_file(); return 0; @@ -229,6 +220,57 @@ int FileAC3::get_index(IndexFile *index_file, MainProgressBar *progress_bar) // { } // }; +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) { @@ -248,14 +290,6 @@ 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++) @@ -271,15 +305,11 @@ int FileAC3::write_samples(double **buffer, int64_t len) 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; @@ -296,18 +326,8 @@ int FileAC3::write_samples(double **buffer, int64_t len) 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); } @@ -317,12 +337,6 @@ int FileAC3::write_samples(double **buffer, int64_t len) (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; } @@ -334,7 +348,7 @@ int FileAC3::write_samples(double **buffer, int64_t len) 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, @@ -354,7 +368,7 @@ void AC3ConfigAudio::create_objects() 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,