audio/mp3: Move t_mpeg1_header to Go

This commit is contained in:
Hajime Hoshi 2017-06-17 17:42:53 +09:00
parent a0af1595de
commit ab8a9a1e36
7 changed files with 45 additions and 89 deletions

View File

@ -17,8 +17,6 @@
package mp3 package mp3
// #include "pdmp3.h" // #include "pdmp3.h"
//
// extern t_mpeg1_header g_frame_header;
import "C" import "C"
import ( import (
@ -37,7 +35,7 @@ func decodeL3() error {
out := make([]uint32, 576) out := make([]uint32, 576)
// Number of channels(1 for mono and 2 for stereo) // Number of channels(1 for mono and 2 for stereo)
nch := 2 nch := 2
if C.g_frame_header.mode == C.mpeg1_mode_single_channel { if theMPEG1FrameHeader.mode == mpeg1ModeSingleChannel {
nch = 1 nch = 1
} }
for gr := 0; gr < 2; gr++ { for gr := 0; gr < 2; gr++ {
@ -64,7 +62,7 @@ func decodeL3() error {
func audioWriteRaw(samples []uint32) error { func audioWriteRaw(samples []uint32) error {
nch := 2 nch := 2
if C.g_frame_header.mode == C.mpeg1_mode_single_channel { if theMPEG1FrameHeader.mode == mpeg1ModeSingleChannel {
nch = 1 nch = 1
} }
s := make([]uint8, len(samples)*2*nch) s := make([]uint8, len(samples)*2*nch)

View File

@ -17,8 +17,6 @@
package mp3 package mp3
// #include "pdmp3.h" // #include "pdmp3.h"
//
// extern t_mpeg1_header g_frame_header;
import "C" import "C"
import ( import (
@ -102,7 +100,7 @@ var (
func l3Requantize(gr int, ch int) { func l3Requantize(gr int, ch int) {
/* Setup sampling frequency index */ /* Setup sampling frequency index */
sfreq := C.g_frame_header.sampling_frequency sfreq := theMPEG1FrameHeader.sampling_frequency
/* Determine type of block to process */ /* Determine type of block to process */
if (theMPEG1SideInfo.win_switch_flag[gr][ch] == 1) && (theMPEG1SideInfo.block_type[gr][ch] == 2) { /* Short blocks */ if (theMPEG1SideInfo.win_switch_flag[gr][ch] == 1) && (theMPEG1SideInfo.block_type[gr][ch] == 2) { /* Short blocks */
/* Check if the first two subbands /* Check if the first two subbands
@ -177,7 +175,7 @@ func l3Requantize(gr int, ch int) {
func l3Reorder(gr int, ch int) { func l3Reorder(gr int, ch int) {
re := make([]float32, 576) re := make([]float32, 576)
sfreq := C.g_frame_header.sampling_frequency /* Setup sampling freq index */ sfreq := theMPEG1FrameHeader.sampling_frequency /* Setup sampling freq index */
/* Only reorder short blocks */ /* Only reorder short blocks */
if (theMPEG1SideInfo.win_switch_flag[gr][ch] == 1) && (theMPEG1SideInfo.block_type[gr][ch] == 2) { /* Short blocks */ if (theMPEG1SideInfo.win_switch_flag[gr][ch] == 1) && (theMPEG1SideInfo.block_type[gr][ch] == 2) { /* Short blocks */
/* Check if the first two subbands /* Check if the first two subbands
@ -232,7 +230,7 @@ func stereoProcessIntensityLong(gr int, sfb int) {
/* Check that((is_pos[sfb]=scalefac) != 7) => no intensity stereo */ /* Check that((is_pos[sfb]=scalefac) != 7) => no intensity stereo */
is_pos := theMPEG1MainData.scalefac_l[gr][0][sfb] is_pos := theMPEG1MainData.scalefac_l[gr][0][sfb]
if is_pos != 7 { if is_pos != 7 {
sfreq := C.g_frame_header.sampling_frequency /* Setup sampling freq index */ sfreq := theMPEG1FrameHeader.sampling_frequency /* Setup sampling freq index */
sfb_start := sfBandIndicesSet[sfreq].l[sfb] sfb_start := sfBandIndicesSet[sfreq].l[sfb]
sfb_stop := sfBandIndicesSet[sfreq].l[sfb+1] sfb_stop := sfBandIndicesSet[sfreq].l[sfb+1]
if is_pos == 6 { /* tan((6*PI)/12 = PI/2) needs special treatment! */ if is_pos == 6 { /* tan((6*PI)/12 = PI/2) needs special treatment! */
@ -253,7 +251,7 @@ func stereoProcessIntensityLong(gr int, sfb int) {
func stereoProcessIntensityShort(gr int, sfb int) { func stereoProcessIntensityShort(gr int, sfb int) {
is_ratio_l := float32(0) is_ratio_l := float32(0)
is_ratio_r := float32(0) is_ratio_r := float32(0)
sfreq := C.g_frame_header.sampling_frequency /* Setup sampling freq index */ sfreq := theMPEG1FrameHeader.sampling_frequency /* Setup sampling freq index */
/* The window length */ /* The window length */
win_len := sfBandIndicesSet[sfreq].s[sfb+1] - sfBandIndicesSet[sfreq].s[sfb] win_len := sfBandIndicesSet[sfreq].s[sfb+1] - sfBandIndicesSet[sfreq].s[sfb]
/* The three windows within the band has different scalefactors */ /* The three windows within the band has different scalefactors */
@ -282,11 +280,11 @@ func stereoProcessIntensityShort(gr int, sfb int) {
func l3Stereo(gr int) { func l3Stereo(gr int) {
/* Do nothing if joint stereo is not enabled */ /* Do nothing if joint stereo is not enabled */
if (C.g_frame_header.mode != 1) || (C.g_frame_header.mode_extension == 0) { if (theMPEG1FrameHeader.mode != 1) || (theMPEG1FrameHeader.mode_extension == 0) {
return return
} }
/* Do Middle/Side("normal") stereo processing */ /* Do Middle/Side("normal") stereo processing */
if (C.g_frame_header.mode_extension & 0x2) != 0 { if (theMPEG1FrameHeader.mode_extension & 0x2) != 0 {
/* Determine how many frequency lines to transform */ /* Determine how many frequency lines to transform */
i := 0 i := 0
if theMPEG1SideInfo.count1[gr][0] > theMPEG1SideInfo.count1[gr][1] { if theMPEG1SideInfo.count1[gr][0] > theMPEG1SideInfo.count1[gr][1] {
@ -303,9 +301,9 @@ func l3Stereo(gr int) {
} }
} }
/* Do intensity stereo processing */ /* Do intensity stereo processing */
if (C.g_frame_header.mode_extension & 0x1) != 0 { if (theMPEG1FrameHeader.mode_extension & 0x1) != 0 {
/* Setup sampling frequency index */ /* Setup sampling frequency index */
sfreq := C.g_frame_header.sampling_frequency sfreq := theMPEG1FrameHeader.sampling_frequency
/* First band that is intensity stereo encoded is first band scale factor /* First band that is intensity stereo encoded is first band scale factor
* band on or above count1 frequency line. N.B.: Intensity stereo coding is * band on or above count1 frequency line. N.B.: Intensity stereo coding is
* only done for higher subbands, but logic is here for lower subbands. */ * only done for higher subbands, but logic is here for lower subbands. */
@ -562,7 +560,7 @@ func l3SubbandSynthesis(gr int, ch int, out []uint32) {
/* Number of channels(1 for mono and 2 for stereo) */ /* Number of channels(1 for mono and 2 for stereo) */
nch := 2 nch := 2
if C.g_frame_header.mode == C.mpeg1_mode_single_channel { if theMPEG1FrameHeader.mode == mpeg1ModeSingleChannel {
nch = 1 nch = 1
} }
/* Setup the n_win windowing vector and the v_vec intermediate vector */ /* Setup the n_win windowing vector and the v_vec intermediate vector */

View File

@ -17,8 +17,6 @@
package mp3 package mp3
// #include "pdmp3.h" // #include "pdmp3.h"
//
// extern t_mpeg1_header g_frame_header;
import "C" import "C"
import ( import (
@ -34,15 +32,15 @@ var mpeg1_scalefac_sizes = [16][2]int{
func readMainL3() error { func readMainL3() error {
/* Number of channels(1 for mono and 2 for stereo) */ /* Number of channels(1 for mono and 2 for stereo) */
nch := 2 nch := 2
if C.g_frame_header.mode == C.mpeg1_mode_single_channel { if theMPEG1FrameHeader.mode == mpeg1ModeSingleChannel {
nch = 1 nch = 1
} }
/* Calculate header audio data size */ /* Calculate header audio data size */
framesize := (144* framesize := (144*
g_mpeg1_bitrates[C.g_frame_header.layer-1][C.g_frame_header.bitrate_index])/ g_mpeg1_bitrates[theMPEG1FrameHeader.layer-1][theMPEG1FrameHeader.bitrate_index])/
g_sampling_frequency[C.g_frame_header.sampling_frequency] + g_sampling_frequency[theMPEG1FrameHeader.sampling_frequency] +
int(C.g_frame_header.padding_bit) int(theMPEG1FrameHeader.padding_bit)
if framesize > 2000 { if framesize > 2000 {
return fmt.Errorf("mp3: framesize = %d", framesize) return fmt.Errorf("mp3: framesize = %d", framesize)
@ -55,7 +53,7 @@ func readMainL3() error {
/* Main data size is the rest of the frame,including ancillary data */ /* Main data size is the rest of the frame,including ancillary data */
main_data_size := framesize - sideinfo_size - 4 /* sync+header */ main_data_size := framesize - sideinfo_size - 4 /* sync+header */
/* CRC is 2 bytes */ /* CRC is 2 bytes */
if C.g_frame_header.protection_bit == 0 { if theMPEG1FrameHeader.protection_bit == 0 {
main_data_size -= 2 main_data_size -= 2
} }
/* Assemble main data buffer with data from this frame and the previous /* Assemble main data buffer with data from this frame and the previous

View File

@ -59,8 +59,6 @@ g_sampling_frequency[3] = { 44100 * Hz,48000 * Hz,32000 * Hz };
unsigned synth_init = 1; unsigned synth_init = 1;
t_mpeg1_header g_frame_header;
#ifdef DEBUG #ifdef DEBUG
static void dmp_fr(t_mpeg1_header *hdr){ static void dmp_fr(t_mpeg1_header *hdr){
printf("rate %d,sfreq %d,pad %d,mod %d,modext %d,emph %d\n", printf("rate %d,sfreq %d,pad %d,mod %d,modext %d,emph %d\n",

View File

@ -16,36 +16,4 @@
#define TRUE 1 #define TRUE 1
#define FALSE 0 #define FALSE 0
/* Types used in the frame header */
typedef enum { /* Layer number */
mpeg1_layer_reserved = 0,
mpeg1_layer_3 = 1,
mpeg1_layer_2 = 2,
mpeg1_layer_1 = 3
}
t_mpeg1_layer;
typedef enum { /* Modes */
mpeg1_mode_stereo = 0,
mpeg1_mode_joint_stereo,
mpeg1_mode_dual_channel,
mpeg1_mode_single_channel
}
t_mpeg1_mode;
typedef struct { /* MPEG1 Layer 1-3 frame header */
unsigned id; /* 1 bit */
t_mpeg1_layer layer; /* 2 bits */
unsigned protection_bit; /* 1 bit */
unsigned bitrate_index; /* 4 bits */
unsigned sampling_frequency; /* 2 bits */
unsigned padding_bit; /* 1 bit */
unsigned private_bit; /* 1 bit */
t_mpeg1_mode mode; /* 2 bits */
unsigned mode_extension; /* 2 bits */
unsigned copyright; /* 1 bit */
unsigned original_or_copy; /* 1 bit */
unsigned emphasis; /* 2 bits */
}
t_mpeg1_header;
#endif #endif

View File

@ -17,8 +17,6 @@
package mp3 package mp3
// #include "pdmp3.h" // #include "pdmp3.h"
//
// extern t_mpeg1_header g_frame_header;
import "C" import "C"
import ( import (
@ -52,14 +50,14 @@ func readFrame() error {
return err return err
} }
// Get CRC word if present // Get CRC word if present
if C.g_frame_header.protection_bit == 0 { if theMPEG1FrameHeader.protection_bit == 0 {
if err := readCRC(); err != nil { if err := readCRC(); err != nil {
return err return err
} }
} }
if C.g_frame_header.layer != 3 { if theMPEG1FrameHeader.layer != 3 {
return fmt.Errorf("mp3: Only layer 3(!= %d) is supported!", C.g_frame_header.layer) return fmt.Errorf("mp3: Only layer 3(!= %d) is supported!", theMPEG1FrameHeader.layer)
} }
// Get side info // Get side info
if err := readAudioL3(); err != nil { if err := readAudioL3(); err != nil {
@ -131,40 +129,40 @@ func readHeader() error {
/* If we get here we've found the sync word,and can decode the header /* If we get here we've found the sync word,and can decode the header
* which is in the low 20 bits of the 32-bit sync+header word. */ * which is in the low 20 bits of the 32-bit sync+header word. */
/* Decode the header */ /* Decode the header */
C.g_frame_header.id = C.uint((header & 0x00180000) >> 19) theMPEG1FrameHeader.id = int((header & 0x00180000) >> 19)
C.g_frame_header.layer = C.t_mpeg1_layer((header & 0x00060000) >> 17) theMPEG1FrameHeader.layer = mpeg1Layer((header & 0x00060000) >> 17)
C.g_frame_header.protection_bit = C.uint((header & 0x00010000) >> 16) theMPEG1FrameHeader.protection_bit = int((header & 0x00010000) >> 16)
C.g_frame_header.bitrate_index = C.uint((header & 0x0000f000) >> 12) theMPEG1FrameHeader.bitrate_index = int((header & 0x0000f000) >> 12)
C.g_frame_header.sampling_frequency = C.uint((header & 0x00000c00) >> 10) theMPEG1FrameHeader.sampling_frequency = int((header & 0x00000c00) >> 10)
C.g_frame_header.padding_bit = C.uint((header & 0x00000200) >> 9) theMPEG1FrameHeader.padding_bit = int((header & 0x00000200) >> 9)
C.g_frame_header.private_bit = C.uint((header & 0x00000100) >> 8) theMPEG1FrameHeader.private_bit = int((header & 0x00000100) >> 8)
C.g_frame_header.mode = C.t_mpeg1_mode((header & 0x000000c0) >> 6) theMPEG1FrameHeader.mode = mpeg1Mode((header & 0x000000c0) >> 6)
C.g_frame_header.mode_extension = C.uint((header & 0x00000030) >> 4) theMPEG1FrameHeader.mode_extension = int((header & 0x00000030) >> 4)
C.g_frame_header.copyright = C.uint((header & 0x00000008) >> 3) theMPEG1FrameHeader.copyright = int((header & 0x00000008) >> 3)
C.g_frame_header.original_or_copy = C.uint((header & 0x00000004) >> 2) theMPEG1FrameHeader.original_or_copy = int((header & 0x00000004) >> 2)
C.g_frame_header.emphasis = C.uint((header & 0x00000003) >> 0) theMPEG1FrameHeader.emphasis = int((header & 0x00000003) >> 0)
/* Check for invalid values and impossible combinations */ /* Check for invalid values and impossible combinations */
if C.g_frame_header.id != 3 { if theMPEG1FrameHeader.id != 3 {
return fmt.Errorf("mp3: ID must be 3\nHeader word is 0x%08x at file pos %d", return fmt.Errorf("mp3: ID must be 3\nHeader word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
if C.g_frame_header.bitrate_index == 0 { if theMPEG1FrameHeader.bitrate_index == 0 {
return fmt.Errorf("mp3: Free bitrate format NIY!\nHeader word is 0x%08x at file pos %d", return fmt.Errorf("mp3: Free bitrate format NIY!\nHeader word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
if C.g_frame_header.bitrate_index == 15 { if theMPEG1FrameHeader.bitrate_index == 15 {
return fmt.Errorf("mp3: bitrate_index = 15 is invalid!\nHeader word is 0x%08x at file pos %d", return fmt.Errorf("mp3: bitrate_index = 15 is invalid!\nHeader word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
if C.g_frame_header.sampling_frequency == 3 { if theMPEG1FrameHeader.sampling_frequency == 3 {
return fmt.Errorf("mp3: sampling_frequency = 3 is invalid! Header word is 0x%08x at file pos %d", return fmt.Errorf("mp3: sampling_frequency = 3 is invalid! Header word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
if C.g_frame_header.layer == 0 { if theMPEG1FrameHeader.layer == 0 {
return fmt.Errorf("mp3: layer = 0 is invalid! Header word is 0x%08x at file pos %d", return fmt.Errorf("mp3: layer = 0 is invalid! Header word is 0x%08x at file pos %d",
header, getFilepos()) header, getFilepos())
} }
C.g_frame_header.layer = 4 - C.g_frame_header.layer theMPEG1FrameHeader.layer = 4 - theMPEG1FrameHeader.layer
return nil return nil
} }
@ -185,7 +183,7 @@ func readHuffman(part_2_start, gr, ch int) error {
region_1_start = 36 /* sfb[9/3]*3=36 */ region_1_start = 36 /* sfb[9/3]*3=36 */
region_2_start = 576 /* No Region2 for short block case. */ region_2_start = 576 /* No Region2 for short block case. */
} else { } else {
sfreq := C.g_frame_header.sampling_frequency sfreq := theMPEG1FrameHeader.sampling_frequency
region_1_start = region_1_start =
sfBandIndicesSet[sfreq].l[theMPEG1SideInfo.region0_count[gr][ch]+1] sfBandIndicesSet[sfreq].l[theMPEG1SideInfo.region0_count[gr][ch]+1]
region_2_start = region_2_start =
@ -193,7 +191,7 @@ func readHuffman(part_2_start, gr, ch int) error {
theMPEG1SideInfo.region1_count[gr][ch]+2] theMPEG1SideInfo.region1_count[gr][ch]+2]
} }
/* Read big_values using tables according to region_x_start */ /* Read big_values using tables according to region_x_start */
for is_pos := 0; is_pos < int(theMPEG1SideInfo.big_values[gr][ch])*2; is_pos++ { for is_pos := 0; is_pos < theMPEG1SideInfo.big_values[gr][ch]*2; is_pos++ {
table_num := 0 table_num := 0
if is_pos < region_1_start { if is_pos < region_1_start {
table_num = int(theMPEG1SideInfo.table_select[gr][ch][0]) table_num = int(theMPEG1SideInfo.table_select[gr][ch][0])

View File

@ -17,8 +17,6 @@
package mp3 package mp3
// #include "pdmp3.h" // #include "pdmp3.h"
//
// extern t_mpeg1_header g_frame_header;
import "C" import "C"
import ( import (
@ -45,14 +43,14 @@ var g_sampling_frequency = [3]int{44100, 48000, 32000}
func readAudioL3() error { func readAudioL3() error {
nch := 2 nch := 2
if C.g_frame_header.mode == C.mpeg1_mode_single_channel { if theMPEG1FrameHeader.mode == mpeg1ModeSingleChannel {
nch = 1 nch = 1
} }
/* Calculate header audio data size */ /* Calculate header audio data size */
framesize := (144* framesize := (144*
g_mpeg1_bitrates[C.g_frame_header.layer-1][C.g_frame_header.bitrate_index])/ g_mpeg1_bitrates[theMPEG1FrameHeader.layer-1][theMPEG1FrameHeader.bitrate_index])/
g_sampling_frequency[C.g_frame_header.sampling_frequency] + g_sampling_frequency[theMPEG1FrameHeader.sampling_frequency] +
int(C.g_frame_header.padding_bit) int(theMPEG1FrameHeader.padding_bit)
if framesize > 2000 { if framesize > 2000 {
return fmt.Errorf("mp3: framesize = %d\n", framesize) return fmt.Errorf("mp3: framesize = %d\n", framesize)
} }
@ -64,7 +62,7 @@ func readAudioL3() error {
/* Main data size is the rest of the frame,including ancillary data */ /* Main data size is the rest of the frame,including ancillary data */
main_data_size := framesize - sideinfo_size - 4 /* sync+header */ main_data_size := framesize - sideinfo_size - 4 /* sync+header */
/* CRC is 2 bytes */ /* CRC is 2 bytes */
if C.g_frame_header.protection_bit == 0 { if theMPEG1FrameHeader.protection_bit == 0 {
main_data_size -= 2 main_data_size -= 2
} }
/* Read sideinfo from bitstream into buffer used by getSideBits() */ /* Read sideinfo from bitstream into buffer used by getSideBits() */
@ -75,7 +73,7 @@ func readAudioL3() error {
/* Pointer to where we should start reading main data */ /* Pointer to where we should start reading main data */
theMPEG1SideInfo.main_data_begin = getSideBits(9) theMPEG1SideInfo.main_data_begin = getSideBits(9)
/* Get private bits. Not used for anything. */ /* Get private bits. Not used for anything. */
if C.g_frame_header.mode == C.mpeg1_mode_single_channel { if theMPEG1FrameHeader.mode == mpeg1ModeSingleChannel {
theMPEG1SideInfo.private_bits = getSideBits(5) theMPEG1SideInfo.private_bits = getSideBits(5)
} else { } else {
theMPEG1SideInfo.private_bits = getSideBits(3) theMPEG1SideInfo.private_bits = getSideBits(3)