1
/* $Id: codec_vectors.c 3664 2011-07-19 03:42:28Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
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.
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.
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
20
#include <pjmedia-codec.h>
22
#define THIS_FILE "codec_vectors.c"
23
#define TMP_OUT "output.tmp"
26
* Encode test. Read input from WAV file, encode to temporary output file,
27
* and compare the temporary output file to the reference file.
29
static int codec_test_encode(pjmedia_codec_mgr *mgr,
33
const char *ref_encoded_file)
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;
47
pool = pj_pool_create(mem, "codec-vectors", 512, 512, NULL);
53
/* Find and open the codec */
55
status = pjmedia_codec_mgr_find_codecs_by_id(mgr, &codec_id, &count, ci, NULL);
56
if (status != PJ_SUCCESS) {
61
status = pjmedia_codec_mgr_alloc_codec(mgr, ci[0], &codec);
62
if (status != PJ_SUCCESS) {
67
status = pjmedia_codec_mgr_get_default_param(mgr, ci[0], &codec_param);
68
if (status != PJ_SUCCESS) {
73
codec_param.info.avg_bps = bitrate;
74
codec_param.setting.vad = 0;
76
status = pjmedia_codec_init(codec, pool);
77
if (status != PJ_SUCCESS) {
82
status = pjmedia_codec_open(codec, &codec_param);
83
if (status != PJ_SUCCESS) {
89
status = pjmedia_wav_player_port_create(pool, wav_file,
90
codec_param.info.frm_ptime,
91
PJMEDIA_FILE_NO_LOOP, 0,
93
if (status != PJ_SUCCESS) {
98
/* Open output file */
99
output = fopen(TMP_OUT, "wb");
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);
110
/* Loop read WAV file and encode and write to output file */
112
in_frame.size = samples_per_frame * 2;
113
in_frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
115
status = pjmedia_port_get_frame(wav_port, &in_frame);
116
if (status != PJ_SUCCESS || in_frame.type != PJMEDIA_FRAME_TYPE_AUDIO)
119
out_frame.size = samples_per_frame;
120
status = pjmedia_codec_encode(codec, &in_frame, samples_per_frame,
122
if (status != PJ_SUCCESS) {
127
if (out_frame.size) {
130
cnt = fwrite(out_frame.buf, out_frame.size, 1, output);
132
if (encoded_frame_len == 0)
133
encoded_frame_len = out_frame.size;
140
/* Compare encoded files */
141
fref = fopen(ref_encoded_file, "rb");
147
output = fopen(TMP_OUT, "rb");
157
count = fread(in_frame.buf, encoded_frame_len, 1, fref);
161
count = fread(out_frame.buf, encoded_frame_len, 1, output);
165
if (memcmp(in_frame.buf, out_frame.buf, encoded_frame_len)) {
167
pj_uint8_t *in = (pj_uint8_t*)in_frame.buf;
168
pj_uint8_t *out = (pj_uint8_t*)out_frame.buf;
170
for (i=0; i<encoded_frame_len; ++i) {
175
PJ_LOG(1,(THIS_FILE," failed: mismatch at pos %d", pos+i));
180
pos += encoded_frame_len;
191
pjmedia_codec_close(codec);
192
pjmedia_codec_mgr_dealloc_codec(mgr, codec);
196
pjmedia_port_destroy(wav_port);
199
pj_pool_release(pool);
206
* Read file in ITU format (".itu" extension).
208
* Set swap_endian to TRUE if the ITU file is stored in little
209
* endian format (normally true).
211
static int read_ITU_format(FILE *fp_bitstream,
213
short *p_frame_error_flag,
214
int number_of_16bit_words_per_frame,
215
pj_bool_t swap_endian)
217
enum { MAX_BITS_PER_FRAME = 160*8 };
223
short in_array[MAX_BITS_PER_FRAME+2];
226
short frame_start = 0x6b21;
228
nsamp = (short)fread(in_array, 2, 2 + 16*number_of_16bit_words_per_frame,
233
if (bit != frame_start) {
234
*p_frame_error_flag = 1;
236
*p_frame_error_flag = 0;
238
/* increment j to skip over the number of bits in frame */
241
for (i=0; i<number_of_16bit_words_per_frame; i++) {
244
while (bit_count >= 0) {
251
*p_frame_error_flag = 1;
254
packed_word = (short )(packed_word + bit);
259
out_words[i] = pj_ntohs(packed_word);
261
out_words[i] = packed_word;
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.
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.
279
static int codec_test_decode(pjmedia_codec_mgr *mgr,
282
unsigned encoded_len,
283
const char *in_encoded_file,
284
const char *ref_pcm_file,
285
void (*manip)(short *pcm, unsigned count))
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;
295
FILE *input = NULL, *output = NULL, *fref = NULL;
296
pj_bool_t is_itu_format = PJ_FALSE;
300
pool = pj_pool_create(mem, "codec-vectors", 512, 512, NULL);
306
/* Find and open the codec */
308
status = pjmedia_codec_mgr_find_codecs_by_id(mgr, &codec_id, &count, ci, NULL);
309
if (status != PJ_SUCCESS) {
314
status = pjmedia_codec_mgr_alloc_codec(mgr, ci[0], &codec);
315
if (status != PJ_SUCCESS) {
320
status = pjmedia_codec_mgr_get_default_param(mgr, ci[0], &codec_param);
321
if (status != PJ_SUCCESS) {
326
codec_param.info.avg_bps = bitrate;
327
codec_param.setting.vad = 0;
329
status = pjmedia_codec_init(codec, pool);
330
if (status != PJ_SUCCESS) {
335
status = pjmedia_codec_open(codec, &codec_param);
336
if (status != PJ_SUCCESS) {
341
/* Open input file */
342
input = fopen(in_encoded_file, "rb");
348
/* Is the file in ITU format? */
349
is_itu_format = pj_ansi_stricmp(in_encoded_file+strlen(in_encoded_file)-4,
352
/* Open output file */
353
output = fopen(TMP_OUT, "wb");
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);
364
/* Loop read WAV file and encode and write to output file */
366
pjmedia_frame in_frame[2];
375
nsamp = read_ITU_format(input, (short*)pkt, &frame_err,
376
encoded_len / 2, PJ_TRUE);
377
if (nsamp != (int)encoded_len / 2)
380
has_frame = !frame_err;
382
if (fread(pkt, encoded_len, 1, input) != 1)
390
if (pjmedia_codec_parse(codec, pkt, encoded_len, &ts,
391
&count, in_frame) != PJ_SUCCESS)
402
if (pjmedia_codec_decode(codec, &in_frame[0], samples_per_frame*2,
403
&out_frame) != PJ_SUCCESS)
409
if (pjmedia_codec_recover(codec, samples_per_frame*2,
410
&out_frame) != PJ_SUCCESS)
418
manip((short*)out_frame.buf, samples_per_frame);
420
if (fwrite(out_frame.buf, out_frame.size, 1, output) != 1) {
432
/* Compare encoded files */
433
fref = fopen(ref_pcm_file, "rb");
439
output = fopen(TMP_OUT, "rb");
449
count = fread(pkt, samples_per_frame*2, 1, fref);
453
count = fread(out_frame.buf, samples_per_frame*2, 1, output);
457
if (memcmp(pkt, out_frame.buf, samples_per_frame*2)) {
459
pj_int16_t *in = (pj_int16_t*)pkt;
460
pj_int16_t *out = (pj_int16_t*)out_frame.buf;
462
for (i=0; i<samples_per_frame; ++i) {
467
PJ_LOG(1,(THIS_FILE," failed: mismatch at samples %d", pos+i));
472
pos += samples_per_frame;
486
pjmedia_codec_close(codec);
487
pjmedia_codec_mgr_dealloc_codec(mgr, codec);
491
pj_pool_release(pool);
496
#if PJMEDIA_HAS_G7221_CODEC
497
/* For ITU testing, off the 2 lsbs. */
498
static void g7221_pcm_manip(short *pcm, unsigned count)
501
for (i=0; i<count; i++)
505
#endif /* PJMEDIA_HAS_G7221_CODEC */
507
int codec_test_vectors(void)
509
pjmedia_endpt *endpt;
510
pjmedia_codec_mgr *mgr;
511
int rc, rc_final = 0;
515
const char *wav_file;
516
const char *ref_encoded_file;
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"
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"
534
unsigned encoded_frame_len;
535
void (*manip)(short *pcm, unsigned count);
536
const char *enc_file;
537
const char *ref_pcm_file;
540
#if PJMEDIA_HAS_G7221_CODEC
541
{ "G7221/16000/1", 24000, 60,
543
"../src/test/vectors/g722_1_enc_out_24000_be.pak",
544
"../src/test/vectors/g722_1_dec_out_24000.pcm"
546
{ "G7221/16000/1", 32000, 80,
548
"../src/test/vectors/g722_1_enc_out_32000_be.pak",
549
"../src/test/vectors/g722_1_dec_out_32000.pcm"
551
{ "G7221/16000/1", 24000, 60,
553
"../src/test/vectors/g722_1_dec_in_24000_fe.itu",
554
"../src/test/vectors/g722_1_dec_out_24000_fe.pcm"
556
{ "G7221/16000/1", 32000, 80,
558
"../src/test/vectors/g722_1_dec_in_32000_fe.itu",
559
"../src/test/vectors/g722_1_dec_out_32000_fe.pcm"
567
status = pjmedia_endpt_create(mem, NULL, 0, &endpt);
568
if (status != PJ_SUCCESS)
571
mgr = pjmedia_endpt_get_codec_mgr(endpt);
573
#if PJMEDIA_HAS_G7221_CODEC
574
status = pjmedia_codec_g7221_init(endpt);
575
if (status != PJ_SUCCESS) {
576
pjmedia_endpt_destroy(endpt);
580
/* Set shift value to zero for the test vectors */
581
pjmedia_codec_g7221_set_pcm_shift(0);
584
PJ_LOG(3,(THIS_FILE," encode tests:"));
585
for (i=0; i<PJ_ARRAY_SIZE(enc_vectors); ++i) {
586
if (!enc_vectors[i].codec_name)
588
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));
593
rc = codec_test_encode(mgr, enc_vectors[i].codec_name,
594
enc_vectors[i].bit_rate,
595
enc_vectors[i].wav_file,
596
enc_vectors[i].ref_encoded_file);
601
PJ_LOG(3,(THIS_FILE," decode tests:"));
602
for (i=0; i<PJ_ARRAY_SIZE(dec_vectors); ++i) {
603
if (!dec_vectors[i].codec_name)
605
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));
610
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,
614
dec_vectors[i].ref_pcm_file,
615
dec_vectors[i].manip);
620
if (pj_file_exists(TMP_OUT))
621
pj_file_delete(TMP_OUT);
623
pjmedia_endpt_destroy(endpt);