[asterisk-commits] russell: trunk r97977 - in /trunk: ./ main/translate.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Jan 10 17:33:24 CST 2008


Author: russell
Date: Thu Jan 10 17:33:24 2008
New Revision: 97977

URL: http://svn.digium.com/view/asterisk?view=rev&rev=97977
Log:
Merged revisions 97976 via svnmerge from 
https://origsvn.digium.com/svn/asterisk/branches/1.4

........
r97976 | russell | 2008-01-10 17:30:40 -0600 (Thu, 10 Jan 2008) | 3 lines

Fix various timing calculations that made assumptions that the audio being
processed was at a sample rate of 8 kHz.

........

Modified:
    trunk/   (props changed)
    trunk/main/translate.c

Propchange: trunk/
------------------------------------------------------------------------------
Binary property 'branch-1.4-merged' - no diff available.

Modified: trunk/main/translate.c
URL: http://svn.digium.com/view/asterisk/trunk/main/translate.c?view=diff&rev=97977&r1=97976&r2=97977
==============================================================================
--- trunk/main/translate.c (original)
+++ trunk/main/translate.c Thu Jan 10 17:33:24 2008
@@ -284,6 +284,14 @@
 	return head;
 }
 
+static inline int is16kHz(int format)
+{
+	if (format == AST_FORMAT_G722 || format == AST_FORMAT_SLINEAR16)
+		return 1;
+
+	return 0;
+}
+
 /*! \brief do the actual translation */
 struct ast_frame *ast_translate(struct ast_trans_pvt *path, struct ast_frame *f, int consume)
 {
@@ -302,6 +310,11 @@
 
 	/* XXX hmmm... check this below */
 	if (!ast_tvzero(f->delivery)) {
+		int in_rate = 8000;
+
+		if (is16kHz(f->subclass))
+			in_rate = 16000;
+		
 		if (!ast_tvzero(path->nextin)) {
 			/* Make sure this is in line with what we were expecting */
 			if (!ast_tveq(path->nextin, f->delivery)) {
@@ -320,7 +333,7 @@
 			path->nextout = f->delivery;
 		}
 		/* Predict next incoming sample */
-		path->nextin = ast_tvadd(path->nextin, ast_samp2tv(f->samples, 8000));
+		path->nextin = ast_tvadd(path->nextin, ast_samp2tv(f->samples, in_rate));
 	}
 	delivery = f->delivery;
 	for ( ; out && p ; p = p->next) {
@@ -333,6 +346,11 @@
 		return NULL;
 	/* we have a frame, play with times */
 	if (!ast_tvzero(delivery)) {
+		int out_rate = 8000;
+
+		if (is16kHz(out->subclass))
+			out_rate = 16000;
+
 		/* Regenerate prediction after a discontinuity */
 		if (ast_tvzero(path->nextout))
 			path->nextout = ast_tvnow();
@@ -342,7 +360,7 @@
 		
 		/* Predict next outgoing timestamp from samples in this
 		   frame. */
-		path->nextout = ast_tvadd(path->nextout, ast_samp2tv( out->samples, 8000));
+		path->nextout = ast_tvadd(path->nextout, ast_samp2tv(out->samples, out_rate));
 	} else {
 		out->delivery = ast_tv(0, 0);
 		out->has_timing_info = has_timing_info;
@@ -361,11 +379,15 @@
 /*! \brief compute the cost of a single translation step */
 static void calc_cost(struct ast_translator *t, int seconds)
 {
-	int sofar=0;
+	int num_samples = 0;
 	struct ast_trans_pvt *pvt;
 	struct rusage start;
 	struct rusage end;
 	int cost;
+	int out_rate = 8000;
+
+	if (is16kHz(t->dstfmt))
+		out_rate = 16000;
 
 	if (!seconds)
 		seconds = 1;
@@ -376,15 +398,18 @@
 		t->cost = 999999;
 		return;
 	}
+
 	pvt = newpvt(t);
 	if (!pvt) {
 		ast_log(LOG_WARNING, "Translator '%s' appears to be broken and will probably fail.\n", t->name);
 		t->cost = 999999;
 		return;
 	}
+
 	getrusage(RUSAGE_SELF, &start);
+
 	/* Call the encoder until we've processed the required number of samples */
-	while (sofar < seconds * 8000) {
+	while (num_samples < seconds * out_rate) {
 		struct ast_frame *f = t->sample();
 		if (!f) {
 			ast_log(LOG_WARNING, "Translator '%s' failed to produce a sample frame.\n", t->name);
@@ -395,15 +420,20 @@
 		framein(pvt, f);
 		ast_frfree(f);
 		while ((f = t->frameout(pvt))) {
-			sofar += f->samples;
+			num_samples += f->samples;
 			ast_frfree(f);
 		}
 	}
+
 	getrusage(RUSAGE_SELF, &end);
-	cost = ((end.ru_utime.tv_sec - start.ru_utime.tv_sec)*1000000) + end.ru_utime.tv_usec - start.ru_utime.tv_usec;
-	cost += ((end.ru_stime.tv_sec - start.ru_stime.tv_sec)*1000000) + end.ru_stime.tv_usec - start.ru_stime.tv_usec;
+
+	cost = ((end.ru_utime.tv_sec - start.ru_utime.tv_sec) * 1000000) + end.ru_utime.tv_usec - start.ru_utime.tv_usec;
+	cost += ((end.ru_stime.tv_sec - start.ru_stime.tv_sec) * 1000000) + end.ru_stime.tv_usec - start.ru_stime.tv_usec;
+
 	destroy(pvt);
+
 	t->cost = cost / seconds;
+
 	if (!t->cost)
 		t->cost = 1;
 }




More information about the asterisk-commits mailing list