r955: Fix the Diffkey icon.
[cinelerra_cv.git] / quicktime / esds.c
blobbd39bdd35a6477923c225d75f35b750e0c26e103
1 #include "funcprotos.h"
2 #include "quicktime.h"
5 static int decode_length(quicktime_t *file)
7 int bytes = 0;
8 int result = 0;
9 int byte;
12 byte = quicktime_read_char(file);
13 result = (result << 7) + (byte & 0x7f);
14 bytes++;
15 }while((byte & 0x80) && bytes < 4);
16 return result;
19 void quicktime_delete_esds(quicktime_esds_t *esds)
21 if(esds->mpeg4_header) free(esds->mpeg4_header);
24 void quicktime_esds_samplerate(quicktime_stsd_table_t *table,
25 quicktime_esds_t *esds)
27 // Straight out of ffmpeg
28 if(esds->mpeg4_header_size > 1 &&
29 quicktime_match_32(table->format, QUICKTIME_MP4A))
31 static int samplerate_table[] =
33 96000, 88200, 64000, 48000, 44100, 32000,
34 24000, 22050, 16000, 12000, 11025, 8000,
35 7350, 0, 0, 0
38 unsigned char *ptr = esds->mpeg4_header;
39 int samplerate_index = ((ptr[0] & 7) << 1) + ((ptr[1] >> 7) & 1);
40 table->channels = (ptr[1] >> 3) & 0xf;
41 table->sample_rate =
42 samplerate_table[samplerate_index];
43 // Faad decodes 1/2 the requested samplerate if the samplerate is <= 22050.
47 void quicktime_read_esds(quicktime_t *file,
48 quicktime_atom_t *parent_atom,
49 quicktime_esds_t *esds)
52 // version
53 quicktime_read_char(file);
54 // flags
55 quicktime_read_int24(file);
56 // elementary stream descriptor tag
58 if(quicktime_read_char(file) == 0x3)
60 int len = decode_length(file);
61 // elementary stream id
62 quicktime_read_int16(file);
63 // stream priority
64 quicktime_read_char(file);
65 // decoder configuration descripton tab
66 if(quicktime_read_char(file) == 0x4)
68 int len2 = decode_length(file);
69 // object type id
70 quicktime_read_char(file);
71 // stream type
72 quicktime_read_char(file);
73 // buffer size
74 quicktime_read_int24(file);
75 // max bitrate
76 quicktime_read_int32(file);
77 // average bitrate
78 quicktime_read_int32(file);
80 // decoder specific description tag
81 if(quicktime_read_char(file) == 0x5)
83 esds->mpeg4_header_size = decode_length(file);
84 if(!esds->mpeg4_header_size) return;
86 // Need padding for FFMPEG
87 esds->mpeg4_header = calloc(1,
88 esds->mpeg4_header_size + 1024);
89 // Get extra data for decoder
90 quicktime_read_data(file,
91 esds->mpeg4_header,
92 esds->mpeg4_header_size);
94 // skip rest
95 quicktime_atom_skip(file, parent_atom);
96 return;
98 else
100 // error
101 quicktime_atom_skip(file, parent_atom);
102 return;
105 else
107 // error
108 quicktime_atom_skip(file, parent_atom);
109 return;
112 else
114 // error
115 quicktime_atom_skip(file, parent_atom);
116 return;
122 void quicktime_write_esds(quicktime_t *file,
123 quicktime_esds_t *esds,
124 int do_video,
125 int do_audio)
127 quicktime_atom_t atom;
128 quicktime_atom_write_header(file, &atom, "esds");
129 // version
130 quicktime_write_char(file, 0);
131 // flags
132 quicktime_write_int24(file, 0);
134 // elementary stream descriptor tag
135 quicktime_write_char(file, 0x3);
137 // length placeholder
138 int64_t length1 = quicktime_position(file);
139 // quicktime_write_int32(file, 0x80808080);
140 quicktime_write_char(file, 0x00);
142 // elementary stream id
143 quicktime_write_int16(file, 0x1);
145 // stream priority
147 * if(do_video)
148 * quicktime_write_char(file, 0x1f);
149 * else
151 quicktime_write_char(file, 0);
154 // decoder configuration description tab
155 quicktime_write_char(file, 0x4);
156 // length placeholder
157 int64_t length2 = quicktime_position(file);
158 // quicktime_write_int32(file, 0x80808080);
159 quicktime_write_char(file, 0x00);
161 // video
162 if(do_video)
164 // object type id
165 quicktime_write_char(file, 0x20);
166 // stream type
167 quicktime_write_char(file, 0x11);
168 // buffer size
169 // quicktime_write_int24(file, 0x007b00);
170 quicktime_write_int24(file, 0x000000);
171 // max bitrate
172 // quicktime_write_int32(file, 0x00014800);
173 quicktime_write_int32(file, 0x000030d40);
174 // average bitrate
175 // quicktime_write_int32(file, 0x00014800);
176 quicktime_write_int32(file, 0x00000000);
178 else
180 // object type id
181 quicktime_write_char(file, 0x40);
182 // stream type
183 quicktime_write_char(file, 0x15);
184 // buffer size
185 quicktime_write_int24(file, 0x001800);
186 // max bitrate
187 quicktime_write_int32(file, 0x00004e20);
188 // average bitrate
189 quicktime_write_int32(file, 0x00003e80);
192 // decoder specific description tag
193 quicktime_write_char(file, 0x05);
194 // length placeholder
195 int64_t length3 = quicktime_position(file);
196 // quicktime_write_int32(file, 0x80808080);
197 quicktime_write_char(file, 0x00);
199 // mpeg4 sequence header
200 quicktime_write_data(file, esds->mpeg4_header, esds->mpeg4_header_size);
203 int64_t current_length2 = quicktime_position(file) - length2 - 1;
204 int64_t current_length3 = quicktime_position(file) - length3 - 1;
206 // unknown tag, length and data
207 quicktime_write_char(file, 0x06);
208 // quicktime_write_int32(file, 0x80808001);
209 quicktime_write_char(file, 0x01);
210 quicktime_write_char(file, 0x02);
213 // write lengths
214 int64_t current_length1 = quicktime_position(file) - length1 - 1;
215 quicktime_atom_write_footer(file, &atom);
216 int64_t current_position = quicktime_position(file);
217 // quicktime_set_position(file, length1 + 3);
218 quicktime_set_position(file, length1);
219 quicktime_write_char(file, current_length1);
220 // quicktime_set_position(file, length2 + 3);
221 quicktime_set_position(file, length2);
222 quicktime_write_char(file, current_length2);
223 // quicktime_set_position(file, length3 + 3);
224 quicktime_set_position(file, length3);
225 quicktime_write_char(file, current_length3);
226 quicktime_set_position(file, current_position);
229 void quicktime_esds_dump(quicktime_esds_t *esds)
231 printf(" elementary stream description\n");
232 printf(" mpeg4_header_size=0x%x\n", esds->mpeg4_header_size);
233 printf(" mpeg4_header=");
234 int i;
235 for(i = 0; i < esds->mpeg4_header_size; i++)
236 printf("%02x ", (unsigned char)esds->mpeg4_header[i]);
237 printf("\n");