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 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
|
/*
* FILE: codec.c
* AUTHORS: Orion Hodson
*
* Copyright (c) 1998-2001 University College London
* All rights reserved.
*/
#ifndef HIDE_SOURCE_STRINGS
static const char cvsid[] =
"$Id: codec.c,v 1.74 2002/03/15 17:57:16 ucacoxh Exp $";
#endif /* HIDE_SOURCE_STRINGS */
#include "config_unix.h"
#include "config_win32.h"
#include "debug.h"
/* These two just for UCL memory debugging fn's */
#include "memory.h"
#include "util.h"
#include "audio_types.h"
#include "codec_types.h"
#include "codec.h"
#include "codec_l8.h"
#include "codec_l16.h"
#include "codec_g711.h"
#include "codec_g726.h"
#include "codec_dvi.h"
#include "codec_gsm.h"
#include "codec_lpc.h"
#include "codec_vdvi.h"
#ifndef REMOVE_WBS_CODEC
#include "codec_wbs.h"
#endif
#include "codec_g728.h"
/* Codec class initialization - hey didn't c++ happen somewhere along the
* timeline ;-)
*/
/* One time codec {con,de}struction */
typedef void (*cx_init_f) (void);
typedef void (*cx_exit_f) (void);
/* Codec Probing functions */
typedef uint16_t (*cx_get_formats_count_f) (void);
typedef const codec_format_t* (*cx_get_format_f) (uint16_t);
/* Codec Encoding functions */
typedef int (*cx_encoder_create_f) (uint16_t idx, u_char **state);
typedef void (*cx_encoder_destroy_f) (uint16_t idx, u_char **state);
typedef int (*cx_encode_f) (uint16_t idx, u_char *state,
sample *in, coded_unit *out);
typedef int (*cx_can_encode_f) (uint16_t idx);
/* Codec Decoding functions */
typedef int (*cx_decoder_create_f) (uint16_t idx, u_char **state);
typedef void (*cx_decoder_destroy_f) (uint16_t idx, u_char **state);
typedef int (*cx_decode_f) (uint16_t idx, u_char *state,
coded_unit *in, sample *out);
typedef int (*cx_can_decode_f) (uint16_t idx);
/* For determining frame sizes of variable bit rate codecs */
typedef int (*cx_peek_size_f) (uint16_t idx, u_char *data, int data_len);
/* For codec domain repair schemes */
typedef int (*cx_repair_f) (uint16_t idx,
u_char *state,
uint16_t consec_missing,
coded_unit *prev,
coded_unit *missing,
coded_unit *next);
/* For layered codecs */
typedef uint8_t (*cx_can_layer_f) (void);
typedef int (*cx_get_layer_f) (uint16_t idx, coded_unit *cu_whole, uint8_t layer,
uint16_t *markers, coded_unit *cu_layer);
typedef int (*cx_combine_layer_f) (uint16_t idx, coded_unit *cu_layer, coded_unit *cu_whole, uint8_t nelem, uint16_t *markers);
typedef struct s_codec_fns {
cx_init_f cx_init;
cx_exit_f cx_exit;
cx_get_formats_count_f cx_get_formats_count;
cx_get_format_f cx_get_format;
cx_encoder_create_f cx_encoder_create;
cx_encoder_destroy_f cx_encoder_destroy;
cx_encode_f cx_encode;
cx_can_encode_f cx_can_encode;
cx_decoder_create_f cx_decoder_create;
cx_decoder_destroy_f cx_decoder_destroy;
cx_decode_f cx_decode;
cx_can_decode_f cx_can_decode;
cx_peek_size_f cx_peek_size;
cx_repair_f cx_repair;
cx_can_layer_f cx_can_layer;
cx_get_layer_f cx_get_layer;
cx_combine_layer_f cx_combine_layer;
} codec_fns_t;
static codec_fns_t codec_table[] = {
{
NULL,
NULL,
l16_get_formats_count,
l16_get_format,
NULL, /* No encoder setup / tear down */
NULL,
l16_encode,
NULL,
NULL,
NULL, /* No decoder setup / tear down */
l16_decode,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
},
{
NULL,
NULL,
l8_get_formats_count,
l8_get_format,
NULL, /* No encoder setup / tear down */
NULL,
l8_encode,
NULL,
NULL,
NULL, /* No decoder setup / tear down */
l8_decode,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
},
{ g711_init,
NULL,
g711_get_formats_count,
g711_get_format,
NULL,
NULL,
g711_encode,
NULL,
NULL,
NULL,
g711_decode,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
},
#ifdef HAVE_G728
{
NULL,
NULL,
g728_get_formats_count,
g728_get_format,
g728_encoder_create,
g728_encoder_destroy,
g728_encoder_do,
NULL,
g728_decoder_create,
g728_decoder_destroy,
g728_decoder_do,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
},
#endif /* HAVE_G728 */
{
NULL,
NULL,
g726_get_formats_count,
g726_get_format,
g726_state_create,
g726_state_destroy,
g726_encode,
NULL,
g726_state_create,
g726_state_destroy,
g726_decode,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
},
{
NULL,
NULL,
dvi_get_formats_count,
dvi_get_format,
dvi_state_create,
dvi_state_destroy,
dvi_encode,
NULL,
dvi_state_create,
dvi_state_destroy,
dvi_decode,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
},
{
NULL,
NULL,
vdvi_get_formats_count,
vdvi_get_format,
vdvi_state_create,
vdvi_state_destroy,
vdvi_encoder,
NULL,
vdvi_state_create,
vdvi_state_destroy,
vdvi_decoder,
NULL,
vdvi_peek_frame_size,
NULL,
NULL,
NULL,
NULL
},
#ifndef REMOVE_WBS_CODEC
/* The WB-ADPCM algorithm was developed by British Telecommunications */
/* plc. Permission has been granted to use it for non-commercial */
/* research and development projects. BT retain the intellectual */
/* property rights to this algorithm. */
{
NULL,
NULL,
wbs_get_formats_count,
wbs_get_format,
wbs_state_create,
wbs_state_destroy,
wbs_encoder,
NULL,
wbs_state_create,
wbs_state_destroy,
wbs_decoder,
NULL,
NULL,
NULL,
wbs_max_layers,
wbs_get_layer,
wbs_combine_layer
},
#endif
{
NULL,
NULL,
gsm_get_formats_count,
gsm_get_format,
gsm_state_create,
gsm_state_destroy,
gsm_encoder,
NULL,
gsm_state_create,
gsm_state_destroy,
gsm_decoder,
NULL,
NULL,
gsm_repair,
NULL,
NULL,
NULL
},
{
lpc_setup,
NULL,
lpc_get_formats_count,
lpc_get_format,
lpc_encoder_state_create,
lpc_encoder_state_destroy,
lpc_encoder,
NULL,
lpc_decoder_state_create,
lpc_decoder_state_destroy,
lpc_decoder,
NULL,
NULL,
lpc_repair,
NULL,
NULL,
NULL
}
};
/* NUM_CODEC_INTERFACES = number of codec interfaces */
/* Applications never know this... */
#define NUM_CODEC_INTERFACES (sizeof(codec_table)/sizeof(codec_fns_t))
/* These are used to save multiple function calls in
* codec_get_codec_number function. */
static uint16_t num_fmts_supported[NUM_CODEC_INTERFACES];
static uint16_t total_fmts_supported;
/* Codec identifier is 32 bits long. It's like an MS handle.
* First byte is always zero.
* Second byte is index in codec_table above plus 1.
* Third and fourth bytes hold index for encoding format
* used by codec.
*/
#define CODEC_GET_IFS_INDEX(id) (uint16_t)(((id & 0x00ff0000) >> 16) - 1)
#define CODEC_GET_FMT_INDEX(id) (uint16_t)((id & 0x0000ffff) - 1)
#define CODEC_MAKE_ID(ifs,fmt) (((ifs) + 1) << 16)|((fmt+1)&0xffff)
#define CODEC_VALID_PAD(id) (!(id & 0xff000000))
#define CODEC_VALID_IFS(id) (id & 0x00ff0000)
#define CODEC_VALID_FMT(id) (id & 0x0000ffff)
int
codec_id_is_valid(codec_id_t id)
{
uint32_t ifs, fmt;
if (codec_is_native_coding(id)) {
/* Native codings should be tested with
* codec_is_native_coding */
debug_msg("Coding is invalid because it is a native coding\n");
return FALSE;
}
if (!CODEC_VALID_PAD(id) ||
!CODEC_VALID_IFS(id) ||
!CODEC_VALID_FMT(id)) {
debug_msg("Codec id (0x%08x) invalid (pad %x, ifs %x, fmt %x)",
id, CODEC_VALID_PAD(id), CODEC_VALID_IFS(id),
CODEC_VALID_FMT(id));
return FALSE;
}
ifs = CODEC_GET_IFS_INDEX(id);
fmt = CODEC_GET_FMT_INDEX(id);
if (ifs >= NUM_CODEC_INTERFACES) {
/* Table index too large */
debug_msg("Codec index outside table\n");
return FALSE;
}
if (fmt >= num_fmts_supported[ifs]) {
/* Format index too large */
debug_msg("Format index outside table %d / %d\n", fmt, num_fmts_supported[ifs]);
return FALSE;
}
return TRUE;
}
static void codec_map_init(void);
static void codec_map_exit(void);
void
codec_init()
{
const codec_format_t *cf;
uint32_t i;
uint16_t j;
if (total_fmts_supported == 0) {
for(i = 0; i < NUM_CODEC_INTERFACES; i++) {
if (codec_table[i].cx_init) codec_table[i].cx_init();
num_fmts_supported[i] = codec_table[i].cx_get_formats_count();
total_fmts_supported += num_fmts_supported[i];
for(j = 0; j < num_fmts_supported[i]; j++) {
cf = codec_table[i].cx_get_format(j);
/* Most compilers should spot this check anyway */
assert(strlen(cf->short_name) < CODEC_SHORT_NAME_LEN);
assert(strlen(cf->long_name) < CODEC_LONG_NAME_LEN);
assert(strlen(cf->description) < CODEC_DESCRIPTION_LEN);
}
}
codec_map_init();
} else {
debug_msg("codec_init already called - ignoring.\n");
}
}
void
codec_exit()
{
uint32_t i;
if (total_fmts_supported != 0) {
for(i = 0; i < NUM_CODEC_INTERFACES; i++) {
if (codec_table[i].cx_exit) codec_table[i].cx_exit();
}
codec_map_exit();
total_fmts_supported = 0;
} else {
debug_msg("codec_exit not inited - ignoring\n");
}
}
uint32_t
codec_get_number_of_codecs()
{
return total_fmts_supported;
}
codec_id_t
codec_get_codec_number(uint32_t n)
{
codec_id_t id;
uint32_t ifs;
assert(n < total_fmts_supported);
for(ifs = 0; n >= num_fmts_supported[ifs]; ifs++) {
n = n - num_fmts_supported[ifs];
}
id = CODEC_MAKE_ID(ifs, n);
assert(codec_id_is_valid(id));
return id;
}
const codec_format_t*
codec_get_format(codec_id_t id)
{
uint16_t ifs, fmt;
assert(codec_id_is_valid(id));
ifs = CODEC_GET_IFS_INDEX(id);
fmt = CODEC_GET_FMT_INDEX(id);
return codec_table[ifs].cx_get_format(fmt);
}
int
codec_can_encode(codec_id_t id)
{
uint16_t ifs;
ifs = CODEC_GET_IFS_INDEX(id);
assert(codec_id_is_valid(id));
assert(ifs < NUM_CODEC_INTERFACES);
if (codec_table[ifs].cx_can_encode) {
/* cx_can_encode only needs to exist if encoder and decoder are asymmetric */
return codec_table[ifs].cx_can_encode(CODEC_GET_FMT_INDEX(id));
} else {
const codec_format_t *cf;
cf = codec_get_format(id);
if (cf->format.sample_rate % 8000) {
return FALSE; /* only m * 8k at the moment */
}
}
return TRUE;
}
int
codec_can_decode(codec_id_t id)
{
uint32_t ifs;
ifs = CODEC_GET_IFS_INDEX(id);
assert(codec_id_is_valid(id));
assert(ifs < NUM_CODEC_INTERFACES);
if (codec_table[ifs].cx_can_decode) {
/* cx_can_encode only needs to exist if encoder and decoder are asymmetric */
return codec_table[ifs].cx_can_decode(CODEC_GET_FMT_INDEX(id));
} else {
const codec_format_t *cf;
cf = codec_get_format(id);
if (cf->format.sample_rate % 8000) {
return FALSE; /* Only m * 8k at moment */
}
}
return TRUE;
}
int
codec_audio_formats_compatible(codec_id_t id1, codec_id_t id2)
{
const codec_format_t *cf1, *cf2;
int match;
assert(codec_id_is_valid(id1));
assert(codec_id_is_valid(id2));
cf1 = codec_get_format(id1);
cf2 = codec_get_format(id2);
match = !memcmp(&cf1->format, &cf2->format, sizeof(audio_format));
return match;
}
uint32_t
codec_get_samples_per_frame(codec_id_t id)
{
const codec_format_t *cf;
uint32_t spf;
assert(codec_id_is_valid(id));
cf = codec_get_format(id);
spf = cf->format.bytes_per_block * 8 /
(cf->format.channels * cf->format.bits_per_sample);
return spf;
}
/* Encoder related ***********************************************************/
int
codec_encoder_create(codec_id_t id, codec_state **cs)
{
if (codec_id_is_valid(id)) {
uint16_t ifs, fmt;
*cs = (codec_state*)block_alloc(sizeof(codec_state));
if (!cs) {
*cs = NULL;
return 0;
}
(*cs)->state = NULL;
(*cs)->id = id;
ifs = CODEC_GET_IFS_INDEX(id);
fmt = CODEC_GET_FMT_INDEX(id);
if (codec_table[ifs].cx_encoder_create) {
/* Must also have a destructor */
assert(codec_table[ifs].cx_encoder_destroy != NULL);
codec_table[ifs].cx_encoder_create(fmt,
&(*cs)->state);
}
return TRUE;
} else {
debug_msg("Attempting to initiate invalid codec\n");
abort();
}
return 0;
}
void
codec_encoder_destroy(codec_state **cs)
{
codec_id_t id;
assert(*cs != NULL);
id = (*cs)->id;
if (codec_id_is_valid(id)) {
uint16_t ifs, fmt;
ifs = CODEC_GET_IFS_INDEX(id);
fmt = CODEC_GET_FMT_INDEX(id);
if (codec_table[ifs].cx_encoder_destroy) {
/* Must also have a destructor */
codec_table[ifs].cx_encoder_destroy(fmt,
&(*cs)->state);
}
block_free(*cs, sizeof(codec_state));
*cs = NULL;
} else {
debug_msg("Destroying corrupted codec\n");
abort();
}
}
int
codec_encode(codec_state *cs,
coded_unit *in_native,
coded_unit *cu)
{
uint16_t ifs, fmt;
int success;
assert(cs != NULL);
assert(in_native != NULL);
assert(cu != NULL);
assert(codec_is_native_coding(in_native->id));
assert (in_native->state == NULL);
#ifdef DEBUG
{
const codec_format_t *cf = codec_get_format(cs->id);
assert (cf->format.bytes_per_block == in_native->data_len);
}
#endif
cu->id = cs->id;
ifs = CODEC_GET_IFS_INDEX(cu->id);
fmt = CODEC_GET_FMT_INDEX(cu->id);
xmemchk();
success = codec_table[ifs].cx_encode(fmt, cs->state, (sample*)in_native->data, cu);
xmemchk();
return success;
}
/* Decoder related ***********************************************************/
int
codec_decoder_create(codec_id_t id, codec_state **cs)
{
if (codec_id_is_valid(id)) {
uint16_t ifs, fmt;
*cs = (codec_state*)block_alloc(sizeof(codec_state));
if (!cs) {
*cs = NULL;
return 0;
}
(*cs)->state = NULL;
(*cs)->id = id;
ifs = CODEC_GET_IFS_INDEX(id);
fmt = CODEC_GET_FMT_INDEX(id);
if (codec_table[ifs].cx_decoder_create) {
/* Must also have a destructor */
assert(codec_table[ifs].cx_decoder_destroy != NULL);
codec_table[ifs].cx_decoder_create(fmt,
&(*cs)->state);
}
return TRUE;
} else {
debug_msg("Attempting to initiate invalid codec\n");
abort();
}
return 0;
}
void
codec_decoder_destroy(codec_state **cs)
{
codec_id_t id;
assert(*cs != NULL);
id = (*cs)->id;
if (codec_id_is_valid(id)) {
uint16_t ifs, fmt;
ifs = CODEC_GET_IFS_INDEX(id);
fmt = CODEC_GET_FMT_INDEX(id);
if (codec_table[ifs].cx_decoder_destroy) {
/* Must also have a destructor */
codec_table[ifs].cx_decoder_destroy(fmt,
&(*cs)->state);
}
block_free(*cs, sizeof(codec_state));
*cs = NULL;
} else {
debug_msg("Destroying corrupted codec\n");
abort();
}
}
int
codec_decode(codec_state *cs,
coded_unit *in,
coded_unit *out)
{
const codec_format_t *cf;
codec_id_t id;
uint16_t ifs, fmt, rate, channels;
int success;
assert(cs != NULL);
assert(out != NULL);
assert(in != NULL);
id = cs->id;
assert(in->id == cs->id);
assert(codec_is_native_coding(in->id) == FALSE);
ifs = CODEC_GET_IFS_INDEX(id);
fmt = CODEC_GET_FMT_INDEX(id);
/* Setup outgoing data block */
cf = codec_get_format(id);
assert(out->state == NULL);
assert(out->data == NULL);
rate = (uint32_t)cf->format.sample_rate;
channels = (uint16_t)cf->format.channels;
out->id = codec_get_native_coding(rate, channels);
out->data_len = cf->format.bytes_per_block;
out->data = (u_char*)block_alloc(out->data_len);
/* Decode */
xmemchk();
success = codec_table[ifs].cx_decode(fmt, cs->state, in, (sample*)out->data);
xmemchk();
return success;
}
int
codec_decoder_can_repair (codec_id_t id)
{
uint16_t ifs;
assert(codec_id_is_valid(id));
ifs = CODEC_GET_IFS_INDEX(id);
if (codec_table[ifs].cx_repair) {
return TRUE;
} else {
return FALSE;
}
}
int
codec_decoder_repair(codec_id_t id, codec_state *cs,
uint16_t consec_missing,
coded_unit *prev,
coded_unit *miss,
coded_unit *next)
{
uint16_t ifs, fmt;
assert(codec_id_is_valid(id));
assert(id == cs->id);
ifs = CODEC_GET_IFS_INDEX(id);
fmt = CODEC_GET_FMT_INDEX(id);
if (miss->id != id) {
debug_msg("Wrong previous unit supplied for repair. Probably a transition in stream.\n");
return FALSE;
}
miss->id = prev->id;
assert(codec_table[ifs].cx_repair != NULL);
return codec_table[ifs].cx_repair(fmt,
cs->state,
consec_missing,
prev, miss, next);
}
uint32_t
codec_peek_frame_size(codec_id_t id, u_char *data, uint16_t blk_len)
{
uint16_t ifs, fmt;
assert(codec_id_is_valid(id));
ifs = CODEC_GET_IFS_INDEX(id);
fmt = CODEC_GET_FMT_INDEX(id);
if (codec_table[ifs].cx_peek_size) {
return codec_table[ifs].cx_peek_size(fmt, data, (int)blk_len);
} else {
const codec_format_t *cf = codec_get_format(id);
return cf->mean_coded_frame_size;
}
}
/* Codec clear coded unit - not sure where this should go - away? :-) */
int
codec_clear_coded_unit(coded_unit *u)
{
if (u->state_len) {
assert(u->state != NULL);
block_free(u->state, u->state_len);
}
if (u->data_len) {
assert(u->data != NULL);
block_free(u->data, u->data_len);
}
memset(u, 0, sizeof(coded_unit));
return TRUE;
}
/* RTP related things like mapping and payload sanity checking ***************/
int
payload_is_valid(u_char pt)
{
/* Per rfc1890.txt (actually 72-95 is unassigned, but we use it anyway */
if (pt < 29 || (pt>=72 && pt <=127)) return TRUE;
return FALSE;
}
/* RTP Mapping interface -
* 2 maps one from payload to codec id and the other from codec id to
* to payload.
*/
#define NUM_PAYLOADS 128
static codec_id_t payload_map[NUM_PAYLOADS];
static u_char *codec_map[NUM_CODEC_INTERFACES];
static void
codec_map_init()
{
uint16_t i,j;
const codec_format_t *fmt;
memset(payload_map, 0, NUM_PAYLOADS * sizeof(codec_id_t));
for(i = 0; i < NUM_CODEC_INTERFACES; i++) {
codec_map[i] = (u_char*)xmalloc(num_fmts_supported[i]);
memset(codec_map[i], CODEC_PAYLOAD_DYNAMIC, num_fmts_supported[i]);
for(j = 0; j < num_fmts_supported[i]; j++) {
fmt = codec_table[i].cx_get_format(j);
if (fmt->default_pt == CODEC_PAYLOAD_DYNAMIC) {
continue;
}
codec_map_payload(CODEC_MAKE_ID(i,j), fmt->default_pt);
}
}
}
static void
codec_map_exit()
{
uint32_t i;
for(i = 0; i < NUM_CODEC_INTERFACES; i++) {
xfree(codec_map[i]);
}
}
int
codec_map_payload(codec_id_t id, u_char pt)
{
if (payload_is_valid(pt) && codec_id_is_valid(id)) {
if (payload_map[pt] != 0) {
codec_unmap_payload(id, pt);
}
payload_map[pt] = id;
codec_map[CODEC_GET_IFS_INDEX(id)][CODEC_GET_FMT_INDEX(id)] = pt;
return TRUE;
}
#ifdef DEBUG
{
const codec_format_t *cf;
cf = codec_get_format(id);
debug_msg("Failed to map payload for %s\n", cf->long_name);
}
#endif /* DEBUG */
return FALSE;
}
u_char
codec_get_payload(codec_id_t id)
{
u_char pt;
assert(codec_id_is_valid(id));
pt = codec_map[CODEC_GET_IFS_INDEX(id)][CODEC_GET_FMT_INDEX(id)];
if (payload_is_valid(pt)) {
assert(codec_get_by_payload(pt) == id);
return pt;
}
return CODEC_PAYLOAD_DYNAMIC;
}
int
codec_unmap_payload(codec_id_t id, u_char pt)
{
if (payload_is_valid(pt) &&
codec_id_is_valid(id) &&
payload_map[pt] == id) {
payload_map[pt] = 0;
codec_map[CODEC_GET_IFS_INDEX(id)][CODEC_GET_FMT_INDEX(id)] = CODEC_PAYLOAD_DYNAMIC;
return TRUE;
}
debug_msg("Failed to unmap payload\n");
return FALSE;
}
codec_id_t
codec_get_by_payload (u_char pt)
{
if (payload_is_valid(pt)) {
#ifdef DEBUG
if (payload_map[pt] == 0) {
debug_msg("No codec for payload %d\n", pt);
}
#endif
return payload_map[pt];
} else {
debug_msg("codec_get_by_payload - invalid payload (%d)\n", pt);
return 0;
}
}
/* For compatibility only */
codec_id_t
codec_get_first_mapped_with(uint32_t sample_rate, uint16_t channels)
{
const codec_format_t *cf;
int pt;
for(pt = 0; pt < NUM_PAYLOADS; pt++) {
if (payload_map[pt]) {
cf = codec_get_format(payload_map[pt]);
if (cf->format.sample_rate == sample_rate &&
cf->format.channels == channels) {
return payload_map[pt];
}
}
}
debug_msg("No mapped codecs compatible (%d, %d)\n",
sample_rate, channels);
return 0;
}
codec_id_t
codec_get_by_name(const char *name)
{
const codec_format_t *cf;
uint16_t ifs, fmt;
for(ifs = 0; ifs < NUM_CODEC_INTERFACES; ifs++) {
for(fmt = 0; fmt < num_fmts_supported[ifs]; fmt++) {
cf = codec_table[ifs].cx_get_format(fmt);
if (!strcasecmp(cf->long_name, name)) {
return CODEC_MAKE_ID(ifs,fmt);
}
}
}
return 0;
}
codec_id_t
codec_get_matching(const char *short_name, uint32_t freq, uint16_t channels)
{
/* This has been changed to try really hard to find a matching codec.
* The reason is that it's now called as part of the command-line
* parsing, and so has to cope with user entered codec names. Also, it
* should recognise the names sdr gives the codecs, for compatibility
* with rat-v3.0. [csp]
*/
/* This is not quite as inefficient as it looks, since stage 1 will
* almost always find a match.
*/
const codec_format_t *cf = NULL;
codec_id_t cid = 0;
uint32_t i, codecs;
char *long_name;
/* Stage 1: Try the designated short names... */
codecs = codec_get_number_of_codecs();
for(i = 0; i < codecs; i++) {
cid = codec_get_codec_number(i);
cf = codec_get_format(cid);
if (cf->format.sample_rate == freq &&
cf->format.channels == channels &&
!strcasecmp(short_name, cf->short_name)) {
return cid;
}
}
/* Stage 2: Try to generate a matching name... */
long_name = (char *) xmalloc(strlen(short_name) + 12);
sprintf(long_name, "%s-%dK-%s", short_name, freq/1000, channels==1?"MONO":"STEREO");
for(i = 0; i < codecs; i++) {
cid = codec_get_codec_number(i);
cf = codec_get_format(cid);
if (cf->format.sample_rate == freq &&
cf->format.channels == channels &&
!strcasecmp(long_name, cf->long_name)) {
xfree(long_name);
return cid;
}
}
/* Stage 3: Nasty hack... PCM->PCMU for compatibility with sdr
* and old rat versions
*/
if (strncasecmp(short_name, "pcm", 3) == 0) {
sprintf(long_name, "PCMU-%dK-%s", freq/1000, channels==1?"MONO":"STEREO");
for(i = 0; i < codecs; i++) {
cid = codec_get_codec_number(i);
cf = codec_get_format(cid);
if (cf->format.sample_rate == freq &&
cf->format.channels == channels &&
!strcasecmp(long_name, cf->long_name)) {
xfree(long_name);
return cid;
}
}
}
xfree(long_name);
debug_msg("Unable to find codec \"%s\" at rate %d channels %d\n", short_name, freq, channels);
return 0;
}
/* These constants are what are supported as native codings */
static uint16_t sampling_rates[] = {8000, 11025, 16000, 22050, 32000, 44100, 48000};
static uint16_t num_sampling_rates = sizeof(sampling_rates)/sizeof(sampling_rates[0]);
static uint16_t max_channels = 2;
/* The following three functions are a hack so we can have encode and
* decode functions take coded_units as input and output. This makes
* paths cleaner since we don't have two data types for coded and raw
* units. */
codec_id_t
codec_get_native_coding(uint32_t sample_rate, uint16_t channels)
{
codec_id_t cid;
uint32_t i, index;
for (i = 0; i < num_sampling_rates; i++) {
if (sampling_rates[i] == sample_rate) {
break;
}
}
assert(i != num_sampling_rates);
assert(channels <= max_channels);
index = i * max_channels + (channels - 1);
/* There is no codec corresponding to this but make it
* have right form we set it interfaces to the number
* of interfaces, i.e. one more than is legal.
*/
cid = CODEC_MAKE_ID(NUM_CODEC_INTERFACES, index);
return cid;
}
int
codec_is_native_coding(codec_id_t cid)
{
return (CODEC_GET_IFS_INDEX(cid) == NUM_CODEC_INTERFACES &&
CODEC_GET_FMT_INDEX(cid) < num_sampling_rates * max_channels);
}
int
codec_get_native_info(codec_id_t cid,
uint32_t *p_rate,
uint16_t *p_channels)
{
uint32_t i, c, index;
if (codec_is_native_coding(cid)) {
index = CODEC_GET_FMT_INDEX(cid);
/* Calculate and verify index in table of acceptable rates */
i = index / max_channels;
if (p_rate != NULL) {
*p_rate = sampling_rates[i];
}
/* Calculate and verify number of channels */
c = (index % max_channels) + 1;
if (p_channels != NULL) {
*p_channels = (uint16_t)c;
}
return TRUE;
}
return FALSE;
}
/* Layered codecs ***********************************************************/
uint8_t
codec_can_layer(codec_id_t id)
{
uint16_t ifs;
ifs = CODEC_GET_IFS_INDEX(id);
assert(codec_id_is_valid(id));
assert(ifs < NUM_CODEC_INTERFACES);
if (codec_table[ifs].cx_can_layer) {
return codec_table[ifs].cx_can_layer();
}
return 1;
}
int
codec_get_layer(codec_id_t id, coded_unit *cu_whole, uint8_t layer, uint16_t *markers, coded_unit *cu_layer)
{
uint16_t ifs, fmt;
ifs = CODEC_GET_IFS_INDEX(id);
fmt = CODEC_GET_FMT_INDEX(id);
if (codec_table[ifs].cx_get_layer) {
return codec_table[ifs].cx_get_layer(fmt, cu_whole, layer, markers, cu_layer);
}
return FALSE;
}
int
codec_combine_layer (codec_id_t id, coded_unit *cu_layer, coded_unit *cu_whole, uint8_t nelem, uint16_t *markers)
{
uint16_t ifs, fmt;
ifs = CODEC_GET_IFS_INDEX(id);
fmt = CODEC_GET_FMT_INDEX(id);
assert(codec_table[ifs].cx_combine_layer);
return codec_table[ifs].cx_combine_layer(fmt, cu_layer, cu_whole, nelem, markers);
}
|