comparison flacenc.c @ 7599:3c07ce8ac9f7 libavcodec

fix and simplify frame size check and reencoding in verbatim mode
author jbr
date Sun, 17 Aug 2008 16:10:46 +0000
parents cd6217c9ce92
children c11e175fc544
comparison
equal deleted inserted replaced
7598:60dd0089cdba 7599:3c07ce8ac9f7
1240 { 1240 {
1241 int ch; 1241 int ch;
1242 FlacEncodeContext *s; 1242 FlacEncodeContext *s;
1243 int16_t *samples = data; 1243 int16_t *samples = data;
1244 int out_bytes; 1244 int out_bytes;
1245 int reencoded=0;
1245 1246
1246 s = avctx->priv_data; 1247 s = avctx->priv_data;
1248
1249 if(buf_size < s->max_framesize*2) {
1250 av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
1251 return 0;
1252 }
1247 1253
1248 init_frame(s); 1254 init_frame(s);
1249 1255
1250 copy_samples(s, samples); 1256 copy_samples(s, samples);
1251 1257
1252 channel_decorrelation(s); 1258 channel_decorrelation(s);
1253 1259
1254 for(ch=0; ch<s->channels; ch++) { 1260 for(ch=0; ch<s->channels; ch++) {
1255 encode_residual(s, ch); 1261 encode_residual(s, ch);
1256 } 1262 }
1263
1264 write_frame:
1257 init_put_bits(&s->pb, frame, buf_size); 1265 init_put_bits(&s->pb, frame, buf_size);
1258 output_frame_header(s); 1266 output_frame_header(s);
1259 output_subframes(s); 1267 output_subframes(s);
1260 output_frame_footer(s); 1268 output_frame_footer(s);
1261 out_bytes = put_bits_count(&s->pb) >> 3; 1269 out_bytes = put_bits_count(&s->pb) >> 3;
1262 1270
1263 if(out_bytes > s->max_framesize || out_bytes >= buf_size) { 1271 if(out_bytes > s->max_framesize) {
1272 if(reencoded) {
1273 /* still too large. must be an error. */
1274 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
1275 return -1;
1276 }
1277
1264 /* frame too large. use verbatim mode */ 1278 /* frame too large. use verbatim mode */
1265 for(ch=0; ch<s->channels; ch++) { 1279 for(ch=0; ch<s->channels; ch++) {
1266 encode_residual_v(s, ch); 1280 encode_residual_v(s, ch);
1267 } 1281 }
1268 init_put_bits(&s->pb, frame, buf_size); 1282 reencoded = 1;
1269 output_frame_header(s); 1283 goto write_frame;
1270 output_subframes(s);
1271 output_frame_footer(s);
1272 out_bytes = put_bits_count(&s->pb) >> 3;
1273
1274 if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
1275 /* still too large. must be an error. */
1276 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
1277 return -1;
1278 }
1279 } 1284 }
1280 1285
1281 s->frame_count++; 1286 s->frame_count++;
1282 return out_bytes; 1287 return out_bytes;
1283 } 1288 }