| 1 |
/* Copyright (C) 2002-2006 Jean-Marc Valin |
| 2 |
File: speexenc.c |
| 3 |
|
| 4 |
Redistribution and use in source and binary forms, with or without |
| 5 |
modification, are permitted provided that the following conditions |
| 6 |
are met: |
| 7 |
|
| 8 |
- Redistributions of source code must retain the above copyright |
| 9 |
notice, this list of conditions and the following disclaimer. |
| 10 |
|
| 11 |
- Redistributions in binary form must reproduce the above copyright |
| 12 |
notice, this list of conditions and the following disclaimer in the |
| 13 |
documentation and/or other materials provided with the distribution. |
| 14 |
|
| 15 |
- Neither the name of the Xiph.org Foundation nor the names of its |
| 16 |
contributors may be used to endorse or promote products derived from |
| 17 |
this software without specific prior written permission. |
| 18 |
|
| 19 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
*/ |
| 31 |
|
| 32 |
#ifdef HAVE_CONFIG_H |
| 33 |
# include "config.h" |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <stdio.h> |
| 37 |
#if !defined WIN32 && !defined _WIN32 |
| 38 |
#include <unistd.h> |
| 39 |
#endif |
| 40 |
#ifdef HAVE_GETOPT_H |
| 41 |
#include <getopt.h> |
| 42 |
#endif |
| 43 |
#ifndef HAVE_GETOPT_LONG |
| 44 |
#include "getopt_win.h" |
| 45 |
#endif |
| 46 |
#include <stdlib.h> |
| 47 |
#include <string.h> |
| 48 |
#include <time.h> |
| 49 |
|
| 50 |
#include "speex/speex.h" |
| 51 |
#include "speex/speex_header.h" |
| 52 |
#include "speex/speex_stereo.h" |
| 53 |
#include <ogg/ogg.h> |
| 54 |
#include "wav_io.h" |
| 55 |
#ifdef USE_SPEEXDSP |
| 56 |
#include <speex/speex_preprocess.h> |
| 57 |
#endif |
| 58 |
|
| 59 |
#if defined WIN32 || defined _WIN32 |
| 60 |
/* We need the following two to set stdout to binary */ |
| 61 |
#include <io.h> |
| 62 |
#include <fcntl.h> |
| 63 |
#endif |
| 64 |
|
| 65 |
#include "skeleton.h" |
| 66 |
|
| 67 |
|
| 68 |
void comment_init(char **comments, int* length, char *vendor_string); |
| 69 |
void comment_add(char **comments, int* length, char *tag, char *val); |
| 70 |
|
| 71 |
|
| 72 |
/*Write an Ogg page to a file pointer*/ |
| 73 |
int oe_write_page(ogg_page *page, FILE *fp) |
| 74 |
{ |
| 75 |
int written; |
| 76 |
written = fwrite(page->header,1,page->header_len, fp); |
| 77 |
written += fwrite(page->body,1,page->body_len, fp); |
| 78 |
|
| 79 |
return written; |
| 80 |
} |
| 81 |
|
| 82 |
#define MAX_FRAME_SIZE 2000 |
| 83 |
#define MAX_FRAME_BYTES 2000 |
| 84 |
|
| 85 |
/* Convert input audio bits, endians and channels */ |
| 86 |
static int read_samples(FILE *fin,int frame_size, int bits, int channels, int lsb, short * input, char *buff, spx_int32_t *size) |
| 87 |
{ |
| 88 |
unsigned char in[MAX_FRAME_BYTES*2]; |
| 89 |
int i; |
| 90 |
short *s; |
| 91 |
int nb_read; |
| 92 |
size_t to_read; |
| 93 |
|
| 94 |
if (size && *size<=0) |
| 95 |
{ |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
to_read = bits/8*channels*frame_size; |
| 100 |
|
| 101 |
/*Read input audio*/ |
| 102 |
if (size) |
| 103 |
{ |
| 104 |
if (*size >= to_read) |
| 105 |
{ |
| 106 |
*size -= to_read; |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
to_read = *size; |
| 111 |
*size = 0; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if (buff) |
| 116 |
{ |
| 117 |
for (i=0;i<12;i++) |
| 118 |
in[i]=buff[i]; |
| 119 |
nb_read = fread(in+12,1,to_read-12,fin) + 12; |
| 120 |
if (size) |
| 121 |
*size += 12; |
| 122 |
} else { |
| 123 |
nb_read = fread(in,1,to_read,fin); |
| 124 |
} |
| 125 |
|
| 126 |
nb_read /= bits/8*channels; |
| 127 |
|
| 128 |
/*fprintf (stderr, "%d\n", nb_read);*/ |
| 129 |
if (nb_read==0) |
| 130 |
return 0; |
| 131 |
|
| 132 |
s=(short*)in; |
| 133 |
if(bits==8) |
| 134 |
{ |
| 135 |
/* Convert 8->16 bits */ |
| 136 |
for(i=frame_size*channels-1;i>=0;i--) |
| 137 |
{ |
| 138 |
s[i]=(in[i]<<8)^0x8000; |
| 139 |
} |
| 140 |
} else |
| 141 |
{ |
| 142 |
/* convert to our endian format */ |
| 143 |
for(i=0;i<frame_size*channels;i++) |
| 144 |
{ |
| 145 |
if(lsb) |
| 146 |
s[i]=le_short(s[i]); |
| 147 |
else |
| 148 |
s[i]=be_short(s[i]); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/* FIXME: This is probably redundent now */ |
| 153 |
/* copy to float input buffer */ |
| 154 |
for (i=0;i<frame_size*channels;i++) |
| 155 |
{ |
| 156 |
input[i]=(short)s[i]; |
| 157 |
} |
| 158 |
|
| 159 |
for (i=nb_read*channels;i<frame_size*channels;i++) |
| 160 |
{ |
| 161 |
input[i]=0; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
return nb_read; |
| 166 |
} |
| 167 |
|
| 168 |
void add_fishead_packet (ogg_stream_state *os) { |
| 169 |
|
| 170 |
fishead_packet fp; |
| 171 |
|
| 172 |
memset(&fp, 0, sizeof(fp)); |
| 173 |
fp.ptime_n = 0; |
| 174 |
fp.ptime_d = 1000; |
| 175 |
fp.btime_n = 0; |
| 176 |
fp.btime_d = 1000; |
| 177 |
|
| 178 |
add_fishead_to_stream(os, &fp); |
| 179 |
} |
| 180 |
|
| 181 |
/* |
| 182 |
* Adds the fishead packets in the skeleton output stream along with the e_o_s packet |
| 183 |
*/ |
| 184 |
void add_fisbone_packet (ogg_stream_state *os, spx_int32_t serialno, SpeexHeader *header) { |
| 185 |
|
| 186 |
fisbone_packet fp; |
| 187 |
|
| 188 |
memset(&fp, 0, sizeof(fp)); |
| 189 |
fp.serial_no = serialno; |
| 190 |
fp.nr_header_packet = 2 + header->extra_headers; |
| 191 |
fp.granule_rate_n = header->rate; |
| 192 |
fp.granule_rate_d = 1; |
| 193 |
fp.start_granule = 0; |
| 194 |
fp.preroll = 3; |
| 195 |
fp.granule_shift = 0; |
| 196 |
|
| 197 |
add_message_header_field(&fp, "Content-Type", "audio/x-speex"); |
| 198 |
|
| 199 |
add_fisbone_to_stream(os, &fp); |
| 200 |
} |
| 201 |
|
| 202 |
void version() |
| 203 |
{ |
| 204 |
const char* speex_version; |
| 205 |
speex_lib_ctl(SPEEX_LIB_GET_VERSION_STRING, (void*)&speex_version); |
| 206 |
printf ("speexenc (Speex encoder) version %s (compiled " __DATE__ ")\n", speex_version); |
| 207 |
printf ("Copyright (C) 2002-2006 Jean-Marc Valin\n"); |
| 208 |
} |
| 209 |
|
| 210 |
void version_short() |
| 211 |
{ |
| 212 |
const char* speex_version; |
| 213 |
speex_lib_ctl(SPEEX_LIB_GET_VERSION_STRING, (void*)&speex_version); |
| 214 |
printf ("speexenc version %s\n", speex_version); |
| 215 |
printf ("Copyright (C) 2002-2006 Jean-Marc Valin\n"); |
| 216 |
} |
| 217 |
|
| 218 |
void usage() |
| 219 |
{ |
| 220 |
printf ("Usage: speexenc [options] input_file output_file\n"); |
| 221 |
printf ("\n"); |
| 222 |
printf ("Encodes input_file using Speex. It can read the WAV or raw files.\n"); |
| 223 |
printf ("\n"); |
| 224 |
printf ("input_file can be:\n"); |
| 225 |
printf (" filename.wav wav file\n"); |
| 226 |
printf (" filename.* Raw PCM file (any extension other than .wav)\n"); |
| 227 |
printf (" - stdin\n"); |
| 228 |
printf ("\n"); |
| 229 |
printf ("output_file can be:\n"); |
| 230 |
printf (" filename.spx Speex file\n"); |
| 231 |
printf (" - stdout\n"); |
| 232 |
printf ("\n"); |
| 233 |
printf ("Options:\n"); |
| 234 |
printf (" -n, --narrowband Narrowband (8 kHz) input file\n"); |
| 235 |
printf (" -w, --wideband Wideband (16 kHz) input file\n"); |
| 236 |
printf (" -u, --ultra-wideband \"Ultra-wideband\" (32 kHz) input file\n"); |
| 237 |
printf (" --quality n Encoding quality (0-10), default 8\n"); |
| 238 |
printf (" --bitrate n Encoding bit-rate (use bit-rate n or lower)\n"); |
| 239 |
printf (" --vbr Enable variable bit-rate (VBR)\n"); |
| 240 |
printf (" --vbr-max-bitrate Set max VBR bit-rate allowed\n"); |
| 241 |
printf (" --abr rate Enable average bit-rate (ABR) at rate bps\n"); |
| 242 |
printf (" --vad Enable voice activity detection (VAD)\n"); |
| 243 |
printf (" --dtx Enable file-based discontinuous transmission (DTX)\n"); |
| 244 |
printf (" --comp n Set encoding complexity (0-10), default 3\n"); |
| 245 |
printf (" --nframes n Number of frames per Ogg packet (1-10), default 1\n"); |
| 246 |
#ifdef USE_SPEEXDSP |
| 247 |
printf (" --denoise Denoise the input before encoding\n"); |
| 248 |
printf (" --agc Apply adaptive gain control (AGC) before encoding\n"); |
| 249 |
#endif |
| 250 |
printf (" --no-highpass Disable the encoder's built-in high-pass filter\n"); |
| 251 |
printf (" --skeleton Outputs ogg skeleton metadata (may cause incompatibilities)\n"); |
| 252 |
printf (" --comment Add the given string as an extra comment. This may be\n"); |
| 253 |
printf (" used multiple times\n"); |
| 254 |
printf (" --author Author of this track\n"); |
| 255 |
printf (" --title Title for this track\n"); |
| 256 |
printf (" -h, --help This help\n"); |
| 257 |
printf (" -v, --version Version information\n"); |
| 258 |
printf (" -V Verbose mode (show bit-rate)\n"); |
| 259 |
printf (" --print-rate Print the bitrate for each frame to standard output\n"); |
| 260 |
printf ("Raw input options:\n"); |
| 261 |
printf (" --rate n Sampling rate for raw input\n"); |
| 262 |
printf (" --stereo Consider raw input as stereo\n"); |
| 263 |
printf (" --le Raw input is little-endian\n"); |
| 264 |
printf (" --be Raw input is big-endian\n"); |
| 265 |
printf (" --8bit Raw input is 8-bit unsigned\n"); |
| 266 |
printf (" --16bit Raw input is 16-bit signed\n"); |
| 267 |
printf ("Default raw PCM input is 16-bit, little-endian, mono\n"); |
| 268 |
printf ("\n"); |
| 269 |
printf ("More information is available from the Speex site: http://www.speex.org\n"); |
| 270 |
printf ("\n"); |
| 271 |
printf ("Please report bugs to the mailing list `[email protected]'.\n"); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
int main(int argc, char **argv) |
| 276 |
{ |
| 277 |
int nb_samples, total_samples=0, nb_encoded; |
| 278 |
int c; |
| 279 |
int option_index = 0; |
| 280 |
char *inFile, *outFile; |
| 281 |
FILE *fin, *fout; |
| 282 |
short input[MAX_FRAME_SIZE]; |
| 283 |
spx_int32_t frame_size; |
| 284 |
int quiet=0; |
| 285 |
spx_int32_t vbr_enabled=0; |
| 286 |
spx_int32_t vbr_max=0; |
| 287 |
int abr_enabled=0; |
| 288 |
spx_int32_t vad_enabled=0; |
| 289 |
spx_int32_t dtx_enabled=0; |
| 290 |
int nbBytes; |
| 291 |
const SpeexMode *mode=NULL; |
| 292 |
int modeID = -1; |
| 293 |
void *st; |
| 294 |
SpeexBits bits; |
| 295 |
char cbits[MAX_FRAME_BYTES]; |
| 296 |
int with_skeleton = 0; |
| 297 |
struct option long_options[] = |
| 298 |
{ |
| 299 |
{"wideband", no_argument, NULL, 0}, |
| 300 |
{"ultra-wideband", no_argument, NULL, 0}, |
| 301 |
{"narrowband", no_argument, NULL, 0}, |
| 302 |
{"vbr", no_argument, NULL, 0}, |
| 303 |
{"vbr-max-bitrate", required_argument, NULL, 0}, |
| 304 |
{"abr", required_argument, NULL, 0}, |
| 305 |
{"vad", no_argument, NULL, 0}, |
| 306 |
{"dtx", no_argument, NULL, 0}, |
| 307 |
{"quality", required_argument, NULL, 0}, |
| 308 |
{"bitrate", required_argument, NULL, 0}, |
| 309 |
{"nframes", required_argument, NULL, 0}, |
| 310 |
{"comp", required_argument, NULL, 0}, |
| 311 |
#ifdef USE_SPEEXDSP |
| 312 |
{"denoise", no_argument, NULL, 0}, |
| 313 |
{"agc", no_argument, NULL, 0}, |
| 314 |
#endif |
| 315 |
{"no-highpass", no_argument, NULL, 0}, |
| 316 |
{"skeleton",no_argument,NULL, 0}, |
| 317 |
{"help", no_argument, NULL, 0}, |
| 318 |
{"quiet", no_argument, NULL, 0}, |
| 319 |
{"le", no_argument, NULL, 0}, |
| 320 |
{"be", no_argument, NULL, 0}, |
| 321 |
{"8bit", no_argument, NULL, 0}, |
| 322 |
{"16bit", no_argument, NULL, 0}, |
| 323 |
{"stereo", no_argument, NULL, 0}, |
| 324 |
{"rate", required_argument, NULL, 0}, |
| 325 |
{"version", no_argument, NULL, 0}, |
| 326 |
{"version-short", no_argument, NULL, 0}, |
| 327 |
{"comment", required_argument, NULL, 0}, |
| 328 |
{"author", required_argument, NULL, 0}, |
| 329 |
{"title", required_argument, NULL, 0}, |
| 330 |
{"print-rate", no_argument, NULL, 0}, |
| 331 |
{0, 0, 0, 0} |
| 332 |
}; |
| 333 |
int print_bitrate=0; |
| 334 |
spx_int32_t rate=0; |
| 335 |
spx_int32_t size; |
| 336 |
int chan=1; |
| 337 |
int fmt=16; |
| 338 |
spx_int32_t quality=-1; |
| 339 |
float vbr_quality=-1; |
| 340 |
int lsb=1; |
| 341 |
ogg_stream_state os; |
| 342 |
ogg_stream_state so; /* ogg stream for skeleton bitstream */ |
| 343 |
ogg_page og; |
| 344 |
ogg_packet op; |
| 345 |
int bytes_written=0, ret, result; |
| 346 |
int id=-1; |
| 347 |
SpeexHeader header; |
| 348 |
int nframes=1; |
| 349 |
spx_int32_t complexity=3; |
| 350 |
const char* speex_version; |
| 351 |
char vendor_string[64]; |
| 352 |
char *comments; |
| 353 |
int comments_length; |
| 354 |
int close_in=0, close_out=0; |
| 355 |
int eos=0; |
| 356 |
spx_int32_t bitrate=0; |
| 357 |
double cumul_bits=0, enc_frames=0; |
| 358 |
char first_bytes[12]; |
| 359 |
int wave_input=0; |
| 360 |
spx_int32_t tmp; |
| 361 |
#ifdef USE_SPEEXDSP |
| 362 |
SpeexPreprocessState *preprocess = NULL; |
| 363 |
int denoise_enabled=0, agc_enabled=0; |
| 364 |
#endif |
| 365 |
int highpass_enabled=1; |
| 366 |
int output_rate=0; |
| 367 |
spx_int32_t lookahead = 0; |
| 368 |
|
| 369 |
speex_lib_ctl(SPEEX_LIB_GET_VERSION_STRING, (void*)&speex_version); |
| 370 |
snprintf(vendor_string, sizeof(vendor_string), "Encoded with Speex %s", speex_version); |
| 371 |
|
| 372 |
comment_init(&comments, &comments_length, vendor_string); |
| 373 |
|
| 374 |
/*Process command-line options*/ |
| 375 |
while(1) |
| 376 |
{ |
| 377 |
c = getopt_long (argc, argv, "nwuhvV", |
| 378 |
long_options, &option_index); |
| 379 |
if (c==-1) |
| 380 |
break; |
| 381 |
|
| 382 |
switch(c) |
| 383 |
{ |
| 384 |
case 0: |
| 385 |
if (strcmp(long_options[option_index].name,"narrowband")==0) |
| 386 |
{ |
| 387 |
modeID = SPEEX_MODEID_NB; |
| 388 |
} else if (strcmp(long_options[option_index].name,"wideband")==0) |
| 389 |
{ |
| 390 |
modeID = SPEEX_MODEID_WB; |
| 391 |
} else if (strcmp(long_options[option_index].name,"ultra-wideband")==0) |
| 392 |
{ |
| 393 |
modeID = SPEEX_MODEID_UWB; |
| 394 |
} else if (strcmp(long_options[option_index].name,"vbr")==0) |
| 395 |
{ |
| 396 |
vbr_enabled=1; |
| 397 |
} else if (strcmp(long_options[option_index].name,"vbr-max-bitrate")==0) |
| 398 |
{ |
| 399 |
vbr_max=atoi(optarg); |
| 400 |
if (vbr_max<1) |
| 401 |
{ |
| 402 |
fprintf (stderr, "Invalid VBR max bit-rate value: %d\n", vbr_max); |
| 403 |
exit(1); |
| 404 |
} |
| 405 |
} else if (strcmp(long_options[option_index].name,"abr")==0) |
| 406 |
{ |
| 407 |
abr_enabled=atoi(optarg); |
| 408 |
if (!abr_enabled) |
| 409 |
{ |
| 410 |
fprintf (stderr, "Invalid ABR value: %d\n", abr_enabled); |
| 411 |
exit(1); |
| 412 |
} |
| 413 |
} else if (strcmp(long_options[option_index].name,"vad")==0) |
| 414 |
{ |
| 415 |
vad_enabled=1; |
| 416 |
} else if (strcmp(long_options[option_index].name,"dtx")==0) |
| 417 |
{ |
| 418 |
dtx_enabled=1; |
| 419 |
} else if (strcmp(long_options[option_index].name,"quality")==0) |
| 420 |
{ |
| 421 |
quality = atoi (optarg); |
| 422 |
vbr_quality=atof(optarg); |
| 423 |
} else if (strcmp(long_options[option_index].name,"bitrate")==0) |
| 424 |
{ |
| 425 |
bitrate = atoi (optarg); |
| 426 |
} else if (strcmp(long_options[option_index].name,"nframes")==0) |
| 427 |
{ |
| 428 |
nframes = atoi (optarg); |
| 429 |
if (nframes<1) |
| 430 |
nframes=1; |
| 431 |
if (nframes>10) |
| 432 |
nframes=10; |
| 433 |
} else if (strcmp(long_options[option_index].name,"comp")==0) |
| 434 |
{ |
| 435 |
complexity = atoi (optarg); |
| 436 |
#ifdef USE_SPEEXDSP |
| 437 |
} else if (strcmp(long_options[option_index].name,"denoise")==0) |
| 438 |
{ |
| 439 |
denoise_enabled=1; |
| 440 |
} else if (strcmp(long_options[option_index].name,"agc")==0) |
| 441 |
{ |
| 442 |
agc_enabled=1; |
| 443 |
#endif |
| 444 |
} else if (strcmp(long_options[option_index].name,"no-highpass")==0) |
| 445 |
{ |
| 446 |
highpass_enabled=0; |
| 447 |
} else if (strcmp(long_options[option_index].name,"skeleton")==0) |
| 448 |
{ |
| 449 |
with_skeleton=1; |
| 450 |
} else if (strcmp(long_options[option_index].name,"help")==0) |
| 451 |
{ |
| 452 |
usage(); |
| 453 |
exit(0); |
| 454 |
} else if (strcmp(long_options[option_index].name,"quiet")==0) |
| 455 |
{ |
| 456 |
quiet = 1; |
| 457 |
} else if (strcmp(long_options[option_index].name,"version")==0) |
| 458 |
{ |
| 459 |
version(); |
| 460 |
exit(0); |
| 461 |
} else if (strcmp(long_options[option_index].name,"version-short")==0) |
| 462 |
{ |
| 463 |
version_short(); |
| 464 |
exit(0); |
| 465 |
} else if (strcmp(long_options[option_index].name,"print-rate")==0) |
| 466 |
{ |
| 467 |
output_rate=1; |
| 468 |
} else if (strcmp(long_options[option_index].name,"le")==0) |
| 469 |
{ |
| 470 |
lsb=1; |
| 471 |
} else if (strcmp(long_options[option_index].name,"be")==0) |
| 472 |
{ |
| 473 |
lsb=0; |
| 474 |
} else if (strcmp(long_options[option_index].name,"8bit")==0) |
| 475 |
{ |
| 476 |
fmt=8; |
| 477 |
} else if (strcmp(long_options[option_index].name,"16bit")==0) |
| 478 |
{ |
| 479 |
fmt=16; |
| 480 |
} else if (strcmp(long_options[option_index].name,"stereo")==0) |
| 481 |
{ |
| 482 |
chan=2; |
| 483 |
} else if (strcmp(long_options[option_index].name,"rate")==0) |
| 484 |
{ |
| 485 |
rate=atoi (optarg); |
| 486 |
} else if (strcmp(long_options[option_index].name,"comment")==0) |
| 487 |
{ |
| 488 |
if (!strchr(optarg, '=')) |
| 489 |
{ |
| 490 |
fprintf (stderr, "Invalid comment: %s\n", optarg); |
| 491 |
fprintf (stderr, "Comments must be of the form name=value\n"); |
| 492 |
exit(1); |
| 493 |
} |
| 494 |
comment_add(&comments, &comments_length, NULL, optarg); |
| 495 |
} else if (strcmp(long_options[option_index].name,"author")==0) |
| 496 |
{ |
| 497 |
comment_add(&comments, &comments_length, "author=", optarg); |
| 498 |
} else if (strcmp(long_options[option_index].name,"title")==0) |
| 499 |
{ |
| 500 |
comment_add(&comments, &comments_length, "title=", optarg); |
| 501 |
} |
| 502 |
|
| 503 |
break; |
| 504 |
case 'n': |
| 505 |
modeID = SPEEX_MODEID_NB; |
| 506 |
break; |
| 507 |
case 'h': |
| 508 |
usage(); |
| 509 |
exit(0); |
| 510 |
break; |
| 511 |
case 'v': |
| 512 |
version(); |
| 513 |
exit(0); |
| 514 |
break; |
| 515 |
case 'V': |
| 516 |
print_bitrate=1; |
| 517 |
break; |
| 518 |
case 'w': |
| 519 |
modeID = SPEEX_MODEID_WB; |
| 520 |
break; |
| 521 |
case 'u': |
| 522 |
modeID = SPEEX_MODEID_UWB; |
| 523 |
break; |
| 524 |
case '?': |
| 525 |
usage(); |
| 526 |
exit(1); |
| 527 |
break; |
| 528 |
} |
| 529 |
} |
| 530 |
if (argc-optind!=2) |
| 531 |
{ |
| 532 |
usage(); |
| 533 |
exit(1); |
| 534 |
} |
| 535 |
inFile=argv[optind]; |
| 536 |
outFile=argv[optind+1]; |
| 537 |
|
| 538 |
/*Initialize Ogg stream struct*/ |
| 539 |
srand(time(NULL)); |
| 540 |
if (ogg_stream_init(&os, rand())==-1) |
| 541 |
{ |
| 542 |
fprintf(stderr,"Error: stream init failed\n"); |
| 543 |
exit(1); |
| 544 |
} |
| 545 |
if (with_skeleton && ogg_stream_init(&so, rand())==-1) |
| 546 |
{ |
| 547 |
fprintf(stderr,"Error: stream init failed\n"); |
| 548 |
exit(1); |
| 549 |
} |
| 550 |
|
| 551 |
if (strcmp(inFile, "-")==0) |
| 552 |
{ |
| 553 |
#if defined WIN32 || defined _WIN32 |
| 554 |
_setmode(_fileno(stdin), _O_BINARY); |
| 555 |
#elif defined OS2 |
| 556 |
_fsetmode(stdin,"b"); |
| 557 |
#endif |
| 558 |
fin=stdin; |
| 559 |
} |
| 560 |
else |
| 561 |
{ |
| 562 |
fin = fopen(inFile, "rb"); |
| 563 |
if (!fin) |
| 564 |
{ |
| 565 |
perror(inFile); |
| 566 |
exit(1); |
| 567 |
} |
| 568 |
close_in=1; |
| 569 |
} |
| 570 |
|
| 571 |
{ |
| 572 |
if (fread(first_bytes, 1, 12, fin) != 12) |
| 573 |
{ |
| 574 |
perror("short file"); |
| 575 |
exit(1); |
| 576 |
} |
| 577 |
if (strncmp(first_bytes,"RIFF",4)==0 || strncmp(first_bytes,"riff",4)==0) |
| 578 |
{ |
| 579 |
if (read_wav_header(fin, &rate, &chan, &fmt, &size)==-1) |
| 580 |
exit(1); |
| 581 |
wave_input=1; |
| 582 |
lsb=1; /* CHECK: exists big-endian .wav ?? */ |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
if (modeID==-1 && !rate) |
| 587 |
{ |
| 588 |
/* By default, use narrowband/8 kHz */ |
| 589 |
modeID = SPEEX_MODEID_NB; |
| 590 |
rate=8000; |
| 591 |
} else if (modeID!=-1 && rate) |
| 592 |
{ |
| 593 |
mode = speex_lib_get_mode (modeID); |
| 594 |
if (rate>48000) |
| 595 |
{ |
| 596 |
fprintf (stderr, "Error: sampling rate too high: %d Hz, try down-sampling\n", rate); |
| 597 |
exit(1); |
| 598 |
} else if (rate>25000) |
| 599 |
{ |
| 600 |
if (modeID != SPEEX_MODEID_UWB) |
| 601 |
{ |
| 602 |
fprintf (stderr, "Warning: Trying to encode in %s at %d Hz. I'll do it but I suggest you try ultra-wideband instead\n", mode->modeName , rate); |
| 603 |
} |
| 604 |
} else if (rate>12500) |
| 605 |
{ |
| 606 |
if (modeID != SPEEX_MODEID_WB) |
| 607 |
{ |
| 608 |
fprintf (stderr, "Warning: Trying to encode in %s at %d Hz. I'll do it but I suggest you try wideband instead\n", mode->modeName , rate); |
| 609 |
} |
| 610 |
} else if (rate>=6000) |
| 611 |
{ |
| 612 |
if (modeID != SPEEX_MODEID_NB) |
| 613 |
{ |
| 614 |
fprintf (stderr, "Warning: Trying to encode in %s at %d Hz. I'll do it but I suggest you try narrowband instead\n", mode->modeName , rate); |
| 615 |
} |
| 616 |
} else { |
| 617 |
fprintf (stderr, "Error: sampling rate too low: %d Hz\n", rate); |
| 618 |
exit(1); |
| 619 |
} |
| 620 |
} else if (modeID==-1) |
| 621 |
{ |
| 622 |
if (rate>48000) |
| 623 |
{ |
| 624 |
fprintf (stderr, "Error: sampling rate too high: %d Hz, try down-sampling\n", rate); |
| 625 |
exit(1); |
| 626 |
} else if (rate>25000) |
| 627 |
{ |
| 628 |
modeID = SPEEX_MODEID_UWB; |
| 629 |
} else if (rate>12500) |
| 630 |
{ |
| 631 |
modeID = SPEEX_MODEID_WB; |
| 632 |
} else if (rate>=6000) |
| 633 |
{ |
| 634 |
modeID = SPEEX_MODEID_NB; |
| 635 |
} else { |
| 636 |
fprintf (stderr, "Error: Sampling rate too low: %d Hz\n", rate); |
| 637 |
exit(1); |
| 638 |
} |
| 639 |
} else if (!rate) |
| 640 |
{ |
| 641 |
if (modeID == SPEEX_MODEID_NB) |
| 642 |
rate=8000; |
| 643 |
else if (modeID == SPEEX_MODEID_WB) |
| 644 |
rate=16000; |
| 645 |
else if (modeID == SPEEX_MODEID_UWB) |
| 646 |
rate=32000; |
| 647 |
} |
| 648 |
|
| 649 |
if (!quiet) |
| 650 |
if (rate!=8000 && rate!=16000 && rate!=32000) |
| 651 |
fprintf (stderr, "Warning: Speex is only optimized for 8, 16 and 32 kHz. It will still work at %d Hz but your mileage may vary\n", rate); |
| 652 |
|
| 653 |
if (!mode) |
| 654 |
mode = speex_lib_get_mode (modeID); |
| 655 |
|
| 656 |
speex_init_header(&header, rate, 1, mode); |
| 657 |
header.frames_per_packet=nframes; |
| 658 |
header.vbr=vbr_enabled; |
| 659 |
header.nb_channels = chan; |
| 660 |
|
| 661 |
{ |
| 662 |
char *st_string="mono"; |
| 663 |
if (chan==2) |
| 664 |
st_string="stereo"; |
| 665 |
if (!quiet) |
| 666 |
fprintf (stderr, "Encoding %d Hz audio using %s mode (%s)\n", |
| 667 |
header.rate, mode->modeName, st_string); |
| 668 |
} |
| 669 |
/*fprintf (stderr, "Encoding %d Hz audio at %d bps using %s mode\n", |
| 670 |
header.rate, mode->bitrate, mode->modeName);*/ |
| 671 |
|
| 672 |
/*Initialize Speex encoder*/ |
| 673 |
st = speex_encoder_init(mode); |
| 674 |
|
| 675 |
if (strcmp(outFile,"-")==0) |
| 676 |
{ |
| 677 |
#if defined WIN32 || defined _WIN32 |
| 678 |
_setmode(_fileno(stdout), _O_BINARY); |
| 679 |
#endif |
| 680 |
fout=stdout; |
| 681 |
} |
| 682 |
else |
| 683 |
{ |
| 684 |
fout = fopen(outFile, "wb"); |
| 685 |
if (!fout) |
| 686 |
{ |
| 687 |
perror(outFile); |
| 688 |
exit(1); |
| 689 |
} |
| 690 |
close_out=1; |
| 691 |
} |
| 692 |
|
| 693 |
speex_encoder_ctl(st, SPEEX_GET_FRAME_SIZE, &frame_size); |
| 694 |
speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &complexity); |
| 695 |
speex_encoder_ctl(st, SPEEX_SET_SAMPLING_RATE, &rate); |
| 696 |
|
| 697 |
if (quality >= 0) |
| 698 |
{ |
| 699 |
if (vbr_enabled) |
| 700 |
{ |
| 701 |
if (vbr_max>0) |
| 702 |
speex_encoder_ctl(st, SPEEX_SET_VBR_MAX_BITRATE, &vbr_max); |
| 703 |
speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_quality); |
| 704 |
} |
| 705 |
else |
| 706 |
speex_encoder_ctl(st, SPEEX_SET_QUALITY, &quality); |
| 707 |
} |
| 708 |
if (bitrate) |
| 709 |
{ |
| 710 |
if (quality >= 0 && vbr_enabled) |
| 711 |
fprintf (stderr, "Warning: --bitrate option is overriding --quality\n"); |
| 712 |
speex_encoder_ctl(st, SPEEX_SET_BITRATE, &bitrate); |
| 713 |
} |
| 714 |
if (vbr_enabled) |
| 715 |
{ |
| 716 |
tmp=1; |
| 717 |
speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp); |
| 718 |
} else if (vad_enabled) |
| 719 |
{ |
| 720 |
tmp=1; |
| 721 |
speex_encoder_ctl(st, SPEEX_SET_VAD, &tmp); |
| 722 |
} |
| 723 |
if (dtx_enabled) |
| 724 |
speex_encoder_ctl(st, SPEEX_SET_DTX, &tmp); |
| 725 |
if (dtx_enabled && !(vbr_enabled || abr_enabled || vad_enabled)) |
| 726 |
{ |
| 727 |
fprintf (stderr, "Warning: --dtx is useless without --vad, --vbr or --abr\n"); |
| 728 |
} else if ((vbr_enabled || abr_enabled) && (vad_enabled)) |
| 729 |
{ |
| 730 |
fprintf (stderr, "Warning: --vad is already implied by --vbr or --abr\n"); |
| 731 |
} |
| 732 |
if (with_skeleton) { |
| 733 |
fprintf (stderr, "Warning: Enabling skeleton output may cause some decoders to fail.\n"); |
| 734 |
} |
| 735 |
|
| 736 |
if (abr_enabled) |
| 737 |
{ |
| 738 |
speex_encoder_ctl(st, SPEEX_SET_ABR, &abr_enabled); |
| 739 |
} |
| 740 |
|
| 741 |
speex_encoder_ctl(st, SPEEX_SET_HIGHPASS, &highpass_enabled); |
| 742 |
|
| 743 |
speex_encoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead); |
| 744 |
|
| 745 |
#ifdef USE_SPEEXDSP |
| 746 |
if (denoise_enabled || agc_enabled) |
| 747 |
{ |
| 748 |
preprocess = speex_preprocess_state_init(frame_size, rate); |
| 749 |
speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_DENOISE, &denoise_enabled); |
| 750 |
speex_preprocess_ctl(preprocess, SPEEX_PREPROCESS_SET_AGC, &agc_enabled); |
| 751 |
lookahead += frame_size; |
| 752 |
} |
| 753 |
#endif |
| 754 |
/* first packet should be the skeleton header. */ |
| 755 |
|
| 756 |
if (with_skeleton) { |
| 757 |
add_fishead_packet(&so); |
| 758 |
if ((ret = flush_ogg_stream_to_file(&so, fout))) { |
| 759 |
fprintf (stderr,"Error: failed skeleton (fishead) header to output stream\n"); |
| 760 |
exit(1); |
| 761 |
} else |
| 762 |
bytes_written += ret; |
| 763 |
} |
| 764 |
|
| 765 |
/*Write header*/ |
| 766 |
{ |
| 767 |
int packet_size; |
| 768 |
op.packet = (unsigned char *)speex_header_to_packet(&header, &packet_size); |
| 769 |
op.bytes = packet_size; |
| 770 |
op.b_o_s = 1; |
| 771 |
op.e_o_s = 0; |
| 772 |
op.granulepos = 0; |
| 773 |
op.packetno = 0; |
| 774 |
ogg_stream_packetin(&os, &op); |
| 775 |
free(op.packet); |
| 776 |
|
| 777 |
while((result = ogg_stream_flush(&os, &og))) |
| 778 |
{ |
| 779 |
if(!result) break; |
| 780 |
ret = oe_write_page(&og, fout); |
| 781 |
if(ret != og.header_len + og.body_len) |
| 782 |
{ |
| 783 |
fprintf (stderr,"Error: failed writing header to output stream\n"); |
| 784 |
exit(1); |
| 785 |
} |
| 786 |
else |
| 787 |
bytes_written += ret; |
| 788 |
} |
| 789 |
|
| 790 |
op.packet = (unsigned char *)comments; |
| 791 |
op.bytes = comments_length; |
| 792 |
op.b_o_s = 0; |
| 793 |
op.e_o_s = 0; |
| 794 |
op.granulepos = 0; |
| 795 |
op.packetno = 1; |
| 796 |
ogg_stream_packetin(&os, &op); |
| 797 |
} |
| 798 |
|
| 799 |
/* fisbone packet should be write after all bos pages */ |
| 800 |
if (with_skeleton) { |
| 801 |
add_fisbone_packet(&so, os.serialno, &header); |
| 802 |
if ((ret = flush_ogg_stream_to_file(&so, fout))) { |
| 803 |
fprintf (stderr,"Error: failed writing skeleton (fisbone )header to output stream\n"); |
| 804 |
exit(1); |
| 805 |
} else |
| 806 |
bytes_written += ret; |
| 807 |
} |
| 808 |
|
| 809 |
/* writing the rest of the speex header packets */ |
| 810 |
while((result = ogg_stream_flush(&os, &og))) |
| 811 |
{ |
| 812 |
if(!result) break; |
| 813 |
ret = oe_write_page(&og, fout); |
| 814 |
if(ret != og.header_len + og.body_len) |
| 815 |
{ |
| 816 |
fprintf (stderr,"Error: failed writing header to output stream\n"); |
| 817 |
exit(1); |
| 818 |
} |
| 819 |
else |
| 820 |
bytes_written += ret; |
| 821 |
} |
| 822 |
|
| 823 |
free(comments); |
| 824 |
|
| 825 |
/* write the skeleton eos packet */ |
| 826 |
if (with_skeleton) { |
| 827 |
add_eos_packet_to_stream(&so); |
| 828 |
if ((ret = flush_ogg_stream_to_file(&so, fout))) { |
| 829 |
fprintf (stderr,"Error: failed writing skeleton header to output stream\n"); |
| 830 |
exit(1); |
| 831 |
} else |
| 832 |
bytes_written += ret; |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
speex_bits_init(&bits); |
| 837 |
|
| 838 |
if (!wave_input) |
| 839 |
{ |
| 840 |
nb_samples = read_samples(fin,frame_size,fmt,chan,lsb,input, first_bytes, NULL); |
| 841 |
} else { |
| 842 |
nb_samples = read_samples(fin,frame_size,fmt,chan,lsb,input, NULL, &size); |
| 843 |
} |
| 844 |
if (nb_samples==0) |
| 845 |
eos=1; |
| 846 |
total_samples += nb_samples; |
| 847 |
nb_encoded = -lookahead; |
| 848 |
/*Main encoding loop (one frame per iteration)*/ |
| 849 |
while (!eos || total_samples>nb_encoded) |
| 850 |
{ |
| 851 |
id++; |
| 852 |
/*Encode current frame*/ |
| 853 |
if (chan==2) |
| 854 |
speex_encode_stereo_int(input, frame_size, &bits); |
| 855 |
|
| 856 |
#ifdef USE_SPEEXDSP |
| 857 |
if (preprocess) |
| 858 |
speex_preprocess(preprocess, input, NULL); |
| 859 |
#endif |
| 860 |
speex_encode_int(st, input, &bits); |
| 861 |
|
| 862 |
nb_encoded += frame_size; |
| 863 |
if (print_bitrate) { |
| 864 |
int tmp; |
| 865 |
char ch=13; |
| 866 |
speex_encoder_ctl(st, SPEEX_GET_BITRATE, &tmp); |
| 867 |
fputc (ch, stderr); |
| 868 |
cumul_bits += tmp; |
| 869 |
enc_frames += 1; |
| 870 |
if (!quiet) |
| 871 |
{ |
| 872 |
if (vad_enabled || vbr_enabled || abr_enabled) |
| 873 |
fprintf (stderr, "Bitrate is use: %d bps (average %d bps) ", tmp, (int)(cumul_bits/enc_frames)); |
| 874 |
else |
| 875 |
fprintf (stderr, "Bitrate is use: %d bps ", tmp); |
| 876 |
if (output_rate) |
| 877 |
printf ("%d\n", tmp); |
| 878 |
} |
| 879 |
|
| 880 |
} |
| 881 |
|
| 882 |
if (wave_input) |
| 883 |
{ |
| 884 |
nb_samples = read_samples(fin,frame_size,fmt,chan,lsb,input, NULL, &size); |
| 885 |
} else { |
| 886 |
nb_samples = read_samples(fin,frame_size,fmt,chan,lsb,input, NULL, NULL); |
| 887 |
} |
| 888 |
if (nb_samples==0) |
| 889 |
{ |
| 890 |
eos=1; |
| 891 |
} |
| 892 |
if (eos && total_samples<=nb_encoded) |
| 893 |
op.e_o_s = 1; |
| 894 |
else |
| 895 |
op.e_o_s = 0; |
| 896 |
total_samples += nb_samples; |
| 897 |
|
| 898 |
if ((id+1)%nframes!=0) |
| 899 |
continue; |
| 900 |
speex_bits_insert_terminator(&bits); |
| 901 |
nbBytes = speex_bits_write(&bits, cbits, MAX_FRAME_BYTES); |
| 902 |
speex_bits_reset(&bits); |
| 903 |
op.packet = (unsigned char *)cbits; |
| 904 |
op.bytes = nbBytes; |
| 905 |
op.b_o_s = 0; |
| 906 |
/*Is this redundent?*/ |
| 907 |
if (eos && total_samples<=nb_encoded) |
| 908 |
op.e_o_s = 1; |
| 909 |
else |
| 910 |
op.e_o_s = 0; |
| 911 |
op.granulepos = (id+1)*frame_size-lookahead; |
| 912 |
if (op.granulepos>total_samples) |
| 913 |
op.granulepos = total_samples; |
| 914 |
/*printf ("granulepos: %d %d %d %d %d %d\n", (int)op.granulepos, id, nframes, lookahead, 5, 6);*/ |
| 915 |
op.packetno = 2+id/nframes; |
| 916 |
ogg_stream_packetin(&os, &op); |
| 917 |
|
| 918 |
/*Write all new pages (most likely 0 or 1)*/ |
| 919 |
while (ogg_stream_pageout(&os,&og)) |
| 920 |
{ |
| 921 |
ret = oe_write_page(&og, fout); |
| 922 |
if(ret != og.header_len + og.body_len) |
| 923 |
{ |
| 924 |
fprintf (stderr,"Error: failed writing header to output stream\n"); |
| 925 |
exit(1); |
| 926 |
} |
| 927 |
else |
| 928 |
bytes_written += ret; |
| 929 |
} |
| 930 |
} |
| 931 |
if ((id+1)%nframes!=0) |
| 932 |
{ |
| 933 |
while ((id+1)%nframes!=0) |
| 934 |
{ |
| 935 |
id++; |
| 936 |
speex_bits_pack(&bits, 15, 5); |
| 937 |
} |
| 938 |
nbBytes = speex_bits_write(&bits, cbits, MAX_FRAME_BYTES); |
| 939 |
op.packet = (unsigned char *)cbits; |
| 940 |
op.bytes = nbBytes; |
| 941 |
op.b_o_s = 0; |
| 942 |
op.e_o_s = 1; |
| 943 |
op.granulepos = (id+1)*frame_size-lookahead; |
| 944 |
if (op.granulepos>total_samples) |
| 945 |
op.granulepos = total_samples; |
| 946 |
|
| 947 |
op.packetno = 2+id/nframes; |
| 948 |
ogg_stream_packetin(&os, &op); |
| 949 |
} |
| 950 |
/*Flush all pages left to be written*/ |
| 951 |
while (ogg_stream_flush(&os, &og)) |
| 952 |
{ |
| 953 |
ret = oe_write_page(&og, fout); |
| 954 |
if(ret != og.header_len + og.body_len) |
| 955 |
{ |
| 956 |
fprintf (stderr,"Error: failed writing header to output stream\n"); |
| 957 |
exit(1); |
| 958 |
} |
| 959 |
else |
| 960 |
bytes_written += ret; |
| 961 |
} |
| 962 |
|
| 963 |
speex_encoder_destroy(st); |
| 964 |
speex_bits_destroy(&bits); |
| 965 |
ogg_stream_clear(&os); |
| 966 |
|
| 967 |
if (close_in) |
| 968 |
fclose(fin); |
| 969 |
if (close_out) |
| 970 |
fclose(fout); |
| 971 |
return 0; |
| 972 |
} |
| 973 |
|
| 974 |
/* |
| 975 |
Comments will be stored in the Vorbis style. |
| 976 |
It is describled in the "Structure" section of |
| 977 |
http://www.xiph.org/ogg/vorbis/doc/v-comment.html |
| 978 |
|
| 979 |
The comment header is decoded as follows: |
| 980 |
1) [vendor_length] = read an unsigned integer of 32 bits |
| 981 |
2) [vendor_string] = read a UTF-8 vector as [vendor_length] octets |
| 982 |
3) [user_comment_list_length] = read an unsigned integer of 32 bits |
| 983 |
4) iterate [user_comment_list_length] times { |
| 984 |
5) [length] = read an unsigned integer of 32 bits |
| 985 |
6) this iteration's user comment = read a UTF-8 vector as [length] octets |
| 986 |
} |
| 987 |
7) [framing_bit] = read a single bit as boolean |
| 988 |
8) if ( [framing_bit] unset or end of packet ) then ERROR |
| 989 |
9) done. |
| 990 |
|
| 991 |
If you have troubles, please write to [email protected]. |
| 992 |
*/ |
| 993 |
|
| 994 |
#define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \ |
| 995 |
((buf[base+2]<<16)&0xff0000)| \ |
| 996 |
((buf[base+1]<<8)&0xff00)| \ |
| 997 |
(buf[base]&0xff)) |
| 998 |
#define writeint(buf, base, val) do{ buf[base+3]=((val)>>24)&0xff; \ |
| 999 |
buf[base+2]=((val)>>16)&0xff; \ |
| 1000 |
buf[base+1]=((val)>>8)&0xff; \ |
| 1001 |
buf[base]=(val)&0xff; \ |
| 1002 |
}while(0) |
| 1003 |
|
| 1004 |
void comment_init(char **comments, int* length, char *vendor_string) |
| 1005 |
{ |
| 1006 |
int vendor_length=strlen(vendor_string); |
| 1007 |
int user_comment_list_length=0; |
| 1008 |
int len=4+vendor_length+4; |
| 1009 |
char *p=(char*)malloc(len); |
| 1010 |
if(p==NULL){ |
| 1011 |
fprintf (stderr, "malloc failed in comment_init()\n"); |
| 1012 |
exit(1); |
| 1013 |
} |
| 1014 |
writeint(p, 0, vendor_length); |
| 1015 |
memcpy(p+4, vendor_string, vendor_length); |
| 1016 |
writeint(p, 4+vendor_length, user_comment_list_length); |
| 1017 |
*length=len; |
| 1018 |
*comments=p; |
| 1019 |
} |
| 1020 |
void comment_add(char **comments, int* length, char *tag, char *val) |
| 1021 |
{ |
| 1022 |
char* p=*comments; |
| 1023 |
int vendor_length=readint(p, 0); |
| 1024 |
int user_comment_list_length=readint(p, 4+vendor_length); |
| 1025 |
int tag_len=(tag?strlen(tag):0); |
| 1026 |
int val_len=strlen(val); |
| 1027 |
int len=(*length)+4+tag_len+val_len; |
| 1028 |
|
| 1029 |
p=(char*)realloc(p, len); |
| 1030 |
if(p==NULL){ |
| 1031 |
fprintf (stderr, "realloc failed in comment_add()\n"); |
| 1032 |
exit(1); |
| 1033 |
} |
| 1034 |
|
| 1035 |
writeint(p, *length, tag_len+val_len); /* length of comment */ |
| 1036 |
if(tag) memcpy(p+*length+4, tag, tag_len); /* comment */ |
| 1037 |
memcpy(p+*length+4+tag_len, val, val_len); /* comment */ |
| 1038 |
writeint(p, 4+vendor_length, user_comment_list_length+1); |
| 1039 |
|
| 1040 |
*comments=p; |
| 1041 |
*length=len; |
| 1042 |
} |
| 1043 |
#undef readint |
| 1044 |
#undef writeint |