1 /*******************************************************************************
2  hdlr.c
3 
4  libquicktime - A library for reading and writing quicktime/avi/mp4 files.
5  http://libquicktime.sourceforge.net
6 
7  Copyright (C) 2002 Heroine Virtual Ltd.
8  Copyright (C) 2002-2011 Members of the libquicktime project.
9 
10  This library is free software; you can redistribute it and/or modify it under
11  the terms of the GNU Lesser General Public License as published by the Free
12  Software Foundation; either version 2.1 of the License, or (at your option)
13  any later version.
14 
15  This library is distributed in the hope that it will be useful, but WITHOUT
16  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18  details.
19 
20  You should have received a copy of the GNU Lesser General Public License along
21  with this library; if not, write to the Free Software Foundation, Inc., 51
22  Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 *******************************************************************************/
24 
25 #include "lqt_private.h"
26 #include <string.h>
27 
quicktime_hdlr_init(quicktime_hdlr_t * hdlr)28 void quicktime_hdlr_init(quicktime_hdlr_t *hdlr)
29 {
30 	hdlr->version = 0;
31 	hdlr->flags = 0;
32 	hdlr->component_type[0] = 'm';
33 	hdlr->component_type[1] = 'h';
34 	hdlr->component_type[2] = 'l';
35 	hdlr->component_type[3] = 'r';
36 	hdlr->component_subtype[0] = 'v';
37 	hdlr->component_subtype[1] = 'i';
38 	hdlr->component_subtype[2] = 'd';
39 	hdlr->component_subtype[3] = 'e';
40 	hdlr->component_manufacturer[0] = 0;
41 	hdlr->component_manufacturer[1] = 0;
42 	hdlr->component_manufacturer[2] = 0;
43 	hdlr->component_manufacturer[3] = 0;
44 	hdlr->component_flags = 0;
45 	hdlr->component_flag_mask = 0;
46 	strcpy(hdlr->component_name, "Libquicktime Media Handler");
47 }
48 
quicktime_hdlr_init_qtvr(quicktime_hdlr_t * hdlr,int track_type)49 int quicktime_hdlr_init_qtvr(quicktime_hdlr_t *hdlr, int track_type)
50 {
51 	switch(track_type)
52 	{
53 	case QTVR_QTVR_OBJ:
54 	case QTVR_QTVR_PAN:
55 		hdlr->component_subtype[0] = 'q';
56 		hdlr->component_subtype[1] = 't';
57 		hdlr->component_subtype[2] = 'v';
58 		hdlr->component_subtype[3] = 'r';
59 		strcpy(hdlr->component_name, "Libquicktime QTVR Handler");
60 		break;
61 	case QTVR_OBJ:
62 		hdlr->component_subtype[0] = 'o';
63 		hdlr->component_subtype[1] = 'b';
64 		hdlr->component_subtype[2] = 'j';
65 		hdlr->component_subtype[3] = 'e';
66 		strcpy(hdlr->component_name, "Libquicktime QTVR Object Handler");
67 		break;
68 	case QTVR_PAN:
69 		hdlr->component_subtype[0] = 'p';
70 		hdlr->component_subtype[1] = 'a';
71 		hdlr->component_subtype[2] = 'n';
72 		hdlr->component_subtype[3] = 'o';
73 		strcpy(hdlr->component_name, "Libquicktime QTVR Panorama Handler");
74 		break;
75 	default:
76 		return -1;
77 	}
78 
79 	return 0;
80 }
81 
quicktime_hdlr_init_panorama(quicktime_hdlr_t * hdlr)82 void quicktime_hdlr_init_panorama(quicktime_hdlr_t *hdlr)
83 {
84 	hdlr->component_subtype[0] = 'S';
85 	hdlr->component_subtype[1] = 'T';
86 	hdlr->component_subtype[2] = 'p';
87 	hdlr->component_subtype[3] = 'n';
88 	strcpy(hdlr->component_name, "Libquicktime Panorama Media Handler");
89 }
90 
quicktime_hdlr_init_video(quicktime_hdlr_t * hdlr)91 void quicktime_hdlr_init_video(quicktime_hdlr_t *hdlr)
92 {
93 	hdlr->component_subtype[0] = 'v';
94 	hdlr->component_subtype[1] = 'i';
95 	hdlr->component_subtype[2] = 'd';
96 	hdlr->component_subtype[3] = 'e';
97 	strcpy(hdlr->component_name, "Libquicktime Video Media Handler");
98 }
99 
quicktime_hdlr_init_audio(quicktime_hdlr_t * hdlr)100 void quicktime_hdlr_init_audio(quicktime_hdlr_t *hdlr)
101 {
102 	hdlr->component_subtype[0] = 's';
103 	hdlr->component_subtype[1] = 'o';
104 	hdlr->component_subtype[2] = 'u';
105 	hdlr->component_subtype[3] = 'n';
106 	strcpy(hdlr->component_name, "Libquicktime Sound Media Handler");
107 }
108 
quicktime_hdlr_init_timecode(quicktime_hdlr_t * hdlr)109 void quicktime_hdlr_init_timecode(quicktime_hdlr_t *hdlr)
110   {
111   hdlr->component_subtype[0] = 't';
112   hdlr->component_subtype[1] = 'm';
113   hdlr->component_subtype[2] = 'c';
114   hdlr->component_subtype[3] = 'd';
115   strcpy(hdlr->component_name, "Libquicktime Time Code Media Handler");
116   }
117 
quicktime_hdlr_init_text(quicktime_hdlr_t * hdlr)118 void quicktime_hdlr_init_text(quicktime_hdlr_t *hdlr)
119   {
120   hdlr->component_subtype[0] = 't';
121   hdlr->component_subtype[1] = 'e';
122   hdlr->component_subtype[2] = 'x';
123   hdlr->component_subtype[3] = 't';
124   strcpy(hdlr->component_name, "Libquicktime Text Media Handler");
125   }
126 
quicktime_hdlr_init_tx3g(quicktime_hdlr_t * hdlr)127 void quicktime_hdlr_init_tx3g(quicktime_hdlr_t *hdlr)
128   {
129   hdlr->component_subtype[0] = 't';
130   hdlr->component_subtype[1] = 'e';
131   hdlr->component_subtype[2] = 'x';
132   hdlr->component_subtype[3] = 't';
133   strcpy(hdlr->component_name, "Libquicktime Streaming Text Handler");
134   }
135 
136 /*
137   	hdlr->version = quicktime_read_char(file);
138 	hdlr->flags = quicktime_read_int24(file);
139 	quicktime_read_char32(file, hdlr->component_type);
140 	quicktime_read_char32(file, hdlr->component_subtype);
141 	hdlr->component_manufacturer = quicktime_read_int32(file);
142 	hdlr->component_flags = quicktime_read_int32(file);
143 	hdlr->component_flag_mask = quicktime_read_int32(file);
144 	quicktime_read_pascal(file, hdlr->component_name);
145 */
146 
quicktime_hdlr_init_udta(quicktime_hdlr_t * hdlr)147 void quicktime_hdlr_init_udta(quicktime_hdlr_t *hdlr)
148 {
149    hdlr->version = 0;
150    hdlr->flags = 0;
151    hdlr->component_type[0] = 0x00;
152    hdlr->component_type[1] = 0x00;
153    hdlr->component_type[2] = 0x00;
154    hdlr->component_type[3] = 0x00;
155    hdlr->component_subtype[0] = 'm';
156    hdlr->component_subtype[1] = 'd';
157    hdlr->component_subtype[2] = 'i';
158    hdlr->component_subtype[3] = 'r';
159    hdlr->component_manufacturer[0] = 'a';
160    hdlr->component_manufacturer[1] = 'p';
161    hdlr->component_manufacturer[2] = 'p';
162    hdlr->component_manufacturer[3] = 'l';
163    hdlr->component_flags = 0;
164    hdlr->component_flag_mask = 0;
165    hdlr->component_name[0] = '\0';
166 }
167 
168 
quicktime_hdlr_init_data(quicktime_hdlr_t * hdlr)169 void quicktime_hdlr_init_data(quicktime_hdlr_t *hdlr)
170 {
171 	hdlr->component_type[0] = 'd';
172 	hdlr->component_type[1] = 'h';
173 	hdlr->component_type[2] = 'l';
174 	hdlr->component_type[3] = 'r';
175 	hdlr->component_subtype[0] = 'a';
176 	hdlr->component_subtype[1] = 'l';
177 	hdlr->component_subtype[2] = 'i';
178 	hdlr->component_subtype[3] = 's';
179 	strcpy(hdlr->component_name, "Linux Alias Data Handler");
180 }
181 
quicktime_hdlr_delete(quicktime_hdlr_t * hdlr)182 void quicktime_hdlr_delete(quicktime_hdlr_t *hdlr)
183 {
184 }
185 
quicktime_hdlr_dump(quicktime_hdlr_t * hdlr)186 void quicktime_hdlr_dump(quicktime_hdlr_t *hdlr)
187 {
188 	lqt_dump("   handler reference (hdlr)\n");
189 	lqt_dump("    version %d\n", hdlr->version);
190 	lqt_dump("    flags %ld\n", hdlr->flags);
191 	lqt_dump("    component_type %c%c%c%c\n", hdlr->component_type[0], hdlr->component_type[1], hdlr->component_type[2], hdlr->component_type[3]);
192 	lqt_dump("    component_subtype %c%c%c%c\n", hdlr->component_subtype[0], hdlr->component_subtype[1], hdlr->component_subtype[2], hdlr->component_subtype[3]);
193 	lqt_dump("    component_name %s\n", hdlr->component_name);
194 }
195 
quicktime_read_hdlr(quicktime_t * file,quicktime_hdlr_t * hdlr,quicktime_atom_t * parent_atom)196 void quicktime_read_hdlr(quicktime_t *file, quicktime_hdlr_t *hdlr, quicktime_atom_t * parent_atom)
197 {
198         int component_name_len;
199 	hdlr->version = quicktime_read_char(file);
200 	hdlr->flags = quicktime_read_int24(file);
201 	quicktime_read_char32(file, hdlr->component_type);
202 	quicktime_read_char32(file, hdlr->component_subtype);
203 	quicktime_read_char32(file, hdlr->component_manufacturer);
204 	hdlr->component_flags = quicktime_read_int32(file);
205 	hdlr->component_flag_mask = quicktime_read_int32(file);
206         /* Difference between mp4 and quicktime: In quicktime, component name
207            is a pascal string. In mp4, it's until the end of the atom */
208         if(!hdlr->component_type[0] && !hdlr->component_type[1] &&
209            !hdlr->component_type[2] && !hdlr->component_type[3])
210           {
211           component_name_len = parent_atom->end - quicktime_position(file);
212           if(component_name_len > 256)
213             component_name_len = 256;
214           quicktime_read_data(file, (uint8_t*)hdlr->component_name, component_name_len);
215           }
216         else
217           {
218           if(quicktime_position(file) < parent_atom->end)
219             quicktime_read_pascal(file, hdlr->component_name);
220           }
221         /* Main Actor doesn't write component name */
222         quicktime_atom_skip(file, parent_atom);
223 
224 }
225 
quicktime_write_hdlr(quicktime_t * file,quicktime_hdlr_t * hdlr)226 void quicktime_write_hdlr(quicktime_t *file, quicktime_hdlr_t *hdlr)
227 {
228 	quicktime_atom_t atom;
229 	quicktime_atom_write_header(file, &atom, "hdlr");
230 
231 	quicktime_write_char(file, hdlr->version);
232 	quicktime_write_int24(file, hdlr->flags);
233 
234         if(IS_MP4(file->file_type))
235           quicktime_write_int32(file, 0);
236         else
237           quicktime_write_char32(file, hdlr->component_type);
238 
239 	quicktime_write_char32(file, hdlr->component_subtype);
240 	quicktime_write_char32(file, hdlr->component_manufacturer);
241 	quicktime_write_int32(file, hdlr->component_flags);
242 	quicktime_write_int32(file, hdlr->component_flag_mask);
243         if(IS_MP4(file->file_type))
244           {
245           // quicktime_write_data(file, (uint8_t*)hdlr->component_name, strlen(hdlr->component_name)+1);
246           quicktime_write_int16(file, 0);
247           }
248         else
249           quicktime_write_pascal(file, hdlr->component_name);
250 	quicktime_atom_write_footer(file, &atom);
251 }
252