http://blog.csdn.net/hfeizi/article/details/6415298
#include <assert.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "avcodec.h"
#include "avformat.h"
#include "avutil.h"
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;
#pragma pack(1)
typedef struct tagBITMAPFILEHEADER{
WORD bfType; // the flag of bmp, value is "BM"
DWORD bfSize; // size BMP file ,unit is bytes
WORD bfReserved1,bfReserved2; // 0
DWORD bfOffBits; // must be 54
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize; // must be 0x28
DWORD biWidth; //
DWORD biHeight; //
WORD biPlanes; // must be 1
WORD biBitCount; //
DWORD biCompression; //
DWORD biSizeImage; //
DWORD biXPelsPerMeter; //
DWORD biYPelsPerMeter; //
DWORD biClrUsed; //
DWORD biClrImportant; //
}BITMAPINFOHEADER;
typedef struct tagRGBQUAD{
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
}RGBQUAD;
typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO;
static int av_create_bmp(char* filename,uint8_t *pRGBBuffer,int width,int height,int bpp)
{
BITMAPFILEHEADER bmpheader;
BITMAPINFO bmpinfo;
FILE *fp;
fp = fopen(filename,"wb");
if(!fp)return -1;
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.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpinfo.bmiHeader.biWidth = width;
bmpinfo.bmiHeader.biHeight = height;
bmpinfo.bmiHeader.biPlanes = 1;
bmpinfo.bmiHeader.biBitCount = bpp;
bmpinfo.bmiHeader.biCompression = 0;
bmpinfo.bmiHeader.biSizeImage = 0;
bmpinfo.bmiHeader.biXPelsPerMeter = 100;
bmpinfo.bmiHeader.biYPelsPerMeter = 100;
bmpinfo.bmiHeader.biClrUsed = 0;
bmpinfo.bmiHeader.biClrImportant = 0;
fwrite(&bmpheader,sizeof(BITMAPFILEHEADER),1,fp);
fwrite(&bmpinfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
fwrite(pRGBBuffer,width*height*bpp/8,1,fp);
fclose(fp);
return 0;
}
int main(int argc, char *argv[])
{
static AVPacket packet;
AVFormatContext *pFormatCtx;
int i, videoStream;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame;
AVFrame *pFrameRGB;
int numBytes;
uint8_t *buffer;
int frameFinished;
const char *filename="/home/lx/video/test.mp4";
// Register all formats and codecs
av_register_all();
// Open video file
if(av_open_input_file(&pFormatCtx, filename, NULL, 0, NULL) !=0)
{
fprintf(stderr, "Open video file/n");
return -1; // Couldn't open file
}
/*if (av_find_stream_info(pFormatCtx) < 0){
fprintf(stderr, "av_find_stream_info error/n");
}*/
fprintf(stderr, "Open video file and nb_streams is %d/n", pFormatCtx->nb_streams);
// Retrieve stream information
if( (pFormatCtx)<0){
fprintf(stderr, "Retrieve stream information/n");
return -1; // Couldn't find stream information
}
// Dump information about file onto standard error
dump_format(pFormatCtx, 0, argv[1], 0);
// Find the first video stream
videoStream=-1;
for(i = 0; i < pFormatCtx->nb_streams; i ++)
if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
{
videoStream=i;
break;
}
if(videoStream==-1)
{
fprintf(stderr, "Didn't find a video stream and pFormatCtx->nb_streams is %d/n", pFormatCtx->nb_streams);
return -1; // Didn't find a video stream
}
// Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
{
fprintf(stderr, "Codec not found/n");
return -1; // Codec not found
}
// Inform the codec that we can handle truncated bitstreams -- i.e.,
// bitstreams where frame boundaries can fall in the middle of packets
if(pCodec->capabilities & CODEC_CAP_TRUNCATED)
pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;
// Open codec
if(avcodec_open(pCodecCtx, pCodec)<0)
{
fprintf(stderr, "pFrameRGB==NULL/n");
return -1; // Could not open codec
}
// Allocate video frame
pFrame=avcodec_alloc_frame();
// Allocate an AVFrame structure
pFrameRGB=avcodec_alloc_frame();
if(pFrameRGB==NULL)
{
fprintf(stderr, "pFrameRGB==NULL/n");
return -1;
}
// Determine required buffer size and allocate buffer
numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height);
buffer=(uint8_t *)malloc(numBytes);
printf("Determine required buffer size and allocate buffer/n");
// Assign appropriate parts of buffer to image planes in pFrameRGB
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
pCodecCtx->width, pCodecCtx->height);
printf("Assign appropriate parts of buffer to image planes in pFrameRGB/n");
// Read frames and save first five frames to disk
i=0;
while(av_read_frame(pFormatCtx,&packet)>=0)
{
if(packet.stream_index==videoStream)
{
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,
packet.data, packet.size);
if(frameFinished)
{
img_convert((AVPicture *)pFrameRGB, PIX_FMT_BGR24, (AVPicture *)pFrame,
pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height);
// Save the frame to disk
char pic[200];
sprintf(pic,"/home/lx/video/pic%d.bmp",i);
i++;
//printf("%d/n",*pFrameRGB);
av_create_bmp(pic,pFrameRGB->data[0],pCodecCtx->width,pCodecCtx->height,24);
//printf("/n/n%",sizeof(pFrameRGB));
}
}
av_free_packet(&packet);
}
// Free the RGB image
av_free(buffer);
av_free(pFrameRGB);
// Free the YUV frame
av_free(pFrame);
// Close the codec
avcodec_close(pCodecCtx);
// Close the video file
av_close_input_file(pFormatCtx);
return 0;
}
这是我的程序源码,实现的是从一个mp4视频文件中读取成bmp图片,可是出现了上下颠倒且图片模糊的显像,另外我仔细看过以前的贴子,知道上下颠倒可能原因在BGR和RGB上,我把img_convert函数中的RGB已经修改成BGR了,可是还是不行,还有图片模糊的问题的可能原因是什么?
img_convert((AVPicture *)pFrameRGB, PIX_FMT_BGR24..中的PIX_FMT_BGR24是必要的,不然颜色不对,但上下颠倒和它无关。bmp文件格式就是需要把图像数据按行上下颠倒存储的。av_create_bmp函数并未作此处理。给你个delphi的例子:
procedure TMediaCentre.SaveToBmpStream(AStream: TStream; ARGBData: Pointer; AWidth, AHeight, ABitsPerPixel: Integer);
var header: BITMAPFILEHEADER; info: BITMAPINFO; y, bpl: Integer; p: PByte;
begin
header.bfType := $4D42;//(Ord('M') shl 8) + Ord('B');
header.bfReserved1 := 0;
header.bfReserved2 := 0;
header.bfOffBits := SizeOf(BITMAPFILEHEADER) + SizeOf(BITMAPINFOHEADER);
header.bfSize := header.bfOffBits + AWidth * AHeight * (ABitsPerPixel div 8);
info.bmiHeader.biSize := SizeOf(BITMAPINFOHEADER);
info.bmiHeader.biWidth := AWidth;
info.bmiHeader.biHeight := AHeight;
info.bmiHeader.biPlanes := 1;
info.bmiHeader.biBitCount := ABitsPerPixel;
info.bmiHeader.biCompression := BI_RGB;
info.bmiHeader.biSizeImage := 0;
info.bmiHeader.biXPelsPerMeter := 100;
info.bmiHeader.biYPelsPerMeter := 100;
info.bmiHeader.biClrUsed := 0;
info.bmiHeader.biClrImportant := 0;
AStream.Write(header, SizeOf(BITMAPFILEHEADER));
AStream.Write(info.bmiHeader, SizeOf(BITMAPINFOHEADER));
bpl := AWidth * (ABitsPerPixel div 8);// bytes per line
for y := AHeight - 1 downto 0 do begin
p := PByte(ARGBData);
Inc(p, y * bpl);
AStream.Write(p^, bpl);
end;
end;
正确存储后图片还模糊吗?
1) why do you fail to adjust the upside-down issue? It is the basic operation in still image process the pixel data is just one dimension array, you just need move the last_line pixel to the first line,and the last-second line to second line,... 2) #if 0 // Inform the codec that we can handle truncated bitstreams -- i.e., // bitstreams where frame boundaries can fall in the middle of packets if(pCodec->capabilities & CODEC_CAP_TRUNCATED) pCodecCtx->flags|=CODEC_FLAG_TRUNCATED; #endif Note: Never touch the features/properties which you do not understand! |
解决方法就是:
删除掉这几句代码
// Inform the codec that we can handle truncated bitstreams -- i.e.,
// bitstreams where frame boundaries can fall in the middle of packets
if(pCodec->capabilities & CODEC_CAP_TRUNCATED)
pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;
参照了上面的代码,可以生产bmp文件,但无法打开,在程序代码中发现pPacket.data为空,在img_convert之前(AVPicture*)pFrame就没有得到数据。我的代码如下,请问是什么原因:
int main(int argc,char **argv)
{
//变量声明
AVFormatContext* pFormetCtx;
AVFrame* pFrame,*pFrameRGB;
AVCodecContext* pCodecCtx;
AVCodec* pCodec;
static AVPacket pPacket;
unsigned char* outBuffer,*pictureBuffer;
FILE* pOut;
int numBytes;
int frameFinished;
const char outputFile[]="e:/test/output.bmp";
//const char inputFile[]="e:/test/akiyo_qcif.yuv";
const char inputFile[]="e:/test/The Simpsons Movie - Trailer.mp4";
/*avcodec_init();
avcodec_register_all();*/
av_register_all();
//open the video
//pFormetCtx=av_alloc_format_context();
if(av_open_input_file(&pFormetCtx,inputFile,NULL,0,NULL)!=0)
{
printf("Open Input File Error!");
return -1;
}
printf("The File have %d streams!/n",pFormetCtx->nb_streams);//输出文件中的流的总个数
dump_format(pFormetCtx,0,inputFile,0);
//find the first video stream
int videoStreamIndex=-1;
for (int i=0;i<pFormetCtx->nb_streams;i++)
{
if (pFormetCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO)
{
videoStreamIndex=i;
break;
}
}
if (videoStreamIndex==-1)
{
printf("/nError!Cannot find video stream!");
}
pCodecCtx=pFormetCtx->streams[videoStreamIndex]->codec;
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if (pCodec==NULL)
{
printf("/nError!Cannot find proper decoder!");
}
if(pCodec->capabilities & CODEC_CAP_TRUNCATED)
pCodecCtx->flags|=CODEC_FLAG_TRUNCATED;
//open the codec
if (avcodec_open(pCodecCtx,pCodec)<0)
{
printf("/nError!Cannot open decoder!");
}
pFrame=avcodec_alloc_frame();
pFrameRGB=avcodec_alloc_frame();
//get the buffer size
numBytes=avpicture_get_size(PIX_FMT_RGB24,pCodecCtx->width,pCodecCtx->height);
/*video_encode_example(inputFile,outputFile);*/
pictureBuffer=(unsigned char*)malloc(numBytes);
avpicture_fill((AVPicture*)pFrameRGB,pictureBuffer,PIX_FMT_RGB24,pCodecCtx->width,pCodecCtx->height);//初始化pFrameRGB的数据区的各个指针
int picNum=0;
av_init_packet(&pPacket);
while ((av_read_frame(pFormetCtx,&pPacket)>=0)&& picNum<=3)
{
if (pPacket.stream_index==videoStreamIndex)
{
avcodec_decode_video(pCodecCtx,pFrame,&frameFinished,pPacket.data,pPacket.size);//??
if (frameFinished)
{
int it=img_convert((AVPicture*)pFrameRGB,PIX_FMT_BGR24,(AVPicture*)pFrame,pCodecCtx->pix_fmt,pCodecCtx->width,pCodecCtx->height);
char picName[200];
sprintf(picName,"e:/test/pic01/beijing%d.bmp",picNum);
picNum++;
create_bmp(picName,pFrameRGB->data[0],pCodecCtx->width,pCodecCtx->height,24);
printf("/npic%d done!",picNum);
}
}
av_free_packet(&pPacket);
}
av_free(pictureBuffer);
av_free(pFrameRGB);
av_free(pFrame);
avcodec_close(pCodecCtx);
av_close_input_file(pFormetCtx);
return 0;
}
BITMAPINFO::bmiHeader ::biHeight 代表的是bmp文件的高度,同时它也决定了图像像素值的存储,如果是正值,则像素值是button-up的,如果是负值是相反。
这里程序运行后生成的bmp文件图像倒立,也正是与这个变量有关。
如果在程序里改动一下:
bmpinfo.bmiHeader.biHeight = -height;
生成的bmp文件图像就正过来了
解决办法:是BGR与RGB的问题