r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / quicktime / moov.c
blobc39eec63ff022371d602a17b30c77070283500c6
1 #include "funcprotos.h"
2 #include "quicktime.h"
3 #include "workarounds.h"
9 int quicktime_moov_init(quicktime_moov_t *moov)
11 int i;
13 moov->total_tracks = 0;
14 for(i = 0 ; i < MAXTRACKS; i++) moov->trak[i] = 0;
15 quicktime_mvhd_init(&(moov->mvhd));
16 quicktime_udta_init(&(moov->udta));
17 quicktime_ctab_init(&(moov->ctab));
18 return 0;
21 int quicktime_moov_delete(quicktime_moov_t *moov)
23 int i;
24 while(moov->total_tracks) quicktime_delete_trak(moov);
25 quicktime_mvhd_delete(&(moov->mvhd));
26 quicktime_udta_delete(&(moov->udta));
27 quicktime_ctab_delete(&(moov->ctab));
28 return 0;
31 void quicktime_moov_dump(quicktime_moov_t *moov)
33 int i;
34 printf("movie\n");
35 quicktime_mvhd_dump(&(moov->mvhd));
36 quicktime_udta_dump(&(moov->udta));
37 for(i = 0; i < moov->total_tracks; i++)
38 quicktime_trak_dump(moov->trak[i]);
39 quicktime_ctab_dump(&(moov->ctab));
43 int quicktime_read_moov(quicktime_t *file, quicktime_moov_t *moov, quicktime_atom_t *parent_atom)
45 /* mandatory mvhd */
46 quicktime_atom_t leaf_atom;
48 // AVI translation:
49 // strh -> mvhd
53 quicktime_atom_read_header(file, &leaf_atom);
55 if(quicktime_atom_is(&leaf_atom, "mvhd"))
57 quicktime_read_mvhd(file, &(moov->mvhd), &leaf_atom);
59 else
60 if(quicktime_atom_is(&leaf_atom, "clip"))
62 quicktime_atom_skip(file, &leaf_atom);
64 else
65 if(quicktime_atom_is(&leaf_atom, "trak"))
67 quicktime_trak_t *trak = quicktime_add_trak(file);
68 quicktime_read_trak(file, trak, &leaf_atom);
70 else
71 if(quicktime_atom_is(&leaf_atom, "udta"))
73 quicktime_read_udta(file, &(moov->udta), &leaf_atom);
74 quicktime_atom_skip(file, &leaf_atom);
76 else
77 if(quicktime_atom_is(&leaf_atom, "ctab"))
79 quicktime_read_ctab(file, &(moov->ctab));
81 else
82 quicktime_atom_skip(file, &leaf_atom);
83 }while(quicktime_position(file) < parent_atom->end);
85 return 0;
88 void quicktime_write_moov(quicktime_t *file, quicktime_moov_t *moov)
90 quicktime_atom_t atom;
91 int i;
92 long int64_t_duration = 0;
93 long duration, timescale;
94 int result;
97 // Try moov header immediately
98 file->mdat.atom.end = quicktime_position(file);
99 result = quicktime_atom_write_header(file, &atom, "moov");
101 // Disk full. Rewind and try again
102 if(result)
104 quicktime_set_position(file, file->mdat.atom.end - (int64_t)0x100000);
105 file->mdat.atom.end = quicktime_position(file);
106 quicktime_atom_write_header(file, &atom, "moov");
109 /* get the duration from the int64_t track in the mvhd's timescale */
110 for(i = 0; i < moov->total_tracks; i++)
112 quicktime_trak_fix_counts(file, moov->trak[i]);
113 quicktime_trak_duration(moov->trak[i], &duration, &timescale);
115 duration = (long)((float)duration / timescale * moov->mvhd.time_scale);
117 if(duration > int64_t_duration)
119 int64_t_duration = duration;
122 moov->mvhd.duration = int64_t_duration;
123 moov->mvhd.selection_duration = int64_t_duration;
125 quicktime_write_mvhd(file, &(moov->mvhd));
126 quicktime_write_udta(file, &(moov->udta));
127 for(i = 0; i < moov->total_tracks; i++)
129 quicktime_write_trak(file, moov->trak[i], moov->mvhd.time_scale);
131 /*quicktime_write_ctab(file, &(moov->ctab)); */
133 quicktime_atom_write_footer(file, &atom);
134 // Rewind to end of mdat
135 quicktime_set_position(file, file->mdat.atom.end);
138 void quicktime_update_durations(quicktime_moov_t *moov)
143 int quicktime_shift_offsets(quicktime_moov_t *moov, int64_t offset)
145 int i;
146 for(i = 0; i < moov->total_tracks; i++)
148 quicktime_trak_shift_offsets(moov->trak[i], offset);
150 return 0;