📄 decode.c
字号:
#include <assert.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include "config.h"
#include "sim.h"
#define GLOBAL 1
#include "global.h"
#include "tmndec.h"
#include "public.h"
//DENGZOUPING
int VideoFormat = 0; // was extern int VideoFormat;
///
void YUV411toRGB555();
void YUV411toRGB888();
//===================test function ============================
int DecodeOneFrame( int BitCount )
{
Destination = InternalDecoderOutputYUV;
DecodeBuf = InternalDecoderInput ;
BitofOneFrame = BitCount ;
BitRead = 0 ;
ld->incnt = 0;
ld->rdptr = ld->rdbfr + 2048;
ld->bitcnt = 0;
//deng: was UpLimit = mmin(2048 , BitofOneFrame);
getheader();
getpicture(&curr_framenum);
curr_framenum++;
tr_framenum++;
//MedianFilter();*/
switch(VideoFormat)
{
case 555: YUV411toRGB555(); break;
case 565: YUV411toRGB555(); break;
case 888: YUV411toRGB888(); break;
default: YUV411toRGB888(); break;
}
//memcpy(InternalDecoderOutputRGB,InternalDecoderOutputM,lsize*3L/2L);
return 1 ;
}
BOOL InitDecoder(unsigned char **DecoderInput,unsigned char **DecoderOutput,int PictureW , int PictureH)
{
int i, cc, size;
curr_framenum = tr_framenum = 0;
verbose = 0;
outtype = T_YUV_CONC;
ld = &base; /* select base layer context */
expand = 0;
if( (PictureW>352) || (PictureH>288) )
return FALSE ;
/* clip table */
if (!(clp=(unsigned char *)malloc(1024)))
error("malloc failed\n");
orgclp = clp ;
clp += 384;
for (i=-384; i<640; i++)
clp[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
/* MPEG-1 = TMN parameters */
matrix_coefficients = 5;
user_horizontal_size =PictureW ;//dMBC*16;
user_vertical_size =PictureH ;//dMBR*16;
if( (user_horizontal_size%16) != 0)
{
horizontal_size=( user_horizontal_size/16 + 1) *16 ;
}else{
horizontal_size =PictureW ;
}
if( (user_vertical_size%16) != 0)
{
vertical_size=( user_vertical_size/16 + 1) *16 ;
}else{
vertical_size =PictureH ;
}
dMBC = mb_width = horizontal_size/16;
dMBR = mb_height = vertical_size/16;
hsize = vertical_size * horizontal_size;
coded_picture_width = 16*mb_width;
coded_picture_height = 16*mb_height;
chrom_width = coded_picture_width>>1;
chrom_height = coded_picture_height>>1;
blk_cnt = 6;
for (cc=0; cc<3; cc++) {
if (cc==0)
size = coded_picture_width*coded_picture_height;
else
size = chrom_width*chrom_height;
if (!(refframe[cc] = (unsigned char *)malloc(size)))
error("malloc failed\n");
if (!(oldrefframe[cc] = (unsigned char *)malloc(size)))
error("malloc failed\n");
if (!(bframe[cc] = (unsigned char *)malloc(size)))
error("malloc failed\n");
}
for (cc=0; cc<3; cc++) {
if (cc==0) {
size = (coded_picture_width+64)*(coded_picture_height+64);
if (!(edgeframeorig[cc] = (unsigned char *)malloc(size)))
error("malloc failed\n");
edgeframe[cc] = edgeframeorig[cc] + (coded_picture_width+64) * 32 + 32;
}
else {
size = (chrom_width+32)*(chrom_height+32);
if (!(edgeframeorig[cc] = (unsigned char *)malloc(size)))
error("malloc failed\n");
edgeframe[cc] = edgeframeorig[cc] + (chrom_width+32) * 16 + 16;
}
}
if (expand) {
for (cc=0; cc<3; cc++) {
if (cc==0)
size = coded_picture_width*coded_picture_height*4;
else
size = chrom_width*chrom_height*4;
if (!(exnewframe[cc] = (unsigned char *)malloc(size)))
error("malloc failed\n");
}
}
if(!(hinputBuf= GlobalAlloc(GHND,hsize*3L/2L)))
error_handler("\ncannot alloc memory");
InternalDecoderInput = (char *)GlobalLock(hinputBuf);
* DecoderInput = InternalDecoderInput ;
if(!(houtputBufYUV= GlobalAlloc(GHND,hsize*3L/2L)))
error_handler("\ncannot alloc memory");
InternalDecoderOutputYUV = (char *)GlobalLock(houtputBufYUV);
if(!(houtputBufM= GlobalAlloc(GHND,hsize*3L/2L)))
error_handler("\ncannot alloc memory");
InternalDecoderOutputM = (char *)GlobalLock(houtputBufYUV);
// if(!(houtputBufRGB= GlobalAlloc(GHND,hsize*2L)))
if(!(houtputBufRGB= GlobalAlloc(GHND,hsize*3L)))
error_handler("\ncannot alloc memory");
InternalDecoderOutputRGB = (char *)GlobalLock(houtputBufRGB);
* DecoderOutput = InternalDecoderOutputRGB ;
/* Clear output file for concatenated storing */
/* IDCT */
dinit_idct();
return TRUE ;
}
void FreeDecoder(void)
{
int cc ;
if(orgclp != NULL )
{
free(orgclp) ;
orgclp = NULL;
}
for( cc = 0 ; cc < 3 ; cc ++ )
{
if(refframe[cc] != NULL )
free(refframe[cc]) ;
if(oldrefframe[cc] != NULL )
free(oldrefframe[cc]) ;
if(exnewframe[cc] != NULL )
free(exnewframe[cc] ) ;
if(edgeframeorig[cc] != NULL )
free(edgeframeorig[cc]) ;
if(bframe[cc] != NULL )
free(bframe[cc]) ;
}
if( hinputBuf != NULL )
{
GlobalUnlock(hinputBuf);
GlobalFree(hinputBuf);
hinputBuf = NULL ;
}
if( houtputBufYUV != NULL )
{
GlobalUnlock(houtputBufYUV);
GlobalFree(houtputBufYUV);
houtputBufYUV = NULL ;
}
if( houtputBufM != NULL )
{
GlobalUnlock(houtputBufM);
GlobalFree(houtputBufM);
houtputBufM = NULL ;
}
if( houtputBufRGB != NULL )
{
GlobalUnlock(houtputBufRGB);
GlobalFree(houtputBufRGB);
houtputBufRGB = NULL ;
}
}
void YUV411toRGB555()
//convert YUV411 in YUVBuffer2 to RGB555 in DecodeDest
{
WORD uvPos=0;
long yPos = 0 ;
int y,u,v,r,g,b;
int i , j ;
int DecodeImageW = horizontal_size;
int DecodeImageH = vertical_size ;
unsigned char _huge * yrec , * urec , * vrec ;
WORD _huge * DecodeDest ;
yrec = InternalDecoderOutputYUV ;
urec = InternalDecoderOutputYUV+ hsize;
vrec = urec + hsize/4L;
DecodeDest = ( WORD * ) InternalDecoderOutputRGB ;
for(i=0;i<user_vertical_size;i+=2)
{
for(j=0;j<user_horizontal_size;j+=2) //4 pixels every time
{
//1
yPos = i * DecodeImageW + j ;
uvPos = i * DecodeImageW/4 + j/2 ;
y = *(yrec+yPos);
u = urec[uvPos] - 0x7f;
v = vrec[uvPos] - 0x7f;
u = (u*1865970)>>20; //226.0/127.0
v = (v*1477914)>>20; //179.0/127.0;
r = v+y;
b = u+y;
//g = 1.706*y-0.509*r-0.194*b;
g = ((y*1788871)>>20) - ((r*533725)>>20) - ((b*203424)>>20);
if(r<0) r=0;
if(r>255) r=255;
if(g<0) g=0;
if(g>255) g=255;
if(b<0) b=0;
if(b>255) b=255;
*(DecodeDest+yPos) = (WORD)((b>>3)+((g>>3)<<5)+((r>>3)<<10));
//2
yPos ++ ;
y = *(yrec+yPos);
r = v+y;
b = u+y;
//g = 1.706*y-0.509*r-0.194*b;
g = ((y*1788871)>>20) - ((r*533725)>>20) - ((b*203424)>>20);
if(r<0) r=0;
if(r>255) r=255;
if(g<0) g=0;
if(g>255) g=255;
if(b<0) b=0;
if(b>255) b=255;
*(DecodeDest+yPos) = (WORD)((b>>3)+((g>>3)<<5)+((r>>3)<<10));
//3
yPos += DecodeImageW-1 ;
y = *(yrec+yPos);
r = v+y;
b = u+y;
//g = 1.706*y-0.509*r-0.194*b;
g = ((y*1788871)>>20) - ((r*533725)>>20) - ((b*203424)>>20);
if(r<0) r=0;
if(r>255) r=255;
if(g<0) g=0;
if(g>255) g=255;
if(b<0) b=0;
if(b>255) b=255;
*(DecodeDest+yPos) = (WORD)((b>>3)+((g>>3)<<5)+((r>>3)<<10));
//4
yPos ++ ;
y = *(yrec+yPos);
r = v+y;
b = u+y;
//g = 1.706*y-0.509*r-0.194*b;
g = ((y*1788871)>>20) - ((r*533725)>>20) - ((b*203424)>>20);
if(r<0) r=0;
if(r>255) r=255;
if(g<0) g=0;
if(g>255) g=255;
if(b<0) b=0;
if(b>255) b=255;
*(DecodeDest+yPos) = (WORD)((b>>3)+((g>>3)<<5)+((r>>3)<<10));
}
}
}
void YUV411toRGB888()
//convert YUV411 in YUVBuffer2 to RGB888 in DecodeDest
{
WORD uvPos=0;
long yPos = 0 ;
int y,u,v,r,g,b;
int i , j ;
int DecodeImageW = horizontal_size;
int DecodeImageH = vertical_size ;
unsigned char _huge * yrec , * urec , * vrec ;
BYTE _huge * DecodeDest ;
yrec = InternalDecoderOutputYUV ;
urec = InternalDecoderOutputYUV+ hsize;
vrec = urec + hsize/4L;
DecodeDest = ( BYTE * ) InternalDecoderOutputRGB ;
for(i=0;i<user_vertical_size;i+=2)
{
for(j=0;j<user_horizontal_size;j+=2) //4 pixels every time
{
//1
yPos = i * DecodeImageW + j ;
uvPos = i * DecodeImageW/4 + j/2 ;
y = *(yrec+yPos);
u = urec[uvPos] - 0x7f;
v = vrec[uvPos] - 0x7f;
u = (u*1865970)>>20; //226.0/127.0
v = (v*1477914)>>20; //179.0/127.0;
r = v+y;
b = u+y;
//g = 1.706*y-0.509*r-0.194*b;
g = ((y*1788871)>>20) - ((r*533725)>>20) - ((b*203424)>>20);
if(r<0) r=0;
if(r>255) r=255;
if(g<0) g=0;
if(g>255) g=255;
if(b<0) b=0;
if(b>255) b=255;
//*(DecodeDest+yPos) = (WORD)((b>>3)+((g>>3)<<5)+((r>>3)<<10));
*(DecodeDest+yPos*3) = r;
*(DecodeDest+yPos*3+1) = g;
*(DecodeDest+yPos*3+2) = b;
//2
yPos ++ ;
y = *(yrec+yPos);
r = v+y;
b = u+y;
//g = 1.706*y-0.509*r-0.194*b;
g = ((y*1788871)>>20) - ((r*533725)>>20) - ((b*203424)>>20);
if(r<0) r=0;
if(r>255) r=255;
if(g<0) g=0;
if(g>255) g=255;
if(b<0) b=0;
if(b>255) b=255;
//*(DecodeDest+yPos) = (WORD)((b>>3)+((g>>3)<<5)+((r>>3)<<10));
*(DecodeDest+yPos*3) = r;
*(DecodeDest+yPos*3+1) = g;
*(DecodeDest+yPos*3+2) = b;
//3
yPos += DecodeImageW-1 ;
y = *(yrec+yPos);
r = v+y;
b = u+y;
//g = 1.706*y-0.509*r-0.194*b;
g = ((y*1788871)>>20) - ((r*533725)>>20) - ((b*203424)>>20);
if(r<0) r=0;
if(r>255) r=255;
if(g<0) g=0;
if(g>255) g=255;
if(b<0) b=0;
if(b>255) b=255;
//*(DecodeDest+yPos) = (WORD)((b>>3)+((g>>3)<<5)+((r>>3)<<10));
*(DecodeDest+yPos*3) = r;
*(DecodeDest+yPos*3+1) = g;
*(DecodeDest+yPos*3+2) = b;
//4
yPos ++ ;
y = *(yrec+yPos);
r = v+y;
b = u+y;
//g = 1.706*y-0.509*r-0.194*b;
g = ((y*1788871)>>20) - ((r*533725)>>20) - ((b*203424)>>20);
if(r<0) r=0;
if(r>255) r=255;
if(g<0) g=0;
if(g>255) g=255;
if(b<0) b=0;
if(b>255) b=255;
//*(DecodeDest+yPos) = (WORD)((b>>3)+((g>>3)<<5)+((r>>3)<<10));
*(DecodeDest+yPos*3) = r;
*(DecodeDest+yPos*3+1) = g;
*(DecodeDest+yPos*3+2) = b;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -