<commit_before>/*
 * Copyright 2008 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __MPF_CODEC_DESCRIPTOR_H__
#define __MPF_CODEC_DESCRIPTOR_H__

/**
 * @file mpf_codec_descriptor.h
 * @brief MPF Codec Descriptor
 */ 

#include <apr_tables.h>
#include "apt_string.h"
#include "mpf.h"

APT_BEGIN_EXTERN_C

/** Codec frame time base in msec */
#define CODEC_FRAME_TIME_BASE 10
/** Bytes per sample for linear pcm */
#define BYTES_PER_SAMPLE 2
/** Bits per sample for linear pcm */
#define BITS_PER_SAMPLE 16

/** Supported sampling rates */
typedef enum {
	MPF_SAMPLE_RATE_NONE  = 0x00,
	MPF_SAMPLE_RATE_8000  = 0x01,
	MPF_SAMPLE_RATE_16000 = 0x02,
	MPF_SAMPLE_RATE_32000 = 0x04,
	MPF_SAMPLE_RATE_48000 = 0x08,

	MPF_SAMPLE_RATE_SUPPORTED =	MPF_SAMPLE_RATE_8000 | MPF_SAMPLE_RATE_16000 | 
								MPF_SAMPLE_RATE_32000 | MPF_SAMPLE_RATE_48000
} mpf_sample_rates_e;

/** Codec descriptor declaration */
typedef struct mpf_codec_descriptor_t mpf_codec_descriptor_t;
/** Codec attributes declaration */
typedef struct mpf_codec_attribs_t mpf_codec_attribs_t;
/** Codec list declaration */
typedef struct mpf_codec_list_t mpf_codec_list_t;
/** Codec capabilities declaration */
typedef struct mpf_codec_capabilities_t mpf_codec_capabilities_t;
/** Codec frame declaration */
typedef struct mpf_codec_frame_t mpf_codec_frame_t;


/** Codec descriptor */
struct mpf_codec_descriptor_t {
	/** Payload type used in RTP packet */
	apr_byte_t   payload_type;
	/** Codec name */
	apt_str_t    name;
	/** Sampling rate */
	apr_uint16_t sampling_rate;
	/** Channel count */
	apr_byte_t   channel_count;
	/** Codec dependent additional format */
	apt_str_t    format;
	/**  Enabled/disabled state */
	apt_bool_t   enabled;
};

/** List of codec descriptors */
struct mpf_codec_list_t {
	/** Dynamic array of codec descriptors (mpf_codec_descriptor_t) */
	apr_array_header_t     *descriptor_arr;
	/** Preffered primary (audio/video codec) descriptor from descriptor_arr */
	mpf_codec_descriptor_t *primary_descriptor;
	/** Preffered named event (telephone-event) descriptor from descriptor_arr */
	mpf_codec_descriptor_t *event_descriptor;
};

/** Codec attributes */
struct mpf_codec_attribs_t {
	/** Codec name */
	apt_str_t  name;
	/** Bits per sample */
	apr_byte_t bits_per_sample;
	/** Supported sampling rates (mpf_sample_rates_e) */
	int        sample_rates;
};

/** List of codec attributes (capabilities) */
struct mpf_codec_capabilities_t {
	/** Dynamic array of codec attributes (mpf_codec_attrribs_t) */
	apr_array_header_t *attrib_arr;
	/** Allow/support named events */
	apt_bool_t          allow_named_events;
};

/** Codec frame */
struct mpf_codec_frame_t {
	/** Raw buffer, which may contain encoded or decoded data */
	void      *buffer;
	/** Buffer size */
	apr_size_t size;
};


/** Initialize codec descriptor */
static APR_INLINE void mpf_codec_descriptor_init(mpf_codec_descriptor_t *descriptor)
{
	descriptor->payload_type = 0;
	apt_string_reset(&descriptor->name);
	descriptor->sampling_rate = 0;
	descriptor->channel_count = 0;
	apt_string_reset(&descriptor->format);
	descriptor->enabled = TRUE;
}

/** Initialize codec descriptor */
static APR_INLINE  mpf_codec_descriptor_t* mpf_codec_descriptor_create(apr_pool_t *pool)
{
	mpf_codec_descriptor_t *descriptor = apr_palloc(pool,sizeof(mpf_codec_descriptor_t));
	mpf_codec_descriptor_init(descriptor);
	return descriptor;
}

/** Calculate encoded frame size in bytes */
static APR_INLINE apr_size_t mpf_codec_frame_size_calculate(const mpf_codec_descriptor_t *descriptor, const mpf_codec_attribs_t *attribs)
{
	return descriptor->channel_count * attribs->bits_per_sample * CODEC_FRAME_TIME_BASE * 
			descriptor->sampling_rate / 1000 / 8; /* 1000 - msec per sec, 8 - bits per byte */
}

/** Calculate samples of the frame (ts) */
static APR_INLINE apr_size_t mpf_codec_frame_samples_calculate(const mpf_codec_descriptor_t *descriptor)
{
	return descriptor->channel_count * CODEC_FRAME_TIME_BASE * descriptor->sampling_rate / 1000;
}

/** Calculate linear frame size in bytes */
static APR_INLINE apr_size_t mpf_codec_linear_frame_size_calculate(apr_uint16_t sampling_rate, apr_byte_t channel_count)
{
	return channel_count * BYTES_PER_SAMPLE * CODEC_FRAME_TIME_BASE * sampling_rate / 1000;
}



/** Reset list of codec descriptors */
static APR_INLINE void mpf_codec_list_reset(mpf_codec_list_t *codec_list)
{<commit_msg>Fixed C++ compile error in codec_descriptor.h (Issue-44, Thanks Vali)<commit_after>/*
 * Copyright 2008 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __MPF_CODEC_DESCRIPTOR_H__
#define __MPF_CODEC_DESCRIPTOR_H__

/**
 * @file mpf_codec_descriptor.h
 * @brief MPF Codec Descriptor
 */ 

#include <apr_tables.h>
#include "apt_string.h"
#include "mpf.h"

APT_BEGIN_EXTERN_C

/** Codec frame time base in msec */
#define CODEC_FRAME_TIME_BASE 10
/** Bytes per sample for linear pcm */
#define BYTES_PER_SAMPLE 2
/** Bits per sample for linear pcm */
#define BITS_PER_SAMPLE 16

/** Supported sampling rates */
typedef enum {
	MPF_SAMPLE_RATE_NONE  = 0x00,
	MPF_SAMPLE_RATE_8000  = 0x01,
	MPF_SAMPLE_RATE_16000 = 0x02,
	MPF_SAMPLE_RATE_32000 = 0x04,
	MPF_SAMPLE_RATE_48000 = 0x08,

	MPF_SAMPLE_RATE_SUPPORTED =	MPF_SAMPLE_RATE_8000 | MPF_SAMPLE_RATE_16000 | 
								MPF_SAMPLE_RATE_32000 | MPF_SAMPLE_RATE_48000
} mpf_sample_rates_e;

/** Codec descriptor declaration */
typedef struct mpf_codec_descriptor_t mpf_codec_descriptor_t;
/** Codec attributes declaration */
typedef struct mpf_codec_attribs_t mpf_codec_attribs_t;
/** Codec list declaration */
typedef struct mpf_codec_list_t mpf_codec_list_t;
/** Codec capabilities declaration */
typedef struct mpf_codec_capabilities_t mpf_codec_capabilities_t;
/** Codec frame declaration */
typedef struct mpf_codec_frame_t mpf_codec_frame_t;


/** Codec descriptor */
struct mpf_codec_descriptor_t {
	/** Payload type used in RTP packet */
	apr_byte_t   payload_type;
	/** Codec name */
	apt_str_t    name;
	/** Sampling rate */
	apr_uint16_t sampling_rate;
	/** Channel count */
	apr_byte_t   channel_count;
	/** Codec dependent additional format */
	apt_str_t    format;
	/**  Enabled/disabled state */
	apt_bool_t   enabled;
};

/** List of codec descriptors */
struct mpf_codec_list_t {
	/** Dynamic array of codec descriptors (mpf_codec_descriptor_t) */
	apr_array_header_t     *descriptor_arr;
	/** Preffered primary (audio/video codec) descriptor from descriptor_arr */
	mpf_codec_descriptor_t *primary_descriptor;
	/** Preffered named event (telephone-event) descriptor from descriptor_arr */
	mpf_codec_descriptor_t *event_descriptor;
};

/** Codec attributes */
struct mpf_codec_attribs_t {
	/** Codec name */
	apt_str_t  name;
	/** Bits per sample */
	apr_byte_t bits_per_sample;
	/** Supported sampling rates (mpf_sample_rates_e) */
	int        sample_rates;
};

/** List of codec attributes (capabilities) */
struct mpf_codec_capabilities_t {
	/** Dynamic array of codec attributes (mpf_codec_attrribs_t) */
	apr_array_header_t *attrib_arr;
	/** Allow/support named events */
	apt_bool_t          allow_named_events;
};

/** Codec frame */
struct mpf_codec_frame_t {
	/** Raw buffer, which may contain encoded or decoded data */
	void      *buffer;
	/** Buffer size */
	apr_size_t size;
};


/** Initialize codec descriptor */
static APR_INLINE void mpf_codec_descriptor_init(mpf_codec_descriptor_t *descriptor)
{
	descriptor->payload_type = 0;
	apt_string_reset(&descriptor->name);
	descriptor->sampling_rate = 0;
	descriptor->channel_count = 0;
	apt_string_reset(&descriptor->format);
	descriptor->enabled = TRUE;
}

/** Initialize codec descriptor */
static APR_INLINE  mpf_codec_descriptor_t* mpf_codec_descriptor_create(apr_pool_t *pool)
{
	mpf_codec_descriptor_t *descriptor = (mpf_codec_descriptor_t*) apr_palloc(pool,sizeof(mpf_codec_descriptor_t));
	mpf_codec_descriptor_init(descriptor);
	return descriptor;
}

/** Calculate encoded frame size in bytes */
static APR_INLINE apr_size_t mpf_codec_frame_size_calculate(const mpf_codec_descriptor_t *descriptor, const mpf_codec_attribs_t *attribs)
{
	return descriptor->channel_count * attribs->bits_per_sample * CODEC_FRAME_TIME_BASE * 
			descriptor->sampling_rate / 1000 / 8; /* 1000 - msec per sec, 8 - bits per byte */
}

/** Calculate samples of the frame (ts) */
static APR_INLINE apr_size_t mpf_codec_frame_samples_calculate(const mpf_codec_descriptor_t *descriptor)
{
	return descriptor->channel_count * CODEC_FRAME_TIME_BASE * descriptor->sampling_rate / 1000;
}

/** Calculate linear frame size in bytes */
static APR_INLINE apr_size_t mpf_codec_linear_frame_size_calculate(apr_uint16_t sampling_rate, apr_byte_t channel_count)
{
	return channel_count * BYTES_PER_SAMPLE * CODEC_FRAME_TIME_BASE * sampling_rate / 1000;
}



/** Reset list of codec descriptors */
static APR_INLINE void mpf_codec_list_reset(mpf_codec_list_t *codec_list)
{
{'fraction_non_alphanumeric': 0.08859737638748738, 'fraction_numerical': 0.018567103935418768, 'mean_word_length': 4.9921402660217655, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5277097', 'n_tokens_mistral': 3567, 'n_tokens_neox': 3306, 'n_words': 837}