gstffmpegcodecmap.c 39.1 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
#include "config.h"
23
#include <gst/gst.h>
24 25 26 27 28
#ifdef HAVE_FFMPEG_UNINSTALLED
#include <avcodec.h>
#else
#include <ffmpeg/avcodec.h>
#endif
29
#include <string.h>
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44
#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.
 */

#define GST_FF_VID_CAPS_NEW(name, mimetype, props...)		\
	(context != NULL) ?					\
	GST_CAPS_NEW (name,					\
		      mimetype,					\
		      "width",  GST_PROPS_INT (context->width),	\
		      "height", GST_PROPS_INT (context->height),\
45 46 47
		      "framerate", GST_PROPS_FLOAT (            \
				1.*context->frame_rate/         \
				context->frame_rate_base),      \
48 49 50 51 52 53
		      ##props)					\
	:							\
	GST_CAPS_NEW (name,					\
		      mimetype,					\
		      "width",  GST_PROPS_INT_RANGE (16, 4096),	\
		      "height", GST_PROPS_INT_RANGE (16, 4096),	\
54 55
		      "framerate", GST_PROPS_FLOAT_RANGE (0,    \
						G_MAXFLOAT),    \
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		      ##props)

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

#define GST_FF_AUD_CAPS_NEW(name, mimetype, props...)			\
	(context != NULL) ?						\
	GST_CAPS_NEW (name,						\
		      mimetype,						\
		      "rate",     GST_PROPS_INT (context->sample_rate),	\
		      "channels", GST_PROPS_INT (context->channels),	\
		      ##props)						\
	:								\
	GST_CAPS_NEW (name,						\
		      mimetype,						\
		      "rate",     GST_PROPS_INT_RANGE (8000, 96000),	\
		      "channels", GST_PROPS_INT_RANGE (1, 2),		\
		      ##props)

/* 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!
 */

82
GstCaps *
83 84
gst_ffmpeg_codecid_to_caps (enum CodecID    codec_id,
                            AVCodecContext *context)
85
{
86 87
  GstCaps *caps = NULL;

88
  switch (codec_id) {
89
    case CODEC_ID_MPEG1VIDEO:
90
      /* this caps doesn't need width/height/framerate */
91 92 93 94 95
      caps = GST_CAPS_NEW ("ffmpeg_mpeg1video",
                           "video/mpeg",
                             "mpegversion",  GST_PROPS_INT (1),
                             "systemstream", GST_PROPS_BOOLEAN (FALSE)
                          );
96
      break;
97 98 99

    case CODEC_ID_H263P:
    case CODEC_ID_H263I:
100
    case CODEC_ID_H263:
101
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_h263",
102
                                  "video/x-h263"
103
                                 );
104
      break;
105

106
    case CODEC_ID_RV10:
107
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_rv10",
108 109
                                  "video/x-pn-realvideo",
                                    "systemstream", GST_PROPS_BOOLEAN (FALSE)
110
                                 );
111
      break;
112

113
    case CODEC_ID_MP2:
114 115 116 117
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_mp2",
                                  "audio/mpeg",
                                    "layer", GST_PROPS_INT (2)
                                 );
118
      break;
119

120
    case CODEC_ID_MP3LAME:
121 122 123 124
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_mp3",
                                  "audio/mpeg",
                                    "layer", GST_PROPS_INT (3)
                                 );
125
      break;
126

127 128 129 130 131
    case CODEC_ID_VORBIS: /* FIXME? vorbis or ogg? */
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_vorbis",
		                  "application/ogg",
			            NULL
			         );
132
      break;
133
      
134
    case CODEC_ID_AC3:
135 136 137 138
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_ac3",
		                  "audio/x-ac3",
			            NULL
			         );
139
      break;
140

141
    case CODEC_ID_MJPEG:
Wim Taymans's avatar
Wim Taymans committed
142
    case CODEC_ID_MJPEGB:
143
    /*case CODEC_ID_LJPEG:*/
144
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_mjpeg",
145
                                  "video/x-jpeg"
146
                                 );
Wim Taymans's avatar
Wim Taymans committed
147
      break;
148

149
    case CODEC_ID_MPEG4:
150 151 152 153 154 155 156
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_mpeg4",
                                  "video/mpeg",
                                    "mpegversion",  GST_PROPS_INT (4),
                                    "systemstream", GST_PROPS_BOOLEAN (FALSE)
                                 );
      caps = gst_caps_append(caps,
             GST_FF_VID_CAPS_NEW ("ffmpeg_divx",
157
                                  "video/x-divx",
158
				    "divxversion",  GST_PROPS_INT (5)
159
                                 ));
160 161 162 163 164
      caps = gst_caps_append(caps,
             GST_FF_VID_CAPS_NEW ("ffmpeg_divx",
                                  "video/x-divx",
				    "divxversion",  GST_PROPS_INT (4)
                                 ));
165 166
      caps = gst_caps_append(caps,
             GST_FF_VID_CAPS_NEW ("ffmpeg_xvid",
167 168 169 170
                                  "video/x-xvid"
                                 ));
      caps = gst_caps_append(caps,
             GST_FF_VID_CAPS_NEW ("ffmpeg_3ivx",
171
                                  "video/x-3ivx"
172
                                 ));
173
      break;
174

175 176 177 178 179
    /* 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);

180
    case CODEC_ID_MSMPEG4V1:
181 182
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_msmpeg4v1",
                                  "video/x-msmpeg",
183
                                    "msmpegversion", GST_PROPS_INT (41)
184
                                 );
185
      break;
186

187
    case CODEC_ID_MSMPEG4V2:
188 189
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_msmpeg4v2",
                                  "video/x-msmpeg",
190
                                    "msmpegversion", GST_PROPS_INT (42)
191
                                 );
192
      break;
193

194
    case CODEC_ID_MSMPEG4V3:
195 196
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_msmpeg4v3",
                                  "video/x-msmpeg",
197
                                    "msmpegversion", GST_PROPS_INT (43)
198
                                 );
199 200 201 202 203
      caps = gst_caps_append(caps,
             GST_FF_VID_CAPS_NEW ("ffmpeg_msmpeg4v3_divx3",
                                  "video/x-divx",
                                    "divxversion", GST_PROPS_INT (3)
                                 ));
204
      break;
205

206
    case CODEC_ID_WMV1:
207
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_wmv1",
208
                                  "video/x-wmv",
209 210
                                    "wmvversion", GST_PROPS_INT (1)
                                 );
211
      break;
212

213
    case CODEC_ID_WMV2:
214
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_wmv2",
215
                                  "video/x-wmv",
216 217
                                    "wmvversion", GST_PROPS_INT (2)
                                 );
218
      break;
219

220
    case CODEC_ID_SVQ1:
221 222 223 224
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_svq1",
                                  "video/x-svq",
                                    "svqversion", GST_PROPS_INT (1)
                                 );
225
      break;
226 227 228 229 230 231

    case CODEC_ID_SVQ3:
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_svq3",
                                  "video/x-svq",
                                    "svqversion", GST_PROPS_INT (3)
                                 );
232
      break;
233

234 235 236
    case CODEC_ID_DVAUDIO:
        caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_dvaudio",
                                    "audio/x-dv"
237
                                   );
238
        break;
239

240 241 242 243
    case CODEC_ID_DVVIDEO:
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_dvvideo",
                                  "video/dv"
                                 );
244
      break;
245

246
    case CODEC_ID_WMAV1:
247 248 249 250
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_wma1",
                                  "audio/x-wma",
                                    "wmaversion", GST_PROPS_INT (1)
                                 );
251
      break;
252

253
    case CODEC_ID_WMAV2:
254 255 256 257
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_wma2",
                                  "audio/x-wma",
                                    "wmaversion", GST_PROPS_INT (2)
                                 );
258
      break;
259

260
    case CODEC_ID_MACE3:
261 262 263 264
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_mace3",
                                  "audio/x-mace",
                                    "maceversion", GST_PROPS_INT (3)
                                 );
265
      break;
266

267
    case CODEC_ID_MACE6:
268 269 270 271
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_mace6",
                                  "audio/x-mace",
                                    "maceversion", GST_PROPS_INT (6)
                                 );
272
      break;
273

Wim Taymans's avatar
Wim Taymans committed
274
    case CODEC_ID_HUFFYUV:
275
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_huffyuv",
276
                                  "video/x-huffyuv"
277 278 279 280 281 282 283 284 285
                                 );
      break;

    case CODEC_ID_CYUV:
      /* .. */
      break;

    case CODEC_ID_H264:
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_h264",
286
                                  "video/x-h264"
287 288 289 290 291
                                 );
      break;

    case CODEC_ID_INDEO3:
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_indeo3",
292 293
                                  "video/x-indeo",
                                    "indeoversion", GST_PROPS_INT (3)
294 295 296 297 298
                                 );
      break;

    case CODEC_ID_VP3:
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_vp3",
299
                                  "video/x-vp3"
300 301 302 303 304 305 306 307 308 309 310 311
                                 );
      caps = gst_caps_append(caps,
             GST_FF_VID_CAPS_NEW ("ffmpeg_theora",
                                  "video/x-theora"
                                 ));
      break;

    case CODEC_ID_AAC:
      /* .. */
      break;

    case CODEC_ID_MPEG4AAC:
312
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_mpeg4aac",
313
                                  "audio/mpeg",
314 315 316 317 318 319 320 321 322
                                    "systemstream", GST_PROPS_BOOLEAN (FALSE),
                                    "mpegversion",  GST_PROPS_INT (4)
                                 );
      break;

    case CODEC_ID_ASV1:
      /* .. */
      break;

323 324
    case CODEC_ID_FFV1:
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_ffv1",
325 326
				  "video/x-ffv",
                                    "ffvversion", GST_PROPS_INT (1)
327 328 329 330
				 );
      break;

    case CODEC_ID_4XM:
331
      caps = GST_FF_VID_CAPS_NEW ("ffmpeg_4xmvideo",
332
                                  "video/x-4xm"
333
                                 );
334 335 336 337 338 339 340 341 342 343
      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 {
344
        gint width = 0, depth = 0, endianness = 0;
345 346 347 348
	gboolean signedness = FALSE; /* blabla */

        switch (codec_id) {
          case CODEC_ID_PCM_S16LE:
349
            width = 16; depth = 16;
350 351 352 353
            endianness = G_LITTLE_ENDIAN;
            signedness = TRUE;
            break;
          case CODEC_ID_PCM_S16BE:
354
            width = 16; depth = 16;
355 356 357 358
            endianness = G_BIG_ENDIAN;
            signedness = TRUE;
            break;
          case CODEC_ID_PCM_U16LE:
359
            width = 16; depth = 16;
360 361 362 363
            endianness = G_LITTLE_ENDIAN;
            signedness = FALSE;
            break;
          case CODEC_ID_PCM_U16BE:
364
            width = 16; depth = 16;
365 366 367 368
            endianness = G_BIG_ENDIAN;
            signedness = FALSE;
            break;
          case CODEC_ID_PCM_S8:
369
            width = 8;  depth = 8;
370 371 372 373
            endianness = G_BYTE_ORDER;
            signedness = TRUE;
            break;
          case CODEC_ID_PCM_U8:
374
            width = 8;  depth = 8;
375 376 377 378 379 380 381 382
            endianness = G_BYTE_ORDER;
            signedness = FALSE;
            break;
          default:
            g_assert(0); /* don't worry, we never get here */
            break;
        }

383 384 385 386 387 388 389 390
        caps = GST_FF_AUD_CAPS_NEW (
                 "ffmpeg_pcmaudio",
		 "audio/x-raw-int",
		   "width",      GST_PROPS_INT (width),
		   "depth",      GST_PROPS_INT (depth),
		   "endianness", GST_PROPS_INT (endianness),
		   "signed",     GST_PROPS_BOOLEAN (signedness)
	       );
391 392 393
      } while (0);
      break;

394 395 396 397 398 399 400 401 402 403
    case CODEC_ID_PCM_MULAW:
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_mulawaudio",
                                  "audio/x-mulaw");
      break;

    case CODEC_ID_PCM_ALAW:
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_alawaudio",
                                  "audio/x-alaw");
      break;

404
    case CODEC_ID_ADPCM_IMA_QT:
405 406 407 408
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_adpcm_ima_qt",
                                  "audio/x-adpcm",
                                    "layout", GST_PROPS_STRING ("quicktime")
                                 );
409
      break;
410

411
    case CODEC_ID_ADPCM_IMA_WAV:
412 413 414 415
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_adpcm_ima_wav",
                                  "audio/x-adpcm",
                                    "layout", GST_PROPS_STRING ("wav")
                                 );
416
      break;
417

418
    case CODEC_ID_ADPCM_MS:
419 420 421 422 423 424 425 426 427 428 429
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_adpcm_ms",
                                  "audio/x-adpcm",
                                    "layout", GST_PROPS_STRING ("microsoft")
                                 );
      break;

    case CODEC_ID_ADPCM_4XM:
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_adpcm_4xm",
                                  "audio/x-adpcm",
                                    "layout", GST_PROPS_STRING ("4xm")
                                 );
430 431 432 433 434 435
      break;

    case CODEC_ID_AMR_NB:
      /* .. */
      break;

436 437 438 439 440 441 442 443 444 445 446 447 448 449
    /*case CODEC_ID_RA_144:
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_realaudio_144",
                                  "audio/x-pn-realaudio",
                                    "bitrate", GST_PROPS_INT (14400)
                                 );
      break;

    case CODEC_ID_RA_288:
      caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_realaudio_288",
                                  "audio/x-pn-realaudio",
                                    "bitrate", GST_PROPS_INT (28800)
                                 );
      break;*/

450 451 452 453 454 455 456 457 458 459
    default:
      /* .. */
      break;
  }

  if (caps != NULL) {
    char *str = g_strdup_printf("The caps that belongs to codec_id=%d",
				codec_id);
    gst_caps_debug(caps, str);
    g_free(str);
460 461 462 463 464
  } else {
    char *str = g_strdup_printf("No caps found for codec_id=%d",
                                codec_id);
    gst_caps_debug(caps, str);
    g_free(str);
465 466 467 468 469 470 471 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
  }

  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;
501 502
      endianness = G_BIG_ENDIAN;
      r_mask = 0x0000ff; g_mask = 0x00ff00; b_mask = 0xff0000;
503 504
      break;
    case PIX_FMT_YUV422P:
505
      fmt = GST_MAKE_FOURCC ('Y','4','2','B');
506 507 508 509 510 511
      break;
    case PIX_FMT_YUV444P:
      /* .. */
      break;
    case PIX_FMT_RGBA32:
      bpp = depth = 32;
512 513 514 515 516 517
      endianness = G_BIG_ENDIAN;
#if (G_BYTE_ORDER == G_BIG_ENDIAN)
      r_mask = 0xff000000; g_mask = 0x00ff0000; b_mask = 0x0000ff00;
#else 
      r_mask = 0x000000ff; g_mask = 0x0000ff00; b_mask = 0x00ff0000;
#endif
518 519
      break;
    case PIX_FMT_YUV410P:
520
      fmt = GST_MAKE_FOURCC ('Y','U','V','9');
521 522
      break;
    case PIX_FMT_YUV411P:
523
      fmt = GST_MAKE_FOURCC ('Y','4','1','B');
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
      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) {
    caps = GST_FF_VID_CAPS_NEW ("ffmpeg_rawvideo",
542
                                "video/x-raw-rgb",
543 544 545 546 547 548 549 550 551
                                  "bpp",        GST_PROPS_INT (bpp),
                                  "depth",      GST_PROPS_INT (depth),
                                  "red_mask",   GST_PROPS_INT (r_mask),
                                  "green_mask", GST_PROPS_INT (g_mask),
                                  "blue_mask",  GST_PROPS_INT (b_mask),
                                  "endianness", GST_PROPS_INT (endianness)
                                );
  } else if (fmt) {
    caps = GST_FF_VID_CAPS_NEW ("ffmpeg_rawvideo",
552
                                "video/x-raw-yuv",
553 554 555 556 557 558 559 560 561
                                  "format",     GST_PROPS_FOURCC (fmt)
                               );
  }

  if (caps != NULL) {
    char *str = g_strdup_printf("The caps that belongs to pix_fmt=%d",
				pix_fmt);
    gst_caps_debug(caps, str);
    g_free(str);
562 563 564 565 566
  } else {
    char *str = g_strdup_printf("No caps found for pix_fmt=%d",
				pix_fmt);
    gst_caps_debug(caps, str);
    g_free(str);
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
  }

  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;
592
      break;
593

594
    default:
595
      /* .. */
596 597 598
      break;
  }

599 600
  if (bpp) {
    caps = GST_FF_AUD_CAPS_NEW ("ffmpeg_rawaudio",
601
                                "audio/x-raw-int",
602 603 604
                                  "signed",     GST_PROPS_BOOLEAN (signedness),
                                  "endianness", GST_PROPS_INT (G_BYTE_ORDER),
                                  "width",      GST_PROPS_INT (bpp),
605
                                  "depth",      GST_PROPS_INT (bpp)
606 607 608 609 610 611 612 613
                                );
  }

  if (caps != NULL) {
    char *str = g_strdup_printf("The caps that belongs to sample_fmt=%d",
				sample_fmt);
    gst_caps_debug(caps, str);
    g_free(str);
614 615 616 617 618
  } else {
    char *str = g_strdup_printf("No caps found for sample_fmt=%d",
				sample_fmt);
    gst_caps_debug(caps, str);
    g_free(str);
619 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 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
  }

  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)
{
  GstCaps *caps = NULL;

  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;

        for (i = 0; i < PIX_FMT_NB; i++) {
          temp = gst_ffmpeg_pixfmt_to_caps (i, NULL);
          if (temp != NULL) {
            caps = gst_caps_append (caps, temp);
          }
        }
      }
      break;

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

        for (i = 0; i <= SAMPLE_FMT_S16; i++) {
          temp = gst_ffmpeg_smpfmt_to_caps (i, NULL);
          if (temp != NULL) {
            caps = gst_caps_append (caps, temp);
          }
        }
      }
      break;

    default:
      /* .. */
      break;
  }

  return caps;
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

/* Convert a GstCaps (audio/raw) to a FFMPEG SampleFmt
 * and other audio properties in a AVCodecContext.
 *
 * For usefullness, see below
 */

static void
gst_ffmpeg_caps_to_smpfmt (GstCaps        *caps,
                           AVCodecContext *context)
{
  if (gst_caps_has_property_typed (caps, "width",
				   GST_PROPS_INT_TYPE) &&
      gst_caps_has_property_typed (caps, "depth",
				   GST_PROPS_INT_TYPE) &&
      gst_caps_has_property_typed (caps, "signed",
				   GST_PROPS_BOOLEAN_TYPE) &&
      gst_caps_has_property_typed (caps, "endianness",
				   GST_PROPS_INT_TYPE)) {
    gint depth = 0, width = 0, endianness = 0;
    gboolean signedness = FALSE;
    gst_caps_get (caps,
                  "width",      &width,
                  "depth",      &depth,
                  "endianness", &endianness,
                  "signed",     &signedness,
                  NULL);
    if (width == 16 && depth == 16 &&
        endianness == G_BYTE_ORDER && signedness == TRUE) {
      context->sample_fmt = SAMPLE_FMT_S16;
    }
  }

  if (gst_caps_has_property_typed (caps, "channels",
				   GST_PROPS_INT_TYPE)) {
Ronald S. Bultje's avatar
Ronald S. Bultje committed
712
    gst_caps_get_int (caps, "channels", &context->channels);
713 714 715 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
  }

  if (gst_caps_has_property_typed (caps, "rate",
				   GST_PROPS_INT_TYPE)) {
    gst_caps_get_int (caps, "rate", &context->sample_rate);
  }
}

/* Convert a GstCaps (video/raw) to a FFMPEG PixFmt
 * and other video properties in a AVCodecContext.
 *
 * For usefullness, see below
 */

static void
gst_ffmpeg_caps_to_pixfmt (GstCaps        *caps,
                           AVCodecContext *context)
{
  if (gst_caps_has_property_typed (caps, "width",
				   GST_PROPS_INT_TYPE) &&
      gst_caps_has_property_typed (caps, "height",
				   GST_PROPS_INT_TYPE)) {
    gst_caps_get (caps,
                  "width",  &context->width,
                  "height", &context->height,
                  NULL);
  }

741 742 743 744 745 746 747 748
  if (gst_caps_has_property_typed (caps, "framerate",
			           GST_PROPS_FLOAT_TYPE)) {
    gfloat fps;
    gst_caps_get_float (caps, "framerate", &fps);
    context->frame_rate = fps * 1000;
    context->frame_rate_base = 1000;
  }

749 750 751 752 753 754 755 756 757 758 759 760
  if (gst_caps_has_property_typed (caps, "format",
				   GST_PROPS_FOURCC_TYPE)) {
    guint32 fourcc;
    gst_caps_get_fourcc_int (caps, "format", &fourcc);

    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;
761
      case GST_MAKE_FOURCC ('Y','4','1','B'):
762 763
        context->pix_fmt = PIX_FMT_YUV411P;
        break;
764 765 766 767 768 769 770 771
      case GST_MAKE_FOURCC ('Y','U','V','9'):
        context->pix_fmt = PIX_FMT_YUV410P;
        break;
#if 0
      case FIXME:
        context->pix_fmt = PIX_FMT_YUV444P;
        break;
#endif
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 842 843 844 845 846 847 848
      case GST_MAKE_FOURCC ('R','G','B',' '):
        if (gst_caps_has_property_typed (caps, "depth",
					 GST_PROPS_INT_TYPE) &&
	    gst_caps_has_property_typed (caps, "endianness",
					 GST_PROPS_INT_TYPE)) {
          gint depth = 0, endianness = 0;
          gst_caps_get_int (caps, "depth", &depth);
          gst_caps_get_int (caps, "endianness", &endianness);

          switch (depth) {
            case 32:
              if (endianness == G_BYTE_ORDER)
                context->pix_fmt = PIX_FMT_RGBA32;
              break;
            case 24:
              switch (endianness) {
                case G_LITTLE_ENDIAN:
                  context->pix_fmt = PIX_FMT_BGR24;
                  break;
                case G_BIG_ENDIAN:
                  context->pix_fmt = PIX_FMT_RGB24;
                  break;
                default:
                  /* nothing */
                  break;
              }
              break;
            case 16:
              if (endianness == G_BYTE_ORDER)
                context->pix_fmt = PIX_FMT_RGB565;
              break;
            case 15:
              if (endianness == G_BYTE_ORDER)
                context->pix_fmt = PIX_FMT_RGB555;
              break;
            default:
              /* nothing */
              break;
          }
        }
        break;
      default:
        /* nothing */
        break;
    }
  }
}

/* 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,
                              GstCaps        *caps,
                              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;
  }
}
849 850 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

/* _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")) {
    caps = GST_CAPS_NEW ("ffmpeg_mpeg",
			 "video/mpeg",
                           "systemstream", GST_PROPS_BOOLEAN (TRUE)
                        );
  } else if (!strcmp (format_name, "mpegts")) {
    caps = GST_CAPS_NEW ("ffmpeg_mpegts",
			 "video/mpegts",
                           "systemstream", GST_PROPS_BOOLEAN (TRUE)
                        );
  } else if (!strcmp (format_name, "rm")) {
    caps = GST_CAPS_NEW ("ffmpeg_rm",
877 878
			 "audio/x-pn-realvideo",
                           "systemstream", GST_PROPS_BOOLEAN (TRUE)
879 880 881
                        );
  } else if (!strcmp (format_name, "asf")) {
    caps = GST_CAPS_NEW ("ffmpeg_asf",
882
			 "video/x-asf",
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
                           NULL
                        );
  } else if (!strcmp (format_name, "avi")) {
    caps = GST_CAPS_NEW ("ffmpeg_avi",
			 "video/avi",
                           NULL
                        );
  } else if (!strcmp (format_name, "wav")) {
    caps = GST_CAPS_NEW ("ffmpeg_wav",
			 "video/x-wav",
                           NULL
                        );
  } else if (!strcmp (format_name, "swf")) {
    caps = GST_CAPS_NEW ("ffmpeg_swf",
			 "application/x-shockwave-flash",
                           NULL
                        );
  } else if (!strcmp (format_name, "au")) {
    caps = GST_CAPS_NEW ("ffmpeg_au",
902
			 "audio/x-au",
903 904 905 906 907 908 909 910 911
                           NULL
                        );
  } else if (!strcmp (format_name, "mov")) {
    caps = GST_CAPS_NEW ("ffmpeg_quicktime",
			 "video/quicktime",
                           NULL
                        );
  } else if (!strcmp (format_name, "dv")) {
    caps = GST_CAPS_NEW ("ffmpeg_dv",
912
			 "video/x-dv",
913 914 915 916 917 918 919 920 921 922 923 924 925
                           "systemstream", GST_PROPS_BOOLEAN (TRUE)
                        );
  } else if (!strcmp (format_name, "4xm")) {
    caps = GST_CAPS_NEW ("ffmpeg_4xm",
			 "video/x-4xm",
                           NULL
                        );
  } else {
    /* unknown! */
  }

  return caps;
}
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955

/* 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
gst_ffmpeg_caps_to_codecid (GstCaps        *caps,
                            AVCodecContext *context)
{
  enum CodecID id = CODEC_ID_NONE;
  const gchar *mimetype;
  gboolean video = FALSE, audio = FALSE; /* we want to be sure! */

  g_return_val_if_fail (caps != NULL, CODEC_ID_NONE);

  mimetype = gst_caps_get_mime(caps);

  if (!strcmp (mimetype, "video/x-raw-rgb") ||
      !strcmp (mimetype, "video/x-raw-yuv")) {

    id = CODEC_ID_RAWVIDEO;

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

956
      if (gst_caps_has_property (caps, "format"))
957 958 959 960 961 962 963 964 965 966 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 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 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 1043 1044 1045 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 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 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 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
        gst_caps_get_fourcc_int (caps, "format", &fmt_fcc);
      else
        fmt_fcc = GST_MAKE_FOURCC ('R','G','B',' ');

      switch (fmt_fcc) {
        case GST_MAKE_FOURCC ('R','G','B',' '):
          gst_caps_get_int (caps, "endianness", &endianness);
          gst_caps_get_int (caps, "depth", &depth);
          switch (depth) {
            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 (endianness == G_BIG_ENDIAN) {
                pix_fmt = PIX_FMT_RGB24;
              } else {
                pix_fmt = PIX_FMT_BGR24;
              }
              break;
            case 32:
              if (endianness == G_BIG_ENDIAN) {
                pix_fmt = PIX_FMT_RGBA32;
              }
              break;
            default:
              /* ... */
              break;
          }
          break;
        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 */

    if (gst_caps_has_property(caps, "signed")) {
      gst_caps_get_int(caps, "endianness", &endianness);
      gst_caps_get_boolean(caps, "signed", &signedness);
      gst_caps_get_int(caps, "width", &width);
      gst_caps_get_int(caps, "depth", &depth);

      if (context) {
        context->sample_rate = 0;
        context->channels = 0;
        gst_caps_get_int(caps, "channels", &context->channels);
        gst_caps_get_int(caps, "rate", &context->sample_rate);
      }

      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;
        }

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

  } 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;
    if (gst_caps_has_property(caps, "systemstream")) {
      gst_caps_get_boolean(caps, "systemstream", &sys_strm);
    }
    if (!sys_strm && gst_caps_has_property(caps, "mpegversion")) {
      gst_caps_get_int(caps, "mpegversion", &mpegversion);
      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")) {

    if (gst_caps_has_property (caps, "wmvversion")) {
      gint wmvversion = 0;

      gst_caps_get_int (caps, "wmvversion", &wmvversion);
      switch (wmvversion) {
        case 1:
          id = CODEC_ID_WMV1;
          break;
        case 2:
          id = CODEC_ID_WMV2;
          break;
        default:
          /* ... */
          break;
      }

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

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

    id = CODEC_ID_VORBIS;

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

    if (gst_caps_has_property (caps, "layer")) {
      gint layer = 0;

      gst_caps_get_int (caps, "layer", &layer);
      switch (layer) {
        case 1:
        case 2:
          id = CODEC_ID_MP2;
          break;
        case 3:
          id = CODEC_ID_MP3LAME;
          break;
        default:
          /* ... */
          break;
      }
    } else if (gst_caps_has_property (caps, "mpegversion")) {
      gint mpegversion = 0;

      gst_caps_get_int (caps, "mpegversion", &mpegversion);
      if (mpegversion == 4) {
        id = CODEC_ID_MPEG4AAC;
      }
    }

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

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

    if (gst_caps_has_property (caps, "wmaversion")) {
      gint wmaversion = 0;

      gst_caps_get_int (caps, "wmaversion", &wmaversion);
      switch (wmaversion) {
        case 1:
          id = CODEC_ID_WMAV1;
          break;
        case 2:
          id = CODEC_ID_WMAV2;
          break;
        default:
          /* ... */
          break;
      }
    }

    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")) {

    if (gst_caps_has_property (caps, "msmpegversion")) {
      gint msmpegversion = 0;

      gst_caps_get_int (caps, "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;
      }
    }

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

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

    if (gst_caps_has_property (caps, "svqversion")) {
      gint svqversion = 0;

      gst_caps_get_int (caps, "svqversion", &svqversion);
      switch (svqversion) {
        case 1:
          id = CODEC_ID_SVQ1;
          break;
        case 3:
          id = CODEC_ID_SVQ3;
          break;
        default:
          /* ... */
          break;
      }
    }

    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")) {

    if (gst_caps_has_property (caps, "maceversion")) {
      gint maceversion;

      gst_caps_get_int (caps, "maceversion", &maceversion);
      switch (maceversion) {
        case 3:
          id = CODEC_ID_MACE3;
          break;
        case 6:
          id = CODEC_ID_MACE6;
          break;
        default:
          /* ... */
          break;
      }
    }

    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")) {

    if (gst_caps_has_property (caps, "indeoversion")) {
      gint indeoversion = 0;

      gst_caps_get_int (caps, "indeoversion", &indeoversion);
      switch (indeoversion) {
        case 3:
          id = CODEC_ID_INDEO3;
          break;
        default:
          /* ... */
          break;
      }
    }

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

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

    if (gst_caps_has_property (caps, "divxversion")) {
      gint divxversion = 0;

      gst_caps_get_int (caps, "divxversion", &divxversion);
      switch (divxversion) {
        case 3:
          id = CODEC_ID_MSMPEG4V3;
          break;
        case 4:
        case 5:
          id = CODEC_ID_MPEG4;
          break;
        default:
          /* ... */
          break;
      }
    }

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

  } else if (!strcmp (mimetype, "video/x-3ivx") ||
             !strcmp (mimetype, "video/x-divx")) {

    id = CODEC_ID_MPEG4;
    video = TRUE;

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

    if (gst_caps_has_property (caps, "ffvversion")) {
      gint ffvversion = 0;

      gst_caps_get_int (caps, "ffvversion", &ffvversion);
      switch (ffvversion) {
        case 1:
          id = CODEC_ID_FFV1;
          break;
        default:
          /* ... */
          break;
      }
    }

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

  } else if (!strcmp (mimetype, "x-adpcm")) {

    if (gst_caps_has_property (caps, "layout")) {
      const gchar *layout = "";

      gst_caps_get_string (caps, "layout", &layout);
      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;
      }
    }

    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) {
    char *str = g_strdup_printf("The id=%d belongs to this caps", id);
    gst_caps_debug(caps, str);
    g_free(str);
  }

  return id;
}