[Ffmpeg-devel] Where to free the memory (AVCodecContext) allocated by avcodec_alloc_context()?

QuickTime ffmpeg
Tue Oct 4 08:02:50 CEST 2005


Dear maillisters:
Where to free the memory (AVCodecContext) allocated by avcodec_alloc_context()?
Please check the following codes!

//@file:output_example.c
/*
 * add an audio output stream
 */
AVStream *add_audio_stream(AVFormatContext *oc, int codec_id)
{
    AVCodecContext *c;
    AVStream *st;

    st = av_new_stream(oc, 1);
    if (!st) {
        fprintf(stderr, "Could not alloc stream\n");
        exit(1);
    }

    c = st->codec;
    c->codec_id = codec_id;
    c->codec_type = CODEC_TYPE_AUDIO;

    /* put sample parameters */
    c->bit_rate = 64000;
    c->sample_rate = 44100;
    c->channels = 2;
    return st;
}

//------------------------------
//@file:libavcodec/utils.c

AVStream *av_new_stream(AVFormatContext *s, int id)
{
    AVStream *st;

    if (s->nb_streams >= MAX_STREAMS)
        return NULL;

    st = av_mallocz(sizeof(AVStream));
    if (!st)
        return NULL;

    st->codec= avcodec_alloc_context();//allocated here
    if (s->iformat) {
        /* no default bitrate if decoding */
        st->codec->bit_rate = 0;
    }
    st->index = s->nb_streams;
    st->id = id;
    st->start_time = AV_NOPTS_VALUE;
    st->duration = AV_NOPTS_VALUE;
    st->cur_dts = AV_NOPTS_VALUE;

    /* default pts settings is MPEG like */
    av_set_pts_info(st, 33, 1, 90000);
    st->last_IP_pts = AV_NOPTS_VALUE;

    s->streams[s->nb_streams++] = st;
    return st;
}

but "st->codec" might be never freed, because the following codes:

//@ output_example.c
void close_audio(AVFormatContext *oc, AVStream *st)
{
    avcodec_close(st->codec);

    av_free(samples);
    av_free(audio_outbuf);
}
//---------------------------
@file:libavcodec/utils.c
int avcodec_close(AVCodecContext *avctx)
{
    entangled_thread_counter++;
    if(entangled_thread_counter != 1){
        av_log(avctx, AV_LOG_ERROR, "insufficient thread locking
around avcodec_open/close()\n");
        entangled_thread_counter--;
        return -1;
    }

    if (avctx->codec->close)
        avctx->codec->close(avctx);
    avcodec_default_free_buffers(avctx);
    av_freep(&avctx->priv_data);
    avctx->codec = NULL;
    entangled_thread_counter--;
    return 0;
}

So the "avcodec_close(...)" do nothing for freeing the "avctx"!
The same thing occurs in "ffmpeg.c"....

Maybe I am wrong at some points!
Does anyone like to explain the logic in detail for me?
Thanks!





More information about the ffmpeg-devel mailing list