blob: f68913d3088e1bee94fd4d62f2c9a9dc6a66f642 [file] [log] [blame]
Benny Prijono1dc0da72009-04-18 00:12:13 +00001/* $Id$ */
2/*
3 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "test.h"
20#include <pjmedia-codec.h>
21
22#define THIS_FILE "codec_vectors.c"
23#define TMP_OUT "output.tmp"
24
Benny Prijono8604b142009-04-18 11:49:54 +000025/*
26 * Encode test. Read input from WAV file, encode to temporary output file,
27 * and compare the temporary output file to the reference file.
28 */
Benny Prijono1dc0da72009-04-18 00:12:13 +000029static int codec_test_encode(pjmedia_codec_mgr *mgr,
30 char *codec_name,
31 unsigned bitrate,
32 const char *wav_file,
Benny Prijono8604b142009-04-18 11:49:54 +000033 const char *ref_encoded_file)
Benny Prijono1dc0da72009-04-18 00:12:13 +000034{
35 pj_str_t codec_id = pj_str(codec_name);
36 pj_pool_t *pool = NULL;
37 unsigned count, samples_per_frame, encoded_frame_len = 0, pos;
38 pjmedia_codec *codec = NULL;
39 const pjmedia_codec_info *ci[1];
40 pjmedia_codec_param codec_param;
41 pjmedia_port *wav_port = NULL;
42 pjmedia_frame in_frame, out_frame;
43 FILE *output = NULL, *fref = NULL;
44 int rc = 0;
45 pj_status_t status;
46
47 pool = pj_pool_create(mem, "codec-vectors", 512, 512, NULL);
48 if (!pool) {
49 rc = -20;
50 goto on_return;
51 }
52
53 /* Find and open the codec */
54 count = 1;
55 status = pjmedia_codec_mgr_find_codecs_by_id(mgr, &codec_id, &count, ci, NULL);
56 if (status != PJ_SUCCESS) {
57 rc = -30;
58 goto on_return;
59 }
60
61 status = pjmedia_codec_mgr_alloc_codec(mgr, ci[0], &codec);
62 if (status != PJ_SUCCESS) {
63 rc = -40;
64 goto on_return;
65 }
66
67 status = pjmedia_codec_mgr_get_default_param(mgr, ci[0], &codec_param);
68 if (status != PJ_SUCCESS) {
69 rc = -50;
70 goto on_return;
71 }
72
73 codec_param.info.avg_bps = bitrate;
74 codec_param.setting.vad = 0;
75
76 status = codec->op->init(codec, pool);
77 if (status != PJ_SUCCESS) {
78 rc = -60;
79 goto on_return;
80 }
81
82 status = codec->op->open(codec, &codec_param);
83 if (status != PJ_SUCCESS) {
84 rc = -70;
85 goto on_return;
86 }
87
88 /* Open WAV file */
89 status = pjmedia_wav_player_port_create(pool, wav_file,
90 codec_param.info.frm_ptime,
91 PJMEDIA_FILE_NO_LOOP, 0,
92 &wav_port);
93 if (status != PJ_SUCCESS) {
94 rc = -80;
95 goto on_return;
96 }
97
98 /* Open output file */
99 output = fopen(TMP_OUT, "wb");
100 if (!output) {
101 rc = -90;
102 goto on_return;
103 }
104
105 /* Allocate buffer for PCM and encoded frames */
106 samples_per_frame = codec_param.info.clock_rate * codec_param.info.frm_ptime / 1000;
107 in_frame.buf = pj_pool_alloc(pool, samples_per_frame * 2);
108 out_frame.buf = (pj_uint8_t*) pj_pool_alloc(pool, samples_per_frame);
109
110 /* Loop read WAV file and encode and write to output file */
111 for (;;) {
112 in_frame.size = samples_per_frame * 2;
113 in_frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
114
115 status = pjmedia_port_get_frame(wav_port, &in_frame);
116 if (status != PJ_SUCCESS || in_frame.type != PJMEDIA_FRAME_TYPE_AUDIO)
117 break;
118
119 out_frame.size = samples_per_frame;
120 status = codec->op->encode(codec, &in_frame, samples_per_frame,
121 &out_frame);
122 if (status != PJ_SUCCESS) {
123 rc = -95;
124 goto on_return;
125 }
126
127 if (out_frame.size) {
Benny Prijono2eabad92009-05-06 14:34:13 +0000128 int cnt;
129
130 cnt = fwrite(out_frame.buf, out_frame.size, 1, output);
Benny Prijono1dc0da72009-04-18 00:12:13 +0000131
132 if (encoded_frame_len == 0)
133 encoded_frame_len = out_frame.size;
Benny Prijono2eabad92009-05-06 14:34:13 +0000134 }
135 }
Benny Prijono1dc0da72009-04-18 00:12:13 +0000136
137 fclose(output);
138 output = NULL;
139
140 /* Compare encoded files */
Benny Prijono8604b142009-04-18 11:49:54 +0000141 fref = fopen(ref_encoded_file, "rb");
Benny Prijono1dc0da72009-04-18 00:12:13 +0000142 if (!fref) {
143 rc = -100;
144 goto on_return;
145 }
146
147 output = fopen(TMP_OUT, "rb");
148 if (!output) {
149 rc = -110;
150 goto on_return;
151 }
152
153 pos = 0;
154 for (;;) {
155 int count;
156
157 count = fread(in_frame.buf, encoded_frame_len, 1, fref);
158 if (count != 1)
159 break;
160
161 count = fread(out_frame.buf, encoded_frame_len, 1, output);
162 if (count != 1)
163 break;
164
165 if (memcmp(in_frame.buf, out_frame.buf, encoded_frame_len)) {
166 unsigned i;
167 pj_uint8_t *in = (pj_uint8_t*)in_frame.buf;
168 pj_uint8_t *out = (pj_uint8_t*)out_frame.buf;
169
170 for (i=0; i<encoded_frame_len; ++i) {
171 if (in[i] != out[i])
172 break;
173 }
174
175 PJ_LOG(1,(THIS_FILE," failed: mismatch at pos %d", pos+i));
176 rc = -200;
177 break;
178 }
179
180 pos += encoded_frame_len;
181 }
182
183on_return:
184 if (output)
185 fclose(output);
186
187 if (fref)
188 fclose(fref);
189
190 if (codec) {
191 codec->op->close(codec);
192 pjmedia_codec_mgr_dealloc_codec(mgr, codec);
193 }
194
195 if (wav_port)
196 pjmedia_port_destroy(wav_port);
197
198 if (pool)
199 pj_pool_release(pool);
200
201 return rc;
202}
203
204
Benny Prijono8604b142009-04-18 11:49:54 +0000205/*
206 * Read file in ITU format (".itu" extension).
207 *
208 * Set swap_endian to TRUE if the ITU file is stored in little
209 * endian format (normally true).
210 */
211static int read_ITU_format(FILE *fp_bitstream,
212 short *out_words,
213 short *p_frame_error_flag,
214 int number_of_16bit_words_per_frame,
215 pj_bool_t swap_endian)
216{
217 enum { MAX_BITS_PER_FRAME = 160*8 };
218 short i,j;
219 short nsamp;
220 short packed_word;
221 short bit_count;
222 short bit;
223 short in_array[MAX_BITS_PER_FRAME+2];
224 short one = 0x0081;
225 short zero = 0x007f;
226 short frame_start = 0x6b21;
Benny Prijono1dc0da72009-04-18 00:12:13 +0000227
Nanang Izzuddin2e4f03f2009-04-27 19:18:38 +0000228 nsamp = (short)fread(in_array, 2, 2 + 16*number_of_16bit_words_per_frame,
229 fp_bitstream);
Benny Prijono8604b142009-04-18 11:49:54 +0000230
231 j = 0;
232 bit = in_array[j++];
233 if (bit != frame_start) {
234 *p_frame_error_flag = 1;
235 } else {
236 *p_frame_error_flag = 0;
237
238 /* increment j to skip over the number of bits in frame */
239 j++;
240
241 for (i=0; i<number_of_16bit_words_per_frame; i++) {
242 packed_word = 0;
243 bit_count = 15;
244 while (bit_count >= 0) {
245 bit = in_array[j++];
246 if (bit == zero)
247 bit = 0;
248 else if (bit == one)
249 bit = 1;
250 else
251 *p_frame_error_flag = 1;
252
253 packed_word <<= 1;
254 packed_word = (short )(packed_word + bit);
255 bit_count--;
256 }
257
258 if (swap_endian)
259 out_words[i] = pj_ntohs(packed_word);
260 else
261 out_words[i] = packed_word;
262 }
263 }
264 return (nsamp-1)/16;
265}
266
267
268/*
269 * Decode test
270 *
271 * Decode the specified encoded file in "in_encoded_file" into temporary
272 * PCM output file, and compare the temporary PCM output file with
273 * the PCM reference file.
274 *
275 * Some reference file requires manipulation to the PCM output
276 * before comparison, such manipulation can be done by supplying
277 * this function with the "manip" function.
278 */
Benny Prijono1dc0da72009-04-18 00:12:13 +0000279static int codec_test_decode(pjmedia_codec_mgr *mgr,
280 char *codec_name,
281 unsigned bitrate,
282 unsigned encoded_len,
Benny Prijono8604b142009-04-18 11:49:54 +0000283 const char *in_encoded_file,
284 const char *ref_pcm_file,
Benny Prijono1dc0da72009-04-18 00:12:13 +0000285 void (*manip)(short *pcm, unsigned count))
286{
287 pj_str_t codec_id = pj_str(codec_name);
288 pj_pool_t *pool = NULL;
289 unsigned count, samples_per_frame, pos;
290 pjmedia_codec *codec = NULL;
291 const pjmedia_codec_info *ci[1];
292 pjmedia_codec_param codec_param;
293 pjmedia_frame out_frame;
294 void *pkt;
295 FILE *input = NULL, *output = NULL, *fref = NULL;
Benny Prijono8604b142009-04-18 11:49:54 +0000296 pj_bool_t is_itu_format = PJ_FALSE;
Benny Prijono1dc0da72009-04-18 00:12:13 +0000297 int rc = 0;
298 pj_status_t status;
299
300 pool = pj_pool_create(mem, "codec-vectors", 512, 512, NULL);
301 if (!pool) {
302 rc = -20;
303 goto on_return;
304 }
305
306 /* Find and open the codec */
307 count = 1;
308 status = pjmedia_codec_mgr_find_codecs_by_id(mgr, &codec_id, &count, ci, NULL);
309 if (status != PJ_SUCCESS) {
310 rc = -30;
311 goto on_return;
312 }
313
314 status = pjmedia_codec_mgr_alloc_codec(mgr, ci[0], &codec);
315 if (status != PJ_SUCCESS) {
316 rc = -40;
317 goto on_return;
318 }
319
320 status = pjmedia_codec_mgr_get_default_param(mgr, ci[0], &codec_param);
321 if (status != PJ_SUCCESS) {
322 rc = -50;
323 goto on_return;
324 }
325
326 codec_param.info.avg_bps = bitrate;
327 codec_param.setting.vad = 0;
328
329 status = codec->op->init(codec, pool);
330 if (status != PJ_SUCCESS) {
331 rc = -60;
332 goto on_return;
333 }
334
335 status = codec->op->open(codec, &codec_param);
336 if (status != PJ_SUCCESS) {
337 rc = -70;
338 goto on_return;
339 }
340
341 /* Open input file */
Benny Prijono8604b142009-04-18 11:49:54 +0000342 input = fopen(in_encoded_file, "rb");
Benny Prijono1dc0da72009-04-18 00:12:13 +0000343 if (!input) {
344 rc = -80;
345 goto on_return;
346 }
347
Benny Prijono8604b142009-04-18 11:49:54 +0000348 /* Is the file in ITU format? */
349 is_itu_format = pj_ansi_stricmp(in_encoded_file+strlen(in_encoded_file)-4,
350 ".itu")==0;
351
Benny Prijono1dc0da72009-04-18 00:12:13 +0000352 /* Open output file */
353 output = fopen(TMP_OUT, "wb");
354 if (!output) {
355 rc = -90;
356 goto on_return;
357 }
358
359 /* Allocate buffer for PCM and encoded frames */
360 samples_per_frame = codec_param.info.clock_rate * codec_param.info.frm_ptime / 1000;
361 pkt = pj_pool_alloc(pool, samples_per_frame * 2);
362 out_frame.buf = (pj_uint8_t*) pj_pool_alloc(pool, samples_per_frame * 2);
363
364 /* Loop read WAV file and encode and write to output file */
365 for (;;) {
366 pjmedia_frame in_frame[2];
367 pj_timestamp ts;
368 unsigned count;
Benny Prijono8604b142009-04-18 11:49:54 +0000369 pj_bool_t has_frame;
Benny Prijono1dc0da72009-04-18 00:12:13 +0000370
Benny Prijono8604b142009-04-18 11:49:54 +0000371 if (is_itu_format) {
372 int nsamp;
373 short frame_err = 0;
Benny Prijono1dc0da72009-04-18 00:12:13 +0000374
Benny Prijono8604b142009-04-18 11:49:54 +0000375 nsamp = read_ITU_format(input, (short*)pkt, &frame_err,
376 encoded_len / 2, PJ_TRUE);
377 if (nsamp != (int)encoded_len / 2)
378 break;
379
380 has_frame = !frame_err;
381 } else {
382 if (fread(pkt, encoded_len, 1, input) != 1)
383 break;
384
385 has_frame = PJ_TRUE;
Benny Prijono1dc0da72009-04-18 00:12:13 +0000386 }
387
Benny Prijono8604b142009-04-18 11:49:54 +0000388 if (has_frame) {
389 count = 2;
390 if (codec->op->parse(codec, pkt, encoded_len, &ts,
391 &count, in_frame) != PJ_SUCCESS)
392 {
393 rc = -100;
394 goto on_return;
395 }
Benny Prijono1dc0da72009-04-18 00:12:13 +0000396
Benny Prijono8604b142009-04-18 11:49:54 +0000397 if (count != 1) {
398 rc = -110;
399 goto on_return;
400 }
401
402 if (codec->op->decode(codec, &in_frame[0], samples_per_frame*2,
403 &out_frame) != PJ_SUCCESS)
404 {
405 rc = -120;
406 goto on_return;
407 }
408 } else {
409 if (codec->op->recover(codec, samples_per_frame*2,
410 &out_frame) != PJ_SUCCESS)
411 {
412 rc = -125;
413 goto on_return;
414 }
Benny Prijono1dc0da72009-04-18 00:12:13 +0000415 }
416
417 if (manip)
418 manip((short*)out_frame.buf, samples_per_frame);
419
420 if (fwrite(out_frame.buf, out_frame.size, 1, output) != 1) {
421 rc = -130;
422 goto on_return;
423 }
424 }
425
426 fclose(input);
427 input = NULL;
428
429 fclose(output);
430 output = NULL;
431
432 /* Compare encoded files */
Benny Prijono8604b142009-04-18 11:49:54 +0000433 fref = fopen(ref_pcm_file, "rb");
Benny Prijono1dc0da72009-04-18 00:12:13 +0000434 if (!fref) {
435 rc = -140;
436 goto on_return;
437 }
438
439 output = fopen(TMP_OUT, "rb");
440 if (!output) {
441 rc = -110;
442 goto on_return;
443 }
444
445 pos = 0;
446 for (;;) {
447 int count;
448
449 count = fread(pkt, samples_per_frame*2, 1, fref);
450 if (count != 1)
451 break;
452
453 count = fread(out_frame.buf, samples_per_frame*2, 1, output);
454 if (count != 1)
455 break;
456
457 if (memcmp(pkt, out_frame.buf, samples_per_frame*2)) {
458 unsigned i;
459 pj_int16_t *in = (pj_int16_t*)pkt;
460 pj_int16_t *out = (pj_int16_t*)out_frame.buf;
461
462 for (i=0; i<samples_per_frame; ++i) {
463 if (in[i] != out[i])
464 break;
465 }
466
467 PJ_LOG(1,(THIS_FILE," failed: mismatch at samples %d", pos+i));
468 rc = -200;
469 break;
470 }
471
472 pos += samples_per_frame;
473 }
474
475on_return:
476 if (output)
477 fclose(output);
478
479 if (fref)
480 fclose(fref);
481
482 if (input)
483 fclose(input);
484
485 if (codec) {
486 codec->op->close(codec);
487 pjmedia_codec_mgr_dealloc_codec(mgr, codec);
488 }
489
490 if (pool)
491 pj_pool_release(pool);
492
493 return rc;
494}
495
Benny Prijono2eabad92009-05-06 14:34:13 +0000496#if PJMEDIA_HAS_G7221_CODEC
Benny Prijono1dc0da72009-04-18 00:12:13 +0000497/* For ITU testing, off the 2 lsbs. */
498static void g7221_pcm_manip(short *pcm, unsigned count)
499{
500 unsigned i;
501 for (i=0; i<count; i++)
502 pcm[i] &= 0xfffc;
503
504}
Benny Prijono2eabad92009-05-06 14:34:13 +0000505#endif /* PJMEDIA_HAS_G7221_CODEC */
Benny Prijono1dc0da72009-04-18 00:12:13 +0000506
507int codec_test_vectors(void)
508{
509 pjmedia_endpt *endpt;
510 pjmedia_codec_mgr *mgr;
511 int rc, rc_final = 0;
512 struct enc_vectors {
513 char *codec_name;
514 unsigned bit_rate;
515 const char *wav_file;
Benny Prijono8604b142009-04-18 11:49:54 +0000516 const char *ref_encoded_file;
Benny Prijono1dc0da72009-04-18 00:12:13 +0000517 } enc_vectors[] =
518 {
519#if PJMEDIA_HAS_G7221_CODEC
520 { "G7221/16000/1", 24000,
521 "../src/test/vectors/g722_1_enc_in.wav",
522 "../src/test/vectors/g722_1_enc_out_24000_be.pak"
523 },
524 { "G7221/16000/1", 32000,
525 "../src/test/vectors/g722_1_enc_in.wav",
526 "../src/test/vectors/g722_1_enc_out_32000_be.pak"
527 },
528#endif
Benny Prijonod4c751e2009-04-18 00:34:45 +0000529 { NULL }
Benny Prijono1dc0da72009-04-18 00:12:13 +0000530 };
531 struct dec_vectors {
532 char *codec_name;
533 unsigned bit_rate;
534 unsigned encoded_frame_len;
535 void (*manip)(short *pcm, unsigned count);
536 const char *enc_file;
Benny Prijono8604b142009-04-18 11:49:54 +0000537 const char *ref_pcm_file;
Benny Prijono1dc0da72009-04-18 00:12:13 +0000538 } dec_vectors[] =
539 {
540#if PJMEDIA_HAS_G7221_CODEC
541 { "G7221/16000/1", 24000, 60,
542 &g7221_pcm_manip,
543 "../src/test/vectors/g722_1_enc_out_24000_be.pak",
544 "../src/test/vectors/g722_1_dec_out_24000.pcm"
545 },
546 { "G7221/16000/1", 32000, 80,
547 &g7221_pcm_manip,
548 "../src/test/vectors/g722_1_enc_out_32000_be.pak",
549 "../src/test/vectors/g722_1_dec_out_32000.pcm"
550 },
Benny Prijono8604b142009-04-18 11:49:54 +0000551 { "G7221/16000/1", 24000, 60,
552 &g7221_pcm_manip,
553 "../src/test/vectors/g722_1_dec_in_24000_fe.itu",
554 "../src/test/vectors/g722_1_dec_out_24000_fe.pcm"
555 },
556 { "G7221/16000/1", 32000, 80,
557 &g7221_pcm_manip,
558 "../src/test/vectors/g722_1_dec_in_32000_fe.itu",
559 "../src/test/vectors/g722_1_dec_out_32000_fe.pcm"
560 },
Benny Prijono1dc0da72009-04-18 00:12:13 +0000561#endif
Benny Prijonod4c751e2009-04-18 00:34:45 +0000562 { NULL }
Benny Prijono1dc0da72009-04-18 00:12:13 +0000563 };
564 unsigned i;
565 pj_status_t status;
566
567 status = pjmedia_endpt_create(mem, NULL, 0, &endpt);
568 if (status != PJ_SUCCESS)
569 return -5;
570
571 mgr = pjmedia_endpt_get_codec_mgr(endpt);
572
573#if PJMEDIA_HAS_G7221_CODEC
574 status = pjmedia_codec_g7221_init(endpt);
575 if (status != PJ_SUCCESS) {
576 pjmedia_endpt_destroy(endpt);
577 return -7;
578 }
Benny Prijono8496b932009-04-20 14:19:11 +0000579
580 /* Set shift value to zero for the test vectors */
581 pjmedia_codec_g7221_set_pcm_shift(0);
Benny Prijono1dc0da72009-04-18 00:12:13 +0000582#endif
583
584 PJ_LOG(3,(THIS_FILE," encode tests:"));
585 for (i=0; i<PJ_ARRAY_SIZE(enc_vectors); ++i) {
Benny Prijonod4c751e2009-04-18 00:34:45 +0000586 if (!enc_vectors[i].codec_name)
587 continue;
Benny Prijono8604b142009-04-18 11:49:54 +0000588 PJ_LOG(3,(THIS_FILE," %s @%d bps %s ==> %s",
589 enc_vectors[i].codec_name,
590 enc_vectors[i].bit_rate,
591 enc_vectors[i].wav_file,
592 enc_vectors[i].ref_encoded_file));
Benny Prijono1dc0da72009-04-18 00:12:13 +0000593 rc = codec_test_encode(mgr, enc_vectors[i].codec_name,
594 enc_vectors[i].bit_rate,
595 enc_vectors[i].wav_file,
Benny Prijono8604b142009-04-18 11:49:54 +0000596 enc_vectors[i].ref_encoded_file);
Benny Prijono1dc0da72009-04-18 00:12:13 +0000597 if (rc != 0)
598 rc_final = rc;
599 }
600
601 PJ_LOG(3,(THIS_FILE," decode tests:"));
602 for (i=0; i<PJ_ARRAY_SIZE(dec_vectors); ++i) {
Benny Prijonod4c751e2009-04-18 00:34:45 +0000603 if (!dec_vectors[i].codec_name)
604 continue;
Benny Prijono8604b142009-04-18 11:49:54 +0000605 PJ_LOG(3,(THIS_FILE," %s @%d bps %s ==> %s",
606 dec_vectors[i].codec_name,
607 dec_vectors[i].bit_rate,
608 dec_vectors[i].enc_file,
609 dec_vectors[i].ref_pcm_file));
Benny Prijono1dc0da72009-04-18 00:12:13 +0000610 rc = codec_test_decode(mgr, dec_vectors[i].codec_name,
611 dec_vectors[i].bit_rate,
612 dec_vectors[i].encoded_frame_len,
613 dec_vectors[i].enc_file,
Benny Prijono8604b142009-04-18 11:49:54 +0000614 dec_vectors[i].ref_pcm_file,
Benny Prijono1dc0da72009-04-18 00:12:13 +0000615 dec_vectors[i].manip);
616 if (rc != 0)
617 rc_final = rc;
618 }
619
620 if (pj_file_exists(TMP_OUT))
621 pj_file_delete(TMP_OUT);
622
623 pjmedia_endpt_destroy(endpt);
624 return rc_final;
625}
626