gstffmpegcodecmap.c 45.9 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
#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.
39 40 41
 *
 * We should actually do this stuff at the end, like in riff-media.c,
 * but I'm too lazy today. Maybe later.
42 43
 */

David Schleef's avatar
David Schleef committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57
#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)
58 59 60 61

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

David Schleef's avatar
David Schleef committed
62 63 64 65 66 67 68 69 70
#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)
71 72 73 74 75 76

/* 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!
77 78 79 80 81 82 83 84 85
 *
 * encode is a special parameter. gstffmpegdec will say
 * FALSE, gstffmpegenc will say TRUE. The output caps
 * depends on this, in such a way that it will be very
 * specific, defined, fixed and correct caps for encoders,
 * yet very wide, "forgiving" caps for decoders. Example
 * for mp3: decode: audio/mpeg,mpegversion=1,layer=[1-3]
 * but encode: audio/mpeg,mpegversion=1,layer=3,bitrate=x,
 * rate=x,channels=x.
86 87
 */

88
GstCaps *
89
gst_ffmpeg_codecid_to_caps (enum CodecID    codec_id,
90 91
                            AVCodecContext *context,
			    gboolean        encode)
92
{
93
  GstCaps *caps = NULL;
94
  gboolean buildcaps = FALSE;
95

96
  switch (codec_id) {
97
    case CODEC_ID_MPEG1VIDEO:
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
      /* For decoding, CODEC_ID_MPEG2VIDEO is preferred... So omit here */
      if (encode) {
        /* FIXME: bitrate */
        caps = GST_FF_VID_CAPS_NEW ("video/mpeg",
	  "mpegversion",  G_TYPE_INT,     1,
	  "systemstream", G_TYPE_BOOLEAN, FALSE,
	  NULL);
      }
      break;

    case CODEC_ID_MPEG2VIDEO:
      if (encode) {
        /* FIXME: bitrate */
        caps = GST_FF_VID_CAPS_NEW ("video/mpeg",
	  "mpegversion",  G_TYPE_INT,     2,
	  "systemstream", G_TYPE_BOOLEAN, FALSE,
	  NULL);
      } else {
        /* decode both MPEG-1 and MPEG-2; width/height/fps are all in
         * the MPEG video stream headers, so may be omitted from caps. */
        caps = gst_caps_new_simple ("video/mpeg",
	  "mpegversion",  GST_TYPE_INT_RANGE, 1, 2,
	  "systemstream", G_TYPE_BOOLEAN,     FALSE,
	  NULL);
      }
123
      break;
124 125 126 127 128 129 130

    case CODEC_ID_MPEG2VIDEO_XVMC:
      /* this is a special ID - don't need it in GStreamer, I think */
      break;

    /* I don't know the exact differences between those... Anyone? */
    case CODEC_ID_H263:
131 132
    case CODEC_ID_H263P:
    case CODEC_ID_H263I:
David Schleef's avatar
David Schleef committed
133
      caps = GST_FF_VID_CAPS_NEW ("video/x-h263");
134
      break;
135

136
    case CODEC_ID_RV10:
137 138 139 140 141 142
    case CODEC_ID_RV20:
      do {
        gint version = (codec_id == CODEC_ID_RV10) ? 1 : 2;

        /* FIXME: context->sub_id must be filled in during decoding */
        caps = GST_FF_VID_CAPS_NEW ("video/x-pn-realvideo",
David Schleef's avatar
David Schleef committed
143
	  "systemstream", G_TYPE_BOOLEAN, FALSE,
144
	  "rmversion",    G_TYPE_INT,     version,
David Schleef's avatar
David Schleef committed
145
	  NULL);
146
      } while (0);
147
      break;
148

149
    case CODEC_ID_MP2:
150 151 152 153
      /* we use CODEC_ID_MP3 for decoding */
      if (encode) {
        /* FIXME: bitrate */
        caps = GST_FF_AUD_CAPS_NEW ("audio/mpeg",
David Schleef's avatar
David Schleef committed
154
	  "mpegversion", G_TYPE_INT, 1,
155 156 157
	  "layer",       G_TYPE_INT, 2,
	  NULL);
      }
158
      break;
159

160 161 162 163
    case CODEC_ID_MP3:
      if (encode) {
        /* FIXME: bitrate */
        caps = GST_FF_AUD_CAPS_NEW ("audio/mpeg",
David Schleef's avatar
David Schleef committed
164
	  "mpegversion", G_TYPE_INT, 1,
165 166 167 168 169 170 171 172
	  "layer",       G_TYPE_INT, 3,
	  NULL);
      } else {
        /* Decodes MPEG-1 layer 1/2/3. Samplerate, channels et al are
         * in the MPEG audio header, so may be omitted from caps. */
        caps = gst_caps_new_simple ("audio/mpeg",
	  "mpegversion", G_TYPE_INT,         1,
	  "layer",       GST_TYPE_INT_RANGE, 1, 3,
David Schleef's avatar
David Schleef committed
173
	  NULL);
174
      }
175
      break;
176

David Schleef's avatar
David Schleef committed
177
    case CODEC_ID_VORBIS:
178 179 180 181 182 183 184
      /* This one is disabled for several reasons:
       * - GStreamer already has perfect Ogg and Vorbis support
       * - The ffmpeg implementation depends on libvorbis/libogg,
       *   which are not included in the ffmpeg that GStreamer ships.
       * - The ffmpeg implementation depends on shared objects between
       *   the ogg demuxer and vorbis decoder, which GStreamer doesn't.
       */
185
      break;
186

187
    case CODEC_ID_AC3:
188 189 190 191 192 193 194 195
      /* Decoding is disabled, because:
       * - it depends on liba52, which we don't ship in ffmpeg.
       * - we already have a liba52 plugin ourselves.
       */
      if (encode) {
        /* FIXME: bitrate */
        caps = GST_FF_AUD_CAPS_NEW ("audio/x-ac3");
      }
196
      break;
197

198 199 200 201 202
    /* MJPEG is normal JPEG, Motion-JPEG and Quicktime MJPEG-A. MJPEGB
     * is Quicktime's MJPEG-B. LJPEG is lossless JPEG. I don't know what
     * sp5x is, but it's apparently something JPEG... We don't separate
     * between those in GStreamer. Should we (at least between MJPEG,
     * MJPEG-B and sp5x decoding...)? */
203
    case CODEC_ID_MJPEG:
Wim Taymans's avatar
Wim Taymans committed
204
    case CODEC_ID_MJPEGB:
205 206
    case CODEC_ID_LJPEG:
    case CODEC_ID_SP5X:
David Schleef's avatar
David Schleef committed
207
      caps = GST_FF_VID_CAPS_NEW ("video/x-jpeg");
Wim Taymans's avatar
Wim Taymans committed
208
      break;
209

210
    case CODEC_ID_MPEG4:
211 212 213 214 215 216 217 218 219 220 221 222 223
      if (encode) {
        /* I'm not exactly sure what ffmpeg outputs... ffmpeg itself uses
         * the AVI fourcc 'DIVX', but 'mp4v' for Quicktime... */
        /* FIXME: bitrate */
        caps = GST_FF_VID_CAPS_NEW ("video/mpeg",
	  "systemstream", G_TYPE_BOOLEAN, FALSE,
	  "mpegversion",  G_TYPE_INT,     4,
	  NULL);
      } else {
        /* The trick here is to separate xvid, divx, mpeg4, 3ivx et al */
        caps = GST_FF_VID_CAPS_NEW ("video/mpeg",
	  "mpegversion",  G_TYPE_INT,     4,
	  "systemstream", G_TYPE_BOOLEAN, FALSE,
David Schleef's avatar
David Schleef committed
224
	  NULL);
225 226 227 228 229 230
        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"));
      }
231
      break;
232

233
    case CODEC_ID_RAWVIDEO:
234
      caps = gst_ffmpeg_codectype_to_caps (CODEC_TYPE_VIDEO, context);
235
      break;
236

237
    case CODEC_ID_MSMPEG4V1:
238 239
    case CODEC_ID_MSMPEG4V2:
    case CODEC_ID_MSMPEG4V3:
240 241 242 243 244 245
      do {
        gint version = 41 + codec_id - CODEC_ID_MSMPEG4V1;

        /* encode-FIXME: bitrate */
        caps = GST_FF_VID_CAPS_NEW ("video/x-msmpeg",
	  "msmpegversion", G_TYPE_INT, version,
David Schleef's avatar
David Schleef committed
246
	  NULL);
247 248 249 250 251 252
        if (!encode && codec_id == CODEC_ID_MSMPEG4V3) {
          gst_caps_append (caps, GST_FF_VID_CAPS_NEW ("video/x-divx",
				   "divxversion", G_TYPE_INT, 3,
				   NULL));
        }
      } while (0);
253
      break;
254

255
    case CODEC_ID_WMV1:
256 257 258 259 260 261 262 263 264
    case CODEC_ID_WMV2:
      do {
        gint version = (codec_id == CODEC_ID_WMV1) ? 1 : 2;

        /* encode-FIXME: bitrate */
        caps = GST_FF_VID_CAPS_NEW ("video/x-wmv",
	  "wmvversion", G_TYPE_INT, version,
	  NULL);
      } while (0);
265
      break;
266

267 268
    case CODEC_ID_FLV1:
      buildcaps = TRUE;
269
      break;
270

271
    case CODEC_ID_SVQ1:
David Schleef's avatar
David Schleef committed
272
      caps = GST_FF_VID_CAPS_NEW ("video/x-svq",
273 274
	"svqversion", G_TYPE_INT, 1,
	NULL);
275
      break;
276 277

    case CODEC_ID_SVQ3:
David Schleef's avatar
David Schleef committed
278
      caps = GST_FF_VID_CAPS_NEW ("video/x-svq",
279 280 281 282 283 284
	"svqversion",        G_TYPE_INT,          3,
	"halfpel_flag",      GST_TYPE_INT_RANGE,  0, 1,
	"thirdpel_flag",     GST_TYPE_INT_RANGE,  0, 1,
	"low_delay",         GST_TYPE_INT_RANGE,  0, 1,
	"unknown_svq3_flag", GST_TYPE_INT_RANGE,  0, 1,
	NULL);
285
      break;
286

287
    case CODEC_ID_DVAUDIO:
288 289
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-dv");
      break;
290

291
    case CODEC_ID_DVVIDEO:
David Schleef's avatar
David Schleef committed
292
      caps = GST_FF_VID_CAPS_NEW ("video/dv");
293
      break;
294

295 296
    case CODEC_ID_WMAV1:
    case CODEC_ID_WMAV2:
297 298 299 300 301 302 303 304 305
      do {
        gint version = (codec_id == CODEC_ID_WMAV1) ? 1 : 2;

        caps = GST_FF_AUD_CAPS_NEW ("audio/x-wma",
	  "wmaversion",  G_TYPE_INT,          version,
	  "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,
David Schleef's avatar
David Schleef committed
306
	  NULL);
307
      } while (0);
308
      break;
309

310 311
    case CODEC_ID_MACE3:
    case CODEC_ID_MACE6:
312 313 314 315 316 317 318
      do {
        gint version = (codec_id == CODEC_ID_MACE3) ? 3 : 6;

        caps = GST_FF_AUD_CAPS_NEW ("audio/x-mace",
	  "maceversion", G_TYPE_INT, version,
	  NULL);
      } while (0);
319
      break;
320

Wim Taymans's avatar
Wim Taymans committed
321
    case CODEC_ID_HUFFYUV:
David Schleef's avatar
David Schleef committed
322
      caps = GST_FF_VID_CAPS_NEW ("video/x-huffyuv");
323 324 325
      break;

    case CODEC_ID_CYUV:
326
      buildcaps = TRUE;
327 328 329
      break;

    case CODEC_ID_H264:
David Schleef's avatar
David Schleef committed
330
      caps = GST_FF_VID_CAPS_NEW ("video/x-h264");
331 332 333
      break;

    case CODEC_ID_INDEO3:
David Schleef's avatar
David Schleef committed
334
      caps = GST_FF_VID_CAPS_NEW ("video/x-indeo",
335 336
	"indeoversion", G_TYPE_INT, 3,
	NULL);
337 338 339
      break;

    case CODEC_ID_VP3:
David Schleef's avatar
David Schleef committed
340
      caps = GST_FF_VID_CAPS_NEW ("video/x-vp3");
341 342
      break;

343 344
    case CODEC_ID_THEORA:
      caps = GST_FF_VID_CAPS_NEW ("video/x-theora");
345 346
      break;

347
    case CODEC_ID_AAC:
348
    case CODEC_ID_MPEG4AAC:
349 350 351
      /* ffmpeg uses libfaac/libfaad for those. We do not ship these as
       * part of ffmpeg, so defining those is useless. Besides, we have
       * our own faad/faac plugins. */
352 353 354
      break;

    case CODEC_ID_ASV1:
355 356
    case CODEC_ID_ASV2:
      buildcaps = TRUE;
357 358
      break;

359
    case CODEC_ID_FFV1:
David Schleef's avatar
David Schleef committed
360
      caps = GST_FF_VID_CAPS_NEW ("video/x-ffv",
361 362
	"ffvversion", G_TYPE_INT, 1,
	NULL);
363 364 365
      break;

    case CODEC_ID_4XM:
David Schleef's avatar
David Schleef committed
366
      caps = GST_FF_VID_CAPS_NEW ("video/x-4xm");
367 368
      break;

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    case CODEC_ID_VCR1:
    case CODEC_ID_CLJR:
    case CODEC_ID_MDEC:
    case CODEC_ID_ROQ:
    case CODEC_ID_INTERPLAY_VIDEO:
    case CODEC_ID_XAN_WC3:
    case CODEC_ID_XAN_WC4:
    case CODEC_ID_RPZA:
      buildcaps = TRUE;
      break;

    case CODEC_ID_CINEPAK:
      caps = GST_FF_VID_CAPS_NEW ("video/x-cinepak");
      break;

    case CODEC_ID_WS_VQA:
    case CODEC_ID_MSRLE:
    case CODEC_ID_MSVIDEO1:
    case CODEC_ID_IDCIN:
    case CODEC_ID_8BPS:
    case CODEC_ID_SMC:
    case CODEC_ID_FLIC:
    case CODEC_ID_TRUEMOTION1:
    case CODEC_ID_VMDVIDEO:
    case CODEC_ID_VMDAUDIO:
    case CODEC_ID_MSZH:
    case CODEC_ID_ZLIB:
    case CODEC_ID_QTRLE:
      buildcaps = TRUE;
      break;

400 401 402 403 404 405 406 407
    /* 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 {
408
        gint width = 0, depth = 0, endianness = 0;
409 410 411 412
	gboolean signedness = FALSE; /* blabla */

        switch (codec_id) {
          case CODEC_ID_PCM_S16LE:
413
            width = 16; depth = 16;
414 415 416 417
            endianness = G_LITTLE_ENDIAN;
            signedness = TRUE;
            break;
          case CODEC_ID_PCM_S16BE:
418
            width = 16; depth = 16;
419 420 421 422
            endianness = G_BIG_ENDIAN;
            signedness = TRUE;
            break;
          case CODEC_ID_PCM_U16LE:
423
            width = 16; depth = 16;
424 425 426 427
            endianness = G_LITTLE_ENDIAN;
            signedness = FALSE;
            break;
          case CODEC_ID_PCM_U16BE:
428
            width = 16; depth = 16;
429 430 431 432
            endianness = G_BIG_ENDIAN;
            signedness = FALSE;
            break;
          case CODEC_ID_PCM_S8:
433
            width = 8;  depth = 8;
434 435 436 437
            endianness = G_BYTE_ORDER;
            signedness = TRUE;
            break;
          case CODEC_ID_PCM_U8:
438
            width = 8;  depth = 8;
439 440 441 442 443 444 445 446
            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
447 448 449 450 451 452
        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);
453 454 455
      } while (0);
      break;

456
    case CODEC_ID_PCM_MULAW:
David Schleef's avatar
David Schleef committed
457
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-mulaw");
458 459 460
      break;

    case CODEC_ID_PCM_ALAW:
David Schleef's avatar
David Schleef committed
461
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-alaw");
462 463
      break;

464 465
    case CODEC_ID_ADPCM_IMA_QT:
    case CODEC_ID_ADPCM_IMA_WAV:
466 467 468 469
    case CODEC_ID_ADPCM_IMA_DK3:
    case CODEC_ID_ADPCM_IMA_DK4:
    case CODEC_ID_ADPCM_IMA_WS:
    case CODEC_ID_ADPCM_IMA_SMJPEG:
470
    case CODEC_ID_ADPCM_MS:
471
    case CODEC_ID_ADPCM_4XM:
472 473 474 475 476 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
    case CODEC_ID_ADPCM_XA:
    case CODEC_ID_ADPCM_ADX:
    case CODEC_ID_ADPCM_EA:
    case CODEC_ID_ADPCM_G726:
      do {
        gchar *layout = NULL;

        switch (codec_id) {
          case CODEC_ID_ADPCM_IMA_QT:
            layout = "quicktime";
            break;
          case CODEC_ID_ADPCM_IMA_WAV:
            layout = "wav";
            break;
          case CODEC_ID_ADPCM_IMA_DK3:
            layout = "dk3";
            break;
          case CODEC_ID_ADPCM_IMA_DK4:
            layout = "dk4";
            break;
          case CODEC_ID_ADPCM_IMA_WS:
            layout = "westwood";
            break;
          case CODEC_ID_ADPCM_IMA_SMJPEG:
            layout = "smjpeg";
            break;
          case CODEC_ID_ADPCM_MS:
            layout = "microsoft";
            break;
          case CODEC_ID_ADPCM_4XM:
            layout = "4xm";
            break;
          case CODEC_ID_ADPCM_XA:
            layout = "xa";
            break;
          case CODEC_ID_ADPCM_ADX:
            layout = "adx";
            break;
          case CODEC_ID_ADPCM_EA:
            layout = "ea";
            break;
          case CODEC_ID_ADPCM_G726:
            layout = "g726";
            break;
          default:
            g_assert (0); /* don't worry, we never get here */
            break;
        }

        /* FIXME: someone please check whether we need additional properties
         * in this caps definition. */
        caps = GST_FF_AUD_CAPS_NEW ("audio/x-adpcm",
	  "layout", G_TYPE_STRING, layout,
David Schleef's avatar
David Schleef committed
525
          NULL);
526
      } while (0);
527 528 529
      break;

    case CODEC_ID_AMR_NB:
530 531 532 533
    case CODEC_ID_AMR_WB:
      /* what's this? ffmpeg uses external libs here that we don't include
       * so there's no point in defining those. Still, I want to know what
       * it actually is... */
534 535
      break;

536
    case CODEC_ID_RA_144:
537
    case CODEC_ID_RA_288:
538 539 540 541 542 543 544 545
      do {
        gint version = (codec_id == CODEC_ID_RA_144) ? 1 : 2;

        /* FIXME: properties? */
        caps = GST_FF_AUD_CAPS_NEW ("audio/x-pn-realaudio",
	  "raversion", G_TYPE_INT, version,
	  NULL);
      } while (0);
546
      break;
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
    case CODEC_ID_ROQ_DPCM:
    case CODEC_ID_INTERPLAY_DPCM:
    case CODEC_ID_XAN_DPCM:
      do {
        gchar *layout = NULL;

        switch (codec_id) {
          case CODEC_ID_ROQ_DPCM:
            layout = "roq";
            break;
          case CODEC_ID_INTERPLAY_DPCM:
            layout = "interplay";
            break;
          case CODEC_ID_XAN_DPCM:
            layout = "xan";
            break;
          default:
            g_assert (0); /* don't worry, we never get here */
            break;
        }

        /* FIXME: someone please check whether we need additional properties
         * in this caps definition. */
        caps = GST_FF_AUD_CAPS_NEW ("audio/x-dpcm",
	  "layout", G_TYPE_STRING, layout,
	  NULL);
      } while (0);
      break;
    
    case CODEC_ID_FLAC:
      /* Note that ffmpeg has no encoder yet, but just for safety. In the
       * encoder case, we want to add things like samplerate, channels... */
      if (!encode) {
        caps = gst_caps_new_simple ("audio/x-flac", NULL);
      }
583 584
      break;

585
    default:
586
      g_warning ("Unknown codec ID %d, please add here", codec_id);
587 588 589
      break;
  }

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
  if (buildcaps) {
    AVCodec *codec;

    if ((codec = avcodec_find_decoder (codec_id)) ||
        (codec = avcodec_find_encoder (codec_id))) {
      gchar *mime = NULL;

      switch (codec->type) {
        case CODEC_TYPE_VIDEO:
          mime = g_strdup_printf ("video/x-gst_ff-%s", codec->name);
          caps = GST_FF_VID_CAPS_NEW (mime);
          g_free (mime);
          break;
        case CODEC_TYPE_AUDIO:
          mime = g_strdup_printf ("audio/x-gst_ff-%s", codec->name);
          caps = GST_FF_AUD_CAPS_NEW (mime);
          g_free (mime);
          break;
        default:
          break;
      }
    }
  }

614
  if (caps != NULL) {
David Schleef's avatar
David Schleef committed
615 616
    char *str = gst_caps_to_string (caps);
    GST_DEBUG ("caps for codec_id=%d: %s", codec_id, str);
617
    g_free (str);
618
  } else {
David Schleef's avatar
David Schleef committed
619
    GST_WARNING ("No caps found for codec_id=%d", codec_id);
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
  }

  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;
656 657
      endianness = G_BIG_ENDIAN;
      r_mask = 0x0000ff; g_mask = 0x00ff00; b_mask = 0xff0000;
658 659
      break;
    case PIX_FMT_YUV422P:
660
      fmt = GST_MAKE_FOURCC ('Y','4','2','B');
661 662 663 664 665
      break;
    case PIX_FMT_YUV444P:
      /* .. */
      break;
    case PIX_FMT_RGBA32:
666
      bpp = 32; depth = 24;
667 668
      endianness = G_BIG_ENDIAN;
#if (G_BYTE_ORDER == G_BIG_ENDIAN)
Benjamin Otte's avatar
Benjamin Otte committed
669
      r_mask = 0x00ff0000; g_mask = 0x0000ff00; b_mask = 0x000000ff;
670 671
#else 
      r_mask = 0x0000ff00; g_mask = 0x00ff0000; b_mask = 0xff000000;
672
#endif
673 674
      break;
    case PIX_FMT_YUV410P:
675
      fmt = GST_MAKE_FOURCC ('Y','U','V','9');
676 677
      break;
    case PIX_FMT_YUV411P:
678
      fmt = GST_MAKE_FOURCC ('Y','4','1','B');
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
      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
696 697 698 699 700 701 702 703
    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);
704
  } else if (fmt) {
David Schleef's avatar
David Schleef committed
705 706 707
    caps = GST_FF_VID_CAPS_NEW ("video/x-raw-yuv",
	"format",     GST_TYPE_FOURCC, fmt,
        NULL);
708 709 710
  }

  if (caps != NULL) {
David Schleef's avatar
David Schleef committed
711 712
    char *str = gst_caps_to_string (caps);
    GST_DEBUG ("caps for pix_fmt=%d: %s", pix_fmt, str);
713
    g_free(str);
714
  } else {
David Schleef's avatar
David Schleef committed
715
    GST_WARNING ("No caps found for pix_fmt=%d", pix_fmt);
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
  }

  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;
741
      break;
742

743
    default:
744
      /* .. */
745 746 747
      break;
  }

748
  if (bpp) {
David Schleef's avatar
David Schleef committed
749 750 751 752 753 754
    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);
755 756 757
  }

  if (caps != NULL) {
David Schleef's avatar
David Schleef committed
758 759
    char *str = gst_caps_to_string (caps);
    GST_DEBUG ("caps for sample_fmt=%d: %s", sample_fmt, str);
760
    g_free(str);
761
  } else {
David Schleef's avatar
David Schleef committed
762
    GST_WARNING ("No caps found for sample_fmt=%d", sample_fmt);
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
  }

  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
779
  GstCaps *caps;
780 781 782 783 784 785 786 787 788

  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
789
        caps = gst_caps_new_empty ();
790 791 792
        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
793
            gst_caps_append (caps, temp);
794 795 796 797 798 799 800 801 802 803 804 805
          }
        }
      }
      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
806
        caps = gst_caps_new_empty ();
807 808 809
        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
810
            gst_caps_append (caps, temp);
811 812 813 814 815 816 817
          }
        }
      }
      break;

    default:
      /* .. */
David Schleef's avatar
David Schleef committed
818
      caps = NULL;
819 820 821 822
      break;
  }

  return caps;
823
}
824

825 826 827 828 829

/* Construct the context extradata from caps
 * when needed.
 */
static void
David Schleef's avatar
David Schleef committed
830
gst_ffmpeg_caps_to_extradata (const GstCaps *caps,
831 832
                              AVCodecContext *context)
{
David Schleef's avatar
David Schleef committed
833
  GstStructure *structure;
834 835
  const gchar *mimetype;

David Schleef's avatar
David Schleef committed
836 837 838 839
  g_return_if_fail (gst_caps_get_size (caps) == 1);
  structure = gst_caps_get_structure (caps, 0);
  
  mimetype = gst_structure_get_name (structure);
840 841

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

844 845
    if (!gst_structure_get_int (structure, "flags1", &flags1) ||
	!gst_structure_get_int (structure, "flags2", &flags2) ||
David Schleef's avatar
David Schleef committed
846 847
	!gst_structure_get_int (structure, "wmaversion", &wmaversion)) {
      g_warning ("invalid caps for audio/x-wma");
848 849 850
      return;
    }

David Schleef's avatar
David Schleef committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
    /* 
     * 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;
878
    }
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914

    return;
  }

  if (!strcmp(mimetype, "video/x-svq")) {
    gint version = 0;

    if (!gst_structure_get_int (structure, "svqversion", &version)) {
      g_warning ("invalid caps for video/x-svq");
      return;
    }

    if (version == 3) {
      gint halfpel_flag, thirdpel_flag, low_delay, unknown_svq3_flag;
      guint16 flags;

      if (!gst_structure_get_int (structure, "halfpel_flag", &halfpel_flag) ||
          !gst_structure_get_int (structure, "thirdpel_flag", &thirdpel_flag) ||
	  !gst_structure_get_int (structure, "low_delay", &low_delay) ||
	  !gst_structure_get_int (structure, "unknown_svq3_flag", &unknown_svq3_flag)) {
	g_warning ("invalid caps for video/x-svq");
	return;
      }	  

      context->extradata = (guint8 *) g_malloc0 (0x64);
      g_stpcpy (context->extradata, "SVQ3");
      flags = 1 << 3;
      flags |= low_delay;
      flags = flags << 2;
      flags |= unknown_svq3_flag;
      flags = flags << 6;
      flags |= halfpel_flag;
      flags = flags << 1;
      flags |= thirdpel_flag;
      flags = flags << 3;

915
      flags = GUINT16_FROM_LE (flags);
916
	
917 918 919
      memcpy (context->extradata + 0x62, &flags, 2);
      context->extradata_size = 0x64;
    }
920 921 922 923
  }
}


924 925 926 927 928 929 930
/* 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
931
gst_ffmpeg_caps_to_smpfmt (const GstCaps *caps,
932 933
                           AVCodecContext *context)
{
David Schleef's avatar
David Schleef committed
934 935 936 937 938 939 940 941 942 943 944
  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)) {
945 946 947 948 949 950
    if (width == 16 && depth == 16 &&
        endianness == G_BYTE_ORDER && signedness == TRUE) {
      context->sample_fmt = SAMPLE_FMT_S16;
    }
  }

David Schleef's avatar
David Schleef committed
951 952 953 954
  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);
955

956
  gst_ffmpeg_caps_to_extradata (caps, context);
957 958
}

959

960 961 962 963 964 965 966
/* 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
967
gst_ffmpeg_caps_to_pixfmt (const GstCaps *caps,
968 969
                           AVCodecContext *context)
{
David Schleef's avatar
David Schleef committed
970 971 972 973 974 975 976 977
  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);
978

David Schleef's avatar
David Schleef committed
979
  if (gst_structure_get_double (structure, "framerate", &fps)) {
980 981
    context->frame_rate = fps * DEFAULT_FRAME_RATE_BASE;
    context->frame_rate_base = DEFAULT_FRAME_RATE_BASE;
982 983
  }

David Schleef's avatar
David Schleef committed
984 985 986 987
  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
988 989 990 991 992 993 994 995 996 997
      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
998 999 1000
	case GST_MAKE_FOURCC ('Y','4','2','B'):
	  context->pix_fmt = PIX_FMT_YUV422P;
	  break;
Benjamin Otte's avatar
Benjamin Otte committed
1001 1002 1003
	case GST_MAKE_FOURCC ('Y','U','V','9'):
	  context->pix_fmt = PIX_FMT_YUV410P;
	  break;
1004
#if 0
Benjamin Otte's avatar
Benjamin Otte committed
1005 1006 1007
	case FIXME:
	  context->pix_fmt = PIX_FMT_YUV444P;
	  break;
1008
#endif
Benjamin Otte's avatar
Benjamin Otte committed
1009 1010
      }
    }
David Schleef's avatar
David Schleef committed
1011 1012 1013 1014 1015 1016
  } 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
1017 1018
      switch (bpp) {
        case 32:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1019
#if (G_BYTE_ORDER == G_BIG_ENDIAN)
1020
          if (rmask == 0x00ff0000)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1021
#else
1022
          if (rmask == 0x0000ff00)
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1023 1024
#endif
            context->pix_fmt = PIX_FMT_RGBA32;
Benjamin Otte's avatar
Benjamin Otte committed
1025 1026
          break;
        case 24:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1027 1028 1029 1030
          if (rmask == 0x0000FF)
            context->pix_fmt = PIX_FMT_BGR24;
          else
            context->pix_fmt = PIX_FMT_RGB24;
Benjamin Otte's avatar
Benjamin Otte committed
1031 1032
	  break;
        case 16:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1033 1034
          if (endianness == G_BYTE_ORDER)
            context->pix_fmt = PIX_FMT_RGB565;
Benjamin Otte's avatar
Benjamin Otte committed
1035 1036
	  break;
        case 15:
Ronald S. Bultje's avatar
Ronald S. Bultje committed
1037 1038
          if (endianness == G_BYTE_ORDER)
            context->pix_fmt = PIX_FMT_RGB555;
Benjamin Otte's avatar
Benjamin Otte committed
1039 1040 1041 1042 1043
	  break;
        default:
          /* nothing */
	  break;
      }
1044 1045
    }
  }
1046 1047

  gst_ffmpeg_caps_to_extradata (caps, context);
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
}

/* 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
1059
                              const GstCaps *caps,
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
                              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;
  }
}
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095

/* _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
1096 1097 1098
    caps = gst_caps_new_simple ("video/mpeg",
	"systemstream", G_TYPE_BOOLEAN, TRUE,
        NULL);
1099
  } else if (!strcmp (format_name, "mpegts")) {
David Schleef's avatar
David Schleef committed
1100 1101 1102
    caps = gst_caps_new_simple ("video/mpegts",
	"systemstream", G_TYPE_BOOLEAN, TRUE,
        NULL);
1103
  } else if (!strcmp (format_name, "rm")) {
1104
    caps = gst_caps_new_simple ("application/x-pn-realmedia",
David Schleef's avatar
David Schleef committed
1105 1106
	"systemstream", G_TYPE_BOOLEAN, TRUE,
        NULL);
1107
  } else if (!strcmp (format_name, "asf")) {
David Schleef's avatar
David Schleef committed
1108 1109
    caps = gst_caps_new_simple ("video/x-ms-asf",
	NULL);
1110
  } else if (!strcmp (format_name, "avi")) {
David Schleef's avatar
David Schleef committed
1111 1112
    caps = gst_caps_new_simple ("video/x-msvideo",
	NULL);
1113
  } else if (!strcmp (format_name, "wav")) {
David Schleef's avatar
David Schleef committed
1114 1115
    caps = gst_caps_new_simple ("video/x-wav",
	NULL);
1116
  } else if (!strcmp (format_name, "swf")) {
David Schleef's avatar
David Schleef committed
1117 1118
    caps = gst_caps_new_simple ("application/x-shockwave-flash",
	NULL);
1119
  } else if (!strcmp (format_name, "au")) {
David Schleef's avatar
David Schleef committed
1120 1121
    caps = gst_caps_new_simple ("audio/x-au",
	NULL);
1122
  } else if (!strcmp (format_name, "mov_mp4_m4a_3gp")) {
David Schleef's avatar
David Schleef committed
1123 1124
    caps = gst_caps_new_simple ("video/quicktime",
	NULL);
1125
  } else if (!strcmp (format_name, "dv")) {
David Schleef's avatar
David Schleef committed
1126 1127 1128
    caps = gst_caps_new_simple ("video/x-dv",
	"systemstream", G_TYPE_BOOLEAN, TRUE,
	NULL);
1129
  } else if (!strcmp (format_name, "4xm")) {
David Schleef's avatar
David Schleef committed
1130 1131
    caps = gst_caps_new_simple ("video/x-4xm",
	NULL);
1132 1133 1134
  } else if (!strcmp (format_name, "matroska")) {
    caps = gst_caps_new_simple ("video/x-matroska",
	NULL);
1135
  } else {
1136 1137 1138 1139 1140 1141
    gchar *name;

    GST_WARNING ("Could not create stream format caps for %s", format_name);
    name = g_strdup_printf ("application/x-gst_ff-%s", format_name);
    caps = gst_caps_new_simple (name, NULL);
    g_free (name);
1142 1143 1144 1145
  }

  return caps;
}
1146 1147 1148 1149 1150

/* 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
1151 1152 1153
 * optional extra info
 *
 * FIXME: lots of ffmpeg decoders need more properties...
1154 1155 1156
 */

enum CodecID
David Schleef's avatar
David Schleef committed
1157
gst_ffmpeg_caps_to_codecid (const GstCaps *caps,
1158 1159 1160 1161
                            AVCodecContext *context)
{
  enum CodecID id = CODEC_ID_NONE;
  const gchar *mimetype;
David Schleef's avatar
David Schleef committed
1162
  const GstStructure *structure;
1163 1164 1165
  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
1166 1167 1168 1169
  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);
1170

Ronald S. Bultje's avatar
Ronald S. Bultje committed
1171 1172 1173 1174 1175 1176 1177 1178
  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
1179 1180 1181
      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
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198