Index: /pjproject/trunk/pjmedia/src/test/codec_vectors.c
===================================================================
--- /pjproject/trunk/pjmedia/src/test/codec_vectors.c	(revision 2614)
+++ /pjproject/trunk/pjmedia/src/test/codec_vectors.c	(revision 2615)
@@ -23,9 +23,13 @@
 #define TMP_OUT	    "output.tmp"
 
+/*
+ * Encode test. Read input from WAV file, encode to temporary output file, 
+ * and compare the temporary output file to the reference file.
+ */
 static int codec_test_encode(pjmedia_codec_mgr *mgr, 
 			     char *codec_name, 
 			     unsigned bitrate,
 			     const char *wav_file,
-			     const char *ref_file)
+			     const char *ref_encoded_file)
 {
     pj_str_t codec_id = pj_str(codec_name);
@@ -132,5 +136,5 @@
     
     /* Compare encoded files */
-    fref = fopen(ref_file, "rb");
+    fref = fopen(ref_encoded_file, "rb");
     if (!fref) {
 	rc = -100;
@@ -196,11 +200,84 @@
 
 
-
+/*
+ * Read file in ITU format (".itu" extension).
+ *
+ * Set swap_endian to TRUE if the ITU file is stored in little 
+ * endian format (normally true).
+ */
+static int read_ITU_format(FILE  *fp_bitstream,
+			   short *out_words,
+			   short *p_frame_error_flag,
+			   int number_of_16bit_words_per_frame,
+			   pj_bool_t swap_endian)
+{
+    enum { MAX_BITS_PER_FRAME = 160*8 };
+    short i,j;
+    short nsamp;
+    short packed_word;
+    short bit_count;
+    short bit;
+    short in_array[MAX_BITS_PER_FRAME+2];
+    short one = 0x0081;
+    short zero = 0x007f;
+    short frame_start = 0x6b21;
+
+    nsamp = fread(in_array, 2, 2 + 16*number_of_16bit_words_per_frame,
+		  fp_bitstream);
+
+    j = 0;
+    bit = in_array[j++];
+    if (bit != frame_start) {
+        *p_frame_error_flag = 1;
+    } else {
+        *p_frame_error_flag = 0;
+        
+        /* increment j to skip over the number of bits in frame */
+        j++;
+
+        for (i=0; i<number_of_16bit_words_per_frame; i++) {
+            packed_word = 0;
+            bit_count = 15;
+            while (bit_count >= 0) {
+    	        bit = in_array[j++];
+    	        if (bit == zero) 
+    	            bit = 0;
+    	        else if (bit == one) 
+    	            bit = 1;
+    	        else 
+    	            *p_frame_error_flag = 1;
+
+	        packed_word <<= 1;
+	        packed_word = (short )(packed_word + bit);
+	        bit_count--;
+            }
+
+	    if (swap_endian)
+		out_words[i] = pj_ntohs(packed_word);
+	    else
+		out_words[i] = packed_word;
+        }
+    }
+    return (nsamp-1)/16;
+}
+
+
+/*
+ * Decode test
+ *
+ * Decode the specified encoded file in "in_encoded_file" into temporary
+ * PCM output file, and compare the temporary PCM output file with
+ * the PCM reference file.
+ *
+ * Some reference file requires manipulation to the PCM output
+ * before comparison, such manipulation can be done by supplying
+ * this function with the "manip" function.
+ */
 static int codec_test_decode(pjmedia_codec_mgr *mgr, 
 			     char *codec_name, 
 			     unsigned bitrate,
 			     unsigned encoded_len,
-			     const char *in_file,
-			     const char *ref_file,
+			     const char *in_encoded_file,
+			     const char *ref_pcm_file,
 			     void (*manip)(short *pcm, unsigned count))
 {
@@ -214,4 +291,5 @@
     void *pkt;
     FILE *input = NULL, *output = NULL, *fref = NULL;
+    pj_bool_t is_itu_format = PJ_FALSE;
     int rc = 0;
     pj_status_t status;
@@ -259,9 +337,13 @@
 
     /* Open input file */
-    input = fopen(in_file, "rb");
+    input = fopen(in_encoded_file, "rb");
     if (!input) {
 	rc = -80;
 	goto on_return;
     }
+
+    /* Is the file in ITU format? */
+    is_itu_format = pj_ansi_stricmp(in_encoded_file+strlen(in_encoded_file)-4,
+				    ".itu")==0;
 
     /* Open output file */
@@ -282,22 +364,50 @@
 	pj_timestamp ts;
 	unsigned count;
-
-	if (fread(pkt, encoded_len, 1, input) != 1)
-	    break;
-
-	count = 2;
-	if (codec->op->parse(codec, pkt, encoded_len, &ts, &count, in_frame) != PJ_SUCCESS) {
-	    rc = -100;
-	    goto on_return;
+	pj_bool_t has_frame;
+
+	if (is_itu_format) {
+	    int nsamp;
+	    short frame_err = 0;
+
+	    nsamp = read_ITU_format(input, (short*)pkt, &frame_err,
+				    encoded_len / 2, PJ_TRUE);
+	    if (nsamp != (int)encoded_len / 2)
+		break;
+
+	    has_frame = !frame_err;
+	} else {
+	    if (fread(pkt, encoded_len, 1, input) != 1)
+		break;
+
+	    has_frame = PJ_TRUE;
 	}
 
-	if (count != 1) {
-	    rc = -110;
-	    goto on_return;
-	}
-
-	if (codec->op->decode(codec, &in_frame[0], samples_per_frame*2, &out_frame) != PJ_SUCCESS) {
-	    rc = -120;
-	    goto on_return;
+	if (has_frame) {
+	    count = 2;
+	    if (codec->op->parse(codec, pkt, encoded_len, &ts, 
+				 &count, in_frame) != PJ_SUCCESS) 
+	    {
+		rc = -100;
+		goto on_return;
+	    }
+
+	    if (count != 1) {
+		rc = -110;
+		goto on_return;
+	    }
+
+	    if (codec->op->decode(codec, &in_frame[0], samples_per_frame*2, 
+				  &out_frame) != PJ_SUCCESS) 
+	    {
+		rc = -120;
+		goto on_return;
+	    }
+	} else {
+	    if (codec->op->recover(codec, samples_per_frame*2, 
+				   &out_frame) != PJ_SUCCESS)
+	    {
+		rc = -125;
+		goto on_return;
+	    }
 	}
 
@@ -318,5 +428,5 @@
     
     /* Compare encoded files */
-    fref = fopen(ref_file, "rb");
+    fref = fopen(ref_pcm_file, "rb");
     if (!fref) {
 	rc = -140;
@@ -400,5 +510,5 @@
 	unsigned     bit_rate;
 	const char  *wav_file;
-	const char  *ref_file;
+	const char  *ref_encoded_file;
     } enc_vectors[] = 
     {
@@ -421,5 +531,5 @@
 	void	    (*manip)(short *pcm, unsigned count);
 	const char  *enc_file;
-	const char  *ref_file;
+	const char  *ref_pcm_file;
     } dec_vectors[] = 
     {
@@ -435,4 +545,14 @@
 	  "../src/test/vectors/g722_1_dec_out_32000.pcm"
 	},
+	{ "G7221/16000/1", 24000, 60,
+	  &g7221_pcm_manip,
+	  "../src/test/vectors/g722_1_dec_in_24000_fe.itu",
+	  "../src/test/vectors/g722_1_dec_out_24000_fe.pcm"
+	},
+	{ "G7221/16000/1", 32000, 80,
+	  &g7221_pcm_manip,
+	  "../src/test/vectors/g722_1_dec_in_32000_fe.itu",
+	  "../src/test/vectors/g722_1_dec_out_32000_fe.pcm"
+	},
 #endif
 	{ NULL }
@@ -457,12 +577,15 @@
     PJ_LOG(3,(THIS_FILE,"  encode tests:"));
     for (i=0; i<PJ_ARRAY_SIZE(enc_vectors); ++i) {
-	PJ_LOG(3,(THIS_FILE,"    %s @%d bps", enc_vectors[i].codec_name, 
-		  enc_vectors[i].bit_rate));
 	if (!enc_vectors[i].codec_name)
 	    continue;
+	PJ_LOG(3,(THIS_FILE,"    %s @%d bps %s ==> %s", 
+		  enc_vectors[i].codec_name, 
+		  enc_vectors[i].bit_rate,
+		  enc_vectors[i].wav_file,
+		  enc_vectors[i].ref_encoded_file));
 	rc = codec_test_encode(mgr, enc_vectors[i].codec_name,
 			       enc_vectors[i].bit_rate,
 			       enc_vectors[i].wav_file,
-			       enc_vectors[i].ref_file);
+			       enc_vectors[i].ref_encoded_file);
 	if (rc != 0)
 	    rc_final = rc;
@@ -471,13 +594,16 @@
     PJ_LOG(3,(THIS_FILE,"  decode tests:"));
     for (i=0; i<PJ_ARRAY_SIZE(dec_vectors); ++i) {
-	PJ_LOG(3,(THIS_FILE,"    %s @%d bps", dec_vectors[i].codec_name, 
-		  dec_vectors[i].bit_rate));
 	if (!dec_vectors[i].codec_name)
 	    continue;
+	PJ_LOG(3,(THIS_FILE,"    %s @%d bps %s ==> %s", 
+		  dec_vectors[i].codec_name, 
+		  dec_vectors[i].bit_rate,
+		  dec_vectors[i].enc_file,
+		  dec_vectors[i].ref_pcm_file));
 	rc = codec_test_decode(mgr, dec_vectors[i].codec_name,
 			       dec_vectors[i].bit_rate,
 			       dec_vectors[i].encoded_frame_len,
 			       dec_vectors[i].enc_file,
-			       dec_vectors[i].ref_file,
+			       dec_vectors[i].ref_pcm_file,
 			       dec_vectors[i].manip);
 	if (rc != 0)
