AVI减压 [英] AVI decompression

查看:150
本文介绍了AVI减压的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一段代码,该代码能够从AVI文件中读取帧并提取其像素数据.到目前为止,我可以成功打开AVI文件并读取任何帧,但是在解压缩时出了点问题.
这是我使用的代码(省略了错误检查):

I''m working on a piece of code which is able to read a frame from an AVI file and extract its pixel data. So far, I can succesfully open the AVI file and read any frame, but something goes wrong when decompressing.
This is the code I use (with error checking left out):

AVIFileInit();
PAVIFILE pf;
AVIFileOpen(&pf, filename, OF_READ, 0);
PAVISTREAM ps;
AVIFileGetStream(pf, &ps, streamtypeVIDEO, 0);
AVISTREAMINFO strhdr = {0};
AVIStreamInfo(ps, &strhdr, sizeof(strhdr));

BITMAPINFO *bi_in, bi_out;
long biSize = sizeof(bi_out);
AVIStreamReadFormat(ps, 0, 0, &biSize);
bi_in = (BITMAPINFO*)malloc(biSize+sizeof(bi_out.bmiColors));
bi_in->bmiHeader.biSize = biSize;
AVIStreamReadFormat(ps, 0, bi_in, &biSize);
bi_out = *bi_in;

HIC hic = 0;
// Check if the data is already in a recognised format (i.e. Uncompressed RGB or YUY2)
if(bi_in->bmiHeader.biCompression != 0 && bi_in->bmiHeader.biCompression != mmioFOURCC(''Y'',''U'',''Y'',''2'')){
	hic = ICDecompressOpen(''CDIV'', 0, &bi_in->bmiHeader, 0);

	biSize = ICDecompressGetFormat(hic, bi_in, &bi_out);
	
	// Check if the codec decompresses to YUY2 by default
	switch(bi_out.bmiHeader.biCompression){
		case mmioFOURCC(''Y'',''U'',''Y'',''2''):
			bi_out.bmiHeader.biBitCount = 16;
			bi_out.bmiHeader.biSizeImage = 
				bi_out.bmiHeader.biWidth*bi_out.bmiHeader.biHeight*bi_out.bmiHeader.biBitCount/8;
			break;
		case 0:
			bi_out.bmiHeader.biBitCount = 24;
			bi_out.bmiHeader.biSizeImage =
				bi_out.bmiHeader.biWidth*bi_out.bmiHeader.biHeight*bi_out.bmiHeader.biBitCount/8;
			break;
		default:
			// Check if the codec is able to decompress to this format
			bi_out.bmiHeader.biCompression = mmioFOURCC(''Y'',''U'',''Y'',''2'');
			bi_out.bmiHeader.biBitCount = 16;
			bi_out.bmiHeader.biSizeImage =
				bi_out.bmiHeader.biWidth*bi_out.bmiHeader.biHeight*bi_out.bmiHeader.biBitCount/8;
			if(ICDecompressQuery(hic, &bi_in, &bi_out) != ICERR_OK)
				return 0;
	}
	ICDecompressBegin(hic,bi_in, &bi_out);
}

int in_buf_siz  = strhdr.dwSuggestedBufferSize;
int out_buf_siz = bi_out.bmiHeader.biSizeImage;
void *inbuf, *outbuf;
if(hic)
	inbuf = malloc(max(out_buf_siz,in_buf_siz));

// This is the buffer the pixels are going to be in
unsigned char *AVIbuffer = (unsigned char *)malloc(out_buf_siz);
if(hic)
	outbuf = AVIbuffer;
else
	inbuf  = AVIbuffer;

long size = 0;
AVIStreamRead(ps, frame-1, 1, inbuf, in_buf_siz, &size, 0);

// Decompress the data
if(hic){
	bi_in->bmiHeader.biSizeImage = size;
	ICDecompress(hic, 0, &bi_in->bmiHeader, inbuf, &bi_out.bmiHeader, outbuf);
}

// At this point, I access AVIbuffer to find the pixel values of the current frame (be it in RGB or YUY2).



该代码成功到达末尾,并且在任何地方都没有出错,但是像素数据不正确.有时会有一个很好的帧(例如每300个一次),并且中间的帧大部分是空的(灰色,对比度很低,偶有像素数据的片段).
我尝试解压缩的AVI文件具有DIV3,DX50和XVID的4CC.我的PC上确实安装了正确的编解码器; Windows Media Player可以正确读取AVI.

任何有关更改内容的提示都将受到高度赞赏.
在此先感谢您!



The code succesfully reaches the end and doesn''t give errors anywhere, but the pixel data isn''t right. Occasionaly there is a good frame (like once every 300) and inbetween the frames are mostly empty (grey, with very low contrast, occasionaly with fragments of the pixel data).
The AVI files I have tried to decompress have 4CC''s of DIV3, DX50 and XVID. I do have the correct codecs installed on my PC; Windows Media Player can read the AVI correctly.

Any tips on what to change would be highly appreciated.
Thanks in advance!

推荐答案

我没有看您的代码,但是从您的描述来看,我认为我知道问题所在.您可能正在正确解码关键帧.其他帧可能是偏移图像,当与前一帧组合时,会生成结果帧.不同的格式对于偏移图像有不同的策略...有些偏移格式是交替进行的,例如,每个帧都相对于之前的帧2偏移.您正在使用的文件格式可能会存储此策略,或者在描述该格式的文档中描述了所使用的策略.
I didn''t look at your code, but from what you describe I think I know the problem. You are probably decoding the keyframes correctly. The other frames are likely offset images that, when combined with the preceding frame, produce the result frame. Different formats have different strategies for the offset images... some do an alternating offset with each frame is offset from the frame 2 before it (for example). The file format you are working with probably stores this strategy, or the strategy used is described in the document describing the format.


这篇关于AVI减压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆