1
/* Copyright (c) 2007 CSIRO
2
Copyright (c) 2007-2009 Xiph.Org Foundation
3
Written by Jean-Marc Valin */
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions
9
- Redistributions of source code must retain the above copyright
10
notice, this list of conditions and the following disclaimer.
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.
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.
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.
37
#include "celt_header.h"
38
#include "os_support.h"
42
_le_32 (celt_uint32 i)
45
#if !defined(__LITTLE_ENDIAN__) && ( defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) )
47
ret += (i>>8) & 0x0000ff00;
48
ret += (i<<8) & 0x00ff0000;
54
int celt_header_init(CELTHeader *header, const CELTMode *m, int frame_size, int channels)
59
CELT_COPY(header->codec_id, "CELT ", 8);
60
CELT_COPY(header->codec_version, "experimental ", 20);
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;
74
int celt_header_to_packet(const CELTHeader *header, unsigned char *packet, celt_uint32 size)
78
if ((size < 56) || (header==NULL) || (packet==NULL))
79
return CELT_BAD_ARG; /* FAIL */
81
CELT_MEMSET(packet, 0, sizeof(*header));
82
/* FIXME: Do it in an alignment-safe manner */
84
/* Copy ident and version */
85
CELT_COPY(packet, (unsigned char*)header, 28);
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);
98
return sizeof(*header);
101
int celt_header_from_packet(const unsigned char *packet, celt_uint32 size, CELTHeader *header)
105
if ((size < 56) || (header==NULL) || (packet==NULL))
106
return CELT_BAD_ARG; /* FAIL */
108
CELT_MEMSET((unsigned char*)header, 0, sizeof(*header));
109
/* FIXME: Do it in an alignment-safe manner */
111
/* Copy ident and version */
112
CELT_COPY((unsigned char*)header, packet, 28);
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);
125
return sizeof(*header);