From 03e23d111385161afc917abe21b19cf8761e0440 Mon Sep 17 00:00:00 2001 From: Andree Buschmann Date: Tue, 10 May 2011 19:04:24 +0000 Subject: Implement error handling for libfaad's memory allocation. Do not allocate PS related types dynamically anymore to minimize code changes. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29854 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/libfaad/decoder.c | 10 ++++++++++ apps/codecs/libfaad/error.c | 3 ++- apps/codecs/libfaad/ps_dec.c | 10 ---------- apps/codecs/libfaad/sbr_dec.c | 24 +++++++++++++++++------- apps/codecs/libfaad/sbr_dec.h | 7 ++----- apps/codecs/libfaad/specrec.c | 22 ++++++++++++++-------- apps/codecs/libfaad/syntax.c | 25 +++++++++++++++++++++---- 7 files changed, 66 insertions(+), 35 deletions(-) diff --git a/apps/codecs/libfaad/decoder.c b/apps/codecs/libfaad/decoder.c index 05d788597e..caedda7f55 100644 --- a/apps/codecs/libfaad/decoder.c +++ b/apps/codecs/libfaad/decoder.c @@ -346,6 +346,11 @@ int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer, hDecoder->time_out[i] = s_time_buf_2048[i]; #else hDecoder->time_out[i] = (real_t*)faad_malloc(2*FRAME_LEN*sizeof(real_t)); + if (hDecoder->time_out[i] == NULL) + { + /* could not allocate memory */ + return -1; + } #endif memset(hDecoder->time_out[i], 0, 2*FRAME_LEN); hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1; @@ -469,6 +474,11 @@ int8_t NEAACDECAPI NeAACDecInit2(NeAACDecHandle hDecoder, uint8_t *pBuffer, hDecoder->time_out[i] = s_time_buf_2048[i]; #else hDecoder->time_out[i] = (real_t*)faad_malloc(2*FRAME_LEN*sizeof(real_t)); + if (hDecoder->time_out[i] == NULL) + { + /* could not allocate memory */ + return -1; + } #endif memset(hDecoder->time_out[i], 0, 2*FRAME_LEN); hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1; diff --git a/apps/codecs/libfaad/error.c b/apps/codecs/libfaad/error.c index 8cfd761754..ff2f9c3d13 100644 --- a/apps/codecs/libfaad/error.c +++ b/apps/codecs/libfaad/error.c @@ -56,6 +56,7 @@ char *err_msg[] = { "Unexpected fill element with SBR data", "Not all elements were provided with SBR data", "LTP decoding not available", - "Output data buffer too small" + "Output data buffer too small", + "Could not allocate enough memory" }; diff --git a/apps/codecs/libfaad/ps_dec.c b/apps/codecs/libfaad/ps_dec.c index 335dac7b1c..2c7e5fdf47 100644 --- a/apps/codecs/libfaad/ps_dec.c +++ b/apps/codecs/libfaad/ps_dec.c @@ -159,10 +159,8 @@ typedef struct /* static variables */ -#ifdef FAAD_STATIC_ALLOC static hyb_info s_hyb_info; static ps_info s_ps_info; -#endif /* static function declarations */ static void ps_data_decode(ps_info *ps); @@ -204,11 +202,7 @@ static void ps_mix_phase(ps_info *ps, static hyb_info *hybrid_init() { -#ifdef FAAD_STATIC_ALLOC hyb_info *hyb = &s_hyb_info; -#else - hyb_info *hyb = (hyb_info*)faad_malloc(sizeof(hyb_info)); -#endif hyb->resolution34[0] = 12; hyb->resolution34[1] = 8; @@ -1826,11 +1820,7 @@ ps_info *ps_init(uint8_t sr_index) uint8_t i; uint8_t short_delay_band; -#ifdef FAAD_STATIC_ALLOC ps_info *ps = &s_ps_info; -#else - ps_info *ps = (ps_info*)faad_malloc(sizeof(ps_info)); -#endif memset(ps, 0, sizeof(ps_info)); (void)sr_index; diff --git a/apps/codecs/libfaad/sbr_dec.c b/apps/codecs/libfaad/sbr_dec.c index 7f6a9bbffe..678ebfe520 100644 --- a/apps/codecs/libfaad/sbr_dec.c +++ b/apps/codecs/libfaad/sbr_dec.c @@ -73,20 +73,25 @@ static void sbr_save_matrix(sbr_info *sbr, uint8_t ch); sbr_info *sbrDecodeInit(uint16_t framelength, uint8_t id_aac, uint8_t id_ele, - uint32_t sample_rate, uint8_t downSampledSBR -#ifdef DRM - , uint8_t IsDRM -#endif - ) + uint32_t sample_rate, uint8_t downSampledSBR, + uint8_t IsDRM) { (void)downSampledSBR; +#ifndef DRM + (void)IsDRM; +#endif /* Allocate sbr_info. */ #if defined(FAAD_STATIC_ALLOC) sbr_info *sbr = &s_sbr[id_ele]; #else (void)id_ele; - sbr_info *sbr =(sbr_info*)faad_malloc(sizeof(sbr_info)); + sbr_info *sbr = (sbr_info*)faad_malloc(sizeof(sbr_info)); + if (sbr == NULL) + { + /* could not allocate memory */ + return NULL; + } #endif memset(sbr, 0, sizeof(sbr_info)); @@ -95,7 +100,12 @@ sbr_info *sbrDecodeInit(uint16_t framelength, uint8_t id_aac, uint8_t id_ele, #if defined(FAAD_STATIC_ALLOC) || defined(FAAD_HAVE_XLR_IN_IRAM) p_XLR = &s_XLR; #else - p_XLR =(XLR_t*)faad_malloc(sizeof(XLR_t)); + p_XLR = (XLR_t*)faad_malloc(sizeof(XLR_t)); + if (p_XLR == NULL) + { + /* could not allocate memory */ + return NULL; + } #endif memset(p_XLR, 0, sizeof(XLR_t)); diff --git a/apps/codecs/libfaad/sbr_dec.h b/apps/codecs/libfaad/sbr_dec.h index 81dac32946..1a1f5b9b32 100644 --- a/apps/codecs/libfaad/sbr_dec.h +++ b/apps/codecs/libfaad/sbr_dec.h @@ -222,11 +222,8 @@ typedef struct } sbr_info; sbr_info *sbrDecodeInit(uint16_t framelength, uint8_t id_aac, uint8_t id_ele, - uint32_t sample_rate, uint8_t downSampledSBR -#ifdef DRM - , uint8_t IsDRM -#endif - ); + uint32_t sample_rate, uint8_t downSampledSBR, + uint8_t IsDRM); uint8_t sbrDecodeCoupleFrame(sbr_info *sbr, real_t *left_chan, real_t *right_chan, const uint8_t just_seeked, const uint8_t downSampledSBR); diff --git a/apps/codecs/libfaad/specrec.c b/apps/codecs/libfaad/specrec.c index b5c8305237..200239bddc 100644 --- a/apps/codecs/libfaad/specrec.c +++ b/apps/codecs/libfaad/specrec.c @@ -808,11 +808,14 @@ uint8_t reconstruct_single_channel(NeAACDecHandle hDecoder, ic_stream *ics, hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength, hDecoder->element_id[ele], ele, 2*get_sample_rate(hDecoder->sf_index), - hDecoder->downSampledSBR -#ifdef DRM - , 0 + hDecoder->downSampledSBR, 0); +#ifndef FAAD_STATIC_ALLOC + if (hDecoder->sbr[ele] == NULL) + { + /* could not allocate memory */ + return 28; + } #endif - ); } if (sce->ics1.window_sequence == EIGHT_SHORT_SEQUENCE) @@ -1058,11 +1061,14 @@ uint8_t reconstruct_channel_pair(NeAACDecHandle hDecoder, ic_stream *ics1, ic_st hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength, hDecoder->element_id[ele], ele, 2*get_sample_rate(hDecoder->sf_index), - hDecoder->downSampledSBR -#ifdef DRM - , 0 + hDecoder->downSampledSBR, 0); +#ifndef FAAD_STATIC_ALLOC + if (hDecoder->sbr[ele] == NULL) + { + /* could not allocate memory */ + return 28; + } #endif - ); } if (cpe->ics1.window_sequence == EIGHT_SHORT_SEQUENCE) diff --git a/apps/codecs/libfaad/syntax.c b/apps/codecs/libfaad/syntax.c index 4c7baab274..af1a50f4d8 100644 --- a/apps/codecs/libfaad/syntax.c +++ b/apps/codecs/libfaad/syntax.c @@ -1031,11 +1031,14 @@ static uint8_t fill_element(NeAACDecHandle hDecoder, bitfile *ld, drc_info *drc hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength, hDecoder->element_id[sbr_ele], sbr_ele, 2*get_sample_rate(hDecoder->sf_index), - hDecoder->downSampledSBR -#ifdef DRM - , 0 + hDecoder->downSampledSBR, 0); +#ifndef FAAD_STATIC_ALLOC + if (hDecoder->sbr[sbr_ele] == NULL) + { + /* could not allocate memory */ + return 28; + } #endif - ); } hDecoder->sbr_present_flag = 1; @@ -1249,10 +1252,24 @@ void aac_scalable_main_element(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo { hDecoder->sbr[0] = sbrDecodeInit(hDecoder->frameLength, hDecoder->element_id[0], 2*get_sample_rate(hDecoder->sf_index), 0 /* ds SBR */, 1); +#ifndef FAAD_STATIC_ALLOC + if (hDecoder->sbr[0] == NULL) + { + /* could not allocate memory */ + hInfo->error = 28; + return; + } +#endif } /* Reverse bit reading of SBR data in DRM audio frame */ revbuffer = (uint8_t*)faad_malloc(buffer_size*sizeof(uint8_t)); + if (revbuffer == NULL) + { + /* could not allocate memory */ + hInfo->error = 28; + return; + } prevbufstart = revbuffer; pbufend = &buffer[buffer_size - 1]; for (i = 0; i < buffer_size; i++) -- cgit v1.2.3