Beispiel #1
0
static void* mpglib_callback(snd_stream_hnd_t hnd, int size, int * actual) {
	//static	int frames = 0;
	int ret, rsize;

	/* Check for file not started or file finished */
	if (mp3_fd == 0)
		return NULL;

	/* Dump the last PCM packet */
	pcm_empty(pcm_discard);

	/* Loop decoding until we have a full buffer */
	while (pcm_count < size) {
		//printf("decoding previously loaded frame into %08x, %d bytes possible\n", pcm_ptr, PCM_WATER - pcm_count);
		
		ret = decodeMP3(&mp, NULL, 0, pcm_ptr, PCM_WATER - pcm_count, &rsize);
		//ret = decodeMP3_clipchoice(&mp, NULL, 0, pcm_ptr, &rsize, synth_1to1_mono, synth_1to1);
		//printf("ret was %s, size is %d\n", ret == MP3_OK ? "OK" : "ERROR", rsize);
		
		if (ret != MP3_OK) {
			//printf("Refilling the buffer\n");
			// Pull in some more data (and check for EOF) 
			if (bs_fill() < 0) {
				printf("Decode completed\r\n");
				goto errorout;
			}
			//printf("trying decode again...\n");
			ret = decodeMP3(&mp, bs_buffer, BS_SIZE, pcm_ptr, PCM_WATER - pcm_count, &rsize);
			//ret = decodeMP3_clipchoice(&mp, bs_ptr, BS_SIZE, pcm_ptr, &size, synth_1to1_mono, synth_1to1);
			//printf("ret was %s, size is %d\n", ret == MP3_OK ? "OK" : "ERROR", rsize);
			//bs_ptr += (8*1024); bs_count -= (8*1024);
		}

		pcm_ptr += rsize; pcm_count += rsize;
		//frames++;
		
		/*if (!(frames % 64)) {
			printf("Decoded %d frames    \r", frames);
		}*/
	}

	pcm_discard = *actual = size;

	/* Got it successfully */
	return pcm_buffer;

errorout:
	fs_close(mp3_fd); mp3_fd = 0;
	return NULL;
}
/* This callback is called each time the sndstream driver needs
   some more data. It will tell us how much it needs in bytes. */
static void* mpglib_callback(int req_size) {
	int size, ret;
	
	/* Check for file not started or file finished */
	if (mp3_fd == 0)
		return NULL;

	/* Dump the last PCM packet */
	pcm_empty(req_size);

	/* Loop decoding until we have a full buffer */
	while (pcm_count < req_size) {
		/* Decode frames until we run out of data or space */
		printf("decoding previously loaded frame into %08x, %d bytes possible\n",
			pcm_ptr, PCM_WATER - pcm_count);
		ret = decodeMP3(&mp, NULL, 0,
			pcm_ptr, PCM_WATER - pcm_count,
			&size);
		printf("ret was %s, size is %d\n",
			ret == MP3_OK ? "OK" : "ERROR", size);
		if (ret != MP3_OK) {
			printf("Refilling the buffer\n");
			/* Pull in some more data (and check for EOF) */
			if (bs_fill() < 0) {
				printf("Decode completed\r\n");
				goto errorout;
			}
			printf("trying decode again...\n");
			ret = decodeMP3(&mp, bs_buffer, BS_SIZE,
				pcm_ptr, PCM_WATER - pcm_count,
				&size);
			printf("ret was %s, size is %d\n",
				ret == MP3_OK ? "OK" : "ERROR", size);
		}

		pcm_ptr += size; pcm_count += size;

		/* frames++;
		if (!(frames % 64)) {
			printf("Decoded %d frames    \r", frames);
		}*/
	}

	/* Got it successfully */
	return pcm_buffer;

errorout:
	fs_close(mp3_fd); mp3_fd = 0;
	return NULL;
}
Beispiel #3
0
/* This callback is called each time the sndstream driver needs
   some more data. It will tell us how much it needs in bytes. */
static void* xing_callback(snd_stream_hnd_t hnd, int size, int * actual) {
	static	int frames = 0;
	IN_OUT	x;

	/* Check for file not started or file finished */
	if (mp3_fd == 0)
		return NULL;

	/* Dump the last PCM packet */
	pcm_empty(pcm_discard);

	/* Loop decoding until we have a full buffer */
	while (pcm_count < size) {
		/* Pull in some more data (and check for EOF) */
		if (bs_fill() < 0 || bs_count < frame_bytes) {
			printf("Decode completed\r\n");
			goto errorout;
		}

		/* Decode a frame */
		x = audio_decode(&mpeg, bs_ptr, (short*)pcm_ptr);
		if (x.in_bytes <= 0) {
			printf("Bad sync in MPEG file\r\n");
			goto errorout;
		}

		bs_ptr += x.in_bytes; bs_count -= x.in_bytes;
		pcm_ptr += x.out_bytes; pcm_count += x.out_bytes;
		
		frames++;
		/*if (!(frames % 64)) {
			printf("Decoded %d frames    \r", frames);
		}*/
	}

	pcm_discard = *actual = size;

	/* Got it successfully */
	return pcm_buffer;

errorout:
	fs_close(mp3_fd); mp3_fd = 0;
	return NULL;
}
Beispiel #4
0
/* Open an MPEG stream and prepare for decode */
static int libmpg123_init(const char *fn) {
	int err;
	uint32	fd;
	
	/* Open the file */
#ifdef BS_SIZE
	mp3_fd = fd = fs_open(fn, O_RDONLY);
#else
	fd = fs_open(fn, O_RDONLY);
#endif
	
	if (fd < 0) {
		printf("Can't open input file %s\r\n", fn);
		printf("getwd() returns '%s'\r\n", fs_getwd());
		return -1;
	}
	
#ifndef BS_SIZE	
	fs_close(fd);
#endif
	
	if (fn != mp3_last_fn) {
		if (fn[0] != '/') {
			strcpy(mp3_last_fn, fs_getwd());
			strcat(mp3_last_fn, "/");
			strcat(mp3_last_fn, fn);
		} else {
			strcpy(mp3_last_fn, fn);
		}
	}

	/* Allocate buffers */
	if (pcm_buffer == NULL)
		pcm_buffer = malloc(PCM_SIZE);
	pcm_ptr = pcm_buffer; pcm_count = pcm_discard = 0;

	
#ifdef BS_SIZE

	if (bs_buffer == NULL)
		bs_buffer = malloc(BS_SIZE);
	bs_ptr = bs_buffer; bs_count = 0;

	/* Fill bitstream buffer */
	if (bs_fill() < 0) {
		printf("Can't read file header\r\n");
		goto errorout;
	}

	/* Are we looking at a RIFF file? (stupid Windows encoders) */
	if (bs_ptr[0] == 'R' && bs_ptr[1] == 'I' && bs_ptr[2] == 'F' && bs_ptr[3] == 'F') {
		/* Found a RIFF header, scan through it until we find the data section */
		printf("Skipping stupid RIFF header\r\n");
		while (bs_ptr[0] != 'd' || bs_ptr[1] != 'a' || bs_ptr[2] != 't'	|| bs_ptr[3] != 'a') {
			bs_ptr++;
			if (bs_ptr >= (bs_buffer + BS_SIZE)) {
				printf("Indeterminately long RIFF header\r\n");
				goto errorout;
			}
		}

		/* Skip 'data' and length */
		bs_ptr += 8;
		bs_count -= (bs_ptr - bs_buffer);
		printf("Final index is %d\r\n", (bs_ptr - bs_buffer));
	}

	if (((uint8)bs_ptr[0] != 0xff) && (!((uint8)bs_ptr[1] & 0xe0))) {
		printf("Definitely not an MPEG file\r\n");
		goto errorout;
	}
#endif
	
	mpg123_init();
    mh = mpg123_new(NULL, &err);

	if(mh == NULL) {
		printf("Can't init mpg123: %s\n", mpg123_strerror(mh));
		goto errorout;
	}
   
    /* Open the MP3 context in open_fd mode */
#ifdef BS_SIZE
    err = mpg123_open_fd(mh, mp3_fd);
#else
	err = mpg123_open(mh, fn);
#endif
 
	if(err != MPG123_OK) {
		printf("Can't open mpg123\n");
		mpg123_exit();
		goto errorout;
	}

	int enc;

	mpg123_getformat(mh, &rate, &channels, &enc);
	mpg123_info(mh, &decinfo);
	
	printf("Output Sampling rate = %ld\r\n", decinfo.rate);
	printf("Output Bitrate       = %d\r\n", decinfo.bitrate);
	printf("Output Frame size    = %d\r\n", decinfo.framesize);

	printf("mpg123 initialized successfully\r\n");
	return 0;

errorout:
	printf("Exiting on error\r\n");
#ifdef BS_SIZE
	if (bs_buffer) {
		free(bs_buffer);
		bs_buffer = NULL;
	}
#endif
	if (pcm_buffer) {
		free(pcm_buffer);
		pcm_buffer = NULL;
	}
#ifdef BS_SIZE
	fs_close(fd);
	mp3_fd = 0;
#endif
	return -1;
}
Beispiel #5
0
/* This callback is called each time the sndstream driver needs
   some more data. It will tell us how much it needs in bytes. */
static void* mpg123_callback(snd_stream_hnd_t hnd, int size, int * actual) {
	//static	int frames = 0;
	size_t done = 0;
	int err = 0;

#ifdef BS_SIZE
	/* Check for file not started or file finished */
	if (mp3_fd == 0)
		return NULL;
#endif

	/* Dump the last PCM packet */
	pcm_empty(pcm_discard);

	/* Loop decoding until we have a full buffer */
	while (pcm_count < size) {
		
#ifdef BS_SIZE		
		/* Pull in some more data (and check for EOF) */
		
		if (bs_fill() < 0 || bs_count < decinfo.framesize) {
			printf("snd_mp3_server: Decode completed\r\n");
			goto errorout;
		}
		err = mpg123_decode(mh, bs_ptr, decinfo.framesize, pcm_ptr, size, &done); 
#else
		err = mpg123_read(mh, pcm_ptr, size, &done);
#endif

		switch(err) {
			case MPG123_DONE:
				printf("snd_mp3_server: Decode completed\r\n");
				goto errorout;
			case MPG123_NEED_MORE:
				printf("snd_mp3_server: MPG123_NEED_MORE\n");
				break;
			case MPG123_ERR:
				printf("snd_mp3_server: %s\n", (char*)mpg123_strerror(mh));
				goto errorout;
				break;
			default:
				break;
		}
		
#ifdef BS_SIZE
		bs_ptr += decinfo.framesize; bs_count -= decinfo.framesize;
#endif
		pcm_ptr += done; pcm_count += done;
		
		//frames++;
		//if (!(frames % 64)) {
			//printf("Decoded %d frames    \r", frames);
		//}
	}

	pcm_discard = *actual = size;

	/* Got it successfully */
	return pcm_buffer;

errorout:
#ifdef BS_SIZE
	fs_close(mp3_fd); mp3_fd = 0;
#endif
	return NULL;
}
Beispiel #6
0
/* Open an MPEG stream and prepare for decode */
static int mpglib_init(const char *fn) {
	uint32	fd;
	int size;

	/* Open the file */
	mp3_fd = fd = fs_open(fn, O_RDONLY);
	if (fd < 0) {
		printf("Can't open input file %s\r\n", fn);
		printf("getwd() returns '%s'\r\n", fs_getwd());
		return -1;
	}
	if (fn != mp3_last_fn) {
		if (fn[0] != '/') {
			strcpy(mp3_last_fn, fs_getwd());
			strcat(mp3_last_fn, "/");
			strcat(mp3_last_fn, fn);
		} else {
			strcpy(mp3_last_fn, fn);
		}
	}

	/* Allocate buffers */
	if (bs_buffer == NULL)
		bs_buffer = malloc(BS_SIZE);
	bs_ptr = bs_buffer; bs_count = 0;
	if (pcm_buffer == NULL)
		pcm_buffer = malloc(PCM_SIZE);
	pcm_ptr = pcm_buffer; pcm_count = pcm_discard = 0;

	/* Fill bitstream buffer */
	if (bs_fill() < 0) {
		printf("Can't read file header\r\n");
		goto errorout;
	}

	/* Are we looking at a RIFF file? (stupid Windows encoders) */
	if (bs_ptr[0] == 'R' && bs_ptr[1] == 'I' && bs_ptr[2] == 'F' && bs_ptr[3] == 'F') {
		/* Found a RIFF header, scan through it until we find the data section */
		printf("Skipping stupid RIFF header\r\n");
		while (bs_ptr[0] != 'd' || bs_ptr[1] != 'a' || bs_ptr[2] != 't'	|| bs_ptr[3] != 'a') {
			bs_ptr++;
			if (bs_ptr >= (bs_buffer + BS_SIZE)) {
				printf("Indeterminately long RIFF header\r\n");
				goto errorout;
			}
		}

		/* Skip 'data' and length */
		bs_ptr += 8;
		bs_count -= (bs_ptr - bs_buffer);
		printf("Final index is %d\r\n", (bs_ptr - bs_buffer));
	}

	if (((uint8)bs_ptr[0] != 0xff) && (!((uint8)bs_ptr[1] & 0xe0))) {
		printf("Definitely not an MPEG file\r\n");
		goto errorout;
	}

	/* Initialize MPEG engines */
	InitMP3(&mp);

	/* Decode the first frame */
	printf("decoding first frame:\n");
	decodeMP3(&mp, bs_buffer, BS_SIZE, pcm_ptr, PCM_WATER - pcm_count, &size);
	printf("decoded size was %d\n", size);
	pcm_ptr += size; pcm_count += size;

	printf("mpglib initialized successfully\r\n");
	return 0;

errorout:
	printf("Exiting on error\r\n");
	if (bs_buffer) {
		free(bs_buffer);
		bs_buffer = NULL;
	}
	if (pcm_buffer) {
		free(pcm_buffer);
		pcm_buffer = NULL;
	}
	fs_close(fd);
	mp3_fd = 0;
	return -1;
}
Beispiel #7
0
/* Open an MPEG stream and prepare for decode */
static int xing_init(const char *fn) {
	uint32	fd;

	/* Open the file */
	mp3_fd = fd = fs_open(fn, O_RDONLY);
	if (fd < 0) {
		printf("Can't open input file %s\r\n", fn);
		printf("getwd() returns '%s'\r\n", fs_getwd());
		return -1;
	}
	if (fn != mp3_last_fn) {
		if (fn[0] != '/') {
			strcpy(mp3_last_fn, fs_getwd());
			strcat(mp3_last_fn, "/");
			strcat(mp3_last_fn, fn);
		} else {
			strcpy(mp3_last_fn, fn);
		}
	}

	/* Allocate buffers */
	if (bs_buffer == NULL)
		bs_buffer = malloc(BS_SIZE);
	bs_ptr = bs_buffer; bs_count = 0;
	if (pcm_buffer == NULL)
		pcm_buffer = malloc(PCM_SIZE);
	pcm_ptr = pcm_buffer; pcm_count = pcm_discard = 0;

	/* Fill bitstream buffer */
	if (bs_fill() < 0) {
		printf("Can't read file header\r\n");
		goto errorout;
	}

	/* Are we looking at a RIFF file? (stupid Windows encoders) */
	if (bs_ptr[0] == 'R' && bs_ptr[1] == 'I' && bs_ptr[2] == 'F' && bs_ptr[3] == 'F') {
		/* Found a RIFF header, scan through it until we find the data section */
		printf("Skipping stupid RIFF header\r\n");
		while (bs_ptr[0] != 'd' || bs_ptr[1] != 'a' || bs_ptr[2] != 't'	|| bs_ptr[3] != 'a') {
			bs_ptr++;
			if (bs_ptr >= (bs_buffer + BS_SIZE)) {
				printf("Indeterminately long RIFF header\r\n");
				goto errorout;
			}
		}

		/* Skip 'data' and length */
		bs_ptr += 8;
		bs_count -= (bs_ptr - bs_buffer);
		printf("Final index is %d\r\n", (bs_ptr - bs_buffer));
	}

	if (((uint8)bs_ptr[0] != 0xff) && (!((uint8)bs_ptr[1] & 0xe0))) {
		printf("Definitely not an MPEG file\r\n");
		goto errorout;
	}

	/* Initialize MPEG engines */
	mpeg_init(&mpeg);
	mpeg_eq_init(&mpeg);

	/* Parse MPEG header */
	frame_bytes = head_info2(bs_ptr, bs_count, &head, &bitrate);
	if (frame_bytes == 0) {
		printf("Bad or unsupported MPEG file\r\n");
		goto errorout;
	}

	/* Print out some info about it */
	{
		static char *layers[] = { "invalid", "3", "2", "1" };
		static char *modes[] = { "stereo", "joint-stereo", "dual", "mono" };
		static int srs[] = { 22050, 24000, 16000, 1, 44100, 48000, 32000, 1 };

		printf("Opened stream %s for decoding:\r\n", fn);
		printf("  %dKbps Layer %s %s at %dHz\r\n",
			bitrate/1000, layers[head.option], modes[head.mode],
			srs[4*head.id + head.sr_index]);
	}

	/* Initialize audio decoder */
	if (!audio_decode_init(&mpeg, &head, frame_bytes,
			REDUCT_NORMAL, 0, CONV_NORMAL, 44100)) {
		printf("Failed to initialize decoder\r\n");
		goto errorout;
	}
	audio_decode_info(&mpeg, &decinfo);
	printf("Output Sampling rate = %ld\r\n", decinfo.samprate);
	printf("Output Channels      = %d\r\n", decinfo.channels);
	printf("Output Bits          = %d\r\n", decinfo.bits);
	printf("Output Type          = %d\r\n", decinfo.type);
	/* if (decinfo.samprate != 44100) {
		printf("Currently cannot process %ld sampling rate\r\n", decinfo.samprate);
		goto errorout;
	} */

	printf("XingMP3 initialized successfully\r\n");
	return 0;

errorout:
	printf("Exiting on error\r\n");
	if (bs_buffer) {
		free(bs_buffer);
		bs_buffer = NULL;
	}
	if (pcm_buffer) {
		free(pcm_buffer);
		pcm_buffer = NULL;
	}
	fs_close(fd);
	mp3_fd = 0;
	return -1;
}