src/ : Remove old broadcast extention handling.

This commit is contained in:
Erik de Castro Lopo 2008-10-26 20:47:57 +11:00
parent ba76501702
commit ab2e63bd21
5 changed files with 108 additions and 279 deletions

View File

@ -12,6 +12,9 @@
* src/common.h src/sndfile.c
Add error number SFE_BAD_BROADCAST_INFO_SIZE.
* src/*
Reimplement handling of broadcast extentioon chunk in WAV/WAVEX files.
2008-10-25 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
* programs/sndfile-metadata-get.c programs/sndfile-info.c

View File

@ -24,158 +24,132 @@
#include "common.h"
static void strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax) ;
static int gen_coding_history (char * added_history, size_t added_history_max, const SF_INFO * psfinfo) ;
/*
** Allocate and initialize a broadcast info structure.
*/
static inline size_t
bc_min_size (const SF_BROADCAST_INFO* info)
{ if (info == NULL)
return 0 ;
SF_BROADCAST_INFO*
broadcast_info_alloc (void)
{ SF_BROADCAST_INFO* bext ;
return offsetof (SF_BROADCAST_INFO, coding_history) + info->coding_history_size ;
} /* broadcast_size */
if ((bext = calloc (1, sizeof (SF_BROADCAST_INFO))) == NULL)
return NULL ;
return bext ;
} /* broadcast_info_alloc */
static inline size_t
bc_var_coding_hist_size (const SF_BROADCAST_VAR* var)
{ return var->size - offsetof (SF_BROADCAST_VAR, binfo.coding_history) ;
} /* broadcast_size */
SF_BROADCAST_VAR*
broadcast_var_alloc (size_t datasize)
{ SF_BROADCAST_VAR * data ;
if ((data = calloc (1, datasize)) != NULL)
data->size = datasize ;
return data ;
} /* broadcast_var_alloc */
int
broadcast_info_copy (SF_BROADCAST_INFO* dst, const SF_BROADCAST_INFO* src)
{ memcpy (dst, src, sizeof (SF_BROADCAST_INFO)) ;
broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * info, size_t datasize)
{ char added_history [256] ;
int added_history_len, total_history_len ;
/* In case the src has the wrong line endings. */
strncpy_crlf (dst->coding_history, src->coding_history, sizeof (dst->coding_history), src->coding_history_size) ;
if (info == NULL)
return SF_FALSE ;
if (bc_min_size (info) > datasize)
{ psf->error = SFE_BAD_BROADCAST_INFO_SIZE ;
return SF_FALSE ;
} ;
added_history_len = gen_coding_history (added_history, sizeof (added_history), &(psf->sf)) ;
if (psf->broadcast_var != NULL
&& psf->broadcast_var->binfo.coding_history_size + added_history_len < datasize)
{ free (psf->broadcast_var) ;
psf->broadcast_var = NULL ;
} ;
if (psf->broadcast_var == NULL)
{ int size = datasize + added_history_len + 512 ;
psf->broadcast_var = calloc (1, size) ;
psf->broadcast_var->size = size ;
} ;
memcpy (&(psf->broadcast_var->binfo), info, offsetof (SF_BROADCAST_INFO, coding_history)) ;
strncpy_crlf (psf->broadcast_var->binfo.coding_history, info->coding_history, bc_var_coding_hist_size (psf->broadcast_var), info->coding_history_size) ;
total_history_len = strlen (psf->broadcast_var->binfo.coding_history) + added_history_len ;
if (psf->mode == SFM_WRITE)
strncat (psf->broadcast_var->binfo.coding_history, added_history, strlen (added_history)) ;
psf->broadcast_var->binfo.coding_history_size = strlen (psf->broadcast_var->binfo.coding_history) ;
/* Fore coding_history_size to be even. */
psf->broadcast_var->binfo.coding_history_size += (psf->broadcast_var->binfo.coding_history_size & 1) ? 1 : 0 ;
/* Currently writing this version. */
dst->version = 1 ;
psf->broadcast_var->binfo.version = 1 ;
return SF_TRUE ;
} /* broadcast_info_copy */
} /* broadcast_var_set */
int
broadcast_add_coding_history (SF_BROADCAST_INFO* bext, unsigned int channels, unsigned int samplerate, int format)
{ const char *newline ;
char chnstr [16], history [2 * sizeof (bext->coding_history)] ;
int count, width, current_history_len ;
broadcast_var_get (SF_PRIVATE *psf, SF_BROADCAST_INFO * data, size_t datasize)
{ size_t size ;
/*
** From : http://www.sr.se/utveckling/tu/bwf/docs/codhist2.htm
**
** Parameter Variable string <allowed option> Unit
** ==========================================================================================
** Coding Algorithm A=<ANALOGUE, PCM, MPEG1L1, MPEG1L2, MPEG1L3,
** MPEG2L1, MPEG2L2, MPEG2L3>
** Sampling frequency F=<11000,22050,24000,32000,44100,48000> [Hz]
** Bit-rate B=<any bit-rate allowed in MPEG 2 (ISO/IEC [kbit/s per channel]
** 13818-3)>
** Word Length W=<8, 12, 14, 16, 18, 20, 22, 24> [bits]
** Mode M=<mono, stereo, dual-mono, joint-stereo>
** Text, free string T=<a free ASCII-text string for in house use.
** This string should contain no commas (ASCII
** 2Chex). Examples of the contents: ID-No; codec
** type; A/D type>
*/
if (psf->broadcast_var == NULL)
return SF_FALSE ;
switch (channels)
{ case 0 :
return SF_FALSE ;
size = SF_MIN (datasize, bc_min_size (&(psf->broadcast_var->binfo))) ;
case 1 :
strncpy (chnstr, "mono", sizeof (chnstr)) ;
break ;
case 2 :
strncpy (chnstr, "stereo", sizeof (chnstr)) ;
break ;
default :
LSF_SNPRINTF (chnstr, sizeof (chnstr), "%uchn", channels) ;
break ;
} ;
switch (SF_CODEC (format))
{ case SF_FORMAT_PCM_U8 :
case SF_FORMAT_PCM_S8 :
width = 8 ;
break ;
case SF_FORMAT_PCM_16 :
width = 16 ;
break ;
case SF_FORMAT_PCM_24 :
width = 24 ;
break ;
case SF_FORMAT_PCM_32 :
width = 32 ;
break ;
case SF_FORMAT_FLOAT :
width = 24 ; /* Bits in the mantissa + 1 */
break ;
case SF_FORMAT_DOUBLE :
width = 53 ; /* Bits in the mantissa + 1 */
break ;
case SF_FORMAT_ULAW :
case SF_FORMAT_ALAW :
width = 12 ;
break ;
default :
width = 42 ;
break ;
} ;
/* Make sure its a terminated C string. */
bext->coding_history [sizeof (bext->coding_history) - 1] = 0 ;
/* Note newlines in Coding History should be '\r\n' as per the URL above. */
if (bext->coding_history_size > 0)
strncpy_crlf (history, bext->coding_history, sizeof (history), bext->coding_history_size) ;
else
history [0] = 0 ;
current_history_len = strlen (history) ;
newline = "" ;
if (current_history_len != 0 && history [current_history_len - 1] != '\n')
newline = "\r\n" ;
count = LSF_SNPRINTF (history + current_history_len, sizeof (history) - current_history_len,
"%sA=PCM,F=%u,W=%hu,M=%s,T=%s-%s\r\n",
newline, samplerate, width, chnstr, PACKAGE, VERSION) ;
count += current_history_len ;
while (count >= SIGNED_SIZEOF (bext->coding_history))
{ /* Coding history is too long, delete oldest part. */
const char *cptr ;
/* Entries should be delimited by a newline, so find first newline. */
if ((cptr = strchr (history, '\n')) == NULL)
{ /* Ooops, fail to safe and leave a message. */
count = snprintf (history, sizeof (bext->coding_history), "Something went wrong!\n") ;
break ;
} ;
cptr ++ ;
count -= cptr - history ;
memmove (history, cptr, count) ;
} ;
/* Zero out end of history chunk. */
memset (history + count, 0, sizeof (history) - count) ;
/* Now copy from temporary storage to bext->coding_history. */
memcpy (bext->coding_history, history, sizeof (bext->coding_history)) ;
count += count & 1 ;
bext->coding_history_size = count ;
memcpy (data, &(psf->broadcast_var->binfo), size) ;
return SF_TRUE ;
} /* broadcast_add_coding_history */
} /* broadcast_var_set */
/*==============================================================================
/*------------------------------------------------------------------------------
** Strncpy which converts all line endings to CR/LF.
*/
#if 1
static void
strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax)
{ char * destend = dest + destmax - 1 ;
const char * srcend = src + srcmax - 1 ;
while (dest < destend && src < srcend)
{ if ((src [0] == '\r' && src [1] == '\n') || (src [0] == '\n' && src [1] == '\r'))
{ *dest++ = '\r' ;
*dest++ = '\n' ;
src += 2 ;
continue ;
} ;
if (src [0] == '\r')
{ *dest++ = '\r' ;
*dest++ = '\n' ;
src += 1 ;
continue ;
} ;
if (src [0] == '\n')
{ *dest++ = '\r' ;
*dest++ = '\n' ;
src += 1 ;
continue ;
} ;
*dest++ = *src++ ;
} ;
/* Make sure dest is terminated. */
*dest = 0 ;
} /* strncpy_crlf */
static int
gen_coding_history (char * added_history, size_t added_history_max, const SF_INFO * psfinfo)
@ -256,141 +230,3 @@ gen_coding_history (char * added_history, size_t added_history_max, const SF_INF
return count ;
} /* gen_coding_history */
static inline size_t
bc_min_size (const SF_BROADCAST_INFO* info)
{ if (info == NULL)
return 0 ;
return offsetof (SF_BROADCAST_INFO, coding_history) + info->coding_history_size ;
} /* broadcast_size */
static inline size_t
bc_var_coding_hist_size (const SF_BROADCAST_VAR* var)
{ return var->size - offsetof (SF_BROADCAST_VAR, binfo.coding_history) ;
} /* broadcast_size */
SF_BROADCAST_VAR*
broadcast_var_alloc (size_t datasize)
{ SF_BROADCAST_VAR * data ;
if ((data = calloc (1, datasize)) != NULL)
data->size = datasize ;
return data ;
} /* broadcast_var_alloc */
int
broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * info, size_t datasize)
{ char added_history [256] ;
int added_history_len, total_history_len ;
if (info == NULL)
return SF_FALSE ;
if (bc_min_size (info) > datasize)
{ psf->error = SFE_BAD_BROADCAST_INFO_SIZE ;
return SF_FALSE ;
} ;
added_history_len = gen_coding_history (added_history, sizeof (added_history), &(psf->sf)) ;
if (psf->broadcast_var != NULL
&& psf->broadcast_var->binfo.coding_history_size + added_history_len < datasize)
{ free (psf->broadcast_var) ;
psf->broadcast_var = NULL ;
} ;
if (psf->broadcast_var == NULL)
{ int size = datasize + added_history_len + 512 ;
psf->broadcast_var = calloc (1, size) ;
psf->broadcast_var->size = size ;
} ;
memcpy (&(psf->broadcast_var->binfo), info, offsetof (SF_BROADCAST_INFO, coding_history)) ;
strncpy_crlf (psf->broadcast_var->binfo.coding_history, info->coding_history, bc_var_coding_hist_size (psf->broadcast_var), info->coding_history_size) ;
total_history_len = strlen (psf->broadcast_var->binfo.coding_history) + added_history_len ;
if (psf->mode == SFM_WRITE)
strncat (psf->broadcast_var->binfo.coding_history, added_history, strlen (added_history)) ;
psf->broadcast_var->binfo.coding_history_size = strlen (psf->broadcast_var->binfo.coding_history) ;
/* Fore coding_history_size to be even. */
psf->broadcast_var->binfo.coding_history_size += (psf->broadcast_var->binfo.coding_history_size & 1) ? 1 : 0 ;
/* Currently writing this version. */
psf->broadcast_var->binfo.version = 1 ;
return SF_TRUE ;
} /* broadcast_var_set */
int
broadcast_var_get (SF_PRIVATE *psf, SF_BROADCAST_INFO * data, size_t datasize)
{ size_t size ;
broadcast_info_to_var (psf) ;
if (psf->broadcast_var == NULL)
return SF_FALSE ;
size = SF_MIN (datasize, bc_min_size (&(psf->broadcast_var->binfo))) ;
memcpy (data, &(psf->broadcast_var->binfo), size) ;
return SF_TRUE ;
} /* broadcast_var_set */
void
broadcast_info_to_var (SF_PRIVATE *psf)
{
if (psf->broadcast_info != NULL)
{ broadcast_var_set (psf, psf->broadcast_info, sizeof (SF_BROADCAST_INFO)) ;
free (psf->broadcast_info) ;
psf->broadcast_info = NULL ;
} ;
} /* broadcast_info_to_var */
#endif
/*------------------------------------------------------------------------------
** Strncpy which converts all line endings to CR/LF.
*/
static void
strncpy_crlf (char *dest, const char *src, size_t destmax, size_t srcmax)
{ char * destend = dest + destmax - 1 ;
const char * srcend = src + srcmax - 1 ;
while (dest < destend && src < srcend)
{ if ((src [0] == '\r' && src [1] == '\n') || (src [0] == '\n' && src [1] == '\r'))
{ *dest++ = '\r' ;
*dest++ = '\n' ;
src += 2 ;
continue ;
} ;
if (src [0] == '\r')
{ *dest++ = '\r' ;
*dest++ = '\n' ;
src += 1 ;
continue ;
} ;
if (src [0] == '\n')
{ *dest++ = '\r' ;
*dest++ = '\n' ;
src += 1 ;
continue ;
} ;
*dest++ = *src++ ;
} ;
/* Make sure dest is terminated. */
*dest = 0 ;
} /* strncpy_crlf */

View File

@ -323,7 +323,6 @@ typedef struct sf_private_tag
SF_INSTRUMENT *instrument ;
/* Broadcast (EBU) Info */
SF_BROADCAST_INFO *broadcast_info ;
SF_BROADCAST_VAR *broadcast_var ;
/* Channel map data (if present) : an array of ints. */
@ -790,14 +789,9 @@ void psf_sanitize_string (char * cptr, int len) ;
/* Generate the current date as a string. */
void psf_get_date_str (char *str, int maxlen) ;
SF_BROADCAST_INFO* broadcast_info_alloc (void) ;
int broadcast_info_copy (SF_BROADCAST_INFO* dst, const SF_BROADCAST_INFO* src) ;
int broadcast_add_coding_history (SF_BROADCAST_INFO* bext, unsigned int channels, unsigned int samplerate, int format) ;
SF_BROADCAST_VAR* broadcast_var_alloc (size_t datasize) ;
int broadcast_var_set (SF_PRIVATE *psf, const SF_BROADCAST_INFO * data, size_t datasize) ;
int broadcast_var_get (SF_PRIVATE *psf, SF_BROADCAST_INFO * data, size_t datasize) ;
void broadcast_info_to_var (SF_PRIVATE *psf) ;
typedef struct

View File

@ -2428,9 +2428,6 @@ psf_close (SF_PRIVATE *psf)
if (psf->peak_info)
free (psf->peak_info) ;
if (psf->broadcast_info)
free (psf->broadcast_info) ;
if (psf->broadcast_var)
free (psf->broadcast_var) ;

View File

@ -575,7 +575,6 @@ wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
if ((error = wav_read_bext_chunk (psf, dword)))
return error ;
broadcast_info_to_var (psf) ;
break ;
case PAD_MARKER :