~ubuntu-branches/ubuntu/quantal/gst-plugins-bad-multiverse0.10/quantal

« back to all changes in this revision

Viewing changes to gst/mve/mveaudioenc.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2007-05-03 19:52:54 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20070503195254-4dovmz251nut3yrt
Tags: 0.10.4+cvs20070502-1
* New CVS snapshot.
* debian/rules,
  debian/build-deps.in:
  + Update build dependencies.
* debian/gstreamer-plugins-bad-multiverse.install:
  + Add x264 plugin.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Interplay MVE audio compressor
 
3
 * Copyright (C) 2003, 2004 Alexander Belyakov <abel@krasu.ru>
 
4
 * Copyright (C) 2006 Jens Granseuer <jensgr@gmx.net>
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Library General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Library General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Library General Public
 
17
 * License along with this library; if not, write to the
 
18
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
 * Boston, MA 02111-1307, USA.
 
20
 */
 
21
 
 
22
#include <math.h>
 
23
#include <gst/gst.h>
 
24
 
 
25
static const gint32 dec_table[256] =
 
26
    { 
 
 
b'0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, '
 
 
b'16, 17, 18, 19,'
 
27
  20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 
 
 
b'32, 33, 34, 35, 36, 37,'
 
28
  38, 39, 40, 41, 42, 43, 47, 51, 56, 61, 
 
 
b'66, 72, 79, 86, 94, 102, 112,'
 
29
  122, 133, 145, 158, 173, 189, 206, 225, 245, 
 
 
b'267, 292, 318, 348, 379,'
 
30
  414, 452, 493, 538, 587, 640, 699, 763, 832, 908, 991, 
 
 
b'1081, 1180, 1288,'
 
31
  1405, 1534, 1673, 1826, 1993, 2175, 2373, 2590, 2826, 3084, 3365, 3672,
 
32
  4008, 
 
 
b'4373, 4772, 5208, 5683, 6202, 6767, 7385, 8059, 8794, 9597, 10472,'
 
33
  11428, 12471, 13609, 14851, 16206, 
 
 
b'17685, 19298, 21060, 22981, 25078,'
 
34
  27367, 29864, 32589, 35563, 38808, 42350, 46214, 50431, 55033, 60055,
 
35
  65535, 
 
 
b'1, -65535, -60055, -55033, -50431, -46214, -42350, -38808, -35563,'
 
36
  -32589, -29864, -27367, -25078, -22981, -21060, -19298, 
0
 
17685, -16206,
 
37
  -14851, -13609, -12471, -11428, -10472, -9597, -8794, -8059, -7385, -6767,
 
38
  -6202, -5683, -5208, -4772, 
1
 
4373, -4008, -3672, -3365, -3084, -2826,
 
39
  -2590, -2373, -2175, -1993, -1826, -1673, -1534, -1405, -1288, -1180,
 
40
  
2
 
1081, -991, -908, -832, -763, -699, -640, -587, -538, -493, -452, -414,
 
41
  -379, -348, -318, -292, 
3
 
267, -245, -225, -206, -189, -173, -158, -145,
 
42
  -133, -122, -112, -102, -94, -86, -79, -72, 
4
 
66, -61, -56, -51, -47, -43,
 
43
  -42, -41, -40, -39, -38, -37, -36, -35, -34, -33, 
5
 
32, -31, -30, -29,
 
44
  -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, 
6
 
16, -15,
 
45
  -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1 
 
46
};
 
47
 
 
48
 
 
49
/* This value could be non-optimal. Without knowledge of the value
 
50
   distribution in the real signal, the actual optimum cannot be evaluated.
 
51
   Should be somewhere between 11.458 and 11.542. */ 
 
52
static const gdouble DPCM_SCALE = 11.5131;
 
53
 
 
 
b'static gint8'
 
54
mve_enc_delta (guint n)
 
55
{
 
56
  if (n < 44)
 
57
    return n;
 
58
  return floor (DPCM_SCALE * log (n));
 
59
}
 
60
 
 
61
gint
 
62
mve_compress_audio (guint8 * dest, const guint8 * src, guint16 len,
 
63
    guint8 channels)
 
64
{
 
65
  gint16 prev[2], s;
 
66
  gint delta, real_res;
 
67
  gint cur_chan;
 
68
  guint8 v;
 
69
 
 
70
  for (cur_chan = 0; cur_chan < channels; ++cur_chan) {
 
71
    prev[cur_chan] = GST_READ_UINT16_LE (src);
 
72
    GST_WRITE_UINT16_LE (dest, prev[cur_chan]);
 
73
    src += 2;
 
74
    dest += 2;
 
75
    len -= 2;
 
76
  }
 
77
 
 
78
  cur_chan = 0;
 
79
  while (len > 0) {
 
80
    s = GST_READ_UINT16_LE (src);
 
81
    src += 2;
 
82
 
 
83
    delta = s - prev[cur_chan];
 
84
    
 
 
b'if (delta >= 0)'
 
85
      
 
 
b'v = mve_enc_delta (delta);'
 
86
    
 
87
    else
 
88
      
 
 
b'v = 256 - mve_enc_delta (-delta);'
 
89
    
 
 
b'real_res = dec_table[v] + prev[cur_chan];'
 
90
    
 
 
b'if (real_res < -32768 || real_res > 32767) {'
 
91
      
 
92
          /* correct overflow */ 
 
93
          /* GST_DEBUG ("co:%d + %d = %d -> new v:%d, dec_table:%d will be %d",
 
94
             prev[cur_chan], dec_table[v], real_res,
 
95
             v, dec_table[v], prev[cur_chan]+dec_table[v]); */
 
96
          if (s > 0) {
 
97
        
 
 
b'if (real_res > 32767)'
 
98
          --v;
 
99
      
 
 
b'} else {'
 
100
        
 
 
b'if (real_res < -32768)'
 
101
          ++v;
 
102
      
 
 
b'}'
 
103
 
 
104
      real_res = dec_table[v] + prev[cur_chan];
 
105
    
 
 
b'}'
 
106
 
 
107
    if (G_UNLIKELY (abs (real_res - s) > 32767)) {
 
108
      GST_ERROR ("sign loss left unfixed in audio stream, deviation:%d",
 
109
          real_res - s);
 
110
      return -1;
 
111
    }
 
112
    
 
 
b'*dest++ = v;'
 
113
    
7
 
-len;
 
114
    /* use previous output instead of input. That way output will not go too far from input. */ 
 
115
        prev[cur_chan] += dec_table[v];
 
116
    cur_chan = channels - 1 - cur_chan;
 
117
  
 
 
b'}'
 
118
 
 
119
  return 0;
 
120
}