r1013: Select the entire clip title when a clip is edited or created.
[cinelerra_cv/ct.git] / quicktime / avi_ix.c
blobb43535d8c00fe07c43b32563e99ace5f4bf0c0f7
1 #include "funcprotos.h"
2 #include "quicktime.h"
3 #include <string.h>
6 static char* make_tag(int number, char *tag)
8 tag[0] = 'i';
9 tag[1] = 'x';
10 tag[2] = '0' + (number / 10);
11 tag[3] = '0' + (number % 10);
12 return tag;
15 quicktime_ix_t* quicktime_new_ix(quicktime_t *file,
16 quicktime_trak_t *trak,
17 quicktime_strl_t *strl)
19 quicktime_ix_t *ix = calloc(1, sizeof(quicktime_ix_t));
20 ix->base_offset = quicktime_position(file);
21 make_tag(trak->tkhd.track_id - 1, ix->tag);
22 ix->longs_per_entry = 2;
23 ix->index_type = AVI_INDEX_OF_CHUNKS;
24 memcpy(ix->chunk_id, strl->tag, 4);
25 return ix;
29 void quicktime_delete_ix(quicktime_ix_t *ix)
31 if(ix->table) free(ix->table);
32 free(ix);
35 void quicktime_update_ixtable(quicktime_t *file,
36 quicktime_trak_t *trak,
37 int64_t offset,
38 int size)
40 quicktime_riff_t *riff = file->riff[file->total_riffs - 1];
41 quicktime_movi_t *movi = &riff->movi;
42 quicktime_ix_t *ix = movi->ix[trak->tkhd.track_id - 1];
43 quicktime_ixtable_t *ix_table;
45 /* Allocation */
46 if(ix->table_size >= ix->table_allocation)
48 quicktime_ixtable_t *old_table = ix->table;
49 int new_allocation = ix->table_allocation * 2;
50 if(new_allocation < 1) new_allocation = 1;
51 ix->table = calloc(1, sizeof(quicktime_ixtable_t) * new_allocation);
52 if(old_table)
54 memcpy(ix->table, old_table, sizeof(quicktime_ixtable_t) * ix->table_size);
55 free(old_table);
57 ix->table_allocation = new_allocation;
60 /* Appendage */
61 ix_table = &ix->table[ix->table_size++];
62 ix_table->relative_offset = offset - ix->base_offset;
63 ix_table->size = size;
67 void quicktime_write_ix(quicktime_t *file,
68 quicktime_ix_t *ix,
69 int track)
71 int i;
72 quicktime_atom_write_header(file, &ix->atom, ix->tag);
74 /* longs per entry */
75 quicktime_write_int16_le(file, ix->longs_per_entry);
76 /* index sub type */
77 quicktime_write_char(file, 0);
78 /* index type */
79 quicktime_write_char(file, ix->index_type);
80 /* entries in use */
81 quicktime_write_int32_le(file, ix->table_size);
82 /* chunk ID */
83 quicktime_write_char32(file, ix->chunk_id);
84 /* base offset */
85 quicktime_write_int64_le(file, ix->base_offset);
86 /* reserved */
87 quicktime_write_int32_le(file, 0);
89 /* table */
90 for(i = 0; i < ix->table_size; i++)
92 quicktime_ixtable_t *table = &ix->table[i];
93 quicktime_write_int32_le(file, table->relative_offset);
94 quicktime_write_int32_le(file, table->size);
97 quicktime_atom_write_footer(file, &ix->atom);
100 /* Update super index */
101 quicktime_riff_t *riff = file->riff[0];
102 quicktime_hdrl_t *hdrl = &riff->hdrl;
103 quicktime_strl_t *strl = hdrl->strl[track];
104 quicktime_indx_t *indx = &strl->indx;
106 quicktime_update_indx(file, indx, ix);
109 void quicktime_read_ix(quicktime_t *file,
110 quicktime_ix_t *ix)
112 int i;
113 quicktime_atom_t leaf_atom;
114 quicktime_atom_read_header(file, &leaf_atom);
116 ix->longs_per_entry = quicktime_read_int16_le(file);
117 /* sub type */
118 quicktime_read_char(file);
119 ix->index_type = quicktime_read_char(file);
120 ix->table_size = quicktime_read_int32_le(file);
121 quicktime_read_char32(file, ix->chunk_id);
122 ix->base_offset = quicktime_read_int64_le(file);
123 /* reserved */
124 quicktime_read_int32_le(file);
126 ix->table = calloc(ix->table_size, sizeof(quicktime_ixtable_t));
128 for(i = 0; i < ix->table_size; i++)
130 quicktime_ixtable_t *ixtable = &ix->table[i];
131 ixtable->relative_offset = quicktime_read_int32_le(file);
132 ixtable->size = quicktime_read_int32_le(file);