All pastes #655976 Raw Edit

Stuff

public text v1 · immutable
#655976 ·published 2007-08-13 19:33 UTC
rendered paste body
int main(int argc, char** argv)
{
	asf_file_t *asf_file = NULL;
	asf_stream_properties_t *asf_stream_props;
	asf_waveformatex_t *wfx;	
	asf_packet_t *current_packet;
	WMADecodeContext *wma;
	int outfile;

	if (argc == 1) { printf("ERR: No input file...\n"); return 1; }
	if (argc == 2) printf("ERR: No output file, not writing.\n"); 
	else {
		outfile = creat(argv[2], 0666);
		if (outfile < 0) { printf("ERR: Can't open output.\n"); return 1; }
	}

	asf_file = asf_open_file(argv[1]);
	if (asf_init(asf_file) != 0) {
		printf("ERR: Failed to init asf stream\n");
		return 1;
	}

	asf_stream_props = asf_get_stream_properties(asf_file, 1);
	if (asf_stream_props == NULL) {
		printf("ERR: Failed to get stream 1 properties\n");
		goto exit;
	}
	wfx = (asf_waveformatex_t *) asf_stream_props->properties;
	prex(wfx);
	write_prelim_header(outfile, wfx);
	//printf ("WMA Stream features: bits: %d, rate: %d, channels: %d\n", wfx->bitspersample, wfx->rate, wfx->channels);

	wma = malloc(sizeof(WMADecodeContext));
	int wmaret = wma_decode_init(wma, wfx);
	switch (wmaret) {
	case -1:
		printf("ERR: cannot decode flavor of WMA: codec id: %d\n", wfx->codec_id);
		goto exit;
	case 0: break; //ok
	default:
		printf("ERR: cannot initialize WMA decoder: error: %d\n", wmaret);
		goto exit;
	}

	int totpacks = 0;
	int totwritten = 0;
	while (1) 
	{
		int ret;

		current_packet = asf_packet_create();
		ret = asf_get_packet(asf_file, current_packet);
		if (ret < 0) {
			printf("ERR: cannot initialize ASF packet: error: %d\n", ret);
			goto bail_packet;
		}
		else if (ret == 0) break;
	
		ret	= wma_decode_superframe_init(wma,
									current_packet->payloads[0].data,
									current_packet->payloads[0].datalen);
		if (!ret)
		{
			printf("ERR: cannot decode superframe: error: %d\n", ret);
			goto bail_packet;
		}
		
		int frame;
		uint32_t totsuper = 0;
		for (frame = 0; frame < wma->nb_frames; frame++)
		{
			int decoded_samples = wma_decode_superframe_frame(wma, decoded, 
															current_packet->payloads[0].data, 
															current_packet->payloads[0].datalen);
			if (decoded_samples <= 0) {
				printf("ERR: cannot decode frame: error: %d. \n", decoded_samples);
				goto bail_packet;
			}
			uint32_t val = decoded_samples * wfx->channels * (wfx->bitspersample / 8);
			printf(">>>> Decoded: %d\n", val);
			totsuper += val;
			if (outfile)
				write(outfile, decoded, val);

			printf(".");
		}
		printf("Superframe done: %d frames, %d samples\n", frame, totsuper);
		totwritten += totsuper;

		asf_free_packet(current_packet);
		totpacks ++;
	}

	printf("T: no more packets to decode. total: %d, %d bytes\n", totpacks, totwritten);

	rewrite_header(outfile, totwritten);

bail_packet:
	if (current_packet) asf_free_packet(current_packet);
exit:
	asf_close(asf_file);
	free(wma);
	if (outfile >= 0) close(outfile);
	return 0;
}