简介
avpicture_fill函数将ptr指向的数据填充到picture内,但并没有拷贝,只是将picture结构内的data指针指向了ptr的数据。其实现如下:
avpiture_fill
avpiture_fill直接调用av_image_fill_arrays函数。
// libavcodec/avpicture.c
int avpicture_fill(AVPicture *picture, const uint8_t *ptr,
enum AVPixelFormat pix_fmt, int width, int height)
{
return av_image_fill_arrays(picture->data, picture->linesize,
ptr, pix_fmt, width, height, );
}
av_image_fill_arrays
// libavutil/imgutils.c
int av_image_fill_arrays(uint8_t *dst_data[], int dst_linesize[],
const uint8_t *src, enum AVPixelFormat pix_fmt,
int width, int height, int align)
{
int ret, i; ret = av_image_check_size(width, height, , NULL);
if (ret < )
return ret; ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
if (ret < )
return ret; for (i = ; i < ; i++)
dst_linesize[i] = FFALIGN(dst_linesize[i], align); return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
}
其中av_image_check_size用来检测输入的widht和height是否可用,判断条件如下:
if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
return 0;
av_image_fill_linesizes
int av_image_fill_linesizes(int linesizes[], enum AVPixelFormat pix_fmt, int width)
{
int i, ret;
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
int max_step []; /* max pixel step for each plane */
int max_step_comp[]; /* the component for each plane which has the max pixel step */ memset(linesizes, , *sizeof(linesizes[])); if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
return AVERROR(EINVAL); av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
for (i = ; i < ; i++) {
if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < ) return ret;
linesizes[i] = ret;
} return ;
}
- 将linsizes数组的内容置为0;
- 利用av_pix_fmt_desc_get函数得到输入格式的AVPixFmtDescriptor指针;
-
最后利用image_get_linesize函数获得linesizes数组中每个元素的值;
FFALIGN
由于在afpicture_fill中填充的align为1, 故该宏返回的值还是linesizes[i];
// libavutil/common.h #define FFALIGN(x, a) (((x)+(a)-1)&~((a)-1))
av_image_fill_pointers
int av_image_fill_pointers(uint8_t *data[], enum AVPixelFormat pix_fmt, int height,
uint8_t *ptr, const int linesizes[])
{
int i, total_size, size[] = { }, has_plane[] = { }; const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
memset(data , , sizeof(data[])*); if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
return AVERROR(EINVAL); data[] = ptr;
if (linesizes[] > (INT_MAX - ) / height)
return AVERROR(EINVAL);
size[] = linesizes[] * height; if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
size[] = (size[] + ) & ~;
data[] = ptr + size[]; /* palette is stored here as 256 32 bits words */
return size[] + * ;
} for (i = ; i < ; i++)
has_plane[desc->comp[i].plane] = ; total_size = size[];
for (i = ; i < && has_plane[i]; i++) {
int h, s = (i == || i == ) ? desc->log2_chroma_h : ;
data[i] = data[i-] + size[i-];
h = (height + ( << s) - ) >> s;
if (linesizes[i] > INT_MAX / h)
return AVERROR(EINVAL);
size[i] = h * linesizes[i];
if (total_size > INT_MAX - size[i])
return AVERROR(EINVAL);
total_size += size[i];
} return total_size;
}将data数组内的指针分别指向ptr内的数据。