<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body><div>why should we debug your code?</div><div><br></div><div><br></div><div><br></div><div id="composer_signature"><div style="font-size:85%;color:#575757" dir="auto">Verzonden vanaf mijn Samsung Galaxy-smartphone.</div></div><div><br></div><div style="font-size:100%;color:#000000"><!-- originalMessage --><div>-------- Oorspronkelijk bericht --------</div><div>Van: 강구철 <kckang@skycom.ne.kr> </div><div>Datum: 23-11-18 07:38 (GMT+01:00) </div><div>Aan: "'This list is about using libavcodec, libavformat, libavutil, libavdevice and libavfilter.'" <libav-user@ffmpeg.org> </div><div>Onderwerp: [Libav-user] transcoder class instance memory leak. </div><div><br></div></div>hear is transcoder class src. use it as a instance and memory leak after delete instance.<br>every resource release and close. what's problem? <br>main transact func is "tcode(opus pkt in, aac pkt out)"<br><br>class trans{<br> public:<br><br> ///DECLARE <br> AVCodecContext *oc;<br> AVCodecContext *opus_context;<br> AVCodecContext *aac_context;<br> AVCodecContext *aac_context2;<br> AVCodecContext *input_codec_context;<br> AVFormatContext *output_format_context;<br> const AVCodec *opus_codec;<br> const AVCodec *aac_codec;<br> const AVCodec *aac_codec2;<br> const AVCodec *h264_codec;<br> float AAC_ENCTIME;<br> int AAC_SAMPRATE;<br> int OPUS_SAMPRATE;<br> int OPUSsize;<br> int OPUScnt ;<br> int AACsize ;<br> int AACcnt ;<br> AVFrame *decoded_frame;///opus decode by ffmpeg<br> AVFrame *decoded_frame2;///opus decode by ffmpeg<br> AVFrame *framebuf;<br> AVPacket *pkt1;<br> AVPacket *pkt2;<br> int data_present;<br> CUnitInspection *gen1;<br> CUnitInspection *gen2;<br><br> AVAudioFifo *fifo;<br> AVAudioFifo *fifo2;<br> SwrContext *resample_context;<br> RTPPaserUtilityLibrary::ConvertStreamAVGranules* _instance;<br><br> int len;<br> string outfile;<br> int pcmsize;<br> uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];<br> char out_buf [2000]; //from file<br> int out_size;<br> uint8_t *data;<br> size_t data_size;<br> int ix1;<br> int channel;<br> int ret;<br> int cnt[100];<br> int init(int a, int b, int c);<br> trans();<br> ~trans();<br><br> int push_q(AVAudioFifo *fifo,<br> uint8_t **converted_input_samples,<br> const int frame_size);<br><br> int pop_q(AVAudioFifo *_queue, AVFrame* mframe);<br><br><br> int tcode(string *src, string* dst);<br> int tcode(AVPacket* src, AVPacket* dst);<br><br>};<br><br>int RTP_REASSEMBLE::trans::init(int a, int b, int c)<br>{<br> opus_context->channels = 1;<br> opus_context->channel_layout = AV_CH_LAYOUT_MONO;<br> opus_context->sample_rate = OPUS_SAMPRATE;<br> opus_context->sample_fmt = AV_SAMPLE_FMT_FLTP;<br> opus_context->bit_rate = OPUS_SAMPRATE;<br><br> aac_context->channels = 1;<br> aac_context->channel_layout = AV_CH_LAYOUT_MONO;<br> aac_context->sample_rate = AAC_SAMPRATE;<br> aac_context->sample_fmt = AV_SAMPLE_FMT_FLTP;<br> aac_context->bit_rate = AAC_SAMPRATE;<br> fifo = av_audio_fifo_alloc(aac_context->sample_fmt, aac_context->channels, 2048);<br> aac_context2->channels = 1;<br> aac_context2->channel_layout = AV_CH_LAYOUT_MONO;<br> aac_context2->sample_rate = AAC_SAMPRATE;<br> aac_context2->sample_fmt = AV_SAMPLE_FMT_FLTP;<br> aac_context2->bit_rate = AAC_SAMPRATE;<br><br><br> return 0;<br>};<br><br>RTP_REASSEMBLE::trans::trans(){<br> oc = NULL;<br> opus_context = NULL;<br> aac_context = NULL;<br> input_codec_context = NULL;<br> output_format_context = NULL;<br> OPUSsize = 0;<br> OPUScnt = 0;<br> AACsize = 0;<br> h264_codec = NULL;<br> AACcnt = 0;<br> decoded_frame = NULL;///opus decode by ffmpeg<br> decoded_frame2 = NULL;///opus decode by ffmpeg<br> fifo = NULL;<br> //fifo2 = NULL;<br> resample_context = NULL;<br> AAC_SAMPRATE=48000;<br> OPUS_SAMPRATE=48000;<br> ix1=0;<br> ret = AVERROR_EXIT;<br> avcodec_register_all();<br> h264_codec = avcodec_find_decoder( AV_CODEC_ID_H264 ); <br> opus_codec = avcodec_find_decoder( AV_CODEC_ID_OPUS ); <br> aac_codec = avcodec_find_encoder( AV_CODEC_ID_AAC ); <br> aac_codec2 = avcodec_find_decoder( AV_CODEC_ID_AAC );// ENCODER <br> //aac_codec = avcodec_find_encoder( V_CODEC_ID_AAC_LATM ); <br><br> framebuf = av_frame_alloc();<br> if(framebuf==NULL) cout << "frame alloc fail."<<endl;<br> _instance = new RTPPaserUtilityLibrary::ConvertStreamAVGranules();<br> pkt1 = av_packet_alloc();<br> pkt2 = av_packet_alloc();<br><br> if (!h264_codec) {<br> cout << "H264 Codec not found"<<endl;<br> return;<br> }<br> else cout << "H264 codec found."<<endl;<br><br> if(framebuf==NULL) cout << "frame alloc fail."<<endl;<br>/*<br> gen1 = new CUnitInspection();<br> gen2 = new CUnitInspection();<br><br> gen1-> ifile = "opus.rtp";<br> gen1->_begin();<br><br> gen2-> ifile = "264.rtp";<br> gen2->_begin();<br>*/<br> if (!opus_codec) {<br> cout << "opus Codec not found"<<endl;<br> return;<br> }<br><br> else cout << "OPUS codec found."<<endl;<br><br> opus_context = avcodec_alloc_context3( opus_codec );<br> //_instance->init_fifo(&fifo, opus_context);<br><br> if (!opus_context) {<br> cout << "Could not allocate audio opus_codec context OPUS"<<endl;<br> return;<br> }<br><br> outfile = "raw.mp4";<br> if (!aac_codec) {<br> cout << "AAC Codec not found"<<endl;<br> return;<br> }<br> else cout << "AAC codec found."<<endl;<br><br> aac_context = avcodec_alloc_context3(aac_codec);<br> //_instance->init_fifo(&fifo2, aac_context);<br><br> if (!aac_context) {<br> cout << "Could not allocate audio codec context AAC"<<endl;<br> return;<br> }<br><br> aac_context2 = avcodec_alloc_context3(aac_codec2);<br> init(1,2,3);<br><br> if (avcodec_open2(opus_context, opus_codec, NULL) < 0) {<br> cout << "Could not open codec opus"<<endl;<br> return;<br> }<br><br> if (avcodec_open2(aac_context, aac_codec, NULL) < 0) {<br> cout << "Could not open codec aac"<<endl;<br> return;<br> }<br><br> if (avcodec_open2(aac_context2, aac_codec2, NULL) < 0) {<br> cout << "Could not open codec aac2"<<endl;<br> return;<br> }<br><br> if (!decoded_frame) <br> {<br> cout << "Got a decoded_frame"<<endl;<br> if (!(decoded_frame = av_frame_alloc())) {<br> cout << "Could not allocate audio frame"<<endl;<br> return;<br> }<br> }else{<br> cout << "nodata decoded_frame"<<endl;<br> }<br><br> if (!decoded_frame2) <br> {<br> cout << "Got a decoded_frame2"<<endl;<br> if (!(decoded_frame2 = av_frame_alloc())) {<br> cout << "Could not allocate audio frame"<<endl;<br> return;<br> }<br> }else{<br> cout << "nodata decoded_frame2"<<endl;<br> }<br><br>}<br><br>RTP_REASSEMBLE::trans::~trans(){<br>OsSysLog::add(FAC_PROCESS, PRI_INFO, "%s:%d:FREE FIFO************************* ",__FILE__,__LINE__); <br> // delete gen1;<br> // delete gen2;<br> av_free_packet(pkt1);<br> av_free_packet(pkt2);<br> av_frame_free(&decoded_frame);<br> av_frame_free(&decoded_frame2);<br> av_audio_fifo_free(fifo);<br> //av_audio_fifo_free(fifo2);<br> delete _instance;<br>}<br><br>/** Add converted input audio samples to the FIFO buffer for later processing. */<br>int RTP_REASSEMBLE::trans::push_q(AVAudioFifo *fifo,<br> uint8_t **converted_input_samples,<br> const int frame_size)<br>{<br> int error;<br> /**<br> * Make the FIFO as large as it needs to be to hold both,<br> * the old and the new samples.<br> */<br> //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); <br><br> if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {<br> return error;<br> }<br> /** Store the new samples in the FIFO buffer. */<br> if (av_audio_fifo_write(fifo, (void **)converted_input_samples, frame_size) < frame_size) {<br> return AVERROR_EXIT;<br> }<br> //OsSysLog::add(FAC_PROCESS, PRI_INFO, "%s:%d:*************************OK PUSH:%d tot:%d", __FILE__, __LINE__, frame_size, av_audio_fifo_size(fifo));<br> return 1;<br>}<br><br><br>int RTP_REASSEMBLE::trans::pop_q(AVAudioFifo *_fifo, AVFrame* mframe)<br>{<br> int ret;<br> if( av_audio_fifo_size (_fifo) >= 1024)<br> {<br> void* ptr;<br> ptr = * mframe->data;<br> ret = av_audio_fifo_read(_fifo, (void**)mframe->data, 1024);//@#@#<br> mframe->nb_samples = ret;<br> return ret;<br> }<br> else <br> {<br> //OsSysLog::add(FAC_PROCESS, PRI_INFO, "%s:%d:*Not enough data popup ret:%d tot:%d",__FILE__,__LINE__, ret, av_audio_fifo_size(_fifo));<br> return 0;<br> }<br>}<br><br>int RTP_REASSEMBLE::trans::tcode(AVPacket* src, AVPacket* dst){<br>//return -1;//KKC 8888<br> AACContext *ac = (AACContext*)aac_context->priv_data;<br><br> /*<br> static int aac_decode_er_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, GetBitContext *gb)<br> {<br> AACContext *ac = avctx->priv_data;<br> const MPEG4AudioConfig *const m4ac = &ac->oc[1].m4ac;<br> ChannelElement *che;<br> int err, i;<br> int samples = m4ac->frame_length_short ? 960 : 1024;<br> int chan_config = m4ac->chan_config;<br> int aot = m4ac->object_type;<br> <br> if (aot == AOT_ER_AAC_LD || aot == AOT_ER_AAC_ELD)<br> samples >>= 1;<br> }*/<br> <br> MPEG4AudioConfig *m4ac = &(ac->oc[0].m4ac);<br> m4ac->frame_length_short = 1;//1:960, 0:1024<br> int ret;<br> FILE *f, *outfile;<br> int data_size;<br> data_size = av_get_bytes_per_sample(opus_context->sample_fmt);<br><br> if (src->size){<br> data_present = 0;<br> //decoded_frame->linesize[0]=1024*4;<br> //decoded_frame->linesize[1]=1024*4;<br> //opus_context->frame_size=960;//TESTTEST<br> //decoded_frame->data[0] = (uint8_t*)av_malloc(4*1024);//TESTTEST<br><br> if(decoded_frame2->data[0]!=NULL)<br> av_free(decoded_frame2->data[0]);<br><br> decoded_frame2->data[0] = (uint8_t*)av_malloc(4*1024);//@#@#<br> ret = avcodec_decode_audio4(opus_context, decoded_frame, &data_present, src);<br><br> if(ret<0) <br> {<br> //OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d DECODE Error ======%d", __FILE__, __LINE__,ret);<br> return ret;<br> }<br>#ifdef _NOUSE<br> else<br> {<br> if(data_present){<br> if(channel%2)//7777<br> outfile = fopen("/temp/abc1.wav", "a");<br> else<br> outfile = fopen("/temp/abc2.wav", "a");<br><br> if (!outfile) {<br> OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d abc.wav open Error ======", __FILE__, __LINE__);<br> //return 0;<br> }<br><br> //av_freep(&decoded_frame);//TESTTEST<br><br> unsigned char cbuf[4]={0,0,0,0};//GEN WAVE------------------;<br> unsigned char *pbuf;<br> cbuf[0]=0xf2;<br> cbuf[1]=0xdb;<br> cbuf[2]=0x0;<br> cbuf[3]=0x3f;<br><br> //decoded_frame->data[0] = (uint8_t*)av_malloc(4*1024);//BLANK 1024<br> //if(1) decoded_frame->nb_samples = 960;//good for mp4<br> //else decoded_frame->nb_samples = 1024;//good for wav space<br> <br> pbuf = decoded_frame->data[0];<br><br>#ifdef _NOUSE<br> for(int i=960; i<1024 ; i++){<br>// OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d i:%d ======", __FILE__, __LINE__, i*4+3);<br> pbuf[i*4+0] = cbuf[0]; //float<br> pbuf[i*4+1] = cbuf[1]; //float<br> pbuf[i*4+2] = cbuf[2]; //float<br> pbuf[i*4+3] = cbuf[3]; //float<br> cbuf[2] += 2; //mod wav to 0~96<br> if(cbuf[2] >= 96) cbuf[2]=0; //mod reset to 0<br> }//END OF GENWAVE---------------------------------------------------------<br>#endif<br><br> fwrite(decoded_frame->data[0] , decoded_frame->nb_samples*data_size, 1, outfile);//opus_context->channels*<br> fclose(outfile); <br><br>//OsSysLog::add(FAC_PROCESS,PRI_INFO,"LN:%d Ddata_present=%d OPUS DECODE Success === duration:%d ret(size):%d linesize:%d nbsampels:%d", __LINE__, data_present, decoded_frame->pkt_duration, ret,decoded_frame->linesize,decoded_frame->nb_samples);<br><br> }//data present growes up and cut off it.<br> }<br>#endif<br> <br>//#ifdef _VIDEODUMP// test file write<br><br><br>//OsSysLog::add(FAC_PROCESS, PRI_INFO, "BAAAAAAAAAAAAAAAAAAAAA ch=%d,samp=%d, fmt=%d, data_size=%d ch=%d chLayout=%ld frame_size=%d\n", \<br> opus_context->channels, decoded_frame->nb_samples, opus_context->sample_fmt, data_size, opus_context->channels, opus_context->channel_layout,opus_context->frame_size);<br><br> OPUSsize += decoded_frame->nb_samples * opus_context->channels * data_size;<br>//OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d RAW PCMsize= %d, opusCNT= %d decodeRAW:%d", __FILE__, __LINE__, OPUSsize, OPUScnt++, decoded_frame->nb_samples);<br>//#endif<br> <br> av_init_packet(dst);<br> dst->data = NULL;//IMPORTANT for dynamic reallocate surface<br> dst->size = 0;ret = -1;<br> /////////////////////////////////////////////////////////////////////////////// <br> //int push_q(AVAudioFifo *fifo, uint8_t **converted_input_samples, const int frame_size);<br> //int pop_q(AVAudioFifo *fifo, AVFrame* mframe);<br> /////////////////////////////////////////////////////////////////////////////<br> uint8_t* ptr;<br> //ptr = <br> push_q(fifo, decoded_frame->data, decoded_frame->nb_samples);//PUSH <br> decoded_frame2->nb_samples = 1024;<br> ret = pop_q (fifo, decoded_frame2); //POP<br> <br> //decoded_frame->nb_samples = 1024;<br> //aac_context->frame_size = 960;<br> aac_context->frame_size = 1024;<br><br> //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);<br> if(ret>0){// pop ok (que hav sample over 1024)<br> ret = avcodec_encode_audio2(aac_context, dst, decoded_frame2, &data_present);//@#@#AAC ENCODING<br><br>#ifdef _AAC_ENCODE_RESULT_PRINT<br> if(ret != 0) <br> {<br> OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d AAC ENCODE Error OK ret:%d ======", __FILE__, __LINE__, ret);<br> }else{<br> OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d AAC ENCODE OK size is %d AACcnt:%d AACsize:%d aacctxFramesize:%d======", __FILE__, __LINE__, dst->size, AACcnt, AACsize,aac_context->frame_size);<br> AACsize += (int)(dst->size);<br><br> if(pkt2->size != 0)<br> AACcnt++;<br> }<br>#endif<br> }<br><br>}<br><br>#ifdef _DECODE_VERIFY_AAC<br> ret = avcodec_decode_audio4(aac_context2, decoded_frame, &data_present, dst);<br> //if(ret<1) OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d AAC REDECODE Error ======%d", __FILE__, __LINE__,ret);<br>#endif<br><br>#ifdef _DUMP_REDECODE<br> if(ret<0) <br> {<br> OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d AAC REDECODE Error ======%d", __FILE__, __LINE__,ret);<br> //return ret;<br> }else{<br> if(data_present){<br> //OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d AAC REDECODE ====%d frsize:%d", __FILE__, __LINE__,ret, decoded_frame->nb_samples);<br> if(channel%2)//7777<br> outfile = fopen("/temp/aac1.wav", "a+");<br> else<br> outfile = fopen("/temp/aac2.wav", "a+");<br> <br> if (!outfile) {<br> OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d aac.wav open Error ======", __FILE__, __LINE__);<br> return 0;<br> }<br><br> fwrite(decoded_frame->data[0] , /*decoded_frame->nb_samples * opus_context->channels */1024 * data_size, 1, outfile);//opus_context->channels*<br> fclose(outfile); <br><br>OsSysLog::add(FAC_PROCESS,PRI_INFO,"LN:%d Ddata_present=%d DECODE Success === duration:%d ret(size):%d linesize:%d nbsampels:%d", __LINE__, data_present, decoded_frame->pkt_duration, ret,decoded_frame->linesize,decoded_frame->nb_samples);<br> }<br> else{<br> OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d DECODE no presents data!! ======", __FILE__, __LINE__);<br> return ret;<br> }<br> }<br>#endif<br> return 0;<br>}<br><br>int RTP_REASSEMBLE::trans::tcode(string *src, string* dst){<br><br> if(pkt1 == NULL)<br> {<br> OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d Error pkt1 NULL====data_size[%d]======", __FILE__, __LINE__, src->length());<br> init(1,2,3);<br> }<br><br> // COPY NEED , dont use STRING <br> pkt1->data = (uint8_t*)src->c_str();<br> pkt1->size = src->length();//solve source packet of opus sound encoded data<br> //memcpy(pkt1->data, src->c_str(), src->length());<br> tcode(pkt1,pkt2);<br><br> if(pkt2 == NULL)<br> {<br> OsSysLog::add(FAC_PROCESS,PRI_INFO,"%s:%d Error pkt2 NUL======data22_size[%d]=====", __FILE__, __LINE__, dst->length());<br> init(1,2,3);<br> }<br><br> dst->assign((uint8_t&)pkt2->data, pkt2->size);//build dst<br> if(pkt2->size==0){<br> OsSysLog::add(FAC_PROCESS,PRI_INFO, "%s:%d Error====data2_size zero[%d:%d]=======", __FILE__, __LINE__, src->length(), dst->length());<br> }<br> return 1;// protection code <br>}<br><br><br><br>_______________________________________________<br>Libav-user mailing list<br>Libav-user@ffmpeg.org<br>http://ffmpeg.org/mailman/listinfo/libav-user<br></body></html>