This is my simple ffmpeg code.
I`m reading a .ogg from the file.
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
int main(int argc, char **argv){
av_register_all();
avcodec_register_all();
static const char *src_filename = NULL;
static AVFormatContext *input_format_context = NULL;
int ret; int error;
src_filename = argv[1];
//open audio file
if ((error = avformat_open_input(&input_format_context, src_filename, NULL, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
return ret;
}else{
fprintf(stderr, "open file + \n");
}
and then call av_format_stream_info function.
// find stream info
error = avformat_find_stream_info(input_format_context, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
}else{
fprintf(stderr, "find stream + \n");
}
everything is ok so far. but now when i call the avcodec_find_decoder function i get the segmentation fault.
for (int i=0; i < input_format_context->nb_streams; i++)
{
fprintf(stderr, "1.1\n");
AVStream *stream = input_format_context->streams[0];
fprintf(stderr, "1.2\n");
AVCodec *dec = avcodec_find_decoder(stream->codecpar->codec_id);
fprintf(stderr, "1.3\n");
}
return 0;
}
And this is my cmake:
cmake_minimum_required(VERSION 2.6)
project(decode_encode)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-D__STDC_CONSTANT_MACROS")
find_path(AVCODEC_INCLUDE_DIR libavcodec/avcodec.h)
find_library(AVCODEC_LIBRARY avcodec)
find_path(AVFORMAT_INCLUDE_DIR libavformat/avformat.h)
find_library(AVFORMAT_LIBRARY avformat)
find_path(AVUTIL_INCLUDE_DIR libavutil/avutil.h)
find_library(AVUTIL_LIBRARY avutil)
find_path(AVDEVICE_INCLUDE_DIR libavdevice/avdevice.h)
find_library(AVDEVICE_LIBRARY avdevice)
add_executable(readFewFrame readFewFrame.c)
target_include_directories(readFewFrame PRIVATE ${AVCODEC_INCLUDE_DIR} ${AVFORMAT_INCLUDE_DIR} ${AVUTIL_INCLUDE_DIR} ${AVDEVICE_INCLUDE_DIR})
target_link_libraries(readFewFrame PRIVATE ${AVCODEC_LIBRARY} ${AVFORMAT_LIBRARY} ${AVUTIL_LIBRARY} ${AVDEVICE_LIBRARY})
what exactly could be the cause of this error?