OLD | NEW |
1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited | 1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited |
2 Written by Jean-Marc Valin and Koen Vos */ | 2 Written by Jean-Marc Valin and Koen Vos */ |
3 /* | 3 /* |
4 Redistribution and use in source and binary forms, with or without | 4 Redistribution and use in source and binary forms, with or without |
5 modification, are permitted provided that the following conditions | 5 modification, are permitted provided that the following conditions |
6 are met: | 6 are met: |
7 | 7 |
8 - Redistributions of source code must retain the above copyright | 8 - Redistributions of source code must retain the above copyright |
9 notice, this list of conditions and the following disclaimer. | 9 notice, this list of conditions and the following disclaimer. |
10 | 10 |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 { | 284 { |
285 toc = 0x60; | 285 toc = 0x60; |
286 toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4; | 286 toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND)<<4; |
287 toc |= (period-2)<<3; | 287 toc |= (period-2)<<3; |
288 } | 288 } |
289 toc |= (channels==2)<<2; | 289 toc |= (channels==2)<<2; |
290 return toc; | 290 return toc; |
291 } | 291 } |
292 | 292 |
293 #ifndef FIXED_POINT | 293 #ifndef FIXED_POINT |
294 void silk_biquad_float( | 294 static void silk_biquad_float( |
295 const opus_val16 *in, /* I: Input signal
*/ | 295 const opus_val16 *in, /* I: Input signal
*/ |
296 const opus_int32 *B_Q28, /* I: MA coefficients [3]
*/ | 296 const opus_int32 *B_Q28, /* I: MA coefficients [3]
*/ |
297 const opus_int32 *A_Q28, /* I: AR coefficients [2]
*/ | 297 const opus_int32 *A_Q28, /* I: AR coefficients [2]
*/ |
298 opus_val32 *S, /* I/O: State vector [2]
*/ | 298 opus_val32 *S, /* I/O: State vector [2]
*/ |
299 opus_val16 *out, /* O: Output signal
*/ | 299 opus_val16 *out, /* O: Output signal
*/ |
300 const opus_int32 len, /* I: Signal length (must be even)
*/ | 300 const opus_int32 len, /* I: Signal length (must be even)
*/ |
301 int stride | 301 int stride |
302 ) | 302 ) |
303 { | 303 { |
304 /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */ | 304 /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */ |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
451 void *silk_enc; | 451 void *silk_enc; |
452 CELTEncoder *celt_enc; | 452 CELTEncoder *celt_enc; |
453 int i; | 453 int i; |
454 int ret=0; | 454 int ret=0; |
455 opus_int32 nBytes; | 455 opus_int32 nBytes; |
456 ec_enc enc; | 456 ec_enc enc; |
457 int bytes_target; | 457 int bytes_target; |
458 int prefill=0; | 458 int prefill=0; |
459 int start_band = 0; | 459 int start_band = 0; |
460 int redundancy = 0; | 460 int redundancy = 0; |
461 int redundancy_bytes = 0; | 461 int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */ |
462 int celt_to_silk = 0; | 462 int celt_to_silk = 0; |
463 VARDECL(opus_val16, pcm_buf); | 463 VARDECL(opus_val16, pcm_buf); |
464 int nb_compr_bytes; | 464 int nb_compr_bytes; |
465 int to_celt = 0; | 465 int to_celt = 0; |
466 opus_uint32 redundant_rng = 0; | 466 opus_uint32 redundant_rng = 0; |
467 int cutoff_Hz, hp_freq_smth1; | 467 int cutoff_Hz, hp_freq_smth1; |
468 int voice_est; | 468 int voice_est; /* Probability of voice in Q7 */ |
469 opus_int32 equiv_rate; | 469 opus_int32 equiv_rate; |
470 int delay_compensation; | 470 int delay_compensation; |
471 int frame_rate; | 471 int frame_rate; |
472 opus_int32 max_rate; | 472 opus_int32 max_rate; /* Max bitrate we're allowed to use */ |
473 int curr_bandwidth; | 473 int curr_bandwidth; |
474 opus_int32 max_data_bytes; | 474 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */ |
475 VARDECL(opus_val16, tmp_prefill); | 475 VARDECL(opus_val16, tmp_prefill); |
476 | 476 |
477 ALLOC_STACK; | 477 ALLOC_STACK; |
478 | 478 |
479 max_data_bytes = IMIN(1276, out_data_bytes); | 479 max_data_bytes = IMIN(1276, out_data_bytes); |
480 | 480 |
481 st->rangeFinal = 0; | 481 st->rangeFinal = 0; |
482 if (400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size !
= st->Fs && | 482 if (400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size !
= st->Fs && |
483 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size !
= 3*st->Fs) | 483 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size !
= 3*st->Fs) |
484 { | 484 { |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
645 /* Switch to SILK/hybrid if frame size is 10 ms or more*/ | 645 /* Switch to SILK/hybrid if frame size is 10 ms or more*/ |
646 if (frame_size >= st->Fs/100) | 646 if (frame_size >= st->Fs/100) |
647 { | 647 { |
648 st->mode = st->prev_mode; | 648 st->mode = st->prev_mode; |
649 to_celt = 1; | 649 to_celt = 1; |
650 } else { | 650 } else { |
651 redundancy=0; | 651 redundancy=0; |
652 } | 652 } |
653 } | 653 } |
654 } | 654 } |
| 655 /* For the first frame at a new SILK bandwidth */ |
655 if (st->silk_bw_switch) | 656 if (st->silk_bw_switch) |
656 { | 657 { |
657 redundancy = 1; | 658 redundancy = 1; |
658 celt_to_silk = 1; | 659 celt_to_silk = 1; |
659 st->silk_bw_switch = 0; | 660 st->silk_bw_switch = 0; |
660 } | 661 } |
661 | 662 |
| 663 if (redundancy) |
| 664 { |
| 665 /* Fair share of the max size allowed */ |
| 666 redundancy_bytes = IMIN(257, max_data_bytes*(opus_int32)(st->Fs/200)/(fra
me_size+st->Fs/200)); |
| 667 /* For VBR, target the actual bitrate (subject to the limit above) */ |
| 668 if (st->use_vbr) |
| 669 redundancy_bytes = IMIN(redundancy_bytes, st->bitrate_bps/1600); |
| 670 } |
| 671 |
662 if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) | 672 if (st->mode != MODE_CELT_ONLY && st->prev_mode == MODE_CELT_ONLY) |
663 { | 673 { |
664 silk_EncControlStruct dummy; | 674 silk_EncControlStruct dummy; |
665 silk_InitEncoder( silk_enc, &dummy); | 675 silk_InitEncoder( silk_enc, &dummy); |
666 prefill=1; | 676 prefill=1; |
667 } | 677 } |
668 | 678 |
669 /* Automatic (rate-dependent) bandwidth selection */ | 679 /* Automatic (rate-dependent) bandwidth selection */ |
670 if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthS
witch) | 680 if (st->mode == MODE_CELT_ONLY || st->first || st->silk_mode.allowBandwidthS
witch) |
671 { | 681 { |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
816 curr_bandwidth = st->bandwidth; | 826 curr_bandwidth = st->bandwidth; |
817 | 827 |
818 /* Chooses the appropriate mode for speech | 828 /* Chooses the appropriate mode for speech |
819 *NEVER* switch to/from CELT-only mode here as this will invalidate some a
ssumptions */ | 829 *NEVER* switch to/from CELT-only mode here as this will invalidate some a
ssumptions */ |
820 if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND) | 830 if (st->mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND) |
821 st->mode = MODE_HYBRID; | 831 st->mode = MODE_HYBRID; |
822 if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND) | 832 if (st->mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND) |
823 st->mode = MODE_SILK_ONLY; | 833 st->mode = MODE_SILK_ONLY; |
824 | 834 |
825 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, c
urr_bandwidth); */ | 835 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, c
urr_bandwidth); */ |
826 bytes_target = IMIN(max_data_bytes, st->bitrate_bps * frame_size / (st->Fs *
8)) - 1; | 836 bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame
_size / (st->Fs * 8)) - 1; |
827 | 837 |
828 data += 1; | 838 data += 1; |
829 | 839 |
830 ec_enc_init(&enc, data, max_data_bytes-1); | 840 ec_enc_init(&enc, data, max_data_bytes-1); |
831 | 841 |
832 ALLOC(pcm_buf, (delay_compensation+frame_size)*st->channels, opus_val16); | 842 ALLOC(pcm_buf, (delay_compensation+frame_size)*st->channels, opus_val16); |
833 for (i=0;i<delay_compensation*st->channels;i++) | 843 for (i=0;i<delay_compensation*st->channels;i++) |
834 pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-delay_compensation)*st-
>channels+i]; | 844 pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-delay_compensation)*st-
>channels+i]; |
835 | 845 |
836 if (st->mode == MODE_CELT_ONLY) | 846 if (st->mode == MODE_CELT_ONLY) |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
922 st->silk_mode.maxInternalSampleRate = 8000; | 932 st->silk_mode.maxInternalSampleRate = 8000; |
923 st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode
.desiredInternalSampleRate); | 933 st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode
.desiredInternalSampleRate); |
924 } | 934 } |
925 } else { | 935 } else { |
926 st->silk_mode.maxInternalSampleRate = 16000; | 936 st->silk_mode.maxInternalSampleRate = 16000; |
927 } | 937 } |
928 | 938 |
929 st->silk_mode.useCBR = !st->use_vbr; | 939 st->silk_mode.useCBR = !st->use_vbr; |
930 | 940 |
931 /* Call SILK encoder for the low band */ | 941 /* Call SILK encoder for the low band */ |
932 nBytes = IMIN(1275, max_data_bytes-1); | 942 nBytes = IMIN(1275, max_data_bytes-1-redundancy_bytes); |
933 | 943 |
934 st->silk_mode.maxBits = nBytes*8; | 944 st->silk_mode.maxBits = nBytes*8; |
935 /* Only allow up to 90% of the bits for hybrid mode*/ | 945 /* Only allow up to 90% of the bits for hybrid mode*/ |
936 if (st->mode == MODE_HYBRID) | 946 if (st->mode == MODE_HYBRID) |
937 st->silk_mode.maxBits = (opus_int32)st->silk_mode.maxBits*9/10; | 947 st->silk_mode.maxBits = (opus_int32)st->silk_mode.maxBits*9/10; |
938 if (st->silk_mode.useCBR) | 948 if (st->silk_mode.useCBR) |
939 { | 949 { |
940 st->silk_mode.maxBits = (st->silk_mode.bitRate * frame_size / (st->Fs
* 8))*8; | 950 st->silk_mode.maxBits = (st->silk_mode.bitRate * frame_size / (st->Fs
* 8))*8; |
941 /* Reduce the initial target to make it easier to reach the CBR rate
*/ | 951 /* Reduce the initial target to make it easier to reach the CBR rate
*/ |
942 st->silk_mode.bitRate = IMAX(1, st->silk_mode.bitRate-2000); | 952 st->silk_mode.bitRate = IMAX(1, st->silk_mode.bitRate-2000); |
943 } | 953 } |
944 if (redundancy) | |
945 st->silk_mode.maxBits -= st->silk_mode.maxBits/(1 + frame_size/(st->F
s/200)); | |
946 | 954 |
947 if (prefill) | 955 if (prefill) |
948 { | 956 { |
949 opus_int32 zero=0; | 957 opus_int32 zero=0; |
950 #ifdef FIXED_POINT | 958 #ifdef FIXED_POINT |
951 pcm_silk = st->delay_buffer; | 959 pcm_silk = st->delay_buffer; |
952 #else | 960 #else |
953 for (i=0;i<st->encoder_buffer*st->channels;i++) | 961 for (i=0;i<st->encoder_buffer*st->channels;i++) |
954 pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]); | 962 pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]); |
955 #endif | 963 #endif |
(...skipping 27 matching lines...) Expand all Loading... |
983 } else if( st->silk_mode.internalSampleRate == 12000 ) { | 991 } else if( st->silk_mode.internalSampleRate == 12000 ) { |
984 curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; | 992 curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
985 } else if( st->silk_mode.internalSampleRate == 16000 ) { | 993 } else if( st->silk_mode.internalSampleRate == 16000 ) { |
986 curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND; | 994 curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
987 } | 995 } |
988 } else { | 996 } else { |
989 silk_assert( st->silk_mode.internalSampleRate == 16000 ); | 997 silk_assert( st->silk_mode.internalSampleRate == 16000 ); |
990 } | 998 } |
991 | 999 |
992 st->silk_mode.opusCanSwitch = st->silk_mode.switchReady; | 1000 st->silk_mode.opusCanSwitch = st->silk_mode.switchReady; |
| 1001 /* FIXME: How do we allocate the redundancy for CBR? */ |
993 if (st->silk_mode.opusCanSwitch) | 1002 if (st->silk_mode.opusCanSwitch) |
994 { | 1003 { |
995 redundancy = 1; | 1004 redundancy = 1; |
996 celt_to_silk = 0; | 1005 celt_to_silk = 0; |
997 st->silk_bw_switch = 1; | 1006 st->silk_bw_switch = 1; |
998 } | 1007 } |
999 } | 1008 } |
1000 | 1009 |
1001 /* CELT processing */ | 1010 /* CELT processing */ |
1002 { | 1011 { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1040 } else { | 1049 } else { |
1041 /* check if SILK used up too much */ | 1050 /* check if SILK used up too much */ |
1042 nb_compr_bytes = len > bytes_target ? len : bytes_target; | 1051 nb_compr_bytes = len > bytes_target ? len : bytes_target; |
1043 } | 1052 } |
1044 } else { | 1053 } else { |
1045 if (st->use_vbr) | 1054 if (st->use_vbr) |
1046 { | 1055 { |
1047 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1)); | 1056 celt_encoder_ctl(celt_enc, OPUS_SET_VBR(1)); |
1048 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_const
raint)); | 1057 celt_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_const
raint)); |
1049 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps)); | 1058 celt_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps)); |
1050 nb_compr_bytes = max_data_bytes-1; | 1059 nb_compr_bytes = max_data_bytes-1-redundancy_bytes; |
1051 } else { | 1060 } else { |
1052 nb_compr_bytes = bytes_target; | 1061 nb_compr_bytes = bytes_target; |
1053 } | 1062 } |
1054 } | 1063 } |
1055 | 1064 |
1056 } else { | 1065 } else { |
1057 nb_compr_bytes = 0; | 1066 nb_compr_bytes = 0; |
1058 } | 1067 } |
1059 | 1068 |
1060 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16); | 1069 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1112 redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600); | 1121 redundancy_bytes = IMIN(max_redundancy, st->bitrate_bps/1600); |
1113 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes)); | 1122 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes)); |
1114 if (st->mode == MODE_HYBRID) | 1123 if (st->mode == MODE_HYBRID) |
1115 ec_enc_uint(&enc, redundancy_bytes-2, 256); | 1124 ec_enc_uint(&enc, redundancy_bytes-2, 256); |
1116 } | 1125 } |
1117 } else { | 1126 } else { |
1118 redundancy = 0; | 1127 redundancy = 0; |
1119 } | 1128 } |
1120 | 1129 |
1121 if (!redundancy) | 1130 if (!redundancy) |
| 1131 { |
1122 st->silk_bw_switch = 0; | 1132 st->silk_bw_switch = 0; |
1123 | 1133 redundancy_bytes = 0; |
| 1134 } |
1124 if (st->mode != MODE_CELT_ONLY)start_band=17; | 1135 if (st->mode != MODE_CELT_ONLY)start_band=17; |
1125 | 1136 |
1126 if (st->mode == MODE_SILK_ONLY) | 1137 if (st->mode == MODE_SILK_ONLY) |
1127 { | 1138 { |
1128 ret = (ec_tell(&enc)+7)>>3; | 1139 ret = (ec_tell(&enc)+7)>>3; |
1129 ec_enc_done(&enc); | 1140 ec_enc_done(&enc); |
1130 nb_compr_bytes = ret; | 1141 nb_compr_bytes = ret; |
1131 } else { | 1142 } else { |
1132 nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes
); | 1143 nb_compr_bytes = IMIN((max_data_bytes-1)-redundancy_bytes, nb_compr_bytes
); |
1133 ec_enc_shrink(&enc, nb_compr_bytes); | 1144 ec_enc_shrink(&enc, nb_compr_bytes); |
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1597 return ret; | 1608 return ret; |
1598 bad_arg: | 1609 bad_arg: |
1599 va_end(ap); | 1610 va_end(ap); |
1600 return OPUS_BAD_ARG; | 1611 return OPUS_BAD_ARG; |
1601 } | 1612 } |
1602 | 1613 |
1603 void opus_encoder_destroy(OpusEncoder *st) | 1614 void opus_encoder_destroy(OpusEncoder *st) |
1604 { | 1615 { |
1605 opus_free(st); | 1616 opus_free(st); |
1606 } | 1617 } |
OLD | NEW |