00001 #include "funcprotos.h"
00002 #include "quicktime.h"
00003
00004
00005
00006 void quicktime_stts_init(quicktime_stts_t *stts)
00007 {
00008 stts->version = 0;
00009 stts->flags = 0;
00010 stts->total_entries = 0;
00011 }
00012
00013 void quicktime_stts_init_table(quicktime_stts_t *stts)
00014 {
00015 if(!stts->total_entries)
00016 {
00017 stts->total_entries = 1;
00018 stts->table = (quicktime_stts_table_t*)malloc(sizeof(quicktime_stts_table_t) * stts->total_entries);
00019 }
00020 }
00021
00022 void quicktime_stts_init_video(quicktime_t *file, quicktime_stts_t *stts, int time_scale, float frame_rate)
00023 {
00024 quicktime_stts_table_t *table;
00025 quicktime_stts_init_table(stts);
00026 table = &(stts->table[0]);
00027
00028 table->sample_count = 0;
00029 table->sample_duration = time_scale / frame_rate;
00030
00031 }
00032
00033 void quicktime_stts_init_audio(quicktime_t *file, quicktime_stts_t *stts, int sample_rate)
00034 {
00035 quicktime_stts_table_t *table;
00036 quicktime_stts_init_table(stts);
00037 table = &(stts->table[0]);
00038
00039 table->sample_count = 0;
00040 table->sample_duration = 1;
00041 }
00042
00043 void quicktime_stts_append_audio(quicktime_t *file,
00044 quicktime_stts_t *stts,
00045 int sample_duration)
00046 {
00047 quicktime_stts_table_t *table;
00048 if(stts->total_entries)
00049 table = &(stts->table[stts->total_entries - 1]);
00050 else
00051 table = 0;
00052
00053 stts->is_vbr = 1;
00054
00055
00056 if(table && table->sample_count)
00057 {
00058 if(table->sample_duration == sample_duration)
00059 {
00060 table->sample_count++;
00061 return;
00062 }
00063 }
00064 else
00065
00066 if(table && table->sample_count == 0)
00067 {
00068 table->sample_duration = sample_duration;
00069 table->sample_count = 1;
00070 return;
00071 }
00072
00073
00074 stts->total_entries++;
00075 stts->table = realloc(stts->table,
00076 sizeof(quicktime_stts_table_t) * stts->total_entries);
00077 table = &(stts->table[stts->total_entries - 1]);
00078 table->sample_duration = sample_duration;
00079 table->sample_count++;
00080 }
00081
00082
00083 int64_t quicktime_stts_total_samples(quicktime_t *file,
00084 quicktime_stts_t *stts)
00085 {
00086 int i;
00087 int64_t result = 0;
00088 for(i = 0; i < stts->total_entries; i++)
00089 {
00090 result += stts->table[i].sample_count;
00091 }
00092 return result;
00093 }
00094
00095
00096 void quicktime_stts_delete(quicktime_stts_t *stts)
00097 {
00098 if(stts->total_entries) free(stts->table);
00099 stts->total_entries = 0;
00100 }
00101
00102 void quicktime_stts_dump(quicktime_stts_t *stts)
00103 {
00104 int i;
00105 printf(" time to sample\n");
00106 printf(" version %d\n", stts->version);
00107 printf(" flags %d\n", stts->flags);
00108 printf(" total_entries %d\n", stts->total_entries);
00109 for(i = 0; i < stts->total_entries; i++)
00110 {
00111 printf(" count %ld duration %ld\n", stts->table[i].sample_count, stts->table[i].sample_duration);
00112 }
00113 }
00114
00115 void quicktime_read_stts(quicktime_t *file, quicktime_stts_t *stts)
00116 {
00117 int i;
00118 stts->version = quicktime_read_char(file);
00119 stts->flags = quicktime_read_int24(file);
00120 stts->total_entries = quicktime_read_int32(file);
00121
00122 stts->table = (quicktime_stts_table_t*)malloc(sizeof(quicktime_stts_table_t) * stts->total_entries);
00123 for(i = 0; i < stts->total_entries; i++)
00124 {
00125 stts->table[i].sample_count = quicktime_read_int32(file);
00126 stts->table[i].sample_duration = quicktime_read_int32(file);
00127 }
00128 }
00129
00130 void quicktime_write_stts(quicktime_t *file, quicktime_stts_t *stts)
00131 {
00132 int i;
00133 quicktime_atom_t atom;
00134 quicktime_atom_write_header(file, &atom, "stts");
00135
00136 quicktime_write_char(file, stts->version);
00137 quicktime_write_int24(file, stts->flags);
00138 quicktime_write_int32(file, stts->total_entries);
00139 for(i = 0; i < stts->total_entries; i++)
00140 {
00141 quicktime_write_int32(file, stts->table[i].sample_count);
00142 quicktime_write_int32(file, stts->table[i].sample_duration);
00143 }
00144
00145 quicktime_atom_write_footer(file, &atom);
00146 }
00147
00148
00149
00150 int quicktime_time_to_sample(quicktime_stts_t *stts,
00151 int64_t *start_time)
00152 {
00153 int result = 0;
00154 int current_entry = 0;
00155 int64_t current_start_time = 0;
00156 while(current_entry < stts->total_entries)
00157 {
00158 quicktime_stts_table_t *stts_table = &stts->table[current_entry];
00159 int sample_count = stts_table->sample_count;
00160 while(sample_count > 0)
00161 {
00162 current_start_time += stts_table->sample_duration;
00163
00164 if(current_start_time > *start_time)
00165 {
00166 current_start_time -= stts_table->sample_duration;
00167 *start_time = current_start_time;
00168 return result;
00169 }
00170
00171
00172 sample_count--;
00173 result++;
00174 }
00175 current_entry++;
00176 }
00177
00178 return result > 0 ? result - 1 : result;
00179 }
00180