GNU Radio's DAB Package
mp2_decode_bs_impl.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 
3 /*
4 * 2017 by Moritz Luca Schmid, Communications Engineering Lab (CEL) / Karlsruhe Institute of Technology (KIT).
5 * A major part of this code is adapted from the kjmp2 library, slightly modified and written into a GNURadio block.
6 * Note that this is an altered version of kjmp2 and not the original library.
7 */
8 
9 /******************************************************************************
10 ** kjmp2 -- a minimal MPEG-1/2 Audio Layer II decoder library **
11 ** version 1.1 **
12 *******************************************************************************
13 ** Copyright (C) 2006-2013 Martin J. Fiedler <martin.fiedler@gmx.net> **
14 ** **
15 ** This software is provided 'as-is', without any express or implied **
16 ** warranty. In no event will the authors be held liable for any damages **
17 ** arising from the use of this software. **
18 ** **
19 ** Permission is granted to anyone to use this software for any purpose, **
20 ** including commercial applications, and to alter it and redistribute it **
21 ** freely, subject to the following restrictions: **
22 ** 1. The origin of this software must not be misrepresented; you must not **
23 ** claim that you wrote the original software. If you use this software **
24 ** in a product, an acknowledgment in the product documentation would **
25 ** be appreciated but is not required. **
26 ** 2. Altered source versions must be plainly marked as such, and must not **
27 ** be misrepresented as being the original software. **
28 ** 3. This notice may not be removed or altered from any source **
29 ** distribution. **
30 ******************************************************************************/
31 
32 #ifndef INCLUDED_DAB_MP2_DECODE_BS_IMPL_H
33 #define INCLUDED_DAB_MP2_DECODE_BS_IMPL_H
34 
35 #include <grdab/mp2_decode_bs.h>
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <math.h>
39 //#include "pad-handler.h"
40 
41 namespace gr {
42  namespace dab {
43 /*! \brief block that decodes DAB audio frames (= MPEG2 audio frames) to PCM frames
44  *
45  * DAB standard conform audio decoding after ETSI TS 103 466 V1.1.1
46  * The block always produces a stereo output. The sampling rate is 48kHz.
47  *
48  * @param bit_rate_n data rate in multiples of 8kbit/s
49  */
50 #define KJMP2_MAX_FRAME_SIZE 1440 // the maximum size of a frame
51 #define KJMP2_SAMPLES_PER_FRAME 1152 // the number of samples per frame
52 
53 // quantizer specification structure
54  struct quantizer_spec {
55  int32_t nlevels;
56  uint8_t grouping;
57  uint8_t cw_bits;
58  };
59 
61  private:
62  int d_bit_rate_n;
63  int d_bit_rate;
64  int d_nproduced;
65  uint16_t *d_out;
66  int32_t d_sample_rate;
67 
68  int16_t d_V_offs;
69  int32_t d_baud_rate;
70  int16_t d_mp2_framesize;
71  int16_t d_output_size;
72  int16_t d_mp2_header_OK;
73  int16_t d_mp2_header_count;
74  int16_t d_mp2_bit_count;
75  int16_t d_number_of_frames;
76  int16_t d_error_frames;
77  int32_t bit_window;
78  int32_t bits_in_window;
79  uint8_t *d_frame_pos;
80  uint8_t *d_mp2_frame;
81  int16_t d_V[2][1024];
82  int16_t d_N[64][32];
83  int32_t d_scfsi[2][32];
84  int32_t d_scalefactor[2][32][3];
85  int32_t d_sample[2][32][3];
86  int32_t d_U[512];
87  bool d_first;
88 
89  void set_samplerate(int32_t);
90 
91  int32_t mp2_samplerate(uint8_t *);
92 
93  struct quantizer_spec *read_allocation(int, int);
94 
95  struct quantizer_spec *d_allocation[2][32];
96 
97  void read_samples(struct quantizer_spec *, int, int *);
98 
99  int32_t get_bits(int32_t);
100 
101  int32_t mp2_decode_frame(uint8_t *, int16_t *);
102 
103  void add_bit_to_mp2(uint8_t *, uint8_t, int16_t);
104 
105  public:
106  mp2_decode_bs_impl(int bit_rate_n);
107 
109 
110  virtual int32_t get_sample_rate()
111  {return d_sample_rate;}
112 
113  // Where all the action really happens
114  void forecast(int noutput_items, gr_vector_int &ninput_items_required);
115 
116  int general_work(int noutput_items,
117  gr_vector_int &ninput_items,
118  gr_vector_const_void_star &input_items,
119  gr_vector_void_star &output_items);
120  };
121 
122  } // namespace dab
123 } // namespace gr
124 
125 #endif /* INCLUDED_DAB_MP2_DECODE_BS_IMPL_H */
126 
gr::dab::quantizer_spec::nlevels
int32_t nlevels
Definition: mp2_decode_bs_impl.h:55
gr::dab::quantizer_spec
Definition: mp2_decode_bs_impl.h:54
gr::dab::mp2_decode_bs_impl
Definition: mp2_decode_bs_impl.h:60
gr::dab::mp2_decode_bs_impl::get_sample_rate
virtual int32_t get_sample_rate()
Definition: mp2_decode_bs_impl.h:110
gr::dab::mp2_decode_bs_impl::general_work
int general_work(int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
gr::dab::mp2_decode_bs
block that decodes DAB audio frames (= MPEG2 audio frames) to PCM frames
Definition: mp2_decode_bs.h:37
gr::dab::mp2_decode_bs_impl::~mp2_decode_bs_impl
~mp2_decode_bs_impl()
gr::dab::quantizer_spec::grouping
uint8_t grouping
Definition: mp2_decode_bs_impl.h:56
mp2_decode_bs.h
gr
Definition: complex_to_interleaved_float_vcf.h:28
gr::dab::mp2_decode_bs_impl::forecast
void forecast(int noutput_items, gr_vector_int &ninput_items_required)
gr::dab::quantizer_spec::cw_bits
uint8_t cw_bits
Definition: mp2_decode_bs_impl.h:57
gr::dab::mp2_decode_bs_impl::mp2_decode_bs_impl
mp2_decode_bs_impl(int bit_rate_n)