comparison main/IMAFencoder.c @ 634:ba338234c001 imaf_enc

IMAF load code from Jesus Corral Garcia
author Chris Cannam
date Mon, 04 Nov 2013 17:15:52 +0000
parents
children 767789a78984
comparison
equal deleted inserted replaced
633:a8da6db5a2c9 634:ba338234c001
1 //***********************************************************//
2 // Interactive Music Audio Format (IMAF) ENCODER //
3 // Version 2.0 //
4 // //
5 // Eugenio Oñate Hospital //
6 // Jesús Corral García & Costantino Taglialatela //
7 // //
8 // Copyright (c) 2013 Centre for Digital Music (C4DM) //
9 // Queen Mary University of London. All rights reserved. //
10 //***********************************************************//
11 // main.c //
12 //***********************************************************//
13
14 //File input/output
15 #include <stdio.h>
16 //Standard library: numeric conversion, memory allocation...
17 #include <stdlib.h>
18 //Operations with strings
19 #include <string.h>
20 //Get the creation time: clock
21 #include <time.h>
22 #include "IMAFencoder.h"
23 //Qt libraries
24 #include <QTextStream>
25 #include <QMessageBox>
26 #include <QByteArray>
27
28
29 /*Prototype*/
30 void filetypebx(FileTypeBox *ftyp);
31 int mdatbox(MediaDataBox *mdat, int, FILE *imf, FILE *song, FILE *text, int, int, u32);
32 void moovheaderbox(MovieBox *moov, int, int, int, int, int, int, int);
33 int trackstructure(MovieBox *moov, int, int, int, int,const char *name);
34 int samplecontainer(MovieBox *moov, int, int, const char *name);
35 int sampledescription(MovieBox *moov, int);
36 int presetcontainer(MovieBox *moov, int, int *vol_values, int type, int fade_in);
37 int rulescontainer(MovieBox *moov, int SelRuleType, int SelRule_PAR1, int SelRule_PAR2,
38 int MixRuleType, int MixRule_PAR1, int MixRule_PAR2, int MixRule_PAR3, int MixRule_PAR4);
39 void writemoovbox(MovieBox moov, int numtrack,int totaltracks, FILE *imf, FILE *text);
40 int readTrack(MovieBox *moov, int,const char *name);
41
42 // Timed Text Functions
43 int trackstructure_text (MovieBox *moov, int numtrack, int clock, int durationTrack, int sizemdat,const char *textfile,FILE *text,int totaltracks);
44 int samplecontainer_text(MovieBox *moov, int numtrack, int sizemdat, const char *textfiles,int totaltracks);
45 int aux (FILE *text,int num,int num1,int num2,int num3,int *sal);
46 int getTextSize (FILE *text);
47 // Group and Preset functions
48 int groupcontainer(MovieBox *moov, int *group_tracks, int grp_vol, QString grp_name,
49 QString grp_description, int totaltracks);
50 void writepresets(MovieBox moov, int numtrack,int totaltracks, FILE *imf); // NOT YET USED
51
52 // MetaData and JPEG Image functions
53 int metacontainer (MetaBox *meta);
54 u32 getImageSize(const char *imag);
55 void insertImage (MetaBox *meta, FILE **imf, u32 imagesize, const char *imagedir);
56 void writemetabox(MetaBox meta, FILE *imf);
57
58 int bytereverse(int num);
59 int bytereverse16(int num);
60 int size_phrase[1000];//the total number of bytes in a phrase including modifiers
61 int phrases;//the number of phrases in the whole text
62
63
64 // Qt Widget for the "Exit Window"
65 class ExitWindow : public QWidget
66 {
67 public:
68 ExitWindow();
69 private:
70 QMessageBox *file_created;
71 };
72
73 ExitWindow::ExitWindow()
74 {
75 file_created = new QMessageBox();
76 file_created->setFixedSize(400,200); // doesn't seem to work
77 file_created->setWindowTitle("IM AF Encoder");
78 file_created->setText("IM AF file successfully created!");
79 file_created->show();
80 }
81
82 int mainIMAFencoder (int totaltracks, QString files_path[maxtracks],QString outimaf,
83 QString picturefile, QString textfile, int vol_values[maxtracks], bool HasImageFile,
84 int SelRuleType, int SelRule_PAR1, int SelRule_PAR2,
85 int MixRuleType, int MixRule_PAR1, int MixRule_PAR2, int MixRule_PAR3, int MixRule_PAR4,
86 int group_tracks[maxtracks], int group_volume, QString group_name, QString group_description,
87 int pres_type, int fade_in)
88 {
89 //Variables
90 FileTypeBox ftyp;
91 MediaDataBox mdat;
92 MovieBox moov;
93 MetaBox meta;
94
95 //Media Data Box - Contains the audio
96 FILE *song; //MP3
97 u32 sizeTRAK = 0;
98 int numtr;
99 //Output File
100 FILE *imf; //IMA
101 int numtrack, sizemdat, durationTrack;
102 //Image
103 u32 imagesize;
104 QTextStream out (stdout);
105 //Timed-Text
106 FILE *text;
107 int sizetext;
108 const char *c_str1[8];//change this value to support more than 8 audio tracks
109 const char *c_str2;
110
111 //Groups, Presets, Rules and Metadata boxes sizes
112 u32 sizeGRCO, sizePRCO, sizeRUCO, sizeMETA;
113
114 /* Obtain current time as seconds elapsed since the Epoch. */
115 time_t clock = time(NULL);
116
117
118 //INPUT: Image
119 if (HasImageFile){
120 c_str2= picturefile.toStdString().c_str(); //convert QString to const char
121 imagesize = getImageSize(c_str2);//calculate the size of the jpeg
122 } else { //if there is no image, the size is 0
123 imagesize = 0;
124 }
125
126 //INPUT: Timed-Text
127 c_str2= textfile.toStdString().c_str(); //convert Qstring to const char
128 text = fopen(c_str2, "rb");
129 sizetext= getTextSize (text); //calculate the size of the text
130 if((text)==NULL) {
131 // do something
132 }
133
134 //Create OUTPUT file (.ima)
135 imf = fopen (outimaf.toStdString().c_str(),"wb");
136
137 //Define the File Type Box
138 filetypebx(&ftyp);
139 fwrite(&ftyp, sizeof(FileTypeBox),1, imf);
140 //AUDIO
141 //Put the tracks in Media Data Box
142 for (numtr=0; numtr<totaltracks; numtr++) {
143 c_str1[numtr]= files_path[numtr].toStdString().c_str(); //convert Qstring to const char
144 song = fopen(c_str1[numtr], "rb");
145
146 //Extract the samples from the audio file and the text
147 sizemdat = mdatbox(&mdat, totaltracks, imf, song, text, numtr, sizetext, imagesize);//sizemdat is the size of one audio track
148
149 fclose(song); //Close the audio file
150 }//close for
151
152 //For each track write track information
153 durationTrack = (sizemdat*8)/128;
154
155 for (numtrack = 0; numtrack < totaltracks; numtrack++) {
156 c_str1[numtrack]= files_path[numtrack].toStdString().c_str(); //convert QString to const char
157 sizeTRAK = trackstructure(&moov, numtrack, clock, durationTrack,sizemdat,c_str1[numtrack])+ sizeTRAK;
158 }
159 //At this point, the variable sizeTRAK will be the sum of the size of the box ´trak´ in all the audio tracks.
160 //The size of the box trak of the text track will be added after.
161
162 if (HasImageFile){
163 //Meta
164 sizeMETA = metacontainer(&meta);
165
166 //Read the image from the JPEG file and write it in the IMAF file
167 c_str2= picturefile.toStdString().c_str(); //convert Qstring to const char
168 insertImage(&meta, &imf, imagesize,c_str2);
169 }
170
171 //Groups
172 sizeGRCO = groupcontainer(&moov, group_tracks, group_volume, group_name, group_description, totaltracks); //Creates the group, returns the size of the box
173
174 //Presets
175 sizePRCO = presetcontainer(&moov, totaltracks, vol_values, pres_type, fade_in); // Creates the preset, returns the size of the box.
176
177 //Rules
178 sizeRUCO = rulescontainer(&moov, SelRuleType, SelRule_PAR1, SelRule_PAR2,
179 MixRuleType, MixRule_PAR1, MixRule_PAR2, MixRule_PAR3, MixRule_PAR4); // Creates the rules, returns the size of the box.
180
181 //Text track
182 c_str2= textfile.toStdString().c_str(); //convert Qstring to const char
183 sizeTRAK = trackstructure_text (&moov, numtrack, clock, durationTrack, sizemdat, c_str2, text, totaltracks) + sizeTRAK;
184
185 //Movie Header - Overall declarations
186 moovheaderbox(&moov, clock, sizeTRAK, sizePRCO, totaltracks, durationTrack, sizeRUCO, sizeGRCO); // -> enter sizeGRCO instead of 0
187
188 //Writes the movie box into the file
189 writemoovbox(moov, numtrack, totaltracks, imf, text);
190
191 //Writes the meta box into the IMAF file
192 if (HasImageFile){
193 writemetabox(meta,imf);
194 }
195
196 //Close Files and show exit dialog window
197 fclose(imf);
198 fclose (text);
199 ExitWindow *window = new ExitWindow();
200 }
201
202
203
204 void filetypebx(FileTypeBox *ftyp){
205 int swap;
206
207 swap = bytereverse(24);// 24 is the size of the box "ftyp"
208 ftyp->size = swap;
209 swap = bytereverse('ftyp');
210 ftyp->type = swap;
211 swap = bytereverse('im02'); // Conformance point 3 (see ISO/IEC 23000-12:2010/FDAM 1:2011(E))
212 ftyp->major_brand = swap;
213 ftyp->minor_version = 0;
214 swap = bytereverse('im02'); // Conformance point 3 (see ISO/IEC 23000-12:2010/FDAM 1:2011(E))
215 ftyp->compatible_brands[0] = swap;
216 swap = bytereverse('isom');
217 ftyp->compatible_brands[1] = swap;
218 }
219
220 int mdatbox(MediaDataBox *mdat, int totaltracks, FILE *imf, FILE *song, FILE *text, int numtr, int sizetext, u32 imagesize){
221
222 int d=0, cnt=0, j, find = 0, sizestring = 0, i = 0,cnt2=0,highlight_end_time=0;
223 int dat = 0, dat1 = 0, dat2 = 0, dat3 = 0,k=0;
224 u32 size = 0, swap, sizeMDAT = 0;
225 unsigned char c1=0,c2=0,numwords=0,initposition[3000],endposition[3000];
226
227 //Positionate the pointer at the end of the file to know the size of it
228 fseek(song, 0, SEEK_END);
229 size = ftell(song);
230 //Positionate the pointer at first
231 fseek(song, 0, SEEK_SET);
232
233 initposition[0]=0; // this array saves the position of the first letter of a word in a phrase
234 phrases=0;// this variable saves the number of phrases in the whole text
235
236 //Find the header of the first frame (the beginning), when find it d=1 and jump out the loop.
237 // The header is 32 bytes. We find in groups of 8 bytes
238 // Contemplate all possible options of headers
239 while (d == 0) {
240 find = 0;
241 fread(&dat, sizeof(unsigned char), 1, song);
242 cnt++;
243
244 if (dat == 0xFF) {
245 cnt++; // cnt : stores the position of the pointer.
246 fread(&dat1, sizeof(unsigned char), 1, song);
247 cnt++;
248 fread(&dat2, sizeof(unsigned char), 1, song);
249 cnt++;
250 fread(&dat3, sizeof(unsigned char), 1, song);
251 if (dat1 == 0xFB && dat2 == 146 && dat3 == 64 ) {
252 find = 1; // find: if the header is found
253 d=1; // d: jump out the loop
254 }
255 if (dat1 == 0xFB && dat2 == 146 && dat3 == 96 ) {
256 d=1;
257 find = 1;
258 }
259 if (dat1 == 0xFB && dat2 == 144 && dat3 == 64 ) {
260 find = 1;
261 d=1;
262 }
263 if (dat1 == 0xFB && dat2 == 144 && dat3 == 96 ) {
264 find = 1;
265 d=1;
266 }
267 if (dat1 == 0xFB && dat2 == 146 && dat3 == 100 ) {
268 d=1;
269 find = 1;
270 }
271 if (dat1 == 0xFB && dat2 == 144 && dat3 == 100 ) {
272 find = 1;
273 d=1;
274 }
275 if (dat1 == 0xFA && dat2 == 146 && dat3 == 64 ) {
276 find = 1;
277 d=1;
278 }
279 if (dat1 == 0xFA && dat2 == 146 && dat3 == 96 ) {
280 d=1;
281 find = 1;
282 }
283 if (dat1 == 0xFA && dat2 == 144 && dat3 == 64 ) {
284 find = 1;
285 d=1;
286 }
287 if (dat1 == 0xFA && dat2 == 144 && dat3 == 96 ) {
288 find = 1;
289 d=1;
290 }
291 if (dat1 == 0xFA && dat2 == 146 && dat3 == 100 ) {
292 d=1;
293 find = 1;
294 }
295 if (dat1 == 0xFA && dat2 == 144 && dat3 == 100 ) {
296 find = 1;
297 d=1;
298 }
299 if (find == 0) {
300 fseek(song, -3, SEEK_CUR); // if 3 last bytes in the header are not correct
301 cnt = cnt - 3;
302 }
303 } // close if
304 if (cnt == size) { //if we have readen all the bytes in the audio file
305 d = 1;
306 }
307 }//close while
308 size = size - (cnt - 4); // Calculate the size of the samples. size = pos. end of file - pos. first header.
309 if (numtr == 0) { //if it is the first audio track the code writes the mdat size
310 sizeMDAT = size*totaltracks + 8 + sizetext + imagesize; // size of the whole media box -> INCLUDING IMAGE and TEXT SIZE.The text size does not include modifiers
311 swap = bytereverse(sizeMDAT);
312 fwrite(&swap, sizeof(u32), 1, imf);
313 swap = bytereverse('mdat');
314 mdat->type = swap;
315 fwrite(&mdat->type, sizeof(u32), 1, imf);
316 }
317 fseek(song, cnt - 4, SEEK_SET);
318 for (j=0; j<size; j++) { //read all the samples of one track and writes them in the IM AF file
319 fread(&mdat->data, sizeof(char), 1, song);
320 fwrite(&mdat->data, sizeof(char), 1, imf);
321 }
322
323 // copy the text in the 3gp to the ima and add the text modifiers
324
325 cnt=0;// the total number of bytes of the whole text including modifiers
326 fseek(text,0,SEEK_CUR);
327 sizestring=0;//number of bytes of a phrase (without modifiers)
328 initposition[0]=0;// this array saves the initial position of a word
329 if(numtr==totaltracks-1){ // writes the text after the samples of all the audio tracks
330 j=0;cnt=0;
331 while(j<sizetext){ //this loop reads the whole text
332 cnt2=0;//the total number of bytes of a phrase including the modifiers
333 fread(&c1,sizeof(char),1,text);
334 fread(&c2,sizeof(char),1,text);
335 fwrite(&c1,sizeof(char),1,imf);//two bytes of sizestring
336 fwrite(&c2,sizeof(char),1,imf);
337 sizestring = (c1<<8) | (c2);//sizestring is the size of one phrase contained in 3gp
338 j=j+2;
339 cnt=cnt+2;
340 cnt2=cnt2+2;
341 phrases++;//the number of phrases in the whole text
342 numwords=0;// the number of words in a phrase
343 initposition[0]=0;//this array saves the first position of a word in a phrase
344 endposition[0]=0;// this array saves the last position of a word in a phrase
345 k=0;//the index for endposition array and initposition array
346 for(i=0;i< sizestring;i++){
347 fread(&mdat->data,sizeof(char),1,text);
348 fwrite(&mdat->data,sizeof(char),1,imf);
349 j++;
350 cnt=cnt+1;
351 cnt2=cnt2+1;
352 if(mdat->data==0x20){ //a blank space separates two words. 0x20 = blank space
353
354 numwords++;
355 endposition[k]=i;
356 initposition[k+1]=i+1;
357 k++;
358 }
359
360
361 } //close for
362 endposition[k]=sizestring-1;//saves the last position of the last word in a phrase
363 numwords++;
364
365
366 mdat->data=0x00;
367 fwrite(&mdat->data,sizeof(char),1,imf);//hclr size
368 fwrite(&mdat->data,sizeof(char),1,imf);//hclr size
369 fwrite(&mdat->data,sizeof(char),1,imf);//hclr size
370 mdat->data=0x0C;
371 fwrite(&mdat->data,sizeof(char),1,imf); //hclr size
372 fwrite("h",sizeof(char),1,imf);
373 fwrite("c",sizeof(char),1,imf);
374 fwrite("l",sizeof(char),1,imf);
375 fwrite("r",sizeof(char),1,imf);
376 mdat->data=0xFF;
377 fwrite(&mdat->data,sizeof(char),1,imf);//highlight color rgba
378 mdat->data=0x62;
379 fwrite(&mdat->data,sizeof(char),1,imf);//highlight color rgba
380 mdat->data=0x04;
381 fwrite(&mdat->data,sizeof(char),1,imf);//highlight color rgba
382 mdat->data=0xFF;
383 fwrite(&mdat->data,sizeof(char),1,imf);//highlight color rgba
384 mdat->data=0x00;
385 fwrite(&mdat->data,sizeof(char),1,imf); //krok size
386 mdat->data=0x00;
387 fwrite(&mdat->data,sizeof(char),1,imf);//krok size
388 mdat->data=0x00;
389 fwrite(&mdat->data,sizeof(char),1,imf);//krok size
390 mdat->data=14+(8*numwords);
391 fwrite(&mdat->data,sizeof(char),1,imf); //krok size
392 fwrite("k",sizeof(char),1,imf);
393 fwrite("r",sizeof(char),1,imf);
394 fwrite("o",sizeof(char),1,imf);
395 fwrite("k",sizeof(char),1,imf);
396 mdat->data=0x00;
397 fwrite(&mdat->data,sizeof(char),1,imf);//highlight-start-time
398 mdat->data=0x00;
399 fwrite(&mdat->data,sizeof(char),1,imf);//highlight-start-time
400 mdat->data=0x00;
401 fwrite(&mdat->data,sizeof(char),1,imf);//highlight-start-time
402 mdat->data=0x00;
403 fwrite(&mdat->data,sizeof(char),1,imf);//highlight-start-time
404 mdat->data=0x00;
405 fwrite(&mdat->data,sizeof(char),1,imf);//entry-count
406 mdat->data=numwords;
407 fwrite(&mdat->data,sizeof(char),1,imf);//entry-count
408
409 for(i=0;i<numwords;i++){
410
411 mdat->data=0x00;
412 fwrite(&mdat->data,sizeof(char),1,imf);//highlight-end-time
413 mdat->data=0x00;
414 fwrite(&mdat->data,sizeof(char),1,imf);//highlight-end-time
415
416
417 if(i==numwords-1){ //if it is the last word in a phrase we put this value
418 mdat->data=0xFF;
419 fwrite(&mdat->data,sizeof(char),1,imf);//highlight-end-time
420 mdat->data=0xFF;
421 fwrite(&mdat->data,sizeof(char),1,imf);//highlight-end-time
422
423 }
424
425 else{ //if it is not the last word in a phrase
426
427 highlight_end_time = (i+1)*(11/numwords);//change the value '11' in order to increase o decrease the speed of highlight
428 mdat->data= highlight_end_time;
429 fwrite(&mdat->data,sizeof(char),1,imf);//highlight-end-time
430 mdat->data=0xFF;
431 fwrite(&mdat->data,sizeof(char),1,imf);//highlight-end-time
432
433
434 }
435
436 mdat->data=0x00;
437 fwrite(&mdat->data,sizeof(char),1,imf);//startcharoffset
438 mdat->data=initposition[i];
439 fwrite(&mdat->data,sizeof(char),1,imf);//startcharoffset
440 mdat->data=0x00;
441 fwrite(&mdat->data,sizeof(char),1,imf);//endcharoffset
442 mdat->data=endposition[i]+1;
443 fwrite(&mdat->data,sizeof(char),1,imf);//endcharoffset
444
445 }//close for
446
447 cnt=cnt+26+(numwords*8);//cnt is the number of bytes of the whole text including modifiers
448 cnt2=cnt2+26+(numwords*8); //cnt2 is the number of bytes of a phrase including the modifiers
449
450 size_phrase[phrases-1]=cnt2 ;
451
452 } //close while
453
454 sizeMDAT = size*totaltracks + 8 + cnt + imagesize; // size value must include image and text sizes
455 swap = bytereverse(sizeMDAT);
456 fseek(imf,-(sizeMDAT-imagesize),SEEK_CUR); //overwrittes sizeMDAT with the total size (Image is yet to be written in the file at this point)
457 fwrite(&swap, sizeof(u32), 1, imf);
458 fseek(imf,(sizeMDAT-imagesize)-4,SEEK_CUR); // (Image is yet to be written in the file at this point)
459 } // close if (numtr==totaltracks-1)
460
461 fclose(song);
462
463 return size;
464 }
465
466 int samplecontainer(MovieBox *moov, int numtrack, int sizemdat, const char *name){
467
468 u32 sizeSTSD, sizeSTSZ, swap, num_samples, dat=0;
469 u32 sizetime, sizeSTTS; //Time to Sample Box
470 u32 sizeSTSC = 28; //Sample to Chunck
471 u32 sizeSTCO = 20; //Chunck offset
472 u32 sizeSTBL; //Sample Table Box //
473
474 //Sample Description Box//
475 sizeSTSD = sampledescription(moov, numtrack);
476
477 //Sample size box//
478 swap = bytereverse('stsz');
479 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
480 SampleSizeBox.type = swap;
481 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
482 SampleSizeBox.version = 0;
483 //Read Track: Frame size and Decoder Times
484 num_samples = readTrack(moov, numtrack, name);
485 sizeSTSZ = num_samples*4 + 20;
486 swap = bytereverse(sizeSTSZ);
487 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
488 SampleSizeBox.size = swap;
489
490 //Time To Sample Box//
491 sizetime = bytereverse(moov->TrackBox[numtrack].MediaBox.MediaInformationBox.
492 SampleTableBox.TimeToSampleBox.entry_count);
493 sizeSTTS = 16 + sizetime*4*2;
494 swap = bytereverse(sizeSTTS);
495 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
496 TimeToSampleBox.size = swap;
497 swap = bytereverse('stts');
498 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
499 TimeToSampleBox.type = swap;
500 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
501 TimeToSampleBox.version = 0;
502
503 //Sample To Chunk//
504 swap = bytereverse(sizeSTSC);
505 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
506 SampleToChunk.size = swap;
507 swap = bytereverse('stsc');
508 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
509 SampleToChunk.type = swap;
510 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
511 SampleToChunk.version = 0;
512 swap = bytereverse(1);
513 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
514 SampleToChunk.entry_count = swap;
515 swap = bytereverse(1);
516 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
517 SampleToChunk.first_chunk = swap;
518 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
519 SampleToChunk.samples_per_chunk = moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleSizeBox.sample_count;
520 swap = bytereverse(1);
521 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
522 SampleToChunk.sample_description_index = swap;
523
524 //Chunk Offset Box//
525 swap = bytereverse(sizeSTCO);
526 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
527 ChunkOffsetBox.size = swap;
528 swap = bytereverse('stco');
529 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
530 ChunkOffsetBox.type = swap;
531 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
532 ChunkOffsetBox.version = 0;
533 swap = bytereverse(1);
534 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
535 ChunkOffsetBox.entry_count = swap;
536 dat = 32 + sizemdat*numtrack;
537 swap = bytereverse(dat);
538 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
539 ChunkOffsetBox.chunk_offset[numtrack] = swap;
540
541 //Sample Table Box //
542 sizeSTBL = 8 + sizeSTSD + sizeSTSZ + sizeSTSC + sizeSTCO + sizeSTTS;
543 swap = bytereverse(sizeSTBL);
544 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.size = swap;
545 swap = bytereverse('stbl');
546 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.type =swap;
547
548 return sizeSTBL;
549 }
550
551 int sampledescription(MovieBox *moov, int numtrack){
552
553 u32 swap, sizeESD = 35;
554
555 u32 sizeMP4a; //Audio Sample Entry//
556 u32 sizeSTSD; //Sample description box //
557
558
559 swap = bytereverse(sizeESD);
560 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
561 SampleDescriptionBox.AudioSampleEntry.ESbox.size = swap;
562 swap = bytereverse('esds');
563 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
564 SampleDescriptionBox.AudioSampleEntry.ESbox.type = swap;
565 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
566 SampleDescriptionBox.AudioSampleEntry.ESbox.version = 0;
567
568 //ES Descriptor//
569 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
570 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.tag = 3;
571 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
572 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.length = 21;
573 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
574 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.ES_ID = 0;
575 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
576 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.mix = 0;
577
578 //Decoder config descriptor//
579 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
580 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.
581 DecoderConfigDescriptor.tag = 4;
582 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
583 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.
584 DecoderConfigDescriptor.length = 13;
585 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
586 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.
587 DecoderConfigDescriptor.objectProfileInd = 0x6B;
588 swap = bytereverse(0x150036B0);
589 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
590 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.
591 DecoderConfigDescriptor.mix = swap;
592 swap = bytereverse(128);
593 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
594 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.
595 DecoderConfigDescriptor.maxBitRate = swap;
596 swap = bytereverse(128);
597 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
598 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.
599 DecoderConfigDescriptor.avgBitrate = swap;
600
601 //SLConfig Descriptor//
602 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
603 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.
604 SLConfigDescriptor.tag = 6;
605 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
606 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.
607 SLConfigDescriptor.length = 1;
608 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
609 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.
610 SLConfigDescriptor.predifined = 2;
611
612 //Audio Sample Entry//
613 sizeMP4a = 36 + sizeESD;
614 swap = bytereverse(sizeMP4a);
615 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
616 SampleDescriptionBox.AudioSampleEntry.size = swap;
617 swap = bytereverse('mp4a');
618 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
619 SampleDescriptionBox.AudioSampleEntry.type =swap;
620 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
621 SampleDescriptionBox.AudioSampleEntry.reserved[0] = 0;
622 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
623 SampleDescriptionBox.AudioSampleEntry.reserved[1] = 0;
624 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
625 SampleDescriptionBox.AudioSampleEntry.reserved[2] = 0;
626 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
627 SampleDescriptionBox.AudioSampleEntry.reserved[3] = 0;
628 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
629 SampleDescriptionBox.AudioSampleEntry.reserved[4] = 0;
630 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
631 SampleDescriptionBox.AudioSampleEntry.reserved[5] = 0;
632 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
633 SampleDescriptionBox.AudioSampleEntry.data_reference_index = bytereverse16(1);
634 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
635 SampleDescriptionBox.AudioSampleEntry.reserved2[0] = 0;
636 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
637 SampleDescriptionBox.AudioSampleEntry.reserved2[1] = 0;
638 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
639 SampleDescriptionBox.AudioSampleEntry.channelcount = 512;
640 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
641 SampleDescriptionBox.AudioSampleEntry.samplesize = 4096; // 16 bits
642 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
643 SampleDescriptionBox.AudioSampleEntry.reserved3 = 0;
644 swap = 44100 << 16;
645 swap = bytereverse(swap);
646 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
647 SampleDescriptionBox.AudioSampleEntry.samplerate = swap;
648
649 //Sample description box //
650 sizeSTSD = 16 + sizeMP4a;
651 swap = bytereverse(sizeSTSD);
652 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
653 SampleDescriptionBox.size = swap;
654 swap = bytereverse('stsd');
655 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
656 SampleDescriptionBox.type = swap;
657 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
658 SampleDescriptionBox.version = 0;
659 swap = bytereverse(1);
660 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
661 SampleDescriptionBox.entry_count = swap;
662
663 return sizeSTSD;
664 }
665
666 int readTrack (MovieBox *moov, int numtrack,const char *name){
667
668 int t=0,k=1, l =0;
669
670 FILE *song;
671 int d=0, cnt = 0, i=0, j=0, cnt2 = 0, find = 0, swap, num_entr = 0;
672 int dat = 0, dat1 = 0, dat2 = 0, dat3 = 0, num_frame = 0, end =0, pos = 0;
673 u32 size[9000];
674
675 //Open the audio file with the name introduced by the user
676 song = fopen (name,"rb");
677 if (song == NULL) {
678 printf("Error opening input file\n");
679 system("pause");
680 exit(1);
681 }
682 //Calculate the size of the track
683 fseek(song, 0, SEEK_END);
684 end = ftell(song);
685 fseek(song, 0, SEEK_SET);
686 d=0, i=0;
687 //Search for each frame one by one, and extratcs the information
688 while (d == 0) {
689 find = 0;
690 fread(&dat, sizeof(unsigned char), 1, song);
691 cnt++;
692
693 if (dat == 0xFF) {
694 cnt++;
695 fread(&dat1, sizeof(unsigned char), 1, song);
696 cnt++;
697 fread(&dat2, sizeof(unsigned char), 1, song);
698 cnt++;
699 fread(&dat3, sizeof(unsigned char), 1, song);
700 if (dat1 == 0xFB && dat2 == 146 && dat3 == 64 ) {
701 pos = cnt - 4; //Pos of the beginning of the frame
702 size[num_frame] = pos - cnt2; //Size of one frame
703 cnt2 = pos; //Pos of the next frame
704 find = 1;
705 num_frame ++; //Number of frames
706 }
707 if (dat1 == 0xFB && dat2 == 146 && dat3 == 96 ) {
708 pos = cnt - 4;
709 size[num_frame] = pos - cnt2;
710 cnt2 = pos;
711 find = 1;
712 num_frame ++;
713 }
714 if (dat1 == 0xFB && dat2 == 144 && dat3 == 64 ) {
715 pos = cnt - 4;
716 size[num_frame] = pos - cnt2;
717 cnt2 = pos;
718 find = 1;
719 num_frame ++;
720 }
721 if (dat1 == 0xFB && dat2 == 144 && dat3 == 96 ) {
722 pos = cnt - 4;
723 size[num_frame] = pos - cnt2;
724 cnt2 = pos;
725 find = 1;
726 num_frame ++;
727 }
728 if (dat1 == 0xFB && dat2 == 146 && dat3 == 100 ) {
729 pos = cnt - 4;
730 size[num_frame] = pos - cnt2;
731 cnt2 = pos;
732 find = 1;
733 num_frame ++;
734 }
735 if (dat1 == 0xFB && dat2 == 144 && dat3 == 100 ) {
736 pos = cnt - 4;
737 size[num_frame] = pos - cnt2;
738 cnt2 = pos;
739 find = 1;
740 num_frame ++;
741 }
742 if (dat1 == 0xFA && dat2 == 146 && dat3 == 64 ) {
743 pos = cnt - 4;
744 size[num_frame] = pos - cnt2;
745 cnt2 = pos;
746 find = 1;
747 num_frame ++;
748 }
749 if (dat1 == 0xFA && dat2 == 146 && dat3 == 96 ) {
750 pos = cnt - 4;
751 size[num_frame] = pos - cnt2;
752 cnt2 = pos;
753 find = 1;
754 num_frame ++;
755 }
756 if (dat1 == 0xFA && dat2 == 144 && dat3 == 64 ) {
757 pos = cnt - 4;
758 size[num_frame] = pos - cnt2;
759 cnt2 = pos;
760 find = 1;
761 num_frame ++;
762 }
763 if (dat1 == 0xFA && dat2 == 144 && dat3 == 96 ) {
764 pos = cnt - 4;
765 size[num_frame] = pos - cnt2;
766 cnt2 = pos;
767 find = 1;
768 num_frame ++;
769 }
770 if (dat1 == 0xFA && dat2 == 146 && dat3 == 100 ) {
771 pos = cnt - 4;
772 size[num_frame] = pos - cnt2;
773 cnt2 = pos;
774 find = 1;
775 num_frame ++;
776 }
777 if (dat1 == 0xFA && dat2 == 144 && dat3 == 100 ) {
778 pos = cnt - 4;
779 size[num_frame] = pos - cnt2;
780 cnt2 = pos;
781 find = 1;
782 num_frame ++;
783 }
784 if (find == 0) { //In case it does not find the header.
785 //It keeps reading next data without jump any position
786 fseek(song, -3, SEEK_CUR);
787 cnt = cnt - 3;
788 }
789 }
790
791 if (cnt == end) {
792 pos = cnt;
793 size[num_frame] = pos - cnt2;
794 d = 1;
795 }
796 }
797
798 //Save Samples size//
799 swap = bytereverse(num_frame);
800 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
801 SampleSizeBox.sample_count = swap;
802 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
803 SampleSizeBox.sample_size = 0;
804
805 for (i=0; i< num_frame; i++) {
806 swap = bytereverse(size[i+1]);
807 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
808 SampleSizeBox.entry_size[i] = swap;
809 }
810
811 //Save Decoding Times//
812 //Writes manually the duration of each frame.
813 //Follows the following structure:
814 // 7 frames of 26 ms
815 // 1 frame of 27 ms
816 // ...
817 // And each 13 rows it writes
818 // 8 frames of 26 ms
819 // 1 frame of 27 ms
820 //It is done for adjusting the different durations of each frame.
821 // as they vary between 26.125 ms and 26.075 ms
822
823 swap = bytereverse(1);
824 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
825 TimeToSampleBox.sample_count[0] = swap;
826 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
827 TimeToSampleBox.sample_delta[0] =0;
828 // int t=0,k=1, l =0;
829 num_entr = 1;
830 j = 0;
831 for (i = 1; i< num_frame; i++) {
832 if (j == 8 && l == 0) {
833 swap = bytereverse(7);
834 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
835 TimeToSampleBox.sample_count[num_entr] = swap;
836 swap = bytereverse(26);
837 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
838 TimeToSampleBox.sample_delta[num_entr] =swap;
839 num_entr ++;
840
841 swap = bytereverse(1);
842 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
843 TimeToSampleBox.sample_count[num_entr] = swap;
844 swap = bytereverse(27);
845 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
846 TimeToSampleBox.sample_delta[num_entr] =swap;
847 num_entr++;
848 j=0;
849 dat = i;
850 if (k == 6 && t == 0) {
851 l = 1;
852 t = 1;
853 k = 1;
854 }
855 if (k == 6 && t ==1) {
856 l = 1;
857 k = 1;
858 }
859 k++;
860 }
861
862 if (j == 9 && l == 1) {
863
864 swap = bytereverse(8);
865 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
866 TimeToSampleBox.sample_count[num_entr] = swap;
867 swap = bytereverse(26);
868 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
869 TimeToSampleBox.sample_delta[num_entr] =swap;
870 num_entr ++;
871
872 swap = bytereverse(1);
873 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
874 TimeToSampleBox.sample_count[num_entr] = swap;
875 swap = bytereverse(27);
876 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
877 TimeToSampleBox.sample_delta[num_entr] =swap;
878 num_entr++;
879 j=0;
880 dat = i;
881 l = 0;
882 }
883 j++;
884 }
885
886 dat = num_frame - dat;
887
888 swap = bytereverse(dat);
889 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
890 TimeToSampleBox.sample_count[num_entr] = swap;
891 swap = bytereverse(26);
892 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
893 TimeToSampleBox.sample_delta[num_entr] =swap;
894 num_entr++;
895 swap = bytereverse(num_entr);
896 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
897 TimeToSampleBox.entry_count = swap;
898
899 fclose(song);
900 return num_frame;
901
902 }
903
904 int trackstructure (MovieBox *moov, int numtrack, int clock,
905 int durationTrack, int sizemdat,const char *name){
906 int swap;
907
908 int sizeSTBL; //Sample Table Box
909 u32 sizeURL; //Data Entry Url Box
910 u32 sizeDREF; //Data Reference
911 u32 sizeSMHD; //Sound Header Box
912 u32 sizeDINF; //Data information Box
913 u32 sizeMINF; //Media Information Box//
914 u32 sizeHDLR; //Handler Box//
915 u32 sizeMDHD; //Media Header Box//
916 u32 sizeMDIA; //Media Box//
917 u32 sizeTKHD; //Track Header//
918 u32 sizeTRAK; //Track container
919
920 //Sample Table Box
921 sizeSTBL = 0;
922 sizeSTBL = samplecontainer(moov, numtrack,sizemdat, name);
923
924 //Data Entry Url Box
925 sizeURL = 12;
926 swap = bytereverse(sizeURL);
927 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
928 DataReferenceBox.DataEntryUrlBox.size = swap;
929 swap = bytereverse('url ');
930 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
931 DataReferenceBox.DataEntryUrlBox.type = swap;
932 swap = bytereverse(1);
933 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
934 DataReferenceBox.DataEntryUrlBox.flags = swap; // =1 Track in same file as movie atom.
935
936 //Data Reference
937 sizeDREF = sizeURL+ 16;
938 swap = bytereverse(sizeDREF);
939 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
940 DataReferenceBox.size = swap;
941 swap = bytereverse('dref');
942 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
943 DataReferenceBox.type = swap;
944 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
945 DataReferenceBox.flags = 0;
946 swap = bytereverse(1);
947 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
948 DataReferenceBox.entry_count = swap;
949
950 //Data information Box//
951 sizeDINF = sizeDREF + 8;
952 swap = bytereverse(sizeDINF);
953 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.size = swap;
954 swap = bytereverse('dinf');
955 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.type = swap;
956
957 //Sound Header Box //
958 sizeSMHD = 16;
959 swap = bytereverse(sizeSMHD);
960 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SoundMediaHeaderBox.size = swap;
961 swap = bytereverse('smhd');
962 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SoundMediaHeaderBox.type = swap;
963 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SoundMediaHeaderBox.version = 0;
964 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SoundMediaHeaderBox.balance = 0;
965 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SoundMediaHeaderBox.reserved = 0;
966
967 //Media Information Box//
968 sizeMINF = sizeDINF + sizeSMHD + sizeSTBL + 8;
969 swap = bytereverse(sizeMINF);
970 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.size = swap;
971 swap = bytereverse('minf');
972 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.type = swap;
973
974 //Handler Box//
975 sizeHDLR = 37;
976 swap = bytereverse(sizeHDLR);
977 moov->TrackBox[numtrack].MediaBox.HandlerBox.size = swap;
978 swap = bytereverse('hdlr');
979 moov->TrackBox[numtrack].MediaBox.HandlerBox.type = swap;
980 moov->TrackBox[numtrack].MediaBox.HandlerBox.version = 0;
981 moov->TrackBox[numtrack].MediaBox.HandlerBox.pre_defined = 0;
982 swap = bytereverse('soun');
983 moov->TrackBox[numtrack].MediaBox.HandlerBox.handler_type = swap;
984 moov->TrackBox[numtrack].MediaBox.HandlerBox.reserved[0] = 0;
985 moov->TrackBox[numtrack].MediaBox.HandlerBox.reserved[1] = 0;
986 moov->TrackBox[numtrack].MediaBox.HandlerBox.reserved[2] = 0;
987 //swap = bytereverse('soun');
988 //moov->TrackBox[numtrack].MediaBox.HandlerBox.data = swap;
989 moov->TrackBox[numtrack].MediaBox.HandlerBox.data[0] = 's';
990 moov->TrackBox[numtrack].MediaBox.HandlerBox.data[1] = 'o';
991 moov->TrackBox[numtrack].MediaBox.HandlerBox.data[2] = 'u';
992 moov->TrackBox[numtrack].MediaBox.HandlerBox.data[3] = 'n';
993 moov->TrackBox[numtrack].MediaBox.HandlerBox.data[4] = '\0';
994
995 //Media Header Box//
996 sizeMDHD = 32;
997 swap = bytereverse(sizeMDHD);
998 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.size = swap;
999 swap = bytereverse('mdhd');
1000 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.type = swap;
1001 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.version = 0;
1002 swap = bytereverse(clock);
1003 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.creation_time = swap;
1004 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.modification_time = swap;
1005 swap = bytereverse(1000);
1006 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.timescale = swap;
1007 swap = bytereverse(durationTrack);
1008 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.duration = swap;
1009 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.language = 0xC455;
1010 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.pre_defined = 0;
1011
1012 //Media Box//
1013 sizeMDIA = sizeMDHD + sizeHDLR + sizeMINF + 8;
1014 swap = bytereverse(sizeMDIA);
1015 moov->TrackBox[numtrack].MediaBox.size = swap;
1016 swap = bytereverse('mdia');
1017 moov->TrackBox[numtrack].MediaBox.type = swap;
1018
1019 //Track Header//
1020 sizeTKHD = 92;
1021 swap = bytereverse (sizeTKHD);
1022 moov->TrackBox[numtrack].TrackHeaderBox.size = swap;
1023 swap = bytereverse ('tkhd');
1024 moov->TrackBox[numtrack].TrackHeaderBox.type = swap ;
1025 swap = bytereverse (0x00000006);
1026 moov->TrackBox[numtrack].TrackHeaderBox.version = swap;
1027 swap = bytereverse (clock);
1028 moov->TrackBox[numtrack].TrackHeaderBox.creation_time = swap;
1029 moov->TrackBox[numtrack].TrackHeaderBox.modification_time = swap;
1030 swap = bytereverse (numtrack+1);
1031 moov->TrackBox[numtrack].TrackHeaderBox.track_ID = swap; //From 0x00000001 - 0x7FFFFFFF (dec 2147483647)
1032 moov->TrackBox[numtrack].TrackHeaderBox.reserved = 0;
1033 swap = bytereverse (durationTrack);
1034 moov->TrackBox[numtrack].TrackHeaderBox.duration = swap;
1035 moov->TrackBox[numtrack].TrackHeaderBox.reserved2[0] = 0;
1036 moov->TrackBox[numtrack].TrackHeaderBox.reserved2[1] = 0;
1037 moov->TrackBox[numtrack].TrackHeaderBox.layer = 0;
1038 moov->TrackBox[numtrack].TrackHeaderBox.alternate_group = 0;
1039 moov->TrackBox[numtrack].TrackHeaderBox.volume = 0x1;
1040 moov->TrackBox[numtrack].TrackHeaderBox.reserved3 = 0;
1041 swap = bytereverse (0x00010000);
1042 moov->TrackBox[numtrack].TrackHeaderBox.matrix[0] = swap;
1043 moov->TrackBox[numtrack].TrackHeaderBox.matrix[1] = 0;
1044 moov->TrackBox[numtrack].TrackHeaderBox.matrix[2] = 0;
1045 moov->TrackBox[numtrack].TrackHeaderBox.matrix[3] = 0;
1046 moov->TrackBox[numtrack].TrackHeaderBox.matrix[4] = swap;
1047 moov->TrackBox[numtrack].TrackHeaderBox.matrix[5] = 0;
1048 moov->TrackBox[numtrack].TrackHeaderBox.matrix[6] = 0;
1049 moov->TrackBox[numtrack].TrackHeaderBox.matrix[7] = 0;
1050 swap = bytereverse(0x40000000);
1051 moov->TrackBox[numtrack].TrackHeaderBox.matrix[8] = swap;
1052 moov->TrackBox[numtrack].TrackHeaderBox.width = 0; //just for video
1053 moov->TrackBox[numtrack].TrackHeaderBox.height = 0; //just for video
1054
1055 //Track container
1056 sizeTRAK = sizeTKHD + sizeMDIA + 8;
1057 swap = bytereverse (sizeTRAK); // Size of one track
1058 moov->TrackBox[numtrack].size = swap;
1059 swap = bytereverse ('trak');
1060 moov->TrackBox[numtrack].type = swap;
1061 return sizeTRAK;
1062
1063 }
1064
1065 int groupcontainer(MovieBox *moov, int *group_tracks, int grp_vol, QString grp_name,
1066 QString grp_description, int totaltracks) {
1067
1068 int i, j, k, numgroups, sizeCont;
1069 int numel = 0;
1070 int sizeBox = 0;
1071 int tempsize = 0;
1072 int active, activenum, addsize;
1073
1074 // ADDED FOR SV
1075 for (j=0; j<totaltracks; j++){
1076 if (group_tracks[j]==1){
1077 numel++;
1078 }
1079 }
1080
1081 if (numel==0){
1082 numgroups = 0;
1083 }else{
1084 numgroups = 1;
1085 }
1086 //
1087
1088 moov->GroupContainerBox.num_groups = bytereverse16(numgroups);
1089
1090 for (i=0; i<numgroups; i++){
1091 addsize = 0;
1092 tempsize = 0;
1093 moov->GroupContainerBox.GroupBox[i].group_ID = bytereverse(2147483649+i+1); // group_ID shall be represented from 0x80000000 to 0xFFFFFFFF
1094 strcpy(moov->GroupContainerBox.GroupBox[i].group_name, grp_name.toStdString().c_str());
1095 strcpy(moov->GroupContainerBox.GroupBox[i].group_description, grp_description.toStdString().c_str());
1096
1097 // numel = 0; // uncomment for more than one group and remove initial "for" with numel
1098 k = 0;
1099 for (j=0; j<totaltracks; j++){
1100 if (group_tracks[j]==1){
1101 moov->GroupContainerBox.GroupBox[i].groupElemId[k].element_ID = bytereverse(j+1);
1102 // numel++; // uncomment for more than one group and remove initial "for" with numel
1103 addsize += 4;
1104 k++;
1105 }
1106 }
1107 moov->GroupContainerBox.GroupBox[i].num_elements = bytereverse16(numel);
1108
1109 // printf("Choose the activation mode: ");
1110 // printf("Activation mode\n");
1111 // printf(" - Switch on the MINIMUN number of elements (0, if no Min/Max rule) [0]\n");
1112 // printf(" - Switch on the MAXIMUM number of elements (All tracks, if no Min/Max rule) [1]\n");
1113 // printf(" - Switch on the defined number of elements (ONLY IF there is Min/Max rule) [2]\n");
1114 // scanf("%d",&active);
1115 // fflush(stdin);
1116
1117 active = 1; // ADDED FOR SV, All tracks enabled
1118
1119 moov->GroupContainerBox.GroupBox[i].group_activation_mode = active;
1120 activenum = 0;
1121 moov->GroupContainerBox.GroupBox[i].group_activation_elements_number = activenum;
1122 if (active==2){
1123 printf("Activation elements number: ");
1124 scanf("%d",&activenum);
1125 moov->GroupContainerBox.GroupBox[i].group_activation_elements_number = bytereverse16(activenum);
1126 addsize += 2;
1127 }
1128
1129 moov->GroupContainerBox.GroupBox[i].group_reference_volume = bytereverse16(grp_vol*256/100); // define 8.8 fixed point
1130
1131 tempsize = 75 + addsize; // ---> before was 66 + addsize;
1132 moov->GroupContainerBox.GroupBox[i].size = bytereverse(tempsize);
1133 moov->GroupContainerBox.GroupBox[i].type = bytereverse('grup');
1134 moov->GroupContainerBox.GroupBox[i].version = bytereverse(0x02); // flags = Display enable, Edit disable
1135
1136 sizeBox += tempsize;
1137
1138 } // close for (numgroups)
1139
1140 sizeCont = sizeBox + 10;
1141
1142 moov->GroupContainerBox.size = bytereverse(sizeCont);
1143 moov->GroupContainerBox.type = bytereverse('grco');
1144
1145 return sizeCont;
1146 }
1147
1148 int presetcontainer(MovieBox *moov, int totaltracks, int *vol_values, int prestype, int fade_in){
1149
1150 int temp, i, j, k, m, t, vol;
1151 int numpres, eq;
1152 //char name[50];
1153 u32 sizePRST = 0;
1154 u32 sizePRCO = 0;
1155 u32 sizeEQ;
1156 u32 addsize = 0;
1157 int updates = 0;
1158
1159 numpres = 1; // ADDED FOR SV
1160
1161 //Preset Box//
1162 for (i=0; i<numpres; i++) {
1163 // printf("\nPRESET LIST:\n");
1164 // printf(" - Static track volume [0]\n");
1165 // printf(" - Static object volume [1]\n");
1166 // printf(" - Dynamic track volume [2]\n");
1167 // printf(" - Dynamic object volume [3]\n");
1168 // printf(" - Dynamic track approximated volume [4]\n");
1169 // printf(" - Dynamic object approximated volume [5]\n");
1170 // printf(" - Static track volume with Equalization [6]\n"); // count preset_type from 8
1171 // printf(" - Static object volume with Equalization [7]\n");
1172 // printf(" - Dynamic track volume with Equalization [8]\n");
1173 // printf(" - Dynamic object volume with Equalization [9]\n");
1174 // printf(" - Dynamic track approximated with Equalization [10]\n");
1175 // printf(" - Dynamic object approximated with Equalization [11]\n");
1176 // printf("\nPlease make your choice: ");
1177 // scanf("%d", &prestype);
1178 // fflush(stdin);
1179
1180 //PresetBOX
1181 // Box size specified in respective case
1182 moov->PresetContainerBox.PresetBox[i].type = bytereverse('prst');
1183 moov->PresetContainerBox.PresetBox[i].flags = bytereverse(0x02); // Display Enable Edit Disable
1184 moov->PresetContainerBox.PresetBox[i].preset_ID = i+1;
1185 moov->PresetContainerBox.PresetBox[i].num_preset_elements = totaltracks; // All the tracks are involved in the preset
1186 for (j=0; j<totaltracks; j++) {
1187 moov->PresetContainerBox.PresetBox[i].presElemId[j].preset_element_ID = bytereverse(j+1);
1188 }
1189 moov->PresetContainerBox.PresetBox[i].preset_global_volume = 100;
1190
1191 // prestype = 0; // ADDED FOR SV
1192
1193 switch (prestype) {
1194 case 0: moov->PresetContainerBox.PresetBox[i].preset_type = 0;
1195 strcpy(moov->PresetContainerBox.PresetBox[i].preset_name, "Static track volume preset");
1196
1197 for (j=0; j<totaltracks; j++) {
1198 vol = vol_values[j]*2;
1199 moov->PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].preset_volume_element = vol/2; //*0.02
1200 }
1201
1202 addsize = totaltracks;
1203 break;
1204 case 1: moov->PresetContainerBox.PresetBox[i].preset_type = 1;
1205 strcpy(moov->PresetContainerBox.PresetBox[i].preset_name, "Static object volume preset");
1206
1207 for (j=0; j<totaltracks; j++) {
1208 moov->PresetContainerBox.PresetBox[i].StaticObjectVolume.InputCH[j].num_input_channel = num_ch;
1209 }
1210 moov->PresetContainerBox.PresetBox[i].StaticObjectVolume.output_channel_type = 1; // STEREO (2 output channels)
1211 for (j=0; j<totaltracks; j++){
1212 for (k=0; k<num_ch; k++){
1213 for (m=0; m<num_ch; m++){
1214 moov->PresetContainerBox.PresetBox[i].StaticObjectVolume.presElVol[j].
1215 Input[k].Output[m].preset_volume_element = (100-(20*m))/2; // INPUT BY USER
1216 }
1217 }
1218 }
1219
1220 addsize = totaltracks + 1+ totaltracks*num_ch*num_ch;
1221 break;
1222 case 2: moov->PresetContainerBox.PresetBox[i].preset_type = 2;
1223 strcpy(moov->PresetContainerBox.PresetBox[i].preset_name, "Dynamic track volume preset");
1224
1225 updates = 2; // volume changes
1226 moov->PresetContainerBox.PresetBox[i].DynamicTrackVolume.num_updates = bytereverse16(updates);
1227 for (j=0; j<updates; j++){ // INPUT BY USER
1228 moov->PresetContainerBox.PresetBox[i].DynamicTrackVolume.
1229 DynamicChange[j].updated_sample_number = bytereverse16(100+(j*100)); // *0.026 = time in seconds
1230 for (k=0; k<totaltracks; k++){
1231 moov->PresetContainerBox.PresetBox[i].DynamicTrackVolume.
1232 DynamicChange[j].presVolumElem[k].preset_volume_element = (50*j)/2; // INPUT BY USER
1233 }
1234 }
1235
1236 addsize = 2 + updates*(2 + totaltracks);
1237 break;
1238 case 3: moov->PresetContainerBox.PresetBox[i].preset_type = 3;
1239 strcpy(moov->PresetContainerBox.PresetBox[i].preset_name, "Dynamic object volume preset");
1240
1241 updates = 2; // volume changes
1242 moov->PresetContainerBox.PresetBox[i].DynamicObjectVolume.num_updates = bytereverse16(updates); // INPUT BY USER (maybe...)
1243 for (j=0; j<totaltracks; j++) {
1244 moov->PresetContainerBox.PresetBox[i].DynamicObjectVolume.InputCH[j].num_input_channel = 2;
1245 }
1246 moov->PresetContainerBox.PresetBox[i].DynamicObjectVolume.output_channel_type = 1; // STEREO (2 output channels)
1247 for (j=0; j<updates; j++){ // INPUT BY USER
1248 moov->PresetContainerBox.PresetBox[i].DynamicObjectVolume.
1249 DynamicChange[j].updated_sample_number = bytereverse16(0+(j*500)); // *0.026 = time in seconds
1250 for (k=0; k<totaltracks; k++){
1251 for (m=0; m<num_ch; m++){
1252 for (t=0; t<num_ch; t++){
1253 moov->PresetContainerBox.PresetBox[i].DynamicObjectVolume.DynamicChange[j].
1254 presElVol[k].Input[m].Output[t].preset_volume_element = (25*(j+1)); // INPUT BY USER
1255 }
1256 }
1257 }
1258 }
1259
1260 addsize = 2 + totaltracks + 1 + updates*(2 + totaltracks*num_ch*num_ch);
1261 break;
1262 case 4: moov->PresetContainerBox.PresetBox[i].preset_type = 4;
1263 strcpy(moov->PresetContainerBox.PresetBox[i].preset_name, "Dynamic track approximated volume");
1264
1265 updates = 2; // volume changes
1266 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.num_updates = bytereverse16(updates);
1267 for (j=0; j<updates; j++){
1268 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1269 DynamicChange[j].start_sample_number = bytereverse16(100); // *0.026 = time in seconds - INPUT BY USER
1270 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1271 DynamicChange[j].duration_update = bytereverse16(500); // *0.026 = time in seconds -INPUT BY USER
1272 for (k=0; k<totaltracks; k++){
1273 if (fade_in){
1274 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[j].
1275 presElVol[k].end_preset_volume_element = (50*j); // Fade IN
1276 } else {
1277 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[j].
1278 presElVol[k].end_preset_volume_element = (100-(100*j))/2; // Fade OUT
1279 }
1280 }
1281 }
1282
1283 /* // some code for test
1284 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[2].start_sample_number = bytereverse16(1100);
1285 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[2].duration_update = bytereverse16(250);
1286 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[2].presElVol[0].end_preset_volume_element = 50;
1287 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[2].presElVol[1].end_preset_volume_element = 50;
1288 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[3].start_sample_number = bytereverse16(1100);
1289 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[3].duration_update = bytereverse16(250);
1290 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[3].presElVol[0].end_preset_volume_element = 1;
1291 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[3].presElVol[1].end_preset_volume_element = 1;
1292 */
1293
1294 addsize = 2 + updates*(2 + 2 + totaltracks);
1295 break;
1296 case 5: moov->PresetContainerBox.PresetBox[i].preset_type = 5; // NOT YET IMPLEMENTED INTO THE PLAYER!
1297 strcpy(moov->PresetContainerBox.PresetBox[i].preset_name, "Dynamic object approximated volume");
1298
1299 printf("\n\nTHIS PRESET IS NOT YET IMPLEMENTED INTO THE PLAYER!!! PLAYER MAY CRASH WITH THIS FILE!!!\n\n");
1300
1301 updates = 2; // volume changes
1302 moov->PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.num_updates = bytereverse16(updates);
1303 for (j=0; j<totaltracks; j++) {
1304 moov->PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.InputCH[j].num_input_channel = 2;
1305 }
1306 moov->PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.output_channel_type = 1; // STEREO (2 output channels)
1307 for (j=0; j<updates; j++){
1308 moov->PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.
1309 DynamicChange[j].start_sample_number = bytereverse16(100); // *0.026 = time in seconds - INPUT BY USER
1310 moov->PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.
1311 DynamicChange[j].duration_update = bytereverse16(250); // *0.026 = time in seconds - INPUT BY USER
1312 for (k=0; k<totaltracks; k++){
1313 for (m=0; m<num_ch; m++){
1314 for (t=0; t<num_ch; t++){
1315 moov->PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.DynamicChange[j].
1316 presElVol[k].Input[m].Output[t].preset_volume_element = (100*j)/2; // INPUT BY USER
1317 }
1318 }
1319 }
1320 }
1321
1322 addsize = 2 + totaltracks + 1 + updates*( 2 + 2 + totaltracks*num_ch*num_ch);
1323 break;
1324 case 6: moov->PresetContainerBox.PresetBox[i].preset_type = 8;
1325 strcpy(moov->PresetContainerBox.PresetBox[i].preset_name, "Static track volume with Equalization");
1326
1327 eq = 0; addsize = 0;
1328 for (j=0; j<totaltracks; j++) {
1329 //printf("Enter volume for %s : ",namet[j].title);
1330 scanf("%d",&vol);
1331 fflush(stdin);
1332 moov->PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].preset_volume_element = vol/2; //*0.02
1333
1334 // Equalization
1335 printf("EQ Filter on this element? [1] Yes - [0] No : ");
1336 scanf("%d",&eq);
1337 fflush(stdin);
1338
1339 if (eq == 1){
1340 moov->PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].EQ.num_eq_filters = 1;
1341 for (k=0; k<moov->PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].EQ.num_eq_filters; k++){
1342 moov->PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].EQ.Filter[k].filter_type = 4; // HPF
1343 moov->PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].EQ.Filter[k].
1344 filter_reference_frequency = bytereverse16(5000); // 10kHz
1345 moov->PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].EQ.Filter[k].filter_gain = -10;
1346 moov->PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].EQ.Filter[k].filter_bandwidth = 4;
1347 addsize += 5;
1348 } //close for
1349 }else{
1350 moov->PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].EQ.num_eq_filters = 0;
1351 } //close if/else
1352 } //close for
1353
1354 addsize += totaltracks + totaltracks; //add preset_volume and num_eq_filters size
1355 break;
1356 case 7: moov->PresetContainerBox.PresetBox[i].preset_type = 9; // NOT YET IMPLEMENTED INTO THE PLAYER!
1357 strcpy(moov->PresetContainerBox.PresetBox[i].preset_name, "Static object volume with Equalization");
1358 printf("\n\nTHIS PRESET IS NOT YET IMPLEMENTED INTO THE PLAYER!!! PLAYER MAY CRASH WITH THIS FILE!!!\n\n");
1359 break;
1360 case 8: moov->PresetContainerBox.PresetBox[i].preset_type = 10; // NOT YET IMPLEMENTED INTO THE PLAYER!
1361 strcpy(moov->PresetContainerBox.PresetBox[i].preset_name, "Dynamic track volume with Equalization");
1362 printf("\n\nTHIS PRESET IS NOT YET IMPLEMENTED INTO THE PLAYER!!! PLAYER MAY CRASH WITH THIS FILE!!!\n\n");
1363 break;
1364 case 9: moov->PresetContainerBox.PresetBox[i].preset_type = 11; // NOT YET IMPLEMENTED INTO THE PLAYER!
1365 strcpy(moov->PresetContainerBox.PresetBox[i].preset_name, "Dynamic object volume with Equalization");
1366 break;
1367 case 10: moov->PresetContainerBox.PresetBox[i].preset_type = 12; // NOT YET IMPLEMENTED INTO THE PLAYER!
1368 strcpy(moov->PresetContainerBox.PresetBox[i].preset_name, "Dynamic track approximated with Equalization");
1369 printf("\n\nTHIS PRESET IS NOT YET IMPLEMENTED INTO THE PLAYER!!! PLAYER MAY CRASH WITH THIS FILE!!!\n\n");
1370
1371 eq = 0; addsize = 0;
1372 updates = 2; // volume changes
1373 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.num_updates = bytereverse16(updates);
1374 for (j=0; j<updates; j++){
1375 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1376 DynamicChange[j].start_sample_number = bytereverse16(100); // *0.026 = time in seconds - INPUT BY USER
1377 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1378 DynamicChange[j].duration_update = bytereverse16(500); // *0.026 = time in seconds -INPUT BY USER
1379 for (k=0; k<totaltracks; k++){
1380 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[j].
1381 presElVol[k].end_preset_volume_element = (50*j); // Fade IN, change in (100-(100*j))/2 for Fade OUT -INPUT BY USER
1382
1383 // Equalization
1384 //printf("EQ Filter on %s ? [1] Yes - [0] No : ",namet[k].title);
1385 scanf("%d",&eq);
1386 fflush(stdin);
1387
1388 if (eq == 1){
1389 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1390 DynamicChange[j].presElVol[k].EQ.num_eq_filters = 1;
1391 for (t=0; t<moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1392 DynamicChange[j].presElVol[k].EQ.num_eq_filters; t++){
1393 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1394 DynamicChange[j].presElVol[k].EQ.Filter[t].filter_type = 4; // HPF
1395 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1396 DynamicChange[j].presElVol[k].EQ.Filter[t].filter_reference_frequency = bytereverse16(5000); // 10kHz
1397 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1398 DynamicChange[j].presElVol[k].EQ.Filter[t].end_filter_gain = -10;
1399 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1400 DynamicChange[j].presElVol[k].EQ.Filter[t].filter_bandwidth = 4;
1401 addsize += 5;
1402 } //close for
1403 }else{
1404 moov->PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1405 DynamicChange[j].presElVol[k].EQ.num_eq_filters = 0;
1406 } //close if/else
1407 } //close for (k)
1408 } //close for (j)
1409
1410 addsize += 2 + updates*(2 + 2 + totaltracks*(1+1));
1411 break;
1412 case 11: moov->PresetContainerBox.PresetBox[i].preset_type = 13; // NOT YET IMPLEMENTED INTO THE PLAYER!
1413 strcpy(moov->PresetContainerBox.PresetBox[i].preset_name, "Dynamic object approximated with Equalization");
1414 printf("\n\nTHIS PRESET IS NOT YET IMPLEMENTED INTO THE PLAYER!!! PLAYER MAY CRASH WITH THIS FILE!!!\n\n");
1415 break;
1416 default: printf("ERROR in PRESET CONTAINER");
1417 system("pause");
1418 exit(1);
1419 break;
1420
1421 } //close switch
1422
1423 temp = 16 + 50 + 4*totaltracks + addsize; // size PresetBox[i]
1424 moov->PresetContainerBox.PresetBox[i].size = bytereverse(temp);
1425
1426 sizePRST += temp; // size of all Preset Boxes
1427
1428 } //close for
1429
1430 //Preset Container//
1431 sizePRCO += sizePRST + 10;
1432 moov->PresetContainerBox.size = bytereverse(sizePRCO);
1433 moov->PresetContainerBox.type = bytereverse('prco');
1434 moov->PresetContainerBox.default_preset_ID = 1; // Indicates initial preset activated.
1435 moov->PresetContainerBox.num_preset = numpres;
1436
1437 return sizePRCO;
1438
1439 } //close function
1440
1441 int rulescontainer(MovieBox *moov, int SelRuleType, int SelRule_PAR1, int SelRule_PAR2,
1442 int MixRuleType, int MixRule_PAR1, int MixRule_PAR2,
1443 int MixRule_PAR3, int MixRule_PAR4) {
1444
1445 int swap;
1446 u32 add_size = 0; //for SelectionRulesBox
1447 u32 sizeRUSC, sizeRUCO, sizeRUMX;
1448
1449
1450 //Selection Rules
1451 moov->RulesContainer.num_selection_rules = bytereverse16(1);
1452 moov->RulesContainer.SelectionRules.selection_rule_ID = bytereverse16(1);
1453
1454 switch (SelRuleType) {
1455
1456 case 0: moov->RulesContainer.SelectionRules.selection_rule_type = 0;
1457 moov->RulesContainer.SelectionRules.element_ID = bytereverse(2147483649+1); // Must be the same ID of the group
1458 moov->RulesContainer.SelectionRules.min_num_elements = bytereverse16(SelRule_PAR1);
1459 moov->RulesContainer.SelectionRules.max_num_elements = bytereverse16(SelRule_PAR2);
1460 strcpy(moov->RulesContainer.SelectionRules.rule_description,"Min/Max Rule");
1461 add_size = 4;
1462 break;
1463 case 1: moov->RulesContainer.SelectionRules.selection_rule_type = 1;
1464 moov->RulesContainer.SelectionRules.element_ID = bytereverse(SelRule_PAR1);
1465 moov->RulesContainer.SelectionRules.key_element_ID =bytereverse(SelRule_PAR2);
1466 strcpy(moov->RulesContainer.SelectionRules.rule_description,"Exclusion Rule");
1467 add_size = 4;
1468 break;
1469 case 2: moov->RulesContainer.SelectionRules.selection_rule_type = 2;
1470 moov->RulesContainer.SelectionRules.element_ID = bytereverse(SelRule_PAR1);
1471 strcpy(moov->RulesContainer.SelectionRules.rule_description,"Not mute Rule");
1472 add_size = 0;
1473 break;
1474 case 3: moov->RulesContainer.SelectionRules.selection_rule_type = 3;
1475 moov->RulesContainer.SelectionRules.element_ID = bytereverse(SelRule_PAR1);
1476 moov->RulesContainer.SelectionRules.key_element_ID =bytereverse(SelRule_PAR2);
1477 strcpy(moov->RulesContainer.SelectionRules.rule_description,"Implication Rule");
1478 add_size = 4;
1479 break;
1480 default: printf("ERROR in RULES CONTAINER/Selection Rules");
1481 system("pause");
1482 break;
1483 }
1484
1485 sizeRUSC = 19 + 20 + add_size;
1486 moov->RulesContainer.SelectionRules.size = bytereverse(sizeRUSC);
1487 moov->RulesContainer.SelectionRules.type = bytereverse('rusc');
1488 moov->RulesContainer.SelectionRules.version = 0;
1489
1490 //Mixing Rule
1491 moov->RulesContainer.num_mixing_rules = bytereverse16(1);
1492 moov->RulesContainer.MixingRules.mixing_rule_ID = bytereverse16(1);
1493
1494 switch (MixRuleType) {
1495
1496 case 0: moov->RulesContainer.MixingRules.mixing_type = 0;
1497 moov->RulesContainer.MixingRules.element_ID = bytereverse(MixRule_PAR1);
1498 moov->RulesContainer.MixingRules.key_elem_ID = bytereverse(MixRule_PAR2);
1499 strcpy(moov->RulesContainer.MixingRules.mix_description, "Equivalence rule");
1500 break;
1501 case 1: moov->RulesContainer.MixingRules.mixing_type = 1;
1502 moov->RulesContainer.MixingRules.element_ID = bytereverse(MixRule_PAR2);
1503 moov->RulesContainer.MixingRules.key_elem_ID = bytereverse(MixRule_PAR1);
1504 strcpy(moov->RulesContainer.MixingRules.mix_description, "Upper rule");
1505 break;
1506 case 2: moov->RulesContainer.MixingRules.mixing_type = 2;
1507 moov->RulesContainer.MixingRules.element_ID = bytereverse(MixRule_PAR2);
1508 moov->RulesContainer.MixingRules.key_elem_ID = bytereverse(MixRule_PAR1);
1509 strcpy(moov->RulesContainer.MixingRules.mix_description, "Lower rule");
1510 break;
1511 case 3: moov->RulesContainer.MixingRules.mixing_type = 3;
1512 moov->RulesContainer.MixingRules.element_ID = bytereverse(MixRule_PAR1);
1513 moov->RulesContainer.MixingRules.min_volume = bytereverse16(1 + MixRule_PAR3*2.5); // 8.8 fixed point
1514 moov->RulesContainer.MixingRules.max_volume = bytereverse16(1 + MixRule_PAR4*2.5); // 8.8 fixed point
1515 strcpy(moov->RulesContainer.MixingRules.mix_description, "Limit rule");
1516 break;
1517 default: printf("ERROR in RULES CONTAINER/Mixing Rules");
1518 system("pause");
1519 exit(1);
1520 break;
1521 }
1522
1523 sizeRUMX = 23 + 17;
1524 moov->RulesContainer.MixingRules.size = bytereverse(sizeRUMX);
1525 moov->RulesContainer.MixingRules.type = bytereverse('rumx');
1526 moov->RulesContainer.MixingRules.version = 0;
1527
1528 //Rule container
1529 sizeRUCO = 12 + sizeRUSC + sizeRUMX;
1530 swap = bytereverse(sizeRUCO);
1531 moov->RulesContainer.size = swap;
1532 swap = bytereverse('ruco');
1533 moov->RulesContainer.type = swap;
1534
1535 return sizeRUCO;
1536
1537 } // close function
1538
1539 void moovheaderbox (MovieBox *moov, int clock, int sizeTRAK, int sizePRCO, int totaltracks,
1540 int durationTrack, int sizeRUCO, int sizeGRCO) {
1541 int swap;
1542 u32 sizeMOOV; //MovieBox
1543
1544 //MovieHeader
1545 u32 sizeMVHD = 108;
1546 swap = bytereverse (sizeMVHD);
1547 moov->MovieHeaderBox.size = swap;
1548 swap = bytereverse ('mvhd');
1549 moov->MovieHeaderBox.type = swap;
1550 moov->MovieHeaderBox.version = 0;
1551 swap = bytereverse (clock);
1552 moov->MovieHeaderBox.creation_time = swap;
1553 moov->MovieHeaderBox.modification_time = swap;
1554 swap = bytereverse (1000);
1555 moov->MovieHeaderBox.timescale = swap;
1556 swap = bytereverse (durationTrack);
1557 moov->MovieHeaderBox.duration = swap;
1558 swap = bytereverse (0x00010000);
1559 moov->MovieHeaderBox.rate = swap;
1560 swap = bytereverse (1);
1561 moov->MovieHeaderBox.volume = 1;
1562 moov->MovieHeaderBox.reserved=0;
1563 moov->MovieHeaderBox.reserved2[0] = 0;
1564 moov->MovieHeaderBox.reserved2[1] = 0;
1565 swap = bytereverse (0x00010000);
1566 moov->MovieHeaderBox.matrix[0] = swap;
1567 moov->MovieHeaderBox.matrix[1] = 0;
1568 moov->MovieHeaderBox.matrix[2] = 0;
1569 moov->MovieHeaderBox.matrix[3] = 0;
1570 moov->MovieHeaderBox.matrix[4] = swap;
1571 moov->MovieHeaderBox.matrix[5] = 0;
1572 moov->MovieHeaderBox.matrix[6] = 0;
1573 moov->MovieHeaderBox.matrix[7] = 0;
1574 swap = bytereverse (0x40000000);
1575 moov->MovieHeaderBox.matrix[8] = 0x40000000;
1576 moov->MovieHeaderBox.pre_defined[0] = 0;
1577 moov->MovieHeaderBox.pre_defined[1] = 0;
1578 moov->MovieHeaderBox.pre_defined[2] = 0;
1579 moov->MovieHeaderBox.pre_defined[3] = 0;
1580 moov->MovieHeaderBox.pre_defined[4] = 0;
1581 moov->MovieHeaderBox.pre_defined[5] = 0;
1582 swap = bytereverse (totaltracks + 1);
1583 moov->MovieHeaderBox.next_track_ID = swap;
1584
1585 //MovieBox
1586 sizeMOOV = sizeMVHD + sizeTRAK + sizePRCO + sizeRUCO + sizeGRCO + 8;
1587 swap = bytereverse (sizeMOOV); //Size movie: Taking into account number tracks
1588 moov->size = swap;
1589 swap = bytereverse ('moov');
1590 moov->type = swap;
1591 }
1592
1593
1594 void writemoovbox(MovieBox moov, int numtrack,int totaltracks, FILE *imf, FILE *text)
1595 {
1596 int i, j, k, m, t, swap, pos, temp, type;
1597 int cnt = 0, cnt2 = 0, d = 0, dat = 0, dat1 = 0, dat2 = 0, dat3 = 0, size = 0;
1598 u16 numgroups, numel;
1599
1600
1601 //Write movie box//
1602 fwrite(&moov.size, sizeof(u32), 1, imf);
1603 fwrite(&moov.type, sizeof(u32), 1, imf);
1604 //Movie header//
1605 fwrite(&moov.MovieHeaderBox, sizeof(moov.MovieHeaderBox), 1, imf);
1606 //Track container//
1607 for (numtrack = 0; numtrack < totaltracks; numtrack++) {
1608 fwrite(&moov.TrackBox[numtrack].size, sizeof(u32), 1, imf);
1609 fwrite(&moov.TrackBox[numtrack].type, sizeof(u32), 1, imf);
1610 //Trck header//
1611 fwrite(&moov.TrackBox[numtrack].TrackHeaderBox, sizeof(moov.TrackBox[numtrack].TrackHeaderBox), 1, imf);
1612 //Media Box//
1613 fwrite(&moov.TrackBox[numtrack].MediaBox.size, sizeof(u32), 1, imf);
1614 fwrite(&moov.TrackBox[numtrack].MediaBox.type, sizeof(u32), 1, imf);
1615 //Media Header//
1616 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaHeaderBox,
1617 sizeof(moov.TrackBox[numtrack].MediaBox.MediaHeaderBox), 1, imf);
1618 //Handler Box//
1619 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.size, sizeof(u32), 1, imf);
1620 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.type, sizeof(u32), 1, imf);
1621 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.version, sizeof(u32), 1, imf);
1622 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.pre_defined, sizeof(u32), 1, imf);
1623 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.handler_type, sizeof(u32), 1, imf);
1624 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.reserved[0], sizeof(u32), 1, imf);
1625 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.reserved[1], sizeof(u32), 1, imf);
1626 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.reserved[2], sizeof(u32), 1, imf);
1627 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.data[0], sizeof(unsigned char), 1, imf);
1628 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.data[1], sizeof(unsigned char), 1, imf);
1629 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.data[2], sizeof(unsigned char), 1, imf);
1630 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.data[3], sizeof(unsigned char), 1, imf);
1631 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.data[4], sizeof(unsigned char), 1, imf);
1632 //Media inforamtion box//
1633 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.size, sizeof(u32), 1, imf);
1634 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.type, sizeof(u32), 1, imf);
1635 //Sound media header//
1636 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SoundMediaHeaderBox,
1637 sizeof(moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SoundMediaHeaderBox), 1, imf);
1638 //Data reference//
1639 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox,
1640 sizeof(moov.TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox), 1, imf);
1641 //Sample table box//
1642 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.size, sizeof(u32), 1, imf);
1643 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.type, sizeof(u32), 1, imf);
1644
1645 //Time to sample box//
1646 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1647 TimeToSampleBox.size, sizeof(u32), 1, imf);
1648 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1649 TimeToSampleBox.type, sizeof(u32), 1, imf);
1650 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1651 TimeToSampleBox.version, sizeof(u32), 1, imf);
1652 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1653 TimeToSampleBox.entry_count, sizeof(u32), 1, imf);
1654
1655 swap = bytereverse(moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1656 TimeToSampleBox.entry_count);
1657 pos = swap;
1658
1659 for (i=0; i<pos; i++) {
1660
1661 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1662 TimeToSampleBox.sample_count[i], sizeof(u32), 1, imf);
1663 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1664 TimeToSampleBox.sample_delta[i], sizeof(u32), 1, imf);
1665 }
1666
1667 //Sample description box//
1668 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1669 SampleDescriptionBox.size, sizeof(u32), 1, imf);
1670 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1671 SampleDescriptionBox.type, sizeof(u32), 1, imf);
1672 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1673 SampleDescriptionBox.version, sizeof(u32), 1, imf);
1674 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1675 SampleDescriptionBox.entry_count, sizeof(u32), 1, imf);
1676 //Audio Sample entry//
1677 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1678 SampleDescriptionBox.AudioSampleEntry.size, sizeof(u32), 1, imf);
1679 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1680 SampleDescriptionBox.AudioSampleEntry.type, sizeof(u32), 1, imf);
1681 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1682 SampleDescriptionBox.AudioSampleEntry.reserved[0], sizeof(unsigned char), 6, imf);
1683 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1684 SampleDescriptionBox.AudioSampleEntry.data_reference_index, sizeof(u16), 1, imf);
1685 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1686 SampleDescriptionBox.AudioSampleEntry.reserved2[0], sizeof(u32), 2, imf);
1687 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1688 SampleDescriptionBox.AudioSampleEntry.channelcount, sizeof(u16), 1, imf);
1689 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1690 SampleDescriptionBox.AudioSampleEntry.samplesize, sizeof(u16), 1, imf);
1691 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1692 SampleDescriptionBox.AudioSampleEntry.reserved3, sizeof(u32), 1, imf);
1693 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1694 SampleDescriptionBox.AudioSampleEntry.samplerate, sizeof(u32), 1, imf);
1695 //ESDBox//
1696 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1697 SampleDescriptionBox.AudioSampleEntry.ESbox.size, sizeof(u32), 1, imf);
1698 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1699 SampleDescriptionBox.AudioSampleEntry.ESbox.type, sizeof(u32), 1, imf);
1700 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1701 SampleDescriptionBox.AudioSampleEntry.ESbox.version, sizeof(u32), 1, imf);
1702 //ES Descriptor//
1703 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1704 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.tag
1705 , sizeof(unsigned char), 1, imf);
1706 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1707 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.length
1708 , sizeof(unsigned char), 1, imf);
1709 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1710 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.ES_ID
1711 , sizeof(u16), 1, imf);
1712 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1713 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.mix
1714 , sizeof(unsigned char), 1, imf);
1715 //Decoder Config//
1716 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1717 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.DecoderConfigDescriptor.
1718 tag, sizeof(unsigned char), 1, imf);
1719 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1720 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.DecoderConfigDescriptor.
1721 length, sizeof(unsigned char), 1, imf);
1722 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1723 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.DecoderConfigDescriptor.
1724 objectProfileInd, sizeof(unsigned char), 1, imf);
1725 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1726 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.DecoderConfigDescriptor.
1727 mix, sizeof(u32), 1, imf);
1728 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1729 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.DecoderConfigDescriptor.
1730 maxBitRate, sizeof(u32), 1, imf);
1731 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1732 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.DecoderConfigDescriptor.
1733 avgBitrate, sizeof(u32), 1, imf);
1734 /* //DecoderSpecificInfo//
1735 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1736 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.DecoderConfigDescriptor.
1737 DecoderSpecificInfo.tag, sizeof(unsigned char), 1, imf);
1738 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1739 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.DecoderConfigDescriptor.
1740 DecoderSpecificInfo.length, sizeof(unsigned char), 1, imf);
1741 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1742 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.DecoderConfigDescriptor.
1743 DecoderSpecificInfo.decSpecificInfoData[0], sizeof(unsigned char), 1, imf);
1744 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1745 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.DecoderConfigDescriptor.
1746 DecoderSpecificInfo.decSpecificInfoData[1], sizeof(unsigned char), 1, imf);
1747 */ //SLConfig//
1748 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1749 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.SLConfigDescriptor.
1750 tag, sizeof(unsigned char), 1, imf);
1751 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1752 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.SLConfigDescriptor.
1753 length, sizeof(unsigned char), 1, imf);
1754 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1755 SampleDescriptionBox.AudioSampleEntry.ESbox.ES_Descriptor.SLConfigDescriptor.
1756 predifined, sizeof(unsigned char), 1, imf);
1757
1758
1759 //Sample Size box//
1760 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1761 SampleSizeBox.size, sizeof(u32), 1, imf);
1762 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1763 SampleSizeBox.type, sizeof(u32), 1, imf);
1764 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1765 SampleSizeBox.version, sizeof(u32), 1, imf);
1766 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1767 SampleSizeBox.sample_size, sizeof(u32), 1, imf);
1768 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1769 SampleSizeBox.sample_count, sizeof(u32), 1, imf);
1770 swap = bytereverse(moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1771 SampleSizeBox.sample_count);
1772 for(i=0; i<swap; i++){
1773 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1774 SampleSizeBox.entry_size[i], sizeof(u32), 1, imf);
1775 }
1776
1777 //Sample to chunk box//
1778 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1779 SampleToChunk.size, sizeof(u32), 1, imf);
1780 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1781 SampleToChunk.type, sizeof(u32), 1, imf);
1782 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1783 SampleToChunk.version, sizeof(u32), 1, imf);
1784 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1785 SampleToChunk.entry_count, sizeof(u32), 1, imf);
1786 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1787 SampleToChunk.first_chunk, sizeof(u32), 1, imf);
1788 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1789 SampleToChunk.samples_per_chunk, sizeof(u32), 1, imf);
1790 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1791 SampleToChunk.sample_description_index, sizeof(u32), 1, imf);
1792
1793 //Chunk offset//
1794 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1795 ChunkOffsetBox.size, sizeof(u32), 1, imf);
1796 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1797 ChunkOffsetBox.type, sizeof(u32), 1, imf);
1798 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1799 ChunkOffsetBox.version, sizeof(u32), 1, imf);
1800 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1801 ChunkOffsetBox.entry_count, sizeof(u32), 1, imf);
1802 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
1803 ChunkOffsetBox.chunk_offset[numtrack], sizeof(u32), 1, imf);
1804 }
1805
1806 //Group Container
1807 fwrite(&moov.GroupContainerBox.size, sizeof(u32), 1, imf);
1808 fwrite(&moov.GroupContainerBox.type, sizeof(u32), 1, imf);
1809 fwrite(&moov.GroupContainerBox.num_groups, sizeof(u16), 1, imf);
1810 //Group Box
1811 numgroups = bytereverse16(moov.GroupContainerBox.num_groups);
1812 for(i=0; i<numgroups; i++){
1813 fwrite(&moov.GroupContainerBox.GroupBox[i].size, sizeof(u32), 1, imf);
1814 fwrite(&moov.GroupContainerBox.GroupBox[i].type, sizeof(u32), 1, imf);
1815 fwrite(&moov.GroupContainerBox.GroupBox[i].version, sizeof(u32), 1, imf);
1816 fwrite(&moov.GroupContainerBox.GroupBox[i].group_ID, sizeof(u32), 1, imf);
1817 fwrite(&moov.GroupContainerBox.GroupBox[i].num_elements, sizeof(u16), 1, imf);
1818 numel = bytereverse16(moov.GroupContainerBox.GroupBox[i].num_elements);
1819 for(j=0; j<numel; j++){
1820 fwrite(&moov.GroupContainerBox.GroupBox[i].groupElemId[j].element_ID, sizeof(u32), 1, imf);
1821 }
1822 fwrite(&moov.GroupContainerBox.GroupBox[i].group_activation_mode, sizeof(unsigned char), 1, imf);
1823 if(moov.GroupContainerBox.GroupBox[i].group_activation_mode==2){
1824 fwrite(&moov.GroupContainerBox.GroupBox[i].group_activation_elements_number, sizeof(u16), 1, imf);
1825 }
1826 fwrite(&moov.GroupContainerBox.GroupBox[i].group_reference_volume, sizeof(u16), 1, imf);
1827 for (j=0; j<22; j++) {
1828 fwrite(&moov.GroupContainerBox.GroupBox[i].group_name[j], sizeof(char), 1, imf);
1829 }
1830 for (j=0; j<32; j++) {
1831 fwrite(&moov.GroupContainerBox.GroupBox[i].group_description[j], sizeof(char), 1, imf);
1832 }
1833
1834 }
1835
1836 //PresetContainerBox
1837 fwrite(&moov.PresetContainerBox.size, sizeof(u32), 1, imf);
1838 fwrite(&moov.PresetContainerBox.type, sizeof(u32), 1, imf);
1839 fwrite(&moov.PresetContainerBox.num_preset, sizeof(unsigned char), 1, imf);
1840 fwrite(&moov.PresetContainerBox.default_preset_ID, sizeof(unsigned char), 1, imf);
1841
1842 //Preset Box
1843 for (i=0; i<moov.PresetContainerBox.num_preset; i++) {
1844 fwrite(&moov.PresetContainerBox.PresetBox[i].size, sizeof(u32), 1, imf);
1845 fwrite(&moov.PresetContainerBox.PresetBox[i].type, sizeof(u32), 1, imf);
1846 fwrite(&moov.PresetContainerBox.PresetBox[i].flags, sizeof(u32), 1, imf);
1847 fwrite(&moov.PresetContainerBox.PresetBox[i].preset_ID, sizeof(unsigned char), 1, imf);
1848 fwrite(&moov.PresetContainerBox.PresetBox[i].num_preset_elements, sizeof(unsigned char), 1, imf);
1849 for (j=0; j< moov.PresetContainerBox.PresetBox[i].num_preset_elements; j++) {
1850 fwrite(&moov.PresetContainerBox.PresetBox[i].presElemId[j].
1851 preset_element_ID, sizeof(u32), 1, imf);
1852 }
1853 fwrite(&moov.PresetContainerBox.PresetBox[i].preset_type , sizeof(unsigned char), 1, imf);
1854 fwrite(&moov.PresetContainerBox.PresetBox[i].preset_global_volume, sizeof(unsigned char), 1, imf);
1855
1856 type = moov.PresetContainerBox.PresetBox[i].preset_type;
1857
1858 switch(type){
1859
1860 case 0: for (j=0; j< moov.PresetContainerBox.PresetBox[i].num_preset_elements; j++) {
1861 fwrite(&moov.PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].
1862 preset_volume_element,sizeof(unsigned char), 1, imf);
1863 }
1864 break;
1865 case 1: for (j=0; j<num_ch; j++){
1866 fwrite(&moov.PresetContainerBox.PresetBox[i].StaticObjectVolume.InputCH[j].
1867 num_input_channel,sizeof(unsigned char), 1, imf);
1868 }
1869 fwrite(&moov.PresetContainerBox.PresetBox[i].StaticObjectVolume.output_channel_type, sizeof(unsigned char), 1, imf);
1870 for (j=0; j<totaltracks; j++){
1871 for (k=0; k<num_ch; k++){
1872 for (m=0; m<num_ch; m++){
1873 fwrite(&moov.PresetContainerBox.PresetBox[i].StaticObjectVolume.presElVol[j].
1874 Input[k].Output[m].preset_volume_element, sizeof(unsigned char), 1, imf);
1875 }
1876 }
1877 }
1878 break;
1879 case 2: fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackVolume.num_updates, sizeof(u16), 1, imf);
1880 temp = bytereverse16(moov.PresetContainerBox.PresetBox[i].DynamicTrackVolume.num_updates);
1881 for (j=0; j<temp; j++){
1882 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackVolume.
1883 DynamicChange[j].updated_sample_number, sizeof(u16), 1, imf);
1884 for (k=0; k<totaltracks; k++){
1885 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackVolume.DynamicChange[j].
1886 presVolumElem[k].preset_volume_element, sizeof(unsigned char), 1, imf);
1887 }
1888 }
1889 break;
1890 case 3: fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicObjectVolume.num_updates, sizeof(u16), 1, imf);
1891 for (j=0; j<totaltracks; j++) {
1892 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicObjectVolume.InputCH[j].num_input_channel, sizeof(u8), 1, imf);
1893 }
1894 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicObjectVolume.output_channel_type, sizeof(u8), 1, imf);
1895 temp = bytereverse16(moov.PresetContainerBox.PresetBox[i].DynamicObjectVolume.num_updates);
1896 for (j=0; j<temp; j++){
1897 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicObjectVolume.
1898 DynamicChange[j].updated_sample_number, sizeof(u16), 1, imf);
1899 for (k=0; k<totaltracks; k++){
1900 for (m=0; m<num_ch; m++){
1901 for (t=0; t<num_ch; t++){
1902 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicObjectVolume.DynamicChange[j].
1903 presElVol[k].Input[m].Output[t].preset_volume_element, sizeof(u8), 1, imf);
1904 }
1905 }
1906 }
1907 }
1908 break;
1909 case 4: fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.num_updates, sizeof(u16), 1, imf);
1910 temp = bytereverse16(moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.num_updates);
1911 for (j=0; j<temp; j++){
1912 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1913 DynamicChange[j].start_sample_number, sizeof(u16), 1, imf);
1914 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1915 DynamicChange[j].duration_update, sizeof(u16), 1, imf);
1916 for (k=0; k<totaltracks; k++){
1917 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[j].
1918 presElVol[k].end_preset_volume_element, sizeof(unsigned char), 1, imf);
1919 }
1920 }
1921 break;
1922 case 5: // NOT YET IMPLEMENTED INTO THE PLAYER!
1923 printf("\n\nTHIS PRESET IS NOT YET IMPLEMENTED INTO THE PLAYER!!! PLAYER MAY CRASH WITH THIS FILE!!!\n\n");
1924 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.num_updates, sizeof(u16), 1, imf);
1925 for (j=0; j<totaltracks; j++) {
1926 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.InputCH[j].num_input_channel, sizeof(u8), 1, imf);
1927 }
1928 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.output_channel_type, sizeof(u8), 1, imf);
1929 temp = bytereverse16(moov.PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.num_updates);
1930 for (j=0; j<temp; j++){
1931 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.
1932 DynamicChange[j].start_sample_number, sizeof(u16), 1, imf);
1933 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.
1934 DynamicChange[j].duration_update, sizeof(u16), 1, imf);
1935 for (k=0; k<totaltracks; k++){
1936 for (m=0; m<num_ch; m++){
1937 for (t=0; t<num_ch; t++){
1938 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicObjectApproxVolume.DynamicChange[j].
1939 presElVol[k].Input[m].Output[t].preset_volume_element, sizeof(u8), 1, imf);
1940 }
1941 }
1942 }
1943 }
1944 break;
1945 case 6: printf("\nERROR WRITING PRESET CONTAINER IN OUTPUT FILE - Not valid case (6)\n");
1946 system("pause");
1947 exit(1);
1948 break;
1949 case 7: printf("\nERROR WRITING PRESET CONTAINER IN OUTPUT FILE - Not valid case (7)\n");
1950 system("pause");
1951 exit(1);
1952 break;
1953 case 8: for (j=0; j< moov.PresetContainerBox.PresetBox[i].num_preset_elements; j++) {
1954 fwrite(&moov.PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].
1955 preset_volume_element,sizeof(unsigned char), 1, imf);
1956 fwrite(&moov.PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].EQ.num_eq_filters, sizeof(u8), 1, imf);
1957 //EQ
1958 if (moov.PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].EQ.num_eq_filters != 0) {
1959 for (k=0; k<moov.PresetContainerBox.PresetBox[i].StaticTrackVolume.presVolumElem[j].EQ.num_eq_filters; k++){
1960 fwrite(&moov.PresetContainerBox.PresetBox[i].StaticTrackVolume.
1961 presVolumElem[j].EQ.Filter[k].filter_type, sizeof(u8), 1, imf);
1962 fwrite(&moov.PresetContainerBox.PresetBox[i].StaticTrackVolume.
1963 presVolumElem[j].EQ.Filter[k].filter_reference_frequency, sizeof(u16), 1, imf);
1964 fwrite(&moov.PresetContainerBox.PresetBox[i].StaticTrackVolume.
1965 presVolumElem[j].EQ.Filter[k].filter_gain, sizeof(u8), 1, imf);
1966 fwrite(&moov.PresetContainerBox.PresetBox[i].StaticTrackVolume.
1967 presVolumElem[j].EQ.Filter[k].filter_bandwidth, sizeof(u8), 1, imf);
1968 } //close for
1969 } //close if
1970 }
1971 break;
1972 case 9: // NOT YET IMPLEMENTED INTO THE PLAYER!
1973 printf("\n\nTHIS PRESET IS NOT YET IMPLEMENTED INTO THE PLAYER!!! PLAYER MAY CRASH WITH THIS FILE!!!\n\n");
1974 break;
1975 case 10:// NOT YET IMPLEMENTED INTO THE PLAYER!
1976 printf("\n\nTHIS PRESET IS NOT YET IMPLEMENTED INTO THE PLAYER!!! PLAYER MAY CRASH WITH THIS FILE!!!\n\n");
1977 break;
1978 case 11:// NOT YET IMPLEMENTED INTO THE PLAYER!
1979 printf("\n\nTHIS PRESET IS NOT YET IMPLEMENTED INTO THE PLAYER!!! PLAYER MAY CRASH WITH THIS FILE!!!\n\n");
1980 break;
1981 case 12:// NOT YET IMPLEMENTED INTO THE PLAYER!
1982 printf("\n\nTHIS PRESET IS NOT YET IMPLEMENTED INTO THE PLAYER!!! PLAYER MAY CRASH WITH THIS FILE!!!\n\n");
1983 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.num_updates, sizeof(u16), 1, imf);
1984 temp = bytereverse16(moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.num_updates);
1985 for (j=0; j<temp; j++){
1986 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1987 DynamicChange[j].start_sample_number, sizeof(u16), 1, imf);
1988 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1989 DynamicChange[j].duration_update, sizeof(u16), 1, imf);
1990 for (k=0; k<totaltracks; k++){
1991 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.DynamicChange[j].
1992 presElVol[k].end_preset_volume_element, sizeof(u8), 1, imf);
1993 //EQ
1994 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1995 DynamicChange[j].presElVol[k].EQ.num_eq_filters, sizeof(u8), 1, imf);
1996 if(moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1997 DynamicChange[j].presElVol[k].EQ.num_eq_filters != 0){
1998 for (t=0; t<moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
1999 DynamicChange[j].presElVol[k].EQ.num_eq_filters; t++){
2000 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
2001 DynamicChange[j].presElVol[k].EQ.Filter[t].filter_type, sizeof(u8), 1, imf);
2002 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
2003 DynamicChange[j].presElVol[k].EQ.Filter[t].filter_reference_frequency, sizeof(u16), 1, imf);
2004 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
2005 DynamicChange[j].presElVol[k].EQ.Filter[t].end_filter_gain, sizeof(u8), 1, imf);
2006 fwrite(&moov.PresetContainerBox.PresetBox[i].DynamicTrackApproxVolume.
2007 DynamicChange[j].presElVol[k].EQ.Filter[t].filter_bandwidth, sizeof(u8), 1, imf);
2008 } // close for (t)
2009 }//close if
2010
2011 }// close for (k)
2012 } //close for (j)
2013 break;
2014 case 13:// NOT YET IMPLEMENTED INTO THE PLAYER!
2015 printf("\n\nTHIS PRESET IS NOT YET IMPLEMENTED INTO THE PLAYER!!! PLAYER MAY CRASH WITH THIS FILE!!!\n\n");
2016 break;
2017 default: printf("\nERROR WRITING PRESET CONTAINER IN OUTPUT FILE - Not valid case (default)\n");
2018 system("pause");
2019 exit(1);
2020 break;
2021
2022 } // close switch
2023
2024 for (j=0; j<50; j++) {
2025 fwrite(&moov.PresetContainerBox.PresetBox[i].preset_name[j], sizeof(char), 1, imf);
2026 }
2027
2028 } // close for
2029
2030
2031 //Rules Container//
2032 fwrite(&moov.RulesContainer.size, sizeof(u32), 1, imf);
2033 fwrite(&moov.RulesContainer.type, sizeof(u32), 1, imf);
2034 fwrite(&moov.RulesContainer.num_selection_rules, sizeof(u16), 1, imf);
2035 fwrite(&moov.RulesContainer.num_mixing_rules, sizeof(u16), 1, imf);
2036 //Selection Rules//
2037 fwrite(&moov.RulesContainer.SelectionRules.size, sizeof(u32), 1, imf);
2038 fwrite(&moov.RulesContainer.SelectionRules.type, sizeof(u32), 1, imf);
2039 fwrite(&moov.RulesContainer.SelectionRules.version, sizeof(u32), 1, imf);
2040 fwrite(&moov.RulesContainer.SelectionRules.selection_rule_ID, sizeof(u16), 1, imf);
2041 fwrite(&moov.RulesContainer.SelectionRules.selection_rule_type, sizeof(unsigned char), 1, imf);
2042 fwrite(&moov.RulesContainer.SelectionRules.element_ID, sizeof(u32), 1, imf);
2043
2044 swap = moov.RulesContainer.SelectionRules.selection_rule_type;
2045 if ( swap==0 ) {
2046 fwrite(&moov.RulesContainer.SelectionRules.min_num_elements, sizeof(u16), 1, imf);
2047 fwrite(&moov.RulesContainer.SelectionRules.max_num_elements, sizeof(u16), 1, imf);
2048 }
2049 if ( swap==1 || swap ==3 ){
2050 fwrite(&moov.RulesContainer.SelectionRules.key_element_ID, sizeof(u32), 1, imf);
2051 }
2052
2053 for(i=0; i<20; i++){
2054 fwrite(&moov.RulesContainer.SelectionRules.rule_description[i], sizeof(char), 1, imf);
2055 }
2056
2057 //Mixing Rules//
2058 fwrite(&moov.RulesContainer.MixingRules.size, sizeof(u32), 1, imf);
2059 fwrite(&moov.RulesContainer.MixingRules.type, sizeof(u32), 1, imf);
2060 fwrite(&moov.RulesContainer.MixingRules.version, sizeof(u32), 1, imf);
2061 fwrite(&moov.RulesContainer.MixingRules.mixing_rule_ID, sizeof(u16), 1, imf);
2062 fwrite(&moov.RulesContainer.MixingRules.mixing_type,sizeof(unsigned char), 1, imf);
2063 fwrite(&moov.RulesContainer.MixingRules.element_ID, sizeof(u32), 1, imf);
2064
2065 swap = moov.RulesContainer.MixingRules.mixing_type;
2066 if( swap==3 ){
2067 fwrite(&moov.RulesContainer.MixingRules.min_volume, sizeof(u16), 1, imf);
2068 fwrite(&moov.RulesContainer.MixingRules.max_volume, sizeof(u16), 1, imf);
2069 } else {
2070 fwrite(&moov.RulesContainer.MixingRules.key_elem_ID, sizeof(u32), 1, imf);
2071 }
2072 for(i=0; i<17; i++){
2073 fwrite(&moov.RulesContainer.MixingRules.mix_description[i],
2074 sizeof(char), 1, imf);
2075 }
2076
2077 //TIMED TEXT
2078 fwrite(&moov.TrackBox[numtrack].size, sizeof(u32), 1, imf);
2079 fwrite(&moov.TrackBox[numtrack].type, sizeof(u32), 1, imf);
2080 //Track header//
2081 fwrite(&moov.TrackBox[numtrack].TrackHeaderBox,
2082 sizeof(moov.TrackBox[numtrack].TrackHeaderBox), 1, imf);
2083 //Media Box//
2084 fwrite(&moov.TrackBox[numtrack].MediaBox.size, sizeof(u32), 1, imf);
2085 fwrite(&moov.TrackBox[numtrack].MediaBox.type, sizeof(u32), 1, imf);
2086 //Media Header//
2087 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaHeaderBox,
2088 sizeof(moov.TrackBox[numtrack].MediaBox.MediaHeaderBox), 1, imf);
2089 //Handler Box//
2090 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.size, sizeof(u32), 1, imf);
2091 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.type, sizeof(u32), 1, imf);
2092 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.version, sizeof(u32), 1, imf);
2093 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.pre_defined, sizeof(u32), 1, imf);
2094 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.handler_type, sizeof(u32), 1, imf);
2095 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.reserved[0], sizeof(u32), 1, imf);
2096 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.reserved[1], sizeof(u32), 1, imf);
2097 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.reserved[2], sizeof(u32), 1, imf);
2098 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.data[0], sizeof(unsigned char), 1, imf);
2099 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.data[1], sizeof(unsigned char), 1, imf);
2100 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.data[2], sizeof(unsigned char), 1, imf);
2101 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.data[3], sizeof(unsigned char), 1, imf);
2102 fwrite(&moov.TrackBox[numtrack].MediaBox.HandlerBox.data[4], sizeof(unsigned char), 1, imf);
2103 //Media information box//
2104 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.size, sizeof(u32), 1, imf);
2105 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.type, sizeof(u32), 1, imf);
2106 //Null media header//
2107 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.NullMediaHeaderBox.size,
2108 sizeof(u32), 1, imf);
2109 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.NullMediaHeaderBox.type,
2110 sizeof(u32), 1, imf);
2111 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.NullMediaHeaderBox.flags,
2112 sizeof(u32), 1, imf);
2113 //Data Information
2114 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.size,sizeof(u32),1,imf);
2115 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.type,sizeof(u32),1,imf);
2116 //Data Reference
2117 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.size,sizeof(u32),1,imf);
2118 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.type,sizeof(u32),1,imf);
2119 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.flags,sizeof(u32),1,imf);
2120 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.entry_count,sizeof(u32),1,imf);
2121 //Data Entry URL
2122 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.DataEntryUrlBox.size,sizeof(u32),1,imf);
2123 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.DataEntryUrlBox.type,sizeof(u32),1,imf);
2124 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.DataReferenceBox.DataEntryUrlBox.flags,sizeof(u32),1,imf);
2125 //Sample table box//
2126 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2127 size, sizeof(u32), 1, imf);
2128 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2129 type, sizeof(u32), 1, imf);
2130 //Time to sample box//
2131 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2132 TimeToSampleBox.size, sizeof(u32), 1, imf);
2133 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2134 TimeToSampleBox.type, sizeof(u32), 1, imf);
2135 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2136 TimeToSampleBox.version, sizeof(u32), 1, imf);
2137 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2138 TimeToSampleBox.entry_count, sizeof(u32), 1, imf);
2139 swap = bytereverse(moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2140 TimeToSampleBox.entry_count);
2141 pos = swap;
2142
2143 for (i=0; i<pos; i++) {
2144 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2145 TimeToSampleBox.sample_count[i], sizeof(u32), 1, imf);
2146 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2147 TimeToSampleBox.sample_delta[i], sizeof(u32), 1, imf);
2148 }
2149
2150 //Sample description box//
2151 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2152 SampleDescriptionBox.size, sizeof(u32), 1, imf);
2153 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2154 SampleDescriptionBox.type, sizeof(u32), 1, imf);
2155 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2156 SampleDescriptionBox.version, sizeof(u32), 1, imf);
2157 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2158 SampleDescriptionBox.entry_count, sizeof(u32), 1, imf);
2159 //tx3g
2160 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2161 SampleTableBox.SampleDescriptionBox.TextSampleEntry.size,sizeof(u32),1,imf);
2162 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2163 SampleTableBox.SampleDescriptionBox.TextSampleEntry.type,sizeof(u32),1,imf);
2164 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2165 SampleTableBox.SampleDescriptionBox.TextSampleEntry.a,sizeof(u32),1,imf);
2166 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2167 SampleTableBox.SampleDescriptionBox.TextSampleEntry.b,sizeof(u32),1,imf);
2168 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2169 SampleTableBox.SampleDescriptionBox.TextSampleEntry.displayFlags,sizeof(u32),1,imf);
2170 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2171 SampleTableBox.SampleDescriptionBox.TextSampleEntry.horizontaljustification,sizeof(u8),1,imf);
2172 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2173 SampleTableBox.SampleDescriptionBox.TextSampleEntry.verticaljustification,sizeof(u8),1,imf);
2174 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2175 SampleTableBox.SampleDescriptionBox.TextSampleEntry.backgroundcolorrgba[0],sizeof(u8),1,imf);
2176 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2177 SampleTableBox.SampleDescriptionBox.TextSampleEntry.backgroundcolorrgba[1],sizeof(u8),1,imf);
2178 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2179 SampleTableBox.SampleDescriptionBox.TextSampleEntry.backgroundcolorrgba[2],sizeof(u8),1,imf);
2180 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2181 SampleTableBox.SampleDescriptionBox.TextSampleEntry.backgroundcolorrgba[3],sizeof(u8),1,imf);
2182 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2183 SampleTableBox.SampleDescriptionBox.TextSampleEntry.top,sizeof(u16),1,imf);
2184 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2185 SampleTableBox.SampleDescriptionBox.TextSampleEntry.left,sizeof(u16),1,imf);
2186 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2187 SampleTableBox.SampleDescriptionBox.TextSampleEntry.bottom,sizeof(u16),1,imf);
2188 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2189 SampleTableBox.SampleDescriptionBox.TextSampleEntry.right,sizeof(u16),1,imf);
2190 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2191 SampleTableBox.SampleDescriptionBox.TextSampleEntry.startChar,sizeof(u16),1,imf);
2192 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2193 SampleTableBox.SampleDescriptionBox.TextSampleEntry.endChar,sizeof(u16),1,imf);
2194 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2195 SampleTableBox.SampleDescriptionBox.TextSampleEntry.fontID,sizeof(u16),1,imf);
2196 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2197 SampleTableBox.SampleDescriptionBox.TextSampleEntry.facestyleflags,sizeof(u8),1,imf);
2198 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2199 SampleTableBox.SampleDescriptionBox.TextSampleEntry.fontsize,sizeof(u8),1,imf);
2200 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2201 SampleTableBox.SampleDescriptionBox.TextSampleEntry.textcolorrgba[0],sizeof(u8),1,imf);
2202 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2203 SampleTableBox.SampleDescriptionBox.TextSampleEntry.textcolorrgba[1],sizeof(u8),1,imf);
2204 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2205 SampleTableBox.SampleDescriptionBox.TextSampleEntry.textcolorrgba[2],sizeof(u8),1,imf);
2206 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2207 SampleTableBox.SampleDescriptionBox.TextSampleEntry.textcolorrgba[3],sizeof(u8),1,imf);
2208 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2209 SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.size,sizeof(u32),1,imf);
2210 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2211 SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.type,sizeof(u32),1,imf);
2212 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2213 SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.entrycount,sizeof(u16),1,imf);
2214 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2215 SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.fontID,sizeof(u16),1,imf);
2216 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2217 SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.fontnamelenght,sizeof(u8),1,imf);
2218 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2219 SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.font[0],sizeof(u8),1,imf);
2220 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2221 SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.font[1],sizeof(u8),1,imf);
2222 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2223 SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.font[2],sizeof(u8),1,imf);
2224 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2225 SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.font[3],sizeof(u8),1,imf);
2226 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.
2227 SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.font[4],sizeof(u8),1,imf);
2228 //Sample Size box//
2229 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2230 SampleSizeBox.size, sizeof(u32), 1, imf);
2231 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2232 SampleSizeBox.type, sizeof(u32), 1, imf);
2233 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2234 SampleSizeBox.version, sizeof(u32), 1, imf);
2235 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2236 SampleSizeBox.sample_size, sizeof(u32), 1, imf);
2237 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2238 SampleSizeBox.sample_count, sizeof(u32), 1, imf);
2239 swap = bytereverse(moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2240 SampleSizeBox.sample_count);
2241
2242 for(i=0; i<swap; i++){
2243 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2244 SampleSizeBox.entry_size[i], sizeof(u32), 1, imf);
2245 }
2246
2247 //Sample to chunk box//
2248 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2249 SampleToChunk.size, sizeof(u32), 1, imf);
2250 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2251 SampleToChunk.type, sizeof(u32), 1, imf);
2252 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2253 SampleToChunk.version, sizeof(u32), 1, imf);
2254 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2255 SampleToChunk.entry_count, sizeof(u32), 1, imf);
2256 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2257 SampleToChunk.first_chunk, sizeof(u32), 1, imf);
2258 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2259 SampleToChunk.samples_per_chunk, sizeof(u32), 1, imf);
2260 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2261 SampleToChunk.sample_description_index, sizeof(u32), 1, imf);
2262
2263 //Chunk offset//
2264 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2265 ChunkOffsetBox.size, sizeof(u32), 1, imf);
2266 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2267 ChunkOffsetBox.type, sizeof(u32), 1, imf);
2268 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2269 ChunkOffsetBox.version, sizeof(u32), 1, imf);
2270 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2271 ChunkOffsetBox.entry_count, sizeof(u32), 1, imf);
2272 swap=0x00;
2273 fwrite(&swap,sizeof(u32),1,imf);
2274 for (i=0;i<bytereverse(moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2275 ChunkOffsetBox.entry_count);i++){
2276
2277 fwrite(&moov.TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2278 ChunkOffsetBox.chunk_offset[i], sizeof(u32), 1, imf);
2279 }
2280
2281 //Song Image//
2282 /* Go to function "writemeta" */
2283
2284 }
2285
2286
2287 void writepresets(MovieBox moov, int numtrack,int totaltracks, FILE *imf) {
2288
2289 //int i, j, k, m, t, temp, type;
2290
2291 // this function is executed in "writemoovbox";
2292 // to be implemented as standalone
2293 }
2294
2295 // Fill the MetaBox
2296
2297 int metacontainer(MetaBox *meta) {
2298 //int swap;
2299
2300 u32 sizeMETA;
2301 u32 sizeHDLR = 36;
2302 u32 sizeDINF;
2303 u32 sizeDREF = 0; //16
2304 u32 sizeURL = 0; //12
2305 u32 sizeILOC = 36 - 2;
2306 u32 sizeIINF;
2307 u32 sizeINFE = 44;
2308 u32 sizeXML = 12 + 2000;
2309 char name[6] = "image";
2310 char type_content[18] = "application/other";
2311 char encoding[4] = "jpg";
2312
2313 //METADATA
2314 char xml[2000] = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><Mpeg7 xmlns=\"urn:mpeg:mpeg7:schema:2001\"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:mpeg7=\"urn:mpeg:mpeg7:schema:2001\"xmlns:xml=\"http://www.w3.org/XML/1998/namespace\"xsi:schemaLocation=\"urn:mpeg:mpeg7:schema:2001 Mpeg7-2001.xsd\"><Description xsi:type=\"CreationDescriptionType\"><CreationInformation><Creation><Title type=\"songTitle\">IMAF Song</Title><Title type=\"albumTitle\">Encoder</Title><Abstract><FreeTextAnnotation>QMUL</FreeTextAnnotation></Abstract><Creator><Role href=\"urn:mpeg:mpeg7:RoleCS:2001:PERFORMER\"/><Agent xsi:type=\"PersonType\"><Name><FamilyName></FamilyName><GivenName>Frank Sinatra</GivenName></Name></Agent></Creator><CreationCoordinates><Date><TimePoint>2013</TimePoint></Date></CreationCoordinates><CopyrightString></CopyrightString></Creation><Classification><Genre href=\"urn:id3:cs:ID3genreCS:v1:12\"><Name>Electro</Name></Genre></Classification></CreationInformation></Description><Description xsi:type=\"SemanticDescriptionType\"> <Semantics> <SemanticBase xsi:type=\"SemanticStateType\"><AttributeValuePair><Attribute><TermUse href=\"urn:mpeg:maf:cs:musicplayer:CollectionElementsCS:2007:assetNum\"/></Attribute><IntegerValue>07</IntegerValue></AttributeValuePair><AttributeValuePair><Attribute><TermUse href=\"urn:mpeg:maf:cs:musicplayer:CollectionElementsCS:2013:assetTot\"/></Attribute><IntegerValue>11</IntegerValue></AttributeValuePair></SemanticBase></Semantics></Description><Description xsi:type=\"SemanticDescriptionType\"><MediaInformation><MediaIdentification><EntityIdentifier></EntityIdentifier></MediaIdentification></MediaInformation></Description></Mpeg7>";
2315
2316 sizeDINF = 8;// + sizeDREF + sizeURL;
2317 sizeIINF = 14 + sizeINFE;
2318 sizeMETA = 12 + sizeHDLR + sizeDINF + sizeILOC + sizeIINF + sizeXML;
2319
2320 meta->size = bytereverse(sizeMETA);
2321 meta->type = bytereverse('meta');
2322 meta->version = 0;
2323 //HandlerBox
2324 meta->theHandler.size = bytereverse(sizeHDLR);
2325 meta->theHandler.type = bytereverse('hdlr');
2326 meta->theHandler.version = 0;
2327 meta->theHandler.pre_defined = 0;
2328 meta->theHandler.handler_type = bytereverse('meta');
2329 meta->theHandler.reserved[0] = 0;
2330 meta->theHandler.reserved[1] = 0;
2331 meta->theHandler.reserved[2] = 0;
2332 meta->theHandler.name[0] = bytereverse('m');
2333 meta->theHandler.name[1] = bytereverse('p');
2334 meta->theHandler.name[2] = bytereverse('7');
2335 meta->theHandler.name[3] = bytereverse('t');
2336 //DataInformationBox
2337 meta->file_locations.size = bytereverse(sizeDINF);
2338 meta->file_locations.type = bytereverse('dinf');
2339 /*
2340 //DataReferenceBox
2341 meta->file_locations.DataReferenceBox.size = bytereverse(sizeDREF);
2342 meta->file_locations.DataReferenceBox.type = bytereverse('dref');
2343 meta->file_locations.DataReferenceBox.flags = bytereverse(0); // CHECK THIS VALUE
2344 meta->file_locations.DataReferenceBox.entry_count = bytereverse(1);
2345 //DataEntryUrlBox
2346 meta->file_locations.DataReferenceBox.DataEntryUrlBox.size = bytereverse(sizeURL);
2347 meta->file_locations.DataReferenceBox.DataEntryUrlBox.type = bytereverse('url '); // 'url ' with blank space
2348 meta->file_locations.DataReferenceBox.DataEntryUrlBox.flags = bytereverse(0); // CHECK THIS VALUE
2349 */
2350 //item_location
2351 meta->item_locations.size = bytereverse(sizeILOC);
2352 meta->item_locations.type = bytereverse('iloc');
2353 meta->item_locations.version = 0;
2354 meta->item_locations.offset_size = 68; // 0100|0100 offset_size = 4 + lenght_size = 4
2355 //meta->item_locations.lenght_size = 4; //already included
2356 meta->item_locations.base_offset_size = 64; // 0100|0000 base_offset_size = 4 + reserved = 0
2357 //meta->item_locations.reserved = 0; //already included
2358 meta->item_locations.item_count = bytereverse16(1);
2359 meta->item_locations.item_ID = bytereverse16(1);
2360 meta->item_locations.data_reference_index = 0;
2361 //meta->item_locations.base_offset = bytereverse(); // corresponding to iloc_offset in insertImage function
2362 meta->item_locations.extent_count = bytereverse16(1);
2363 //meta->item_locations.extent_offset = bytereverse(); // corresponding to iloc_offset in insertImage function
2364 //meta->item_locations.extent_length = bytereverse(); // corresponding to imagesize in insertImage function
2365
2366 //ItemInfoBox
2367 meta->item_infos.size = bytereverse(sizeIINF);
2368 meta->item_infos.type = bytereverse('iinf');
2369 meta->item_infos.version = 0;
2370 meta->item_infos.entry_count = bytereverse16(1);
2371
2372 //info_entry
2373 meta->item_infos.info_entry.size = bytereverse(sizeINFE);
2374 meta->item_infos.info_entry.type = bytereverse('infe');
2375 meta->item_infos.info_entry.version = 0;
2376 meta->item_infos.info_entry.item_ID = bytereverse16(1);
2377 meta->item_infos.info_entry.item_protection_index = 0;
2378 strcpy(meta->item_infos.info_entry.item_name, name);
2379 strcpy(meta->item_infos.info_entry.content_type, type_content);
2380 strcpy(meta->item_infos.info_entry.content_encoding, encoding);
2381
2382 //XMLBox (MetaData)
2383 meta->XMLBox.size = bytereverse(sizeXML);
2384 meta->XMLBox.type = bytereverse('xml ');
2385 meta->XMLBox.version = 0;
2386 strcpy(meta->XMLBox.string, xml);
2387
2388 return sizeMETA;
2389 }
2390
2391 //Read the image's size
2392 u32 getImageSize(const char *imagedir){
2393 u32 size;
2394 FILE *img;
2395
2396 img = fopen(imagedir,"rb");
2397
2398 //Calculate size of the picture
2399 fseek(img,0,SEEK_END);
2400 size = ftell(img);
2401 fclose(img);
2402
2403 return size;
2404 }
2405 //Read the text´s size
2406 int getTextSize (FILE *text){
2407
2408 int d=0,sizetext=0,dat=0,dat1=0,dat2=0,dat3=0;
2409
2410 //TEXT-FILE
2411 //Find mdat in text file in order to know the size of the text file
2412 d=0;
2413 while(d==0){
2414
2415 fread (&dat,sizeof(unsigned char),1,text); //read a byte and saves it in dat
2416 fread (&dat1,sizeof(unsigned char),1,text);
2417 fread (&dat2,sizeof(unsigned char),1,text);
2418 fread (&dat3,sizeof(unsigned char),1,text);
2419 fseek(text,-3,SEEK_CUR);
2420
2421 if(dat == 0x6D && dat1 == 0x64 && dat2 == 0x61 && dat3 == 0x74){ // 6D646174 = mdat
2422 d=1;
2423 }
2424
2425 } //close while
2426
2427 fseek (text,-5,SEEK_CUR);//positionate the pointer at the first byte of size
2428 fread (&dat,sizeof(unsigned char),1,text); //first byte of size
2429 fread (&dat1,sizeof(unsigned char),1,text);//second byte of size
2430 fread (&dat2,sizeof(unsigned char),1,text);//third byte of size
2431 fread (&dat3,sizeof(unsigned char),1,text);//fourth byte of size
2432
2433 sizetext = (dat<<24) | (dat1<<16) | (dat2<<8) | (dat3);
2434 sizetext = sizetext-8-16; //4 bytes of size and 4 bytes of type and 16 bytes of padding
2435
2436 fseek (text,20,SEEK_CUR);//Advance 20 bytes, because the pointer must advance 20 bytes to read the size of the first string.This is because of the way that Quicktime creates the 3gp
2437
2438 return sizetext;
2439
2440
2441 }
2442
2443 //Read the JPEG file
2444
2445 void insertImage (MetaBox *meta, FILE **imf, u32 imagesize, const char *imagedir) {
2446
2447 FILE *img;
2448 u64 iloc_offset = 0;
2449 unsigned char *imgbuffer;
2450
2451 img = fopen(imagedir,"rb");
2452
2453 // Binary reading
2454 fseek(img,0,SEEK_SET);
2455 imgbuffer= (unsigned char*)malloc(sizeof(unsigned char)*imagesize);
2456 fread(imgbuffer, 1, imagesize, img);
2457 fclose(img);
2458
2459 // Find position in the IMAF file and write the image
2460 iloc_offset = ftell(*imf);
2461 fwrite(imgbuffer,1,imagesize, *imf);
2462
2463 // Set image size and offset values
2464 meta->item_locations.base_offset = bytereverse(iloc_offset);
2465 meta->item_locations.extent_length = bytereverse(imagesize);
2466 meta->item_locations.extent_offset = bytereverse(iloc_offset);
2467
2468 }
2469
2470
2471 void writemetabox(MetaBox meta, FILE *imf) {
2472
2473 int i=0;
2474
2475 //MetaBox
2476 fwrite(&meta.size, sizeof(u32), 1, imf);
2477 fwrite(&meta.type, sizeof(u32), 1, imf);
2478 fwrite(&meta.version, sizeof(u32), 1, imf);
2479 //Handler
2480 fwrite(&meta.theHandler.size, sizeof(u32), 1, imf);
2481 fwrite(&meta.theHandler.type, sizeof(u32), 1, imf);
2482 fwrite(&meta.theHandler.version, sizeof(u32), 1, imf);
2483 fwrite(&meta.theHandler.pre_defined, sizeof(u32), 1, imf);
2484 fwrite(&meta.theHandler.handler_type, sizeof(u32), 1, imf);
2485 fwrite(&meta.theHandler.reserved[0], sizeof(u32), 1, imf);
2486 fwrite(&meta.theHandler.reserved[1], sizeof(u32), 1, imf);
2487 fwrite(&meta.theHandler.reserved[2], sizeof(u32), 1, imf);
2488 fwrite(&meta.theHandler.name[0], sizeof(unsigned char), 1, imf);
2489 fwrite(&meta.theHandler.name[1], sizeof(unsigned char), 1, imf);
2490 fwrite(&meta.theHandler.name[2], sizeof(unsigned char), 1, imf);
2491 fwrite(&meta.theHandler.name[3], sizeof(unsigned char), 1, imf);
2492 //Data Information Box
2493 fwrite(&meta.file_locations.size, sizeof(u32), 1, imf);
2494 fwrite(&meta.file_locations.type, sizeof(u32), 1, imf);
2495 /*
2496 //Data Reference
2497 fwrite(&meta.file_locations.DataReferenceBox.size, sizeof(u32), 1, imf);
2498 fwrite(&meta.file_locations.DataReferenceBox.type, sizeof(u32), 1, imf);
2499 fwrite(&meta.file_locations.DataReferenceBox.flags, sizeof(u32), 1, imf);
2500 fwrite(&meta.file_locations.DataReferenceBox.entry_count, sizeof(u32), 1, imf);
2501
2502 fwrite(&meta.file_locations.DataReferenceBox.DataEntryUrlBox.size, sizeof(u32), 1, imf);
2503 fwrite(&meta.file_locations.DataReferenceBox.DataEntryUrlBox.type, sizeof(u32), 1, imf);
2504 fwrite(&meta.file_locations.DataReferenceBox.DataEntryUrlBox.flags, sizeof(u32), 1, imf);
2505 */
2506 //Item Location Box
2507 fwrite(&meta.item_locations.size, sizeof(u32), 1, imf);
2508 fwrite(&meta.item_locations.type, sizeof(u32), 1, imf);
2509 fwrite(&meta.item_locations.version, sizeof(u32), 1, imf);
2510 fwrite(&meta.item_locations.offset_size, sizeof(unsigned char), 1, imf);
2511 //fwrite(&meta.item_locations.lenght_size, sizeof(unsigned char), 1, imf);
2512 fwrite(&meta.item_locations.base_offset_size, sizeof(unsigned char), 1, imf);
2513 //fwrite(&meta.item_locations.reserved, sizeof(unsigned char), 1, imf);
2514 fwrite(&meta.item_locations.item_count, sizeof(u16), 1, imf);
2515 fwrite(&meta.item_locations.item_ID, sizeof(u16), 1, imf);
2516 fwrite(&meta.item_locations.data_reference_index, sizeof(u16), 1, imf);
2517 fwrite(&meta.item_locations.base_offset, sizeof(u32), 1, imf);
2518 fwrite(&meta.item_locations.extent_count, sizeof(u16), 1, imf);
2519 fwrite(&meta.item_locations.extent_offset, sizeof(u32), 1, imf);
2520 fwrite(&meta.item_locations.extent_length, sizeof(u32), 1, imf);
2521 //Item Info
2522 fwrite(&meta.item_infos.size, sizeof(u32), 1, imf);
2523 fwrite(&meta.item_infos.type, sizeof(u32), 1, imf);
2524 fwrite(&meta.item_infos.version, sizeof(u32), 1, imf);
2525 fwrite(&meta.item_infos.entry_count, sizeof(u16), 1, imf);
2526 //Info Entry
2527 fwrite(&meta.item_infos.info_entry.size, sizeof(u32), 1, imf);
2528 fwrite(&meta.item_infos.info_entry.type, sizeof(u32), 1, imf);
2529 fwrite(&meta.item_infos.info_entry.version, sizeof(u32), 1, imf);
2530 fwrite(&meta.item_infos.info_entry.item_ID, sizeof(u16), 1, imf);
2531 fwrite(&meta.item_infos.info_entry.item_protection_index, sizeof(u16), 1, imf);
2532 for (i=0; i<6; i++){
2533 fwrite(&meta.item_infos.info_entry.item_name[i], sizeof(char), 1, imf);
2534 }
2535 for (i=0; i<18; i++){
2536 fwrite(&meta.item_infos.info_entry.content_type[i], sizeof(char), 1, imf);
2537 }
2538 for (i=0; i<4; i++){
2539 fwrite(&meta.item_infos.info_entry.content_encoding[i], sizeof(char), 1, imf);
2540 }
2541 //XML for metadata
2542 fwrite(&meta.XMLBox.size, sizeof(u32), 1, imf);
2543 fwrite(&meta.XMLBox.type, sizeof(u32), 1, imf);
2544 fwrite(&meta.XMLBox.version, sizeof(u32), 1, imf);
2545 for(i=0; i<2000; i++){
2546 fwrite(&meta.XMLBox.string[i], sizeof(char), 1, imf);
2547 }
2548
2549 }
2550
2551 //TIMED-TEXT's funcionts
2552 int trackstructure_text (MovieBox *moov, int numtrack, int clock,
2553 int durationTrack, int sizemdat, const char *textfile,FILE *text,int totaltracks ) { // creates the text trak structure
2554 int swap;
2555 u32 sizeURL;
2556 u32 sizeDREF;
2557 u32 sizeDINF;
2558 u32 sizeSMHD;
2559 u32 sizeMINF;
2560 u32 sizeHDLR;
2561 u32 sizeMDHD;
2562 u32 sizeMDIA;
2563 u32 sizeTKHD;
2564 u32 sizeTRAK;
2565
2566
2567 //Sample Table Box
2568 int sizeSTBL = 0;
2569
2570 sizeSTBL = samplecontainer_text(moov, numtrack,sizemdat, textfile,totaltracks);
2571
2572 //Data Entry Url Box
2573 sizeURL = 12;
2574 swap = bytereverse(sizeURL);
2575 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
2576 DataReferenceBox.DataEntryUrlBox.size = swap;
2577 swap = bytereverse('url ');
2578 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
2579 DataReferenceBox.DataEntryUrlBox.type = swap;
2580 swap = bytereverse(1);
2581 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
2582 DataReferenceBox.DataEntryUrlBox.flags = swap; // =1 Track in same file as movie atom.
2583
2584 //Data Reference
2585 sizeDREF = sizeURL+ 16;
2586 swap = bytereverse(sizeDREF);
2587 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
2588 DataReferenceBox.size = swap;
2589 swap = bytereverse('dref');
2590 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
2591 DataReferenceBox.type = swap;
2592 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
2593 DataReferenceBox.flags = 0;
2594 swap = bytereverse(1);
2595 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.
2596 DataReferenceBox.entry_count = swap;
2597
2598 //Data information Box//
2599 sizeDINF = sizeDREF + 8;
2600 swap = bytereverse(sizeDINF);
2601 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.size = swap;
2602 swap = bytereverse('dinf');
2603 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.DataInformationBox.type = swap;
2604
2605
2606 //Null Media Header Box
2607 swap = bytereverse(12);
2608 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.NullMediaHeaderBox.size = swap ;
2609 swap = bytereverse ('nmhd');
2610 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.NullMediaHeaderBox.type = swap;
2611 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.NullMediaHeaderBox.flags = 0;
2612
2613 //Media Information Box//
2614 sizeMINF = sizeDINF + sizeSTBL + 8 + 12;
2615 swap = bytereverse(sizeMINF);
2616 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.size = swap;
2617 swap = bytereverse('minf');
2618 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.type = swap;
2619
2620 //Handler Box//
2621 sizeHDLR = 37;
2622 swap = bytereverse(sizeHDLR);
2623 moov->TrackBox[numtrack].MediaBox.HandlerBox.size = swap;
2624 swap = bytereverse('hdlr');
2625 moov->TrackBox[numtrack].MediaBox.HandlerBox.type = swap;
2626 moov->TrackBox[numtrack].MediaBox.HandlerBox.version = 0;
2627 moov->TrackBox[numtrack].MediaBox.HandlerBox.pre_defined = 0;
2628 swap = bytereverse('text');
2629 moov->TrackBox[numtrack].MediaBox.HandlerBox.handler_type = swap;
2630 moov->TrackBox[numtrack].MediaBox.HandlerBox.reserved[0] = 0;
2631 moov->TrackBox[numtrack].MediaBox.HandlerBox.reserved[1] = 0;
2632 moov->TrackBox[numtrack].MediaBox.HandlerBox.reserved[2] = 0;
2633 moov->TrackBox[numtrack].MediaBox.HandlerBox.data[0] = 't';
2634 moov->TrackBox[numtrack].MediaBox.HandlerBox.data[1] = 'e';
2635 moov->TrackBox[numtrack].MediaBox.HandlerBox.data[2] = 'x';
2636 moov->TrackBox[numtrack].MediaBox.HandlerBox.data[3] = 't';
2637 moov->TrackBox[numtrack].MediaBox.HandlerBox.data[4] = '\0';
2638
2639 //Media Header Box//
2640 sizeMDHD = 32;
2641 swap = bytereverse(sizeMDHD);
2642 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.size = swap;
2643 swap = bytereverse('mdhd');
2644 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.type = swap;
2645 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.version = 0;
2646 swap = bytereverse(clock);
2647 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.creation_time = swap;
2648 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.modification_time = swap;
2649 swap = bytereverse(1000);
2650 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.timescale = swap;
2651 swap = bytereverse(durationTrack);
2652 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.duration = swap;
2653 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.language = 0xC455;
2654 moov->TrackBox[numtrack].MediaBox.MediaHeaderBox.pre_defined = 0;
2655
2656 //Media Box//
2657 sizeMDIA = sizeMDHD + sizeHDLR + sizeMINF + 8;
2658 swap = bytereverse(sizeMDIA);
2659 moov->TrackBox[numtrack].MediaBox.size = swap;
2660 swap = bytereverse('mdia');
2661 moov->TrackBox[numtrack].MediaBox.type = swap;
2662
2663 //Track Header//
2664 sizeTKHD = 92;
2665 swap = bytereverse (sizeTKHD);
2666 moov->TrackBox[numtrack].TrackHeaderBox.size = swap;
2667 swap = bytereverse ('tkhd');
2668 moov->TrackBox[numtrack].TrackHeaderBox.type = swap ;
2669 swap = bytereverse (0x00000007);
2670 moov->TrackBox[numtrack].TrackHeaderBox.version = swap;
2671 swap = bytereverse (clock);
2672 moov->TrackBox[numtrack].TrackHeaderBox.creation_time = swap;
2673 moov->TrackBox[numtrack].TrackHeaderBox.modification_time = swap;
2674 moov->TrackBox[numtrack].TrackHeaderBox.track_ID = bytereverse(12345678);
2675 moov->TrackBox[numtrack].TrackHeaderBox.reserved = 0;
2676 swap = bytereverse (durationTrack);
2677 moov->TrackBox[numtrack].TrackHeaderBox.duration = swap;
2678 moov->TrackBox[numtrack].TrackHeaderBox.reserved2[0] = 0;
2679 moov->TrackBox[numtrack].TrackHeaderBox.reserved2[1] = 0;
2680 moov->TrackBox[numtrack].TrackHeaderBox.layer = 0;
2681 moov->TrackBox[numtrack].TrackHeaderBox.alternate_group = 0;
2682 moov->TrackBox[numtrack].TrackHeaderBox.volume = 0;
2683 moov->TrackBox[numtrack].TrackHeaderBox.reserved3 = 0;
2684 swap = bytereverse (0x00010000);
2685 moov->TrackBox[numtrack].TrackHeaderBox.matrix[0] = swap;
2686 moov->TrackBox[numtrack].TrackHeaderBox.matrix[1] = 0;
2687 moov->TrackBox[numtrack].TrackHeaderBox.matrix[2] = 0;
2688 moov->TrackBox[numtrack].TrackHeaderBox.matrix[3] = 0;
2689 moov->TrackBox[numtrack].TrackHeaderBox.matrix[4] = swap;
2690 moov->TrackBox[numtrack].TrackHeaderBox.matrix[5] = 0;
2691 moov->TrackBox[numtrack].TrackHeaderBox.matrix[6] = 0;
2692 moov->TrackBox[numtrack].TrackHeaderBox.matrix[7] = 0;
2693 swap = bytereverse(0x40000000);
2694 moov->TrackBox[numtrack].TrackHeaderBox.matrix[8] = swap;
2695 moov->TrackBox[numtrack].TrackHeaderBox.width =0;
2696 moov->TrackBox[numtrack].TrackHeaderBox.height = 0;
2697
2698 //Track container
2699 sizeTRAK = sizeTKHD + sizeMDIA + 8;
2700 swap = bytereverse (sizeTRAK); // Size of one track
2701 moov->TrackBox[numtrack].size = swap;
2702 swap = bytereverse ('trak');
2703 moov->TrackBox[numtrack].type = swap;
2704
2705 return sizeTRAK;
2706 }
2707
2708
2709 int aux (FILE *text,int num,int num1,int num2,int num3,int *sal) {
2710 int dat=0,dat1=0,dat2=0,dat3=0,size=0,d=0,cnt=0,i=0;
2711 fseek(text,0,SEEK_SET);
2712 d=0;
2713 while(d==0){
2714
2715 fread (&dat,sizeof(unsigned char),1,text);
2716 cnt++;
2717 fread (&dat1,sizeof(unsigned char),1,text);
2718 cnt++;
2719 fread (&dat2,sizeof(unsigned char),1,text);
2720 cnt++;
2721 fread (&dat3,sizeof(unsigned char),1,text);
2722 cnt++;
2723 fseek(text, -3 ,SEEK_CUR);
2724 if(dat == num && dat1 == num1 && dat2 == num2 && dat3 == num3){
2725
2726 d=1;
2727 }
2728
2729 else{
2730
2731 cnt=cnt-3;
2732 }
2733
2734 } //end while
2735
2736
2737 fseek (text,0,SEEK_SET);//positionate the pointer at first
2738
2739 cnt=cnt-7;//size is located 7 bytes before type
2740 for (d=1;d<=cnt;d++){
2741
2742 fread (&dat,sizeof(unsigned char),1,text);
2743
2744 }
2745 fread (&dat1,sizeof(unsigned char),1,text);
2746 fread (&dat2,sizeof(unsigned char),1,text);
2747 fread (&dat3,sizeof(unsigned char),1,text);
2748
2749 size = (dat<<24) | (dat1<<16) | (dat2<<8) | (dat3);
2750
2751 for (i=1;i<=8;i++) { //advance sample_size
2752 fread (&dat,sizeof(unsigned char),1,text);
2753 }
2754 fread(&dat,1,1,text);
2755 fread(&dat1,1,1,text);
2756 fread(&dat2,1,1,text);
2757 fread(&dat3,1,1,text);
2758
2759 *sal = (dat<<24) | (dat1<<16) | (dat2<<8) | (dat3);
2760
2761
2762 return size;
2763
2764 }
2765
2766 int samplecontainer_text(MovieBox *moov, int numtrack, int sizemdat,const char *textfiles,int totaltracks) {
2767
2768 u32 sizeSTSD, sizeSTSZ, swap,i=0;
2769 u32 sizeSTTS;
2770 u32 sizeSTSC = 0;
2771 u32 sizeSTCO = 0;
2772 u32 sizeSTBL=0;
2773 int dat=0,dat1=0,dat2=0,dat3=0,sal=0, samplecount=0;
2774
2775 //Sample Description Box//
2776 FILE *text;
2777
2778 text=fopen ( textfiles,"rb");
2779 fseek(text,0,SEEK_CUR);
2780
2781 //stsd
2782
2783 sizeSTSD =16+64;
2784 swap = bytereverse(sizeSTSD);
2785 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2786 SampleDescriptionBox.size = swap;
2787 swap = bytereverse('stsd');
2788 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2789 SampleDescriptionBox.type = swap;
2790 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2791 SampleDescriptionBox.version = 0;
2792 swap = bytereverse(1);
2793 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2794 SampleDescriptionBox.entry_count = swap;
2795
2796
2797 //tx3g
2798 swap = bytereverse(64);
2799 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.size=swap;
2800 swap = bytereverse('tx3g');
2801 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.type=swap;
2802 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.a=0;
2803 swap=bytereverse(1);
2804 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.b=swap;
2805 swap = bytereverse(0x00000800);//continuous karaoke
2806 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.displayFlags = swap;
2807 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.horizontaljustification=0x01;
2808 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.verticaljustification=0x01;
2809 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.backgroundcolorrgba[0]= 0x00;
2810 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.backgroundcolorrgba[1]= 0x00;
2811 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.backgroundcolorrgba[2]= 0x00;
2812 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.backgroundcolorrgba[3]= 0xFF;
2813 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.top=0;
2814 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.left=0;
2815 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.bottom=0x6400;
2816 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.right=0x2C01;
2817 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.startChar=0;
2818 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.endChar=0;
2819 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.fontID=0x0100;
2820 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.facestyleflags=0x01;
2821 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.fontsize=0x0A;
2822 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.textcolorrgba[0]=0xFF;
2823 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.textcolorrgba[1]=0xFF;
2824 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.textcolorrgba[2]=0xFF;
2825 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.textcolorrgba[3]=0xFF;
2826 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.size =0x12000000;
2827 swap =bytereverse('ftab');
2828 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.type =swap;
2829 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.entrycount=0x0100;
2830 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.fontID =0x0200;
2831 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.fontnamelenght =0x05;
2832 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.font[0] =0x53;
2833 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.font[1] =0x65;
2834 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.font[2] =0x72;
2835 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.font[3] =0x69;
2836 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleDescriptionBox.TextSampleEntry.FontTableBox.font[4] =0x66;
2837
2838 //stsz
2839
2840 sizeSTSZ=aux(text,0x73,0x74,0x73,0x7A,&sal)+4; // the function aux looks for the box stsz in 3gp file and returns the size
2841 swap = bytereverse('stsz');
2842 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2843 SampleSizeBox.type = swap;
2844 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2845 SampleSizeBox.version = 0;
2846 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleSizeBox.sample_size=0;
2847
2848 fseek(text,0,SEEK_CUR);
2849
2850 fread(&dat,1,1,text); //read sample_count
2851 fread(&dat1,1,1,text);
2852 fread(&dat2,1,1,text);
2853 fread(&dat3,1,1,text);
2854
2855 samplecount = (dat<<24) | (dat1<<16) | (dat2<<8) | (dat3);
2856
2857 swap= bytereverse(samplecount);
2858 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleSizeBox.sample_count=swap;
2859
2860 for (i=1;i<=samplecount;i++){
2861
2862 swap = bytereverse(size_phrase[i-1]);
2863 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleSizeBox.entry_size[i-1]=swap;
2864 }
2865
2866 sizeSTSZ= 20 + bytereverse(moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleSizeBox.sample_count)*4;
2867 swap = bytereverse(sizeSTSZ);
2868 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2869 SampleSizeBox.size = swap;
2870
2871
2872 //Time To Sample Box//
2873
2874 fseek (text,0,SEEK_SET);//positionate the pointer at first
2875 sizeSTTS=aux(text,0x73,0x74,0x74,0x73,&sal);
2876
2877 swap = bytereverse(sizeSTTS);
2878 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2879 TimeToSampleBox.size = swap;
2880 swap = bytereverse('stts');
2881 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2882 TimeToSampleBox.type = swap;
2883 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2884 TimeToSampleBox.version = 0;
2885 swap = bytereverse(sal);
2886 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.TimeToSampleBox.entry_count=swap;
2887 fseek(text,0,SEEK_CUR);
2888 for(i=1;i<=sal;i++){
2889
2890 fread(&dat,1,1,text);//read count
2891 fread(&dat1,1,1,text);
2892 fread(&dat2,1,1,text);
2893 fread(&dat3,1,1,text);
2894
2895 dat = (dat<<24) | (dat1<<16) | (dat2<<8) | (dat3);
2896
2897
2898 swap = bytereverse(dat);
2899 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.TimeToSampleBox.sample_count[i-1]=swap;
2900 fread(&dat,1,1,text);//read delta
2901
2902 fread(&dat1,1,1,text);
2903
2904 fread(&dat2,1,1,text);
2905
2906 fread(&dat3,1,1,text);
2907
2908
2909 dat = (dat<<24) | (dat1<<16) | (dat2<<8) | (dat3);
2910
2911 swap = bytereverse(dat/0.6);// the input text file has time_scale = 600 but the audio has time scale=1000
2912 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.TimeToSampleBox.sample_delta[i-1]=swap;
2913 }
2914
2915 //Sample To Chunk//
2916
2917 dat=0;
2918 fseek (text,0,SEEK_SET);//positionate the pointer at first
2919 sizeSTSC = aux(text,0x73,0x74,0x73,0x63,&sal);
2920 dat=sal;
2921 swap = bytereverse(sizeSTSC);
2922 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2923 SampleToChunk.size = swap;
2924 swap = bytereverse('stsc');
2925 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2926 SampleToChunk.type = swap;
2927 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2928 SampleToChunk.version = 0;
2929 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2930 SampleToChunk.entry_count = bytereverse(1);
2931 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleToChunk.first_chunk=bytereverse(1);
2932 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleToChunk.samples_per_chunk=moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleSizeBox.sample_count;
2933 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.SampleToChunk.sample_description_index = bytereverse(1);
2934
2935 //Chunk Offset Box//
2936 sizeSTCO=24;
2937 swap = bytereverse(sizeSTCO);
2938 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2939 ChunkOffsetBox.size = swap;
2940 swap = bytereverse('co64');
2941 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2942 ChunkOffsetBox.type = swap;
2943 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2944 ChunkOffsetBox.version = 0;
2945 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2946 ChunkOffsetBox.entry_count = bytereverse(1);
2947 dat=32+sizemdat*totaltracks;
2948 swap=bytereverse(dat);
2949 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.
2950 ChunkOffsetBox.chunk_offset [0]= swap;
2951
2952 //Sample Table Box //
2953 sizeSTBL = 8 + sizeSTSD + sizeSTSZ + sizeSTSC + sizeSTCO + sizeSTTS;
2954 swap = bytereverse(sizeSTBL);
2955 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.size = swap;
2956 swap = bytereverse('stbl');
2957 moov->TrackBox[numtrack].MediaBox.MediaInformationBox.SampleTableBox.type =swap;
2958 return sizeSTBL;
2959 }
2960
2961 //AUX Functions for endianness
2962
2963 int bytereverse(int num) {
2964 int swapped;
2965 swapped = ((num>>24)&0xff) | // move byte 3 to byte 0
2966 ((num<<8)&0xff0000) | // move byte 1 to byte 2
2967 ((num>>8)&0xff00) | // move byte 2 to byte 1
2968 ((num<<24)&0xff000000); // byte 0 to byte 3
2969 return swapped;
2970 }
2971 int bytereverse16(int num) {
2972 int swapped;
2973 swapped = ( ((num<<8)&0xff00) | // move byte 1 to byte 2
2974 ((num>>8)&0x00ff) ); // byte 2 to byte 1
2975 return swapped;
2976 }