1 /*******************************************************************************
2  mdia.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_mdia_init(quicktime_mdia_t * mdia)27 void quicktime_mdia_init(quicktime_mdia_t *mdia)
28   {
29   quicktime_mdhd_init(&mdia->mdhd);
30   quicktime_hdlr_init(&mdia->hdlr);
31   quicktime_minf_init(&mdia->minf);
32   }
33 
quicktime_mdia_init_qtvr(quicktime_t * file,quicktime_mdia_t * mdia,int track_type,int timescale,int frame_duration)34 void quicktime_mdia_init_qtvr(quicktime_t *file,
35                               quicktime_mdia_t *mdia,
36                               int track_type,
37                               int timescale,
38                               int frame_duration)
39   {
40   quicktime_mdhd_init_video(file, &mdia->mdhd, timescale);
41   quicktime_minf_init_qtvr(file, &mdia->minf, track_type, frame_duration);
42   quicktime_hdlr_init_qtvr(&mdia->hdlr, track_type);
43   }
44 
quicktime_mdia_init_panorama(quicktime_t * file,quicktime_mdia_t * mdia,int width,int height,int timescale,int frame_duration)45 void quicktime_mdia_init_panorama(quicktime_t *file,
46                                   quicktime_mdia_t *mdia,
47                                   int width,
48                                   int height,
49                                   int timescale,
50                                   int frame_duration)
51   {
52   quicktime_mdhd_init_video(file, &mdia->mdhd, timescale);
53   quicktime_minf_init_panorama(file, &mdia->minf, width, height, frame_duration);
54   quicktime_hdlr_init_panorama(&mdia->hdlr);
55   }
56 
quicktime_mdia_init_video(quicktime_t * file,quicktime_mdia_t * mdia,int frame_w,int frame_h,int frame_duration,int timescale,char * compressor)57 void quicktime_mdia_init_video(quicktime_t *file,
58                                quicktime_mdia_t *mdia,
59                                int frame_w,
60                                int frame_h,
61                                int frame_duration,
62                                int timescale,
63                                char *compressor)
64   {
65   quicktime_mdhd_init_video(file, &mdia->mdhd, timescale);
66   quicktime_minf_init_video(file, &mdia->minf, frame_w, frame_h,
67                             frame_duration, timescale, compressor);
68   quicktime_hdlr_init_video(&mdia->hdlr);
69   }
70 
quicktime_mdia_init_timecode(quicktime_t * file,quicktime_mdia_t * mdia,int time_scale,int frame_duration,int num_frames,uint32_t flags)71 void quicktime_mdia_init_timecode(quicktime_t *file,
72                                   quicktime_mdia_t *mdia,
73                                   int time_scale,
74                                   int frame_duration,
75                                   int num_frames,
76                                   uint32_t flags)
77   {
78   quicktime_mdhd_init_video(file, &mdia->mdhd, time_scale);
79   quicktime_minf_init_timecode(file, &mdia->minf, time_scale,
80                                frame_duration, num_frames, flags);
81   quicktime_hdlr_init_timecode(&mdia->hdlr);
82   }
83 
quicktime_mdia_init_audio(quicktime_t * file,quicktime_mdia_t * mdia,int channels,int sample_rate,int bits,char * compressor)84 void quicktime_mdia_init_audio(quicktime_t *file,
85                                quicktime_mdia_t *mdia,
86                                int channels,
87                                int sample_rate,
88                                int bits,
89                                char *compressor)
90   {
91   quicktime_mdhd_init_audio(file,
92                             &mdia->mdhd,
93                             sample_rate);
94   quicktime_minf_init_audio(file,
95                             &mdia->minf,
96                             channels,
97                             sample_rate,
98                             bits,
99                             compressor);
100   quicktime_hdlr_init_audio(&mdia->hdlr);
101   }
102 
quicktime_mdia_init_text(quicktime_t * file,quicktime_mdia_t * mdia,int timescale)103 void quicktime_mdia_init_text(quicktime_t * file,
104                               quicktime_mdia_t * mdia,
105                               int timescale)
106   {
107   quicktime_hdlr_init_text(&mdia->hdlr);
108   quicktime_mdhd_init_text(file, &mdia->mdhd,
109                            timescale);
110 
111   quicktime_minf_init_text(file, &mdia->minf);
112   }
113 
quicktime_mdia_init_tx3g(quicktime_t * file,quicktime_mdia_t * mdia,int timescale)114 void quicktime_mdia_init_tx3g(quicktime_t * file,
115                               quicktime_mdia_t * mdia,
116                               int timescale)
117   {
118   quicktime_hdlr_init_tx3g(&mdia->hdlr);
119   quicktime_mdhd_init_text(file, &mdia->mdhd,
120                            timescale);
121   quicktime_minf_init_tx3g(file, &mdia->minf);
122   }
123 
124 
quicktime_mdia_delete(quicktime_mdia_t * mdia)125 void quicktime_mdia_delete(quicktime_mdia_t *mdia)
126   {
127   quicktime_mdhd_delete(&mdia->mdhd);
128   quicktime_hdlr_delete(&mdia->hdlr);
129   quicktime_minf_delete(&mdia->minf);
130   }
131 
quicktime_mdia_dump(quicktime_mdia_t * mdia)132 void quicktime_mdia_dump(quicktime_mdia_t *mdia)
133   {
134   lqt_dump("  media (mdia)\n");
135   quicktime_mdhd_dump(&mdia->mdhd);
136   quicktime_hdlr_dump(&mdia->hdlr);
137   quicktime_minf_dump(&mdia->minf);
138   }
139 
quicktime_read_mdia(quicktime_t * file,quicktime_trak_t * trak,quicktime_mdia_t * mdia,quicktime_atom_t * trak_atom)140 int quicktime_read_mdia(quicktime_t *file, quicktime_trak_t *trak,
141                         quicktime_mdia_t *mdia, quicktime_atom_t *trak_atom)
142   {
143   quicktime_atom_t leaf_atom;
144 
145   do
146     {
147     quicktime_atom_read_header(file, &leaf_atom);
148 
149     /* mandatory */
150     if(quicktime_atom_is(&leaf_atom, "mdhd"))
151       quicktime_read_mdhd(file, &mdia->mdhd);
152     else if(quicktime_atom_is(&leaf_atom, "hdlr"))
153       quicktime_read_hdlr(file, &mdia->hdlr, &leaf_atom);
154     else if(quicktime_atom_is(&leaf_atom, "minf"))
155       quicktime_read_minf(file, trak, &mdia->minf, &leaf_atom);
156     else
157       quicktime_atom_skip(file, &leaf_atom);
158     }while(quicktime_position(file) < trak_atom->end);
159 
160   return 0;
161   }
162 
quicktime_write_mdia(quicktime_t * file,quicktime_mdia_t * mdia)163 void quicktime_write_mdia(quicktime_t *file, quicktime_mdia_t *mdia)
164   {
165   quicktime_atom_t atom;
166   quicktime_atom_write_header(file, &atom, "mdia");
167 
168   quicktime_write_mdhd(file, &mdia->mdhd);
169   quicktime_write_hdlr(file, &mdia->hdlr);
170   quicktime_write_minf(file, &mdia->minf);
171 
172   quicktime_atom_write_footer(file, &atom);
173   }
174