fix logic and possible memleak

Origin commit data
------------------
Branch: master
Commit: 6b9e79094a
Author: Jacek Jendrzej <overx300@gmail.com>
Date: 2017-11-17 (Fri, 17 Nov 2017)


------------------
No further description and justification available within origin commit message!

------------------
This commit was generated by Migit
This commit is contained in:
Jacek Jendrzej
2017-11-17 06:34:13 +01:00
committed by TangoCash
parent 54b4c2a877
commit a23d33b8d0

View File

@@ -220,16 +220,20 @@ int decode_frame(AVCodecContext *codecContext,AVPacket &packet, FILE* fp)
AVCodecContext* open_codec(AVMediaType mediaType, AVFormatContext* formatContext) AVCodecContext* open_codec(AVMediaType mediaType, AVFormatContext* formatContext)
{ {
int stream_index = av_find_best_stream(formatContext, mediaType, -1, -1, NULL, 0); int stream_index = av_find_best_stream(formatContext, mediaType, -1, -1, NULL, 0);
if (stream_index < 0){ if (stream_index >=0 ){
AVCodecContext * codecContext = formatContext->streams[stream_index]->codec;
if(codecContext){
AVCodec *codec = avcodec_find_decoder(codecContext->codec_id);
if(codec){
if ((avcodec_open2(codecContext, codec, NULL)) != 0){
return NULL; return NULL;
} }
AVCodecContext * codecContext = formatContext->streams[stream_index]->codec;
AVCodec *codec = avcodec_find_decoder(codecContext->codec_id);
if (codec && (avcodec_open2(codecContext, codec, NULL)) != 0){
return NULL;
} }
return codecContext; return codecContext;
} }
}
return NULL;
}
int image_to_mpeg2(const char *image_name, const char *encode_name) int image_to_mpeg2(const char *image_name, const char *encode_name)
{ {
@@ -253,11 +257,11 @@ int image_to_mpeg2(const char *image_name, const char *encode_name)
} }
fclose(fp); fclose(fp);
} }
avcodec_close(codecContext);
av_free_packet(&packet); av_free_packet(&packet);
} }
avformat_close_input(&formatContext); avcodec_close(codecContext);
} }
avformat_close_input(&formatContext);
} }
av_free(formatContext); av_free(formatContext);
return 0; return 0;