rhino/demo/c/dr_libs/tests/opus/dr_opus_decoding.c
Go to the documentation of this file.
1 /*#define DR_OPUS_DEBUGGING*/
2 
3 #define DR_OPUS_IMPLEMENTATION
4 #include "../../wip/dr_opus.h"
5 #include "../common/dr_common.c"
6 
7 const char* dropus_mode_to_string(dropus_mode mode) /* Move this into dr_opus.h? */
8 {
9  switch (mode)
10  {
11  case dropus_mode_silk: return "SILK";
12  case dropus_mode_celt: return "CELT";
13  case dropus_mode_hybrid: return "Hybrid";
14  default: break;
15  }
16 
17  return "Unknown";
18 }
19 
20 #include <stdio.h>
21 
22 /* Forward declare our debugging entry point if necessary. */
23 #ifdef DR_OPUS_DEBUGGING
24 int main_debugging(int argc, char** argv);
25 #endif
26 
27 dropus_result test_standard_vector(const char* pFilePath)
28 {
29  dropus_result result;
30  dropus_stream stream;
31  void* pFileData;
32  size_t fileSize;
33  const dropus_uint8* pRunningData8;
34  size_t runningPos;
35  dropus_uint32 iPacket = 0; /* For error reporting. */
36 
37 
38  /* Load the entire file into memory first. */
39  pFileData = dr_open_and_read_file(pFilePath, &fileSize);
40  if (pFileData == NULL) {
41  return DROPUS_ERROR; /* File not found. */
42  }
43 
44  /* Now initialize the stream in preparation for decoding packets. */
45  result = dropus_stream_init(&stream);
46  if (result != DROPUS_SUCCESS) {
47  free(pFileData);
48  return result;
49  }
50 
51  /*
52  Here is where we scan the test vector. The way the file is structured is quite simple. It is made up of a list of Opus packets, each
53  of which include an 8 byte header where the first 4 bytes is the size in bytes of the Opus packet (little endian) and the next 4 bytes
54  contain the 32-bit range state which we'll use for validation.
55  */
56  pRunningData8 = (const dropus_uint8*)pFileData;
57  runningPos = 0;
58 
59  /* For each packet... */
60  while (runningPos < fileSize) {
61  dropus_result decodeResult;
62  dropus_uint32 packetSize;
63  dropus_uint32 rangeState;
64 
65  memcpy(&packetSize, pRunningData8 + 0, 4);
66  memcpy(&rangeState, pRunningData8 + 4, 4);
67 
68  packetSize = dropus__be2host_32(packetSize);
69  rangeState = dropus__be2host_32(rangeState);
70 
71  pRunningData8 += 8;
72  runningPos += 8;
73 
74  /* Safety. Break if we've run out of data. */
75  if ((runningPos + packetSize) > fileSize) {
76  printf("WARNING: Ran out of data before the end of the file.\n");
77  break;
78  }
79 
80  decodeResult = dropus_stream_decode_packet(&stream, pRunningData8, packetSize);
81  if (decodeResult != DROPUS_SUCCESS) {
82  result = DROPUS_ERROR;
83  printf("Failed to decode packet %d\n", iPacket);
84  }
85 
86  printf("Opus Packet %d: Mode=%s\n", iPacket, dropus_mode_to_string(dropus_toc_mode(stream.packet.toc)));
87 
88  pRunningData8 += packetSize;
89  runningPos += packetSize;
90  iPacket += 1;
91  }
92 
93  free(pFileData);
94  return result;
95 }
96 
98 {
99  dropus_bool32 foundError = DROPUS_FALSE;
100  dr_file_iterator iteratorState;
101  dr_file_iterator* pFile;
102 
103  pFile = dr_file_iterator_begin(pFolderPath, &iteratorState);
104  while (pFile != NULL) {
105  /* Only look at files with the extension "bit". */
106  if (dr_extension_equal(pFile->relativePath, "bit")) {
108  foundError = DROPUS_TRUE;
109  }
110  }
111 
112  pFile = dr_file_iterator_next(pFile);
113  }
114 
115  if (foundError) {
116  return DROPUS_ERROR;
117  } else {
118  return DROPUS_SUCCESS;
119  }
120 }
121 
123 {
124  dropus_bool32 foundError = DROPUS_FALSE;
125 
126  /* Two groups of standard test vectors. The original vectors and the "new" vectors. */
127 
128  /* Original vectors. */
129  if (test_standard_vectors_folder("testvectors/opus/opus_testvectors") != DROPUS_SUCCESS) {
130  foundError = DROPUS_TRUE;
131  }
132 
133  /* New vectors. */
134  if (test_standard_vectors_folder("testvectors/opus/opus_newvectors") != DROPUS_SUCCESS) {
135  foundError = DROPUS_TRUE;
136  }
137 
138  if (foundError) {
139  return DROPUS_ERROR;
140  } else {
141  return DROPUS_SUCCESS;
142  }
143 }
144 
146 {
147  dropus_result result;
148 
149  /* Ogg Opus test vectors are in the "oggopus" folder. */
150  result = DROPUS_SUCCESS;
151 
152  return result;
153 }
154 
155 int main(int argc, char** argv)
156 {
157  dropus_result result;
158 
159  /* Do debugging stuff first. */
160 #ifdef DR_OPUS_DEBUGGING
161  main_debugging(argc, argv);
162 #endif
163 
164  result = test_standard_vector("testvectors/opus/opus_testvectors/testvector02.bit");
165  if (result != DROPUS_SUCCESS) {
166  /*return (int)result;*/
167  }
168 
169  /* Test standard vectors first. */
170  result = test_standard_vectors();
171  if (result != DROPUS_SUCCESS) {
172  /*return (int)result;*/
173  }
174 
175  /* Ogg Opus. */
176  result = test_ogg_vectors();
177  if (result != DROPUS_SUCCESS) {
178  /*return (int)result;*/
179  }
180 
181  (void)argc;
182  (void)argv;
183  return 0;
184 }
185 
186 #ifdef DR_OPUS_DEBUGGING
187 int main_debugging(int argc, char** argv)
188 {
189  dr_file_iterator iterator;
190  dr_file_iterator* pFile = dr_file_iterator_begin("testvectors/opus/oggopus", &iterator);
191  while (pFile != NULL) {
192  if (pFile->isDirectory) {
193  printf("DIRECTORY: %s : %s\n", pFile->relativePath, pFile->absolutePath);
194  } else {
195  printf("FILE: %s : %s\n", pFile->relativePath, pFile->absolutePath);
196  }
197 
198  pFile = dr_file_iterator_next(pFile);
199  }
200 
201  (void)argc;
202  (void)argv;
203  return 0;
204 }
205 #endif
dr_file_iterator_next
dr_file_iterator * dr_file_iterator_next(dr_file_iterator *pState)
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:495
DROPUS_ERROR
#define DROPUS_ERROR
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:132
dropus_uint32
uint32_t dropus_uint32
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:57
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
dr_extension_equal
dr_bool32 dr_extension_equal(const char *path, const char *extension)
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:343
DROPUS_SUCCESS
#define DROPUS_SUCCESS
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:131
DROPUS_FALSE
#define DROPUS_FALSE
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:65
dropus_mode_to_string
const char * dropus_mode_to_string(dropus_mode mode)
Definition: rhino/demo/c/dr_libs/tests/opus/dr_opus_decoding.c:7
dropus_mode
dropus_mode
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:199
dropus_bool32
dropus_uint32 dropus_bool32
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:63
dropus_stream
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:217
dropus_mode_celt
@ dropus_mode_celt
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:202
dropus_mode_hybrid
@ dropus_mode_hybrid
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:203
test_standard_vectors
dropus_result test_standard_vectors()
Definition: rhino/demo/c/dr_libs/tests/opus/dr_opus_decoding.c:122
dr_file_iterator::isDirectory
dr_bool32 isDirectory
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:383
dr_file_iterator
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:378
dropus_stream::packet
dropus_stream_packet packet
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:219
dr_open_and_read_file
void * dr_open_and_read_file(const char *filePath, size_t *pFileSizeOut)
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:675
dropus_stream_packet::toc
dropus_uint8 toc
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:213
dropus_stream_init
DROPUS_API dropus_result dropus_stream_init(dropus_stream *pOpusStream)
dr_file_iterator::relativePath
char relativePath[256]
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:381
test_ogg_vectors
dropus_result test_ogg_vectors()
Definition: rhino/demo/c/dr_libs/tests/opus/dr_opus_decoding.c:145
dropus_stream_decode_packet
DROPUS_API dropus_result dropus_stream_decode_packet(dropus_stream *pOpusStream, const void *pData, size_t dataSize)
main
int main(int argc, char **argv)
Definition: rhino/demo/c/dr_libs/tests/opus/dr_opus_decoding.c:155
dropus_mode_silk
@ dropus_mode_silk
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:201
dr_file_iterator::absolutePath
char absolutePath[256]
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:382
dropus_uint8
uint8_t dropus_uint8
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:53
python.test_porcupine.argv
argv
Definition: test_porcupine.py:158
test_standard_vectors_folder
dropus_result test_standard_vectors_folder(const char *pFolderPath)
Definition: rhino/demo/c/dr_libs/tests/opus/dr_opus_decoding.c:97
dr_file_iterator_begin
dr_file_iterator * dr_file_iterator_begin(const char *pFolderPath, dr_file_iterator *pState)
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:391
test_standard_vector
dropus_result test_standard_vector(const char *pFilePath)
Definition: rhino/demo/c/dr_libs/tests/opus/dr_opus_decoding.c:27
dropus_result
int dropus_result
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:130
DROPUS_TRUE
#define DROPUS_TRUE
Definition: porcupine/demo/c/dr_libs/wip/dr_opus.h:64


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:55