利用ffmpeg将H264解码为RGB

转自:http://blog.csdn.net/q339659207/article/details/23435761


由于公司买到了一个不提供解码器的设备,我不得已还要做解码的工作。在网上找了一圈,H264解码比较方便的也就是ffmpeg一系列的函数库了,原本设备中也是用这套函数库解码,但厂家不给提供,没办法,只得自己搞了。

利用H264解码分为几个步骤:

 

注意一点在添加头文件的时候要添加extern "C",不然会出现错误

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. extern "C"  
  2. {  
  3. #include <avcodec.h>  
  4. #include <avformat.h>  
  5. #include <avutil.h>  
  6. #include <swscale.h>  
  7. };  


 

 

这里申明了几个全局变量

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. AVCodec         *pCodec = NULL;  
  2. AVCodecContext  *pCodecCtx = NULL;  
  3. SwsContext      *img_convert_ctx = NULL;  
  4. AVFrame         *pFrame = NULL;  
  5. AVFrame         *pFrameRGB = NULL;  


 

1. 初始化

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int H264_Init(void)  
  2. {  
  3.     /* must be called before using avcodec lib*/  
  4.     avcodec_init();  
  5.     /* register all the codecs */  
  6.     avcodec_register_all();  
  7.   
  8.     /* find the h264 video decoder */  
  9.     pCodec = avcodec_find_decoder(CODEC_ID_H264);  
  10.     if (!pCodec) {  
  11.         fprintf(stderr, "codec not found\n");  
  12.     }  
  13.     pCodecCtx = avcodec_alloc_context();  
  14.   
  15.     /* open the coderc */  
  16.     if (avcodec_open(pCodecCtx, pCodec) < 0) {  
  17.         fprintf(stderr, "could not open codec\n");  
  18.     }  
  19.     // Allocate video frame  
  20.     pFrame = avcodec_alloc_frame();  
  21.     if(pFrame == NULL)  
  22.         return -1;  
  23.     // Allocate an AVFrame structure  
  24.     pFrameRGB=avcodec_alloc_frame();  
  25.     if(pFrameRGB == NULL)  
  26.         return -1;  
  27.   
  28.   
  29.       
  30.     return 0;  
  31.   
  32. }  

在最早使用的时候没有使用全局变量,初始化中也就只有init和regisger这两个函数,而这样做的下场是,非关键帧全部无法解码,只有关键帧才有办法解码。

2. 解码

解码的时候avcodec_decode_video函数是进行解码操作,在外部定义outputbuf的大小时,pixes*3,outsize是返回的outputbuf的size,值也是pixes*3。

 

在解码的时候这几句话的意义是将YUV420P的数据倒置。在原先使用中,发现解出来的图像居然是中心旋转图,后面在网上找了些办法,觉得这个比较实用。解码实时是很重要的,图像转化完之后也可以讲RGB图再次转化,那样也能成为一个正的图,但是那样效率就明显低了。

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. pFrame->data[0] += pFrame->linesize[0] * (pCodecCtx->height-1);  
  2. pFrame->linesize[0] *= -1;  
  3. pFrame->data[1] += pFrame->linesize[1] * (pCodecCtx->height/2 - 1);;  
  4. pFrame->linesize[1] *= -1;  
  5. pFrame->data[2] += pFrame->linesize[2] * (pCodecCtx->height/2 - 1);;  
  6. pFrame->linesize[2] *= -1;  


 

 

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. int H264_2_RGB(unsigned char *inputbuf, int frame_size, unsigned char *outputbuf, unsigned int*outsize)  
  2. {  
  3.       
  4.     int             decode_size;  
  5.     int             numBytes;  
  6.     int             av_result;  
  7.     uint8_t         *buffer = NULL;  
  8.   
  9.     printf("Video decoding\n");  
  10.   
  11.     av_result = avcodec_decode_video(pCodecCtx, pFrame, &decode_size, inputbuf, frame_size);  
  12.     if (av_result < 0)  
  13.     {  
  14.         fprintf(stderr, "decode failed: inputbuf = 0x%x , input_framesize = %d\n", inputbuf, frame_size);  
  15.         return -1;  
  16.     }  
  17.   
  18.     // Determine required buffer size and allocate buffer  
  19.     numBytes=avpicture_get_size(PIX_FMT_BGR24, pCodecCtx->width,  
  20.         pCodecCtx->height);  
  21.     buffer = (uint8_t*)malloc(numBytes * sizeof(uint8_t));  
  22.     // Assign appropriate parts of buffer to image planes in pFrameRGB  
  23.     avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_BGR24,  
  24.         pCodecCtx->width, pCodecCtx->height);  
  25.   
  26.     img_convert_ctx = sws_getCachedContext(img_convert_ctx,pCodecCtx->width,pCodecCtx->height,  
  27.         //PIX_FMT_YUV420P,pCodecCtx->width,pCodecCtx->height,pCodecCtx->pix_fmt,  
  28.         pCodecCtx->pix_fmt,pCodecCtx->width,pCodecCtx->height,PIX_FMT_RGB24 ,  
  29.         SWS_X ,NULL,NULL,NULL) ;  
  30.     if (img_convert_ctx == NULL)   
  31.     {  
  32.   
  33.         printf("can't init convert context!\n") ;  
  34.         return -1;  
  35.     }  
  36.     pFrame->data[0] += pFrame->linesize[0] * (pCodecCtx->height-1);  
  37.     pFrame->linesize[0] *= -1;  
  38.     pFrame->data[1] += pFrame->linesize[1] * (pCodecCtx->height/2 - 1);;  
  39.     pFrame->linesize[1] *= -1;  
  40.     pFrame->data[2] += pFrame->linesize[2] * (pCodecCtx->height/2 - 1);;  
  41.     pFrame->linesize[2] *= -1;  
  42.     sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize,  
  43.         0, 0 - pCodecCtx->width, pFrameRGB->data, pFrameRGB->linesize);  
  44.       
  45.     if (decode_size)  
  46.     {  
  47.         *outsize = pCodecCtx->width * pCodecCtx->height * 3;  
  48.         memcpy(outputbuf, pFrameRGB->data[0], *outsize);  
  49.     }     
  50.   
  51.   
  52.     free(buffer);  
  53.     return 0;  
  54. }  


3. 释放资源

资源的回收。

[cpp]  view plain cops 在CODE上查看代码片 派生到我的代码片
  1. void H264_Release(void)  
  2. {  
  3.     avcodec_close(pCodecCtx);  
  4.     av_free(pCodecCtx);  
  5.     av_free(pFrame);  
  6.     av_free(pFrameRGB);  
  7. }  
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用`ffmpeg`库来解码H.264视频并在Qt应用程序中显示。下面是一个基本的示例代码: ```cpp #include <QApplication> #include <QLabel> #include <QTimer> #include <QDebug> #include <QByteArray> extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> } AVFormatContext *pFormatCtx = nullptr; AVCodecContext *pCodecCtx = nullptr; AVCodec *pCodec = nullptr; AVFrame *pFrame = nullptr; AVFrame *pFrameRGB = nullptr; uint8_t *buffer = nullptr; struct SwsContext *sws_ctx = nullptr; bool openVideo(const char *filename) { if (avformat_open_input(&pFormatCtx, filename, nullptr, nullptr) != 0) { qDebug() << "Could not open file"; return false; } if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) { qDebug() << "Could not find stream information"; return false; } int videoStream = -1; for (unsigned int i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } } if (videoStream == -1) { qDebug() << "Could not find video stream"; return false; } pCodecCtx = avcodec_alloc_context3(nullptr); if (avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoStream]->codecpar) != 0) { qDebug() << "Could not initialize codec context"; return false; } pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if (pCodec == nullptr) { qDebug() << "Codec not found"; return false; } if (avcodec_open2(pCodecCtx, pCodec, nullptr) < 0) { qDebug() << "Could not open codec"; return false; } pFrame = av_frame_alloc(); pFrameRGB = av_frame_alloc(); int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1); buffer = new uint8_t[numBytes]; av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1); sws_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, SWS_BILINEAR, nullptr, nullptr, nullptr); return true; } void closeVideo() { if (sws_ctx != nullptr) sws_freeContext(sws_ctx); if (pFrameRGB != nullptr) av_frame_free(&pFrameRGB); if (pFrame != nullptr) av_frame_free(&pFrame); if (pCodecCtx != nullptr) avcodec_free_context(&pCodecCtx); if (pFormatCtx != nullptr) avformat_close_input(&pFormatCtx); if (buffer != nullptr) delete[] buffer; } int main(int argc, char *argv[]) { QApplication a(argc, argv); QLabel label; label.show(); if (!openVideo("path/to/your/video.mp4")) { qDebug() << "Failed to open video"; return -1; } QTimer timer; QObject::connect(&timer, &QTimer::timeout, [&]() { AVPacket packet; while (av_read_frame(pFormatCtx, &packet) >= 0) { if (packet.stream_index == pCodecCtx->video_stream_index) { avcodec_send_packet(pCodecCtx, &packet); while (avcodec_receive_frame(pCodecCtx, pFrame) == 0) { sws_scale(sws_ctx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize); QImage img(pFrameRGB->data[0], pCodecCtx->width, pCodecCtx->height, QImage::Format_RGB888); label.setPixmap(QPixmap::fromImage(img)); label.adjustSize(); } } av_packet_unref(&packet); } }); timer.start(33); // 30 FPS int result = a.exec(); closeVideo(); return result; } ``` 你需要将路径 `path/to/your/video.mp4` 替换为你的H.264视频文件路径。这个示例会在一个Qt窗口中显示视频帧。请注意,你需要通过Qt的一些方法来显示图像,例如使用`QLabel`来显示 `QImage`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值