broadcast.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. ** Copyright (C) 2006-2016 Erik de Castro Lopo <[email protected]>
  3. ** Copyright (C) 2006 Paul Davis <[email protected]>
  4. **
  5. ** This program is free software; you can redistribute it and/or modify
  6. ** it under the terms of the GNU Lesser General Public License as published by
  7. ** the Free Software Foundation; either version 2.1 of the License, or
  8. ** (at your option) any later version.
  9. **
  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 Lesser General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU Lesser 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.
  18. */
  19. #include "sfconfig.h"
  20. #include <stdio.h>
  21. #include <stddef.h>
  22. #include <string.h>
  23. #include "common.h"
  24. static int gen_coding_history (char * added_history, int added_history_max, const SF_INFO * psfinfo) ;
  25. static inline size_t
  26. bc_min_size (const SF_BROADCAST_INFO* info)
  27. { if (info == NULL)
  28. return 0 ;
  29. return offsetof (SF_BROADCAST_INFO, coding_history) + info->coding_history_size ;
  30. } /* bc_min_size */
  31. SF_BROADCAST_INFO_16K*
  32. broadcast_var_alloc (void)
  33. { return calloc (1, sizeof (SF_BROADCAST_INFO_16K)) ;
  34. } /* broadcast_var_alloc */
  35. int
  36. broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * info, size_t datasize)
  37. { size_t len ;
  38. if (info == NULL)
  39. return SF_FALSE ;
  40. if (bc_min_size (info) > datasize)
  41. { psf->error = SFE_BAD_BROADCAST_INFO_SIZE ;
  42. return SF_FALSE ;
  43. } ;
  44. if (datasize >= sizeof (SF_BROADCAST_INFO_16K))
  45. { psf->error = SFE_BAD_BROADCAST_INFO_TOO_BIG ;
  46. return SF_FALSE ;
  47. } ;
  48. if (psf->broadcast_16k == NULL)
  49. { if ((psf->broadcast_16k = broadcast_var_alloc ()) == NULL)
  50. { psf->error = SFE_MALLOC_FAILED ;
  51. return SF_FALSE ;
  52. } ;
  53. } ;
  54. /* Only copy the first part of the struct. */
  55. memcpy (psf->broadcast_16k, info, offsetof (SF_BROADCAST_INFO, coding_history)) ;
  56. psf_strlcpy_crlf (psf->broadcast_16k->coding_history, info->coding_history, sizeof (psf->broadcast_16k->coding_history), datasize - offsetof (SF_BROADCAST_INFO, coding_history)) ;
  57. len = strlen (psf->broadcast_16k->coding_history) ;
  58. if (len > 0 && psf->broadcast_16k->coding_history [len - 1] != '\n')
  59. psf_strlcat (psf->broadcast_16k->coding_history, sizeof (psf->broadcast_16k->coding_history), "\r\n") ;
  60. if (psf->file.mode == SFM_WRITE)
  61. { char added_history [256] ;
  62. gen_coding_history (added_history, sizeof (added_history), &(psf->sf)) ;
  63. psf_strlcat (psf->broadcast_16k->coding_history, sizeof (psf->broadcast_16k->coding_history), added_history) ;
  64. } ;
  65. /* Force coding_history_size to be even. */
  66. len = strlen (psf->broadcast_16k->coding_history) ;
  67. len += (len & 1) ? 1 : 0 ;
  68. psf->broadcast_16k->coding_history_size = (uint32_t) len ;
  69. /* Currently writing this version. */
  70. psf->broadcast_16k->version = 2 ;
  71. return SF_TRUE ;
  72. } /* broadcast_var_set */
  73. int
  74. broadcast_var_get (SF_PRIVATE *psf, SF_BROADCAST_INFO * data, size_t datasize)
  75. { size_t size ;
  76. if (psf->broadcast_16k == NULL)
  77. return SF_FALSE ;
  78. size = SF_MIN (datasize, bc_min_size ((const SF_BROADCAST_INFO *) psf->broadcast_16k)) ;
  79. memcpy (data, psf->broadcast_16k, size) ;
  80. return SF_TRUE ;
  81. } /* broadcast_var_get */
  82. /*------------------------------------------------------------------------------
  83. */
  84. static int
  85. gen_coding_history (char * added_history, int added_history_max, const SF_INFO * psfinfo)
  86. { char chnstr [16] ;
  87. int count, width ;
  88. /*
  89. ** From : http://www.sr.se/utveckling/tu/bwf/docs/codhist2.htm
  90. **
  91. ** Parameter Variable string <allowed option> Unit
  92. ** ==========================================================================================
  93. ** Coding Algorithm A=<ANALOGUE, PCM, MPEG1L1, MPEG1L2, MPEG1L3,
  94. ** MPEG2L1, MPEG2L2, MPEG2L3>
  95. ** Sampling frequency F=<11000,22050,24000,32000,44100,48000> [Hz]
  96. ** Bit-rate B=<any bit-rate allowed in MPEG 2 (ISO/IEC [kbit/s per channel]
  97. ** 13818-3)>
  98. ** Word Length W=<8, 12, 14, 16, 18, 20, 22, 24> [bits]
  99. ** Mode M=<mono, stereo, dual-mono, joint-stereo>
  100. ** Text, free string T=<a free ASCII-text string for in house use.
  101. ** This string should contain no commas (ASCII
  102. ** 2Chex). Examples of the contents: ID-No; codec
  103. ** type; A/D type>
  104. */
  105. switch (psfinfo->channels)
  106. { case 0 :
  107. return SF_FALSE ;
  108. case 1 :
  109. psf_strlcpy (chnstr, sizeof (chnstr), "mono") ;
  110. break ;
  111. case 2 :
  112. psf_strlcpy (chnstr, sizeof (chnstr), "stereo") ;
  113. break ;
  114. default :
  115. snprintf (chnstr, sizeof (chnstr), "%dchn", psfinfo->channels) ;
  116. break ;
  117. } ;
  118. switch (SF_CODEC (psfinfo->format))
  119. { case SF_FORMAT_PCM_U8 :
  120. case SF_FORMAT_PCM_S8 :
  121. width = 8 ;
  122. break ;
  123. case SF_FORMAT_PCM_16 :
  124. width = 16 ;
  125. break ;
  126. case SF_FORMAT_PCM_24 :
  127. width = 24 ;
  128. break ;
  129. case SF_FORMAT_PCM_32 :
  130. width = 32 ;
  131. break ;
  132. case SF_FORMAT_FLOAT :
  133. width = 24 ; /* Bits in the mantissa + 1 */
  134. break ;
  135. case SF_FORMAT_DOUBLE :
  136. width = 53 ; /* Bits in the mantissa + 1 */
  137. break ;
  138. case SF_FORMAT_ULAW :
  139. case SF_FORMAT_ALAW :
  140. width = 12 ;
  141. break ;
  142. default :
  143. width = 42 ;
  144. break ;
  145. } ;
  146. count = snprintf (added_history, added_history_max,
  147. "A=PCM,F=%d,W=%d,M=%s,T=%s-%s\r\n",
  148. psfinfo->samplerate, width, chnstr, PACKAGE_NAME, PACKAGE_VERSION) ;
  149. if (count >= added_history_max)
  150. return 0 ;
  151. return count ;
  152. } /* gen_coding_history */