g192_bit_stream.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * broadvoice - a library for the BroadVoice 16 and 32 codecs
  3. *
  4. * g192_bit_stream.c
  5. *
  6. * Copyright 2008-2009 Steve Underwood <steveu@coppice.org>
  7. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License version 2.1,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. /*! \file */
  24. #if defined(HAVE_CONFIG_H)
  25. #include "config.h"
  26. #endif
  27. #include <inttypes.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <audiofile.h>
  31. #include "g192_bit_stream.h"
  32. #if !defined(FALSE)
  33. #define FALSE 0
  34. #endif
  35. #if !defined(TRUE)
  36. #define TRUE (!FALSE)
  37. #endif
  38. enum
  39. {
  40. G192_FRAME_ERASURE = 0x6B20,
  41. G192_FRAME_SYNC_1 = 0x6B21,
  42. G192_FRAME_SYNC_2 = 0x6B22,
  43. G192_FRAME_SYNC_3 = 0x6B23,
  44. G192_FRAME_SYNC_4 = 0x6B24,
  45. G192_FRAME_SYNC_5 = 0x6B25,
  46. G192_FRAME_SYNC_6 = 0x6B26,
  47. G192_FRAME_SYNC_7 = 0x6B27,
  48. G192_FRAME_SYNC_8 = 0x6B28,
  49. G192_FRAME_SYNC_9 = 0x6B29,
  50. G192_FRAME_SYNC_10 = 0x6B2A,
  51. G192_FRAME_SYNC_11 = 0x6B2B,
  52. G192_FRAME_SYNC_12 = 0x6B2C,
  53. G192_FRAME_SYNC_13 = 0x6B2D,
  54. G192_FRAME_SYNC_14 = 0x6B2E,
  55. G192_FRAME_SYNC_15 = 0x6B2F,
  56. G192_HARD_ZERO = 0x7F,
  57. G192_INDETERMINATE = 0x00,
  58. G192_HARD_ONE = 0x81
  59. };
  60. int itu_codec_bitstream_write(const uint8_t out_data[],
  61. int number_of_bits,
  62. int mode,
  63. FILE *fp_bitstream)
  64. {
  65. int i;
  66. int j;
  67. int bit_count;
  68. int number_of_bytes;
  69. uint8_t packed_word;
  70. int16_t out_array[2 + number_of_bits + 7];
  71. number_of_bytes = (number_of_bits + 7)/8;
  72. if (mode == ITU_CODEC_BITSTREAM_PACKED)
  73. {
  74. return fwrite(out_data, 1, number_of_bytes, fp_bitstream);
  75. }
  76. j = 0;
  77. out_array[j++] = G192_FRAME_SYNC_1;
  78. out_array[j++] = number_of_bits;
  79. for (i = 0; i < number_of_bytes; i++)
  80. {
  81. packed_word = out_data[i];
  82. for (bit_count = 7; bit_count >= 0; bit_count--)
  83. out_array[j++] = ((packed_word >> bit_count) & 1) ? G192_HARD_ONE : G192_HARD_ZERO;
  84. }
  85. return fwrite(out_array, sizeof(int16_t), number_of_bits + 2, fp_bitstream);
  86. }
  87. /*- End of function --------------------------------------------------------*/
  88. int itu_codec_bitstream_read(uint8_t in_data[],
  89. int16_t *erasure,
  90. int number_of_bits,
  91. int mode,
  92. FILE *fp_bitstream)
  93. {
  94. int i;
  95. int j;
  96. int bit_pos;
  97. int nsamp;
  98. int limit;
  99. int rem;
  100. int len;
  101. int erased_frame;
  102. int16_t packed_word;
  103. int16_t bit;
  104. int16_t in_array[2 + number_of_bits];
  105. *erasure = FALSE;
  106. if (mode == ITU_CODEC_BITSTREAM_PACKED)
  107. {
  108. nsamp = fread(in_data, 1, number_of_bits/8, fp_bitstream);
  109. if (nsamp <= 0)
  110. return -1;
  111. return nsamp*8;
  112. }
  113. nsamp = fread(in_array, sizeof(int16_t), 2, fp_bitstream);
  114. if (nsamp < 2)
  115. return -1;
  116. if (in_array[0] < G192_FRAME_ERASURE || in_array[0] > G192_FRAME_SYNC_15)
  117. {
  118. *erasure = TRUE;
  119. return 0;
  120. }
  121. erased_frame = (in_array[0] == G192_FRAME_ERASURE);
  122. len = in_array[1];
  123. if (len > number_of_bits)
  124. {
  125. *erasure = TRUE;
  126. return 0;
  127. }
  128. nsamp = fread(in_array, sizeof(int16_t), len, fp_bitstream);
  129. if (nsamp != len)
  130. {
  131. *erasure = TRUE;
  132. return nsamp;
  133. }
  134. limit = (nsamp + 7)/8;
  135. for (i = 0, j = 0; i < limit; i++)
  136. {
  137. packed_word = 0;
  138. rem = (i == (limit - 1)) ? (limit*8 - nsamp) : 0;
  139. for (bit_pos = 7; bit_pos >= rem; bit_pos--)
  140. {
  141. bit = in_array[j++];
  142. if (bit >= 0x0001 && bit <= G192_HARD_ZERO)
  143. {
  144. /* Its a zero */
  145. }
  146. else if (bit >= G192_HARD_ONE && bit <= 0x00FF)
  147. {
  148. /* Its a one */
  149. packed_word |= (1 << bit_pos);
  150. }
  151. else
  152. {
  153. /* Bad bit */
  154. *erasure = 1;
  155. }
  156. }
  157. in_data[i] = packed_word;
  158. }
  159. if (erased_frame)
  160. *erasure = TRUE;
  161. return nsamp;
  162. }
  163. /*- End of function --------------------------------------------------------*/
  164. /*- End of file ------------------------------------------------------------*/