1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
|
/*
* tc_functions.c - various common functions for transcode
* Written by Thomas Oestreich, Francesco Romani, Andrew Church, and others
*
* This file is part of transcode, a video stream processing tool.
* transcode is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* transcode is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Make; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <sys/stat.h>
#include "xio.h"
#include "libtc.h"
#include "tc_defaults.h"
#include "transcode.h"
#include "ratiocodes.h"
/*************************************************************************/
#ifdef HAVE_FFMPEG
pthread_mutex_t tc_libavcodec_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
/*************************************************************************/
#define TC_MSG_BUF_SIZE (TC_BUF_MIN * 2)
/* WARNING: we MUST keep in sync preambles order with TC_LOG* macros */
static const char *tc_log_preambles[] = {
/* TC_LOG_ERR */
"["COL_RED"%s"COL_GRAY"]"COL_RED" critical"COL_GRAY": %s\n",
/* TC_LOG_WARN */
"["COL_RED"%s"COL_GRAY"]"COL_YELLOW" warning"COL_GRAY": %s\n",
/* TC_LOG_INFO */
"["COL_BLUE"%s"COL_GRAY"] %s\n",
/* TC_LOG_MSG */
"[%s] %s\n",
/* TC_LOG_EXTRA */
"%s%s" /* tag placeholder must be present but tag will be ignored */
};
static int tc_log_use_colors = TC_TRUE;
/*
* I'm not so proud of doing so. If someone has a better (cleaner)
* solution, just speak up. -- fromani 20060919
*/
void libtc_init(int *argc, char ***argv)
{
int ret = tc_mangle_cmdline(argc, argv, TC_LOG_COLOR_OPTION, NULL);
if (ret == 0) {
tc_log_use_colors = TC_FALSE;
} else {
const char *envvar = getenv(TC_LOG_COLOR_ENV_VAR);
if (envvar != NULL) {
tc_log_use_colors = TC_FALSE;
}
}
}
int tc_log(TCLogLevel level, const char *tag, const char *fmt, ...)
{
char buf[TC_MSG_BUF_SIZE];
char *msg = buf;
size_t size = sizeof(buf);
int dynbuf = TC_FALSE, truncated = TC_FALSE;
/* flag: we must use a dynamic (larger than static) buffer? */
va_list ap;
/* sanity check, avoid {under,over}flow; */
level = (level < TC_LOG_ERR) ?TC_LOG_ERR :level;
level = (level > TC_LOG_EXTRA) ?TC_LOG_EXTRA :level;
/* sanity check, avoid dealing with NULL as much as we can */
if (!tc_log_use_colors && level != TC_LOG_EXTRA) {
/* TC_LOG_EXTRA and TC_LOG_MSG must not use colors */
level = TC_LOG_MSG;
}
tag = (tag != NULL) ?tag :"";
fmt = (fmt != NULL) ?fmt :"";
/* TC_LOG_EXTRA special handling: force always empty tag */
tag = (level == TC_LOG_EXTRA) ?"" :tag;
size = strlen(tc_log_preambles[level])
+ strlen(tag) + strlen(fmt) + 1;
if (size > sizeof(buf)) {
/*
* we use malloc/fprintf instead of tc_malloc because
* we want custom error messages
*/
msg = malloc(size);
if (msg != NULL) {
dynbuf = TC_TRUE;
} else {
fprintf(stderr, "(%s) CRITICAL: can't get memory in "
"tc_log() output will be truncated.\n",
__FILE__);
/* force reset to default values */
msg = buf;
size = sizeof(buf) - 1;
truncated = TC_TRUE;
}
} else {
size = sizeof(buf) - 1;
}
/* construct real format string */
tc_snprintf(msg, size, tc_log_preambles[level], tag, fmt);
// msg[size] = '\0';
// /* `size' value was already scaled for the final '\0' */
// trusting valgrind, this assignement don't help us and can
// lead to troubles. So it's temporary disable until I gain
// some time to experiment -- fromani 20060919
va_start(ap, fmt);
vfprintf(stderr, msg, ap);
va_end(ap);
if (dynbuf == 1) {
free(msg);
}
/* ensure that all *other* messages are written */
fflush(stderr);
return (truncated) ?-1 :0;
}
/*************************************************************************/
int tc_mangle_cmdline(int *argc, char ***argv,
const char *opt, const char **optval)
{
int i = 0, skew = (optval == NULL) ?1 :2, err = -1;
if (argc == NULL || argv == NULL || opt == NULL) {
return err;
}
err = 1;
/* first we looking for our option (and it's value) */
for (i = 1; i < *argc; i++) {
if ((*argv)[i] && strcmp((*argv)[i], opt) == 0) {
if (optval == NULL) {
err = 0; /* we're set */
} else {
/* don't peek after the end... */
if (i + 1 >= *argc || (*argv)[i + 1][0] == '-') {
tc_log_warn(__FILE__, "wrong usage for option '%s'", opt);
err = 1; /* no option and/or value found */
} else {
*optval = (*argv)[i + 1];
err = 0;
}
}
break;
}
}
/*
* if we've found our option, now we must shift back all
* the other options after the ours and we must also update argc.
*/
if (!err) {
for (; i < (*argc - skew); i++) {
(*argv)[i] = (*argv)[i + skew];
}
(*argc) -= skew;
}
return err;
}
int tc_test_program(const char *name)
{
#ifndef NON_POSIX_PATH
const char *path = getenv("PATH");
char *tok_path = NULL;
char *compl_path = NULL;
char *tmp_path;
char **strtokbuf;
char done;
size_t pathlen;
long sret;
int error = 0;
if (name == NULL) {
tc_warn("ERROR: Searching for a NULL program!\n");
return ENOENT;
}
if (path == NULL) {
tc_warn("The '%s' program could not be found. \n", name);
tc_warn("Because your PATH environment variable is not set.\n");
return ENOENT;
}
pathlen = strlen(path) + 1;
tmp_path = tc_malloc(pathlen);
strtokbuf = tc_malloc(pathlen);
sret = strlcpy(tmp_path, path, pathlen);
tc_test_string(__FILE__, __LINE__, pathlen, sret, errno);
/* iterate through PATH tokens */
for (done = 0, tok_path = strtok_r(tmp_path, ":", strtokbuf);
!done && tok_path;
tok_path = strtok_r((char *)0, ":", strtokbuf)) {
pathlen = strlen(tok_path) + strlen(name) + 2;
compl_path = tc_malloc(pathlen * sizeof(char));
sret = tc_snprintf(compl_path, pathlen, "%s/%s", tok_path, name);
if (access(compl_path, X_OK) == 0) {
error = 0;
done = 1;
} else { /* access != 0 */
if (errno != ENOENT) {
done = 1;
error = errno;
}
}
tc_free(compl_path);
}
tc_free(tmp_path);
tc_free(strtokbuf);
if (!done) {
tc_warn("The '%s' program could not be found. \n", name);
tc_warn("Please check your installation.\n");
return ENOENT;
}
if (error != 0) {
/* access returned an unhandled error */
tc_warn("The '%s' program was found, but is not accessible.\n", name);
tc_warn("%s\n", strerror(errno));
tc_warn("Please check your installation.\n");
return error;
}
#endif
return 0;
}
int tc_test_string(const char *file, int line, int limit, long ret, int errnum)
{
if (ret < 0) {
fprintf(stderr, "[%s:%d] string error: %s\n",
file, line, strerror(errnum));
return 1;
}
if (ret >= limit) {
fprintf(stderr, "[%s:%d] truncated %ld characters\n",
file, line, (ret - limit) + 1);
return 1;
}
return 0;
}
/*************************************************************************/
/*
* These versions of [v]snprintf() return -1 if the string was truncated,
* printing a message to stderr in case of truncation (or other error).
*/
int _tc_vsnprintf(const char *file, int line, char *buf, size_t limit,
const char *format, va_list args)
{
int res = vsnprintf(buf, limit, format, args);
return tc_test_string(file, line, limit, res, errno) ? -1 : res;
}
int _tc_snprintf(const char *file, int line, char *buf, size_t limit,
const char *format, ...)
{
va_list args;
int res;
va_start(args, format);
res = _tc_vsnprintf(file, line, buf, limit, format, args);
va_end(args);
return res;
}
/*************************************************************************/
/* simple malloc wrapper with failure guard. */
void *_tc_malloc(const char *file, int line, size_t size)
{
void *p = malloc(size);
if (p == NULL) {
fprintf(stderr, "[%s:%d] tc_malloc(): can't allocate %lu bytes\n",
file, line, (unsigned long)size);
}
return p;
}
/* allocate a chunk of memory (like tc_malloc), but zeroes memory before
* returning. */
void *_tc_zalloc(const char *file, int line, size_t size)
{
void *p = malloc(size);
if (p == NULL) {
fprintf(stderr, "[%s:%d] tc_zalloc(): can't allocate %lu bytes\n",
file, line, (unsigned long)size);
} else {
memset(p, 0, size);
}
return p;
}
/* realloc() wrapper. */
void *_tc_realloc(const char *file, int line, void *p, size_t size)
{
p = realloc(p, size);
if (p == NULL && size > 0) {
fprintf(stderr, "[%s:%d] tc_realloc(): can't allocate %lu bytes\n",
file, line, (unsigned long)size);
}
return p;
}
/*** FIXME ***: find a clean way to refactorize above functions */
/* Allocate a buffer aligned to the machine's page size, if known. The
* buffer must be freed with buffree() (not free()). */
void *_tc_bufalloc(const char *file, int line, size_t size)
{
#ifdef HAVE_GETPAGESIZE
unsigned long pagesize = getpagesize();
int8_t *base = malloc(size + sizeof(void *) + pagesize);
int8_t *ptr = NULL;
unsigned long offset = 0;
if (base == NULL) {
fprintf(stderr, "[%s:%d] tc_bufalloc(): can't allocate %lu bytes\n",
file, line, (unsigned long)size);
} else {
ptr = base + sizeof(void *);
offset = (unsigned long)ptr % pagesize;
if (offset)
ptr += (pagesize - offset);
((void **)ptr)[-1] = base; /* save the base pointer for freeing */
}
return ptr;
#else /* !HAVE_GETPAGESIZE */
return malloc(size);
#endif
}
char *_tc_strndup(const char *file, int line, const char *s, size_t n)
{
char *pc = NULL;
if (s != NULL) {
pc = _tc_malloc(file, line, n + 1);
if (pc != NULL) {
memcpy(pc, s, n);
pc[n] = '\0';
}
}
return pc;
}
/* Free a buffer allocated with tc_bufalloc(). */
void tc_buffree(void *ptr)
{
#ifdef HAVE_GETPAGESIZE
if (ptr)
free(((void **)ptr)[-1]);
#else
free(ptr);
#endif
}
/*************************************************************************/
ssize_t tc_pread(int fd, uint8_t *buf, size_t len)
{
ssize_t n = 0;
ssize_t r = 0;
while (r < len) {
n = xio_read(fd, buf + r, len - r);
if (n == 0) { /* EOF */
break;
}
if (n < 0) {
if (errno == EINTR) {
continue;
} else {
break;
}
}
r += n;
}
return r;
}
ssize_t tc_pwrite(int fd, const uint8_t *buf, size_t len)
{
ssize_t n = 0;
ssize_t r = 0;
while (r < len) {
n = xio_write(fd, buf + r, len - r);
if (n < 0) {
if (errno == EINTR) {
continue;
} else {
break;
}
}
r += n;
}
return r;
}
#ifdef PIPE_BUF
# define BLOCKSIZE PIPE_BUF /* 4096 on linux-x86 */
#else
# define BLOCKSIZE 4096
#endif
int tc_preadwrite(int fd_in, int fd_out)
{
uint8_t buffer[BLOCKSIZE];
ssize_t bytes;
int error = 0;
do {
bytes = tc_pread(fd_in, buffer, BLOCKSIZE);
/* error on read? */
if (bytes < 0) {
return -1;
}
/* read stream end? */
if (bytes != BLOCKSIZE) {
error = 1;
}
if (bytes) {
/* write stream problems? */
if (tc_pwrite(fd_out, buffer, bytes) != bytes) {
error = 1;
}
}
} while (!error);
return 0;
}
int tc_file_check(const char *name)
{
struct stat fbuf;
if(xio_stat(name, &fbuf)) {
tc_log_warn(__FILE__, "invalid file \"%s\"", name);
return -1;
}
/* file or directory? */
if(S_ISDIR(fbuf.st_mode)) {
return 1;
}
return 0;
}
#ifndef major
# define major(dev) (((dev) >> 8) & 0xff)
#endif
int tc_probe_path(const char *name)
{
struct stat fbuf;
if(name == NULL) {
tc_log_warn(__FILE__, "invalid file \"%s\"", name);
return TC_PROBE_PATH_INVALID;
}
if(xio_stat(name, &fbuf) == 0) {
/* inode exists */
/* treat DVD device as absolute directory path */
if (S_ISBLK(fbuf.st_mode)) {
return TC_PROBE_PATH_ABSPATH;
}
/* char device could be several things, depending on system */
/* *BSD DVD device? v4l? bktr? sunau? */
if(S_ISCHR(fbuf.st_mode)) {
switch (major(fbuf.st_rdev)) {
#ifdef OS_BSD
# ifdef __OpenBSD__
case 15: /* rcd */
return TC_PROBE_PATH_ABSPATH;
case 42: /* sunau */
return TC_PROBE_PATH_SUNAU;
case 49: /* bktr */
return TC_PROBE_PATH_BKTR;
# endif
# ifdef __FreeBSD__
case 4: /* acd */
return TC_PROBE_PATH_ABSPATH;
case 229: /* bktr */
return TC_PROBE_PATH_BKTR;
case 0: /* OSS */
return TC_PROBE_PATH_OSS;
# endif
default: /* libdvdread uses "raw" disk devices here */
return TC_PROBE_PATH_ABSPATH;
#else
case 81: /* v4l (Linux) */
return TC_PROBE_PATH_V4L_VIDEO;
case 14: /* OSS */
return TC_PROBE_PATH_OSS;
default:
break;
#endif
}
}
/* file or directory? */
if (!S_ISDIR(fbuf.st_mode)) {
return TC_PROBE_PATH_FILE;
}
/* directory, check for absolute path */
if(name[0] == '/') {
return TC_PROBE_PATH_ABSPATH;
}
/* directory mode */
return TC_PROBE_PATH_RELDIR;
} else {
tc_log_warn(__FILE__, "invalid filename \"%s\"", name);
return TC_PROBE_PATH_INVALID;
}
return TC_PROBE_PATH_INVALID;
}
/*************************************************************************/
#define RESIZE_DIV 8
#define DIM_IS_OK(dim) ((dim) % RESIZE_DIV == 0)
int tc_compute_fast_resize_values(void *_vob, int strict)
{
int ret = -1;
int dw = 0, dh = 0; /* delta width, height */
vob_t *vob = _vob; /* adjust pointer */
/* sanity checks first */
if (vob == NULL) {
return -1;
}
if (!DIM_IS_OK(vob->ex_v_width) || !DIM_IS_OK(vob->ex_v_width)) {
return -1;
}
if (!DIM_IS_OK(vob->zoom_width) || !DIM_IS_OK(vob->zoom_width)) {
return -1;
}
dw = vob->ex_v_width - vob->zoom_width;
dh = vob->ex_v_height - vob->zoom_height;
/* MORE sanity checks */
if (!DIM_IS_OK(dw) || !DIM_IS_OK(dh)) {
return -1;
}
if (dw == 0 && dh == 0) {
/* we're already fine */
ret = 0;
} else if (dw > 0 && dh > 0) {
/* smaller destination frame -> -B */
vob->resize1_mult = RESIZE_DIV;
vob->hori_resize1 = dw / RESIZE_DIV;
vob->vert_resize1 = dh / RESIZE_DIV;
ret = 0;
} else if (dw < 0 && dh < 0) {
/* bigger destination frame -> -X */
vob->resize2_mult = RESIZE_DIV;
vob->hori_resize2 = -dw / RESIZE_DIV;
vob->vert_resize2 = -dh / RESIZE_DIV;
ret = 0;
} else if (strict == 0) {
/* always needed in following cases */
vob->resize1_mult = RESIZE_DIV;
vob->resize2_mult = RESIZE_DIV;
ret = 0;
if (dw <= 0 && dh >= 0) {
vob->hori_resize2 = -dw / RESIZE_DIV;
vob->vert_resize1 = dh / RESIZE_DIV;
} else if (dw >= 0 && dh <= 0) {
vob->hori_resize1 = dw / RESIZE_DIV;
vob->vert_resize2 = -dh / RESIZE_DIV;
}
}
if (ret == 0) {
vob->zoom_width = 0;
vob->zoom_height = 0;
}
return ret;
}
#undef RESIZE_DIV
#undef DIM_IS_OK
/*************************************************************************/
int tc_find_best_aspect_ratio(const void *_vob,
int *sar_num, int *sar_den,
const char *tag)
{
const vob_t *vob = _vob; /* adjust pointer */
int num, den;
if (!vob || !sar_num || !sar_den) {
return TC_ERROR;
}
/* Aspect Ratio Calculations (modified code from export_ffmpeg.c) */
if (vob->export_attributes & TC_EXPORT_ATTRIBUTE_PAR) {
if (vob->ex_par > 0) {
/*
* vob->ex_par MUST be guarantee to be in a sane range
* by core (transcode/tcexport
*/
tc_par_code_to_ratio(vob->ex_par, &num, &den);
} else {
/* same as above */
num = vob->ex_par_width;
den = vob->ex_par_height;
}
tc_log_info(tag, "DAR value ratio calculated as %f = %d/%d",
(double)num/(double)den, num, den);
} else {
if (vob->export_attributes & TC_EXPORT_ATTRIBUTE_ASR) {
/* same as above for PAR stuff */
tc_asr_code_to_ratio(vob->ex_asr, &num, &den);
tc_log_info(tag, "display aspect ratio calculated as %f = %d/%d",
(double)num/(double)den, num, den);
/* ffmpeg FIXME:
* This original code might lead to rounding/truncating errors
* and maybe produces too high values for "den" and
* "num" for -y ffmpeg -F mpeg4
*
* sar = dar * ((double)vob->ex_v_height / (double)vob->ex_v_width);
* lavc_venc_context->sample_aspect_ratio.num = (int)(sar * 1000);
* lavc_venc_context->sample_aspect_ratio.den = 1000;
*/
num *= vob->ex_v_height;
den *= vob->ex_v_width;
/* I don't need to reduce since x264 does it itself :-) */
tc_log_info(tag, "sample aspect ratio calculated as"
" %f = %d/%d",
(double)num/(double)den, num, den);
} else { /* user did not specify asr at all, assume no change */
tc_log_info(tag, "set display aspect ratio to input");
num = 1;
den = 1;
}
}
*sar_num = num;
*sar_den = den;
return TC_OK;
}
/*************************************************************************/
void tc_strstrip(char *s)
{
char *start;
if (s == NULL) {
return;
}
start = s;
while ((*start != 0) && isspace(*start)) {
start++;
}
memmove(s, start, strlen(start) + 1);
if (strlen(s) == 0) {
return;
}
start = &s[strlen(s) - 1];
while ((start != s) && isspace(*start)) {
*start = 0;
start--;
}
}
char **tc_strsplit(const char *str, char sep, size_t *pieces_num)
{
const char *begin = str, *end = NULL;
char **pieces = NULL, *pc = NULL;
size_t i = 0, n = 2;
int failed = TC_FALSE;
if (!str || !strlen(str)) {
return NULL;
}
while (begin != NULL) {
begin = strchr(begin, sep);
if (begin != NULL) {
begin++;
n++;
}
}
pieces = tc_malloc(n * sizeof(char*));
if (!pieces) {
return NULL;
}
begin = str;
while (begin != NULL) {
size_t len;
end = strchr(begin, sep);
if (end != NULL) {
len = (end - begin);
} else {
len = strlen(begin);
}
if (len > 0) {
pc = tc_strndup(begin, len);
if (pc == NULL) {
failed = TC_TRUE;
break;
} else {
pieces[i] = pc;
i++;
}
}
if (end != NULL) {
begin = end + 1;
} else {
break;
}
}
if (failed) {
/* one or more copy of pieces failed */
tc_free(pieces);
pieces = NULL;
} else { /* i == n - 1 -> all pieces copied */
pieces[n - 1] = NULL; /* end marker */
if (pieces_num != NULL) {
*pieces_num = i;
}
}
return pieces;
}
void tc_strfreev(char **pieces)
{
if (pieces != NULL) {
int i = 0;
for (i = 0; pieces[i] != NULL; i++) {
tc_free(pieces[i]);
}
tc_free(pieces);
}
}
/*************************************************************************/
/*
* clamp an unsigned value so it can be safely (without any loss) in
* an another unsigned integer of <butsize> bits.
*/
static int32_t clamp(int32_t value, uint8_t bitsize)
{
value = (value < 1) ?1 :value;
value = (value > (1 << bitsize)) ?(1 << bitsize) :value;
return value;
}
int tc_read_matrix(const char *filename, uint8_t *m8, uint16_t *m16)
{
int i = 0;
FILE *input = NULL;
/* Open the matrix file */
input = fopen(filename, "rb");
if (!input) {
tc_log_warn("read_matrix",
"Error opening the matrix file %s",
filename);
return -1;
}
if (!m8 && !m16) {
tc_log_warn("read_matrix", "bad matrix reference");
return -1;
}
/* Read the matrix */
for(i = 0; i < TC_MATRIX_SIZE; i++) {
int value;
/* If fscanf fails then get out of the loop */
if(fscanf(input, "%d", &value) != 1) {
tc_log_warn("read_matrix",
"Error reading the matrix file %s",
filename);
fclose(input);
return 1;
}
if (m8 != NULL) {
m8[i] = clamp(value, 8);
} else {
m16[i] = clamp(value, 16);
}
}
/* We're done */
fclose(input);
return 0;
}
void tc_print_matrix(uint8_t *m8, uint16_t *m16)
{
int i;
if (!m8 && !m16) {
tc_log_warn("print_matrix", "bad matrix reference");
return;
}
// XXX: magic number
for(i = 0; i < TC_MATRIX_SIZE; i += 8) {
if (m8 != NULL) {
tc_log_info("print_matrix",
"%3d %3d %3d %3d "
"%3d %3d %3d %3d",
(int)m8[i ], (int)m8[i+1],
(int)m8[i+2], (int)m8[i+3],
(int)m8[i+4], (int)m8[i+5],
(int)m8[i+6], (int)m8[i+7]);
} else {
tc_log_info("print_matrix",
"%3d %3d %3d %3d "
"%3d %3d %3d %3d",
(int)m16[i ], (int)m16[i+1],
(int)m16[i+2], (int)m16[i+3],
(int)m16[i+4], (int)m16[i+5],
(int)m16[i+6], (int)m16[i+7]);
}
}
return;
}
/*************************************************************************/
/*
* Local variables:
* c-file-style: "stroustrup"
* c-file-offsets: ((case-label . *) (statement-case-intro . *))
* indent-tabs-mode: nil
* End:
*
* vim: expandtab shiftwidth=4:
*/
|