[Libav-user] transcoder class instance memory leak. (more simplified code)
강구철
kckang at skycom.ne.kr
Fri Nov 23 08:55:15 EET 2018
hear is transcoder class src. use it as a instance and memory leak after
delete instance.
every resource release and close. what's problem?
main transact func is "tcode(opus pkt in, aac pkt out)"
///DECLARE
AVCodecContext *oc;
AVCodecContext *opus_context;
AVCodecContext *aac_context;
AVCodecContext *aac_context2;
AVCodecContext *input_codec_context;
AVFormatContext *output_format_context;
const AVCodec *opus_codec;
const AVCodec *aac_codec;
const AVCodec *aac_codec2;
const AVCodec *h264_codec;
float AAC_ENCTIME;
int AAC_SAMPRATE;
int OPUS_SAMPRATE;
int OPUSsize;
int OPUScnt ;
int AACsize ;
int AACcnt ;
AVFrame *decoded_frame;///opus decode by ffmpeg
AVFrame *decoded_frame2;///opus decode by ffmpeg
AVFrame *framebuf;
AVPacket *pkt1;
AVPacket *pkt2;
AVAudioFifo *fifo;
AVAudioFifo *fifo2;
SwrContext *resample_context;
RTPPaserUtilityLibrary::ConvertStreamAVGranules* _instance;
int len;
string outfile;
int pcmsize;
uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
char out_buf [2000]; //from file
int out_size;
uint8_t *data;
size_t data_size;
int ix1;
int channel;
int ret;
int cnt[100];
int init(int a, int b, int c);
trans();
~trans();
int push_q(AVAudioFifo *fifo,
uint8_t **converted_input_samples,
const int frame_size);
int pop_q(AVAudioFifo *_queue, AVFrame* mframe);
int tcode(string *src, string* dst);
int tcode(AVPacket* src, AVPacket* dst);
};
int RTP_REASSEMBLE::trans::init(int a, int b, int c)
{
opus_context->channels = 1;
opus_context->channel_layout = AV_CH_LAYOUT_MONO;
opus_context->sample_rate = OPUS_SAMPRATE;
opus_context->sample_fmt = AV_SAMPLE_FMT_FLTP;
opus_context->bit_rate = OPUS_SAMPRATE;
aac_context->channels = 1;
aac_context->channel_layout = AV_CH_LAYOUT_MONO;
aac_context->sample_rate = AAC_SAMPRATE;
aac_context->sample_fmt = AV_SAMPLE_FMT_FLTP;
aac_context->bit_rate = AAC_SAMPRATE;
fifo = av_audio_fifo_alloc(aac_context->sample_fmt, aac_context-
>channels, 2048);
aac_context2->channels = 1;
aac_context2->channel_layout = AV_CH_LAYOUT_MONO;
aac_context2->sample_rate = AAC_SAMPRATE;
aac_context2->sample_fmt = AV_SAMPLE_FMT_FLTP;
aac_context2->bit_rate = AAC_SAMPRATE;
return 0;
};
RTP_REASSEMBLE::trans::trans(){
oc = NULL;
opus_context = NULL;
aac_context = NULL;
input_codec_context = NULL;
output_format_context = NULL;
OPUSsize = 0;
OPUScnt = 0;
AACsize = 0;
h264_codec = NULL;
AACcnt = 0;
decoded_frame = NULL;///opus decode by ffmpeg
decoded_frame2 = NULL;///opus decode by ffmpeg
fifo = NULL;
//fifo2 = NULL;
resample_context = NULL;
AAC_SAMPRATE=48000;
OPUS_SAMPRATE=48000;
ix1=0;
ret = AVERROR_EXIT;
avcodec_register_all();
h264_codec = avcodec_find_decoder( AV_CODEC_ID_H264 );
opus_codec = avcodec_find_decoder( AV_CODEC_ID_OPUS );
aac_codec = avcodec_find_encoder( AV_CODEC_ID_AAC );
aac_codec2 = avcodec_find_decoder( AV_CODEC_ID_AAC );// ENCODER
//aac_codec = avcodec_find_encoder( V_CODEC_ID_AAC_LATM );
framebuf = av_frame_alloc();
if(framebuf==NULL) cout << "frame alloc fail."<<endl;
_instance = new RTPPaserUtilityLibrary::ConvertStreamAVGranules();
pkt1 = av_packet_alloc();
pkt2 = av_packet_alloc();
if (!h264_codec) {
cout << "H264 Codec not found"<<endl;
return;
}
else cout << "H264 codec found."<<endl;
if(framebuf==NULL) cout << "frame alloc fail."<<endl;
if (!opus_codec) {
cout << "opus Codec not found"<<endl;
return;
}
else cout << "OPUS codec found."<<endl;
opus_context = avcodec_alloc_context3( opus_codec );
//_instance->init_fifo(&fifo, opus_context);
if (!opus_context) {
cout << "Could not allocate audio opus_codec context
OPUS"<<endl;
return;
}
outfile = "raw.mp4";
if (!aac_codec) {
cout << "AAC Codec not found"<<endl;
return;
}
else cout << "AAC codec found."<<endl;
aac_context = avcodec_alloc_context3(aac_codec);
//_instance->init_fifo(&fifo2, aac_context);
if (!aac_context) {
cout << "Could not allocate audio codec context AAC"<<endl;
return;
}
aac_context2 = avcodec_alloc_context3(aac_codec2);
init(1,2,3);
if (avcodec_open2(opus_context, opus_codec, NULL) < 0) {
cout << "Could not open codec opus"<<endl;
return;
}
if (avcodec_open2(aac_context, aac_codec, NULL) < 0) {
cout << "Could not open codec aac"<<endl;
return;
}
if (avcodec_open2(aac_context2, aac_codec2, NULL) < 0) {
cout << "Could not open codec aac2"<<endl;
return;
}
if (!decoded_frame)
{
cout << "Got a decoded_frame"<<endl;
if (!(decoded_frame = av_frame_alloc())) {
cout << "Could not allocate audio frame"<<endl;
return;
}
}else{
cout << "nodata decoded_frame"<<endl;
}
if (!decoded_frame2)
{
cout << "Got a decoded_frame2"<<endl;
if (!(decoded_frame2 = av_frame_alloc())) {
cout << "Could not allocate audio frame"<<endl;
return;
}
}else{
cout << "nodata decoded_frame2"<<endl;
}
}
RTP_REASSEMBLE::trans::~trans(){
OsSysLog::add(FAC_PROCESS, PRI_INFO, "%s:%d:FREE
FIFO************************* ",__FILE__,__LINE__);
av_free_packet(pkt1);
av_free_packet(pkt2);
av_frame_free(&decoded_frame);
av_frame_free(&decoded_frame2);
av_audio_fifo_free(fifo);
}
/** Add converted input audio samples to the FIFO buffer for later
processing. */
int RTP_REASSEMBLE::trans::push_q(AVAudioFifo *fifo,
uint8_t **converted_input_samples,
const int frame_size)
{
int error;
/**
* Make the FIFO as large as it needs to be to hold both,
* the old and the new samples.
*/
//OsSysLog::add(FAC_PROCESS, PRI_INFO,
"%s:%d:*************************PUSH called fifo:0x%x, data:0x%x,
frame_size:%d",__FILE__,__LINE__,(int64_t)fifo,
(void*)**converted_input_samples, frame_size);
if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) +
frame_size)) < 0) {
return error;
}
/** Store the new samples in the FIFO buffer. */
if (av_audio_fifo_write(fifo, (void **)converted_input_samples,
frame_size) < frame_size) {
return AVERROR_EXIT;
}
//OsSysLog::add(FAC_PROCESS, PRI_INFO,
"%s:%d:*************************OK PUSH:%d tot:%d", __FILE__, __LINE__,
frame_size, av_audio_fifo_size(fifo));
return 1;
}
int RTP_REASSEMBLE::trans::pop_q(AVAudioFifo *_fifo, AVFrame* mframe)
{
int ret;
if( av_audio_fifo_size (_fifo) >= 1024)
{
void* ptr;
ptr = * mframe->data;
ret = av_audio_fifo_read(_fifo, (void**)mframe->data, 1024);//@#@#
mframe->nb_samples = ret;
return ret;
}
else
{
//OsSysLog::add(FAC_PROCESS, PRI_INFO, "%s:%d:*Not enough data
popup ret:%d tot:%d",__FILE__,__LINE__, ret, av_audio_fifo_size(_fifo));
return 0;
}
}
int RTP_REASSEMBLE::trans::tcode(AVPacket* src, AVPacket* dst){
//return -1;//KKC 8888
AACContext *ac = (AACContext*)aac_context->priv_data;
MPEG4AudioConfig *m4ac = &(ac->oc[0].m4ac);
m4ac->frame_length_short = 1;//1:960, 0:1024
int ret;
FILE *f, *outfile;
int data_size;
data_size = av_get_bytes_per_sample(opus_context->sample_fmt);
if (src->size){
data_present = 0;
if(decoded_frame2->data[0]!=NULL)
av_free(decoded_frame2->data[0]);
decoded_frame2->data[0] = (uint8_t*)av_malloc(4*1024);//@#@#
ret = avcodec_decode_audio4(opus_context, decoded_frame,
&data_present, src);
if(ret<0)
{
//OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d DECODE Error
======%d", __FILE__, __LINE__,ret);
return ret;
}
av_init_packet(dst);
dst->data = NULL;//IMPORTANT for dynamic reallocate surface
dst->size = 0;ret = -1;
////////////////////////////////////////////////////////////////////////////
///
//int push_q(AVAudioFifo *fifo, uint8_t **converted_input_samples,
const int frame_size);
//int pop_q(AVAudioFifo *fifo, AVFrame* mframe);
////////////////////////////////////////////////////////////////////////////
/
uint8_t* ptr;
//ptr =
push_q(fifo, decoded_frame->data, decoded_frame->nb_samples);//PUSH
decoded_frame2->nb_samples = 1024;
ret = pop_q (fifo, decoded_frame2); //POP
//decoded_frame->nb_samples = 1024;
//aac_context->frame_size = 960;
aac_context->frame_size = 1024;
//OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d AAC ENCODE try
decodedframeSize:%d aacctxFramesieze:%d======frameLnShort:%d
privdataSIZE:0x%x", __FILE__, __LINE__, decoded_frame2->nb_samples,
aac_context->frame_size, m4ac->frame_length_short,aac_context->priv_data);
if(ret>0){// pop ok (que hav sample over 1024)
ret = avcodec_encode_audio2(aac_context, dst, decoded_frame2,
&data_present);//@#@#AAC ENCODING
}
}
return 0;
}
KangKooChul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20181123/bf8ac9cf/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.gif
Type: image/gif
Size: 8057 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20181123/bf8ac9cf/attachment.gif>
More information about the Libav-user
mailing list