Changeset 584
- Timestamp:
- Jul 4, 2006 11:43:31 PM (18 years ago)
- Location:
- pjproject/trunk/pjmedia
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjmedia/build/Makefile
r558 r584 65 65 export PJMEDIA_SRCDIR = ../src/pjmedia 66 66 export PJMEDIA_OBJS += $(OS_OBJS) $(M_OBJS) $(CC_OBJS) $(HOST_OBJS) \ 67 alaw_ulaw.o \ 67 68 clock_thread.o codec.o conference.o endpoint.o errno.o \ 68 69 g711.o jbuf.o master_port.o mem_capture.o mem_player.o \ -
pjproject/trunk/pjmedia/build/pjmedia.dsp
r532 r584 88 88 # Begin Source File 89 89 90 SOURCE=..\src\pjmedia\alaw_ulaw.c 91 # End Source File 92 # Begin Source File 93 90 94 SOURCE=..\src\pjmedia\clock_thread.c 91 95 # End Source File -
pjproject/trunk/pjmedia/src/pjmedia/g711.c
r582 r584 554 554 555 555 556 /* 557 * This source code is a product of Sun Microsystems, Inc. and is provided 558 * for unrestricted use. Users may copy or modify this source code without 559 * charge. 560 * 561 * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING 562 * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 563 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 564 * 565 * Sun source code is provided with no support and without any obligation on 566 * the part of Sun Microsystems, Inc. to assist in its use, correction, 567 * modification or enhancement. 568 * 569 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 570 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE 571 * OR ANY PART THEREOF. 572 * 573 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 574 * or profits or other special, indirect and consequential damages, even if 575 * Sun has been advised of the possibility of such damages. 576 * 577 * Sun Microsystems, Inc. 578 * 2550 Garcia Avenue 579 * Mountain View, California 94043 580 */ 581 582 583 584 #ifdef _MSC_VER 585 # pragma warning ( disable: 4244 ) /* Conversion from int to char etc */ 586 #endif 587 588 /* 589 * g711.c 590 * 591 * u-law, A-law and linear PCM conversions. 592 */ 593 #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */ 594 #define QUANT_MASK (0xf) /* Quantization field mask. */ 595 #define NSEGS (8) /* Number of A-law segments. */ 596 #define SEG_SHIFT (4) /* Left shift for segment number. */ 597 #define SEG_MASK (0x70) /* Segment field mask. */ 598 599 static short seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF, 600 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF}; 601 602 /* copy from CCITT G.711 specifications */ 603 static unsigned char _u2a[128] = { /* u- to A-law conversions */ 604 1, 1, 2, 2, 3, 3, 4, 4, 605 5, 5, 6, 6, 7, 7, 8, 8, 606 9, 10, 11, 12, 13, 14, 15, 16, 607 17, 18, 19, 20, 21, 22, 23, 24, 608 25, 27, 29, 31, 33, 34, 35, 36, 609 37, 38, 39, 40, 41, 42, 43, 44, 610 46, 48, 49, 50, 51, 52, 53, 54, 611 55, 56, 57, 58, 59, 60, 61, 62, 612 64, 65, 66, 67, 68, 69, 70, 71, 613 72, 73, 74, 75, 76, 77, 78, 79, 614 81, 82, 83, 84, 85, 86, 87, 88, 615 89, 90, 91, 92, 93, 94, 95, 96, 616 97, 98, 99, 100, 101, 102, 103, 104, 617 105, 106, 107, 108, 109, 110, 111, 112, 618 113, 114, 115, 116, 117, 118, 119, 120, 619 121, 122, 123, 124, 125, 126, 127, 128}; 620 621 static unsigned char _a2u[128] = { /* A- to u-law conversions */ 622 1, 3, 5, 7, 9, 11, 13, 15, 623 16, 17, 18, 19, 20, 21, 22, 23, 624 24, 25, 26, 27, 28, 29, 30, 31, 625 32, 32, 33, 33, 34, 34, 35, 35, 626 36, 37, 38, 39, 40, 41, 42, 43, 627 44, 45, 46, 47, 48, 48, 49, 49, 628 50, 51, 52, 53, 54, 55, 56, 57, 629 58, 59, 60, 61, 62, 63, 64, 64, 630 65, 66, 67, 68, 69, 70, 71, 72, 631 73, 74, 75, 76, 77, 78, 79, 79, 632 80, 81, 82, 83, 84, 85, 86, 87, 633 88, 89, 90, 91, 92, 93, 94, 95, 634 96, 97, 98, 99, 100, 101, 102, 103, 635 104, 105, 106, 107, 108, 109, 110, 111, 636 112, 113, 114, 115, 116, 117, 118, 119, 637 120, 121, 122, 123, 124, 125, 126, 127}; 638 639 static int 640 search( 641 int val, 642 short *table, 643 int size) 644 { 645 int i; 646 647 for (i = 0; i < size; i++) { 648 if (val <= *table++) 649 return (i); 650 } 651 return (size); 652 } 653 654 /* 655 * linear2alaw() - Convert a 16-bit linear PCM value to 8-bit A-law 656 * 657 * linear2alaw() accepts an 16-bit integer and encodes it as A-law data. 658 * 659 * Linear Input Code Compressed Code 660 * ------------------------ --------------- 661 * 0000000wxyza 000wxyz 662 * 0000001wxyza 001wxyz 663 * 000001wxyzab 010wxyz 664 * 00001wxyzabc 011wxyz 665 * 0001wxyzabcd 100wxyz 666 * 001wxyzabcde 101wxyz 667 * 01wxyzabcdef 110wxyz 668 * 1wxyzabcdefg 111wxyz 669 * 670 * For further information see John C. Bellamy's Digital Telephony, 1982, 671 * John Wiley & Sons, pps 98-111 and 472-476. 672 */ 673 unsigned char 674 linear2alaw( 675 int pcm_val) /* 2's complement (16-bit range) */ 676 { 677 int mask; 678 int seg; 679 unsigned char aval; 680 681 if (pcm_val >= 0) { 682 mask = 0xD5; /* sign (7th) bit = 1 */ 683 } else { 684 mask = 0x55; /* sign bit = 0 */ 685 pcm_val = -pcm_val - 8; 686 } 687 688 /* Convert the scaled magnitude to segment number. */ 689 seg = search(pcm_val, seg_end, 8); 690 691 /* Combine the sign, segment, and quantization bits. */ 692 693 if (seg >= 8) /* out of range, return maximum value. */ 694 return (0x7F ^ mask); 695 else { 696 aval = seg << SEG_SHIFT; 697 if (seg < 2) 698 aval |= (pcm_val >> 4) & QUANT_MASK; 699 else 700 aval |= (pcm_val >> (seg + 3)) & QUANT_MASK; 701 return (aval ^ mask); 702 } 703 } 704 705 /* 706 * alaw2linear() - Convert an A-law value to 16-bit linear PCM 707 * 708 */ 709 int 710 alaw2linear( 711 unsigned char a_val) 712 { 713 int t; 714 int seg; 715 716 a_val ^= 0x55; 717 718 t = (a_val & QUANT_MASK) << 4; 719 seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT; 720 switch (seg) { 721 case 0: 722 t += 8; 723 break; 724 case 1: 725 t += 0x108; 726 break; 727 default: 728 t += 0x108; 729 t <<= seg - 1; 730 } 731 return ((a_val & SIGN_BIT) ? t : -t); 732 } 733 734 #define BIAS (0x84) /* Bias for linear code. */ 735 736 /* 737 * linear2ulaw() - Convert a linear PCM value to u-law 738 * 739 * In order to simplify the encoding process, the original linear magnitude 740 * is biased by adding 33 which shifts the encoding range from (0 - 8158) to 741 * (33 - 8191). The result can be seen in the following encoding table: 742 * 743 * Biased Linear Input Code Compressed Code 744 * ------------------------ --------------- 745 * 00000001wxyza 000wxyz 746 * 0000001wxyzab 001wxyz 747 * 000001wxyzabc 010wxyz 748 * 00001wxyzabcd 011wxyz 749 * 0001wxyzabcde 100wxyz 750 * 001wxyzabcdef 101wxyz 751 * 01wxyzabcdefg 110wxyz 752 * 1wxyzabcdefgh 111wxyz 753 * 754 * Each biased linear code has a leading 1 which identifies the segment 755 * number. The value of the segment number is equal to 7 minus the number 756 * of leading 0's. The quantization interval is directly available as the 757 * four bits wxyz. * The trailing bits (a - h) are ignored. 758 * 759 * Ordinarily the complement of the resulting code word is used for 760 * transmission, and so the code word is complemented before it is returned. 761 * 762 * For further information see John C. Bellamy's Digital Telephony, 1982, 763 * John Wiley & Sons, pps 98-111 and 472-476. 764 */ 765 unsigned char 766 linear2ulaw( 767 int pcm_val) /* 2's complement (16-bit range) */ 768 { 769 int mask; 770 int seg; 771 unsigned char uval; 772 773 /* Get the sign and the magnitude of the value. */ 774 if (pcm_val < 0) { 775 pcm_val = BIAS - pcm_val; 776 mask = 0x7F; 777 } else { 778 pcm_val += BIAS; 779 mask = 0xFF; 780 } 781 782 /* Convert the scaled magnitude to segment number. */ 783 seg = search(pcm_val, seg_end, 8); 784 785 /* 786 * Combine the sign, segment, quantization bits; 787 * and complement the code word. 788 */ 789 if (seg >= 8) /* out of range, return maximum value. */ 790 return (0x7F ^ mask); 791 else { 792 uval = (seg << 4) | ((pcm_val >> (seg + 3)) & 0xF); 793 return (uval ^ mask); 794 } 795 796 } 797 798 /* 799 * ulaw2linear() - Convert a u-law value to 16-bit linear PCM 800 * 801 * First, a biased linear code is derived from the code word. An unbiased 802 * output can then be obtained by subtracting 33 from the biased code. 803 * 804 * Note that this function expects to be passed the complement of the 805 * original code word. This is in keeping with ISDN conventions. 806 */ 807 int 808 ulaw2linear( 809 unsigned char u_val) 810 { 811 int t; 812 813 /* Complement to obtain normal u-law value. */ 814 u_val = ~u_val; 815 816 /* 817 * Extract and bias the quantization bits. Then 818 * shift up by the segment number and subtract out the bias. 819 */ 820 t = ((u_val & QUANT_MASK) << 3) + BIAS; 821 t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT; 822 823 return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS)); 824 } 825 826 /* A-law to u-law conversion */ 827 unsigned char 828 alaw2ulaw( 829 unsigned char aval) 830 { 831 aval &= 0xff; 832 return ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) : 833 (0x7F ^ _a2u[aval ^ 0x55])); 834 } 835 836 /* u-law to A-law conversion */ 837 unsigned char 838 ulaw2alaw( 839 unsigned char uval) 840 { 841 uval &= 0xff; 842 return ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) : 843 (0x55 ^ (_u2a[0x7F ^ uval] - 1))); 844 } 845 846 847 848 556
Note: See TracChangeset
for help on using the changeset viewer.