Mercurial > libavcodec.hg
comparison ac3enc.c @ 4704:0cb02a723a62 libavcodec
utilize multi-stage AC-3 bit allocation. speeds up encoding by 25-30%
author | jbr |
---|---|
date | Thu, 22 Mar 2007 05:34:26 +0000 |
parents | 3a891d2379ce |
children | 84a455e801c0 |
comparison
equal
deleted
inserted
replaced
4703:07f7ca450695 | 4704:0cb02a723a62 |
---|---|
429 } | 429 } |
430 return bits; | 430 return bits; |
431 } | 431 } |
432 | 432 |
433 | 433 |
434 static void bit_alloc_masking(AC3EncodeContext *s, | |
435 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |
436 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], | |
437 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |
438 int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50]) | |
439 { | |
440 int blk, ch; | |
441 int16_t bndpsd[NB_BLOCKS][AC3_MAX_CHANNELS][50]; | |
442 | |
443 for(blk=0; blk<NB_BLOCKS; blk++) { | |
444 for(ch=0;ch<s->nb_all_channels;ch++) { | |
445 if(exp_strategy[blk][ch] == EXP_REUSE) { | |
446 memcpy(psd[blk][ch], psd[blk-1][ch], (N/2)*sizeof(int16_t)); | |
447 memcpy(mask[blk][ch], mask[blk-1][ch], 50*sizeof(int16_t)); | |
448 } else { | |
449 ff_ac3_bit_alloc_calc_psd(encoded_exp[blk][ch], 0, | |
450 s->nb_coefs[ch], | |
451 psd[blk][ch], bndpsd[blk][ch]); | |
452 ff_ac3_bit_alloc_calc_mask(&s->bit_alloc, bndpsd[blk][ch], | |
453 0, s->nb_coefs[ch], | |
454 ff_fgaintab[s->fgaincod[ch]], | |
455 ch == s->lfe_channel, | |
456 2, 0, NULL, NULL, NULL, | |
457 mask[blk][ch]); | |
458 } | |
459 } | |
460 } | |
461 } | |
462 | |
434 static int bit_alloc(AC3EncodeContext *s, | 463 static int bit_alloc(AC3EncodeContext *s, |
464 int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50], | |
465 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |
435 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | 466 uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], |
436 uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2], | |
437 uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS], | |
438 int frame_bits, int csnroffst, int fsnroffst) | 467 int frame_bits, int csnroffst, int fsnroffst) |
439 { | 468 { |
440 int i, ch; | 469 int i, ch; |
441 | 470 |
442 /* compute size */ | 471 /* compute size */ |
443 for(i=0;i<NB_BLOCKS;i++) { | 472 for(i=0;i<NB_BLOCKS;i++) { |
444 s->mant1_cnt = 0; | 473 s->mant1_cnt = 0; |
445 s->mant2_cnt = 0; | 474 s->mant2_cnt = 0; |
446 s->mant4_cnt = 0; | 475 s->mant4_cnt = 0; |
447 for(ch=0;ch<s->nb_all_channels;ch++) { | 476 for(ch=0;ch<s->nb_all_channels;ch++) { |
448 ac3_parametric_bit_allocation(&s->bit_alloc, | 477 ff_ac3_bit_alloc_calc_bap(mask[i][ch], psd[i][ch], |
449 bap[i][ch], (int8_t *)encoded_exp[i][ch], | |
450 0, s->nb_coefs[ch], | 478 0, s->nb_coefs[ch], |
451 (((csnroffst-15) << 4) + | 479 (((csnroffst-15) << 4) + |
452 fsnroffst) << 2, | 480 fsnroffst) << 2, |
453 ff_fgaintab[s->fgaincod[ch]], | 481 s->bit_alloc.floor, bap[i][ch]); |
454 ch == s->lfe_channel, | |
455 2, 0, NULL, NULL, NULL); | |
456 frame_bits += compute_mantissa_size(s, bap[i][ch], | 482 frame_bits += compute_mantissa_size(s, bap[i][ch], |
457 s->nb_coefs[ch]); | 483 s->nb_coefs[ch]); |
458 } | 484 } |
459 } | 485 } |
460 #if 0 | 486 #if 0 |
474 int frame_bits) | 500 int frame_bits) |
475 { | 501 { |
476 int i, ch; | 502 int i, ch; |
477 int csnroffst, fsnroffst; | 503 int csnroffst, fsnroffst; |
478 uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | 504 uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; |
505 int16_t psd[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; | |
506 int16_t mask[NB_BLOCKS][AC3_MAX_CHANNELS][50]; | |
479 static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; | 507 static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; |
480 | 508 |
481 /* init default parameters */ | 509 /* init default parameters */ |
482 s->sdecaycod = 2; | 510 s->sdecaycod = 2; |
483 s->fdecaycod = 1; | 511 s->fdecaycod = 1; |
531 frame_bits += 2; | 559 frame_bits += 2; |
532 | 560 |
533 /* CRC */ | 561 /* CRC */ |
534 frame_bits += 16; | 562 frame_bits += 16; |
535 | 563 |
564 /* calculate psd and masking curve before doing bit allocation */ | |
565 bit_alloc_masking(s, encoded_exp, exp_strategy, psd, mask); | |
566 | |
536 /* now the big work begins : do the bit allocation. Modify the snr | 567 /* now the big work begins : do the bit allocation. Modify the snr |
537 offset until we can pack everything in the requested frame size */ | 568 offset until we can pack everything in the requested frame size */ |
538 | 569 |
539 csnroffst = s->csnroffst; | 570 csnroffst = s->csnroffst; |
540 while (csnroffst >= 0 && | 571 while (csnroffst >= 0 && |
541 bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0) | 572 bit_alloc(s, mask, psd, bap, frame_bits, csnroffst, 0) < 0) |
542 csnroffst -= SNR_INC1; | 573 csnroffst -= SNR_INC1; |
543 if (csnroffst < 0) { | 574 if (csnroffst < 0) { |
544 av_log(NULL, AV_LOG_ERROR, "Bit allocation failed, try increasing the bitrate, -ab 384 for example!\n"); | 575 av_log(NULL, AV_LOG_ERROR, "Bit allocation failed, try increasing the bitrate, -ab 384 for example!\n"); |
545 return -1; | 576 return -1; |
546 } | 577 } |
547 while ((csnroffst + SNR_INC1) <= 63 && | 578 while ((csnroffst + SNR_INC1) <= 63 && |
548 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, | 579 bit_alloc(s, mask, psd, bap1, frame_bits, |
549 csnroffst + SNR_INC1, 0) >= 0) { | 580 csnroffst + SNR_INC1, 0) >= 0) { |
550 csnroffst += SNR_INC1; | 581 csnroffst += SNR_INC1; |
551 memcpy(bap, bap1, sizeof(bap1)); | 582 memcpy(bap, bap1, sizeof(bap1)); |
552 } | 583 } |
553 while ((csnroffst + 1) <= 63 && | 584 while ((csnroffst + 1) <= 63 && |
554 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, csnroffst + 1, 0) >= 0) { | 585 bit_alloc(s, mask, psd, bap1, frame_bits, csnroffst + 1, 0) >= 0) { |
555 csnroffst++; | 586 csnroffst++; |
556 memcpy(bap, bap1, sizeof(bap1)); | 587 memcpy(bap, bap1, sizeof(bap1)); |
557 } | 588 } |
558 | 589 |
559 fsnroffst = 0; | 590 fsnroffst = 0; |
560 while ((fsnroffst + SNR_INC1) <= 15 && | 591 while ((fsnroffst + SNR_INC1) <= 15 && |
561 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, | 592 bit_alloc(s, mask, psd, bap1, frame_bits, |
562 csnroffst, fsnroffst + SNR_INC1) >= 0) { | 593 csnroffst, fsnroffst + SNR_INC1) >= 0) { |
563 fsnroffst += SNR_INC1; | 594 fsnroffst += SNR_INC1; |
564 memcpy(bap, bap1, sizeof(bap1)); | 595 memcpy(bap, bap1, sizeof(bap1)); |
565 } | 596 } |
566 while ((fsnroffst + 1) <= 15 && | 597 while ((fsnroffst + 1) <= 15 && |
567 bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, | 598 bit_alloc(s, mask, psd, bap1, frame_bits, |
568 csnroffst, fsnroffst + 1) >= 0) { | 599 csnroffst, fsnroffst + 1) >= 0) { |
569 fsnroffst++; | 600 fsnroffst++; |
570 memcpy(bap, bap1, sizeof(bap1)); | 601 memcpy(bap, bap1, sizeof(bap1)); |
571 } | 602 } |
572 | 603 |