video_decode_example
ctx->pix_fmt:表示图像的像素格式,即图像数据的存储格式,如RGB、YUV等。
ctx->width:表示图像的宽度,即图像的水平像素数。
ctx->height:表示图像的高度,即图像的垂直像素数。
16:表示图像数据的对齐方式,通常为1、2、4或8等。这里的16表示按照16字节对齐。
函数av_image_get_buffer_size会根据所提供的图像参数计算出所需的缓冲区大小,并返回该值。这个大小通常用于分配足够的内存来存储解码后的图像数据。
enum AVPixelFormat pix_fmt = AV_PIX_FMT_YUV420P; // YUV420P像素格式
int width = 1920; // 图像宽度
int height = 1080; // 图像高度
int align = 16; // 对齐方式
byte_buffer_size = 1920 * 1080 * 3 / 2 = 3110400
这里的计算公式是这样的:
YUV420P格式的图像数据由一个Y分量和两个色度分量(U和V)组成。
Y分量占据图像大小的一半(即1920x1080的一半),而每个色度分量占据Y分量大小的1/4。
因此,总的数据大小为1920 * 1080 + 1920 * 1080 / 4 + 1920 * 1080 / 4 = 3110400字节。
// 解码视频文件的函数
static int video_decode_example(const char *input_filename)
{
AVCodec *codec = NULL; // 解码器
AVCodecContext *ctx= NULL; // 解码器上下文
AVCodecParameters *origin_par = NULL; // 原始参数
AVFrame *fr = NULL; // 帧
uint8_t *byte_buffer = NULL; // 字节缓冲区
AVPacket pkt; // 数据包
AVFormatContext *fmt_ctx = NULL; // 格式上下文
int number_of_written_bytes; // 写入的字节数
int video_stream; // 视频流索引
int got_frame = 0; // 是否获取了帧
int byte_buffer_size; // 字节缓冲区大小
int i = 0; // 计数器
int result; // 操作结果
int end_of_stream = 0; // 是否到达流的结尾
// 打开输入文件
result = avformat_open_input(&fmt_ctx, input_filename, NULL, NULL);
if (result < 0) {
av_log(NULL, AV_LOG_ERROR, "Can't open file\n");
return result;
}
// 获取流信息
result = avformat_find_stream_info(fmt_ctx, NULL);
if (result < 0) {
av_log(NULL, AV_LOG_ERROR, "Can't get stream info\n");
return result;
}
// 找到最佳的视频流
video_stream = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (video_stream < 0) {
av_log(NULL, AV_LOG_ERROR, "Can't find video stream in input file\n");
return -1;
}
// 获取视频流的原始参数
origin_par = fmt_ctx->streams[video_stream]->codecpar;
// 查找解码器
codec = avcodec_find_decoder(origin_par->codec_id);
if (!codec) {
av_log(NULL, AV_LOG_ERROR, "Can't find decoder\n");
return -1;
}
// 分配解码器上下文
ctx = avcodec_alloc_context3(codec);
if (!ctx) {
av_log(NULL, AV_LOG_ERROR, "Can't allocate decoder context\n");
return AVERROR(ENOMEM);
}
// 将原始参数复制到解码器上下文
result = avcodec_parameters_to_context(ctx, origin_par);
if (result) {
av_log(NULL, AV_LOG_ERROR, "Can't copy decoder context\n");
return result;
}
// 打开解码器
result = avcodec_open2(ctx, codec, NULL);
if (result < 0) {
av_log(ctx, AV_LOG_ERROR, "Can't open decoder\n");
return result;
}
// 分配帧
fr = av_frame_alloc();
if (!fr) {
av_log(NULL, AV_LOG_ERROR, "Can't allocate frame\n");
return AVERROR(ENOMEM);
}
// 获取字节缓冲区大小
byte_buffer_size = av_image_get_buffer_size(ctx->pix_fmt, ctx->width, ctx->height, 16);
byte_buffer = av_malloc(byte_buffer_size);
if (!byte_buffer) {
av_log(NULL, AV_LOG_ERROR, "Can't allocate buffer\n");
return AVERROR(ENOMEM);
}
// 打印视频流的时间基准信息
printf("#tb %d: %d/%d\n", video_stream, fmt_ctx->streams[video_stream]->time_base.num, fmt_ctx->streams[video_stream]->time_base.den);
i = 0;
av_init_packet(&pkt);
// 循环读取数据包并解码
do {
if (!end_of_stream)
if (av_read_frame(fmt_ctx, &pkt) < 0)
end_of_stream = 1;
if (end_of_stream) {
pkt.data = NULL;
pkt.size = 0;
}
if (pkt.stream_index == video_stream || end_of_stream) {
got_frame = 0;
if (pkt.pts == AV_NOPTS_VALUE)
pkt.pts = pkt.dts = i;
// 解码视频帧
result = avcodec_decode_video2(ctx, fr, &got_frame, &pkt);
if (result < 0) {
av_log(NULL, AV_LOG_ERROR, "Error decoding frame\n");
return result;
}
if (got_frame) {
// 将解码后的帧数据复制到字节缓冲区
number_of_written_bytes = av_image_copy_to_buffer(byte_buffer, byte_buffer_size,
(const uint8_t* const *)fr->data, (const int*) fr->linesize,
ctx->pix_fmt, ctx->width, ctx->height, 1);
if (number_of_written_bytes < 0) {
av_log(NULL, AV_LOG_ERROR, "Can't copy image to buffer\n");
return number_of_written_bytes;
}
// 打印帧信息
printf("%d, %10"PRId64", %10"PRId64", %8"PRId64", %8d, 0x%08lx\n", video_stream,
fr->pts, fr->pkt_dts, fr->pkt_duration,
number_of_written_bytes, av_adler32_update(0, (const uint8_t*)byte_buffer, number_of_written_bytes));
}
// 释放数据包
av_packet_unref(&pkt);
av_init_packet(&pkt);
}
i++;
} while (!end_of_stream || got_frame);
// 释放数据包和帧
av_packet_unref(&pkt);
av_frame_free(&fr);
// 关闭解码器
avcodec_close(ctx);
// 关闭输入文件
avformat_close_input(&fmt_ctx);
// 释放解码器上下文
avcodec_free_context(&ctx);
// 释放字节缓冲区
av_freep(&byte_buffer);
return 0;
}