<commit_before>/*
 *  MAST: Multicast Audio Streaming Toolkit
 *
 *  Copyright (C) 2003-2007 <NAME> <<EMAIL>>
 *
 *  This program 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 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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.
 *
 *  $Id$
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>

#include <sys/types.h>
#include <sys/stat.h>

#include "config.h"
#include "codecs.h"
#include "mast.h"


// Table of codecs
static struct {
	char* mime_subtype;
	int (*init)( mast_codec_t* codec );
} mast_codecs[] = {
//	{ "DVI4",	mast_init_dvi4 },
	{ "GSM",	mast_init_gsm },
	{ "L16",	mast_init_l16 },
	{ "LPC",	mast_init_lpc },
	{ "PCMA",	mast_init_pcma },
	{ "PCMU",	mast_init_pcmu },
	{ "MPA",	mast_init_mpa },
	{ NULL, 	NULL }
};





// Find a codec and initialise it
mast_codec_t* mast_codec_init( const char* subtype, int samplerate, int channels )
{
	mast_codec_t* codec = NULL;
	int found_codec = FALSE;
	int i;


	// Allocate memory for the codec
	codec = malloc( sizeof(mast_codec_t) );
	if (codec==NULL) {
		MAST_FATAL("Failed to allocate memory for mast_codec_t data structure");
	}
	
	// Zero the memory and fill in
	memset( codec, 0, sizeof(mast_codec_t) );
	codec->channels = channels;
	codec->samplerate = samplerate;
	codec->subtype = subtype;

	
	// Search for a matching codec
	for(i=0; mast_codecs[i].mime_subtype; i++) {
		if (strcasecmp( subtype, mast_codecs[i].mime_subtype ) == 0) {
			// Found a matching codec
			if (mast_codecs[i].init( codec )) {
				MAST_DEBUG( "Failed to initialise codec" );
				free( codec );
				return NULL;
			}
			found_codec = TRUE;
			break;
		}
	}
	
	// Didn't find match
	if(!found_codec) {
		MAST_ERROR( "Failed to find codec for MIME type audio/%s", subtype );
		free(codec);
		return NULL;
	}
	
	
	
	// FIXME: set parameters
	
	
	// Prepare to start encoding/decoding
	if (codec->prepare) {
		if (codec->prepare( codec )) {
			MAST_WARNING( "Codec failed to prepare to encode/decode." );
			mast_codec_deinit(codec);
			return NULL;
		}
	}	
	
	return codec;
}


// Get the number of samples per packet for the current parameters
int mast_codec_samples_per_packet( mast_codec_t* codec, int max_bytes )
{
	int samples_per_packet = 0;

	if (codec->samples_per_packet) {
		samples_per_packet = codec->samples_per_packet( codec, max_bytes );
	} else {
	
		// Assume worst case (4 bytes per sample)
		samples_per_packet = ( max_bytes / (4*SAMPLES_PER_UNIT*codec->channels));

	}
	
	MAST_DEBUG("samples_per_packet=%d", samples_per_packet );
	<commit_msg>Decided that it is best not to guess the number of samples per packet if codec can't calculate it.<commit_after>/*
 *  MAST: Multicast Audio Streaming Toolkit
 *
 *  Copyright (C) 2003-2007 <NAME> <<EMAIL>>
 *
 *  This program 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 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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.
 *
 *  $Id$
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>

#include <sys/types.h>
#include <sys/stat.h>

#include "config.h"
#include "codecs.h"
#include "mast.h"


// Table of codecs
static struct {
	char* mime_subtype;
	int (*init)( mast_codec_t* codec );
} mast_codecs[] = {
//	{ "DVI4",	mast_init_dvi4 },
	{ "GSM",	mast_init_gsm },
	{ "L16",	mast_init_l16 },
	{ "LPC",	mast_init_lpc },
	{ "PCMA",	mast_init_pcma },
	{ "PCMU",	mast_init_pcmu },
	{ "MPA",	mast_init_mpa },
	{ NULL, 	NULL }
};





// Find a codec and initialise it
mast_codec_t* mast_codec_init( const char* subtype, int samplerate, int channels )
{
	mast_codec_t* codec = NULL;
	int found_codec = FALSE;
	int i;


	// Allocate memory for the codec
	codec = malloc( sizeof(mast_codec_t) );
	if (codec==NULL) {
		MAST_FATAL("Failed to allocate memory for mast_codec_t data structure");
	}
	
	// Zero the memory and fill in
	memset( codec, 0, sizeof(mast_codec_t) );
	codec->channels = channels;
	codec->samplerate = samplerate;
	codec->subtype = subtype;

	
	// Search for a matching codec
	for(i=0; mast_codecs[i].mime_subtype; i++) {
		if (strcasecmp( subtype, mast_codecs[i].mime_subtype ) == 0) {
			// Found a matching codec
			if (mast_codecs[i].init( codec )) {
				MAST_DEBUG( "Failed to initialise codec" );
				free( codec );
				return NULL;
			}
			found_codec = TRUE;
			break;
		}
	}
	
	// Didn't find match
	if(!found_codec) {
		MAST_ERROR( "Failed to find codec for MIME type audio/%s", subtype );
		free(codec);
		return NULL;
	}
	
	
	
	// FIXME: set parameters
	
	
	// Prepare to start encoding/decoding
	if (codec->prepare) {
		if (codec->prepare( codec )) {
			MAST_WARNING( "Codec failed to prepare to encode/decode." );
			mast_codec_deinit(codec);
			return NULL;
		}
	}	
	
	return codec;
}


// Get the number of samples per packet for the current parameters
int mast_codec_samples_per_packet( mast_codec_t* codec, int max_bytes )
{
	int samples_per_packet = 0;

	if (codec->samples_per_packet) {
		samples_per_packet = codec->samples_per_packet( codec, max_bytes );
	} else {
		MAST_ERROR("Codec does not support calculating the number of samples per packet");
		return -1;
	}
	
	MAST_DEBUG("samples_per_packet=%d", samples_per_packet );
	
{'fraction_non_alphanumeric': 0.12124183006535948, 'fraction_numerical': 0.006699346405228758, 'mean_word_length': 3.7048424289008457, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2072502', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2167, 'n_words': 653}