2014-10-05 120 views
5

我最近一直有一個奇怪的問題。 根據我在windows(stereo/quad/5.1)中設置音頻配置的方式,對avcodec_open2()的ffmpeg調用失敗,出現錯誤-22或者正常工作。 無法找到有關該錯誤的更多信息,我想我應該在此處詢問。 主流程是這樣的:ffmpeg avcodec_open2返回-22如果我改變我的揚聲器配置

c = st->codec; 
avformat_alloc_output_context2(&oc, NULL, NULL, "video.mpeg"); 
oc->fmt->audio_codec = AV_CODEC_ID_MP2; 
AVDictionary* dict = NULL; 
ret = av_dict_set(&dict, "ac", "2", 0); 
c->request_channels = 2; 

ret = avcodec_open2(c, codec, &dict); //HERE IT FAILS WITH -22 if speaker configuration is not stereo 

該編解碼器的上下文「c」的設置是這樣的流:

st = avformat_new_stream(oc, *codec); 
c = st->codec; 
c->channels  = 2; 
c->channel_layout = AV_CH_LAYOUT_STEREO; 
c->sample_fmt = AV_SAMPLE_FMT_S16; 
c->codec_id  = codec_id; 

大部分是從他們的發現的多路轉換的例子之一複製在文檔中。 如果在Windows中我將輸出設置爲立體聲,一切都按預期工作。

如果我將揚聲器配置設置爲5.1(6通道),則avcodec_open2將失敗,並顯示錯誤-22。

所以我很難理解我做錯了什麼。通常,我的揚聲器配置與avcodec_open2的結果之間不應該有任何關係。

是否還有其他一些參數需要設置?

+0

只是要格外清楚,如果Windows揚聲器配置設置爲立體聲,一切正常。 – Alex 2014-10-05 17:02:53

+0

我GOOGLE了,我發現你應該嘗試像'av_log_set_level(AV_LOG_VERBOSE);'來了解更詳細的錯誤。然後'void my_log_callback(void * ptr,int level,const char * fmt,va_list vargs){printf(「\ n%s」,fmt);}'然後'av_log_set_callback(my_log_callback);' – SSpoke 2014-10-05 17:05:26

+0

但是錯誤'-22 '真是錯誤'22',因爲任何可能的數字意味着它應該返回多少字節,所以..錯誤22是'#define EINVAL 22/*無效參數* /'這意味着參數無效。 – SSpoke 2014-10-05 17:11:10

回答

5

下面是從How can I find out what this ffmpeg error code means?

#if EINVAL > 0 
#define AVERROR(e) (-(e)) /**< Returns a negative error code from a POSIX error code, to return from library functions. */ 
#define AVUNERROR(e) (-(e)) /**< Returns a POSIX error code from a library function error return value. */ 
#else 
/* Some platforms have E* and errno already negated. */ 
#define AVERROR(e) (e) 
#define AVUNERROR(e) (e) 
#endif 
#define AVERROR_UNKNOWN  AVERROR(EINVAL) /**< unknown error */ 
#define AVERROR_IO   AVERROR(EIO)  /**< I/O error */ 
#define AVERROR_NUMEXPECTED AVERROR(EDOM) /**< Number syntax expected in filename. */ 
#define AVERROR_INVALIDDATA AVERROR(EINVAL) /**< invalid data found */ 
#define AVERROR_NOMEM  AVERROR(ENOMEM) /**< not enough memory */ 
#define AVERROR_NOFMT  AVERROR(EILSEQ) /**< unknown format */ 
#define AVERROR_NOTSUPP  AVERROR(ENOSYS) /**< Operation not supported. */ 
#define AVERROR_NOENT  AVERROR(ENOENT) /**< No such file or directory. */ 
#define AVERROR_EOF   AVERROR(EPIPE) /**< End of file. */ 
#define AVERROR_PATCHWELCOME -MKTAG('P','A','W','E') /**< Not yet implemented in FFmpeg. Patches welcome. */ 

採取文件libavcodec\avcodec.h頭然後爲EINVAL

#define EINVAL   22  /* Invalid argument */ 

P.S. errno.h頭文件AVERROR意味着(-(-22)) = 22

佈局渠道頭channel_layout.h頭文件

/** 
* @file 
* audio conversion routines 
*/ 

/* Audio channel masks */ 
#define AV_CH_FRONT_LEFT    0x00000001 
#define AV_CH_FRONT_RIGHT   0x00000002 
#define AV_CH_FRONT_CENTER   0x00000004 
#define AV_CH_LOW_FREQUENCY   0x00000008 
#define AV_CH_BACK_LEFT    0x00000010 
#define AV_CH_BACK_RIGHT    0x00000020 
#define AV_CH_FRONT_LEFT_OF_CENTER 0x00000040 
#define AV_CH_FRONT_RIGHT_OF_CENTER 0x00000080 
#define AV_CH_BACK_CENTER   0x00000100 
#define AV_CH_SIDE_LEFT    0x00000200 
#define AV_CH_SIDE_RIGHT    0x00000400 
#define AV_CH_TOP_CENTER    0x00000800 
#define AV_CH_TOP_FRONT_LEFT   0x00001000 
#define AV_CH_TOP_FRONT_CENTER  0x00002000 
#define AV_CH_TOP_FRONT_RIGHT  0x00004000 
#define AV_CH_TOP_BACK_LEFT   0x00008000 
#define AV_CH_TOP_BACK_CENTER  0x00010000 
#define AV_CH_TOP_BACK_RIGHT   0x00020000 
#define AV_CH_STEREO_LEFT   0x20000000 ///< Stereo downmix. 
#define AV_CH_STEREO_RIGHT   0x40000000 ///< See AV_CH_STEREO_LEFT. 

/** Channel mask value used for AVCodecContext.request_channel_layout 
    to indicate that the user requests the channel order of the decoder output 
    to be the native codec channel order. */ 
#define AV_CH_LAYOUT_NATIVE   0x8000000000000000LL 

/* Audio channel convenience macros */ 
#define AV_CH_LAYOUT_MONO    (AV_CH_FRONT_CENTER) 
#define AV_CH_LAYOUT_STEREO   (AV_CH_FRONT_LEFT|AV_CH_FRONT_RIGHT) 
#define AV_CH_LAYOUT_2_1    (AV_CH_LAYOUT_STEREO|AV_CH_BACK_CENTER) 
#define AV_CH_LAYOUT_SURROUND   (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) 
#define AV_CH_LAYOUT_4POINT0   (AV_CH_LAYOUT_SURROUND|AV_CH_BACK_CENTER) 
#define AV_CH_LAYOUT_2_2    (AV_CH_LAYOUT_STEREO|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT) 
#define AV_CH_LAYOUT_QUAD    (AV_CH_LAYOUT_STEREO|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT) 
#define AV_CH_LAYOUT_5POINT0   (AV_CH_LAYOUT_SURROUND|AV_CH_SIDE_LEFT|AV_CH_SIDE_RIGHT) 
#define AV_CH_LAYOUT_5POINT1   (AV_CH_LAYOUT_5POINT0|AV_CH_LOW_FREQUENCY) 
#define AV_CH_LAYOUT_5POINT0_BACK  (AV_CH_LAYOUT_SURROUND|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT) 
#define AV_CH_LAYOUT_5POINT1_BACK  (AV_CH_LAYOUT_5POINT0_BACK|AV_CH_LOW_FREQUENCY) 
#define AV_CH_LAYOUT_7POINT0   (AV_CH_LAYOUT_5POINT0|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT) 
#define AV_CH_LAYOUT_7POINT1   (AV_CH_LAYOUT_5POINT1|AV_CH_BACK_LEFT|AV_CH_BACK_RIGHT) 
#define AV_CH_LAYOUT_7POINT1_WIDE  (AV_CH_LAYOUT_5POINT1_BACK|AV_CH_FRONT_LEFT_OF_CENTER|AV_CH_FRONT_RIGHT_OF_CENTER) 
#define AV_CH_LAYOUT_STEREO_DOWNMIX (AV_CH_STEREO_LEFT|AV_CH_STEREO_RIGHT) 
+1

我投這個答案。事實證明,我做了一個意外錯誤,並設置了5比特的不同比特率。1揚聲器配置。 (實際系統比上面的示例代碼複雜一點)。這幫助我瞭解了這個問題,所以我投了它作爲答案。謝謝 – Alex 2014-10-05 17:49:32

相關問題