00001 #include <stdint.h>
00002 #include <stdio.h>
00003
00004 #include <a52dec/a52.h>
00005 #include "mpeg3private.h"
00006 #include "mpeg3protos.h"
00007
00008 #include <string.h>
00009
00010
00011 mpeg3_ac3_t* mpeg3_new_ac3()
00012 {
00013 mpeg3_ac3_t *result = calloc(1, sizeof(mpeg3_ac3_t));
00014 result->stream = mpeg3bits_new_stream(0, 0);
00015 result->state = a52_init(0);
00016 result->output = a52_samples(result->state);
00017 return result;
00018 }
00019
00020 void mpeg3_delete_ac3(mpeg3_ac3_t *audio)
00021 {
00022 mpeg3bits_delete_stream(audio->stream);
00023 a52_free(audio->state);
00024 free(audio);
00025 }
00026
00027
00028
00029 int mpeg3_ac3_check(unsigned char *header)
00030 {
00031 int flags, samplerate, bitrate;
00032 return !a52_syncinfo(header,
00033 &flags,
00034 &samplerate,
00035 &bitrate);
00036 }
00037
00038
00039 int mpeg3_ac3_header(mpeg3_ac3_t *audio, unsigned char *header)
00040 {
00041 int result = 0;
00042 audio->flags = 0;
00043
00044
00045 result = a52_syncinfo(header,
00046 &audio->flags,
00047 &audio->samplerate,
00048 &audio->bitrate);
00049
00050
00051 if(result)
00052 {
00053
00054 audio->framesize = result;
00055 audio->channels = 0;
00056
00057 if(audio->flags & A52_LFE)
00058 audio->channels++;
00059
00060
00061
00062
00063
00064 switch(audio->flags & A52_CHANNEL_MASK)
00065 {
00066 case A52_CHANNEL:
00067 audio->channels++;
00068 break;
00069 case A52_MONO:
00070 audio->channels++;
00071 break;
00072 case A52_STEREO:
00073 audio->channels += 2;
00074 break;
00075 case A52_3F:
00076 audio->channels += 3;
00077 break;
00078 case A52_2F1R:
00079 audio->channels += 3;
00080 break;
00081 case A52_3F1R:
00082 audio->channels += 4;
00083 break;
00084 case A52_2F2R:
00085 audio->channels += 4;
00086 break;
00087 case A52_3F2R:
00088 audio->channels += 5;
00089 break;
00090 case A52_DOLBY:
00091 audio->channels += 2;
00092 break;
00093 default:
00094 printf("mpeg3_ac3_header: unknown channel code: %p\n", audio->flags & A52_CHANNEL_MASK);
00095 break;
00096 }
00097 }
00098
00099 return result;
00100 }
00101
00102
00103 int mpeg3audio_doac3(mpeg3_ac3_t *audio,
00104 char *frame,
00105 int frame_size,
00106 float **output,
00107 int render)
00108 {
00109 int output_position = 0;
00110 sample_t level = 1;
00111 int i, j, k, l;
00112
00113
00114 a52_frame(audio->state,
00115 frame,
00116 &audio->flags,
00117 &level,
00118 0);
00119
00120 a52_dynrng(audio->state, NULL, NULL);
00121
00122 for(i = 0; i < 6; i++)
00123 {
00124 if(!a52_block(audio->state))
00125 {
00126 l = 0;
00127 if(render)
00128 {
00129
00130 for(j = 0; j < audio->channels; j++)
00131 {
00132 int dst_channel = j;
00133
00134
00135
00136 if((audio->flags & A52_LFE))
00137 {
00138 if(j == 0)
00139 dst_channel = audio->channels - 1;
00140 else
00141 dst_channel--;
00142 }
00143
00144
00145 switch(audio->flags & A52_CHANNEL_MASK)
00146 {
00147 case A52_3F:
00148 case A52_3F1R:
00149 case A52_3F2R:
00150 if(dst_channel == 0) dst_channel = 1;
00151 else
00152 if(dst_channel == 1) dst_channel = 0;
00153 break;
00154 }
00155
00156 for(k = 0; k < 256; k++)
00157 {
00158 output[dst_channel][output_position + k] = ((sample_t*)audio->output)[l];
00159 l++;
00160 }
00161 }
00162 }
00163 output_position += 256;
00164 }
00165 }
00166
00167
00168 return output_position;
00169 }
00170
00171
00172