Decoder 解码器:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#define WORD uint16_t
#define DWORD uint32_t
#define LONG int32_t
#pragma pack(2)
typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
void saveBMP(struct SwsContext *img_convert_ctx, AVFrame *frame, int w, int h, char *filename)
{
//1 先进行转换, YUV420=>RGB24:
// int w = img_convert_ctx->frame_dst->width;
// int h = img_convert_ctx->frame_dst->height;
int data_size = w * h * 3;
AVFrame *pFrameRGB = av_frame_alloc();
//avpicture_fill((AVPicture *)pFrameRGB, buffer, AV_PIX_FMT_BGR24, w, h);
pFrameRGB->width = w;
pFrameRGB->height = h;
pFrameRGB->format = AV_PIX_FMT_BGR24;
av_frame_get_buffer(pFrameRGB, 0);
sws_scale(img_convert_ctx,
(const uint8_t* const *)frame->data,
frame->linesize,
0, frame->height, pFrameRGB->data, pFrameRGB->linesize);
//2 构造 BITMAPINFOHEADER
BITMAPINFOHEADER header;
header.biSize = sizeof(BITMAPINFOHEADER);
header.biWidth = w;
header.biHeight = h*(-1);
header.biBitCount = 24;
header.biCompression = 0;
header.biSizeImage = 0;
header.biClrImportant = 0;
header.biClrUsed = 0;
header.biXPelsPerMeter = 0;
header.biYPelsPerMeter = 0;
header.biPlanes = 1;
//3 构造文件头
BITMAPFILEHEADER bmpFileHeader = {0,};
//HANDLE hFile = NULL;
DWORD dwTotalWriten = 0;
DWORD dwWriten;
bmpFileHeader.bfType = 0x4d42; //'BM';
bmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+ data_size;
bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
FILE* pf = fopen(filename, "wb");
fwrite(&bmpFileHeader, sizeof(BITMAPFILEHEADER), 1, pf);
fwrite(&header, sizeof(BITMAPINFOHEADER), 1, pf);
fwrite(pFrameRGB->data[0], 1, data_size, pf);
fclose(pf);
//释放资源
//av_free(buffer);
av_freep(&pFrameRGB[0]);
av_free(pFrameRGB);
}
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
char *filename)
{
FILE *f;
int i;
f = fopen(filename,"w");
fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
for (i = 0; i < ysize; i++)
fwrite(buf + i * wrap, 1, xsize, f);
fclose(f);
}
static int decode_write_frame(const char *outfilename, AVCodecContext *avctx,
struct SwsContext *img_convert_ctx, AVFrame *frame, AVPacket *pkt)
{
int ret = -1;
char buf[1024];
ret = avcodec_send_packet(avctx, pkt);
if (ret < 0) {
fprintf(stderr, "Error while decoding frame, %s(%d)\n", av_err2str(ret), ret);
return ret;
}
while (ret >= 0) {
fflush(stdout);
ret = avcodec_receive_frame(avctx, frame);
if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF){
return 0;
}else if( ret < 0){
return -1;
}
/* the picture is allocated by the decoder, no need to free it */
snprintf(buf, sizeof(buf), "%s-%d.bmp", outfilename, avctx->frame_number);
/*pgm_save(frame->data[0], frame->linesize[0],
frame->width, frame->height, buf);*/
saveBMP(img_convert_ctx, frame, 160, 120, buf);
}
return 0;
}
int main(int argc, char **argv)
{
int ret;
int idx;
const char *filename, *outfilename;
AVFormatContext *fmt_ctx = NULL;
const AVCodec *codec = NULL;
AVCodecContext *ctx = NULL;
AVStream *inStream = NULL;
AVFrame *frame = NULL;
AVPacket avpkt;
struct SwsContext *img_convert_ctx;
if (argc <= 2) {
fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
exit(0);
}
filename = argv[1];
outfilename = argv[2];
/* open input file, and allocate format context */
if (avformat_open_input(&fmt_ctx, filename, NULL, NULL) < 0) {
fprintf(stderr, "Could not open source file %s\n", filename);
exit(1);
}
/* retrieve stream information */
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
fprintf(stderr, "Could not find stream information\n");
exit(1);
}
/* dump input information to stderr */
//av_dump_format(fmt_ctx, 0, filename, 0);
//av_init_packet(&avpkt);
/* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
//memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
//
idx = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (idx < 0) {
fprintf(stderr, "Could not find %s stream in input file '%s'\n",
av_get_media_type_string(AVMEDIA_TYPE_VIDEO), filename);
return idx;
}
inStream = fmt_ctx->streams[idx];
/* find decoder for the stream */
codec = avcodec_find_decoder(inStream->codecpar->codec_id);
if (!codec) {
fprintf(stderr, "Failed to find %s codec\n",
av_get_media_type_string(AVMEDIA_TYPE_VIDEO));
return AVERROR(EINVAL);
}
ctx = avcodec_alloc_context3(NULL);
if (!ctx) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
/* Copy codec parameters from input stream to output codec context */
if ((ret = avcodec_parameters_to_context(ctx, inStream->codecpar)) < 0) {
fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",
av_get_media_type_string(AVMEDIA_TYPE_VIDEO));
return ret;
}
/* open it */
if (avcodec_open2(ctx, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}
img_convert_ctx = sws_getContext(ctx->width, ctx->height,
ctx->pix_fmt,
160, 120,
AV_PIX_FMT_BGR24,
SWS_BICUBIC, NULL, NULL, NULL);
if (img_convert_ctx == NULL)
{
fprintf(stderr, "Cannot initialize the conversion context\n");
exit(1);
}
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
while (av_read_frame(fmt_ctx, &avpkt) >= 0) {
if(avpkt.stream_index == idx){
if (decode_write_frame(outfilename, ctx, img_convert_ctx, frame, &avpkt) < 0)
exit(1);
}
av_packet_unref(&avpkt);
}
decode_write_frame(outfilename, ctx, img_convert_ctx, frame, NULL);
avformat_close_input(&fmt_ctx);
sws_freeContext(img_convert_ctx);
avcodec_free_context(&ctx);
av_frame_free(&frame);
return 0;
}
saveBMP
函数分析
这个函数负责将一帧 AVFrame
(假设是 YUV 格式) 转换为 BGR24 格式,并将其保存为 BMP 文件。
void saveBMP(struct SwsContext *img_convert_ctx, AVFrame *frame, int w, int h, char *filename)
{
// 定义 saveBMP 函数。
// - SwsContext *img_convert_ctx: FFmpeg 的图像转换上下文。
// - AVFrame *frame: 输入的原始视频帧 (YUV)。
// - int w, int h: 目标 BMP 图像的宽度和高度。
// - char *filename: 要保存的 BMP 文件名。
// 1 先进行转换, YUV420=>RGB24: (中文注释)
int data_size = w * h * 3; // 计算 BGR24 图像数据的大小 (宽 * 高 * 3 字节/像素)。
AVFrame *pFrameRGB = av_frame_alloc(); // 分配一个新的 AVFrame 用于存储转换后的 BGR 数据。
pFrameRGB->width = w; // 设置 BGR 帧的宽度。
pFrameRGB->height = h; // 设置 BGR 帧的高度。
pFrameRGB->format = AV_PIX_FMT_BGR24;// 设置 BGR 帧的像素格式为 BGR24 (BMP 通常使用 BGR)。
av_frame_get_buffer(pFrameRGB, 0); // 为 BGR 帧分配数据缓冲区。
sws_scale(img_convert_ctx, // 调用 sws_scale 执行转换和缩放。
(const uint8_t* const *)frame->data, // 输入帧的数据指针。
frame->linesize, // 输入帧的行大小数组。
0, frame->height, // 输入帧的起始行和高度 (0 表示从头开始,处理整个高度)。
pFrameRGB->data, // 输出帧的数据指针。
pFrameRGB->linesize); // 输出帧的行大小数组。
// 2 构造 BITMAPINFOHEADER (中文注释)
BITMAPINFOHEADER header; // 声明 BMP 信息头。
header.biSize = sizeof(BITMAPINFOHEADER); // 设置结构体大小。
header.biWidth = w; // 设置宽度。
header.biHeight = h*(-1); // 设置高度为负数,表示图像是 *自顶向下* 存储的,这是 BMP 的常见做法。
header.biBitCount = 24; // 设置位深为 24。
header.biCompression = 0; // 设置不压缩。
header.biSizeImage = 0; // 设置图像大小为 0。
header.biClrImportant = 0; // 设置重要颜色数为 0。
header.biClrUsed = 0; // 设置使用颜色数为 0。
header.biXPelsPerMeter = 0; // 设置水平分辨率为 0。
header.biYPelsPerMeter = 0; // 设置垂直分辨率为 0。
header.biPlanes = 1; // 设置平面数为 1。
// 3 构造文件头 (中文注释)
BITMAPFILEHEADER bmpFileHeader = {0,}; // 声明并清零 BMP 文件头。
DWORD dwTotalWriten = 0; // (未使用)
DWORD dwWriten; // (未使用)
bmpFileHeader.bfType = 0x4d42; // 设置文件类型为 'BM'。
bmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+ data_size; // 计算总文件大小。
bmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); // 计算数据偏移量。
FILE* pf = fopen(filename, "wb"); // 以二进制写入模式打开输出文件。
fwrite(&bmpFileHeader, sizeof(BITMAPFILEHEADER), 1, pf); // 写入文件头。
fwrite(&header, sizeof(BITMAPINFOHEADER), 1, pf); // 写入信息头。
fwrite(pFrameRGB->data[0], 1, data_size, pf); // 写入 BGR 像素数据。
fclose(pf); // 关闭文件。
// 释放资源 (中文注释)
av_freep(&pFrameRGB->data[0]); // 释放 BGR 帧的数据缓冲区 (注意:av_frame_get_buffer 分配的内存通常由 av_frame_free 统一管理,直接释放 data[0] 可能不安全,更好的做法是只调用 av_frame_free)。
av_frame_free(&pFrameRGB); // 释放 BGR 帧结构体。
}
pgm_save
函数分析
这个函数用于将 YUV 帧的 Y 分量 (灰度图) 保存为 PGM 格式的文件。虽然在 main
函数中被注释掉了,但它是一个有用的调试工具。
static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
char *filename)
{
// 定义 pgm_save 函数。
// - buf: Y 分量数据指针。
// - wrap: Y 分量的行大小 (linesize)。
// - xsize, ysize: 图像的宽和高。
// - filename: 输出文件名。
FILE *f; // 文件指针。
int i; // 循环变量。
f = fopen(filename,"w"); // 打开文件 (文本模式,但 PGM P5 是二进制)。
fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255); // 写入 PGM P5 格式的头 (P5 表示二进制灰度图,255 表示最大灰度值)。
for (i = 0; i < ysize; i++) // 循环每一行。
fwrite(buf + i * wrap, 1, xsize, f); // 写入该行的像素数据。注意:这里没有处理行大小 (wrap) 可能大于宽度 (xsize) 的情况,但 fwrite 会正确写入 xsize 字节。
fclose(f); // 关闭文件。
}
main
函数分析
int main(int argc, char **argv)
{
int ret; // 返回值。
int idx; // 视频流索引。
const char *filename, *outfilename; // 输入文件名和输出 *基础* 文件名。
AVFormatContext *fmt_ctx = NULL; // 格式上下文。
const AVCodec *codec = NULL; // 解码器。
AVCodecContext *ctx = NULL; // 解码器上下文。
AVStream *inStream = NULL; // 输入视频流。
AVFrame *frame = NULL; // 用于接收解码帧。
AVPacket avpkt; // 用于读取包。
struct SwsContext *img_convert_ctx; // 图像转换上下文。
if (argc <= 2) { // 检查参数。
fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
exit(0);
}
filename = argv[1]; // 获取输入文件名。
outfilename = argv[2]; // 获取输出基础文件名。
/* open input file, and allocate format context */
if (avformat_open_input(&fmt_ctx, filename, NULL, NULL) < 0) { // 打开文件。
fprintf(stderr, "Could not open source file %s\n", filename);
exit(1);
}
/* retrieve stream information */
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) { // 获取流信息。
fprintf(stderr, "Could not find stream information\n");
exit(1);
}
/* dump input information to stderr */
//av_dump_format(fmt_ctx, 0, filename, 0); // (注释掉了) 打印文件信息。
idx = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); // 查找最佳视频流。
if (idx < 0) { // 检查是否找到。
fprintf(stderr, "Could not find %s stream in input file '%s'\n",
av_get_media_type_string(AVMEDIA_TYPE_VIDEO), filename);
return idx;
}
inStream = fmt_ctx->streams[idx]; // 获取视频流指针。
/* find decoder for the stream */
codec = avcodec_find_decoder(inStream->codecpar->codec_id); // 查找解码器。
if (!codec) { // 检查是否找到。
fprintf(stderr, "Failed to find %s codec\n",
av_get_media_type_string(AVMEDIA_TYPE_VIDEO));
return AVERROR(EINVAL);
}
ctx = avcodec_alloc_context3(NULL); // 分配解码器上下文。
if (!ctx) { // 检查分配。
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
/* Copy codec parameters from input stream to output codec context */
if ((ret = avcodec_parameters_to_context(ctx, inStream->codecpar)) < 0) { // 复制参数。
fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",
av_get_media_type_string(AVMEDIA_TYPE_VIDEO));
return ret;
}
/* open it */
if (avcodec_open2(ctx, codec, NULL) < 0) { // 打开解码器。
fprintf(stderr, "Could not open codec\n");
exit(1);
}
img_convert_ctx = sws_getContext(ctx->width, ctx->height, // 获取图像转换上下文。
ctx->pix_fmt, // 输入宽度、高度、格式。
160, 120, // 输出宽度、高度 (硬编码)。
AV_PIX_FMT_BGR24, // 输出格式 (BGR24)。
SWS_BICUBIC, NULL, NULL, NULL); // 缩放算法 (双三次插值)。
if (img_convert_ctx == NULL) // 检查转换上下文是否创建成功。
{
fprintf(stderr, "Cannot initialize the conversion context\n");
exit(1);
}
frame = av_frame_alloc(); // 分配 AVFrame 用于解码。
if (!frame) { // 检查分配。
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
while (av_read_frame(fmt_ctx, &avpkt) >= 0) { // 循环读取数据包。
if(avpkt.stream_index == idx){ // 如果包属于视频流。
if (decode_write_frame(outfilename, ctx, img_convert_ctx, frame, &avpkt) < 0) // 调用解码和保存函数。
exit(1); // 如果失败则退出。
}
av_packet_unref(&avpkt); // 释放包引用。
}
decode_write_frame(outfilename, ctx, img_convert_ctx, frame, NULL); // 发送 NULL 包以刷新解码器。
avformat_close_input(&fmt_ctx); // 关闭输入文件。
sws_freeContext(img_convert_ctx); // 释放转换上下文。
avcodec_free_context(&ctx); // 释放解码器上下文。
av_frame_free(&frame); // 释放 AVFrame。
return 0; // 程序结束。
}
总结:
这个程序演示了如何:
- 使用
libavformat
打开视频文件并读取数据包。 - 使用
libavcodec
解码视频数据包为原始AVFrame
。 - 使用
libswscale
将解码后的帧进行颜色空间转换(例如 YUV 到 BGR)和图像缩放。 - 手动构建 BMP 文件头和信息头。
- 将转换后的图像数据写入 BMP 文件,实现视频抽帧并保存为图片序列的功能。
它是一个将视频转换为一系列 BMP 图像的实用工具。