Debian Codegraph @

Xpjproject/pjmedia/src/test/codec_vectors.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include "test.h"
19 #include <pjmedia-codec.h>
20
21 #define THIS_FILE "codec_vectors.c"
22 #define TMP_OUT "output.tmp"
23
24 /*
25 * Encode test. Read input from WAV file, encode to temporary output file,
26 * and compare the temporary output file to the reference file.
27 */
28 static int codec_test_encode(pjmedia_codec_mgr *mgr,
29 char *codec_name,
30 unsigned bitrate,
31 const char *wav_file,
32 const char *ref_encoded_file)
33 {
34 pj_str_t codec_id = pj_str(codec_name);
35 pj_pool_t *pool = NULL;
36 unsigned count, samples_per_frame;
37 pj_size_t 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 /* For G7221, the bitrate is set via param.setting.dec_fmtp, if it has
77 * no info about bitrate, the codec will check info.avg_bps. So, let's
78 * just clear the SDP fmtp.
79 */
80 if (pj_ansi_strstr(codec_name, "G7221/")) {
81 codec_param.setting.dec_fmtp.cnt = 0;
82 }
83
84 status = pjmedia_codec_init(codec, pool);
85 if (status != PJ_SUCCESS) {
86 rc = -60;
87 goto on_return;
88 }
89
90 status = pjmedia_codec_open(codec, &codec_param);
91 if (status != PJ_SUCCESS) {
92 rc = -70;
93 goto on_return;
94 }
95
96 /* Open WAV file */
97 status = pjmedia_wav_player_port_create(pool, wav_file,
98 codec_param.info.frm_ptime,
99 PJMEDIA_FILE_NO_LOOP, 0,
100 &wav_port);
101 if (status != PJ_SUCCESS) {
102 rc = -80;
103 goto on_return;
104 }
105
106 /* Open output file */
107 output = fopen(TMP_OUT, "wb");
108 if (!output) {
109 rc = -90;
110 goto on_return;
111 }
112
113 /* Allocate buffer for PCM and encoded frames */
114 samples_per_frame = codec_param.info.clock_rate * codec_param.info.frm_ptime / 1000;
115 in_frame.buf = pj_pool_alloc(pool, samples_per_frame * 2);
116 out_frame.buf = (pj_uint8_t*) pj_pool_alloc(pool, samples_per_frame);
117
118 /* Loop read WAV file and encode and write to output file */
119 for (;;) {
120 in_frame.size = samples_per_frame * 2;
121 in_frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
122
123 status = pjmedia_port_get_frame(wav_port, &in_frame);
124 if (status != PJ_SUCCESS || in_frame.type != PJMEDIA_FRAME_TYPE_AUDIO)
125 break;
126
127 out_frame.size = samples_per_frame;
128 status = pjmedia_codec_encode(codec, &in_frame, samples_per_frame,
129 &out_frame);
130 if (status != PJ_SUCCESS) {
131 rc = -95;
132 goto on_return;
133 }
134
135 if (out_frame.size) {
136 fwrite(out_frame.buf, out_frame.size, 1, output);
137
138 if (encoded_frame_len == 0)
139 encoded_frame_len = out_frame.size;
140 }
141 }
142
143 fclose(output);
144 output = NULL;
145
146 /* Compare encoded files */
147 fref = fopen(ref_encoded_file, "rb");
148 if (!fref) {
149 rc = -100;
150 goto on_return;
151 }
152
153 output = fopen(TMP_OUT, "rb");
154 if (!output) {
155 rc = -110;
156 goto on_return;
157 }
158
159 pos = 0;
160 for (;;) {
161 pj_size_t count2;
162
163 count2 = fread(in_frame.buf, encoded_frame_len, 1, fref);
164 if (count2 != 1)
165 break;
166
167 count2 = fread(out_frame.buf, encoded_frame_len, 1, output);
168 if (count2 != 1)
169 break;
170
171 if (memcmp(in_frame.buf, out_frame.buf, encoded_frame_len)) {
172 unsigned i;
173 pj_uint8_t *in = (pj_uint8_t*)in_frame.buf;
174 pj_uint8_t *out = (pj_uint8_t*)out_frame.buf;
175
176 for (i=0; i<encoded_frame_len; ++i) {
177 if (in[i] != out[i])
178 break;
179 }
180
181 PJ_LOG(1,(THIS_FILE," failed: mismatch at pos %lu",
182 (unsigned long)(pos+i)));
183 rc = -200;
184 break;
185 }
186
187 pos += encoded_frame_len;
188 }
189
190 on_return:
191 if (output)
192 fclose(output);
193
194 if (fref)
195 fclose(fref);
196
197 if (codec) {
198 pjmedia_codec_close(codec);
199 pjmedia_codec_mgr_dealloc_codec(mgr, codec);
200 }
201
202 if (wav_port)
203 pjmedia_port_destroy(wav_port);
204
205 if (pool)
206 pj_pool_release(pool);
207
208 return rc;
209 }
210
211
212 /*
213 * Read file in ITU format (".itu" extension).
214 *
215 * Set swap_endian to TRUE if the ITU file is stored in little
216 * endian format (normally true).
217 */
218 static int read_ITU_format(FILE *fp_bitstream,
219 short *out_words,
220 short *p_frame_error_flag,
221 int number_of_16bit_words_per_frame,
222 pj_bool_t swap_endian)
223 {
224 enum { MAX_BITS_PER_FRAME = 160*8 };
225 int i,j;
226 short nsamp;
227 short packed_word;
228 short bit_count;
229 short bit;
230 short in_array[MAX_BITS_PER_FRAME+2];
231 short one = 0x0081;
232 short zero = 0x007f;
233 short frame_start = 0x6b21;
234
235 nsamp = (short)fread(in_array, 2, 2 + 16*number_of_16bit_words_per_frame,
236 fp_bitstream);
237
238 j = 0;
239 bit = in_array[j++];
240 if (bit != frame_start) {
241 *p_frame_error_flag = 1;
242 } else {
243 *p_frame_error_flag = 0;
244
245 /* increment j to skip over the number of bits in frame */
246 j++;
247
248 for (i=0; i<number_of_16bit_words_per_frame; i++) {
249 packed_word = 0;
250 bit_count = 15;
251 while (bit_count >= 0) {
252 bit = in_array[j++];
253 if (bit == zero)
254 bit = 0;
255 else if (bit == one)
256 bit = 1;
257 else
258 *p_frame_error_flag = 1;
259
260 packed_word <<= 1;
261 packed_word = (short )(packed_word + bit);
262 bit_count--;
263 }
264
265 if (swap_endian)
266 out_words[i] = pj_ntohs(packed_word);
267 else
268 out_words[i] = packed_word;
269 }
270 }
271 return (nsamp-1)/16;
272 }
273
274
275 /*
276 * Decode test
277 *
278 * Decode the specified encoded file in "in_encoded_file" into temporary
279 * PCM output file, and compare the temporary PCM output file with
280 * the PCM reference file.
281 *
282 * Some reference file requires manipulation to the PCM output
283 * before comparison, such manipulation can be done by supplying
284 * this function with the "manip" function.
285 */
286 static int codec_test_decode(pjmedia_codec_mgr *mgr,
287 char *codec_name,
288 unsigned bitrate,
289 unsigned encoded_len,
290 const char *in_encoded_file,
291 const char *ref_pcm_file,
292 void (*manip)(short *pcm, unsigned count))
293 {
294 pj_str_t codec_id = pj_str(codec_name);
295 pj_pool_t *pool = NULL;
296 unsigned count, samples_per_frame, pos;
297 pjmedia_codec *codec = NULL;
298 const pjmedia_codec_info *ci[1];
299 pjmedia_codec_param codec_param;
300 pjmedia_frame out_frame;
301 void *pkt;
302 FILE *input = NULL, *output = NULL, *fref = NULL;
303 pj_bool_t is_itu_format = PJ_FALSE;
304 int rc = 0;
305 pj_status_t status;
306
307 pool = pj_pool_create(mem, "codec-vectors", 512, 512, NULL);
308 if (!pool) {
309 rc = -20;
310 goto on_return;
311 }
312
313 /* Find and open the codec */
314 count = 1;
315 status = pjmedia_codec_mgr_find_codecs_by_id(mgr, &codec_id, &count, ci, NULL);
316 if (status != PJ_SUCCESS) {
317 rc = -30;
318 goto on_return;
319 }
320
321 status = pjmedia_codec_mgr_alloc_codec(mgr, ci[0], &codec);
322 if (status != PJ_SUCCESS) {
323 rc = -40;
324 goto on_return;
325 }
326
327 status = pjmedia_codec_mgr_get_default_param(mgr, ci[0], &codec_param);
328 if (status != PJ_SUCCESS) {
329 rc = -50;
330 goto on_return;
331 }
332
333 codec_param.info.avg_bps = bitrate;
334 codec_param.setting.vad = 0;
335
336 /* For G7221, the bitrate is set via param.setting.dec_fmtp, if it has
337 * no info about bitrate, the codec will check info.avg_bps. So, let's
338 * just clear the SDP fmtp.
339 */
340 if (pj_ansi_strstr(codec_name, "G7221/")) {
341 codec_param.setting.dec_fmtp.cnt = 0;
342 }
343
344 status = pjmedia_codec_init(codec, pool);
345 if (status != PJ_SUCCESS) {
346 rc = -60;
347 goto on_return;
348 }
349
350 status = pjmedia_codec_open(codec, &codec_param);
351 if (status != PJ_SUCCESS) {
352 rc = -70;
353 goto on_return;
354 }
355
356 /* Open input file */
357 input = fopen(in_encoded_file, "rb");
358 if (!input) {
359 rc = -80;
360 goto on_return;
361 }
362
363 /* Is the file in ITU format? */
364 is_itu_format = pj_ansi_stricmp(in_encoded_file+strlen(in_encoded_file)-4,
365 ".itu")==0;
366
367 /* Open output file */
368 output = fopen(TMP_OUT, "wb");
369 if (!output) {
370 rc = -90;
371 goto on_return;
372 }
373
374 /* Allocate buffer for PCM and encoded frames */
375 samples_per_frame = codec_param.info.clock_rate * codec_param.info.frm_ptime / 1000;
376 pkt = pj_pool_alloc(pool, samples_per_frame * 2);
377 out_frame.buf = (pj_uint8_t*) pj_pool_alloc(pool, samples_per_frame * 2);
378
379 /* Loop read WAV file and encode and write to output file */
380 for (;;) {
381 pjmedia_frame in_frame[2];
382 pj_timestamp ts;
383 unsigned count2;
384 pj_bool_t has_frame;
385
386 if (is_itu_format) {
387 int nsamp;
388 short frame_err = 0;
389
390 nsamp = read_ITU_format(input, (short*)pkt, &frame_err,
391 encoded_len / 2, PJ_TRUE);
392 if (nsamp != (int)encoded_len / 2)
393 break;
394
395 has_frame = !frame_err;
396 } else {
397 if (fread(pkt, encoded_len, 1, input) != 1)
398 break;
399
400 has_frame = PJ_TRUE;
401 }
402
403 if (has_frame) {
404 count2 = 2;
405 if (pjmedia_codec_parse(codec, pkt, encoded_len, &ts,
406 &count2, in_frame) != PJ_SUCCESS)
407 {
408 rc = -100;
409 goto on_return;
410 }
411
412 if (count2 != 1) {
413 rc = -110;
414 goto on_return;
415 }
416
417 if (pjmedia_codec_decode(codec, &in_frame[0], samples_per_frame*2,
418 &out_frame) != PJ_SUCCESS)
419 {
420 rc = -120;
421 goto on_return;
422 }
423 } else {
424 if (pjmedia_codec_recover(codec, samples_per_frame*2,
425 &out_frame) != PJ_SUCCESS)
426 {
427 rc = -125;
428 goto on_return;
429 }
430 }
431
432 if (manip)
433 manip((short*)out_frame.buf, samples_per_frame);
434
435 if (fwrite(out_frame.buf, out_frame.size, 1, output) != 1) {
436 rc = -130;
437 goto on_return;
438 }
439 }
440
441 fclose(input);
442 input = NULL;
443
444 fclose(output);
445 output = NULL;
446
447 /* Compare encoded files */
448 fref = fopen(ref_pcm_file, "rb");
449 if (!fref) {
450 rc = -140;
451 goto on_return;
452 }
453
454 output = fopen(TMP_OUT, "rb");
455 if (!output) {
456 rc = -110;
457 goto on_return;
458 }
459
460 pos = 0;
461 for (;;) {
462 pj_size_t count2;
463
464 count2 = fread(pkt, samples_per_frame*2, 1, fref);
465 if (count2 != 1)
466 break;
467
468 count2 = fread(out_frame.buf, samples_per_frame*2, 1, output);
469 if (count2 != 1)
470 break;
471
472 if (memcmp(pkt, out_frame.buf, samples_per_frame*2)) {
473 unsigned i;
474 pj_int16_t *in = (pj_int16_t*)pkt;
475 pj_int16_t *out = (pj_int16_t*)out_frame.buf;
476
477 for (i=0; i<samples_per_frame; ++i) {
478 if (in[i] != out[i])
479 break;
480 }
481
482 PJ_LOG(1,(THIS_FILE," failed: mismatch at samples %d", pos+i));
483 rc = -200;
484 break;
485 }
486
487 pos += samples_per_frame;
488 }
489
490 on_return:
491 if (output)
492 fclose(output);
493
494 if (fref)
495 fclose(fref);
496
497 if (input)
498 fclose(input);
499
500 if (codec) {
501 pjmedia_codec_close(codec);
502 pjmedia_codec_mgr_dealloc_codec(mgr, codec);
503 }
504
505 if (pool)
506 pj_pool_release(pool);
507
508 return rc;
509 }
510
511 #if PJMEDIA_HAS_G7221_CODEC
512 /* For ITU testing, off the 2 lsbs. */
513 static void g7221_pcm_manip(short *pcm, unsigned count)
514 {
515 unsigned i;
516 for (i=0; i<count; i++)
517 pcm[i] &= 0xfffc;
518
519 }
520 #endif /* PJMEDIA_HAS_G7221_CODEC */
521
522 int codec_test_vectors(void)
523 {
524 pjmedia_endpt *endpt;
525 pjmedia_codec_mgr *mgr;
526 int rc, rc_final = 0;
527 struct enc_vectors {
528 char *codec_name;
529 unsigned bit_rate;
530 const char *wav_file;
531 const char *ref_encoded_file;
532 } enc_vectors[] =
533 {
534 #if PJMEDIA_HAS_G7221_CODEC
535 { "G7221/16000/1", 24000,
536 "../src/test/vectors/g722_1_enc_in.wav",
537 "../src/test/vectors/g722_1_enc_out_24000_be.pak"
538 },
539 { "G7221/16000/1", 32000,
540 "../src/test/vectors/g722_1_enc_in.wav",
541 "../src/test/vectors/g722_1_enc_out_32000_be.pak"
542 },
543 #endif
544 { NULL }
545 };
546 struct dec_vectors {
547 char *codec_name;
548 unsigned bit_rate;
549 unsigned encoded_frame_len;
550 void (*manip)(short *pcm, unsigned count);
551 const char *enc_file;
552 const char *ref_pcm_file;
553 } dec_vectors[] =
554 {
555 #if PJMEDIA_HAS_G7221_CODEC
556 { "G7221/16000/1", 24000, 60,
557 &g7221_pcm_manip,
558 "../src/test/vectors/g722_1_enc_out_24000_be.pak",
559 "../src/test/vectors/g722_1_dec_out_24000.pcm"
560 },
561 { "G7221/16000/1", 32000, 80,
562 &g7221_pcm_manip,
563 "../src/test/vectors/g722_1_enc_out_32000_be.pak",
564 "../src/test/vectors/g722_1_dec_out_32000.pcm"
565 },
566 { "G7221/16000/1", 24000, 60,
567 &g7221_pcm_manip,
568 "../src/test/vectors/g722_1_dec_in_24000_fe.itu",
569 "../src/test/vectors/g722_1_dec_out_24000_fe.pcm"
570 },
571 { "G7221/16000/1", 32000, 80,
572 &g7221_pcm_manip,
573 "../src/test/vectors/g722_1_dec_in_32000_fe.itu",
574 "../src/test/vectors/g722_1_dec_out_32000_fe.pcm"
575 },
576 #endif
577 { NULL }
578 };
579 unsigned i;
580 pj_status_t status;
581
582 status = pjmedia_endpt_create2(mem, NULL, 0, &endpt);
583 if (status != PJ_SUCCESS)
584 return -5;
585
586 mgr = pjmedia_endpt_get_codec_mgr(endpt);
587
588 #if PJMEDIA_HAS_G7221_CODEC
589 status = pjmedia_codec_g7221_init(endpt);
590 if (status != PJ_SUCCESS) {
591 pjmedia_endpt_destroy2(endpt);
592 return -7;
593 }
594
595 /* Set shift value to zero for the test vectors */
596 pjmedia_codec_g7221_set_pcm_shift(0);
597 #endif
598
599 PJ_LOG(3,(THIS_FILE," encode tests:"));
600 for (i=0; i<PJ_ARRAY_SIZE(enc_vectors); ++i) {
601 if (!enc_vectors[i].codec_name)
602 continue;
603 PJ_LOG(3,(THIS_FILE," %s @%d bps %s ==> %s",
604 enc_vectors[i].codec_name,
605 enc_vectors[i].bit_rate,
606 enc_vectors[i].wav_file,
607 enc_vectors[i].ref_encoded_file));
608 rc = codec_test_encode(mgr, enc_vectors[i].codec_name,
609 enc_vectors[i].bit_rate,
610 enc_vectors[i].wav_file,
611 enc_vectors[i].ref_encoded_file);
612 if (rc != 0)
613 rc_final = rc;
614 }
615
616 PJ_LOG(3,(THIS_FILE," decode tests:"));
617 for (i=0; i<PJ_ARRAY_SIZE(dec_vectors); ++i) {
618 if (!dec_vectors[i].codec_name)
619 continue;
620 PJ_LOG(3,(THIS_FILE," %s @%d bps %s ==> %s",
621 dec_vectors[i].codec_name,
622 dec_vectors[i].bit_rate,
623 dec_vectors[i].enc_file,
624 dec_vectors[i].ref_pcm_file));
625 rc = codec_test_decode(mgr, dec_vectors[i].codec_name,
626 dec_vectors[i].bit_rate,
627 dec_vectors[i].encoded_frame_len,
628 dec_vectors[i].enc_file,
629 dec_vectors[i].ref_pcm_file,
630 dec_vectors[i].manip);
631 if (rc != 0)
632 rc_final = rc;
633 }
634
635 if (pj_file_exists(TMP_OUT))
636 pj_file_delete(TMP_OUT);
637
638 pjmedia_endpt_destroy2(endpt);
639 return rc_final;
640 }
641