sarah john
sarah john

Reputation: 101

Segmentation fault when media played using libavcodec

Myself trying to play media using libavcodec as backend.I downloaded ffmpeg-2.0.1 and installed using ./configure,make and make install. While trying to run an application to play an audio file, i'm getting segmentation fault while checking for the first audio stream.my program is like

AVFormatContext* container = avformat_alloc_context();
if (avformat_open_input(&container, input_filename, NULL, NULL) < 0) {
    die(“Could not open file”);
}

if (av_find_stream_info(container) < 0) {
    die(“Could not find file info”);
}

av_dump_format(container, 0, input_filename, false);
int stream_id = -1;
int i;

for (i = 0; i < container->nb_streams; i++) {
    if (container->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
        stream_id = i;
        break;
    }
}

Segmentation fault occurs at if(container->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)

How can i fix this? I am working in ubuntu 12.04.

Upvotes: 1

Views: 1980

Answers (1)

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14510

You don't need to allocate your AVFormatContext at the begginning.

Also the function av_find_stream_info is deprecated, you have to change it to avformat_find_stream_info :

av_register_all();
avcodec_register_all();

AVFormatContext* container = NULL;
if (avformat_open_input(&container, input_filename, NULL, NULL) < 0) {
    die(“Could not open file”);
}

if (avformat_find_stream_info(container, NULL) < 0) {
    die(“Could not find file info”);
}

// av_dump_format(container, 0, input_filename, false);

int stream_id = -1;
int i;

for (i = 0; i < container->nb_streams; i++) {
    if (container->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO) {
        stream_id = i;
        break;
    }
}

Also I'm not sure that av_dump_format was useful here...


EDIT : Did you tried something like :

av_register_all();
avcodec_register_all();

AVFormatContext* container = NULL;
AVCodec *dec;

if ( avformat_open_input(&container, input_filename, NULL, NULL) < 0) {
    // ERROR
}

if ( avformat_find_stream_info(container, NULL) < 0) {
    // ERROR
}

/* select the audio stream */
if ( av_find_best_stream(container, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0) < 0 ) {
    // ERROR
}

Upvotes: 1

Related Questions