* Output the mpeg audio layer 2 frame. Note how the code is small * compared to other encoders :-) */
| 609 | * compared to other encoders :-) |
| 610 | */ |
| 611 | static void encode_frame(MpegAudioContext *s, |
| 612 | unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], |
| 613 | int padding) |
| 614 | { |
| 615 | int i, j, k, l, bit_alloc_bits, b, ch; |
| 616 | unsigned char *sf; |
| 617 | int q[3]; |
| 618 | PutBitContext *p = &s->pb; |
| 619 | |
| 620 | /* header */ |
| 621 | |
| 622 | put_bits(p, 12, 0xfff); |
| 623 | put_bits(p, 1, 1 - s->lsf); /* 1 = mpeg1 ID, 0 = mpeg2 lsf ID */ |
| 624 | put_bits(p, 2, 4-2); /* layer 2 */ |
| 625 | put_bits(p, 1, 1); /* no error protection */ |
| 626 | put_bits(p, 4, s->bitrate_index); |
| 627 | put_bits(p, 2, s->freq_index); |
| 628 | put_bits(p, 1, s->do_padding); /* use padding */ |
| 629 | put_bits(p, 1, 0); /* private_bit */ |
| 630 | put_bits(p, 2, s->nb_channels == 2 ? MPA_STEREO : MPA_MONO); |
| 631 | put_bits(p, 2, 0); /* mode_ext */ |
| 632 | put_bits(p, 1, 0); /* no copyright */ |
| 633 | put_bits(p, 1, 1); /* original */ |
| 634 | put_bits(p, 2, 0); /* no emphasis */ |
| 635 | |
| 636 | /* bit allocation */ |
| 637 | j = 0; |
| 638 | for(i=0;i<s->sblimit;i++) { |
| 639 | bit_alloc_bits = s->alloc_table[j]; |
| 640 | for(ch=0;ch<s->nb_channels;ch++) { |
| 641 | put_bits(p, bit_alloc_bits, bit_alloc[ch][i]); |
| 642 | } |
| 643 | j += 1 << bit_alloc_bits; |
| 644 | } |
| 645 | |
| 646 | /* scale codes */ |
| 647 | for(i=0;i<s->sblimit;i++) { |
| 648 | for(ch=0;ch<s->nb_channels;ch++) { |
| 649 | if (bit_alloc[ch][i]) |
| 650 | put_bits(p, 2, s->scale_code[ch][i]); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | /* scale factors */ |
| 655 | for(i=0;i<s->sblimit;i++) { |
| 656 | for(ch=0;ch<s->nb_channels;ch++) { |
| 657 | if (bit_alloc[ch][i]) { |
| 658 | sf = &s->scale_factors[ch][i][0]; |
| 659 | switch(s->scale_code[ch][i]) { |
| 660 | case 0: |
| 661 | put_bits(p, 6, sf[0]); |
| 662 | put_bits(p, 6, sf[1]); |
| 663 | put_bits(p, 6, sf[2]); |
| 664 | break; |
| 665 | case 3: |
| 666 | case 1: |
| 667 | put_bits(p, 6, sf[0]); |
| 668 | put_bits(p, 6, sf[2]); |
no test coverage detected