1 #ifndef STANDALONE
2 #include <Python.h>
3 #endif
4 #include <stdint.h>
5 #include "../bitstream.h"
6 #include "../array.h"
7 #include "../common/md5.h"
8 #include "../common/flac_crc.h"
9 
10 /********************************************************
11  Audio Tools, a module and set of tools for manipulating audio data
12  Copyright (C) 2007-2014  Brian Langenberger
13 
14  This program is free software; you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation; either version 2 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program; if not, write to the Free Software
26  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
27 *******************************************************/
28 
29 struct flac_STREAMINFO {
30     unsigned minimum_block_size;  /*16  bits*/
31     unsigned maximum_block_size;  /*16  bits*/
32     unsigned minimum_frame_size;  /*24  bits*/
33     unsigned maximum_frame_size;  /*24  bits*/
34     unsigned sample_rate;         /*20  bits*/
35     unsigned channels;            /*3   bits*/
36     unsigned bits_per_sample;     /*5   bits*/
37     uint64_t total_samples;       /*36  bits*/
38     uint8_t md5sum[16];           /*128 bits*/
39 };
40 
41 struct flac_SEEKPOINT {
42     uint64_t sample_number;       /*64 bits*/
43     uint64_t byte_offset;         /*64 bits*/
44     unsigned samples;             /*16 bits*/
45 };
46 
47 struct flac_frame_header {
48     unsigned blocking_strategy;
49     unsigned block_size;
50     unsigned sample_rate;
51     unsigned channel_assignment;
52     unsigned channel_count;
53     unsigned bits_per_sample;
54     unsigned frame_number;
55 };
56 
57 typedef enum {FLAC_SUBFRAME_CONSTANT,
58               FLAC_SUBFRAME_VERBATIM,
59               FLAC_SUBFRAME_FIXED,
60               FLAC_SUBFRAME_LPC} flac_subframe_type;
61 
62 struct flac_subframe_header {
63     flac_subframe_type type;
64     unsigned order;
65     unsigned wasted_bits_per_sample;
66 };
67 
68 typedef enum {OK,
69               ERROR,
70               ERR_INVALID_SYNC_CODE,
71               ERR_INVALID_RESERVED_BIT,
72               ERR_INVALID_BITS_PER_SAMPLE,
73               ERR_INVALID_SAMPLE_RATE,
74               ERR_INVALID_FRAME_CRC,
75               ERR_SAMPLE_RATE_MISMATCH,
76               ERR_CHANNEL_COUNT_MISMATCH,
77               ERR_BITS_PER_SAMPLE_MISMATCH,
78               ERR_MAXIMUM_BLOCK_SIZE_EXCEEDED,
79               ERR_INVALID_CODING_METHOD,
80               ERR_INVALID_FIXED_ORDER,
81               ERR_INVALID_SUBFRAME_TYPE} flac_status;
82 
83 #ifndef STANDALONE
84 typedef struct {
85     PyObject_HEAD
86 
87     BitstreamReader* bitstream;
88     int channel_mask;
89 
90     struct flac_STREAMINFO streaminfo;
91     a_obj* seektable;
92     uint64_t remaining_samples;
93     int closed;
94 
95     audiotools__MD5Context md5;
96     int perform_validation;
97     int stream_finalized;
98 
99     /*temporary buffers we don't want to reallocate each time*/
100     aa_int* subframe_data;
101     a_int* residuals;
102     a_int* qlp_coeffs;
103     a_int* framelist_data;
104 
105     /*a framelist generator*/
106     PyObject* audiotools_pcm;
107 
108     /*a mark for seeking purposes*/
109     br_pos_t* beginning_of_frames;
110 } decoders_FlacDecoder;
111 
112 static PyObject*
113 FlacDecoder_sample_rate(decoders_FlacDecoder *self, void *closure);
114 
115 static PyObject*
116 FlacDecoder_bits_per_sample(decoders_FlacDecoder *self, void *closure);
117 
118 static PyObject*
119 FlacDecoder_channels(decoders_FlacDecoder *self, void *closure);
120 
121 static PyObject*
122 FlacDecoder_channel_mask(decoders_FlacDecoder *self, void *closure);
123 
124 static PyObject*
125 FlacDecoder_read(decoders_FlacDecoder* self, PyObject *args);
126 
127 static PyObject*
128 FlacDecoder_seek(decoders_FlacDecoder* self, PyObject *args);
129 
130 static PyObject*
131 FlacDecoder_offsets(decoders_FlacDecoder* self, PyObject *args);
132 
133 static PyObject*
134 FlacDecoder_close(decoders_FlacDecoder* self, PyObject *args);
135 
136 static PyObject*
137 FlacDecoder_enter(decoders_FlacDecoder* self, PyObject *args);
138 
139 static PyObject*
140 FlacDecoder_exit(decoders_FlacDecoder* self, PyObject *args);
141 
142 /*the FlacDecoder.__init__() method*/
143 int
144 FlacDecoder_init(decoders_FlacDecoder *self,
145                  PyObject *args, PyObject *kwds);
146 
147 PyGetSetDef FlacDecoder_getseters[] = {
148     {"sample_rate",
149      (getter)FlacDecoder_sample_rate, NULL, "sample rate", NULL},
150     {"bits_per_sample",
151      (getter)FlacDecoder_bits_per_sample, NULL, "bits-per-sample", NULL},
152     {"channels",
153      (getter)FlacDecoder_channels, NULL, "channels", NULL},
154     {"channel_mask",
155      (getter)FlacDecoder_channel_mask, NULL, "channel mask", NULL},
156     {NULL}
157 };
158 
159 PyMethodDef FlacDecoder_methods[] = {
160     {"read", (PyCFunction)FlacDecoder_read,
161      METH_VARARGS, "read(pcm_frame_count) -> FrameList"},
162     {"seek", (PyCFunction)FlacDecoder_seek,
163      METH_VARARGS, "seek(desired_pcm_offset) -> actual_pcm_offset"},
164     {"offsets", (PyCFunction)FlacDecoder_offsets,
165      METH_NOARGS, "offsets() -> [(byte_offset, pcm_frame_count), ...]"},
166     {"close", (PyCFunction)FlacDecoder_close,
167      METH_NOARGS, "close() -> None"},
168     {"__enter__", (PyCFunction)FlacDecoder_enter,
169      METH_NOARGS, "enter() -> self"},
170     {"__exit__", (PyCFunction)FlacDecoder_exit,
171      METH_VARARGS, "exit(exc_type, exc_value, traceback) -> None"},
172     {NULL}
173 };
174 
175 void
176 FlacDecoder_dealloc(decoders_FlacDecoder *self);
177 
178 static PyObject*
179 FlacDecoder_new(PyTypeObject *type,
180                 PyObject *args, PyObject *kwds);
181 
182 /*reads the STREAMINFO, SEEKTABLE and VORBIS_COMMENT blocks,
183   skips any other metadata blocks,
184   placing our internal stream at the first FLAC frame
185 
186   returns 0 on success, 1 on failure with PyErr set*/
187 int
188 flacdec_read_metadata(BitstreamReader *bitstream,
189                       struct flac_STREAMINFO *streaminfo,
190                       a_obj* seektable,
191                       int *channel_mask);
192 #endif
193 
194 /*reads a FLAC frame header from the sync code to the CRC-8
195   and places the result in "header"*/
196 flac_status
197 flacdec_read_frame_header(BitstreamReader *bitstream,
198                           struct flac_STREAMINFO *streaminfo,
199                           struct flac_frame_header *header);
200 
201 /*reads a FLAC subframe header from the padding bit to the wasted bps (if any)
202   and places the result in "subframe_header"*/
203 flac_status
204 flacdec_read_subframe_header(BitstreamReader *bitstream,
205                              struct flac_subframe_header *subframe_header);
206 
207 /*returns a subframe's effective bits per sample
208   based on the frame header and whether the subframe
209   is the side channel in left-side/side-right/mid-side encoding*/
210 unsigned int
211 flacdec_subframe_bits_per_sample(struct flac_frame_header *frame_header,
212                                  unsigned int channel_number);
213 
214 /*reads a FLAC subframe from bitstream
215   with block_size and effective bits_per_sample
216   and places the result in "samples"
217 
218   "qlp_coeffs" and "residuals" are temporary buffers
219   to be recycled as needed*/
220 flac_status
221 flacdec_read_subframe(BitstreamReader* bitstream,
222                       a_int* qlp_coeffs,
223                       a_int* residuals,
224                       unsigned block_size,
225                       unsigned bits_per_sample,
226                       a_int* samples);
227 
228 /*the following four functions are called by FlacDecoder_read_subframe
229   depending on the subframe type in the subframe header
230   all take the same arguments as FlacDecoder_read_subframe
231   and, for fixed and lpc, an "order" argument - also from the subframe header*/
232 flac_status
233 flacdec_read_constant_subframe(BitstreamReader* bitstream,
234                                unsigned block_size,
235                                unsigned bits_per_sample,
236                                a_int* samples);
237 
238 flac_status
239 flacdec_read_verbatim_subframe(BitstreamReader* bitstream,
240                                unsigned block_size,
241                                unsigned bits_per_sample,
242                                a_int* samples);
243 
244 flac_status
245 flacdec_read_fixed_subframe(BitstreamReader* bitstream,
246                             a_int* residuals,
247                             unsigned order,
248                             unsigned block_size,
249                             unsigned bits_per_sample,
250                             a_int* samples);
251 
252 flac_status
253 flacdec_read_lpc_subframe(BitstreamReader* bitstream,
254                           a_int* qlp_coeffs,
255                           a_int* residuals,
256                           unsigned order,
257                           unsigned block_size,
258                           unsigned bits_per_sample,
259                           a_int* samples);
260 
261 /*reads a chunk of residuals with the given "order" and "block_size"
262   (determined from read_fixed_subframe or read_lpc_subframe)
263   and places the result in "residuals"*/
264 flac_status
265 flacdec_read_residual(BitstreamReader* bitstream,
266                       unsigned order,
267                       unsigned block_size,
268                       a_int* residuals);
269 
270 void
271 flacdec_decorrelate_channels(unsigned channel_assignment,
272                              const aa_int* subframes,
273                              a_int* framelist);
274 
275 const char*
276 FlacDecoder_strerror(flac_status error);
277 
278 unsigned read_utf8(BitstreamReader *stream);
279 
280 struct flac_SEEKPOINT*
281 seekpoint_copy(struct flac_SEEKPOINT* seekpoint);
282 
283 #ifndef STANDALONE
284 
285 flac_status
286 FlacDecoder_update_md5sum(decoders_FlacDecoder *self,
287                           PyObject *framelist);
288 
289 int
290 FlacDecoder_verify_okay(decoders_FlacDecoder *self);
291 
292 #ifdef IS_PY3K
293 
294 static PyTypeObject decoders_FlacDecoderType = {
295     PyVarObject_HEAD_INIT(NULL, 0)
296     "decoders.FlacDecoder",     /* tp_name */
297     sizeof(decoders_FlacDecoder), /* tp_basicsize */
298     0,                         /* tp_itemsize */
299     (destructor)FlacDecoders_dealloc, /* tp_dealloc */
300     0,                         /* tp_print */
301     0,                         /* tp_getattr */
302     0,                         /* tp_setattr */
303     0,                         /* tp_reserved */
304     0,                         /* tp_repr */
305     0,                         /* tp_as_number */
306     0,                         /* tp_as_sequence */
307     0,                         /* tp_as_mapping */
308     0,                         /* tp_hash  */
309     0,                         /* tp_call */
310     0,                         /* tp_str */
311     0,                         /* tp_getattro */
312     0,                         /* tp_setattro */
313     0,                         /* tp_as_buffer */
314     Py_TPFLAGS_DEFAULT |
315     Py_TPFLAGS_BASETYPE,       /* tp_flags */
316     "FlacDecoder objects",     /* tp_doc */
317     0,                         /* tp_traverse */
318     0,                         /* tp_clear */
319     0,                         /* tp_richcompare */
320     0,                         /* tp_weaklistoffset */
321     0,                         /* tp_iter */
322     0,                         /* tp_iternext */
323     FlacDecoder_methods,       /* tp_methods */
324     0,                         /* tp_members */
325     FlacDecoder_getseters,     /* tp_getset */
326     0,                         /* tp_base */
327     0,                         /* tp_dict */
328     0,                         /* tp_descr_get */
329     0,                         /* tp_descr_set */
330     0,                         /* tp_dictoffset */
331     (initproc)FlacDecoder_init,/* tp_init */
332     0,                         /* tp_alloc */
333     FlacDecoder_new,           /* tp_new */
334 };
335 
336 #else
337 
338 PyTypeObject decoders_FlacDecoderType = {
339     PyVarObject_HEAD_INIT(NULL, 0)
340     "decoders.FlacDecoder",    /*tp_name*/
341     sizeof(decoders_FlacDecoder), /*tp_basicsize*/
342     0,                         /*tp_itemsize*/
343     (destructor)FlacDecoder_dealloc, /*tp_dealloc*/
344     0,                         /*tp_print*/
345     0,                         /*tp_getattr*/
346     0,                         /*tp_setattr*/
347     0,                         /*tp_compare*/
348     0,                         /*tp_repr*/
349     0,                         /*tp_as_number*/
350     0,                         /*tp_as_sequence*/
351     0,                         /*tp_as_mapping*/
352     0,                         /*tp_hash */
353     0,                         /*tp_call*/
354     0,                         /*tp_str*/
355     0,                         /*tp_getattro*/
356     0,                         /*tp_setattro*/
357     0,                         /*tp_as_buffer*/
358     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
359     "FlacDecoder objects",     /* tp_doc */
360     0,                         /* tp_traverse */
361     0,                         /* tp_clear */
362     0,                         /* tp_richcompare */
363     0,                         /* tp_weaklistoffset */
364     0,                         /* tp_iter */
365     0,                         /* tp_iternext */
366     FlacDecoder_methods,       /* tp_methods */
367     0,                         /* tp_members */
368     FlacDecoder_getseters,     /* tp_getset */
369     0,                         /* tp_base */
370     0,                         /* tp_dict */
371     0,                         /* tp_descr_get */
372     0,                         /* tp_descr_set */
373     0,                         /* tp_dictoffset */
374     (initproc)FlacDecoder_init,/* tp_init */
375     0,                         /* tp_alloc */
376     FlacDecoder_new,           /* tp_new */
377 };
378 
379 #endif
380 #endif
381