1
/* $Id: codec_vectors.c 4712 2014-01-23 08:09:29Z 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;
38
pj_size_t encoded_frame_len = 0, pos;
39
pjmedia_codec *codec = NULL;
40
const pjmedia_codec_info *ci[1];
41
pjmedia_codec_param codec_param;
42
pjmedia_port *wav_port = NULL;
43
pjmedia_frame in_frame, out_frame;
44
FILE *output = NULL, *fref = NULL;
48
pool = pj_pool_create(mem, "codec-vectors", 512, 512, NULL);
54
/* Find and open the codec */
56
status = pjmedia_codec_mgr_find_codecs_by_id(mgr, &codec_id, &count, ci, NULL);
57
if (status != PJ_SUCCESS) {
62
status = pjmedia_codec_mgr_alloc_codec(mgr, ci[0], &codec);
63
if (status != PJ_SUCCESS) {
68
status = pjmedia_codec_mgr_get_default_param(mgr, ci[0], &codec_param);
69
if (status != PJ_SUCCESS) {
74
codec_param.info.avg_bps = bitrate;
75
codec_param.setting.vad = 0;
77
status = pjmedia_codec_init(codec, pool);
78
if (status != PJ_SUCCESS) {
83
status = pjmedia_codec_open(codec, &codec_param);
84
if (status != PJ_SUCCESS) {
90
status = pjmedia_wav_player_port_create(pool, wav_file,
91
codec_param.info.frm_ptime,
92
PJMEDIA_FILE_NO_LOOP, 0,
94
if (status != PJ_SUCCESS) {
99
/* Open output file */
100
output = fopen(TMP_OUT, "wb");
106
/* Allocate buffer for PCM and encoded frames */
107
samples_per_frame = codec_param.info.clock_rate * codec_param.info.frm_ptime / 1000;
108
in_frame.buf = pj_pool_alloc(pool, samples_per_frame * 2);
109
out_frame.buf = (pj_uint8_t*) pj_pool_alloc(pool, samples_per_frame);
111
/* Loop read WAV file and encode and write to output file */
113
in_frame.size = samples_per_frame * 2;
114
in_frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
116
status = pjmedia_port_get_frame(wav_port, &in_frame);
117
if (status != PJ_SUCCESS || in_frame.type != PJMEDIA_FRAME_TYPE_AUDIO)
120
out_frame.size = samples_per_frame;
121
status = pjmedia_codec_encode(codec, &in_frame, samples_per_frame,
123
if (status != PJ_SUCCESS) {
128
if (out_frame.size) {
129
fwrite(out_frame.buf, out_frame.size, 1, output);
131
if (encoded_frame_len == 0)
132
encoded_frame_len = out_frame.size;
139
/* Compare encoded files */
140
fref = fopen(ref_encoded_file, "rb");
146
output = fopen(TMP_OUT, "rb");
156
count = fread(in_frame.buf, encoded_frame_len, 1, fref);
160
count = fread(out_frame.buf, encoded_frame_len, 1, output);
164
if (memcmp(in_frame.buf, out_frame.buf, encoded_frame_len)) {
166
pj_uint8_t *in = (pj_uint8_t*)in_frame.buf;
167
pj_uint8_t *out = (pj_uint8_t*)out_frame.buf;
169
for (i=0; i<encoded_frame_len; ++i) {
174
PJ_LOG(1,(THIS_FILE," failed: mismatch at pos %d", pos+i));
179
pos += encoded_frame_len;
190
pjmedia_codec_close(codec);
191
pjmedia_codec_mgr_dealloc_codec(mgr, codec);
195
pjmedia_port_destroy(wav_port);
198
pj_pool_release(pool);
205
* Read file in ITU format (".itu" extension).
207
* Set swap_endian to TRUE if the ITU file is stored in little
208
* endian format (normally true).
210
static int read_ITU_format(FILE *fp_bitstream,
212
short *p_frame_error_flag,
213
int number_of_16bit_words_per_frame,
214
pj_bool_t swap_endian)
216
enum { MAX_BITS_PER_FRAME = 160*8 };
222
short in_array[MAX_BITS_PER_FRAME+2];
225
short frame_start = 0x6b21;
227
nsamp = (short)fread(in_array, 2, 2 + 16*number_of_16bit_words_per_frame,
232
if (bit != frame_start) {
233
*p_frame_error_flag = 1;
235
*p_frame_error_flag = 0;
237
/* increment j to skip over the number of bits in frame */
240
for (i=0; i<number_of_16bit_words_per_frame; i++) {
243
while (bit_count >= 0) {
250
*p_frame_error_flag = 1;
253
packed_word = (short )(packed_word + bit);
258
out_words[i] = pj_ntohs(packed_word);
260
out_words[i] = packed_word;
270
* Decode the specified encoded file in "in_encoded_file" into temporary
271
* PCM output file, and compare the temporary PCM output file with
272
* the PCM reference file.
274
* Some reference file requires manipulation to the PCM output
275
* before comparison, such manipulation can be done by supplying
276
* this function with the "manip" function.
278
static int codec_test_decode(pjmedia_codec_mgr *mgr,
281
unsigned encoded_len,
282
const char *in_encoded_file,
283
const char *ref_pcm_file,
284
void (*manip)(short *pcm, unsigned count))
286
pj_str_t codec_id = pj_str(codec_name);
287
pj_pool_t *pool = NULL;
288
unsigned count, samples_per_frame, pos;
289
pjmedia_codec *codec = NULL;
290
const pjmedia_codec_info *ci[1];
291
pjmedia_codec_param codec_param;
292
pjmedia_frame out_frame;
294
FILE *input = NULL, *output = NULL, *fref = NULL;
295
pj_bool_t is_itu_format = PJ_FALSE;
299
pool = pj_pool_create(mem, "codec-vectors", 512, 512, NULL);
305
/* Find and open the codec */
307
status = pjmedia_codec_mgr_find_codecs_by_id(mgr, &codec_id, &count, ci, NULL);
308
if (status != PJ_SUCCESS) {
313
status = pjmedia_codec_mgr_alloc_codec(mgr, ci[0], &codec);
314
if (status != PJ_SUCCESS) {
319
status = pjmedia_codec_mgr_get_default_param(mgr, ci[0], &codec_param);
320
if (status != PJ_SUCCESS) {
325
codec_param.info.avg_bps = bitrate;
326
codec_param.setting.vad = 0;
328
status = pjmedia_codec_init(codec, pool);
329
if (status != PJ_SUCCESS) {
334
status = pjmedia_codec_open(codec, &codec_param);
335
if (status != PJ_SUCCESS) {
340
/* Open input file */
341
input = fopen(in_encoded_file, "rb");
347
/* Is the file in ITU format? */
348
is_itu_format = pj_ansi_stricmp(in_encoded_file+strlen(in_encoded_file)-4,
351
/* Open output file */
352
output = fopen(TMP_OUT, "wb");
358
/* Allocate buffer for PCM and encoded frames */
359
samples_per_frame = codec_param.info.clock_rate * codec_param.info.frm_ptime / 1000;
360
pkt = pj_pool_alloc(pool, samples_per_frame * 2);
361
out_frame.buf = (pj_uint8_t*) pj_pool_alloc(pool, samples_per_frame * 2);
363
/* Loop read WAV file and encode and write to output file */
365
pjmedia_frame in_frame[2];
374
nsamp = read_ITU_format(input, (short*)pkt, &frame_err,
375
encoded_len / 2, PJ_TRUE);
376
if (nsamp != (int)encoded_len / 2)
379
has_frame = !frame_err;
381
if (fread(pkt, encoded_len, 1, input) != 1)
389
if (pjmedia_codec_parse(codec, pkt, encoded_len, &ts,
390
&count, in_frame) != PJ_SUCCESS)
401
if (pjmedia_codec_decode(codec, &in_frame[0], samples_per_frame*2,
402
&out_frame) != PJ_SUCCESS)
408
if (pjmedia_codec_recover(codec, samples_per_frame*2,
409
&out_frame) != PJ_SUCCESS)
417
manip((short*)out_frame.buf, samples_per_frame);
419
if (fwrite(out_frame.buf, out_frame.size, 1, output) != 1) {
431
/* Compare encoded files */
432
fref = fopen(ref_pcm_file, "rb");
438
output = fopen(TMP_OUT, "rb");
448
count = fread(pkt, samples_per_frame*2, 1, fref);
452
count = fread(out_frame.buf, samples_per_frame*2, 1, output);
456
if (memcmp(pkt, out_frame.buf, samples_per_frame*2)) {
458
pj_int16_t *in = (pj_int16_t*)pkt;
459
pj_int16_t *out = (pj_int16_t*)out_frame.buf;
461
for (i=0; i<samples_per_frame; ++i) {
466
PJ_LOG(1,(THIS_FILE," failed: mismatch at samples %d", pos+i));
471
pos += samples_per_frame;
485
pjmedia_codec_close(codec);
486
pjmedia_codec_mgr_dealloc_codec(mgr, codec);
490
pj_pool_release(pool);
495
#if PJMEDIA_HAS_G7221_CODEC
496
/* For ITU testing, off the 2 lsbs. */
497
static void g7221_pcm_manip(short *pcm, unsigned count)
500
for (i=0; i<count; i++)
504
#endif /* PJMEDIA_HAS_G7221_CODEC */
506
int codec_test_vectors(void)
508
pjmedia_endpt *endpt;
509
pjmedia_codec_mgr *mgr;
510
int rc, rc_final = 0;
514
const char *wav_file;
515
const char *ref_encoded_file;
518
#if PJMEDIA_HAS_G7221_CODEC
519
{ "G7221/16000/1", 24000,
520
"../src/test/vectors/g722_1_enc_in.wav",
521
"../src/test/vectors/g722_1_enc_out_24000_be.pak"
523
{ "G7221/16000/1", 32000,
524
"../src/test/vectors/g722_1_enc_in.wav",
525
"../src/test/vectors/g722_1_enc_out_32000_be.pak"
533
unsigned encoded_frame_len;
534
void (*manip)(short *pcm, unsigned count);
535
const char *enc_file;
536
const char *ref_pcm_file;
539
#if PJMEDIA_HAS_G7221_CODEC
540
{ "G7221/16000/1", 24000, 60,
542
"../src/test/vectors/g722_1_enc_out_24000_be.pak",
543
"../src/test/vectors/g722_1_dec_out_24000.pcm"
545
{ "G7221/16000/1", 32000, 80,
547
"../src/test/vectors/g722_1_enc_out_32000_be.pak",
548
"../src/test/vectors/g722_1_dec_out_32000.pcm"
550
{ "G7221/16000/1", 24000, 60,
552
"../src/test/vectors/g722_1_dec_in_24000_fe.itu",
553
"../src/test/vectors/g722_1_dec_out_24000_fe.pcm"
555
{ "G7221/16000/1", 32000, 80,
557
"../src/test/vectors/g722_1_dec_in_32000_fe.itu",
558
"../src/test/vectors/g722_1_dec_out_32000_fe.pcm"
566
status = pjmedia_endpt_create(mem, NULL, 0, &endpt);
567
if (status != PJ_SUCCESS)
570
mgr = pjmedia_endpt_get_codec_mgr(endpt);
572
#if PJMEDIA_HAS_G7221_CODEC
573
status = pjmedia_codec_g7221_init(endpt);
574
if (status != PJ_SUCCESS) {
575
pjmedia_endpt_destroy(endpt);
579
/* Set shift value to zero for the test vectors */
580
pjmedia_codec_g7221_set_pcm_shift(0);
583
PJ_LOG(3,(THIS_FILE," encode tests:"));
584
for (i=0; i<PJ_ARRAY_SIZE(enc_vectors); ++i) {
585
if (!enc_vectors[i].codec_name)
587
PJ_LOG(3,(THIS_FILE," %s @%d bps %s ==> %s",
588
enc_vectors[i].codec_name,
589
enc_vectors[i].bit_rate,
590
enc_vectors[i].wav_file,
591
enc_vectors[i].ref_encoded_file));
592
rc = codec_test_encode(mgr, enc_vectors[i].codec_name,
593
enc_vectors[i].bit_rate,
594
enc_vectors[i].wav_file,
595
enc_vectors[i].ref_encoded_file);
600
PJ_LOG(3,(THIS_FILE," decode tests:"));
601
for (i=0; i<PJ_ARRAY_SIZE(dec_vectors); ++i) {
602
if (!dec_vectors[i].codec_name)
604
PJ_LOG(3,(THIS_FILE," %s @%d bps %s ==> %s",
605
dec_vectors[i].codec_name,
606
dec_vectors[i].bit_rate,
607
dec_vectors[i].enc_file,
608
dec_vectors[i].ref_pcm_file));
609
rc = codec_test_decode(mgr, dec_vectors[i].codec_name,
610
dec_vectors[i].bit_rate,
611
dec_vectors[i].encoded_frame_len,
612
dec_vectors[i].enc_file,
613
dec_vectors[i].ref_pcm_file,
614
dec_vectors[i].manip);
619
if (pj_file_exists(TMP_OUT))
620
pj_file_delete(TMP_OUT);
622
pjmedia_endpt_destroy(endpt);