简体   繁体   中英

av_write_header - Error with sample format

I'm writing program with libav/ffmpeg to download internet radio stream and play it on soundcard with alsa.

I've managed to download stream and extract packet and frame.

I'm having problem with av_write_header() function which (according to this https://www.ffmpeg.org/doxygen/3.2/group__lavf__encoding.html#details ) I must call. It crashes and gives me the following error:

[alsa @ 0x55d7ba32e580] sample format 0x15001 is not supported

Number 0x15001 is 86017 in decimal, which is index in enum AVCodecID of MP3 format(AV_CODEC_ID_MP3) used by this stream. The sample format has index 3. I can't figure out why libav parses the header wrong.

Here is a part of my code that is responsible for configuring output:

    avdevice_register_all();

    AVOutputFormat *output = av_guess_format("alsa",NULL,NULL);

    AVFormatContext *outputFormatContext = avformat_alloc_context();
    outputFormatContext->oformat = output;
    outputFormatContext->flags = AVFMT_NOFILE;

    AVStream *stream = avformat_new_stream(outputFormatContext,NULL);

    AVCodecParameters *oCodecParameters = avcodec_parameters_alloc();

    ret = avcodec_parameters_copy(oCodecParameters,iCodecParameters);
    if(ret < 0){
        printf("avformat_parameters_copy\n");
        exit(0);
    }

    stream->codecpar = oCodecParameters;

    if(avformat_write_header(outputFormatContext,NULL)<0){
        dumpParameters(stream->codecpar);
        printf("avformat_write_header\n");
        exit(0);
    }

The full code is here: https://github.com/szymonbarszcz99/C-internet-radio

It seems that in libav we can't do simple copy. Instead I have to manually give it requested parameters. Changing avcodec_parameters_copy() to this

    AVCodecParameters *oCodecParameters = avcodec_parameters_alloc();
    oCodecParameters->format = 8;
    oCodecParameters->codec_type = 1;
    oCodecParameters->sample_rate = 44100;
    oCodecParameters->channels = 2;

    stream->codecpar = oCodecParameters;

fixes this problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM