quicktime bites the dust, so far only with --with-external-ffmpeg, remaining build...
[cinelerra_cv/ct.git] / quicktime / avi_idx1.c
blob1bf6d7f867d0da88c0398bedbab77ba02ca22de5
1 #include "funcprotos.h"
2 #include "quicktime.h"
3 #include <string.h>
7 typedef struct
9 char tag[4];
10 int32_t flags;
11 int32_t offset;
12 int32_t size;
13 } avi_tag_t;
17 static int is_keyframe(quicktime_trak_t *trak, int frame)
19 int i;
20 quicktime_stss_t *stss = &trak->mdia.minf.stbl.stss;
21 frame++;
22 for(i = 0; i < stss->total_entries; i++)
24 if(stss->table[i].sample == frame) return 1;
26 return 0;
30 void quicktime_delete_idx1(quicktime_idx1_t *idx1)
32 if(idx1->table) free(idx1->table);
35 void quicktime_read_idx1(quicktime_t *file,
36 quicktime_riff_t *riff,
37 quicktime_atom_t *parent_atom)
39 int i;
40 quicktime_riff_t *first_riff = file->riff[0];
41 quicktime_hdrl_t *hdrl = &first_riff->hdrl;
42 quicktime_idx1_t *idx1 = &riff->idx1;
44 //printf("quicktime_read_idx1 1 %llx\n", quicktime_position(file));
46 // Allocate table.
47 idx1->table_size = (parent_atom->end - quicktime_position(file)) / 16;
48 idx1->table_allocation = idx1->table_size;
49 idx1->table = calloc(sizeof(quicktime_idx1table_t), idx1->table_size);
50 //printf("quicktime_read_idx1 10\n");
52 // Store it in idx1 table now.
53 // Wait for full ix table discovery before converting to stco.
54 for(i = 0; i < idx1->table_size; i++)
56 quicktime_idx1table_t *idx1table = idx1->table + i;
58 quicktime_read_data(file, idx1table->tag, 4);
59 idx1table->flags = quicktime_read_int32_le(file);
60 idx1table->offset = quicktime_read_int32_le(file);
61 idx1table->size = quicktime_read_int32_le(file);
64 //printf("quicktime_read_idx1 100\n");
74 void quicktime_write_idx1(quicktime_t *file,
75 quicktime_idx1_t *idx1)
77 int i;
78 quicktime_idx1table_t *table = idx1->table;
79 int table_size = idx1->table_size;
83 // Write table
84 quicktime_atom_write_header(file, &idx1->atom, "idx1");
86 for(i = 0; i < table_size; i++)
88 quicktime_idx1table_t *entry = &table[i];
89 quicktime_write_char32(file, entry->tag);
90 quicktime_write_int32_le(file, entry->flags);
91 quicktime_write_int32_le(file, entry->offset);
92 quicktime_write_int32_le(file, entry->size);
96 quicktime_atom_write_footer(file, &idx1->atom);
99 void quicktime_set_idx1_keyframe(quicktime_t *file,
100 quicktime_trak_t *trak,
101 int new_keyframe)
103 quicktime_riff_t *riff = file->riff[0];
104 quicktime_hdrl_t *hdrl = &riff->hdrl;
105 quicktime_strl_t *strl = hdrl->strl[trak->tkhd.track_id - 1];
106 char *tag = strl->tag;
107 quicktime_idx1_t *idx1 = &riff->idx1;
108 int i;
109 int counter = -1;
111 // Search through entire index for right numbered tag.
112 // Since all the tracks are combined in the same index, this is unavoidable.
113 for(i = 0; i < idx1->table_size; i++)
115 quicktime_idx1table_t *idx1_table = &idx1->table[i];
116 if(!memcmp(idx1_table->tag, tag, 4))
118 counter++;
119 if(counter == new_keyframe)
121 idx1_table->flags |= AVI_KEYFRAME;
122 break;
128 void quicktime_update_idx1table(quicktime_t *file,
129 quicktime_trak_t *trak,
130 int offset,
131 int size)
133 quicktime_riff_t *riff = file->riff[0];
134 quicktime_hdrl_t *hdrl = &riff->hdrl;
135 quicktime_strl_t *strl = hdrl->strl[trak->tkhd.track_id - 1];
136 char *tag = strl->tag;
137 quicktime_idx1_t *idx1 = &riff->idx1;
138 quicktime_movi_t *movi = &riff->movi;
139 quicktime_idx1table_t *idx1_table;
140 quicktime_stss_t *stss = &trak->mdia.minf.stbl.stss;
141 uint32_t flags = 0;
142 int i;
143 int keyframe_frame = idx1->table_size + 1;
145 // Set flag for keyframe
146 for(i = stss->total_entries - 1; i >= 0; i--)
148 if(stss->table[i].sample == keyframe_frame)
150 flags |= AVI_KEYFRAME;
151 break;
153 else
154 if(stss->table[i].sample < keyframe_frame)
156 break;
161 // Allocation
162 if(idx1->table_size >= idx1->table_allocation)
164 quicktime_idx1table_t *old_table = idx1->table;
165 int new_allocation = idx1->table_allocation * 2;
166 if(new_allocation < 1) new_allocation = 1;
167 idx1->table = calloc(1, sizeof(quicktime_idx1table_t) * new_allocation);
168 if(old_table)
170 memcpy(idx1->table, old_table, sizeof(quicktime_idx1table_t) * idx1->table_size);
171 free(old_table);
173 idx1->table_allocation = new_allocation;
177 // Appendage
178 idx1_table = &idx1->table[idx1->table_size];
179 memcpy(idx1_table->tag, tag, 4);
180 idx1_table->flags = flags;
181 idx1_table->offset = offset - 8 - movi->atom.start;
182 idx1_table->size = size;
183 idx1->table_size++;