1 /* Quicktime muxer plugin for GStreamer
2  * Copyright (C) 2008-2010 Thiago Santos <thiagoss@embedded.ufcg.edu.br>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 /*
20  * Unless otherwise indicated, Source Code is licensed under MIT license.
21  * See further explanation attached in License Statement (distributed in the file
22  * LICENSE).
23  *
24  * Permission is hereby granted, free of charge, to any person obtaining a copy of
25  * this software and associated documentation files (the "Software"), to deal in
26  * the Software without restriction, including without limitation the rights to
27  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28  * of the Software, and to permit persons to whom the Software is furnished to do
29  * so, subject to the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be included in all
32  * copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40  * SOFTWARE.
41  */
42 
43 #ifndef __GST_QT_MUX_H__
44 #define __GST_QT_MUX_H__
45 
46 #include <gst/gst.h>
47 #include <gst/base/gstcollectpads.h>
48 
49 #include "fourcc.h"
50 #include "atoms.h"
51 #include "atomsrecovery.h"
52 #include "gstqtmuxmap.h"
53 
54 G_BEGIN_DECLS
55 
56 #define GST_TYPE_QT_MUX (gst_qt_mux_get_type())
57 #define GST_QT_MUX(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_QT_MUX, GstQTMux))
58 #define GST_QT_MUX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_QT_MUX, GstQTMux))
59 #define GST_IS_QT_MUX(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_QT_MUX))
60 #define GST_IS_QT_MUX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_QT_MUX))
61 #define GST_QT_MUX_CAST(obj) ((GstQTMux*)(obj))
62 
63 
64 typedef struct _GstQTMux GstQTMux;
65 typedef struct _GstQTMuxClass GstQTMuxClass;
66 typedef struct _GstQTPad GstQTPad;
67 
68 /*
69  * GstQTPadPrepareBufferFunc
70  *
71  * Receives a buffer (takes ref) and returns a new buffer that should
72  * replace the passed one.
73  *
74  * Useful for when the pad/datatype needs some manipulation before
75  * being muxed. (Originally added for image/x-jpc support, for which buffers
76  * need to be wrapped into a isom box)
77  */
78 typedef GstBuffer * (*GstQTPadPrepareBufferFunc) (GstQTPad * pad,
79     GstBuffer * buf, GstQTMux * qtmux);
80 
81 typedef gboolean (*GstQTPadSetCapsFunc) (GstQTPad * pad, GstCaps * caps);
82 typedef GstBuffer * (*GstQTPadCreateEmptyBufferFunc) (GstQTPad * pad, gint64 duration);
83 
84 #define QTMUX_NO_OF_TS   10
85 
86 struct _GstQTPad
87 {
88   GstCollectData collect;       /* we extend the CollectData */
89 
90   /* fourcc id of stream */
91   guint32 fourcc;
92   /* whether using format that have out of order buffers */
93   gboolean is_out_of_order;
94   /* if not 0, track with constant sized samples, e.g. raw audio */
95   guint sample_size;
96   /* make sync table entry */
97   gboolean sync;
98   /* if it is a sparse stream
99    * (meaning we can't use PTS differences to compute duration) */
100   gboolean sparse;
101   /* bitrates */
102   guint32 avg_bitrate, max_bitrate;
103   /* expected sample duration */
104   guint expected_sample_duration_n;
105   guint expected_sample_duration_d;
106 
107   /* for avg bitrate calculation */
108   guint64 total_bytes;
109   guint64 total_duration;
110 
111   GstBuffer *last_buf;
112   /* dts of last_buf */
113   GstClockTime last_dts;
114   guint64 sample_offset;
115 
116   /* This is compensate for CTTS */
117   GstClockTime dts_adjustment;
118 
119   /* store the first timestamp for comparing with other streams and
120    * know if there are late streams */
121   /* subjected to dts adjustment */
122   GstClockTime first_ts;
123   GstClockTime first_dts;
124 
125   /* all the atom and chunk book-keeping is delegated here
126    * unowned/uncounted reference, parent MOOV owns */
127   AtomTRAK *trak;
128   AtomTRAK *tc_trak;
129   SampleTableEntry *trak_ste;
130   /* fragmented support */
131   /* meta data book-keeping delegated here */
132   AtomTRAF *traf;
133   /* fragment buffers */
134   ATOM_ARRAY (GstBuffer *) fragment_buffers;
135   /* running fragment duration */
136   gint64 fragment_duration;
137   /* optional fragment index book-keeping */
138   AtomTFRA *tfra;
139 
140   /* Set when tags are received, cleared when written to moov */
141   gboolean tags_changed;
142 
143   GstTagList *tags;
144 
145   /* if nothing is set, it won't be called */
146   GstQTPadPrepareBufferFunc prepare_buf_func;
147   GstQTPadSetCapsFunc set_caps;
148   GstQTPadCreateEmptyBufferFunc create_empty_buffer;
149 
150   /* SMPTE timecode */
151   GstVideoTimeCode *first_tc;
152   GstClockTime first_pts;
153   guint64 tc_pos;
154 
155   /* for keeping track in pre-fill mode */
156   GArray *samples;
157   /* current sample */
158   GstAdapter *raw_audio_adapter;
159   guint64 raw_audio_adapter_offset;
160   GstClockTime raw_audio_adapter_pts;
161 };
162 
163 typedef enum _GstQTMuxState
164 {
165   GST_QT_MUX_STATE_NONE,
166   GST_QT_MUX_STATE_STARTED,
167   GST_QT_MUX_STATE_DATA,
168   GST_QT_MUX_STATE_EOS
169 } GstQTMuxState;
170 
171 typedef enum _GstQtMuxMode {
172     GST_QT_MUX_MODE_MOOV_AT_END,
173     GST_QT_MUX_MODE_FRAGMENTED,
174     GST_QT_MUX_MODE_FRAGMENTED_STREAMABLE,
175     GST_QT_MUX_MODE_FAST_START,
176     GST_QT_MUX_MODE_ROBUST_RECORDING,
177     GST_QT_MUX_MODE_ROBUST_RECORDING_PREFILL,
178 } GstQtMuxMode;
179 
180 struct _GstQTMux
181 {
182   GstElement element;
183 
184   GstPad *srcpad;
185   GstCollectPads *collect;
186   GSList *sinkpads;
187 
188   /* state */
189   GstQTMuxState state;
190 
191   /* Mux mode, inferred from property
192    * set in gst_qt_mux_start_file() */
193   GstQtMuxMode mux_mode;
194 
195   /* size of header (prefix, atoms (ftyp, possibly moov, mdat header)) */
196   guint64 header_size;
197   /* accumulated size of raw media data (not including mdat header) */
198   guint64 mdat_size;
199   /* position of the moov (for fragmented mode) or reserved moov atom
200    * area (for robust-muxing mode) */
201   guint64 moov_pos;
202   /* position of mdat atom header (for later updating of size) in
203    * moov-at-end, fragmented and robust-muxing modes */
204   guint64 mdat_pos;
205 
206   /* keep track of the largest chunk to fine-tune brands */
207   GstClockTime longest_chunk;
208 
209   /* Earliest timestamp across all pads/traks
210    * (unadjusted incoming PTS) */
211   GstClockTime first_ts;
212   /* Last DTS across all pads (= duration) */
213   GstClockTime last_dts;
214 
215   /* Last pad we used for writing the current chunk */
216   GstQTPad *current_pad;
217   guint64 current_chunk_size;
218   GstClockTime current_chunk_duration;
219   guint64 current_chunk_offset;
220 
221   /* atom helper objects */
222   AtomsContext *context;
223   AtomFTYP *ftyp;
224   AtomMOOV *moov;
225   GSList *extra_atoms; /* list of extra top-level atoms (e.g. UUID for xmp)
226                         * Stored as AtomInfo structs */
227 
228   /* Set when tags are received, cleared when written to moov */
229   gboolean tags_changed;
230 
231   /* fragmented file index */
232   AtomMFRA *mfra;
233 
234   /* fast start */
235   FILE *fast_start_file;
236 
237   /* moov recovery */
238   FILE *moov_recov_file;
239 
240   /* fragment sequence */
241   guint32 fragment_sequence;
242 
243   /* properties */
244   guint32 timescale;
245   guint32 trak_timescale;
246   AtomsTreeFlavor flavor;
247   gboolean fast_start;
248   gboolean guess_pts;
249 #ifndef GST_REMOVE_DEPRECATED
250   gint dts_method;
251 #endif
252   gchar *fast_start_file_path;
253   gchar *moov_recov_file_path;
254   guint32 fragment_duration;
255   /* Whether or not to work in 'streamable' mode and not
256    * seek to rewrite headers - only valid for fragmented
257    * mode. */
258   gboolean streamable;
259 
260   /* Requested target maximum duration */
261   GstClockTime reserved_max_duration;
262   /* Estimate of remaining reserved header space (in ns of recording) */
263   GstClockTime reserved_duration_remaining;
264   /* Multiplier for conversion from reserved_max_duration to bytes */
265   guint reserved_bytes_per_sec_per_trak;
266 
267   guint64 interleave_bytes;
268   GstClockTime interleave_time;
269   gboolean interleave_bytes_set, interleave_time_set;
270 
271   GstClockTime max_raw_audio_drift;
272 
273   /* Reserved minimum MOOV size in bytes
274    * This is converted from reserved_max_duration
275    * using the bytes/trak/sec estimate */
276   guint32 reserved_moov_size;
277   /* Basic size of the moov (static headers + tags) */
278   guint32 base_moov_size;
279   /* Size of the most recently generated moov header */
280   guint32 last_moov_size;
281   /* True if the first moov in the ping-pong buffers
282    * is the active one. See gst_qt_mux_robust_recording_rewrite_moov() */
283   gboolean reserved_moov_first_active;
284 
285   /* Tracking of periodic MOOV updates */
286   GstClockTime last_moov_update;
287   GstClockTime reserved_moov_update_period;
288   GstClockTime muxed_since_last_update;
289 
290   gboolean reserved_prefill;
291 
292   GstClockTime start_gap_threshold;
293 
294   /* for request pad naming */
295   guint video_pads, audio_pads, subtitle_pads, caption_pads;
296 };
297 
298 struct _GstQTMuxClass
299 {
300   GstElementClass parent_class;
301 
302   GstQTMuxFormat format;
303 };
304 
305 /* type register helper struct */
306 typedef struct _GstQTMuxClassParams
307 {
308   GstQTMuxFormatProp *prop;
309   GstCaps *src_caps;
310   GstCaps *video_sink_caps;
311   GstCaps *audio_sink_caps;
312   GstCaps *subtitle_sink_caps;
313   GstCaps *caption_sink_caps;
314 } GstQTMuxClassParams;
315 
316 #define GST_QT_MUX_PARAMS_QDATA g_quark_from_static_string("qt-mux-params")
317 
318 GType gst_qt_mux_get_type (void);
319 gboolean gst_qt_mux_register (GstPlugin * plugin);
320 
321 /* FIXME: ideally classification tag should be added and
322  * registered in gstreamer core gsttaglist
323  *
324  * this tag is a string in the format: entityfourcc://table_num/content
325  * FIXME Shouldn't we add a field for 'language'?
326  */
327 #define GST_TAG_3GP_CLASSIFICATION "classification"
328 
329 G_END_DECLS
330 
331 #endif /* __GST_QT_MUX_H__ */
332