Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: src/opus_encoder.c

Issue 882843002: Update to opus-HEAD-66611f1. (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/opus.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 20 matching lines...) Expand all
31 31
32 #include <stdarg.h> 32 #include <stdarg.h>
33 #include "celt.h" 33 #include "celt.h"
34 #include "entenc.h" 34 #include "entenc.h"
35 #include "modes.h" 35 #include "modes.h"
36 #include "API.h" 36 #include "API.h"
37 #include "stack_alloc.h" 37 #include "stack_alloc.h"
38 #include "float_cast.h" 38 #include "float_cast.h"
39 #include "opus.h" 39 #include "opus.h"
40 #include "arch.h" 40 #include "arch.h"
41 #include "pitch.h"
41 #include "opus_private.h" 42 #include "opus_private.h"
42 #include "os_support.h" 43 #include "os_support.h"
43 #include "cpu_support.h" 44 #include "cpu_support.h"
44 #include "analysis.h" 45 #include "analysis.h"
45 #include "mathops.h" 46 #include "mathops.h"
46 #include "tuning_parameters.h" 47 #include "tuning_parameters.h"
47 #ifdef FIXED_POINT 48 #ifdef FIXED_POINT
48 #include "fixed/structs_FIX.h" 49 #include "fixed/structs_FIX.h"
49 #else 50 #else
50 #include "float/structs_FLP.h" 51 #include "float/structs_FLP.h"
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 st->signal_type = OPUS_AUTO; 225 st->signal_type = OPUS_AUTO;
225 st->user_bandwidth = OPUS_AUTO; 226 st->user_bandwidth = OPUS_AUTO;
226 st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND; 227 st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
227 st->force_channels = OPUS_AUTO; 228 st->force_channels = OPUS_AUTO;
228 st->user_forced_mode = OPUS_AUTO; 229 st->user_forced_mode = OPUS_AUTO;
229 st->voice_ratio = -1; 230 st->voice_ratio = -1;
230 st->encoder_buffer = st->Fs/100; 231 st->encoder_buffer = st->Fs/100;
231 st->lsb_depth = 24; 232 st->lsb_depth = 24;
232 st->variable_duration = OPUS_FRAMESIZE_ARG; 233 st->variable_duration = OPUS_FRAMESIZE_ARG;
233 234
234 /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead 235 /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead
235 + 1.5 ms for SILK resamplers and stereo prediction) */ 236 + 1.5 ms for SILK resamplers and stereo prediction) */
236 st->delay_compensation = st->Fs/250; 237 st->delay_compensation = st->Fs/250;
237 238
238 st->hybrid_stereo_width_Q14 = 1 << 14; 239 st->hybrid_stereo_width_Q14 = 1 << 14;
239 st->prev_HB_gain = Q15ONE; 240 st->prev_HB_gain = Q15ONE;
240 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOF F_HZ ), 8 ); 241 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOF F_HZ ), 8 );
241 st->first = 1; 242 st->first = 1;
242 st->mode = MODE_HYBRID; 243 st->mode = MODE_HYBRID;
243 st->bandwidth = OPUS_BANDWIDTH_FULLBAND; 244 st->bandwidth = OPUS_BANDWIDTH_FULLBAND;
244 245
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
641 /* Follow transitions back */ 642 /* Follow transitions back */
642 for (i=N-1;i>=0;i--) 643 for (i=N-1;i>=0;i--)
643 { 644 {
644 /*printf("%d ", best_state);*/ 645 /*printf("%d ", best_state);*/
645 best_state = states[i][best_state]; 646 best_state = states[i][best_state];
646 } 647 }
647 /*printf("%d\n", best_state);*/ 648 /*printf("%d\n", best_state);*/
648 return best_state; 649 return best_state;
649 } 650 }
650 651
651 int optimize_framesize(const opus_val16 *x, int len, int C, opus_int32 Fs, 652 static int optimize_framesize(const void *x, int len, int C, opus_int32 Fs,
652 int bitrate, opus_val16 tonality, float *mem, int buffering, 653 int bitrate, opus_val16 tonality, float *mem, int buffering,
653 downmix_func downmix) 654 downmix_func downmix)
654 { 655 {
655 int N; 656 int N;
656 int i; 657 int i;
657 float e[MAX_DYNAMIC_FRAMESIZE+4]; 658 float e[MAX_DYNAMIC_FRAMESIZE+4];
658 float e_1[MAX_DYNAMIC_FRAMESIZE+3]; 659 float e_1[MAX_DYNAMIC_FRAMESIZE+3];
659 opus_val32 memx; 660 opus_val32 memx;
660 int bestLM=0; 661 int bestLM=0;
661 int subframe; 662 int subframe;
662 int pos; 663 int pos;
664 int offset;
663 VARDECL(opus_val32, sub); 665 VARDECL(opus_val32, sub);
664 666
665 subframe = Fs/400; 667 subframe = Fs/400;
666 ALLOC(sub, subframe, opus_val32); 668 ALLOC(sub, subframe, opus_val32);
667 e[0]=mem[0]; 669 e[0]=mem[0];
668 e_1[0]=1.f/(EPSILON+mem[0]); 670 e_1[0]=1.f/(EPSILON+mem[0]);
669 if (buffering) 671 if (buffering)
670 { 672 {
671 /* Consider the CELT delay when not in restricted-lowdelay */ 673 /* Consider the CELT delay when not in restricted-lowdelay */
672 /* We assume the buffering is between 2.5 and 5 ms */ 674 /* We assume the buffering is between 2.5 and 5 ms */
673 int offset = 2*subframe - buffering; 675 offset = 2*subframe - buffering;
674 celt_assert(offset>=0 && offset <= subframe); 676 celt_assert(offset>=0 && offset <= subframe);
675 x += C*offset;
676 len -= offset; 677 len -= offset;
677 e[1]=mem[1]; 678 e[1]=mem[1];
678 e_1[1]=1.f/(EPSILON+mem[1]); 679 e_1[1]=1.f/(EPSILON+mem[1]);
679 e[2]=mem[2]; 680 e[2]=mem[2];
680 e_1[2]=1.f/(EPSILON+mem[2]); 681 e_1[2]=1.f/(EPSILON+mem[2]);
681 pos = 3; 682 pos = 3;
682 } else { 683 } else {
683 pos=1; 684 pos=1;
685 offset=0;
684 } 686 }
685 N=IMIN(len/subframe, MAX_DYNAMIC_FRAMESIZE); 687 N=IMIN(len/subframe, MAX_DYNAMIC_FRAMESIZE);
686 /* Just silencing a warning, it's really initialized later */ 688 /* Just silencing a warning, it's really initialized later */
687 memx = 0; 689 memx = 0;
688 for (i=0;i<N;i++) 690 for (i=0;i<N;i++)
689 { 691 {
690 float tmp; 692 float tmp;
691 opus_val32 tmpx; 693 opus_val32 tmpx;
692 int j; 694 int j;
693 tmp=EPSILON; 695 tmp=EPSILON;
694 696
695 downmix(x, sub, subframe, i*subframe, 0, -2, C); 697 downmix(x, sub, subframe, i*subframe+offset, 0, -2, C);
696 if (i==0) 698 if (i==0)
697 memx = sub[0]; 699 memx = sub[0];
698 for (j=0;j<subframe;j++) 700 for (j=0;j<subframe;j++)
699 { 701 {
700 tmpx = sub[j]; 702 tmpx = sub[j];
701 tmp += (tmpx-memx)*(float)(tmpx-memx); 703 tmp += (tmpx-memx)*(float)(tmpx-memx);
702 memx = tmpx; 704 memx = tmpx;
703 } 705 }
704 e[i+pos] = tmp; 706 e[i+pos] = tmp;
705 e_1[i+pos] = 1.f/tmp; 707 e_1[i+pos] = 1.f/tmp;
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
829 #ifndef DISABLE_FLOAT_API 831 #ifndef DISABLE_FLOAT_API
830 if (variable_duration == OPUS_FRAMESIZE_VARIABLE && frame_size >= Fs/200) 832 if (variable_duration == OPUS_FRAMESIZE_VARIABLE && frame_size >= Fs/200)
831 { 833 {
832 int LM = 3; 834 int LM = 3;
833 LM = optimize_framesize(analysis_pcm, frame_size, C, Fs, bitrate_bps, 835 LM = optimize_framesize(analysis_pcm, frame_size, C, Fs, bitrate_bps,
834 0, subframe_mem, delay_compensation, downmix); 836 0, subframe_mem, delay_compensation, downmix);
835 while ((Fs/400<<LM)>frame_size) 837 while ((Fs/400<<LM)>frame_size)
836 LM--; 838 LM--;
837 frame_size = (Fs/400<<LM); 839 frame_size = (Fs/400<<LM);
838 } else 840 } else
841 #else
842 (void)analysis_pcm;
843 (void)C;
844 (void)bitrate_bps;
845 (void)delay_compensation;
846 (void)downmix;
839 #endif 847 #endif
840 { 848 {
841 frame_size = frame_size_select(frame_size, variable_duration, Fs); 849 frame_size = frame_size_select(frame_size, variable_duration, Fs);
842 } 850 }
843 if (frame_size<0) 851 if (frame_size<0)
844 return -1; 852 return -1;
845 return frame_size; 853 return frame_size;
846 } 854 }
847 855
848 opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int3 2 Fs, StereoWidthState *mem) 856 opus_val16 compute_stereo_width(const opus_val16 *pcm, int frame_size, opus_int3 2 Fs, StereoWidthState *mem)
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
917 width = 0; 925 width = 0;
918 corr=Q15ONE; 926 corr=Q15ONE;
919 ldiff=0; 927 ldiff=0;
920 } 928 }
921 /*printf("%f %f %f %f %f ", corr/(float)Q15ONE, ldiff/(float)Q15ONE, width/(f loat)Q15ONE, mem->smoothed_width/(float)Q15ONE, mem->max_follower/(float)Q15ONE) ;*/ 929 /*printf("%f %f %f %f %f ", corr/(float)Q15ONE, ldiff/(float)Q15ONE, width/(f loat)Q15ONE, mem->smoothed_width/(float)Q15ONE, mem->max_follower/(float)Q15ONE) ;*/
922 return EXTRACT16(MIN32(Q15ONE,20*mem->max_follower)); 930 return EXTRACT16(MIN32(Q15ONE,20*mem->max_follower));
923 } 931 }
924 932
925 opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_ size, 933 opus_int32 opus_encode_native(OpusEncoder *st, const opus_val16 *pcm, int frame_ size,
926 unsigned char *data, opus_int32 out_data_bytes, int lsb_depth, 934 unsigned char *data, opus_int32 out_data_bytes, int lsb_depth,
927 const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2, int analysis_channels, downmix_func downmix) 935 const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2,
936 int analysis_channels, downmix_func downmix, int float_api)
928 { 937 {
929 void *silk_enc; 938 void *silk_enc;
930 CELTEncoder *celt_enc; 939 CELTEncoder *celt_enc;
931 int i; 940 int i;
932 int ret=0; 941 int ret=0;
933 opus_int32 nBytes; 942 opus_int32 nBytes;
934 ec_enc enc; 943 ec_enc enc;
935 int bytes_target; 944 int bytes_target;
936 int prefill=0; 945 int prefill=0;
937 int start_band = 0; 946 int start_band = 0;
938 int redundancy = 0; 947 int redundancy = 0;
939 int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */ 948 int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
940 int celt_to_silk = 0; 949 int celt_to_silk = 0;
941 VARDECL(opus_val16, pcm_buf); 950 VARDECL(opus_val16, pcm_buf);
942 int nb_compr_bytes; 951 int nb_compr_bytes;
943 int to_celt = 0; 952 int to_celt = 0;
944 opus_uint32 redundant_rng = 0; 953 opus_uint32 redundant_rng = 0;
945 int cutoff_Hz, hp_freq_smth1; 954 int cutoff_Hz, hp_freq_smth1;
946 int voice_est; /* Probability of voice in Q7 */ 955 int voice_est; /* Probability of voice in Q7 */
947 opus_int32 equiv_rate; 956 opus_int32 equiv_rate;
948 int delay_compensation; 957 int delay_compensation;
949 int frame_rate; 958 int frame_rate;
950 opus_int32 max_rate; /* Max bitrate we're allowed to use */ 959 opus_int32 max_rate; /* Max bitrate we're allowed to use */
951 int curr_bandwidth; 960 int curr_bandwidth;
952 opus_val16 HB_gain; 961 opus_val16 HB_gain;
953 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */ 962 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
954 int total_buffer; 963 int total_buffer;
955 opus_val16 stereo_width; 964 opus_val16 stereo_width;
956 const CELTMode *celt_mode; 965 const CELTMode *celt_mode;
966 #ifndef DISABLE_FLOAT_API
957 AnalysisInfo analysis_info; 967 AnalysisInfo analysis_info;
958 int analysis_read_pos_bak=-1; 968 int analysis_read_pos_bak=-1;
959 int analysis_read_subframe_bak=-1; 969 int analysis_read_subframe_bak=-1;
970 #endif
960 VARDECL(opus_val16, tmp_prefill); 971 VARDECL(opus_val16, tmp_prefill);
961 972
962 ALLOC_STACK; 973 ALLOC_STACK;
963 974
964 max_data_bytes = IMIN(1276, out_data_bytes); 975 max_data_bytes = IMIN(1276, out_data_bytes);
965 976
966 st->rangeFinal = 0; 977 st->rangeFinal = 0;
967 if ((!st->variable_duration && 400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs && 978 if ((!st->variable_duration && 400*frame_size != st->Fs && 200*frame_size != st->Fs && 100*frame_size != st->Fs &&
968 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size ! = 3*st->Fs) 979 50*frame_size != st->Fs && 25*frame_size != st->Fs && 50*frame_size ! = 3*st->Fs)
969 || (400*frame_size < st->Fs) 980 || (400*frame_size < st->Fs)
970 || max_data_bytes<=0 981 || max_data_bytes<=0
971 ) 982 )
972 { 983 {
973 RESTORE_STACK; 984 RESTORE_STACK;
974 return OPUS_BAD_ARG; 985 return OPUS_BAD_ARG;
975 } 986 }
976 silk_enc = (char*)st+st->silk_enc_offset; 987 silk_enc = (char*)st+st->silk_enc_offset;
977 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset); 988 celt_enc = (CELTEncoder*)((char*)st+st->celt_enc_offset);
978 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 989 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
979 delay_compensation = 0; 990 delay_compensation = 0;
980 else 991 else
981 delay_compensation = st->delay_compensation; 992 delay_compensation = st->delay_compensation;
982 993
983 lsb_depth = IMIN(lsb_depth, st->lsb_depth); 994 lsb_depth = IMIN(lsb_depth, st->lsb_depth);
984 995
985 analysis_info.valid = 0;
986 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode)); 996 celt_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode));
987 #ifndef DISABLE_FLOAT_API 997 #ifndef DISABLE_FLOAT_API
998 analysis_info.valid = 0;
988 #ifdef FIXED_POINT 999 #ifdef FIXED_POINT
989 if (st->silk_mode.complexity >= 10 && st->Fs==48000) 1000 if (st->silk_mode.complexity >= 10 && st->Fs==48000)
990 #else 1001 #else
991 if (st->silk_mode.complexity >= 7 && st->Fs==48000) 1002 if (st->silk_mode.complexity >= 7 && st->Fs==48000)
992 #endif 1003 #endif
993 { 1004 {
994 analysis_read_pos_bak = st->analysis.read_pos; 1005 analysis_read_pos_bak = st->analysis.read_pos;
995 analysis_read_subframe_bak = st->analysis.read_subframe; 1006 analysis_read_subframe_bak = st->analysis.read_subframe;
996 run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame _size, 1007 run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame _size,
997 c1, c2, analysis_channels, st->Fs, 1008 c1, c2, analysis_channels, st->Fs,
998 lsb_depth, downmix, &analysis_info); 1009 lsb_depth, downmix, &analysis_info);
999 } 1010 }
1011 #else
1012 (void)analysis_pcm;
1013 (void)analysis_size;
1000 #endif 1014 #endif
1001 1015
1002 st->voice_ratio = -1; 1016 st->voice_ratio = -1;
1003 1017
1004 #ifndef DISABLE_FLOAT_API 1018 #ifndef DISABLE_FLOAT_API
1005 st->detected_bandwidth = 0; 1019 st->detected_bandwidth = 0;
1006 if (analysis_info.valid) 1020 if (analysis_info.valid)
1007 { 1021 {
1008 int analysis_bandwidth; 1022 int analysis_bandwidth;
1009 if (st->signal_type == OPUS_AUTO) 1023 if (st->signal_type == OPUS_AUTO)
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
1370 st->prev_channels = st->stream_channels; 1384 st->prev_channels = st->stream_channels;
1371 for (i=0;i<nb_frames;i++) 1385 for (i=0;i<nb_frames;i++)
1372 { 1386 {
1373 int tmp_len; 1387 int tmp_len;
1374 st->silk_mode.toMono = 0; 1388 st->silk_mode.toMono = 0;
1375 /* When switching from SILK/Hybrid to CELT, only ask for a switch at t he last frame */ 1389 /* When switching from SILK/Hybrid to CELT, only ask for a switch at t he last frame */
1376 if (to_celt && i==nb_frames-1) 1390 if (to_celt && i==nb_frames-1)
1377 st->user_forced_mode = MODE_CELT_ONLY; 1391 st->user_forced_mode = MODE_CELT_ONLY;
1378 tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->F s/50, 1392 tmp_len = opus_encode_native(st, pcm+i*(st->channels*st->Fs/50), st->F s/50,
1379 tmp_data+i*bytes_per_frame, bytes_per_frame, lsb_depth, 1393 tmp_data+i*bytes_per_frame, bytes_per_frame, lsb_depth,
1380 NULL, 0, c1, c2, analysis_channels, downmix); 1394 NULL, 0, c1, c2, analysis_channels, downmix, float_api);
1381 if (tmp_len<0) 1395 if (tmp_len<0)
1382 { 1396 {
1383 RESTORE_STACK; 1397 RESTORE_STACK;
1384 return OPUS_INTERNAL_ERROR; 1398 return OPUS_INTERNAL_ERROR;
1385 } 1399 }
1386 ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len); 1400 ret = opus_repacketizer_cat(rp, tmp_data+i*bytes_per_frame, tmp_len);
1387 if (ret<0) 1401 if (ret<0)
1388 { 1402 {
1389 RESTORE_STACK; 1403 RESTORE_STACK;
1390 return OPUS_INTERNAL_ERROR; 1404 return OPUS_INTERNAL_ERROR;
(...skipping 26 matching lines...) Expand all
1417 st->mode = MODE_SILK_ONLY; 1431 st->mode = MODE_SILK_ONLY;
1418 1432
1419 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, c urr_bandwidth); */ 1433 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, c urr_bandwidth); */
1420 bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame _size / (st->Fs * 8)) - 1; 1434 bytes_target = IMIN(max_data_bytes-redundancy_bytes, st->bitrate_bps * frame _size / (st->Fs * 8)) - 1;
1421 1435
1422 data += 1; 1436 data += 1;
1423 1437
1424 ec_enc_init(&enc, data, max_data_bytes-1); 1438 ec_enc_init(&enc, data, max_data_bytes-1);
1425 1439
1426 ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_val16); 1440 ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_val16);
1427 for (i=0;i<total_buffer*st->channels;i++) 1441 OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->c hannels], total_buffer*st->channels);
1428 pcm_buf[i] = st->delay_buffer[(st->encoder_buffer-total_buffer)*st->chann els+i];
1429 1442
1430 if (st->mode == MODE_CELT_ONLY) 1443 if (st->mode == MODE_CELT_ONLY)
1431 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 ); 1444 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 );
1432 else 1445 else
1433 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_ smth1_Q15; 1446 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_ smth1_Q15;
1434 1447
1435 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15, 1448 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,
1436 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP _SMTH_COEF2, 16 ) ); 1449 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP _SMTH_COEF2, 16 ) );
1437 1450
1438 /* convert from log scale to Hertz */ 1451 /* convert from log scale to Hertz */
1439 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) ); 1452 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 ) );
1440 1453
1441 if (st->application == OPUS_APPLICATION_VOIP) 1454 if (st->application == OPUS_APPLICATION_VOIP)
1442 { 1455 {
1443 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem , frame_size, st->channels, st->Fs); 1456 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem , frame_size, st->channels, st->Fs);
1444 } else { 1457 } else {
1445 dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_ size, st->channels, st->Fs); 1458 dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_ size, st->channels, st->Fs);
1446 } 1459 }
1447 1460 #ifndef FIXED_POINT
1461 if (float_api)
1462 {
1463 opus_val32 sum;
1464 sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total _buffer*st->channels], frame_size*st->channels, st->arch);
1465 /* This should filter out both NaNs and ridiculous signals that could
1466 cause NaNs further down. */
1467 if (!(sum < 1e9f) || celt_isnan(sum))
1468 OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channel s);
1469 }
1470 #endif
1448 1471
1449 1472
1450 /* SILK processing */ 1473 /* SILK processing */
1451 HB_gain = Q15ONE; 1474 HB_gain = Q15ONE;
1452 if (st->mode != MODE_CELT_ONLY) 1475 if (st->mode != MODE_CELT_ONLY)
1453 { 1476 {
1454 opus_int32 total_bitRate, celt_rate; 1477 opus_int32 total_bitRate, celt_rate;
1455 #ifdef FIXED_POINT 1478 #ifdef FIXED_POINT
1456 const opus_int16 *pcm_silk; 1479 const opus_int16 *pcm_silk;
1457 #else 1480 #else
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
1592 int prefill_offset; 1615 int prefill_offset;
1593 /* Use a smooth onset for the SILK prefill to avoid the encoder tryi ng to encode 1616 /* Use a smooth onset for the SILK prefill to avoid the encoder tryi ng to encode
1594 a discontinuity. The exact location is what we need to avoid leav ing any "gap" 1617 a discontinuity. The exact location is what we need to avoid leav ing any "gap"
1595 in the audio when mixing with the redundant CELT frame. Here we c an afford to 1618 in the audio when mixing with the redundant CELT frame. Here we c an afford to
1596 overwrite st->delay_buffer because the only thing that uses it be fore it gets 1619 overwrite st->delay_buffer because the only thing that uses it be fore it gets
1597 rewritten is tmp_prefill[] and even then only the part after the ramp really 1620 rewritten is tmp_prefill[] and even then only the part after the ramp really
1598 gets used (rather than sent to the encoder and discarded) */ 1621 gets used (rather than sent to the encoder and discarded) */
1599 prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensa tion-st->Fs/400); 1622 prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensa tion-st->Fs/400);
1600 gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_ offset, 1623 gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_ offset,
1601 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_ mode->window, st->Fs); 1624 0, Q15ONE, celt_mode->overlap, st->Fs/400, st->channels, celt_ mode->window, st->Fs);
1602 for(i=0;i<prefill_offset;i++) 1625 OPUS_CLEAR(st->delay_buffer, prefill_offset);
1603 st->delay_buffer[i]=0;
1604 #ifdef FIXED_POINT 1626 #ifdef FIXED_POINT
1605 pcm_silk = st->delay_buffer; 1627 pcm_silk = st->delay_buffer;
1606 #else 1628 #else
1607 for (i=0;i<st->encoder_buffer*st->channels;i++) 1629 for (i=0;i<st->encoder_buffer*st->channels;i++)
1608 pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]); 1630 pcm_silk[i] = FLOAT2INT16(st->delay_buffer[i]);
1609 #endif 1631 #endif
1610 silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 ); 1632 silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL, &zero, 1 );
1611 } 1633 }
1612 1634
1613 #ifdef FIXED_POINT 1635 #ifdef FIXED_POINT
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1720 } 1742 }
1721 } 1743 }
1722 1744
1723 } else { 1745 } else {
1724 nb_compr_bytes = 0; 1746 nb_compr_bytes = 0;
1725 } 1747 }
1726 1748
1727 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16); 1749 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_val16);
1728 if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0) 1750 if (st->mode != MODE_SILK_ONLY && st->mode != st->prev_mode && st->prev_mode > 0)
1729 { 1751 {
1730 for (i=0;i<st->channels*st->Fs/400;i++) 1752 OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer -st->Fs/400)*st->channels], st->channels*st->Fs/400);
1731 tmp_prefill[i] = st->delay_buffer[(st->encoder_buffer-total_buffer-st- >Fs/400)*st->channels + i];
1732 } 1753 }
1733 1754
1734 for (i=0;i<st->channels*(st->encoder_buffer-(frame_size+total_buffer));i++) 1755 if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0)
1735 st->delay_buffer[i] = st->delay_buffer[i+st->channels*frame_size]; 1756 {
1736 for (;i<st->encoder_buffer*st->channels;i++) 1757 OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], s t->channels*(st->encoder_buffer-frame_size-total_buffer));
1737 st->delay_buffer[i] = pcm_buf[(frame_size+total_buffer-st->encoder_buffe r)*st->channels+i]; 1758 OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-t otal_buffer)],
1738 1759 &pcm_buf[0],
1760 (frame_size+total_buffer)*st->channels);
1761 } else {
1762 OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder _buffer)*st->channels], st->encoder_buffer*st->channels);
1763 }
1739 /* gain_fade() and stereo_fade() need to be after the buffer copying 1764 /* gain_fade() and stereo_fade() need to be after the buffer copying
1740 because we don't want any of this to affect the SILK part */ 1765 because we don't want any of this to affect the SILK part */
1741 if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) { 1766 if( st->prev_HB_gain < Q15ONE || HB_gain < Q15ONE ) {
1742 gain_fade(pcm_buf, pcm_buf, 1767 gain_fade(pcm_buf, pcm_buf,
1743 st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->chan nels, celt_mode->window, st->Fs); 1768 st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->chan nels, celt_mode->window, st->Fs);
1744 } 1769 }
1745 st->prev_HB_gain = HB_gain; 1770 st->prev_HB_gain = HB_gain;
1746 if (st->mode != MODE_HYBRID || st->stream_channels==1) 1771 if (st->mode != MODE_HYBRID || st->stream_channels==1)
1747 st->silk_mode.stereoWidth_Q14 = IMIN((1<<14),2*IMAX(0,equiv_rate-30000)); 1772 st->silk_mode.stereoWidth_Q14 = IMIN((1<<14),2*IMAX(0,equiv_rate-30000));
1748 if( !st->energy_masking && st->channels == 2 ) { 1773 if( !st->energy_masking && st->channels == 2 ) {
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
1948 else 1973 else
1949 delay_compensation = st->delay_compensation; 1974 delay_compensation = st->delay_compensation;
1950 frame_size = compute_frame_size(pcm, analysis_frame_size, 1975 frame_size = compute_frame_size(pcm, analysis_frame_size,
1951 st->variable_duration, st->channels, st->Fs, st->bitrate_bps, 1976 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
1952 delay_compensation, downmix_float, st->analysis.subframe_mem); 1977 delay_compensation, downmix_float, st->analysis.subframe_mem);
1953 1978
1954 ALLOC(in, frame_size*st->channels, opus_int16); 1979 ALLOC(in, frame_size*st->channels, opus_int16);
1955 1980
1956 for (i=0;i<frame_size*st->channels;i++) 1981 for (i=0;i<frame_size*st->channels;i++)
1957 in[i] = FLOAT2INT16(pcm[i]); 1982 in[i] = FLOAT2INT16(pcm[i]);
1958 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, a nalysis_frame_size, 0, -2, st->channels, downmix_float); 1983 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
1984 pcm, analysis_frame_size, 0, -2, st->channels, downm ix_float, 1);
1959 RESTORE_STACK; 1985 RESTORE_STACK;
1960 return ret; 1986 return ret;
1961 } 1987 }
1962 #endif 1988 #endif
1963 1989
1964 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_fram e_size, 1990 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_fram e_size,
1965 unsigned char *data, opus_int32 out_data_bytes) 1991 unsigned char *data, opus_int32 out_data_bytes)
1966 { 1992 {
1967 int frame_size; 1993 int frame_size;
1968 int delay_compensation; 1994 int delay_compensation;
1969 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 1995 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1970 delay_compensation = 0; 1996 delay_compensation = 0;
1971 else 1997 else
1972 delay_compensation = st->delay_compensation; 1998 delay_compensation = st->delay_compensation;
1973 frame_size = compute_frame_size(pcm, analysis_frame_size, 1999 frame_size = compute_frame_size(pcm, analysis_frame_size,
1974 st->variable_duration, st->channels, st->Fs, st->bitrate_bps, 2000 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
1975 delay_compensation, downmix_int 2001 delay_compensation, downmix_int
1976 #ifndef DISABLE_FLOAT_API 2002 #ifndef DISABLE_FLOAT_API
1977 , st->analysis.subframe_mem 2003 , st->analysis.subframe_mem
1978 #endif 2004 #endif
1979 ); 2005 );
1980 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16, pcm, analysis_frame_size, 0, -2, st->channels, downmix_int); 2006 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 16,
2007 pcm, analysis_frame_size, 0, -2, st->channels, down mix_int, 0);
1981 } 2008 }
1982 2009
1983 #else 2010 #else
1984 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_fram e_size, 2011 opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_fram e_size,
1985 unsigned char *data, opus_int32 max_data_bytes) 2012 unsigned char *data, opus_int32 max_data_bytes)
1986 { 2013 {
1987 int i, ret; 2014 int i, ret;
1988 int frame_size; 2015 int frame_size;
1989 int delay_compensation; 2016 int delay_compensation;
1990 VARDECL(float, in); 2017 VARDECL(float, in);
1991 ALLOC_STACK; 2018 ALLOC_STACK;
1992 2019
1993 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 2020 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
1994 delay_compensation = 0; 2021 delay_compensation = 0;
1995 else 2022 else
1996 delay_compensation = st->delay_compensation; 2023 delay_compensation = st->delay_compensation;
1997 frame_size = compute_frame_size(pcm, analysis_frame_size, 2024 frame_size = compute_frame_size(pcm, analysis_frame_size,
1998 st->variable_duration, st->channels, st->Fs, st->bitrate_bps, 2025 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
1999 delay_compensation, downmix_int, st->analysis.subframe_mem); 2026 delay_compensation, downmix_int, st->analysis.subframe_mem);
2000 2027
2001 ALLOC(in, frame_size*st->channels, float); 2028 ALLOC(in, frame_size*st->channels, float);
2002 2029
2003 for (i=0;i<frame_size*st->channels;i++) 2030 for (i=0;i<frame_size*st->channels;i++)
2004 in[i] = (1.0f/32768)*pcm[i]; 2031 in[i] = (1.0f/32768)*pcm[i];
2005 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16, pcm, a nalysis_frame_size, 0, -2, st->channels, downmix_int); 2032 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
2033 pcm, analysis_frame_size, 0, -2, st->channels, downm ix_int, 0);
2006 RESTORE_STACK; 2034 RESTORE_STACK;
2007 return ret; 2035 return ret;
2008 } 2036 }
2009 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_fra me_size, 2037 opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_fra me_size,
2010 unsigned char *data, opus_int32 out_data_bytes) 2038 unsigned char *data, opus_int32 out_data_bytes)
2011 { 2039 {
2012 int frame_size; 2040 int frame_size;
2013 int delay_compensation; 2041 int delay_compensation;
2014 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY) 2042 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY)
2015 delay_compensation = 0; 2043 delay_compensation = 0;
2016 else 2044 else
2017 delay_compensation = st->delay_compensation; 2045 delay_compensation = st->delay_compensation;
2018 frame_size = compute_frame_size(pcm, analysis_frame_size, 2046 frame_size = compute_frame_size(pcm, analysis_frame_size,
2019 st->variable_duration, st->channels, st->Fs, st->bitrate_bps, 2047 st->variable_duration, st->channels, st->Fs, st->bitrate_bps,
2020 delay_compensation, downmix_float, st->analysis.subframe_mem); 2048 delay_compensation, downmix_float, st->analysis.subframe_mem);
2021 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24, 2049 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, 24,
2022 pcm, analysis_frame_size, 0, -2, st->channels, down mix_float); 2050 pcm, analysis_frame_size, 0, -2, st->channels, down mix_float, 1);
2023 } 2051 }
2024 #endif 2052 #endif
2025 2053
2026 2054
2027 int opus_encoder_ctl(OpusEncoder *st, int request, ...) 2055 int opus_encoder_ctl(OpusEncoder *st, int request, ...)
2028 { 2056 {
2029 int ret; 2057 int ret;
2030 CELTEncoder *celt_enc; 2058 CELTEncoder *celt_enc;
2031 va_list ap; 2059 va_list ap;
2032 2060
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
2101 if (!value) 2129 if (!value)
2102 { 2130 {
2103 goto bad_arg; 2131 goto bad_arg;
2104 } 2132 }
2105 *value = st->force_channels; 2133 *value = st->force_channels;
2106 } 2134 }
2107 break; 2135 break;
2108 case OPUS_SET_MAX_BANDWIDTH_REQUEST: 2136 case OPUS_SET_MAX_BANDWIDTH_REQUEST:
2109 { 2137 {
2110 opus_int32 value = va_arg(ap, opus_int32); 2138 opus_int32 value = va_arg(ap, opus_int32);
2111 if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULL BAND) 2139 if (value < OPUS_BANDWIDTH_NARROWBAND || value > OPUS_BANDWIDTH_FULL BAND)
2112 { 2140 {
2113 goto bad_arg; 2141 goto bad_arg;
2114 } 2142 }
2115 st->max_bandwidth = value; 2143 st->max_bandwidth = value;
2116 if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) { 2144 if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND) {
2117 st->silk_mode.maxInternalSampleRate = 8000; 2145 st->silk_mode.maxInternalSampleRate = 8000;
2118 } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) { 2146 } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND) {
2119 st->silk_mode.maxInternalSampleRate = 12000; 2147 st->silk_mode.maxInternalSampleRate = 12000;
2120 } else { 2148 } else {
2121 st->silk_mode.maxInternalSampleRate = 16000; 2149 st->silk_mode.maxInternalSampleRate = 16000;
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
2479 return ret; 2507 return ret;
2480 bad_arg: 2508 bad_arg:
2481 va_end(ap); 2509 va_end(ap);
2482 return OPUS_BAD_ARG; 2510 return OPUS_BAD_ARG;
2483 } 2511 }
2484 2512
2485 void opus_encoder_destroy(OpusEncoder *st) 2513 void opus_encoder_destroy(OpusEncoder *st)
2486 { 2514 {
2487 opus_free(st); 2515 opus_free(st);
2488 } 2516 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698