~ubuntu-branches/ubuntu/raring/mumble/raring

« back to all changes in this revision

Viewing changes to celt-0.11.0-src/libcelt/header.c

  • Committer: Bazaar Package Importer
  • Author(s): Thorvald Natvig, Patrick Matthäi, Thorvald Natvig
  • Date: 2011-02-19 22:58:58 UTC
  • mfrom: (9.1.15 sid)
  • Revision ID: james.westby@ubuntu.com-20110219225858-0xlftrf4z1z4jt9e
Tags: 1.2.3-1
[ Patrick Matthäi ]
* Do not build with non existant libpulse-dev on hurd-i386.

[ Thorvald Natvig ]
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2007 CSIRO
 
2
   Copyright (c) 2007-2009 Xiph.Org Foundation
 
3
   Written by Jean-Marc Valin */
 
4
/*
 
5
   Redistribution and use in source and binary forms, with or without
 
6
   modification, are permitted provided that the following conditions
 
7
   are met:
 
8
   
 
9
   - Redistributions of source code must retain the above copyright
 
10
   notice, this list of conditions and the following disclaimer.
 
11
   
 
12
   - Redistributions in binary form must reproduce the above copyright
 
13
   notice, this list of conditions and the following disclaimer in the
 
14
   documentation and/or other materials provided with the distribution.
 
15
   
 
16
   - Neither the name of the Xiph.org Foundation nor the names of its
 
17
   contributors may be used to endorse or promote products derived from
 
18
   this software without specific prior written permission.
 
19
   
 
20
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
21
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
22
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
23
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
 
24
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 
25
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
26
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
27
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
28
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
29
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 
30
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
31
*/
 
32
 
 
33
#ifdef HAVE_CONFIG_H
 
34
#include "config.h"
 
35
#endif
 
36
 
 
37
#include "celt_header.h"
 
38
#include "os_support.h"
 
39
#include "modes.h"
 
40
 
 
41
static  celt_uint32
 
42
_le_32 (celt_uint32 i)
 
43
{
 
44
   celt_uint32 ret=i;
 
45
#if !defined(__LITTLE_ENDIAN__) && ( defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) )
 
46
   ret =  (i>>24);
 
47
   ret += (i>>8) & 0x0000ff00;
 
48
   ret += (i<<8) & 0x00ff0000;
 
49
   ret += (i<<24);
 
50
#endif
 
51
   return ret;
 
52
}
 
53
 
 
54
int celt_header_init(CELTHeader *header, const CELTMode *m, int frame_size, int channels)
 
55
{
 
56
   if (header==NULL)
 
57
     return CELT_BAD_ARG;        
 
58
 
 
59
   CELT_COPY(header->codec_id, "CELT    ", 8);
 
60
   CELT_COPY(header->codec_version, "experimental        ", 20);
 
61
 
 
62
   celt_mode_info(m, CELT_GET_BITSTREAM_VERSION, &header->version_id);
 
63
   header->header_size = 56;
 
64
   header->sample_rate = m->Fs;
 
65
   header->nb_channels = channels;
 
66
   /*FIXME: This won't work for variable frame size */
 
67
   header->frame_size = frame_size;
 
68
   header->overlap = m->overlap;
 
69
   header->bytes_per_packet = -1;
 
70
   header->extra_headers = 0;
 
71
   return CELT_OK;
 
72
}
 
73
 
 
74
int celt_header_to_packet(const CELTHeader *header, unsigned char *packet, celt_uint32 size)
 
75
{
 
76
   celt_int32 * h;
 
77
 
 
78
   if ((size < 56) || (header==NULL) || (packet==NULL))
 
79
     return CELT_BAD_ARG; /* FAIL */
 
80
 
 
81
   CELT_MEMSET(packet, 0, sizeof(*header));
 
82
   /* FIXME: Do it in an alignment-safe manner */
 
83
 
 
84
   /* Copy ident and version */
 
85
   CELT_COPY(packet, (unsigned char*)header, 28);
 
86
 
 
87
   /* Copy the int32 fields */
 
88
   h = (celt_int32*)(packet+28);
 
89
   *h++ = _le_32 (header->version_id);
 
90
   *h++ = _le_32 (header->header_size);
 
91
   *h++ = _le_32 (header->sample_rate);
 
92
   *h++ = _le_32 (header->nb_channels);
 
93
   *h++ = _le_32 (header->frame_size);
 
94
   *h++ = _le_32 (header->overlap);
 
95
   *h++ = _le_32 (header->bytes_per_packet);
 
96
   *h   = _le_32 (header->extra_headers);
 
97
 
 
98
   return sizeof(*header);
 
99
}
 
100
 
 
101
int celt_header_from_packet(const unsigned char *packet, celt_uint32 size, CELTHeader *header)
 
102
{
 
103
   celt_int32 * h;
 
104
 
 
105
   if ((size < 56) || (header==NULL) || (packet==NULL))
 
106
     return CELT_BAD_ARG; /* FAIL */
 
107
 
 
108
   CELT_MEMSET((unsigned char*)header, 0, sizeof(*header));
 
109
   /* FIXME: Do it in an alignment-safe manner */
 
110
 
 
111
   /* Copy ident and version */
 
112
   CELT_COPY((unsigned char*)header, packet, 28);
 
113
 
 
114
   /* Copy the int32 fields */
 
115
   h = (celt_int32*)(packet+28);
 
116
   header->version_id       = _le_32(*h++);
 
117
   header->header_size      = _le_32(*h++);
 
118
   header->sample_rate      = _le_32(*h++);
 
119
   header->nb_channels      = _le_32(*h++);
 
120
   header->frame_size       = _le_32(*h++);
 
121
   header->overlap          = _le_32(*h++);
 
122
   header->bytes_per_packet = _le_32(*h++);
 
123
   header->extra_headers    = _le_32(*h);
 
124
 
 
125
   return sizeof(*header);
 
126
}
 
127