qtdemux: Add/Fix comments on the various structure variables
[platform/upstream/gst-plugins-good.git] / gst / isomp4 / gstqtmuxmap.c
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, " \
64   "alignment = (string) au, " \
65   COMMON_VIDEO_CAPS
66
67 #define MPEG4V_CAPS \
68   "video/mpeg, " \
69   "mpegversion = (int) 4, "\
70   "systemstream = (boolean) false, " \
71   COMMON_VIDEO_CAPS "; " \
72   "video/x-divx, " \
73   "divxversion = (int) 5, "\
74   COMMON_VIDEO_CAPS
75
76 #define PRORES_CAPS \
77   "video/x-prores, variant = (string) {standard, lt, hq, proxy}, " \
78   COMMON_VIDEO_CAPS
79
80 #define SVQ_CAPS \
81   "video/x-svq, " \
82   "svqversion = (int) 3, " \
83   COMMON_VIDEO_CAPS
84
85 #define COMMON_AUDIO_CAPS(c, r) \
86   "channels = (int) [ 1, " G_STRINGIFY (c) " ], " \
87   "rate = (int) [ 1, " G_STRINGIFY (r) " ]"
88
89 #define PCM_CAPS \
90   "audio/x-raw, " \
91   "format = (string) { S16LE, S16BE, S8, U8 }, " \
92   "layout = (string) interleaved, " \
93   COMMON_AUDIO_CAPS (2, MAX)
94
95 #define PCM_CAPS_FULL \
96   "audio/x-raw, " \
97   "format = (string) { S32LE, S32BE, S24LE, S24BE, S16LE, S16BE, S8, U8 }, " \
98   "layout = (string) interleaved, " \
99   COMMON_AUDIO_CAPS (2, MAX)
100
101 #define MP3_CAPS \
102   "audio/mpeg, " \
103   "mpegversion = (int) 1, " \
104   "layer = (int) 3, " \
105   COMMON_AUDIO_CAPS (2, MAX)
106
107 #define MP123_CAPS \
108   "audio/mpeg, " \
109   "mpegversion = (int) 1, " \
110   "layer = (int) [1, 3], " \
111   COMMON_AUDIO_CAPS (2, MAX)
112
113 #define AAC_CAPS \
114   "audio/mpeg, " \
115   "mpegversion = (int) 4, " \
116   "stream-format = (string) raw, " \
117   COMMON_AUDIO_CAPS (8, MAX)
118
119 #define AC3_CAPS \
120   "audio/x-ac3, " \
121   COMMON_AUDIO_CAPS (6, MAX)
122
123 #define AMR_CAPS \
124   "audio/AMR, " \
125   "rate = (int) 8000, " \
126   "channels = [ 1, 2 ]; " \
127   "audio/AMR-WB, " \
128   "rate = (int) 16000, " \
129   "channels = [ 1, 2 ] "
130
131 #define ADPCM_CAPS  \
132   "audio/x-adpcm, " \
133   "layout = (string)dvi, " \
134   "block_align = (int)[64, 8096], " \
135   COMMON_AUDIO_CAPS(2, MAX)
136
137 #define ALAC_CAPS \
138   "audio/x-alac, " \
139   COMMON_AUDIO_CAPS(2, MAX)
140
141 #define OPUS_CAPS \
142   "audio/x-opus, " \
143   "channel-mapping-family = (int) [0, 255], " \
144   COMMON_AUDIO_CAPS(8, MAX)
145
146
147 #define TEXT_UTF8 \
148   "text/x-raw, " \
149   "format=(string)utf8"
150
151 /* FIXME 0.11 - take a look at bugs #580005 and #340375 */
152 GstQTMuxFormatProp gst_qt_mux_format_list[] = {
153   /* original QuickTime format; see Apple site (e.g. qtff.pdf) */
154   {
155         GST_QT_MUX_FORMAT_QT,
156         GST_RANK_PRIMARY,
157         "qtmux",
158         "QuickTime",
159         "GstQTMux",
160         GST_STATIC_CAPS ("video/quicktime, variant = (string) apple; "
161             "video/quicktime"),
162         GST_STATIC_CAPS ("video/x-raw, "
163             "format = (string) { RGB, UYVY, v210 }, "
164             COMMON_VIDEO_CAPS "; "
165             MPEG4V_CAPS "; "
166             PRORES_CAPS "; "
167             H263_CAPS "; "
168             H264_CAPS "; "
169             SVQ_CAPS "; "
170             "video/x-dv, "
171             "systemstream = (boolean) false, "
172             COMMON_VIDEO_CAPS "; "
173             "image/jpeg, "
174             COMMON_VIDEO_CAPS_NO_FRAMERATE "; "
175             "video/x-vp8, "
176             COMMON_VIDEO_CAPS "; "
177             "video/x-dirac, "
178             COMMON_VIDEO_CAPS "; " "video/x-qt-part, " COMMON_VIDEO_CAPS),
179         GST_STATIC_CAPS (PCM_CAPS_FULL "; "
180             MP3_CAPS " ; "
181             AAC_CAPS " ; "
182             ADPCM_CAPS " ; "
183             "audio/x-alaw, " COMMON_AUDIO_CAPS (2, MAX) "; "
184             "audio/x-mulaw, " COMMON_AUDIO_CAPS (2, MAX) "; "
185             AMR_CAPS " ; " ALAC_CAPS),
186       GST_STATIC_CAPS (TEXT_UTF8)}
187   ,
188   /* ISO 14496-14: mp42 as ISO base media extension
189    * (supersedes original ISO 144996-1 mp41) */
190   {
191         GST_QT_MUX_FORMAT_MP4,
192         GST_RANK_PRIMARY,
193         "mp4mux",
194         "MP4",
195         "GstMP4Mux",
196         GST_STATIC_CAPS ("video/quicktime, variant = (string) iso"),
197         GST_STATIC_CAPS (MPEG4V_CAPS "; " H264_CAPS ";"
198             "video/x-mp4-part," COMMON_VIDEO_CAPS),
199         GST_STATIC_CAPS (MP123_CAPS "; "
200             AAC_CAPS " ; " AC3_CAPS " ; " ALAC_CAPS " ; " OPUS_CAPS),
201       GST_STATIC_CAPS (TEXT_UTF8)}
202   ,
203   /* Microsoft Smooth Streaming fmp4/isml */
204   /* TODO add WMV/WMA support */
205   {
206         GST_QT_MUX_FORMAT_ISML,
207         GST_RANK_PRIMARY,
208         "ismlmux",
209         "ISML",
210         "GstISMLMux",
211         GST_STATIC_CAPS ("video/quicktime, variant = (string) iso-fragmented"),
212         GST_STATIC_CAPS (MPEG4V_CAPS "; " H264_CAPS),
213         GST_STATIC_CAPS (MP3_CAPS "; " AAC_CAPS),
214       GST_STATIC_CAPS_NONE}
215   ,
216   /* 3GPP Technical Specification 26.244 V7.3.0
217    * (extended in 3GPP2 File Formats for Multimedia Services) */
218   {
219         GST_QT_MUX_FORMAT_3GP,
220         GST_RANK_PRIMARY,
221         "3gppmux",
222         "3GPP",
223         "Gst3GPPMux",
224         GST_STATIC_CAPS ("video/quicktime, variant = (string) 3gpp"),
225         GST_STATIC_CAPS (H263_CAPS "; " MPEG4V_CAPS "; " H264_CAPS),
226         GST_STATIC_CAPS (AMR_CAPS "; " MP3_CAPS "; " AAC_CAPS "; " AC3_CAPS),
227       GST_STATIC_CAPS (TEXT_UTF8)}
228   ,
229   /* ISO 15444-3: Motion-JPEG-2000 (also ISO base media extension) */
230   {
231         GST_QT_MUX_FORMAT_MJ2,
232         GST_RANK_PRIMARY,
233         "mj2mux",
234         "MJ2",
235         "GstMJ2Mux",
236         GST_STATIC_CAPS ("video/mj2"),
237         GST_STATIC_CAPS ("image/x-j2c, " COMMON_VIDEO_CAPS "; "
238             "image/x-jpc, " COMMON_VIDEO_CAPS),
239         GST_STATIC_CAPS (PCM_CAPS),
240       GST_STATIC_CAPS_NONE}
241   ,
242   {
243         GST_QT_MUX_FORMAT_NONE,
244       }
245 };
246
247 /* pretty static, but may turn out needed a few times */
248 AtomsTreeFlavor
249 gst_qt_mux_map_format_to_flavor (GstQTMuxFormat format)
250 {
251   if (format == GST_QT_MUX_FORMAT_QT)
252     return ATOMS_TREE_FLAVOR_MOV;
253   else if (format == GST_QT_MUX_FORMAT_3GP)
254     return ATOMS_TREE_FLAVOR_3GP;
255   else if (format == GST_QT_MUX_FORMAT_ISML)
256     return ATOMS_TREE_FLAVOR_ISML;
257   else
258     return ATOMS_TREE_FLAVOR_ISOM;
259 }
260
261 static void
262 gst_qt_mux_map_check_tracks (AtomMOOV * moov, gint * _video, gint * _audio,
263     gboolean * _has_h264)
264 {
265   GList *it;
266   gint video = 0, audio = 0;
267   gboolean has_h264 = FALSE;
268
269   for (it = moov->traks; it != NULL; it = g_list_next (it)) {
270     AtomTRAK *track = it->data;
271
272     if (track->is_video) {
273       video++;
274       if (track->is_h264)
275         has_h264 = TRUE;
276     } else
277       audio++;
278   }
279
280   if (_video)
281     *_video = video;
282   if (_audio)
283     *_audio = audio;
284   if (_has_h264)
285     *_has_h264 = has_h264;
286 }
287
288 /* pretty static, but possibly dynamic format info */
289
290 /* notes:
291  * - avc1 brand is not used, since the specific extensions indicated by it
292  *   are not used (e.g. sample groupings, etc)
293  * - TODO: maybe even more 3GPP brand fine-tuning ??
294  *   (but that might need ftyp rewriting at the end) */
295 void
296 gst_qt_mux_map_format_to_header (GstQTMuxFormat format, GstBuffer ** _prefix,
297     guint32 * _major, guint32 * _version, GList ** _compatible, AtomMOOV * moov,
298     GstClockTime longest_chunk, gboolean faststart)
299 {
300   static const guint32 qt_brands[] = { 0 };
301   static const guint32 mp4_brands[] =
302       { FOURCC_mp41, FOURCC_isom, FOURCC_iso2, 0 };
303   static const guint32 isml_brands[] = { FOURCC_iso2, 0 };
304   static const guint32 gpp_brands[] = { FOURCC_isom, FOURCC_iso2, 0 };
305   static const guint32 mjp2_brands[] = { FOURCC_isom, FOURCC_iso2, 0 };
306   static const guint8 mjp2_prefix[] =
307       { 0, 0, 0, 12, 'j', 'P', ' ', ' ', 0x0D, 0x0A, 0x87, 0x0A };
308   const guint32 *comp = NULL;
309   guint32 major = 0, version = 0;
310   GstBuffer *prefix = NULL;
311   GList *result = NULL;
312
313   g_return_if_fail (_prefix != NULL);
314   g_return_if_fail (_major != NULL);
315   g_return_if_fail (_version != NULL);
316   g_return_if_fail (_compatible != NULL);
317
318   switch (format) {
319     case GST_QT_MUX_FORMAT_QT:
320       major = FOURCC_qt__;
321       comp = qt_brands;
322       version = 0x20050300;
323       break;
324     case GST_QT_MUX_FORMAT_MP4:
325       major = FOURCC_mp42;
326       comp = mp4_brands;
327       break;
328     case GST_QT_MUX_FORMAT_ISML:
329       major = FOURCC_isml;
330       comp = isml_brands;
331       break;
332     case GST_QT_MUX_FORMAT_3GP:
333     {
334       gint video, audio;
335       gboolean has_h264;
336
337       gst_qt_mux_map_check_tracks (moov, &video, &audio, &has_h264);
338       /* only track restriction really matters for Basic Profile */
339       if (video <= 1 && audio <= 1) {
340         /* it seems only newer spec knows about H264 */
341         major = has_h264 ? FOURCC_3gp6 : FOURCC_3gp4;
342         version = has_h264 ? 0x100 : 0x200;
343       } else {
344         major = FOURCC_3gg6;
345         version = 0x100;
346       }
347       comp = gpp_brands;
348
349       /*
350        * We assume that we have chunks in dts order
351        */
352       if (faststart && longest_chunk <= GST_SECOND) {
353         /* add progressive download profile */
354         result = g_list_append (result, GUINT_TO_POINTER (FOURCC_3gr6));
355       }
356       break;
357     }
358     case GST_QT_MUX_FORMAT_MJ2:
359     {
360       major = FOURCC_mjp2;
361       comp = mjp2_brands;
362       version = 0;
363       prefix = gst_buffer_new_and_alloc (sizeof (mjp2_prefix));
364       gst_buffer_fill (prefix, 0, mjp2_prefix, sizeof (mjp2_prefix));
365       break;
366     }
367     default:
368       g_assert_not_reached ();
369       break;
370   }
371
372   /* convert list to list, hm */
373   while (comp && *comp != 0) {
374     /* order matters over efficiency */
375     result = g_list_append (result, GUINT_TO_POINTER (*comp));
376     comp++;
377   }
378
379   *_major = major;
380   *_version = version;
381   *_prefix = prefix;
382   *_compatible = result;
383
384   /* TODO 3GPP may include mp42 as compatible if applicable */
385   /* TODO 3GPP major brand 3gp7 if at most 1 video and audio track */
386 }