File: ac3.c

package info (click to toggle)
libmpeg3 1.8.dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 3,900 kB
  • ctags: 2,881
  • sloc: ansic: 18,524; asm: 761; makefile: 301; sh: 26
file content (174 lines) | stat: -rw-r--r-- 3,379 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include "a52.h"
#include "mpeg3private.h"
#include "mpeg3protos.h"

#include <string.h>


mpeg3_ac3_t* mpeg3_new_ac3()
{
	mpeg3_ac3_t *result = calloc(1, sizeof(mpeg3_ac3_t));
	result->stream = mpeg3bits_new_stream(0, 0);
	result->state = a52_init(0);
	result->output = a52_samples(result->state);
	return result;
}

void mpeg3_delete_ac3(mpeg3_ac3_t *audio)
{
	mpeg3bits_delete_stream(audio->stream);
	a52_free(audio->state);
	free(audio);
}


/* Return 1 if it isn't an AC3 header */
int mpeg3_ac3_check(unsigned char *header)
{
	int flags, samplerate, bitrate;
	return !a52_syncinfo(header,
		&flags,
		&samplerate,
		&bitrate);
}

/* Decode AC3 header */
int mpeg3_ac3_header(mpeg3_ac3_t *audio, unsigned char *header)
{
	int result = 0;
	audio->flags = 0;

//printf("mpeg3_ac3_header %02x%02x%02x%02x%02x%02x%02x%02x\n", header[0], header[1], header[2], header[3], header[4], header[5], header[6], header[7]);
	result = a52_syncinfo(header, 
		&audio->flags,
        &audio->samplerate, 
		&audio->bitrate);


	if(result)
	{
//printf("%d\n", result);
		audio->framesize = result;
		audio->channels = 0;

		if(audio->flags & A52_LFE)
			audio->channels++;

/*
 * printf("mpeg3_ac3_header %08x %08x\n", 
 * audio->flags & A52_LFE, 
 * audio->flags & A52_CHANNEL_MASK);
 */
		switch(audio->flags & A52_CHANNEL_MASK)
		{
			case A52_CHANNEL:
				audio->channels++;
				break;
			case A52_MONO:
				audio->channels++;
				break;
			case A52_STEREO:
				audio->channels += 2;
				break;
			case A52_3F:
				audio->channels += 3;
				break;
			case A52_2F1R:
				audio->channels += 3;
				break;
			case A52_3F1R:
				audio->channels += 4;
				break;
			case A52_2F2R:
				audio->channels += 4;
				break;
			case A52_3F2R:
				audio->channels += 5;
				break;
			case A52_DOLBY:
				audio->channels += 2;
				break;
			default:
				printf("mpeg3_ac3_header: unknown channel code: %x\n", audio->flags & A52_CHANNEL_MASK);
				break;
		}
	}
//printf("mpeg3_ac3_header 1 %d\n", audio->channels);
	return result;
}


int mpeg3audio_doac3(mpeg3_ac3_t *audio, 
	char *frame, 
	int frame_size, 
	float **output,
	int render)
{
	int output_position = 0;
	sample_t level = 1;
	int i, j, k, l;

//printf("mpeg3audio_doac3 1\n");
	a52_frame(audio->state, 
		frame, 
		&audio->flags,
	   	&level, 
		0);
//printf("mpeg3audio_doac3 2\n");
	a52_dynrng(audio->state, NULL, NULL);
//printf("mpeg3audio_doac3 3\n");
	for(i = 0; i < 6; i++)
	{
		if(!a52_block(audio->state))
		{
			l = 0;
			if(render)
			{
// Remap the channels to conform to encoders.
				for(j = 0; j < audio->channels; j++)
				{
					int dst_channel = j;

// Make LFE last channel.
// Shift all other channels down 1.
					if((audio->flags & A52_LFE))
					{
						if(j == 0)
							dst_channel = audio->channels - 1;
						else
							dst_channel--;
					}

// Swap front left and center for certain configurations
					switch(audio->flags & A52_CHANNEL_MASK)
					{
						case A52_3F:
						case A52_3F1R:
						case A52_3F2R:
							if(dst_channel == 0) dst_channel = 1;
							else
							if(dst_channel == 1) dst_channel = 0;
							break;
					}

					for(k = 0; k < 256; k++)
					{
						output[dst_channel][output_position + k] = ((sample_t*)audio->output)[l];
						l++;
					}
				}
			}
			output_position += 256;
		}
	}


	return output_position;
}