AVChannel枚舉是在2022-12-20的提交中添加的,對應的版本號是5.1.
這個提交的描述是"avutil/channel_layout: add AVChannel enum and related
functions"。
原型
typedef struct AVChannelCustom {
enum AVChannel id;
char name[16];
void *opaque;
} AVChannelCustom;
enum AVChannel {
///< Invalid channel index
AV_CHAN_NONE = -1,
AV_CHAN_FRONT_LEFT,
AV_CHAN_FRONT_RIGHT,
AV_CHAN_FRONT_CENTER,
AV_CHAN_LOW_FREQUENCY,
AV_CHAN_BACK_LEFT,
AV_CHAN_BACK_RIGHT,
AV_CHAN_FRONT_LEFT_OF_CENTER,
AV_CHAN_FRONT_RIGHT_OF_CENTER,
AV_CHAN_BACK_CENTER,
AV_CHAN_SIDE_LEFT,
AV_CHAN_SIDE_RIGHT,
AV_CHAN_TOP_CENTER,
AV_CHAN_TOP_FRONT_LEFT,
AV_CHAN_TOP_FRONT_CENTER,
AV_CHAN_TOP_FRONT_RIGHT,
AV_CHAN_TOP_BACK_LEFT,
AV_CHAN_TOP_BACK_CENTER,
AV_CHAN_TOP_BACK_RIGHT,
/** Stereo downmix. */
AV_CHAN_STEREO_LEFT = 29,
/** See above. */
AV_CHAN_STEREO_RIGHT,
AV_CHAN_WIDE_LEFT,
AV_CHAN_WIDE_RIGHT,
AV_CHAN_SURROUND_DIRECT_LEFT,
AV_CHAN_SURROUND_DIRECT_RIGHT,
AV_CHAN_LOW_FREQUENCY_2,
AV_CHAN_TOP_SIDE_LEFT,
AV_CHAN_TOP_SIDE_RIGHT,
AV_CHAN_BOTTOM_FRONT_CENTER,
AV_CHAN_BOTTOM_FRONT_LEFT,
AV_CHAN_BOTTOM_FRONT_RIGHT,
/** Channel is empty can be safely skipped. */
AV_CHAN_UNUSED = 0x200,
/** Channel contains data, but its position is unknown. */
AV_CHAN_UNKNOWN = 0x300,
/**
* Range of channels between AV_CHAN_AMBISONIC_BASE and
* AV_CHAN_AMBISONIC_END represent Ambisonic components using the ACN system.
*
* Given a channel id <i> between AV_CHAN_AMBISONIC_BASE and
* AV_CHAN_AMBISONIC_END (inclusive), the ACN index of the channel <n> is
* <n> = <i> - AV_CHAN_AMBISONIC_BASE.
*
* @note these values are only used for AV_CHANNEL_ORDER_CUSTOM channel
* orderings, the AV_CHANNEL_ORDER_AMBISONIC ordering orders the channels
* implicitly by their position in the stream.
*/
AV_CHAN_AMBISONIC_BASE = 0x400,
// leave space for 1024 ids, which correspond to maximum order-32 harmonics,
// which should be enough for the foreseeable use cases
AV_CHAN_AMBISONIC_END = 0x7ff,
};
描述
AVChannel
是一個枚舉類型,用于表示音頻數(shù)據(jù)的各個聲道。在音頻數(shù)據(jù)中,一個“聲道”是一個獨立的音頻信號,它可以被獨立處理和播放。例如,在立體聲音頻中,有兩個聲道:左聲道和右聲道。
這個枚舉中的每個值都代表一個特定的聲道。例如,AV_CHAN_FRONT_LEFT
代表前左聲道,AV_CHAN_FRONT_RIGHT
代表前右聲道,等等。這些值可以用于處理具有特定聲道布局的音頻數(shù)據(jù)。
以下是一些特殊的枚舉值的解釋:
-
AV_CHAN_NONE
:無效的聲道索引。 -
AV_CHAN_STEREO_LEFT
和AV_CHAN_STEREO_RIGHT
:立體聲混音的左聲道和右聲道。 -
AV_CHAN_UNUSED
:聲道是空的,可以安全地跳過。 -
AV_CHAN_UNKNOWN
:聲道包含數(shù)據(jù),但其位置未知。 -
AV_CHAN_AMBISONIC_BASE
和AV_CHAN_AMBISONIC_END
:表示使用 ACN 系統(tǒng)的 Ambisonic 組件的聲道范圍。Ambisonics 是一種全方位的聲音錄制和再現(xiàn)技術,可以捕捉和再現(xiàn)聲音場的三維空間信息。
請注意,這個枚舉的具體值可能會根據(jù)你使用的 FFmpeg 版本和 API 而有所不同。你應該查閱你正在使用的 FFmpeg 版本和 API 的文檔,以獲取最準確的信息。
相關輸出接口
/**
* Get the channel with the given index in a channel layout.
*
* @param channel_layout input channel layout
* @return channel with the index idx in channel_layout on success or
* AV_CHAN_NONE on failure (if idx is not valid or the channel order is
* unspecified)
*/
enum AVChannel
av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx);
/**
* Get a channel described by the given string.
*
* This function accepts channel names in the same format as
* @ref av_channel_from_string().
*
* @param channel_layout input channel layout
* @return a channel described by the given string in channel_layout on success
* or AV_CHAN_NONE on failure (if the string is not valid or the channel
* order is unspecified)
*/
enum AVChannel
av_channel_layout_channel_from_string(const AVChannelLayout *channel_layout,
const char *name);
/**
* This is the inverse function of @ref av_channel_name().
*
* @return the channel with the given name
* AV_CHAN_NONE when name does not identify a known channel
*/
enum AVChannel av_channel_from_string(const char *name);
這些函數(shù)都是用來從不同的輸入獲取 AVChannel
枚舉類型的。下面是每個函數(shù)的簡單解釋和示例:
-
av_channel_layout_channel_from_string
:這個函數(shù)從給定的聲道布局中獲取由給定字符串描述的聲道。例如:
const AVChannelLayout *layout = codec_ctx_->channel_layout;
const char *name = "FL"; // Front Left
AVChannel channel = av_channel_layout_channel_from_string(layout, name);
-
av_channel_layout_channel_from_index
:這個函數(shù)從給定的聲道布局中獲取給定索引的聲道。例如:
const AVChannelLayout *layout = codec_ctx_->channel_layout;
unsigned int idx = 0; // Index of the channel
AVChannel channel = av_channel_layout_channel_from_index(layout, idx);
-
av_channel_from_string
:這個函數(shù)獲取給定名稱的聲道。例如:
const char *name = "FL"; // Front Left
AVChannel channel = av_channel_from_string(name);
請注意,這些函數(shù)都可能返回 AV_CHAN_NONE
,這表示輸入無效,或者聲道順序未指定。在使用這些函數(shù)時,你應該檢查返回值是否為 AV_CHAN_NONE
。
相關輸入接口
/**
* Get a human readable string in an abbreviated form describing a given channel.
* This is the inverse function of @ref av_channel_from_string().
*
* @param buf pre-allocated buffer where to put the generated string
* @param buf_size size in bytes of the buffer.
* @return amount of bytes needed to hold the output string, or a negative AVERROR
* on failure. If the returned value is bigger than buf_size, then the
* string was truncated.
*/
int av_channel_name(char *buf, size_t buf_size, enum AVChannel channel);
/**
* bprint variant of av_channel_name().
*
* @note the string will be appended to the bprint buffer.
*/
void av_channel_name_bprint(struct AVBPrint *bp, enum AVChannel channel_id);
/**
* Get a human readable string describing a given channel.
*
* @param buf pre-allocated buffer where to put the generated string
* @param buf_size size in bytes of the buffer.
* @return amount of bytes needed to hold the output string, or a negative AVERROR
* on failure. If the returned value is bigger than buf_size, then the
* string was truncated.
*/
int av_channel_description(char *buf, size_t buf_size, enum AVChannel channel);
/**
* bprint variant of av_channel_description().
*
* @note the string will be appended to the bprint buffer.
*/
void av_channel_description_bprint(struct AVBPrint *bp, enum AVChannel channel_id);
/**
* Get the index of a given channel in a channel layout. In case multiple
* channels are found, only the first match will be returned.
*
* @param channel_layout input channel layout
* @return index of channel in channel_layout on success or a negative number if
* channel is not present in channel_layout.
*/
int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout,
enum AVChannel channel);
這些函數(shù)都是用來獲取 AVChannel
枚舉值的描述的。下面是每個函數(shù)的簡單解釋和示例:
-
av_channel_name
:這個函數(shù)獲取給定聲道的簡短描述。例如:
char buf[256];
AVChannel channel = AV_CHAN_FRONT_LEFT;
int ret = av_channel_name(buf, sizeof(buf), channel);
if (ret < 0) {
// Handle error
} else {
printf("Channel name: %s\n", buf);
}
-
av_channel_name_bprint
:這個函數(shù)是av_channel_name
的 bprint 變體,它將字符串追加到 bprint 緩沖區(qū)。例如:
AVBPrint bp;
av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
AVChannel channel = AV_CHAN_FRONT_LEFT;
av_channel_name_bprint(&bp, channel);
printf("Channel name: %s\n", bp.str);
av_bprint_finalize(&bp, NULL);
-
av_channel_description
:這個函數(shù)獲取給定聲道的詳細描述。例如:
char buf[256];
AVChannel channel = AV_CHAN_FRONT_LEFT;
int ret = av_channel_description(buf, sizeof(buf), channel);
if (ret < 0) {
// Handle error
} else {
printf("Channel description: %s\n", buf);
}
-
av_channel_description_bprint
:這個函數(shù)是av_channel_description
的 bprint 變體,它將字符串追加到 bprint 緩沖區(qū)。例如:
AVBPrint bp;
av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
AVChannel channel = AV_CHAN_FRONT_LEFT;
av_channel_description_bprint(&bp, channel);
printf("Channel description: %s\n", bp.str);
av_bprint_finalize(&bp, NULL);
請注意,這些函數(shù)都可能返回錯誤。在使用這些函數(shù)時,你應該檢查返回值是否為負數(shù),如果是,那么說明發(fā)生了錯誤。
5. av_channel_layout_index_from_channel
函數(shù)用于獲取給定聲道在聲道布局中的索引。如果在聲道布局中找到多個相同的聲道,只會返回第一個匹配的索引。
這個函數(shù)接受兩個參數(shù):
-
channel_layout
:輸入的聲道布局。 -
channel
:要查找的聲道。
如果聲道在聲道布局中存在,函數(shù)會返回聲道的索引;如果聲道在聲道布局中不存在,函數(shù)會返回一個負數(shù)。
以下是一個使用示例:
const AVChannelLayout *layout = codec_ctx_->channel_layout;
AVChannel channel = AV_CHAN_FRONT_LEFT;
int index = av_channel_layout_index_from_channel(layout, channel);
if (index < 0) {
printf("Channel is not present in the layout\n");
} else {
printf("Index of the channel in the layout: %d\n", index);
}
在這個示例中,我們首先獲取編解碼器上下文的聲道布局,然后定義我們要查找的聲道(在這個例子中是前左聲道)。然后,我們調用 av_channel_layout_index_from_channel
函數(shù)來獲取聲道在聲道布局中的索引。如果索引是負數(shù),那么聲道在聲道布局中不存在;否則,我們打印出聲道在聲道布局中的索引。
應用場景
FFmpeg 是一個非常靈活的庫,它允許你以多種方式處理音頻和視頻數(shù)據(jù)。這種靈活性的一個結果是,很多函數(shù)需要你手動指定參數(shù),而不是自動從結構體中獲取信息。這是因為在不同的上下文中,你可能需要以不同的方式處理數(shù)據(jù)。
例如,av_channel_layout_channel_from_string
和 av_channel_layout_channel_from_index
函數(shù)允許你從一個聲道布局中獲取一個特定的聲道。你需要手動指定聲道的名稱或索引,因為 FFmpeg 不知道你想獲取哪個聲道。你可能想獲取前左聲道,也可能想獲取前右聲道,或者其他任何聲道。FFmpeg 提供了這些函數(shù),讓你可以根據(jù)你的需要獲取任何聲道。
同樣,av_channel_from_string
函數(shù)允許你獲取一個給定名稱的聲道。你需要手動指定聲道的名稱,因為 FFmpeg 不知道你想獲取哪個聲道。你可能想獲取 “FL”(前左)聲道,也可能想獲取 “FR”(前右)聲道,或者其他任何聲道。FFmpeg 提供了這個函數(shù),讓你可以根據(jù)你的需要獲取任何聲道。文章來源:http://www.zghlxwxcb.cn/news/detail-508843.html
雖然這些函數(shù)需要你手動指定參數(shù),但這并不意味著代碼的兼容性不強。相反,這種設計使得你的代碼可以在不同的上下文中工作,只要你正確地指定了參數(shù)。如果你想寫更通用的代碼,你可以編寫一個函數(shù),它接受一個 FFmpeg 結構體作為參數(shù),然后從這個結構體中獲取所需的信息,然后調用這些 FFmpeg 函數(shù)。文章來源地址http://www.zghlxwxcb.cn/news/detail-508843.html
到了這里,關于ffmpeg enum AVChannel枚舉解析的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!