comparison flacenc.c @ 3365:84f29207af3a libavcodec

flacenc - rice param search patch by (Justin Ruggles jruggle earthlink net 1) search for optimal rice parameters and partition order. i also modified the stereo method estimation to use this to calculate estimated bit count instead of using just the pure sums. 2) search for the best fixed prediction order 3) constant subframe mode (good for encoding silence) Note that the regression test for the decoded wav file also changed. This is due to FFmpeg's FLAC decoder truncating the file, which it did before anyway...just at a different cutoff point. The generated FLAC files are still 100% lossless. With this update, FFmpeg's FLAC encoder has speed and compression somewhere between "flac -1" and "flac -2". On my machine, it's about 15% faster than "flac -2", and about 10% slower than "flac -1". The encoding parameters are identical to "flac -2" (fixed predictors, 1152 blocksize, partition order 0 to 3).
author michael
date Tue, 27 Jun 2006 21:01:21 +0000
parents 4ae69b5b596b
children 573fed4bf20f
comparison
equal deleted inserted replaced
3364:59c10b66fbbc 3365:84f29207af3a
37 #define FLAC_CHMODE_RIGHT_SIDE 9 37 #define FLAC_CHMODE_RIGHT_SIDE 9
38 #define FLAC_CHMODE_MID_SIDE 10 38 #define FLAC_CHMODE_MID_SIDE 10
39 39
40 #define FLAC_STREAMINFO_SIZE 34 40 #define FLAC_STREAMINFO_SIZE 34
41 41
42 typedef struct RiceContext {
43 int porder;
44 int params[256];
45 } RiceContext;
46
42 typedef struct FlacSubframe { 47 typedef struct FlacSubframe {
43 int type; 48 int type;
44 int type_code; 49 int type_code;
45 int obits; 50 int obits;
46 int order; 51 int order;
52 RiceContext rc;
47 int32_t samples[FLAC_MAX_BLOCKSIZE]; 53 int32_t samples[FLAC_MAX_BLOCKSIZE];
48 int32_t residual[FLAC_MAX_BLOCKSIZE]; 54 int32_t residual[FLAC_MAX_BLOCKSIZE];
49 } FlacSubframe; 55 } FlacSubframe;
50 56
51 typedef struct FlacFrame { 57 typedef struct FlacFrame {
64 int sr_code[2]; 70 int sr_code[2];
65 int blocksize; 71 int blocksize;
66 int max_framesize; 72 int max_framesize;
67 uint32_t frame_count; 73 uint32_t frame_count;
68 FlacFrame frame; 74 FlacFrame frame;
75 AVCodecContext *avctx;
69 } FlacEncodeContext; 76 } FlacEncodeContext;
70 77
71 static const int flac_samplerates[16] = { 78 static const int flac_samplerates[16] = {
72 0, 0, 0, 0, 79 0, 0, 0, 0,
73 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000, 80 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
103 flush_put_bits(&pb); 110 flush_put_bits(&pb);
104 /* total samples = 0 */ 111 /* total samples = 0 */
105 /* MD5 signature = 0 */ 112 /* MD5 signature = 0 */
106 } 113 }
107 114
108 #define BLOCK_TIME_MS 105 115 #define BLOCK_TIME_MS 27
109 116
110 /** 117 /**
111 * Sets blocksize based on samplerate 118 * Sets blocksize based on samplerate
112 * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds 119 * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds
113 */ 120 */
133 int freq = avctx->sample_rate; 140 int freq = avctx->sample_rate;
134 int channels = avctx->channels; 141 int channels = avctx->channels;
135 FlacEncodeContext *s = avctx->priv_data; 142 FlacEncodeContext *s = avctx->priv_data;
136 int i; 143 int i;
137 uint8_t *streaminfo; 144 uint8_t *streaminfo;
145
146 s->avctx = avctx;
138 147
139 if(avctx->sample_fmt != SAMPLE_FMT_S16) { 148 if(avctx->sample_fmt != SAMPLE_FMT_S16) {
140 return -1; 149 return -1;
141 } 150 }
142 151
242 frame->subframes[ch].samples[i] = samples[j]; 251 frame->subframes[ch].samples[i] = samples[j];
243 } 252 }
244 } 253 }
245 } 254 }
246 255
247 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n) 256
248 { 257 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
249 int i, best; 258
250 int32_t lt, rt; 259 static int find_optimal_param(uint32_t sum, int n)
251 uint64_t left, right, mid, side; 260 {
252 uint64_t score[4]; 261 int k, k_opt;
253 262 uint32_t nbits, nbits_opt;
254 /* calculate sum of squares for each channel */ 263
255 left = right = mid = side = 0; 264 k_opt = 0;
256 for(i=2; i<n; i++) { 265 nbits_opt = rice_encode_count(sum, n, 0);
257 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2]; 266 for(k=1; k<=14; k++) {
258 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2]; 267 nbits = rice_encode_count(sum, n, k);
259 mid += ABS((lt + rt) >> 1); 268 if(nbits < nbits_opt) {
260 side += ABS(lt - rt); 269 nbits_opt = nbits;
261 left += ABS(lt); 270 k_opt = k;
262 right += ABS(rt); 271 }
263 } 272 }
264 273 return k_opt;
265 /* calculate score for each mode */ 274 }
266 score[0] = left + right; 275
267 score[1] = left + side; 276 static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
268 score[2] = right + side; 277 uint32_t *sums, int n, int pred_order)
269 score[3] = mid + side; 278 {
270 279 int i;
271 /* return mode with lowest score */ 280 int k, cnt, part;
272 best = 0; 281 uint32_t all_bits;
273 for(i=1; i<4; i++) { 282
274 if(score[i] < score[best]) { 283 part = (1 << porder);
275 best = i; 284 all_bits = 0;
276 } 285
277 } 286 cnt = (n >> porder) - pred_order;
278 if(best == 0) { 287 for(i=0; i<part; i++) {
279 return FLAC_CHMODE_LEFT_RIGHT; 288 if(i == 1) cnt = (n >> porder);
280 } else if(best == 1) { 289 k = find_optimal_param(sums[i], cnt);
281 return FLAC_CHMODE_LEFT_SIDE; 290 rc->params[i] = k;
282 } else if(best == 2) { 291 all_bits += rice_encode_count(sums[i], cnt, k);
283 return FLAC_CHMODE_RIGHT_SIDE; 292 }
284 } else { 293 all_bits += (4 * part);
285 return FLAC_CHMODE_MID_SIDE; 294
286 } 295 rc->porder = porder;
287 } 296
288 297 return all_bits;
289 /** 298 }
290 * Perform stereo channel decorrelation 299
291 */ 300 static void calc_sums(int pmax, uint32_t *data, int n, int pred_order,
292 static void channel_decorrelation(FlacEncodeContext *ctx) 301 uint32_t sums[][256])
293 { 302 {
294 FlacFrame *frame; 303 int i, j;
295 int32_t *left, *right; 304 int parts, cnt;
296 int i, n; 305 uint32_t *res;
297 306
298 frame = &ctx->frame; 307 /* sums for highest level */
299 n = frame->blocksize; 308 parts = (1 << pmax);
300 left = frame->subframes[0].samples; 309 res = &data[pred_order];
301 right = frame->subframes[1].samples; 310 cnt = (n >> pmax) - pred_order;
302 311 for(i=0; i<parts; i++) {
303 if(ctx->channels != 2) { 312 if(i == 1) cnt = (n >> pmax);
304 frame->ch_mode = FLAC_CHMODE_NOT_STEREO; 313 if(i > 0) res = &data[i*cnt];
305 return; 314 sums[pmax][i] = 0;
306 } 315 for(j=0; j<cnt; j++) {
307 316 sums[pmax][i] += res[j];
308 frame->ch_mode = estimate_stereo_mode(left, right, n); 317 }
309 318 }
310 /* perform decorrelation and adjust bits-per-sample */ 319 /* sums for lower levels */
311 if(frame->ch_mode == FLAC_CHMODE_LEFT_RIGHT) { 320 for(i=pmax-1; i>=0; i--) {
312 return; 321 parts = (1 << i);
313 } 322 for(j=0; j<parts; j++) {
314 if(frame->ch_mode == FLAC_CHMODE_MID_SIDE) { 323 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
315 int32_t tmp; 324 }
316 for(i=0; i<n; i++) { 325 }
317 tmp = left[i]; 326 }
318 left[i] = (tmp + right[i]) >> 1; 327
319 right[i] = tmp - right[i]; 328 static uint32_t calc_rice_params(RiceContext *rc, int pmax, int32_t *data,
320 } 329 int n, int pred_order)
321 frame->subframes[1].obits++; 330 {
322 } else if(frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) { 331 int i;
323 for(i=0; i<n; i++) { 332 uint32_t bits, opt_bits;
324 right[i] = left[i] - right[i]; 333 int opt_porder;
325 } 334 RiceContext opt_rc;
326 frame->subframes[1].obits++; 335 uint32_t *udata;
327 } else { 336 uint32_t sums[9][256];
328 for(i=0; i<n; i++) { 337
329 left[i] -= right[i]; 338 assert(pmax >= 0 && pmax <= 8);
330 } 339
331 frame->subframes[0].obits++; 340 udata = av_malloc(n * sizeof(uint32_t));
332 } 341 for(i=0; i<n; i++) {
333 } 342 udata[i] = (2*data[i]) ^ (data[i]>>31);
334 343 }
335 static void encode_residual_verbatim(FlacEncodeContext *s, int ch) 344
336 { 345 calc_sums(pmax, udata, n, pred_order, sums);
337 FlacFrame *frame; 346
338 FlacSubframe *sub; 347 opt_porder = 0;
339 int32_t *res; 348 opt_bits = UINT32_MAX;
340 int32_t *smp; 349 for(i=0; i<=pmax; i++) {
341 int n; 350 bits = calc_optimal_rice_params(rc, i, sums[i], n, pred_order);
342 351 if(bits < opt_bits) {
343 frame = &s->frame; 352 opt_bits = bits;
344 sub = &frame->subframes[ch]; 353 opt_porder = i;
345 res = sub->residual; 354 memcpy(&opt_rc, rc, sizeof(RiceContext));
346 smp = sub->samples; 355 }
347 n = frame->blocksize; 356 }
348 357 if(opt_porder != pmax) {
349 sub->order = 0; 358 memcpy(rc, &opt_rc, sizeof(RiceContext));
350 sub->type = FLAC_SUBFRAME_VERBATIM; 359 }
351 sub->type_code = sub->type; 360
352 361 av_freep(&udata);
362 return opt_bits;
363 }
364
365 static uint32_t calc_rice_params_fixed(RiceContext *rc, int pmax, int32_t *data,
366 int n, int pred_order, int bps)
367 {
368 uint32_t bits;
369 bits = pred_order*bps + 6;
370 bits += calc_rice_params(rc, pmax, data, n, pred_order);
371 return bits;
372 }
373
374 static void encode_residual_verbatim(int32_t *res, int32_t *smp, int n)
375 {
376 assert(n > 0);
353 memcpy(res, smp, n * sizeof(int32_t)); 377 memcpy(res, smp, n * sizeof(int32_t));
354 } 378 }
355 379
356 static void encode_residual_fixed(int32_t *res, int32_t *smp, int n, int order) 380 static void encode_residual_fixed(int32_t *res, int32_t *smp, int n, int order)
357 { 381 {
377 for(i=order; i<n; i++) 401 for(i=order; i<n; i++)
378 res[i]= smp[i] - 4*smp[i-1] + 6*smp[i-2] - 4*smp[i-3] + smp[i-4]; 402 res[i]= smp[i] - 4*smp[i-1] + 6*smp[i-2] - 4*smp[i-3] + smp[i-4];
379 } 403 }
380 } 404 }
381 405
382 static void encode_residual(FlacEncodeContext *s, int ch) 406 static int get_max_p_order(int max_porder, int n, int order)
383 { 407 {
408 int porder, max_parts;
409
410 porder = max_porder;
411 while(porder > 0) {
412 max_parts = (1 << porder);
413 if(!(n % max_parts) && (n > max_parts*order)) {
414 break;
415 }
416 porder--;
417 }
418 return porder;
419 }
420
421 static int encode_residual(FlacEncodeContext *ctx, int ch)
422 {
423 int i, opt_order, porder, max_porder, n;
384 FlacFrame *frame; 424 FlacFrame *frame;
385 FlacSubframe *sub; 425 FlacSubframe *sub;
386 int32_t *res; 426 uint32_t bits[5];
387 int32_t *smp; 427 int32_t *res, *smp;
388 int n; 428
389 429 frame = &ctx->frame;
390 frame = &s->frame;
391 sub = &frame->subframes[ch]; 430 sub = &frame->subframes[ch];
392 res = sub->residual; 431 res = sub->residual;
393 smp = sub->samples; 432 smp = sub->samples;
394 n = frame->blocksize; 433 n = frame->blocksize;
395 434
396 sub->order = 2; 435 /* CONSTANT */
436 for(i=1; i<n; i++) {
437 if(smp[i] != smp[0]) break;
438 }
439 if(i == n) {
440 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
441 res[0] = smp[0];
442 return sub->obits;
443 }
444
445 /* VERBATIM */
446 if(n < 5) {
447 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
448 encode_residual_verbatim(res, smp, n);
449 return sub->obits * n;
450 }
451
452 max_porder = 3;
453
454 /* FIXED */
455 opt_order = 0;
456 bits[0] = UINT32_MAX;
457 for(i=0; i<=4; i++) {
458 encode_residual_fixed(res, smp, n, i);
459 porder = get_max_p_order(max_porder, n, i);
460 bits[i] = calc_rice_params_fixed(&sub->rc, porder, res, n, i, sub->obits);
461 if(bits[i] < bits[opt_order]) {
462 opt_order = i;
463 }
464 }
465 sub->order = opt_order;
397 sub->type = FLAC_SUBFRAME_FIXED; 466 sub->type = FLAC_SUBFRAME_FIXED;
398 sub->type_code = sub->type | sub->order; 467 sub->type_code = sub->type | sub->order;
399 encode_residual_fixed(res, smp, n, sub->order); 468 if(sub->order != 4) {
400 } 469 encode_residual_fixed(res, smp, n, sub->order);
401 470 porder = get_max_p_order(max_porder, n, sub->order);
402 static void 471 calc_rice_params_fixed(&sub->rc, porder, res, n, sub->order, sub->obits);
403 put_sbits(PutBitContext *pb, int bits, int32_t val) 472 }
473 return bits[sub->order];
474 }
475
476 static int encode_residual_v(FlacEncodeContext *ctx, int ch)
477 {
478 int i, n;
479 FlacFrame *frame;
480 FlacSubframe *sub;
481 int32_t *res, *smp;
482
483 frame = &ctx->frame;
484 sub = &frame->subframes[ch];
485 res = sub->residual;
486 smp = sub->samples;
487 n = frame->blocksize;
488
489 /* CONSTANT */
490 for(i=1; i<n; i++) {
491 if(smp[i] != smp[0]) break;
492 }
493 if(i == n) {
494 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
495 res[0] = smp[0];
496 return sub->obits;
497 }
498
499 /* VERBATIM */
500 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
501 encode_residual_verbatim(res, smp, n);
502 return sub->obits * n;
503 }
504
505 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
506 {
507 int i, best;
508 int32_t lt, rt;
509 uint64_t sum[4];
510 uint64_t score[4];
511 int k;
512
513 /* calculate sum of squares for each channel */
514 sum[0] = sum[1] = sum[2] = sum[3] = 0;
515 for(i=2; i<n; i++) {
516 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
517 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
518 sum[2] += ABS((lt + rt) >> 1);
519 sum[3] += ABS(lt - rt);
520 sum[0] += ABS(lt);
521 sum[1] += ABS(rt);
522 }
523 for(i=0; i<4; i++) {
524 k = find_optimal_param(2*sum[i], n);
525 sum[i] = rice_encode_count(2*sum[i], n, k);
526 }
527
528 /* calculate score for each mode */
529 score[0] = sum[0] + sum[1];
530 score[1] = sum[0] + sum[3];
531 score[2] = sum[1] + sum[3];
532 score[3] = sum[2] + sum[3];
533
534 /* return mode with lowest score */
535 best = 0;
536 for(i=1; i<4; i++) {
537 if(score[i] < score[best]) {
538 best = i;
539 }
540 }
541 if(best == 0) {
542 return FLAC_CHMODE_LEFT_RIGHT;
543 } else if(best == 1) {
544 return FLAC_CHMODE_LEFT_SIDE;
545 } else if(best == 2) {
546 return FLAC_CHMODE_RIGHT_SIDE;
547 } else {
548 return FLAC_CHMODE_MID_SIDE;
549 }
550 }
551
552 /**
553 * Perform stereo channel decorrelation
554 */
555 static void channel_decorrelation(FlacEncodeContext *ctx)
556 {
557 FlacFrame *frame;
558 int32_t *left, *right;
559 int i, n;
560
561 frame = &ctx->frame;
562 n = frame->blocksize;
563 left = frame->subframes[0].samples;
564 right = frame->subframes[1].samples;
565
566 if(ctx->channels != 2) {
567 frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
568 return;
569 }
570
571 frame->ch_mode = estimate_stereo_mode(left, right, n);
572
573 /* perform decorrelation and adjust bits-per-sample */
574 if(frame->ch_mode == FLAC_CHMODE_LEFT_RIGHT) {
575 return;
576 }
577 if(frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
578 int32_t tmp;
579 for(i=0; i<n; i++) {
580 tmp = left[i];
581 left[i] = (tmp + right[i]) >> 1;
582 right[i] = tmp - right[i];
583 }
584 frame->subframes[1].obits++;
585 } else if(frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
586 for(i=0; i<n; i++) {
587 right[i] = left[i] - right[i];
588 }
589 frame->subframes[1].obits++;
590 } else {
591 for(i=0; i<n; i++) {
592 left[i] -= right[i];
593 }
594 frame->subframes[0].obits++;
595 }
596 }
597
598 static void put_sbits(PutBitContext *pb, int bits, int32_t val)
404 { 599 {
405 assert(bits >= 0 && bits <= 31); 600 assert(bits >= 0 && bits <= 31);
406 601
407 put_bits(pb, bits, val & ((1<<bits)-1)); 602 put_bits(pb, bits, val & ((1<<bits)-1));
408 } 603 }
409 604
410 static void 605 static void write_utf8(PutBitContext *pb, uint32_t val)
411 write_utf8(PutBitContext *pb, uint32_t val)
412 { 606 {
413 int bytes, shift; 607 int bytes, shift;
414 608
415 if(val < 0x80){ 609 if(val < 0x80){
416 put_bits(pb, 8, val); 610 put_bits(pb, 8, val);
424 shift -= 6; 618 shift -= 6;
425 put_bits(pb, 8, 0x80 | ((val >> shift) & 0x3F)); 619 put_bits(pb, 8, 0x80 | ((val >> shift) & 0x3F));
426 } 620 }
427 } 621 }
428 622
429 static void 623 static void output_frame_header(FlacEncodeContext *s)
430 output_frame_header(FlacEncodeContext *s)
431 { 624 {
432 FlacFrame *frame; 625 FlacFrame *frame;
433 int crc; 626 int crc;
434 627
435 frame = &s->frame; 628 frame = &s->frame;
458 flush_put_bits(&s->pb); 651 flush_put_bits(&s->pb);
459 crc = av_crc(av_crc07, 0, s->pb.buf, put_bits_count(&s->pb)>>3); 652 crc = av_crc(av_crc07, 0, s->pb.buf, put_bits_count(&s->pb)>>3);
460 put_bits(&s->pb, 8, crc); 653 put_bits(&s->pb, 8, crc);
461 } 654 }
462 655
656 static void output_subframe_constant(FlacEncodeContext *s, int ch)
657 {
658 FlacSubframe *sub;
659 int32_t res;
660
661 sub = &s->frame.subframes[ch];
662 res = sub->residual[0];
663 put_sbits(&s->pb, sub->obits, res);
664 }
665
463 static void output_subframe_verbatim(FlacEncodeContext *s, int ch) 666 static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
464 { 667 {
465 int i; 668 int i;
466 FlacFrame *frame; 669 FlacFrame *frame;
467 FlacSubframe *sub; 670 FlacSubframe *sub;
474 res = sub->residual[i]; 677 res = sub->residual[i];
475 put_sbits(&s->pb, sub->obits, res); 678 put_sbits(&s->pb, sub->obits, res);
476 } 679 }
477 } 680 }
478 681
479 static void 682 static void output_residual(FlacEncodeContext *ctx, int ch)
480 output_residual(FlacEncodeContext *ctx, int ch) 683 {
481 { 684 int i, j, p, n, parts;
482 int i, j, p;
483 int k, porder, psize, res_cnt; 685 int k, porder, psize, res_cnt;
484 FlacFrame *frame; 686 FlacFrame *frame;
485 FlacSubframe *sub; 687 FlacSubframe *sub;
688 int32_t *res;
486 689
487 frame = &ctx->frame; 690 frame = &ctx->frame;
488 sub = &frame->subframes[ch]; 691 sub = &frame->subframes[ch];
692 res = sub->residual;
693 n = frame->blocksize;
489 694
490 /* rice-encoded block */ 695 /* rice-encoded block */
491 put_bits(&ctx->pb, 2, 0); 696 put_bits(&ctx->pb, 2, 0);
492 697
493 /* partition order */ 698 /* partition order */
494 porder = 0; 699 porder = sub->rc.porder;
495 psize = frame->blocksize; 700 psize = n >> porder;
496 //porder = sub->rc.porder; 701 parts = (1 << porder);
497 //psize = frame->blocksize >> porder;
498 put_bits(&ctx->pb, 4, porder); 702 put_bits(&ctx->pb, 4, porder);
499 res_cnt = psize - sub->order; 703 res_cnt = psize - sub->order;
500 704
501 /* residual */ 705 /* residual */
502 j = sub->order; 706 j = sub->order;
503 for(p=0; p<(1 << porder); p++) { 707 for(p=0; p<parts; p++) {
504 //k = sub->rc.params[p]; 708 k = sub->rc.params[p];
505 k = 9;
506 put_bits(&ctx->pb, 4, k); 709 put_bits(&ctx->pb, 4, k);
507 if(p == 1) res_cnt = psize; 710 if(p == 1) res_cnt = psize;
508 for(i=0; i<res_cnt && j<frame->blocksize; i++, j++) { 711 for(i=0; i<res_cnt && j<n; i++, j++) {
509 set_sr_golomb_flac(&ctx->pb, sub->residual[j], k, INT32_MAX, 0); 712 set_sr_golomb_flac(&ctx->pb, res[j], k, INT32_MAX, 0);
510 } 713 }
511 } 714 }
512 } 715 }
513 716
514 static void 717 static void output_subframe_fixed(FlacEncodeContext *ctx, int ch)
515 output_subframe_fixed(FlacEncodeContext *ctx, int ch)
516 { 718 {
517 int i; 719 int i;
518 FlacFrame *frame; 720 FlacFrame *frame;
519 FlacSubframe *sub; 721 FlacSubframe *sub;
520 722
545 put_bits(&s->pb, 1, 0); 747 put_bits(&s->pb, 1, 0);
546 put_bits(&s->pb, 6, sub->type_code); 748 put_bits(&s->pb, 6, sub->type_code);
547 put_bits(&s->pb, 1, 0); /* no wasted bits */ 749 put_bits(&s->pb, 1, 0); /* no wasted bits */
548 750
549 /* subframe */ 751 /* subframe */
550 if(sub->type == FLAC_SUBFRAME_VERBATIM) { 752 if(sub->type == FLAC_SUBFRAME_CONSTANT) {
753 output_subframe_constant(s, ch);
754 } else if(sub->type == FLAC_SUBFRAME_VERBATIM) {
551 output_subframe_verbatim(s, ch); 755 output_subframe_verbatim(s, ch);
552 } else { 756 } else if(sub->type == FLAC_SUBFRAME_FIXED) {
553 output_subframe_fixed(s, ch); 757 output_subframe_fixed(s, ch);
554 } 758 }
555 } 759 }
556 } 760 }
557 761
591 out_bytes = put_bits_count(&s->pb) >> 3; 795 out_bytes = put_bits_count(&s->pb) >> 3;
592 796
593 if(out_bytes > s->max_framesize || out_bytes >= buf_size) { 797 if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
594 /* frame too large. use verbatim mode */ 798 /* frame too large. use verbatim mode */
595 for(ch=0; ch<s->channels; ch++) { 799 for(ch=0; ch<s->channels; ch++) {
596 encode_residual_verbatim(s, ch); 800 encode_residual_v(s, ch);
597 } 801 }
598 init_put_bits(&s->pb, frame, buf_size); 802 init_put_bits(&s->pb, frame, buf_size);
599 output_frame_header(s); 803 output_frame_header(s);
600 output_subframes(s); 804 output_subframes(s);
601 output_frame_footer(s); 805 output_frame_footer(s);