/*
** Load block 'blk' into the cache of pFile.
*/
static int cacheBlock(OsTestFile *pFile, int blk){
  if( blk>=pFile->nBlk ){
    int n = ((pFile->nBlk * 2) + 100 + blk);
    /* if( pFile->nBlk==0 ){ printf("DIRTY %s\n", pFile->zName); } */
    pFile->apBlk = (u8 **)sqliteRealloc(pFile->apBlk, n * sizeof(u8*));
    if( !pFile->apBlk ) return SQLITE_NOMEM;
    memset(&pFile->apBlk[pFile->nBlk], 0, (n - pFile->nBlk)*sizeof(u8*));
    pFile->nBlk = n;
  }

  if( !pFile->apBlk[blk] ){
    i64 filesize;
    int rc;

    u8 *p = sqliteMalloc(BLOCKSIZE);
    if( !p ) return SQLITE_NOMEM;
    pFile->apBlk[blk] = p;

    rc = sqlite3RealFileSize(&pFile->fd, &filesize);
    if( rc!=SQLITE_OK ) return rc;

    if( BLOCK_OFFSET(blk)<filesize ){
      int len = BLOCKSIZE;
      rc = sqlite3RealSeek(&pFile->fd, blk*BLOCKSIZE);
      if( BLOCK_OFFSET(blk+1)>filesize ){
        len = filesize - BLOCK_OFFSET(blk);
      }
      if( rc!=SQLITE_OK ) return rc;
      rc = sqlite3RealRead(&pFile->fd, p, len);
      if( rc!=SQLITE_OK ) return rc;
    }
  }

  return SQLITE_OK;
}
Exemplo n.º 2
0
void vp9_xform_quant(MACROBLOCK *x, int plane, int block,
                     BLOCK_SIZE plane_bsize, TX_SIZE tx_size) {
  MACROBLOCKD *const xd = &x->e_mbd;
  const struct macroblock_plane *const p = &x->plane[plane];
  const struct macroblockd_plane *const pd = &xd->plane[plane];
  const scan_order *const scan_order = &vp9_default_scan_orders[tx_size];
  int16_t *const coeff = BLOCK_OFFSET(p->coeff, block);
  int16_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  uint16_t *const eob = &p->eobs[block];
  const int diff_stride = 4 * num_4x4_blocks_wide_lookup[plane_bsize];
  int i, j;
  const int16_t *src_diff;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
  src_diff = &p->src_diff[4 * (j * diff_stride + i)];

  switch (tx_size) {
    case TX_32X32:
      fdct32x32(x->use_lp32x32fdct, src_diff, coeff, diff_stride);
      vp9_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin, p->round,
                           p->quant, p->quant_shift, qcoeff, dqcoeff,
                           pd->dequant, p->zbin_extra, eob, scan_order->scan,
                           scan_order->iscan);
      break;
    case TX_16X16:
      vp9_fdct16x16(src_diff, coeff, diff_stride);
      vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
                     pd->dequant, p->zbin_extra, eob,
                     scan_order->scan, scan_order->iscan);
      break;
    case TX_8X8:
      vp9_fdct8x8(src_diff, coeff, diff_stride);
      vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
                     pd->dequant, p->zbin_extra, eob,
                     scan_order->scan, scan_order->iscan);
      break;
    case TX_4X4:
      x->fwd_txm4x4(src_diff, coeff, diff_stride);
      vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round,
                     p->quant, p->quant_shift, qcoeff, dqcoeff,
                     pd->dequant, p->zbin_extra, eob,
                     scan_order->scan, scan_order->iscan);
      break;
    default:
      assert(0);
  }
}
static void decode_block(int plane, int block, BLOCK_SIZE_TYPE bsize,
                         int ss_txfrm_size, void *arg) {
  MACROBLOCKD* const xd = arg;
  struct macroblockd_plane *pd = &xd->plane[plane];
  int16_t* const qcoeff = BLOCK_OFFSET(pd->qcoeff, block, 16);
  const int stride = pd->dst.stride;
  const int raster_block = txfrm_block_to_raster_block(xd, bsize, plane,
                                                       block, ss_txfrm_size);
  uint8_t* const dst = raster_block_offset_uint8(xd, bsize, plane,
                                                 raster_block,
                                                 pd->dst.buf, stride);

  TX_TYPE tx_type;

  switch (ss_txfrm_size / 2) {
    case TX_4X4:
      tx_type = plane == 0 ? get_tx_type_4x4(xd, raster_block) : DCT_DCT;
      if (tx_type == DCT_DCT)
        xd->itxm_add(qcoeff, dst, stride, pd->eobs[block]);
      else
        vp9_iht_add_c(tx_type, qcoeff, dst, stride, pd->eobs[block]);
      break;
    case TX_8X8:
      tx_type = plane == 0 ? get_tx_type_8x8(xd, raster_block) : DCT_DCT;
      vp9_iht_add_8x8_c(tx_type, qcoeff, dst, stride, pd->eobs[block]);
      break;
    case TX_16X16:
      tx_type = plane == 0 ? get_tx_type_16x16(xd, raster_block) : DCT_DCT;
      vp9_iht_add_16x16_c(tx_type, qcoeff, dst, stride, pd->eobs[block]);
      break;
    case TX_32X32:
      vp9_idct_add_32x32(qcoeff, dst, stride, pd->eobs[block]);
      break;
  }
}
Exemplo n.º 4
0
static void encode_block(int plane, int block, BLOCK_SIZE plane_bsize,
                         TX_SIZE tx_size, void *arg) {
  struct encode_b_args *const args = arg;
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
  struct optimize_ctx *const ctx = args->ctx;
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  int i, j;
  uint8_t *dst;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
  dst = &pd->dst.buf[4 * j * pd->dst.stride + 4 * i];

  // TODO(jingning): per transformed block zero forcing only enabled for
  // luma component. will integrate chroma components as well.
  if (x->zcoeff_blk[tx_size][block] && plane == 0) {
    p->eobs[block] = 0;
    ctx->ta[plane][i] = 0;
    ctx->tl[plane][j] = 0;
    return;
  }

  if (!x->skip_recode)
    vp9_xform_quant(x, plane, block, plane_bsize, tx_size);

  if (x->optimize && (!x->skip_recode || !x->skip_optimize)) {
    optimize_b(plane, block, plane_bsize, tx_size, x, ctx);
  } else {
    ctx->ta[plane][i] = p->eobs[block] > 0;
    ctx->tl[plane][j] = p->eobs[block] > 0;
  }

  if (p->eobs[block])
    *(args->skip) = 0;

  if (x->skip_encode || p->eobs[block] == 0)
    return;

  switch (tx_size) {
    case TX_32X32:
      vp9_idct32x32_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
      break;
    case TX_16X16:
      vp9_idct16x16_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
      break;
    case TX_8X8:
      vp9_idct8x8_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
      break;
    case TX_4X4:
      // this is like vp9_short_idct4x4 but has a special case around eob<=1
      // which is significant (not just an optimization) for the lossless
      // case.
      xd->itxm_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
      break;
    default:
      assert(0 && "Invalid transform size");
  }
}
int sqlite3OsWrite(OsFile *id, const void *pBuf, int amt){
  i64 offset;       /* The current offset from the start of the file */
  i64 end;          /* The byte just past the last byte written */
  int blk;            /* Block number the write starts on */
  int i;
  const u8 *zCsr;
  int rc = SQLITE_OK;
  OsTestFile *pFile = *id;

  offset = osTell(pFile);
  end = offset+amt;
  blk = (offset/BLOCKSIZE);

  zCsr = (u8 *)pBuf;
  for(i=blk; i*BLOCKSIZE<end; i++){
    u8 *pBlk;
    int off = 0;
    int len = 0;

    /* Make sure the block is in the cache */
    rc = cacheBlock(pFile, i);
    if( rc!=SQLITE_OK ) return rc;

    /* Write into the cache */
    pBlk = pFile->apBlk[i];
    assert( pBlk );

    if( BLOCK_OFFSET(i) < offset ){
      off = offset-BLOCK_OFFSET(i);
    }
    len = BLOCKSIZE - off;
    if( BLOCK_OFFSET(i+1) > end ){
      len = len - (BLOCK_OFFSET(i+1)-end);
    }
    memcpy(&pBlk[off], zCsr, len);
    zCsr += len;
  }
  if( pFile->nMaxWrite<end ){
    pFile->nMaxWrite = end;
  }
  assert( zCsr==&((u8 *)pBuf)[amt] );

  rc = sqlite3RealSeek(&pFile->fd, end);
  return rc;
}
int sqlite3OsRead(OsFile *id, void *pBuf, int amt){
  i64 offset;       /* The current offset from the start of the file */
  i64 end;          /* The byte just past the last byte read */
  int blk;            /* Block number the read starts on */
  int i;
  u8 *zCsr;
  int rc = SQLITE_OK;
  OsTestFile *pFile = *id;

  offset = osTell(pFile);
  end = offset+amt;
  blk = (offset/BLOCKSIZE);

  zCsr = (u8 *)pBuf;
  for(i=blk; i*BLOCKSIZE<end; i++){
    int off = 0;
    int len = 0;


    if( BLOCK_OFFSET(i) < offset ){
      off = offset-BLOCK_OFFSET(i);
    }
    len = BLOCKSIZE - off;
    if( BLOCK_OFFSET(i+1) > end ){
      len = len - (BLOCK_OFFSET(i+1)-end);
    }

    if( i<pFile->nBlk && pFile->apBlk[i]){
      u8 *pBlk = pFile->apBlk[i];
      memcpy(zCsr, &pBlk[off], len);
    }else{
      rc = sqlite3RealSeek(&pFile->fd, BLOCK_OFFSET(i) + off);
      if( rc!=SQLITE_OK ) return rc;
      rc = sqlite3RealRead(&pFile->fd, zCsr, len);
      if( rc!=SQLITE_OK ) return rc;
    }

    zCsr += len;
  }
  assert( zCsr==&((u8 *)pBuf)[amt] );

  rc = sqlite3RealSeek(&pFile->fd, end);
  return rc;
}
/**
 * @brief 输出文件 
 * @param[in] absolute_path 文件的绝对路径
 * @param[in] all           是否输出全部文件,包括隐藏文件,以及.和..
 * @param[in] almost_all    是否“几乎全部”输出,包括隐藏文件,不包括.和..
 * @param[in] long_list     是否按长列表方式输出
 */
void output_files( const char * absolute_path, bool all, bool almost_all, bool long_list )  // ls
{
	struct ext2_group_desc group;
	struct ext2_inode inode;
	void * block = NULL;

	int inode_no = get_file_inode_no(absolute_path);
	if ( inode_no == -1 )
	{
		fprintf(stderr, "Current directory does not exist.\n");
		return;
	}
	read_group_descriptor(&group);
	read_inode(&group, &inode, inode_no);

	if (S_ISDIR(inode.i_mode))
	{
		struct ext2_dir_entry_2 * entry = NULL;
		unsigned int size = 0;
		if ((block = malloc(block_size)) == NULL)
		{
			fprintf(stderr, "Insufficient memory.\n");
			exit(EXIT_FAILURE);
		}

		lseek(fd_ext2, BLOCK_OFFSET(inode.i_block[0]), SEEK_SET);
		read(fd_ext2, block, block_size);                // read block from disk

		entry = (struct ext2_dir_entry_2 *) block;  // first entry in the directory
		// Notice that the list may be terminated with a NULL entry (entry->inode == NULL)
		while((size < inode.i_size) && entry->inode)
		{
			if ( entry->name[0] == '.' )
			{
				if ( all )
					output_entry(entry, long_list, entry->inode);
				else if ( almost_all )
				{
					if ( strcmp(entry->name, ".") && strcmp(entry->name, "..") )
						output_entry(entry, long_list, entry->inode);
				}
			}
			else
				output_entry(entry, long_list, entry->inode);
			entry = (struct ext2_dir_entry_2 *)((void *)entry + entry->rec_len);
			size += entry->rec_len;
		}
		puts("");

		free(block);
		block = entry = NULL;
	}
	else
		fprintf(stderr, "Current directory does not exist.\n");
}
Exemplo n.º 8
0
int vp9_optimize_b(MACROBLOCK *mb, int plane, int block, TX_SIZE tx_size,
                   int ctx) {
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  const int ref = is_inter_block(xd->mi[0]);
  vp9_token_state tokens[1025][2];
  uint8_t token_cache[1024];
  const tran_low_t *const coeff = BLOCK_OFFSET(mb->plane[plane].coeff, block);
  tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  const int eob = p->eobs[block];
  const PLANE_TYPE type = get_plane_type(plane);
  const int default_eob = 16 << (tx_size << 1);
  const int shift = (tx_size == TX_32X32);
  const int16_t *const dequant_ptr = pd->dequant;
  const uint8_t *const band_translate = get_band_translate(tx_size);
  const scan_order *const so = get_scan(xd, tx_size, type, block);
  const int16_t *const scan = so->scan;
  const int16_t *const nb = so->neighbors;
  const int dq_step[2] = { dequant_ptr[0] >> shift, dequant_ptr[1] >> shift };
Exemplo n.º 9
0
int vp9_decode_block_tokens(VP9_COMMON *cm, MACROBLOCKD *xd,
                            int plane, int block, BLOCK_SIZE plane_bsize,
                            int x, int y, TX_SIZE tx_size, vp9_reader *r) {
  struct macroblockd_plane *const pd = &xd->plane[plane];
  const int ctx = get_entropy_context(tx_size, pd->above_context + x,
                                               pd->left_context + y);
  const scan_order *so = get_scan(xd, tx_size, pd->plane_type, block);
  const int eob = decode_coefs(cm, xd, pd->plane_type,
                               BLOCK_OFFSET(pd->dqcoeff, block), tx_size,
                               pd->dequant, ctx, so->scan, so->neighbors, r);
  vp9_set_contexts(xd, pd, plane_bsize, tx_size, eob > 0, x, y);
  return eob;
}
Exemplo n.º 10
0
unsigned int readAtOffset(int blockNum, int offset, int size) {
  char read_c[size + 1];
  int i;
  int block = blockNum;
  // if first time getting block size
  if (blockNum == -1)
    block = 1;
  for (i = 0; i <= size; i++) {
    pread(fd, &read_c[i], 1, BLOCK_OFFSET(block) + offset + i);
  }
  unsigned int num = *(int *)read_c;
  return num;
}
Exemplo n.º 11
0
static void encode_block_pass1(int plane, int block, BLOCK_SIZE plane_bsize,
                               TX_SIZE tx_size, void *arg) {
  MACROBLOCK *const x = (MACROBLOCK *)arg;
  MACROBLOCKD *const xd = &x->e_mbd;
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  int i, j;
  uint8_t *dst;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
  dst = &pd->dst.buf[4 * j * pd->dst.stride + 4 * i];

  vp9_xform_quant(x, plane, block, plane_bsize, tx_size);

  if (p->eobs[block] > 0)
    xd->itxm_add(dqcoeff, dst, pd->dst.stride, p->eobs[block]);
}
Exemplo n.º 12
0
static void inverse_transform_block(MACROBLOCKD* xd, int plane, int block,
                                    BLOCK_SIZE plane_bsize, TX_SIZE tx_size) {
  struct macroblockd_plane *const pd = &xd->plane[plane];
  int16_t* const qcoeff = BLOCK_OFFSET(pd->qcoeff, block);
  const int stride = pd->dst.stride;
  const int eob = pd->eobs[block];
  if (eob > 0) {
    TX_TYPE tx_type;
    const int raster_block = txfrm_block_to_raster_block(plane_bsize, tx_size,
                                                         block);
    uint8_t* const dst = raster_block_offset_uint8(plane_bsize, raster_block,
                                                   pd->dst.buf, stride);
    switch (tx_size) {
      case TX_4X4:
        tx_type = get_tx_type_4x4(pd->plane_type, xd, raster_block);
        if (tx_type == DCT_DCT)
          xd->itxm_add(qcoeff, dst, stride, eob);
        else
          vp9_iht4x4_add(tx_type, qcoeff, dst, stride, eob);
        break;
      case TX_8X8:
        tx_type = get_tx_type_8x8(pd->plane_type, xd);
        vp9_iht8x8_add(tx_type, qcoeff, dst, stride, eob);
        break;
      case TX_16X16:
        tx_type = get_tx_type_16x16(pd->plane_type, xd);
        vp9_iht16x16_add(tx_type, qcoeff, dst, stride, eob);
        break;
      case TX_32X32:
        tx_type = DCT_DCT;
        vp9_idct32x32_add(qcoeff, dst, stride, eob);
        break;
      default:
        assert(!"Invalid transform size");
    }

    if (eob == 1) {
      vpx_memset(qcoeff, 0, 2 * sizeof(qcoeff[0]));
    } else {
      if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
        vpx_memset(qcoeff, 0, 4 * (4 << tx_size) * sizeof(qcoeff[0]));
      else
        vpx_memset(qcoeff, 0, (16 << (tx_size << 1)) * sizeof(qcoeff[0]));
    }
  }
}
Exemplo n.º 13
0
int vp9_decode_block_tokens(VP9_COMMON *cm, MACROBLOCKD *xd,
                            int plane, int block, BLOCK_SIZE plane_bsize,
                            TX_SIZE tx_size, vp9_reader *r) {
  struct macroblockd_plane *const pd = &xd->plane[plane];
  const int seg_eob = get_tx_eob(&cm->seg, xd->mi_8x8[0]->mbmi.segment_id,
                                 tx_size);
  int aoff, loff, eob, pt;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);
  pt = get_entropy_context(tx_size, pd->above_context + aoff,
                                    pd->left_context + loff);

  eob = decode_coefs(cm, xd, r, block,
                     pd->plane_type, seg_eob, BLOCK_OFFSET(pd->qcoeff, block),
                     tx_size, pd->dequant, pt);

  set_contexts(xd, pd, plane_bsize, tx_size, eob > 0, aoff, loff);

  pd->eobs[block] = eob;
  return eob;
}
Exemplo n.º 14
0
int iterateOverDirectoryBlockGivenBlockNum(unsigned int block_num, unsigned int super_num_inodes, unsigned int super_block_size, unsigned int inode_num, FILE *file) {
  // traversing linked list of directory entries)
  int num_entries = 0;
  int entry_base = 0;
  if (block_num == 0)
    return num_entries;

  unsigned int entry_length;
  do {
    // check if directory is valid/allocated
    entry_length = readAtOffset(block_num, entry_base + 4, 1);
    entry_length &= 0xFFFF;
    // stop looking through directory block if no more entries
    if (entry_length <= 0)
      return num_entries;
    unsigned int parent_inode_num = readAtOffset(block_num, entry_base, 4);
    unsigned int entry_name_length = readAtOffset(block_num, entry_base + 6, 1);
    entry_name_length &= 0xFF;

    // get file name
    int char_index;
    char name[entry_name_length + 1];
    pread(fd, &name, entry_name_length, BLOCK_OFFSET(block_num) + entry_base + 8);
    name[entry_name_length] = 0;

    entry_base += entry_length;
    if (entry_base % 4 != 0) {
      entry_base += (4 - entry_base % 4);
    }
    if (parent_inode_num == 0 || parent_inode_num > super_num_inodes || strlen(name) == 0) {
      continue;
    } 
    if (file != NULL)
      fprintf(file, "%u,%u,%u,%u,%u,\"%s\"\n", inode_num, num_entries, entry_length, entry_name_length, parent_inode_num, name);
	  
    num_entries++;
  } while (entry_base < 1024);

  return num_entries;
}
Exemplo n.º 15
0
static void tokenize_b(int plane, int block, BLOCK_SIZE plane_bsize,
                       TX_SIZE tx_size, void *arg) {
  struct tokenize_b_args* const args = arg;
  VP9_COMP *cpi = args->cpi;
  MACROBLOCKD *xd = args->xd;
  TOKENEXTRA **tp = args->tp;
  uint8_t *token_cache = args->token_cache;
  struct macroblock_plane *p = &cpi->mb.plane[plane];
  struct macroblockd_plane *pd = &xd->plane[plane];
  MB_MODE_INFO *mbmi = &xd->mi_8x8[0]->mbmi;
  int pt; /* near block/prev token context index */
  int c = 0;
  TOKENEXTRA *t = *tp;        /* store tokens starting here */
  int eob = p->eobs[block];
  const PLANE_TYPE type = pd->plane_type;
  const int16_t *qcoeff_ptr = BLOCK_OFFSET(p->qcoeff, block);
  const int segment_id = mbmi->segment_id;
  const int16_t *scan, *nb;
  const scan_order *so;
  vp9_coeff_count *const counts = cpi->coef_counts[tx_size];
  vp9_coeff_probs_model *const coef_probs = cpi->common.fc.coef_probs[tx_size];
  const int ref = is_inter_block(mbmi);
  const uint8_t *const band = get_band_translate(tx_size);
  const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size);

  int aoff, loff;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);

  pt = get_entropy_context(tx_size, pd->above_context + aoff,
                           pd->left_context + loff);
  so = get_scan(xd, tx_size, type, block);
  scan = so->scan;
  nb = so->neighbors;
  c = 0;
  while (c < eob) {
    int v = 0;
    int skip_eob = 0;
    v = qcoeff_ptr[scan[c]];

    while (!v) {
      add_token(&t, coef_probs[type][ref][band[c]][pt], 0, ZERO_TOKEN, skip_eob,
                counts[type][ref][band[c]][pt]);

      cpi->common.counts.eob_branch[tx_size][type][ref][band[c]][pt] +=
          !skip_eob;

      skip_eob = 1;
      token_cache[scan[c]] = 0;
      ++c;
      pt = get_coef_context(nb, token_cache, c);
      v = qcoeff_ptr[scan[c]];
    }
    add_token(&t, coef_probs[type][ref][band[c]][pt],
              vp9_dct_value_tokens_ptr[v].extra,
              vp9_dct_value_tokens_ptr[v].token, skip_eob,
              counts[type][ref][band[c]][pt]);

    cpi->common.counts.eob_branch[tx_size][type][ref][band[c]][pt] += !skip_eob;

    token_cache[scan[c]] =
        vp9_pt_energy_class[vp9_dct_value_tokens_ptr[v].token];
    ++c;
    pt = get_coef_context(nb, token_cache, c);
  }
  if (c < seg_eob) {
    add_token(&t, coef_probs[type][ref][band[c]][pt], 0, EOB_TOKEN, 0,
              counts[type][ref][band[c]][pt]);
    ++cpi->common.counts.eob_branch[tx_size][type][ref][band[c]][pt];
  }

  *tp = t;

  set_contexts(xd, pd, plane_bsize, tx_size, c > 0, aoff, loff);
}
Exemplo n.º 16
0
static void tokenize_b(int plane, int block, BLOCK_SIZE plane_bsize,
                       TX_SIZE tx_size, void *arg) {
  struct tokenize_b_args* const args = arg;
  VP9_COMP *cpi = args->cpi;
  ThreadData *const td = args->td;
  MACROBLOCK *const x = &td->mb;
  MACROBLOCKD *const xd = &x->e_mbd;
  TOKENEXTRA **tp = args->tp;
  uint8_t token_cache[32 * 32];
  struct macroblock_plane *p = &x->plane[plane];
  struct macroblockd_plane *pd = &xd->plane[plane];
  MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
  int pt; /* near block/prev token context index */
  int c;
  TOKENEXTRA *t = *tp;        /* store tokens starting here */
  int eob = p->eobs[block];
  const PLANE_TYPE type = pd->plane_type;
  const tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  const int segment_id = mbmi->segment_id;
  const int16_t *scan, *nb;
  const scan_order *so;
  const int ref = is_inter_block(mbmi);
  unsigned int (*const counts)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
      td->rd_counts.coef_counts[tx_size][type][ref];
  vp9_prob (*const coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
      cpi->common.fc->coef_probs[tx_size][type][ref];
  unsigned int (*const eob_branch)[COEFF_CONTEXTS] =
      td->counts->eob_branch[tx_size][type][ref];
  const uint8_t *const band = get_band_translate(tx_size);
  const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size);
  int16_t token;
  EXTRABIT extra;
  int aoff, loff;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);

  pt = get_entropy_context(tx_size, pd->above_context + aoff,
                           pd->left_context + loff);
  so = get_scan(xd, tx_size, type, block);
  scan = so->scan;
  nb = so->neighbors;
  c = 0;

  while (c < eob) {
    int v = 0;
    int skip_eob = 0;
    v = qcoeff[scan[c]];

    while (!v) {
      add_token_no_extra(&t, coef_probs[band[c]][pt], ZERO_TOKEN, skip_eob,
                         counts[band[c]][pt]);
      eob_branch[band[c]][pt] += !skip_eob;

      skip_eob = 1;
      token_cache[scan[c]] = 0;
      ++c;
      pt = get_coef_context(nb, token_cache, c);
      v = qcoeff[scan[c]];
    }

    vp9_get_token_extra(v, &token, &extra);

    add_token(&t, coef_probs[band[c]][pt], extra, (uint8_t)token,
              (uint8_t)skip_eob, counts[band[c]][pt]);
    eob_branch[band[c]][pt] += !skip_eob;

    token_cache[scan[c]] = vp9_pt_energy_class[token];
    ++c;
    pt = get_coef_context(nb, token_cache, c);
  }
  if (c < seg_eob) {
    add_token_no_extra(&t, coef_probs[band[c]][pt], EOB_TOKEN, 0,
                       counts[band[c]][pt]);
    ++eob_branch[band[c]][pt];
  }

  *tp = t;

  vp9_set_contexts(xd, pd, plane_bsize, tx_size, c > 0, aoff, loff);
}
Exemplo n.º 17
0
static void encode_block_intra(int plane, int block, BLOCK_SIZE plane_bsize,
                               TX_SIZE tx_size, void *arg) {
  struct encode_b_args* const args = arg;
  MACROBLOCK *const x = args->x;
  MACROBLOCKD *const xd = &x->e_mbd;
  MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
  struct macroblock_plane *const p = &x->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  int16_t *coeff = BLOCK_OFFSET(p->coeff, block);
  int16_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  int16_t *dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  const scan_order *scan_order;
  TX_TYPE tx_type;
  PREDICTION_MODE mode;
  const int bwl = b_width_log2(plane_bsize);
  const int diff_stride = 4 * (1 << bwl);
  uint8_t *src, *dst;
  int16_t *src_diff;
  uint16_t *eob = &p->eobs[block];
  const int src_stride = p->src.stride;
  const int dst_stride = pd->dst.stride;
  int i, j;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j);
  dst = &pd->dst.buf[4 * (j * dst_stride + i)];
  src = &p->src.buf[4 * (j * src_stride + i)];
  src_diff = &p->src_diff[4 * (j * diff_stride + i)];

  switch (tx_size) {
    case TX_32X32:
      scan_order = &vp9_default_scan_orders[TX_32X32];
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
      vp9_predict_intra_block(xd, block >> 6, bwl, TX_32X32, mode,
                              x->skip_encode ? src : dst,
                              x->skip_encode ? src_stride : dst_stride,
                              dst, dst_stride, i, j, plane);
      if (!x->skip_recode) {
        vp9_subtract_block(32, 32, src_diff, diff_stride,
                           src, src_stride, dst, dst_stride);
        fdct32x32(x->use_lp32x32fdct, src_diff, coeff, diff_stride);
        vp9_quantize_b_32x32(coeff, 1024, x->skip_block, p->zbin, p->round,
                             p->quant, p->quant_shift, qcoeff, dqcoeff,
                             pd->dequant, p->zbin_extra, eob, scan_order->scan,
                             scan_order->iscan);
      }
      if (!x->skip_encode && *eob)
        vp9_idct32x32_add(dqcoeff, dst, dst_stride, *eob);
      break;
    case TX_16X16:
      tx_type = get_tx_type(pd->plane_type, xd);
      scan_order = &vp9_scan_orders[TX_16X16][tx_type];
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
      vp9_predict_intra_block(xd, block >> 4, bwl, TX_16X16, mode,
                              x->skip_encode ? src : dst,
                              x->skip_encode ? src_stride : dst_stride,
                              dst, dst_stride, i, j, plane);
      if (!x->skip_recode) {
        vp9_subtract_block(16, 16, src_diff, diff_stride,
                           src, src_stride, dst, dst_stride);
        vp9_fht16x16(src_diff, coeff, diff_stride, tx_type);
        vp9_quantize_b(coeff, 256, x->skip_block, p->zbin, p->round,
                       p->quant, p->quant_shift, qcoeff, dqcoeff,
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
      }
      if (!x->skip_encode && *eob)
        vp9_iht16x16_add(tx_type, dqcoeff, dst, dst_stride, *eob);
      break;
    case TX_8X8:
      tx_type = get_tx_type(pd->plane_type, xd);
      scan_order = &vp9_scan_orders[TX_8X8][tx_type];
      mode = plane == 0 ? mbmi->mode : mbmi->uv_mode;
      vp9_predict_intra_block(xd, block >> 2, bwl, TX_8X8, mode,
                              x->skip_encode ? src : dst,
                              x->skip_encode ? src_stride : dst_stride,
                              dst, dst_stride, i, j, plane);
      if (!x->skip_recode) {
        vp9_subtract_block(8, 8, src_diff, diff_stride,
                           src, src_stride, dst, dst_stride);
        vp9_fht8x8(src_diff, coeff, diff_stride, tx_type);
        vp9_quantize_b(coeff, 64, x->skip_block, p->zbin, p->round, p->quant,
                       p->quant_shift, qcoeff, dqcoeff,
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
      }
      if (!x->skip_encode && *eob)
        vp9_iht8x8_add(tx_type, dqcoeff, dst, dst_stride, *eob);
      break;
    case TX_4X4:
      tx_type = get_tx_type_4x4(pd->plane_type, xd, block);
      scan_order = &vp9_scan_orders[TX_4X4][tx_type];
      mode = plane == 0 ? get_y_mode(xd->mi[0], block) : mbmi->uv_mode;
      vp9_predict_intra_block(xd, block, bwl, TX_4X4, mode,
                              x->skip_encode ? src : dst,
                              x->skip_encode ? src_stride : dst_stride,
                              dst, dst_stride, i, j, plane);

      if (!x->skip_recode) {
        vp9_subtract_block(4, 4, src_diff, diff_stride,
                           src, src_stride, dst, dst_stride);
        if (tx_type != DCT_DCT)
          vp9_fht4x4(src_diff, coeff, diff_stride, tx_type);
        else
          x->fwd_txm4x4(src_diff, coeff, diff_stride);
        vp9_quantize_b(coeff, 16, x->skip_block, p->zbin, p->round, p->quant,
                       p->quant_shift, qcoeff, dqcoeff,
                       pd->dequant, p->zbin_extra, eob, scan_order->scan,
                       scan_order->iscan);
      }

      if (!x->skip_encode && *eob) {
        if (tx_type == DCT_DCT)
          // this is like vp9_short_idct4x4 but has a special case around eob<=1
          // which is significant (not just an optimization) for the lossless
          // case.
          xd->itxm_add(dqcoeff, dst, dst_stride, *eob);
        else
          vp9_iht4x4_16_add(dqcoeff, dst, dst_stride, tx_type);
      }
      break;
    default:
      assert(0);
  }
  if (*eob)
    *(args->skip) = 0;
}
/**
 * @brief 读取inode的数据
 * @param[in]  pgroup    组描述符
 * @param[out] pinode    inode
 * @param[in]  inode_no  inode号
 */
void read_inode(const struct ext2_group_desc * pgroup, struct ext2_inode * pinode, int inode_no )
{
	lseek(fd_ext2, BLOCK_OFFSET(pgroup->bg_inode_table)+(inode_no-1)*sizeof(struct ext2_inode), SEEK_SET);
	read(fd_ext2, pinode, sizeof(struct ext2_inode));
}
/**
 * @brief 删除文件
 * @param[in] absolute_path 文件的绝对路径
 * @return 删除成功返回true,删除失败返回false
 */
bool remove_file(const char * absolute_path)  // rm
{
	struct ext2_group_desc group;
	struct ext2_inode inode;
	ssize_t bytesread = 0;

	void * block = NULL;
	int inode_no = root_inode_no;
	link_list path_list = NULL;
	link_list p = NULL;

	assert(absolute_path != NULL);
	assert(absolute_path[0] == '/');

	generate_path_linklist(&path_list, absolute_path);
	for ( p = path_list; p->next != NULL; p = p->next )
	{
		read_group_descriptor(&group);
		read_inode(&group, &inode, inode_no);

		struct ext2_dir_entry_2 * last_entry = NULL;
		struct ext2_dir_entry_2 * this_entry = NULL;
		unsigned int size = 0;

		if ((block = malloc(block_size)) == NULL)
		{
			fprintf(stderr, "No sufficient memory\n");
			exit(EXIT_FAILURE);
		}

		lseek(fd_ext2, BLOCK_OFFSET(inode.i_block[0]), SEEK_SET);
		bytesread = read(fd_ext2, block, block_size);                // read block from disk

		this_entry = (struct ext2_dir_entry_2 *) block;  // first entry in the directory
		// Notice that the list may be terminated with a NULL entry (entry->inode == NULL)

		inode_no = -1;
		while((size < inode.i_size) && this_entry->inode)
		{
			char file_name[EXT2_NAME_LEN+1];
			memcpy(file_name, this_entry->name, this_entry->name_len);
			file_name[this_entry->name_len] = '\0';
			if ( !strcmp(file_name, p->next->directory_name) )
			{
				inode_no = this_entry->inode;
				if ( p->next->next == NULL )
				{
					last_entry->rec_len += this_entry->rec_len;
					lseek(fd_ext2, BLOCK_OFFSET(inode.i_block[0]), SEEK_SET);
					write(fd_ext2, block, bytesread);
				}
				break;
			}
			last_entry = this_entry;
			this_entry = (struct ext2_dir_entry_2 *)((void *)this_entry + this_entry->rec_len);
			size += this_entry->rec_len;
		}

		free(block);
		block = this_entry = last_entry = NULL;

		if( inode_no == -1 )
			break;
	}
	destroy_path_linklist(&path_list);


	if ( inode_no == -1 )
		return false;

	return true;
}
/**
 * @brief 输出文件中的内容
 * @details cat函数调用这个函数
 * @param[in] absolute_path 绝对路径
 * @return 成功返回true,失败返回false
 */
bool output_file_data(const char * absolute_path) // cat
{
	int i, j;
	int inode_no = -1;

	struct ext2_group_desc group;
	struct ext2_inode inode;
	void * block = NULL;

	assert(absolute_path != NULL);
	assert(absolute_path[0] == '/');

	inode_no = get_file_inode_no(absolute_path);

	if ( inode_no == -1 )
	{
		fprintf(stderr, "cat: %s: No such file or directory\n", absolute_path);
		return false;
	}

	read_group_descriptor(&group);
	read_inode(&group, &inode, inode_no);
	if (S_ISDIR(inode.i_mode))
	{
		fprintf(stderr, "cat: %s: Is a directory\n", absolute_path);
		return false;
	}
	else if ( S_ISREG(inode.i_mode) )
	{
		if ((block = malloc(block_size)) == NULL)
		{
			fprintf(stderr, "Insufficient memory.\n");
			exit(EXIT_FAILURE);
		}

		for ( i = 0; i < inode.i_blocks; ++ i )
		{
			lseek(fd_ext2, BLOCK_OFFSET(inode.i_block[i]), SEEK_SET);
			read(fd_ext2, block, block_size);                // read block from disk
			if ( i < EXT2_NDIR_BLOCKS )
			{
				for ( j = 0; j < block_size; ++ j )
					putchar(((char *)block)[j]);
			}
			else if ( i == EXT2_IND_BLOCK )
			{
				// very complicated here, unfinished
				/*
				inode_no = inode.i_block[i];
				read_group_descriptor(&group);
				read_inode(&group, &inode_single_indirect_block, inode_no);
				for ( j = 0; j < 4 && i + j < block_size; ++ j )
				{
					lseek(fd_ext2, BLOCK_OFFSET(inode_single_indirect_block.i_block[j]), SEEK_SET);
					read(fd_ext2, block, block_size);                // read block from disk
					for ( k = 0; k < block_size; ++ k )
						putchar(((char *)block)[k]);
				}
				*/
			}
			else if ( i == EXT2_DIND_BLOCK )
			{
			}
			else if ( i == EXT2_TIND_BLOCK )
			{
			}
		}
	}
	else
	{
		fprintf(stderr, "cat: %s: Not supported\n", absolute_path);
		return false;
	}

	return true;
}
Exemplo n.º 21
0
void parseBlocks() {
  // SUPER BLOCK
  //  printf("SUPERBLOCK\n");

  // Block size
  unsigned int super_block_size_shift = readAtOffset(-1, 24, 4);
  super_block_size = 1024 << super_block_size_shift;

  // Magic number
  char super_magic_num_c[2];
  int i;
  for (i = 0; i < 2; i++) {
    pread(fd, &super_magic_num_c[i], 1, BLOCK_OFFSET(1) + 56 + i);
  }
  unsigned int super_magic_num = *(int *)super_magic_num_c & 0xFFFF;
  
  // Number of inodes
  super_num_inodes = readAtOffset(1, 0, 4);
  //  printf("num inodes: %u\n", super_num_inodes); 

  // Number of blocks
  super_num_blocks = readAtOffset(1, 4, 4);
  //  printf("num blocks: %u\n", super_num_blocks); 

  // Fragment size
  unsigned int super_fragment_size_shift = readAtOffset(1, 28, 4);
  unsigned int super_fragment_size = 1024 << super_fragment_size_shift;
  //  printf("fragment size: %u\n", super_fragment_size); 

  // Blocks per group
  super_blocks_per_group = readAtOffset(1, 32, 4);
  //  printf("blocks per group: %u\n", super_blocks_per_group); 

  // Inodes per group
  super_inodes_per_group = readAtOffset(1, 40, 4);
  //  printf("inodes per group: %u\n", super_inodes_per_group); 

  // Fragments per group
  unsigned int super_fragments_per_group = readAtOffset(1, 36, 4);
  //  printf("fragments per group: %u\n", super_fragments_per_group); 

  // First data block
  int first_data_block;
  if (super_block_size >= 1000)
    first_data_block = 1;
  else
    first_data_block = 0;
  //  printf("first data block: %d\n", first_data_block);

  // first non reserved inode
  unsigned int first_inode = readAtOffset(1, 84, 4);
  //  printf("first unreserved inode: %u\n", first_inode);
  
  // Make super.csv file
  FILE *superfd = fopen("super.csv", "w");
  fprintf(superfd, "%x,%u,%u,%u,%u,%u,%u,%u,%u\n", super_magic_num, super_num_inodes, super_num_blocks, super_block_size, super_fragment_size, super_blocks_per_group, super_inodes_per_group, super_fragments_per_group, first_data_block);
  fclose(superfd);
  //  printf("END SUPERBLOCK\n\n");

  //  printf("GROUP DESCRIPTOR\n");
  int num_gd = 1 + (super_num_blocks - 1) / super_blocks_per_group;
  int gd_size = 32;

  // Make group/csv
  FILE *groupfd = fopen("group.csv", "w");

  for (i = 0; i < num_gd; i++) {
    unsigned int gd_num_contained_blocks = super_blocks_per_group;
    if (i == num_gd - 1)
      gd_num_contained_blocks = super_num_blocks % super_blocks_per_group;
    unsigned int gd_num_free_blocks = readAtOffset(2, 12 + gd_size * i, 1);
    gd_num_free_blocks &= 0xFFFF;
    //    printf("num free blocks: %u\n", gd_num_free_blocks);
    unsigned int gd_num_free_inodes = readAtOffset(2, 14 + gd_size * i, 1);
    gd_num_free_inodes &= 0xFFFF;
    //    printf("num free inodes: %u\n", gd_num_free_inodes);
    unsigned int gd_num_dir = readAtOffset(2, 16 + gd_size * i, 1);
    gd_num_dir &= 0xFFFF;
    //    printf("num dir: %u\n", gd_num_dir);
    unsigned int gd_inode_bm_block = readAtOffset(2, 4 + gd_size * i, 4);
    //    printf("free inode bitmap block: 0x%02x\n", gd_inode_bm_block);
    unsigned int gd_block_bm_block = readAtOffset(2, gd_size * i, 4);
    //    printf("free block bitmap block: 0x%02x\n", gd_block_bm_block);
    unsigned int gd_inode_table_start = readAtOffset(2, 8 + gd_size * i, 4);
    //    printf("inode table start block: 0x%02x\n\n", gd_inode_table_start);
    fprintf(groupfd, "%u,%u,%u,%u,%x,%x,%x\n", gd_num_contained_blocks, gd_num_free_blocks, gd_num_free_inodes, gd_num_dir, gd_inode_bm_block, gd_block_bm_block, gd_inode_table_start);
  }
  fclose(groupfd);
  //  printf("END GROUP DESCRIPTOR\n\n");
  
  //  printf("BITMAP\n");
  FILE *bitmapfd = fopen("bitmap.csv", "w");
  int gd;
  for (gd = 0; gd < num_gd; gd++) {
    int byte_index;
    // Block bitmap
    unsigned int bm_block_num_block = readAtOffset(2, gd_size * gd, 4);
    iterateOverBitmap(-1, bm_block_num_block, gd_size, gd, super_blocks_per_group, bitmapfd);

    // Inode bitmap
    unsigned int bm_block_num_inode = readAtOffset(2, 4 + gd_size * gd, 4);
    iterateOverBitmap(-1, bm_block_num_inode, gd_size, gd, super_inodes_per_group, bitmapfd);
  }
  fclose(bitmapfd);
  //  printf("END BITMAP\n\n");
  
  //  printf("INODE\n");
  FILE *inodefd = fopen("inode.csv", "w");
  unsigned int inode_size = readAtOffset(1, 88, 1) & 0xFFFF;
  int inode_num;
  
  for (inode_num = 1; inode_num <= super_num_inodes; inode_num++) {
    int block_group = (inode_num - 1) / super_inodes_per_group;
    unsigned int inode_table_start = readAtOffset(2, 8 + gd_size * block_group, 4);
    unsigned int gd_inode_bm_block = 0;
    // Check if valid inode
    for (gd = 0; gd < num_gd; gd++) {
      unsigned int gd_inode_table_start = readAtOffset(2, 8 + gd_size * gd, 4);
      if (gd_inode_table_start == inode_table_start) {
	 gd_inode_bm_block = readAtOffset(2, 4 + gd_size * gd, 4);
	 break;
      }
    }
    unsigned int bm_inode = readAtOffset(2, 4 + gd_size * gd, 4);
    int allocated = iterateOverBitmap(inode_num, gd_inode_bm_block, gd_size, gd, super_inodes_per_group, NULL);
    if (!allocated)
      continue;

    unsigned int offset = inode_size * ((inode_num - 1) - super_inodes_per_group * block_group);
    unsigned int inode_data = readAtOffset(inode_table_start, offset, 1);
    unsigned int inode_type = inode_data & 0xF000;

    char inode_type_c;
    switch(inode_type) {
    case 0x4000:
      inode_type_c = 'd';
      break;
    case 0x8000:
      inode_type_c = 'f';
      break;
    case 0xA000:
      inode_type_c = 's';
      break;
    default:
      inode_type_c = '?';
      break;
    }

    unsigned int inode_mode = inode_data & 0xFFFF;
    unsigned int inode_owner = readAtOffset(inode_table_start, 2 + offset, 1);
    inode_owner &= 0xFFFF;
    unsigned int inode_group = readAtOffset(inode_table_start, 24 + offset, 1);
    inode_group &= 0xFFFF;
    unsigned int inode_link_num = readAtOffset(inode_table_start, 26 + offset, 1);
    inode_link_num &= 0xFFFF;
    unsigned int inode_creation_time = readAtOffset(inode_table_start, 12 + offset, 4);
    unsigned int inode_modification_time = readAtOffset(inode_table_start, 16 + offset, 4);
    unsigned int inode_access_time = readAtOffset(inode_table_start, 8 + offset, 4);
    unsigned int inode_file_size = readAtOffset(inode_table_start, 4 + offset, 4);
    unsigned int inode_block_count = readAtOffset(inode_table_start, 28 + offset, 4) / 2;
    
    fprintf(inodefd, "%d,%c,%o,%u,%u,%u,%x,%x,%x,%u,%u,", inode_num, inode_type_c, inode_mode, inode_owner, inode_group, inode_link_num, inode_creation_time, inode_modification_time, inode_access_time, inode_file_size, inode_block_count);

    int num_direct_block;
    for (num_direct_block = 0; num_direct_block <= 11; num_direct_block++) {
      int direct_block_num = readAtOffset(inode_table_start, 40 + 4 * num_direct_block + offset, 4);
      fprintf(inodefd, "%x,", direct_block_num);
    }

    unsigned int single_indirect_block_num = readAtOffset(inode_table_start, 88 + offset, 4);
    //    printf("single indirect %02x\n", single_indirect_block_num);
    fprintf(inodefd, "%x,", single_indirect_block_num);

    unsigned int double_indirect_block_num = readAtOffset(inode_table_start, 92 + offset, 4);
    //    printf("double indirect %02x\n", double_indirect_block_num);
    fprintf(inodefd, "%x,", double_indirect_block_num);

    unsigned int triple_indirect_block_num = readAtOffset(inode_table_start, 96 + offset, 4);
    //    printf("triple indirect %02x\n", triple_indirect_block_num);
    fprintf(inodefd, "%x\n", triple_indirect_block_num);
  }
  fclose(inodefd);
  //  printf("END INODES\n");
  
  //  printf("DIRECTORIES\n");

  FILE *directoryfd = fopen("directory.csv", "w");
  for (inode_num = 1; inode_num <= super_num_inodes; inode_num++) {
    int block_group = (inode_num - 1) / super_inodes_per_group;
    unsigned int inode_table_start_block = readAtOffset(2, 8 + gd_size * block_group, 4);
    unsigned int offset = inode_size * ((inode_num - 1) - super_inodes_per_group * block_group);
    unsigned int inode_data = readAtOffset(inode_table_start_block, offset, 1);
    unsigned int inode_type = inode_data & 0xF000;
    
    if (inode_type == 0x4000) {
      // for each direct pointer block of the directory
      int num_direct_p_block;
      int dir_entry_index = 0;
      int num_entries = 0;
      for (num_direct_p_block = 0; num_direct_p_block <= 11; num_direct_p_block++) {
     	int direct_p_block_num = readAtOffset(inode_table_start_block, 40 + 4 * num_direct_p_block + offset, 4);
	// traversing linked list of directory entries)
	if (direct_p_block_num == 0)
	  break;
	int entry_base = 0;
	unsigned int entry_length;
	do {
	  // check if directory is valid/allocated
	  entry_length = readAtOffset(direct_p_block_num, entry_base + 4, 1);
	  entry_length &= 0xFFFF;
	  // stop looking through directory block if no more entries
	  if (entry_length <= 0)
	    break;
	  unsigned int parent_inode_num = readAtOffset(direct_p_block_num, entry_base, 4);
	  unsigned int entry_name_length = readAtOffset(direct_p_block_num, entry_base + 6, 1);
	  entry_name_length &= 0xFF;

	  // get file name
	  int char_index;
	  char name[entry_name_length + 1];
	  pread(fd, &name, entry_name_length, BLOCK_OFFSET(direct_p_block_num) + entry_base + 8);
	  name[entry_name_length] = 0;

	  entry_base += entry_length;
	  if (entry_base % 4 != 0) {
	    entry_base += (4 - entry_base % 4);
	  }
	  if (parent_inode_num == 0 || parent_inode_num > super_num_inodes || strlen(name) == 0) {
	    num_entries++;
	    continue;
	  }

	  fprintf(directoryfd, "%u,%u,%u,%u,%u,\"%s\"\n", inode_num, num_entries, entry_length, entry_name_length, parent_inode_num, name);
	  
	  num_entries++;
	} while (entry_base < 1024);
      } // end direct blocks
    }     
  }
  fclose(directoryfd);
  //  printf("END DIRECTORIES\n");
  
  //  printf("INDIRECT BLOCK ENTRIES\n");

  FILE *indirectfd = fopen("indirect.csv", "w");

  for (inode_num = 1; inode_num <= super_num_inodes; inode_num++) {
    int block_group = (inode_num - 1) / super_inodes_per_group;
    unsigned int inode_table_start_block = readAtOffset(2, 8 + gd_size * block_group, 4);
    unsigned int offset = inode_size * ((inode_num - 1) - super_inodes_per_group * block_group);

    int count = 0;
    unsigned int single_indirect_p_block_num = readAtOffset(inode_table_start_block, 88 + offset, 4);

    if (single_indirect_p_block_num != 0) {
      int entry_num_single = 0;
      int num_single_indirect_p_block;
      for (num_single_indirect_p_block = 0; num_single_indirect_p_block < super_block_size / 4; num_single_indirect_p_block++) {
	unsigned int indirect_block_num = readAtOffset(single_indirect_p_block_num, num_single_indirect_p_block * 4 , 4);
	if (indirect_block_num == 0) {
	  entry_num_single++;
	  continue;
	}
	fprintf(indirectfd, "%x,%d,%x\n", single_indirect_p_block_num, entry_num_single, indirect_block_num);
	entry_num_single++;
      }
     
      single_indirect_p_block_num++;
      
    }

    unsigned int double_indirect_p_block_num = readAtOffset(inode_table_start_block, 92 + offset, 4);
    handleIndirectPointer(double_indirect_p_block_num, indirectfd);
    
    // Doubly indirect pointer block
    if (double_indirect_p_block_num != 0) {
      int num_doubly_indirect_p_block;
      for (num_doubly_indirect_p_block = 0; num_doubly_indirect_p_block < super_block_size / 4; num_doubly_indirect_p_block++) {
	unsigned int indirect_block_num = readAtOffset(double_indirect_p_block_num, num_doubly_indirect_p_block * 4 , 4);
	handleIndirectPointer(indirect_block_num, indirectfd);
      }
    } // end doubly indirect pointer block
    
    unsigned int triple_indirect_p_block_num = readAtOffset(inode_table_start_block, 96 + offset, 4);
    handleIndirectPointer(triple_indirect_p_block_num, indirectfd);

    if (triple_indirect_p_block_num != 0) {
      int num_triple_indirect_p_block;
      for (num_triple_indirect_p_block = 0; num_triple_indirect_p_block < super_block_size / 4; num_triple_indirect_p_block++) {
	unsigned int indirect_block_num = readAtOffset(double_indirect_p_block_num, num_triple_indirect_p_block * 4 , 4);
	if (indirect_block_num == 0)
	  continue;
	int num_indirect;
	for (num_indirect = 0; num_indirect < super_block_size / 4; num_indirect++) {
	  unsigned int block_num = readAtOffset(indirect_block_num, num_indirect * 4 , 4);
	  handleIndirectPointer(block_num, indirectfd);
	}
      }
    }
  } // end for each inode for indirect pointers
    
  fclose(indirectfd);
  //  printf("END INDIRECT BLOCK ENTRIES\n");
  
}
/**
 * @brief 获取文件的inode号
 * @param[in] absolute_path  文件的绝对路径
 * @return 返回文件的inode号,失败返回-1
 */
int get_file_inode_no(const char * absolute_path)
{
	struct ext2_group_desc group;
	struct ext2_inode inode;
	void * block = NULL;
	int inode_no = root_inode_no;

	link_list path_list = NULL;
	link_list p = NULL;

	assert(absolute_path != NULL);
	assert(absolute_path[0] == '/');

	generate_path_linklist(&path_list, absolute_path);
	for ( p = path_list; p != NULL; p = p->next )
	{
		read_group_descriptor(&group);
		read_inode(&group, &inode, inode_no);

		struct ext2_dir_entry_2 * entry = NULL;
		unsigned int size = 0;

		if ((block = malloc(block_size)) == NULL)
		{
			fprintf(stderr, "No sufficient memory\n");
			exit(EXIT_FAILURE);
		}

		lseek(fd_ext2, BLOCK_OFFSET(inode.i_block[0]), SEEK_SET);
		read(fd_ext2, block, block_size);                // read block from disk

		entry = (struct ext2_dir_entry_2 *) block;  // first entry in the directory
		// Notice that the list may be terminated with a NULL entry (entry->inode == NULL)
		if ( p->next == NULL )
		{
			free(block);
			block = NULL;
			break;
		}
		else
		{
			inode_no = -1;
			while((size < inode.i_size) && entry->inode)
			{
				char file_name[EXT2_NAME_LEN+1];
				memcpy(file_name, entry->name, entry->name_len);
				file_name[entry->name_len] = '\0';
				if ( !strcmp(file_name, p->next->directory_name) )
				{
					inode_no = entry->inode;
					break;
				}
				entry = (struct ext2_dir_entry_2 *)((void *)entry + entry->rec_len);
				size += entry->rec_len;
			}
			free(block);
			block = entry = NULL;
		}

		if( inode_no == -1 )
			break;
	}
	destroy_path_linklist(&path_list);
	return inode_no;
}
Exemplo n.º 23
0
static int optimize_b(MACROBLOCK *mb, int plane, int block,
                      TX_SIZE tx_size, int ctx) {
    MACROBLOCKD *const xd = &mb->e_mbd;
    struct macroblock_plane *const p = &mb->plane[plane];
    struct macroblockd_plane *const pd = &xd->plane[plane];
    const int ref = is_inter_block(xd->mi[0]);
    vp9_token_state tokens[1025][2];
    unsigned best_index[1025][2];
    uint8_t token_cache[1024];
    const tran_low_t *const coeff = BLOCK_OFFSET(mb->plane[plane].coeff, block);
    tran_low_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
    tran_low_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
    const int eob = p->eobs[block];
    const PLANE_TYPE type = get_plane_type(plane);
    const int default_eob = 16 << (tx_size << 1);
    const int mul = 1 + (tx_size == TX_32X32);
    const int16_t *dequant_ptr = pd->dequant;
    const uint8_t *const band_translate = get_band_translate(tx_size);
    const scan_order *const so = get_scan(xd, tx_size, type, block);
    const int16_t *const scan = so->scan;
    const int16_t *const nb = so->neighbors;
    int next = eob, sz = 0;
    int64_t rdmult = mb->rdmult * plane_rd_mult[type], rddiv = mb->rddiv;
    int64_t rd_cost0, rd_cost1;
    int rate0, rate1, error0, error1;
    int16_t t0, t1;
    EXTRABIT e0;
    int best, band, pt, i, final_eob;
#if CONFIG_VP9_HIGHBITDEPTH
    const int *cat6_high_cost = vp9_get_high_cost_table(xd->bd);
#else
    const int *cat6_high_cost = vp9_get_high_cost_table(8);
#endif

    assert((!type && !plane) || (type && plane));
    assert(eob <= default_eob);

    /* Now set up a Viterbi trellis to evaluate alternative roundings. */
    if (!ref)
        rdmult = (rdmult * 9) >> 4;

    /* Initialize the sentinel node of the trellis. */
    tokens[eob][0].rate = 0;
    tokens[eob][0].error = 0;
    tokens[eob][0].next = default_eob;
    tokens[eob][0].token = EOB_TOKEN;
    tokens[eob][0].qc = 0;
    tokens[eob][1] = tokens[eob][0];

    for (i = 0; i < eob; i++)
        token_cache[scan[i]] =
            vp9_pt_energy_class[vp9_get_token(qcoeff[scan[i]])];

    for (i = eob; i-- > 0;) {
        int base_bits, d2, dx;
        const int rc = scan[i];
        int x = qcoeff[rc];
        /* Only add a trellis state for non-zero coefficients. */
        if (x) {
            int shortcut = 0;
            error0 = tokens[next][0].error;
            error1 = tokens[next][1].error;
            /* Evaluate the first possibility for this state. */
            rate0 = tokens[next][0].rate;
            rate1 = tokens[next][1].rate;
            vp9_get_token_extra(x, &t0, &e0);
            /* Consider both possible successor states. */
            if (next < default_eob) {
                band = band_translate[i + 1];
                pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache);
                rate0 += mb->token_costs[tx_size][type][ref][band][0][pt]
                         [tokens[next][0].token];
                rate1 += mb->token_costs[tx_size][type][ref][band][0][pt]
                         [tokens[next][1].token];
            }
            UPDATE_RD_COST();
            /* And pick the best. */
            best = rd_cost1 < rd_cost0;
            base_bits = vp9_get_cost(t0, e0, cat6_high_cost);
            dx = mul * (dqcoeff[rc] - coeff[rc]);
#if CONFIG_VP9_HIGHBITDEPTH
            if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
                dx >>= xd->bd - 8;
            }
#endif  // CONFIG_VP9_HIGHBITDEPTH
            d2 = dx * dx;
            tokens[i][0].rate = base_bits + (best ? rate1 : rate0);
            tokens[i][0].error = d2 + (best ? error1 : error0);
            tokens[i][0].next = next;
            tokens[i][0].token = t0;
            tokens[i][0].qc = x;
            best_index[i][0] = best;

            /* Evaluate the second possibility for this state. */
            rate0 = tokens[next][0].rate;
            rate1 = tokens[next][1].rate;

            if ((abs(x) * dequant_ptr[rc != 0] > abs(coeff[rc]) * mul) &&
                    (abs(x) * dequant_ptr[rc != 0] < abs(coeff[rc]) * mul +
                     dequant_ptr[rc != 0]))
                shortcut = 1;
            else
                shortcut = 0;

            if (shortcut) {
                sz = -(x < 0);
                x -= 2 * sz + 1;
            }

            /* Consider both possible successor states. */
            if (!x) {
                /* If we reduced this coefficient to zero, check to see if
                 *  we need to move the EOB back here.
                 */
                t0 = tokens[next][0].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
                t1 = tokens[next][1].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
                e0 = 0;
            } else {
                vp9_get_token_extra(x, &t0, &e0);
                t1 = t0;
            }
            if (next < default_eob) {
                band = band_translate[i + 1];
                if (t0 != EOB_TOKEN) {
                    pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache);
                    rate0 += mb->token_costs[tx_size][type][ref][band][!x][pt]
                             [tokens[next][0].token];
                }
                if (t1 != EOB_TOKEN) {
                    pt = trellis_get_coeff_context(scan, nb, i, t1, token_cache);
                    rate1 += mb->token_costs[tx_size][type][ref][band][!x][pt]
                             [tokens[next][1].token];
                }
            }

            UPDATE_RD_COST();
            /* And pick the best. */
            best = rd_cost1 < rd_cost0;
            base_bits = vp9_get_cost(t0, e0, cat6_high_cost);

            if (shortcut) {
#if CONFIG_VP9_HIGHBITDEPTH
                if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
                    dx -= ((dequant_ptr[rc != 0] >> (xd->bd - 8)) + sz) ^ sz;
                } else {
                    dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
                }
#else
                dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
#endif  // CONFIG_VP9_HIGHBITDEPTH
                d2 = dx * dx;
            }
/*
** Write the cache of pFile to disk. If crash is non-zero, randomly
** skip blocks when writing. The cache is deleted before returning.
*/
static int writeCache2(OsTestFile *pFile, int crash){
  int i;
  int nMax = pFile->nMaxWrite;
  i64 offset;
  int rc = SQLITE_OK;

  offset = osTell(pFile);
  for(i=0; i<pFile->nBlk; i++){
    u8 *p = pFile->apBlk[i];
    if( p ){
      int skip = 0;
      int trash = 0;
      if( crash ){
        char random;
        sqlite3Randomness(1, &random);
        if( random & 0x01 ){
          if( random & 0x02 ){
            trash = 1;
#ifdef TRACE_WRITECACHE
printf("Trashing block %d of %s\n", i, pFile->zName); 
#endif
          }else{
            skip = 1;
#ifdef TRACE_WRITECACHE
printf("Skiping block %d of %s\n", i, pFile->zName); 
#endif
          }
        }else{
#ifdef TRACE_WRITECACHE
printf("Writing block %d of %s\n", i, pFile->zName); 
#endif
        }
      }
      if( rc==SQLITE_OK ){
        rc = sqlite3RealSeek(&pFile->fd, BLOCK_OFFSET(i));
      }
      if( rc==SQLITE_OK && !skip ){
        int len = BLOCKSIZE;
        if( BLOCK_OFFSET(i+1)>nMax ){
          len = nMax-BLOCK_OFFSET(i);
        }
        if( len>0 ){
          if( trash ){
            sqlite3Randomness(len, p);
          }
          rc = sqlite3RealWrite(&pFile->fd, p, len);
        }
      }
      sqliteFree(p);
    }
  }
  sqliteFree(pFile->apBlk);
  pFile->nBlk = 0;
  pFile->apBlk = 0;
  pFile->nMaxWrite = 0;

  if( rc==SQLITE_OK ){
    rc = sqlite3RealSeek(&pFile->fd, offset);
  }
  return rc;
}
Exemplo n.º 25
0
static void tokenize_b(int plane, int block, BLOCK_SIZE plane_bsize,
                       TX_SIZE tx_size, void *arg) {
  struct tokenize_b_args* const args = arg;
  VP9_COMP *cpi = args->cpi;
  MACROBLOCKD *xd = args->xd;
  TOKENEXTRA **tp = args->tp;
  uint8_t token_cache[32 * 32];
  struct macroblock_plane *p = &cpi->mb.plane[plane];
  struct macroblockd_plane *pd = &xd->plane[plane];
  MB_MODE_INFO *mbmi = &xd->mi[0].src_mi->mbmi;
  int pt; /* near block/prev token context index */
  int c;
  TOKENEXTRA *t = *tp;        /* store tokens starting here */
  int eob = p->eobs[block];
  const PLANE_TYPE type = pd->plane_type;
  const tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  const int segment_id = mbmi->segment_id;
  const int16_t *scan, *nb;
  const scan_order *so;
  const int ref = is_inter_block(mbmi);
  unsigned int (*const counts)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
      cpi->coef_counts[tx_size][type][ref];
  vp9_prob (*const coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
      cpi->common.fc.coef_probs[tx_size][type][ref];
  unsigned int (*const eob_branch)[COEFF_CONTEXTS] =
      cpi->common.counts.eob_branch[tx_size][type][ref];
  const uint8_t *const band = get_band_translate(tx_size);
  const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size);
  const TOKENVALUE *dct_value_tokens;

  int aoff, loff;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);

  pt = get_entropy_context(tx_size, pd->above_context + aoff,
                           pd->left_context + loff);
  so = get_scan(xd, tx_size, type, block);
  scan = so->scan;
  nb = so->neighbors;
  c = 0;
#if CONFIG_VP9_HIGHBITDEPTH
  if (cpi->common.profile >= PROFILE_2) {
    dct_value_tokens = (cpi->common.bit_depth == VPX_BITS_10 ?
                        vp9_dct_value_tokens_high10_ptr :
                        vp9_dct_value_tokens_high12_ptr);
  } else {
    dct_value_tokens = vp9_dct_value_tokens_ptr;
  }
#else
  dct_value_tokens = vp9_dct_value_tokens_ptr;
#endif

  while (c < eob) {
    int v = 0;
    int skip_eob = 0;
    v = qcoeff[scan[c]];

    while (!v) {
      add_token_no_extra(&t, coef_probs[band[c]][pt], ZERO_TOKEN, skip_eob,
                         counts[band[c]][pt]);
      eob_branch[band[c]][pt] += !skip_eob;

      skip_eob = 1;
      token_cache[scan[c]] = 0;
      ++c;
      pt = get_coef_context(nb, token_cache, c);
      v = qcoeff[scan[c]];
    }

    add_token(&t, coef_probs[band[c]][pt],
              dct_value_tokens[v].extra,
              (uint8_t)dct_value_tokens[v].token,
              (uint8_t)skip_eob,
              counts[band[c]][pt]);
    eob_branch[band[c]][pt] += !skip_eob;

    token_cache[scan[c]] = vp9_pt_energy_class[dct_value_tokens[v].token];
    ++c;
    pt = get_coef_context(nb, token_cache, c);
  }
  if (c < seg_eob) {
    add_token_no_extra(&t, coef_probs[band[c]][pt], EOB_TOKEN, 0,
                       counts[band[c]][pt]);
    ++eob_branch[band[c]][pt];
  }

  *tp = t;

  vp9_set_contexts(xd, pd, plane_bsize, tx_size, c > 0, aoff, loff);
}
Exemplo n.º 26
0
static void tokenize_b(int plane, int block, BLOCK_SIZE_TYPE plane_bsize,
                       TX_SIZE tx_size, void *arg) {
  struct tokenize_b_args* const args = arg;
  VP9_COMP *cpi = args->cpi;
  MACROBLOCKD *xd = args->xd;
  TOKENEXTRA **tp = args->tp;
  struct macroblockd_plane *pd = &xd->plane[plane];
  MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
  int pt; /* near block/prev token context index */
  int c = 0, rc = 0;
  TOKENEXTRA *t = *tp;        /* store tokens starting here */
  const int eob = pd->eobs[block];
  const PLANE_TYPE type = pd->plane_type;
  const int16_t *qcoeff_ptr = BLOCK_OFFSET(pd->qcoeff, block);
  int seg_eob;
  const int segment_id = mbmi->segment_id;
  const int16_t *scan, *nb;
  vp9_coeff_count *const counts = cpi->coef_counts[tx_size];
  vp9_coeff_probs_model *const coef_probs = cpi->common.fc.coef_probs[tx_size];
  const int ref = is_inter_block(mbmi);
  ENTROPY_CONTEXT above_ec, left_ec;
  uint8_t token_cache[1024];
  const uint8_t *band_translate;
  ENTROPY_CONTEXT *A, *L;
  int aoff, loff;
  txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &aoff, &loff);

  A = pd->above_context + aoff;
  L = pd->left_context + loff;

  assert((!type && !plane) || (type && plane));

  switch (tx_size) {
    case TX_4X4:
      above_ec = A[0] != 0;
      left_ec = L[0] != 0;
      seg_eob = 16;
      scan = get_scan_4x4(get_tx_type_4x4(type, xd, block));
      band_translate = vp9_coefband_trans_4x4;
      break;
    case TX_8X8:
      above_ec = !!*(uint16_t *)A;
      left_ec  = !!*(uint16_t *)L;
      seg_eob = 64;
      scan = get_scan_8x8(get_tx_type_8x8(type, xd));
      band_translate = vp9_coefband_trans_8x8plus;
      break;
    case TX_16X16:
      above_ec = !!*(uint32_t *)A;
      left_ec  = !!*(uint32_t *)L;
      seg_eob = 256;
      scan = get_scan_16x16(get_tx_type_16x16(type, xd));
      band_translate = vp9_coefband_trans_8x8plus;
      break;
    case TX_32X32:
      above_ec = !!*(uint64_t *)A;
      left_ec  = !!*(uint64_t *)L;
      seg_eob = 1024;
      scan = vp9_default_scan_32x32;
      band_translate = vp9_coefband_trans_8x8plus;
      break;
    default:
      assert(!"Invalid transform size");
  }

  pt = combine_entropy_contexts(above_ec, left_ec);
  nb = vp9_get_coef_neighbors_handle(scan);

  if (vp9_segfeature_active(&cpi->common.seg, segment_id, SEG_LVL_SKIP))
    seg_eob = 0;

  c = 0;
  do {
    const int band = get_coef_band(band_translate, c);
    int token;
    int v = 0;
    rc = scan[c];
    if (c)
      pt = get_coef_context(nb, token_cache, c);
    if (c < eob) {
      v = qcoeff_ptr[rc];
      assert(-DCT_MAX_VALUE <= v  &&  v < DCT_MAX_VALUE);

      t->extra = vp9_dct_value_tokens_ptr[v].extra;
      token    = vp9_dct_value_tokens_ptr[v].token;
    } else {
      token = DCT_EOB_TOKEN;
    }

    t->token = token;
    t->context_tree = coef_probs[type][ref][band][pt];
    t->skip_eob_node = (c > 0) && (token_cache[scan[c - 1]] == 0);

    assert(vp9_coef_encodings[t->token].len - t->skip_eob_node > 0);

    ++counts[type][ref][band][pt][token];
    if (!t->skip_eob_node)
      ++cpi->common.counts.eob_branch[tx_size][type][ref][band][pt];

    token_cache[rc] = vp9_pt_energy_class[token];
    ++t;
  } while (c < eob && ++c < seg_eob);

  *tp = t;

  set_contexts(xd, pd, plane_bsize, tx_size, c > 0, aoff, loff);
}
Exemplo n.º 27
0
static int optimize_b(MACROBLOCK *mb, int plane, int block,
                      TX_SIZE tx_size, int ctx) {
  MACROBLOCKD *const xd = &mb->e_mbd;
  struct macroblock_plane *const p = &mb->plane[plane];
  struct macroblockd_plane *const pd = &xd->plane[plane];
  const int ref = is_inter_block(&xd->mi[0]->mbmi);
  vp9_token_state tokens[1025][2];
  unsigned best_index[1025][2];
  uint8_t token_cache[1024];
  const int16_t *const coeff = BLOCK_OFFSET(mb->plane[plane].coeff, block);
  int16_t *const qcoeff = BLOCK_OFFSET(p->qcoeff, block);
  int16_t *const dqcoeff = BLOCK_OFFSET(pd->dqcoeff, block);
  const int eob = p->eobs[block];
  const PLANE_TYPE type = pd->plane_type;
  const int default_eob = 16 << (tx_size << 1);
  const int mul = 1 + (tx_size == TX_32X32);
  const int16_t *dequant_ptr = pd->dequant;
  const uint8_t *const band_translate = get_band_translate(tx_size);
  const scan_order *const so = get_scan(xd, tx_size, type, block);
  const int16_t *const scan = so->scan;
  const int16_t *const nb = so->neighbors;
  int next = eob, sz = 0;
  int64_t rdmult = mb->rdmult * plane_rd_mult[type], rddiv = mb->rddiv;
  int64_t rd_cost0, rd_cost1;
  int rate0, rate1, error0, error1, t0, t1;
  int best, band, pt, i, final_eob;

  assert((!type && !plane) || (type && plane));
  assert(eob <= default_eob);

  /* Now set up a Viterbi trellis to evaluate alternative roundings. */
  if (!ref)
    rdmult = (rdmult * 9) >> 4;

  /* Initialize the sentinel node of the trellis. */
  tokens[eob][0].rate = 0;
  tokens[eob][0].error = 0;
  tokens[eob][0].next = default_eob;
  tokens[eob][0].token = EOB_TOKEN;
  tokens[eob][0].qc = 0;
  tokens[eob][1] = tokens[eob][0];

  for (i = 0; i < eob; i++)
    token_cache[scan[i]] =
        vp9_pt_energy_class[vp9_dct_value_tokens_ptr[qcoeff[scan[i]]].token];

  for (i = eob; i-- > 0;) {
    int base_bits, d2, dx;
    const int rc = scan[i];
    int x = qcoeff[rc];
    /* Only add a trellis state for non-zero coefficients. */
    if (x) {
      int shortcut = 0;
      error0 = tokens[next][0].error;
      error1 = tokens[next][1].error;
      /* Evaluate the first possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;
      t0 = (vp9_dct_value_tokens_ptr + x)->token;
      /* Consider both possible successor states. */
      if (next < default_eob) {
        band = band_translate[i + 1];
        pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache);
        rate0 += mb->token_costs[tx_size][type][ref][band][0][pt]
                                [tokens[next][0].token];
        rate1 += mb->token_costs[tx_size][type][ref][band][0][pt]
                                [tokens[next][1].token];
      }
      UPDATE_RD_COST();
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
      base_bits = vp9_dct_value_cost_ptr[x];
      dx = mul * (dqcoeff[rc] - coeff[rc]);
      d2 = dx * dx;
      tokens[i][0].rate = base_bits + (best ? rate1 : rate0);
      tokens[i][0].error = d2 + (best ? error1 : error0);
      tokens[i][0].next = next;
      tokens[i][0].token = t0;
      tokens[i][0].qc = x;
      best_index[i][0] = best;

      /* Evaluate the second possibility for this state. */
      rate0 = tokens[next][0].rate;
      rate1 = tokens[next][1].rate;

      if ((abs(x) * dequant_ptr[rc != 0] > abs(coeff[rc]) * mul) &&
          (abs(x) * dequant_ptr[rc != 0] < abs(coeff[rc]) * mul +
                                               dequant_ptr[rc != 0]))
        shortcut = 1;
      else
        shortcut = 0;

      if (shortcut) {
        sz = -(x < 0);
        x -= 2 * sz + 1;
      }

      /* Consider both possible successor states. */
      if (!x) {
        /* If we reduced this coefficient to zero, check to see if
         *  we need to move the EOB back here.
         */
        t0 = tokens[next][0].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
        t1 = tokens[next][1].token == EOB_TOKEN ? EOB_TOKEN : ZERO_TOKEN;
      } else {
        t0 = t1 = (vp9_dct_value_tokens_ptr + x)->token;
      }
      if (next < default_eob) {
        band = band_translate[i + 1];
        if (t0 != EOB_TOKEN) {
          pt = trellis_get_coeff_context(scan, nb, i, t0, token_cache);
          rate0 += mb->token_costs[tx_size][type][ref][band][!x][pt]
                                  [tokens[next][0].token];
        }
        if (t1 != EOB_TOKEN) {
          pt = trellis_get_coeff_context(scan, nb, i, t1, token_cache);
          rate1 += mb->token_costs[tx_size][type][ref][band][!x][pt]
                                  [tokens[next][1].token];
        }
      }

      UPDATE_RD_COST();
      /* And pick the best. */
      best = rd_cost1 < rd_cost0;
      base_bits = vp9_dct_value_cost_ptr[x];

      if (shortcut) {
        dx -= (dequant_ptr[rc != 0] + sz) ^ sz;
        d2 = dx * dx;
      }
      tokens[i][1].rate = base_bits + (best ? rate1 : rate0);
      tokens[i][1].error = d2 + (best ? error1 : error0);
      tokens[i][1].next = next;
      tokens[i][1].token = best ? t1 : t0;
      tokens[i][1].qc = x;
      best_index[i][1] = best;
      /* Finally, make this the new head of the trellis. */
      next = i;
    } else {
      /* There's no choice to make for a zero coefficient, so we don't
       *  add a new trellis node, but we do need to update the costs.
       */
      band = band_translate[i + 1];
      t0 = tokens[next][0].token;
      t1 = tokens[next][1].token;
      /* Update the cost of each path if we're past the EOB token. */
      if (t0 != EOB_TOKEN) {
        tokens[next][0].rate +=
            mb->token_costs[tx_size][type][ref][band][1][0][t0];
        tokens[next][0].token = ZERO_TOKEN;
      }
      if (t1 != EOB_TOKEN) {
        tokens[next][1].rate +=
            mb->token_costs[tx_size][type][ref][band][1][0][t1];
        tokens[next][1].token = ZERO_TOKEN;
      }
      best_index[i][0] = best_index[i][1] = 0;
      /* Don't update next, because we didn't add a new node. */
    }
  }

  /* Now pick the best path through the whole trellis. */
  band = band_translate[i + 1];
  rate0 = tokens[next][0].rate;
  rate1 = tokens[next][1].rate;
  error0 = tokens[next][0].error;
  error1 = tokens[next][1].error;
  t0 = tokens[next][0].token;
  t1 = tokens[next][1].token;
  rate0 += mb->token_costs[tx_size][type][ref][band][0][ctx][t0];
  rate1 += mb->token_costs[tx_size][type][ref][band][0][ctx][t1];
  UPDATE_RD_COST();
  best = rd_cost1 < rd_cost0;
  final_eob = -1;
  vpx_memset(qcoeff, 0, sizeof(*qcoeff) * (16 << (tx_size * 2)));
  vpx_memset(dqcoeff, 0, sizeof(*dqcoeff) * (16 << (tx_size * 2)));
  for (i = next; i < eob; i = next) {
    const int x = tokens[i][best].qc;
    const int rc = scan[i];
    if (x) {
      final_eob = i;
    }

    qcoeff[rc] = x;
    dqcoeff[rc] = (x * dequant_ptr[rc != 0]) / mul;

    next = tokens[i][best].next;
    best = best_index[i][best];
  }
  final_eob++;

  mb->plane[plane].eobs[block] = final_eob;
  return final_eob;
}