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