repair flickering video encode, bug created last checkin
[goodguy/history.git] / cinelerra-5.0 / quicktime / avi_indx.c
1 #include "funcprotos.h"
2 #include "quicktime.h"
3 #include <string.h>
4
5
6 void quicktime_delete_indx(quicktime_indx_t *indx)
7 {
8         int i;
9         if(indx->table)
10         {
11                 for(i = 0; i < indx->table_size; i++)
12                 {
13                         quicktime_indxtable_t *indx_table = &indx->table[i];
14                         if(indx_table->ix) quicktime_delete_ix(indx_table->ix);
15                 }
16                 free(indx->table);
17         }
18 }
19
20 void quicktime_init_indx(quicktime_t *file,
21         quicktime_indx_t *indx,
22         quicktime_strl_t *strl)
23 {
24         indx->longs_per_entry = 4;
25         indx->index_subtype = 0;
26         indx->index_type = AVI_INDEX_OF_INDEXES;
27         memcpy(indx->chunk_id, strl->tag, 4);
28 }
29
30 void quicktime_update_indx(quicktime_t *file,
31         quicktime_indx_t *indx,
32         quicktime_ix_t *ix)
33 {
34         quicktime_indxtable_t *indx_table;
35
36 /* Allocate */
37         if(indx->table_size >= indx->table_allocation)
38         {
39                 quicktime_indxtable_t *old_table = indx->table;
40                 int new_allocation = indx->table_allocation * 2;
41                 if(new_allocation < 1) new_allocation = 1;
42                 indx->table = calloc(1, sizeof(quicktime_indxtable_t) * new_allocation);
43                 if(old_table)
44                 {
45                         memcpy(indx->table, old_table, sizeof(quicktime_indxtable_t) * indx->table_size);
46                         free(old_table);
47                 }
48                 indx->table_allocation = new_allocation;
49         }
50
51 /* Append */
52         indx_table = &indx->table[indx->table_size++];
53         indx_table->index_offset = ix->atom.start - 8;
54         indx_table->index_size = ix->atom.size;
55         indx_table->duration = ix->table_size;
56 }
57
58
59
60 void quicktime_finalize_indx(quicktime_t *file)
61 {
62         int i, j;
63         quicktime_riff_t *riff = file->riff[0];
64         quicktime_hdrl_t *hdrl = &riff->hdrl;
65         quicktime_strl_t *strl;
66         quicktime_indx_t *indx;
67         quicktime_atom_t junk_atom;
68         int junk_size;
69
70
71         for(i = 0; i < file->moov.total_tracks; i++)
72         {
73                 strl = hdrl->strl[i];
74                 indx = &strl->indx;
75
76 /* Write indx */
77                 quicktime_set_position(file, strl->indx_offset);
78                 quicktime_atom_write_header(file, &indx->atom, "indx");
79 /* longs per entry */
80                 quicktime_write_int16_le(file, indx->longs_per_entry);
81 /* index sub type */
82                 quicktime_write_char(file, indx->index_subtype);
83 /* index type */
84                 quicktime_write_char(file, indx->index_type);
85 /* entries in use */
86                 quicktime_write_int32_le(file, indx->table_size);
87 /* chunk ID */
88                 quicktime_write_char32(file, indx->chunk_id);
89 /* reserved */
90                 quicktime_write_int32_le(file, 0);
91                 quicktime_write_int32_le(file, 0);
92                 quicktime_write_int32_le(file, 0);
93
94 /* table */
95                 for(j = 0; j < indx->table_size; j++)
96                 {
97                         quicktime_indxtable_t *indx_table = &indx->table[j];
98                         quicktime_write_int64_le(file, indx_table->index_offset);
99                         quicktime_write_int32_le(file, indx_table->index_size);
100                         quicktime_write_int32_le(file, indx_table->duration);
101                 }
102
103                 quicktime_atom_write_footer(file, &indx->atom);
104
105
106
107 /* Rewrite JUNK less indx size and indx header size */
108                 junk_size = strl->padding_size - indx->atom.size - 8;
109                 quicktime_atom_write_header(file, &junk_atom, "JUNK");
110                 for(j = 0; j < junk_size; j += 4)
111                         quicktime_write_int32_le(file, 0);
112                 quicktime_atom_write_footer(file, &junk_atom);
113
114         }
115 }
116
117
118
119
120
121
122
123
124
125 void quicktime_read_indx(quicktime_t *file,
126         quicktime_strl_t *strl,
127         quicktime_atom_t *parent_atom)
128 {
129         quicktime_indx_t *indx = &strl->indx;
130         quicktime_indxtable_t *indx_table;
131         quicktime_ix_t *ix;
132         int i;
133         int64_t offset;
134
135         indx->longs_per_entry = quicktime_read_int16_le(file);
136         indx->index_subtype = quicktime_read_char(file);
137         indx->index_type = quicktime_read_char(file);
138         indx->table_size = quicktime_read_int32_le(file);
139         quicktime_read_char32(file, indx->chunk_id);
140         quicktime_read_int32_le(file);
141         quicktime_read_int32_le(file);
142         quicktime_read_int32_le(file);
143
144
145 //printf("quicktime_read_indx 1\n");
146 /* Read indx entries */
147         indx->table = calloc(indx->table_size, sizeof(quicktime_indxtable_t));
148         for(i = 0; i < indx->table_size; i++)
149         {
150                 indx_table = &indx->table[i];
151                 indx_table->index_offset = quicktime_read_int64_le(file);
152                 indx_table->index_size = quicktime_read_int32_le(file);
153                 indx_table->duration = quicktime_read_int32_le(file);
154                 offset = quicktime_position(file);
155
156                 indx_table->ix = calloc(indx->table_size, sizeof(quicktime_ix_t*));
157
158 /* Now read the partial index */
159                 ix = indx_table->ix = calloc(1, sizeof(quicktime_ix_t));
160                 quicktime_set_position(file, indx_table->index_offset);
161                 quicktime_read_ix(file, ix);
162                 quicktime_set_position(file, offset);
163         }
164 //printf("quicktime_read_indx 100\n");
165
166 }
167
168
169
170