I'm trying to write an utility dealing with ffmpeg. Once i need to copy image plane from one pointer to another. From AVPicture structure to my own. Here is some sources.
My own frame structe. Memory allocated in constructor, deallocated in destructor
template <class DataType>
struct Frame
{
DataType* data; //!< Pointer to image data
int f_type; //!< Type of color space (ex. RGB, HSV, YUV)
int timestamp; //!< Like ID of frame. Time of this frame in the video file
int height; //!< Height of frame
int width; //!< Width of frame
Frame(int _height, int _width, int _f_type=0):
height(_height),width(_width),f_type(_f_type)
{
data = new DataType[_width*_height*3];
}
~Frame()
{
delete[] data;
}
};
Here is the main loop performing conversion. If the line with memcpy is commented, there are no memory leaks at all. But if I uncomment it, the memory leak are present.
for(int i = begin; i < end; i++)
{
AVPicture pict;
avpicture_alloc(&pict, PIX_FMT_BGR24, _width, _height);
std::shared_ptr<Frame<char>> frame(new Frame<char>(_height, _width, (int)PIX_FMT_BGR24));
sws_scale(ctx, frame_list[i]->data, frame_list[i]->linesize, 0, frame_list[i]->height, pict.data, pict.linesize);
memcpy(frame->data,pict.data[0],_width*_height*3);
//temp_to_add->push_back(std::shared_ptr<Frame<char>>(frame));
avpicture_free(&pict);
}
I've been trying lot of things such as: allocating memory via malloc and deallocating via free, copying memory from pict to frame by hands (in for loop), using std::copy and avpicture_layout that is ffmpeg helper function. Nothing helps. So the question: do I forget about something important?
I will be grateful for every answer.