示例#1
0
static VideoDecoder *
init(unsigned int fourcc, void *priv)
{
  VideoDecoder *vdec;
  struct videodecoder_divx *vdm;
  int input_format;

  switch (fourcc) {
  case 0:
  case FCC_div3:
  case FCC_DIV3:
  case FCC_DIV4:
  case FCC_DIV5:
  case FCC_DIV6:
  case FCC_MP41:
  case FCC_MP43:
    debug_message_fnc("Identified as DivX ;-) 3.11\n");
    input_format = 311;
    break;
  case FCC_DIVX:
  case FCC_divx:
    debug_message_fnc("Identified as DivX 4\n");
    input_format = 412;
    break;
  case FCC_DX50:
  case FCC_XVID:
  case FCC_xvid:
    debug_message_fnc("Identified as DivX 5\n");
    input_format = 500;
    break;
  default:
    debug_message_fnc("Not identified as any DivX format: %c%c%c%c(%08X)\n",
		       fourcc        & 0xff,
		      (fourcc >>  8) & 0xff,
		      (fourcc >> 16) & 0xff,
		      (fourcc >> 24) & 0xff,
		       fourcc);
    return NULL;
  }

  if ((vdec = _videodecoder_init()) == NULL)
    return NULL;
  if ((vdec->opaque = vdm = calloc(1, sizeof(*vdm))) == NULL) {
    free(vdec);
    return NULL;
  }
  vdec->name = plugin.name;
  vdec->setup = setup;
  vdec->decode = decode;
  vdec->destroy = destroy;
  vdm->input_format = input_format;

  return vdec;
}
示例#2
0
static AudioDevice *
open_device(void *data, Config *c)
{
  AudioDevice *ad;
  ALSA_data *alsa;
  char *device = data;
  int err;

  if ((ad = calloc(1, sizeof(AudioDevice))) == NULL)
    return NULL;
  ad->c = c;
  if ((alsa = calloc(1, sizeof(ALSA_data))) == NULL) {
    free(ad);
    return NULL;
  }
  ad->private_data = alsa;

  if ((device == NULL) && ((device = config_get_str(c, "/enfle/plugins/audio/alsa/device")) == NULL))
    device = (char *)"default";

  err = snd_output_stdio_attach(&alsa->log, stderr, 0);

  if ((err = snd_pcm_open(&alsa->fd, device, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
    show_message_fnc("error in opening device \"%s\"\n", device);
    return NULL;
  }

  ad->bytes_written = 0;
  ad->opened = 1;

  debug_message_fnc("opened device %s successfully.\n", device);

  return ad;
}
示例#3
0
static int
write_device(AudioDevice *ad, unsigned char *data, int size)
{
  ALSA_data *alsa = (ALSA_data *)ad->private_data;
  snd_pcm_sframes_t r;
  ssize_t unit = snd_pcm_samples_to_bytes(alsa->fd, 1) * ad->channels;
  snd_pcm_uframes_t count = size / unit;

  while (count > 0) {
    if ((r = snd_pcm_writei(alsa->fd, data, count)) == -EAGAIN) {
      //debug_message_fnc(" EAGAIN\n");
      snd_pcm_wait(alsa->fd, 1000);
    } else if (r > 0) {
      //debug_message_fnc(" wrote %d bytes\n", (int)(r * unit));
      ad->bytes_written += r * unit;
      count -= r;
      data += r * unit;
    } else if (r == -EPIPE) {
      debug_message_fnc("EPIPE: ");
      if (snd_pcm_state(alsa->fd) == SND_PCM_STATE_XRUN) {
	if ((r = snd_pcm_prepare(alsa->fd)) < 0) {
	  debug_message("failed\n");
	  warning_fnc("snd_pcm_prepare() failed.");
	} else {
	  debug_message("OK\n");
	}
      }
    } else {
      warning_fnc(" r = %d < 0...\n", (int)r);
    }
  }

  return 1;
}
示例#4
0
static int
load_image(Image *p, Stream *st)
{
  unsigned char *buf, *d;
  unsigned int size;
  int w, h;

  /* Read whole stream into buffer... */
  size = 0;
  buf = NULL;
  {
    unsigned char *tmp;
    int len;
    int bufsize = 65536;

    for (;;) {
      if ((tmp = realloc(buf, bufsize)) == NULL) {
        free(buf);
        return LOAD_ERROR;
      }
      buf = tmp;
      len = stream_read(st, buf + size, bufsize - size);
      size += len;
      if (len < bufsize - size)
        break;
      bufsize += 65536;
    }
  }

  d = WebPDecodeRGB(buf, size, &w, &h);
  if (d == NULL)
    return 0;

  image_width(p) = w;
  image_height(p) = h;
  p->type = _RGB24;
  p->bits_per_pixel = 24;
  p->depth = 24;

  debug_message_fnc("WEBP (%d,%d) %d bytes\n", image_width(p), image_height(p), size);

  image_bpl(p) = (image_width(p) * p->bits_per_pixel) >> 3;
  memory_set(image_image(p), d, _NORMAL, image_bpl(p) * image_height(p), image_bpl(p) * image_height(p));

  return 1;
}
示例#5
0
static int
get_buffer(AVCodecContext *vcodec_ctx, AVFrame *vcodec_picture)
{
  VideoDecoder *vdec = (VideoDecoder *)vcodec_ctx->opaque;
  struct videodecoder_avcodec *vdm = (struct videodecoder_avcodec *)vdec->opaque;
  int width, height;
  Picture_buffer *buf;

  /* alignment */
  width  = (vcodec_ctx->width  + 15) & ~15;
  height = (vcodec_ctx->height + 15) & ~15;

  if (vcodec_ctx->pix_fmt != PIX_FMT_YUV420P ||
      width != vcodec_ctx->width || height != vcodec_ctx->height) {
    debug_message_fnc("avcodec: unsupported frame format, DR1 disabled.\n");
    vdm->vcodec_ctx->get_buffer = avcodec_default_get_buffer;
    vdm->vcodec_ctx->reget_buffer = avcodec_default_reget_buffer;
    vdm->vcodec_ctx->release_buffer = avcodec_default_release_buffer;
    return avcodec_default_get_buffer(vcodec_ctx, vcodec_picture);
  }

  buf = &vdm->picture_buffer[vdm->picture_buffer_count];
  if (buf->base == NULL) {
    int datasize = image_bpl(vdm->p) * image_height(vdm->p);
    int size = sizeof(struct pic_buf) + datasize;
    struct pic_buf *pb;

    if ((buf->base = memory_create()) == NULL) {
      err_message_fnc("No enough memory for Memory object buf->base.\n");
      return -1;
    }

    if ((pb = memory_alloc(buf->base, size)) == NULL) {
      err_message_fnc("Cannot allocate %d bytes.  No enough memory for pic_buf.\n", size);
      return -1;
    }
    pb->idx = vdm->picture_buffer_count;
    pb->mem = buf->base;
    memset(pb->data, 128, datasize);

    buf->pb = pb;
    buf->linesize[0] = image_width(vdm->p);
    buf->linesize[1] = image_width(vdm->p) >> 1;
    buf->linesize[2] = image_width(vdm->p) >> 1;
  }
示例#6
0
int
lzma_decode(void *_dec, unsigned char *output, UINT64 *outsize)
{
  LZMA_Decoder *dec = _dec;
  UINT64 decoded;
  UINT32 kBlockSize = 0x100000;
  int result;

  decoded = 0;
  while (decoded < *outsize) {
    SizeT blocksize = *outsize - decoded;
    SizeT processed;

    if (blocksize > kBlockSize)
      blocksize = kBlockSize;
    processed = 0;
    result = LzmaDecode(&dec->state, &dec->rs->InCallback,
			output + decoded, blocksize,
			&processed);
    switch (result) {
    case 0:
      break;
    case LZMA_RESULT_DATA_ERROR:
      err_message_fnc("Data error.\n");
      return LZMA_DATA_ERROR;
    default:
      err_message_fnc("LzmaDecode returned %d.\n", result);
      return UNKNOWN_ERROR;
    }
    if (processed == 0) {
      *outsize = decoded;
      break;
    }
    debug_message_fnc("LzmaDecode() returned %d, processed = %d.\n", result, processed);
    decoded += processed;
    dec->offset += processed;
  }

  return 0;
}
示例#7
0
文件: url_enfle.c 项目: sina-ht/enfle
URL
url_enfle_open(char *urlstr)
{
  URL_enfle *url;

  if ((url = (URL_enfle *)alloc_url(sizeof(URL_enfle))) == NULL)
    return NULL;

  /* common members */
  URLm(url, type)      = URL_extension_t;
  URLm(url, url_read)  = url_enfle_read;
  URLm(url, url_gets)  = url_enfle_gets;
  URLm(url, url_fgetc) = url_enfle_fgetc;
  URLm(url, url_seek)  = url_enfle_seek;
  URLm(url, url_tell)  = url_enfle_tell;
  URLm(url, url_close) = url_enfle_close;

  /* private members */
  sscanf(urlstr, "enfle:%p", &url->st);
  debug_message_fnc("st = %p\n", url->st);

  return (URL)url;
}
示例#8
0
static void
search_dictionaries(int out, Dlist *dic_list, char *rbuf)
{
  Dlist_data *dd;
  Hash *word_hash;
  char word[SKKSERV_WORD_SIZE];
  char result[SKKSERV_RESULT_SIZE];
  char tmpresult[SKKSERV_RESULT_SIZE];
  char *end;
  int i, p, r, len, rlen, ncandidates;

  if ((end = strchr(rbuf + 1, ' ')) == NULL) {
    rbuf[0] = SKKSERV_S_ERROR;
    write(out, rbuf, strlen(rbuf));
    return;
  }
  len = end - (rbuf + 1);
  memcpy(word, rbuf + 1, len);
  word[len] = '\0';

  word_hash = hash_create(HASH_SIZE);
  rlen = 1;
  ncandidates = 0;
  result[0] = SKKSERV_S_FOUND;
  dlist_iter(dic_list, dd) {
    Dictionary *dic = dlist_data(dd);

    pthread_mutex_lock(&dic->mutex);
    __cdb_findstart(dic);
    if ((r = __cdb_findnext(dic, word, len)) == -1) {
      err_message_fnc("cdb_findnext() failed.\n");
      if (!ncandidates) {
	rbuf[0] = SKKSERV_S_ERROR;
	write(out, rbuf, strlen(rbuf));
      }
      pthread_mutex_unlock(&dic->mutex);
      hash_destroy(word_hash);
      return;
    }
    debug_message_fnc("word %s, len %d, r = %d\n", word, len, r);
    if (r) {
      int dpos = cdb_datapos(&dic->cdb);
      int dlen = cdb_datalen(&dic->cdb);
      debug_message_fnc("%s found, r = %d, dpos = %d, dlen = %d.\n", word, r, dpos, dlen);
      if (rlen + dlen + 2 > SKKSERV_RESULT_SIZE) {
	err_message_fnc("Truncated: %s\n", word);
	r = SKKSERV_RESULT_SIZE - rlen - 2;
      } else {
	r = dlen;
      }
      debug_message_fnc("read %d bytes\n", r);
      if (cdb_read(&dic->cdb, tmpresult, r, dpos) == -1) {
	if (!ncandidates) {
	  err_message_fnc("cdb_read() failed.\n");
	  rbuf[0] = SKKSERV_S_ERROR;
	  write(out, rbuf, strlen(rbuf));
	  pthread_mutex_unlock(&dic->mutex);
	  hash_destroy(word_hash);
	  return;
	} else {
	  result[rlen] = '\0';
	  continue;
	}
      }

      /* Merge */
      p = 0;
      i = 1;
      while (i < r) {
	if (tmpresult[i] == '/') {
	  if (i - p - 1 > 0 &&
	      hash_define_value(word_hash, tmpresult + p + 1, i - p - 1, (void *)1) == 1) {
	    memcpy(result + rlen, tmpresult + p, i - p);
	    rlen += i - p;
	    ncandidates++;
	  }
	  p = i;
	}
	i++;
      }
    }
    pthread_mutex_unlock(&dic->mutex);
  }
示例#9
0
static int
set_params(AudioDevice *ad, AudioFormat *format_p, int *ch_p, unsigned int *rate_p)
{
  int c, f, r;
  AudioFormat format = *format_p;
  int ch = *ch_p;
  unsigned int rate = *rate_p;

  /* format part */
  switch (format) {
  case _AUDIO_FORMAT_MU_LAW:
    f = AFMT_MU_LAW;
    break;
  case _AUDIO_FORMAT_A_LAW:
    f = AFMT_A_LAW;
    break;
  case _AUDIO_FORMAT_ADPCM:
    f = AFMT_IMA_ADPCM;
    break;
  case _AUDIO_FORMAT_U8:
    f = AFMT_U8;
    break;
  case _AUDIO_FORMAT_S8:
    f = AFMT_S8;
    break;
  case _AUDIO_FORMAT_U16_LE:
    f = AFMT_U16_LE;
    break;
  case _AUDIO_FORMAT_U16_BE:
    f = AFMT_U16_BE;
    break;
  case _AUDIO_FORMAT_S16_LE:
    f = AFMT_S16_LE;
    break;
  case _AUDIO_FORMAT_S16_BE:
    f = AFMT_S16_BE;
    break;
#if 0
  case _AUDIO_FORMAT_S32_LE:
    f = AFMT_S32_LE;
    break;
  case _AUDIO_FORMAT_S32_BE:
    f = AFMT_S32_BE;
    break;
#endif
  default:
    show_message_fnc("format %d is invalid.\n", format);
    ad->format = _AUDIO_FORMAT_UNSET;
    *format_p = _AUDIO_FORMAT_UNSET;
    return 0;
  }

  if (ioctl(ad->fd, SNDCTL_DSP_SETFMT, &f) == -1) {
    show_message_fnc("in setting format as %d(to ioctl %d).\n", format, f);
    perror("OSS");
    ad->format = _AUDIO_FORMAT_UNSET;
    return 0;
  }

  ad->bytes_per_sample = 2;
  switch (f) {
  case AFMT_MU_LAW:
    ad->format = _AUDIO_FORMAT_MU_LAW;
    break;
  case AFMT_A_LAW:
    ad->format = _AUDIO_FORMAT_A_LAW;
    break;
  case AFMT_IMA_ADPCM:
    ad->format = _AUDIO_FORMAT_ADPCM;
    break;
  case AFMT_U8:
    ad->format = _AUDIO_FORMAT_U8;
    ad->bytes_per_sample = 1;
    break;
  case AFMT_S8:
    ad->format = _AUDIO_FORMAT_S8;
    ad->bytes_per_sample = 1;
    break;
  case AFMT_U16_LE:
    ad->format = _AUDIO_FORMAT_U16_LE;
    ad->bytes_per_sample = 2;
    break;
  case AFMT_U16_BE:
    ad->format = _AUDIO_FORMAT_U16_BE;
    ad->bytes_per_sample = 2;
    break;
  case AFMT_S16_LE:
    ad->format = _AUDIO_FORMAT_S16_LE;
    ad->bytes_per_sample = 2;
    break;
  case AFMT_S16_BE:
    ad->format = _AUDIO_FORMAT_S16_BE;
    ad->bytes_per_sample = 2;
    break;
#if 0
  case AFMT_S32_LE:
    ad->format = _AUDIO_FORMAT_S32_LE;
    ad->bytes_per_sample = 4;
    break;
  case AFMT_S32_BE:
    ad->format = _AUDIO_FORMAT_S32_BE;
    ad->bytes_per_sample = 4;
    break;
#endif
  case AFMT_MPEG:
    ad->format = _AUDIO_FORMAT_UNSET;
    show_message_fnc("unsupported AFMT_MPEG\n");
    break;
  }

  *format_p = ad->format;

  /* channel part */
  c = ch;
  if (ioctl(ad->fd, SNDCTL_DSP_CHANNELS, &c) == -1) {
    show_message_fnc("in setting channels to %d.\n", ch);
    perror("OSS");
    *ch_p = 0;
    return 0;
  }

  debug_message_fnc("set to %d ch\n", c);

  *ch_p = c;

  /* rate part */
  r = rate;
  if (ioctl(ad->fd, SNDCTL_DSP_SPEED, &r) == -1) {
    show_message_fnc("in setting speed to %d.\n", rate);
    perror("OSS");
    *rate_p = 0;
    return 0;
  }

  debug_message_fnc("set to %d Hz\n", r);

  *rate_p = r;

  ad->channels = ch;
  ad->speed = rate;

  return 1;
}
示例#10
0
static int
load_image(Image *p, Stream *st)
{
  unsigned char buf[BUFSIZE], *pp, *d;
  unsigned int file_size, header_size, image_size, offset_to_image;
  unsigned short int biPlanes;
  unsigned int bytes_per_pal;
  int i, c, c2, y, compress_method;

  if (stream_read(st, buf, 12) != 12)
    return 0;

  file_size = utils_get_little_uint32(&buf[0]);
  offset_to_image = utils_get_little_uint32(&buf[8]);
  if (file_size < offset_to_image)
    return 0;

  if (!stream_read_little_uint32(st, &header_size))
    return 0;
  if (header_size > 64)
    return 0;
  if (stream_read(st, buf, header_size - 4) != (int)(header_size - 4))
    return 0;

  if (header_size >= WIN_BMP_HEADER_SIZE) {
    image_width(p) = utils_get_little_uint32(&buf[0]);
    image_height(p) = utils_get_little_uint32(&buf[4]);
    biPlanes = utils_get_little_uint16(&buf[8]);
    p->bits_per_pixel = utils_get_little_uint16(&buf[10]);
  } else {
    image_width(p) = utils_get_little_uint16(&buf[0]);
    image_height(p) = utils_get_little_uint16(&buf[2]);
    biPlanes = utils_get_little_uint16(&buf[4]);
    p->bits_per_pixel = utils_get_little_uint16(&buf[6]);
  }

  if (biPlanes != 1)
    return 0;
  if (image_width(p) > 10000 || image_height(p) > 10000)
    return 0;

  switch (p->bits_per_pixel) {
  case 1:
    p->type = _BITMAP_MSBFirst; /* XXX: check order */
    p->depth = p->bits_per_pixel;
    break;
  case 4:
    p->type = _INDEX44;
    p->depth = 4;
    break;
  case 8:
    p->type = _INDEX;
    p->depth = 8;
    break;
  case 16:
    p->type = _RGB555;
    p->depth = 16;
    break;
  case 24:
    p->type = _BGR24;
    p->depth = 24;
    break;
  case 32:
    p->type = _BGRA32;
    p->depth = 24;
    break;
  default:
    show_message("bmp: read_header: unknown bpp %d detected.\n", p->bits_per_pixel);
    return 0;
  }

  compress_method = 0;
  if (header_size >= WIN_BMP_HEADER_SIZE) {
    compress_method = utils_get_little_uint16(&buf[12]);
    image_size = utils_get_little_uint32(&buf[16]);
    image_size = image_size; // dummy
  }
  /* other header informations are intentionally ignored */

  if (p->depth <= 8) {
    p->ncolors = 1 << p->depth;
    bytes_per_pal = (header_size >= WIN_BMP_HEADER_SIZE) ? 4 : 3;
    if (p->ncolors * bytes_per_pal > BUFSIZE) {
      err_message_fnc("BUFSIZE is too small.  It must be greater than %d.\n", p->ncolors * bytes_per_pal);
      return 0;
    }
    if (stream_read(st, buf, p->ncolors * bytes_per_pal) != (int)(p->ncolors * bytes_per_pal))
      return 0;
    for (i = 0; i < (int)p->ncolors; i++) {
      p->colormap[i][0] = buf[bytes_per_pal * i + 2];
      p->colormap[i][1] = buf[bytes_per_pal * i + 1];
      p->colormap[i][2] = buf[bytes_per_pal * i + 0];
    }
  } else if (p->depth == 16 && compress_method == 3) {
    /* Read bitmasks */
    if (stream_read(st, buf, 3 * 4) != 3 * 4)
      return 0;
    unsigned int rm = utils_get_little_uint32(buf);
    unsigned int gm = utils_get_little_uint32(buf + 4);
    unsigned int bm = utils_get_little_uint32(buf + 8);
    if (rm == 0xf800)
      p->type = _RGB565;
    else if (rm == 0x7c00)
      p->type = _RGB555;
    else
      warning_fnc("Mask: R %X G %X B %X is not supported\n", rm, gm, bm);
    compress_method = 0;
  }

  image_bpl(p) = (image_width(p) * p->bits_per_pixel) >> 3;
  image_bpl(p) += (4 - (image_bpl(p) % 4)) % 4;

  debug_message_fnc("(%d, %d): bpp %d depth %d compress %d\n", image_width(p), image_height(p), p->bits_per_pixel, p->depth, compress_method);

  if ((d = memory_alloc(image_image(p), image_bpl(p) * image_height(p))) == NULL)
    return 0;

  stream_seek(st, offset_to_image, _SET);
  pp = d + image_bpl(p) * (image_height(p) - 1);

  switch (compress_method) {
  case BI_RGB:
    for (i = (int)(image_height(p) - 1); i >= 0; i--) {
      stream_read(st, pp, image_bpl(p));
      pp -= image_bpl(p);
    }
    break;
  case BI_RLE4:
    if (p->depth != 4)
      show_message("Compressed RI_BLE4 bitmap with depth %d != 4.\n", p->depth);
    /* RLE compressed data */
    /* Not complete. */
    y = image_height(p);
    while ((c = stream_getc(st)) != -1 && y >= 0) {
      if (c != 0) {
        /* code mode */
        c2 = stream_getc(st);
	show_message_fnc("len %d data %d\n", c, c2);
        for (i = 0; i < c; i += 2) {
	  *pp++ = (unsigned char)c2;
        }
      } else {
	if ((c = stream_getc(st)) == 0) {
	  /* line end */
	  show_message_fnc("line end %d\n", y);
	  pp = d + image_bpl(p) * (y - 1);
	  y--;
	} else if (c == 1) {
	  /* image end */
	  break;
	} else if (c == 2) {
	  /* offset */
	  c = stream_getc(st);
	  c2 = stream_getc(st);
	  show_message_fnc("offset %d, %d\n", c, c2);
	  pp += (c - c2 * image_width(p)) / 2;
	} else {
	  /* absolute mode */
	  show_message_fnc("abs len %d\n", c);
	  for (i = 0; i < c; i += 2)
	    *pp++ = (unsigned char)stream_getc(st);
	  if (c % 2 != 0)
	    /* read dummy */
	    c = stream_getc(st);
	}
      }
    }
    break;
  case BI_RLE8:
    if (p->depth != 8)
      show_message("Compressed RI_BLE8 bitmap with depth %d != 8.\n", p->depth);
    /* RLE compressed data */
    y = image_height(p);
    while ((c = stream_getc(st)) != -1 && y >= 0) {
      if (c != 0) {
	/* code mode */
	c2 = stream_getc(st);
	for (i = 0; i < c; i++) {
	  *pp++ = (unsigned char)c2;
	}
      } else {
	if ((c = stream_getc(st)) == 0) {
	  /* line end */
	  pp = d + image_bpl(p) * (y - 1);
	  y--;
	} else if (c == 1) {
	  /* image end */
	  break;
	} else if (c == 2) {
	  /* offset */
	  c = stream_getc(st);
	  c2 = stream_getc(st);
	  pp += (c - c2 * image_width(p));
	} else {
	  /* absolute mode */
	  for (i = 0; i < c; i++)
	    *pp++ = (unsigned char)stream_getc(st);
	  if (c % 2 != 0)
	    /* read dummy */
	    c = stream_getc(st);
	}
      }
    }
    break;
  default:
    show_message("Compressed bitmap (method = %d) not supported.\n", compress_method);
    return 0;
  }

  return 1;
}
示例#11
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);
}
示例#12
0
文件: gifimage.c 项目: sina-ht/enfle
int GIFParseImageBlock(Stream *st, GIF_info *info)
{
  unsigned char c[9];
  GIF_id *id;
  char *tmp;

  if ((tmp = (char *)calloc(1, sizeof(GIF_image))) == NULL) {
    info->err = (char *)"No enough memory for image block";
    return PARSE_ERROR;
  }

  if (info->top == NULL)
    info->top = info->image = (GIF_image *)tmp;
  else {
    info->image->next = (GIF_image *)tmp;
    info->image = info->image->next;
    info->image->next = (GIF_image *)NULL;
  }
  info->image->gc = info->recent_gc;
  if ((info->image->id = id = calloc(1, sizeof(GIF_id))) == NULL) {
    info->err = (char *)"No enough memory for image descriptor";
    return PARSE_ERROR;
  }

  stream_read(st, c, 9);
  id->left   = (c[1] << 8) + c[0];
  id->top    = (c[3] << 8) + c[2];
  id->width  = (c[5] << 8) + c[4];
  id->height = (c[7] << 8) + c[6];
  id->lct_follows = c[8] & (1 << 7);
  id->interlace = c[8] & (1 << 6);
  id->lct_sorted = c[8] & (1 << 5);
  id->depth = (c[8] & 7) + 1;

  debug_message_fnc("(%d, %d) - (%d, %d)\n",
		    id->left, id->top, id->left + id->width, id->top + id->height);

  /* load local color table */
  if (id->lct_follows) {
    int i, ncell;
    GIF_ct *lct;
    GIF_ccell *lccell;

    debug_message_fnc("Load local color table\n");

    if ((id->lct = lct = (GIF_ct *)malloc(sizeof(GIF_ct))) == NULL) {
      info->err = (char *)"No enough memory for local color table";
      return PARSE_ERROR;
    }

    ncell = lct->nentry = 1 << id->depth;
    if ((lccell = (GIF_ccell *)malloc(sizeof(GIF_ccell) * ncell)) == NULL) {
      info->err = (char *)"No enough memory for local color cell";
      return PARSE_ERROR;
    }

    if ((lct->cell = (GIF_ccell **)malloc(sizeof(GIF_ccell *) * ncell)) == NULL) {
      info->err = (char *)"No enough memory for local color cell pointer";
      return PARSE_ERROR;
    }

    for (i = 0; i < lct->nentry; i++)
      lct->cell[i] = lccell + i;

    stream_read(st, (unsigned char *)lccell, sizeof(GIF_ccell) * ncell);
  }

  return GIFDecodeImage(st, info);
}
示例#13
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);
}
示例#14
0
static void
encode(VMPM *vmpm)
{
  Arithcoder *ac;
  Arithmodel **ams;
  Arithmodel *bin_am;
  Arithmodel *level_am;
  unsigned int j, pos, nsymbols, *s_to_i;
  int i, match_found;

  //debug_message("Recur3: %s()\n", __FUNCTION__);

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

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

  match_found = 0;
  for (i = vmpm->I; i >= 1; i--) {
    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;
    }
  }

  fputc(i, vmpm->outfile);
  vmpm->I = i;

  if ((ams = calloc(vmpm->I + 1, sizeof(Arithmodel **))) == NULL)
    memory_error(NULL, MEMORY_ERROR);

  for (i = 0; i <= (int)vmpm->I; i++) {
    ams[i] = arithmodel_order_zero_create();
    arithmodel_encode_init(ams[i], ac);
    arithmodel_order_zero_reset(ams[i], 0, 0);
  }
  arithmodel_order_zero_reset(ams[0], 0, 1);

  bin_am = arithmodel_order_zero_create();
  arithmodel_encode_init(bin_am, ac);
  arithmodel_order_zero_reset(bin_am, 0, 0);
  arithmodel_install_symbol(bin_am, 1);
  arithmodel_install_symbol(bin_am, 1);

  level_am = arithmodel_order_zero_create();
  arithmodel_encode_init(level_am, ac);
  arithmodel_order_zero_reset(level_am, 0, 0);
  arithmodel_install_symbol(level_am, 1);

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

  pos = 0;
  for (j = 0; j < vmpm->token_index[vmpm->I]; j++)
    pos += encode_recursively(vmpm, ams, bin_am, level_am, s_to_i, vmpm->I, j, pos);
  for (i = vmpm->I - 1; i >= 0; i--) {
    unsigned int n = (vmpm->newtoken[i + 1] - 1) * vmpm->r;

    for (j = n; j < vmpm->token_index[i]; j++)
      pos += encode_recursively(vmpm, ams, bin_am, level_am, s_to_i, i, j, pos);
  }
  stat_message(vmpm, "\n%d symbols encoded.\n", pos);

  free(s_to_i);

  for (i = 0; i <= (int)vmpm->I; i++)
    arithmodel_encode_final(ams[i]);
  arithmodel_encode_final(level_am);
  arithcoder_encode_final(ac);

  for (i = 0; i <= (int)vmpm->I; i++)
    arithmodel_destroy(ams[i]);
  arithmodel_destroy(level_am);
  arithcoder_destroy(ac);
}
示例#15
0
static int
set_params(AudioDevice *ad, AudioFormat *format_p, int *ch_p, unsigned int *rate_p)
{
  ALSA_data *alsa = (ALSA_data *)ad->private_data;
  int r, err;
  snd_pcm_format_t f;
  AudioFormat format = *format_p;
  snd_pcm_hw_params_t *hwparams;
  snd_pcm_sw_params_t *swparams;
  int ch = *ch_p;
  unsigned int rate = *rate_p;
  unsigned int buffer, period;

  snd_pcm_hw_params_alloca(&hwparams);
  snd_pcm_sw_params_alloca(&swparams);

  ad->format = _AUDIO_FORMAT_UNSET;
  *format_p = _AUDIO_FORMAT_UNSET;
  *ch_p = 0;
  *rate_p = 0;

  /* format part */
  switch (format) {
  case _AUDIO_FORMAT_MU_LAW:
    f = SND_PCM_FORMAT_MU_LAW;
    break;
  case _AUDIO_FORMAT_A_LAW:
    f = SND_PCM_FORMAT_A_LAW;
    break;
  case _AUDIO_FORMAT_ADPCM:
    f = SND_PCM_FORMAT_IMA_ADPCM;
    break;
  case _AUDIO_FORMAT_U8:
    f = SND_PCM_FORMAT_U8;
    break;
  case _AUDIO_FORMAT_S8:
    f = SND_PCM_FORMAT_S8;
    break;
  case _AUDIO_FORMAT_U16_LE:
    f = SND_PCM_FORMAT_U16_LE;
    break;
  case _AUDIO_FORMAT_U16_BE:
    f = SND_PCM_FORMAT_U16_BE;
    break;
  case _AUDIO_FORMAT_S16_LE:
    f = SND_PCM_FORMAT_S16_LE;
    break;
  case _AUDIO_FORMAT_S16_BE:
    f = SND_PCM_FORMAT_S16_BE;
    break;
#if 0
  case _AUDIO_FORMAT_S32_LE:
    f = SND_PCM_FORMAT_U32_LE;
    break;
  case _AUDIO_FORMAT_S32_BE:
    f = SND_PCM_FORMAT_S32_BE;
    break;
#endif
  default:
    show_message_fnc("format %d is invalid.\n", format);
    return 0;
  }

  if ((err = snd_pcm_hw_params_any(alsa->fd, hwparams)) < 0) {
    show_message_fnc("snd_pcm_hw_params_any() failed.\n");
    return 0;
  }

  if ((err = snd_pcm_hw_params_set_access(alsa->fd, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
    show_message_fnc("snd_pcm_hw_params_set_access() failed.\n");
    return 0;
  }

  if ((err = snd_pcm_hw_params_set_format(alsa->fd, hwparams, f)) < 0) {
    show_message_fnc("snd_pcm_hw_params_set_format() failed.\n");
    return 0;
  }
  ad->format = format;
  *format_p = format;

  /* channel part */
  if ((err = snd_pcm_hw_params_set_channels(alsa->fd, hwparams, ch)) < 0) {
    show_message_fnc("snd_pcm_hw_params_set_channels() failed.\n");
    return 0;
  }
  *ch_p = ch;

  /* rate part */
  if ((err = snd_pcm_hw_params_set_rate_near(alsa->fd, hwparams, &rate, 0)) < 0) {
    show_message_fnc("snd_pcm_hw_params_set_rate_near() failed.\n");
    return 0;
  }
  *rate_p = rate;

  /* buffer & period */
  buffer = 500000;
  period = 500000 / 4;
  if ((err = snd_pcm_hw_params_set_buffer_time_near(alsa->fd, hwparams, &buffer, 0)) < 0) {
    show_message_fnc("snd_pcm_hw_params_set_buffer_near() failed.\n");
    return 0;
  }
  r = snd_pcm_hw_params_get_buffer_size(hwparams, &alsa->buffer_size);
  if ((err = snd_pcm_hw_params_set_period_time_near(alsa->fd, hwparams, &period, 0)) < 0) {
    show_message_fnc("snd_pcm_hw_params_set_period_near() failed.\n");
    return 0;
  }
  r = snd_pcm_hw_params_get_period_size(hwparams, &alsa->period_size, 0);

  if ((err = snd_pcm_hw_params(alsa->fd, hwparams)) < 0) {
    perror("snd_pcm_hw_params");
    err_message_fnc("snd_pcm_hw_params() failed.\n");
    snd_pcm_hw_params_dump(hwparams, alsa->log);
    return 0;
  }

  r = snd_pcm_hw_params_get_channels(hwparams, &ad->channels);
  r = snd_pcm_hw_params_get_rate(hwparams, &ad->speed, 0);
  r = r; // dummy

#ifdef DEBUG
  {
    snd_pcm_format_t form;

    debug_message_fnc("format ");
    r = snd_pcm_hw_params_get_format(hwparams, &form);
    switch (form) {
    case SND_PCM_FORMAT_MU_LAW:    debug_message("MU_LAW "); break;
    case SND_PCM_FORMAT_A_LAW:     debug_message("A_LAW "); break;
    case SND_PCM_FORMAT_IMA_ADPCM: debug_message("ADPCM "); break;
    case SND_PCM_FORMAT_U8:        debug_message("U8 "); break;
    case SND_PCM_FORMAT_S8:        debug_message("S8 "); break;
    case SND_PCM_FORMAT_U16_LE:    debug_message("U16LE "); break;
    case SND_PCM_FORMAT_U16_BE:    debug_message("U16BE "); break;
    case SND_PCM_FORMAT_S16_LE:    debug_message("S16LE "); break;
    case SND_PCM_FORMAT_S16_BE:    debug_message("S16BE "); break;
#if 0
    case SND_PCM_FORMAT_U32_LE:    debug_message("U32LE "); break;
    case SND_PCM_FORMAT_S32_BE:    debug_message("S32BE "); break;
#endif
    default:                       debug_message("UNKNOWN "); break;
    }

    debug_message("%d ch %d Hz buffer %ld period %ld OK\n", ad->channels, ad->speed, alsa->buffer_size, alsa->period_size);
  }
#endif

  /* sw_params */
  if ((err = snd_pcm_sw_params_current(alsa->fd, swparams)) < 0) {
    show_message_fnc("snd_pcm_sw_params_any() failed.\n");
    return 0;
  }
  if ((err = snd_pcm_sw_params(alsa->fd, swparams)) < 0) {
    show_message_fnc("snd_pcm_sw_params() failed.\n");
    return 0;
  }

  debug_message_fnc("1 sample -> %ld bytes\n", snd_pcm_samples_to_bytes(alsa->fd, 1));

  return 1;
}