`
844604778
  • 浏览: 551399 次
文章分类
社区版块
存档分类
最新评论

基于ffmpeg-1.1的视频监控,输出到LCD上

 
阅读更多
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
#include <libavfilter/avfilter.h>
#include <signal.h>
#include <pthread.h>
#include "myhead.h"
#include "lcd.h"


struct lcd_info_t *lcdinfo = NULL;
AVCodecContext *pCodecCtx = NULL; // 保存了相应流的详细编码信息,比如视频的宽、高,编码类型等
AVFrame *pFrameRGB = NULL;


pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; /* 初始化互斥锁 */
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; /* 初始化条件变量 */
int stop = 0;


void *lcd_routine(void *arg)
{
sleep(1);
while (!stop) {
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond,&mutex); /* 等待 */
show16bpp(lcdinfo, 0, 16, pCodecCtx->width, pCodecCtx->height, (u16 *)pFrameRGB->data[0]);
pthread_mutex_unlock(&mutex);
}

return (void *)0;
}


void signal_handle(int sig)
{
if (SIGINT == sig) {
stop = 1;
}
}


/* ffmpeg中的AVFormat库可以帮助进行这一“分拆音视频流”的过程;而AVCodec则帮助解码视频。 */
int main (int argc, char **argv)
{
if (argc < 2) {
my_debug("Usage:%s file_name\n", argv[0]);
exit(1);
}

if (SIG_ERR == signal(SIGINT, signal_handle))
err_exit("signal");


/* LCD设备文件 */
char *fb_dev = "/dev/fb0";
lcdinfo = lcd_init(fb_dev);
if (NULL == lcdinfo)
err_exit("lcd initialize failure");

av_register_all();// 调用它用以注册所有支持的文件格式以及编解码器
avdevice_register_all(); // 初始化和注册所有设备

/* AVFormatContext保存需要读入的文件的格式信息,比如流的个数以及流数据等*/
AVFormatContext *pFormatCtx = NULL;// 必须为NULL或者由avformat_alloc_context分配得到

if (strstr(argv[1], "/dev/video")) {
AVInputFormat *pInputFmt = av_find_input_format("video4linux2");
if (NULL == pInputFmt)
err_exit("av_find_input_format");

if (avformat_open_input(&pFormatCtx, argv[1], pInputFmt, NULL) != 0)
err_exit("avformat_open_input");
} else if (avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) != 0)
err_exit("avformat_open_input");

/*
** avformat_open_input函数只是读文件头,并不会填充流信息,因此我们需要接下来调用
** avformat_find_stream_info获取文件中的流信息,此函数会读取packet,并确定文件中所有的流信息,
** pFormatCtx->streams指向文件中的流,但此函数并不会改变文件指针,读取的packet会给后面的
** 解码进行处理.最后调用一个帮助函数av_dump_format,输出文件的信息,也就是我们在使用ffmpeg时
** 能看到的文件详细信息.第二个参数指定输出哪条流的信息,-1表示给ffmpeg自己选择.最后一个参数
** 用于指定dump的是不是输出文件,我们dump的是输入文件,因此一定要是0.
*/
if(avformat_find_stream_info(pFormatCtx, NULL ) < 0)
err_exit("avformat_find_stream_info");
av_dump_format(pFormatCtx, -1, argv[1], 0);


/* 现在 pFormatCtx->streams 中已经有所有流了,因此现在我们遍历它找到第一条视频流 */
int videoStream = -1, i;
for(i = 0; i < pFormatCtx->nb_streams; i++)
if( pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
break;
}
if(videoStream == -1)
err_exit("not find video stream");

/* 现在 pFormatCtx->streams 中已经有所有流了,因此现在我们遍历它找到第一条视频流 */
AVCodec *pCodec = NULL; // 真正的编解码器,其中有编解码需要调用的函数
pCodecCtx = pFormatCtx->streams[videoStream]->codec;
pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec == NULL)
err_exit("not find video decode");
if( avcodec_open2(pCodecCtx, pCodec, NULL) < 0 )
err_exit("not open video decode");

/* 接下来我们准备给即将解码的图片分配内存空间 */
/* AVFrame:用于保存数据帧的数据结构,这里的两个帧分别是保存颜色转换前后的两帧图像 */
/* pFrame用于存储解码后的数据,pFrameRGB用于存储转换后的数据 */
AVFrame *pFrame = NULL;
pFrame = avcodec_alloc_frame();
if(pFrame == NULL)
err_exit("avcodec_alloc_frame");
pFrameRGB = avcodec_alloc_frame();
if(pFrameRGB == NULL)
err_exit("avcodec_alloc_frame");


my_debug("width:%dheight:%d\n", pCodecCtx->width, pCodecCtx->height);
/*
** 调用 avpicture_get_size 根据 pCodecCtx 中原始图像的宽高计算 RGB24 格式
** 的图像需要占用的空间大小,这是为了之后给 pFrameRGB 分配空间:
*/
int numBytes = avpicture_get_size(AV_PIX_FMT_RGB565LE, pCodecCtx->width, pCodecCtx->height);


/*
** 首先是用 av_malloc 分配上面计算大小的内存空间,然后调用
** avpicture_fill 将 pFrameRGB 跟 buffer 指向的内存关联起来
*/
uint8_t *buffer = NULL;
buffer = av_malloc(numBytes);
avpicture_fill((AVPicture *)pFrameRGB, buffer, AV_PIX_FMT_RGB565LE, pCodecCtx->width, pCodecCtx->height);
/* 一切准备好就可以开始从文件中读取视频帧并解码得到图像了
** av_read_frame 从文件中读取一个packet,对于视频来说一个packet里面包含一帧图像数据,音频可能包含多
** 个帧(当音频帧长度固定时),读到这一帧后,如果是视频帧,则使用 avcodec_decode_video2 对packet中的帧
** 进行解码,有时候解码器并不能从一个packet中解码得到一帧图像数据(比如在需要其他参考帧的情况下),因
** 此会设置 frameFinished,如果已经得到下一帧图像则设置 frameFinished 非零,否则为零.所以这里我们判
** 断 frameFinished 是否为零来确定 pFrame 中是否已经得到解码的图像.注意在每次处理完后需要调用
** av_free_packet 释放读取的packet.解码得到图像后,很有可能不是我们想要的 RGB24 格式,因此需要使用
** swscale 来做转换,调用 sws_getCachedContext 得到转换上下文,使用 sws_scale 将图形从解码后的格式转
** 换为 RGB24,最后将前50帧写人 ppm 文件.最后释放图像以及关闭文件
*/
i = 0;
int frameFinished;
AVPacket packet;// 解析文件时会将音/视频帧读入到packet中
lcd_printf(lcdinfo, 0, 0, RED, 0, 0, "制作:赵建辉 QQ:809205580");
pthread_t tid;
if (pthread_create(&tid, NULL, lcd_routine, NULL) != 0)
err_exit("pthread_create");


pthread_detach(tid);/* 设置线程分离属性 */

struct SwsContext *img_convert_ctx = NULL;
img_convert_ctx = sws_getCachedContext(img_convert_ctx, pCodecCtx->width,
pCodecCtx->height, pCodecCtx->pix_fmt,
pCodecCtx->width, pCodecCtx->height,
AV_PIX_FMT_RGB565LE, SWS_BICUBIC,
NULL, NULL, NULL);
if(!img_convert_ctx)
err_exit("Cannot initialize sws conversion context\n");


while(!stop && av_read_frame(pFormatCtx, &packet) >= 0 ) {
if(packet.stream_index == videoStream) {
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if(frameFinished) {
pthread_mutex_lock(&mutex);
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0,
pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
pthread_cond_signal(&cond); /* 获取新的一帧图像,唤醒lcd显示线程 */
pthread_mutex_unlock(&mutex);
}
}
av_free_packet(&packet);
}


if (buffer)
av_free(buffer);
if (pFrameRGB)
av_free(pFrameRGB);
if (pFrame)
av_free(pFrame);
if (pFormatCtx)
avformat_close_input(&pFormatCtx);
if (img_convert_ctx)
sws_freeContext(img_convert_ctx);
if (lcdinfo) {
if (lcd_release(lcdinfo) < 0)
err_exit("lcd_release");
}

avcodec_close(pCodecCtx);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);


return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics