[libav-api] problem with codec_tag set to 0

Florian Iragne florian at iragne.fr
Mon Oct 28 13:01:55 CET 2013


I'm trying to write a simple program that transforms any audio input 
into wav/mono/44100/pcm, using libav.

I've come accross a problem with the codec_tag member of AVCodecContext.

Here is my code:
     //open output
     AVOutputFormat* fmt;
     /* Autodetect the output format from the name. default is WAV. */
     fmt = av_guess_format(NULL, argv[2], NULL);
     if (!fmt) {
         printf("Could not deduce output format from file extension: 
using WAV\n");
         fmt = av_guess_format("wav", NULL, NULL);
     if (!fmt) {
         printf("Could not find suitable output format\n");
         return 1;

     //open the source file
     _oFormatCtx = avformat_alloc_context();
     _oFormatCtx->oformat = fmt;
     snprintf(_oFormatCtx->filename, sizeof (_oFormatCtx->filename), 
"%s", argv[2]);

     if (fmt->audio_codec == AV_CODEC_ID_NONE) {
         printf("Audio codec is none\n");
         return 1;
     _oCodec = avcodec_find_encoder(AV_CODEC_ID_PCM_S16LE);
     if (!_oCodec) {
         printf("No such encoder\n");
         return 1;
     _oStream = avformat_new_stream(_oFormatCtx, _oCodec);
     if (!_oStream) {
         printf("Unable to open stream\n");
         return false;
     _oStream->id = 0;
     _oStream->codec->sample_fmt = AV_SAMPLE_FMT_S16;
     _oStream->codec->sample_rate = sampleRate;
     _oStream->codec->channels = channels;
     _oStream->codec->time_base = _iCodecCtx->time_base;

     // some formats want stream headers to be separate
     if (fmt->flags & AVFMT_GLOBALHEADER)
         _oStream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;

     _oCodecCtx = _oStream->codec;
     int ret;
     char error [128];
     av_dump_format(_oFormatCtx, 0, argv[2], 1);

     /* open the output file, if needed */
     if (!(fmt->flags & AVFMT_NOFILE)) {
         ret = avio_open(&_oFormatCtx->pb, argv[2], AVIO_FLAG_WRITE);
         if (ret < 0) {
             av_strerror(ret, error, 128);
             printf("Could not open %s %s\n", argv[2], error);
             return 1;

     /* Write the stream header, if any. */
     ret = avformat_write_header(_oFormatCtx, NULL);
     if (ret < 0) {
         av_strerror(ret, error, 128);
         printf("Error occurred when opening output file : %s\n", error);
         return false;

Here, i get the error "permission denied"

I've traced it through the libav code and it happens because 
_oCodecCtx->codec_tag is evaluated to 0, so ff_put_wav_header return -1 
(line 382 of riff.c)

So, i wonder why this codec_tag is set to 0, instead of 0x0001 (if i've 
read correctly). Is it supposed to be set by me (haven't found the way) 
or by any helper in libav?

thanks for your read and your help


More information about the libav-api mailing list