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

📄 mpeg_sc.cpp

📁 H.264完整的C语言代码和DCT的代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
          quantizer_scale, pframe, qs, iquantizer_scale,
          outfilename, i, Input, n, quant_scale);
      }
      
      if_VARIABLE (pict_type == P_TYPE && pcount % 2 == 1)
      {
        PFrame(scd, byrow, num_MB, mb_width, mb_height, 
          lasttype, lastcoltype, image_size_param, 
          fa, ilen, iwidth, images[i], run_levels[i], 
          Yref, CrCbref, Yref2, CrCbref2, Y2rle, CrCb2rle, MV, 
          dct_non_intra_consts, idct_non_intra_consts, rle_consts,
          quantizer_scale, pframe, qs, iquantizer_scale, 
          outfilename, i, Input, n, quant_scale);
      }

      if_VARIABLE (pict_type == P_TYPE && pcount % 2 == 0)
      {
        PFrame(scd, byrow, num_MB, mb_width, mb_height, 
          lasttype, lastcoltype, image_size_param, 
          fa, ilen, iwidth, images[i], run_levels[i], 
          Yref2, CrCbref2, Yref, CrCbref, Y2rle, CrCb2rle, MV, 
          dct_non_intra_consts, idct_non_intra_consts, rle_consts,
          quantizer_scale, pframe, qs, iquantizer_scale, 
          outfilename, i, Input, n, quant_scale);
      }

      if (!byrow)
      {
        im_stream<im_half2> NAMED(zerolen_str) = newStreamData<im_half2>(0);

        // RLE
        rle(Y2rle, CrCb2rle, rle_consts,
            (pict_type == I_TYPE) ? zerolen_str : MV, run_levels[i], pframe, qs);

        if (dumpfiles) {
          String rlfname = String("mpeg_sc/rle") + String(n, 10) + ".txt";
          char *rn = rlfname.char_ptr();

          streamSaveFile(rn, "txt", "x2", run_levels[i]);

          String::delete_char_ptr(rn);
        }
      }
    }
  }

  if (parm2 == "rle") {

    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);
      streamSaveFile(buffer2, "txt", "x4", run_levels[i]);
      streamCompareFile(buffer, run_levels[i], 0.0, "rle");
    }

  } else {

    // Encode the frames (does not support B frames)
    for (i = 0, n = frame0; i < nframes; n++, i++) {

      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;
      }
      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;

        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();

//      encode_stream(scd, run_levels[i]);
    }


    // Put sequence end code into the im_stream
    putseqend();

    // Align to word boundary
    flushbits();
    
    // Put output into coded_stream
    streamLoadVect(getVLCvector(), coded_stream);
    //  streamSaveFile("vlc.txt", "txt", "x4", coded_stream);
    cout << "final bitstream length: " << coded_stream.getLength() << endl;

    vector<unsigned int> vlc = getVLCvector();
    unsigned int length;
    char *ptr = vlc.readBin(length);

    FILE *fd = fopen("mpeg_sc/vlc.bin", "wb");
    fwrite(ptr, sizeof(char), length, fd);
    fclose(fd);

    if (parm2 != "") {
      streamCompareFile(parm2.char_ptr(), coded_stream, 0.0, "a");
    }
  }

  String::delete_char_ptr(framename);
}




void dctref(StreamSchedulerInterface& scd, 
            im_stream<im_half2> datain, im_stream<im_half2> out)
{
  int i,j,k;
  short blk1[64], blk2[64];
  vector<unsigned int> v1(datain.getLength()), v2(datain.getLength());
  streamSaveVect(v1, datain);
  for (i = 0; i < datain.getLength() / (8*8); i++) {
    for (j = 0; j < 64; j++) {
      blk1[j] = v1[i*8*8 + j] & 0xffff;
      blk2[j] = v1[i*8*8 + j] >> 16;
    }
    fdct(blk1);
    fdct(blk2);
    for (j = 0; j < 8; j++) {
      for (k = 0; k < 8; k++) {
        v2[i*8*8 + k*8 + j] = ((((unsigned int)blk1[j*8+k]) & 0xffff)
                               + (((unsigned int)blk2[j*8+k])<<16));
      }
    }
  }
  streamLoadVect(v2, out);
}


void idctref(StreamSchedulerInterface& scd, 
             im_stream<im_half2> datain, im_stream<im_half2> out)
{
  int i,j,k;
  short blk1[64], blk2[64];
  vector<unsigned int> v1(datain.getLength()), v2(datain.getLength());
  streamSaveVect(v1, datain);
  for (i = 0; i < datain.getLength() / (8*8); i++) {
    for (j = 0; j < 64; j++) {
      blk1[j] = v1[i*8*8 + j] & 0xffff;
      blk2[j] = v1[i*8*8 + j] >> 16;
    }
    idctFunc(blk1);
    idctFunc(blk2);
    for (j = 0; j < 8; j++) {
      for (k = 0; k < 8; k++) {
        v2[i*8*8 + k*8 + j] = ((((unsigned int)blk1[j*8+k]) & 0xffff)
                               + (((unsigned int)blk2[j*8+k])<<16));
      }
    }
  }
  streamLoadVect(v2, out);
}

void quantref(StreamSchedulerInterface& scd,
              im_stream<im_half2> datain, im_stream<im_half2> out,
              unsigned int quant_scale, bool intra)
{
  int i,j;
  short blk1[64], blk1q[64], blk2[64], blk2q[64];
  vector<unsigned int> v1(datain.getLength()), v2(datain.getLength());
  streamSaveVect(v1, datain);
  unsigned int q_scale = quant_scale * 2;
  for (i = 0; i < datain.getLength() / (8*8); i++) {
    for (j = 0; j < 64; j++) {
      blk1[j] = v1[i*8*8 + j] & 0xffff;
      blk2[j] = v1[i*8*8 + j] >> 16;
    }
    if (intra) {
      quant_intra(blk1,blk1q,dc_prec, default_intra_quantizer_matrix, q_scale);
      quant_intra(blk2,blk2q,dc_prec, default_intra_quantizer_matrix, q_scale);
    } else {
      quant_non_intra(blk1, blk1q, default_non_intra_quantizer_matrix, q_scale);
      quant_non_intra(blk2, blk2q, default_non_intra_quantizer_matrix, q_scale);
    }
    for (j = 0; j < 64; j++) {
      v2[i*8*8 + j] = ((((unsigned int)blk1q[j]) & 0xffff)
                       + (((unsigned int)blk2q[j]) << 16));
    }
  }
  streamLoadVect(v2, out);
}

void iquantref(StreamSchedulerInterface& scd,
               im_stream<im_half2> datain, im_stream<im_half2> out,
               unsigned int quant_scale, bool intra)
{
  int i,j;
  short blk1[64], blk1q[64], blk2[64], blk2q[64];
  vector<unsigned int> v1(datain.getLength()), v2(datain.getLength());
  streamSaveVect(v1, datain);
  unsigned int q_scale = quant_scale * 2;
  for (i = 0; i < datain.getLength() / (8*8); i++) {
    for (j = 0; j < 64; j++) {
      blk1[j] = v1[i*8*8 + j] & 0xffff;
      blk2[j] = v1[i*8*8 + j] >> 16;
    }
    if (intra) {
      iquant_intra(blk1,blk1q,dc_prec,default_intra_quantizer_matrix, q_scale);
      iquant_intra(blk2,blk2q,dc_prec,default_intra_quantizer_matrix, q_scale);
    } else {
      iquant_non_intra(blk1, blk1q, default_non_intra_quantizer_matrix, q_scale);
      iquant_non_intra(blk2, blk2q, default_non_intra_quantizer_matrix, q_scale);
    }
    for (j = 0; j < 64; j++) {
      v2[i*8*8 + j] = ((((unsigned int)blk1q[j]) & 0xffff)
                       + (((unsigned int)blk2q[j]) << 16));
    }
  }
  streamLoadVect(v2, out);
}

void icolorref(StreamSchedulerInterface& scd,
               int fnum, im_stream<im_half2> Yout, im_stream<im_half2> CrCbout)
{
  char fname[256];
  sprintf(fname, tplorg, fnum);
  readframe(fname, neworgframe);
  vector<unsigned int> vy(mb_height*mb_width*4*8*8/2);
  vector<unsigned int> vc(mb_height*mb_width*2*8*8/2);
  for (int i = 0; i < mb_height; i++) {
    for (int j = 0; j < mb_width; j++) {
      for (int k = 0; k < 8; k++) {
        for (int l = 0; l < 8; l++) {
          int ay1 = i*mb_width*16*16 + j*16 + k*mb_width*16 + l;
          int avy1 = i*mb_width*8*8*2 + j*8*8*2 + k*8 + l;
          int ay2 = ay1 + mb_width*16*8;
          int avy2 = avy1 + 64;
          vy[avy1] = ((neworgframe[0][ay1] & 0xffff) +
                      (neworgframe[0][ay1+8] << 16));
          vy[avy2] = ((neworgframe[0][ay2] & 0xffff) +
                      (neworgframe[0][ay2+8] << 16));

          int ac = i*mb_width*8*8 + j*8 + k*mb_width*8 + l;
          int avc = i*mb_width*8*8 + j*8*8 + k*8 + l;
          vc[avc] = ((neworgframe[1][ac] & 0xffff) +
                     (neworgframe[2][ac] << 16));
        }
      }
    }
  }
  streamLoadVect(vy, Yout);
  streamLoadVect(vc, CrCbout);
}

void pcolorref(StreamSchedulerInterface& scd,
               int fnum, im_stream<im_ubyte4> Yout, im_stream<im_half2> CrCbout)
{
  char fname[256];
  sprintf(fname, tplorg, fnum);
  readframe(fname, neworgframe);
  vector<unsigned int> vy(mb_height*mb_width*16*16/4);
  vector<unsigned int> vc(mb_height*mb_width*2*8*8/2);
  for (int i = 0; i < mb_height; i++) {
    for (int j = 0; j < mb_width; j++) {
      for (int k = 0; k < 4; k++) {
        for (int l = 0; l < 16; l++) {
          int ay = i*mb_width*16*16 + j*16 + k*4 + l*mb_width*16;
          int avy = i*mb_width*4*8*2 + j*4*8*2 + k*16 + l;
          vy[avy] = (((neworgframe[0][ay] & 0xff) << 24 ) +
                     ((neworgframe[0][ay+1] & 0xff) << 16) +
                     ((neworgframe[0][ay+2] & 0xff) << 8) +
                     ((neworgframe[0][ay+3] & 0xff) << 0));
        }
      }
      for (int k = 0; k < 8; k++) {
        for (int l = 0; l < 8; l++) {
          int ac = i*mb_width*8*8 + j*8 + k*mb_width*8 + l;
          int avc = i*mb_width*8*8 + j*8*8 + k*8 + l;
          vc[avc] = ((neworgframe[1][ac] & 0xffff) +
                     (neworgframe[2][ac] << 16));
        }
      }
    }
  }
  streamLoadVect(vy, Yout);
  streamLoadVect(vc, CrCbout);
}

void blocksearchref(StreamSchedulerInterface& scd,
                    im_stream<im_ubyte4> curr,      
                    im_stream<im_ubyte4> ref,     
                    im_stream<im_half2> motion)
{
  int i;
  vector<unsigned int> v(2 * curr.getLength() / 64);
  for (i = 0; i < curr.getLength() / 64; i++) {
    unsigned int dx = 4;
    unsigned int dy = 2;
    if ((i % mb_width) == (mb_width - 1))
      dx = (unsigned int) -4;
    if ((i / mb_width) == (mb_height - 1))
      dy = (unsigned int) -2;

    // Assuming dx,dy are full pixel, must shift to make 1/2 pixel
    // v[2*i] = (dy << 17) | ((dx << 1) & 0xffff);
    v[2*i] = 0;
    v[2*i+1] = 0;
  }
  streamLoadVect(v, motion);
}

int get_offset_y(int x, int y, int rowlen)
{
  int mb_width = rowlen/16;
  int i = x/16;
  int j = y/16;
  int ii = x%16;
  int jj = y%16;
  int base = j*mb_width*16*16/4;
  base += i*16*16/4;
  base += (ii/4)*16 + jj;
  return(base);
}

int get_offset_c(int x, int y, int rowlen)
{
  int mb_width = rowlen/8;
  int i = x/8;
  int j = y/8;
  int ii = x%8;
  int jj = y%8;
  int base = j*mb_width*2*8*8/2;
  base += i*2*8*8/2;
  base += ii + jj*8;
  return(base);  
}

void mv2idxref(StreamSchedulerInterface& scd,
               im_stream<im_half2> motion, im_stream<im_uint> yindices,
               im_stream<im_uint> crcbindices, int rowlen)
{
  int i, k;
  vector<unsigned int> vm;
  vector<unsigned int> vyi((motion.getLength()/2)*16*16/4);
  vector<unsigned int> vci((motion.getLength()/2)*2*8*8/2);
  streamSaveVect(vm, motion);

⌨️ 快捷键说明

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