帧结构体
typedef struct AVFrame {
uint8_t *data[8];
int linesize[8];
int width, height;
int format;
...
} AVFrame;
RGBA
数据以 RGBARGBARGBA… 格式存放,起始地址为 frame->data[0]。其实,除了 RGBA,还有 BGRA,AGBR,ARGB,GBRA。
遍历访问 RGBA 像素帧
for (int row = 0; row < frame->height; row++) {
uint8_t *data = frame->data[0] + row * frame->linesize[0];
for (int col = 0; col < frame->width; col++) {
uint8_t R = data[(col << 2) + /*plane=*/0]; //R
uint8_t G = data[(col << 2) + /*plane=*/1]; //G
uint8_t B = data[(col << 2) + /*plane=*/2]; //B
uint8_t A = data[(col << 2) + /*plane=*/3]; //A
}
}
YUVA
Y,U,V,A 四种数据分开存放,起始地址分别为 frame->data[0],frame->data[1],frame->data[2],frame->data[3]。
遍历访问 YUV 像素帧
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
int hsub = desc->log2_chroma_w;
int vsub = desc->log2_chroma_h;
int have_alpha = desc->flags & AV_PIX_FMT_FLAG_ALPHA;
if (have_alpha) {
for (int y = 0; y < frame->height; y++) {
row[0] = frame->data[0] + y * frame->linesize[0];
row[1] = frame->data[1] + (y >> vsub) * frame->linesize[1];
row[2] = frame->data[2] + (y >> vsub) * frame->linesize[2]
row[3] = frame->data[3] + y * frame->linesize[3];
for (int x = 0; x < frame->width; x++) {
row[0][x ]; // Y
row[1][x >> hsub]; // U
row[2][x >> hsub]; // V
row[3][x ]; // A
}
}
} else {
for (int y = 0; y < frame->height; y++) {
row[0] = frame->data[0] + y * frame->linesize[0];
row[1] = frame->data[1] + (y >> vsub) * frame->linesize[1];
row[2] = frame->data[2] + (y >> vsub) * frame->linesize[2];
for (int x = 0; x < frame->width; x++) {
row[0][x ]; // Y
row[1][x >> hsub]; // U
row[2][x >> hsub]; // V
}
}
}