summaryrefslogtreecommitdiff
path: root/gst/isomp4/gstqtmuxmap.c
blob: e0fb78bf18d05c562b3431fad1dfba6ca6947a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* Quicktime muxer plugin for GStreamer
 * Copyright (C) 2008 Thiago Sousa Santos <thiagoss@embedded.ufcg.edu.br>
 * Copyright (C) 2008 Mark Nauwelaerts <mnauw@users.sf.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/*
 * Unless otherwise indicated, Source Code is licensed under MIT license.
 * See further explanation attached in License Statement (distributed in the file
 * LICENSE).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "gstqtmuxmap.h"
#include "fourcc.h"
#include "ftypcc.h"

/* static info related to various format */

#define COMMON_VIDEO_CAPS \
  "width = (int) [ 16, 4096 ], " \
  "height = (int) [ 16, 4096 ], " \
  "framerate = (fraction) [ 0, MAX ]"

#define COMMON_VIDEO_CAPS_NO_FRAMERATE \
  "width = (int) [ 16, 4096 ], " \
  "height = (int) [ 16, 4096 ] "

#define H263_CAPS \
  "video/x-h263, " \
  COMMON_VIDEO_CAPS

#define H264_CAPS \
  "video/x-h264, " \
  "stream-format = (string) avc, " \
  "alignment = (string) au, " \
  COMMON_VIDEO_CAPS

#define MPEG4V_CAPS \
  "video/mpeg, " \
  "mpegversion = (int) 4, "\
  "systemstream = (boolean) false, " \
  COMMON_VIDEO_CAPS "; " \
  "video/x-divx, " \
  "divxversion = (int) 5, "\
  COMMON_VIDEO_CAPS

#define SVQ_CAPS \
  "video/x-svq, " \
  "svqversion = (int) 3, " \
  COMMON_VIDEO_CAPS

#define COMMON_AUDIO_CAPS(c, r) \
  "channels = (int) [ 1, " G_STRINGIFY (c) " ], " \
  "rate = (int) [ 1, " G_STRINGIFY (r) " ]"

#define PCM_CAPS \
  "audio/x-raw-int, " \
  "width = (int) 8, " \
  "depth = (int) 8, " \
  COMMON_AUDIO_CAPS (2, MAX) ", " \
  "signed = (boolean) { true, false }; " \
  "audio/x-raw-int, " \
  "width = (int) 16, " \
  "depth = (int) 16, " \
  "endianness = (int) { BIG_ENDIAN, LITTLE_ENDIAN }, " \
  COMMON_AUDIO_CAPS (2, MAX) ", " \
  "signed = (boolean) true " \

#define PCM_CAPS_FULL \
  PCM_CAPS "; " \
  "audio/x-raw-int, " \
  "width = (int) 24, " \
  "depth = (int) 24, " \
  "endianness = (int) { BIG_ENDIAN, LITTLE_ENDIAN }, " \
  COMMON_AUDIO_CAPS (2, MAX) ", " \
  "signed = (boolean) true; " \
  "audio/x-raw-int, " \
  "width = (int) 32, " \
  "depth = (int) 32, " \
  "endianness = (int) { BIG_ENDIAN, LITTLE_ENDIAN }, " \
  COMMON_AUDIO_CAPS (2, MAX) ", " \
  "signed = (boolean) true "

#define MP3_CAPS \
  "audio/mpeg, " \
  "mpegversion = (int) 1, " \
  "layer = (int) 3, " \
  COMMON_AUDIO_CAPS (2, MAX)

#define AAC_CAPS \
  "audio/mpeg, " \
  "mpegversion = (int) 4, " \
  "stream-format = (string) raw, " \
  COMMON_AUDIO_CAPS (8, MAX)

#define AMR_CAPS \
  "audio/AMR, " \
  "rate = (int) 8000, " \
  "channels = [ 1, 2 ]; " \
  "audio/AMR-WB, " \
  "rate = (int) 16000, " \
  "channels = [ 1, 2 ] "

#define ADPCM_CAPS  \
  "audio/x-adpcm, " \
  "layout = (string)dvi, " \
  "block_align = (int)[64, 8096], " \
  COMMON_AUDIO_CAPS(2, MAX)

#define ALAC_CAPS \
  "audio/x-alac, " \
  COMMON_AUDIO_CAPS(2, MAX)

/* FIXME 0.11 - take a look at bugs #580005 and #340375 */
GstQTMuxFormatProp gst_qt_mux_format_list[] = {
  /* original QuickTime format; see Apple site (e.g. qtff.pdf) */
  {
        GST_QT_MUX_FORMAT_QT,
        GST_RANK_PRIMARY,
        "qtmux",
        "QuickTime",
        "GstQTMux",
        GST_STATIC_CAPS ("video/quicktime, variant = (string) apple; "
            "video/quicktime"),
        GST_STATIC_CAPS ("video/x-raw-rgb, "
            COMMON_VIDEO_CAPS "; "
            "video/x-raw-yuv, "
            "format = (fourcc) UYVY, "
            COMMON_VIDEO_CAPS "; "
            MPEG4V_CAPS "; "
            H263_CAPS "; "
            H264_CAPS "; "
            SVQ_CAPS "; "
            "video/x-dv, "
            "systemstream = (boolean) false, "
            COMMON_VIDEO_CAPS "; "
            "image/jpeg, "
            COMMON_VIDEO_CAPS_NO_FRAMERATE "; "
            "video/x-vp8, "
            COMMON_VIDEO_CAPS "; "
            "video/x-dirac, "
            COMMON_VIDEO_CAPS "; " "video/x-qt-part, " COMMON_VIDEO_CAPS),
        GST_STATIC_CAPS (PCM_CAPS_FULL "; "
            MP3_CAPS " ; "
            AAC_CAPS " ; "
            ADPCM_CAPS " ; "
            "audio/x-alaw, " COMMON_AUDIO_CAPS (2, MAX) "; "
            AMR_CAPS " ; " ALAC_CAPS)
      }
  ,
  /* ISO 14496-14: mp42 as ISO base media extension
   * (supersedes original ISO 144996-1 mp41) */
  {
        GST_QT_MUX_FORMAT_MP4,
        GST_RANK_PRIMARY,
        "mp4mux",
        "MP4",
        "GstMP4Mux",
        GST_STATIC_CAPS ("video/quicktime, variant = (string) iso"),
        GST_STATIC_CAPS (MPEG4V_CAPS "; " H264_CAPS ";"
            "video/x-mp4-part," COMMON_VIDEO_CAPS),
        GST_STATIC_CAPS (MP3_CAPS "; " AAC_CAPS " ; " ALAC_CAPS)
      }
  ,
  /* Microsoft Smooth Streaming fmp4/isml */
  /* TODO add WMV/WMA support */
  {
        GST_QT_MUX_FORMAT_ISML,
        GST_RANK_PRIMARY,
        "ismlmux",
        "ISML",
        "GstISMLMux",
        GST_STATIC_CAPS ("video/quicktime, variant = (string) iso-fragmented"),
        GST_STATIC_CAPS (MPEG4V_CAPS "; " H264_CAPS),
        GST_STATIC_CAPS (MP3_CAPS "; " AAC_CAPS)
      }
  ,
  /* 3GPP Technical Specification 26.244 V7.3.0
   * (extended in 3GPP2 File Formats for Multimedia Services) */
  {
        GST_QT_MUX_FORMAT_3GP,
        GST_RANK_PRIMARY,
        "3gppmux",
        "3GPP",
        "Gst3GPPMux",
        GST_STATIC_CAPS ("video/quicktime, variant = (string) 3gpp"),
        GST_STATIC_CAPS (H263_CAPS "; " MPEG4V_CAPS "; " H264_CAPS),
        GST_STATIC_CAPS (AMR_CAPS "; " MP3_CAPS "; " AAC_CAPS)
      }
  ,
#ifndef GST_REMOVE_DEPRECATED
  /* 3GPP Technical Specification 26.244 V7.3.0
   * (extended in 3GPP2 File Formats for Multimedia Services) */
  {
        GST_QT_MUX_FORMAT_3GP,
        GST_RANK_NONE,
        "gppmux",
        "3GPP",
        "GstGPPMux",
        GST_STATIC_CAPS ("video/quicktime, variant = (string) 3gpp"),
        GST_STATIC_CAPS (H263_CAPS "; " MPEG4V_CAPS "; " H264_CAPS),
        GST_STATIC_CAPS (AMR_CAPS "; " MP3_CAPS "; " AAC_CAPS)
      }
  ,
#endif
  /* ISO 15444-3: Motion-JPEG-2000 (also ISO base media extension) */
  {
        GST_QT_MUX_FORMAT_MJ2,
        GST_RANK_PRIMARY,
        "mj2mux",
        "MJ2",
        "GstMJ2Mux",
        GST_STATIC_CAPS ("video/mj2"),
        GST_STATIC_CAPS ("image/x-j2c, " COMMON_VIDEO_CAPS "; "
            "image/x-jpc, " COMMON_VIDEO_CAPS),
        GST_STATIC_CAPS (PCM_CAPS)
      }
  ,
  {
        GST_QT_MUX_FORMAT_NONE,
      }
};

/* pretty static, but may turn out needed a few times */
AtomsTreeFlavor
gst_qt_mux_map_format_to_flavor (GstQTMuxFormat format)
{
  if (format == GST_QT_MUX_FORMAT_QT)
    return ATOMS_TREE_FLAVOR_MOV;
  else if (format == GST_QT_MUX_FORMAT_3GP)
    return ATOMS_TREE_FLAVOR_3GP;
  else if (format == GST_QT_MUX_FORMAT_ISML)
    return ATOMS_TREE_FLAVOR_ISML;
  else
    return ATOMS_TREE_FLAVOR_ISOM;
}

static void
gst_qt_mux_map_check_tracks (AtomMOOV * moov, gint * _video, gint * _audio,
    gboolean * _has_h264)
{
  GList *it;
  gint video = 0, audio = 0;
  gboolean has_h264 = FALSE;

  for (it = moov->traks; it != NULL; it = g_list_next (it)) {
    AtomTRAK *track = it->data;

    if (track->is_video) {
      video++;
      if (track->is_h264)
        has_h264 = TRUE;
    } else
      audio++;
  }

  if (_video)
    *_video = video;
  if (_audio)
    *_audio = audio;
  if (_has_h264)
    *_has_h264 = has_h264;
}

/* pretty static, but possibly dynamic format info */

/* notes:
 * - avc1 brand is not used, since the specific extensions indicated by it
 *   are not used (e.g. sample groupings, etc)
 * - TODO: maybe even more 3GPP brand fine-tuning ??
 *   (but that might need ftyp rewriting at the end) */
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)
{
  static guint32 qt_brands[] = { 0 };
  static guint32 mp4_brands[] = { FOURCC_mp41, FOURCC_isom, FOURCC_iso2, 0 };
  static guint32 isml_brands[] = { FOURCC_iso2, 0 };
  static guint32 gpp_brands[] = { FOURCC_isom, FOURCC_iso2, 0 };
  static guint32 mjp2_brands[] = { FOURCC_isom, FOURCC_iso2, 0 };
  static guint8 mjp2_prefix[] =
      { 0, 0, 0, 12, 'j', 'P', ' ', ' ', 0x0D, 0x0A, 0x87, 0x0A };
  guint32 *comp = NULL;
  guint32 major = 0, version = 0;
  GstBuffer *prefix = NULL;
  GList *result = NULL;

  g_return_if_fail (_prefix != NULL);
  g_return_if_fail (_major != NULL);
  g_return_if_fail (_version != NULL);
  g_return_if_fail (_compatible != NULL);

  switch (format) {
    case GST_QT_MUX_FORMAT_QT:
      major = FOURCC_qt__;
      comp = qt_brands;
      version = 0x20050300;
      break;
    case GST_QT_MUX_FORMAT_MP4:
      major = FOURCC_mp42;
      comp = mp4_brands;
      break;
    case GST_QT_MUX_FORMAT_ISML:
      major = FOURCC_isml;
      comp = isml_brands;
      break;
    case GST_QT_MUX_FORMAT_3GP:
    {
      gint video, audio;
      gboolean has_h264;

      gst_qt_mux_map_check_tracks (moov, &video, &audio, &has_h264);
      /* only track restriction really matters for Basic Profile */
      if (video <= 1 && audio <= 1) {
        /* it seems only newer spec knows about H264 */
        major = has_h264 ? FOURCC_3gp6 : FOURCC_3gp4;
        version = has_h264 ? 0x100 : 0x200;
      } else {
        major = FOURCC_3gg6;
        version = 0x100;
      }
      comp = gpp_brands;

      /*
       * We assume that we have chunks in dts order
       */
      if (faststart && longest_chunk <= GST_SECOND) {
        /* add progressive download profile */
        result = g_list_append (result, GUINT_TO_POINTER (FOURCC_3gr6));
      }
      break;
    }
    case GST_QT_MUX_FORMAT_MJ2:
      major = FOURCC_mjp2;
      comp = mjp2_brands;
      version = 0;
      prefix = gst_buffer_new_and_alloc (sizeof (mjp2_prefix));
      memcpy (GST_BUFFER_DATA (prefix), mjp2_prefix, GST_BUFFER_SIZE (prefix));
      break;
    default:
      g_assert_not_reached ();
      break;
  }

  /* convert list to list, hm */
  while (comp && *comp != 0) {
    /* order matters over efficiency */
    result = g_list_append (result, GUINT_TO_POINTER (*comp));
    comp++;
  }

  *_major = major;
  *_version = version;
  *_prefix = prefix;
  *_compatible = result;

  /* TODO 3GPP may include mp42 as compatible if applicable */
  /* TODO 3GPP major brand 3gp7 if at most 1 video and audio track */
}