Skip to content

Commit 83a63a6

Browse files
author
Clement Champetier
committed
FormatContext: fix destructor by freeing the streams added
* According to the doc, for each new stream added, the user should call avcodec_close and avformat_free_context. * See the doc: https://www.ffmpeg.org/doxygen/3.0/group__lavf__core.html#gadcb0fd3e507d9b58fe78f61f8ad39827
1 parent 0654fa8 commit 83a63a6

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/AvTranscoder/file/FormatContext.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ FormatContext::FormatContext(const std::string& filename, int req_flags, AVDicti
1212
, _options()
1313
, _isOpen(false)
1414
{
15-
int ret = avformat_open_input(&_avFormatContext, filename.c_str(), NULL, options);
15+
const int ret = avformat_open_input(&_avFormatContext, filename.c_str(), NULL, options);
1616
if(ret < 0)
1717
{
1818
std::string msg = "Unable to open file ";
@@ -44,10 +44,14 @@ FormatContext::~FormatContext()
4444
if(!_avFormatContext)
4545
return;
4646

47+
// free the streams added
48+
for(unsigned int i = 0; i < _avFormatContext->nb_streams; ++i)
49+
avcodec_close(_avFormatContext->streams[i]->codec);
50+
51+
// free the format context
4752
if(_isOpen)
4853
avformat_close_input(&_avFormatContext);
49-
else
50-
avformat_free_context(_avFormatContext);
54+
avformat_free_context(_avFormatContext);
5155
_avFormatContext = NULL;
5256
}
5357

src/AvTranscoder/file/FormatContext.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ class AvExport FormatContext
2323
public:
2424
/**
2525
* @brief Allocate an AVFormatContext by opening an input file
26+
* @see InputFile
2627
*/
2728
FormatContext(const std::string& filename, int req_flags = 0, AVDictionary** options = NULL);
2829

2930
/**
3031
* @brief Allocate an AVFormatContext with default values
32+
* @see OutputFile
3133
*/
3234
FormatContext(int req_flags = 0);
3335

0 commit comments

Comments
 (0)