r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / quicktime / avi_idx1.c
blob428eb5313993eecdbda4bb711fd7deb71cb5cb0d
1 #include "funcprotos.h"
2 #include "quicktime.h"
6 typedef struct
8 char tag[4];
9 int32_t flags;
10 int32_t offset;
11 int32_t size;
12 } avi_tag_t;
16 static int is_keyframe(quicktime_trak_t *trak, int frame)
18 int i;
19 quicktime_stss_t *stss = &trak->mdia.minf.stbl.stss;
20 frame++;
21 for(i = 0; i < stss->total_entries; i++)
23 if(stss->table[i].sample == frame) return 1;
25 return 0;
29 void quicktime_delete_idx1(quicktime_idx1_t *idx1)
31 if(idx1->table) free(idx1->table);
34 void quicktime_read_idx1(quicktime_t *file,
35 quicktime_riff_t *riff,
36 quicktime_atom_t *parent_atom)
38 int i;
39 quicktime_riff_t *first_riff = file->riff[0];
40 quicktime_hdrl_t *hdrl = &first_riff->hdrl;
41 quicktime_idx1_t *idx1 = &riff->idx1;
43 //printf("quicktime_read_idx1 1 %llx\n", quicktime_position(file));
45 // Allocate table.
46 idx1->table_size = (parent_atom->end - quicktime_position(file)) / 16;
47 idx1->table_allocation = idx1->table_size;
48 idx1->table = calloc(sizeof(quicktime_idx1table_t), idx1->table_size);
49 //printf("quicktime_read_idx1 10\n");
51 // Store it in idx1 table now.
52 // Wait for full ix table discovery before converting to stco.
53 for(i = 0; i < idx1->table_size; i++)
55 quicktime_idx1table_t *idx1table = idx1->table + i;
57 quicktime_read_data(file, idx1table->tag, 4);
58 idx1table->flags = quicktime_read_int32_le(file);
59 idx1table->offset = quicktime_read_int32_le(file);
60 idx1table->size = quicktime_read_int32_le(file);
63 //printf("quicktime_read_idx1 100\n");
68 #if 0
69 while(quicktime_position(file) <= parent_atom->end - 0x10)
71 int current_vtrack = 0;
72 int current_atrack = 0;
73 quicktime_read_data(file, data, 4);
75 for(i = 0; i < file->moov.total_tracks; i++)
77 if(quicktime_match_24(data, tags[i]))
79 // Flags
80 int flags = quicktime_read_int32_le(file);
81 int64_t offset = quicktime_read_int32_le(file);
83 // Offset from start of movi
84 quicktime_update_stco(&file->moov.trak[i]->mdia.minf.stbl.stco,
85 file->moov.trak[i]->mdia.minf.stbl.stco.total_entries + 1,
86 offset);
88 if(file->moov.trak[i]->mdia.minf.is_audio)
90 long chunk_size;
92 // Bytes in chunk
93 chunk_size = quicktime_read_int32_le(file);
96 // Update samples per chunk table if PCM
97 if(file->moov.trak[i]->mdia.minf.stbl.stsd.table[0].packet_size > 0)
99 quicktime_update_stsc(&file->moov.trak[i]->mdia.minf.stbl.stsc,
100 file->moov.trak[i]->mdia.minf.stbl.stsc.total_entries + 1,
101 chunk_size /
102 file->moov.trak[i]->mdia.minf.stbl.stsd.table[0].packet_size);
105 else
107 long sample_size;
109 // Bytes in chunk
110 sample_size = quicktime_read_int32_le(file);
112 //printf("quicktime_read_idx1 5 %d %d\n", file->moov.trak[i]->mdia.minf.stbl.stsz.total_entries, current_vtrack);
114 // Update keyframe table
115 if((flags & AVI_KEYFRAME) == AVI_KEYFRAME)
116 quicktime_insert_keyframe(file,
117 file->moov.trak[i]->mdia.minf.stbl.stsz.total_entries,
118 current_vtrack);
119 //printf("quicktime_read_idx1 6\n");
121 // Update samplesize table
122 quicktime_update_stsz(&file->moov.trak[i]->mdia.minf.stbl.stsz,
123 file->moov.trak[i]->mdia.minf.stbl.stsz.total_entries,
124 sample_size);
125 //printf("quicktime_read_idx1 7\n");
129 if(file->moov.trak[i]->mdia.minf.is_audio)
130 current_atrack++;
131 else
132 current_vtrack++;
136 quicktime_atom_skip(file, parent_atom);
137 #endif
143 void quicktime_write_idx1(quicktime_t *file,
144 quicktime_idx1_t *idx1)
146 int i;
147 quicktime_idx1table_t *table = idx1->table;
148 int table_size = idx1->table_size;
152 // Write table
153 quicktime_atom_write_header(file, &idx1->atom, "idx1");
155 for(i = 0; i < table_size; i++)
157 quicktime_idx1table_t *entry = &table[i];
158 quicktime_write_char32(file, entry->tag);
159 quicktime_write_int32_le(file, entry->flags);
160 quicktime_write_int32_le(file, entry->offset);
161 quicktime_write_int32_le(file, entry->size);
165 quicktime_atom_write_footer(file, &idx1->atom);
168 void quicktime_set_idx1_keyframe(quicktime_t *file,
169 quicktime_trak_t *trak,
170 int new_keyframe)
172 quicktime_riff_t *riff = file->riff[0];
173 quicktime_hdrl_t *hdrl = &riff->hdrl;
174 quicktime_strl_t *strl = hdrl->strl[trak->tkhd.track_id - 1];
175 char *tag = strl->tag;
176 quicktime_idx1_t *idx1 = &riff->idx1;
177 int i;
178 int counter = -1;
180 // Search through entire index for right numbered tag.
181 // Since all the tracks are combined in the same index, this is unavoidable.
182 for(i = 0; i < idx1->table_size; i++)
184 quicktime_idx1table_t *idx1_table = &idx1->table[i];
185 if(!memcmp(idx1_table->tag, tag, 4))
187 counter++;
188 if(counter == new_keyframe)
190 idx1_table->flags |= AVI_KEYFRAME;
191 break;
197 void quicktime_update_idx1table(quicktime_t *file,
198 quicktime_trak_t *trak,
199 int offset,
200 int size)
202 quicktime_riff_t *riff = file->riff[0];
203 quicktime_hdrl_t *hdrl = &riff->hdrl;
204 quicktime_strl_t *strl = hdrl->strl[trak->tkhd.track_id - 1];
205 char *tag = strl->tag;
206 quicktime_idx1_t *idx1 = &riff->idx1;
207 quicktime_movi_t *movi = &riff->movi;
208 quicktime_idx1table_t *idx1_table;
209 quicktime_stss_t *stss = &trak->mdia.minf.stbl.stss;
210 uint32_t flags = 0;
211 int i;
212 int keyframe_frame = idx1->table_size + 1;
214 // Set flag for keyframe
215 for(i = stss->total_entries - 1; i >= 0; i--)
217 if(stss->table[i].sample == keyframe_frame)
219 flags |= AVI_KEYFRAME;
220 break;
222 else
223 if(stss->table[i].sample < keyframe_frame)
225 break;
230 // Allocation
231 if(idx1->table_size >= idx1->table_allocation)
233 quicktime_idx1table_t *old_table = idx1->table;
234 int new_allocation = idx1->table_allocation * 2;
235 if(new_allocation < 1) new_allocation = 1;
236 idx1->table = calloc(1, sizeof(quicktime_idx1table_t) * new_allocation);
237 if(old_table)
239 memcpy(idx1->table, old_table, sizeof(quicktime_idx1table_t) * idx1->table_size);
240 free(old_table);
242 idx1->table_allocation = new_allocation;
246 // Appendage
247 idx1_table = &idx1->table[idx1->table_size];
248 memcpy(idx1_table->tag, tag, 4);
249 idx1_table->flags = flags;
250 idx1_table->offset = offset - 8 - movi->atom.start;
251 idx1_table->size = size;
252 idx1->table_size++;