comparison flacenc.c @ 12329:6644e439130d libavcodec

Calculate an exact frame size before writing. Now the buffer size requirements can be known exactly, so larger frame sizes can be safely encoded without buffer overwrite.
author jbr
date Sat, 31 Jul 2010 20:32:12 +0000
parents 6b57b1b2095c
children 18c7965807bf
comparison
equal deleted inserted replaced
12328:6b57b1b2095c 12329:6644e439130d
488 488
489 frame = &s->frame; 489 frame = &s->frame;
490 for (i = 0, j = 0; i < frame->blocksize; i++) 490 for (i = 0, j = 0; i < frame->blocksize; i++)
491 for (ch = 0; ch < s->channels; ch++, j++) 491 for (ch = 0; ch < s->channels; ch++, j++)
492 frame->subframes[ch].samples[i] = samples[j]; 492 frame->subframes[ch].samples[i] = samples[j];
493 }
494
495
496 static int rice_count_exact(int32_t *res, int n, int k)
497 {
498 int i;
499 int count = 0;
500
501 for (i = 0; i < n; i++) {
502 int32_t v = -2 * res[i] - 1;
503 v ^= v >> 31;
504 count += (v >> k) + 1 + k;
505 }
506 return count;
507 }
508
509
510 static int subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub,
511 int pred_order)
512 {
513 int p, porder, psize;
514 int i, part_end;
515 int count = 0;
516
517 /* subframe header */
518 count += 8;
519
520 /* subframe */
521 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
522 count += sub->obits;
523 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
524 count += s->frame.blocksize * sub->obits;
525 } else {
526 /* warm-up samples */
527 count += pred_order * sub->obits;
528
529 /* LPC coefficients */
530 if (sub->type == FLAC_SUBFRAME_LPC)
531 count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
532
533 /* rice-encoded block */
534 count += 2;
535
536 /* partition order */
537 porder = sub->rc.porder;
538 psize = s->frame.blocksize >> porder;
539 count += 4;
540
541 /* residual */
542 i = pred_order;
543 part_end = psize;
544 for (p = 0; p < 1 << porder; p++) {
545 int k = sub->rc.params[p];
546 count += 4;
547 count += rice_count_exact(&sub->residual[i], part_end - i, k);
548 i = part_end;
549 part_end = FFMIN(s->frame.blocksize, part_end + psize);
550 }
551 }
552
553 return count;
493 } 554 }
494 555
495 556
496 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k))) 557 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
497 558
799 if(smp[i] != smp[0]) 860 if(smp[i] != smp[0])
800 break; 861 break;
801 if (i == n) { 862 if (i == n) {
802 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT; 863 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
803 res[0] = smp[0]; 864 res[0] = smp[0];
804 return sub->obits; 865 return subframe_count_exact(s, sub, 0);
805 } 866 }
806 867
807 /* VERBATIM */ 868 /* VERBATIM */
808 if (frame->verbatim_only || n < 5) { 869 if (frame->verbatim_only || n < 5) {
809 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM; 870 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
810 memcpy(res, smp, n * sizeof(int32_t)); 871 memcpy(res, smp, n * sizeof(int32_t));
811 return sub->obits * n; 872 return subframe_count_exact(s, sub, 0);
812 } 873 }
813 874
814 min_order = s->options.min_prediction_order; 875 min_order = s->options.min_prediction_order;
815 max_order = s->options.max_prediction_order; 876 max_order = s->options.max_prediction_order;
816 omethod = s->options.prediction_order_method; 877 omethod = s->options.prediction_order_method;
832 } 893 }
833 sub->order = opt_order; 894 sub->order = opt_order;
834 sub->type_code = sub->type | sub->order; 895 sub->type_code = sub->type | sub->order;
835 if (sub->order != max_order) { 896 if (sub->order != max_order) {
836 encode_residual_fixed(res, smp, n, sub->order); 897 encode_residual_fixed(res, smp, n, sub->order);
837 return find_subframe_rice_params(s, sub, sub->order); 898 find_subframe_rice_params(s, sub, sub->order);
838 } 899 }
839 return bits[sub->order]; 900 return subframe_count_exact(s, sub, sub->order);
840 } 901 }
841 902
842 /* LPC */ 903 /* LPC */
843 sub->type = FLAC_SUBFRAME_LPC; 904 sub->type = FLAC_SUBFRAME_LPC;
844 opt_order = ff_lpc_calc_coefs(&s->dsp, smp, n, min_order, max_order, 905 opt_order = ff_lpc_calc_coefs(&s->dsp, smp, n, min_order, max_order,
906 for (i = 0; i < sub->order; i++) 967 for (i = 0; i < sub->order; i++)
907 sub->coefs[i] = coefs[sub->order-1][i]; 968 sub->coefs[i] = coefs[sub->order-1][i];
908 969
909 encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift); 970 encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
910 971
911 return find_subframe_rice_params(s, sub, sub->order); 972 find_subframe_rice_params(s, sub, sub->order);
973
974 return subframe_count_exact(s, sub, sub->order);
912 } 975 }
913 976
914 977
915 static int count_frame_header(FlacEncodeContext *s) 978 static int count_frame_header(FlacEncodeContext *s)
916 { 979 {
1195 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame, 1258 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
1196 int buf_size, void *data) 1259 int buf_size, void *data)
1197 { 1260 {
1198 FlacEncodeContext *s; 1261 FlacEncodeContext *s;
1199 const int16_t *samples = data; 1262 const int16_t *samples = data;
1200 int out_bytes; 1263 int frame_bytes, out_bytes;
1201 1264
1202 s = avctx->priv_data; 1265 s = avctx->priv_data;
1203
1204 if (buf_size < s->max_framesize * 2) {
1205 av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
1206 return 0;
1207 }
1208 1266
1209 /* when the last block is reached, update the header in extradata */ 1267 /* when the last block is reached, update the header in extradata */
1210 if (!data) { 1268 if (!data) {
1211 s->max_framesize = s->max_encoded_framesize; 1269 s->max_framesize = s->max_encoded_framesize;
1212 av_md5_final(s->md5ctx, s->md5sum); 1270 av_md5_final(s->md5ctx, s->md5sum);
1218 1276
1219 copy_samples(s, samples); 1277 copy_samples(s, samples);
1220 1278
1221 channel_decorrelation(s); 1279 channel_decorrelation(s);
1222 1280
1223 encode_frame(s); 1281 frame_bytes = encode_frame(s);
1224 1282 if (buf_size < frame_bytes) {
1283 av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
1284 return 0;
1285 }
1225 out_bytes = write_frame(s, frame, buf_size); 1286 out_bytes = write_frame(s, frame, buf_size);
1226 1287
1227 /* fallback to verbatim mode if the compressed frame is larger than it 1288 /* fallback to verbatim mode if the compressed frame is larger than it
1228 would be if encoded uncompressed. */ 1289 would be if encoded uncompressed. */
1229 if (out_bytes > s->max_framesize) { 1290 if (out_bytes > s->max_framesize) {
1230 s->frame.verbatim_only = 1; 1291 s->frame.verbatim_only = 1;
1231 encode_frame(s); 1292 frame_bytes = encode_frame(s);
1293 if (buf_size < frame_bytes) {
1294 av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
1295 return 0;
1296 }
1232 out_bytes = write_frame(s, frame, buf_size); 1297 out_bytes = write_frame(s, frame, buf_size);
1233 } 1298 }
1234 1299
1235 s->frame_count++; 1300 s->frame_count++;
1236 avctx->coded_frame->pts = s->sample_count; 1301 avctx->coded_frame->pts = s->sample_count;