⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpeg_sc.cpp

📁 H.264完整的C语言代码和DCT的代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
#ifdef _MSC_VER
extern "C"
{
#endif
#include "global.h"
#ifdef _MSC_VER
}
#endif
void(*idctFunc)(short*) = &idct;

#include "putstream.h"
#include "common.hpp"

#include "vector.hpp"
#include <math.h>
#include <stdlib.h>

#include "idb_streamc.hpp"
#include "mpeg.hpp"

bool byrow  = true;
bool allref = false;

bool icref  = !byrow && (allref          || false);
bool pcref  = !byrow && (allref          || false);
bool dref   = !byrow && (allref          || false);
bool qref   = !byrow && (allref ||  dref || false);
bool idref  = !byrow && (allref          || false);
bool iqref  = !byrow && (allref || idref || false);
bool bsref  = !byrow && (allref          || false);
bool mvref  = false; // mv2idxref is broken...

bool dumpfiles = false;
bool isimdumpfiles = false;
int  srf_strip_size = 8;

#ifdef _MSC_VER
extern "C" void readparmfile _ANSI_ARGS_((char *fname));
#else
void readparmfile _ANSI_ARGS_((char *fname));
#endif

//void encode_stream(StreamSchedulerInterface& scd,
//                   im_stream<im_half2> run_levels);
void dctref(StreamSchedulerInterface& scd,
            im_stream<im_half2> datain, im_stream<im_half2> out);
void idctref(StreamSchedulerInterface& scd,
             im_stream<im_half2> datain, im_stream<im_half2> out);
void quantref(StreamSchedulerInterface& scd, 
              im_stream<im_half2> datain, im_stream<im_half2> out,
              unsigned int quant_scale, bool intra);
void iquantref(StreamSchedulerInterface& scd, 
               im_stream<im_half2> datain, im_stream<im_half2> out,
               unsigned int quant_scale, bool intra);
void icolorref(StreamSchedulerInterface& scd, 
               int fnum, im_stream<im_half2> Yout, im_stream<im_half2> CrCbout);
void pcolorref(StreamSchedulerInterface& scd, 
               int fnum, im_stream<im_ubyte4> Yout, im_stream<im_half2> CrCbout);
void blocksearchref(StreamSchedulerInterface& scd, 
                    im_stream<im_ubyte4> curr, im_stream<im_ubyte4> ref,
                   im_stream<im_half2> motion);
void mv2idxref(StreamSchedulerInterface& scd, 
               im_stream<im_half2> motion, im_stream<im_uint> yindices,
               im_stream<im_uint> crcbindices, int rowlen);

/* null intra quantization matrix */
unsigned char null_quantizer_matrix[64] =
{
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
};

void diff_test();
void pcolor_test();
void mv2idx_test();
void corr_test();
void pframe_test();
void blocksearch_test( );
void idct_test();
void encode_rle(StreamSchedulerInterface& scd);

/* null intra quantization matrix */
unsigned char default_non_intra_quantizer_matrix[64] =
{
  16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
  16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
  16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
  16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16
};

// round *up* to nearest y
inline int mpeg_rtny(int x, int y)
{
  int z = ((x / y) + (((x % y) == 0) ? 0 : 1)) * y;
  return (z);
}

STREAMPROG(mpeg_macro)

void IFrame (STREAM_SCHEDULER,
             bool byRow, 
             int num_MB, 
             int mb_width, int mb_height,
             int lasttype,
             int lastcoltype,
             im_uc<im_int>& image_size_param,
             int fa,
             int ilen,
             int iwidth,
             im_stream<im_ubyte4>& image,
             im_stream<im_half2>& run_levels,
             im_stream<im_ubyte4>& Yref,
             im_stream<im_half2>& CrCbref,
             im_stream<im_half2>& Y2rle,
             im_stream<im_half2>& CrCb2rle,
             im_stream<im_uhalf2>& dct_intra_consts,
             im_stream<im_uhalf2>& idct_intra_consts,
             im_stream<im_int>& rle_consts,
             im_uc<im_uhalf2>& quantizer_scale,
             im_uc<im_uint>& pframe,
             im_uc<im_uint>& qs,
             im_uc<im_half2>& iquantizer_scale,
             char* outfilename,
             int i,
             im_stream<im_ubyte4>& Input,
             int n,
             unsigned int quant_scale
             )
{
  im_stream<im_half2> NAMED(Y1) = newStreamData<im_half2>(num_MB*4*64/2); 
  im_stream<im_half2> NAMED(CrCb1) = newStreamData<im_half2>(num_MB*2*64/2); 
  im_stream<im_half2> NAMED(Y2)    = newStreamData<im_half2>(num_MB*4*64/2); 
  im_stream<im_half2> NAMED(CrCb2) = newStreamData<im_half2>(num_MB*2*64/2); 
  im_stream<im_half2> NAMED(Y3)    = newStreamData<im_half2>(num_MB*4*64/2); 
  im_stream<im_half2> NAMED(CrCb3) = newStreamData<im_half2>(num_MB*2*64/2); 
  im_stream<im_half2> NAMED(Y1dct)    = newStreamData<im_half2>(num_MB*4*64/2); 
  im_stream<im_half2> NAMED(CrCb1dct) = newStreamData<im_half2>(num_MB*2*64/2); 

  im_stream<im_uint> NAMED(offsets) = newStreamData<im_uint>(6*64/2); 
  im_stream<im_uint> NAMED(Yidx)    = newStreamData<im_uint>(num_MB*16*16/4); 
  im_stream<im_uint> NAMED(CrCbidx) = newStreamData<im_uint>(num_MB*2*8*8/2); 

  im_stream<im_ubyte4> NAMED(Y1blk)   = newStreamData<im_ubyte4>(num_MB*16*16/4); 
  im_stream<im_half2> NAMED(CrCb1blk) = newStreamData<im_half2>(num_MB*8*8/4); 

  im_stream<im_ubyte4> NAMED(YMVref)   = newStreamData<im_ubyte4>(num_MB*4*8*8/4); 
  im_stream<im_half2> NAMED(CrCbMVref) = newStreamData<im_half2>(num_MB*2*8*8/2); 
  im_stream<im_half2> NAMED(Ydiff)     = newStreamData<im_half2>(num_MB*4*64/2); 
  im_stream<im_half2> NAMED(CrCbdiff)  = newStreamData<im_half2>(num_MB*2*64/2); 

  im_stream<im_half2>  NAMED(rowMV) = newStreamData<im_half2>(mpeg_rtny(mb_width*2, 8)); 
  im_stream<im_ubyte4> NAMED(row0) = newStreamData<im_ubyte4>(mb_width*16*16/4); 
  im_stream<im_ubyte4> NAMED(row1) = newStreamData<im_ubyte4>(mb_width*16*16/4); 
  im_stream<im_ubyte4> NAMED(row2) = newStreamData<im_ubyte4>(mb_width*16*16/4); 
  im_stream<im_ubyte4> NAMED(mblks) = newStreamData<im_ubyte4>(mb_width*16*16/4);

  im_stream<im_int> NAMED(idx_gen_str);

  im_stream<im_half2> NAMED(zerolen_str) = newStreamData<im_half2>(0);

  if (byrow)
  {
    int run_levels_pos = 0;
    int last_idx_type = -1;

    im_stream<im_half2>  NAMED(rowRunLevels);

    for (int row = 0; row < mb_height; row++)
    // for_FIXED (int row = 0; row < mb_height; row++)
    {
      // loopIter(row == 0 || row < mb_height - 1, (row % 2) == 1);

      int idx_type = (row == mb_height - 1 ? lasttype : lastcoltype);
      if (last_idx_type == -1 || last_idx_type != idx_type) {
        im_uc<im_int> idx_params = (idx_type << 16) + mb_width;
        idx_gen_str = newStreamData<im_int>(mb_width*16*16);
        idxGen(idx_gen_str, image_size_param, idx_params);
        last_idx_type = idx_type;
      }

      fa = row*16*iwidth;
      im_stream<im_ubyte4> NAMED(rowInput) = 
        newStreamData<im_ubyte4>(mb_width*16*16);
      im_stream<im_half2>  NAMED(rowY1dct) = 
        Y1dct(row*(mb_width*4*64/2), (row+1)*(mb_width*4*64/2));
      im_stream<im_half2>  NAMED(rowCrCb1dct) = 
        CrCb1dct(row*(mb_width*2*64/2), (row+1)*(mb_width*2*64/2));
      im_stream<im_half2>  NAMED(rowY2rle) = 
        Y2rle(row*(mb_width*4*64/2), (row+1)*(mb_width*4*64/2));
      im_stream<im_half2>  NAMED(rowCrCb2rle) = 
        CrCb2rle(row*(mb_width*2*64/2), (row+1)*(mb_width*2*64/2));
      im_stream<im_half2>  NAMED(rowY3) = 
        Y3(row*(mb_width*4*64/2), (row+1)*(mb_width*4*64/2));
      im_stream<im_half2>  NAMED(rowCrCb3) = 
        CrCb3(row*(mb_width*2*64/2), (row+1)*(mb_width*2*64/2));
      im_stream<im_ubyte4> NAMED(rowYref) = 
        Yref(row*(mb_width*4*8*8/4), (row+1)*(mb_width*4*8*8/4));
      im_stream<im_half2>  NAMED(rowCrCbref) = 
        CrCbref(row*(mb_width*2*8*8/2), (row+1)*(mb_width*2*8*8/2));
      // dummy streams
      im_stream<im_ubyte4> NAMED(rowYMVref) = 
        YMVref(row*(mb_width*4*8*8/4), (row+1)*(mb_width*4*8*8/4));
      im_stream<im_half2>  NAMED(rowCrCbMVref) = 
        CrCbMVref(row*(mb_width*2*8*8/2), (row+1)*(mb_width*2*8*8/2));

      streamCopy(image(fa, ilen, im_var_pos, im_acc_index, idx_gen_str),
        rowInput);

      icolor(rowInput, rowY1dct, rowCrCb1dct);

      dct(rowY1dct, dct_intra_consts, rowY2rle, quantizer_scale);
      dct(rowCrCb1dct, dct_intra_consts, rowCrCb2rle, quantizer_scale);

      // Keep track of current position in run_levels so that we
      // can append to the stream correctly - hopefully Peter will
      // make this work nicer someday.
      if (row != 0) {
        run_levels_pos += rowRunLevels.getLength();
      }
      rowRunLevels =
        run_levels(run_levels_pos, run_levels.getLength(), 
        im_countup | im_var_pos | im_var_size);
      rle(rowY2rle, rowCrCb2rle, rle_consts, zerolen_str, rowRunLevels,
          pframe, qs);

      // save run levels to memory as output frame

      idct(rowY2rle, idct_intra_consts, rowY3, iquantizer_scale);
      idct(rowCrCb2rle, idct_intra_consts, rowCrCb3, iquantizer_scale);

      correlate(rowY3, rowCrCb3, rowYMVref, rowCrCbMVref, 
                rowYref, rowCrCbref, pframe);

      // save Yref and CrCbref to memory as new reference frame
    }
  }
  else
  {

    if (dumpfiles)
    {
      sprintf(outfilename, "mpeg_sc/iinput%d.txt", i);
      streamSaveFile(outfilename, "txt", "x4", Input);
    }

    // COLOR
    if (!icref) {
      icolor(Input, Y1dct, CrCb1dct);
    } else {
      icolorref(scd, n, Y1dct, CrCb1dct);
    }

    if (dumpfiles)
    {
      sprintf(outfilename, "mpeg_sc/yicolor%d.txt", i);
      streamSaveFile(outfilename, "txt", "x2", Y1dct);
      sprintf(outfilename, "mpeg_sc/cicolor%d.txt", i);
      streamSaveFile(outfilename, "txt", "x2", CrCb1dct);
    }

    // DCT
    if (!dref) {
      dct(Y1dct, dct_intra_consts, qref ? Y2:Y2rle, quantizer_scale);
      dct(CrCb1dct, dct_intra_consts, qref ? CrCb2:CrCb2rle,
          quantizer_scale);
    } else {
      dctref(scd, Y1dct, Y2);
      dctref(scd, CrCb1dct, CrCb2);
    }
    if (qref) {
      quantref(scd, Y2, Y2rle, quant_scale, true);
      quantref(scd, CrCb2, CrCb2rle, quant_scale, true);
    }

    if (dumpfiles)
    {
      sprintf(outfilename, "mpeg_sc/yidct%d.txt", i);
      streamSaveFile(outfilename, "txt", "x2", Y2rle);
      sprintf(outfilename, "mpeg_sc/cidct%d.txt", i);
      streamSaveFile(outfilename, "txt", "x2", CrCb2rle);
    }

    // IDCT
    if (iqref) {
      iquantref(scd, Y2rle, Y2, quant_scale, true);
      iquantref(scd, CrCb2rle, CrCb2, quant_scale, true);
    }
    if (!idref) {
      idct(iqref ? Y2:Y2rle, idct_intra_consts, Y3, iquantizer_scale);
      idct(iqref ? CrCb2:CrCb2rle, idct_intra_consts,CrCb3,iquantizer_scale);
    } else {
      idctref(scd, Y2, Y3);
      idctref(scd, CrCb2, CrCb3);
    }

    if (dumpfiles)
    {
      sprintf(outfilename, "mpeg_sc/yiidct%d.txt", i);
      streamSaveFile(outfilename, "txt", "x2", Y3);
      sprintf(outfilename, "mpeg_sc/ciidct%d.txt", i);
      streamSaveFile(outfilename, "txt", "x2", CrCb3);
    }

    // Correlate - dummy ref im_stream
    correlate(Y3, CrCb3, YMVref, CrCbMVref, Yref, CrCbref, pframe);

    if (dumpfiles)
    {
      sprintf(outfilename, "mpeg_sc/yiref%d.txt", i);
      streamSaveFile(outfilename, "txt", "x4", Yref);
      sprintf(outfilename, "mpeg_sc/ciref%d.txt", i);
      streamSaveFile(outfilename, "txt", "x2", CrCbref);
    }
  }
}


void PFrame (STREAM_SCHEDULER,
             bool byRow, 
             int num_MB, 
             int mb_width, int mb_height,
             int lasttype,
             int lastcoltype,
             im_uc<im_int>& image_size_param,
             int fa,
             int ilen,
             int iwidth,
             im_stream<im_ubyte4>& image,
             im_stream<im_half2>& run_levels,
             im_stream<im_ubyte4>& Yref,
             im_stream<im_half2>& CrCbref,
             im_stream<im_ubyte4>& Yref2,
             im_stream<im_half2>& CrCbref2,
             im_stream<im_half2>& Y2rle,
             im_stream<im_half2>& CrCb2rle,
             im_stream<im_half2>& MV,
             im_stream<im_uhalf2>& dct_non_intra_consts,
             im_stream<im_uhalf2>& idct_non_intra_consts,
             im_stream<im_int>& rle_consts,
             im_uc<im_uhalf2>& quantizer_scale,
             im_uc<im_uint>& pframe,
             im_uc<im_uint>& qs,
             im_uc<im_half2>& iquantizer_scale,
             char* outfilename,
             int i,
             im_stream<im_ubyte4>& Input,
             int n,
             unsigned int quant_scale)
{
    im_stream<im_half2> NAMED(Y1) = newStreamData<im_half2>(num_MB*4*64/2); 
    im_stream<im_half2> NAMED(CrCb1) = newStreamData<im_half2>(num_MB*2*64/2); 
    im_stream<im_half2> NAMED(Y2)    = newStreamData<im_half2>(num_MB*4*64/2); 
    im_stream<im_half2> NAMED(CrCb2) = newStreamData<im_half2>(num_MB*2*64/2); 
    im_stream<im_half2> NAMED(Y3)    = newStreamData<im_half2>(num_MB*4*64/2); 
    im_stream<im_half2> NAMED(CrCb3) = newStreamData<im_half2>(num_MB*2*64/2); 
    im_stream<im_half2> NAMED(Y1dct)    = newStreamData<im_half2>(num_MB*4*64/2); 
    im_stream<im_half2> NAMED(CrCb1dct) = newStreamData<im_half2>(num_MB*2*64/2); 

    im_stream<im_uint> NAMED(offsets) = newStreamData<im_uint>(6*64/2); 
    im_stream<im_uint> NAMED(Yidx)    = newStreamData<im_uint>(num_MB*16*16/4); 
    im_stream<im_uint> NAMED(CrCbidx) = newStreamData<im_uint>(num_MB*2*8*8/2); 

    im_stream<im_ubyte4> NAMED(Y1blk)   = newStreamData<im_ubyte4>(num_MB*16*16/4); 
    im_stream<im_half2> NAMED(CrCb1blk) = newStreamData<im_half2>(num_MB*8*8/4); 

    im_stream<im_ubyte4> NAMED(YMVref)   = newStreamData<im_ubyte4>(num_MB*4*8*8/4); 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -