History

some small changes... - less memory usage - no more mallocing on every frame

menno committed 23 years ago

Commit e8edc601 parent 2569c4733 files · +866 −726Browse files

Showing the changes to libfaad/decoder.c (1 of 33 files). Show all

Unified · Split

libfaad/decoder.c+48−243
@@ -1,19 +1,19 @@
11 /*
22 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
33 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
4−**
4+**
55 ** This program is free software; you can redistribute it and/or modify
66 ** it under the terms of the GNU General Public License as published by
77 ** the Free Software Foundation; either version 2 of the License, or
88 ** (at your option) any later version.
9−**
9+**
1010 ** This program is distributed in the hope that it will be useful,
1111 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
1212 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1313 ** GNU General Public License for more details.
14−**
14+**
1515 ** You should have received a copy of the GNU General Public License
16−** along with this program; if not, write to the Free Software
16+** along with this program; if not, write to the Free Software
1717 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1818 **
1919 ** Any non-GPL usage of this software or parts of this software is strictly
@@ -22,7 +22,7 @@ · 2 unmodified lines
2222 ** Commercial non-GPL licensing of this software is possible.
2323 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
2424 **
25−** $Id: decoder.c,v 1.73 2003/10/05 15:22:10 menno Exp $
25+** $Id: decoder.c,v 1.74 2003/10/09 20:04:24 menno Exp $
2626 **/
2727
2828 #include "common.h"
@@ -34,20 +34,9 @@ · 5 unmodified lines
3434 #include "decoder.h"
3535 #include "mp4.h"
3636 #include "syntax.h"
37−#include "tns.h"
38−#include "pns.h"
39−#include "is.h"
40−#include "ms.h"
41−#include "ic_predict.h"
42−#include "lt_predict.h"
43−#include "drc.h"
4437 #include "error.h"
4538 #include "output.h"
4639 #include "dither.h"
47−#ifdef SSR_DEC
48−#include "ssr.h"
49−#include "ssr_fb.h"
50−#endif
5140 #ifdef SBR_DEC
5241 #include "sbr_dec.h"
5342 #endif
@@ -102,6 +91,7 @@ faacDecHandle FAADAPI faacDecOpen() · 48 unmodified lines
10291 hDecoder->config.outputFormat = FAAD_FMT_16BIT;
10392 hDecoder->config.defObjectType = MAIN;
10493 hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
94+ hDecoder->config.downMatrix = 0;
10595 hDecoder->adts_header_present = 0;
10696 hDecoder->adif_header_present = 0;
10797 #ifdef ERROR_RESILIENCE
@@ -377,7 +367,6 @@ int8_t FAADAPI faacDecInit2(faacDecHandle hDecoder, uint8_t *pBuffer, · 269 unmodified lines
377367 return 0;
378368 }
379369
380−#ifdef DRM
381370 int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate,
382371 uint8_t channels)
383372 {
@@ -385,9 +374,11 @@ int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate, · 1 unmodified line
385374 hDecoder->config.defObjectType = DRM_ER_LC;
386375
387376 hDecoder->config.defSampleRate = samplerate;
377+#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
388378 hDecoder->aacSectionDataResilienceFlag = 1; /* VCB11 */
389379 hDecoder->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
390380 hDecoder->aacSpectralDataResilienceFlag = 1; /* HCR */
381+#endif
391382 hDecoder->frameLength = 960;
392383 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
393384 hDecoder->object_type = hDecoder->config.defObjectType;
@@ -398,6 +389,7 @@ int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate, · 4 unmodified lines
398389 hDecoder->channelConfiguration = 1;
399390
400391 #ifdef SBR_DEC
392+#ifdef DRM
401393 if (channels == DRMCH_SBR_LC_STEREO)
402394 hDecoder->lcstereo_flag = 1;
403395 else
@@ -411,6 +403,7 @@ int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate, · 7 unmodified lines
411403 /* Reset sbr for new initialization */
412404 sbrDecodeEnd(hDecoder->sbr[0]);
413405 hDecoder->sbr[0] = NULL;
406+#endif
414407 #endif
415408
416409 /* must be done before frameLength is divided by 2 for LD */
@@ -423,7 +416,6 @@ int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate, · 6 unmodified lines
423416
424417 return 0;
425418 }
426−#endif
427419
428420 void FAADAPI faacDecClose(faacDecHandle hDecoder)
429421 {
@@ -699,7 +691,6 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 269 unmodified lines
699691 uint8_t *buffer, uint32_t buffer_size)
700692 {
701693 int32_t i;
702− uint8_t ch;
703694 adts_header adts;
704695 uint8_t channels = 0, ch_ele = 0;
705696 uint8_t output_channels = 0;
@@ -707,8 +698,8 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 1 unmodified line
707698 uint32_t bitsconsumed;
708699 #ifdef DRM
709700 uint8_t *revbuffer;
710− uint8_t *prevbufstart;
711− uint8_t *pbufend;
701+ uint8_t *prevbufstart;
702+ uint8_t *pbufend;
712703 #endif
713704
714705 /* local copy of globals */
@@ -736,9 +727,6 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 21 unmodified lines
736727 program_config *pce;
737728
738729 void *sample_buffer;
739− element *syntax_elements[MAX_SYNTAX_ELEMENTS];
740− element **elements;
741− real_t *spec_coef[MAX_CHANNELS];
742730
743731 /* safety checks */
744732 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL) || (ld == NULL))
@@ -804,11 +792,8 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 59 unmodified lines
804792 dbg_count = 0;
805793 #endif
806794
807− elements = syntax_elements;
808−
809795 /* decode the complete bitstream */
810− elements = raw_data_block(hDecoder, hInfo, ld, syntax_elements,
811− spec_coef, pce, drc);
796+ raw_data_block(hDecoder, hInfo, ld, pce, drc);
812797
813798 ch_ele = hDecoder->fr_ch_ele;
814799 channels = hDecoder->fr_channels;
@@ -912,202 +897,27 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 97 unmodified lines
912897 return NULL;
913898 }
914899
900+ /* allocate the buffer for the final samples */
915901 if (hDecoder->sample_buffer == NULL)
916902 {
903+ static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
904+ sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
905+ sizeof(int16_t), sizeof(int16_t), 0, 0, 0
906+ };
907+ uint8_t stride = str[outputFormat-1];
917908 #ifdef SBR_DEC
918909 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
919− {
920− if (hDecoder->config.outputFormat == FAAD_FMT_DOUBLE)
921− hDecoder->sample_buffer = malloc(2*frame_len*channels*sizeof(double));
922− else
923− hDecoder->sample_buffer = malloc(2*frame_len*channels*sizeof(real_t));
924− } else {
925−#endif
926− if (hDecoder->config.outputFormat == FAAD_FMT_DOUBLE)
927− hDecoder->sample_buffer = malloc(frame_len*channels*sizeof(double));
928− else
929− hDecoder->sample_buffer = malloc(frame_len*channels*sizeof(real_t));
930−#ifdef SBR_DEC
931− }
910+ stride = 2 * stride;
932911 #endif
912+ hDecoder->sample_buffer = malloc(frame_len*channels*stride);
933913 }
934914
935915 sample_buffer = hDecoder->sample_buffer;
936916
937−
938− /* Because for ms, is and pns both channels spectral coefficients are needed
939− we have to restart running through all channels here.
940− */
941− for (ch = 0; ch < channels; ch++)
942− {
943− int16_t pch = -1;
944− uint8_t right_channel;
945− ic_stream *ics, *icsr;
946− ltp_info *ltp;
947−
948− /* find the syntax element to which this channel belongs */
949− if (syntax_elements[hDecoder->channel_element[ch]]->channel == ch)
950− {
951− ics = &(syntax_elements[hDecoder->channel_element[ch]]->ics1);
952− icsr = &(syntax_elements[hDecoder->channel_element[ch]]->ics2);
953− ltp = &(ics->ltp);
954− pch = syntax_elements[hDecoder->channel_element[ch]]->paired_channel;
955− right_channel = 0;
956− } else if (syntax_elements[hDecoder->channel_element[ch]]->paired_channel == ch) {
957− ics = &(syntax_elements[hDecoder->channel_element[ch]]->ics2);
958− if (syntax_elements[hDecoder->channel_element[ch]]->common_window)
959− ltp = &(ics->ltp2);
960− else
961− ltp = &(ics->ltp);
962− right_channel = 1;
963− }
964−
965− /* pns decoding */
966− if ((!right_channel) && (pch != -1) && (ics->ms_mask_present))
967− pns_decode(ics, icsr, spec_coef[ch], spec_coef[pch], frame_len, 1, object_type);
968− else if ((pch == -1) || ((pch != -1) && (!ics->ms_mask_present)))
969− pns_decode(ics, NULL, spec_coef[ch], NULL, frame_len, 0, object_type);
970−
971− if (!right_channel && (pch != -1))
972− {
973− /* mid/side decoding */
974− ms_decode(ics, icsr, spec_coef[ch], spec_coef[pch], frame_len);
975−
976− /* intensity stereo decoding */
977− is_decode(ics, icsr, spec_coef[ch], spec_coef[pch], frame_len);
978− }
979−
980−#ifdef MAIN_DEC
981− /* MAIN object type prediction */
982− if (object_type == MAIN)
983− {
984− /* allocate the state only when needed */
985− if (pred_stat[ch] == NULL)
986− {
987− pred_stat[ch] = (pred_state*)malloc(frame_len * sizeof(pred_state));
988− reset_all_predictors(pred_stat[ch], frame_len);
989− }
990−
991− /* intra channel prediction */
992− ic_prediction(ics, spec_coef[ch], pred_stat[ch], frame_len);
993−
994− /* In addition, for scalefactor bands coded by perceptual
995− noise substitution the predictors belonging to the
996− corresponding spectral coefficients are reset.
997− */
998− pns_reset_pred_state(ics, pred_stat[ch]);
999− }
1000−#endif
1001−#ifdef LTP_DEC
1002− if ((object_type == LTP)
1003−#ifdef ERROR_RESILIENCE
1004− || (object_type == ER_LTP)
1005−#endif
1006−#ifdef LD_DEC
1007− || (object_type == LD)
1008−#endif
1009− )
1010− {
1011−#ifdef LD_DEC
1012− if (object_type == LD)
1013− {
1014− if (ltp->data_present)
1015− {
1016− if (ltp->lag_update)
1017− ltp_lag[ch] = ltp->lag;
1018− }
1019− ltp->lag = ltp_lag[ch];
1020− }
1021−#endif
1022−
1023− /* allocate the state only when needed */
1024− if (lt_pred_stat[ch] == NULL)
1025− {
1026− lt_pred_stat[ch] = (real_t*)malloc(frame_len*4 * sizeof(real_t));
1027− memset(lt_pred_stat[ch], 0, frame_len*4 * sizeof(real_t));
1028− }
1029−
1030− /* long term prediction */
1031− lt_prediction(ics, ltp, spec_coef[ch], lt_pred_stat[ch], fb,
1032− ics->window_shape, window_shape_prev[ch],
1033− sf_index, object_type, frame_len);
1034− }
1035−#endif
1036−
1037− /* tns decoding */
1038− tns_decode_frame(ics, &(ics->tns), sf_index, object_type,
1039− spec_coef[ch], frame_len);
1040−
1041− /* drc decoding */
1042− if (drc->present)
1043− {
1044− if (!drc->exclude_mask[ch] || !drc->excluded_chns_present)
1045− drc_decode(drc, spec_coef[ch]);
1046− }
1047−
1048− if (time_out[ch] == NULL)
1049− {
1050− time_out[ch] = (real_t*)malloc(frame_len*2*sizeof(real_t));
1051− memset(time_out[ch], 0, frame_len*2*sizeof(real_t));
1052− }
1053−#ifdef SBR_DEC
1054− if (time_out2[ch] == NULL)
1055− {
1056− time_out2[ch] = (real_t*)malloc(frame_len*2*sizeof(real_t));
1057− memset(time_out2[ch], 0, frame_len*2*sizeof(real_t));
1058− }
1059−#endif
1060−
1061− /* filter bank */
1062−#ifdef SSR_DEC
1063− if (object_type != SSR)
1064− {
1065−#endif
1066− ifilter_bank(fb, ics->window_sequence, ics->window_shape,
1067− window_shape_prev[ch], spec_coef[ch],
1068− time_out[ch], object_type, frame_len);
1069−#ifdef SSR_DEC
1070− } else {
1071− if (ssr_overlap[ch] == NULL)
1072− {
1073− ssr_overlap[ch] = (real_t*)malloc(2*frame_len*sizeof(real_t));
1074− memset(ssr_overlap[ch], 0, 2*frame_len*sizeof(real_t));
1075− }
1076− if (prev_fmd[ch] == NULL)
1077− {
1078− uint16_t k;
1079− prev_fmd[ch] = (real_t*)malloc(2*frame_len*sizeof(real_t));
1080− for (k = 0; k < 2*frame_len; k++)
1081− prev_fmd[ch][k] = REAL_CONST(-1);
1082− }
1083−
1084− ssr_decode(&(ics->ssr), fb, ics->window_sequence, ics->window_shape,
1085− window_shape_prev[ch], spec_coef[ch], time_out[ch],
1086− ssr_overlap[ch], hDecoder->ipqf_buffer[ch], prev_fmd[ch], frame_len);
1087− }
1088−#endif
1089− /* save window shape for next frame */
1090− window_shape_prev[ch] = ics->window_shape;
1091−
1092−#ifdef LTP_DEC
1093− if ((object_type == LTP)
1094−#ifdef ERROR_RESILIENCE
1095− || (object_type == ER_LTP)
1096−#endif
1097−#ifdef LD_DEC
1098− || (object_type == LD)
1099−#endif
1100− )
1101− {
1102− lt_update_state(lt_pred_stat[ch], time_out[ch], time_out[ch]+frame_len,
1103− frame_len, object_type);
1104− }
1105−#endif
1106− }
1107−
1108917 #ifdef SBR_DEC
1109918 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1110919 {
920+ uint8_t ch = 0;
1111921 for (i = 0; i < ch_ele; i++)
1112922 {
1113923 /* following case can happen when forceUpSampling == 1 */
@@ -1120,25 +930,40 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 6 unmodified lines
1120930 );
1121931 hDecoder->sbr[i]->data = NULL;
1122932 hDecoder->sbr[i]->data_size = 0;
933+ hDecoder->sbr[i]->id_aac = hDecoder->element_id[i];
934+ }
935+
936+ /* Allocate space for SBR output */
937+ if (hDecoder->time_out2[ch] == NULL)
938+ {
939+ hDecoder->time_out2[ch] = (real_t*)malloc(hDecoder->frameLength*2*sizeof(real_t));
940+ memset(hDecoder->time_out2[ch], 0, hDecoder->frameLength*2*sizeof(real_t));
1123941 }
1124942
1125− if (syntax_elements[i]->paired_channel != -1)
943+ if (hDecoder->sbr[i]->id_aac == ID_CPE)
1126944 {
1127− memcpy(time_out2[syntax_elements[i]->channel],
1128− time_out[syntax_elements[i]->channel], frame_len*sizeof(real_t));
1129− memcpy(time_out2[syntax_elements[i]->paired_channel],
1130− time_out[syntax_elements[i]->paired_channel], frame_len*sizeof(real_t));
945+ /* space for 2 channels needed */
946+ if (hDecoder->time_out2[ch+1] == NULL)
947+ {
948+ hDecoder->time_out2[ch+1] = (real_t*)malloc(hDecoder->frameLength*2*sizeof(real_t));
949+ memset(hDecoder->time_out2[ch+1], 0, hDecoder->frameLength*2*sizeof(real_t));
950+ }
951+
952+ memcpy(time_out2[ch],
953+ time_out[ch], frame_len*sizeof(real_t));
954+ memcpy(time_out2[ch+1],
955+ time_out[ch+1], frame_len*sizeof(real_t));
1131956 sbrDecodeFrame(hDecoder->sbr[i],
1132− time_out2[syntax_elements[i]->channel],
1133− time_out2[syntax_elements[i]->paired_channel], ID_CPE,
957+ time_out2[ch], time_out2[ch+1],
1134958 hDecoder->postSeekResetFlag, hDecoder->forceUpSampling);
959+ ch += 2;
1135960 } else {
1136− memcpy(time_out2[syntax_elements[i]->channel],
1137− time_out[syntax_elements[i]->channel], frame_len*sizeof(real_t));
961+ memcpy(time_out2[ch],
962+ time_out[ch], frame_len*sizeof(real_t));
1138963 sbrDecodeFrame(hDecoder->sbr[i],
1139− time_out2[syntax_elements[i]->channel],
1140− NULL, ID_SCE,
964+ time_out2[ch], NULL,
1141965 hDecoder->postSeekResetFlag, hDecoder->forceUpSampling);
966+ ch++;
1142967 }
1143968 }
1144969 frame_len *= 2;
@@ -1181,16 +1006,6 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 36 unmodified lines
11811006 #endif
11821007
11831008 /* cleanup */
1184− for (ch = 0; ch < channels; ch++)
1185− {
1186− if (spec_coef[ch]) free(spec_coef[ch]);
1187− }
1188−
1189− for (i = 0; i < ch_ele; i++)
1190− {
1191− if (syntax_elements[i]) free(syntax_elements[i]);
1192− }
1193−
11941009 #ifdef ANALYSIS
11951010 fflush(stdout);
11961011 #endif
@@ -1203,16 +1018,6 @@ error: · 6 unmodified lines
12031018 if (ld) free(ld);
12041019
12051020 /* cleanup */
1206− for (ch = 0; ch < channels; ch++)
1207− {
1208− if (spec_coef[ch]) free(spec_coef[ch]);
1209− }
1210−
1211− for (i = 0; i < ch_ele; i++)
1212− {
1213− if (syntax_elements[i]) free(syntax_elements[i]);
1214− }
1215−
12161021 #ifdef ANALYSIS
12171022 fflush(stdout);
12181023 #endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t