/*
 * Purpose: Helper functions used by audiofmt.c
 */
/*
 * This file is part of Open Sound System
 *
 * Copyright (C) 4Front Technologies 1996-2007.
 *
 * This software is released under CDDL 1.0 source license for operating systems
 * that have their full source code available under the BSD or CDDL licenses.
 *
 * See the COPYING file included in the main directory of this source
 * distribution. Please contact sales@opensound.com for further info.
 */

/*
 * NOTE! This is not the development version of this file. Please download the
 * sources of the latest Open Sound System version from 
 * http://developer.opensound.com/sources if you plan to make any changes
 * to the software.
 */

static int
cnv_F8bits_T16bits (adev_p adev, dmap_p dmap, void **srcp, int *srcl,
		    void **tgtp, sample_parms * source, sample_parms * target)
{
  unsigned char *p1 = *srcp;
  short *p2 = *tgtp;
  int len = *srcl;
  int i, l;

  VMEM_CHECK (p1, len);
  VMEM_CHECK (p2, len * 2 / 1);

  l = len / 1;
  for (i = 0; i < l; i++)
    p2[i] = ((int) p1[i] - 128) << 8;

  *srcl = len * 2 / 1;
  *srcp = p2;
  *tgtp = p1;

  return 0;
}

static int
cnv_F8bits_T32bits (adev_p adev, dmap_p dmap, void **srcp, int *srcl,
		    void **tgtp, sample_parms * source, sample_parms * target)
{
  unsigned char *p1 = *srcp;
  int *p2 = *tgtp;
  int len = *srcl;
  int i, l;

  VMEM_CHECK (p1, len);
  VMEM_CHECK (p2, len * 4 / 1);

  l = len / 1;
  for (i = 0; i < l; i++)
    p2[i] = ((int) p1[i] - 128) << 24;

  *srcl = len * 4 / 1;
  *srcp = p2;
  *tgtp = p1;

  return 0;
}

static int
cnv_F16bits_T8bits (adev_p adev, dmap_p dmap, void **srcp, int *srcl,
		    void **tgtp, sample_parms * source, sample_parms * target)
{
  short *p1 = *srcp;
  unsigned char *p2 = *tgtp;
  int len = *srcl;
  int i, l;

  VMEM_CHECK (p1, len);
  VMEM_CHECK (p2, len * 1 / 2);

  l = len / 2;
  for (i = 0; i < l; i++)
    p2[i] = (((int) p1[i]) >> 8) + 128;

  *srcl = len * 1 / 2;
  *srcp = p2;
  *tgtp = p1;

  return 0;
}

static int
cnv_F16bits_T32bits (adev_p adev, dmap_p dmap, void **srcp, int *srcl,
		     void **tgtp, sample_parms * source,
		     sample_parms * target)
{
  short *p1 = *srcp;
  int *p2 = *tgtp;
  int len = *srcl;
  int i, l;

  VMEM_CHECK (p1, len);
  VMEM_CHECK (p2, len * 4 / 2);

  l = len / 2;
  for (i = 0; i < l; i++)
    p2[i] = ((int) p1[i]) << 16;

  *srcl = len * 4 / 2;
  *srcp = p2;
  *tgtp = p1;

  return 0;
}

static int
cnv_F32bits_T8bits (adev_p adev, dmap_p dmap, void **srcp, int *srcl,
		    void **tgtp, sample_parms * source, sample_parms * target)
{
  int *p1 = *srcp;
  unsigned char *p2 = *tgtp;
  int len = *srcl;
  int i, l;

  VMEM_CHECK (p1, len);
  VMEM_CHECK (p2, len * 1 / 4);

  l = len / 4;
  for (i = 0; i < l; i++)
    p2[i] = (((int) p1[i]) >> 24) + 128;

  *srcl = len * 1 / 4;
  *srcp = p2;
  *tgtp = p1;

  return 0;
}

static int
cnv_F32bits_T16bits (adev_p adev, dmap_p dmap, void **srcp, int *srcl,
		     void **tgtp, sample_parms * source,
		     sample_parms * target)
{
  int *p1 = *srcp;
  short *p2 = *tgtp;
  int len = *srcl;
  int i, l;

  VMEM_CHECK (p1, len);
  VMEM_CHECK (p2, len * 2 / 4);

  l = len / 4;
  for (i = 0; i < l; i++)
    p2[i] = (((int) p1[i]) >> 16);

  *srcl = len * 2 / 4;
  *srcp = p2;
  *tgtp = p1;

  return 0;
}
