File: ac3.c

package info (click to toggle)
libmpeg3 1.5.4-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze, wheezy
  • size: 992 kB
  • ctags: 1,485
  • sloc: ansic: 17,241; asm: 761; makefile: 276; sh: 14
file content (143 lines) | stat: -rw-r--r-- 2,761 bytes parent folder | download | duplicates (2)
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
#include <stdint.h>
#include <stdio.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)
	{
		audio->framesize = result;
		audio->channels = 0;

		if(audio->flags & A52_LFE)
			audio->channels++;
//printf("mpeg3_ac3_header %02x\n", 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: %p\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)
			{
				for(j = 0; j < audio->channels; j++)
				{
					for(k = 0; k < 256; k++)
					{
						output[j][output_position + k] = ((sample_t*)audio->output)[l];
						l++;
					}
				}
			}
			output_position += 256;
		}
	}
//printf("mpeg3audio_doac3 4 %d\n", output_position);


	return output_position;
}