blob: d20ac9b894d0685731d042bab42a011b71393761 [file] [log] [blame]
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001/* Quicktime muxer plugin for GStreamer
Thiago Santosb692f9f2009-12-12 16:07:15 -03002 * Copyright (C) 2008-2010 Thiago Santos <thiagoss@embedded.ufcg.edu.br>
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003 * Copyright (C) 2008 Mark Nauwelaerts <mnauw@users.sf.net>
Mark Nauwelaertsb852dde2011-01-03 10:43:15 +01004 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
5 * Contact: Stefan Kost <stefan.kost@nokia.com>
6
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00007 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
Tim-Philipp Müller230cf412012-11-04 00:07:18 +000019 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
Thiago Sousa Santosc991a042008-11-08 02:00:58 +000021 */
Mark Nauwelaerts9b0cbfe2008-12-19 18:33:47 +000022/*
23 * Unless otherwise indicated, Source Code is licensed under MIT license.
24 * See further explanation attached in License Statement (distributed in the file
25 * LICENSE).
26 *
27 * Permission is hereby granted, free of charge, to any person obtaining a copy of
28 * this software and associated documentation files (the "Software"), to deal in
29 * the Software without restriction, including without limitation the rights to
30 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
31 * of the Software, and to permit persons to whom the Software is furnished to do
32 * so, subject to the following conditions:
33 *
34 * The above copyright notice and this permission notice shall be included in all
35 * copies or substantial portions of the Software.
36 *
37 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
43 * SOFTWARE.
44 */
Thiago Sousa Santosc991a042008-11-08 02:00:58 +000045
46
47/**
Mark Nauwelaerts07be8102010-12-03 18:09:41 +010048 * SECTION:element-qtmux
Thiago Sousa Santosc991a042008-11-08 02:00:58 +000049 * @short_description: Muxer for quicktime(.mov) files
50 *
Mark Nauwelaerts07be8102010-12-03 18:09:41 +010051 * This element merges streams (audio and video) into QuickTime(.mov) files.
52 *
53 * The following background intends to explain why various similar muxers
54 * are present in this plugin.
55 *
56 * The <ulink url="http://www.apple.com/quicktime/resources/qtfileformat.pdf">
57 * QuickTime file format specification</ulink> served as basis for the MP4 file
58 * format specification (mp4mux), and as such the QuickTime file structure is
59 * nearly identical to the so-called ISO Base Media file format defined in
60 * ISO 14496-12 (except for some media specific parts).
61 * In turn, the latter ISO Base Media format was further specialized as a
62 * Motion JPEG-2000 file format in ISO 15444-3 (mj2mux)
63 * and in various 3GPP(2) specs (gppmux).
64 * The fragmented file features defined (only) in ISO Base Media are used by
65 * ISMV files making up (a.o.) Smooth Streaming (ismlmux).
66 *
Stefan Sauer35da4632014-02-18 21:44:24 +010067 * A few properties (#GstQTMux:movie-timescale, #GstQTMux:trak-timescale) allow
68 * adjusting some technical parameters, which might be useful in (rare) cases to
69 * resolve compatibility issues in some situations.
Mark Nauwelaertsb852dde2011-01-03 10:43:15 +010070 *
71 * Some other properties influence the result more fundamentally.
Stefan Sauer35da4632014-02-18 21:44:24 +010072 * A typical mov/mp4 file's metadata (aka moov) is located at the end of the
73 * file, somewhat contrary to this usually being called "the header".
74 * However, a #GstQTMux:faststart file will (with some effort) arrange this to
75 * be located near start of the file, which then allows it e.g. to be played
76 * while downloading. Alternatively, rather than having one chunk of metadata at
77 * start (or end), there can be some metadata at start and most of the other
78 * data can be spread out into fragments of #GstQTMux:fragment-duration.
Mark Nauwelaertsb852dde2011-01-03 10:43:15 +010079 * If such fragmented layout is intended for streaming purposes, then
Stefan Sauer35da4632014-02-18 21:44:24 +010080 * #GstQTMux:streamable allows foregoing to add index metadata (at the end of
81 * file).
Mark Nauwelaertsb852dde2011-01-03 10:43:15 +010082 *
Thiago Sousa Santosc991a042008-11-08 02:00:58 +000083 * <refsect2>
Thiago Sousa Santosc991a042008-11-08 02:00:58 +000084 * <title>Example pipelines</title>
Mark Nauwelaerts07be8102010-12-03 18:09:41 +010085 * |[
Tim-Philipp Müller4bb52bb2012-08-26 22:39:55 +010086 * gst-launch-1.0 v4l2src num-buffers=500 ! video/x-raw,width=320,height=240 ! videoconvert ! qtmux ! filesink location=video.mov
Mark Nauwelaerts07be8102010-12-03 18:09:41 +010087 * ]|
Thiago Sousa Santosc991a042008-11-08 02:00:58 +000088 * Records a video stream captured from a v4l2 device and muxes it into a qt file.
Thiago Sousa Santosc991a042008-11-08 02:00:58 +000089 * </refsect2>
Thiago Sousa Santosc991a042008-11-08 02:00:58 +000090 */
91
92/*
93 * Based on avimux
94 */
95
96#ifdef HAVE_CONFIG_H
97#include "config.h"
98#endif
99
100#include <glib/gstdio.h>
101
102#include <gst/gst.h>
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +0200103#include <gst/base/gstcollectpads.h>
Sebastian Dröge940807b2012-01-04 11:29:26 +0100104#include <gst/audio/audio.h>
Mark Nauwelaertsf8866812011-06-29 12:46:20 +0200105#include <gst/video/video.h>
Matej Knoppd8ac6662013-03-22 02:24:01 +0100106#include <gst/tag/tag.h>
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000107
Wim Taymans1fbca3b2009-03-23 11:17:39 +0100108#include <sys/types.h>
LRN61d88bd2009-03-20 14:20:16 +0100109#ifdef G_OS_WIN32
110#include <io.h> /* lseek, open, close, read */
111#undef lseek
112#define lseek _lseeki64
113#undef off_t
114#define off_t guint64
115#endif
116
David Hoyt62484ac2010-12-01 10:45:49 +0100117#ifdef _MSC_VER
118#define ftruncate g_win32_ftruncate
119#endif
120
Wim Taymans1fbca3b2009-03-23 11:17:39 +0100121#ifdef HAVE_UNISTD_H
122# include <unistd.h>
123#endif
124
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000125#include "gstqtmux.h"
126
127GST_DEBUG_CATEGORY_STATIC (gst_qt_mux_debug);
128#define GST_CAT_DEFAULT gst_qt_mux_debug
129
Jan Schmidtfe739b72015-04-01 11:15:38 +1100130/* Hacker notes.
131 *
132 * The basic building blocks of MP4 files are:
133 * - an 'ftyp' box at the very start
134 * - an 'mdat' box which contains the raw audio/video/subtitle data;
135 * this is just a bunch of bytes, completely unframed and possibly
136 * unordered with no additional meta-information
137 * - a 'moov' box that contains information about the different streams
138 * and what they contain, as well as sample tables for each stream
139 * that tell the demuxer where in the mdat box each buffer/sample is
140 * and what its duration/timestamp etc. is, and whether it's a
141 * keyframe etc.
142 * Additionally, fragmented MP4 works by writing chunks of data in
143 * pairs of 'moof' and 'mdat' boxes:
144 * - 'moof' boxes, header preceding each mdat fragment describing the
145 * contents, like a moov but only for that fragment.
146 * - a 'mfra' box for Fragmented MP4, which is written at the end and
147 * contains a summary of all fragments and seek tables.
148 *
149 * Currently mp4mux can work in 3 different modes / generate 3 types
150 * of output files/streams:
151 *
152 * - Normal mp4: mp4mux will write a little ftyp identifier at the
153 * beginning, then start an mdat box into which it will write all the
154 * sample data. At EOS it will then write the moov header with track
155 * headers and sample tables at the end of the file, and rewrite the
156 * start of the file to fix up the mdat box size at the beginning.
157 * It has to wait for EOS to write the moov (which includes the
158 * sample tables) because it doesn't know how much space those
159 * tables will be. The output downstream must be seekable to rewrite
160 * the mdat box at EOS.
161 *
162 * - Fragmented mp4: moov header with track headers at start
Jan Schmidt0c1bcc62015-04-08 01:41:18 +1000163 * but no sample table, followed by N fragments, each containing
Jan Schmidtfe739b72015-04-01 11:15:38 +1100164 * track headers with sample tables followed by some data. Downstream
165 * does not need to be seekable if the 'streamable' flag is TRUE,
166 * as the final mfra and total duration will be omitted.
167 *
168 * - Fast-start mp4: the goal here is to create a file where the moov
169 * headers are at the beginning; what mp4mux will do is write all
170 * sample data into a temp file and build moov header plus sample
171 * tables in memory and then when EOS comes, it will push out the
172 * moov header plus sample tables at the beginning, followed by the
173 * mdat sample data at the end which is read in from the temp file
174 * Files created in this mode are better for streaming over the
175 * network, since the client doesn't have to seek to the end of the
176 * file to get the headers, but it requires copying all sample data
177 * out of the temp file at EOS, which can be expensive. Downstream does
178 * not need to be seekable, because of the use of the temp file.
179 *
180 */
181
David Schleef6571e382013-03-22 13:24:33 -0700182#ifndef GST_REMOVE_DEPRECATED
Mark Nauwelaertsae644932011-01-03 17:24:23 +0100183enum
184{
185 DTS_METHOD_DD,
186 DTS_METHOD_REORDER,
187 DTS_METHOD_ASC
188};
189
190static GType
191gst_qt_mux_dts_method_get_type (void)
192{
193 static GType gst_qt_mux_dts_method = 0;
194
195 if (!gst_qt_mux_dts_method) {
196 static const GEnumValue dts_methods[] = {
197 {DTS_METHOD_DD, "delta/duration", "dd"},
198 {DTS_METHOD_REORDER, "reorder", "reorder"},
199 {DTS_METHOD_ASC, "ascending", "asc"},
200 {0, NULL, NULL},
201 };
202
203 gst_qt_mux_dts_method =
204 g_enum_register_static ("GstQTMuxDtsMethods", dts_methods);
205 }
206
207 return gst_qt_mux_dts_method;
208}
209
210#define GST_TYPE_QT_MUX_DTS_METHOD \
211 (gst_qt_mux_dts_method_get_type ())
David Schleef6571e382013-03-22 13:24:33 -0700212#endif
Mark Nauwelaertsae644932011-01-03 17:24:23 +0100213
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000214/* QTMux signals and args */
215enum
216{
217 /* FILL ME */
218 LAST_SIGNAL
219};
220
221enum
222{
223 PROP_0,
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000224 PROP_MOVIE_TIMESCALE,
Marc-André Lureaubee00aa2010-07-30 12:48:29 +0200225 PROP_TRAK_TIMESCALE,
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000226 PROP_FAST_START,
Thiago Santosb692f9f2009-12-12 16:07:15 -0300227 PROP_FAST_START_TEMP_FILE,
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +0100228 PROP_MOOV_RECOV_FILE,
Marc-André Lureau61964eb2010-08-06 13:26:27 +0200229 PROP_FRAGMENT_DURATION,
Mark Nauwelaertsae644932011-01-03 17:24:23 +0100230 PROP_STREAMABLE,
David Schleef6571e382013-03-22 13:24:33 -0700231#ifndef GST_REMOVE_DEPRECATED
Mark Nauwelaertsae644932011-01-03 17:24:23 +0100232 PROP_DTS_METHOD,
David Schleef6571e382013-03-22 13:24:33 -0700233#endif
Mark Nauwelaertsae644932011-01-03 17:24:23 +0100234 PROP_DO_CTTS,
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000235};
236
Mark Nauwelaertsc3f462d2009-06-11 15:54:42 +0200237/* some spare for header size as well */
238#define MDAT_LARGE_FILE_LIMIT ((guint64) 1024 * 1024 * 1024 * 2)
239
Jan Schmidt6e7930a2014-08-15 10:09:56 +1000240#define DEFAULT_MOVIE_TIMESCALE 1800
Marc-André Lureaubee00aa2010-07-30 12:48:29 +0200241#define DEFAULT_TRAK_TIMESCALE 0
Tim-Philipp Müllerc8b50142011-04-13 12:37:09 +0100242#define DEFAULT_DO_CTTS TRUE
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000243#define DEFAULT_FAST_START FALSE
244#define DEFAULT_FAST_START_TEMP_FILE NULL
Thiago Santosb692f9f2009-12-12 16:07:15 -0300245#define DEFAULT_MOOV_RECOV_FILE NULL
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +0100246#define DEFAULT_FRAGMENT_DURATION 0
Andoni Morales Alastruey9ae50822013-09-17 12:06:06 +0200247#define DEFAULT_STREAMABLE TRUE
David Schleef6571e382013-03-22 13:24:33 -0700248#ifndef GST_REMOVE_DEPRECATED
Tim-Philipp Müllerc8b50142011-04-13 12:37:09 +0100249#define DEFAULT_DTS_METHOD DTS_METHOD_REORDER
David Schleef6571e382013-03-22 13:24:33 -0700250#endif
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +0100251
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000252static void gst_qt_mux_finalize (GObject * object);
253
254static GstStateChangeReturn gst_qt_mux_change_state (GstElement * element,
255 GstStateChange transition);
256
257/* property functions */
258static void gst_qt_mux_set_property (GObject * object,
259 guint prop_id, const GValue * value, GParamSpec * pspec);
260static void gst_qt_mux_get_property (GObject * object,
261 guint prop_id, GValue * value, GParamSpec * pspec);
262
263/* pad functions */
264static GstPad *gst_qt_mux_request_new_pad (GstElement * element,
Mark Nauwelaertsf8866812011-06-29 12:46:20 +0200265 GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000266static void gst_qt_mux_release_pad (GstElement * element, GstPad * pad);
267
268/* event */
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +0200269static gboolean gst_qt_mux_sink_event (GstCollectPads * pads,
270 GstCollectData * data, GstEvent * event, gpointer user_data);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000271
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +0200272static GstFlowReturn gst_qt_mux_handle_buffer (GstCollectPads * pads,
273 GstCollectData * cdata, GstBuffer * buf, gpointer user_data);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000274static GstFlowReturn gst_qt_mux_add_buffer (GstQTMux * qtmux, GstQTPad * pad,
275 GstBuffer * buf);
276
277static GstElementClass *parent_class = NULL;
278
279static void
280gst_qt_mux_base_init (gpointer g_class)
281{
282 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
283 GstQTMuxClass *klass = (GstQTMuxClass *) g_class;
284 GstQTMuxClassParams *params;
Thiago Santos2ae18972014-02-05 10:27:54 -0300285 GstPadTemplate *videosinktempl, *audiosinktempl, *subtitlesinktempl;
286 GstPadTemplate *srctempl;
Benjamin Otteaf347a92010-03-18 17:30:26 +0100287 gchar *longname, *description;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000288
289 params =
290 (GstQTMuxClassParams *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (g_class),
291 GST_QT_MUX_PARAMS_QDATA);
292 g_assert (params != NULL);
293
294 /* construct the element details struct */
Benjamin Otteaf347a92010-03-18 17:30:26 +0100295 longname = g_strdup_printf ("%s Muxer", params->prop->long_name);
Tim-Philipp Müllercf9b2142012-07-06 12:54:02 +0100296 description = g_strdup_printf ("Multiplex audio and video into a %s file",
297 params->prop->long_name);
Tim-Philipp Müllere09ae572012-04-10 00:51:41 +0100298 gst_element_class_set_static_metadata (element_class, longname,
Benjamin Otteaf347a92010-03-18 17:30:26 +0100299 "Codec/Muxer", description,
300 "Thiago Sousa Santos <thiagoss@embedded.ufcg.edu.br>");
301 g_free (longname);
302 g_free (description);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000303
304 /* pad templates */
305 srctempl = gst_pad_template_new ("src", GST_PAD_SRC,
306 GST_PAD_ALWAYS, params->src_caps);
307 gst_element_class_add_pad_template (element_class, srctempl);
308
309 if (params->audio_sink_caps) {
Wim Taymansa95acb72011-11-04 11:58:22 +0100310 audiosinktempl = gst_pad_template_new ("audio_%u",
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000311 GST_PAD_SINK, GST_PAD_REQUEST, params->audio_sink_caps);
312 gst_element_class_add_pad_template (element_class, audiosinktempl);
313 }
314
315 if (params->video_sink_caps) {
Wim Taymansa95acb72011-11-04 11:58:22 +0100316 videosinktempl = gst_pad_template_new ("video_%u",
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000317 GST_PAD_SINK, GST_PAD_REQUEST, params->video_sink_caps);
318 gst_element_class_add_pad_template (element_class, videosinktempl);
319 }
320
Thiago Santos2ae18972014-02-05 10:27:54 -0300321 if (params->subtitle_sink_caps) {
322 subtitlesinktempl = gst_pad_template_new ("subtitle_%u",
323 GST_PAD_SINK, GST_PAD_REQUEST, params->subtitle_sink_caps);
324 gst_element_class_add_pad_template (element_class, subtitlesinktempl);
325 }
326
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000327 klass->format = params->prop->format;
328}
329
330static void
331gst_qt_mux_class_init (GstQTMuxClass * klass)
332{
333 GObjectClass *gobject_class;
334 GstElementClass *gstelement_class;
Tim-Philipp Müller3956f5a2014-11-02 16:51:23 +0000335 GParamFlags streamable_flags;
Andoni Morales Alastruey57326842013-09-17 12:01:30 +0200336 const gchar *streamable_desc;
Andoni Morales Alastruey9ae50822013-09-17 12:06:06 +0200337 gboolean streamable;
Andoni Morales Alastruey57326842013-09-17 12:01:30 +0200338#define STREAMABLE_DESC "If set to true, the output should be as if it is to "\
339 "be streamed and hence no indexes written or duration written."
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000340
341 gobject_class = (GObjectClass *) klass;
342 gstelement_class = (GstElementClass *) klass;
343
344 parent_class = g_type_class_peek_parent (klass);
345
346 gobject_class->finalize = gst_qt_mux_finalize;
347 gobject_class->get_property = gst_qt_mux_get_property;
348 gobject_class->set_property = gst_qt_mux_set_property;
349
Tim-Philipp Müller3956f5a2014-11-02 16:51:23 +0000350 streamable_flags = G_PARAM_READWRITE | G_PARAM_CONSTRUCT;
Andoni Morales Alastruey57326842013-09-17 12:01:30 +0200351 if (klass->format == GST_QT_MUX_FORMAT_ISML) {
352 streamable_desc = STREAMABLE_DESC;
Andoni Morales Alastruey9ae50822013-09-17 12:06:06 +0200353 streamable = DEFAULT_STREAMABLE;
Andoni Morales Alastruey57326842013-09-17 12:01:30 +0200354 } else {
355 streamable_desc =
356 STREAMABLE_DESC " (DEPRECATED, only valid for fragmented MP4)";
Tim-Philipp Müller3956f5a2014-11-02 16:51:23 +0000357 streamable_flags |= G_PARAM_DEPRECATED;
Andoni Morales Alastruey9ae50822013-09-17 12:06:06 +0200358 streamable = FALSE;
Andoni Morales Alastruey57326842013-09-17 12:01:30 +0200359 }
360
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000361 g_object_class_install_property (gobject_class, PROP_MOVIE_TIMESCALE,
362 g_param_spec_uint ("movie-timescale", "Movie timescale",
363 "Timescale to use in the movie (units per second)",
364 1, G_MAXUINT32, DEFAULT_MOVIE_TIMESCALE,
Thiago Santos5ccc0662010-10-03 23:45:46 -0300365 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
Marc-André Lureaubee00aa2010-07-30 12:48:29 +0200366 g_object_class_install_property (gobject_class, PROP_TRAK_TIMESCALE,
367 g_param_spec_uint ("trak-timescale", "Track timescale",
368 "Timescale to use for the tracks (units per second, 0 is automatic)",
369 0, G_MAXUINT32, DEFAULT_TRAK_TIMESCALE,
370 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000371 g_object_class_install_property (gobject_class, PROP_DO_CTTS,
372 g_param_spec_boolean ("presentation-time",
373 "Include presentation-time info",
Thiago Sousa Santosd24d1e42009-11-17 09:26:05 -0300374 "Calculate and include presentation/composition time "
Tim-Philipp Müllerc8b50142011-04-13 12:37:09 +0100375 "(in addition to decoding time)", DEFAULT_DO_CTTS,
Thiago Santos5ccc0662010-10-03 23:45:46 -0300376 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
David Schleef6571e382013-03-22 13:24:33 -0700377#ifndef GST_REMOVE_DEPRECATED
Mark Nauwelaertsae644932011-01-03 17:24:23 +0100378 g_object_class_install_property (gobject_class, PROP_DTS_METHOD,
379 g_param_spec_enum ("dts-method", "dts-method",
Tim-Philipp Müller3956f5a2014-11-02 16:51:23 +0000380 "Method to determine DTS time (DEPRECATED)",
Mark Nauwelaertsae644932011-01-03 17:24:23 +0100381 GST_TYPE_QT_MUX_DTS_METHOD, DEFAULT_DTS_METHOD,
David Schleef6571e382013-03-22 13:24:33 -0700382 G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
383 G_PARAM_STATIC_STRINGS));
384#endif
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000385 g_object_class_install_property (gobject_class, PROP_FAST_START,
386 g_param_spec_boolean ("faststart", "Format file to faststart",
Tim-Philipp Müllerc8b50142011-04-13 12:37:09 +0100387 "If the file should be formatted for faststart (headers first)",
Thiago Santos5ccc0662010-10-03 23:45:46 -0300388 DEFAULT_FAST_START, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000389 g_object_class_install_property (gobject_class, PROP_FAST_START_TEMP_FILE,
390 g_param_spec_string ("faststart-file", "File to use for storing buffers",
Thiago Sousa Santosd24d1e42009-11-17 09:26:05 -0300391 "File that will be used temporarily to store data from the stream "
392 "when creating a faststart file. If null a filepath will be "
393 "created automatically", DEFAULT_FAST_START_TEMP_FILE,
Thiago Santos5ccc0662010-10-03 23:45:46 -0300394 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
Thiago Santosb692f9f2009-12-12 16:07:15 -0300395 g_object_class_install_property (gobject_class, PROP_MOOV_RECOV_FILE,
Thiago Santos5ccc0662010-10-03 23:45:46 -0300396 g_param_spec_string ("moov-recovery-file",
397 "File to store data for posterior moov atom recovery",
398 "File to be used to store "
Thiago Santosb692f9f2009-12-12 16:07:15 -0300399 "data for moov atom making movie file recovery possible in case "
400 "of a crash during muxing. Null for disabled. (Experimental)",
Thiago Santos5ccc0662010-10-03 23:45:46 -0300401 DEFAULT_MOOV_RECOV_FILE,
402 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +0100403 g_object_class_install_property (gobject_class, PROP_FRAGMENT_DURATION,
404 g_param_spec_uint ("fragment-duration", "Fragment duration",
405 "Fragment durations in ms (produce a fragmented file if > 0)",
Mark Nauwelaertsf490e3a2010-11-19 19:17:45 +0100406 0, G_MAXUINT32, klass->format == GST_QT_MUX_FORMAT_ISML ?
407 2000 : DEFAULT_FRAGMENT_DURATION,
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +0100408 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
Marc-André Lureau61964eb2010-08-06 13:26:27 +0200409 g_object_class_install_property (gobject_class, PROP_STREAMABLE,
Andoni Morales Alastruey57326842013-09-17 12:01:30 +0200410 g_param_spec_boolean ("streamable", "Streamable", streamable_desc,
Tim-Philipp Müller3956f5a2014-11-02 16:51:23 +0000411 streamable, streamable_flags | G_PARAM_STATIC_STRINGS));
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000412
413 gstelement_class->request_new_pad =
414 GST_DEBUG_FUNCPTR (gst_qt_mux_request_new_pad);
415 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_qt_mux_change_state);
416 gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_qt_mux_release_pad);
417}
418
419static void
420gst_qt_mux_pad_reset (GstQTPad * qtpad)
421{
422 qtpad->fourcc = 0;
423 qtpad->is_out_of_order = FALSE;
424 qtpad->sample_size = 0;
425 qtpad->sync = FALSE;
426 qtpad->last_dts = 0;
Thiago Santos8d80e932009-11-06 10:34:39 -0300427 qtpad->first_ts = GST_CLOCK_TIME_NONE;
Thiago Santosc5f6e742009-12-10 22:20:45 -0300428 qtpad->prepare_buf_func = NULL;
Thiago Santosf89ba822014-02-07 01:49:26 -0300429 qtpad->create_empty_buffer = NULL;
Arun Raghavan4b4398c2010-07-05 14:09:50 +0530430 qtpad->avg_bitrate = 0;
431 qtpad->max_bitrate = 0;
Thiago Santos7a143ea2011-09-28 11:41:49 -0300432 qtpad->total_duration = 0;
433 qtpad->total_bytes = 0;
Thiago Santosf89ba822014-02-07 01:49:26 -0300434 qtpad->sparse = FALSE;
Mark Nauwelaertsae644932011-01-03 17:24:23 +0100435
436 qtpad->buf_head = 0;
437 qtpad->buf_tail = 0;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000438
Thiago Santos84b7cf62015-02-18 10:10:53 -0300439 gst_buffer_replace (&qtpad->last_buf, NULL);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000440
Thiago Santose52b2cb2015-01-31 14:32:34 -0300441 if (qtpad->tags) {
442 gst_tag_list_unref (qtpad->tags);
443 qtpad->tags = NULL;
444 }
445
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000446 /* reference owned elsewhere */
447 qtpad->trak = NULL;
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +0100448
449 if (qtpad->traf) {
450 atom_traf_free (qtpad->traf);
451 qtpad->traf = NULL;
452 }
453 atom_array_clear (&qtpad->fragment_buffers);
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +0100454
455 /* reference owned elsewhere */
456 qtpad->tfra = NULL;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000457}
458
459/*
460 * Takes GstQTMux back to its initial state
461 */
462static void
463gst_qt_mux_reset (GstQTMux * qtmux, gboolean alloc)
464{
465 GSList *walk;
466
467 qtmux->state = GST_QT_MUX_STATE_NONE;
468 qtmux->header_size = 0;
469 qtmux->mdat_size = 0;
470 qtmux->mdat_pos = 0;
Thiago Santos4ff2fad2009-10-16 10:47:32 -0300471 qtmux->longest_chunk = GST_CLOCK_TIME_NONE;
Stefan Kost05257fe2009-12-27 23:51:50 +0200472 qtmux->video_pads = 0;
473 qtmux->audio_pads = 0;
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +0100474 qtmux->fragment_sequence = 0;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000475
476 if (qtmux->ftyp) {
477 atom_ftyp_free (qtmux->ftyp);
478 qtmux->ftyp = NULL;
479 }
480 if (qtmux->moov) {
481 atom_moov_free (qtmux->moov);
482 qtmux->moov = NULL;
483 }
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +0100484 if (qtmux->mfra) {
485 atom_mfra_free (qtmux->mfra);
486 qtmux->mfra = NULL;
487 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000488 if (qtmux->fast_start_file) {
489 fclose (qtmux->fast_start_file);
Marc-André Lureau3e6a4152009-09-28 16:53:51 +0200490 g_remove (qtmux->fast_start_file_path);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000491 qtmux->fast_start_file = NULL;
492 }
Thiago Santosb692f9f2009-12-12 16:07:15 -0300493 if (qtmux->moov_recov_file) {
494 fclose (qtmux->moov_recov_file);
495 qtmux->moov_recov_file = NULL;
496 }
Thiago Santos7065a652010-09-15 17:54:49 -0300497 for (walk = qtmux->extra_atoms; walk; walk = g_slist_next (walk)) {
498 AtomInfo *ainfo = (AtomInfo *) walk->data;
499 ainfo->free_func (ainfo->atom);
Sebastian Dröged8a690e2010-12-19 12:53:34 +0100500 g_free (ainfo);
Thiago Santos7065a652010-09-15 17:54:49 -0300501 }
502 g_slist_free (qtmux->extra_atoms);
503 qtmux->extra_atoms = NULL;
Michael Smith8b3aca92010-02-26 11:50:25 -0800504
505 GST_OBJECT_LOCK (qtmux);
Stefan Kost6a47f1f2008-12-16 16:26:52 +0000506 gst_tag_setter_reset_tags (GST_TAG_SETTER (qtmux));
Michael Smith8b3aca92010-02-26 11:50:25 -0800507 GST_OBJECT_UNLOCK (qtmux);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000508
509 /* reset pad data */
Thiago Santosc92785e2010-02-22 10:48:11 -0300510 for (walk = qtmux->sinkpads; walk; walk = g_slist_next (walk)) {
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000511 GstQTPad *qtpad = (GstQTPad *) walk->data;
512 gst_qt_mux_pad_reset (qtpad);
513
514 /* hm, moov_free above yanked the traks away from us,
515 * so do not free, but do clear */
516 qtpad->trak = NULL;
517 }
518
519 if (alloc) {
520 qtmux->moov = atom_moov_new (qtmux->context);
Mark Nauwelaerts4da6cd72009-03-25 21:24:44 +0100521 /* ensure all is as nice and fresh as request_new_pad would provide it */
Thiago Santosc92785e2010-02-22 10:48:11 -0300522 for (walk = qtmux->sinkpads; walk; walk = g_slist_next (walk)) {
Mark Nauwelaerts4da6cd72009-03-25 21:24:44 +0100523 GstQTPad *qtpad = (GstQTPad *) walk->data;
524
Mark Nauwelaertsce865cc2010-11-05 12:08:15 +0100525 qtpad->trak = atom_trak_new (qtmux->context);
526 atom_moov_add_trak (qtmux->moov, qtpad->trak);
Mark Nauwelaerts4da6cd72009-03-25 21:24:44 +0100527 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000528 }
529}
530
531static void
532gst_qt_mux_init (GstQTMux * qtmux, GstQTMuxClass * qtmux_klass)
533{
534 GstElementClass *klass = GST_ELEMENT_CLASS (qtmux_klass);
535 GstPadTemplate *templ;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000536
537 templ = gst_element_class_get_pad_template (klass, "src");
538 qtmux->srcpad = gst_pad_new_from_template (templ, "src");
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000539 gst_pad_use_fixed_caps (qtmux->srcpad);
540 gst_element_add_pad (GST_ELEMENT (qtmux), qtmux->srcpad);
541
Thiago Santosc92785e2010-02-22 10:48:11 -0300542 qtmux->sinkpads = NULL;
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +0200543 qtmux->collect = gst_collect_pads_new ();
544 gst_collect_pads_set_buffer_function (qtmux->collect,
Mark Nauwelaerts0c7a4912011-12-16 18:03:01 +0100545 GST_DEBUG_FUNCPTR (gst_qt_mux_handle_buffer), qtmux);
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +0200546 gst_collect_pads_set_event_function (qtmux->collect,
Mark Nauwelaerts0c7a4912011-12-16 18:03:01 +0100547 GST_DEBUG_FUNCPTR (gst_qt_mux_sink_event), qtmux);
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +0200548 gst_collect_pads_set_clip_function (qtmux->collect,
549 GST_DEBUG_FUNCPTR (gst_collect_pads_clip_running_time), qtmux);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000550
551 /* properties set to default upon construction */
552
553 /* always need this */
554 qtmux->context =
555 atoms_context_new (gst_qt_mux_map_format_to_flavor (qtmux_klass->format));
556
557 /* internals to initial state */
558 gst_qt_mux_reset (qtmux, TRUE);
559}
560
561
562static void
563gst_qt_mux_finalize (GObject * object)
564{
565 GstQTMux *qtmux = GST_QT_MUX_CAST (object);
566
567 gst_qt_mux_reset (qtmux, FALSE);
568
Thiago Santos6dc515b2010-01-26 13:44:04 -0300569 g_free (qtmux->fast_start_file_path);
Thiago Santosb692f9f2009-12-12 16:07:15 -0300570 g_free (qtmux->moov_recov_file_path);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000571
572 atoms_context_free (qtmux->context);
573 gst_object_unref (qtmux->collect);
574
Mark Nauwelaerts50464d52010-11-19 14:44:45 +0100575 g_slist_free (qtmux->sinkpads);
576
Thiago Sousa Santosc991a042008-11-08 02:00:58 +0000577 G_OBJECT_CLASS (parent_class)->finalize (object);
578}
579
Thiago Santosc5f6e742009-12-10 22:20:45 -0300580static GstBuffer *
581gst_qt_mux_prepare_jpc_buffer (GstQTPad * qtpad, GstBuffer * buf,
582 GstQTMux * qtmux)
583{
584 GstBuffer *newbuf;
Wim Taymans583d39d2012-01-23 17:25:37 +0100585 GstMapInfo map;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +0200586 gsize size;
Thiago Santosc5f6e742009-12-10 22:20:45 -0300587
588 GST_LOG_OBJECT (qtmux, "Preparing jpc buffer");
589
590 if (buf == NULL)
591 return NULL;
592
Mark Nauwelaertsf8866812011-06-29 12:46:20 +0200593 size = gst_buffer_get_size (buf);
594 newbuf = gst_buffer_new_and_alloc (size + 8);
595 gst_buffer_copy_into (newbuf, buf, GST_BUFFER_COPY_ALL, 8, size);
Thiago Santosc5f6e742009-12-10 22:20:45 -0300596
Wim Taymans583d39d2012-01-23 17:25:37 +0100597 gst_buffer_map (newbuf, &map, GST_MAP_WRITE);
598 GST_WRITE_UINT32_BE (map.data, map.size);
599 GST_WRITE_UINT32_LE (map.data + 4, FOURCC_jp2c);
Thiago Santosc5f6e742009-12-10 22:20:45 -0300600
Wim Taymans583d39d2012-01-23 17:25:37 +0100601 gst_buffer_unmap (buf, &map);
Thiago Santosc5f6e742009-12-10 22:20:45 -0300602 gst_buffer_unref (buf);
603
604 return newbuf;
605}
606
Thiago Santos99e966e2014-02-06 12:15:22 -0300607static GstBuffer *
608gst_qt_mux_prepare_tx3g_buffer (GstQTPad * qtpad, GstBuffer * buf,
609 GstQTMux * qtmux)
610{
611 GstBuffer *newbuf;
612 GstMapInfo frommap;
613 GstMapInfo tomap;
614 gsize size;
615
616 GST_LOG_OBJECT (qtmux, "Preparing tx3g buffer %" GST_PTR_FORMAT, buf);
617
618 if (buf == NULL)
619 return NULL;
620
Matej Knopp66953412014-09-23 19:07:25 +0200621 gst_buffer_map (buf, &frommap, GST_MAP_READ);
622
623 size = (gint16) strnlen ((const char *) frommap.data, frommap.size);
Thiago Santos99e966e2014-02-06 12:15:22 -0300624 newbuf = gst_buffer_new_and_alloc (size + 2);
625
Thiago Santos99e966e2014-02-06 12:15:22 -0300626 gst_buffer_map (newbuf, &tomap, GST_MAP_WRITE);
627
628 GST_WRITE_UINT16_BE (tomap.data, size);
629 memcpy (tomap.data + 2, frommap.data, size);
630
631 gst_buffer_unmap (newbuf, &tomap);
632 gst_buffer_unmap (buf, &frommap);
633
634 gst_buffer_copy_into (newbuf, buf, GST_BUFFER_COPY_METADATA, 0, size);
635
Matej Knopp66953412014-09-23 19:07:25 +0200636 /* gst_buffer_copy_into is trying to be too clever and
637 * won't copy duration when size is different */
638 GST_BUFFER_DURATION (newbuf) = GST_BUFFER_DURATION (buf);
639
Thiago Santos99e966e2014-02-06 12:15:22 -0300640 gst_buffer_unref (buf);
641
642 return newbuf;
643}
644
Thiago Santosf89ba822014-02-07 01:49:26 -0300645static GstBuffer *
646gst_qt_mux_create_empty_tx3g_buffer (GstQTPad * qtpad, gint64 duration)
647{
648 guint8 *data;
649
650 data = g_malloc (2);
651 GST_WRITE_UINT16_BE (data, 0);
652
Matej Knopp66953412014-09-23 19:07:25 +0200653 return gst_buffer_new_wrapped (data, 2);
Thiago Santosf89ba822014-02-07 01:49:26 -0300654}
655
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200656static void
657gst_qt_mux_add_mp4_tag (GstQTMux * qtmux, const GstTagList * list,
Thiago Santos6321cde2015-01-31 13:14:44 -0300658 AtomUDTA * udta, const char *tag, const char *tag2, guint32 fourcc)
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200659{
660 switch (gst_tag_get_type (tag)) {
661 /* strings */
662 case G_TYPE_STRING:
663 {
664 gchar *str = NULL;
665
666 if (!gst_tag_list_get_string (list, tag, &str) || !str)
667 break;
668 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
669 GST_FOURCC_ARGS (fourcc), str);
Thiago Santos6321cde2015-01-31 13:14:44 -0300670 atom_udta_add_str_tag (udta, fourcc, str);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200671 g_free (str);
672 break;
673 }
674 /* double */
675 case G_TYPE_DOUBLE:
676 {
677 gdouble value;
678
679 if (!gst_tag_list_get_double (list, tag, &value))
680 break;
681 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %u",
682 GST_FOURCC_ARGS (fourcc), (gint) value);
Thiago Santos6321cde2015-01-31 13:14:44 -0300683 atom_udta_add_uint_tag (udta, fourcc, 21, (gint) value);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200684 break;
685 }
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200686 case G_TYPE_UINT:
687 {
Thiago Santosac7f4cb2010-06-25 20:19:20 -0300688 guint value = 0;
Thiago Santos592171b2009-12-21 13:58:30 -0300689 if (tag2) {
690 /* paired unsigned integers */
Thiago Santosac7f4cb2010-06-25 20:19:20 -0300691 guint count = 0;
Mark Nauwelaerts57684fe2011-10-31 16:18:32 +0100692 gboolean got_tag;
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200693
Mark Nauwelaerts57684fe2011-10-31 16:18:32 +0100694 got_tag = gst_tag_list_get_uint (list, tag, &value);
695 got_tag = gst_tag_list_get_uint (list, tag2, &count) || got_tag;
696 if (!got_tag)
Thiago Santos592171b2009-12-21 13:58:30 -0300697 break;
698 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %u/%u",
699 GST_FOURCC_ARGS (fourcc), value, count);
Thiago Santos6321cde2015-01-31 13:14:44 -0300700 atom_udta_add_uint_tag (udta, fourcc, 0,
Thiago Santos592171b2009-12-21 13:58:30 -0300701 value << 16 | (count & 0xFFFF));
702 } else {
703 /* unpaired unsigned integers */
704 if (!gst_tag_list_get_uint (list, tag, &value))
705 break;
706 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %u",
707 GST_FOURCC_ARGS (fourcc), value);
Thiago Santos6321cde2015-01-31 13:14:44 -0300708 atom_udta_add_uint_tag (udta, fourcc, 1, value);
Thiago Santos592171b2009-12-21 13:58:30 -0300709 }
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200710 break;
711 }
712 default:
713 g_assert_not_reached ();
714 break;
715 }
716}
717
718static void
719gst_qt_mux_add_mp4_date (GstQTMux * qtmux, const GstTagList * list,
Thiago Santos6321cde2015-01-31 13:14:44 -0300720 AtomUDTA * udta, const char *tag, const char *tag2, guint32 fourcc)
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200721{
722 GDate *date = NULL;
723 GDateYear year;
724 GDateMonth month;
725 GDateDay day;
726 gchar *str;
727
Tim-Philipp Müllerd681af82012-01-12 23:55:31 +0000728 g_return_if_fail (gst_tag_get_type (tag) == G_TYPE_DATE);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200729
730 if (!gst_tag_list_get_date (list, tag, &date) || !date)
731 return;
732
733 year = g_date_get_year (date);
734 month = g_date_get_month (date);
735 day = g_date_get_day (date);
736
Mark Nauwelaertscc526d22011-06-29 12:05:04 +0200737 g_date_free (date);
738
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200739 if (year == G_DATE_BAD_YEAR && month == G_DATE_BAD_MONTH &&
740 day == G_DATE_BAD_DAY) {
741 GST_WARNING_OBJECT (qtmux, "invalid date in tag");
742 return;
743 }
744
745 str = g_strdup_printf ("%u-%u-%u", year, month, day);
746 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
747 GST_FOURCC_ARGS (fourcc), str);
Thiago Santos6321cde2015-01-31 13:14:44 -0300748 atom_udta_add_str_tag (udta, fourcc, str);
Thiago Sousa Santosd24d1e42009-11-17 09:26:05 -0300749 g_free (str);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200750}
751
752static void
753gst_qt_mux_add_mp4_cover (GstQTMux * qtmux, const GstTagList * list,
Thiago Santos6321cde2015-01-31 13:14:44 -0300754 AtomUDTA * udta, const char *tag, const char *tag2, guint32 fourcc)
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200755{
756 GValue value = { 0, };
757 GstBuffer *buf;
Matej Knopp688e8202012-03-07 04:20:00 -0500758 GstSample *sample;
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200759 GstCaps *caps;
760 GstStructure *structure;
761 gint flags = 0;
Wim Taymans583d39d2012-01-23 17:25:37 +0100762 GstMapInfo map;
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200763
Matej Knopp688e8202012-03-07 04:20:00 -0500764 g_return_if_fail (gst_tag_get_type (tag) == GST_TYPE_SAMPLE);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200765
766 if (!gst_tag_list_copy_value (&value, list, tag))
767 return;
768
Matej Knopp688e8202012-03-07 04:20:00 -0500769 sample = gst_value_get_sample (&value);
770
771 if (!sample)
772 goto done;
773
774 buf = gst_sample_get_buffer (sample);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200775 if (!buf)
776 goto done;
777
Matej Knopp688e8202012-03-07 04:20:00 -0500778 caps = gst_sample_get_caps (sample);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200779 if (!caps) {
780 GST_WARNING_OBJECT (qtmux, "preview image without caps");
781 goto done;
782 }
783
784 GST_DEBUG_OBJECT (qtmux, "preview image caps %" GST_PTR_FORMAT, caps);
785
786 structure = gst_caps_get_structure (caps, 0);
787 if (gst_structure_has_name (structure, "image/jpeg"))
788 flags = 13;
789 else if (gst_structure_has_name (structure, "image/png"))
790 flags = 14;
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200791
792 if (!flags) {
793 GST_WARNING_OBJECT (qtmux, "preview image format not supported");
794 goto done;
795 }
796
Wim Taymans583d39d2012-01-23 17:25:37 +0100797 gst_buffer_map (buf, &map, GST_MAP_READ);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200798 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT
Wim Taymans583d39d2012-01-23 17:25:37 +0100799 " -> image size %" G_GSIZE_FORMAT "", GST_FOURCC_ARGS (fourcc), map.size);
Thiago Santos6321cde2015-01-31 13:14:44 -0300800 atom_udta_add_tag (udta, fourcc, flags, map.data, map.size);
Wim Taymans583d39d2012-01-23 17:25:37 +0100801 gst_buffer_unmap (buf, &map);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200802done:
803 g_value_unset (&value);
804}
805
806static void
807gst_qt_mux_add_3gp_str (GstQTMux * qtmux, const GstTagList * list,
Thiago Santos6321cde2015-01-31 13:14:44 -0300808 AtomUDTA * udta, const char *tag, const char *tag2, guint32 fourcc)
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200809{
810 gchar *str = NULL;
811 guint number;
812
813 g_return_if_fail (gst_tag_get_type (tag) == G_TYPE_STRING);
814 g_return_if_fail (!tag2 || gst_tag_get_type (tag2) == G_TYPE_UINT);
815
816 if (!gst_tag_list_get_string (list, tag, &str) || !str)
817 return;
818
819 if (tag2)
820 if (!gst_tag_list_get_uint (list, tag2, &number))
821 tag2 = NULL;
822
823 if (!tag2) {
824 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
825 GST_FOURCC_ARGS (fourcc), str);
Thiago Santos6321cde2015-01-31 13:14:44 -0300826 atom_udta_add_3gp_str_tag (udta, fourcc, str);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200827 } else {
828 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s/%d",
829 GST_FOURCC_ARGS (fourcc), str, number);
Thiago Santos6321cde2015-01-31 13:14:44 -0300830 atom_udta_add_3gp_str_int_tag (udta, fourcc, str, number);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200831 }
832
833 g_free (str);
834}
835
836static void
837gst_qt_mux_add_3gp_date (GstQTMux * qtmux, const GstTagList * list,
Thiago Santos6321cde2015-01-31 13:14:44 -0300838 AtomUDTA * udta, const char *tag, const char *tag2, guint32 fourcc)
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200839{
840 GDate *date = NULL;
841 GDateYear year;
842
Tim-Philipp Müllerd681af82012-01-12 23:55:31 +0000843 g_return_if_fail (gst_tag_get_type (tag) == G_TYPE_DATE);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200844
845 if (!gst_tag_list_get_date (list, tag, &date) || !date)
846 return;
847
848 year = g_date_get_year (date);
849
850 if (year == G_DATE_BAD_YEAR) {
851 GST_WARNING_OBJECT (qtmux, "invalid date in tag");
852 return;
853 }
854
Edward Hervey430953e2009-06-25 08:38:10 +0200855 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %d",
856 GST_FOURCC_ARGS (fourcc), year);
Thiago Santos6321cde2015-01-31 13:14:44 -0300857 atom_udta_add_3gp_uint_tag (udta, fourcc, year);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200858}
859
860static void
861gst_qt_mux_add_3gp_location (GstQTMux * qtmux, const GstTagList * list,
Thiago Santos6321cde2015-01-31 13:14:44 -0300862 AtomUDTA * udta, const char *tag, const char *tag2, guint32 fourcc)
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200863{
864 gdouble latitude = -360, longitude = -360, altitude = 0;
865 gchar *location = NULL;
866 guint8 *data, *ddata;
867 gint size = 0, len = 0;
868 gboolean ret = FALSE;
869
870 g_return_if_fail (strcmp (tag, GST_TAG_GEO_LOCATION_NAME) == 0);
871
872 ret = gst_tag_list_get_string (list, tag, &location);
873 ret |= gst_tag_list_get_double (list, GST_TAG_GEO_LOCATION_LONGITUDE,
874 &longitude);
875 ret |= gst_tag_list_get_double (list, GST_TAG_GEO_LOCATION_LATITUDE,
876 &latitude);
877 ret |= gst_tag_list_get_double (list, GST_TAG_GEO_LOCATION_ELEVATION,
878 &altitude);
879
880 if (!ret)
881 return;
882
883 if (location)
884 len = strlen (location);
885 size += len + 1 + 2;
886
887 /* role + (long, lat, alt) + body + notes */
888 size += 1 + 3 * 4 + 1 + 1;
889
890 data = ddata = g_malloc (size);
891
892 /* language tag */
893 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
894 /* location */
895 if (location)
896 memcpy (data + 2, location, len);
897 GST_WRITE_UINT8 (data + 2 + len, 0);
898 data += len + 1 + 2;
899 /* role */
900 GST_WRITE_UINT8 (data, 0);
901 /* long, lat, alt */
Edward Herveycca48a42011-05-05 13:24:23 +0200902#define QT_WRITE_SFP32(data, fp) GST_WRITE_UINT32_BE(data, (guint32) ((gint) (fp * 65536.0)))
903 QT_WRITE_SFP32 (data + 1, longitude);
904 QT_WRITE_SFP32 (data + 5, latitude);
905 QT_WRITE_SFP32 (data + 9, altitude);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200906 /* neither astronomical body nor notes */
907 GST_WRITE_UINT16_BE (data + 13, 0);
908
909 GST_DEBUG_OBJECT (qtmux, "Adding tag 'loci'");
Thiago Santos6321cde2015-01-31 13:14:44 -0300910 atom_udta_add_3gp_tag (udta, fourcc, ddata, size);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200911 g_free (ddata);
912}
913
914static void
915gst_qt_mux_add_3gp_keywords (GstQTMux * qtmux, const GstTagList * list,
Thiago Santos6321cde2015-01-31 13:14:44 -0300916 AtomUDTA * udta, const char *tag, const char *tag2, guint32 fourcc)
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200917{
918 gchar *keywords = NULL;
919 guint8 *data, *ddata;
920 gint size = 0, i;
921 gchar **kwds;
922
923 g_return_if_fail (strcmp (tag, GST_TAG_KEYWORDS) == 0);
924
925 if (!gst_tag_list_get_string (list, tag, &keywords) || !keywords)
926 return;
927
928 kwds = g_strsplit (keywords, ",", 0);
Sebastian Dröge6e350ec2010-12-19 12:50:30 +0100929 g_free (keywords);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200930
931 size = 0;
932 for (i = 0; kwds[i]; i++) {
933 /* size byte + null-terminator */
934 size += strlen (kwds[i]) + 1 + 1;
935 }
936
937 /* language tag + count + keywords */
938 size += 2 + 1;
939
940 data = ddata = g_malloc (size);
941
942 /* language tag */
943 GST_WRITE_UINT16_BE (data, language_code (GST_QT_MUX_DEFAULT_TAG_LANGUAGE));
944 /* count */
945 GST_WRITE_UINT8 (data + 2, i);
946 data += 3;
947 /* keywords */
948 for (i = 0; kwds[i]; ++i) {
949 gint len = strlen (kwds[i]);
950
951 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
952 GST_FOURCC_ARGS (fourcc), kwds[i]);
953 /* size */
954 GST_WRITE_UINT8 (data, len + 1);
955 memcpy (data + 1, kwds[i], len + 1);
956 data += len + 2;
957 }
958
959 g_strfreev (kwds);
960
Thiago Santos6321cde2015-01-31 13:14:44 -0300961 atom_udta_add_3gp_tag (udta, fourcc, ddata, size);
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +0200962 g_free (ddata);
963}
964
Thiago Santos083f8252009-12-10 16:53:19 -0300965static gboolean
966gst_qt_mux_parse_classification_string (GstQTMux * qtmux, const gchar * input,
967 guint32 * p_fourcc, guint16 * p_table, gchar ** p_content)
968{
969 guint32 fourcc;
970 gint table;
971 gint size;
972 const gchar *data;
973
974 data = input;
975 size = strlen (input);
976
977 if (size < 4 + 3 + 1 + 1 + 1) {
978 /* at least the minimum xxxx://y/z */
979 GST_WARNING_OBJECT (qtmux, "Classification tag input (%s) too short, "
980 "ignoring", input);
981 return FALSE;
982 }
983
984 /* read the fourcc */
985 memcpy (&fourcc, data, 4);
986 size -= 4;
987 data += 4;
988
989 if (strncmp (data, "://", 3) != 0) {
990 goto mismatch;
991 }
992 data += 3;
993 size -= 3;
994
995 /* read the table number */
996 if (sscanf (data, "%d", &table) != 1) {
997 goto mismatch;
998 }
999 if (table < 0) {
1000 GST_WARNING_OBJECT (qtmux, "Invalid table number in classification tag (%d)"
1001 ", table numbers should be positive, ignoring tag", table);
1002 return FALSE;
1003 }
1004
1005 /* find the next / */
1006 while (size > 0 && data[0] != '/') {
1007 data += 1;
1008 size -= 1;
1009 }
1010 if (size == 0) {
1011 goto mismatch;
1012 }
1013 g_assert (data[0] == '/');
1014
1015 /* skip the '/' */
1016 data += 1;
1017 size -= 1;
1018 if (size == 0) {
1019 goto mismatch;
1020 }
1021
1022 /* read up the rest of the string */
1023 *p_content = g_strdup (data);
1024 *p_table = (guint16) table;
1025 *p_fourcc = fourcc;
1026 return TRUE;
1027
1028mismatch:
1029 {
1030 GST_WARNING_OBJECT (qtmux, "Ignoring classification tag as "
1031 "input (%s) didn't match the expected entitycode://table/content",
1032 input);
1033 return FALSE;
1034 }
1035}
1036
1037static void
1038gst_qt_mux_add_3gp_classification (GstQTMux * qtmux, const GstTagList * list,
Thiago Santos6321cde2015-01-31 13:14:44 -03001039 AtomUDTA * udta, const char *tag, const char *tag2, guint32 fourcc)
Thiago Santos083f8252009-12-10 16:53:19 -03001040{
1041 gchar *clsf_data = NULL;
1042 gint size = 0;
1043 guint32 entity = 0;
1044 guint16 table = 0;
1045 gchar *content = NULL;
1046 guint8 *data;
1047
1048 g_return_if_fail (strcmp (tag, GST_TAG_3GP_CLASSIFICATION) == 0);
1049
1050 if (!gst_tag_list_get_string (list, tag, &clsf_data) || !clsf_data)
1051 return;
1052
1053 GST_DEBUG_OBJECT (qtmux, "Adding tag %" GST_FOURCC_FORMAT " -> %s",
1054 GST_FOURCC_ARGS (fourcc), clsf_data);
1055
1056 /* parse the string, format is:
1057 * entityfourcc://table/content
1058 */
1059 gst_qt_mux_parse_classification_string (qtmux, clsf_data, &entity, &table,
1060 &content);
1061 g_free (clsf_data);
1062 /* +1 for the \0 */
1063 size = strlen (content) + 1;
1064
1065 /* now we have everything, build the atom
1066 * atom description is at 3GPP TS 26.244 V8.2.0 (2009-09) */
1067 data = g_malloc (4 + 2 + 2 + size);
1068 GST_WRITE_UINT32_LE (data, entity);
1069 GST_WRITE_UINT16_BE (data + 4, (guint16) table);
1070 GST_WRITE_UINT16_BE (data + 6, 0);
1071 memcpy (data + 8, content, size);
1072 g_free (content);
1073
Thiago Santos6321cde2015-01-31 13:14:44 -03001074 atom_udta_add_3gp_tag (udta, fourcc, data, 4 + 2 + 2 + size);
Thiago Santos083f8252009-12-10 16:53:19 -03001075 g_free (data);
1076}
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +02001077
Thiago Santos6321cde2015-01-31 13:14:44 -03001078typedef void (*GstQTMuxAddUdtaTagFunc) (GstQTMux * mux,
1079 const GstTagList * list, AtomUDTA * udta, const char *tag,
1080 const char *tag2, guint32 fourcc);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001081
1082/*
1083 * Struct to record mappings from gstreamer tags to fourcc codes
1084 */
1085typedef struct _GstTagToFourcc
1086{
1087 guint32 fourcc;
1088 const gchar *gsttag;
1089 const gchar *gsttag2;
Thiago Santos6321cde2015-01-31 13:14:44 -03001090 const GstQTMuxAddUdtaTagFunc func;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001091} GstTagToFourcc;
1092
1093/* tag list tags to fourcc matching */
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +02001094static const GstTagToFourcc tag_matches_mp4[] = {
1095 {FOURCC__alb, GST_TAG_ALBUM, NULL, gst_qt_mux_add_mp4_tag},
Thiago Santos9a6514e2009-12-21 12:05:37 -03001096 {FOURCC_soal, GST_TAG_ALBUM_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +02001097 {FOURCC__ART, GST_TAG_ARTIST, NULL, gst_qt_mux_add_mp4_tag},
Thiago Santos9a6514e2009-12-21 12:05:37 -03001098 {FOURCC_soar, GST_TAG_ARTIST_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
1099 {FOURCC_aART, GST_TAG_ALBUM_ARTIST, NULL, gst_qt_mux_add_mp4_tag},
1100 {FOURCC_soaa, GST_TAG_ALBUM_ARTIST_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
Thiago Santosf0fde8b2015-01-31 10:47:40 -03001101 {FOURCC__swr, GST_TAG_APPLICATION_NAME, NULL, gst_qt_mux_add_mp4_tag},
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +02001102 {FOURCC__cmt, GST_TAG_COMMENT, NULL, gst_qt_mux_add_mp4_tag},
1103 {FOURCC__wrt, GST_TAG_COMPOSER, NULL, gst_qt_mux_add_mp4_tag},
Thiago Santos9a6514e2009-12-21 12:05:37 -03001104 {FOURCC_soco, GST_TAG_COMPOSER_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
1105 {FOURCC_tvsh, GST_TAG_SHOW_NAME, NULL, gst_qt_mux_add_mp4_tag},
1106 {FOURCC_sosn, GST_TAG_SHOW_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
1107 {FOURCC_tvsn, GST_TAG_SHOW_SEASON_NUMBER, NULL, gst_qt_mux_add_mp4_tag},
1108 {FOURCC_tves, GST_TAG_SHOW_EPISODE_NUMBER, NULL, gst_qt_mux_add_mp4_tag},
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +02001109 {FOURCC__gen, GST_TAG_GENRE, NULL, gst_qt_mux_add_mp4_tag},
1110 {FOURCC__nam, GST_TAG_TITLE, NULL, gst_qt_mux_add_mp4_tag},
Thiago Santos9a6514e2009-12-21 12:05:37 -03001111 {FOURCC_sonm, GST_TAG_TITLE_SORTNAME, NULL, gst_qt_mux_add_mp4_tag},
1112 {FOURCC_perf, GST_TAG_PERFORMER, NULL, gst_qt_mux_add_mp4_tag},
1113 {FOURCC__grp, GST_TAG_GROUPING, NULL, gst_qt_mux_add_mp4_tag},
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +02001114 {FOURCC__des, GST_TAG_DESCRIPTION, NULL, gst_qt_mux_add_mp4_tag},
Thiago Santos9a6514e2009-12-21 12:05:37 -03001115 {FOURCC__lyr, GST_TAG_LYRICS, NULL, gst_qt_mux_add_mp4_tag},
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +02001116 {FOURCC__too, GST_TAG_ENCODER, NULL, gst_qt_mux_add_mp4_tag},
1117 {FOURCC_cprt, GST_TAG_COPYRIGHT, NULL, gst_qt_mux_add_mp4_tag},
1118 {FOURCC_keyw, GST_TAG_KEYWORDS, NULL, gst_qt_mux_add_mp4_tag},
1119 {FOURCC__day, GST_TAG_DATE, NULL, gst_qt_mux_add_mp4_date},
1120 {FOURCC_tmpo, GST_TAG_BEATS_PER_MINUTE, NULL, gst_qt_mux_add_mp4_tag},
1121 {FOURCC_trkn, GST_TAG_TRACK_NUMBER, GST_TAG_TRACK_COUNT,
1122 gst_qt_mux_add_mp4_tag},
1123 {FOURCC_disk, GST_TAG_ALBUM_VOLUME_NUMBER, GST_TAG_ALBUM_VOLUME_COUNT,
1124 gst_qt_mux_add_mp4_tag},
1125 {FOURCC_covr, GST_TAG_PREVIEW_IMAGE, NULL, gst_qt_mux_add_mp4_cover},
Antonio Frediani63d350c2011-05-30 12:09:31 +02001126 {FOURCC_covr, GST_TAG_IMAGE, NULL, gst_qt_mux_add_mp4_cover},
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +02001127 {0, NULL,}
1128};
1129
1130static const GstTagToFourcc tag_matches_3gp[] = {
1131 {FOURCC_titl, GST_TAG_TITLE, NULL, gst_qt_mux_add_3gp_str},
1132 {FOURCC_dscp, GST_TAG_DESCRIPTION, NULL, gst_qt_mux_add_3gp_str},
1133 {FOURCC_cprt, GST_TAG_COPYRIGHT, NULL, gst_qt_mux_add_3gp_str},
1134 {FOURCC_perf, GST_TAG_ARTIST, NULL, gst_qt_mux_add_3gp_str},
1135 {FOURCC_auth, GST_TAG_COMPOSER, NULL, gst_qt_mux_add_3gp_str},
1136 {FOURCC_gnre, GST_TAG_GENRE, NULL, gst_qt_mux_add_3gp_str},
1137 {FOURCC_kywd, GST_TAG_KEYWORDS, NULL, gst_qt_mux_add_3gp_keywords},
1138 {FOURCC_yrrc, GST_TAG_DATE, NULL, gst_qt_mux_add_3gp_date},
1139 {FOURCC_albm, GST_TAG_ALBUM, GST_TAG_TRACK_NUMBER, gst_qt_mux_add_3gp_str},
1140 {FOURCC_loci, GST_TAG_GEO_LOCATION_NAME, NULL, gst_qt_mux_add_3gp_location},
Thiago Santos083f8252009-12-10 16:53:19 -03001141 {FOURCC_clsf, GST_TAG_3GP_CLASSIFICATION, NULL,
1142 gst_qt_mux_add_3gp_classification},
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001143 {0, NULL,}
1144};
1145
1146/* qtdemux produces these for atoms it cannot parse */
1147#define GST_QT_DEMUX_PRIVATE_TAG "private-qt-tag"
1148
1149static void
Thiago Santosa740adc2010-02-22 16:45:34 -03001150gst_qt_mux_add_xmp_tags (GstQTMux * qtmux, const GstTagList * list)
1151{
1152 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
Thiago Santosaba80002011-03-21 10:56:51 -03001153 GstBuffer *xmp = NULL;
Thiago Santosa740adc2010-02-22 16:45:34 -03001154
Thiago Santos7065a652010-09-15 17:54:49 -03001155 /* adobe specs only have 'quicktime' and 'mp4',
1156 * but I guess we can extrapolate to gpp.
1157 * Keep mj2 out for now as we don't add any tags for it yet.
Thiago Santos281ef572010-03-12 10:53:36 -03001158 * If you have further info about xmp on these formats, please share */
1159 if (qtmux_klass->format == GST_QT_MUX_FORMAT_MJ2)
1160 return;
Thiago Santosa740adc2010-02-22 16:45:34 -03001161
1162 GST_DEBUG_OBJECT (qtmux, "Adding xmp tags");
1163
Thiago Santos7065a652010-09-15 17:54:49 -03001164 if (qtmux_klass->format == GST_QT_MUX_FORMAT_QT) {
Thiago Santosaba80002011-03-21 10:56:51 -03001165 xmp = gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER (qtmux),
1166 list, TRUE);
1167 if (xmp)
Thiago Santos6321cde2015-01-31 13:14:44 -03001168 atom_udta_add_xmp_tags (&qtmux->moov->udta, xmp);
Thiago Santos7065a652010-09-15 17:54:49 -03001169 } else {
Thiago Santosaba80002011-03-21 10:56:51 -03001170 AtomInfo *ainfo;
Thiago Santos7065a652010-09-15 17:54:49 -03001171 /* for isom/mp4, it is a top level uuid atom */
Thiago Santosaba80002011-03-21 10:56:51 -03001172 xmp = gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER (qtmux),
1173 list, TRUE);
1174 if (xmp) {
1175 ainfo = build_uuid_xmp_atom (xmp);
1176 if (ainfo) {
1177 qtmux->extra_atoms = g_slist_prepend (qtmux->extra_atoms, ainfo);
1178 }
Thiago Santos7065a652010-09-15 17:54:49 -03001179 }
1180 }
Thiago Santosaba80002011-03-21 10:56:51 -03001181 if (xmp)
1182 gst_buffer_unref (xmp);
Thiago Santosa740adc2010-02-22 16:45:34 -03001183}
1184
1185static void
Thiago Santos6321cde2015-01-31 13:14:44 -03001186gst_qt_mux_add_metadata_tags (GstQTMux * qtmux, const GstTagList * list,
1187 AtomUDTA * udta)
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001188{
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +02001189 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001190 guint32 fourcc;
1191 gint i;
1192 const gchar *tag, *tag2;
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +02001193 const GstTagToFourcc *tag_matches;
1194
1195 switch (qtmux_klass->format) {
1196 case GST_QT_MUX_FORMAT_3GP:
1197 tag_matches = tag_matches_3gp;
1198 break;
1199 case GST_QT_MUX_FORMAT_MJ2:
1200 tag_matches = NULL;
1201 break;
1202 default:
1203 /* sort of iTunes style for mp4 and QT (?) */
1204 tag_matches = tag_matches_mp4;
1205 break;
1206 }
1207
1208 if (!tag_matches)
1209 return;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001210
1211 for (i = 0; tag_matches[i].fourcc; i++) {
1212 fourcc = tag_matches[i].fourcc;
1213 tag = tag_matches[i].gsttag;
1214 tag2 = tag_matches[i].gsttag2;
1215
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +02001216 g_assert (tag_matches[i].func);
Thiago Santos6321cde2015-01-31 13:14:44 -03001217 tag_matches[i].func (qtmux, list, udta, tag, tag2, fourcc);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001218 }
1219
1220 /* add unparsed blobs if present */
1221 if (gst_tag_exists (GST_QT_DEMUX_PRIVATE_TAG)) {
1222 guint num_tags;
1223
1224 num_tags = gst_tag_list_get_tag_size (list, GST_QT_DEMUX_PRIVATE_TAG);
1225 for (i = 0; i < num_tags; ++i) {
Mark Nauwelaertsdd254112012-07-27 12:05:44 +02001226 GstSample *sample = NULL;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001227 GstBuffer *buf;
Mark Nauwelaertsdd254112012-07-27 12:05:44 +02001228 const GstStructure *s;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001229
Mark Nauwelaertsdd254112012-07-27 12:05:44 +02001230 if (!gst_tag_list_get_sample_index (list, GST_QT_DEMUX_PRIVATE_TAG, i,
1231 &sample))
1232 continue;
1233 buf = gst_sample_get_buffer (sample);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001234
Mark Nauwelaertsdd254112012-07-27 12:05:44 +02001235 if (buf && (s = gst_sample_get_info (sample))) {
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001236 const gchar *style = NULL;
Wim Taymans583d39d2012-01-23 17:25:37 +01001237 GstMapInfo map;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001238
Wim Taymans583d39d2012-01-23 17:25:37 +01001239 gst_buffer_map (buf, &map, GST_MAP_READ);
Matej Knopp1e5dd9e2011-11-21 20:31:31 +01001240 GST_DEBUG_OBJECT (qtmux,
Mark Nauwelaertsdd254112012-07-27 12:05:44 +02001241 "Found private tag %d/%d; size %" G_GSIZE_FORMAT ", info %"
1242 GST_PTR_FORMAT, i, num_tags, map.size, s);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001243 if (s && (style = gst_structure_get_string (s, "style"))) {
Mark Nauwelaerts7c9a6092009-06-10 12:42:44 +02001244 /* try to prevent some style tag ending up into another variant
1245 * (todo: make into a list if more cases) */
1246 if ((strcmp (style, "itunes") == 0 &&
1247 qtmux_klass->format == GST_QT_MUX_FORMAT_MP4) ||
1248 (strcmp (style, "iso") == 0 &&
1249 qtmux_klass->format == GST_QT_MUX_FORMAT_3GP)) {
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001250 GST_DEBUG_OBJECT (qtmux, "Adding private tag");
Thiago Santos6321cde2015-01-31 13:14:44 -03001251 atom_udta_add_blob_tag (udta, map.data, map.size);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001252 }
1253 }
Wim Taymans583d39d2012-01-23 17:25:37 +01001254 gst_buffer_unmap (buf, &map);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001255 }
1256 }
1257 }
1258
1259 return;
1260}
1261
1262/*
1263 * Gets the tagsetter iface taglist and puts the known tags
1264 * into the output stream
1265 */
1266static void
1267gst_qt_mux_setup_metadata (GstQTMux * qtmux)
1268{
Stefan Kost6a47f1f2008-12-16 16:26:52 +00001269 const GstTagList *tags;
Thiago Santose52b2cb2015-01-31 14:32:34 -03001270 GSList *walk;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001271
Michael Smith8b3aca92010-02-26 11:50:25 -08001272 GST_OBJECT_LOCK (qtmux);
Stefan Kost6a47f1f2008-12-16 16:26:52 +00001273 tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (qtmux));
Michael Smith8b3aca92010-02-26 11:50:25 -08001274 GST_OBJECT_UNLOCK (qtmux);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001275
Stefan Kost6a47f1f2008-12-16 16:26:52 +00001276 GST_LOG_OBJECT (qtmux, "tags: %" GST_PTR_FORMAT, tags);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001277
Stefan Kost6a47f1f2008-12-16 16:26:52 +00001278 if (tags && !gst_tag_list_is_empty (tags)) {
Edward Hervey099ad632010-07-02 12:45:20 +02001279 GstTagList *copy = gst_tag_list_copy (tags);
1280
1281 GST_DEBUG_OBJECT (qtmux, "Removing bogus tags");
1282 gst_tag_list_remove_tag (copy, GST_TAG_VIDEO_CODEC);
1283 gst_tag_list_remove_tag (copy, GST_TAG_AUDIO_CODEC);
1284 gst_tag_list_remove_tag (copy, GST_TAG_CONTAINER_FORMAT);
1285
Stefan Kost6a47f1f2008-12-16 16:26:52 +00001286 GST_DEBUG_OBJECT (qtmux, "Formatting tags");
Thiago Santos6321cde2015-01-31 13:14:44 -03001287 gst_qt_mux_add_metadata_tags (qtmux, copy, &qtmux->moov->udta);
Edward Hervey099ad632010-07-02 12:45:20 +02001288 gst_qt_mux_add_xmp_tags (qtmux, copy);
Tim-Philipp Müllerc074bfd2012-08-04 16:10:16 +01001289 gst_tag_list_unref (copy);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001290 } else {
Stefan Kost6a47f1f2008-12-16 16:26:52 +00001291 GST_DEBUG_OBJECT (qtmux, "No tags received");
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001292 }
Thiago Santos6321cde2015-01-31 13:14:44 -03001293
Thiago Santose52b2cb2015-01-31 14:32:34 -03001294 for (walk = qtmux->sinkpads; walk; walk = g_slist_next (walk)) {
1295 GstCollectData *cdata = (GstCollectData *) walk->data;
1296 GstQTPad *qpad = (GstQTPad *) cdata;
1297 GstPad *pad = qpad->collect.pad;
1298
1299 if (qpad->tags) {
1300 GST_DEBUG_OBJECT (pad, "Adding tags");
1301 gst_tag_list_remove_tag (qpad->tags, GST_TAG_CONTAINER_FORMAT);
1302 gst_qt_mux_add_metadata_tags (qtmux, qpad->tags, &qpad->trak->udta);
1303 GST_DEBUG_OBJECT (pad, "Tags added");
1304 } else {
1305 GST_DEBUG_OBJECT (pad, "No tags received");
1306 }
1307 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001308}
1309
Mark Nauwelaerts79f565f2010-11-09 15:01:15 +01001310static inline GstBuffer *
1311_gst_buffer_new_take_data (guint8 * data, guint size)
1312{
1313 GstBuffer *buf;
1314
1315 buf = gst_buffer_new ();
Wim Taymans3d61d122012-03-30 18:13:08 +02001316 gst_buffer_append_memory (buf,
Wim Taymansca9532c2012-02-22 02:06:17 +01001317 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
Mark Nauwelaerts79f565f2010-11-09 15:01:15 +01001318
1319 return buf;
1320}
1321
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001322static GstFlowReturn
1323gst_qt_mux_send_buffer (GstQTMux * qtmux, GstBuffer * buf, guint64 * offset,
1324 gboolean mind_fast)
1325{
1326 GstFlowReturn res;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02001327 gsize size;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001328
1329 g_return_val_if_fail (buf != NULL, GST_FLOW_ERROR);
1330
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02001331 size = gst_buffer_get_size (buf);
Matej Knopp1e5dd9e2011-11-21 20:31:31 +01001332 GST_LOG_OBJECT (qtmux, "sending buffer size %" G_GSIZE_FORMAT, size);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001333
1334 if (mind_fast && qtmux->fast_start_file) {
Wim Taymans583d39d2012-01-23 17:25:37 +01001335 GstMapInfo map;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001336 gint ret;
1337
1338 GST_LOG_OBJECT (qtmux, "to temporary file");
Wim Taymans583d39d2012-01-23 17:25:37 +01001339 gst_buffer_map (buf, &map, GST_MAP_READ);
1340 ret = fwrite (map.data, sizeof (guint8), map.size, qtmux->fast_start_file);
1341 gst_buffer_unmap (buf, &map);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001342 gst_buffer_unref (buf);
1343 if (ret != size)
1344 goto write_error;
1345 else
1346 res = GST_FLOW_OK;
1347 } else {
1348 GST_LOG_OBJECT (qtmux, "downstream");
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001349 res = gst_pad_push (qtmux->srcpad, buf);
1350 }
1351
Mark Nauwelaertsc3f462d2009-06-11 15:54:42 +02001352 if (G_LIKELY (offset))
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001353 *offset += size;
1354
1355 return res;
1356
1357 /* ERRORS */
1358write_error:
1359 {
1360 GST_ELEMENT_ERROR (qtmux, RESOURCE, WRITE,
1361 ("Failed to write to temporary file"), GST_ERROR_SYSTEM);
1362 return GST_FLOW_ERROR;
1363 }
1364}
1365
Marc-André Lureauc2718fd2009-09-30 17:16:30 +02001366static gboolean
1367gst_qt_mux_seek_to_beginning (FILE * f)
1368{
1369#ifdef HAVE_FSEEKO
1370 if (fseeko (f, (off_t) 0, SEEK_SET) != 0)
1371 return FALSE;
1372#elif defined (G_OS_UNIX) || defined (G_OS_WIN32)
1373 if (lseek (fileno (f), (off_t) 0, SEEK_SET) == (off_t) - 1)
1374 return FALSE;
1375#else
1376 if (fseek (f, (long) 0, SEEK_SET) != 0)
1377 return FALSE;
1378#endif
1379 return TRUE;
1380}
1381
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001382static GstFlowReturn
1383gst_qt_mux_send_buffered_data (GstQTMux * qtmux, guint64 * offset)
1384{
1385 GstFlowReturn ret = GST_FLOW_OK;
1386 GstBuffer *buf = NULL;
1387
1388 if (fflush (qtmux->fast_start_file))
1389 goto flush_failed;
1390
Marc-André Lureauc2718fd2009-09-30 17:16:30 +02001391 if (!gst_qt_mux_seek_to_beginning (qtmux->fast_start_file))
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001392 goto seek_failed;
1393
1394 /* hm, this could all take a really really long time,
1395 * but there may not be another way to get moov atom first
1396 * (somehow optimize copy?) */
1397 GST_DEBUG_OBJECT (qtmux, "Sending buffered data");
1398 while (ret == GST_FLOW_OK) {
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001399 const int bufsize = 4096;
Wim Taymans583d39d2012-01-23 17:25:37 +01001400 GstMapInfo map;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02001401 gsize size;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001402
1403 buf = gst_buffer_new_and_alloc (bufsize);
Wim Taymans583d39d2012-01-23 17:25:37 +01001404 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1405 size = fread (map.data, sizeof (guint8), bufsize, qtmux->fast_start_file);
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02001406 if (size == 0) {
Wim Taymans583d39d2012-01-23 17:25:37 +01001407 gst_buffer_unmap (buf, &map);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001408 break;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02001409 }
Michael Smithb85c5f22013-03-19 18:09:31 -07001410 GST_LOG_OBJECT (qtmux, "Pushing buffered buffer of size %d", (gint) size);
Wim Taymans583d39d2012-01-23 17:25:37 +01001411 gst_buffer_unmap (buf, &map);
Michael Smithb85c5f22013-03-19 18:09:31 -07001412 if (size != bufsize)
1413 gst_buffer_set_size (buf, size);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001414 ret = gst_qt_mux_send_buffer (qtmux, buf, offset, FALSE);
1415 buf = NULL;
1416 }
1417 if (buf)
1418 gst_buffer_unref (buf);
1419
Marc-André Lureauc2718fd2009-09-30 17:16:30 +02001420 if (ftruncate (fileno (qtmux->fast_start_file), 0))
1421 goto seek_failed;
1422 if (!gst_qt_mux_seek_to_beginning (qtmux->fast_start_file))
1423 goto seek_failed;
1424
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001425 return ret;
1426
1427 /* ERRORS */
1428flush_failed:
1429 {
1430 GST_ELEMENT_ERROR (qtmux, RESOURCE, WRITE,
1431 ("Failed to flush temporary file"), GST_ERROR_SYSTEM);
1432 ret = GST_FLOW_ERROR;
Mark Nauwelaertsc3507442010-11-05 11:08:01 +01001433 goto fail;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001434 }
1435seek_failed:
1436 {
1437 GST_ELEMENT_ERROR (qtmux, RESOURCE, SEEK,
1438 ("Failed to seek temporary file"), GST_ERROR_SYSTEM);
1439 ret = GST_FLOW_ERROR;
Mark Nauwelaertsc3507442010-11-05 11:08:01 +01001440 goto fail;
1441 }
1442fail:
1443 {
1444 /* clear descriptor so we don't remove temp file later on,
1445 * might be possible to recover */
1446 fclose (qtmux->fast_start_file);
1447 qtmux->fast_start_file = NULL;
Marc-André Lureau3e6a4152009-09-28 16:53:51 +02001448 return ret;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001449 }
1450}
1451
1452/*
1453 * Sends the initial mdat atom fields (size fields and fourcc type),
1454 * the subsequent buffers are considered part of it's data.
1455 * As we can't predict the amount of data that we are going to place in mdat
1456 * we need to record the position of the size field in the stream so we can
1457 * seek back to it later and update when the streams have finished.
1458 */
1459static GstFlowReturn
Mark Nauwelaertsc3f462d2009-06-11 15:54:42 +02001460gst_qt_mux_send_mdat_header (GstQTMux * qtmux, guint64 * off, guint64 size,
1461 gboolean extended)
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001462{
1463 Atom *node_header;
1464 GstBuffer *buf;
1465 guint8 *data = NULL;
1466 guint64 offset = 0;
1467
1468 GST_DEBUG_OBJECT (qtmux, "Sending mdat's atom header, "
1469 "size %" G_GUINT64_FORMAT, size);
1470
1471 node_header = g_malloc0 (sizeof (Atom));
1472 node_header->type = FOURCC_mdat;
Mark Nauwelaertsc3f462d2009-06-11 15:54:42 +02001473 if (extended) {
1474 /* use extended size */
1475 node_header->size = 1;
1476 node_header->extended_size = 0;
1477 if (size)
1478 node_header->extended_size = size + 16;
1479 } else {
1480 node_header->size = size + 8;
1481 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001482
1483 size = offset = 0;
1484 if (atom_copy_data (node_header, &data, &size, &offset) == 0)
1485 goto serialize_error;
1486
Mark Nauwelaerts79f565f2010-11-09 15:01:15 +01001487 buf = _gst_buffer_new_take_data (data, offset);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001488 g_free (node_header);
1489
1490 GST_LOG_OBJECT (qtmux, "Pushing mdat start");
1491 return gst_qt_mux_send_buffer (qtmux, buf, off, FALSE);
1492
1493 /* ERRORS */
1494serialize_error:
1495 {
1496 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
Thiago Santosb692f9f2009-12-12 16:07:15 -03001497 ("Failed to serialize mdat"));
Edward Hervey112d9482014-05-12 16:53:32 +02001498 g_free (node_header);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001499 return GST_FLOW_ERROR;
1500 }
1501}
1502
1503/*
1504 * We get the position of the mdat size field, seek back to it
1505 * and overwrite with the real value
1506 */
1507static GstFlowReturn
1508gst_qt_mux_update_mdat_size (GstQTMux * qtmux, guint64 mdat_pos,
1509 guint64 mdat_size, guint64 * offset)
1510{
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001511 GstBuffer *buf;
Mark Nauwelaertsc3f462d2009-06-11 15:54:42 +02001512 gboolean large_file;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02001513 GstSegment segment;
Wim Taymans583d39d2012-01-23 17:25:37 +01001514 GstMapInfo map;
Mark Nauwelaertsc3f462d2009-06-11 15:54:42 +02001515
1516 large_file = (mdat_size > MDAT_LARGE_FILE_LIMIT);
1517
1518 if (large_file)
1519 mdat_pos += 8;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001520
1521 /* seek and rewrite the header */
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02001522 gst_segment_init (&segment, GST_FORMAT_BYTES);
1523 segment.start = mdat_pos;
1524 gst_pad_push_event (qtmux->srcpad, gst_event_new_segment (&segment));
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001525
Mark Nauwelaertsc3f462d2009-06-11 15:54:42 +02001526 if (large_file) {
1527 buf = gst_buffer_new_and_alloc (sizeof (guint64));
Wim Taymans583d39d2012-01-23 17:25:37 +01001528 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1529 GST_WRITE_UINT64_BE (map.data, mdat_size + 16);
Mark Nauwelaertsc3f462d2009-06-11 15:54:42 +02001530 } else {
Mark Nauwelaertsc3f462d2009-06-11 15:54:42 +02001531 buf = gst_buffer_new_and_alloc (16);
Wim Taymans583d39d2012-01-23 17:25:37 +01001532 gst_buffer_map (buf, &map, GST_MAP_WRITE);
1533 GST_WRITE_UINT32_BE (map.data, 8);
1534 GST_WRITE_UINT32_LE (map.data + 4, FOURCC_free);
1535 GST_WRITE_UINT32_BE (map.data + 8, mdat_size + 8);
1536 GST_WRITE_UINT32_LE (map.data + 12, FOURCC_mdat);
Mark Nauwelaertsc3f462d2009-06-11 15:54:42 +02001537 }
Wim Taymans583d39d2012-01-23 17:25:37 +01001538 gst_buffer_unmap (buf, &map);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001539
1540 return gst_qt_mux_send_buffer (qtmux, buf, offset, FALSE);
1541}
1542
1543static GstFlowReturn
Thiago Santose8c21f12009-10-15 21:11:16 -03001544gst_qt_mux_send_ftyp (GstQTMux * qtmux, guint64 * off)
1545{
1546 GstBuffer *buf;
1547 guint64 size = 0, offset = 0;
1548 guint8 *data = NULL;
1549
1550 GST_DEBUG_OBJECT (qtmux, "Sending ftyp atom");
1551
1552 if (!atom_ftyp_copy_data (qtmux->ftyp, &data, &size, &offset))
1553 goto serialize_error;
1554
Mark Nauwelaerts79f565f2010-11-09 15:01:15 +01001555 buf = _gst_buffer_new_take_data (data, offset);
Thiago Santose8c21f12009-10-15 21:11:16 -03001556
1557 GST_LOG_OBJECT (qtmux, "Pushing ftyp");
1558 return gst_qt_mux_send_buffer (qtmux, buf, off, FALSE);
1559
1560 /* ERRORS */
1561serialize_error:
1562 {
1563 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
1564 ("Failed to serialize ftyp"));
1565 return GST_FLOW_ERROR;
1566 }
1567}
1568
Thiago Santosb692f9f2009-12-12 16:07:15 -03001569static void
1570gst_qt_mux_prepare_ftyp (GstQTMux * qtmux, AtomFTYP ** p_ftyp,
1571 GstBuffer ** p_prefix)
1572{
1573 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
1574 guint32 major, version;
1575 GList *comp;
1576 GstBuffer *prefix = NULL;
1577 AtomFTYP *ftyp = NULL;
1578
1579 GST_DEBUG_OBJECT (qtmux, "Preparing ftyp and possible prefix atom");
1580
1581 /* init and send context and ftyp based on current property state */
1582 gst_qt_mux_map_format_to_header (qtmux_klass->format, &prefix, &major,
1583 &version, &comp, qtmux->moov, qtmux->longest_chunk,
1584 qtmux->fast_start_file != NULL);
1585 ftyp = atom_ftyp_new (qtmux->context, major, version, comp);
1586 if (comp)
1587 g_list_free (comp);
1588 if (prefix) {
1589 if (p_prefix)
1590 *p_prefix = prefix;
1591 else
1592 gst_buffer_unref (prefix);
1593 }
1594 *p_ftyp = ftyp;
1595}
1596
Thiago Santose8c21f12009-10-15 21:11:16 -03001597static GstFlowReturn
1598gst_qt_mux_prepare_and_send_ftyp (GstQTMux * qtmux)
1599{
1600 GstFlowReturn ret = GST_FLOW_OK;
Thiago Santosb692f9f2009-12-12 16:07:15 -03001601 GstBuffer *prefix = NULL;
Thiago Santose8c21f12009-10-15 21:11:16 -03001602
1603 GST_DEBUG_OBJECT (qtmux, "Preparing to send ftyp atom");
1604
1605 /* init and send context and ftyp based on current property state */
Thiago Santosb692f9f2009-12-12 16:07:15 -03001606 if (qtmux->ftyp) {
Thiago Santose8c21f12009-10-15 21:11:16 -03001607 atom_ftyp_free (qtmux->ftyp);
Thiago Santosb692f9f2009-12-12 16:07:15 -03001608 qtmux->ftyp = NULL;
1609 }
1610 gst_qt_mux_prepare_ftyp (qtmux, &qtmux->ftyp, &prefix);
Thiago Santose8c21f12009-10-15 21:11:16 -03001611 if (prefix) {
1612 ret = gst_qt_mux_send_buffer (qtmux, prefix, &qtmux->header_size, FALSE);
1613 if (ret != GST_FLOW_OK)
1614 return ret;
1615 }
1616 return gst_qt_mux_send_ftyp (qtmux, &qtmux->header_size);
1617}
1618
Mark Nauwelaerts69d07462010-11-09 16:49:07 +01001619static void
Marc-André Lureaubd0d3202010-07-22 19:40:07 +02001620gst_qt_mux_set_header_on_caps (GstQTMux * mux, GstBuffer * buf)
1621{
1622 GstStructure *structure;
1623 GValue array = { 0 };
1624 GValue value = { 0 };
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02001625 GstCaps *caps, *tcaps;
Marc-André Lureaubd0d3202010-07-22 19:40:07 +02001626
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02001627 tcaps = gst_pad_get_current_caps (mux->srcpad);
1628 caps = gst_caps_copy (tcaps);
1629 gst_caps_unref (tcaps);
1630
Marc-André Lureaubd0d3202010-07-22 19:40:07 +02001631 structure = gst_caps_get_structure (caps, 0);
1632
1633 g_value_init (&array, GST_TYPE_ARRAY);
1634
Wim Taymansbb2bd602012-01-30 17:16:51 +01001635 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_HEADER);
Marc-André Lureaubd0d3202010-07-22 19:40:07 +02001636 g_value_init (&value, GST_TYPE_BUFFER);
1637 gst_value_take_buffer (&value, gst_buffer_ref (buf));
1638 gst_value_array_append_value (&array, &value);
1639 g_value_unset (&value);
1640
1641 gst_structure_set_value (structure, "streamheader", &array);
1642 g_value_unset (&array);
1643 gst_pad_set_caps (mux->srcpad, caps);
1644 gst_caps_unref (caps);
1645}
1646
1647static void
Mark Nauwelaerts69d07462010-11-09 16:49:07 +01001648gst_qt_mux_configure_moov (GstQTMux * qtmux, guint32 * _timescale)
1649{
Mark Nauwelaerts60c8ed22010-12-03 15:23:00 +01001650 gboolean fragmented;
Mark Nauwelaerts69d07462010-11-09 16:49:07 +01001651 guint32 timescale;
1652
Mark Nauwelaerts69d07462010-11-09 16:49:07 +01001653 GST_OBJECT_LOCK (qtmux);
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01001654 timescale = qtmux->timescale;
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01001655 fragmented = qtmux->fragment_sequence > 0;
Mark Nauwelaerts69d07462010-11-09 16:49:07 +01001656 GST_OBJECT_UNLOCK (qtmux);
1657
1658 /* inform lower layers of our property wishes, and determine duration.
1659 * Let moov take care of this using its list of traks;
1660 * so that released pads are also included */
Mark Nauwelaerts69d07462010-11-09 16:49:07 +01001661 GST_DEBUG_OBJECT (qtmux, "Updating timescale to %" G_GUINT32_FORMAT,
1662 timescale);
1663 atom_moov_update_timescale (qtmux->moov, timescale);
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01001664 atom_moov_set_fragmented (qtmux->moov, fragmented);
Mark Nauwelaerts69d07462010-11-09 16:49:07 +01001665
1666 atom_moov_update_duration (qtmux->moov);
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01001667
1668 if (_timescale)
1669 *_timescale = timescale;
Mark Nauwelaerts69d07462010-11-09 16:49:07 +01001670}
1671
1672static GstFlowReturn
1673gst_qt_mux_send_moov (GstQTMux * qtmux, guint64 * _offset, gboolean mind_fast)
1674{
1675 guint64 offset = 0, size = 0;
1676 guint8 *data;
1677 GstBuffer *buf;
1678 GstFlowReturn ret = GST_FLOW_OK;
1679
1680 /* serialize moov */
1681 offset = size = 0;
1682 data = NULL;
1683 GST_LOG_OBJECT (qtmux, "Copying movie header into buffer");
1684 if (!atom_moov_copy_data (qtmux->moov, &data, &size, &offset))
1685 goto serialize_error;
1686
1687 buf = _gst_buffer_new_take_data (data, offset);
1688 GST_DEBUG_OBJECT (qtmux, "Pushing moov atoms");
Marc-André Lureaubd0d3202010-07-22 19:40:07 +02001689 gst_qt_mux_set_header_on_caps (qtmux, buf);
Mark Nauwelaerts69d07462010-11-09 16:49:07 +01001690 ret = gst_qt_mux_send_buffer (qtmux, buf, _offset, mind_fast);
1691
1692 return ret;
1693
1694serialize_error:
1695 {
1696 g_free (data);
1697 return GST_FLOW_ERROR;
1698 }
1699}
1700
Mark Nauwelaerts35e028f2010-11-09 15:54:44 +01001701/* either calculates size of extra atoms or pushes them */
1702static GstFlowReturn
1703gst_qt_mux_send_extra_atoms (GstQTMux * qtmux, gboolean send, guint64 * offset,
1704 gboolean mind_fast)
1705{
1706 GSList *walk;
1707 guint64 loffset = 0, size = 0;
1708 guint8 *data;
1709 GstFlowReturn ret = GST_FLOW_OK;
1710
1711 for (walk = qtmux->extra_atoms; walk; walk = g_slist_next (walk)) {
1712 AtomInfo *ainfo = (AtomInfo *) walk->data;
1713
1714 loffset = size = 0;
1715 data = NULL;
1716 if (!ainfo->copy_data_func (ainfo->atom,
1717 send ? &data : NULL, &size, &loffset))
1718 goto serialize_error;
1719
1720 if (send) {
1721 GstBuffer *buf;
1722
1723 GST_DEBUG_OBJECT (qtmux,
1724 "Pushing extra top-level atom %" GST_FOURCC_FORMAT,
1725 GST_FOURCC_ARGS (ainfo->atom->type));
1726 buf = _gst_buffer_new_take_data (data, loffset);
Mark Nauwelaerts69d07462010-11-09 16:49:07 +01001727 ret = gst_qt_mux_send_buffer (qtmux, buf, offset, FALSE);
Mark Nauwelaerts35e028f2010-11-09 15:54:44 +01001728 if (ret != GST_FLOW_OK)
1729 break;
1730 } else {
1731 if (offset)
1732 *offset += loffset;
1733 }
1734 }
1735
1736 return ret;
1737
1738serialize_error:
1739 {
1740 g_free (data);
1741 return GST_FLOW_ERROR;
1742 }
1743}
1744
Jan Schmidt3d59b5f2015-04-01 23:46:13 +11001745static gboolean
1746gst_qt_mux_downstream_is_seekable (GstQTMux * qtmux)
1747{
1748 gboolean seekable = FALSE;
1749 GstQuery *query = gst_query_new_seeking (GST_FORMAT_BYTES);
1750
1751 if (gst_pad_peer_query (qtmux->srcpad, query)) {
1752 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
1753 GST_INFO_OBJECT (qtmux, "downstream is %sseekable", seekable ? "" : "not ");
1754 } else {
1755 /* have to assume seeking is not supported if query not handled downstream */
1756 GST_WARNING_OBJECT (qtmux, "downstream did not handle seeking query");
1757 seekable = FALSE;
1758 }
1759 gst_query_unref (query);
1760
1761 return seekable;
1762}
1763
Jan Schmidtfe739b72015-04-01 11:15:38 +11001764static void
1765gst_qt_mux_prepare_moov_recovery (GstQTMux * qtmux)
1766{
1767 GSList *walk;
1768 gboolean fail = FALSE;
1769 AtomFTYP *ftyp = NULL;
1770 GstBuffer *prefix = NULL;
1771
1772 GST_DEBUG_OBJECT (qtmux, "Openning moov recovery file: %s",
1773 qtmux->moov_recov_file_path);
1774
1775 qtmux->moov_recov_file = g_fopen (qtmux->moov_recov_file_path, "wb+");
1776 if (qtmux->moov_recov_file == NULL) {
1777 GST_WARNING_OBJECT (qtmux, "Failed to open moov recovery file in %s",
1778 qtmux->moov_recov_file_path);
1779 return;
1780 }
1781
1782 gst_qt_mux_prepare_ftyp (qtmux, &ftyp, &prefix);
1783
1784 if (!atoms_recov_write_headers (qtmux->moov_recov_file, ftyp, prefix,
1785 qtmux->moov, qtmux->timescale, g_slist_length (qtmux->sinkpads))) {
1786 GST_WARNING_OBJECT (qtmux, "Failed to write moov recovery file " "headers");
1787 goto fail;
1788 }
1789
1790 atom_ftyp_free (ftyp);
1791 if (prefix)
1792 gst_buffer_unref (prefix);
1793
1794 for (walk = qtmux->sinkpads; walk && !fail; walk = g_slist_next (walk)) {
1795 GstCollectData *cdata = (GstCollectData *) walk->data;
1796 GstQTPad *qpad = (GstQTPad *) cdata;
1797 /* write info for each stream */
1798 fail = atoms_recov_write_trak_info (qtmux->moov_recov_file, qpad->trak);
1799 if (fail) {
1800 GST_WARNING_OBJECT (qtmux, "Failed to write trak info to recovery "
1801 "file");
1802 break;
1803 }
1804 }
1805
1806fail:
1807 /* cleanup */
1808 fclose (qtmux->moov_recov_file);
1809 qtmux->moov_recov_file = NULL;
1810 GST_WARNING_OBJECT (qtmux, "An error was detected while writing to "
1811 "recover file, moov recovery won't work");
1812}
Jan Schmidt3d59b5f2015-04-01 23:46:13 +11001813
Thiago Santose8c21f12009-10-15 21:11:16 -03001814static GstFlowReturn
1815gst_qt_mux_start_file (GstQTMux * qtmux)
1816{
Jan Schmidtfe739b72015-04-01 11:15:38 +11001817 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
Thiago Santose8c21f12009-10-15 21:11:16 -03001818 GstFlowReturn ret = GST_FLOW_OK;
Mark Nauwelaerts20e4bde2011-01-13 15:27:36 +01001819 GstCaps *caps;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02001820 GstSegment segment;
Tim-Philipp Müllerbc37b9f2012-09-23 15:57:35 +01001821 gchar s_id[32];
Thiago Santose8c21f12009-10-15 21:11:16 -03001822
1823 GST_DEBUG_OBJECT (qtmux, "starting file");
1824
Tim-Philipp Müllerbc37b9f2012-09-23 15:57:35 +01001825 /* stream-start (FIXME: create id based on input ids) */
1826 g_snprintf (s_id, sizeof (s_id), "qtmux-%08x", g_random_int ());
1827 gst_pad_push_event (qtmux->srcpad, gst_event_new_stream_start (s_id));
1828
Mark Nauwelaerts20e4bde2011-01-13 15:27:36 +01001829 caps = gst_caps_copy (gst_pad_get_pad_template_caps (qtmux->srcpad));
Tim-Philipp Müller025417f2011-04-13 15:18:11 +01001830 /* qtmux has structure with and without variant, remove all but the first */
1831 while (gst_caps_get_size (caps) > 1)
1832 gst_caps_remove_structure (caps, 1);
Mark Nauwelaerts20e4bde2011-01-13 15:27:36 +01001833 gst_pad_set_caps (qtmux->srcpad, caps);
1834 gst_caps_unref (caps);
1835
Jan Schmidtfe739b72015-04-01 11:15:38 +11001836 /* Default is 'normal' mode */
1837 qtmux->mux_mode = GST_QT_MUX_MODE_MOOV_AT_END;
1838
1839 /* Require a sensible fragment duration when muxing
1840 * using the ISML muxer */
1841 if (qtmux_klass->format == GST_QT_MUX_FORMAT_ISML &&
1842 qtmux->fragment_duration == 0)
1843 goto invalid_isml;
1844
1845 if (qtmux->fragment_duration > 0) {
1846 if (qtmux->streamable)
1847 qtmux->mux_mode = GST_QT_MUX_MODE_FRAGMENTED_STREAMABLE;
1848 else
1849 qtmux->mux_mode = GST_QT_MUX_MODE_FRAGMENTED;
1850 } else if (qtmux->fast_start) {
1851 qtmux->mux_mode = GST_QT_MUX_MODE_FAST_START;
1852 }
1853
1854 switch (qtmux->mux_mode) {
1855 case GST_QT_MUX_MODE_MOOV_AT_END:
1856 /* We have to be able to seek to rewrite the mdat header, or any
1857 * moov atom we write will not be visible in the file, because an
1858 * MDAT with 0 as the size covers the rest of the file. A file
1859 * with no moov is not playable, so error out now. */
1860 if (!gst_qt_mux_downstream_is_seekable (qtmux)) {
1861 GST_ELEMENT_ERROR (qtmux, STREAM, MUX,
1862 ("Downstream is not seekable - will not be able to create a playable file"),
1863 (NULL));
1864 return GST_FLOW_ERROR;
1865 }
1866 break;
1867 case GST_QT_MUX_MODE_FAST_START:
1868 case GST_QT_MUX_MODE_FRAGMENTED_STREAMABLE:
1869 break; /* Don't need seekability, ignore */
1870 case GST_QT_MUX_MODE_FRAGMENTED:
1871 if (!gst_qt_mux_downstream_is_seekable (qtmux)) {
Andoni Morales Alastrueycfefdae2013-09-17 12:17:54 +02001872 GST_WARNING_OBJECT (qtmux, "downstream is not seekable, but "
1873 "streamable=false. Will ignore that and create streamable output "
1874 "instead");
1875 qtmux->streamable = TRUE;
1876 g_object_notify (G_OBJECT (qtmux), "streamable");
1877 }
Jan Schmidtfe739b72015-04-01 11:15:38 +11001878 break;
Tim-Philipp Müller676b3712012-02-25 15:55:15 +00001879 }
1880
Thiago Santose8c21f12009-10-15 21:11:16 -03001881 /* let downstream know we think in BYTES and expect to do seeking later on */
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02001882 gst_segment_init (&segment, GST_FORMAT_BYTES);
1883 gst_pad_push_event (qtmux->srcpad, gst_event_new_segment (&segment));
Thiago Santose8c21f12009-10-15 21:11:16 -03001884
Thiago Santosb692f9f2009-12-12 16:07:15 -03001885 /* initialize our moov recovery file */
1886 GST_OBJECT_LOCK (qtmux);
1887 if (qtmux->moov_recov_file_path) {
Jan Schmidtfe739b72015-04-01 11:15:38 +11001888 gst_qt_mux_prepare_moov_recovery (qtmux);
Thiago Santosb692f9f2009-12-12 16:07:15 -03001889 }
1890 GST_OBJECT_UNLOCK (qtmux);
1891
Jan Schmidtfe739b72015-04-01 11:15:38 +11001892 /*
Thiago Santose8c21f12009-10-15 21:11:16 -03001893 * send mdat header if already needed, and mark position for later update.
1894 * We don't send ftyp now if we are on fast start mode, because we can
1895 * better fine tune using the information we gather to create the whole moov
1896 * atom.
1897 */
Jan Schmidtfe739b72015-04-01 11:15:38 +11001898 switch (qtmux->mux_mode) {
1899 case GST_QT_MUX_MODE_MOOV_AT_END:
1900 ret = gst_qt_mux_prepare_and_send_ftyp (qtmux);
1901 if (ret != GST_FLOW_OK)
1902 break;
Thiago Santose8c21f12009-10-15 21:11:16 -03001903
Jan Schmidtfe739b72015-04-01 11:15:38 +11001904 /* store the mdat position for rewriting later ... */
1905 qtmux->mdat_pos = qtmux->header_size;
1906 /* extended atom in case we go over 4GB while writing and need
1907 * the full 64-bit atom */
1908 ret = gst_qt_mux_send_mdat_header (qtmux, &qtmux->header_size, 0, TRUE);
1909 break;
1910 case GST_QT_MUX_MODE_FAST_START:
1911 GST_OBJECT_LOCK (qtmux);
1912 qtmux->fast_start_file = g_fopen (qtmux->fast_start_file_path, "wb+");
1913 if (!qtmux->fast_start_file)
1914 goto open_failed;
1915 GST_OBJECT_UNLOCK (qtmux);
1916 /* send a dummy buffer for preroll */
1917 ret = gst_qt_mux_send_buffer (qtmux, gst_buffer_new (), NULL, FALSE);
1918 break;
1919 case GST_QT_MUX_MODE_FRAGMENTED:
1920 case GST_QT_MUX_MODE_FRAGMENTED_STREAMABLE:
1921 ret = gst_qt_mux_prepare_and_send_ftyp (qtmux);
1922 if (ret != GST_FLOW_OK)
1923 break;
1924 /* well, it's moov pos if fragmented ... */
1925 qtmux->mdat_pos = qtmux->header_size;
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01001926 GST_DEBUG_OBJECT (qtmux, "fragment duration %d ms, writing headers",
1927 qtmux->fragment_duration);
1928 /* also used as snapshot marker to indicate fragmented file */
1929 qtmux->fragment_sequence = 1;
1930 /* prepare moov and/or tags */
1931 gst_qt_mux_configure_moov (qtmux, NULL);
1932 gst_qt_mux_setup_metadata (qtmux);
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +01001933 ret = gst_qt_mux_send_moov (qtmux, &qtmux->header_size, FALSE);
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01001934 if (ret != GST_FLOW_OK)
1935 return ret;
1936 /* extra atoms */
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +01001937 ret =
1938 gst_qt_mux_send_extra_atoms (qtmux, TRUE, &qtmux->header_size, FALSE);
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01001939 if (ret != GST_FLOW_OK)
Jan Schmidtfe739b72015-04-01 11:15:38 +11001940 break;
1941 /* prepare index if not streamable */
1942 if (qtmux->mux_mode == GST_QT_MUX_MODE_FRAGMENTED)
Marc-André Lureau61964eb2010-08-06 13:26:27 +02001943 qtmux->mfra = atom_mfra_new (qtmux->context);
Jan Schmidtfe739b72015-04-01 11:15:38 +11001944 break;
Thiago Santose8c21f12009-10-15 21:11:16 -03001945 }
1946
Thiago Santose8c21f12009-10-15 21:11:16 -03001947 return ret;
Thiago Santose8c21f12009-10-15 21:11:16 -03001948 /* ERRORS */
Jan Schmidtfe739b72015-04-01 11:15:38 +11001949invalid_isml:
1950 {
1951 GST_ELEMENT_ERROR (qtmux, STREAM, MUX,
1952 ("Cannot create an ISML file with 0 fragment duration"), (NULL));
1953 return GST_FLOW_ERROR;
1954 }
1955
Thiago Santose8c21f12009-10-15 21:11:16 -03001956open_failed:
1957 {
1958 GST_ELEMENT_ERROR (qtmux, RESOURCE, OPEN_READ_WRITE,
Jan Schmidtfe739b72015-04-01 11:15:38 +11001959 (("Could not open temporary file \"%s\""),
1960 qtmux->fast_start_file_path), GST_ERROR_SYSTEM);
Thiago Santose8c21f12009-10-15 21:11:16 -03001961 GST_OBJECT_UNLOCK (qtmux);
1962 return GST_FLOW_ERROR;
1963 }
1964}
1965
1966static GstFlowReturn
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001967gst_qt_mux_stop_file (GstQTMux * qtmux)
1968{
1969 gboolean ret = GST_FLOW_OK;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001970 guint64 offset = 0, size = 0;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001971 GSList *walk;
1972 gboolean large_file;
1973 guint32 timescale;
Thiago Santos8d80e932009-11-06 10:34:39 -03001974 GstClockTime first_ts = GST_CLOCK_TIME_NONE;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001975
Thiago Santos99e966e2014-02-06 12:15:22 -03001976 /* for setting some subtitles fields */
1977 guint max_width = 0;
1978 guint max_height = 0;
1979
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001980 GST_DEBUG_OBJECT (qtmux, "Updating remaining values and sending last data");
1981
1982 /* pushing last buffers for each pad */
1983 for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +02001984 GstCollectData *cdata = (GstCollectData *) walk->data;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001985 GstQTPad *qtpad = (GstQTPad *) cdata;
1986
Lasse Laukkanen056e9182011-09-16 16:53:22 +03001987 /* avoid add_buffer complaining if not negotiated
1988 * in which case no buffers either, so skipping */
1989 if (!qtpad->fourcc) {
1990 GST_DEBUG_OBJECT (qtmux, "Pad %s has never had buffers",
1991 GST_PAD_NAME (qtpad->collect.pad));
1992 continue;
1993 }
1994
1995 /* send last buffer; also flushes possibly queued buffers/ts */
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00001996 GST_DEBUG_OBJECT (qtmux, "Sending the last buffer for pad %s",
1997 GST_PAD_NAME (qtpad->collect.pad));
1998 ret = gst_qt_mux_add_buffer (qtmux, qtpad, NULL);
Lasse Laukkanen056e9182011-09-16 16:53:22 +03001999 if (ret != GST_FLOW_OK) {
Thiago Santos009d1042009-11-06 00:46:12 -03002000 GST_WARNING_OBJECT (qtmux, "Failed to send last buffer for %s, "
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002001 "flow return: %s", GST_PAD_NAME (qtpad->collect.pad),
2002 gst_flow_get_name (ret));
Lasse Laukkanen056e9182011-09-16 16:53:22 +03002003 }
Mark Nauwelaerts4df05e72011-04-15 12:34:05 +02002004
Lasse Laukkanen056e9182011-09-16 16:53:22 +03002005 /* having flushed above, can check for buffers now */
Mark Nauwelaerts4df05e72011-04-15 12:34:05 +02002006 if (!GST_CLOCK_TIME_IS_VALID (qtpad->first_ts)) {
2007 GST_DEBUG_OBJECT (qtmux, "Pad %s has no buffers",
2008 GST_PAD_NAME (qtpad->collect.pad));
2009 continue;
2010 }
2011
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002012 /* determine max stream duration */
2013 if (!GST_CLOCK_TIME_IS_VALID (first_ts) ||
2014 (GST_CLOCK_TIME_IS_VALID (qtpad->first_ts) &&
2015 qtpad->last_dts > first_ts)) {
2016 first_ts = qtpad->last_dts;
2017 }
Thiago Santos7a143ea2011-09-28 11:41:49 -03002018
Thiago Santos99e966e2014-02-06 12:15:22 -03002019 /* subtitles need to know the video width/height,
2020 * it is stored shifted 16 bits to the left according to the
2021 * spec */
2022 max_width = MAX (max_width, (qtpad->trak->tkhd.width >> 16));
2023 max_height = MAX (max_height, (qtpad->trak->tkhd.height >> 16));
2024
Thiago Santos7a143ea2011-09-28 11:41:49 -03002025 /* update average bitrate of streams if needed */
2026 {
2027 guint32 avgbitrate = 0;
2028 guint32 maxbitrate = qtpad->max_bitrate;
2029
2030 if (qtpad->avg_bitrate)
2031 avgbitrate = qtpad->avg_bitrate;
2032 else if (qtpad->total_duration > 0)
2033 avgbitrate = (guint32) gst_util_uint64_scale_round (qtpad->total_bytes,
2034 8 * GST_SECOND, qtpad->total_duration);
2035
2036 atom_trak_update_bitrates (qtpad->trak, avgbitrate, maxbitrate);
2037 }
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002038 }
2039
Thiago Santos99e966e2014-02-06 12:15:22 -03002040 /* need to update values on subtitle traks now that we know the
2041 * max width and height */
2042 for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
2043 GstCollectData *cdata = (GstCollectData *) walk->data;
2044 GstQTPad *qtpad = (GstQTPad *) cdata;
2045
2046 if (!qtpad->fourcc) {
2047 GST_DEBUG_OBJECT (qtmux, "Pad %s has never had buffers",
2048 GST_PAD_NAME (qtpad->collect.pad));
2049 continue;
2050 }
2051
2052 if (qtpad->fourcc == FOURCC_tx3g) {
2053 atom_trak_tx3g_update_dimension (qtpad->trak, max_width, max_height);
2054 }
2055 }
2056
Jan Schmidtfe739b72015-04-01 11:15:38 +11002057 switch (qtmux->mux_mode) {
2058 case GST_QT_MUX_MODE_FRAGMENTED_STREAMABLE:
2059 {
2060 /* Streamable mode; no need to write duration or MFRA */
2061 GST_DEBUG_OBJECT (qtmux, "streamable file; nothing to stop");
2062 return GST_FLOW_OK;
2063 }
2064 case GST_QT_MUX_MODE_FRAGMENTED:
2065 {
2066 GstSegment segment;
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +01002067 guint8 *data = NULL;
2068 GstBuffer *buf;
2069
2070 size = offset = 0;
2071 GST_DEBUG_OBJECT (qtmux, "adding mfra");
2072 if (!atom_mfra_copy_data (qtmux->mfra, &data, &size, &offset))
2073 goto serialize_error;
2074 buf = _gst_buffer_new_take_data (data, offset);
2075 ret = gst_qt_mux_send_buffer (qtmux, buf, NULL, FALSE);
2076 if (ret != GST_FLOW_OK)
2077 return ret;
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +01002078
Jan Schmidtfe739b72015-04-01 11:15:38 +11002079 timescale = qtmux->timescale;
2080 /* only mvex duration is updated,
2081 * mvhd should be consistent with empty moov
2082 * (but TODO maybe some clients do not handle that well ?) */
2083 qtmux->moov->mvex.mehd.fragment_duration =
2084 gst_util_uint64_scale (first_ts, timescale, GST_SECOND);
2085 GST_DEBUG_OBJECT (qtmux, "rewriting moov with mvex duration %"
2086 GST_TIME_FORMAT, GST_TIME_ARGS (first_ts));
2087 /* seek and rewrite the header */
2088 gst_segment_init (&segment, GST_FORMAT_BYTES);
2089 segment.start = qtmux->mdat_pos;
2090 gst_pad_push_event (qtmux->srcpad, gst_event_new_segment (&segment));
2091 /* no need to seek back */
2092 return gst_qt_mux_send_moov (qtmux, NULL, FALSE);
2093 }
2094 default:
2095 break;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002096 }
2097
Jan Schmidtfe739b72015-04-01 11:15:38 +11002098 /* Moov-at-end or fast-start mode from here down */
Mark Nauwelaerts69d07462010-11-09 16:49:07 +01002099 gst_qt_mux_configure_moov (qtmux, &timescale);
Jan Schmidtfe739b72015-04-01 11:15:38 +11002100 /* check for late streams. First, find the earliest start time */
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002101 first_ts = GST_CLOCK_TIME_NONE;
Thiago Santos8d80e932009-11-06 10:34:39 -03002102 for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +02002103 GstCollectData *cdata = (GstCollectData *) walk->data;
Thiago Santos8d80e932009-11-06 10:34:39 -03002104 GstQTPad *qtpad = (GstQTPad *) cdata;
2105
2106 if (!GST_CLOCK_TIME_IS_VALID (first_ts) ||
2107 (GST_CLOCK_TIME_IS_VALID (qtpad->first_ts) &&
2108 qtpad->first_ts < first_ts)) {
2109 first_ts = qtpad->first_ts;
2110 }
2111 }
Jan Schmidt3d7b3432015-04-01 00:58:52 +11002112
Thiago Santos8d80e932009-11-06 10:34:39 -03002113 GST_DEBUG_OBJECT (qtmux, "Media first ts selected: %" GST_TIME_FORMAT,
2114 GST_TIME_ARGS (first_ts));
Jan Schmidt3d7b3432015-04-01 00:58:52 +11002115 /* add/update EDTSs for late streams. configure_moov will have
2116 * set the trak durations above by summing the sample tables */
Thiago Santos8d80e932009-11-06 10:34:39 -03002117 for (walk = qtmux->collect->data; walk; walk = g_slist_next (walk)) {
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +02002118 GstCollectData *cdata = (GstCollectData *) walk->data;
Thiago Santos8d80e932009-11-06 10:34:39 -03002119 GstQTPad *qtpad = (GstQTPad *) cdata;
2120 guint32 lateness;
2121 guint32 duration;
2122
Jan Schmidt3d7b3432015-04-01 00:58:52 +11002123 if (GST_CLOCK_TIME_IS_VALID (qtpad->first_ts)
2124 && qtpad->first_ts > first_ts) {
Thiago Santos8d80e932009-11-06 10:34:39 -03002125 GST_DEBUG_OBJECT (qtmux, "Pad %s is a late stream by %" GST_TIME_FORMAT,
2126 GST_PAD_NAME (qtpad->collect.pad),
2127 GST_TIME_ARGS (qtpad->first_ts - first_ts));
Jan Schmidt3d7b3432015-04-01 00:58:52 +11002128 lateness =
2129 gst_util_uint64_scale_round (qtpad->first_ts - first_ts, timescale,
2130 GST_SECOND);
Thiago Santos8d80e932009-11-06 10:34:39 -03002131 duration = qtpad->trak->tkhd.duration;
Jan Schmidt3d7b3432015-04-01 00:58:52 +11002132 atom_trak_set_elst_entry (qtpad->trak, 0, lateness, (guint32) - 1,
Thiago Santos8d80e932009-11-06 10:34:39 -03002133 (guint32) (1 * 65536.0));
Jan Schmidt3d7b3432015-04-01 00:58:52 +11002134 atom_trak_set_elst_entry (qtpad->trak, 1, duration, 0,
Thiago Santos8d80e932009-11-06 10:34:39 -03002135 (guint32) (1 * 65536.0));
2136
2137 /* need to add the empty time to the trak duration */
Jan Schmidtd9e1aa42014-08-08 03:58:14 +10002138 duration += lateness;
2139
2140 qtpad->trak->tkhd.duration = duration;
2141
2142 /* And possibly grow the moov duration */
2143 if (duration > qtmux->moov->mvhd.time_info.duration) {
2144 qtmux->moov->mvhd.time_info.duration = duration;
2145 qtmux->moov->mvex.mehd.fragment_duration = duration;
2146 }
Thiago Santos8d80e932009-11-06 10:34:39 -03002147 }
2148 }
2149
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002150 /* tags into file metadata */
2151 gst_qt_mux_setup_metadata (qtmux);
Mark Nauwelaertsc3f462d2009-06-11 15:54:42 +02002152 large_file = (qtmux->mdat_size > MDAT_LARGE_FILE_LIMIT);
Jan Schmidtfe739b72015-04-01 11:15:38 +11002153
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002154 /* if faststart, update the offset of the atoms in the movie with the offset
Thiago Santose8c21f12009-10-15 21:11:16 -03002155 * that the movie headers before mdat will cause.
2156 * Also, send the ftyp */
Jan Schmidtfe739b72015-04-01 11:15:38 +11002157 if (qtmux->mux_mode == GST_QT_MUX_MODE_FAST_START) {
Thiago Santose8c21f12009-10-15 21:11:16 -03002158 GstFlowReturn flow_ret;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002159 offset = size = 0;
Thiago Santose8c21f12009-10-15 21:11:16 -03002160
2161 flow_ret = gst_qt_mux_prepare_and_send_ftyp (qtmux);
2162 if (flow_ret != GST_FLOW_OK) {
2163 goto ftyp_error;
2164 }
2165 /* copy into NULL to obtain size */
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002166 if (!atom_moov_copy_data (qtmux->moov, NULL, &size, &offset))
2167 goto serialize_error;
2168 GST_DEBUG_OBJECT (qtmux, "calculated moov atom size %" G_GUINT64_FORMAT,
Mark Nauwelaerts7bf67a62009-11-19 11:36:14 +01002169 offset);
Mark Nauwelaertsc3f462d2009-06-11 15:54:42 +02002170 offset += qtmux->header_size + (large_file ? 16 : 8);
Thiago Santos7065a652010-09-15 17:54:49 -03002171
2172 /* sum up with the extra atoms size */
Mark Nauwelaerts35e028f2010-11-09 15:54:44 +01002173 ret = gst_qt_mux_send_extra_atoms (qtmux, FALSE, &offset, FALSE);
2174 if (ret != GST_FLOW_OK)
2175 return ret;
2176 } else {
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002177 offset = qtmux->header_size;
Mark Nauwelaerts35e028f2010-11-09 15:54:44 +01002178 }
Jan Schmidtfe739b72015-04-01 11:15:38 +11002179
2180 /* Now that we know the size of moov + extra atoms, we can adjust
2181 * the chunk offsets stored into the moov */
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002182 atom_moov_chunks_add_offset (qtmux->moov, offset);
2183
Jan Schmidtfe739b72015-04-01 11:15:38 +11002184 /* write out moov and extra atoms */
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002185 /* note: as of this point, we no longer care about tracking written data size,
2186 * since there is no more use for it anyway */
Mark Nauwelaerts69d07462010-11-09 16:49:07 +01002187 ret = gst_qt_mux_send_moov (qtmux, NULL, FALSE);
Mark Nauwelaerts35e028f2010-11-09 15:54:44 +01002188 if (ret != GST_FLOW_OK)
2189 return ret;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002190
Mark Nauwelaerts35e028f2010-11-09 15:54:44 +01002191 /* extra atoms */
2192 ret = gst_qt_mux_send_extra_atoms (qtmux, TRUE, NULL, FALSE);
2193 if (ret != GST_FLOW_OK)
2194 return ret;
Thiago Santos7065a652010-09-15 17:54:49 -03002195
Jan Schmidtfe739b72015-04-01 11:15:38 +11002196 switch (qtmux->mux_mode) {
2197 case GST_QT_MUX_MODE_MOOV_AT_END:
2198 {
2199 /* mdat needs update iff not using faststart */
2200 GST_DEBUG_OBJECT (qtmux, "updating mdat size");
2201 ret = gst_qt_mux_update_mdat_size (qtmux, qtmux->mdat_pos,
2202 qtmux->mdat_size, NULL);
2203 /* note; no seeking back to the end of file is done,
2204 * since we no longer write anything anyway */
2205 break;
2206 }
2207 case GST_QT_MUX_MODE_FAST_START:
2208 {
2209 /* send mdat atom and move buffered data into it */
2210 /* mdat_size = accumulated (buffered data) */
2211 ret = gst_qt_mux_send_mdat_header (qtmux, NULL, qtmux->mdat_size,
2212 large_file);
2213 if (ret != GST_FLOW_OK)
2214 return ret;
2215 ret = gst_qt_mux_send_buffered_data (qtmux, NULL);
2216 if (ret != GST_FLOW_OK)
2217 return ret;
2218 break;
2219 }
2220 default:
2221 g_assert_not_reached ();
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002222 }
2223
2224 return ret;
2225
2226 /* ERRORS */
2227serialize_error:
2228 {
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002229 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
2230 ("Failed to serialize moov"));
2231 return GST_FLOW_ERROR;
2232 }
Thiago Santose8c21f12009-10-15 21:11:16 -03002233ftyp_error:
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002234 {
Thiago Santose8c21f12009-10-15 21:11:16 -03002235 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL), ("Failed to send ftyp"));
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002236 return GST_FLOW_ERROR;
2237 }
2238}
2239
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002240static GstFlowReturn
2241gst_qt_mux_pad_fragment_add_buffer (GstQTMux * qtmux, GstQTPad * pad,
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +01002242 GstBuffer * buf, gboolean force, guint32 nsamples, gint64 dts,
Mark Nauwelaertsb6945d42011-01-03 16:56:57 +01002243 guint32 delta, guint32 size, gboolean sync, gint64 pts_offset)
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002244{
2245 GstFlowReturn ret = GST_FLOW_OK;
2246
2247 /* setup if needed */
2248 if (G_UNLIKELY (!pad->traf || force))
2249 goto init;
2250
2251flush:
2252 /* flush pad fragment if threshold reached,
2253 * or at new keyframe if we should be minding those in the first place */
2254 if (G_UNLIKELY (force || (sync && pad->sync) ||
2255 pad->fragment_duration < (gint64) delta)) {
2256 AtomMOOF *moof;
2257 guint64 size = 0, offset = 0;
2258 guint8 *data = NULL;
2259 GstBuffer *buffer;
2260 guint i, total_size;
2261
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +01002262 /* now we know where moof ends up, update offset in tfra */
2263 if (pad->tfra)
2264 atom_tfra_update_offset (pad->tfra, qtmux->header_size);
2265
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002266 moof = atom_moof_new (qtmux->context, qtmux->fragment_sequence);
2267 /* takes ownership */
2268 atom_moof_add_traf (moof, pad->traf);
2269 pad->traf = NULL;
2270 atom_moof_copy_data (moof, &data, &size, &offset);
2271 buffer = _gst_buffer_new_take_data (data, offset);
Matej Knopp1e5dd9e2011-11-21 20:31:31 +01002272 GST_LOG_OBJECT (qtmux, "writing moof size %" G_GSIZE_FORMAT,
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02002273 gst_buffer_get_size (buffer));
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +01002274 ret = gst_qt_mux_send_buffer (qtmux, buffer, &qtmux->header_size, FALSE);
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002275
2276 /* and actual data */
2277 total_size = 0;
2278 for (i = 0; i < atom_array_get_len (&pad->fragment_buffers); i++) {
2279 total_size +=
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02002280 gst_buffer_get_size (atom_array_index (&pad->fragment_buffers, i));
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002281 }
2282
2283 GST_LOG_OBJECT (qtmux, "writing %d buffers, total_size %d",
2284 atom_array_get_len (&pad->fragment_buffers), total_size);
2285 if (ret == GST_FLOW_OK)
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +01002286 ret = gst_qt_mux_send_mdat_header (qtmux, &qtmux->header_size, total_size,
2287 FALSE);
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002288 for (i = 0; i < atom_array_get_len (&pad->fragment_buffers); i++) {
2289 if (G_LIKELY (ret == GST_FLOW_OK))
2290 ret = gst_qt_mux_send_buffer (qtmux,
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +01002291 atom_array_index (&pad->fragment_buffers, i), &qtmux->header_size,
2292 FALSE);
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002293 else
2294 gst_buffer_unref (atom_array_index (&pad->fragment_buffers, i));
2295 }
2296
2297 atom_array_clear (&pad->fragment_buffers);
2298 atom_moof_free (moof);
2299 qtmux->fragment_sequence++;
2300 force = FALSE;
2301 }
2302
2303init:
2304 if (G_UNLIKELY (!pad->traf)) {
2305 GST_LOG_OBJECT (qtmux, "setting up new fragment");
2306 pad->traf = atom_traf_new (qtmux->context, atom_trak_get_id (pad->trak));
2307 atom_array_init (&pad->fragment_buffers, 512);
2308 pad->fragment_duration = gst_util_uint64_scale (qtmux->fragment_duration,
2309 atom_trak_get_timescale (pad->trak), 1000);
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +01002310
2311 if (G_UNLIKELY (qtmux->mfra && !pad->tfra)) {
2312 pad->tfra = atom_tfra_new (qtmux->context, atom_trak_get_id (pad->trak));
2313 atom_mfra_add_tfra (qtmux->mfra, pad->tfra);
2314 }
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002315 }
2316
2317 /* add buffer and metadata */
Mark Nauwelaertsb6945d42011-01-03 16:56:57 +01002318 atom_traf_add_samples (pad->traf, delta, size, sync, pts_offset,
Mark Nauwelaerts92b8a002010-11-19 17:41:41 +01002319 pad->sync && sync);
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002320 atom_array_append (&pad->fragment_buffers, buf, 256);
2321 pad->fragment_duration -= delta;
2322
Mark Nauwelaerts26a281b2010-11-18 16:48:06 +01002323 if (pad->tfra) {
2324 guint32 sn = atom_traf_get_sample_num (pad->traf);
2325
2326 if ((sync && pad->sync) || (sn == 1 && !pad->sync))
2327 atom_tfra_add_entry (pad->tfra, dts, sn);
2328 }
2329
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002330 if (G_UNLIKELY (force))
2331 goto flush;
2332
2333 return ret;
2334}
2335
Thiago Santosf89ba822014-02-07 01:49:26 -03002336static GstFlowReturn
2337gst_qt_mux_register_and_push_sample (GstQTMux * qtmux, GstQTPad * pad,
2338 GstBuffer * buffer, gboolean is_last_buffer, guint nsamples,
2339 gint64 last_dts, gint64 scaled_duration, guint sample_size,
Matej Knopp2505e342014-12-09 02:43:00 +01002340 guint64 chunk_offset, gboolean sync, gboolean do_pts, gint64 pts_offset)
Thiago Santosf89ba822014-02-07 01:49:26 -03002341{
2342 GstFlowReturn ret = GST_FLOW_OK;
2343
2344 /* note that a new chunk is started each time (not fancy but works) */
2345 if (qtmux->moov_recov_file) {
2346 if (!atoms_recov_write_trak_samples (qtmux->moov_recov_file, pad->trak,
2347 nsamples, (gint32) scaled_duration, sample_size, chunk_offset, sync,
2348 do_pts, pts_offset)) {
2349 GST_WARNING_OBJECT (qtmux, "Failed to write sample information to "
2350 "recovery file, disabling recovery");
2351 fclose (qtmux->moov_recov_file);
2352 qtmux->moov_recov_file = NULL;
2353 }
2354 }
2355
Jan Schmidtfe739b72015-04-01 11:15:38 +11002356 switch (qtmux->mux_mode) {
2357 case GST_QT_MUX_MODE_MOOV_AT_END:
2358 case GST_QT_MUX_MODE_FAST_START:
2359
2360 atom_trak_add_samples (pad->trak, nsamples, (gint32) scaled_duration,
2361 sample_size, chunk_offset, sync, pts_offset);
2362 ret = gst_qt_mux_send_buffer (qtmux, buffer, &qtmux->mdat_size, TRUE);
2363 break;
2364 case GST_QT_MUX_MODE_FRAGMENTED:
2365 case GST_QT_MUX_MODE_FRAGMENTED_STREAMABLE:
2366 /* ensure that always sync samples are marked as such */
2367 ret = gst_qt_mux_pad_fragment_add_buffer (qtmux, pad, buffer,
2368 is_last_buffer, nsamples, last_dts, (gint32) scaled_duration,
2369 sample_size, !pad->sync || sync, pts_offset);
2370 break;
Thiago Santosf89ba822014-02-07 01:49:26 -03002371 }
2372
2373 return ret;
2374}
2375
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002376/*
2377 * Here we push the buffer and update the tables in the track atoms
2378 */
2379static GstFlowReturn
2380gst_qt_mux_add_buffer (GstQTMux * qtmux, GstQTPad * pad, GstBuffer * buf)
2381{
2382 GstBuffer *last_buf = NULL;
2383 GstClockTime duration;
2384 guint nsamples, sample_size;
Mark Nauwelaertsae644932011-01-03 17:24:23 +01002385 guint64 chunk_offset;
2386 gint64 last_dts, scaled_duration;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002387 gint64 pts_offset = 0;
Thiago Santos39c09282015-04-07 16:12:40 -03002388 gboolean sync = FALSE;
Mark Nauwelaerts4df05e72011-04-15 12:34:05 +02002389 GstFlowReturn ret = GST_FLOW_OK;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002390
2391 if (!pad->fourcc)
2392 goto not_negotiated;
2393
Thiago Santosc5f6e742009-12-10 22:20:45 -03002394 /* if this pad has a prepare function, call it */
2395 if (pad->prepare_buf_func != NULL) {
2396 buf = pad->prepare_buf_func (pad, buf, qtmux);
2397 }
2398
Mark Nauwelaerts4df05e72011-04-15 12:34:05 +02002399 last_buf = pad->last_buf;
Mark Nauwelaertsae644932011-01-03 17:24:23 +01002400
Matej Knopp9f85dfd2014-09-23 23:33:37 +02002401 /* DTS delta is used to calculate sample duration.
2402 * If buffer has missing DTS, we take either segment start or
2403 * previous buffer end time, whichever is later.
2404 * This must only be done for non sparse streams, sparse streams
2405 * can have gaps between buffers (which is handled later by adding
2406 * extra empty buffer with duration that fills the gap). */
2407 if (!pad->sparse && buf && !GST_BUFFER_DTS_IS_VALID (buf)) {
Matej Knopp4e3c13c2013-09-14 15:54:29 +02002408 GstClockTime last_buf_duration = last_buf
2409 && GST_BUFFER_DURATION_IS_VALID (last_buf) ?
2410 GST_BUFFER_DURATION (last_buf) : 0;
2411
2412 buf = gst_buffer_make_writable (buf);
Thiago Santos5780afe2015-04-07 00:53:35 -03002413 GST_BUFFER_DTS (buf) = 0; /* running-time 0 */
Matej Knopp4e3c13c2013-09-14 15:54:29 +02002414
2415 if (last_buf
2416 && (GST_BUFFER_DTS (last_buf) + last_buf_duration) >
2417 GST_BUFFER_DTS (buf)) {
2418 GST_BUFFER_DTS (buf) = GST_BUFFER_DTS (last_buf) + last_buf_duration;
2419 }
Thiago Santosf5f27f72014-02-07 10:07:41 -03002420
2421 if (GST_BUFFER_PTS_IS_VALID (buf))
2422 GST_BUFFER_DTS (buf) = MIN (GST_BUFFER_DTS (buf), GST_BUFFER_PTS (buf));
Matej Knopp4e3c13c2013-09-14 15:54:29 +02002423 }
2424
Matej Knopp39f7e522013-09-14 15:55:22 +02002425 if (last_buf && !buf && !GST_BUFFER_DURATION_IS_VALID (last_buf)) {
2426 /* this is last buffer; there is no next buffer so we need valid number as duration */
2427 last_buf = gst_buffer_make_writable (last_buf);
2428 GST_BUFFER_DURATION (last_buf) = 0;
2429 }
2430
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002431 if (last_buf == NULL) {
2432#ifndef GST_DISABLE_GST_DEBUG
2433 if (buf == NULL) {
2434 GST_DEBUG_OBJECT (qtmux, "Pad %s has no previous buffer stored and "
2435 "received NULL buffer, doing nothing",
2436 GST_PAD_NAME (pad->collect.pad));
2437 } else {
2438 GST_LOG_OBJECT (qtmux,
2439 "Pad %s has no previous buffer stored, storing now",
2440 GST_PAD_NAME (pad->collect.pad));
2441 }
2442#endif
2443 pad->last_buf = buf;
Mark Nauwelaerts4df05e72011-04-15 12:34:05 +02002444 goto exit;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002445 } else
2446 gst_buffer_ref (last_buf);
2447
Thiago Santos261d11a2011-09-09 09:12:56 -03002448 /* if this is the first buffer, store the timestamp */
2449 if (G_UNLIKELY (pad->first_ts == GST_CLOCK_TIME_NONE) && last_buf) {
Thiago Santosf5f27f72014-02-07 10:07:41 -03002450 if (GST_BUFFER_DTS_IS_VALID (last_buf)) {
Matej Knoppb3638322013-09-14 15:56:04 +02002451 pad->first_ts = GST_BUFFER_DTS (last_buf);
2452 } else if (GST_BUFFER_PTS_IS_VALID (last_buf)) {
David Schleef258c40c2013-02-14 16:34:34 -08002453 pad->first_ts = GST_BUFFER_PTS (last_buf);
Matej Knoppb3638322013-09-14 15:56:04 +02002454 }
2455
2456 if (GST_CLOCK_TIME_IS_VALID (pad->first_ts)) {
David Schleefee56a7c2013-03-13 17:08:03 -07002457 GST_DEBUG ("setting first_ts to %" G_GUINT64_FORMAT, pad->first_ts);
Thiago Santos261d11a2011-09-09 09:12:56 -03002458 } else {
Thiago Santos5780afe2015-04-07 00:53:35 -03002459 GST_WARNING_OBJECT (qtmux, "First buffer for pad %s has no timestamp, "
Thiago Santos261d11a2011-09-09 09:12:56 -03002460 "using 0 as first timestamp", GST_PAD_NAME (pad->collect.pad));
2461 pad->first_ts = 0;
2462 }
2463 GST_DEBUG_OBJECT (qtmux, "Stored first timestamp for pad %s %"
2464 GST_TIME_FORMAT, GST_PAD_NAME (pad->collect.pad),
2465 GST_TIME_ARGS (pad->first_ts));
Thiago Santos261d11a2011-09-09 09:12:56 -03002466 }
2467
David Schleefee56a7c2013-03-13 17:08:03 -07002468 if (last_buf && buf && GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (buf)) &&
2469 GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (last_buf)) &&
2470 GST_BUFFER_DTS (buf) < GST_BUFFER_DTS (last_buf)) {
2471 GST_ERROR ("decreasing DTS value %" GST_TIME_FORMAT " < %" GST_TIME_FORMAT,
2472 GST_TIME_ARGS (GST_BUFFER_DTS (buf)),
2473 GST_TIME_ARGS (GST_BUFFER_DTS (last_buf)));
2474 GST_BUFFER_DTS (buf) = GST_BUFFER_DTS (last_buf);
2475 }
2476
Matej Knopp56865122013-03-23 05:22:23 +01002477 /* duration actually means time delta between samples, so we calculate
2478 * the duration based on the difference in DTS or PTS, falling back
2479 * to DURATION if the other two don't exist, such as with the last
2480 * sample before EOS. */
Thiago Santosf89ba822014-02-07 01:49:26 -03002481 duration = GST_BUFFER_DURATION (last_buf);
2482 if (!pad->sparse) {
2483 if (last_buf && buf && GST_BUFFER_DTS_IS_VALID (buf)
2484 && GST_BUFFER_DTS_IS_VALID (last_buf))
2485 duration = GST_BUFFER_DTS (buf) - GST_BUFFER_DTS (last_buf);
2486 else if (last_buf && buf && GST_BUFFER_PTS_IS_VALID (buf)
2487 && GST_BUFFER_PTS_IS_VALID (last_buf))
2488 duration = GST_BUFFER_PTS (buf) - GST_BUFFER_PTS (last_buf);
2489 }
2490
2491 gst_buffer_replace (&pad->last_buf, buf);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002492
Thiago Santos7a143ea2011-09-28 11:41:49 -03002493 /* for computing the avg bitrate */
2494 if (G_LIKELY (last_buf)) {
Wim Taymans94021222011-10-06 12:26:33 +02002495 pad->total_bytes += gst_buffer_get_size (last_buf);
Thiago Santos7a143ea2011-09-28 11:41:49 -03002496 pad->total_duration += duration;
2497 }
2498
Thiago Santos79a90312009-11-25 21:41:27 -03002499 last_dts = gst_util_uint64_scale_round (pad->last_dts,
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002500 atom_trak_get_timescale (pad->trak), GST_SECOND);
2501
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01002502 /* fragments only deal with 1 buffer == 1 chunk (== 1 sample) */
2503 if (pad->sample_size && !qtmux->fragment_sequence) {
Michael Smithd784a8d2010-01-27 15:37:37 -08002504 /* Constant size packets: usually raw audio (with many samples per
2505 buffer (= chunk)), but can also be fixed-packet-size codecs like ADPCM
2506 */
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002507 sample_size = pad->sample_size;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02002508 if (gst_buffer_get_size (last_buf) % sample_size != 0)
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002509 goto fragmented_sample;
2510 /* note: qt raw audio storage warps it implicitly into a timewise
2511 * perfect stream, discarding buffer times */
Michael Smithd784a8d2010-01-27 15:37:37 -08002512 if (GST_BUFFER_DURATION (last_buf) != GST_CLOCK_TIME_NONE) {
2513 nsamples = gst_util_uint64_scale_round (GST_BUFFER_DURATION (last_buf),
2514 atom_trak_get_timescale (pad->trak), GST_SECOND);
2515 } else {
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02002516 nsamples = gst_buffer_get_size (last_buf) / sample_size;
Michael Smithd784a8d2010-01-27 15:37:37 -08002517 }
Vincent Penquerc'h1be86eb2014-05-02 10:21:09 +01002518 if (nsamples > 0)
2519 duration = GST_BUFFER_DURATION (last_buf) / nsamples;
2520 else
2521 duration = 0;
Michael Smithd784a8d2010-01-27 15:37:37 -08002522
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002523 /* timescale = samplerate */
2524 scaled_duration = 1;
David Schleefb11186e2008-11-15 02:56:31 +00002525 pad->last_dts += duration * nsamples;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002526 } else {
2527 nsamples = 1;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02002528 sample_size = gst_buffer_get_size (last_buf);
Thiago Santosf5f27f72014-02-07 10:07:41 -03002529 if ((pad->last_buf && GST_BUFFER_DTS_IS_VALID (pad->last_buf))
2530 || GST_BUFFER_DTS_IS_VALID (last_buf)) {
David Schleefb11186e2008-11-15 02:56:31 +00002531 gint64 scaled_dts;
Thiago Santosf5f27f72014-02-07 10:07:41 -03002532 if (pad->last_buf && GST_BUFFER_DTS_IS_VALID (pad->last_buf)) {
Matej Knopp85728c02013-09-03 18:14:04 +02002533 pad->last_dts = GST_BUFFER_DTS (pad->last_buf);
David Schleefee56a7c2013-03-13 17:08:03 -07002534 } else {
2535 pad->last_dts = GST_BUFFER_DTS (last_buf) +
2536 GST_BUFFER_DURATION (last_buf);
2537 }
David Schleefb11186e2008-11-15 02:56:31 +00002538 if ((gint64) (pad->last_dts) < 0) {
Thiago Santos79a90312009-11-25 21:41:27 -03002539 scaled_dts = -gst_util_uint64_scale_round (-pad->last_dts,
David Schleefb11186e2008-11-15 02:56:31 +00002540 atom_trak_get_timescale (pad->trak), GST_SECOND);
2541 } else {
Thiago Santos79a90312009-11-25 21:41:27 -03002542 scaled_dts = gst_util_uint64_scale_round (pad->last_dts,
David Schleefb11186e2008-11-15 02:56:31 +00002543 atom_trak_get_timescale (pad->trak), GST_SECOND);
2544 }
2545 scaled_duration = scaled_dts - last_dts;
2546 last_dts = scaled_dts;
2547 } else {
2548 /* first convert intended timestamp (in GstClockTime resolution) to
2549 * trak timescale, then derive delta;
2550 * this ensures sums of (scale)delta add up to converted timestamp,
2551 * which only deviates at most 1/scale from timestamp itself */
Thiago Santos79a90312009-11-25 21:41:27 -03002552 scaled_duration = gst_util_uint64_scale_round (pad->last_dts + duration,
David Schleefb11186e2008-11-15 02:56:31 +00002553 atom_trak_get_timescale (pad->trak), GST_SECOND) - last_dts;
2554 pad->last_dts += duration;
2555 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002556 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002557 chunk_offset = qtmux->mdat_size;
2558
2559 GST_LOG_OBJECT (qtmux,
2560 "Pad (%s) dts updated to %" GST_TIME_FORMAT,
2561 GST_PAD_NAME (pad->collect.pad), GST_TIME_ARGS (pad->last_dts));
2562 GST_LOG_OBJECT (qtmux,
2563 "Adding %d samples to track, duration: %" G_GUINT64_FORMAT
2564 " size: %" G_GUINT32_FORMAT " chunk offset: %" G_GUINT64_FORMAT,
2565 nsamples, scaled_duration, sample_size, chunk_offset);
2566
2567 /* might be a sync sample */
2568 if (pad->sync &&
2569 !GST_BUFFER_FLAG_IS_SET (last_buf, GST_BUFFER_FLAG_DELTA_UNIT)) {
2570 GST_LOG_OBJECT (qtmux, "Adding new sync sample entry for track of pad %s",
2571 GST_PAD_NAME (pad->collect.pad));
2572 sync = TRUE;
2573 }
2574
David Schleef258c40c2013-02-14 16:34:34 -08002575 if (GST_CLOCK_TIME_IS_VALID (GST_BUFFER_DTS (last_buf))) {
David Schleef258c40c2013-02-14 16:34:34 -08002576 last_dts = gst_util_uint64_scale_round (GST_BUFFER_DTS (last_buf),
2577 atom_trak_get_timescale (pad->trak), GST_SECOND);
2578 pts_offset =
2579 (gint64) (gst_util_uint64_scale_round (GST_BUFFER_PTS (last_buf),
2580 atom_trak_get_timescale (pad->trak), GST_SECOND) - last_dts);
2581
2582 } else {
2583 pts_offset = 0;
David Schleef258c40c2013-02-14 16:34:34 -08002584 last_dts = gst_util_uint64_scale_round (GST_BUFFER_PTS (last_buf),
2585 atom_trak_get_timescale (pad->trak), GST_SECOND);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002586 }
David Schleef258c40c2013-02-14 16:34:34 -08002587 GST_DEBUG ("dts: %" GST_TIME_FORMAT " pts: %" GST_TIME_FORMAT
2588 " timebase_dts: %d pts_offset: %d",
2589 GST_TIME_ARGS (GST_BUFFER_DTS (last_buf)),
2590 GST_TIME_ARGS (GST_BUFFER_PTS (last_buf)),
2591 (int) (last_dts), (int) (pts_offset));
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002592
Thiago Santos4ff2fad2009-10-16 10:47:32 -03002593 /*
2594 * Each buffer starts a new chunk, so we can assume the buffer
2595 * duration is the chunk duration
2596 */
2597 if (GST_CLOCK_TIME_IS_VALID (duration) && (duration > qtmux->longest_chunk ||
2598 !GST_CLOCK_TIME_IS_VALID (qtmux->longest_chunk))) {
2599 GST_DEBUG_OBJECT (qtmux, "New longest chunk found: %" GST_TIME_FORMAT
2600 ", pad %s", GST_TIME_ARGS (duration), GST_PAD_NAME (pad->collect.pad));
2601 qtmux->longest_chunk = duration;
2602 }
2603
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002604 /* now we go and register this buffer/sample all over */
Thiago Santosf89ba822014-02-07 01:49:26 -03002605 ret = gst_qt_mux_register_and_push_sample (qtmux, pad, last_buf,
2606 buf == NULL, nsamples, last_dts, scaled_duration, sample_size,
Thiago Santos39c09282015-04-07 16:12:40 -03002607 chunk_offset, sync, TRUE, pts_offset);
Thiago Santosf89ba822014-02-07 01:49:26 -03002608
2609 /* if this is sparse and we have a next buffer, check if there is any gap
2610 * between them to insert an empty sample */
2611 if (pad->sparse && buf) {
2612 if (pad->create_empty_buffer) {
2613 GstBuffer *empty_buf;
2614 gint64 empty_duration =
2615 GST_BUFFER_TIMESTAMP (buf) - (GST_BUFFER_TIMESTAMP (last_buf) +
2616 duration);
2617 gint64 empty_duration_scaled;
2618
2619 empty_buf = pad->create_empty_buffer (pad, empty_duration);
2620
2621 empty_duration_scaled = gst_util_uint64_scale_round (empty_duration,
2622 atom_trak_get_timescale (pad->trak), GST_SECOND);
2623
2624 pad->total_bytes += gst_buffer_get_size (empty_buf);
2625 pad->total_duration += duration;
2626
2627 ret =
2628 gst_qt_mux_register_and_push_sample (qtmux, pad, empty_buf, FALSE, 1,
2629 last_dts + scaled_duration, empty_duration_scaled,
Thiago Santos39c09282015-04-07 16:12:40 -03002630 gst_buffer_get_size (empty_buf), qtmux->mdat_size, sync, TRUE, 0);
Thiago Santosf89ba822014-02-07 01:49:26 -03002631 } else {
2632 /* our only case currently is tx3g subtitles, so there is no reason to fill this yet */
2633 g_assert_not_reached ();
2634 GST_WARNING_OBJECT (qtmux,
2635 "no empty buffer creation function found for pad %s",
2636 GST_PAD_NAME (pad->collect.pad));
Thiago Santosb692f9f2009-12-12 16:07:15 -03002637 }
2638 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002639
2640 if (buf)
2641 gst_buffer_unref (buf);
2642
Mark Nauwelaerts4df05e72011-04-15 12:34:05 +02002643exit:
Mark Nauwelaertsae644932011-01-03 17:24:23 +01002644
2645 return ret;
2646
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002647 /* ERRORS */
2648bail:
2649 {
2650 if (buf)
2651 gst_buffer_unref (buf);
2652 gst_buffer_unref (last_buf);
2653 return GST_FLOW_ERROR;
2654 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002655fragmented_sample:
2656 {
2657 GST_ELEMENT_ERROR (qtmux, STREAM, MUX, (NULL),
2658 ("Audio buffer contains fragmented sample."));
2659 goto bail;
2660 }
2661not_negotiated:
2662 {
2663 GST_ELEMENT_ERROR (qtmux, CORE, NEGOTIATION, (NULL),
2664 ("format wasn't negotiated before buffer flow on pad %s",
2665 GST_PAD_NAME (pad->collect.pad)));
Thiago Santos009d1042009-11-06 00:46:12 -03002666 if (buf)
2667 gst_buffer_unref (buf);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002668 return GST_FLOW_NOT_NEGOTIATED;
2669 }
2670}
2671
2672static GstFlowReturn
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +02002673gst_qt_mux_handle_buffer (GstCollectPads * pads, GstCollectData * cdata,
Mark Nauwelaerts0c7a4912011-12-16 18:03:01 +01002674 GstBuffer * buf, gpointer user_data)
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002675{
2676 GstFlowReturn ret = GST_FLOW_OK;
2677 GstQTMux *qtmux = GST_QT_MUX_CAST (user_data);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002678 GstQTPad *best_pad = NULL;
Mark Nauwelaerts0c7a4912011-12-16 18:03:01 +01002679 GstClockTime best_time = GST_CLOCK_TIME_NONE;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002680
2681 if (G_UNLIKELY (qtmux->state == GST_QT_MUX_STATE_STARTED)) {
2682 if ((ret = gst_qt_mux_start_file (qtmux)) != GST_FLOW_OK)
2683 return ret;
2684 else
2685 qtmux->state = GST_QT_MUX_STATE_DATA;
2686 }
2687
2688 if (G_UNLIKELY (qtmux->state == GST_QT_MUX_STATE_EOS))
Wim Taymans5fd2b7a2012-01-03 15:26:21 +01002689 return GST_FLOW_EOS;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002690
Mark Nauwelaerts0c7a4912011-12-16 18:03:01 +01002691 best_pad = (GstQTPad *) cdata;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002692
Mark Nauwelaerts0c7a4912011-12-16 18:03:01 +01002693 /* clipping already converted to running time */
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002694 if (best_pad != NULL) {
Mark Nauwelaerts0c7a4912011-12-16 18:03:01 +01002695 g_assert (buf);
David Schleef258c40c2013-02-14 16:34:34 -08002696 best_time = GST_BUFFER_PTS (buf);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002697 GST_LOG_OBJECT (qtmux, "selected pad %s with time %" GST_TIME_FORMAT,
2698 GST_PAD_NAME (best_pad->collect.pad), GST_TIME_ARGS (best_time));
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002699 ret = gst_qt_mux_add_buffer (qtmux, best_pad, buf);
2700 } else {
2701 ret = gst_qt_mux_stop_file (qtmux);
2702 if (ret == GST_FLOW_OK) {
Thiago Santos7065a652010-09-15 17:54:49 -03002703 GST_DEBUG_OBJECT (qtmux, "Pushing eos");
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002704 gst_pad_push_event (qtmux->srcpad, gst_event_new_eos ());
Wim Taymans5fd2b7a2012-01-03 15:26:21 +01002705 ret = GST_FLOW_EOS;
Thiago Santos7065a652010-09-15 17:54:49 -03002706 } else {
2707 GST_WARNING_OBJECT (qtmux, "Failed to stop file: %s",
2708 gst_flow_get_name (ret));
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002709 }
2710 qtmux->state = GST_QT_MUX_STATE_EOS;
2711 }
2712
2713 return ret;
2714}
2715
2716static gboolean
Thiago Santosa5f22f02010-01-13 23:33:51 -03002717check_field (GQuark field_id, const GValue * value, gpointer user_data)
2718{
2719 GstStructure *structure = (GstStructure *) user_data;
2720 const GValue *other = gst_structure_id_get_value (structure, field_id);
2721 if (other == NULL)
2722 return FALSE;
2723 return gst_value_compare (value, other) == GST_VALUE_EQUAL;
2724}
2725
2726static gboolean
2727gst_qtmux_caps_is_subset_full (GstQTMux * qtmux, GstCaps * subset,
2728 GstCaps * superset)
2729{
2730 GstStructure *sub_s = gst_caps_get_structure (subset, 0);
2731 GstStructure *sup_s = gst_caps_get_structure (superset, 0);
2732
2733 return gst_structure_foreach (sub_s, check_field, sup_s);
2734}
2735
2736static gboolean
Thiago Santos2ae18972014-02-05 10:27:54 -03002737gst_qt_mux_audio_sink_set_caps (GstQTPad * qtpad, GstCaps * caps)
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002738{
Thiago Santos2ae18972014-02-05 10:27:54 -03002739 GstPad *pad = qtpad->collect.pad;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002740 GstQTMux *qtmux = GST_QT_MUX_CAST (gst_pad_get_parent (pad));
2741 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002742 GstStructure *structure;
2743 const gchar *mimetype;
2744 gint rate, channels;
2745 const GValue *value = NULL;
2746 const GstBuffer *codec_data = NULL;
2747 GstQTMuxFormat format;
2748 AudioSampleEntry entry = { 0, };
2749 AtomInfo *ext_atom = NULL;
2750 gint constant_size = 0;
Thiago Santoscbc0b052010-01-11 10:34:32 -03002751 const gchar *stream_format;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002752
Thiago Santosc5f6e742009-12-10 22:20:45 -03002753 qtpad->prepare_buf_func = NULL;
2754
Thiago Santosa5f22f02010-01-13 23:33:51 -03002755 /* does not go well to renegotiate stream mid-way, unless
2756 * the old caps are a subset of the new one (this means upstream
2757 * added more info to the caps, as both should be 'fixed' caps) */
2758 if (qtpad->fourcc) {
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02002759 GstCaps *current_caps;
2760
2761 current_caps = gst_pad_get_current_caps (pad);
Thiago Santosa5f22f02010-01-13 23:33:51 -03002762 g_assert (caps != NULL);
2763
2764 if (!gst_qtmux_caps_is_subset_full (qtmux, current_caps, caps)) {
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02002765 gst_caps_unref (current_caps);
Thiago Santosa5f22f02010-01-13 23:33:51 -03002766 goto refuse_renegotiation;
2767 }
2768 GST_DEBUG_OBJECT (qtmux,
2769 "pad %s accepted renegotiation to %" GST_PTR_FORMAT " from %"
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02002770 GST_PTR_FORMAT, GST_PAD_NAME (pad), caps, current_caps);
2771 gst_caps_unref (current_caps);
Thiago Santosa5f22f02010-01-13 23:33:51 -03002772 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002773
2774 GST_DEBUG_OBJECT (qtmux, "%s:%s, caps=%" GST_PTR_FORMAT,
2775 GST_DEBUG_PAD_NAME (pad), caps);
2776
2777 format = qtmux_klass->format;
2778 structure = gst_caps_get_structure (caps, 0);
2779 mimetype = gst_structure_get_name (structure);
2780
2781 /* common info */
2782 if (!gst_structure_get_int (structure, "channels", &channels) ||
2783 !gst_structure_get_int (structure, "rate", &rate)) {
2784 goto refuse_caps;
2785 }
2786
2787 /* optional */
2788 value = gst_structure_get_value (structure, "codec_data");
2789 if (value != NULL)
2790 codec_data = gst_value_get_buffer (value);
2791
2792 qtpad->is_out_of_order = FALSE;
2793
2794 /* set common properties */
2795 entry.sample_rate = rate;
2796 entry.channels = channels;
2797 /* default */
2798 entry.sample_size = 16;
2799 /* this is the typical compressed case */
2800 if (format == GST_QT_MUX_FORMAT_QT) {
2801 entry.version = 1;
2802 entry.compression_id = -2;
2803 }
2804
2805 /* now map onto a fourcc, and some extra properties */
2806 if (strcmp (mimetype, "audio/mpeg") == 0) {
2807 gint mpegversion = 0;
2808 gint layer = -1;
2809
2810 gst_structure_get_int (structure, "mpegversion", &mpegversion);
2811 switch (mpegversion) {
2812 case 1:
2813 gst_structure_get_int (structure, "layer", &layer);
2814 switch (layer) {
2815 case 3:
2816 /* mp3 */
2817 /* note: QuickTime player does not like mp3 either way in iso/mp4 */
2818 if (format == GST_QT_MUX_FORMAT_QT)
2819 entry.fourcc = FOURCC__mp3;
2820 else {
2821 entry.fourcc = FOURCC_mp4a;
Mark Nauwelaerts3f72fad2008-11-14 20:17:10 +00002822 ext_atom =
2823 build_esds_extension (qtpad->trak, ESDS_OBJECT_TYPE_MPEG1_P3,
Arun Raghavan4b4398c2010-07-05 14:09:50 +05302824 ESDS_STREAM_TYPE_AUDIO, codec_data, qtpad->avg_bitrate,
2825 qtpad->max_bitrate);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002826 }
2827 entry.samples_per_packet = 1152;
2828 entry.bytes_per_sample = 2;
2829 break;
2830 }
2831 break;
2832 case 4:
Thiago Santoscbc0b052010-01-11 10:34:32 -03002833
2834 /* check stream-format */
2835 stream_format = gst_structure_get_string (structure, "stream-format");
2836 if (stream_format) {
Thiago Santos87450c12010-01-11 13:13:41 -03002837 if (strcmp (stream_format, "raw") != 0) {
Thiago Santoscbc0b052010-01-11 10:34:32 -03002838 GST_WARNING_OBJECT (qtmux, "Unsupported AAC stream-format %s, "
Thiago Santos87450c12010-01-11 13:13:41 -03002839 "please use 'raw'", stream_format);
Thiago Santoscbc0b052010-01-11 10:34:32 -03002840 goto refuse_caps;
2841 }
2842 } else {
2843 GST_WARNING_OBJECT (qtmux, "No stream-format present in caps, "
Thiago Santos87450c12010-01-11 13:13:41 -03002844 "assuming 'raw'");
Thiago Santoscbc0b052010-01-11 10:34:32 -03002845 }
2846
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02002847 if (!codec_data || gst_buffer_get_size ((GstBuffer *) codec_data) < 2)
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002848 GST_WARNING_OBJECT (qtmux, "no (valid) codec_data for AAC audio");
2849 else {
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02002850 guint8 profile;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002851
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02002852 gst_buffer_extract ((GstBuffer *) codec_data, 0, &profile, 1);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002853 /* warn if not Low Complexity profile */
2854 profile >>= 3;
2855 if (profile != 2)
2856 GST_WARNING_OBJECT (qtmux,
2857 "non-LC AAC may not run well on (Apple) QuickTime/iTunes");
2858 }
Thiago Santoscbc0b052010-01-11 10:34:32 -03002859
2860 /* AAC */
2861 entry.fourcc = FOURCC_mp4a;
2862
Mark Nauwelaerts3f72fad2008-11-14 20:17:10 +00002863 if (format == GST_QT_MUX_FORMAT_QT)
Arun Raghavan4b4398c2010-07-05 14:09:50 +05302864 ext_atom = build_mov_aac_extension (qtpad->trak, codec_data,
2865 qtpad->avg_bitrate, qtpad->max_bitrate);
Mark Nauwelaerts3f72fad2008-11-14 20:17:10 +00002866 else
2867 ext_atom =
2868 build_esds_extension (qtpad->trak, ESDS_OBJECT_TYPE_MPEG4_P3,
Arun Raghavan4b4398c2010-07-05 14:09:50 +05302869 ESDS_STREAM_TYPE_AUDIO, codec_data, qtpad->avg_bitrate,
2870 qtpad->max_bitrate);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002871 break;
2872 default:
2873 break;
2874 }
2875 } else if (strcmp (mimetype, "audio/AMR") == 0) {
2876 entry.fourcc = FOURCC_samr;
2877 entry.sample_size = 16;
2878 entry.samples_per_packet = 160;
2879 entry.bytes_per_sample = 2;
Mark Nauwelaertsffd2ff32009-01-28 13:25:14 +01002880 ext_atom = build_amr_extension ();
2881 } else if (strcmp (mimetype, "audio/AMR-WB") == 0) {
2882 entry.fourcc = FOURCC_sawb;
2883 entry.sample_size = 16;
2884 entry.samples_per_packet = 320;
2885 entry.bytes_per_sample = 2;
2886 ext_atom = build_amr_extension ();
Sebastian Dröge940807b2012-01-04 11:29:26 +01002887 } else if (strcmp (mimetype, "audio/x-raw") == 0) {
2888 GstAudioInfo info;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002889
Sebastian Dröge940807b2012-01-04 11:29:26 +01002890 gst_audio_info_init (&info);
2891 if (!gst_audio_info_from_caps (&info, caps))
Jan Urbanskid43db1b2009-08-04 12:58:35 +02002892 goto refuse_caps;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002893
2894 /* spec has no place for a distinction in these */
Sebastian Dröge940807b2012-01-04 11:29:26 +01002895 if (info.finfo->width != info.finfo->depth) {
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002896 GST_DEBUG_OBJECT (qtmux, "width must be same as depth!");
2897 goto refuse_caps;
2898 }
2899
Sebastian Dröge940807b2012-01-04 11:29:26 +01002900 if ((info.finfo->flags & GST_AUDIO_FORMAT_FLAG_SIGNED)) {
2901 if (info.finfo->endianness == G_LITTLE_ENDIAN)
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002902 entry.fourcc = FOURCC_sowt;
Sebastian Dröge940807b2012-01-04 11:29:26 +01002903 else if (info.finfo->endianness == G_BIG_ENDIAN)
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002904 entry.fourcc = FOURCC_twos;
2905 /* maximum backward compatibility; only new version for > 16 bit */
Sebastian Dröge940807b2012-01-04 11:29:26 +01002906 if (info.finfo->depth <= 16)
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002907 entry.version = 0;
2908 /* not compressed in any case */
2909 entry.compression_id = 0;
2910 /* QT spec says: max at 16 bit even if sample size were actually larger,
2911 * however, most players (e.g. QuickTime!) seem to disagree, so ... */
Sebastian Dröge940807b2012-01-04 11:29:26 +01002912 entry.sample_size = info.finfo->depth;
2913 entry.bytes_per_sample = info.finfo->depth / 8;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002914 entry.samples_per_packet = 1;
Sebastian Dröge940807b2012-01-04 11:29:26 +01002915 entry.bytes_per_packet = info.finfo->depth / 8;
2916 entry.bytes_per_frame = entry.bytes_per_packet * info.channels;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002917 } else {
Sebastian Dröge940807b2012-01-04 11:29:26 +01002918 if (info.finfo->width == 8 && info.finfo->depth == 8) {
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002919 /* fall back to old 8-bit version */
2920 entry.fourcc = FOURCC_raw_;
2921 entry.version = 0;
2922 entry.compression_id = 0;
2923 entry.sample_size = 8;
2924 } else {
2925 GST_DEBUG_OBJECT (qtmux, "non 8-bit PCM must be signed");
2926 goto refuse_caps;
2927 }
2928 }
Sebastian Dröge940807b2012-01-04 11:29:26 +01002929 constant_size = (info.finfo->depth / 8) * info.channels;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002930 } else if (strcmp (mimetype, "audio/x-alaw") == 0) {
2931 entry.fourcc = FOURCC_alaw;
2932 entry.samples_per_packet = 1023;
2933 entry.bytes_per_sample = 2;
2934 } else if (strcmp (mimetype, "audio/x-mulaw") == 0) {
2935 entry.fourcc = FOURCC_ulaw;
2936 entry.samples_per_packet = 1023;
2937 entry.bytes_per_sample = 2;
Michael Smithd74567c2009-12-08 17:55:56 -08002938 } else if (strcmp (mimetype, "audio/x-adpcm") == 0) {
2939 gint blocksize;
2940 if (!gst_structure_get_int (structure, "block_align", &blocksize)) {
2941 GST_DEBUG_OBJECT (qtmux, "broken caps, block_align missing");
2942 goto refuse_caps;
2943 }
Michael Smitha597bd72009-12-08 17:59:04 -08002944 /* Currently only supports WAV-style IMA ADPCM, for which the codec id is
2945 0x11 */
Michael Smithd74567c2009-12-08 17:55:56 -08002946 entry.fourcc = MS_WAVE_FOURCC (0x11);
Michael Smitha597bd72009-12-08 17:59:04 -08002947 /* 4 byte header per channel (including one sample). 2 samples per byte
2948 remaining. Simplifying gives the following (samples per block per
2949 channel) */
Michael Smithd74567c2009-12-08 17:55:56 -08002950 entry.samples_per_packet = 2 * blocksize / channels - 7;
2951 entry.bytes_per_sample = 2;
2952
Michael Smithd784a8d2010-01-27 15:37:37 -08002953 entry.bytes_per_frame = blocksize;
2954 entry.bytes_per_packet = blocksize / channels;
2955 /* ADPCM has constant size packets */
2956 constant_size = 1;
2957 /* TODO: I don't really understand why this helps, but it does! Constant
2958 * size and compression_id of -2 seem to be incompatible, and other files
2959 * in the wild use this too. */
2960 entry.compression_id = -1;
2961
Michael Smithd74567c2009-12-08 17:55:56 -08002962 ext_atom = build_ima_adpcm_extension (channels, rate, blocksize);
Mark Nauwelaertsbcc9fe02010-02-16 16:24:12 +01002963 } else if (strcmp (mimetype, "audio/x-alac") == 0) {
2964 GstBuffer *codec_config;
2965 gint len;
Wim Taymans583d39d2012-01-23 17:25:37 +01002966 GstMapInfo map;
Mark Nauwelaertsbcc9fe02010-02-16 16:24:12 +01002967
2968 entry.fourcc = FOURCC_alac;
Wim Taymans583d39d2012-01-23 17:25:37 +01002969 gst_buffer_map ((GstBuffer *) codec_data, &map, GST_MAP_READ);
Mark Nauwelaertsbcc9fe02010-02-16 16:24:12 +01002970 /* let's check if codec data already comes with 'alac' atom prefix */
Wim Taymans583d39d2012-01-23 17:25:37 +01002971 if (!codec_data || (len = map.size) < 28) {
Mark Nauwelaertsbcc9fe02010-02-16 16:24:12 +01002972 GST_DEBUG_OBJECT (qtmux, "broken caps, codec data missing");
Wim Taymans583d39d2012-01-23 17:25:37 +01002973 gst_buffer_unmap ((GstBuffer *) codec_data, &map);
Mark Nauwelaertsbcc9fe02010-02-16 16:24:12 +01002974 goto refuse_caps;
2975 }
Wim Taymans583d39d2012-01-23 17:25:37 +01002976 if (GST_READ_UINT32_LE (map.data + 4) == FOURCC_alac) {
Mark Nauwelaertsbcc9fe02010-02-16 16:24:12 +01002977 len -= 8;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02002978 codec_config =
Tim-Philipp Mülleraa94fc62014-12-25 21:32:40 +00002979 gst_buffer_copy_region ((GstBuffer *) codec_data,
2980 GST_BUFFER_COPY_MEMORY, 8, len);
Mark Nauwelaertsbcc9fe02010-02-16 16:24:12 +01002981 } else {
2982 codec_config = gst_buffer_ref ((GstBuffer *) codec_data);
2983 }
Wim Taymans583d39d2012-01-23 17:25:37 +01002984 gst_buffer_unmap ((GstBuffer *) codec_data, &map);
Mark Nauwelaertsbcc9fe02010-02-16 16:24:12 +01002985 if (len != 28) {
2986 /* does not look good, but perhaps some trailing unneeded stuff */
2987 GST_WARNING_OBJECT (qtmux, "unexpected codec-data size, possibly broken");
2988 }
2989 if (format == GST_QT_MUX_FORMAT_QT)
2990 ext_atom = build_mov_alac_extension (qtpad->trak, codec_config);
2991 else
2992 ext_atom = build_codec_data_extension (FOURCC_alac, codec_config);
2993 /* set some more info */
Wim Taymans583d39d2012-01-23 17:25:37 +01002994 gst_buffer_map (codec_config, &map, GST_MAP_READ);
Mark Nauwelaertsbcc9fe02010-02-16 16:24:12 +01002995 entry.bytes_per_sample = 2;
Wim Taymans583d39d2012-01-23 17:25:37 +01002996 entry.samples_per_packet = GST_READ_UINT32_BE (map.data + 4);
2997 gst_buffer_unmap (codec_config, &map);
Mark Nauwelaertsbcc9fe02010-02-16 16:24:12 +01002998 gst_buffer_unref (codec_config);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00002999 }
3000
3001 if (!entry.fourcc)
3002 goto refuse_caps;
3003
3004 /* ok, set the pad info accordingly */
3005 qtpad->fourcc = entry.fourcc;
3006 qtpad->sample_size = constant_size;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003007 atom_trak_set_audio_type (qtpad->trak, qtmux->context, &entry,
Marc-André Lureaubee00aa2010-07-30 12:48:29 +02003008 qtmux->trak_timescale ? qtmux->trak_timescale : entry.sample_rate,
3009 ext_atom, constant_size);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003010
3011 gst_object_unref (qtmux);
3012 return TRUE;
3013
3014 /* ERRORS */
3015refuse_caps:
3016 {
3017 GST_WARNING_OBJECT (qtmux, "pad %s refused caps %" GST_PTR_FORMAT,
3018 GST_PAD_NAME (pad), caps);
3019 gst_object_unref (qtmux);
3020 return FALSE;
3021 }
3022refuse_renegotiation:
3023 {
3024 GST_WARNING_OBJECT (qtmux,
3025 "pad %s refused renegotiation to %" GST_PTR_FORMAT,
3026 GST_PAD_NAME (pad), caps);
3027 gst_object_unref (qtmux);
3028 return FALSE;
3029 }
3030}
3031
3032/* scale rate up or down by factor of 10 to fit into [1000,10000] interval */
3033static guint32
3034adjust_rate (guint64 rate)
3035{
Mark Nauwelaertsa5edbe32010-10-13 17:47:29 +02003036 if (rate == 0)
3037 return 10000;
3038
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003039 while (rate >= 10000)
3040 rate /= 10;
3041
3042 while (rate < 1000)
3043 rate *= 10;
3044
3045 return (guint32) rate;
3046}
3047
3048static gboolean
Thiago Santos2ae18972014-02-05 10:27:54 -03003049gst_qt_mux_video_sink_set_caps (GstQTPad * qtpad, GstCaps * caps)
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003050{
Thiago Santos2ae18972014-02-05 10:27:54 -03003051 GstPad *pad = qtpad->collect.pad;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003052 GstQTMux *qtmux = GST_QT_MUX_CAST (gst_pad_get_parent (pad));
3053 GstQTMuxClass *qtmux_klass = (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003054 GstStructure *structure;
3055 const gchar *mimetype;
3056 gint width, height, depth = -1;
3057 gint framerate_num, framerate_den;
3058 guint32 rate;
3059 const GValue *value = NULL;
3060 const GstBuffer *codec_data = NULL;
3061 VisualSampleEntry entry = { 0, };
3062 GstQTMuxFormat format;
3063 AtomInfo *ext_atom = NULL;
Thiago Santos496bd012009-10-29 08:36:02 -03003064 GList *ext_atom_list = NULL;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003065 gboolean sync = FALSE;
David Schleef71eacf82008-11-18 01:09:09 +00003066 int par_num, par_den;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003067
Thiago Santosc5f6e742009-12-10 22:20:45 -03003068 qtpad->prepare_buf_func = NULL;
3069
Thiago Santosa5f22f02010-01-13 23:33:51 -03003070 /* does not go well to renegotiate stream mid-way, unless
3071 * the old caps are a subset of the new one (this means upstream
3072 * added more info to the caps, as both should be 'fixed' caps) */
3073 if (qtpad->fourcc) {
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003074 GstCaps *current_caps;
3075
3076 current_caps = gst_pad_get_current_caps (pad);
Thiago Santosa5f22f02010-01-13 23:33:51 -03003077 g_assert (caps != NULL);
3078
3079 if (!gst_qtmux_caps_is_subset_full (qtmux, current_caps, caps)) {
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003080 gst_caps_unref (current_caps);
Thiago Santosa5f22f02010-01-13 23:33:51 -03003081 goto refuse_renegotiation;
3082 }
3083 GST_DEBUG_OBJECT (qtmux,
3084 "pad %s accepted renegotiation to %" GST_PTR_FORMAT " from %"
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003085 GST_PTR_FORMAT, GST_PAD_NAME (pad), caps, current_caps);
3086 gst_caps_unref (current_caps);
Thiago Santosa5f22f02010-01-13 23:33:51 -03003087 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003088
3089 GST_DEBUG_OBJECT (qtmux, "%s:%s, caps=%" GST_PTR_FORMAT,
3090 GST_DEBUG_PAD_NAME (pad), caps);
3091
3092 format = qtmux_klass->format;
3093 structure = gst_caps_get_structure (caps, 0);
3094 mimetype = gst_structure_get_name (structure);
3095
3096 /* required parts */
3097 if (!gst_structure_get_int (structure, "width", &width) ||
Mark Nauwelaerts3f72fad2008-11-14 20:17:10 +00003098 !gst_structure_get_int (structure, "height", &height))
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003099 goto refuse_caps;
3100
3101 /* optional */
Mark Nauwelaerts3f72fad2008-11-14 20:17:10 +00003102 depth = -1;
3103 /* works as a default timebase */
3104 framerate_num = 10000;
3105 framerate_den = 1;
3106 gst_structure_get_fraction (structure, "framerate", &framerate_num,
3107 &framerate_den);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003108 gst_structure_get_int (structure, "depth", &depth);
3109 value = gst_structure_get_value (structure, "codec_data");
3110 if (value != NULL)
3111 codec_data = gst_value_get_buffer (value);
3112
David Schleef71eacf82008-11-18 01:09:09 +00003113 par_num = 1;
3114 par_den = 1;
3115 gst_structure_get_fraction (structure, "pixel-aspect-ratio", &par_num,
3116 &par_den);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003117
3118 qtpad->is_out_of_order = FALSE;
3119
3120 /* bring frame numerator into a range that ensures both reasonable resolution
3121 * as well as a fair duration */
Marc-André Lureaubee00aa2010-07-30 12:48:29 +02003122 rate = qtmux->trak_timescale ?
3123 qtmux->trak_timescale : adjust_rate (framerate_num);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003124 GST_DEBUG_OBJECT (qtmux, "Rate of video track selected: %" G_GUINT32_FORMAT,
3125 rate);
3126
3127 /* set common properties */
3128 entry.width = width;
3129 entry.height = height;
Mark Nauwelaerts65b69dd2009-06-01 23:00:44 +02003130 entry.par_n = par_num;
3131 entry.par_d = par_den;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003132 /* should be OK according to qt and iso spec, override if really needed */
3133 entry.color_table_id = -1;
3134 entry.frame_count = 1;
3135 entry.depth = 24;
3136
3137 /* sync entries by default */
3138 sync = TRUE;
3139
3140 /* now map onto a fourcc, and some extra properties */
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003141 if (strcmp (mimetype, "video/x-raw") == 0) {
3142 const gchar *format;
3143 GstVideoFormat fmt;
Wim Taymans5fa0c282011-08-05 08:59:59 +02003144 const GstVideoFormatInfo *vinfo;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003145
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003146 format = gst_structure_get_string (structure, "format");
3147 fmt = gst_video_format_from_string (format);
Wim Taymans5fa0c282011-08-05 08:59:59 +02003148 vinfo = gst_video_format_get_info (fmt);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003149
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003150 switch (fmt) {
3151 case GST_VIDEO_FORMAT_UYVY:
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003152 if (depth == -1)
3153 depth = 24;
3154 entry.fourcc = FOURCC_2vuy;
3155 entry.depth = depth;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003156 sync = FALSE;
3157 break;
Sebastian Drögedc2251a2015-01-13 19:05:20 +01003158 case GST_VIDEO_FORMAT_v210:
3159 if (depth == -1)
3160 depth = 24;
3161 entry.fourcc = FOURCC_v210;
3162 entry.depth = depth;
3163 sync = FALSE;
3164 break;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003165 default:
Wim Taymans5fa0c282011-08-05 08:59:59 +02003166 if (GST_VIDEO_FORMAT_INFO_FLAGS (vinfo) & GST_VIDEO_FORMAT_FLAG_RGB) {
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003167 entry.fourcc = FOURCC_raw_;
Wim Taymans5fa0c282011-08-05 08:59:59 +02003168 entry.depth = GST_VIDEO_FORMAT_INFO_PSTRIDE (vinfo, 0) * 8;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003169 sync = FALSE;
3170 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003171 break;
3172 }
3173 } else if (strcmp (mimetype, "video/x-h263") == 0) {
Thiago Santos496bd012009-10-29 08:36:02 -03003174 ext_atom = NULL;
Mark Nauwelaerts4dbeaee2009-05-28 13:56:10 +02003175 if (format == GST_QT_MUX_FORMAT_QT)
3176 entry.fourcc = FOURCC_h263;
3177 else
3178 entry.fourcc = FOURCC_s263;
Mark Nauwelaertsffd2ff32009-01-28 13:25:14 +01003179 ext_atom = build_h263_extension ();
Thiago Santos496bd012009-10-29 08:36:02 -03003180 if (ext_atom != NULL)
3181 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003182 } else if (strcmp (mimetype, "video/x-divx") == 0 ||
3183 strcmp (mimetype, "video/mpeg") == 0) {
3184 gint version = 0;
3185
3186 if (strcmp (mimetype, "video/x-divx") == 0) {
3187 gst_structure_get_int (structure, "divxversion", &version);
3188 version = version == 5 ? 1 : 0;
3189 } else {
3190 gst_structure_get_int (structure, "mpegversion", &version);
3191 version = version == 4 ? 1 : 0;
3192 }
3193 if (version) {
3194 entry.fourcc = FOURCC_mp4v;
Mark Nauwelaerts3f72fad2008-11-14 20:17:10 +00003195 ext_atom =
3196 build_esds_extension (qtpad->trak, ESDS_OBJECT_TYPE_MPEG4_P2,
Arun Raghavan4b4398c2010-07-05 14:09:50 +05303197 ESDS_STREAM_TYPE_VISUAL, codec_data, qtpad->avg_bitrate,
3198 qtpad->max_bitrate);
Thiago Santos496bd012009-10-29 08:36:02 -03003199 if (ext_atom != NULL)
3200 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003201 if (!codec_data)
3202 GST_WARNING_OBJECT (qtmux, "no codec_data for MPEG4 video; "
3203 "output might not play in Apple QuickTime (try global-headers?)");
3204 }
3205 } else if (strcmp (mimetype, "video/x-h264") == 0) {
Thiago Santosc385a462011-04-13 16:37:07 +01003206 /* check if we accept these caps */
3207 if (gst_structure_has_field (structure, "stream-format")) {
3208 const gchar *format;
3209 const gchar *alignment;
3210
3211 format = gst_structure_get_string (structure, "stream-format");
3212 alignment = gst_structure_get_string (structure, "alignment");
3213
3214 if (strcmp (format, "avc") != 0 || alignment == NULL ||
3215 strcmp (alignment, "au") != 0) {
3216 GST_WARNING_OBJECT (qtmux, "Rejecting h264 caps, qtmux only accepts "
3217 "avc format with AU aligned samples");
3218 goto refuse_caps;
3219 }
3220 } else {
3221 GST_WARNING_OBJECT (qtmux, "no stream-format field in h264 caps");
Tim-Philipp Müllercf953fe2011-04-16 18:17:01 +01003222 goto refuse_caps;
Thiago Santosc385a462011-04-13 16:37:07 +01003223 }
3224
3225 if (!codec_data) {
3226 GST_WARNING_OBJECT (qtmux, "no codec_data in h264 caps");
Tim-Philipp Müllercf953fe2011-04-16 18:17:01 +01003227 goto refuse_caps;
Thiago Santosc385a462011-04-13 16:37:07 +01003228 }
3229
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003230 entry.fourcc = FOURCC_avc1;
Marc-André Lureau9e793072010-07-28 16:15:53 +02003231 if (qtpad->avg_bitrate == 0) {
3232 gint avg_bitrate = 0;
3233 gst_structure_get_int (structure, "bitrate", &avg_bitrate);
3234 qtpad->avg_bitrate = avg_bitrate;
3235 }
Arun Raghavan9e5c5752010-07-06 14:48:08 +05303236 ext_atom = build_btrt_extension (0, qtpad->avg_bitrate, qtpad->max_bitrate);
3237 if (ext_atom != NULL)
3238 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
Mark Nauwelaerts3f72fad2008-11-14 20:17:10 +00003239 ext_atom = build_codec_data_extension (FOURCC_avcC, codec_data);
Thiago Santos496bd012009-10-29 08:36:02 -03003240 if (ext_atom != NULL)
3241 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
3242 } else if (strcmp (mimetype, "video/x-svq") == 0) {
3243 gint version = 0;
3244 const GstBuffer *seqh = NULL;
3245 const GValue *seqh_value;
3246 gdouble gamma = 0;
3247
3248 gst_structure_get_int (structure, "svqversion", &version);
3249 if (version == 3) {
3250 entry.fourcc = FOURCC_SVQ3;
3251 entry.version = 3;
3252 entry.depth = 32;
Thiago Santos496bd012009-10-29 08:36:02 -03003253
3254 seqh_value = gst_structure_get_value (structure, "seqh");
3255 if (seqh_value) {
3256 seqh = gst_value_get_buffer (seqh_value);
3257 ext_atom = build_SMI_atom (seqh);
3258 if (ext_atom)
3259 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
3260 }
3261
3262 /* we need to add the gamma anyway because quicktime might crash
3263 * when it doesn't find it */
3264 if (!gst_structure_get_double (structure, "applied-gamma", &gamma)) {
3265 /* it seems that using 0 here makes it ignored */
3266 gamma = 0.0;
3267 }
3268 ext_atom = build_gama_atom (gamma);
3269 if (ext_atom)
3270 ext_atom_list = g_list_prepend (ext_atom_list, ext_atom);
3271 } else {
3272 GST_WARNING_OBJECT (qtmux, "SVQ version %d not supported. Please file "
3273 "a bug at http://bugzilla.gnome.org", version);
3274 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003275 } else if (strcmp (mimetype, "video/x-dv") == 0) {
3276 gint version = 0;
3277 gboolean pal = TRUE;
3278
3279 sync = FALSE;
3280 if (framerate_num != 25 || framerate_den != 1)
3281 pal = FALSE;
3282 gst_structure_get_int (structure, "dvversion", &version);
3283 /* fall back to typical one */
3284 if (!version)
3285 version = 25;
3286 switch (version) {
3287 case 25:
3288 if (pal)
3289 entry.fourcc = GST_MAKE_FOURCC ('d', 'v', 'c', 'p');
3290 else
3291 entry.fourcc = GST_MAKE_FOURCC ('d', 'v', 'c', ' ');
3292 break;
3293 case 50:
3294 if (pal)
3295 entry.fourcc = GST_MAKE_FOURCC ('d', 'v', '5', 'p');
3296 else
3297 entry.fourcc = GST_MAKE_FOURCC ('d', 'v', '5', 'n');
3298 break;
3299 default:
3300 GST_WARNING_OBJECT (qtmux, "unrecognized dv version");
3301 break;
3302 }
3303 } else if (strcmp (mimetype, "image/jpeg") == 0) {
3304 entry.fourcc = FOURCC_jpeg;
3305 sync = FALSE;
Thiago Santosc5f6e742009-12-10 22:20:45 -03003306 } else if (strcmp (mimetype, "image/x-j2c") == 0 ||
3307 strcmp (mimetype, "image/x-jpc") == 0) {
Wim Taymans9d637142011-08-22 12:24:15 +02003308 const gchar *colorspace;
Thiago Santosc5f6e742009-12-10 22:20:45 -03003309 const GValue *cmap_array;
3310 const GValue *cdef_array;
3311 gint ncomp = 0;
3312 gint fields = 1;
3313
3314 if (strcmp (mimetype, "image/x-jpc") == 0) {
3315 qtpad->prepare_buf_func = gst_qt_mux_prepare_jpc_buffer;
3316 }
3317
3318 gst_structure_get_int (structure, "num-components", &ncomp);
3319 gst_structure_get_int (structure, "fields", &fields);
3320 cmap_array = gst_structure_get_value (structure, "component-map");
3321 cdef_array = gst_structure_get_value (structure, "channel-definitions");
Mark Nauwelaerts3f72fad2008-11-14 20:17:10 +00003322
Thiago Santos496bd012009-10-29 08:36:02 -03003323 ext_atom = NULL;
Mark Nauwelaerts3f72fad2008-11-14 20:17:10 +00003324 entry.fourcc = FOURCC_mjp2;
3325 sync = FALSE;
Wim Taymans9d637142011-08-22 12:24:15 +02003326
3327 colorspace = gst_structure_get_string (structure, "colorspace");
3328 if (colorspace &&
Thiago Santosc5f6e742009-12-10 22:20:45 -03003329 (ext_atom =
Wim Taymans9d637142011-08-22 12:24:15 +02003330 build_jp2h_extension (qtpad->trak, width, height, colorspace, ncomp,
Thiago Santosc5f6e742009-12-10 22:20:45 -03003331 cmap_array, cdef_array)) != NULL) {
3332 ext_atom_list = g_list_append (ext_atom_list, ext_atom);
3333
3334 ext_atom = build_fiel_extension (fields);
3335 if (ext_atom)
3336 ext_atom_list = g_list_append (ext_atom_list, ext_atom);
3337
3338 ext_atom = build_jp2x_extension (codec_data);
3339 if (ext_atom)
3340 ext_atom_list = g_list_append (ext_atom_list, ext_atom);
3341 } else {
Mark Nauwelaerts3f72fad2008-11-14 20:17:10 +00003342 GST_DEBUG_OBJECT (qtmux, "missing or invalid fourcc in jp2 caps");
3343 goto refuse_caps;
3344 }
David Schleef4bb9ffa2010-05-12 18:50:34 -07003345 } else if (strcmp (mimetype, "video/x-vp8") == 0) {
3346 entry.fourcc = FOURCC_VP80;
3347 sync = FALSE;
David Schleef4dcc9ee2011-07-06 17:18:05 -07003348 } else if (strcmp (mimetype, "video/x-dirac") == 0) {
3349 entry.fourcc = FOURCC_drac;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003350 } else if (strcmp (mimetype, "video/x-qt-part") == 0) {
3351 guint32 fourcc;
3352
Wim Taymans9d637142011-08-22 12:24:15 +02003353 gst_structure_get_uint (structure, "format", &fourcc);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003354 entry.fourcc = fourcc;
David Schleefb11186e2008-11-15 02:56:31 +00003355 } else if (strcmp (mimetype, "video/x-mp4-part") == 0) {
3356 guint32 fourcc;
3357
Wim Taymans9d637142011-08-22 12:24:15 +02003358 gst_structure_get_uint (structure, "format", &fourcc);
David Schleefb11186e2008-11-15 02:56:31 +00003359 entry.fourcc = fourcc;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003360 }
3361
3362 if (!entry.fourcc)
3363 goto refuse_caps;
3364
3365 /* ok, set the pad info accordingly */
3366 qtpad->fourcc = entry.fourcc;
3367 qtpad->sync = sync;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003368 atom_trak_set_video_type (qtpad->trak, qtmux->context, &entry, rate,
Thiago Santos496bd012009-10-29 08:36:02 -03003369 ext_atom_list);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003370
3371 gst_object_unref (qtmux);
3372 return TRUE;
3373
3374 /* ERRORS */
3375refuse_caps:
3376 {
3377 GST_WARNING_OBJECT (qtmux, "pad %s refused caps %" GST_PTR_FORMAT,
3378 GST_PAD_NAME (pad), caps);
3379 gst_object_unref (qtmux);
3380 return FALSE;
3381 }
3382refuse_renegotiation:
3383 {
3384 GST_WARNING_OBJECT (qtmux,
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003385 "pad %s refused renegotiation to %" GST_PTR_FORMAT, GST_PAD_NAME (pad),
3386 caps);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003387 gst_object_unref (qtmux);
3388 return FALSE;
3389 }
3390}
3391
3392static gboolean
Thiago Santos2ae18972014-02-05 10:27:54 -03003393gst_qt_mux_subtitle_sink_set_caps (GstQTPad * qtpad, GstCaps * caps)
3394{
3395 GstPad *pad = qtpad->collect.pad;
3396 GstQTMux *qtmux = GST_QT_MUX_CAST (gst_pad_get_parent (pad));
Thiago Santos99e966e2014-02-06 12:15:22 -03003397 GstStructure *structure;
3398 SubtitleSampleEntry entry = { 0, };
Thiago Santos2ae18972014-02-05 10:27:54 -03003399
3400 qtpad->prepare_buf_func = NULL;
3401
3402 /* does not go well to renegotiate stream mid-way, unless
3403 * the old caps are a subset of the new one (this means upstream
3404 * added more info to the caps, as both should be 'fixed' caps) */
3405 if (qtpad->fourcc) {
3406 GstCaps *current_caps;
3407
3408 current_caps = gst_pad_get_current_caps (pad);
3409 g_assert (caps != NULL);
3410
3411 if (!gst_qtmux_caps_is_subset_full (qtmux, current_caps, caps)) {
3412 gst_caps_unref (current_caps);
3413 goto refuse_renegotiation;
3414 }
3415 GST_DEBUG_OBJECT (qtmux,
3416 "pad %s accepted renegotiation to %" GST_PTR_FORMAT " from %"
3417 GST_PTR_FORMAT, GST_PAD_NAME (pad), caps, current_caps);
3418 gst_caps_unref (current_caps);
3419 }
3420
3421 GST_DEBUG_OBJECT (qtmux, "%s:%s, caps=%" GST_PTR_FORMAT,
3422 GST_DEBUG_PAD_NAME (pad), caps);
3423
3424 /* subtitles default */
Thiago Santos99e966e2014-02-06 12:15:22 -03003425 subtitle_sample_entry_init (&entry);
Thiago Santos2ae18972014-02-05 10:27:54 -03003426 qtpad->is_out_of_order = FALSE;
3427 qtpad->sync = FALSE;
Thiago Santosf89ba822014-02-07 01:49:26 -03003428 qtpad->sparse = TRUE;
Thiago Santos99e966e2014-02-06 12:15:22 -03003429 qtpad->prepare_buf_func = NULL;
Thiago Santos2ae18972014-02-05 10:27:54 -03003430
Thiago Santos99e966e2014-02-06 12:15:22 -03003431 structure = gst_caps_get_structure (caps, 0);
3432
3433 if (gst_structure_has_name (structure, "text/x-raw")) {
3434 const gchar *format = gst_structure_get_string (structure, "format");
3435 if (format && strcmp (format, "utf8") == 0) {
3436 entry.fourcc = FOURCC_tx3g;
3437 qtpad->prepare_buf_func = gst_qt_mux_prepare_tx3g_buffer;
Thiago Santosf89ba822014-02-07 01:49:26 -03003438 qtpad->create_empty_buffer = gst_qt_mux_create_empty_tx3g_buffer;
Thiago Santos99e966e2014-02-06 12:15:22 -03003439 }
3440 }
3441
3442 if (!entry.fourcc)
3443 goto refuse_caps;
3444
3445 qtpad->fourcc = entry.fourcc;
3446 atom_trak_set_subtitle_type (qtpad->trak, qtmux->context, &entry);
Thiago Santos2ae18972014-02-05 10:27:54 -03003447
3448 gst_object_unref (qtmux);
Thiago Santos99e966e2014-02-06 12:15:22 -03003449 return TRUE;
Thiago Santos2ae18972014-02-05 10:27:54 -03003450
3451 /* ERRORS */
Thiago Santos99e966e2014-02-06 12:15:22 -03003452refuse_caps:
3453 {
3454 GST_WARNING_OBJECT (qtmux, "pad %s refused caps %" GST_PTR_FORMAT,
3455 GST_PAD_NAME (pad), caps);
3456 gst_object_unref (qtmux);
3457 return FALSE;
3458 }
Thiago Santos2ae18972014-02-05 10:27:54 -03003459refuse_renegotiation:
3460 {
3461 GST_WARNING_OBJECT (qtmux,
3462 "pad %s refused renegotiation to %" GST_PTR_FORMAT, GST_PAD_NAME (pad),
3463 caps);
3464 gst_object_unref (qtmux);
3465 return FALSE;
3466 }
3467}
3468
3469static gboolean
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +02003470gst_qt_mux_sink_event (GstCollectPads * pads, GstCollectData * data,
Mark Nauwelaerts7480b442011-12-15 16:15:22 +01003471 GstEvent * event, gpointer user_data)
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003472{
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003473 GstQTMux *qtmux;
Arun Raghavan4b4398c2010-07-05 14:09:50 +05303474 guint32 avg_bitrate = 0, max_bitrate = 0;
Mark Nauwelaerts7480b442011-12-15 16:15:22 +01003475 GstPad *pad = data->pad;
Mark Nauwelaerts04b4d302012-04-16 16:37:49 +02003476 gboolean ret = TRUE;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003477
Mark Nauwelaerts7480b442011-12-15 16:15:22 +01003478 qtmux = GST_QT_MUX_CAST (user_data);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003479 switch (GST_EVENT_TYPE (event)) {
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003480 case GST_EVENT_CAPS:
3481 {
3482 GstCaps *caps;
3483 GstQTPad *collect_pad;
3484
3485 gst_event_parse_caps (event, &caps);
3486
3487 /* find stream data */
3488 collect_pad = (GstQTPad *) gst_pad_get_element_private (pad);
3489 g_assert (collect_pad);
3490 g_assert (collect_pad->set_caps);
3491
Thiago Santos2ae18972014-02-05 10:27:54 -03003492 ret = collect_pad->set_caps (collect_pad, caps);
Sebastian Dröge30689a82012-01-26 10:40:06 +01003493 gst_event_unref (event);
Mark Nauwelaerts04b4d302012-04-16 16:37:49 +02003494 event = NULL;
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003495 break;
3496 }
Stefan Kost6a47f1f2008-12-16 16:26:52 +00003497 case GST_EVENT_TAG:{
3498 GstTagList *list;
3499 GstTagSetter *setter = GST_TAG_SETTER (qtmux);
Michael Smith8b3aca92010-02-26 11:50:25 -08003500 GstTagMergeMode mode;
Matej Knoppd8ac6662013-03-22 02:24:01 +01003501 gchar *code;
Thiago Santose52b2cb2015-01-31 14:32:34 -03003502 GstQTPad *collect_pad;
Michael Smith8b3aca92010-02-26 11:50:25 -08003503
3504 GST_OBJECT_LOCK (qtmux);
3505 mode = gst_tag_setter_get_tag_merge_mode (setter);
Thiago Santose52b2cb2015-01-31 14:32:34 -03003506 collect_pad = (GstQTPad *) gst_pad_get_element_private (pad);
Stefan Kost6a47f1f2008-12-16 16:26:52 +00003507
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003508 gst_event_parse_tag (event, &list);
Thiago Santos4a5364d2011-12-12 11:51:06 -03003509 GST_DEBUG_OBJECT (qtmux, "received tag event on pad %s:%s : %"
3510 GST_PTR_FORMAT, GST_DEBUG_PAD_NAME (pad), list);
Michael Smith8b3aca92010-02-26 11:50:25 -08003511
Thiago Santose52b2cb2015-01-31 14:32:34 -03003512 if (gst_tag_list_get_scope (list) == GST_TAG_SCOPE_GLOBAL) {
3513 gst_tag_setter_merge_tags (setter, list, mode);
3514 } else {
3515 if (!collect_pad->tags)
3516 collect_pad->tags = gst_tag_list_new_empty ();
3517 gst_tag_list_insert (collect_pad->tags, list, mode);
3518 }
Michael Smith8b3aca92010-02-26 11:50:25 -08003519 GST_OBJECT_UNLOCK (qtmux);
Arun Raghavan4b4398c2010-07-05 14:09:50 +05303520
3521 if (gst_tag_list_get_uint (list, GST_TAG_BITRATE, &avg_bitrate) |
3522 gst_tag_list_get_uint (list, GST_TAG_MAXIMUM_BITRATE, &max_bitrate)) {
3523 GstQTPad *qtpad = gst_pad_get_element_private (pad);
3524 g_assert (qtpad);
3525
3526 if (avg_bitrate > 0 && avg_bitrate < G_MAXUINT32)
3527 qtpad->avg_bitrate = avg_bitrate;
3528 if (max_bitrate > 0 && max_bitrate < G_MAXUINT32)
3529 qtpad->max_bitrate = max_bitrate;
3530 }
3531
Matej Knoppd8ac6662013-03-22 02:24:01 +01003532 if (gst_tag_list_get_string (list, GST_TAG_LANGUAGE_CODE, &code)) {
3533 const char *iso_code = gst_tag_get_language_code_iso_639_2T (code);
3534 if (iso_code) {
3535 GstQTPad *qtpad = gst_pad_get_element_private (pad);
3536 g_assert (qtpad);
3537 if (qtpad->trak) {
3538 /* https://developer.apple.com/library/mac/#documentation/QuickTime/QTFF/QTFFChap4/qtff4.html */
3539 qtpad->trak->mdia.mdhd.language_code =
3540 (iso_code[0] - 0x60) * 0x400 + (iso_code[1] - 0x60) * 0x20 +
3541 (iso_code[2] - 0x60);
3542 }
3543 }
3544 g_free (code);
3545 }
3546
Sebastian Dröge30689a82012-01-26 10:40:06 +01003547 gst_event_unref (event);
Mark Nauwelaerts04b4d302012-04-16 16:37:49 +02003548 event = NULL;
Sebastian Dröge30689a82012-01-26 10:40:06 +01003549 ret = TRUE;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003550 break;
Stefan Kost6a47f1f2008-12-16 16:26:52 +00003551 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003552 default:
3553 break;
3554 }
3555
Mark Nauwelaerts04b4d302012-04-16 16:37:49 +02003556 if (event != NULL)
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +02003557 return gst_collect_pads_event_default (pads, data, event, FALSE);
Mark Nauwelaerts04b4d302012-04-16 16:37:49 +02003558
Sebastian Dröge30689a82012-01-26 10:40:06 +01003559 return ret;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003560}
3561
3562static void
3563gst_qt_mux_release_pad (GstElement * element, GstPad * pad)
3564{
3565 GstQTMux *mux = GST_QT_MUX_CAST (element);
Thiago Santosc92785e2010-02-22 10:48:11 -03003566 GSList *walk;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003567
Edward Hervey0fa380b2010-06-21 19:39:54 +02003568 GST_DEBUG_OBJECT (element, "Releasing %s:%s", GST_DEBUG_PAD_NAME (pad));
3569
3570 for (walk = mux->sinkpads; walk; walk = g_slist_next (walk)) {
3571 GstQTPad *qtpad = (GstQTPad *) walk->data;
3572 GST_DEBUG ("Checking %s:%s", GST_DEBUG_PAD_NAME (qtpad->collect.pad));
3573 if (qtpad->collect.pad == pad) {
3574 /* this is it, remove */
3575 mux->sinkpads = g_slist_delete_link (mux->sinkpads, walk);
3576 gst_element_remove_pad (element, pad);
3577 break;
Thiago Santosc92785e2010-02-22 10:48:11 -03003578 }
Thiago Santosc92785e2010-02-22 10:48:11 -03003579 }
Edward Hervey0fa380b2010-06-21 19:39:54 +02003580
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +02003581 gst_collect_pads_remove_pad (mux->collect, pad);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003582}
3583
3584static GstPad *
3585gst_qt_mux_request_new_pad (GstElement * element,
Mark Nauwelaertsf8866812011-06-29 12:46:20 +02003586 GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003587{
3588 GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
3589 GstQTMux *qtmux = GST_QT_MUX_CAST (element);
3590 GstQTPad *collect_pad;
3591 GstPad *newpad;
Thiago Santos2ae18972014-02-05 10:27:54 -03003592 GstQTPadSetCapsFunc setcaps_func;
Stefan Kost05257fe2009-12-27 23:51:50 +02003593 gchar *name;
Mark Nauwelaerts611c9502011-07-04 11:07:13 +02003594 gint pad_id;
Matej Knoppfd3e8c52014-09-23 19:08:48 +02003595 gboolean lock = TRUE;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003596
Stefan Kost7d9ed162009-12-27 23:32:58 +02003597 if (templ->direction != GST_PAD_SINK)
3598 goto wrong_direction;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003599
Mark Nauwelaerts6beda8a2010-01-13 19:30:45 +01003600 if (qtmux->state > GST_QT_MUX_STATE_STARTED)
Stefan Kost7d9ed162009-12-27 23:32:58 +02003601 goto too_late;
3602
Wim Taymansa95acb72011-11-04 11:58:22 +01003603 if (templ == gst_element_class_get_pad_template (klass, "audio_%u")) {
Thiago Santos2ae18972014-02-05 10:27:54 -03003604 setcaps_func = gst_qt_mux_audio_sink_set_caps;
Wim Taymansa95acb72011-11-04 11:58:22 +01003605 if (req_name != NULL && sscanf (req_name, "audio_%u", &pad_id) == 1) {
Mark Nauwelaerts611c9502011-07-04 11:07:13 +02003606 name = g_strdup (req_name);
3607 } else {
Wim Taymansa95acb72011-11-04 11:58:22 +01003608 name = g_strdup_printf ("audio_%u", qtmux->audio_pads++);
Mark Nauwelaerts611c9502011-07-04 11:07:13 +02003609 }
Wim Taymansa95acb72011-11-04 11:58:22 +01003610 } else if (templ == gst_element_class_get_pad_template (klass, "video_%u")) {
Thiago Santos2ae18972014-02-05 10:27:54 -03003611 setcaps_func = gst_qt_mux_video_sink_set_caps;
Wim Taymansa95acb72011-11-04 11:58:22 +01003612 if (req_name != NULL && sscanf (req_name, "video_%u", &pad_id) == 1) {
Mark Nauwelaerts611c9502011-07-04 11:07:13 +02003613 name = g_strdup (req_name);
3614 } else {
Wim Taymansa95acb72011-11-04 11:58:22 +01003615 name = g_strdup_printf ("video_%u", qtmux->video_pads++);
Mark Nauwelaerts611c9502011-07-04 11:07:13 +02003616 }
Thiago Santos2ae18972014-02-05 10:27:54 -03003617 } else if (templ == gst_element_class_get_pad_template (klass, "subtitle_%u")) {
3618 setcaps_func = gst_qt_mux_subtitle_sink_set_caps;
3619 if (req_name != NULL && sscanf (req_name, "subtitle_%u", &pad_id) == 1) {
3620 name = g_strdup (req_name);
3621 } else {
3622 name = g_strdup_printf ("subtitle_%u", qtmux->subtitle_pads++);
3623 }
Matej Knoppfd3e8c52014-09-23 19:08:48 +02003624 lock = FALSE;
Stefan Kost7d9ed162009-12-27 23:32:58 +02003625 } else
3626 goto wrong_template;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003627
Stefan Kost05257fe2009-12-27 23:51:50 +02003628 GST_DEBUG_OBJECT (qtmux, "Requested pad: %s", name);
3629
3630 /* create pad and add to collections */
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003631 newpad = gst_pad_new_from_template (templ, name);
Stefan Kost05257fe2009-12-27 23:51:50 +02003632 g_free (name);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003633 collect_pad = (GstQTPad *)
Stefan Sauerf8749222012-09-12 21:05:44 +02003634 gst_collect_pads_add_pad (qtmux->collect, newpad, sizeof (GstQTPad),
Matej Knoppfd3e8c52014-09-23 19:08:48 +02003635 (GstCollectDataDestroyNotify) (gst_qt_mux_pad_reset), lock);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003636 /* set up pad */
3637 gst_qt_mux_pad_reset (collect_pad);
Mark Nauwelaertsce865cc2010-11-05 12:08:15 +01003638 collect_pad->trak = atom_trak_new (qtmux->context);
3639 atom_moov_add_trak (qtmux->moov, collect_pad->trak);
Marc-André Lureau2618cb02009-09-28 16:11:35 +02003640
Thiago Santosc92785e2010-02-22 10:48:11 -03003641 qtmux->sinkpads = g_slist_append (qtmux->sinkpads, collect_pad);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003642
3643 /* set up pad functions */
Thiago Santos2ae18972014-02-05 10:27:54 -03003644 collect_pad->set_caps = setcaps_func;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003645
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003646 gst_pad_set_active (newpad, TRUE);
3647 gst_element_add_pad (element, newpad);
3648
3649 return newpad;
Stefan Kost7d9ed162009-12-27 23:32:58 +02003650
3651 /* ERRORS */
3652wrong_direction:
3653 {
3654 GST_WARNING_OBJECT (qtmux, "Request pad that is not a SINK pad.");
3655 return NULL;
3656 }
3657too_late:
3658 {
3659 GST_WARNING_OBJECT (qtmux, "Not providing request pad after stream start.");
3660 return NULL;
3661 }
3662wrong_template:
3663 {
3664 GST_WARNING_OBJECT (qtmux, "This is not our template!");
3665 return NULL;
3666 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003667}
3668
3669static void
3670gst_qt_mux_get_property (GObject * object,
3671 guint prop_id, GValue * value, GParamSpec * pspec)
3672{
3673 GstQTMux *qtmux = GST_QT_MUX_CAST (object);
3674
3675 GST_OBJECT_LOCK (qtmux);
3676 switch (prop_id) {
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003677 case PROP_MOVIE_TIMESCALE:
3678 g_value_set_uint (value, qtmux->timescale);
3679 break;
Marc-André Lureaubee00aa2010-07-30 12:48:29 +02003680 case PROP_TRAK_TIMESCALE:
3681 g_value_set_uint (value, qtmux->trak_timescale);
3682 break;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003683 case PROP_DO_CTTS:
David Schleefb11186e2008-11-15 02:56:31 +00003684 g_value_set_boolean (value, qtmux->guess_pts);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003685 break;
David Schleef6571e382013-03-22 13:24:33 -07003686#ifndef GST_REMOVE_DEPRECATED
Mark Nauwelaertsae644932011-01-03 17:24:23 +01003687 case PROP_DTS_METHOD:
3688 g_value_set_enum (value, qtmux->dts_method);
3689 break;
David Schleef6571e382013-03-22 13:24:33 -07003690#endif
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003691 case PROP_FAST_START:
3692 g_value_set_boolean (value, qtmux->fast_start);
3693 break;
3694 case PROP_FAST_START_TEMP_FILE:
3695 g_value_set_string (value, qtmux->fast_start_file_path);
3696 break;
Thiago Santosb692f9f2009-12-12 16:07:15 -03003697 case PROP_MOOV_RECOV_FILE:
3698 g_value_set_string (value, qtmux->moov_recov_file_path);
3699 break;
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01003700 case PROP_FRAGMENT_DURATION:
3701 g_value_set_uint (value, qtmux->fragment_duration);
3702 break;
Marc-André Lureau61964eb2010-08-06 13:26:27 +02003703 case PROP_STREAMABLE:
3704 g_value_set_boolean (value, qtmux->streamable);
3705 break;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003706 default:
3707 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3708 break;
3709 }
3710 GST_OBJECT_UNLOCK (qtmux);
3711}
3712
3713static void
3714gst_qt_mux_generate_fast_start_file_path (GstQTMux * qtmux)
3715{
3716 gchar *tmp;
3717
Thiago Santos6dc515b2010-01-26 13:44:04 -03003718 g_free (qtmux->fast_start_file_path);
3719 qtmux->fast_start_file_path = NULL;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003720
3721 tmp = g_strdup_printf ("%s%d", "qtmux", g_random_int ());
3722 qtmux->fast_start_file_path = g_build_filename (g_get_tmp_dir (), tmp, NULL);
3723 g_free (tmp);
3724}
3725
3726static void
3727gst_qt_mux_set_property (GObject * object,
3728 guint prop_id, const GValue * value, GParamSpec * pspec)
3729{
3730 GstQTMux *qtmux = GST_QT_MUX_CAST (object);
3731
3732 GST_OBJECT_LOCK (qtmux);
3733 switch (prop_id) {
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003734 case PROP_MOVIE_TIMESCALE:
3735 qtmux->timescale = g_value_get_uint (value);
3736 break;
Marc-André Lureaubee00aa2010-07-30 12:48:29 +02003737 case PROP_TRAK_TIMESCALE:
3738 qtmux->trak_timescale = g_value_get_uint (value);
3739 break;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003740 case PROP_DO_CTTS:
David Schleefb11186e2008-11-15 02:56:31 +00003741 qtmux->guess_pts = g_value_get_boolean (value);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003742 break;
David Schleef6571e382013-03-22 13:24:33 -07003743#ifndef GST_REMOVE_DEPRECATED
Mark Nauwelaertsae644932011-01-03 17:24:23 +01003744 case PROP_DTS_METHOD:
3745 qtmux->dts_method = g_value_get_enum (value);
3746 break;
David Schleef6571e382013-03-22 13:24:33 -07003747#endif
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003748 case PROP_FAST_START:
3749 qtmux->fast_start = g_value_get_boolean (value);
3750 break;
3751 case PROP_FAST_START_TEMP_FILE:
Thiago Santos6dc515b2010-01-26 13:44:04 -03003752 g_free (qtmux->fast_start_file_path);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003753 qtmux->fast_start_file_path = g_value_dup_string (value);
3754 /* NULL means to generate a random one */
3755 if (!qtmux->fast_start_file_path) {
3756 gst_qt_mux_generate_fast_start_file_path (qtmux);
3757 }
3758 break;
Thiago Santosb692f9f2009-12-12 16:07:15 -03003759 case PROP_MOOV_RECOV_FILE:
3760 g_free (qtmux->moov_recov_file_path);
3761 qtmux->moov_recov_file_path = g_value_dup_string (value);
3762 break;
Mark Nauwelaerts4b64dc22010-11-15 15:17:59 +01003763 case PROP_FRAGMENT_DURATION:
3764 qtmux->fragment_duration = g_value_get_uint (value);
3765 break;
Andoni Morales Alastruey57326842013-09-17 12:01:30 +02003766 case PROP_STREAMABLE:{
3767 GstQTMuxClass *qtmux_klass =
3768 (GstQTMuxClass *) (G_OBJECT_GET_CLASS (qtmux));
3769 if (qtmux_klass->format == GST_QT_MUX_FORMAT_ISML) {
3770 qtmux->streamable = g_value_get_boolean (value);
3771 }
Marc-André Lureau61964eb2010-08-06 13:26:27 +02003772 break;
Andoni Morales Alastruey57326842013-09-17 12:01:30 +02003773 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003774 default:
3775 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3776 break;
3777 }
3778 GST_OBJECT_UNLOCK (qtmux);
3779}
3780
3781static GstStateChangeReturn
3782gst_qt_mux_change_state (GstElement * element, GstStateChange transition)
3783{
3784 GstStateChangeReturn ret;
3785 GstQTMux *qtmux = GST_QT_MUX_CAST (element);
3786
3787 switch (transition) {
3788 case GST_STATE_CHANGE_NULL_TO_READY:
3789 break;
3790 case GST_STATE_CHANGE_READY_TO_PAUSED:
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +02003791 gst_collect_pads_start (qtmux->collect);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003792 qtmux->state = GST_QT_MUX_STATE_STARTED;
3793 break;
3794 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3795 break;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003796 case GST_STATE_CHANGE_PAUSED_TO_READY:
Mark Nauwelaerts67e168a2012-04-17 15:14:27 +02003797 gst_collect_pads_stop (qtmux->collect);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003798 break;
3799 default:
3800 break;
3801 }
3802
3803 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3804
3805 switch (transition) {
3806 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3807 break;
3808 case GST_STATE_CHANGE_PAUSED_TO_READY:
3809 gst_qt_mux_reset (qtmux, TRUE);
3810 break;
3811 case GST_STATE_CHANGE_READY_TO_NULL:
3812 break;
3813 default:
3814 break;
3815 }
3816
3817 return ret;
3818}
3819
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003820gboolean
3821gst_qt_mux_register (GstPlugin * plugin)
3822{
3823 GTypeInfo typeinfo = {
3824 sizeof (GstQTMuxClass),
3825 (GBaseInitFunc) gst_qt_mux_base_init,
3826 NULL,
3827 (GClassInitFunc) gst_qt_mux_class_init,
3828 NULL,
3829 NULL,
3830 sizeof (GstQTMux),
3831 0,
3832 (GInstanceInitFunc) gst_qt_mux_init,
3833 };
3834 static const GInterfaceInfo tag_setter_info = {
3835 NULL, NULL, NULL
3836 };
Thiago Santosaba80002011-03-21 10:56:51 -03003837 static const GInterfaceInfo tag_xmp_writer_info = {
3838 NULL, NULL, NULL
3839 };
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003840 GType type;
3841 GstQTMuxFormat format;
3842 GstQTMuxClassParams *params;
3843 guint i = 0;
3844
Tim-Philipp Müller1663c512010-03-25 00:20:54 +00003845 GST_DEBUG_CATEGORY_INIT (gst_qt_mux_debug, "qtmux", 0, "QT Muxer");
3846
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003847 GST_LOG ("Registering muxers");
3848
3849 while (TRUE) {
3850 GstQTMuxFormatProp *prop;
Thiago Santos2ae18972014-02-05 10:27:54 -03003851 GstCaps *subtitle_caps;
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003852
3853 prop = &gst_qt_mux_format_list[i];
3854 format = prop->format;
3855 if (format == GST_QT_MUX_FORMAT_NONE)
3856 break;
3857
3858 /* create a cache for these properties */
3859 params = g_new0 (GstQTMuxClassParams, 1);
3860 params->prop = prop;
3861 params->src_caps = gst_static_caps_get (&prop->src_caps);
3862 params->video_sink_caps = gst_static_caps_get (&prop->video_sink_caps);
3863 params->audio_sink_caps = gst_static_caps_get (&prop->audio_sink_caps);
Thiago Santos2ae18972014-02-05 10:27:54 -03003864 subtitle_caps = gst_static_caps_get (&prop->subtitle_sink_caps);
3865 if (!gst_caps_is_equal (subtitle_caps, GST_CAPS_NONE)) {
3866 params->subtitle_sink_caps = subtitle_caps;
3867 } else {
3868 gst_caps_unref (subtitle_caps);
3869 }
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003870
3871 /* create the type now */
3872 type = g_type_register_static (GST_TYPE_ELEMENT, prop->type_name, &typeinfo,
3873 0);
3874 g_type_set_qdata (type, GST_QT_MUX_PARAMS_QDATA, (gpointer) params);
3875 g_type_add_interface_static (type, GST_TYPE_TAG_SETTER, &tag_setter_info);
Thiago Santosaba80002011-03-21 10:56:51 -03003876 g_type_add_interface_static (type, GST_TYPE_TAG_XMP_WRITER,
3877 &tag_xmp_writer_info);
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003878
Tim-Philipp Müller0671b232011-04-21 22:51:52 +01003879 if (!gst_element_register (plugin, prop->name, prop->rank, type))
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003880 return FALSE;
3881
3882 i++;
3883 }
3884
3885 GST_LOG ("Finished registering muxers");
3886
Thiago Santos083f8252009-12-10 16:53:19 -03003887 /* FIXME: ideally classification tag should be added and
3888 registered in gstreamer core gsttaglist
3889 */
3890
3891 GST_LOG ("Registering tags");
3892
3893 gst_tag_register (GST_TAG_3GP_CLASSIFICATION, GST_TAG_FLAG_META,
3894 G_TYPE_STRING, GST_TAG_3GP_CLASSIFICATION, "content classification",
3895 gst_tag_merge_use_first);
3896
3897 GST_LOG ("Finished registering tags");
3898
Thiago Sousa Santosc991a042008-11-08 02:00:58 +00003899 return TRUE;
3900}