1 #include "funcprotos.h"
3 #include "workarounds.h"
9 int quicktime_moov_init(quicktime_moov_t
*moov
)
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
));
21 int quicktime_moov_delete(quicktime_moov_t
*moov
)
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
));
31 void quicktime_moov_dump(quicktime_moov_t
*moov
)
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
)
46 quicktime_atom_t leaf_atom
;
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
);
60 if(quicktime_atom_is(&leaf_atom
, "clip"))
62 quicktime_atom_skip(file
, &leaf_atom
);
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
);
71 if(quicktime_atom_is(&leaf_atom
, "udta"))
73 quicktime_read_udta(file
, &(moov
->udta
), &leaf_atom
);
74 quicktime_atom_skip(file
, &leaf_atom
);
77 if(quicktime_atom_is(&leaf_atom
, "ctab"))
79 quicktime_read_ctab(file
, &(moov
->ctab
));
82 quicktime_atom_skip(file
, &leaf_atom
);
83 }while(quicktime_position(file
) < parent_atom
->end
);
88 void quicktime_write_moov(quicktime_t
*file
, quicktime_moov_t
*moov
)
90 quicktime_atom_t atom
;
92 long int64_t_duration
= 0;
93 long duration
, timescale
;
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
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
, ×cale
);
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
)
146 for(i
= 0; i
< moov
->total_tracks
; i
++)
148 quicktime_trak_shift_offsets(moov
->trak
[i
], offset
);