1 /**
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <string.h>
17 #include <stdlib.h>
18 #define REF_COUNT     1
19 #define DECODE_PACKET 1
20 
21 extern "C" {
22 #include <Tremolo/codec_internal.h>
23 
24 int _vorbis_unpack_books(vorbis_info *vi, oggpack_buffer *opb);
25 int _vorbis_unpack_info(vorbis_info *vi, oggpack_buffer *opb);
26 int _vorbis_unpack_comment(vorbis_comment *vc, oggpack_buffer *opb);
27 }
28 
29 const uint8_t packInfoData[] = { 0x00, 0x00, 0x00, 0x00, 0x02, 0x80, 0xBB, 0x00,
30         0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31         0x00, 0xBB, 0x01, 0xFF, 0xFF, 0xFF, 0xFF };
32 
33 unsigned char unpackBookData[] = { 0x00, 0x42, 0x43, 0x56, 0x1E, 0x00, 0x10,
34         0x00, 0x00, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x10, 0x0A, 0xFF, 0x00, 0x00,
35         0x00, 0x06, 0xD0, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x1D, 0x00, 0x00, 0x00,
36         0x2C, 0x00, 0x03, 0x3C, 0x51, 0x04, 0x34, 0x4F, 0x04, 0x00, 0x40, 0x00,
37         0x00, 0x00, 0x00, 0x00, 0xCB, 0x00, 0x40, 0x00, 0x00, 0x01, 0x4F, 0xF4,
38         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xFF, 0xFF, 0xFF, 0xFF };
42 
43 unsigned char bufData[] = { 0x00, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00, 0xE7,
44         0x00, 0x00, 0xE9, 0x00 };
45 
makeBitReader(const void * data,size_t size,ogg_buffer * buf,ogg_reference * ref,oggpack_buffer * bits)46 static void makeBitReader(const void *data, size_t size, ogg_buffer *buf,
47                           ogg_reference *ref, oggpack_buffer *bits) {
48     buf->data = (uint8_t *) data;
49     buf->size = size;
50     buf->refcount = REF_COUNT;
51 
52     ref->buffer = buf;
53     ref->length = size;
54     oggpack_readinit(bits, ref);
55 }
56 
main()57 int main() {
58     ogg_buffer buf;
59     ogg_reference ref;
60     oggpack_buffer bits;
61 
62     memset(&buf, 0, sizeof(ogg_buffer));
63     memset(&ref, 0, sizeof(ogg_reference));
64     memset(&bits, 0, sizeof(oggpack_buffer));
65 
66     makeBitReader(packInfoData, sizeof(packInfoData), &buf, &ref, &bits);
67 
68     vorbis_info *mVi = new vorbis_info;
69     vorbis_info_init(mVi);
70 
71     int ret = _vorbis_unpack_info(mVi, &bits);
72     if (!ret) {
73         memset(&buf, 0, sizeof(ogg_buffer));
74         memset(&ref, 0, sizeof(ogg_reference));
75         memset(&bits, 0, sizeof(oggpack_buffer));
76 
77         makeBitReader(unpackBookData, sizeof(unpackBookData), &buf, &ref,
78                       &bits);
79 
80         ret = _vorbis_unpack_books(mVi, &bits);
81         if (!ret) {
82             ogg_packet pack;
83             memset(&pack, 0, sizeof(ogg_packet));
84             memset(&buf, 0, sizeof(ogg_buffer));
85             memset(&ref, 0, sizeof(ogg_reference));
86 
87             vorbis_dsp_state *mState = new vorbis_dsp_state;
88             vorbis_dsp_init(mState, mVi);
89 
90             buf.data = bufData;
91             buf.size = sizeof(bufData);
92             buf.refcount = REF_COUNT;
93 
94             ref.buffer = &buf;
95             ref.length = buf.size;
96 
97             pack.packet = &ref;
98             pack.bytes = ref.length;
99 
100             vorbis_dsp_synthesis(mState, &pack, DECODE_PACKET);
101         }
102     }
103     return EXIT_SUCCESS;
104 }
105