FFmpeg之a(chǎn)v_image_get_buffer_size函數(shù)
/**
* Return the size in bytes of the amount of data required to store an
* image with the given parameters.
* 返回使用給定參數(shù)存儲圖像所需數(shù)據(jù)量的字節(jié)大小
*
* @param pix_fmt the pixel format of the image 圖像的像素格式
* @param width the width of the image in pixels 以像素為單位的圖像寬度
* @param height the height of the image in pixels 圖像的高度(像素)
* @param align the assumed linesize alignment 假定的線寬對齊
* @return the buffer size in bytes, a negative error code in case of failure
* 緩沖區(qū)大?。ㄒ宰止?jié)為單位),出現(xiàn)故障時為負錯誤代碼
*/
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align);
函數(shù)的作用是通過指定像素格式、圖像寬、圖像高來計算所需的內(nèi)存大小
重點說明一個參數(shù)align:此參數(shù)是設(shè)定內(nèi)存對齊的對齊數(shù),也就是按多大的字節(jié)進行內(nèi)存對齊。比如設(shè)置為1,表示按1字節(jié)對齊,那么得到的結(jié)果就是與實際的內(nèi)存大小一樣。再比如設(shè)置為4,表示按4字節(jié)對齊。也就是內(nèi)存的起始地址必須是4的整倍數(shù)。
ffmpeg的ffmpeg中的align
ffmpeg之所以給了這個參數(shù)讓?設(shè)置,應(yīng)該就是為了兼容各個硬件平臺,因為不是所有的硬件平臺都能訪 問任意地址上的任意數(shù)據(jù)的, 某些硬件平臺只能在某些地址處取某些特定類型的數(shù)據(jù),否則拋出硬件異常
以ffmpeg的av_image_get_buffer_size為例,你能準確說出下?的結(jié)果嗎,如果可以,那么證明你確實 理解了ffmpeg中的對?了。
值得注意的是yuv的計算,以w*h
的yuv420p為例,
他是分三個平?存儲三個分量的,?u和v的計算是?致 的,
也就是說計算出了u即可得到v;
對于y來說,它有w?h列,因此需要計算w?的對?后字節(jié)數(shù)再乘以h;
對于u來說,它有w/2
?h/2
列(這是因為每4個y共享?組uv),
因此需要計算w/2
?的對?后字節(jié)數(shù)再乘以 h/2
;
v的計算與u的?模?樣,最后將這個三個數(shù)相加即可
例如310的圖?,每個像素點占2個字節(jié),對?數(shù)align為4,那么?數(shù)是多少呢,從條件可知,每?3 個像素點,每個像素點占2字節(jié),那么每?就是6字節(jié),?對?數(shù)是4,6不是4的整數(shù)倍,因此6需要補 2個字節(jié)湊成8,8就是4的整數(shù)倍了,那么我們就知道每?在內(nèi)存中實際占?了8個字節(jié),后兩個字節(jié)是 為了對?補上的,總共有10?,那么這張圖?在實際內(nèi)存中就占?了810=80個字節(jié),?不是60個字 節(jié)了
本來這張圖?只需要60個字節(jié),為何要?80個字節(jié)來存儲呢,這是因為cpu并不能從任意地址開始讀 取數(shù)據(jù),如果不對?,那么可能需要多次讀取才能讀到完整數(shù)據(jù),因此對?主要是為了提升性能,典型的空間換取時間
ffmpeg的linesize
linesize其實就是我們上?提及到的?字節(jié)數(shù),在我們解碼出數(shù)據(jù)后,經(jīng)常會遇到這個linesize,既然我們知 道了align的概念,就該明?這個linesize就是為了讓你取出真實的數(shù)據(jù)的 解碼后的數(shù)據(jù)中可能是經(jīng)過對?的,既然有對?,那就是數(shù)據(jù)?加多了?些為了對??多余的字節(jié),如果 我們想最后顯示視頻數(shù)據(jù),那么這些多余的數(shù)據(jù)勢必要進?剔除掉,那么怎么剔除呢,linesize就是來幫你 ?這事的,有了它,你就可以一行一行?較,然后把每?最后為了對??補的字節(jié)刪除,還原出視頻的真 實數(shù)據(jù)
av_image_alloc函數(shù)
/**
* Allocate an image with size w and h and pixel format pix_fmt, and
* fill pointers and linesizes accordingly.
* 分配具有大小w和h以及像素格式pix_fmt的圖像,并相應(yīng)地填充指針和線條大小
*
* The allocated image buffer has to be freed by using
* av_freep(&pointers[0]).
* 必須釋放分配的圖像緩沖區(qū)使用av_freep
*
* @param align the value to use for buffer size alignment
* 用于緩沖區(qū)大小對齊的值
*
* @return the size in bytes required for the image buffer, a negative
* error code in case of failure
* 圖像緩沖區(qū)所需的字節(jié)大小,出現(xiàn)故障時為負錯誤代碼
*/
int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
int w, int h, enum AVPixelFormat pix_fmt, int align);
av_image_fill_arrays函數(shù)
/**
Setup the data pointers and linesizes based on the specified image
parameters and the provided array.
根據(jù)指定的圖像參數(shù)和提供的數(shù)組設(shè)置數(shù)據(jù)指針和行大小
The fields of the given image are filled in by using the src
address which points to the image data buffer. Depending on the
specified pixel format, one or multiple image data pointers and
line sizes will be set. If a planar format is specified, several
pointers will be set pointing to the different picture planes and
the line sizes of the different planes will be stored in the
lines_sizes array. Call with src == NULL to get the required
size for the src buffer.
給定圖像的字段是使用指向圖像數(shù)據(jù)緩沖區(qū)的src地址填充的。
根據(jù)指定的像素格式,將設(shè)置一個或多個圖像數(shù)據(jù)指針和線條大小。
如果指定了平面格式,將設(shè)置多個指向不同圖片平面的指針,
不同平面的線條大小將存儲在lines_sizes數(shù)組中。
使用src==NULL調(diào)用以獲取src緩沖區(qū)所需的大小
To allocate the buffer and fill in the dst_data and dst_linesize in
one call, use av_image_alloc().
要在一次調(diào)用中分配緩沖區(qū)并填寫dst_data和dst_linesize,請使用av_image_alloc
@param dst_data data pointers to be filled in
@param dst_linesize linesizes for the image in dst_data to be filled in
@param src buffer which will contain or contains the actual image data, can be NULL
@param pix_fmt the pixel format of the image
@param width the width of the image in pixels
@param height the height of the image in pixels
@param align the value used in src for linesize alignment
@return the size in bytes required for src, a negative error code
in case of failure
*/
int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
const uint8_t *src,
enum AVPixelFormat pix_fmt, int width, int height, int align);
通過以上實例可以看到,(a)
計算所需內(nèi)存大小av_image_get_bufferz_size()(b)
按計算的內(nèi)存大小申請所需內(nèi)存 av_malloc()(c)
對申請的內(nèi)存進行格式化 av_image_fill_arrays()文章來源:http://www.zghlxwxcb.cn/news/detail-413945.html
參考
av_image_get_buffer_size
FFmpeg簡單分析系列----內(nèi)存對齊簡要說明文章來源地址http://www.zghlxwxcb.cn/news/detail-413945.html
到了這里,關(guān)于音視頻從入門到精通——FFmpeg之a(chǎn)v_image_get_buffer_size函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!