• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Quicktime muxer plugin for GStreamer
2  * Copyright (C) 2008 Thiago Sousa Santos <thiagoss@embedded.ufcg.edu.br>
3  * Copyright (C) 2008 Mark Nauwelaerts <mnauw@users.sf.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 /*
21  * Unless otherwise indicated, Source Code is licensed under MIT license.
22  * See further explanation attached in License Statement (distributed in the file
23  * LICENSE).
24  *
25  * Permission is hereby granted, free of charge, to any person obtaining a copy of
26  * this software and associated documentation files (the "Software"), to deal in
27  * the Software without restriction, including without limitation the rights to
28  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29  * of the Software, and to permit persons to whom the Software is furnished to do
30  * so, subject to the following conditions:
31  *
32  * The above copyright notice and this permission notice shall be included in all
33  * copies or substantial portions of the Software.
34  *
35  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41  * SOFTWARE.
42  */
43 
44 #include "gstqtmuxmap.h"
45 #include "fourcc.h"
46 
47 /* static info related to various format */
48 
49 #define COMMON_VIDEO_CAPS \
50   "width = (int) [ 16, MAX ], " \
51   "height = (int) [ 16, MAX ]"
52 
53 #define COMMON_VIDEO_CAPS_NO_FRAMERATE \
54   "width = (int) [ 16, MAX ], " \
55   "height = (int) [ 16, MAX ] "
56 
57 #define H263_CAPS \
58   "video/x-h263, " \
59   COMMON_VIDEO_CAPS
60 
61 #define H264_CAPS \
62   "video/x-h264, " \
63   "stream-format = (string) { avc, avc3 }, " \
64   "alignment = (string) au, " \
65   COMMON_VIDEO_CAPS
66 
67 #define H265_CAPS \
68   "video/x-h265, " \
69   "stream-format = (string) { hvc1, hev1 }, " \
70   "alignment = (string) au, " \
71   COMMON_VIDEO_CAPS
72 
73 #define MPEG4V_CAPS \
74   "video/mpeg, " \
75   "mpegversion = (int) 4, "\
76   "systemstream = (boolean) false, " \
77   COMMON_VIDEO_CAPS "; " \
78   "video/x-divx, " \
79   "divxversion = (int) 5, "\
80   COMMON_VIDEO_CAPS
81 
82 #define PRORES_CAPS \
83   "video/x-prores, " \
84   "variant = (string) {standard, lt, hq, proxy, 4444, 4444xq}, " \
85   COMMON_VIDEO_CAPS
86 
87 #define CINEFORM_CAPS \
88   "video/x-cineform, " \
89   COMMON_VIDEO_CAPS
90 
91 #define SVQ_CAPS \
92   "video/x-svq, " \
93   "svqversion = (int) 3, " \
94   COMMON_VIDEO_CAPS
95 
96 #define COMMON_AUDIO_CAPS(c, r) \
97   "channels = (int) [ 1, " G_STRINGIFY (c) " ], " \
98   "rate = (int) [ 1, " G_STRINGIFY (r) " ]"
99 
100 #define PCM_CAPS \
101   "audio/x-raw, " \
102   "format = (string) { S16LE, S16BE, S8, U8 }, " \
103   "layout = (string) interleaved, " \
104   COMMON_AUDIO_CAPS (2, MAX)
105 
106 #define PCM_CAPS_FULL \
107   "audio/x-raw, " \
108   "format = (string) { S32LE, S32BE, S24LE, S24BE, S16LE, S16BE, S8, U8 }, " \
109   "layout = (string) interleaved, " \
110   COMMON_AUDIO_CAPS (2, MAX)
111 
112 #define PCM_CAPS_UNPOSITIONED \
113   "audio/x-raw, " \
114   "format = (string) { S32LE, S32BE, S24LE, S24BE, S16LE, S16BE, S8, U8 }, " \
115   "layout = (string) interleaved, " \
116   "channel-mask = (bitmask) 0, " \
117   COMMON_AUDIO_CAPS (16, MAX)
118 
119 #define MP3_CAPS \
120   "audio/mpeg, " \
121   "mpegversion = (int) 1, " \
122   "layer = (int) 3, " \
123   COMMON_AUDIO_CAPS (2, MAX)
124 
125 #define MP123_CAPS \
126   "audio/mpeg, " \
127   "mpegversion = (int) 1, " \
128   "layer = (int) [1, 3], " \
129   COMMON_AUDIO_CAPS (2, MAX)
130 
131 #define AAC_CAPS \
132   "audio/mpeg, " \
133   "mpegversion = (int) 4, " \
134   "stream-format = (string) raw, " \
135   COMMON_AUDIO_CAPS (8, MAX)
136 
137 #define AC3_CAPS \
138   "audio/x-ac3, " \
139   COMMON_AUDIO_CAPS (6, MAX)
140 
141 #define AMR_CAPS \
142   "audio/AMR, " \
143   "rate = (int) 8000, " \
144   "channels = [ 1, 2 ]; " \
145   "audio/AMR-WB, " \
146   "rate = (int) 16000, " \
147   "channels = [ 1, 2 ] "
148 
149 #define ADPCM_CAPS  \
150   "audio/x-adpcm, " \
151   "layout = (string)dvi, " \
152   "block_align = (int)[64, 8096], " \
153   COMMON_AUDIO_CAPS(2, MAX)
154 
155 #define ALAC_CAPS \
156   "audio/x-alac, " \
157   COMMON_AUDIO_CAPS(2, MAX)
158 
159 #define OPUS_CAPS \
160   "audio/x-opus, " \
161   "channel-mapping-family = (int) [0, 255], " \
162   COMMON_AUDIO_CAPS(8, MAX)
163 
164 
165 #define TEXT_UTF8 \
166   "text/x-raw, " \
167   "format=(string)utf8"
168 
169 #define CEA608_CAPS \
170   "closedcaption/x-cea-608, format=(string)s334-1a"
171 #define CEA708_CAPS \
172   "closedcaption/x-cea-708, format=(string)cdp"
173 
174 /* FIXME 0.11 - take a look at bugs #580005 and #340375 */
175 GstQTMuxFormatProp gst_qt_mux_format_list[] = {
176   /* original QuickTime format; see Apple site (e.g. qtff.pdf) */
177   {
178         GST_QT_MUX_FORMAT_QT,
179         GST_RANK_PRIMARY,
180         "qtmux",
181         "QuickTime",
182         "GstQTMux",
183         GST_STATIC_CAPS ("video/quicktime, variant = (string) apple; "
184             "video/quicktime"),
185         GST_STATIC_CAPS ("video/x-raw, "
186             "format = (string) { RGB, UYVY, v210 }, "
187             COMMON_VIDEO_CAPS "; "
188             MPEG4V_CAPS "; "
189             PRORES_CAPS "; "
190             CINEFORM_CAPS "; "
191             H263_CAPS "; "
192             H264_CAPS "; "
193             H265_CAPS "; "
194             SVQ_CAPS "; "
195             "video/x-dv, "
196             "systemstream = (boolean) false, "
197             COMMON_VIDEO_CAPS "; "
198             "image/jpeg, "
199             COMMON_VIDEO_CAPS_NO_FRAMERATE "; "
200             "image/png, "
201             COMMON_VIDEO_CAPS_NO_FRAMERATE "; "
202             "video/x-vp8, "
203             COMMON_VIDEO_CAPS "; "
204             "video/x-vp9, "
205             COMMON_VIDEO_CAPS "; "
206             "video/x-dirac, "
207             COMMON_VIDEO_CAPS "; " "video/x-qt-part, " COMMON_VIDEO_CAPS "; "
208             "video/x-av1, " COMMON_VIDEO_CAPS),
209         GST_STATIC_CAPS (PCM_CAPS_FULL "; "
210             PCM_CAPS_UNPOSITIONED " ; "
211             MP123_CAPS " ; "
212             AAC_CAPS " ; "
213             AC3_CAPS " ; "
214             ADPCM_CAPS " ; "
215             "audio/x-alaw, " COMMON_AUDIO_CAPS (2, MAX) "; "
216             "audio/x-mulaw, " COMMON_AUDIO_CAPS (2, MAX) "; "
217             AMR_CAPS " ; " ALAC_CAPS " ; " OPUS_CAPS),
218         GST_STATIC_CAPS (TEXT_UTF8),
219       GST_STATIC_CAPS (CEA608_CAPS "; " CEA708_CAPS)}
220   ,
221   /* ISO 14496-14: mp42 as ISO base media extension
222    * (supersedes original ISO 144996-1 mp41) */
223   {
224         GST_QT_MUX_FORMAT_MP4,
225         GST_RANK_PRIMARY,
226         "mp4mux",
227         "MP4",
228         "GstMP4Mux",
229         GST_STATIC_CAPS ("video/quicktime, variant = (string) iso"),
230         GST_STATIC_CAPS (MPEG4V_CAPS "; " H264_CAPS ";" H265_CAPS ";"
231             "video/x-mp4-part," COMMON_VIDEO_CAPS "; "
232             "video/x-av1, " COMMON_VIDEO_CAPS),
233         GST_STATIC_CAPS (MP123_CAPS "; "
234             AAC_CAPS " ; " AC3_CAPS " ; " ALAC_CAPS " ; " OPUS_CAPS),
235         GST_STATIC_CAPS (TEXT_UTF8),
236       GST_STATIC_CAPS_NONE}
237   ,
238   /* Microsoft Smooth Streaming fmp4/isml */
239   /* TODO add WMV/WMA support */
240   {
241         GST_QT_MUX_FORMAT_ISML,
242         GST_RANK_PRIMARY,
243         "ismlmux",
244         "ISML",
245         "GstISMLMux",
246         GST_STATIC_CAPS ("video/quicktime, variant = (string) iso-fragmented"),
247         GST_STATIC_CAPS (MPEG4V_CAPS "; " H264_CAPS),
248         GST_STATIC_CAPS (MP3_CAPS "; " AAC_CAPS),
249         GST_STATIC_CAPS_NONE,
250       GST_STATIC_CAPS_NONE}
251   ,
252   /* 3GPP Technical Specification 26.244 V7.3.0
253    * (extended in 3GPP2 File Formats for Multimedia Services) */
254   {
255         GST_QT_MUX_FORMAT_3GP,
256         GST_RANK_PRIMARY,
257         "3gppmux",
258         "3GPP",
259         "Gst3GPPMux",
260         GST_STATIC_CAPS ("video/quicktime, variant = (string) 3gpp"),
261         GST_STATIC_CAPS (H263_CAPS "; " MPEG4V_CAPS "; " H264_CAPS),
262         GST_STATIC_CAPS (AMR_CAPS "; " MP3_CAPS "; " AAC_CAPS "; " AC3_CAPS),
263         GST_STATIC_CAPS (TEXT_UTF8),
264       GST_STATIC_CAPS_NONE}
265   ,
266   /* ISO 15444-3: Motion-JPEG-2000 (also ISO base media extension) */
267   {
268         GST_QT_MUX_FORMAT_MJ2,
269         GST_RANK_PRIMARY,
270         "mj2mux",
271         "MJ2",
272         "GstMJ2Mux",
273         GST_STATIC_CAPS ("video/mj2"),
274         GST_STATIC_CAPS ("image/x-j2c, " COMMON_VIDEO_CAPS "; "
275             "image/x-jpc, " COMMON_VIDEO_CAPS),
276         GST_STATIC_CAPS (PCM_CAPS),
277         GST_STATIC_CAPS_NONE,
278       GST_STATIC_CAPS_NONE}
279   ,
280   {
281         GST_QT_MUX_FORMAT_NONE,
282       }
283 };
284 
285 /* pretty static, but may turn out needed a few times */
286 AtomsTreeFlavor
gst_qt_mux_map_format_to_flavor(GstQTMuxFormat format)287 gst_qt_mux_map_format_to_flavor (GstQTMuxFormat format)
288 {
289   if (format == GST_QT_MUX_FORMAT_QT)
290     return ATOMS_TREE_FLAVOR_MOV;
291   else if (format == GST_QT_MUX_FORMAT_3GP)
292     return ATOMS_TREE_FLAVOR_3GP;
293   else if (format == GST_QT_MUX_FORMAT_ISML)
294     return ATOMS_TREE_FLAVOR_ISML;
295   else
296     return ATOMS_TREE_FLAVOR_ISOM;
297 }
298 
299 static void
gst_qt_mux_map_check_tracks(AtomMOOV * moov,gint * _video,gint * _audio,gboolean * _has_h264)300 gst_qt_mux_map_check_tracks (AtomMOOV * moov, gint * _video, gint * _audio,
301     gboolean * _has_h264)
302 {
303   GList *it;
304   gint video = 0, audio = 0;
305   gboolean has_h264 = FALSE;
306 
307   for (it = moov->traks; it != NULL; it = g_list_next (it)) {
308     AtomTRAK *track = it->data;
309 
310     if (track->is_video) {
311       video++;
312       if (track->is_h264)
313         has_h264 = TRUE;
314     } else
315       audio++;
316   }
317 
318   if (_video)
319     *_video = video;
320   if (_audio)
321     *_audio = audio;
322   if (_has_h264)
323     *_has_h264 = has_h264;
324 }
325 
326 /* pretty static, but possibly dynamic format info */
327 
328 /* notes:
329  * - avc1 brand is not used, since the specific extensions indicated by it
330  *   are not used (e.g. sample groupings, etc)
331  * - TODO: maybe even more 3GPP brand fine-tuning ??
332  *   (but that might need ftyp rewriting at the end) */
333 void
gst_qt_mux_map_format_to_header(GstQTMuxFormat format,GstBuffer ** _prefix,guint32 * _major,guint32 * _version,GList ** _compatible,AtomMOOV * moov,GstClockTime longest_chunk,gboolean faststart)334 gst_qt_mux_map_format_to_header (GstQTMuxFormat format, GstBuffer ** _prefix,
335     guint32 * _major, guint32 * _version, GList ** _compatible, AtomMOOV * moov,
336     GstClockTime longest_chunk, gboolean faststart)
337 {
338   static const guint32 qt_brands[] = { 0 };
339   static const guint32 mp4_brands[] =
340       { FOURCC_mp41, FOURCC_isom, FOURCC_iso2, 0 };
341   static const guint32 isml_brands[] = { FOURCC_iso2, 0 };
342   static const guint32 gpp_brands[] = { FOURCC_isom, FOURCC_iso2, 0 };
343   static const guint32 mjp2_brands[] = { FOURCC_isom, FOURCC_iso2, 0 };
344   static const guint8 mjp2_prefix[] =
345       { 0, 0, 0, 12, 'j', 'P', ' ', ' ', 0x0D, 0x0A, 0x87, 0x0A };
346   const guint32 *comp = NULL;
347   guint32 major = 0, version = 0;
348   GstBuffer *prefix = NULL;
349   GList *result = NULL;
350 
351   g_return_if_fail (_prefix != NULL);
352   g_return_if_fail (_major != NULL);
353   g_return_if_fail (_version != NULL);
354   g_return_if_fail (_compatible != NULL);
355 
356   switch (format) {
357     case GST_QT_MUX_FORMAT_QT:
358       major = FOURCC_qt__;
359       comp = qt_brands;
360       version = 0x20050300;
361       break;
362     case GST_QT_MUX_FORMAT_MP4:
363       major = FOURCC_mp42;
364       comp = mp4_brands;
365       break;
366     case GST_QT_MUX_FORMAT_ISML:
367       major = FOURCC_isml;
368       comp = isml_brands;
369       break;
370     case GST_QT_MUX_FORMAT_3GP:
371     {
372       gint video, audio;
373       gboolean has_h264;
374 
375       gst_qt_mux_map_check_tracks (moov, &video, &audio, &has_h264);
376       /* only track restriction really matters for Basic Profile */
377       if (video <= 1 && audio <= 1) {
378         /* it seems only newer spec knows about H264 */
379         major = has_h264 ? FOURCC_3gp6 : FOURCC_3gp4;
380         version = has_h264 ? 0x100 : 0x200;
381       } else {
382         major = FOURCC_3gg6;
383         version = 0x100;
384       }
385       comp = gpp_brands;
386 
387       /*
388        * We assume that we have chunks in dts order
389        */
390       if (faststart && longest_chunk <= GST_SECOND) {
391         /* add progressive download profile */
392         result = g_list_append (result, GUINT_TO_POINTER (FOURCC_3gr6));
393       }
394       break;
395     }
396     case GST_QT_MUX_FORMAT_MJ2:
397     {
398       major = FOURCC_mjp2;
399       comp = mjp2_brands;
400       version = 0;
401       prefix = gst_buffer_new_and_alloc (sizeof (mjp2_prefix));
402       gst_buffer_fill (prefix, 0, mjp2_prefix, sizeof (mjp2_prefix));
403       break;
404     }
405     default:
406       g_assert_not_reached ();
407       break;
408   }
409 
410   /* convert list to list, hm */
411   while (comp && *comp != 0) {
412     /* order matters over efficiency */
413     result = g_list_append (result, GUINT_TO_POINTER (*comp));
414     comp++;
415   }
416 
417   *_major = major;
418   *_version = version;
419   *_prefix = prefix;
420   *_compatible = result;
421 
422   /* TODO 3GPP may include mp42 as compatible if applicable */
423   /* TODO 3GPP major brand 3gp7 if at most 1 video and audio track */
424 }
425