diff options
Diffstat (limited to 'libtoolame-dab')
-rw-r--r-- | libtoolame-dab/audio_read.c | 689 | ||||
-rw-r--r-- | libtoolame-dab/audio_read.h | 48 | ||||
-rw-r--r-- | libtoolame-dab/bitstream.c | 8 | ||||
-rw-r--r-- | libtoolame-dab/vlc_input.c | 430 | ||||
-rw-r--r-- | libtoolame-dab/vlc_input.h | 34 | ||||
-rw-r--r-- | libtoolame-dab/xpad.c | 88 | ||||
-rw-r--r-- | libtoolame-dab/xpad.h | 28 | ||||
-rw-r--r-- | libtoolame-dab/zmqoutput.c | 120 | ||||
-rw-r--r-- | libtoolame-dab/zmqoutput.h | 37 |
9 files changed, 0 insertions, 1482 deletions
diff --git a/libtoolame-dab/audio_read.c b/libtoolame-dab/audio_read.c deleted file mode 100644 index 3649ef4..0000000 --- a/libtoolame-dab/audio_read.c +++ /dev/null @@ -1,689 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include "common.h" -#include "encoder.h" -#include "options.h" -#include "portableio.h" -#if defined(JACK_INPUT) -#include <pthread.h> -#include <jack/jack.h> -#include <jack/ringbuffer.h> -#endif -#include "audio_read.h" -#include "vlc_input.h" - -#if defined(JACK_INPUT) -jack_port_t *input_port_left; -jack_port_t *input_port_right; -jack_client_t *client; -pthread_mutex_t encode_thread_lock = PTHREAD_MUTEX_INITIALIZER; -pthread_cond_t data_ready = PTHREAD_COND_INITIALIZER; - -/* shutdown can tell get_audio to stop */ -typedef struct _thread_info { - volatile int connected; -} jack_thread_info_t; - -jack_thread_info_t thread_info; -const size_t sample_size = sizeof(jack_default_audio_sample_t); - -#define DEFAULT_RB_SIZE 16384 /* ringbuffer size in frames */ -jack_ringbuffer_t *rb; - -/* setup_jack() - * - * PURPOSE: connect to jack, setup the ports, the ringbuffer - * - * frame_header is needed (fill information about sampling rate) - */ - -void setup_jack(frame_header *header, const char* jackname) { - const char *client_name = jackname; - const char *server_name = NULL; - jack_options_t options = JackNullOption; - jack_status_t status; - - /* open a client connection to the JACK server */ - - client = jack_client_open(client_name, options, &status, server_name); - if (client == NULL) { - fprintf(stderr, "jack_client_open() failed, " - "status = 0x%2.0x\n", status); - if (status & JackServerFailed) { - fprintf(stderr, "Unable to connect to JACK server\n"); - } - exit(1); - } - if (status & JackServerStarted) { - fprintf(stderr, "JACK server started\n"); - } - if (status & JackNameNotUnique) { - client_name = jack_get_client_name(client); - fprintf(stderr, "unique name `%s' assigned\n", client_name); - } - - thread_info.connected = 1; - - /* tell the JACK server to call `process()' whenever - there is work to be done. - */ - - jack_set_process_callback(client, process, &thread_info); - - /* tell the JACK server to call `jack_shutdown()' if - it ever shuts down, either entirely, or if it - just decides to stop calling us. - */ - - jack_on_shutdown(client, jack_shutdown, &thread_info); - - /* display the current sample rate. - */ - - printf ("engine sample rate: %" PRIu32 "\n", - jack_get_sample_rate(client)); - - if ((header->sampling_frequency = SmpFrqIndex((long) jack_get_sample_rate(client), &header->version)) < 0) { - fprintf (stderr, "invalid sample rate\n"); - exit(1); - } - - /* create two ports */ - - input_port_left = jack_port_register(client, "input0", - JACK_DEFAULT_AUDIO_TYPE, - JackPortIsInput, 0); - input_port_right = jack_port_register(client, "input1", - JACK_DEFAULT_AUDIO_TYPE, - JackPortIsInput, 0); - - if ((input_port_left == NULL) || (input_port_right == NULL)) { - fprintf(stderr, "no more JACK ports available\n"); - exit(1); - } - - - /* setup the ringbuffer */ - rb = jack_ringbuffer_create(2 * sample_size * DEFAULT_RB_SIZE); - fprintf(stderr, "jack sample_size: %zu\n", sample_size); - - - /* take the mutex */ - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); - pthread_mutex_lock(&encode_thread_lock); - - /* Tell the JACK server that we are ready to roll. Our - * process() callback will start running now. */ - - if (jack_activate(client)) { - fprintf (stderr, "cannot activate client"); - exit(1); - } -} - -/** - * The process callback for this JACK application is called in a - * special realtime thread once for each audio cycle. - * - * It fills the ringbuffer - */ -int process(jack_nframes_t nframes, void *arg) { - int i; - int samp; - //jack_thread_info_t *info = (jack_thread_info_t *) arg; - - jack_default_audio_sample_t *in_left, *in_right; - in_left = jack_port_get_buffer(input_port_left, nframes); - in_right = jack_port_get_buffer(input_port_right, nframes); - - /* Sndfile requires interleaved data. It is simpler here to - * just queue interleaved samples to a single ringbuffer. */ - //fprintf(stderr, "process()\n"); - for (i = 0; i < nframes; i++) { - /* - jack_ringbuffer_write(rb, (void *)(in_left + i), sample_size); - jack_ringbuffer_write(rb, (void *)(in_right + i), sample_size); - */ - /* convert to shorts, then insert into ringbuffer */ - samp = lrintf(in_left[i] * 1.0 * 0x7FFF); - jack_ringbuffer_write(rb, (char*)&samp, 2); - samp = lrintf(in_right[i] * 1.0 * 0x7FFF); - jack_ringbuffer_write(rb, (char*)&samp, 2); - - } - //fprintf(stderr, "PROCESS()\n"); - - /* tell read_samples that we've got new data */ - pthread_cond_signal(&data_ready); - - return 0; -} - -/** - * JACK calls this shutdown_callback if the server ever shuts down or - * decides to disconnect the client. - */ -void jack_shutdown(void *arg) -{ - jack_thread_info_t *info = (jack_thread_info_t *) arg; - - info->connected = 0; - /* tell read_samples to move on */ - - pthread_cond_signal(&data_ready); -} -#endif // defined(JACK_INPUT) - -/************************************************************************ - * - * read_samples() - * - * PURPOSE: reads the PCM samples from a file to the buffer - * - * SEMANTICS: - * Reads #samples_read# number of shorts from #musicin# filepointer - * into #sample_buffer[]#. Returns the number of samples read. - * - ************************************************************************/ - -unsigned long read_samples (music_in_t* musicin, short sample_buffer[2304], - unsigned long num_samples, unsigned long frame_size) -{ - unsigned long samples_read; - static unsigned long samples_to_read; - static char init = TRUE; - - void* jack_sample_buffer; - - if (init) { - samples_to_read = num_samples; - init = FALSE; - } - if (samples_to_read >= frame_size) - samples_read = frame_size; - else - samples_read = samples_to_read; - - if (0) { } -#if defined(JACK_INPUT) - else if (glopts.input_select == INPUT_SELECT_JACK) { - int f = 2; - while (jack_ringbuffer_read_space(rb) < f * samples_read) { - /* wait until process() signals more data */ - pthread_cond_wait(&data_ready, &encode_thread_lock); - - if (thread_info.connected == 0) { - pthread_mutex_unlock(&encode_thread_lock); - jack_client_close(client); - jack_ringbuffer_free(rb); - return 0; - } - } - - jack_sample_buffer = malloc(f * (int)samples_read); - int bytes_read = jack_ringbuffer_read(rb, jack_sample_buffer, f * (int)samples_read); - //fprintf(stderr, " read_bytes / f = %d, should be %d\n", (int)bytes_read/f, samples_read); - samples_read = bytes_read / f; - if (bytes_read % f != 0) { - fprintf(stderr, "cannot divide bytes_read by f: %d mod f = %d", bytes_read, bytes_read % f); - } - //fprintf(stderr, " #%d(%d) \n", (int)bytes_read, samples_read); - //f2les_array(jack_sample_buffer, sample_buffer, samples_read, 1); - - memcpy(sample_buffer, jack_sample_buffer, bytes_read); - - free(jack_sample_buffer); - - } -#endif // defined(JACK_INPUT) - else if (glopts.input_select == INPUT_SELECT_WAV) { - if ((samples_read = - fread (sample_buffer, sizeof (short), (int) samples_read, - musicin->wav_input)) == 0) - fprintf (stderr, "Hit end of WAV audio data\n"); - } - else if (glopts.input_select == INPUT_SELECT_VLC) { -#if defined(VLC_INPUT) - ssize_t bytes_read = vlc_in_read(sample_buffer, sizeof(short) * (int)samples_read); - if (bytes_read == -1) { - fprintf (stderr, "VLC input error\n"); - samples_read = 0; - } - else { - samples_read = bytes_read / sizeof(short); - } -#else - samples_read = 0; -#endif - } - - /* - Samples are big-endian. If this is a little-endian machine - we must swap - */ - if (NativeByteOrder == order_unknown) { - NativeByteOrder = DetermineByteOrder (); - if (NativeByteOrder == order_unknown) { - fprintf (stderr, "byte order not determined\n"); - exit (1); - } - } - if (NativeByteOrder != order_littleEndian || (glopts.byteswap == TRUE)) - SwapBytesInWords (sample_buffer, samples_read); - - if (num_samples != MAX_U_32_NUM) - samples_to_read -= samples_read; - - if (samples_read < frame_size && samples_read > 0) { - /* fill out frame with zeros */ - for (; samples_read < frame_size; sample_buffer[samples_read++] = 0); - samples_to_read = 0; - samples_read = frame_size; - } - return (samples_read); -} - -/************************************************************************ - * - * get_audio() - * - * PURPOSE: reads a frame of audio data from a file to the buffer, - * aligns the data for future processing, and separates the - * left and right channels - * - * - ************************************************************************/ - unsigned long -get_audio (music_in_t* musicin, short buffer[2][1152], unsigned long num_samples, - int nch, frame_header *header) -{ - int j; - short insamp[2304]; - unsigned long samples_read; - - if (nch == 2) { /* stereo */ - samples_read = - read_samples (musicin, insamp, num_samples, (unsigned long) 2304); - if (glopts.channelswap == TRUE) { - for (j = 0; j < 1152; j++) { - buffer[1][j] = insamp[2 * j]; - buffer[0][j] = insamp[2 * j + 1]; - } - } else { - for (j = 0; j < 1152; j++) { - buffer[0][j] = insamp[2 * j]; - buffer[1][j] = insamp[2 * j + 1]; - } - } - } else if (glopts.downmix == TRUE) { - samples_read = - read_samples (musicin, insamp, num_samples, (unsigned long) 2304); - for (j = 0; j < 1152; j++) { - buffer[0][j] = 0.5 * (insamp[2 * j] + insamp[2 * j + 1]); - } - } else { /* mono */ - samples_read = - read_samples (musicin, insamp, num_samples, (unsigned long) 1152); - for (j = 0; j < 1152; j++) { - buffer[0][j] = insamp[j]; - /* buffer[1][j] = 0; don't bother zeroing this buffer. MFC Nov 99 */ - } - } - return (samples_read); -} - - -/***************************************************************************** - * - * Routines to determine byte order and swap bytes - * - *****************************************************************************/ - -enum byte_order DetermineByteOrder (void) -{ - char s[sizeof (long) + 1]; - union { - long longval; - char charval[sizeof (long)]; - } probe; - probe.longval = 0x41424344L; /* ABCD in ASCII */ - strncpy (s, probe.charval, sizeof (long)); - s[sizeof (long)] = '\0'; - /* fprintf( stderr, "byte order is %s\n", s ); */ - if (strcmp (s, "ABCD") == 0) - return order_bigEndian; - else if (strcmp (s, "DCBA") == 0) - return order_littleEndian; - else - return order_unknown; -} - -void SwapBytesInWords (short *loc, int words) -{ - int i; - short thisval; - char *dst, *src; - src = (char *) &thisval; - for (i = 0; i < words; i++) { - thisval = *loc; - dst = (char *) loc++; - dst[0] = src[1]; - dst[1] = src[0]; - } -} - -/***************************************************************************** - * - * Read Audio Interchange File Format (AIFF) headers. - * - *****************************************************************************/ - -int aiff_read_headers (FILE * file_ptr, IFF_AIFF * aiff_ptr) -{ - int chunkSize, subSize, sound_position; - - if (fseek (file_ptr, 0, SEEK_SET) != 0) - return -1; - - if (Read32BitsHighLow (file_ptr) != IFF_ID_FORM) - return -1; - - chunkSize = Read32BitsHighLow (file_ptr); - - if (Read32BitsHighLow (file_ptr) != IFF_ID_AIFF) - return -1; - - sound_position = 0; - while (chunkSize > 0) { - chunkSize -= 4; - switch (Read32BitsHighLow (file_ptr)) { - - case IFF_ID_COMM: - chunkSize -= subSize = Read32BitsHighLow (file_ptr); - aiff_ptr->numChannels = Read16BitsHighLow (file_ptr); - subSize -= 2; - aiff_ptr->numSampleFrames = Read32BitsHighLow (file_ptr); - subSize -= 4; - aiff_ptr->sampleSize = Read16BitsHighLow (file_ptr); - subSize -= 2; - aiff_ptr->sampleRate = ReadIeeeExtendedHighLow (file_ptr); - subSize -= 10; - while (subSize > 0) { - getc (file_ptr); - subSize -= 1; - } - break; - - case IFF_ID_SSND: - chunkSize -= subSize = Read32BitsHighLow (file_ptr); - aiff_ptr->blkAlgn.offset = Read32BitsHighLow (file_ptr); - subSize -= 4; - aiff_ptr->blkAlgn.blockSize = Read32BitsHighLow (file_ptr); - subSize -= 4; - sound_position = ftell (file_ptr) + aiff_ptr->blkAlgn.offset; - if (fseek (file_ptr, (long) subSize, SEEK_CUR) != 0) - return -1; - aiff_ptr->sampleType = IFF_ID_SSND; - break; - - default: - chunkSize -= subSize = Read32BitsHighLow (file_ptr); - while (subSize > 0) { - getc (file_ptr); - subSize -= 1; - } - break; - } - } - return sound_position; -} - -/***************************************************************************** - * - * Seek past some Audio Interchange File Format (AIFF) headers to sound data. - * - *****************************************************************************/ - -int aiff_seek_to_sound_data (FILE * file_ptr) -{ - if (fseek - (file_ptr, AIFF_FORM_HEADER_SIZE + AIFF_SSND_HEADER_SIZE, - SEEK_SET) != 0) - return (-1); - return (0); -} - -/************************************************************ - * parse_input_file() - * Determine the type of sound file. (stdin, wav, aiff, raw pcm) - * Determine Sampling Frequency - * number of samples - * whether the new sample is stereo or mono. - * - * If file is coming from /dev/stdin assume it is raw PCM. (it's what I use. YMMV) - * - * This is just a hacked together function. The aiff parsing comes from the ISO code. - * The WAV code comes from Nick Burch - * The ugly /dev/stdin hack comes from me. - * MFC Dec 99 - **************************************************************/ - void -parse_input_file (FILE * musicin, char inPath[MAX_NAME_SIZE], frame_header *header, - unsigned long *num_samples) -{ - - IFF_AIFF pcm_aiff_data; - long soundPosition; - - unsigned char wave_header_buffer[40]; //HH fixed - int wave_header_read = 0; - int wave_header_stereo = -1; - int wave_header_16bit = -1; - unsigned long samplerate; - - /*************************** STDIN ********************************/ - /* check if we're reading from stdin. Assume it's a raw PCM file. */ - /* Of course, you could be piping a WAV file into stdin. Not done in this code */ - /* this code is probably very dodgy and was written to suit my needs. MFC Dec 99 */ - if ((strcmp (inPath, "/dev/stdin") == 0)) { - fprintf (stderr, "Reading from stdin\n"); - fprintf (stderr, "Remember to set samplerate with '-s'.\n"); - *num_samples = MAX_U_32_NUM; /* huge sound file */ - return; - } - - if (fseek (musicin, 0L, SEEK_SET) == -1) { - fprintf (stderr, "Input is not seekable, assuming pipe with raw PCM\n"); - fprintf (stderr, "Remember to set samplerate with '-s'.\n"); - *num_samples = MAX_U_32_NUM; /* huge sound file */ - return; - } - - /**************************** AIFF ********************************/ - if ((soundPosition = aiff_read_headers (musicin, &pcm_aiff_data)) != -1) { - fprintf (stderr, ">>> Using Audio IFF sound file headers\n"); - aiff_check (inPath, &pcm_aiff_data, &header->version); - if (fseek (musicin, soundPosition, SEEK_SET) != 0) { - fprintf (stderr, "Could not seek to PCM sound data in \"%s\".\n", - inPath); - exit (1); - } - fprintf (stderr, "Parsing AIFF audio file \n"); - header->sampling_frequency = - SmpFrqIndex ((long) pcm_aiff_data.sampleRate, &header->version); - fprintf (stderr, ">>> %f Hz sampling frequency selected\n", - pcm_aiff_data.sampleRate); - - /* Determine number of samples in sound file */ - *num_samples = pcm_aiff_data.numChannels * pcm_aiff_data.numSampleFrames; - - if (pcm_aiff_data.numChannels == 1) { - header->mode = MPG_MD_MONO; - header->mode_ext = 0; - } - return; - } - - /**************************** WAVE *********************************/ - /* Nick Burch <The_Leveller@newmail.net> */ - /*********************************/ - /* Wave File Headers: (Dec) */ - /* 8-11 = "WAVE" */ - /* 22 = Stereo / Mono */ - /* 01 = mono, 02 = stereo */ - /* 24 = Sampling Frequency */ - /* 32 = Data Rate */ - /* 01 = x1 (8bit Mono) */ - /* 02 = x2 (8bit Stereo or */ - /* 16bit Mono) */ - /* 04 = x4 (16bit Stereo) */ - /*********************************/ - - fseek (musicin, 0, SEEK_SET); - fread (wave_header_buffer, 1, 40, musicin); - - if (wave_header_buffer[8] == 'W' && wave_header_buffer[9] == 'A' - && wave_header_buffer[10] == 'V' && wave_header_buffer[11] == 'E') { - fprintf (stderr, "Parsing Wave File Header\n"); - if (NativeByteOrder == order_unknown) { - NativeByteOrder = DetermineByteOrder (); - if (NativeByteOrder == order_unknown) { - fprintf (stderr, "byte order not determined\n"); - exit (1); - } - } - if (NativeByteOrder == order_littleEndian) { - samplerate = wave_header_buffer[24] + - (wave_header_buffer[25] << 8) + - (wave_header_buffer[26] << 16) + - (wave_header_buffer[27] << 24); - } else { - samplerate = wave_header_buffer[27] + - (wave_header_buffer[26] << 8) + - (wave_header_buffer[25] << 16) + - (wave_header_buffer[24] << 24); - } - /* Wave File */ - wave_header_read = 1; - switch (samplerate) { - case 44100: - case 48000: - case 32000: - case 24000: - case 22050: - case 16000: - fprintf (stderr, ">>> %ld Hz sampling freq selected\n", samplerate); - break; - default: - /* Unknown Unsupported Frequency */ - fprintf (stderr, ">>> Unknown samp freq %ld Hz in Wave Header\n", - samplerate); - fprintf (stderr, ">>> Default 44.1 kHz samp freq selected\n"); - samplerate = 44100; - } - - if ((header->sampling_frequency = - SmpFrqIndex ((long) samplerate, &header->version)) < 0) { - fprintf (stderr, "invalid sample rate\n"); - exit (0); - } - - if ((long) wave_header_buffer[22] == 1) { - fprintf (stderr, ">>> Input Wave File is Mono\n"); - wave_header_stereo = 0; - header->mode = MPG_MD_MONO; - header->mode_ext = 0; - } - if ((long) wave_header_buffer[22] == 2) { - fprintf (stderr, ">>> Input Wave File is Stereo\n"); - wave_header_stereo = 1; - } - if ((long) wave_header_buffer[32] == 1) { - fprintf (stderr, ">>> Input Wave File is 8 Bit\n"); - wave_header_16bit = 0; - fprintf (stderr, "Input File must be 16 Bit! Please Re-sample"); - exit (1); - } - if ((long) wave_header_buffer[32] == 2) { - if (wave_header_stereo == 1) { - fprintf (stderr, ">>> Input Wave File is 8 Bit\n"); - wave_header_16bit = 0; - fprintf (stderr, "Input File must be 16 Bit! Please Re-sample"); - exit (1); - } else { - /* fprintf(stderr, ">>> Input Wave File is 16 Bit\n" ); */ - wave_header_16bit = 1; - } - } - if ((long) wave_header_buffer[32] == 4) { - /* fprintf(stderr, ">>> Input Wave File is 16 Bit\n" ); */ - wave_header_16bit = 1; - } - /* should probably use the wave header to determine size here FIXME MFC Feb 2003 */ - *num_samples = MAX_U_32_NUM; - if (fseek (musicin, 44, SEEK_SET) != 0) { /* there's a way of calculating the size of the - wave header. i'll just jump 44 to start with */ - fprintf (stderr, "Could not seek to PCM sound data in \"%s\".\n", - inPath); - exit (1); - } - return; - } - - /*************************** PCM **************************/ - fprintf (stderr, "No header found. Assuming Raw PCM sound file\n"); - /* Raw PCM. No header. Reset the input file to read from the start */ - fseek (musicin, 0, SEEK_SET); - /* Assume it is a huge sound file since there's no real info available */ - /* FIXME: Could always fstat the file? Probably not worth it. MFC Feb 2003 */ - *num_samples = MAX_U_32_NUM; -} - - - -/************************************************************************ - * - * aiff_check - * - * PURPOSE: Checks AIFF header information to make sure it is valid. - * Exits if not. - * - ************************************************************************/ - -void aiff_check (char *file_name, IFF_AIFF * pcm_aiff_data, int *version) -{ - if (pcm_aiff_data->sampleType != IFF_ID_SSND) { - fprintf (stderr, "Sound data is not PCM in \"%s\".\n", file_name); - exit (1); - } - - if (SmpFrqIndex ((long) pcm_aiff_data->sampleRate, version) < 0) { - fprintf (stderr, "in \"%s\".\n", file_name); - exit (1); - } - - if (pcm_aiff_data->sampleSize != sizeof (short) * BITS_IN_A_BYTE) { - fprintf (stderr, "Sound data is not %zu bits in \"%s\".\n", - sizeof (short) * BITS_IN_A_BYTE, file_name); - exit (1); - } - - if (pcm_aiff_data->numChannels != MONO - && pcm_aiff_data->numChannels != STEREO) { - fprintf (stderr, "Sound data is not mono or stereo in \"%s\".\n", - file_name); - exit (1); - } - - if (pcm_aiff_data->blkAlgn.blockSize != 0) { - fprintf (stderr, "Block size is not %d bytes in \"%s\".\n", 0, file_name); - exit (1); - } - - if (pcm_aiff_data->blkAlgn.offset != 0) { - fprintf (stderr, "Block offset is not %d bytes in \"%s\".\n", 0, - file_name); - exit (1); - } -} diff --git a/libtoolame-dab/audio_read.h b/libtoolame-dab/audio_read.h deleted file mode 100644 index 215e89d..0000000 --- a/libtoolame-dab/audio_read.h +++ /dev/null @@ -1,48 +0,0 @@ -/* AIFF Definitions */ - -#define IFF_ID_FORM 0x464f524d /* "FORM" */ -#define IFF_ID_AIFF 0x41494646 /* "AIFF" */ -#define IFF_ID_COMM 0x434f4d4d /* "COMM" */ -#define IFF_ID_SSND 0x53534e44 /* "SSND" */ -#define IFF_ID_MPEG 0x4d504547 /* "MPEG" */ - -#define AIFF_FORM_HEADER_SIZE 12 -#define AIFF_SSND_HEADER_SIZE 16 - - -typedef struct blockAlign_struct -{ - unsigned long offset; - unsigned long blockSize; -} -blockAlign; - -typedef struct IFF_AIFF_struct -{ - short numChannels; - unsigned long numSampleFrames; - short sampleSize; - double sampleRate; - unsigned long sampleType; - blockAlign blkAlgn; -} -IFF_AIFF; - -#if defined(JACK_INPUT) -void setup_jack(frame_header *header, const char* jackname); -int process(jack_nframes_t nframes, void *arg); -#endif -void jack_shutdown(void *arg); - -void parse_input_file (FILE *musicin, char *, frame_header *header, unsigned long *num_samples); -void aiff_check (char *file_name, IFF_AIFF * pcm_aiff_data, int *version); - -int aiff_read_headers (FILE *, IFF_AIFF *); -int aiff_seek_to_sound_data (FILE *); -enum byte_order DetermineByteOrder (void); -void SwapBytesInWords (short *loc, int words); - unsigned long read_samples (music_in_t*, short[2304], unsigned long, - unsigned long); - unsigned long get_audio (music_in_t*, short[2][1152], unsigned long, - int, frame_header *header); - diff --git a/libtoolame-dab/bitstream.c b/libtoolame-dab/bitstream.c index 4346d60..410ef5b 100644 --- a/libtoolame-dab/bitstream.c +++ b/libtoolame-dab/bitstream.c @@ -1,7 +1,6 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "zmqoutput.h" #include "common.h" #include "mem.h" #include "bitstream.h" @@ -112,10 +111,6 @@ void empty_buffer (Bit_stream_struc * bs, int minimum) fflush (bs->pt); /* NEW SS to assist in debugging */ } - else if (bs->zmq_sock) { - for (i = bs->buf_size - 1; i >= minimum; i--) - zmqoutput_write_byte(bs, bs->buf[i]); - } for (i = minimum - 1; i >= 0; i--) bs->buf[bs->buf_size - minimum + i] = bs->buf[i]; @@ -129,8 +124,6 @@ void empty_buffer (Bit_stream_struc * bs, int minimum) /* open the device to write the bit stream into it */ void open_bit_stream_w (Bit_stream_struc * bs, int size) { - bs->zmq_sock = NULL; - bs->pt = NULL; // we're not using file output alloc_buffer (bs, size); bs->buf_byte_idx = size - 1; @@ -147,7 +140,6 @@ void close_bit_stream_w (Bit_stream_struc * bs) putbits (bs, 0, 7); empty_buffer (bs, bs->buf_byte_idx + 1); if (bs->pt) fclose(bs->pt); - zmqoutput_close(bs); desalloc_buffer (bs); } diff --git a/libtoolame-dab/vlc_input.c b/libtoolame-dab/vlc_input.c deleted file mode 100644 index 11e04c7..0000000 --- a/libtoolame-dab/vlc_input.c +++ /dev/null @@ -1,430 +0,0 @@ -#include "vlc_input.h" - -#if defined(VLC_INPUT) -#include <stdlib.h> -#include <errno.h> -#include <pthread.h> -#include <semaphore.h> -#include <assert.h> -#include <unistd.h> -#include <string.h> -#include <stdio.h> - -int check_vlc_uses_size_t(); -struct vlc_buffer* vlc_buffer_new(); -void vlc_buffer_free(struct vlc_buffer* node); - -libvlc_instance_t *m_vlc; -libvlc_media_player_t *m_mp; - -unsigned int vlc_rate; -unsigned int vlc_channels; - -struct vlc_buffer *head_buffer; - -// now playing information can get written to -// a file. This writing happens in a separate thread -#define NOWPLAYING_LEN 512 -char vlc_nowplaying[NOWPLAYING_LEN]; -int vlc_nowplaying_running; -pthread_t vlc_nowplaying_thread; -const char* vlc_nowplaying_filename; - -struct icywriter_task_data { - char text[NOWPLAYING_LEN]; - int success; - sem_t sem; -}; - -struct icywriter_task_data icy_task_data; - - -pthread_mutex_t buffer_lock = PTHREAD_MUTEX_INITIALIZER; - -struct vlc_buffer* vlc_buffer_new() -{ - struct vlc_buffer* node; - node = malloc(sizeof(struct vlc_buffer)); - memset(node, 0, sizeof(struct vlc_buffer)); - return node; -} - -void vlc_buffer_free(struct vlc_buffer* node) -{ - if (node->buf) { - free(node->buf); - } - - free(node); -} - -size_t vlc_buffer_totalsize(struct vlc_buffer* node) -{ - size_t totalsize = 0; - for (; node != NULL; node = node->next) { - totalsize += node->size; - } - - return totalsize; -} - -// VLC Audio prerender callback, we must allocate a buffer here -void prepareRender_size_t( - void* p_audio_data, - uint8_t** pp_pcm_buffer, - size_t size) -{ - *pp_pcm_buffer = malloc(size); -} - -void prepareRender( - void* p_audio_data, - uint8_t** pp_pcm_buffer, - unsigned int size) -{ - *pp_pcm_buffer = malloc(size); -} - - -// Audio postrender callback -void handleStream_size_t( - void* p_audio_data, - uint8_t* p_pcm_buffer, - unsigned int channels, - unsigned int rate, - unsigned int nb_samples, - unsigned int bits_per_sample, - size_t size, - int64_t pts) -{ - assert(channels == vlc_channels); - assert(rate == vlc_rate); - assert(bits_per_sample == 16); - - // 16 is a bit arbitrary, if it's too small we might enter - // a deadlock if toolame asks for too much data - const size_t max_length = 16 * size; - - for (;;) { - pthread_mutex_lock(&buffer_lock); - - if (vlc_buffer_totalsize(head_buffer) < max_length) { - struct vlc_buffer* newbuf = vlc_buffer_new(); - - newbuf->buf = p_pcm_buffer; - newbuf->size = size; - - // Append the new buffer to the end of the linked list - struct vlc_buffer* tail = head_buffer; - while (tail->next) { - tail = tail->next; - } - tail->next = newbuf; - - pthread_mutex_unlock(&buffer_lock); - return; - } - - pthread_mutex_unlock(&buffer_lock); - usleep(100); - } -} - -// convert from unsigned int size to size_t size -void handleStream( - void* p_audio_data, - uint8_t* p_pcm_buffer, - unsigned int channels, - unsigned int rate, - unsigned int nb_samples, - unsigned int bits_per_sample, - unsigned int size, - int64_t pts) -{ - handleStream_size_t( - p_audio_data, - p_pcm_buffer, - channels, - rate, - nb_samples, - bits_per_sample, - size, - pts); -} - -int vlc_in_prepare( - unsigned verbosity, - unsigned int rate, - const char* uri, - unsigned channels, - const char* icy_write_file - ) -{ - fprintf(stderr, "Initialising VLC...\n"); - - vlc_nowplaying_running = 0; - vlc_nowplaying_filename = icy_write_file; - - long long int handleStream_address; - long long int prepareRender_address; - - int vlc_version_check = check_vlc_uses_size_t(); - if (vlc_version_check == 0) { - fprintf(stderr, "You are using VLC with unsigned int size callbacks\n"); - - handleStream_address = (long long int)(intptr_t)(void*)&handleStream; - prepareRender_address = (long long int)(intptr_t)(void*)&prepareRender; - } - else if (vlc_version_check == 1) { - fprintf(stderr, "You are using VLC with size_t size callbacks\n"); - - handleStream_address = (long long int)(intptr_t)(void*)&handleStream_size_t; - prepareRender_address = (long long int)(intptr_t)(void*)&prepareRender_size_t; - } - else { - fprintf(stderr, "Error detecting VLC version!\n"); - fprintf(stderr, " you are using %s\n", libvlc_get_version()); - return -1; - } - - vlc_rate = rate; - vlc_channels = channels; - - // VLC options - char smem_options[512]; - snprintf(smem_options, sizeof(smem_options), - "#transcode{acodec=s16l,samplerate=%d}:" - // We are using transcode because smem only support raw audio and - // video formats - "smem{" - "audio-postrender-callback=%lld," - "audio-prerender-callback=%lld" - "}", - vlc_rate, - handleStream_address, - prepareRender_address); - - char verb_options[512]; - snprintf(verb_options, sizeof(verb_options), - "--verbose=%d", verbosity); - - const char * const vlc_args[] = { - verb_options, - "--sout", smem_options // Stream to memory - }; - - // Launch VLC - m_vlc = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args); - - // Load the media - libvlc_media_t *m; - m = libvlc_media_new_location(m_vlc, uri); - m_mp = libvlc_media_player_new_from_media(m); - libvlc_media_release(m); - - // Allocate the list - head_buffer = vlc_buffer_new(); - - // Start playing - int ret = libvlc_media_player_play(m_mp); - - if (ret == 0) { - libvlc_media_t *media = libvlc_media_player_get_media(m_mp); - libvlc_state_t st; - - ret = -1; - - int timeout; - for (timeout = 0; timeout < 100; timeout++) { - st = libvlc_media_get_state(media); - usleep(10*1000); - if (st != libvlc_NothingSpecial) { - ret = 0; - break; - } - } - } - - return ret; -} - -ssize_t vlc_in_read(void *buf, size_t len) -{ - if (len == 0) { - return 0; - } - - assert(buf); - - size_t requested = len; - for (;;) { - pthread_mutex_lock(&buffer_lock); - - if (vlc_buffer_totalsize(head_buffer) >= len) { - while (len >= head_buffer->size) { - if (head_buffer->buf && head_buffer->size) { - // Get all the data from this list element - memcpy(buf, head_buffer->buf, head_buffer->size); - - buf += head_buffer->size; - len -= head_buffer->size; - } - - if (head_buffer->next) { - struct vlc_buffer *next_head = head_buffer->next; - vlc_buffer_free(head_buffer); - head_buffer = next_head; - } - else { - vlc_buffer_free(head_buffer); - head_buffer = vlc_buffer_new(); - break; - } - } - - if (len > 0) { - assert(len < head_buffer->size); - assert(head_buffer->buf); - - memcpy(buf, head_buffer->buf, len); - - // split the current head into two parts - size_t remaining = head_buffer->size - len; - uint8_t *newbuf = malloc(remaining); - - memcpy(newbuf, head_buffer->buf + len, remaining); - free(head_buffer->buf); - head_buffer->buf = newbuf; - head_buffer->size = remaining; - } - - pthread_mutex_unlock(&buffer_lock); - return requested; - } - - pthread_mutex_unlock(&buffer_lock); - usleep(100); - - libvlc_media_t *media = libvlc_media_player_get_media(m_mp); - libvlc_state_t st = libvlc_media_get_state(media); - if (!(st == libvlc_Opening || - st == libvlc_Buffering || - st == libvlc_Playing) ) { - return -1; - } - - char* nowplaying_sz = libvlc_media_get_meta(media, libvlc_meta_NowPlaying); - if (nowplaying_sz) { - snprintf(vlc_nowplaying, NOWPLAYING_LEN, "%s", nowplaying_sz); - free(nowplaying_sz); - } - } - - abort(); -} - -// This task is run in a separate thread -void* vlc_in_write_icy_task(void* arg) -{ - struct icywriter_task_data* data = arg; - - FILE* fd = fopen(vlc_nowplaying_filename, "wb"); - if (fd) { - int ret = fputs(data->text, fd); - fclose(fd); - - if (ret >= 0) { - data->success = 1; - } - } - else { - data->success = 0; - } - - sem_post(&data->sem); - return NULL; -} - -void vlc_in_write_icy(void) -{ - if (vlc_nowplaying_filename == NULL) { - return; - } - else if (vlc_nowplaying_running == 0) { - memcpy(icy_task_data.text, vlc_nowplaying, NOWPLAYING_LEN); - icy_task_data.success = 0; - - int ret = sem_init(&icy_task_data.sem, 0, 0); - if (ret == 0) { - ret = pthread_create(&vlc_nowplaying_thread, NULL, vlc_in_write_icy_task, &icy_task_data); - - if (ret == 0) { - vlc_nowplaying_running = 1; - } - else { - fprintf(stderr, "ICY Text writer: thread start failed: %s\n", strerror(ret)); - } - } - else { - fprintf(stderr, "ICY Text writer: semaphore init failed: %s\n", strerror(errno)); - } - - } - else { - int ret = sem_trywait(&icy_task_data.sem); - if (ret == -1 && errno == EAGAIN) { - return; - } - else if (ret == 0) { - ret = pthread_join(vlc_nowplaying_thread, NULL); - if (ret != 0) { - fprintf(stderr, "ICY Text writer: pthread_join error: %s\n", strerror(ret)); - } - - vlc_nowplaying_running = 0; - } - else { - fprintf(stderr, "ICY Text writer: semaphore trywait failed: %s\n", strerror(errno)); - } - } -} - - -/* VLC up to version 2.1.0 used a different callback function signature. - * VLC 2.2.0 uses size_t - * - * \return 1 if the callback with size_t size should be used. - * 0 if the callback with unsigned int size should be used. - * -1 if there was an error. - */ -int check_vlc_uses_size_t() -{ - int retval = -1; - - char libvlc_version[256]; - strncpy(libvlc_version, libvlc_get_version(), 255); - - char *space_position = strstr(libvlc_version, " "); - - if (space_position) { - *space_position = '\0'; - } - - char *saveptr; - char *major_ver_sz = strtok_r(libvlc_version, ".", &saveptr); - if (major_ver_sz) { - int major_ver = atoi(major_ver_sz); - - char *minor_ver_sz = strtok_r(NULL, ".", &saveptr); - if (minor_ver_sz) { - int minor_ver = atoi(minor_ver_sz); - - retval = (major_ver >= 2 && minor_ver >= 2) ? 1 : 0; - } - } - - return retval; -} - -#endif // defined(VLC_INPUT) - diff --git a/libtoolame-dab/vlc_input.h b/libtoolame-dab/vlc_input.h deleted file mode 100644 index a2ecefa..0000000 --- a/libtoolame-dab/vlc_input.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef __VLC_INPUT_H_ -#define __VLC_INPUT_H_ - -# if defined(VLC_INPUT) - -#include <stdint.h> -#include <stddef.h> -#include <sys/types.h> -#include <vlc/vlc.h> - - -// A linked list structure for the incoming buffers -struct vlc_buffer { - uint8_t *buf; - size_t size; - struct vlc_buffer *next; -}; - -// Open the VLC input -int vlc_in_prepare( - unsigned verbosity, - unsigned int rate, - const char* uri, - unsigned channels, - const char* icy_write_file); - -// Read len audio bytes into buf -ssize_t vlc_in_read(void *buf, size_t len); - -void vlc_in_write_icy(void); - -# endif // VLC_INPUT -#endif // __VLC_INPUT_H_ - diff --git a/libtoolame-dab/xpad.c b/libtoolame-dab/xpad.c deleted file mode 100644 index 6c97ea8..0000000 --- a/libtoolame-dab/xpad.c +++ /dev/null @@ -1,88 +0,0 @@ -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <sys/types.h> -#include <unistd.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <assert.h> -#include <errno.h> - -#include "xpad.h" - -static int xpad_fd = 0; - -/* The F-PAD has to be: - uint16_t fpad = 0x2; // CI flag - - if (xpad_len()) { - fpad |= 1<<13; // variable length X-PAD - } - - which is included by mot-encoder in the file/fifo - it generates - */ - -/* Create and open the desired PAD input fifo - */ -int xpad_init(char* pad_fifo, int pad_len) -{ - if (mkfifo(pad_fifo, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH) != 0) { - if (errno != EEXIST) { - fprintf(stderr, "Can't create pad file: %d!\n", errno); - return -1; - } - } - - xpad_fd = open(pad_fifo, O_RDONLY | O_NONBLOCK); - if (xpad_fd == -1) { - fprintf(stderr, "Can't open pad file!\n"); - return -1; - } - - int flags = fcntl(xpad_fd, F_GETFL, 0); - if (fcntl(xpad_fd, F_SETFL, flags | O_NONBLOCK)) { - fprintf(stderr, "Can't set non-blocking mode in pad file!\n"); - return -1; - } - - return 0; -} - -int xpad_read_len(uint8_t* buf, int len) -{ - if (xpad_fd == 0) return 0; - - ssize_t num_read = 0; - - while (num_read < len) { - ssize_t r = read(xpad_fd, buf + num_read, len - num_read); - - if(r < 0) { - if (errno == EAGAIN) { - return 0; - } - else { - perror("PAD input read error"); - return -1; - } - } - else if (r == 0) { - // reached end of data - return 0; - } - - num_read += r; - } - -#if XPAD_DEBUG - int i; - for (i = 0; i < len; i++) { - fprintf(stderr, "%02x ", buf[i]); - } - fprintf(stderr, "\n"); -#endif - - return num_read; -} - diff --git a/libtoolame-dab/xpad.h b/libtoolame-dab/xpad.h deleted file mode 100644 index cd0a434..0000000 --- a/libtoolame-dab/xpad.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef _XPAD_H_ -#define _XPAD_H_ - -#include <stdint.h> - -/* Initialise the xpad reader - * - * pad_fifo is the filename of the FIFO that will be created, and - * can be used with mot-encoder. - * - * pad_len is the XPAD length, that also has to be given - * to mot-encoder. - * - * returns 0 on success - * -1 on failure - */ -int xpad_init(char* pad_fifo, int pad_len); - -/* Get len bytes of x-pad data, write into buf - * returns either - * - len if the read was sucessful - * - 0 if there was no data - * - -1 if there was an error (errno will be set) - */ -int xpad_read_len(uint8_t* buf, int len); - -#endif - diff --git a/libtoolame-dab/zmqoutput.c b/libtoolame-dab/zmqoutput.c deleted file mode 100644 index 03007cc..0000000 --- a/libtoolame-dab/zmqoutput.c +++ /dev/null @@ -1,120 +0,0 @@ -#include "zmqoutput.h" -#include <zmq.h> -#include <stdlib.h> -#include <string.h> -#include "common.h" - -static void *zmq_context; - -// Buffer containing at maximum one frame -unsigned char* zmqbuf; - -// The current data length (smaller than allocated -// buffer size) -size_t zmqbuf_len; - -static int zmq_peak_left = 0; -static int zmq_peak_right = 0; - -void zmqoutput_set_peaks(int left, int right) -{ - zmq_peak_left = left; - zmq_peak_right = right; -} - -int zmqoutput_open(Bit_stream_struc *bs, const char* uri_list) -{ - zmq_context = zmq_ctx_new(); - bs->zmq_sock = zmq_socket(zmq_context, ZMQ_PUB); - if (bs->zmq_sock == NULL) { - fprintf(stderr, "Error occurred during zmq_socket: %s\n", - zmq_strerror(errno)); - return -1; - } - - char* uris = strdup(uri_list); - char* saveptr = NULL; - - for (; ; uris = NULL) { - char* uri = strtok_r(uris, ";", &saveptr); - - - if (uri) { - fprintf(stderr, "Connecting ZMQ to %s\n", uri); - if (zmq_connect(bs->zmq_sock, uri) != 0) { - fprintf(stderr, "Error occurred during zmq_connect: %s\n", - zmq_strerror(errno)); - free(uris); - return -1; - } - } - else { - break; - } - } - - free(uris); - - zmqbuf = (unsigned char*)malloc(bs->zmq_framesize); - if (zmqbuf == NULL) { - fprintf(stderr, "Unable to allocate ZMQ buffer\n"); - exit(0); - } - zmqbuf_len = 0; - return 0; -} - -int zmqoutput_write_byte(Bit_stream_struc *bs, unsigned char data) -{ - zmqbuf[zmqbuf_len++] = data; - - if (zmqbuf_len == bs->zmq_framesize) { - - int frame_length = sizeof(struct zmq_frame_header) + zmqbuf_len; - - struct zmq_frame_header* header = - malloc(frame_length); - - uint8_t* txframe = ((uint8_t*)header) + sizeof(struct zmq_frame_header); - - header->version = 1; - header->encoder = ZMQ_ENCODER_TOOLAME; - header->datasize = zmqbuf_len; - header->audiolevel_left = zmq_peak_left; - header->audiolevel_right = zmq_peak_right; - - memcpy(txframe, zmqbuf, zmqbuf_len); - - int send_error = zmq_send(bs->zmq_sock, header, frame_length, - ZMQ_DONTWAIT); - - free(header); - header = NULL; - - if (send_error < 0) { - fprintf(stderr, "ZeroMQ send failed! %s\n", zmq_strerror(errno)); - } - - zmqbuf_len = 0; - - return bs->zmq_framesize; - } - - return 0; - -} - -void zmqoutput_close(Bit_stream_struc *bs) -{ - if (bs->zmq_sock) - zmq_close(bs->zmq_sock); - - if (zmq_context) - zmq_ctx_destroy(zmq_context); - - if (zmqbuf) { - free(zmqbuf); - zmqbuf = NULL; - } -} - diff --git a/libtoolame-dab/zmqoutput.h b/libtoolame-dab/zmqoutput.h deleted file mode 100644 index 7f4eb59..0000000 --- a/libtoolame-dab/zmqoutput.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef _ZMQOUTPUT_H_ -#define _ZMQOUTPUT_H_ - -#include <stdint.h> -#include "common.h" - -#define ZMQ_ENCODER_TOOLAME 2 - -struct zmq_frame_header -{ - uint16_t version; // we support version=1 now - uint16_t encoder; // see ZMQ_ENCODER_XYZ - - /* length of the 'data' field */ - uint32_t datasize; - - /* Audio level, peak, linear PCM */ - int16_t audiolevel_left; - int16_t audiolevel_right; - - /* Data follows this header */ -} __attribute__ ((packed)); - - -/* Open the zmq socket and connect it to all URIs in the list. - * The URIs are semicolon delimited - */ -int zmqoutput_open(Bit_stream_struc * bs, const char* uri_list); - -int zmqoutput_write_byte(Bit_stream_struc *bs, unsigned char data); - -void zmqoutput_close(Bit_stream_struc *bs); - -void zmqoutput_set_peaks(int left, int right); - -#endif - |