2014년 1월 8일 수요일

copy AVFrame using av_image_copy

AVFrame* clone_av_frame(AVFrame *frame)
{
    AVFrame *new_frame = avcodec_alloc_frame();
    enum AVPixelFormat fmt = (enum AVPixelFormat)frame->format;
    int ret = av_image_alloc(new_frame->data, new_frame->linesize, frame->width, frame-height, fmt, 1);
    if (ret < 0) {
        av_free(new_frame->data[0]);
        av_free(new_frame);
        return 0;
    }
    
    av_image_copy(new_frame->data, new_frame->linesize, (const uint8_t **)frame->data, 
                            frame->linesize, fmt, frame->width, frame->height);

    // copy to new frame
    new_frame->width = frame->width;
    new_frame->height = frame->height;
    new_frame->pict_type = frame->pict_type;
    new_frame->format = (int)fmt;
    ...

    return new_frame;
}

void free_av_frame(AVFrame *frame)
{
    if (frame) {
        av_free(frame->data[0]);
        av_free(frame);
    }
}