1 /*******************************************************************************
2  mvhd.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 
quicktime_mvhd_init(quicktime_mvhd_t * mvhd)27 int quicktime_mvhd_init(quicktime_mvhd_t *mvhd)
28 {
29 	int i;
30 	mvhd->version = 0;
31 	mvhd->flags = 0;
32 	mvhd->creation_time = quicktime_current_time();
33 	mvhd->modification_time = quicktime_current_time();
34 	mvhd->time_scale = 600;
35 	mvhd->duration = 0;
36 	mvhd->preferred_rate = 1.0;
37 	mvhd->preferred_volume = 1.0;
38 	for(i = 0; i < 10; i++) mvhd->reserved[i] = 0;
39 	quicktime_matrix_init(&mvhd->matrix);
40 	mvhd->preview_time = 0;
41 	mvhd->preview_duration = 0;
42 	mvhd->poster_time = 0;
43 	mvhd->selection_time = 0;
44 	mvhd->selection_duration = 0;
45 	mvhd->current_time = 0;
46 	mvhd->next_track_id = 1;
47 	return 0;
48 }
49 
quicktime_mvhd_delete(quicktime_mvhd_t * mvhd)50 int quicktime_mvhd_delete(quicktime_mvhd_t *mvhd)
51 {
52 	return 0;
53 }
54 
quicktime_mvhd_dump(quicktime_mvhd_t * mvhd)55 void quicktime_mvhd_dump(quicktime_mvhd_t *mvhd)
56 {
57 	lqt_dump(" movie header (mvhd)\n");
58 	lqt_dump("  version %d\n", mvhd->version);
59 	lqt_dump("  flags %ld\n", mvhd->flags);
60 	lqt_dump("  creation_time ");
61         lqt_dump_time(mvhd->creation_time);
62         lqt_dump("\n");
63 	lqt_dump("  modification_time ");
64         lqt_dump_time(mvhd->modification_time);
65         lqt_dump("\n");
66 	lqt_dump("  time_scale %ld\n", mvhd->time_scale);
67 	lqt_dump("  duration %"PRId64"\n", mvhd->duration);
68 	lqt_dump("  preferred_rate %f\n", mvhd->preferred_rate);
69 	lqt_dump("  preferred_volume %f\n", mvhd->preferred_volume);
70 	quicktime_print_chars("  reserved ", mvhd->reserved, 10);
71 	quicktime_matrix_dump(&mvhd->matrix);
72 	lqt_dump("  preview_time %ld\n", mvhd->preview_time);
73 	lqt_dump("  preview_duration %ld\n", mvhd->preview_duration);
74 	lqt_dump("  poster_time %ld\n", mvhd->poster_time);
75 	lqt_dump("  selection_time %ld\n", mvhd->selection_time);
76 	lqt_dump("  selection_duration %ld\n", mvhd->selection_duration);
77 	lqt_dump("  current_time %ld\n", mvhd->current_time);
78 	lqt_dump("  next_track_id %ld\n", mvhd->next_track_id);
79 }
80 
quicktime_read_mvhd(quicktime_t * file,quicktime_mvhd_t * mvhd,quicktime_atom_t * parent_atom)81 void quicktime_read_mvhd(quicktime_t *file, quicktime_mvhd_t *mvhd, quicktime_atom_t *parent_atom)
82 {
83 	mvhd->version = quicktime_read_char(file);
84 	mvhd->flags = quicktime_read_int24(file);
85 
86         if(mvhd->version == 0)
87           {
88           mvhd->creation_time = quicktime_read_int32(file);
89           mvhd->modification_time = quicktime_read_int32(file);
90           }
91         else if(mvhd->version == 1)
92           {
93           mvhd->creation_time = quicktime_read_int64(file);
94           mvhd->modification_time = quicktime_read_int64(file);
95           }
96 	mvhd->time_scale = quicktime_read_int32(file);
97 	if(mvhd->version == 0)
98           mvhd->duration = quicktime_read_int32(file);
99         else if(mvhd->version == 1)
100           mvhd->duration = quicktime_read_int64(file);
101 	mvhd->preferred_rate = quicktime_read_fixed32(file);
102 	mvhd->preferred_volume = quicktime_read_fixed16(file);
103 	quicktime_read_data(file, mvhd->reserved, 10);
104 	quicktime_read_matrix(file, &mvhd->matrix);
105 	mvhd->preview_time = quicktime_read_int32(file);
106 	mvhd->preview_duration = quicktime_read_int32(file);
107 	mvhd->poster_time = quicktime_read_int32(file);
108 	mvhd->selection_time = quicktime_read_int32(file);
109 	mvhd->selection_duration = quicktime_read_int32(file);
110 	mvhd->current_time = quicktime_read_int32(file);
111 	mvhd->next_track_id = quicktime_read_int32(file);
112 }
113 
quicktime_mhvd_init_video(quicktime_t * file,quicktime_mvhd_t * mvhd,int timescale)114 void quicktime_mhvd_init_video(quicktime_t *file, quicktime_mvhd_t *mvhd, int timescale)
115   {
116   if((mvhd->time_scale % timescale) || (mvhd->time_scale < timescale))
117     mvhd->time_scale = timescale;
118   }
119 
quicktime_write_mvhd(quicktime_t * file,quicktime_mvhd_t * mvhd)120 void quicktime_write_mvhd(quicktime_t *file, quicktime_mvhd_t *mvhd)
121 {
122 	quicktime_atom_t atom;
123 	quicktime_atom_write_header(file, &atom, "mvhd");
124 
125 	quicktime_write_char(file, mvhd->version);
126 	quicktime_write_int24(file, mvhd->flags);
127 
128         if(mvhd->version == 0)
129           {
130           quicktime_write_int32(file, mvhd->creation_time);
131           quicktime_write_int32(file, mvhd->modification_time);
132           }
133         else if(mvhd->version == 1)
134           {
135           quicktime_write_int64(file, mvhd->creation_time);
136           quicktime_write_int64(file, mvhd->modification_time);
137           }
138 	quicktime_write_int32(file, mvhd->time_scale);
139         if(mvhd->version == 0)
140           quicktime_write_int32(file, mvhd->duration);
141         else if(mvhd->version == 1)
142           quicktime_write_int64(file, mvhd->duration);
143 
144 	quicktime_write_fixed32(file, mvhd->preferred_rate);
145 	quicktime_write_fixed16(file, mvhd->preferred_volume);
146 	quicktime_write_data(file, mvhd->reserved, 10);
147 	quicktime_write_matrix(file, &mvhd->matrix);
148 	quicktime_write_int32(file, mvhd->preview_time);
149 	quicktime_write_int32(file, mvhd->preview_duration);
150 	quicktime_write_int32(file, mvhd->poster_time);
151 	quicktime_write_int32(file, mvhd->selection_time);
152 	quicktime_write_int32(file, mvhd->selection_duration);
153 	quicktime_write_int32(file, mvhd->current_time);
154 	quicktime_write_int32(file, mvhd->next_track_id);
155 
156 	quicktime_atom_write_footer(file, &atom);
157 }
158