r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / quicktime / avi_hdrl.c
blob3b0b557d8d08b4a347a75dc1d9500e5516126ef1
1 #include "funcprotos.h"
2 #include "quicktime.h"
6 void quicktime_delete_hdrl(quicktime_t *file, quicktime_hdrl_t *hdrl)
8 int i;
9 for(i = 0; i < file->moov.total_tracks; i++)
11 quicktime_delete_strl(hdrl->strl[i]);
16 void quicktime_read_hdrl(quicktime_t *file,
17 quicktime_hdrl_t *hdrl,
18 quicktime_atom_t *parent_atom)
20 quicktime_atom_t leaf_atom;
21 char data[4];
22 int current_track = 0;
24 //printf("quicktime_read_hdrl 1\n");
27 quicktime_atom_read_header(file, &leaf_atom);
29 /* Got LIST */
30 if(quicktime_atom_is(&leaf_atom, "LIST"))
32 data[0] = data[1] = data[2] = data[3] = 0;
33 quicktime_read_data(file, data, 4);
35 /* Got strl */
36 if(quicktime_match_32(data, "strl"))
38 quicktime_strl_t *strl =
39 hdrl->strl[current_track++] =
40 quicktime_new_strl();
41 quicktime_read_strl(file, strl, &leaf_atom);
45 quicktime_atom_skip(file, &leaf_atom);
46 }while(quicktime_position(file) < parent_atom->end);
48 quicktime_atom_skip(file, &leaf_atom);
51 void quicktime_init_hdrl(quicktime_t *file, quicktime_hdrl_t *hdrl)
53 int i;
54 quicktime_atom_t avih_atom;
55 int current_track;
57 // LIST 'hdrl'
58 quicktime_atom_write_header(file, &hdrl->atom, "LIST");
59 quicktime_write_char32(file, "hdrl");
62 // avih
63 quicktime_atom_write_header(file, &avih_atom, "avih");
65 if(file->total_vtracks)
66 quicktime_write_int32_le(file,
67 (uint32_t)(1000000 /
68 quicktime_frame_rate(file, 0)));
69 else
70 quicktime_write_int32_le(file, 0);
72 hdrl->bitrate_offset = quicktime_position(file);
73 quicktime_write_int32_le(file, 0); /* bitrate in bytes */
74 quicktime_write_int32_le(file, 0); /* padding */
75 quicktime_write_int32_le(file,
76 AVI_TRUSTCKTYPE |
77 AVI_HASINDEX |
78 AVI_MUSTUSEINDEX |
79 AVI_ISINTERLEAVED); /* flags */
80 hdrl->frames_offset = quicktime_position(file);
81 quicktime_write_int32_le(file, 0); /* nb frames, filled later */
82 quicktime_write_int32_le(file, 0); /* initial frame */
83 quicktime_write_int32_le(file, file->moov.total_tracks); /* nb streams */
84 quicktime_write_int32_le(file, 0); /* suggested buffer size */
86 if(file->total_vtracks)
88 quicktime_write_int32_le(file, file->vtracks[0].track->tkhd.track_width);
89 quicktime_write_int32_le(file, file->vtracks[0].track->tkhd.track_height);
91 else
93 quicktime_write_int32_le(file, 0);
94 quicktime_write_int32_le(file, 0);
96 quicktime_write_int32_le(file, 0); /* reserved */
97 quicktime_write_int32_le(file, 0); /* reserved */
98 quicktime_write_int32_le(file, 0); /* reserved */
99 quicktime_write_int32_le(file, 0); /* reserved */
101 quicktime_atom_write_footer(file, &avih_atom);
104 /* Write stream lists. */
105 /* Need the track maps to get the WAV ID for audio. */
106 current_track = 0;
107 for(i = 0; i < file->total_vtracks; i++)
109 quicktime_video_map_t *video_map = &file->vtracks[i];
110 quicktime_trak_t *trak = video_map->track;
111 quicktime_strl_t *strl =
112 hdrl->strl[current_track++] =
113 quicktime_new_strl();
114 quicktime_init_strl(file,
116 video_map,
117 trak,
118 strl);
121 for(i = 0; i < file->total_atracks; i++)
123 quicktime_audio_map_t *audio_map = &file->atracks[i];
124 quicktime_trak_t *trak = audio_map->track;
125 quicktime_strl_t *strl =
126 hdrl->strl[current_track++] =
127 quicktime_new_strl();
128 quicktime_init_strl(file,
129 audio_map,
131 trak,
132 strl);
136 * for(i = 0; i < file->moov.total_tracks; i++)
138 * printf("quicktime_init_hdrl 10 %d %p\n", i, file->riff[0]->hdrl.strl[i]->tag);
142 /* ODML header */
143 quicktime_init_odml(file, hdrl);
144 quicktime_atom_write_footer(file, &hdrl->atom);
148 void quicktime_finalize_hdrl(quicktime_t *file, quicktime_hdrl_t *hdrl)
150 int i;
151 int64_t position = quicktime_position(file);
152 int64_t total_frames = 0;
153 double frame_rate = 0;
155 for(i = 0; i < file->moov.total_tracks; i++)
157 quicktime_trak_t *trak = file->moov.trak[i];
158 quicktime_strl_t *strl = hdrl->strl[i];
160 if(trak->mdia.minf.is_video)
162 int length;
163 quicktime_set_position(file, strl->length_offset);
164 total_frames = length = quicktime_track_samples(file, trak);
165 quicktime_write_int32_le(file, length);
166 frame_rate = (double)trak->mdia.mdhd.time_scale /
167 trak->mdia.minf.stbl.stts.table[0].sample_duration;
169 else
170 if(trak->mdia.minf.is_audio)
172 int length, samples_per_chunk;
173 quicktime_set_position(file, strl->length_offset);
174 length = quicktime_track_samples(file, trak);
175 quicktime_write_int32_le(file, length);
176 quicktime_set_position(file, strl->samples_per_chunk_offset);
178 samples_per_chunk = quicktime_avg_chunk_samples(file, trak);
179 quicktime_write_int32_le(file,
180 samples_per_chunk);
181 quicktime_write_int32_le(file,
182 samples_per_chunk *
183 trak->mdia.minf.stbl.stsd.table[0].sample_rate);
185 quicktime_set_position(file, strl->sample_size_offset);
186 quicktime_write_int32_le(file,
187 trak->mdia.minf.stbl.stsd.table[0].sample_size);
191 if(total_frames)
193 quicktime_set_position(file, hdrl->bitrate_offset);
194 quicktime_write_int32_le(file,
195 file->total_length / (total_frames / frame_rate));
196 quicktime_set_position(file, hdrl->frames_offset);
197 quicktime_write_int32_le(file, total_frames);
200 quicktime_set_position(file, position);