r730: Added italian translation
[cinelerra_cv/mob.git] / quicktime / stts.c
bloba3701e3d0972f39ee4e82112509b033eba2a8f11
1 #include "funcprotos.h"
2 #include "quicktime.h"
6 void quicktime_stts_init(quicktime_stts_t *stts)
8 stts->version = 0;
9 stts->flags = 0;
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,
45 int sample_duration)
47 quicktime_stts_table_t *table;
48 if(stts->total_entries)
49 table = &(stts->table[stts->total_entries - 1]);
50 else
51 table = 0;
53 stts->is_vbr = 1;
55 // Expand existing entry
56 if(table && table->sample_count)
58 if(table->sample_duration == sample_duration)
60 table->sample_count++;
61 return;
64 else
65 // Override existing entry
66 if(table && table->sample_count == 0)
68 table->sample_duration = sample_duration;
69 table->sample_count = 1;
70 return;
73 // Append new entry
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)
86 int i;
87 int64_t result = 0;
88 for(i = 0; i < stts->total_entries; i++)
90 result += stts->table[i].sample_count;
92 return result;
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)
104 int i;
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)
117 int i;
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)
132 int i;
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,
151 int64_t *start_time)
153 int result = 0;
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;
168 return result;
171 // Next sample
172 sample_count--;
173 result++;
175 current_entry++;
178 return result > 0 ? result - 1 : result;