mpeg2 decoder - Input buffer question

mpeg2 decoder - Input buffer question

Hi, I want to reopen an issue regarding buffer handling for DataIn object in the GetFrame function.

This is the old thread about it ( unfortunately, there was no working solution for it.

The main problem: How do I handle the input buffers. I see that decoder needs at least 1 frame, and that's fine. But when I try to call the 2nd time with less than a frame, I don't get a fully decoded picture.

Below is the code I am using (the sample code from the above thread). I can decode only the first BSIZE bytes from the bitstream. No more...

Any ideas?



#define vidW 480
#define vidH 576
#define BSIZE 150000
#define MIN_SIZE 10000
UMC::VideoDecoderParams VDecParams;
UMC::MediaData dataIn;
UMC::VideoData dataOut;
UMC::Status umcRes = UMC::UMC_OK;
FILE* fp=fopen("input.m2v","rb");
unsigned char * memBuf=(unsigned char*)malloc(5*1024*1024);
int br; //Read file to memory
br = fread(memBuf,5000000,1,fp);

MPEG2VideoDecoder* mpg2d=new UMC::MPEG2VideoDecoder();
VDecParams.m_pData = &dataIn;
VDecParams.lFlags = 0;//UMC::FLAG_VDEC_REORDER;
umcRes = mpg2d->Init(&VDecParams);
UMC::VideoDecoderParams vParams;
umcRes = mpg2d->GetInfo(&vParams);

umcRes = dataOut.Init(,,
int cc=0;
while (cc<150)
umcRes = mpg2d->GetFrame(&dataIn, &dataOut);
if (umcRes!=UMC::UMC_OK && umcRes!=UMC::UMC_ERR_NOT_ENOUGH_DATA)
if (umcRes==0)
p; static int ctr = 0;
printf("got frame: %d
char fname[256];
FILE* fp_out = fopen(fname,"wb");
if(! fp_out)
printf("Can't open file: %s
Ipp8u* y = (Ipp8u*)dataOut.GetPlanePointer(0);
Ipp8u* u = (Ipp8u*)dataOut.GetPlanePointer(1);
Ipp8u* v = (Ipp8u*)dataOut.GetPlanePointer(2);
cc++; //set to next data in buffer, just for testing
int datasize = dataIn.GetDataSize();
if(datasize {
Ipp8u* dataptr = (Ipp8u*)dataIn.GetDataPointer();
ippsCopy_8u(dataptr, memBuf, datasize); // copy tail to the head of buffer
br=fread(memBuf+datasize, BUF_SIZE-datasize, 1, fp); // read to remained space
if(br==0 && datasize < 16) break; // need more accurate EOF detection
dataIn.MoveDataPointer(memBuf - dataptr); // back to the start of the buffer
dataIn.SetDataSize(datasize + br); // remained + new read
bsp; } // end of while
delete mpg2d;

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Chgange first file-reading line from

br = fread(memBuf,5000000,1,fp);


br = fread(memBuf, BUF_SIZE,1,fp);


Thanks, ofcourse you were rightbut still, I amfacing the same problem.

Does this code run for you?


1. Make BSIZE and MIN_SIZE bigger. MIN_SIZE in your code in fact is maximum size of the encoded frame which can be in the stream.

2. While you don't guarantee that complete encoded frame is provided to decoder it makes sense to add 3-4 zero bytes in the end of the valid data. This will stop VLC decoding from out of the buffer.

If decoder decoded only part of the frame it returns UMC_OK.You can'tknow easily if it was complete frame. But you can check if next bytes are start codes higher than slice level.

Anyway, most safe and correct way is to scan for the next picture level start code.

Leave a Comment

Please sign in to add a comment. Not a member? Join today