ffmpeg 视频流解码
时间: 2023-12-02 22:43:10 浏览: 89
以下是使用FFmpeg API实现视频流解码的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
// 视频流解码线程
void *decode_video_thread(void *arg)
{
AVFormatContext *pFormatCtx = NULL;
AVCodecContext *pCodecCtx = NULL;
AVCodec *pCodec = NULL;
AVFrame *pFrame = NULL;
AVPacket packet;
int videoStream = -1;
int frameFinished = 0;
struct SwsContext *img_convert_ctx = NULL;
// 打开视频流
if (avformat_open_input(&pFormatCtx, "rtsp://xxx.xxx.xxx.xxx:xxxx/xxx", NULL, NULL) != 0) {
printf("Couldn't open input stream.\n");
return NULL;
}
// 查找视频流信息
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
printf("Couldn't find stream information.\n");
return NULL;
}
// 查找视频流
for (int i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
break;
}
}
if (videoStream == -1) {
printf("Couldn't find a video stream.\n");
return NULL;
}
// 获取视频解码器
pCodec = avcodec_find_decoder(pFormatCtx->streams[videoStream]->codecpar->codec_id);
if (pCodec == NULL) {
printf("Codec not found.\n");
return NULL;
}
// 初始化解码器上下文
pCodecCtx = avcodec_alloc_context3(pCodec);
if (avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoStream]->codecpar) < 0) {
printf("Couldn't copy codec context.\n");
return NULL;
}
// 打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
printf("Couldn't open codec.\n");
return NULL;
}
// 分配视频帧内存
pFrame = av_frame_alloc();
if (pFrame == NULL) {
printf("Couldn't allocate video frame.\n");
return NULL;
}
// 初始化图像转换上下文
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24,
SWS_BICUBIC, NULL, NULL, NULL);
if (img_convert_ctx == NULL) {
printf("Couldn't initialize sws context.\n");
return NULL;
}
// 读取视频流
while (av_read_frame(pFormatCtx, &packet) >= 0) {
if (packet.stream_index == videoStream) {
// 解码视频帧
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if (frameFinished) {
// 转换图像格式
uint8_t *out_buffer = (uint8_t *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1));
av_image_fill_arrays(pFrame->data, pFrame->linesize, out_buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1);
sws_scale(img_convert_ctx, (const uint8_t *const *)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrame->data, pFrame->linesize);
// 处理图像数据
// ...
av_free(out_buffer);
}
}
av_packet_unref(&packet);
}
// 释放资源
sws_freeContext(img_convert_ctx);
av_frame_free(&pFrame);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
return NULL;
}
// 主函数
int main(int argc, char *argv[])
{
// 初始化FFmpeg库
av_register_all();
avformat_network_init();
// 创建视频流解码线程
pthread_t tid;
pthread_create(&tid, NULL, decode_video_thread, NULL);
// 等待线程结束
pthread_join(tid, NULL);
return 0;
}
```
阅读全文