r609: Make it compile with xorg
[cinelerra_cv/mob.git] / quicktime / avi_riff.c
blobe891d6fd6fa381d738e88734645fe022f9a58c81
1 #include "funcprotos.h"
2 #include "quicktime.h"
5 void quicktime_read_riff(quicktime_t *file, quicktime_atom_t *parent_atom)
7 quicktime_riff_t *riff = quicktime_new_riff(file);
8 quicktime_atom_t leaf_atom;
9 int result = 0;
10 int i;
11 char data[5];
13 riff->atom = *parent_atom;
15 // AVI
16 quicktime_read_data(file, data, 4);
17 //printf("quicktime_read_riff 1 %llx\n", quicktime_position(file));
19 // Certain AVI parameters must be copied over to quicktime objects:
20 // hdrl -> moov
21 // movi -> mdat
22 // idx1 -> moov
25 result = quicktime_atom_read_header(file, &leaf_atom);
28 * printf("quicktime_read_riff 1 %llx %llx %c%c%c%c\n",
29 * leaf_atom.start,
30 * leaf_atom.size,
31 * leaf_atom.type[0],
32 * leaf_atom.type[1],
33 * leaf_atom.type[2],
34 * leaf_atom.type[3]);
36 if(!result)
38 if(quicktime_atom_is(&leaf_atom, "LIST"))
40 data[4] = 0;
43 result = !quicktime_read_data(file, data, 4);
46 if(!result)
48 // Got LIST 'hdrl'
49 if(quicktime_match_32(data, "hdrl"))
52 // No size here.
53 //printf("quicktime_read_riff 10 %llx\n", quicktime_position(file));
54 quicktime_read_hdrl(file, &riff->hdrl, &leaf_atom);
55 //printf("quicktime_read_riff 20 %llx\n", quicktime_position(file));
57 else
58 // Got LIST 'movi'
59 if(quicktime_match_32(data, "movi"))
61 //printf("quicktime_read_riff 30 %llx\n", quicktime_position(file));
62 quicktime_read_movi(file, &leaf_atom, &riff->movi);
63 //printf("quicktime_read_riff 40 %llx\n", quicktime_position(file));
67 // Skip it
68 quicktime_atom_skip(file, &leaf_atom);
71 else
72 // Got 'movi'
73 if(quicktime_atom_is(&leaf_atom, "movi"))
75 quicktime_read_movi(file, &leaf_atom, &riff->movi);
78 else
79 // Got 'idx1' original index
80 if(quicktime_atom_is(&leaf_atom, "idx1"))
83 //printf("quicktime_read_riff 50 %llx\n", quicktime_position(file));
84 // Preload idx1 here
85 int64_t start_position = quicktime_position(file);
86 long temp_size = leaf_atom.end - start_position;
87 unsigned char *temp = malloc(temp_size);
88 quicktime_set_preload(file,
89 (temp_size < 0x100000) ? 0x100000 : temp_size);
90 quicktime_read_data(file, temp, temp_size);
91 quicktime_set_position(file, start_position);
92 free(temp);
94 // Read idx1
95 quicktime_read_idx1(file, riff, &leaf_atom);
96 //printf("quicktime_read_riff 60 %llx\n", quicktime_position(file));
99 else
100 /* Skip it */
103 quicktime_atom_skip(file, &leaf_atom);
107 }while(!result && quicktime_position(file) < parent_atom->end);
109 //printf("quicktime_read_riff 10\n");
115 quicktime_riff_t* quicktime_new_riff(quicktime_t *file)
117 if(file->total_riffs >= MAX_RIFFS)
119 fprintf(stderr, "quicktime_new_riff file->total_riffs >= MAX_RIFFS\n");
120 return 0;
122 else
124 quicktime_riff_t *riff = calloc(1, sizeof(quicktime_riff_t));
125 file->riff[file->total_riffs++] = riff;
126 return riff;
132 void quicktime_delete_riff(quicktime_t *file, quicktime_riff_t *riff)
134 int i = 0;
135 quicktime_delete_hdrl(file, &riff->hdrl);
136 quicktime_delete_movi(file, &riff->movi);
137 quicktime_delete_idx1(&riff->idx1);
138 free(riff);
141 void quicktime_init_riff(quicktime_t *file)
144 // Create new RIFF
145 quicktime_riff_t *riff = quicktime_new_riff(file);
147 // Write riff header
148 // RIFF 'AVI '
149 quicktime_atom_write_header(file, &riff->atom, "RIFF");
150 quicktime_write_char32(file, "AVI ");
152 // Write header list in first RIFF only
153 if(file->total_riffs < 2)
155 quicktime_init_hdrl(file, &riff->hdrl);
156 riff->have_hdrl = 1;
159 quicktime_init_movi(file, riff);
162 void quicktime_finalize_riff(quicktime_t *file, quicktime_riff_t *riff)
164 // Write partial indexes
165 quicktime_finalize_movi(file, &riff->movi);
166 if(riff->have_hdrl)
168 //printf("quicktime_finalize_riff 1\n");
169 quicktime_finalize_hdrl(file, &riff->hdrl);
170 //printf("quicktime_finalize_riff 10\n");
171 // Write original index for first RIFF
172 quicktime_write_idx1(file, &riff->idx1);
173 //printf("quicktime_finalize_riff 100\n");
175 quicktime_atom_write_footer(file, &riff->atom);
181 void quicktime_import_avi(quicktime_t *file)
183 int i, j, k;
184 quicktime_riff_t *first_riff = file->riff[0];
185 quicktime_idx1_t *idx1 = &first_riff->idx1;
186 quicktime_hdrl_t *hdrl = &first_riff->hdrl;
190 /* Determine whether to use idx1 or indx indexes for offsets. */
191 /* idx1 must always be used for keyframes but it also must be */
192 /* ignored for offsets if indx exists. */
195 //printf("quicktime_import_avi 1\n");
196 /* Convert idx1 to keyframes and load offsets and sizes */
198 // This is a check from mplayer that gives us the right strategy
199 // for calculating real offset.
200 // is_odml is not currently set anywhere, but when we will support
201 // odml, this will be ready...
203 int index_format = 0;
205 if(idx1->table_size > 1)
207 if((idx1->table[0].offset < first_riff->movi.atom.start + 4 ||
208 idx1->table[1].offset < first_riff->movi.atom.start + 4) &&
209 !file->is_odml)
210 index_format = 1;
211 else
212 index_format = 0;
215 for(i = 0; i < idx1->table_size; i++)
217 quicktime_idx1table_t *idx1table = idx1->table + i;
218 char *tag = idx1table->tag;
219 int track_number = (tag[0] - '0') * 10 + (tag[1] - '0');
220 if(track_number < file->moov.total_tracks)
222 quicktime_trak_t *trak = file->moov.trak[track_number];
223 int is_audio = trak->mdia.minf.is_audio;
224 int is_video = trak->mdia.minf.is_video;
227 /* Chunk offset */
228 quicktime_stco_t *stco = &trak->mdia.minf.stbl.stco;
229 /* Sample size */
230 quicktime_stsz_t *stsz = &trak->mdia.minf.stbl.stsz;
231 /* Samples per chunk */
232 quicktime_stsc_t *stsc = &trak->mdia.minf.stbl.stsc;
233 /* Sample description */
234 quicktime_stsd_t *stsd = &trak->mdia.minf.stbl.stsd;
239 /* Enter the offset and size no matter what so the sample counts */
240 /* can be used to set keyframes */
241 if (index_format == 1)
242 quicktime_update_stco(stco,
243 stco->total_entries + 1,
244 idx1table->offset + first_riff->movi.atom.start);
245 else
246 quicktime_update_stco(stco,
247 stco->total_entries + 1,
248 idx1table->offset);
250 if(is_video)
252 /* Just get the keyframe flag. don't call quicktime_insert_keyframe because */
253 /* that updates idx1 and we don't have a track map. */
254 int is_keyframe = (idx1table->flags & AVI_KEYFRAME) == AVI_KEYFRAME;
255 if(is_keyframe)
257 quicktime_stss_t *stss = &trak->mdia.minf.stbl.stss;
258 /* This is done before the image size table so this value is right */
259 int frame = stsz->total_entries;
261 /* Expand table */
262 if(stss->entries_allocated <= stss->total_entries)
264 stss->entries_allocated *= 2;
265 stss->table = realloc(stss->table,
266 sizeof(quicktime_stss_table_t) * stss->entries_allocated);
268 stss->table[stss->total_entries++].sample = frame;
271 /* Set image size */
272 quicktime_update_stsz(stsz,
273 stsz->total_entries,
274 idx1table->size);
276 else
277 if(is_audio)
279 /* Set samples per chunk if PCM */
280 if(stsd->table[0].sample_size > 0)
282 quicktime_update_stsc(stsc,
283 stsc->total_entries + 1,
284 idx1table->size *
285 8 /
286 stsd->table[0].sample_size /
287 stsd->table[0].channels);
293 //printf("quicktime_import_avi 10\n");
296 /* Convert super indexes into Quicktime indexes. */
297 /* Append to existing entries if idx1 exists. */
298 /* No keyframes here. */
299 for(i = 0; i < file->moov.total_tracks; i++)
301 quicktime_strl_t *strl = first_riff->hdrl.strl[i];
303 if(strl->have_indx)
305 quicktime_indx_t *indx = &strl->indx;
306 quicktime_trak_t *trak = file->moov.trak[i];
307 quicktime_stco_t *stco = &trak->mdia.minf.stbl.stco;
308 quicktime_stsz_t *stsz = &trak->mdia.minf.stbl.stsz;
309 quicktime_stsc_t *stsc = &trak->mdia.minf.stbl.stsc;
310 quicktime_stsd_t *stsd = &trak->mdia.minf.stbl.stsd;
311 /* Get existing chunk count from the idx1 */
312 int existing_chunks = stco->total_entries;
314 /* Read each indx entry */
315 for(j = 0; j < indx->table_size; j++)
317 quicktime_indxtable_t *indx_table = &indx->table[j];
318 quicktime_ix_t *ix = indx_table->ix;
320 for(k = 0; k < ix->table_size; k++)
322 /* Copy from existing chunk count to end of ix table */
323 if(existing_chunks <= 0)
325 quicktime_ixtable_t *ixtable = &ix->table[k];
327 /* Do the same things that idx1 did to the chunk tables */
328 /* Subtract the super indexes by size of the header. McRoweSoft seems to */
329 /* want the header before the ix offset but after the idx1 offset. */
330 quicktime_update_stco(stco,
331 stco->total_entries + 1,
332 ixtable->relative_offset + ix->base_offset - 8);
333 if(strl->is_video)
335 quicktime_update_stsz(stsz,
336 stsz->total_entries,
337 ixtable->size);
339 else
340 if(strl->is_audio)
342 if(stsd->table[0].sample_size > 0)
344 quicktime_update_stsc(stsc,
345 stsc->total_entries + 1,
346 ixtable->size *
347 8 /
348 stsd->table[0].sample_size /
349 stsd->table[0].channels);
353 else
354 existing_chunks--;
361 //printf("quicktime_import_avi 20\n");
366 /* Set total samples, time to sample, for audio */
367 for(i = 0; i < file->moov.total_tracks; i++)
369 quicktime_trak_t *trak = file->moov.trak[i];
370 quicktime_stsz_t *stsz = &trak->mdia.minf.stbl.stsz;
371 quicktime_stsc_t *stsc = &trak->mdia.minf.stbl.stsc;
372 quicktime_stco_t *stco = &trak->mdia.minf.stbl.stco;
373 quicktime_stts_t *stts = &trak->mdia.minf.stbl.stts;
375 if(trak->mdia.minf.is_audio)
377 quicktime_stsc_table_t *stsc_table = stsc->table;
378 long total_entries = stsc->total_entries;
379 long chunk = stco->total_entries;
380 long sample = 0;
382 if(chunk > 0)
384 sample = quicktime_sample_of_chunk(trak, chunk) +
385 stsc_table[total_entries - 1].samples;
388 stsz->sample_size = 1;
389 stsz->total_entries = sample;
390 stts->table[0].sample_count = sample;
392 else
393 if(trak->mdia.minf.is_video)
395 stsc->total_entries = 1;
396 /* stts has 1 allocation by default */
397 stts->table[0].sample_count = stco->total_entries;
401 //printf("quicktime_import_avi 30\n");