blob: 37b11bf5c9b0a717a18ac665fe386c856081aefa [file] [log] [blame]
Jean-Marc Valin8b2ff0d2009-10-17 21:40:10 -04001/* Copyright (c) 2007 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Written by Jean-Marc Valin */
Jean-Marc Valin17e5b802008-02-01 09:02:05 +11004/*
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
Jean-Marc Valin17e5b802008-02-01 09:02:05 +110016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
Conrad Parker52156232008-02-13 16:31:36 +110029#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
Jean-Marc Valin17e5b802008-02-01 09:02:05 +110033#include "celt_header.h"
34#include "os_support.h"
Jean-Marc Valin14f5e7c2008-02-21 23:59:17 +110035#include "modes.h"
Jean-Marc Valin17e5b802008-02-01 09:02:05 +110036
Jean-Marc Valin30f7f812009-10-17 14:35:13 -040037static celt_uint32
38_le_32 (celt_uint32 i)
Conrad Parker52156232008-02-13 16:31:36 +110039{
Jean-Marc Valin30f7f812009-10-17 14:35:13 -040040 celt_uint32 ret=i;
Gregory Maxwell66a57c82009-06-29 14:03:17 -040041#if !defined(__LITTLE_ENDIAN__) && ( defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) )
Conrad Parker52156232008-02-13 16:31:36 +110042 ret = (i>>24);
43 ret += (i>>8) & 0x0000ff00;
44 ret += (i<<8) & 0x00ff0000;
45 ret += (i<<24);
46#endif
47 return ret;
48}
49
Jean-Marc Valinb35807d2011-01-31 13:27:21 -050050int celt_header_init(CELTHeader *header, const CELTMode *m, int frame_size, int channels)
Jean-Marc Valin17e5b802008-02-01 09:02:05 +110051{
Gregory Maxwell66a57c82009-06-29 14:03:17 -040052 if (header==NULL)
53 return CELT_BAD_ARG;
54
Jean-Marc Valin17e5b802008-02-01 09:02:05 +110055 CELT_COPY(header->codec_id, "CELT ", 8);
56 CELT_COPY(header->codec_version, "experimental ", 20);
57
Jean-Marc Valinff96b162011-03-21 11:32:50 -040058 /* FIXME: Set that to zero when we freeze */
59 header->version_id = 0x80001000;
Jean-Marc Valin17e5b802008-02-01 09:02:05 +110060 header->header_size = 56;
Jean-Marc Valin14f5e7c2008-02-21 23:59:17 +110061 header->sample_rate = m->Fs;
Jean-Marc Valin80ed1472009-10-15 21:45:32 -040062 header->nb_channels = channels;
Jean-Marc Valinbce1dd02010-08-27 16:10:39 -040063 /*FIXME: This won't work for variable frame size */
Jean-Marc Valinb35807d2011-01-31 13:27:21 -050064 header->frame_size = frame_size;
Jean-Marc Valin14f5e7c2008-02-21 23:59:17 +110065 header->overlap = m->overlap;
Jean-Marc Valin17e5b802008-02-01 09:02:05 +110066 header->bytes_per_packet = -1;
Conrad Parkerdae5ecc2008-02-13 14:12:51 +110067 header->extra_headers = 0;
Gregory Maxwell66a57c82009-06-29 14:03:17 -040068 return CELT_OK;
Jean-Marc Valin17e5b802008-02-01 09:02:05 +110069}
70
Jean-Marc Valin30f7f812009-10-17 14:35:13 -040071int celt_header_to_packet(const CELTHeader *header, unsigned char *packet, celt_uint32 size)
Jean-Marc Valin17e5b802008-02-01 09:02:05 +110072{
Jean-Marc Valin30f7f812009-10-17 14:35:13 -040073 celt_int32 * h;
Conrad Parker52156232008-02-13 16:31:36 +110074
Gregory Maxwell66a57c82009-06-29 14:03:17 -040075 if ((size < 56) || (header==NULL) || (packet==NULL))
76 return CELT_BAD_ARG; /* FAIL */
Conrad Parker52156232008-02-13 16:31:36 +110077
Jean-Marc Valin17e5b802008-02-01 09:02:05 +110078 CELT_MEMSET(packet, 0, sizeof(*header));
Conrad Parker52156232008-02-13 16:31:36 +110079 /* FIXME: Do it in an alignment-safe manner */
80
81 /* Copy ident and version */
82 CELT_COPY(packet, (unsigned char*)header, 28);
83
84 /* Copy the int32 fields */
Jean-Marc Valin30f7f812009-10-17 14:35:13 -040085 h = (celt_int32*)(packet+28);
Conrad Parker52156232008-02-13 16:31:36 +110086 *h++ = _le_32 (header->version_id);
87 *h++ = _le_32 (header->header_size);
Conrad Parker52156232008-02-13 16:31:36 +110088 *h++ = _le_32 (header->sample_rate);
89 *h++ = _le_32 (header->nb_channels);
Jean-Marc Valin14f5e7c2008-02-21 23:59:17 +110090 *h++ = _le_32 (header->frame_size);
91 *h++ = _le_32 (header->overlap);
Conrad Parker52156232008-02-13 16:31:36 +110092 *h++ = _le_32 (header->bytes_per_packet);
Gregory Maxwell66a57c82009-06-29 14:03:17 -040093 *h = _le_32 (header->extra_headers);
Conrad Parker52156232008-02-13 16:31:36 +110094
Jean-Marc Valin17e5b802008-02-01 09:02:05 +110095 return sizeof(*header);
96}
97
Jean-Marc Valin30f7f812009-10-17 14:35:13 -040098int celt_header_from_packet(const unsigned char *packet, celt_uint32 size, CELTHeader *header)
Jean-Marc Valin17e5b802008-02-01 09:02:05 +110099{
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400100 celt_int32 * h;
Gregory Maxwell66a57c82009-06-29 14:03:17 -0400101
102 if ((size < 56) || (header==NULL) || (packet==NULL))
103 return CELT_BAD_ARG; /* FAIL */
104
105 CELT_MEMSET((unsigned char*)header, 0, sizeof(*header));
106 /* FIXME: Do it in an alignment-safe manner */
107
108 /* Copy ident and version */
109 CELT_COPY((unsigned char*)header, packet, 28);
110
111 /* Copy the int32 fields */
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400112 h = (celt_int32*)(packet+28);
Gregory Maxwell66a57c82009-06-29 14:03:17 -0400113 header->version_id = _le_32(*h++);
114 header->header_size = _le_32(*h++);
115 header->sample_rate = _le_32(*h++);
116 header->nb_channels = _le_32(*h++);
117 header->frame_size = _le_32(*h++);
118 header->overlap = _le_32(*h++);
119 header->bytes_per_packet = _le_32(*h++);
120 header->extra_headers = _le_32(*h);
121
Jean-Marc Valin17e5b802008-02-01 09:02:05 +1100122 return sizeof(*header);
123}
124