FFmpeg源码分析:图像常⽤操作
FFmpeg有封装图像的常⽤操作,位于libavutil/imgutils.c,包括图像拷贝、图像填充、获取图像⼤⼩、分配图像、检测图像宽⾼⽐是否有效。在视频图像缩放、像素格式转换、视频截图保存等操作,经常需要⽤到图像操作⽅法。
⼀、获取图像⼤⼩
1、av_image_get_linesize
根据图像宽与像素格式,获取⼀⾏图像⼤⼩:
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
{
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
int max_step [4]; /* max pixel step for each plane */
int max_step_comp[4]; /* the component for each plane which has the max pixel step */
if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
return AVERROR(EINVAL);
av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
}
2、av_image_fill_linesizes
在av_image_get_linesize()基础上,获取每⾏图像⼤⼩:
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
{
千钧一发是什么意思
int i, ret;
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
essayist
int max_step [4]; /* max pixel step for each plane */
int max_step_comp[4]; /* the component for each plane which has the max pixel step */
memt(linesizes, 0, 4*sizeof(linesizes[0]));
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 = 0; i < 4; i++) {
if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
return ret;
linesizes[i] = ret;
}
英语杂志
return 0;
}
词典英语3、av_image_fill_plane_sizes
根据图像的⾼、每⾏⼤⼩、像素格式,计算每个图像平⾯的⼤⼩:
int av_image_fill_plane_sizes(size_t sizes[4], enum AVPixelFormat pix_fmt, int height, const ptrdiff_t linesizes[4])
{
int i, has_plane[4] = { 0 };
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
特梅尔
memt(sizes , 0, sizeof(sizes[0])*4);
if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
return AVERROR(EINVAL);
if (linesizes[0] > SIZE_MAX / height)
return AVERROR(EINVAL);
sizes[0] = linesizes[0] * (size_t)height;
if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
desc->flags & FF_PSEUDOPAL) {
sizes[1] = 256 * 4; /* palette is stored here as 256 32 bits words */
return 0;
}
for (i = 0; i < 4; i++)
has_plane[desc->comp[i].plane] = 1;
for (i = 1; i < 4 && has_plane[i]; i++) {
int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
h = (height + (1 << s) - 1) >> s;
if (linesizes[i] > SIZE_MAX / h)
return AVERROR(EINVAL);
sizes[i] = (size_t)h * linesizes[i];
}
return 0;
}
4、av_image_get_buffer_size
根据图像宽⾼与像素格式,获取图像⼤⼩,⽀持配置是否对齐模式:
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
hrt
int width, int height, int align)
{
int ret, i;
int linesize[4];
ptrdiff_t aligned_linesize[4];
size_t sizes[4];
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
if (!desc)
return AVERROR(EINVAL);
ret = av_image_check_size(width, height, 0, NULL);
if (ret < 0)
return ret;
if (desc->flags & FF_PSEUDOPAL)
return FFALIGN(width, align) * height;
ret = av_image_fill_linesizes(linesize, pix_fmt, width);
if (ret < 0)
return ret;
for (i = 0; i < 4; i++)
aligned_linesize[i] = FFALIGN(linesize[i], align);
ret = av_image_fill_plane_sizes(sizes, pix_fmt, height, aligned_linesize); if (ret < 0)
return ret;
ret = 0;
for (i = 0; i < 4; i++) {
if (sizes[i] > INT_MAX - ret)
return AVERROR(EINVAL);
ret += sizes[i];
}
return ret;
}
⼆、图像填充
1、av_image_fill_arrays
使⽤源图像,结合图像宽、⾼、像素格式,填充⽬标图像数据:
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)
{
int ret, i;
ret = av_image_check_size(width, height, 0, NULL);
if (ret < 0)
return ret;
ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
if (ret < 0)
return ret;
for (i = 0; i < 4; i++)
dst_linesize[i] = FFALIGN(dst_linesize[i], align);
return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
}
2、av_image_fill_black
填充图像为⿊⾊,如果有透明通道,就重置为不透明:
int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4],
enum AVPixelFormat pix_fmt, enum AVColorRange range,
int width, int height)
{
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
int nb_planes = av_pix_fmt_count_planes(pix_fmt);
/
/ A pixel or a group of pixels on each plane, with a value that reprents black.
uint8_t clear_block[4][MAX_BLOCK_SIZE] = {{0}}; // clear padding with 0
int clear_block_size[4] = {0};
ptrdiff_t plane_line_bytes[4] = {0};
int rgb, limited;
int plane, c;
if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
return AVERROR(EINVAL);
rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
limited = !rgb && range != AVCOL_RANGE_JPEG;
if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
ptrdiff_t bytewidth = av_image_get_linesize(pix_fmt, width, 0);
uint8_t *data;
int mono = pix_fmt == AV_PIX_FMT_MONOWHITE || pix_fmt == AV_PIX_FMT_MONOBLACK; int fill = pix_fmt == AV_PIX_FMT_MONOWHITE ? 0xFF : 0;
if (nb_planes != 1 || !(rgb || mono) || bytewidth < 1)
lonelynana
return AVERROR(EINVAL);
if (!dst_data)
return 0;
data = dst_data[0];eurovision
// (Bitstream + alpha will be handled incorrectly - it'll remain transparent.)
for (;height > 0; height--) {
memt(data, fill, bytewidth);
data += dst_linesize[0];
}
return 0;
}
for (c = 0; c < desc->nb_components; c++) {fcl
const AVComponentDescriptor comp = desc->comp[c];
// We try to operate on entire non-subsampled pixel groups (for
// AV_PIX_FMT_UYVY422 this would mean two concutive pixels).
clear_block_size[comp.plane] = FFMAX(clear_block_size[comp.plane], comp.step);
if (clear_block_size[comp.plane] > MAX_BLOCK_SIZE)
return AVERROR(EINVAL);
}
// Create a byte array for clearing 1 pixel (sometimes veral pixels).
for (c = 0; c < desc->nb_components; c++) {
const AVComponentDescriptor comp = desc->comp[c];
// (Multiple pixels with AV_PIX_FMT_UYVY422.)
int w = clear_block_size[comp.plane] / comp.step;
uint8_t *c_data[4];
const int c_linesize[4] = {0};
uint16_t src_array[MAX_BLOCK_SIZE];
uint16_t src = 0;
int x;
if (comp.depth > 16)
return AVERROR(EINVAL);
if (!rgb && comp.depth < 8)
decursive
return AVERROR(EINVAL);
if (w < 1)
return AVERROR(EINVAL);
if (c == 0 && limited) {
src = 16 << (comp.depth - 8);
} el if ((c == 1 || c == 2) && !rgb) {
src = 128 << (comp.depth - 8);
} el if (c == 3) {
/
/ (Assume even limited YUV us full range alpha.)
src = (1 << comp.depth) - 1;
}
for (x = 0; x < w; x++)
src_array[x] = src;
for (x = 0; x < 4; x++)
c_data[x] = &clear_block[x][0];
av_write_image_line(src_array, c_data, c_linesize, desc, 0, 0, c, w);
}
for (plane = 0; plane < nb_planes; plane++) {
plane_line_bytes[plane] = av_image_get_linesize(pix_fmt, width, plane);
if (plane_line_bytes[plane] < 0)
return AVERROR(EINVAL);
}
if (!dst_data)
return 0;
for (plane = 0; plane < nb_planes; plane++) {
size_t bytewidth = plane_line_bytes[plane];
uint8_t *data = dst_data[plane];
int chroma_div = plane == 1 || plane == 2 ? desc->log2_chroma_h : 0;
int plane_h = ((height + ( 1 << chroma_div) - 1)) >> chroma_div;
for (; plane_h > 0; plane_h--) {
memt_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]);