Mercurial > libavcodec.hg
comparison mpegaudiodec.c @ 10494:ead8d96926c4 libavcodec
Reduce stack memory allocation in MP3 decoder
author | vitor |
---|---|
date | Wed, 04 Nov 2009 23:33:08 +0000 |
parents | 23ca6d60184d |
children | 95f3daa991a2 |
comparison
equal
deleted
inserted
replaced
10493:5f2ced30548b | 10494:ead8d96926c4 |
---|---|
46 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5)) | 46 #define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5)) |
47 | 47 |
48 /****************/ | 48 /****************/ |
49 | 49 |
50 #define HEADER_SIZE 4 | 50 #define HEADER_SIZE 4 |
51 | |
52 /* layer 3 "granule" */ | |
53 typedef struct GranuleDef { | |
54 uint8_t scfsi; | |
55 int part2_3_length; | |
56 int big_values; | |
57 int global_gain; | |
58 int scalefac_compress; | |
59 uint8_t block_type; | |
60 uint8_t switch_point; | |
61 int table_select[3]; | |
62 int subblock_gain[3]; | |
63 uint8_t scalefac_scale; | |
64 uint8_t count1table_select; | |
65 int region_size[3]; /* number of huffman codes in each region */ | |
66 int preflag; | |
67 int short_start, long_end; /* long/short band indexes */ | |
68 uint8_t scale_factors[40]; | |
69 int32_t sb_hybrid[SBLIMIT * 18]; /* 576 samples */ | |
70 } GranuleDef; | |
71 | 51 |
72 #include "mpegaudiodata.h" | 52 #include "mpegaudiodata.h" |
73 #include "mpegaudiodectab.h" | 53 #include "mpegaudiodectab.h" |
74 | 54 |
75 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g); | 55 static void compute_antialias_integer(MPADecodeContext *s, GranuleDef *g); |
1910 /* main layer3 decoding function */ | 1890 /* main layer3 decoding function */ |
1911 static int mp_decode_layer3(MPADecodeContext *s) | 1891 static int mp_decode_layer3(MPADecodeContext *s) |
1912 { | 1892 { |
1913 int nb_granules, main_data_begin, private_bits; | 1893 int nb_granules, main_data_begin, private_bits; |
1914 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos; | 1894 int gr, ch, blocksplit_flag, i, j, k, n, bits_pos; |
1915 GranuleDef granules[2][2], *g; | 1895 GranuleDef *g; |
1916 int16_t exponents[576]; | 1896 int16_t exponents[576]; |
1917 | 1897 |
1918 /* read side info */ | 1898 /* read side info */ |
1919 if (s->lsf) { | 1899 if (s->lsf) { |
1920 main_data_begin = get_bits(&s->gb, 8); | 1900 main_data_begin = get_bits(&s->gb, 8); |
1926 private_bits = get_bits(&s->gb, 3); | 1906 private_bits = get_bits(&s->gb, 3); |
1927 else | 1907 else |
1928 private_bits = get_bits(&s->gb, 5); | 1908 private_bits = get_bits(&s->gb, 5); |
1929 nb_granules = 2; | 1909 nb_granules = 2; |
1930 for(ch=0;ch<s->nb_channels;ch++) { | 1910 for(ch=0;ch<s->nb_channels;ch++) { |
1931 granules[ch][0].scfsi = 0; /* all scale factors are transmitted */ | 1911 s->granules[ch][0].scfsi = 0;/* all scale factors are transmitted */ |
1932 granules[ch][1].scfsi = get_bits(&s->gb, 4); | 1912 s->granules[ch][1].scfsi = get_bits(&s->gb, 4); |
1933 } | 1913 } |
1934 } | 1914 } |
1935 | 1915 |
1936 for(gr=0;gr<nb_granules;gr++) { | 1916 for(gr=0;gr<nb_granules;gr++) { |
1937 for(ch=0;ch<s->nb_channels;ch++) { | 1917 for(ch=0;ch<s->nb_channels;ch++) { |
1938 dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch); | 1918 dprintf(s->avctx, "gr=%d ch=%d: side_info\n", gr, ch); |
1939 g = &granules[ch][gr]; | 1919 g = &s->granules[ch][gr]; |
1940 g->part2_3_length = get_bits(&s->gb, 12); | 1920 g->part2_3_length = get_bits(&s->gb, 12); |
1941 g->big_values = get_bits(&s->gb, 9); | 1921 g->big_values = get_bits(&s->gb, 9); |
1942 if(g->big_values > 288){ | 1922 if(g->big_values > 288){ |
1943 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n"); | 1923 av_log(s->avctx, AV_LOG_ERROR, "big_values too big\n"); |
1944 return -1; | 1924 return -1; |
2006 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin)); | 1986 skip_bits_long(&s->gb, 8*(s->last_buf_size - main_data_begin)); |
2007 } | 1987 } |
2008 | 1988 |
2009 for(gr=0;gr<nb_granules;gr++) { | 1989 for(gr=0;gr<nb_granules;gr++) { |
2010 for(ch=0;ch<s->nb_channels;ch++) { | 1990 for(ch=0;ch<s->nb_channels;ch++) { |
2011 g = &granules[ch][gr]; | 1991 g = &s->granules[ch][gr]; |
2012 if(get_bits_count(&s->gb)<0){ | 1992 if(get_bits_count(&s->gb)<0){ |
2013 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n", | 1993 av_log(s->avctx, AV_LOG_DEBUG, "mdb:%d, lastbuf:%d skipping granule %d\n", |
2014 main_data_begin, s->last_buf_size, gr); | 1994 main_data_begin, s->last_buf_size, gr); |
2015 skip_bits_long(&s->gb, g->part2_3_length); | 1995 skip_bits_long(&s->gb, g->part2_3_length); |
2016 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid)); | 1996 memset(g->sb_hybrid, 0, sizeof(g->sb_hybrid)); |
2050 }else{ | 2030 }else{ |
2051 for(i=0;i<21;i++) | 2031 for(i=0;i<21;i++) |
2052 g->scale_factors[j++] = 0; | 2032 g->scale_factors[j++] = 0; |
2053 } | 2033 } |
2054 } else { | 2034 } else { |
2055 sc = granules[ch][0].scale_factors; | 2035 sc = s->granules[ch][0].scale_factors; |
2056 j = 0; | 2036 j = 0; |
2057 for(k=0;k<4;k++) { | 2037 for(k=0;k<4;k++) { |
2058 n = (k == 0 ? 6 : 5); | 2038 n = (k == 0 ? 6 : 5); |
2059 if ((g->scfsi & (0x8 >> k)) == 0) { | 2039 if ((g->scfsi & (0x8 >> k)) == 0) { |
2060 slen = (k < 2) ? slen1 : slen2; | 2040 slen = (k < 2) ? slen1 : slen2; |
2135 /* read Huffman coded residue */ | 2115 /* read Huffman coded residue */ |
2136 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length); | 2116 huffman_decode(s, g, exponents, bits_pos + g->part2_3_length); |
2137 } /* ch */ | 2117 } /* ch */ |
2138 | 2118 |
2139 if (s->nb_channels == 2) | 2119 if (s->nb_channels == 2) |
2140 compute_stereo(s, &granules[0][gr], &granules[1][gr]); | 2120 compute_stereo(s, &s->granules[0][gr], &s->granules[1][gr]); |
2141 | 2121 |
2142 for(ch=0;ch<s->nb_channels;ch++) { | 2122 for(ch=0;ch<s->nb_channels;ch++) { |
2143 g = &granules[ch][gr]; | 2123 g = &s->granules[ch][gr]; |
2144 | 2124 |
2145 reorder_block(s, g); | 2125 reorder_block(s, g); |
2146 s->compute_antialias(s, g); | 2126 s->compute_antialias(s, g); |
2147 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]); | 2127 compute_imdct(s, g, &s->sb_samples[ch][18 * gr][0], s->mdct_buf[ch]); |
2148 } | 2128 } |