#include "../ibifscodec.h"

M4Err SampleCallBack(void *calling_object, M4Sample *samp)
{
	fprintf(stdout, "Received at time %d, buffer %d bytes long.\n", samp->DTS, samp->dataLength);
	return M4OK;
}

int main(int argc, char **argv)
{
	int i;
	void * codec1 = NULL;
	void * codec2 = NULL;
	if (0) {
		char update[] = "\n AT \n 500 \n { \n REPLACE \n M.emissiveColor BY 1 0 0 } \n";

		codec1 = BENC_Init(NULL, argv[1], NULL);
		BENC_SaveCurrentContext(codec1, "initial_context.mp4", NULL);
		BENC_EncodeAUFromString(codec1, update, SampleCallBack, NULL);
		BENC_SaveCurrentContext(codec1, "non_aggregated_context.mp4", NULL);
		BENC_AggregateCurrentContext(codec1, NULL);
		BENC_SaveCurrentContext(codec1, "aggregated_context.mp4", NULL);
		BENC_Terminate(codec1);
	} else {

		for (i = 0; i <10; i++) {
			char context_rootname[] = "rect_context";
			char in_context[100], 
				 bt_out_na_context[100], bt_out_agg_context[100],
				 mp4_out_na_context[100], mp4_out_agg_context[100];
			char update[1000];// = "REPLACE M.emissiveColor BY 1 1 0";
			char timed_update[1000];

			sprintf(update, "REPLACE M.emissiveColor BY %f 0 0", i/10.0f);

			if (i != 0) {
				sprintf(in_context, "na_%s_%i.bt", context_rootname, i);
			} else {
				strcpy(in_context, "rect.bt");
			}

			codec2 = BENC_Init(NULL, in_context, NULL);

			sprintf(timed_update, "AT %i { %s }", 1000 + i, update);
			
			BENC_EncodeAUFromString(codec2, timed_update, SampleCallBack, NULL);

			sprintf(mp4_out_na_context, "na_%s_%i.mp4", context_rootname, i+1);
			sprintf(bt_out_na_context, "na_%s_%i.bt", context_rootname, i+1);
			sprintf(mp4_out_agg_context, "agg_%s_%i.mp4", context_rootname, i+1);
			sprintf(bt_out_agg_context, "agg_%s_%i.bt", context_rootname, i+1);

			BENC_SaveCurrentContext(codec2, mp4_out_na_context, NULL);
			BENC_SaveCurrentContext(codec2, bt_out_na_context, NULL);
			BENC_AggregateCurrentContext(codec2, NULL);
			BENC_SaveCurrentContext(codec2, mp4_out_agg_context, NULL);
			BENC_SaveCurrentContext(codec2, bt_out_agg_context, NULL);
		
			BENC_Terminate(codec2);
		}
		fprintf(stdout, "Done.\n");
	}
	return 0;
}


