gstffmpegcodecmap.c 34.7 KB
Newer Older
1 2
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 4
 * This file:
 * Copyright (c) 2002-2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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.
 */

22
#ifdef HAVE_CONFIG_H
23
#include "config.h"
24
#endif
25
#include <gst/gst.h>
26 27 28 29 30
#ifdef HAVE_FFMPEG_UNINSTALLED
#include <avcodec.h>
#else
#include <ffmpeg/avcodec.h>
#endif
31
#include <string.h>
32

33 34 35 36 37 38 39 40
#include "gstffmpegcodecmap.h"

/* this macro makes a caps width fixed or unfixed width/height
 * properties depending on whether we've got a context.
 *
 * See below for why we use this.
 */

David Schleef's avatar
David Schleef committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54
#define GST_FF_VID_CAPS_NEW(mimetype, props...)			\
    (context != NULL) ?						\
    gst_caps_new_simple (mimetype,			      	\
	"width",     G_TYPE_INT,   context->width,	      	\
	"height",    G_TYPE_INT,   context->height,	  	\
	"framerate", G_TYPE_DOUBLE, 1. * context->frame_rate /  \
				   context->frame_rate_base,    \
	##props, NULL)	  					\
    :	  							\
    gst_caps_new_simple (mimetype,			      	\
	"width",     GST_TYPE_INT_RANGE, 16, 4096,      	\
	"height",    GST_TYPE_INT_RANGE, 16, 4096,	      	\
	"framerate", GST_TYPE_DOUBLE_RANGE, 0., G_MAXDOUBLE,	\
	##props, NULL)
55 56 57 58

/* same for audio - now with channels/sample rate
 */

David Schleef's avatar
David Schleef committed
59 60 61 62 63 64 65 66 67
#define GST_FF_AUD_CAPS_NEW(mimetype, props...)			\
    (context != NULL) ?					      	\
    gst_caps_new_simple (mimetype,	      			\
	"rate", G_TYPE_INT, context->sample_rate,		\
	"channels", G_TYPE_INT, context->channels,		\
	##props, NULL)						\
    :								\
    gst_caps_new_simple (mimetype,	      			\
	##props, NULL)
68 69 70 71 72 73 74 75

/* Convert a FFMPEG codec ID and optional AVCodecContext
 * to a GstCaps. If the context is ommitted, no fixed values
 * for video/audio size will be included in the GstCaps
 *
 * CodecID is primarily meant for compressed data GstCaps!
 */

76
GstCaps *
77 78
gst_ffmpeg_codecid_to_caps (enum CodecID    codec_id,
                            AVCodecContext *context)
79
{
80 81
  GstCaps *caps = NULL;

82
  switch (codec_id) {
83
    case CODEC_ID_MPEG1VIDEO:
84
      /* this caps doesn't need width/height/framerate */
David Schleef's avatar
David Schleef committed
85 86 87 88
      caps = gst_caps_new_simple ("video/mpeg",
	  "mpegversion",  G_TYPE_INT,	  1,
          "systemstream", G_TYPE_BOOLEAN, FALSE,
          NULL);
89
      break;
David Schleef's avatar
David Schleef committed
90
      
91 92
    case CODEC_ID_H263P:
    case CODEC_ID_H263I:
93
    case CODEC_ID_H263:
David Schleef's avatar
David Schleef committed
94
      caps = GST_FF_VID_CAPS_NEW ("video/x-h263");
95
      break;
96

97
    case CODEC_ID_RV10:
David Schleef's avatar
David Schleef committed
98 99 100
      caps = GST_FF_VID_CAPS_NEW ("video/x-pn-realvideo",
	  "systemstream", G_TYPE_BOOLEAN, FALSE,
	  NULL);
101
      break;
102

103
    case CODEC_ID_MP2:
David Schleef's avatar
David Schleef committed
104 105 106 107
      caps = GST_FF_AUD_CAPS_NEW ("audio/mpeg",
	  "mpegversion", G_TYPE_INT, 1,
	  "layer", G_TYPE_INT, 2,
          NULL);
108
      break;
109

110
    case CODEC_ID_MP3LAME:
David Schleef's avatar
David Schleef committed
111 112 113 114
      caps = GST_FF_AUD_CAPS_NEW ("audio/mpeg",
	  "mpegversion", G_TYPE_INT, 1,
	  "layer", G_TYPE_INT, 3,
	  NULL);
115
      break;
116

David Schleef's avatar
David Schleef committed
117 118 119 120 121
    /* FIXME: This could become problematic when we fully switched to seperation
       of ogg and vorbis, because ffmpeg does ass ume ogg == ogg/vorbis 
       Maybe we want to disable this? */
    case CODEC_ID_VORBIS:
      caps = GST_FF_AUD_CAPS_NEW ("application/ogg");
122
      break;
123
      
124
    case CODEC_ID_AC3:
David Schleef's avatar
David Schleef committed
125
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-ac3");
126
      break;
127

128
    case CODEC_ID_MJPEG:
Wim Taymans's avatar
Wim Taymans committed
129
    case CODEC_ID_MJPEGB:
130
    /*case CODEC_ID_LJPEG:*/
David Schleef's avatar
David Schleef committed
131
      caps = GST_FF_VID_CAPS_NEW ("video/x-jpeg");
Wim Taymans's avatar
Wim Taymans committed
132
      break;
133

134
    case CODEC_ID_MPEG4:
David Schleef's avatar
David Schleef committed
135 136 137 138 139 140 141 142 143 144 145 146
      caps = GST_FF_VID_CAPS_NEW ("video/mpeg",
	  "mpegversion",  G_TYPE_INT, 4,
          "systemstream", G_TYPE_BOOLEAN, FALSE,
	  NULL);
      gst_caps_append(caps,
	  GST_FF_VID_CAPS_NEW ("video/x-divx",
	      "divxversion", GST_TYPE_INT_RANGE, 4, 5,
	      NULL));
      gst_caps_append(caps,
             GST_FF_VID_CAPS_NEW ("video/x-xvid"));
      gst_caps_append(caps,
             GST_FF_VID_CAPS_NEW ("video/x-3ivx"));
147
      break;
148

149 150 151 152 153
    /* weird quasi-codecs for the demuxers only */
    case CODEC_ID_RAWVIDEO:
      /* we use a shortcut to the raw-video pad function */
      return gst_ffmpeg_codectype_to_caps (CODEC_TYPE_VIDEO, context);

154
    case CODEC_ID_MSMPEG4V1:
David Schleef's avatar
David Schleef committed
155 156 157
      caps = GST_FF_VID_CAPS_NEW ("video/x-msmpeg",
	  "msmpegversion", G_TYPE_INT, 41,
	  NULL);
158
      break;
159

160
    case CODEC_ID_MSMPEG4V2:
David Schleef's avatar
David Schleef committed
161 162 163
      caps = GST_FF_VID_CAPS_NEW ("video/x-msmpeg",
          "msmpegversion", G_TYPE_INT, 42,
          NULL);
164
      break;
165

166
    case CODEC_ID_MSMPEG4V3:
David Schleef's avatar
David Schleef committed
167 168 169 170 171 172 173
      caps = GST_FF_VID_CAPS_NEW ("video/x-msmpeg",
	  "msmpegversion", G_TYPE_INT, 43,
	  NULL);
      gst_caps_append(caps,
          GST_FF_VID_CAPS_NEW ("video/x-divx",
	      "divxversion", G_TYPE_INT, 3,
              NULL));
174
      break;
175

176
    case CODEC_ID_WMV1:
David Schleef's avatar
David Schleef committed
177 178 179
      caps = GST_FF_VID_CAPS_NEW ("video/x-wmv",
	  "wmvversion", G_TYPE_INT, 1,
          NULL);
180
      break;
181

182
    case CODEC_ID_WMV2:
David Schleef's avatar
David Schleef committed
183 184 185
      caps = GST_FF_VID_CAPS_NEW ("video/x-wmv",
	  "wmvversion", G_TYPE_INT, 2,
          NULL);
186
      break;
187

188
    case CODEC_ID_SVQ1:
David Schleef's avatar
David Schleef committed
189 190 191
      caps = GST_FF_VID_CAPS_NEW ("video/x-svq",
	  "svqversion", G_TYPE_INT, 1,
          NULL);
192
      break;
193 194

    case CODEC_ID_SVQ3:
David Schleef's avatar
David Schleef committed
195
      caps = GST_FF_VID_CAPS_NEW ("video/x-svq",
196
	  "svqversion", G_TYPE_INT, 3,
David Schleef's avatar
David Schleef committed
197
          NULL);
198
      break;
199

200
    case CODEC_ID_DVAUDIO:
David Schleef's avatar
David Schleef committed
201
        caps = GST_FF_AUD_CAPS_NEW ("audio/x-dv");
202
        break;
203

204
    case CODEC_ID_DVVIDEO:
David Schleef's avatar
David Schleef committed
205
      caps = GST_FF_VID_CAPS_NEW ("video/dv");
206
      break;
207

208
    case CODEC_ID_WMAV1:
David Schleef's avatar
David Schleef committed
209 210 211 212 213 214 215
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-wma",
	  "wmaversion",  G_TYPE_INT,	      1,
          "flags1",      GST_TYPE_INT_RANGE,  G_MININT, G_MAXINT,
          "flags2",      GST_TYPE_INT_RANGE,  G_MININT, G_MAXINT,
          "block_align", GST_TYPE_INT_RANGE,  0, G_MAXINT,
          "bitrate",     GST_TYPE_INT_RANGE,  0, G_MAXINT,
	  NULL);
216
      break;
217

218
    case CODEC_ID_WMAV2:
David Schleef's avatar
David Schleef committed
219 220 221 222 223 224 225
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-wma",
	  "wmaversion",  G_TYPE_INT,	      2,
          "flags1",      GST_TYPE_INT_RANGE,  G_MININT, G_MAXINT,
          "flags2",      GST_TYPE_INT_RANGE,  G_MININT, G_MAXINT,
          "block_align", GST_TYPE_INT_RANGE,  0, G_MAXINT,
          "bitrate",     GST_TYPE_INT_RANGE,  0, G_MAXINT,
	  NULL);
226
      break;
227

228
    case CODEC_ID_MACE3:
David Schleef's avatar
David Schleef committed
229 230 231
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-mace",
	  "maceversion", G_TYPE_INT, 3,
          NULL);
232
      break;
233

234
    case CODEC_ID_MACE6:
David Schleef's avatar
David Schleef committed
235 236 237
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-mace",
	  "maceversion", G_TYPE_INT, 6,
          NULL);
238
      break;
239

Wim Taymans's avatar
Wim Taymans committed
240
    case CODEC_ID_HUFFYUV:
David Schleef's avatar
David Schleef committed
241
      caps = GST_FF_VID_CAPS_NEW ("video/x-huffyuv");
242 243 244 245 246 247 248
      break;

    case CODEC_ID_CYUV:
      /* .. */
      break;

    case CODEC_ID_H264:
David Schleef's avatar
David Schleef committed
249
      caps = GST_FF_VID_CAPS_NEW ("video/x-h264");
250 251 252
      break;

    case CODEC_ID_INDEO3:
David Schleef's avatar
David Schleef committed
253 254 255
      caps = GST_FF_VID_CAPS_NEW ("video/x-indeo",
	  "indeoversion", G_TYPE_INT, 3,
	  NULL);
256 257 258
      break;

    case CODEC_ID_VP3:
David Schleef's avatar
David Schleef committed
259 260 261
      caps = GST_FF_VID_CAPS_NEW ("video/x-vp3");
      gst_caps_append(caps,
             GST_FF_VID_CAPS_NEW ("video/x-theora"));
262 263 264
      break;

    case CODEC_ID_AAC:
David Schleef's avatar
David Schleef committed
265 266 267 268
      caps = GST_FF_AUD_CAPS_NEW ("audio/mpeg",
	  "systemstream", G_TYPE_BOOLEAN, FALSE,
	  "mpegversion",  G_TYPE_INT,	  2,
	  NULL);
269 270 271
      break;

    case CODEC_ID_MPEG4AAC:
David Schleef's avatar
David Schleef committed
272 273 274 275
      caps = GST_FF_AUD_CAPS_NEW ("audio/mpeg",
	  "systemstream", G_TYPE_BOOLEAN, FALSE,
	  "mpegversion",  G_TYPE_INT,	  4,
	  NULL);
276 277 278 279 280 281
      break;

    case CODEC_ID_ASV1:
      /* .. */
      break;

282
    case CODEC_ID_FFV1:
David Schleef's avatar
David Schleef committed
283 284 285
      caps = GST_FF_VID_CAPS_NEW ("video/x-ffv",
	  "ffvversion", G_TYPE_INT, 1,
	  NULL);
286 287 288
      break;

    case CODEC_ID_4XM:
David Schleef's avatar
David Schleef committed
289
      caps = GST_FF_VID_CAPS_NEW ("video/x-4xm");
290 291 292 293 294 295 296 297 298 299
      break;

    /* weird quasi-codecs for the demuxers only */
    case CODEC_ID_PCM_S16LE:
    case CODEC_ID_PCM_S16BE:
    case CODEC_ID_PCM_U16LE:
    case CODEC_ID_PCM_U16BE:
    case CODEC_ID_PCM_S8:
    case CODEC_ID_PCM_U8:
      do {
300
        gint width = 0, depth = 0, endianness = 0;
301 302 303 304
	gboolean signedness = FALSE; /* blabla */

        switch (codec_id) {
          case CODEC_ID_PCM_S16LE:
305
            width = 16; depth = 16;
306 307 308 309
            endianness = G_LITTLE_ENDIAN;
            signedness = TRUE;
            break;
          case CODEC_ID_PCM_S16BE:
310
            width = 16; depth = 16;
311 312 313 314
            endianness = G_BIG_ENDIAN;
            signedness = TRUE;
            break;
          case CODEC_ID_PCM_U16LE:
315
            width = 16; depth = 16;
316 317 318 319
            endianness = G_LITTLE_ENDIAN;
            signedness = FALSE;
            break;
          case CODEC_ID_PCM_U16BE:
320
            width = 16; depth = 16;
321 322 323 324
            endianness = G_BIG_ENDIAN;
            signedness = FALSE;
            break;
          case CODEC_ID_PCM_S8:
325
            width = 8;  depth = 8;
326 327 328 329
            endianness = G_BYTE_ORDER;
            signedness = TRUE;
            break;
          case CODEC_ID_PCM_U8:
330
            width = 8;  depth = 8;
331 332 333 334 335 336 337 338
            endianness = G_BYTE_ORDER;
            signedness = FALSE;
            break;
          default:
            g_assert(0); /* don't worry, we never get here */
            break;
        }

David Schleef's avatar
David Schleef committed
339 340 341 342 343 344
        caps = GST_FF_AUD_CAPS_NEW ("audio/x-raw-int",
	    "width",      G_TYPE_INT,	  width,
	    "depth",      G_TYPE_INT,	  depth,
	    "endianness", G_TYPE_INT,	  endianness,
	    "signed",     G_TYPE_BOOLEAN, signedness,
	    NULL);
345 346 347
      } while (0);
      break;

348
    case CODEC_ID_PCM_MULAW:
David Schleef's avatar
David Schleef committed
349
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-mulaw");
350 351 352
      break;

    case CODEC_ID_PCM_ALAW:
David Schleef's avatar
David Schleef committed
353
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-alaw");
354 355
      break;

356
    case CODEC_ID_ADPCM_IMA_QT:
David Schleef's avatar
David Schleef committed
357 358 359
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-adpcm",
	  "layout", G_TYPE_STRING, "quicktime",
          NULL);
360
      break;
361

362
    case CODEC_ID_ADPCM_IMA_WAV:
David Schleef's avatar
David Schleef committed
363 364 365
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-adpcm",
	  "layout", G_TYPE_STRING, "wav",
          NULL);
366
      break;
367

368
    case CODEC_ID_ADPCM_MS:
David Schleef's avatar
David Schleef committed
369 370 371
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-adpcm",
	  "layout", G_TYPE_STRING, "microsoft",
          NULL);
372 373 374
      break;

    case CODEC_ID_ADPCM_4XM:
David Schleef's avatar
David Schleef committed
375 376 377
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-adpcm",
	  "layout", G_TYPE_STRING, "4xm",
          NULL);
378 379 380 381 382 383
      break;

    case CODEC_ID_AMR_NB:
      /* .. */
      break;

384
    case CODEC_ID_RA_144:
David Schleef's avatar
David Schleef committed
385 386 387
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-pn-realaudio",
	  "bitrate", G_TYPE_INT, 14400,
          NULL);
388 389 390
      break;

    case CODEC_ID_RA_288:
David Schleef's avatar
David Schleef committed
391 392 393
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-pn-realaudio",
	  "bitrate", G_TYPE_INT, 28800,
          NULL);
394
      break;
395

396 397 398 399 400 401
    default:
      /* .. */
      break;
  }

  if (caps != NULL) {
David Schleef's avatar
David Schleef committed
402 403
    char *str = gst_caps_to_string (caps);
    GST_DEBUG ("caps for codec_id=%d: %s", codec_id, str);
404
    g_free(str);
405
  } else {
David Schleef's avatar
David Schleef committed
406
    GST_WARNING ("No caps found for codec_id=%d", codec_id);
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
  }

  return caps;
}

/* Convert a FFMPEG Pixel Format and optional AVCodecContext
 * to a GstCaps. If the context is ommitted, no fixed values
 * for video/audio size will be included in the GstCaps
 *
 * See below for usefullness
 */

static GstCaps *
gst_ffmpeg_pixfmt_to_caps (enum PixelFormat  pix_fmt,
                           AVCodecContext   *context)
{
  GstCaps *caps = NULL;

  int bpp = 0, depth = 0, endianness = 0;
  gulong g_mask = 0, r_mask = 0, b_mask = 0;
  guint32 fmt = 0;

  switch (pix_fmt) {
    case PIX_FMT_YUV420P:
      fmt = GST_MAKE_FOURCC ('I','4','2','0');
      break;
    case PIX_FMT_YUV422:
      fmt = GST_MAKE_FOURCC ('Y','U','Y','2');
      break;
    case PIX_FMT_RGB24:
      bpp = depth = 24;
      endianness = G_BIG_ENDIAN;
      r_mask = 0xff0000; g_mask = 0x00ff00; b_mask = 0x0000ff;
      break;
    case PIX_FMT_BGR24:
      bpp = depth = 24;
443 444
      endianness = G_BIG_ENDIAN;
      r_mask = 0x0000ff; g_mask = 0x00ff00; b_mask = 0xff0000;
445 446
      break;
    case PIX_FMT_YUV422P:
447
      fmt = GST_MAKE_FOURCC ('Y','4','2','B');
448 449 450 451 452
      break;
    case PIX_FMT_YUV444P:
      /* .. */
      break;
    case PIX_FMT_RGBA32:
453
      bpp = 32; depth = 24;
454 455
      endianness = G_BIG_ENDIAN;
#if (G_BYTE_ORDER == G_BIG_ENDIAN)
Benjamin Otte's avatar
Benjamin Otte committed
456
      r_mask = 0x00ff0000; g_mask = 0x0000ff00; b_mask = 0x000000ff;
457 458
#else 
      r_mask = 0x0000ff00; g_mask = 0x00ff0000; b_mask = 0xff000000;
459
#endif
460 461
      break;
    case PIX_FMT_YUV410P:
462
      fmt = GST_MAKE_FOURCC ('Y','U','V','9');
463 464
      break;
    case PIX_FMT_YUV411P:
465
      fmt = GST_MAKE_FOURCC ('Y','4','1','B');
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
      break;
    case PIX_FMT_RGB565:
      bpp = depth = 16;
      endianness = G_BYTE_ORDER;
      r_mask = 0xf800; g_mask = 0x07e0; b_mask = 0x001f;
      break;
    case PIX_FMT_RGB555:
      bpp = 16; depth = 15;
      endianness = G_BYTE_ORDER;
      r_mask = 0x7c00; g_mask = 0x03e0; b_mask = 0x001f;
      break;
    default:
      /* give up ... */
      break;
  }

  if (bpp != 0) {
David Schleef's avatar
David Schleef committed
483 484 485 486 487 488 489 490
    caps = GST_FF_VID_CAPS_NEW ("video/x-raw-rgb",
	"bpp",	      G_TYPE_INT, bpp,
        "depth",      G_TYPE_INT, depth,
        "red_mask",   G_TYPE_INT, r_mask,
        "green_mask", G_TYPE_INT, g_mask,
        "blue_mask",  G_TYPE_INT, b_mask,
	"endianness", G_TYPE_INT, endianness,
        NULL);
491
  } else if (fmt) {
David Schleef's avatar
David Schleef committed
492 493 494
    caps = GST_FF_VID_CAPS_NEW ("video/x-raw-yuv",
	"format",     GST_TYPE_FOURCC, fmt,
        NULL);
495 496 497
  }

  if (caps != NULL) {
David Schleef's avatar
David Schleef committed
498 499
    char *str = gst_caps_to_string (caps);
    GST_DEBUG ("caps for pix_fmt=%d: %s", pix_fmt, str);
500
    g_free(str);
501
  } else {
David Schleef's avatar
David Schleef committed
502
    GST_WARNING ("No caps found for pix_fmt=%d", pix_fmt);
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
  }

  return caps;
}

/* Convert a FFMPEG Sample Format and optional AVCodecContext
 * to a GstCaps. If the context is ommitted, no fixed values
 * for video/audio size will be included in the GstCaps
 *
 * See below for usefullness
 */

static GstCaps *
gst_ffmpeg_smpfmt_to_caps (enum SampleFormat  sample_fmt,
                           AVCodecContext    *context)
{
  GstCaps *caps = NULL;

  int bpp = 0;
  gboolean signedness = FALSE;

  switch (sample_fmt) {
    case SAMPLE_FMT_S16:
      signedness = TRUE;
      bpp = 16;
528
      break;
529

530
    default:
531
      /* .. */
532 533 534
      break;
  }

535
  if (bpp) {
David Schleef's avatar
David Schleef committed
536 537 538 539 540 541
    caps = GST_FF_AUD_CAPS_NEW ("audio/x-raw-int",
	"signed",     G_TYPE_BOOLEAN, signedness,
        "endianness", G_TYPE_INT,     G_BYTE_ORDER,
        "width",      G_TYPE_INT,     bpp,
        "depth",      G_TYPE_INT,     bpp,
        NULL);
542 543 544
  }

  if (caps != NULL) {
David Schleef's avatar
David Schleef committed
545 546
    char *str = gst_caps_to_string (caps);
    GST_DEBUG ("caps for sample_fmt=%d: %s", sample_fmt, str);
547
    g_free(str);
548
  } else {
David Schleef's avatar
David Schleef committed
549
    GST_WARNING ("No caps found for sample_fmt=%d", sample_fmt);
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
  }

  return caps;
}

/* Convert a FFMPEG codec Type and optional AVCodecContext
 * to a GstCaps. If the context is ommitted, no fixed values
 * for video/audio size will be included in the GstCaps
 *
 * CodecType is primarily meant for uncompressed data GstCaps!
 */

GstCaps *
gst_ffmpeg_codectype_to_caps (enum CodecType  codec_type,
                              AVCodecContext *context)
{
David Schleef's avatar
David Schleef committed
566
  GstCaps *caps;
567 568 569 570 571 572 573 574 575

  switch (codec_type) {
    case CODEC_TYPE_VIDEO:
      if (context) {
        caps = gst_ffmpeg_pixfmt_to_caps (context->pix_fmt, context);
      } else {
        GstCaps *temp;
        enum PixelFormat i;

David Schleef's avatar
David Schleef committed
576
        caps = gst_caps_new_empty ();
577 578 579
        for (i = 0; i < PIX_FMT_NB; i++) {
          temp = gst_ffmpeg_pixfmt_to_caps (i, NULL);
          if (temp != NULL) {
David Schleef's avatar
David Schleef committed
580
            gst_caps_append (caps, temp);
581 582 583 584 585 586 587 588 589 590 591 592
          }
        }
      }
      break;

    case CODEC_TYPE_AUDIO:
      if (context) {
        caps = gst_ffmpeg_smpfmt_to_caps (context->sample_fmt, context);
      } else {
        GstCaps *temp;
        enum SampleFormat i;

David Schleef's avatar
David Schleef committed
593
        caps = gst_caps_new_empty ();
594 595 596
        for (i = 0; i <= SAMPLE_FMT_S16; i++) {
          temp = gst_ffmpeg_smpfmt_to_caps (i, NULL);
          if (temp != NULL) {
David Schleef's avatar
David Schleef committed
597
            gst_caps_append (caps, temp);
598 599 600 601 602 603 604
          }
        }
      }
      break;

    default:
      /* .. */
David Schleef's avatar
David Schleef committed
605
      caps = NULL;
606 607 608 609
      break;
  }

  return caps;
610
}
611

612 613 614 615 616

/* Construct the context extradata from caps
 * when needed.
 */
static void
David Schleef's avatar
David Schleef committed
617
gst_ffmpeg_caps_to_extradata (const GstCaps *caps,
618 619
                              AVCodecContext *context)
{
David Schleef's avatar
David Schleef committed
620
  GstStructure *structure;
621 622
  const gchar *mimetype;

David Schleef's avatar
David Schleef committed
623 624 625 626
  g_return_if_fail (gst_caps_get_size (caps) == 1);
  structure = gst_caps_get_structure (caps, 0);
  
  mimetype = gst_structure_get_name (structure);
627 628

  if (!strcmp(mimetype, "audio/x-wma")) {
David Schleef's avatar
David Schleef committed
629
    gint flags1, flags2, wmaversion = 0;
630

David Schleef's avatar
David Schleef committed
631 632 633 634
    if (!gst_structure_get_int (structure, "flags1", &flags1) &&
	!gst_structure_get_int (structure, "flags2", &flags2) &&
	!gst_structure_get_int (structure, "wmaversion", &wmaversion)) {
      g_warning ("invalid caps for audio/x-wma");
635 636 637
      return;
    }

David Schleef's avatar
David Schleef committed
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    /* 
     * Rebuild context data from flags1 & flags2 
     * see wmadec in ffmpeg/libavcodec/wmadec.c 
     */
    gst_structure_get_int (structure, "wmaversion", &wmaversion);
    switch (wmaversion) {
      case 1:
	/* FIXME: is this freed with g_free? If not, don't use g_malloc */
	context->extradata = (guint8 *) g_malloc0 (4); 
	((guint8 *)context->extradata)[0] = flags1;
	((guint8 *)context->extradata)[2] = flags2;
	context->extradata_size = 4;
	break;
      case 2:
	/* FIXME: is this freed with g_free? If not, don't use g_malloc */
	context->extradata = (guint8 *) g_malloc0 (6);        
	((guint8 *) context->extradata)[0] = flags1;
	((guint8 *) context->extradata)[1] = flags1 >> 8;
	((guint8 *) context->extradata)[2] = flags1 >> 16;
	((guint8 *) context->extradata)[3] = flags1 >> 24;
	((guint8 *) context->extradata)[4] = flags2; 
	((guint8 *) context->extradata)[5] = flags2 >> 8;
	context->extradata_size = 6;
	break;
      default:
	g_warning ("Unknown wma version %d\n", wmaversion);
	break;
665 666 667 668 669
    }
  }
}


670 671 672 673 674 675 676
/* Convert a GstCaps (audio/raw) to a FFMPEG SampleFmt
 * and other audio properties in a AVCodecContext.
 *
 * For usefullness, see below
 */

static void
David Schleef's avatar
David Schleef committed
677
gst_ffmpeg_caps_to_smpfmt (const GstCaps *caps,
678 679
                           AVCodecContext *context)
{
David Schleef's avatar
David Schleef committed
680 681 682 683 684 685 686 687 688 689 690
  GstStructure *structure;
  gint depth = 0, width = 0, endianness = 0;
  gboolean signedness = FALSE;

  g_return_if_fail (gst_caps_get_size (caps) == 1);
  structure = gst_caps_get_structure (caps, 0);
  
  if (gst_structure_get_int (structure, "width", &width) &&
      gst_structure_get_int (structure, "depth", &depth) &&
      gst_structure_get_int (structure, "signed", &signedness) &&
      gst_structure_get_int (structure, "endianness", &endianness)) {
691 692 693 694 695 696
    if (width == 16 && depth == 16 &&
        endianness == G_BYTE_ORDER && signedness == TRUE) {
      context->sample_fmt = SAMPLE_FMT_S16;
    }
  }

David Schleef's avatar
David Schleef committed
697 698 699 700
  gst_structure_get_int (structure, "channels", &context->channels);
  gst_structure_get_int (structure, "rate", &context->sample_rate);
  gst_structure_get_int (structure, "block_align", &context->block_align);
  gst_structure_get_int (structure, "bitrate", &context->bit_rate);
701

702
  gst_ffmpeg_caps_to_extradata (caps, context);
703 704
}

705

706 707 708 709 710 711 712
/* Convert a GstCaps (video/raw) to a FFMPEG PixFmt
 * and other video properties in a AVCodecContext.
 *
 * For usefullness, see below
 */

static void
David Schleef's avatar
David Schleef committed
713
gst_ffmpeg_caps_to_pixfmt (const GstCaps *caps,
714 715
                           AVCodecContext *context)
{
David Schleef's avatar
David Schleef committed
716 717 718 719 720 721 722 723
  GstStructure *structure;
  gdouble fps;
  
  g_return_if_fail (gst_caps_get_size (caps) == 1);
  structure = gst_caps_get_structure (caps, 0);
  
  gst_structure_get_int (structure, "width", &context->width);
  gst_structure_get_int (structure, "height", &context->height);
724

David Schleef's avatar
David Schleef committed
725
  if (gst_structure_get_double (structure, "framerate", &fps)) {
726 727
    context->frame_rate = fps * DEFAULT_FRAME_RATE_BASE;
    context->frame_rate_base = DEFAULT_FRAME_RATE_BASE;
728 729
  }

David Schleef's avatar
David Schleef committed
730 731 732 733
  if (strcmp (gst_structure_get_name (structure), "video/x-raw-yuv") == 0) {
    guint32 fourcc;
    
    if (gst_structure_get_fourcc (structure, "format", &fourcc)) {
Benjamin Otte's avatar
Benjamin Otte committed
734 735 736 737 738 739 740 741 742 743
      switch (fourcc) {
	case GST_MAKE_FOURCC ('Y','U','Y','2'):
	  context->pix_fmt = PIX_FMT_YUV422;
	  break;
	case GST_MAKE_FOURCC ('I','4','2','0'):
	  context->pix_fmt = PIX_FMT_YUV420P;
	  break;
	case GST_MAKE_FOURCC ('Y','4','1','B'):
	  context->pix_fmt = PIX_FMT_YUV411P;
	  break;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
744 745 746
	case GST_MAKE_FOURCC ('Y','4','2','B'):
	  context->pix_fmt = PIX_FMT_YUV422P;
	  break;
Benjamin Otte's avatar
Benjamin Otte committed
747 748 749
	case GST_MAKE_FOURCC ('Y','U','V','9'):
	  context->pix_fmt = PIX_FMT_YUV410P;
	  break;
750
#if 0
Benjamin Otte's avatar
Benjamin Otte committed
751 752 753
	case FIXME:
	  context->pix_fmt = PIX_FMT_YUV444P;
	  break;
754
#endif
Benjamin Otte's avatar
Benjamin Otte committed
755 756
      }
    }
David Schleef's avatar
David Schleef committed
757 758 759 760 761 762
  } else if (strcmp (gst_structure_get_name (structure), "video/x-raw-rgb") == 0) {
    gint bpp = 0, rmask = 0, endianness = 0;
    
    if (gst_structure_get_int (structure, "bpp", &bpp) &&
	gst_structure_get_int (structure, "endianness", &endianness) &&
	gst_structure_get_int (structure, "red_mask", &rmask)) {
Benjamin Otte's avatar
Benjamin Otte committed
763 764
      switch (bpp) {
        case 32:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
765
#if (G_BYTE_ORDER == G_BIG_ENDIAN)
766
          if (rmask == 0x00ff0000)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
767
#else
768
          if (rmask == 0x0000ff00)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
769 770
#endif
            context->pix_fmt = PIX_FMT_RGBA32;
Benjamin Otte's avatar
Benjamin Otte committed
771 772
          break;
        case 24:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
773 774 775 776
          if (rmask == 0x0000FF)
            context->pix_fmt = PIX_FMT_BGR24;
          else
            context->pix_fmt = PIX_FMT_RGB24;
Benjamin Otte's avatar
Benjamin Otte committed
777 778
	  break;
        case 16:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
779 780
          if (endianness == G_BYTE_ORDER)
            context->pix_fmt = PIX_FMT_RGB565;
Benjamin Otte's avatar
Benjamin Otte committed
781 782
	  break;
        case 15:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
783 784
          if (endianness == G_BYTE_ORDER)
            context->pix_fmt = PIX_FMT_RGB555;
Benjamin Otte's avatar
Benjamin Otte committed
785 786 787 788 789
	  break;
        default:
          /* nothing */
	  break;
      }
790 791 792 793 794 795 796 797 798 799 800 801 802
    }
  }
}

/* Convert a GstCaps and a FFMPEG codec Type to a
 * AVCodecContext. If the context is ommitted, no fixed values
 * for video/audio size will be included in the context
 *
 * CodecType is primarily meant for uncompressed data GstCaps!
 */

void
gst_ffmpeg_caps_to_codectype (enum CodecType  type,
David Schleef's avatar
David Schleef committed
803
                              const GstCaps *caps,
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
                              AVCodecContext *context)
{
  if (context == NULL)
    return;

  switch (type) {
    case CODEC_TYPE_VIDEO:
      gst_ffmpeg_caps_to_pixfmt (caps, context);
      break;

    case CODEC_TYPE_AUDIO:
      gst_ffmpeg_caps_to_smpfmt (caps, context);
      break;

    default:
      /* unknown */
      break;
  }
}
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839

/* _formatid_to_caps () is meant for muxers/demuxers, it
 * transforms a name (ffmpeg way of ID'ing these, why don't
 * they have unique numerical IDs?) to the corresponding
 * caps belonging to that mux-format
 *
 * Note: we don't need any additional info because the caps
 * isn't supposed to contain any useful info besides the
 * media type anyway
 */

GstCaps *
gst_ffmpeg_formatid_to_caps (const gchar *format_name)
{
  GstCaps *caps = NULL;

  if (!strcmp (format_name, "mpeg")) {
David Schleef's avatar
David Schleef committed
840 841 842
    caps = gst_caps_new_simple ("video/mpeg",
	"systemstream", G_TYPE_BOOLEAN, TRUE,
        NULL);
843
  } else if (!strcmp (format_name, "mpegts")) {
David Schleef's avatar
David Schleef committed
844 845 846
    caps = gst_caps_new_simple ("video/mpegts",
	"systemstream", G_TYPE_BOOLEAN, TRUE,
        NULL);
847
  } else if (!strcmp (format_name, "rm")) {
David Schleef's avatar
David Schleef committed
848 849 850
    caps = gst_caps_new_simple ("ffmpeg_rm", "audio/x-pn-realvideo",
	"systemstream", G_TYPE_BOOLEAN, TRUE,
        NULL);
851
  } else if (!strcmp (format_name, "asf")) {
David Schleef's avatar
David Schleef committed
852 853
    caps = gst_caps_new_simple ("video/x-ms-asf",
	NULL);
854
  } else if (!strcmp (format_name, "avi")) {
David Schleef's avatar
David Schleef committed
855 856
    caps = gst_caps_new_simple ("video/x-msvideo",
	NULL);
857
  } else if (!strcmp (format_name, "wav")) {
David Schleef's avatar
David Schleef committed
858 859
    caps = gst_caps_new_simple ("video/x-wav",
	NULL);
860
  } else if (!strcmp (format_name, "swf")) {
David Schleef's avatar
David Schleef committed
861 862
    caps = gst_caps_new_simple ("application/x-shockwave-flash",
	NULL);
863
  } else if (!strcmp (format_name, "au")) {
David Schleef's avatar
David Schleef committed
864 865
    caps = gst_caps_new_simple ("audio/x-au",
	NULL);
866
  } else if (!strcmp (format_name, "mov")) {
David Schleef's avatar
David Schleef committed
867 868
    caps = gst_caps_new_simple ("video/quicktime",
	NULL);
869
  } else if (!strcmp (format_name, "dv")) {
David Schleef's avatar
David Schleef committed
870 871 872
    caps = gst_caps_new_simple ("video/x-dv",
	"systemstream", G_TYPE_BOOLEAN, TRUE,
	NULL);
873
  } else if (!strcmp (format_name, "4xm")) {
David Schleef's avatar
David Schleef committed
874 875
    caps = gst_caps_new_simple ("video/x-4xm",
	NULL);
876 877 878 879 880 881
  } else {
    /* unknown! */
  }

  return caps;
}
882 883 884 885 886 887 888 889 890

/* Convert a GstCaps to a FFMPEG codec ID. Size et all
 * are omitted, that can be queried by the user itself,
 * we're not eating the GstCaps or anything
 * A pointer to an allocated context is also needed for
 * optional extra info (not used yet, though)
 */

enum CodecID
David Schleef's avatar
David Schleef committed
891
gst_ffmpeg_caps_to_codecid (const GstCaps *caps,
892 893 894 895
                            AVCodecContext *context)
{
  enum CodecID id = CODEC_ID_NONE;
  const gchar *mimetype;
David Schleef's avatar
David Schleef committed
896
  const GstStructure *structure;
897 898 899
  gboolean video = FALSE, audio = FALSE; /* we want to be sure! */

  g_return_val_if_fail (caps != NULL, CODEC_ID_NONE);
David Schleef's avatar
David Schleef committed
900 901 902 903
  g_return_val_if_fail (gst_caps_get_size (caps) == 1, CODEC_ID_NONE);
  structure = gst_caps_get_structure (caps, 0);
  
  mimetype = gst_structure_get_name (structure);
904

Ronald S. Bultje's avatar
Ronald S. Bultje committed
905 906 907 908 909 910 911 912
  if (!strcmp (mimetype, "video/x-raw-rgb")) {

    id = CODEC_ID_RAWVIDEO;

    if (context != NULL) {
      gint bpp = 0, endianness = 0, rmask = 0;
      enum PixelFormat pix_fmt = -1;

David Schleef's avatar
David Schleef committed
913 914 915
      gst_structure_get_int (structure, "bpp",        &bpp);
      gst_structure_get_int (structure, "endianness", &endianness);
      gst_structure_get_int (structure, "rmask",      &rmask);
Ronald S. Bultje's avatar
Ronald S. Bultje committed
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
  
      switch (bpp) {
        case 15:
          if (endianness == G_BYTE_ORDER) {
            pix_fmt = PIX_FMT_RGB555;
          }
          break;
        case 16:
          if (endianness == G_BYTE_ORDER) {
            pix_fmt = PIX_FMT_RGB565;
          }
          break;
        case 24:
          if (rmask == 0xff0000) {
            pix_fmt = PIX_FMT_RGB24;
          } else {
            pix_fmt = PIX_FMT_BGR24;
          }
          break;
        case 32:
#if (G_BYTE_ORDER == G_BIG_ENDIAN)
937
          if (rmask == 0x00ff0000) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
938
#else
939
          if (rmask == 0x0000ff00) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
#endif
            pix_fmt = PIX_FMT_RGBA32;
          }
          break;
        default:
          /* ... */
          break;
      }

      /* only set if actually recognized! */
      if (pix_fmt != -1) {
        video = TRUE;
        context->pix_fmt = pix_fmt;
      } else {
        id = CODEC_ID_NONE;
      }
    }

  } else if (!strcmp (mimetype, "video/x-raw-yuv")) {
959 960 961 962 963 964 965

    id = CODEC_ID_RAWVIDEO;

    if (context != NULL) {
      guint32 fmt_fcc = 0;
      enum PixelFormat pix_fmt = -1;

David Schleef's avatar
David Schleef committed
966
      gst_structure_get_fourcc (structure, "format", &fmt_fcc);
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002

      switch (fmt_fcc) {
        case GST_MAKE_FOURCC ('Y','U','Y','2'):
          pix_fmt = PIX_FMT_YUV422;
          break;
        case GST_MAKE_FOURCC ('I','4','2','0'):
          pix_fmt = PIX_FMT_YUV420P;
          break;
        case GST_MAKE_FOURCC ('Y','4','1','B'):
          pix_fmt = PIX_FMT_YUV411P;
          break;
        case GST_MAKE_FOURCC ('Y','4','2','B'):
          pix_fmt = PIX_FMT_YUV422P;
          break;
        case GST_MAKE_FOURCC ('Y','U','V','9'):
          pix_fmt = PIX_FMT_YUV410P;
          break;
        default:
          /* ... */
          break;
      }

      /* only set if actually recognized! */
      if (pix_fmt != -1) {
        video = TRUE;
        context->pix_fmt = pix_fmt;
      } else {
        id = CODEC_ID_NONE;
      }
    }

  } else if (!strcmp (mimetype, "audio/x-raw-int")) {

    gint depth = 0, width = 0, endianness = 0;
    gboolean signedness = FALSE; /* bla default value */

David Schleef's avatar
David Schleef committed
1003 1004 1005 1006
    gst_structure_get_int (structure, "endianness", &endianness);
    gst_structure_get_boolean (structure, "signed", &signedness);
    gst_structure_get_int (structure, "width", &width);
    gst_structure_get_int (structure, "depth", &depth);
1007

David Schleef's avatar
David Schleef committed
1008 1009 1010 1011 1012 1013
    if (context) {
      context->sample_rate = 0;
      context->channels = 0;
      gst_structure_get_int (structure, "channels", &context->channels);
      gst_structure_get_int (structure, "rate", &context->sample_rate);
    }
1014

David Schleef's avatar
David Schleef committed
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
    if (depth == width) {
      switch (depth) {
	case 8:
	  if (signedness) {
	    id = CODEC_ID_PCM_S8;
	  } else {
	    id = CODEC_ID_PCM_U8;
	  }
	  break;
	case 16:
	  switch (endianness) {
	    case G_BIG_ENDIAN:
	      if (signedness) {
		id = CODEC_ID_PCM_S16BE;
	      } else {
		id = CODEC_ID_PCM_U16BE;
	      }
	      break;
	    case G_LITTLE_ENDIAN:
	      if (signedness) {
		id = CODEC_ID_PCM_S16LE;
	      } else {
		id = CODEC_ID_PCM_U16LE;
	      }
	      break;
	  }
	  break;
      }
1043

David Schleef's avatar
David Schleef committed
1044 1045
      if (id != CODEC_ID_NONE) {
	audio = TRUE;
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
      }
    }

  } else if (!strcmp(mimetype, "audio/x-mulaw")) {

    id = CODEC_ID_PCM_MULAW;
    audio = TRUE;

  } else if (!strcmp(mimetype, "audio/x-alaw")) {

    id = CODEC_ID_PCM_ALAW;
    audio = TRUE;

  } else if (!strcmp(mimetype, "video/x-dv")) {

    id = CODEC_ID_DVVIDEO;
    video = TRUE;

  } else if (!strcmp(mimetype, "audio/x-dv")) { /* ??? */

    id = CODEC_ID_DVAUDIO;
    audio = TRUE;

  } else if (!strcmp(mimetype, "video/x-h263")) {

    id = CODEC_ID_H263; /* or H263[IP] */
    video = TRUE;

  } else if (!strcmp(mimetype, "video/mpeg")) {

    gboolean sys_strm = TRUE;
    gint mpegversion = 0;
David Schleef's avatar
David Schleef committed
1078 1079 1080
    gst_structure_get_boolean (structure, "systemstream", &sys_strm);
    gst_structure_get_int (structure, "mpegversion", &mpegversion);
    if (!sys_strm) {
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
      switch (mpegversion) {
        case 1:
          id = CODEC_ID_MPEG1VIDEO;
          break;
        case 4:
          id = CODEC_ID_MPEG4;
          break;
        default:
          /* ... */
          break;
      }
    }

    if (id != CODEC_ID_NONE) {
      video = TRUE;
    }

  } else if (!strcmp(mimetype, "video/x-jpeg")) {

    id = CODEC_ID_MJPEG; /* A... B... */
    video = TRUE;

  } else if (!strcmp(mimetype, "video/x-wmv")) {
David Schleef's avatar
David Schleef committed
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
    gint wmvversion = 0;

    gst_structure_get_int (structure, "wmvversion", &wmvversion);

    switch (wmvversion) {
      case 1:
	id = CODEC_ID_WMV1;
	break;
      case 2:
	id = CODEC_ID_WMV2;
	break;
      default:
	/* ... */
	break;
    }
1119

David Schleef's avatar
David Schleef committed
1120 1121
    if (id != CODEC_ID_NONE) {
      video = TRUE;
1122 1123 1124 1125 1126 1127 1128
    }

  } else if (!strcmp(mimetype, "application/ogg")) {

    id = CODEC_ID_VORBIS;

  } else if (!strcmp(mimetype, "audio/mpeg")) {
David Schleef's avatar
David Schleef committed
1129 1130
    gint layer = 0;
    gint mpegversion = 0;
1131

David Schleef's avatar
David Schleef committed
1132
    if (gst_structure_get_int (structure, "mpegversion", &mpegversion)) {
1133 1134 1135 1136
      switch (mpegversion) {
        case 2: /* ffmpeg uses faad for both... */
        case 4:
          id = CODEC_ID_MPEG4AAC;
1137
          break;
1138
        case 1:
David Schleef's avatar
David Schleef committed
1139
          if (gst_structure_get_int (structure, "layer", &layer)) {
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
            switch (layer) {
              case 1:
              case 2:
                id = CODEC_ID_MP2;
                break;
              case 3:
                id = CODEC_ID_MP3;
                break;
              default:
                /* ... */
                break;
            }
          }
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
        default:
          /* ... */
          break;
      }
    }

    if (id != CODEC_ID_NONE) {
      audio = TRUE;
    }

  } else if (!strcmp(mimetype, "audio/x-wma")) {
David Schleef's avatar
David Schleef committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
    gint wmaversion = 0;

    gst_structure_get_int (structure, "wmaversion", &wmaversion);

    switch (wmaversion) {
      case 1:
	id = CODEC_ID_WMAV1;
	break;
      case 2:
	id = CODEC_ID_WMAV2;
	break;
      default:
	/* ... */
	break;
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
    }

    if (id != CODEC_ID_NONE) {
      audio = TRUE;
    }

  } else if (!strcmp(mimetype, "audio/x-ac3")) {

    id = CODEC_ID_AC3;

  } else if (!strcmp(mimetype, "video/x-msmpeg")) {
David Schleef's avatar
David Schleef committed
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
    gint msmpegversion = 0;

    gst_structure_get_int (structure, "msmpegversion", &msmpegversion);

    switch (msmpegversion) {
      case 41:
	id = CODEC_ID_MSMPEG4V1;
	break;
      case 42:
	id = CODEC_ID_MSMPEG4V2;
	break;
      case 43:
	id = CODEC_ID_MSMPEG4V3;
	break;
      default:
	/* ... */
	break;
1206 1207 1208 1209 1210 1211 1212
    }

    if (id != CODEC_ID_NONE) {
      video = TRUE;
    }

  } else if (!strcmp(mimetype, "video/x-svq")) {
David Schleef's avatar
David Schleef committed
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
    gint svqversion = 0;

    gst_structure_get_int (structure, "svqversion", &svqversion);

    switch (svqversion) {
      case 1:
	id = CODEC_ID_SVQ1;
	break;
      case 3:
	id = CODEC_ID_SVQ3;
	break;
      default:
	/* ... */
	break;
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
    }

    if (id != CODEC_ID_NONE) {
      video = TRUE;
    }

  } else if (!strcmp (mimetype, "video/x-huffyuv")) {

    id = CODEC_ID_HUFFYUV;
    video = TRUE;

  } else if (!strcmp (mimetype, "audio/x-mace")) {
David Schleef's avatar
David Schleef committed
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
    gint maceversion = 0;

    gst_structure_get_int (structure, "maceversion", &maceversion);
    switch (maceversion) {
      case 3:
	id = CODEC_ID_MACE3;
	break;
      case 6:
	id = CODEC_ID_MACE6;
	break;
      default:
	/* ... */
	break;
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    }

    if (id != CODEC_ID_NONE) {
      audio = TRUE;
    }

  } else if (!strcmp (mimetype, "video/x-theora") ||
             !strcmp (mimetype, "video/x-vp3")) {

    id = CODEC_ID_VP3;
    video = TRUE;

  } else if (!strcmp (mimetype, "video/x-indeo")) {
David Schleef's avatar
David Schleef committed
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
    gint indeoversion = 0;

    gst_structure_get_int (structure, "indeoversion", &indeoversion);
    switch (indeoversion) {
      case 3:
	id = CODEC_ID_INDEO3;
	break;
      default:
	/* ... */
	break;
1275 1276 1277 1278 1279 1280 1281
    }

    if (id != CODEC_ID_NONE) {
      video = TRUE;
    }

  } else if (!strcmp (mimetype, "video/x-divx")) {
David Schleef's avatar
David Schleef committed
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
    gint divxversion = 0;

    gst_structure_get_int (structure, "divxversion", &divxversion);
    switch (divxversion) {
      case 3:
	id = CODEC_ID_MSMPEG4V3;
	break;
      case 4:
      case 5:
	id = CODEC_ID_MPEG4;
	break;
      default:
	/* ... */
	break;
1296 1297 1298 1299 1300 1301
    }

    if (id != CODEC_ID_NONE) {
      video = TRUE;
    }

1302
  } else if (!strcmp (mimetype, "video/x-3ivx")) {
1303 1304 1305 1306

    id = CODEC_ID_MPEG4;
    video = TRUE;

1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
    if (context) {
      context->codec_tag = GST_MAKE_FOURCC ('3','I','V','X');
    }

  } else if (!strcmp (mimetype, "video/x-xvid")) {

    id = CODEC_ID_MPEG4;
    video = TRUE;

    if (context) {
      context->codec_tag = GST_MAKE_FOURCC ('X','V','I','D');
    }

1320
  } else if (!strcmp (mimetype, "video/x-ffv")) {
David Schleef's avatar
David Schleef committed
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
    gint ffvversion = 0;

    gst_structure_get_int (structure, "ffvversion", &ffvversion);
    switch (ffvversion) {
      case 1:
	id = CODEC_ID_FFV1;
	break;
      default:
	/* ... */
	break;
1331 1332 1333 1334 1335 1336 1337
    }

    if (id != CODEC_ID_NONE) {
      video = TRUE;
    }

  } else if (!strcmp (mimetype, "x-adpcm")) {
David Schleef's avatar
David Schleef committed
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
    const gchar *layout;

    layout = gst_structure_get_string (structure, "layout");
    if (layout == NULL) {
      /* break */
    } else if (!strcmp (layout, "quicktime")) {
      id = CODEC_ID_ADPCM_IMA_QT;
    } else if (!strcmp (layout, "microsoft")) {
      id = CODEC_ID_ADPCM_MS;
    } else if (!strcmp (layout, "wav")) {
      id = CODEC_ID_ADPCM_IMA_WAV;
    } else if (!strcmp (layout, "4xm")) {
      id = CODEC_ID_ADPCM_4XM;
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
    }

    if (id != CODEC_ID_NONE) {
      audio = TRUE;
    }
    
  } else if (!strcmp (mimetype, "video/x-4xm")) {

    id = CODEC_ID_4XM;
    video = TRUE;

  }

  /* TODO: realvideo/audio (well, we can't write them anyway) */

  if (context != NULL) {
    if (video == TRUE) {
      gst_ffmpeg_caps_to_pixfmt (caps, context);
      context->codec_type = CODEC_TYPE_VIDEO;
    } else if (audio == TRUE) {
      gst_ffmpeg_caps_to_smpfmt (caps, context);
      context->codec_type = CODEC_TYPE_AUDIO;
    }

    context->codec_id = id;
  }

  if (id != CODEC_ID_NONE) {
David Schleef's avatar
David Schleef committed
1379 1380
    char *str = gst_caps_to_string (caps);
    GST_DEBUG ("The id=%d belongs to the caps %s", id, str);
1381 1382 1383 1384 1385
    g_free(str);
  }

  return id;
}