gstvorbisdeclib.c 4.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* GStreamer
 * Copyright (C) 2010 Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>
 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
 *   Contact: Stefan Kost <stefan.kost@nokia.com>
 *
 * Tremor modifications <2006>:
 *   Chris Lord, OpenedHand Ltd. <chris@openedhand.com>, http://www.o-hand.com/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
21 22
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
23 24 25 26 27 28
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

29
#include <string.h>
30
#include "gstvorbisdeclib.h"
31
#include "gstvorbiscommon.h"
32 33 34 35

#ifndef TREMOR
/* These samples can be outside of the float -1.0 -- 1.0 range, this
 * is allowed, downstream elements are supposed to clip */
36 37
static void
copy_samples_m (vorbis_sample_t * out, vorbis_sample_t ** in, guint samples,
Wim Taymans's avatar
Wim Taymans committed
38
    gint channels)
39
{
40 41
  memcpy (out, in[0], samples * sizeof (float));
}
42

43 44
static void
copy_samples_s (vorbis_sample_t * out, vorbis_sample_t ** in, guint samples,
Wim Taymans's avatar
Wim Taymans committed
45
    gint channels)
46 47 48 49 50 51 52 53 54 55 56 57 58 59
{
#ifdef GST_VORBIS_DEC_SEQUENTIAL
  memcpy (out, in[0], samples * sizeof (float));
  out += samples;
  memcpy (out, in[1], samples * sizeof (float));
#else
  gint j;

  for (j = 0; j < samples; j++) {
    *out++ = in[0][j];
    *out++ = in[1][j];
  }
#endif
}
60

61 62
static void
copy_samples (vorbis_sample_t * out, vorbis_sample_t ** in, guint samples,
Wim Taymans's avatar
Wim Taymans committed
63
    gint channels)
64
{
65
#ifdef GST_VORBIS_DEC_SEQUENTIAL
66 67
  gint i;

68
  for (i = 0; i < channels; i++) {
69 70
    memcpy (out, in[gst_vorbis_reorder_map[channels - 1][i]],
        samples * sizeof (float));
71 72 73
    out += samples;
  }
#else
74 75
  gint i, j;

76 77
  for (j = 0; j < samples; j++) {
    for (i = 0; i < channels; i++) {
78
      *out++ = in[gst_vorbis_reorder_map[channels - 1][i]][j];
79 80 81 82 83
    }
  }
#endif
}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static void
copy_samples_no_reorder (vorbis_sample_t * out, vorbis_sample_t ** in,
    guint samples, gint channels)
{
#ifdef GST_VORBIS_DEC_SEQUENTIAL
  gint i;

  for (i = 0; i < channels; i++) {
    memcpy (out, in[i], samples * sizeof (float));
    out += samples;
  }
#else
  gint i, j;

  for (j = 0; j < samples; j++) {
    for (i = 0; i < channels; i++) {
      *out++ = in[i][j];
    }
  }
#endif
}

106
CopySampleFunc
107
gst_vorbis_get_copy_sample_func (gint channels)
108 109 110 111 112 113 114 115 116 117
{
  CopySampleFunc f = NULL;

  switch (channels) {
    case 1:
      f = copy_samples_m;
      break;
    case 2:
      f = copy_samples_s;
      break;
118 119 120 121 122 123
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
124 125
      f = copy_samples;
      break;
126 127 128
    default:
      f = copy_samples_no_reorder;
      break;
129 130 131 132 133
  }

  return f;
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#else

/* Taken from Tremor, misc.h */
#ifdef _ARM_ASSEM_
static inline ogg_int32_t
CLIP_TO_15 (ogg_int32_t x)
{
  int tmp;
  asm volatile ("subs	%1, %0, #32768\n\t"
      "movpl	%0, #0x7f00\n\t"
      "orrpl	%0, %0, #0xff\n"
      "adds	%1, %0, #32768\n\t"
      "movmi	%0, #0x8000":"+r" (x), "=r" (tmp)
      ::"cc");

  return (x);
}
#else
static inline ogg_int32_t
CLIP_TO_15 (ogg_int32_t x)
{
  int ret = x;

  ret -= ((x <= 32767) - 1) & (x - 32767);
  ret -= ((x >= -32768) - 1) & (x + 32768);
  return (ret);
}
#endif

static void
164
copy_samples_16_m (vorbis_sample_t * _out, vorbis_sample_t ** _in,
Wim Taymans's avatar
Wim Taymans committed
165
    guint samples, gint channels)
166 167 168 169 170 171 172 173 174 175 176 177
{
  gint16 *out = (gint16 *) _out;
  ogg_int32_t **in = (ogg_int32_t **) _in;
  gint j;

  for (j = 0; j < samples; j++) {
    *out++ = CLIP_TO_15 (in[0][j] >> 9);
  }
}

static void
copy_samples_16_s (vorbis_sample_t * _out, vorbis_sample_t ** _in,
Wim Taymans's avatar
Wim Taymans committed
178
    guint samples, gint channels)
179 180 181 182 183 184 185 186 187 188 189 190 191
{
  gint16 *out = (gint16 *) _out;
  ogg_int32_t **in = (ogg_int32_t **) _in;
  gint j;

  for (j = 0; j < samples; j++) {
    *out++ = CLIP_TO_15 (in[0][j] >> 9);
    *out++ = CLIP_TO_15 (in[1][j] >> 9);
  }
}

static void
copy_samples_16 (vorbis_sample_t * _out, vorbis_sample_t ** _in, guint samples,
Wim Taymans's avatar
Wim Taymans committed
192
    gint channels)
193
{
194 195
  gint16 *out = (gint16 *) _out;
  ogg_int32_t **in = (ogg_int32_t **) _in;
196 197 198 199
  gint i, j;

  for (j = 0; j < samples; j++) {
    for (i = 0; i < channels; i++) {
200
      *out++ = CLIP_TO_15 (in[gst_vorbis_reorder_map[channels - 1][i]][j] >> 9);
201 202 203 204
    }
  }
}

205
CopySampleFunc
206
gst_vorbis_get_copy_sample_func (gint channels)
207
{
208 209
  CopySampleFunc f = NULL;

Wim Taymans's avatar
Wim Taymans committed
210 211 212 213 214 215 216 217 218 219
  switch (channels) {
    case 1:
      f = copy_samples_16_m;
      break;
    case 2:
      f = copy_samples_16_s;
      break;
    default:
      f = copy_samples_16;
      break;
220
  }
221
  return f;
222 223 224
}

#endif