Jean-Marc Valin | 8b2ff0d | 2009-10-17 21:40:10 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2007 CSIRO |
| 2 | Copyright (c) 2007-2009 Xiph.Org Foundation |
| 3 | Written by Jean-Marc Valin */ |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 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 | |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 16 | 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 Parker | 5215623 | 2008-02-13 16:31:36 +1100 | [diff] [blame] | 29 | #ifdef HAVE_CONFIG_H |
| 30 | #include "config.h" |
| 31 | #endif |
| 32 | |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 33 | #include "celt_header.h" |
| 34 | #include "os_support.h" |
Jean-Marc Valin | 14f5e7c | 2008-02-21 23:59:17 +1100 | [diff] [blame] | 35 | #include "modes.h" |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 36 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 37 | static celt_uint32 |
| 38 | _le_32 (celt_uint32 i) |
Conrad Parker | 5215623 | 2008-02-13 16:31:36 +1100 | [diff] [blame] | 39 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 40 | celt_uint32 ret=i; |
Gregory Maxwell | 66a57c8 | 2009-06-29 14:03:17 -0400 | [diff] [blame] | 41 | #if !defined(__LITTLE_ENDIAN__) && ( defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__) ) |
Conrad Parker | 5215623 | 2008-02-13 16:31:36 +1100 | [diff] [blame] | 42 | 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 Valin | b35807d | 2011-01-31 13:27:21 -0500 | [diff] [blame] | 50 | int celt_header_init(CELTHeader *header, const CELTMode *m, int frame_size, int channels) |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 51 | { |
Gregory Maxwell | 66a57c8 | 2009-06-29 14:03:17 -0400 | [diff] [blame] | 52 | if (header==NULL) |
| 53 | return CELT_BAD_ARG; |
| 54 | |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 55 | CELT_COPY(header->codec_id, "CELT ", 8); |
| 56 | CELT_COPY(header->codec_version, "experimental ", 20); |
| 57 | |
Jean-Marc Valin | ff96b16 | 2011-03-21 11:32:50 -0400 | [diff] [blame] | 58 | /* FIXME: Set that to zero when we freeze */ |
| 59 | header->version_id = 0x80001000; |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 60 | header->header_size = 56; |
Jean-Marc Valin | 14f5e7c | 2008-02-21 23:59:17 +1100 | [diff] [blame] | 61 | header->sample_rate = m->Fs; |
Jean-Marc Valin | 80ed147 | 2009-10-15 21:45:32 -0400 | [diff] [blame] | 62 | header->nb_channels = channels; |
Jean-Marc Valin | bce1dd0 | 2010-08-27 16:10:39 -0400 | [diff] [blame] | 63 | /*FIXME: This won't work for variable frame size */ |
Jean-Marc Valin | b35807d | 2011-01-31 13:27:21 -0500 | [diff] [blame] | 64 | header->frame_size = frame_size; |
Jean-Marc Valin | 14f5e7c | 2008-02-21 23:59:17 +1100 | [diff] [blame] | 65 | header->overlap = m->overlap; |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 66 | header->bytes_per_packet = -1; |
Conrad Parker | dae5ecc | 2008-02-13 14:12:51 +1100 | [diff] [blame] | 67 | header->extra_headers = 0; |
Gregory Maxwell | 66a57c8 | 2009-06-29 14:03:17 -0400 | [diff] [blame] | 68 | return CELT_OK; |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 69 | } |
| 70 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 71 | int celt_header_to_packet(const CELTHeader *header, unsigned char *packet, celt_uint32 size) |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 72 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 73 | celt_int32 * h; |
Conrad Parker | 5215623 | 2008-02-13 16:31:36 +1100 | [diff] [blame] | 74 | |
Gregory Maxwell | 66a57c8 | 2009-06-29 14:03:17 -0400 | [diff] [blame] | 75 | if ((size < 56) || (header==NULL) || (packet==NULL)) |
| 76 | return CELT_BAD_ARG; /* FAIL */ |
Conrad Parker | 5215623 | 2008-02-13 16:31:36 +1100 | [diff] [blame] | 77 | |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 78 | CELT_MEMSET(packet, 0, sizeof(*header)); |
Conrad Parker | 5215623 | 2008-02-13 16:31:36 +1100 | [diff] [blame] | 79 | /* 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 Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 85 | h = (celt_int32*)(packet+28); |
Conrad Parker | 5215623 | 2008-02-13 16:31:36 +1100 | [diff] [blame] | 86 | *h++ = _le_32 (header->version_id); |
| 87 | *h++ = _le_32 (header->header_size); |
Conrad Parker | 5215623 | 2008-02-13 16:31:36 +1100 | [diff] [blame] | 88 | *h++ = _le_32 (header->sample_rate); |
| 89 | *h++ = _le_32 (header->nb_channels); |
Jean-Marc Valin | 14f5e7c | 2008-02-21 23:59:17 +1100 | [diff] [blame] | 90 | *h++ = _le_32 (header->frame_size); |
| 91 | *h++ = _le_32 (header->overlap); |
Conrad Parker | 5215623 | 2008-02-13 16:31:36 +1100 | [diff] [blame] | 92 | *h++ = _le_32 (header->bytes_per_packet); |
Gregory Maxwell | 66a57c8 | 2009-06-29 14:03:17 -0400 | [diff] [blame] | 93 | *h = _le_32 (header->extra_headers); |
Conrad Parker | 5215623 | 2008-02-13 16:31:36 +1100 | [diff] [blame] | 94 | |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 95 | return sizeof(*header); |
| 96 | } |
| 97 | |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 98 | int celt_header_from_packet(const unsigned char *packet, celt_uint32 size, CELTHeader *header) |
Jean-Marc Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 99 | { |
Jean-Marc Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 100 | celt_int32 * h; |
Gregory Maxwell | 66a57c8 | 2009-06-29 14:03:17 -0400 | [diff] [blame] | 101 | |
| 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 Valin | 30f7f81 | 2009-10-17 14:35:13 -0400 | [diff] [blame] | 112 | h = (celt_int32*)(packet+28); |
Gregory Maxwell | 66a57c8 | 2009-06-29 14:03:17 -0400 | [diff] [blame] | 113 | 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 Valin | 17e5b80 | 2008-02-01 09:02:05 +1100 | [diff] [blame] | 122 | return sizeof(*header); |
| 123 | } |
| 124 | |