riff-media.c 51.3 KB
Newer Older
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
/* GStreamer RIFF I/O
 * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * riff-media.h: RIFF-id to/from caps routines
 *
 * 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "riff-ids.h"
#include "riff-media.h"

29
30
31
#include <gst/audio/multichannel.h>

#include <string.h>
32
#include <math.h>
33
34
35
36

GST_DEBUG_CATEGORY_EXTERN (riff_debug);
#define GST_CAT_DEFAULT riff_debug

37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
 * gst_riff_create_video_caps_with_data:
 * @codec_fcc: fourCC codec for this codec.
 * @strh: pointer to the strh stream header structure.
 * @strf: pointer to the strf stream header structure, including any
 *        data that is within the range of strf.size, but excluding any
 *        additional data withint this chunk but outside strf.size.
 * @strf_data: a #GstBuffer containing the additional data in the strf
 *             chunk outside reach of strf.size. Ususally a palette.
 * @strd_data: a #GstBuffer containing the data in the strd stream header
 *             chunk. Usually codec initialization data.
 * @codec_name: if given, will be filled with a human-readable codec name.
 */

51
GstCaps *
52
gst_riff_create_video_caps (guint32 codec_fcc,
53
54
    gst_riff_strh * strh, gst_riff_strf_vids * strf,
    GstBuffer * strf_data, GstBuffer * strd_data, char **codec_name)
55
56
{
  GstCaps *caps = NULL;
57
  GstBuffer *palette = NULL;
58

59
60
  GST_DEBUG ("video fourcc %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS (codec_fcc));

61
  switch (codec_fcc) {
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    case GST_MAKE_FOURCC ('D', 'I', 'B', ' '):{
      gint bpp = (strf && strf->bit_cnt != 0) ? strf->bit_cnt : 8;

      if (strf) {
        if (bpp == 8) {
          caps = gst_caps_new_simple ("video/x-raw-rgb",
              "bpp", G_TYPE_INT, 8, "depth", G_TYPE_INT, 8,
              "endianness", G_TYPE_INT, G_BYTE_ORDER, NULL);
        } else if (bpp == 24) {
          caps = gst_caps_new_simple ("video/x-raw-rgb",
              "bpp", G_TYPE_INT, 24, "depth", G_TYPE_INT, 24,
              "endianness", G_TYPE_INT, G_BIG_ENDIAN,
              "red_mask", G_TYPE_INT, 0xff, "green_mask", G_TYPE_INT, 0xff00,
              "blue_mask", G_TYPE_INT, 0xff0000, NULL);
        } else {
          GST_WARNING ("Unhandled DIB RGB depth: %d", bpp);
        }
      } else {
        /* for template */
        caps = gst_caps_from_string ("video/x-raw-rgb, bpp = (int) { 8, 24 }, "
            "depth = (int) { 8, 24}");
      }

85
86
87
      palette = strf_data;
      strf_data = NULL;
      if (codec_name)
88
        *codec_name = g_strdup_printf ("Palettized %d-bit RGB", bpp);
89
      break;
90
    }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
91
    case GST_MAKE_FOURCC ('I', '4', '2', '0'):
92
      caps = gst_caps_new_simple ("video/x-raw-yuv",
93
          "format", GST_TYPE_FOURCC, codec_fcc, NULL);
94
      if (codec_name)
95
        *codec_name = g_strdup ("Uncompressed planar YUV 4:2:0");
96
      break;
97

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
98
    case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
David Schleef's avatar
David Schleef committed
99
      caps = gst_caps_new_simple ("video/x-raw-yuv",
100
          "format", GST_TYPE_FOURCC, codec_fcc, NULL);
101
      if (codec_name)
102
        *codec_name = g_strdup ("Uncompressed packed YUV 4:2:2");
103
104
      break;

105
106
107
108
109
110
111
    case GST_MAKE_FOURCC ('Y', 'V', 'U', '9'):
      caps = gst_caps_new_simple ("video/x-raw-yuv",
          "format", GST_TYPE_FOURCC, codec_fcc, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Uncompressed packed YVU 4:1:0");
      break;

112
    case GST_MAKE_FOURCC ('M', 'J', 'P', 'G'): /* YUY2 MJPEG */
113
114
115
116
    case GST_MAKE_FOURCC ('A', 'V', 'R', 'n'):
    case GST_MAKE_FOURCC ('I', 'J', 'P', 'G'):
    case GST_MAKE_FOURCC ('i', 'j', 'p', 'g'):
    case GST_MAKE_FOURCC ('J', 'P', 'G', 'L'):
117
      caps = gst_caps_new_simple ("image/jpeg", NULL);
118
      if (codec_name)
119
        *codec_name = g_strdup ("Motion JPEG");
120
      break;
121

122
    case GST_MAKE_FOURCC ('J', 'P', 'E', 'G'): /* generic (mostly RGB) MJPEG */
123
      caps = gst_caps_new_simple ("image/jpeg", NULL);
124
      if (codec_name)
125
        *codec_name = g_strdup ("JPEG Still Image");
126
      break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
127

128
129
    case GST_MAKE_FOURCC ('P', 'I', 'X', 'L'): /* Miro/Pinnacle fourccs */
    case GST_MAKE_FOURCC ('V', 'I', 'X', 'L'): /* Miro/Pinnacle fourccs */
130
      caps = gst_caps_new_simple ("image/jpeg", NULL);
131
      if (codec_name)
132
        *codec_name = g_strdup ("Miro/Pinnacle Motion JPEG Video");
133
      break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
134

135
136
137
138
139
140
141
142
143
144
145
    case GST_MAKE_FOURCC ('S', 'P', '5', '3'):
    case GST_MAKE_FOURCC ('S', 'P', '5', '4'):
    case GST_MAKE_FOURCC ('S', 'P', '5', '5'):
    case GST_MAKE_FOURCC ('S', 'P', '5', '6'):
    case GST_MAKE_FOURCC ('S', 'P', '5', '7'):
    case GST_MAKE_FOURCC ('S', 'P', '5', '8'):
      caps = gst_caps_new_simple ("video/sp5x", NULL);
      if (codec_name)
        *codec_name = g_strdup ("Sp5x-like JPEG");
      break;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
146
147
    case GST_MAKE_FOURCC ('H', 'F', 'Y', 'U'):
      caps = gst_caps_new_simple ("video/x-huffyuv", NULL);
148
149
150
151
      if (strf) {
        gst_caps_set_simple (caps, "bpp",
            G_TYPE_INT, (int) strf->bit_cnt, NULL);
      }
152
      if (codec_name)
153
        *codec_name = g_strdup ("Huffman Lossless Codec");
154
      break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
155
156
157

    case GST_MAKE_FOURCC ('M', 'P', 'E', 'G'):
    case GST_MAKE_FOURCC ('M', 'P', 'G', 'I'):
158
159
    case GST_MAKE_FOURCC ('m', 'p', 'g', '1'):
    case GST_MAKE_FOURCC ('M', 'P', 'G', '1'):
160
    case GST_MAKE_FOURCC ('P', 'I', 'M', '1'):
David Schleef's avatar
David Schleef committed
161
      caps = gst_caps_new_simple ("video/mpeg",
162
          "systemstream", G_TYPE_BOOLEAN, FALSE,
163
          "mpegversion", G_TYPE_INT, 1, NULL);
164
      if (codec_name)
165
        *codec_name = g_strdup ("MPEG-1 video");
166
      break;
167
168
169
170
171
172
    case GST_MAKE_FOURCC ('M', 'P', 'G', '2'):
    case GST_MAKE_FOURCC ('m', 'p', 'g', '2'):
      caps = gst_caps_new_simple ("video/mpeg",
          "systemstream", G_TYPE_BOOLEAN, FALSE,
          "mpegversion", G_TYPE_INT, 2, NULL);
      if (codec_name)
173
        *codec_name = g_strdup ("MPEG-2 video");
174
      break;
175

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
176
    case GST_MAKE_FOURCC ('H', '2', '6', '3'):
177
    case GST_MAKE_FOURCC ('h', '2', '6', '3'):
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
178
    case GST_MAKE_FOURCC ('i', '2', '6', '3'):
179
    case GST_MAKE_FOURCC ('U', '2', '6', '3'):
180
181
      caps = gst_caps_new_simple ("video/x-h263",
          "variant", G_TYPE_STRING, "itu", NULL);
182
      if (codec_name)
183
        *codec_name = g_strdup ("ITU H.26n");
184
      break;
185

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
186
    case GST_MAKE_FOURCC ('L', '2', '6', '3'):
187
188
189
      /* http://www.leadcodecs.com/Codecs/LEAD-H263.htm */
      caps = gst_caps_new_simple ("video/x-h263",
          "variant", G_TYPE_STRING, "lead", NULL);
190
      if (codec_name)
191
        *codec_name = g_strdup ("Lead H.263");
192
      break;
193

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
194
    case GST_MAKE_FOURCC ('M', '2', '6', '3'):
195
    case GST_MAKE_FOURCC ('m', '2', '6', '3'):
196
197
      caps = gst_caps_new_simple ("video/x-h263",
          "variant", G_TYPE_STRING, "microsoft", NULL);
198
      if (codec_name)
199
        *codec_name = g_strdup ("Microsoft H.263");
200
      break;
201

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
202
    case GST_MAKE_FOURCC ('V', 'D', 'O', 'W'):
203
204
      caps = gst_caps_new_simple ("video/x-h263",
          "variant", G_TYPE_STRING, "vdolive", NULL);
205
      if (codec_name)
206
        *codec_name = g_strdup ("VDOLive");
207
      break;
208

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
209
    case GST_MAKE_FOURCC ('V', 'I', 'V', 'O'):
210
211
      caps = gst_caps_new_simple ("video/x-h263",
          "variant", G_TYPE_STRING, "vivo", NULL);
212
      if (codec_name)
213
        *codec_name = g_strdup ("Vivo H.263");
214
      break;
215

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
216
    case GST_MAKE_FOURCC ('x', '2', '6', '3'):
217
218
      caps = gst_caps_new_simple ("video/x-h263",
          "variant", G_TYPE_STRING, "xirlink", NULL);
219
      if (codec_name)
220
        *codec_name = g_strdup ("Xirlink H.263");
221
222
      break;

223
224
      /* apparently not standard H.263...? */
    case GST_MAKE_FOURCC ('I', '2', '6', '3'):
225
226
      caps = gst_caps_new_simple ("video/x-intel-h263",
          "variant", G_TYPE_STRING, "intel", NULL);
227
228
229
230
      if (codec_name)
        *codec_name = g_strdup ("Intel H.263");
      break;

231
    case GST_MAKE_FOURCC ('H', '2', '6', '4'):
232
    case GST_MAKE_FOURCC ('h', '2', '6', '4'):
233
234
      caps = gst_caps_new_simple ("video/x-h264",
          "variant", G_TYPE_STRING, "itu", NULL);
235
236
237
238
239
      if (codec_name)
        *codec_name = g_strdup ("ITU H.264");
      break;

    case GST_MAKE_FOURCC ('V', 'S', 'S', 'H'):
240
241
      caps = gst_caps_new_simple ("video/x-h264",
          "variant", G_TYPE_STRING, "videosoft", NULL);
242
243
244
245
      if (codec_name)
        *codec_name = g_strdup ("VideoSoft H.264");
      break;

246
247
248
249
250
251
252
    case GST_MAKE_FOURCC ('S', 'E', 'D', 'G'):
      caps = gst_caps_new_simple ("video/mpeg",
          "mpegversion", G_TYPE_INT, 4, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Samsung MPEG-4");
      break;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
253
    case GST_MAKE_FOURCC ('D', 'I', 'V', '3'):
254
    case GST_MAKE_FOURCC ('d', 'i', 'v', '3'):
255
    case GST_MAKE_FOURCC ('D', 'I', 'V', '4'):
256
    case GST_MAKE_FOURCC ('d', 'i', 'v', '4'):
257
    case GST_MAKE_FOURCC ('D', 'I', 'V', '5'):
258
    case GST_MAKE_FOURCC ('d', 'i', 'v', '5'):
259
    case GST_MAKE_FOURCC ('D', 'I', 'V', '6'):
260
    case GST_MAKE_FOURCC ('d', 'i', 'v', '6'):
261
262
263
264
265
266
267
    case GST_MAKE_FOURCC ('M', 'P', 'G', '3'):
    case GST_MAKE_FOURCC ('m', 'p', 'g', '3'):
    case GST_MAKE_FOURCC ('c', 'o', 'l', '0'):
    case GST_MAKE_FOURCC ('C', 'O', 'L', '0'):
    case GST_MAKE_FOURCC ('c', 'o', 'l', '1'):
    case GST_MAKE_FOURCC ('C', 'O', 'L', '1'):
    case GST_MAKE_FOURCC ('A', 'P', '4', '1'):
David Schleef's avatar
David Schleef committed
268
      caps = gst_caps_new_simple ("video/x-divx",
269
          "divxversion", G_TYPE_INT, 3, NULL);
270
      if (codec_name)
271
        *codec_name = g_strdup ("DivX MS-MPEG-4 Version 3");
272
      break;
273

274
275
    case GST_MAKE_FOURCC ('d', 'i', 'v', 'x'):
    case GST_MAKE_FOURCC ('D', 'I', 'V', 'X'):
276
      caps = gst_caps_new_simple ("video/x-divx",
277
          "divxversion", G_TYPE_INT, 4, NULL);
278
      if (codec_name)
279
        *codec_name = g_strdup ("DivX MPEG-4 Version 4");
280
      break;
281

282
283
284
285
286
287
288
    case GST_MAKE_FOURCC ('B', 'L', 'Z', '0'):
      caps = gst_caps_new_simple ("video/x-divx",
          "divxversion", G_TYPE_INT, 4, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Blizzard DivX");
      break;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
289
    case GST_MAKE_FOURCC ('D', 'X', '5', '0'):
David Schleef's avatar
David Schleef committed
290
      caps = gst_caps_new_simple ("video/x-divx",
291
          "divxversion", G_TYPE_INT, 5, NULL);
292
      if (codec_name)
293
        *codec_name = g_strdup ("DivX MPEG-4 Version 5");
294
295
      break;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
296
297
    case GST_MAKE_FOURCC ('X', 'V', 'I', 'D'):
    case GST_MAKE_FOURCC ('x', 'v', 'i', 'd'):
David Schleef's avatar
David Schleef committed
298
      caps = gst_caps_new_simple ("video/x-xvid", NULL);
299
      if (codec_name)
300
        *codec_name = g_strdup ("XVID MPEG-4");
301
302
      break;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
303
    case GST_MAKE_FOURCC ('M', 'P', 'G', '4'):
304
    case GST_MAKE_FOURCC ('M', 'P', '4', 'S'):
David Schleef's avatar
David Schleef committed
305
      caps = gst_caps_new_simple ("video/x-msmpeg",
306
          "msmpegversion", G_TYPE_INT, 41, NULL);
307
      if (codec_name)
308
        *codec_name = g_strdup ("Microsoft MPEG-4 4.1");
309
310
      break;

311
    case GST_MAKE_FOURCC ('m', 'p', '4', '2'):
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
312
    case GST_MAKE_FOURCC ('M', 'P', '4', '2'):
David Schleef's avatar
David Schleef committed
313
      caps = gst_caps_new_simple ("video/x-msmpeg",
314
          "msmpegversion", G_TYPE_INT, 42, NULL);
315
      if (codec_name)
316
        *codec_name = g_strdup ("Microsoft MPEG-4 4.2");
317
318
      break;

319
    case GST_MAKE_FOURCC ('m', 'p', '4', '3'):
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
320
    case GST_MAKE_FOURCC ('M', 'P', '4', '3'):
David Schleef's avatar
David Schleef committed
321
      caps = gst_caps_new_simple ("video/x-msmpeg",
322
          "msmpegversion", G_TYPE_INT, 43, NULL);
323
      if (codec_name)
324
        *codec_name = g_strdup ("Microsoft MPEG-4 4.3");
325
326
      break;

327
328
329
330
331
332
333
334
    case GST_MAKE_FOURCC ('M', '4', 'S', '2'):
      caps = gst_caps_new_simple ("video/mpeg",
          "mpegversion", G_TYPE_INT, 4, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Microsoft ISO MPEG-4 1.1");
      break;

    case GST_MAKE_FOURCC ('F', 'M', 'P', '4'):
335
    case GST_MAKE_FOURCC ('U', 'M', 'P', '4'):
336
337
338
339
340
341
      caps = gst_caps_new_simple ("video/mpeg",
          "mpegversion", G_TYPE_INT, 4, NULL);
      if (codec_name)
        *codec_name = g_strdup ("FFmpeg MPEG-4");
      break;

342
343
344
345
346
347
    case GST_MAKE_FOURCC ('3', 'i', 'v', 'd'):
    case GST_MAKE_FOURCC ('3', 'I', 'V', 'D'):
      if (codec_name)
        *codec_name = g_strdup ("Microsoft MPEG-4 4.3");        /* FIXME? */
      return gst_caps_from_string ("video/x-msmpeg, msmpegversion = (int) 43");

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
348
349
350
    case GST_MAKE_FOURCC ('3', 'I', 'V', '1'):
    case GST_MAKE_FOURCC ('3', 'I', 'V', '2'):
      caps = gst_caps_new_simple ("video/x-3ivx", NULL);
351
      if (codec_name)
352
        *codec_name = g_strdup ("3ivx");
353
354
      break;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
355
356
    case GST_MAKE_FOURCC ('D', 'V', 'S', 'D'):
    case GST_MAKE_FOURCC ('d', 'v', 's', 'd'):
357
    case GST_MAKE_FOURCC ('C', 'D', 'V', 'C'):
David Schleef's avatar
David Schleef committed
358
      caps = gst_caps_new_simple ("video/x-dv",
359
          "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
360
      if (codec_name)
361
        *codec_name = g_strdup ("Generic DV");
362
363
      break;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
364
    case GST_MAKE_FOURCC ('W', 'M', 'V', '1'):
David Schleef's avatar
David Schleef committed
365
      caps = gst_caps_new_simple ("video/x-wmv",
366
          "wmvversion", G_TYPE_INT, 1, NULL);
367
      if (codec_name)
368
        *codec_name = g_strdup ("Microsoft Windows Media 7");
369
370
      break;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
371
    case GST_MAKE_FOURCC ('W', 'M', 'V', '2'):
David Schleef's avatar
David Schleef committed
372
      caps = gst_caps_new_simple ("video/x-wmv",
373
          "wmvversion", G_TYPE_INT, 2, NULL);
374
      if (codec_name)
375
        *codec_name = g_strdup ("Microsoft Windows Media 8");
376
      break;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
377
378

    case GST_MAKE_FOURCC ('W', 'M', 'V', '3'):
379
      caps = gst_caps_new_simple ("video/x-wmv",
380
          "wmvversion", G_TYPE_INT, 3, NULL);
381
      if (codec_name)
382
        *codec_name = g_strdup ("Microsoft Windows Media 9");
383
384
      break;

385
386
387
388
389
390
391
392
    case GST_MAKE_FOURCC ('W', 'M', 'V', 'A'):
      caps = gst_caps_new_simple ("video/x-wmv",
          "wmvversion", G_TYPE_INT, 3, "fourcc", GST_TYPE_FOURCC,
          codec_fcc, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Microsoft Windows Media Advanced Profile");
      break;

393
394
395
396
397
398
399
400
    case GST_MAKE_FOURCC ('W', 'V', 'C', '1'):
      caps = gst_caps_new_simple ("video/x-wmv",
          "wmvversion", G_TYPE_INT, 3, "fourcc", GST_TYPE_FOURCC,
          codec_fcc, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Microsoft Windows Media VC-1");
      break;

401
402
403
404
405
406
    case GST_MAKE_FOURCC ('c', 'v', 'i', 'd'):
      caps = gst_caps_new_simple ("video/x-cinepak", NULL);
      if (codec_name)
        *codec_name = g_strdup ("Cinepak video");
      break;

407
408
409
410
411
412
413
414
415
416
    case GST_MAKE_FOURCC ('M', 'S', 'V', 'C'):
    case GST_MAKE_FOURCC ('m', 's', 'v', 'c'):
    case GST_MAKE_FOURCC ('C', 'R', 'A', 'M'):
    case GST_MAKE_FOURCC ('c', 'r', 'a', 'm'):
    case GST_MAKE_FOURCC ('W', 'H', 'A', 'M'):
    case GST_MAKE_FOURCC ('w', 'h', 'a', 'm'):
      caps = gst_caps_new_simple ("video/x-msvideocodec",
          "msvideoversion", G_TYPE_INT, 1, NULL);
      if (codec_name)
        *codec_name = g_strdup ("MS video v1");
417
418
      palette = strf_data;
      strf_data = NULL;
419
420
      break;

421
422
423
424
425
    case GST_MAKE_FOURCC ('R', 'L', 'E', ' '):
    case GST_MAKE_FOURCC ('m', 'r', 'l', 'e'):
    case GST_MAKE_FOURCC (0x1, 0x0, 0x0, 0x0): /* why, why, why? */
      caps = gst_caps_new_simple ("video/x-rle",
          "layout", G_TYPE_STRING, "microsoft", NULL);
426
427
      palette = strf_data;
      strf_data = NULL;
428
429
430
431
432
433
434
      if (strf) {
        gst_caps_set_simple (caps,
            "depth", G_TYPE_INT, (gint) strf->bit_cnt, NULL);
      } else {
        gst_caps_set_simple (caps, "depth", GST_TYPE_INT_RANGE, 1, 64, NULL);
      }
      if (codec_name)
435
436
437
438
439
440
441
442
        *codec_name = g_strdup ("Microsoft RLE");
      break;

    case GST_MAKE_FOURCC ('X', 'x', 'a', 'n'):
      caps = gst_caps_new_simple ("video/x-xan",
          "wcversion", G_TYPE_INT, 4, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Xan Wing Commander 4");
443
444
      break;

445
446
447
448
449
450
451
452
    case GST_MAKE_FOURCC ('R', 'T', '2', '1'):
      caps = gst_caps_new_simple ("video/x-indeo",
          "indeoversion", G_TYPE_INT, 2, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Intel Video 2");
      break;

    case GST_MAKE_FOURCC ('I', 'V', '3', '1'):
453
    case GST_MAKE_FOURCC ('I', 'V', '3', '2'):
454
455
    case GST_MAKE_FOURCC ('i', 'v', '3', '1'):
    case GST_MAKE_FOURCC ('i', 'v', '3', '2'):
456
457
458
459
460
461
      caps = gst_caps_new_simple ("video/x-indeo",
          "indeoversion", G_TYPE_INT, 3, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Intel Video 3");
      break;

462
463
464
465
466
467
468
469
    case GST_MAKE_FOURCC ('I', 'V', '4', '1'):
    case GST_MAKE_FOURCC ('i', 'v', '4', '1'):
      caps = gst_caps_new_simple ("video/x-indeo",
          "indeoversion", G_TYPE_INT, 4, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Intel Video 4");
      break;

470
    case GST_MAKE_FOURCC ('I', 'V', '5', '0'):
471
472
      caps = gst_caps_new_simple ("video/x-indeo",
          "indeoversion", G_TYPE_INT, 5, NULL);
473
474
475
476
      if (codec_name)
        *codec_name = g_strdup ("Intel Video 5");
      break;

477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
    case GST_MAKE_FOURCC ('M', 'S', 'Z', 'H'):
      caps = gst_caps_new_simple ("video/x-mszh", NULL);
      if (codec_name)
        *codec_name = g_strdup ("Lossless MSZH Video");
      break;

    case GST_MAKE_FOURCC ('Z', 'L', 'I', 'B'):
      caps = gst_caps_new_simple ("video/x-zlib", NULL);
      if (codec_name)
        *codec_name = g_strdup ("Lossless zlib video");
      break;

    case GST_MAKE_FOURCC ('C', 'L', 'J', 'R'):
      caps = gst_caps_new_simple ("video/x-cirrus-logic-accupak", NULL);
      if (codec_name)
        *codec_name = g_strdup ("Cirrus Logipak AccuPak");
      break;

    case GST_MAKE_FOURCC ('C', 'Y', 'U', 'V'):
    case GST_MAKE_FOURCC ('c', 'y', 'u', 'v'):
      caps = gst_caps_new_simple ("video/x-compressed-yuv", NULL);
      if (codec_name)
        *codec_name = g_strdup ("CYUV Lossless");
      break;

    case GST_MAKE_FOURCC ('D', 'U', 'C', 'K'):
      caps = gst_caps_new_simple ("video/x-truemotion",
          "trueversion", G_TYPE_INT, 1, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Duck Truemotion1");
      break;

    case GST_MAKE_FOURCC ('T', 'M', '2', '0'):
      caps = gst_caps_new_simple ("video/x-truemotion",
          "trueversion", G_TYPE_INT, 2, NULL);
      if (codec_name)
        *codec_name = g_strdup ("TrueMotion 2.0");
      break;

    case GST_MAKE_FOURCC ('V', 'P', '3', '0'):
    case GST_MAKE_FOURCC ('v', 'p', '3', '0'):
    case GST_MAKE_FOURCC ('V', 'P', '3', '1'):
    case GST_MAKE_FOURCC ('v', 'p', '3', '1'):
    case GST_MAKE_FOURCC ('V', 'P', '3', ' '):
      caps = gst_caps_new_simple ("video/x-vp3", NULL);
      if (codec_name)
        *codec_name = g_strdup ("VP3");
      break;

    case GST_MAKE_FOURCC ('U', 'L', 'T', 'I'):
      caps = gst_caps_new_simple ("video/x-ultimotion", NULL);
      if (codec_name)
        *codec_name = g_strdup ("IBM UltiMotion");
      break;

    case GST_MAKE_FOURCC ('T', 'S', 'C', 'C'):
533
534
    case GST_MAKE_FOURCC ('t', 's', 'c', 'c'):{
      if (strf) {
535
        gint depth = (strf->bit_cnt != 0) ? (gint) strf->bit_cnt : 24;
536

537
538
        caps = gst_caps_new_simple ("video/x-camtasia", "depth", G_TYPE_INT,
            depth, NULL);
539
540
541
542
      } else {
        /* template caps */
        caps = gst_caps_new_simple ("video/x-camtasia", NULL);
      }
543
544
545
      if (codec_name)
        *codec_name = g_strdup ("TechSmith Camtasia");
      break;
546
    }
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583

    case GST_MAKE_FOURCC ('V', 'C', 'R', '1'):
      caps = gst_caps_new_simple ("video/x-ati-vcr",
          "vcrversion", G_TYPE_INT, 1, NULL);
      if (codec_name)
        *codec_name = g_strdup ("ATI VCR 1");
      break;

    case GST_MAKE_FOURCC ('V', 'C', 'R', '2'):
      caps = gst_caps_new_simple ("video/x-ati-vcr",
          "vcrversion", G_TYPE_INT, 2, NULL);
      if (codec_name)
        *codec_name = g_strdup ("ATI VCR 2");
      break;

    case GST_MAKE_FOURCC ('A', 'S', 'V', '1'):
      caps = gst_caps_new_simple ("video/x-asus",
          "asusversion", G_TYPE_INT, 1, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Asus Video 1");
      break;

    case GST_MAKE_FOURCC ('A', 'S', 'V', '2'):
      caps = gst_caps_new_simple ("video/x-asus",
          "asusversion", G_TYPE_INT, 2, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Asus Video 2");
      break;

    case GST_MAKE_FOURCC ('M', 'P', 'N', 'G'):
    case GST_MAKE_FOURCC ('m', 'p', 'n', 'g'):
    case GST_MAKE_FOURCC ('P', 'N', 'G', ' '):
      caps = gst_caps_new_simple ("image/png", NULL);
      if (codec_name)
        *codec_name = g_strdup ("PNG image");
      break;

584
585
586
587
588
589
590
    case GST_MAKE_FOURCC ('F', 'L', 'V', '1'):
      caps = gst_caps_new_simple ("video/x-flash-video",
          "flvversion", G_TYPE_INT, 1, NULL);
      if (codec_name)
        *codec_name = g_strdup ("Flash Video 1");
      break;

591
592
593
594
595
596
597
    case GST_MAKE_FOURCC ('V', 'M', 'n', 'c'):
      caps = gst_caps_new_simple ("video/x-vmnc",
          "version", G_TYPE_INT, 1, NULL);
      if (codec_name)
        *codec_name = g_strdup ("VMWare NC Video");
      break;

598
599
600
601
602
603
    case GST_MAKE_FOURCC ('d', 'r', 'a', 'c'):
      caps = gst_caps_new_simple ("video/x-dirac", NULL);
      if (codec_name)
        *codec_name = g_strdup ("Dirac");
      break;

604
    default:
605
      GST_WARNING ("Unknown video fourcc %" GST_FOURCC_FORMAT,
606
          GST_FOURCC_ARGS (codec_fcc));
David Schleef's avatar
David Schleef committed
607
      return NULL;
608
609
  }

David Schleef's avatar
David Schleef committed
610
  if (strh != NULL) {
611
612
    gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION,
        strh->rate, strh->scale, NULL);
David Schleef's avatar
David Schleef committed
613
614
  } else {
    gst_caps_set_simple (caps,
615
        "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
David Schleef's avatar
David Schleef committed
616
617
618
619
  }

  if (strf != NULL) {
    gst_caps_set_simple (caps,
620
621
        "width", G_TYPE_INT, strf->width,
        "height", G_TYPE_INT, strf->height, NULL);
David Schleef's avatar
David Schleef committed
622
623
  } else {
    gst_caps_set_simple (caps,
624
625
        "width", GST_TYPE_INT_RANGE, 16, 4096,
        "height", GST_TYPE_INT_RANGE, 16, 4096, NULL);
626
627
  }

628
629
630
631
632
633
  /* extradata */
  if (strf_data || strd_data) {
    gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER,
        strf_data ? strf_data : strd_data, NULL);
  }

634
  /* palette */
635
636
637
638
639
640
641
642
643
  if (palette) {
    GstBuffer *copy;
    guint num_colors;

    if (strf != NULL)
      num_colors = strf->num_colors;
    else
      num_colors = 256;

644
645
    if (GST_BUFFER_SIZE (palette) >= (num_colors * 4)) {
      /* palette is always at least 256*4 bytes */
646
647
      copy =
          gst_buffer_new_and_alloc (MAX (GST_BUFFER_SIZE (palette), 256 * 4));
648
649
      memcpy (GST_BUFFER_DATA (copy), GST_BUFFER_DATA (palette),
          GST_BUFFER_SIZE (palette));
650
651

#if (G_BYTE_ORDER == G_BIG_ENDIAN)
652
653
654
655
656
657
658
659
660
661
      {
        guint8 *data = GST_BUFFER_DATA (copy);
        gint n;

        /* own endianness */
        for (n = 0; n < num_colors; n++) {
          GST_WRITE_UINT32_BE (data, GST_READ_UINT32_LE (data));
          data += sizeof (guint32);
        }
      }
662
#endif
663
664
665
666
667
      gst_caps_set_simple (caps, "palette_data", GST_TYPE_BUFFER, copy, NULL);
      gst_buffer_unref (copy);
    } else {
      GST_WARNING ("Palette smaller than expected: broken file");
    }
668
669
  }

670
671
672
  return caps;
}

673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
static const struct
{
  const guint32 ms_mask;
  const GstAudioChannelPosition gst_pos;
} layout_mapping[] = {
  {
  0x00001, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT}, {
  0x00002, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
  0x00004, GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}, {
  0x00008, GST_AUDIO_CHANNEL_POSITION_LFE}, {
  0x00010, GST_AUDIO_CHANNEL_POSITION_REAR_LEFT}, {
  0x00020, GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
  0x00040, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER}, {
  0x00080, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, {
  0x00100, GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}, {
  0x00200, GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT}, {
  0x00400, GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT}, {
  0x00800, GST_AUDIO_CHANNEL_POSITION_INVALID}, /* TOP_CENTER       */
  {
  0x01000, GST_AUDIO_CHANNEL_POSITION_INVALID}, /* TOP_FRONT_LEFT   */
  {
  0x02000, GST_AUDIO_CHANNEL_POSITION_INVALID}, /* TOP_FRONT_CENTER */
  {
  0x04000, GST_AUDIO_CHANNEL_POSITION_INVALID}, /* TOP_FRONT_RIGHT  */
  {
  0x08000, GST_AUDIO_CHANNEL_POSITION_INVALID}, /* TOP_BACK_LEFT    */
  {
  0x10000, GST_AUDIO_CHANNEL_POSITION_INVALID}, /* TOP_BACK_CENTER  */
  {
  0x20000, GST_AUDIO_CHANNEL_POSITION_INVALID}  /* TOP_BACK_RIGHT   */
};

#define MAX_CHANNEL_POSITIONS G_N_ELEMENTS (layout_mapping)

static gboolean
gst_riff_wavext_add_channel_layout (GstCaps * caps, guint32 layout)
{
  GstAudioChannelPosition pos[MAX_CHANNEL_POSITIONS];
  GstStructure *s;
  gint num_channels, i, p;

  s = gst_caps_get_structure (caps, 0);
  if (!gst_structure_get_int (s, "channels", &num_channels))
    g_return_val_if_reached (FALSE);

718
719
720
721
  /* In theory this should be done for 1 and 2 channels too but
   * apparently breaks too many things currently.
   */
  if (num_channels <= 2 || num_channels > MAX_CHANNEL_POSITIONS) {
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
    GST_DEBUG ("invalid number of channels: %d", num_channels);
    return FALSE;
  }

  p = 0;
  for (i = 0; i < MAX_CHANNEL_POSITIONS; ++i) {
    if ((layout & layout_mapping[i].ms_mask) != 0) {
      if (p >= num_channels) {
        GST_WARNING ("More bits set in the channel layout map than there "
            "are channels! Broken file");
        return FALSE;
      }
      if (layout_mapping[i].gst_pos == GST_AUDIO_CHANNEL_POSITION_INVALID) {
        GST_WARNING ("Unsupported channel position (mask 0x%08x) in channel "
            "layout map - ignoring those channels", layout_mapping[i].ms_mask);
        /* what to do? just ignore it and let downstream deal with a channel
         * layout that has INVALID positions in it for now ... */
      }
      pos[p] = layout_mapping[i].gst_pos;
      ++p;
    }
  }

  if (p != num_channels) {
    GST_WARNING ("Only %d bits set in the channel layout map, but there are "
        "supposed to be %d channels! Broken file", p, num_channels);
    return FALSE;
  }

  gst_audio_set_channel_positions (s, pos);
  return TRUE;
}

755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
static gboolean
gst_riff_wave_add_default_channel_layout (GstCaps * caps)
{
  GstAudioChannelPosition pos[8] = { GST_AUDIO_CHANNEL_POSITION_NONE, };
  GstStructure *s;
  gint nchannels;

  s = gst_caps_get_structure (caps, 0);

  if (!gst_structure_get_int (s, "channels", &nchannels))
    g_return_val_if_reached (FALSE);

  if (nchannels > 8) {
    GST_DEBUG ("invalid number of channels: %d", nchannels);
    return FALSE;
  }

  /* This uses the default channel mapping from ALSA which
   * is used in quite a few surround test files and seems to be
   * the defacto standard. The channel mapping from
   * WAVE_FORMAT_EXTENSIBLE doesn't seem to be used in normal
   * wav files like chan-id.wav.
   * http://bugzilla.gnome.org/show_bug.cgi?id=489010
   */
  switch (nchannels) {
    case 1:
      pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_MONO;
      break;
    case 8:
      pos[7] = GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT;
      pos[6] = GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT;
      /* fall through */
    case 6:
      pos[5] = GST_AUDIO_CHANNEL_POSITION_LFE;
      /* fall through */
    case 5:
      pos[4] = GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
      /* fall through */
    case 4:
      pos[3] = GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
      pos[2] = GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
      /* fall through */
    case 2:
      pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
      pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
      break;
    default:
      return FALSE;
  }

  gst_audio_set_channel_positions (s, pos);
  return TRUE;
}

static guint32
gst_riff_wavext_get_default_channel_mask (guint nchannels)
{
  guint32 channel_mask = 0;

  /* Set the default channel mask for the given number of channels.
   * http://www.microsoft.com/whdc/device/audio/multichaud.mspx
   */
  switch (nchannels) {
    case 11:
      channel_mask |= 0x00400;
      channel_mask |= 0x00200;
    case 9:
      channel_mask |= 0x00100;
    case 8:
      channel_mask |= 0x00080;
      channel_mask |= 0x00040;
    case 6:
      channel_mask |= 0x00020;
      channel_mask |= 0x00010;
    case 4:
      channel_mask |= 0x00008;
    case 3:
      channel_mask |= 0x00004;
    case 2:
      channel_mask |= 0x00002;
      channel_mask |= 0x00001;
      break;
  }

  return channel_mask;
}

842
GstCaps *
843
gst_riff_create_audio_caps (guint16 codec_id,
844
845
    gst_riff_strh * strh, gst_riff_strf_auds * strf,
    GstBuffer * strf_data, GstBuffer * strd_data, char **codec_name)
846
{
847
  gboolean block_align = FALSE, rate_chan = TRUE;
848
  GstCaps *caps = NULL;
849
  gint rate_min = 1000, rate_max = 96000;
850
  gint channels_max = 2;
851
852

  switch (codec_id) {
853
    case GST_RIFF_WAVE_FORMAT_PCM:     /* PCM */
854
855
      channels_max = 8;

856
      if (strf != NULL) {
857
858
        gint ba = strf->blockalign;
        gint ch = strf->channels;
859
        gint wd = ba * 8 / ch;
860
861
862
863
864
865
866
        gint ws;

        if (strf->size > 32) {
          GST_WARNING ("invalid depth (%d) of pcm audio, overwriting.",
              strf->size);
          strf->size = 8 * (guint) ceil (wd / 8.0);
        }
867
868
869
870
871
        /* in riff, the depth is stored in the size field but it just means that
         * the _least_ significant bits are cleared. We can therefore just play
         * the sample as if it had a depth == width */
        /* For reference, the actual depth is in strf->size */
        ws = wd;
872

873
874
875
        caps = gst_caps_new_simple ("audio/x-raw-int",
            "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
            "channels", G_TYPE_INT, ch,
876
877
            "width", G_TYPE_INT, wd,
            "depth", G_TYPE_INT, ws, "signed", G_TYPE_BOOLEAN, wd != 8, NULL);
878

879
880
881
882
        /* Add default channel layout. In theory this should be done
         * for 1 and 2 channels too but apparently breaks too many
         * things currently. Also we know no default layout for more than
         * 8 channels. */
883
        if (ch > 2) {
884
885
886
          if (ch > 8)
            GST_WARNING ("don't know default layout for %d channels", ch);
          else if (gst_riff_wave_add_default_channel_layout (caps))
887
            GST_DEBUG ("using default channel layout for %d channels", ch);
888
889
          else
            GST_WARNING ("failed to add channel layout");
890
        }
891
      } else {
892
        /* FIXME: this is pretty useless - we need fixed caps */
893
894
895
        caps = gst_caps_from_string ("audio/x-raw-int, "
            "endianness = (int) LITTLE_ENDIAN, "
            "signed = (boolean) { true, false }, "
896
            "width = (int) { 8, 16, 24, 32 }, " "depth = (int) [ 1, 32 ]");
897
      }
898
899
900
      if (codec_name && strf)
        *codec_name = g_strdup_printf ("Uncompressed %d-bit PCM audio",
            strf->size);
901
902
      break;

903
904
905
906
907
    case GST_RIFF_WAVE_FORMAT_ADPCM:
      caps = gst_caps_new_simple ("audio/x-adpcm",
          "layout", G_TYPE_STRING, "microsoft", NULL);
      if (codec_name)
        *codec_name = g_strdup ("ADPCM audio");
908
909
910
      block_align = TRUE;
      break;

911
    case GST_RIFF_WAVE_FORMAT_IEEE_FLOAT:
912
913
      channels_max = 8;

914
915
916
917
918
919
920
921
922
      if (strf != NULL) {
        gint ba = strf->blockalign;
        gint ch = strf->channels;
        gint wd = ba * 8 / ch;

        caps = gst_caps_new_simple ("audio/x-raw-float",
            "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
            "channels", G_TYPE_INT, ch, "width", G_TYPE_INT, wd, NULL);

923
924
925
926
        /* Add default channel layout. In theory this should be done
         * for 1 and 2 channels too but apparently breaks too many
         * things currently. Also we know no default layout for more than
         * 8 channels. */
927
        if (ch > 2) {
928
929
930
          if (ch > 8)
            GST_WARNING ("don't know default layout for %d channels", ch);
          else if (gst_riff_wave_add_default_channel_layout (caps))
931
            GST_DEBUG ("using default channel layout for %d channels", ch);
932
933
          else
            GST_WARNING ("failed to add channel layout");
934
935
936
937
938
939
940
941
942
943
944
        }
      } else {
        /* FIXME: this is pretty useless - we need fixed caps */
        caps = gst_caps_from_string ("audio/x-raw-float, "
            "endianness = (int) LITTLE_ENDIAN, " "width = (int) { 32, 64 }");
      }
      if (codec_name && strf)
        *codec_name = g_strdup_printf ("Uncompressed %d-bit IEEE float audio",
            strf->size);
      break;

945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
    case GST_RIFF_WAVE_FORMAT_IBM_CVSD:
      goto unknown;

    case GST_RIFF_WAVE_FORMAT_ALAW:
      if (strf != NULL) {
        if (strf->size != 8) {
          GST_WARNING ("invalid depth (%d) of alaw audio, overwriting.",
              strf->size);
          strf->size = 8;
          strf->av_bps = 8;
          strf->blockalign = strf->av_bps * strf->channels;
        }
        if (strf->av_bps == 0 || strf->blockalign == 0) {
          GST_WARNING ("fixing av_bps (%d) and blockalign (%d) of alaw audio",
              strf->av_bps, strf->blockalign);
          strf->av_bps = strf->size;
          strf->blockalign = strf->av_bps * strf->channels;
        }
      }
964
      rate_max = 48000;
965
966
967
968
969
      caps = gst_caps_new_simple ("audio/x-alaw", NULL);
      if (codec_name)
        *codec_name = g_strdup ("A-law audio");
      break;

970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
    case GST_RIFF_WAVE_FORMAT_WMS:
      caps = gst_caps_new_simple ("audio/x-wms", NULL);
      if (strf != NULL) {
        gst_caps_set_simple (caps,
            "bitrate", G_TYPE_INT, strf->av_bps * 8,
            "width", G_TYPE_INT, strf->size,
            "depth", G_TYPE_INT, strf->size, NULL);
      } else {
        gst_caps_set_simple (caps,
            "bitrate", GST_TYPE_INT_RANGE, 0, G_MAXINT, NULL);
      }
      if (codec_name)
        *codec_name = g_strdup ("Windows Media Audio Speech");
      block_align = TRUE;
      break;

986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
    case GST_RIFF_WAVE_FORMAT_MULAW:
      if (strf != NULL) {
        if (strf->size != 8) {
          GST_WARNING ("invalid depth (%d) of mulaw audio, overwriting.",
              strf->size);
          strf->size = 8;
          strf->av_bps = 8;
          strf->blockalign = strf->av_bps * strf->channels;
        }
        if (strf->av_bps == 0 || strf->blockalign == 0) {
          GST_WARNING ("fixing av_bps (%d) and blockalign (%d) of mulaw audio",
              strf->av_bps, strf->blockalign);
          strf->av_bps = strf->size;
          strf->blockalign = strf->av_bps * strf->channels;
        }
      }
1002
      rate_max = 48000;
1003
1004
1005
1006
1007
1008
1009
1010
      caps = gst_caps_new_simple ("audio/x-mulaw", NULL);
      if (codec_name)
        *codec_name = g_strdup ("Mu-law audio");
      break;

    case GST_RIFF_WAVE_FORMAT_OKI_ADPCM:
      goto unknown;

1011
    case GST_RIFF_WAVE_FORMAT_DVI_ADPCM:
1012
      rate_max = 48000;
1013
1014
1015
1016
1017
      caps = gst_caps_new_simple ("audio/x-adpcm",
          "layout", G_TYPE_STRING, "dvi", NULL);
      if (codec_name)
        *codec_name = g_strdup ("DVI ADPCM audio");
      block_align = TRUE;
1018
1019
      break;

1020
1021
1022
    case GST_RIFF_WAVE_FORMAT_GSM610:
    case GST_RIFF_WAVE_FORMAT_MSN:
      caps = gst_caps_new_simple ("audio/ms-gsm", NULL);
1023
      if (codec_name)
1024
        *codec_name = g_strdup ("MS GSM audio");
1025
1026
      break;

1027
    case GST_RIFF_WAVE_FORMAT_MPEGL12: /* mp1 or mp2 */
1028
1029
      rate_min = 16000;
      rate_max = 48000;
1030
1031
      caps = gst_caps_new_simple ("audio/mpeg",
          "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 2, NULL);
1032
      if (codec_name)
1033
1034
1035
1036
        *codec_name = g_strdup ("MPEG-1 layer 2");
      break;

    case GST_RIFF_WAVE_FORMAT_MPEGL3:  /* mp3 */
1037
      rate_min = 8000;
1038
      rate_max = 48000;
1039
1040
1041
1042
      caps = gst_caps_new_simple ("audio/mpeg",
          "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 3, NULL);
      if (codec_name)
        *codec_name = g_strdup ("MPEG-1 layer 3");
1043
1044
      break;

1045
1046
1047
1048
1049
1050
    case GST_RIFF_WAVE_FORMAT_VORBIS1: /* ogg/vorbis mode 1 */
    case GST_RIFF_WAVE_FORMAT_VORBIS2: /* ogg/vorbis mode 2 */
    case GST_RIFF_WAVE_FORMAT_VORBIS3: /* ogg/vorbis mode 3 */
    case GST_RIFF_WAVE_FORMAT_VORBIS1PLUS:     /* ogg/vorbis mode 1+ */
    case GST_RIFF_WAVE_FORMAT_VORBIS2PLUS:     /* ogg/vorbis mode 2+ */
    case GST_RIFF_WAVE_FORMAT_VORBIS3PLUS:     /* ogg/vorbis mode 3+ */
David Schleef's avatar
David Schleef committed
1051
      caps = gst_caps_new_simple ("audio/x-vorbis", NULL);
1052
      if (codec_name)
1053
        *codec_name = g_strdup ("Vorbis");
1054
1055
1056
      break;

    case GST_RIFF_WAVE_FORMAT_A52:
1057
      channels_max = 6;
David Schleef's avatar
David Schleef committed
1058
      caps = gst_caps_new_simple ("audio/x-ac3", NULL);
1059
      if (codec_name)
1060
        *codec_name = g_strdup ("AC-3 audio");
1061
      break;
1062
1063
1064
1065
1066
    case GST_RIFF_WAVE_FORMAT_DTS:
      channels_max = 6;
      caps = gst_caps_new_simple ("audio/x-dts", NULL);
      if (codec_name)
        *codec_name = g_strdup ("DTS audio");
1067
1068
      /* wavparse is not always able to specify rate/channels for DTS-in-wav */
      rate_chan = FALSE;
1069
      break;
1070
1071
1072
1073
1074
1075
1076
1077
1078
    case GST_RIFF_WAVE_FORMAT_AAC:
    {
      channels_max = 8;
      caps = gst_caps_new_simple ("audio/mpeg",
          "mpegversion", G_TYPE_INT, 4, NULL);
      if (codec_name)
        *codec_name = g_strdup ("MPEG-4 AAC audio");
      break;
    }
1079
1080
    case GST_RIFF_WAVE_FORMAT_WMAV1:
    case GST_RIFF_WAVE_FORMAT_WMAV2:
1081
    case GST_RIFF_WAVE_FORMAT_WMAV3:
1082
    {
1083
      gint version = (codec_id - GST_RIFF_WAVE_FORMAT_WMAV1) + 1;
1084

1085
      channels_max = 6;
1086
1087
1088
1089
1090
1091
      block_align = TRUE;

      caps = gst_caps_new_simple ("audio/x-wma",
          "wmaversion", G_TYPE_INT, version, NULL);

      if (codec_name)
1092
        *codec_name = g_strdup_printf ("WMA Version %d", version + 6);
1093

1094
1095
      if (strf != NULL) {
        gst_caps_set_simple (caps,
Fabrizio's avatar
Fabrizio committed
1096
1097
            "bitrate", G_TYPE_INT, strf->av_bps * 8,
            "depth", G_TYPE_INT, strf->size, NULL);
1098
1099
1100
1101
1102
1103
      } else {
        gst_caps_set_simple (caps,
            "bitrate", GST_TYPE_INT_RANGE, 0, G_MAXINT, NULL);
      }
      break;
    }
1104
1105
1106
1107
1108
    case GST_RIFF_WAVE_FORMAT_WMAV3_L:
    {
      /* WMA Version 9 Lossless */
      goto unknown;
    }
1109
1110
1111
1112
1113
1114
    case GST_RIFF_WAVE_FORMAT_SONY_ATRAC3:
      caps = gst_caps_new_simple ("audio/x-vnd.sony.atrac3", NULL);
      if (codec_name)
        *codec_name = g_strdup ("Sony ATRAC3");
      break;

1115
1116
1117
1118
1119
1120
    case GST_RIFF_WAVE_FORMAT_EXTENSIBLE:{
      guint16 valid_bits_per_sample;
      guint32 channel_mask;
      guint32 subformat_guid[4];
      const guint8 *data;

1121
1122
      channels_max = 8;

1123
1124
      /* should be at least 22 bytes */
      if (strf_data == NULL || GST_BUFFER_SIZE (strf_data) < 22) {
1125
        GST_WARNING ("WAVE_FORMAT_EXTENSIBLE data size is %d (expected: 22)",
1126
            (strf_data) ? GST_BUFFER_SIZE (strf_data) : -1);
1127
1128
1129
1130
        return NULL;
      }

      data = GST_BUFFER_DATA (strf_data);
1131

1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
      valid_bits_per_sample = GST_READ_UINT16_LE (data);
      channel_mask = GST_READ_UINT32_LE (data + 2);
      subformat_guid[0] = GST_READ_UINT32_LE (data + 6);
      subformat_guid[1] = GST_READ_UINT32_LE (data + 10);
      subformat_guid[2] = GST_READ_UINT32_LE (data + 14);
      subformat_guid[3] = GST_READ_UINT32_LE (data + 18);

      GST_DEBUG ("valid bps    = %u", valid_bits_per_sample);
      GST_DEBUG ("channel mask = 0x%08x", channel_mask);
      GST_DEBUG ("GUID         = %08x-%08x-%08x-%08x", subformat_guid[0],
          subformat_guid[1], subformat_guid[2], subformat_guid[3]);

      if (subformat_guid[1] == 0x00100000 &&
          subformat_guid[2] == 0xaa000080 && subformat_guid[3] == 0x719b3800) {
        if (subformat_guid[0] == 0x00000001) {
          GST_DEBUG ("PCM");
          if (strf != NULL) {
            gint ba = strf->blockalign;
1150
            gint wd = ba * 8 / strf->channels;
1151
            gint ws;
1152

1153
1154
1155
1156
1157
1158
1159
1160
1161
            /* in riff, the depth is stored in the size field but it just
             * means that the _least_ significant bits are cleared. We can
             * therefore just play the sample as if it had a depth == width */
            ws = wd;

            /* For reference, use this to get the actual depth:
             * ws = strf->size;
             * if (valid_bits_per_sample != 0)
             *   ws = valid_bits_per_sample; */
1162
1163
1164
1165

            caps = gst_caps_new_simple ("audio/x-raw-int",
                "endianness", G_TYPE_INT, G_LITTLE_ENDIAN,
                "channels", G_TYPE_INT, strf->channels,
1166
1167
                "width", G_TYPE_INT, wd,
                "depth", G_TYPE_INT, ws,
1168
                "rate", G_TYPE_INT, strf->rate,
1169
1170
                "signed", G_TYPE_BOOLEAN, wd != 8, NULL);

1171
            /* If channel_mask == 0 and channels > 2 let's
1172
             * assume default layout as some wav files don't have the