1 #include "funcprotos.h"
6 void quicktime_stts_init(quicktime_stts_t
*stts
)
10 stts
->total_entries
= 0;
13 void quicktime_stts_init_table(quicktime_stts_t
*stts
)
15 if(!stts
->total_entries
)
17 stts
->total_entries
= 1;
18 stts
->table
= (quicktime_stts_table_t
*)malloc(sizeof(quicktime_stts_table_t
) * stts
->total_entries
);
22 void quicktime_stts_init_video(quicktime_t
*file
, quicktime_stts_t
*stts
, int time_scale
, float frame_rate
)
24 quicktime_stts_table_t
*table
;
25 quicktime_stts_init_table(stts
);
26 table
= &(stts
->table
[0]);
28 table
->sample_count
= 0; /* need to set this when closing */
29 table
->sample_duration
= time_scale
/ frame_rate
;
30 //printf("quicktime_stts_init_video %d %f\n", time_scale, frame_rate);
33 void quicktime_stts_init_audio(quicktime_t
*file
, quicktime_stts_t
*stts
, int sample_rate
)
35 quicktime_stts_table_t
*table
;
36 quicktime_stts_init_table(stts
);
37 table
= &(stts
->table
[0]);
39 table
->sample_count
= 0; /* need to set this when closing */
40 table
->sample_duration
= 1;
43 void quicktime_stts_append_audio(quicktime_t
*file
,
44 quicktime_stts_t
*stts
,
47 quicktime_stts_table_t
*table
;
48 if(stts
->total_entries
)
49 table
= &(stts
->table
[stts
->total_entries
- 1]);
55 // Expand existing entry
56 if(table
&& table
->sample_count
)
58 if(table
->sample_duration
== sample_duration
)
60 table
->sample_count
++;
65 // Override existing entry
66 if(table
&& table
->sample_count
== 0)
68 table
->sample_duration
= sample_duration
;
69 table
->sample_count
= 1;
74 stts
->total_entries
++;
75 stts
->table
= realloc(stts
->table
,
76 sizeof(quicktime_stts_table_t
) * stts
->total_entries
);
77 table
= &(stts
->table
[stts
->total_entries
- 1]);
78 table
->sample_duration
= sample_duration
;
79 table
->sample_count
++;
83 int64_t quicktime_stts_total_samples(quicktime_t
*file
,
84 quicktime_stts_t
*stts
)
88 for(i
= 0; i
< stts
->total_entries
; i
++)
90 result
+= stts
->table
[i
].sample_count
;
96 void quicktime_stts_delete(quicktime_stts_t
*stts
)
98 if(stts
->total_entries
) free(stts
->table
);
99 stts
->total_entries
= 0;
102 void quicktime_stts_dump(quicktime_stts_t
*stts
)
105 printf(" time to sample\n");
106 printf(" version %d\n", stts
->version
);
107 printf(" flags %d\n", stts
->flags
);
108 printf(" total_entries %d\n", stts
->total_entries
);
109 for(i
= 0; i
< stts
->total_entries
; i
++)
111 printf(" count %ld duration %ld\n", stts
->table
[i
].sample_count
, stts
->table
[i
].sample_duration
);
115 void quicktime_read_stts(quicktime_t
*file
, quicktime_stts_t
*stts
)
118 stts
->version
= quicktime_read_char(file
);
119 stts
->flags
= quicktime_read_int24(file
);
120 stts
->total_entries
= quicktime_read_int32(file
);
122 stts
->table
= (quicktime_stts_table_t
*)malloc(sizeof(quicktime_stts_table_t
) * stts
->total_entries
);
123 for(i
= 0; i
< stts
->total_entries
; i
++)
125 stts
->table
[i
].sample_count
= quicktime_read_int32(file
);
126 stts
->table
[i
].sample_duration
= quicktime_read_int32(file
);
130 void quicktime_write_stts(quicktime_t
*file
, quicktime_stts_t
*stts
)
133 quicktime_atom_t atom
;
134 quicktime_atom_write_header(file
, &atom
, "stts");
136 quicktime_write_char(file
, stts
->version
);
137 quicktime_write_int24(file
, stts
->flags
);
138 quicktime_write_int32(file
, stts
->total_entries
);
139 for(i
= 0; i
< stts
->total_entries
; i
++)
141 quicktime_write_int32(file
, stts
->table
[i
].sample_count
);
142 quicktime_write_int32(file
, stts
->table
[i
].sample_duration
);
145 quicktime_atom_write_footer(file
, &atom
);
148 // Return the sample which contains the start_time argument.
149 // Stores the actual starting time of the sample in the start_time argument.
150 int quicktime_time_to_sample(quicktime_stts_t
*stts
,
154 int current_entry
= 0;
155 int64_t current_start_time
= 0;
156 while(current_entry
< stts
->total_entries
)
158 quicktime_stts_table_t
*stts_table
= &stts
->table
[current_entry
];
159 int sample_count
= stts_table
->sample_count
;
160 while(sample_count
> 0)
162 current_start_time
+= stts_table
->sample_duration
;
163 // Current sample contains start time
164 if(current_start_time
> *start_time
)
166 current_start_time
-= stts_table
->sample_duration
;
167 *start_time
= current_start_time
;
178 return result
> 0 ? result
- 1 : result
;