/* --------------------------------------------------------------------------*/
void audio_start(void **priv)
{
    audio_decode_init(priv);
    audio_decode_start(*priv);
}
Esempio n. 2
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;
}