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 @@
1/*1/*
2** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding2** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com3** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
4**4**
5** This program is free software; you can redistribute it and/or modify5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.8** (at your option) any later version.
9**9**
10** This program is distributed in the hope that it will be useful,10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.13** GNU General Public License for more details.
14**14**
15** You should have received a copy of the GNU General Public License15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.17** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18**18**
19** Any non-GPL usage of this software or parts of this software is strictly19** Any non-GPL usage of this software or parts of this software is strictly
@@ -22,7 +22,7 @@ · 2 unmodified lines
22** Commercial non-GPL licensing of this software is possible.22** Commercial non-GPL licensing of this software is possible.
23** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.23** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24**24**
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 $
26**/26**/
27 27
28#include "common.h"28#include "common.h"
@@ -34,20 +34,9 @@ · 5 unmodified lines
34#include "decoder.h"34#include "decoder.h"
35#include "mp4.h"35#include "mp4.h"
36#include "syntax.h"36#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"
44#include "error.h"37#include "error.h"
45#include "output.h"38#include "output.h"
46#include "dither.h"39#include "dither.h"
47#ifdef SSR_DEC
48#include "ssr.h"
49#include "ssr_fb.h"
50#endif
51#ifdef SBR_DEC40#ifdef SBR_DEC
52#include "sbr_dec.h"41#include "sbr_dec.h"
53#endif42#endif
@@ -102,6 +91,7 @@ faacDecHandle FAADAPI faacDecOpen() · 48 unmodified lines
102 hDecoder->config.outputFormat = FAAD_FMT_16BIT;91 hDecoder->config.outputFormat = FAAD_FMT_16BIT;
103 hDecoder->config.defObjectType = MAIN;92 hDecoder->config.defObjectType = MAIN;
104 hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */93 hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
94 hDecoder->config.downMatrix = 0;
105 hDecoder->adts_header_present = 0;95 hDecoder->adts_header_present = 0;
106 hDecoder->adif_header_present = 0;96 hDecoder->adif_header_present = 0;
107#ifdef ERROR_RESILIENCE97#ifdef ERROR_RESILIENCE
@@ -377,7 +367,6 @@ int8_t FAADAPI faacDecInit2(faacDecHandle hDecoder, uint8_t *pBuffer, · 269 unmodified lines
377 return 0;367 return 0;
378}368}
379 369
380#ifdef DRM
381int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate,370int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate,
382 uint8_t channels)371 uint8_t channels)
383{372{
@@ -385,9 +374,11 @@ int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate, · 1 unmodified line
385 hDecoder->config.defObjectType = DRM_ER_LC;374 hDecoder->config.defObjectType = DRM_ER_LC;
386 375
387 hDecoder->config.defSampleRate = samplerate;376 hDecoder->config.defSampleRate = samplerate;
377#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
388 hDecoder->aacSectionDataResilienceFlag = 1; /* VCB11 */378 hDecoder->aacSectionDataResilienceFlag = 1; /* VCB11 */
389 hDecoder->aacScalefactorDataResilienceFlag = 0; /* no RVLC */379 hDecoder->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
390 hDecoder->aacSpectralDataResilienceFlag = 1; /* HCR */380 hDecoder->aacSpectralDataResilienceFlag = 1; /* HCR */
381#endif
391 hDecoder->frameLength = 960;382 hDecoder->frameLength = 960;
392 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);383 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
393 hDecoder->object_type = hDecoder->config.defObjectType;384 hDecoder->object_type = hDecoder->config.defObjectType;
@@ -398,6 +389,7 @@ int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate, · 4 unmodified lines
398 hDecoder->channelConfiguration = 1;389 hDecoder->channelConfiguration = 1;
399 390
400#ifdef SBR_DEC391#ifdef SBR_DEC
392#ifdef DRM
401 if (channels == DRMCH_SBR_LC_STEREO)393 if (channels == DRMCH_SBR_LC_STEREO)
402 hDecoder->lcstereo_flag = 1;394 hDecoder->lcstereo_flag = 1;
403 else395 else
@@ -411,6 +403,7 @@ int8_t FAADAPI faacDecInitDRM(faacDecHandle hDecoder, uint32_t samplerate, · 7 unmodified lines
411 /* Reset sbr for new initialization */403 /* Reset sbr for new initialization */
412 sbrDecodeEnd(hDecoder->sbr[0]);404 sbrDecodeEnd(hDecoder->sbr[0]);
413 hDecoder->sbr[0] = NULL;405 hDecoder->sbr[0] = NULL;
406#endif
414#endif407#endif
415 408
416 /* must be done before frameLength is divided by 2 for LD */409 /* 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
423 416
424 return 0;417 return 0;
425}418}
426#endif
427 419
428void FAADAPI faacDecClose(faacDecHandle hDecoder)420void FAADAPI faacDecClose(faacDecHandle hDecoder)
429{421{
@@ -699,7 +691,6 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 269 unmodified lines
699 uint8_t *buffer, uint32_t buffer_size)691 uint8_t *buffer, uint32_t buffer_size)
700{692{
701 int32_t i;693 int32_t i;
702 uint8_t ch;
703 adts_header adts;694 adts_header adts;
704 uint8_t channels = 0, ch_ele = 0;695 uint8_t channels = 0, ch_ele = 0;
705 uint8_t output_channels = 0;696 uint8_t output_channels = 0;
@@ -707,8 +698,8 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 1 unmodified line
707 uint32_t bitsconsumed;698 uint32_t bitsconsumed;
708#ifdef DRM699#ifdef DRM
709 uint8_t *revbuffer;700 uint8_t *revbuffer;
710 uint8_t *prevbufstart;701 uint8_t *prevbufstart;
711 uint8_t *pbufend;702 uint8_t *pbufend;
712#endif703#endif
713 704
714 /* local copy of globals */705 /* local copy of globals */
@@ -736,9 +727,6 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 21 unmodified lines
736 program_config *pce;727 program_config *pce;
737 728
738 void *sample_buffer;729 void *sample_buffer;
739 element *syntax_elements[MAX_SYNTAX_ELEMENTS];
740 element **elements;
741 real_t *spec_coef[MAX_CHANNELS];
742 730
743 /* safety checks */731 /* safety checks */
744 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL) || (ld == NULL))732 if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL) || (ld == NULL))
@@ -804,11 +792,8 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 59 unmodified lines
804 dbg_count = 0;792 dbg_count = 0;
805#endif793#endif
806 794
807 elements = syntax_elements;
808
809 /* decode the complete bitstream */795 /* decode the complete bitstream */
810 elements = raw_data_block(hDecoder, hInfo, ld, syntax_elements,796 raw_data_block(hDecoder, hInfo, ld, pce, drc);
811 spec_coef, pce, drc);
812 797
813 ch_ele = hDecoder->fr_ch_ele;798 ch_ele = hDecoder->fr_ch_ele;
814 channels = hDecoder->fr_channels;799 channels = hDecoder->fr_channels;
@@ -912,202 +897,27 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 97 unmodified lines
912 return NULL;897 return NULL;
913 }898 }
914 899
900 /* allocate the buffer for the final samples */
915 if (hDecoder->sample_buffer == NULL)901 if (hDecoder->sample_buffer == NULL)
916 {902 {
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];
917#ifdef SBR_DEC908#ifdef SBR_DEC
918 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))909 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
919 {910 stride = 2 * stride;
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 }
932#endif911#endif
912 hDecoder->sample_buffer = malloc(frame_len*channels*stride);
933 }913 }
934 914
935 sample_buffer = hDecoder->sample_buffer;915 sample_buffer = hDecoder->sample_buffer;
936 916
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
1108#ifdef SBR_DEC917#ifdef SBR_DEC
1109 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))918 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1110 {919 {
920 uint8_t ch = 0;
1111 for (i = 0; i < ch_ele; i++)921 for (i = 0; i < ch_ele; i++)
1112 {922 {
1113 /* following case can happen when forceUpSampling == 1 */923 /* following case can happen when forceUpSampling == 1 */
@@ -1120,25 +930,40 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 6 unmodified lines
1120 );930 );
1121 hDecoder->sbr[i]->data = NULL;931 hDecoder->sbr[i]->data = NULL;
1122 hDecoder->sbr[i]->data_size = 0;932 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));
1123 }941 }
1124 942
1125 if (syntax_elements[i]->paired_channel != -1)943 if (hDecoder->sbr[i]->id_aac == ID_CPE)
1126 {944 {
1127 memcpy(time_out2[syntax_elements[i]->channel],945 /* space for 2 channels needed */
1128 time_out[syntax_elements[i]->channel], frame_len*sizeof(real_t));946 if (hDecoder->time_out2[ch+1] == NULL)
1129 memcpy(time_out2[syntax_elements[i]->paired_channel],947 {
1130 time_out[syntax_elements[i]->paired_channel], frame_len*sizeof(real_t));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));
1131 sbrDecodeFrame(hDecoder->sbr[i],956 sbrDecodeFrame(hDecoder->sbr[i],
1132 time_out2[syntax_elements[i]->channel],957 time_out2[ch], time_out2[ch+1],
1133 time_out2[syntax_elements[i]->paired_channel], ID_CPE,
1134 hDecoder->postSeekResetFlag, hDecoder->forceUpSampling);958 hDecoder->postSeekResetFlag, hDecoder->forceUpSampling);
959 ch += 2;
1135 } else {960 } else {
1136 memcpy(time_out2[syntax_elements[i]->channel],961 memcpy(time_out2[ch],
1137 time_out[syntax_elements[i]->channel], frame_len*sizeof(real_t));962 time_out[ch], frame_len*sizeof(real_t));
1138 sbrDecodeFrame(hDecoder->sbr[i],963 sbrDecodeFrame(hDecoder->sbr[i],
1139 time_out2[syntax_elements[i]->channel],964 time_out2[ch], NULL,
1140 NULL, ID_SCE,
1141 hDecoder->postSeekResetFlag, hDecoder->forceUpSampling);965 hDecoder->postSeekResetFlag, hDecoder->forceUpSampling);
966 ch++;
1142 }967 }
1143 }968 }
1144 frame_len *= 2;969 frame_len *= 2;
@@ -1181,16 +1006,6 @@ void* FAADAPI faacDecDecode(faacDecHandle hDecoder, · 36 unmodified lines
1181#endif1006#endif
1182 1007
1183 /* cleanup */1008 /* 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
1194#ifdef ANALYSIS1009#ifdef ANALYSIS
1195 fflush(stdout);1010 fflush(stdout);
1196#endif1011#endif
@@ -1203,16 +1018,6 @@ error: · 6 unmodified lines
1203 if (ld) free(ld);1018 if (ld) free(ld);
1204 1019
1205 /* cleanup */1020 /* 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
1216#ifdef ANALYSIS1021#ifdef ANALYSIS
1217 fflush(stdout);1022 fflush(stdout);
1218#endif1023#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