r999: maintainers added to README_en.
[cinelerra_cv/mob.git] / quicktime / avi_strl.c
blob0b9854f2e2bb94d56727fbf15329d12529855612
1 #include "funcprotos.h"
2 #include "quicktime.h"
3 #include <string.h>
6 // Update during close:
7 // length
8 // samples per chunk
9 #define JUNK_SIZE 0x1018
13 quicktime_strl_t* quicktime_new_strl()
15 quicktime_strl_t *strl = calloc(1, sizeof(quicktime_strl_t));
16 return strl;
20 void quicktime_init_strl(quicktime_t *file,
21 quicktime_audio_map_t *atrack,
22 quicktime_video_map_t *vtrack,
23 quicktime_trak_t *trak,
24 quicktime_strl_t *strl)
26 quicktime_atom_t list_atom, strh_atom, strf_atom;
27 quicktime_atom_t junk_atom;
28 int i;
30 /* Construct tag */
31 if(vtrack)
33 strl->tag[0] = '0' + (trak->tkhd.track_id - 1) / 10;
34 strl->tag[1] = '0' + (trak->tkhd.track_id - 1) % 10;
35 strl->tag[2] = 'd';
36 strl->tag[3] = 'c';
38 else
39 if(atrack)
41 strl->tag[0] = '0' + (trak->tkhd.track_id - 1) / 10;
42 strl->tag[1] = '0' + (trak->tkhd.track_id - 1) % 10;
43 strl->tag[2] = 'w';
44 strl->tag[3] = 'b';
48 /* LIST 'strl' */
49 quicktime_atom_write_header(file, &list_atom, "LIST");
50 quicktime_write_char32(file, "strl");
52 /* 'strh' */
53 quicktime_atom_write_header(file, &strh_atom, "strh");
57 /* vids */
58 if(vtrack)
60 quicktime_write_char32(file, "vids");
61 quicktime_write_char32(file,
62 trak->mdia.minf.stbl.stsd.table[0].format);
63 /* flags */
64 quicktime_write_int32_le(file, 0);
65 /* priority */
66 quicktime_write_int16_le(file, 0);
67 /* language */
68 quicktime_write_int16_le(file, 0);
69 /* initial frame */
70 quicktime_write_int32_le(file, 0);
72 /* framerate denominator */
73 quicktime_write_int32_le(file,
74 trak->mdia.minf.stbl.stts.table[0].sample_duration);
75 /* framerate numerator */
76 quicktime_write_int32_le(file,
77 trak->mdia.mdhd.time_scale);
79 /* start */
80 quicktime_write_int32_le(file, 0);
81 strl->length_offset = quicktime_position(file);
82 /* length: fill later */
83 quicktime_write_int32_le(file, 0);
84 /* suggested buffer size */
85 quicktime_write_int32_le(file, 0);
86 /* quality */
87 quicktime_write_int32_le(file, -1);
88 /* sample size */
89 quicktime_write_int32_le(file, 0);
90 quicktime_write_int16_le(file, 0);
91 quicktime_write_int16_le(file, 0);
92 quicktime_write_int16_le(file, trak->tkhd.track_width);
93 quicktime_write_int16_le(file, trak->tkhd.track_height);
95 else
96 /* auds */
97 if(atrack)
99 quicktime_write_char32(file, "auds");
100 quicktime_write_int32_le(file, 0);
101 /* flags */
102 quicktime_write_int32_le(file, 0);
103 /* priority */
104 quicktime_write_int16_le(file, 0);
105 /* language */
106 quicktime_write_int16_le(file, 0);
107 /* initial frame */
108 quicktime_write_int32_le(file, 0);
109 strl->samples_per_chunk_offset = quicktime_position(file);
110 /* samples per chunk */
111 quicktime_write_int32_le(file, 0);
112 /* sample rate * samples per chunk if uncompressed */
113 /* sample rate if compressed */
114 quicktime_write_int32_le(file, 0);
115 /* start */
116 quicktime_write_int32_le(file, 0);
117 strl->length_offset = quicktime_position(file);
118 /* length, XXX: filled later */
119 quicktime_write_int32_le(file, 0);
120 /* suggested buffer size */
121 quicktime_write_int32_le(file, 0);
122 /* quality */
123 quicktime_write_int32_le(file, -1);
124 /* sample size: 0 for compressed and number of bytes for uncompressed */
125 strl->sample_size_offset = quicktime_position(file);
126 quicktime_write_int32_le(file, 0);
127 quicktime_write_int32_le(file, 0);
128 quicktime_write_int32_le(file, 0);
130 quicktime_atom_write_footer(file, &strh_atom);
138 /* strf */
139 quicktime_atom_write_header(file, &strf_atom, "strf");
141 if(vtrack)
143 /* atom size repeated */
144 quicktime_write_int32_le(file, 40);
145 quicktime_write_int32_le(file, trak->tkhd.track_width);
146 quicktime_write_int32_le(file, trak->tkhd.track_height);
147 /* planes */
148 quicktime_write_int16_le(file, 1);
149 /* depth */
150 quicktime_write_int16_le(file, 24);
151 quicktime_write_char32(file,
152 trak->mdia.minf.stbl.stsd.table[0].format);
153 quicktime_write_int32_le(file,
154 trak->tkhd.track_width * trak->tkhd.track_height * 3);
155 quicktime_write_int32_le(file, 0);
156 quicktime_write_int32_le(file, 0);
157 quicktime_write_int32_le(file, 0);
158 quicktime_write_int32_le(file, 0);
160 else
161 if(atrack)
163 /* By now the codec is instantiated so the WAV ID is available. */
164 quicktime_codec_t *codec_base = atrack->codec;
165 int wav_id = codec_base->wav_id;
167 quicktime_write_int16_le(file,
168 wav_id);
169 quicktime_write_int16_le(file,
170 trak->mdia.minf.stbl.stsd.table[0].channels);
171 /* nSamplesPerSec as per MSDN */
172 quicktime_write_int32_le(file,
173 trak->mdia.minf.stbl.stsd.table[0].sample_rate);
174 if (wav_id == 0x01) // PCM
176 /* nAvgBytesPerSec as per MSDN*/
177 quicktime_write_int32_le(file, trak->mdia.minf.stbl.stsd.table[0].sample_rate * trak->mdia.minf.stbl.stsd.table[0].sample_size * trak->mdia.minf.stbl.stsd.table[0].channels / 8);
178 /* nBlockAlign as per MSDN, very important value */
179 /* nBlockAlign have to be the same is used in many players instead of dwSampleSize - so they should be the same */
180 /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcemultimedia5/html/wce50conAVIStreamHeaders.asp */
181 /* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcemultimedia5/html/wce50lrfwaveformatex91.asp */
182 quicktime_write_int16_le(file, trak->mdia.minf.stbl.stsd.table[0].sample_size * trak->mdia.minf.stbl.stsd.table[0].channels / 8);
184 else
186 /* FIXME: These two are complete rubbish, according to my knowledge, they depend on the codec */
187 quicktime_write_int32_le(file, 256000 / 8); // nAvgBytesPerSec
188 quicktime_write_int16_le(file, 1); // nBlockAling
191 /* bits per sample */
192 quicktime_write_int16_le(file,
193 trak->mdia.minf.stbl.stsd.table[0].sample_size);
194 quicktime_write_int16_le(file, 0);
197 quicktime_atom_write_footer(file, &strf_atom);
202 /* Junk is required in Windows. */
203 /* In Heroine Kernel it's padding for the super index */
204 strl->indx_offset = quicktime_position(file);
205 strl->padding_size = JUNK_SIZE;
209 quicktime_atom_write_header(file, &junk_atom, "JUNK");
210 for(i = 0; i < strl->padding_size; i += 4)
211 quicktime_write_int32_le(file, 0);
212 quicktime_atom_write_footer(file, &junk_atom);
215 /* Initialize super index */
216 quicktime_init_indx(file, &strl->indx, strl);
219 quicktime_atom_write_footer(file, &list_atom);
224 void quicktime_delete_strl(quicktime_strl_t *strl)
226 quicktime_delete_indx(&strl->indx);
227 free(strl);
230 void quicktime_read_strl(quicktime_t *file,
231 quicktime_strl_t *strl,
232 quicktime_atom_t *parent_atom)
234 // These are 0 if no track is currently being processed.
235 // Set to 1 if audio or video track is being processed.
236 char data[4], codec[4];
237 int denominator;
238 int numerator;
239 double frame_rate;
240 int width;
241 int height;
242 int depth;
243 int frames;
244 int bytes_per_sample;
245 int sample_size;
246 int samples;
247 int samples_per_chunk;
248 int channels;
249 int sample_rate;
250 int compression_id;
251 int bytes_per_second;
252 quicktime_trak_t *trak = 0;
253 quicktime_riff_t *first_riff = file->riff[0];
256 codec[0] = codec[1] = codec[2] = codec[3] = 0;
258 /* AVI translation: */
259 /* vids -> trak */
260 /* auds -> trak */
261 /* Only one track is in each strl object */
264 quicktime_atom_t leaf_atom;
265 quicktime_atom_read_header(file, &leaf_atom);
267 // strh
268 if(quicktime_atom_is(&leaf_atom, "strh"))
270 // stream type
271 quicktime_read_data(file, data, 4);
273 if(quicktime_match_32(data, "vids"))
275 trak = quicktime_add_trak(file);
276 width = 0;
277 height = 0;
278 depth = 24;
279 frames = 0;
280 strl->is_video = 1;
283 trak->tkhd.track_id = file->moov.mvhd.next_track_id;
284 file->moov.mvhd.next_track_id++;
287 /* Codec */
288 quicktime_read_data(file,
289 codec,
291 //printf("quicktime_read_strl 1 %c%c%c%c\n", codec[0], codec[1], codec[2], codec[3]);
292 /* Blank */
293 quicktime_set_position(file, quicktime_position(file) + 12);
294 denominator = quicktime_read_int32_le(file);
295 numerator = quicktime_read_int32_le(file);
296 if(denominator != 0)
297 frame_rate = (double)numerator / denominator;
298 else
299 frame_rate = numerator;
301 /* Blank */
302 quicktime_set_position(file, quicktime_position(file) + 4);
303 frames = quicktime_read_int32_le(file);
305 else
306 if(quicktime_match_32(data, "auds"))
308 trak = quicktime_add_trak(file);
309 sample_size = 16;
310 channels = 2;
311 sample_rate = 0;
312 compression_id = 0;
313 strl->is_audio = 1;
315 trak->tkhd.track_id = file->moov.mvhd.next_track_id;
316 file->moov.mvhd.next_track_id++;
317 quicktime_read_data(file,
318 codec,
320 //printf("quicktime_read_strl 2 %c%c%c%c\n", codec[0], codec[1], codec[2], codec[3]);
321 quicktime_set_position(file, quicktime_position(file) + 12);
322 samples_per_chunk = quicktime_read_int32_le(file);
323 bytes_per_second = quicktime_read_int32_le(file);
324 /* start */
325 quicktime_set_position(file, quicktime_position(file) + 4);
326 /* length of track */
327 samples = quicktime_read_int32_le(file);
328 /* suggested buffer size, quality */
329 quicktime_set_position(file, quicktime_position(file) + 8);
331 // If this is 0 use constant samples_per_chunk to guess locations.
332 // If it isn't 0 synthesize samples per chunk table to get locations.
333 // McRowesoft doesn't really obey this rule so we may need to base it on codec ID.
334 bytes_per_sample = quicktime_read_int32_le(file);
335 //printf("quicktime_read_strl 20 %d\n", samples_per_chunk);
338 // strf
339 else
340 if(quicktime_atom_is(&leaf_atom, "strf"))
342 if(strl->is_video)
344 /* atom size repeated */
345 quicktime_read_int32_le(file);
346 width = quicktime_read_int32_le(file);
347 height = quicktime_read_int32_le(file);
348 /* Panes */
349 quicktime_read_int16_le(file);
350 /* Depth in bits */
351 depth = quicktime_read_int16_le(file);
352 quicktime_read_data(file,
353 codec,
356 else
357 if(strl->is_audio)
359 compression_id = quicktime_read_int16_le(file);
360 channels = quicktime_read_int16_le(file);
361 sample_rate = quicktime_read_int32_le(file);
362 quicktime_set_position(file, quicktime_position(file) + 6);
363 sample_size = quicktime_read_int16_le(file);
364 //printf("quicktime_read_strl 40 %d %d %d\n", channels, sample_rate, sample_size);
367 else
368 // Super index.
369 // Read the super index + all the partial indexes now
370 if(quicktime_atom_is(&leaf_atom, "indx"))
372 //printf("quicktime_read_strl 50\n");
373 quicktime_read_indx(file, strl, &leaf_atom);
374 strl->have_indx = 1;
377 //printf("quicktime_read_strl 60\n");
380 // Next object
381 quicktime_atom_skip(file, &leaf_atom);
382 }while(quicktime_position(file) < parent_atom->end);
383 //printf("quicktime_read_strl 70 %d %d\n", strl->is_audio, strl->is_video);
386 if(strl->is_video)
388 /* Generate quicktime structures */
389 quicktime_trak_init_video(file,
390 trak,
391 width,
392 height,
393 frame_rate,
394 codec);
395 quicktime_mhvd_init_video(file,
396 &file->moov.mvhd,
397 frame_rate);
398 trak->mdia.mdhd.duration = frames;
399 // trak->mdia.mdhd.time_scale = 1;
400 memcpy(trak->mdia.minf.stbl.stsd.table[0].format, codec, 4);
401 trak->mdia.minf.stbl.stsd.table[0].depth = depth;
403 else
404 if(strl->is_audio)
406 /* Generate quicktime structures */
407 //printf("quicktime_read_strl 70 %d\n", sample_size);
408 quicktime_trak_init_audio(file,
409 trak,
410 channels,
411 sample_rate,
412 sample_size,
413 codec);
416 // We store a constant samples per chunk based on the
417 // packet size if sample_size zero
418 // and calculate the samples per chunk based on the chunk size if sample_size
419 // is nonzero.
420 // trak->mdia.minf.stbl.stsd.table[0].sample_size = bytes_per_sample;
421 trak->mdia.minf.stbl.stsd.table[0].compression_id = compression_id;
423 /* Synthesize stsc table for constant samples per chunk */
424 if(!bytes_per_sample)
426 /* Should be enough entries allocated in quicktime_stsc_init_table */
427 trak->mdia.minf.stbl.stsc.table[0].samples = samples_per_chunk;
428 trak->mdia.minf.stbl.stsc.total_entries = 1;
433 //printf("quicktime_read_strl 100\n");