Revision 386268df libavcodec/ac3enc.c
libavcodec/ac3enc.c | ||
---|---|---|
1115 | 1115 |
|
1116 | 1116 |
|
1117 | 1117 |
/** |
1118 |
* Downgrade exponent strategies to reduce the bits used by the exponents. |
|
1119 |
* This is a fallback for when bit allocation fails with the normal exponent |
|
1120 |
* strategies. Each time this function is run it only downgrades the |
|
1121 |
* strategy in 1 channel of 1 block. |
|
1122 |
* @return non-zero if downgrade was unsuccessful |
|
1123 |
*/ |
|
1124 |
static int downgrade_exponents(AC3EncodeContext *s) |
|
1125 |
{ |
|
1126 |
int ch, blk; |
|
1127 |
|
|
1128 |
for (ch = 0; ch < s->fbw_channels; ch++) { |
|
1129 |
for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) { |
|
1130 |
if (s->blocks[blk].exp_strategy[ch] == EXP_D15) { |
|
1131 |
s->blocks[blk].exp_strategy[ch] = EXP_D25; |
|
1132 |
return 0; |
|
1133 |
} |
|
1134 |
} |
|
1135 |
} |
|
1136 |
for (ch = 0; ch < s->fbw_channels; ch++) { |
|
1137 |
for (blk = AC3_MAX_BLOCKS-1; blk >= 0; blk--) { |
|
1138 |
if (s->blocks[blk].exp_strategy[ch] == EXP_D25) { |
|
1139 |
s->blocks[blk].exp_strategy[ch] = EXP_D45; |
|
1140 |
return 0; |
|
1141 |
} |
|
1142 |
} |
|
1143 |
} |
|
1144 |
for (ch = 0; ch < s->fbw_channels; ch++) { |
|
1145 |
/* block 0 cannot reuse exponents, so only downgrade D45 to REUSE if |
|
1146 |
the block number > 0 */ |
|
1147 |
for (blk = AC3_MAX_BLOCKS-1; blk > 0; blk--) { |
|
1148 |
if (s->blocks[blk].exp_strategy[ch] > EXP_REUSE) { |
|
1149 |
s->blocks[blk].exp_strategy[ch] = EXP_REUSE; |
|
1150 |
return 0; |
|
1151 |
} |
|
1152 |
} |
|
1153 |
} |
|
1154 |
return -1; |
|
1155 |
} |
|
1156 |
|
|
1157 |
|
|
1158 |
/** |
|
1159 |
* Reduce the bandwidth to reduce the number of bits used for a given SNR offset. |
|
1160 |
* This is a second fallback for when bit allocation still fails after exponents |
|
1161 |
* have been downgraded. |
|
1162 |
* @return non-zero if bandwidth reduction was unsuccessful |
|
1163 |
*/ |
|
1164 |
static int reduce_bandwidth(AC3EncodeContext *s, int min_bw_code) |
|
1165 |
{ |
|
1166 |
int ch; |
|
1167 |
|
|
1168 |
if (s->bandwidth_code[0] > min_bw_code) { |
|
1169 |
for (ch = 0; ch < s->fbw_channels; ch++) { |
|
1170 |
s->bandwidth_code[ch]--; |
|
1171 |
s->nb_coefs[ch] = s->bandwidth_code[ch] * 3 + 73; |
|
1172 |
} |
|
1173 |
return 0; |
|
1174 |
} |
|
1175 |
return -1; |
|
1176 |
} |
|
1177 |
|
|
1178 |
|
|
1179 |
/** |
|
1118 | 1180 |
* Perform bit allocation search. |
1119 | 1181 |
* Finds the SNR offset value that maximizes quality and fits in the specified |
1120 | 1182 |
* frame size. Output is the SNR offset and a set of bit allocation pointers |
... | ... | |
1122 | 1184 |
*/ |
1123 | 1185 |
static int compute_bit_allocation(AC3EncodeContext *s) |
1124 | 1186 |
{ |
1187 |
int ret; |
|
1188 |
|
|
1125 | 1189 |
count_frame_bits(s); |
1126 | 1190 |
|
1127 | 1191 |
bit_alloc_masking(s); |
1128 | 1192 |
|
1129 |
return cbr_bit_allocation(s); |
|
1193 |
ret = cbr_bit_allocation(s); |
|
1194 |
while (ret) { |
|
1195 |
/* fallback 1: downgrade exponents */ |
|
1196 |
if (!downgrade_exponents(s)) { |
|
1197 |
extract_exponents(s); |
|
1198 |
encode_exponents(s); |
|
1199 |
group_exponents(s); |
|
1200 |
ret = compute_bit_allocation(s); |
|
1201 |
continue; |
|
1202 |
} |
|
1203 |
|
|
1204 |
/* fallback 2: reduce bandwidth */ |
|
1205 |
/* only do this if the user has not specified a specific cutoff |
|
1206 |
frequency */ |
|
1207 |
if (!s->cutoff && !reduce_bandwidth(s, 0)) { |
|
1208 |
process_exponents(s); |
|
1209 |
ret = compute_bit_allocation(s); |
|
1210 |
continue; |
|
1211 |
} |
|
1212 |
|
|
1213 |
/* fallbacks were not enough... */ |
|
1214 |
break; |
|
1215 |
} |
|
1216 |
|
|
1217 |
return ret; |
|
1130 | 1218 |
} |
1131 | 1219 |
|
1132 | 1220 |
|
Also available in: Unified diff