通過FFmpeg將多媒體文件解碼后保存成Bmp圖像(YUV420 RGB32)


/* g++ -o test test.cpp -lavformat -lavcodec -lavutil -lz -lm -lpthread -lswscale */

#include <string>
#include <cassert>
#include <iostream>
#include <sstream>
//#include <tchar.h>


extern "C"
{
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
#include <libavutil/avutil.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavcodec/avcodec.h>
#include <stdlib.h>
#include <unistd.h>
};
//#pragma comment(lib, "avformat.lib")
//#pragma comment(lib, "avutil.lib")
//#pragma comment(lib, "avcodec.lib")
//#pragma comment(lib, "swscale.lib")
#pragma pack(1)
#define BOOL int
#define TRUE 1
#define FALSE 0
#define BI_RGB 0x0

char *itoa(int num,char *str,int radix) {
/* 索引表 */
char index[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
unsigned unum; /* 中間變量 */
int i=0,j,k;
/* 確定unum的值 */
if(radix==10&&num<0) /* 十進制負數 */
{
unum=(unsigned)-num;
str[i++]='-';
} else unum=(unsigned)num; /* 其他情況 */
/* 逆序 */
do {
str[i++]=index[unum%(unsigned)radix];
unum/=radix;
}while(unum);
str[i]='\0';
/* 轉換 */
if(str[0]=='-') k=1; /* 十進制負數 */
else k=0;
char temp;
for(j=k;j<=(i-k-1)/2;j++)
{
temp=str[j];
str[j]=str[i-j-1];
str[i-j-1]=temp;
}
return str;
}

static AVFrame *alloc_picture(enum PixelFormat pix_fmt, int width, int height);
BOOL CreateBmp(const char *filename, uint8_t *pRGBBuffer, int width, int height, int bpp) ;
int main(int argc, char* argv[])
{
int iResult = 0;
//* 注冊
av_register_all();
//* 文件名.
//std::string strFile = "e:\\高碼\\13587戈壁母親片花__016.mpg";
const char *strFile = "input.MP4";
//* 打開文件
AVFormatContext* pavFmtCxt = NULL;
//iResult = av_open_input_file(&pavFmtCxt, strFile.c_str(), NULL, 0, NULL);
iResult = avformat_open_input(&pavFmtCxt, strFile, NULL, NULL);
assert(iResult == 0);

iResult = avformat_find_stream_info(pavFmtCxt, NULL);
assert(iResult >= 0);
int iVidStrmID = -1;
for (int i = 0; i < pavFmtCxt->nb_streams; ++i)
{
if (AVMEDIA_TYPE_VIDEO == pavFmtCxt->streams[i]->codec->codec_type)
{
iVidStrmID = i;
}
}
assert(iVidStrmID != -1);

//* 查找,打開解碼器.
AVCodec* pDecodec = avcodec_find_decoder(
pavFmtCxt->streams[iVidStrmID]->codec->codec_id);
iResult = avcodec_open2(pavFmtCxt
->streams[iVidStrmID]->codec, pDecodec, NULL);
assert(iResult >= 0);

av_dump_format(pavFmtCxt, iVidStrmID, strFile, 0);
//* 讀取文件,解碼.
AVFrame* pFrame = avcodec_alloc_frame();
AVPacket pkt;
av_init_packet(&pkt);
//* Seek
//av_seek_frame(pavFmtCxt, 0, 493, AVSEEK_FLAG_FRAME);
while (av_read_frame(pavFmtCxt, &pkt)>= 0)
{
if (pkt.stream_index == iVidStrmID)
{
int iFinished = 0;
AVCodecContext* pavCCxt = NULL;
pavCCxt = pavFmtCxt->streams[iVidStrmID]->codec;
int iDecoded = avcodec_decode_video2(pavCCxt, pFrame,
&iFinished, &pkt);
if (iDecoded > 0 && iFinished)
{
std::ostringstream ostrm;
//* 解碼成功.輸出PTS,
ostrm<<"pts_"
<<pkt.pts//<<pavFmtCxt->streams[iVidStrmID]->pts_buffer[0]
<<"\n";
//OutputDebugStringA(ostrm.str().c_str());

int width, height;
width = pavFmtCxt->streams[iVidStrmID]->codec->width;
height = pavFmtCxt->streams[iVidStrmID]->codec->height;
//* 將YUV420P轉換成RGB32.
SwsContext* pSwsCxt = sws_getContext(width,
height,
PIX_FMT_YUV420P,
width,
height,
PIX_FMT_RGB32,
SWS_BILINEAR, NULL, NULL, NULL);
uint8_t *rgb_data = static_cast<uint8_t*>(av_malloc(width*height*4));
uint8_t *rgb_src[3]= {rgb_data, NULL, NULL};
int rgb_stride[3]={4*width, 0, 0};
assert(pSwsCxt);
iResult = sws_scale(pSwsCxt, pFrame->data, pFrame->linesize,
0, height, rgb_src, rgb_stride);
assert(iResult == height);

/* {{ 測試代碼,RGB32,YUV420之間的轉換.
//* 將RGB32轉換為YUV420P
AVFrame* pYUVFrm = alloc_picture(PIX_FMT_YUV420P, width, height);
SwsContext* pSwsCxtYUV = sws_getContext(width, height, PIX_FMT_RGB32,
width, height,
PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);//* 注意Flag的值.
iResult = sws_scale(pSwsCxtYUV, rgb_src, rgb_stride,
0, height, pYUVFrm->data, pYUVFrm->linesize);
assert(iResult == height);

//* 再轉換成RGB32
::memset(rgb_data, 0, width*height*4);
iResult = sws_scale(pSwsCxt, pYUVFrm->data, pYUVFrm->linesize,
0, height, rgb_src, rgb_stride);
assert(iResult == height);
//* }} */
char sz[100];
itoa(pkt.pts, sz, 10);
CreateBmp(sz, rgb_data, width, height, 32);
::memset(rgb_data, 0, width*height*4);
av_freep(&rgb_data);

//* 注意SwsContext必須用這個函數釋放.
sws_freeContext(pSwsCxt);

/* {{ 測試代碼, 打開上面必須打開這里.否則會內存泄漏.
sws_freeContext(pSwsCxtYUV);

av_free(pYUVFrm->data[0]);
av_free(pYUVFrm);
pYUVFrm = NULL;
//* }} */
}
else
{
//::OutputDebugStringA("解碼失敗");
printf("解碼失敗");
}
}
}
return 0;
}

typedef struct tagBITMAPFILEHEADER
{
unsigned short bfType; //2 位圖文件的類型,必須為“BM”
unsigned long bfSize; //4 位圖文件的大小,以字節為單位
unsigned short bfReserved1; //2 位圖文件保留字,必須為0
unsigned short bfReserved2; //2 位圖文件保留字,必須為0
unsigned long bfOffBits; //4 位圖數據的起始位置,以相對於位圖文件頭的偏移量表示,以字節為單位
} BITMAPFILEHEADER;//該結構占據14個字節。
// printf("%d\n",sizeof(BITMAPFILEHEADER));

typedef struct tagBITMAPINFOHEADER{
unsigned long biSize; //4 本結構所占用字節數
long biWidth; //4 位圖的寬度,以像素為單位
long biHeight; //4 位圖的高度,以像素為單位
unsigned short biPlanes; //2 目標設備的平面數不清,必須為1
unsigned short biBitCount;//2 每個像素所需的位數,必須是1(雙色), 4(16色),8(256色)或24(真彩色)之一
unsigned long biCompression; //4 位圖壓縮類型,必須是 0(不壓縮),1(BI_RLE8壓縮類型)或2(BI_RLE4壓縮類型)之一
unsigned long biSizeImage; //4 位圖的大小,以字節為單位
long biXPelsPerMeter; //4 位圖水平分辨率,每米像素數
long biYPelsPerMeter; //4 位圖垂直分辨率,每米像素數
unsigned long biClrUsed;//4 位圖實際使用的顏色表中的顏色數
unsigned long biClrImportant;//4 位圖顯示過程中重要的顏色數
} BITMAPINFOHEADER;//該結構占據40個字節。

BOOL CreateBmp(const char *filename, uint8_t *pRGBBuffer, int width, int height, int bpp)
{
BITMAPFILEHEADER bmpheader;
BITMAPINFOHEADER bmpinfo;
FILE *fp = NULL;

fp = fopen(filename,"wb");
if( fp == NULL )
{
return FALSE;
}

bmpheader.bfType = ('M' <<8)|'B';
bmpheader.bfReserved1 = 0;
bmpheader.bfReserved2 = 0;
bmpheader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmpheader.bfSize = bmpheader.bfOffBits + width*height*bpp/8;

bmpinfo.biSize = sizeof(BITMAPINFOHEADER);
bmpinfo.biWidth = width;
bmpinfo.biHeight = 0 - height;
bmpinfo.biPlanes = 1;
bmpinfo.biBitCount = bpp;
bmpinfo.biCompression = BI_RGB;
bmpinfo.biSizeImage = 0;
bmpinfo.biXPelsPerMeter = 100;
bmpinfo.biYPelsPerMeter = 100;
bmpinfo.biClrUsed = 0;
bmpinfo.biClrImportant = 0;

fwrite(&bmpheader,sizeof(BITMAPFILEHEADER),1,fp);
fwrite(&bmpinfo,sizeof(BITMAPINFOHEADER),1,fp);
fwrite(pRGBBuffer,width*height*bpp/8,1,fp);
fclose(fp);
fp = NULL;

return TRUE;
}

static AVFrame *alloc_picture(enum PixelFormat pix_fmt, int width, int height)
{
AVFrame *picture;
uint8_t *picture_buf;
int size;

picture = avcodec_alloc_frame();
if (!picture)
return NULL;
size = avpicture_get_size(pix_fmt, width, height);
picture_buf = (uint8_t*)av_malloc(size);
if (!picture_buf) {
av_free(picture);
return NULL;
}
avpicture_fill((AVPicture *)picture, picture_buf,
pix_fmt, width, height);
return picture;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM