コード例 #1
0
int
arithmodel_encode_cbt(Arithmodel *am, unsigned int n, unsigned int nmax, Index bit0, Index bit1)
{
  int k = calc_num_of_figures(nmax + 1);
  unsigned int kn = (1 << k) - (nmax + 1);

  if (n > nmax) {
    fprintf(stderr, "%s: n = %d > %d = nmax\n", __FUNCTION__, n, nmax);
    return -1;
  }

  if (n < kn) {
    arithmodel_encode_bits(am, n, k - 1, bit0, bit1);
    return k - 1;
  }

  arithmodel_encode_bits(am, n + kn, k, bit0, bit1);
  return k;
}
コード例 #2
0
int
arithmodel_encode_delta(Arithmodel *m, unsigned int n, Index bit0, Index bit1)
{
  int t;
  int nbits = calc_num_of_figures(n);

  t = arithmodel_encode_gamma(m, nbits, bit0, bit1);
  if (nbits > 1)
    arithmodel_encode_bits(m, n, nbits - 1, bit0, bit1);

  return t + nbits - 1;
}
コード例 #3
0
int
arithmodel_encode_gamma(Arithmodel *m, unsigned int n, Index bit0, Index bit1)
{
  int i;
  int nbits = calc_num_of_figures(n);

  for (i = 0; i < nbits - 1; i++)
    arithmodel_encode(m, bit0);
  arithmodel_encode_bits(m, n, nbits, bit0, bit1);

  return nbits * 2 - 1;
}
コード例 #4
0
static void
encode(VMPM *vmpm)
{
  Arithcoder *ac;
  Arithmodel *char_am;
  Arithmodel *am;
  Arithmodel *bin_am;
  unsigned int j, nsymbols, *symbol_to_index;
  int i, match_found;

  //debug_message_fn("()\n");

  if (!vmpm->outfile) {
    debug_message_fnc("outfile is NULL.\n");
    return;
  }

  ac = arithcoder_arith_create();
  arithcoder_encode_init(ac, vmpm->outfile);

  char_am = arithmodel_order_zero_create();
  arithmodel_encode_init(char_am, ac);
  arithmodel_order_zero_set_update_escape_freq(char_am, update_escape_freq);

  am = arithmodel_order_zero_create();
  arithmodel_encode_init(am, ac);

  bin_am = arithmodel_order_zero_create();
  arithmodel_encode_init(bin_am, ac);

  match_found = 0;
  for (i = vmpm->I; i >= 1; i--) {
    nsymbols = 0;
    for (j = 0; j < vmpm->token_index[i]; j++) {
      Token_value tv = vmpm->token[i][j]->value - 1;

      if (nsymbols == tv) {
	nsymbols++;
      } else {
	match_found++;
	break;
      }
    }
    if (match_found) {
      stat_message(vmpm, "Match found at Level %d\n", i);
      break;
    }
  }

  fputc(i, vmpm->outfile);
  if (match_found) {
    for (; i >= 1; i--) {
      stat_message(vmpm, "Level %d (%d tokens, %d distinct): ", i, vmpm->token_index[i], vmpm->newtoken[i] - 1);
      arithmodel_order_zero_reset(bin_am, 0, 0);
      arithmodel_install_symbol(bin_am, 1);
      arithmodel_install_symbol(bin_am, 1);
      /* The first token of each level must be t_0. */
      if (vmpm->token[i][0]->value != 1)
	generic_error((char *)"Invalid token value.\n", INVALID_TOKEN_VALUE_ERROR);
      /* Hence, we don't need to encode it. */
      stat_message(vmpm, "e ");
      nsymbols = 1;
      arithmodel_order_zero_reset(am, 0, 0);
      arithmodel_install_symbol(am, 1);
      for (j = 1; j < vmpm->token_index[i]; j++) {
	Token_value tv = vmpm->token[i][j]->value - 1;

	if (nsymbols == tv) {
	  stat_message(vmpm, "e ");
	  nsymbols++;
	  arithmodel_encode(bin_am, 1);
	  arithmodel_install_symbol(am, 1);
	} else {
	  stat_message(vmpm, "%d ", tv);
	  arithmodel_encode(bin_am, 0);
	  arithmodel_encode(am, tv);
	}
      }
      stat_message(vmpm, "\n");
      stat_message(vmpm, "Level %d: %ld bytes\n", i, ftell(vmpm->outfile));
    }
  }

  if ((symbol_to_index = malloc(vmpm->alphabetsize * sizeof(unsigned int))) == NULL)
    memory_error(NULL, MEMORY_ERROR);
  memset(symbol_to_index, 255, vmpm->alphabetsize * sizeof(unsigned int));

  nsymbols = 0;
  arithmodel_order_zero_reset(bin_am, 0, 0);
  arithmodel_install_symbol(bin_am, 1);
  arithmodel_install_symbol(bin_am, 1);
  arithmodel_order_zero_reset(char_am, 0, vmpm->alphabetsize - 1);
  stat_message(vmpm, "Level 0 (%d tokens): ", vmpm->token_index[0]);
  for (j = 0; j < vmpm->token_index[0]; j++) {
    if (symbol_to_index[(int)vmpm->token[0][j]] == (unsigned int)-1) {
      stat_message(vmpm, "e ");
      arithmodel_encode(char_am, nsymbols);
      symbol_to_index[(int)vmpm->token[0][j]] = nsymbols++;
      arithmodel_encode_bits(bin_am, (int)vmpm->token[0][j], vmpm->bits_per_symbol, 0, 1);
    } else {
      stat_message(vmpm, "%d ", symbol_to_index[(int)vmpm->token[0][j]]);
      arithmodel_encode(char_am, symbol_to_index[(int)vmpm->token[0][j]]);
    }
  }
  stat_message(vmpm, "\n");
  free(symbol_to_index);

  arithmodel_encode_final(bin_am);
  arithmodel_encode_final(char_am);
  arithcoder_encode_final(ac);

  arithmodel_destroy(bin_am);
  arithmodel_destroy(char_am);
  arithcoder_destroy(ac);
}
コード例 #5
0
static void
encode(VMPM *vmpm)
{
  Arithcoder *ac;
  Arithmodel *am;
  Arithmodel *bin_am;
  Arithmodel **low_ams;
  unsigned int *symbol_to_index;
  int i, n, match_found;
  unsigned int j;

  //debug_message_fn("()\n");

  if (!vmpm->outfile) {
    debug_message_fnc("outfile is NULL.\n");
    return;
  }

  ac = arithcoder_arith_create();
  arithcoder_encode_init(ac, vmpm->outfile);

  if (vmpm->nlowbits < 8) {
    am = arithmodel_order_zero_create();

    arithmodel_encode_init(am, ac);
    arithmodel_order_zero_set_update_escape_freq(am, update_escape_freq);

    bin_am = arithmodel_order_zero_create();
    arithmodel_encode_init(bin_am, ac);
    {
      Arithmodel_order_zero *am_oz = (Arithmodel_order_zero *)am;
      am_oz->bin_am = bin_am;
      am_oz->escape_encoded_with_rle = 1;
    }

    match_found = 0;
    for (i = vmpm->I; i >= 1; i--) {
      int nsymbols = 0;

      for (j = 0; j < vmpm->token_index[i]; j++) {
	Token *t = vmpm->token[i][j];
	Token_value tv = t->value - 1;

	if (nsymbols == tv) {
	  nsymbols++;
	} else {
	  match_found++;
	  break;
	}
      }
      if (match_found) {
	stat_message(vmpm, "Match found at Level %d\n", i);
	break;
      }
    }

    fprintf(vmpm->outfile, "%c", i);
    if (match_found) {
      for (; i >= 1; i--) {
	int nsymbols = 0;

	stat_message(vmpm, "Level %d (%d tokens, %d distinct): ", i, vmpm->token_index[i], vmpm->newtoken[i] - 1);
	arithmodel_order_zero_reset(bin_am, 0, 0);
	arithmodel_install_symbol(bin_am, 1);
	arithmodel_install_symbol(bin_am, 1);
	/* Send the number of distinct symbols. */
	arithmodel_encode_cbt(bin_am, vmpm->newtoken[i] - 1, vmpm->token_index[i], 0, 1);
	arithmodel_order_zero_reset(am, 0, vmpm->newtoken[i]);
	for (j = 0; j < vmpm->token_index[i]; j++) {
	  Token *t = vmpm->token[i][j];
	  Token_value tv = t->value - 1;

	  if (nsymbols == tv) {
	    stat_message(vmpm, "e ");
	    nsymbols++;
	  } else {
	    stat_message(vmpm, "%d ", tv);
	  }

	  arithmodel_encode(am, tv);
	}
	stat_message(vmpm, "\n");
	stat_message(vmpm, "Level %d: %ld bytes\n", i, ftell(vmpm->outfile));
      }
    }

    if ((symbol_to_index = malloc(vmpm->alphabetsize * sizeof(unsigned int))) == NULL)
      memory_error(NULL, MEMORY_ERROR);
    memset(symbol_to_index, 255, vmpm->alphabetsize * sizeof(unsigned int));

    n = 0;
    arithmodel_order_zero_reset(am, 0, vmpm->alphabetsize - 1);
    arithmodel_order_zero_reset(bin_am, 0, 0);
    arithmodel_install_symbol(bin_am, 1);
    arithmodel_install_symbol(bin_am, 1);
    stat_message(vmpm, "Level 0 (%d tokens): ", vmpm->token_index[0]);
    for (j = 0; j < vmpm->token_index[0]; j++) {
      if (symbol_to_index[(int)vmpm->token[0][j]] == (unsigned int)-1) {
	stat_message(vmpm, "e ");
	arithmodel_encode(am, n);
	symbol_to_index[(int)vmpm->token[0][j]] = n++;
	arithmodel_encode_bits(bin_am, (int)vmpm->token[0][j], vmpm->bits_per_symbol, 0, 1);
      } else {
	stat_message(vmpm, "%d ", symbol_to_index[(int)vmpm->token[0][j]]);
	arithmodel_encode(am, symbol_to_index[(int)vmpm->token[0][j]]);
      }
    }
    stat_message(vmpm, "\n");
    free(symbol_to_index);

    arithmodel_encode_final(bin_am);
    arithmodel_encode_final(am);

    arithmodel_destroy(bin_am);
    arithmodel_destroy(am);
  }

  stat_message(vmpm, "Higher part: %ld bytes (%s)\n", ftell(vmpm->outfile), vmpm->outfilepath);

  if ((low_ams = calloc(1 << (8 - vmpm->nlowbits), sizeof(Arithmodel *))) == NULL)
    memory_error(NULL, MEMORY_ERROR);
  for (i = 0; i < (1 << (8 - vmpm->nlowbits)); i++) {
    low_ams[i] = arithmodel_order_zero_create();
    arithmodel_encode_init(low_ams[i], ac);
    arithmodel_order_zero_reset(low_ams[i], 0, 0);
    for (j = 0; j < (1 << vmpm->nlowbits); j++)
      arithmodel_install_symbol(low_ams[i], 1);
  }

  for (i = 0; i < vmpm->buffer_low_size; i++)
    arithmodel_encode(low_ams[vmpm->buffer_high[i]], vmpm->buffer_low[i]);

  for (i = 0; i < (1 << (8 - vmpm->nlowbits)); i++)
    arithmodel_encode_final(low_ams[i]);
  for (i = 0; i < (1 << (8 - vmpm->nlowbits)); i++)
    arithmodel_destroy(low_ams[i]);

  arithcoder_encode_final(ac);
  arithcoder_destroy(ac);

  free(low_ams);
}
コード例 #6
0
static int
encode_recursively(VMPM *vmpm, Arithmodel **ams, Arithmodel *bin_am, Arithmodel *level_am,
		   unsigned int *s_to_i, int i, int j, unsigned int pos)
{
  unsigned int len;

  if (i == 0) {
    /* Reach the lowest level. */
    unsigned int nsymbols;
    int lowest  = calc_lowest_level(vmpm, ams, pos);
    int highest = calc_highest_level(vmpm, level_am, pos);

    //debug_message_fnc("level %d, lowest %d, highest %d, pos %d\n", i, lowest, highest, pos);
    if (!arithmodel_order_zero_encode_with_range(level_am, i, lowest, highest))
      fatal("%s: arithmodel_order_zero_encode_with_range() failed.\n", __FUNCTION__);
    nsymbols = arithmodel_order_zero_nsymbols(ams[0]);
    if (s_to_i[(int)vmpm->token[0][j]] == (unsigned int)-1) {
      arithmodel_encode(ams[0], nsymbols);
      s_to_i[(int)vmpm->token[0][j]] = nsymbols;
      arithmodel_encode_bits(bin_am, (int)vmpm->token[0][j], vmpm->bits_per_symbol, 0, 1);
    } else {
      arithmodel_encode(ams[0], s_to_i[(int)vmpm->token[0][j]]);
    }
    stat_message(vmpm, " %d", (int)vmpm->token[0][j]);
    len = 1;
  } else {
    Token_value tv;

    tv = vmpm->token[i][j]->value - 1;
    if (arithmodel_order_zero_nsymbols(ams[i]) > tv) {
      int lowest  = calc_lowest_level(vmpm, ams, pos);
      int highest = calc_highest_level(vmpm, level_am, pos);

      //debug_message_fnc("level %d, lowest %d, highest %d, pos %d\n", i, lowest, highest, pos);
      /* Found. Encode at this level. */
      stat_message(vmpm, "(%d,%d)", i, tv);
      if (!arithmodel_order_zero_encode_with_range(level_am, i, lowest, highest))
	fatal("%s: arithmodel_order_zero_encode_with_range() failed.\n", __FUNCTION__);
      arithmodel_encode(ams[i], tv);
      len = ipow(vmpm->r, i);
    } else if (arithmodel_order_zero_nsymbols(ams[i]) == tv) {
      unsigned int k;

      /* Not found. Encode at lower level. */
      arithmodel_install_symbol(ams[i], 1);
      len = 0;
      for (k = 0; k < vmpm->r; k++)
	len += encode_recursively(vmpm, ams, bin_am, level_am, s_to_i, i - 1, tv * vmpm->r + k, pos + len);
      return len;
    } else {
      fatal("nsymbols = %d < %d = tv\n", arithmodel_order_zero_nsymbols(ams[i]), tv);
    }
  }

  if (pos + len >= ipow(vmpm->r, arithmodel_order_zero_nsymbols(level_am)) &&
      arithmodel_order_zero_nsymbols(level_am) <= vmpm->I + 1) {
    //debug_message_fnc("pos %d len %d sum %d r %d nsymbols %d\n", pos, len, pos + len, vmpm->r, arithmodel_order_zero_nsymbols(level_am));
    arithmodel_install_symbol(level_am, 1);
  }

  return len;
}