📄 mpeg_sc.cpp
字号:
sprintf(tplorg2, "mpeg_sc/%s", tplorg);
strcpy(tplorg, tplorg2);
char *framename = (String(tplorg) + String(".ppm")).char_ptr();
int iwidth = horizontal_size;
int iheight = vertical_size;
mpeg_init();
if (encode_isim)
{
encode_rle(scd);
return;
}
int n, i, j, row, col;
im_stream<im_int> NAMED(rle_consts) = newStreamData<im_int>(128);
streamLoadFile("mpeg_sc/rle_consts.txt", "txt", "", rle_consts);
// generate index streams
vector<unsigned int> rgb_Idx_v(256, -1);
vector<unsigned int> rgb_Idx_vx(256, -1);
vector<unsigned int> rgb_Idx_vy(256, -1);
vector<unsigned int> rgb_Idx_vxy(256, -1);
im_stream<im_uint> NAMED(rgb_Idx) = newStreamData<im_uint>(256, false);
im_stream<im_uint> NAMED(rgb_Idx_ext_x) = newStreamData<im_uint>(256, false);
im_stream<im_uint> NAMED(rgb_Idx_ext_y) = newStreamData<im_uint>(256, false);
im_stream<im_uint> NAMED(rgb_Idx_ext_xy) = newStreamData<im_uint>(256, false);
int xx = (iwidth%16 > 0) ? iwidth%16 : 16;
int yy = (iheight%16 > 0) ? iheight%16 : 16;
for (i = 0; i < yy; i++) {
for (j = 0; j < xx; j++) {
rgb_Idx_v[i*16+j] = i*iwidth+j;
rgb_Idx_vx[i*16+j] = i*iwidth+j;
rgb_Idx_vy[i*16+j] = i*iwidth+j;
rgb_Idx_vxy[i*16+j] = i*iwidth+j;
}
for (j = xx; j < 16; j++) {
rgb_Idx_v[i*16+j] = i*iwidth+j;
rgb_Idx_vx[i*16+j] = i*iwidth+xx-1;
rgb_Idx_vy[i*16+j] = i*iwidth+j;
rgb_Idx_vxy[i*16+j] = i*iwidth+xx-1;
}
}
for (i = yy; i < 16; i++) {
for (j = 0; j < xx; j++) {
rgb_Idx_v[i*16+j] = i*iwidth+j;
rgb_Idx_vx[i*16+j] = i*iwidth+j;
rgb_Idx_vy[i*16+j] = (yy-1)*iwidth+j;
rgb_Idx_vxy[i*16+j] = (yy-1)*iwidth+j;
}
for (j = xx; j < 16; j++) {
rgb_Idx_v[i*16+j] = i*iwidth+j;
rgb_Idx_vx[i*16+j] = i*iwidth+xx-1;
rgb_Idx_vy[i*16+j] = (yy-1)*iwidth+j;
rgb_Idx_vxy[i*16+j] = (yy-1)*iwidth+xx-1;
}
}
streamLoadVect(rgb_Idx_v, rgb_Idx);
streamLoadVect(rgb_Idx_vx, rgb_Idx_ext_x);
streamLoadVect(rgb_Idx_vy, rgb_Idx_ext_y);
streamLoadVect(rgb_Idx_vxy, rgb_Idx_ext_xy);
// generate index file for isim
if (isimdumpfiles) {
vector<unsigned int> out_idx;
int num_mb = srf_strip_size;
for (i = 0; i < num_mb; i++) {
for (int k = 0; k < 16; k++) {
for (int l = 0; l < 16; l++) {
out_idx.push_back(i*16+k*width+l);
}
}
}
im_stream<im_uint> NAMED(oi) = newStreamData<im_uint>(256*num_mb);
streamLoadVect(out_idx, oi);
char nm[1024];
sprintf(nm, "mpeg_sc/color_indices%d.txt", mb_width);
streamSaveFile(nm, "txt", "x", oi);
}
// generate DCT consts
unsigned char *quant_mat;
unsigned int q_scale, dc_p, iq_scale, idc_p;
float DCT_K_Consts[8];
float IDCT_K_Consts[8];
DCT_K_Consts[0] = (float)(0.25 * sqrt(2.0));
for (i = 1; i < 8; i++) {
DCT_K_Consts[i] = (float)(0.25 / cos(float(i) * (2.0 * acos(0)) / 16));
}
IDCT_K_Consts[0] = (float)(0.25 * sqrt(2.0));
for (i = 1; i < 8; i++) {
IDCT_K_Consts[i] = (float)(0.5 * cos(float(i) * (2.0 * acos(0)) / 16));
}
vector<unsigned int> dct_intra_consts_v(64, -1);
vector<unsigned int> dct_non_intra_consts_v(64, -1);
vector<unsigned int> idct_intra_consts_v(64, -1);
vector<unsigned int> idct_non_intra_consts_v(64, -1);
// DCT intra constants
float tmp, tmp4;
double tmp3;
if (!qref) {
quant_mat = default_intra_quantizer_matrix; q_scale = 1; dc_p = dc_prec;
} else {
quant_mat = null_quantizer_matrix; q_scale = 1; dc_p = 3;
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
tmp = (float)((DCT_K_Consts[i]
* (1.0 / (((i == 0)&&(j==0)) ? (float)(8 >> dc_p) : 1.0))
* (8.0 / (float)(quant_mat[i+j*8])) * 32768.0));
tmp4 = (float)((modf(tmp, &tmp3) < 0.5) ? floor(tmp) : ceil(tmp));
dct_intra_consts_v[i*8+j] = int(tmp4) + (int(tmp4) << 16);
}
}
im_stream<im_uhalf2> NAMED(dct_intra_consts) = newStreamData<im_uhalf2>(64);
streamLoadVect(dct_intra_consts_v, dct_intra_consts);
if (isimdumpfiles)
{
char *nm = "mpeg_sc/dct_intra_consts.txt";
streamSaveFile(nm, "txt", "x2", dct_intra_consts);
}
// IDCT intra constants
if (!iqref) {
quant_mat = default_intra_quantizer_matrix; iq_scale = q_scale;
idc_p = dc_prec;
} else {
quant_mat = null_quantizer_matrix; iq_scale = 1;
idc_p = 3;
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
tmp = (float)(((((i == 0)&&(j==0)) ? (float)(8 >> idc_p) : 1.0)
* ((float)(quant_mat[i+j*8]))));
tmp4 = (modf(tmp, &tmp3) < 0.5) ? (float)floor(tmp) : (float)ceil(tmp);
idct_intra_consts_v[i*8+j] = int(tmp4) + (int(tmp4) << 16);
}
}
im_stream<im_uhalf2> NAMED(idct_intra_consts) = newStreamData<im_uhalf2>(64);
streamLoadVect(idct_intra_consts_v, idct_intra_consts);
if (isimdumpfiles)
{
char *nm = "mpeg_sc/idct_intra_consts.txt";
streamSaveFile(nm, "txt", "x2", idct_intra_consts);
}
// DCT non_intra constants
if (!qref) {
quant_mat = default_non_intra_quantizer_matrix;
} else {
quant_mat = null_quantizer_matrix;
}
quant_mat = default_non_intra_quantizer_matrix;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
tmp = (float)(DCT_K_Consts[i] * (8.0 / float(quant_mat[i+j*8])) * 32768.0);
tmp4 = (float)((modf(tmp, &tmp3) < 0.5) ? floor(tmp) : ceil(tmp));
dct_non_intra_consts_v[i*8+j] = int(tmp4) + (int(tmp4) << 16);
}
}
im_stream<im_uhalf2> NAMED(dct_non_intra_consts) = newStreamData<im_uhalf2>(64);
streamLoadVect(dct_non_intra_consts_v, dct_non_intra_consts);
if (isimdumpfiles)
{
char *nm = "mpeg_sc/dct_non_intra_consts.txt";
streamSaveFile(nm, "txt", "x2", dct_non_intra_consts);
}
// IDCT non-intra constants
if (!iqref) {
quant_mat = default_non_intra_quantizer_matrix;
} else {
quant_mat = null_quantizer_matrix;
}
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
tmp = (float)(quant_mat[i*8+j]);
tmp4 = (float)((modf(tmp, &tmp3) < 0.5) ? floor(tmp) : ceil(tmp));
idct_non_intra_consts_v[i*8+j] = int(tmp4) + (int(tmp4) << 16);
}
}
im_stream<im_uhalf2> NAMED(idct_non_intra_consts) = newStreamData<im_uhalf2>(64);
streamLoadVect(idct_non_intra_consts_v, idct_non_intra_consts);
if (isimdumpfiles)
{
char *nm = "mpeg_sc/idct_non_intra_consts.txt";
streamSaveFile(nm, "txt", "x2", idct_non_intra_consts);
}
unsigned int quant_scale = q_scale;
im_uc<im_uint> qs = int(quant_scale);
tmp = (float)((1.0 / float(quant_scale)) * 65536.0);
tmp4 = (float)((modf(tmp, &tmp3) < 0.5) ? floor(tmp) : ceil(tmp));
if (tmp4 > 65535) { tmp4 = 65535; }
im_uc<im_uhalf2> quantizer_scale = int(tmp4) + (int(tmp4) << 16);
im_uc<im_half2> iquantizer_scale = int(iq_scale) + (int(iq_scale) << 16);
// declare variables
const int MaxFrames = 16;
char fname[256];
int fa, ia;
im_stream<im_ubyte4> images[MaxFrames];
im_stream<im_uint> *idx_str;
int num_MB = mb_width * mb_height;
int rl_size = mpeg_rtny(num_MB*((4+2)*(64+1)+3), 8);
im_stream<im_half2> run_levels[MaxFrames];
im_stream<im_uint> NAMED(coded_stream) = newStreamData<im_uint>(rl_size * nframes,
true);
// Initialize bitstream
initbits();
// Put the sequence header into the im_stream
putseqhdr();
if (!mpeg1)
{
putseqext();
putseqdispext();
}
/* optionally output some text data (description, copyright or whatever) */
if (strlen(id_string) > 1)
putuserdata(id_string);
// Currently does not support B frames
if (M != 1)
{
cerr << "Cannot encode B frames! 'M' must be set to 1." << endl;
exit(1);
}
char outfilename[1024];
// used for index generation
int lasttype, lastcoltype, lastrowtype;
bool evenrows = (horizontal_size == width);
bool evencols = (vertical_size == height);
if (evenrows && evencols) { lasttype = lastcoltype = lastrowtype = 0; }
else if (evenrows) { lasttype = lastrowtype = 2; lastcoltype = 0; }
else if (evencols) { lasttype = lastcoltype = 1; lastrowtype = 0; }
else { lasttype = 3; lastcoltype = 2; lastrowtype = 1; }
im_uc<im_int> image_size_param = (vertical_size<<16) + horizontal_size;
// Load the frames
int img_size = iwidth*iheight;
im_stream<im_ubyte4> all_images = newStreamData<im_ubyte4>(img_size*nframes);
im_stream<im_half2> all_run_levels = newStreamData<im_half2>(rl_size*nframes);
for (i = 0, n = frame0; i < nframes; n++, i++) {
images[i] = all_images(img_size * i, img_size * (i + 1));
sprintf(fname, framename, n);
streamLoadFile(fname, "ppm", "", images[i]);
run_levels[i] = all_run_levels(rl_size * i, rl_size * (i + 1), (byrow ? im_fixed : im_countup));
}
/*
for (i = 0, n = frame0; i < nframes; n++, i++) {
char buffer[256];
char buffer2[256];
sprintf(buffer, parm3.char_ptr(), i);
sprintf(buffer2, "mpeg_sc/test-rle%d.txt", i);
streamLoadFile(buffer2, "txt", "", run_levels[i]);
streamCompareFile(buffer, run_levels[i], 0.0, "rle");
}
return;
*/
profile("mpeg_sc/profile/mpeg_sc") {
im_stream<im_ubyte4> NAMED(Yref) = newStreamData<im_ubyte4>(num_MB*4*8*8/4);
im_stream<im_half2> NAMED(CrCbref) = newStreamData<im_half2>(num_MB*2*8*8/2);
im_stream<im_ubyte4> NAMED(Yref2) = newStreamData<im_ubyte4>(num_MB*4*8*8/4);
im_stream<im_half2> NAMED(CrCbref2) = newStreamData<im_half2>(num_MB*2*8*8/2);
im_stream<im_half2> NAMED(MV) = newStreamData<im_half2>(mpeg_rtny(num_MB*2 + 8, 8));
int pcount;
// Encode the frames (does not support B frames)
for_VARIABLE (i = 0, n = frame0; i < nframes; n++, i++) {
loopIter();
cout << "Encoding frame " << i << " of " << nframes << endl;
int f0 = N * (i / N); // I Frame reference for this frame
im_uc<im_uint> pframe = 0;
if (i == f0) // I Frame (first frame of a GOP)
{
// putgophdr(f0, i == 0); // We could probably always set "closed_GOP"
/* I frame */
pict_type = I_TYPE;
forw_hor_f_code = forw_vert_f_code = 15;
back_hor_f_code = back_vert_f_code = 15;
pict_struct = FRAME_PICTURE;
pcount = 0;
}
else if (true) // P Frame (all other frames of GOP)
{
/* P frame */
pict_type = P_TYPE;
forw_hor_f_code = motion_data[0].forw_hor_f_code;
forw_vert_f_code = motion_data[0].forw_vert_f_code;
back_hor_f_code = back_vert_f_code = 15;
pcount++;
pframe = (int)0xffffffff;
}
temp_ref = i - f0;
frame_pred_dct = frame_pred_dct_tab[pict_type-1];
q_scale_type = qscale_tab[pict_type-1];
intravlc = intravlc_tab[pict_type-1];
altscan = altscan_tab[pict_type-1];
// Put the frame header into the im_stream
// putpicthdr();
// if (!mpeg1)
// putpictcodext();
im_stream<im_ubyte4> NAMED(Input) = newStreamData<im_ubyte4>(num_MB*16*16);
// Input Data
int ilen = images[i].getLength();
if (!byrow) {
for (row = 0; row < mb_height; row++) {
for (col = 0; col < mb_width; col++) {
fa = row*16*iwidth + col*16;
ia = row*16*16*mb_width + col*16*16;
bool by = (row == mb_height-1);
bool bx = (col == mb_width-1);
idx_str = ((bx && by) ? &rgb_Idx_ext_xy :
(bx) ? &rgb_Idx_ext_x :
(by) ? &rgb_Idx_ext_y :
&rgb_Idx);
streamCopy(images[i](fa, ilen, im_var_pos, im_acc_index, *idx_str),
Input(ia, ia+16*16));
}
}
}
im_stream<im_half2> NAMED(Y2rle) = newStreamData<im_half2>(num_MB*4*64/2);
im_stream<im_half2> NAMED(CrCb2rle) = newStreamData<im_half2>(num_MB*2*64/2);
if_VARIABLE (pict_type == I_TYPE)
{
IFrame(scd, byrow, num_MB, mb_width, mb_height,
lasttype, lastcoltype, image_size_param,
fa, ilen, iwidth, images[i], run_levels[i],
Yref, CrCbref, Y2rle, CrCb2rle,
dct_intra_consts, idct_intra_consts, rle_consts,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -