gstffmpegcodecmap.c 67 KB
Newer Older
1 2
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3
 * This file:
4
 * Copyright (c) 2002-2004 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
#include "gstffmpegcodecmap.h"

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
/*
 * Read a palette from a caps.
 */

static void
gst_ffmpeg_get_palette (const GstCaps *caps, AVCodecContext *context)
{
  GstStructure *str = gst_caps_get_structure (caps, 0);
  const GValue *palette_v;
  const GstBuffer *palette;

  /* do we have a palette? */
  if ((palette_v = gst_structure_get_value (str,
          "palette_data")) && context) {
    palette = g_value_get_boxed (palette_v);
    if (GST_BUFFER_SIZE (palette) >= 256 * 4) {
      if (context->palctrl)
        av_free (context->palctrl);
      context->palctrl = av_malloc (sizeof (AVPaletteControl));
      context->palctrl->palette_changed = 1;
      memcpy (context->palctrl->palette, GST_BUFFER_DATA (palette),
          AVPALETTE_SIZE);
    }
  }
}

static void
gst_ffmpeg_set_palette (GstCaps *caps, AVCodecContext *context)
{
  if (context->palctrl) {
    GstBuffer *palette = gst_buffer_new_and_alloc (256 * 4);

    memcpy (GST_BUFFER_DATA (palette), context->palctrl->palette,
        AVPALETTE_SIZE);
    gst_caps_set_simple (caps,
        "palette_data", GST_TYPE_BUFFER, palette, NULL);
  }
}

74 75 76 77
/* 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.
78 79 80
 *
 * We should actually do this stuff at the end, like in riff-media.c,
 * but I'm too lazy today. Maybe later.
81 82
 */

83
#define GST_FF_VID_CAPS_NEW(mimetype, ...)			\
David Schleef's avatar
David Schleef committed
84 85 86 87 88 89
    (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,    \
90
	__VA_ARGS__, NULL)  					\
David Schleef's avatar
David Schleef committed
91 92 93 94 95
    :	  							\
    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,	\
96
	__VA_ARGS__, NULL)
97 98 99 100

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

101
#define GST_FF_AUD_CAPS_NEW(mimetype, ...)			\
David Schleef's avatar
David Schleef committed
102 103 104 105
    (context != NULL) ?					      	\
    gst_caps_new_simple (mimetype,	      			\
	"rate", G_TYPE_INT, context->sample_rate,		\
	"channels", G_TYPE_INT, context->channels,		\
106
	__VA_ARGS__, NULL)					\
David Schleef's avatar
David Schleef committed
107 108
    :								\
    gst_caps_new_simple (mimetype,	      			\
109
	__VA_ARGS__, NULL)
110 111 112 113 114 115

/* 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!
116 117 118 119 120 121 122 123 124
 *
 * 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.
125 126
 */

127
GstCaps *
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
128 129
gst_ffmpeg_codecid_to_caps (enum CodecID codec_id,
    AVCodecContext * context, gboolean encode)
130
{
131
  GstCaps *caps = NULL;
132
  gboolean buildcaps = FALSE;
133

134
  switch (codec_id) {
135
    case CODEC_ID_MPEG1VIDEO:
136 137 138 139
      /* For decoding, CODEC_ID_MPEG2VIDEO is preferred... So omit here */
      if (encode) {
        /* FIXME: bitrate */
        caps = GST_FF_VID_CAPS_NEW ("video/mpeg",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
140 141
            "mpegversion", G_TYPE_INT, 1,
            "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
142 143 144 145 146 147 148
      }
      break;

    case CODEC_ID_MPEG2VIDEO:
      if (encode) {
        /* FIXME: bitrate */
        caps = GST_FF_VID_CAPS_NEW ("video/mpeg",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
149 150
            "mpegversion", G_TYPE_INT, 2,
            "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
151 152 153 154
      } 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",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
155 156
            "mpegversion", GST_TYPE_INT_RANGE, 1, 2,
            "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
157
      }
158
      break;
159 160 161 162 163

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

Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
164
      /* I don't know the exact differences between those... Anyone? */
165
    case CODEC_ID_H263:
166 167
    case CODEC_ID_H263P:
    case CODEC_ID_H263I:
168
      caps = GST_FF_VID_CAPS_NEW ("video/x-h263", NULL);
169
      break;
170

171 172 173 174
    case CODEC_ID_H261:
      caps = GST_FF_VID_CAPS_NEW ("video/x-h261", NULL);
      break;

175
    case CODEC_ID_RV10:
176 177 178 179 180 181
    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",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
182 183
            "systemstream", G_TYPE_BOOLEAN, FALSE,
            "rmversion", G_TYPE_INT, version, NULL);
184 185 186 187
        if (context) {
          gst_caps_set_simple (caps,
              "rmsubid", GST_TYPE_FOURCC, context->sub_id, NULL);
        }
188
      } while (0);
189
      break;
190

191
    case CODEC_ID_MP2:
192 193 194 195
      /* we use CODEC_ID_MP3 for decoding */
      if (encode) {
        /* FIXME: bitrate */
        caps = GST_FF_AUD_CAPS_NEW ("audio/mpeg",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
196
            "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 2, NULL);
197
      }
198
      break;
199

200 201 202 203
    case CODEC_ID_MP3:
      if (encode) {
        /* FIXME: bitrate */
        caps = GST_FF_AUD_CAPS_NEW ("audio/mpeg",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
204
            "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 3, NULL);
205 206 207 208
      } 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",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
209 210
            "mpegversion", G_TYPE_INT, 1,
            "layer", GST_TYPE_INT_RANGE, 1, 3, NULL);
211
      }
212
      break;
213

David Schleef's avatar
David Schleef committed
214
    case CODEC_ID_VORBIS:
215 216 217 218 219 220 221
      /* 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.
       */
222
      break;
223

224
    case CODEC_ID_AC3:
225 226 227 228 229 230
      /* 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 */
231
        caps = GST_FF_AUD_CAPS_NEW ("audio/x-ac3", NULL);
232
      }
233
      break;
234

Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
235 236 237 238 239
      /* 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...)? */
240
    case CODEC_ID_MJPEG:
Wim Taymans's avatar
Wim Taymans committed
241
    case CODEC_ID_MJPEGB:
242 243
    case CODEC_ID_LJPEG:
    case CODEC_ID_SP5X:
244
      caps = GST_FF_VID_CAPS_NEW ("image/jpeg", NULL);
Wim Taymans's avatar
Wim Taymans committed
245
      break;
246

247
    case CODEC_ID_MPEG4:
248
      if (encode && context != NULL) {
249 250
        /* I'm not exactly sure what ffmpeg outputs... ffmpeg itself uses
         * the AVI fourcc 'DIVX', but 'mp4v' for Quicktime... */
251
        switch (context->codec_tag) {
252 253 254 255 256 257 258 259 260 261 262 263
          case GST_MAKE_FOURCC ('D', 'I', 'V', 'X'):
            caps = GST_FF_VID_CAPS_NEW ("video/x-divx",
	        "divxversion", G_TYPE_INT, 5, NULL);
            break;
          case GST_MAKE_FOURCC ('m', 'p', '4', 'v'):
          default:
            /* FIXME: bitrate */
            caps = GST_FF_VID_CAPS_NEW ("video/mpeg",
                "systemstream", G_TYPE_BOOLEAN, FALSE,
                "mpegversion", G_TYPE_INT, 4, NULL);
            break;
        }
264 265 266
      } else {
        /* The trick here is to separate xvid, divx, mpeg4, 3ivx et al */
        caps = GST_FF_VID_CAPS_NEW ("video/mpeg",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
267 268
            "mpegversion", G_TYPE_INT, 4,
            "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
269 270 271 272 273 274 275 276 277
        if (encode) {
          gst_caps_append (caps, GST_FF_VID_CAPS_NEW ("video/x-divx",
              "divxversion", G_TYPE_INT, 5, NULL));
        } else {
          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", NULL));
          gst_caps_append (caps, GST_FF_VID_CAPS_NEW ("video/x-3ivx", NULL));
        }
278
      }
279
      break;
280

281
    case CODEC_ID_RAWVIDEO:
282
      caps = gst_ffmpeg_codectype_to_caps (CODEC_TYPE_VIDEO, context);
283
      break;
284

285
    case CODEC_ID_MSMPEG4V1:
286 287
    case CODEC_ID_MSMPEG4V2:
    case CODEC_ID_MSMPEG4V3:
288 289 290 291 292
      do {
        gint version = 41 + codec_id - CODEC_ID_MSMPEG4V1;

        /* encode-FIXME: bitrate */
        caps = GST_FF_VID_CAPS_NEW ("video/x-msmpeg",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
293
            "msmpegversion", G_TYPE_INT, version, NULL);
294 295
        if (!encode && codec_id == CODEC_ID_MSMPEG4V3) {
          gst_caps_append (caps, GST_FF_VID_CAPS_NEW ("video/x-divx",
296
              "divxversion", G_TYPE_INT, 3, NULL));
297 298
        }
      } while (0);
299
      break;
300

301
    case CODEC_ID_WMV1:
302 303 304 305
    case CODEC_ID_WMV2:
      do {
        gint version = (codec_id == CODEC_ID_WMV1) ? 1 : 2;

306
        caps = GST_FF_VID_CAPS_NEW ("video/x-wmv",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
307
            "wmvversion", G_TYPE_INT, version, NULL);
308
      } while (0);
309
      break;
310

311 312
    case CODEC_ID_FLV1:
      buildcaps = TRUE;
313
      break;
314

315
    case CODEC_ID_SVQ1:
David Schleef's avatar
David Schleef committed
316
      caps = GST_FF_VID_CAPS_NEW ("video/x-svq",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
317
          "svqversion", G_TYPE_INT, 1, NULL);
318
      break;
319 320

    case CODEC_ID_SVQ3:
David Schleef's avatar
David Schleef committed
321
      caps = GST_FF_VID_CAPS_NEW ("video/x-svq",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
322 323 324 325 326
          "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);
327
      break;
328

329
    case CODEC_ID_DVAUDIO:
330
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-dv", NULL);
331
      break;
332

333
    case CODEC_ID_DVVIDEO:
334
      caps = GST_FF_VID_CAPS_NEW ("video/x-dv", NULL);
335
      break;
336

337 338
    case CODEC_ID_WMAV1:
    case CODEC_ID_WMAV2:
339 340 341
      do {
        gint version = (codec_id == CODEC_ID_WMAV1) ? 1 : 2;

342
        if (context) {
343 344 345 346
          caps = GST_FF_AUD_CAPS_NEW ("audio/x-wma",
             "wmaversion", G_TYPE_INT, version,
             "block_align", G_TYPE_INT, context->block_align,
             "bitrate", G_TYPE_INT, context->bit_rate, NULL);
347
        } else {
348 349 350 351 352
          caps = GST_FF_AUD_CAPS_NEW ("audio/x-wma",
             "wmaversion", G_TYPE_INT, version,
             "block_align", GST_TYPE_INT_RANGE, 0, G_MAXINT,
             "bitrate", GST_TYPE_INT_RANGE, 0, G_MAXINT, NULL);
        }
353
      } while (0);
354
      break;
355

356 357
    case CODEC_ID_MACE3:
    case CODEC_ID_MACE6:
358 359 360 361
      do {
        gint version = (codec_id == CODEC_ID_MACE3) ? 3 : 6;

        caps = GST_FF_AUD_CAPS_NEW ("audio/x-mace",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
362
            "maceversion", G_TYPE_INT, version, NULL);
363
      } while (0);
364
      break;
365

Wim Taymans's avatar
Wim Taymans committed
366
    case CODEC_ID_HUFFYUV:
367
      caps = GST_FF_VID_CAPS_NEW ("video/x-huffyuv", NULL);
368 369 370
      break;

    case CODEC_ID_CYUV:
371
      buildcaps = TRUE;
372 373 374
      break;

    case CODEC_ID_H264:
375
      caps = GST_FF_VID_CAPS_NEW ("video/x-h264", NULL);
376 377 378
      break;

    case CODEC_ID_INDEO3:
David Schleef's avatar
David Schleef committed
379
      caps = GST_FF_VID_CAPS_NEW ("video/x-indeo",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
380
          "indeoversion", G_TYPE_INT, 3, NULL);
381 382 383
      break;

    case CODEC_ID_VP3:
384
      caps = GST_FF_VID_CAPS_NEW ("video/x-vp3", NULL);
385 386
      break;

387
    case CODEC_ID_THEORA:
388
      caps = GST_FF_VID_CAPS_NEW ("video/x-theora", NULL);
389 390
      break;

391
    case CODEC_ID_AAC:
392
    case CODEC_ID_MPEG4AAC:
393 394 395
      /* 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. */
396 397 398
      break;

    case CODEC_ID_ASV1:
399 400
    case CODEC_ID_ASV2:
      buildcaps = TRUE;
401 402
      break;

403
    case CODEC_ID_FFV1:
David Schleef's avatar
David Schleef committed
404
      caps = GST_FF_VID_CAPS_NEW ("video/x-ffv",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
405
          "ffvversion", G_TYPE_INT, 1, NULL);
406 407 408
      break;

    case CODEC_ID_4XM:
409
      caps = GST_FF_VID_CAPS_NEW ("video/x-4xm", NULL);
410 411
      break;

412 413 414 415 416 417
    case CODEC_ID_XAN_WC3:
    case CODEC_ID_XAN_WC4:
      caps = GST_FF_VID_CAPS_NEW ("video/x-xan",
          "wcversion", G_TYPE_INT, 3 - CODEC_ID_XAN_WC3 + codec_id, NULL);
      break;

418 419 420 421 422 423 424 425
    case CODEC_ID_VCR1:
    case CODEC_ID_CLJR:
    case CODEC_ID_MDEC:
    case CODEC_ID_ROQ:
    case CODEC_ID_INTERPLAY_VIDEO:
      buildcaps = TRUE;
      break;

426 427 428 429
    case CODEC_ID_RPZA:
      caps = GST_FF_VID_CAPS_NEW ("video/x-apple-video", NULL);
      break;

430
    case CODEC_ID_CINEPAK:
431
      caps = GST_FF_VID_CAPS_NEW ("video/x-cinepak", NULL);
432 433
      break;

434 435 436 437 438 439 440 441 442 443 444 445 446
    /* WS_VQA belogns here (order) */

    case CODEC_ID_MSRLE:
      caps = GST_FF_VID_CAPS_NEW ("video/x-rle",
	  "layout", G_TYPE_STRING, "microsoft", NULL);
      if (context) {
        gst_caps_set_simple (caps,
	    "depth", G_TYPE_INT, (gint) context->bits_per_sample, NULL);
      } else {
        gst_caps_set_simple (caps, "depth", GST_TYPE_INT_RANGE, 1, 64, NULL);
      }
      break;

447 448 449 450 451
    case CODEC_ID_MSVIDEO1:
      caps = GST_FF_VID_CAPS_NEW ("video/x-msvideocodec",
	  "msvideoversion", G_TYPE_INT, 1, NULL);
      break;

452 453 454 455 456 457 458 459 460 461 462
    case CODEC_ID_WS_VQA:
    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:
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
    case CODEC_ID_SONIC:
    case CODEC_ID_SONIC_LS:
    case CODEC_ID_SNOW:
    case CODEC_ID_TSCC:
    case CODEC_ID_ULTI:
    case CODEC_ID_QDRAW:
    case CODEC_ID_VIXL:
    case CODEC_ID_QPEG:
    case CODEC_ID_XVID:
    case CODEC_ID_PNG:
    case CODEC_ID_PPM:
    case CODEC_ID_PBM:
    case CODEC_ID_PGM:
    case CODEC_ID_PGMYUV:
    case CODEC_ID_PAM:
    case CODEC_ID_FFVHUFF:
479 480 481
      buildcaps = TRUE;
      break;

Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
482
      /* weird quasi-codecs for the demuxers only */
483 484 485 486 487 488 489
    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 {
490
        gint width = 0, depth = 0, endianness = 0;
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
491
        gboolean signedness = FALSE;    /* blabla */
492 493 494

        switch (codec_id) {
          case CODEC_ID_PCM_S16LE:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
495 496
            width = 16;
            depth = 16;
497 498 499 500
            endianness = G_LITTLE_ENDIAN;
            signedness = TRUE;
            break;
          case CODEC_ID_PCM_S16BE:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
501 502
            width = 16;
            depth = 16;
503 504 505 506
            endianness = G_BIG_ENDIAN;
            signedness = TRUE;
            break;
          case CODEC_ID_PCM_U16LE:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
507 508
            width = 16;
            depth = 16;
509 510 511 512
            endianness = G_LITTLE_ENDIAN;
            signedness = FALSE;
            break;
          case CODEC_ID_PCM_U16BE:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
513 514
            width = 16;
            depth = 16;
515 516 517 518
            endianness = G_BIG_ENDIAN;
            signedness = FALSE;
            break;
          case CODEC_ID_PCM_S8:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
519 520
            width = 8;
            depth = 8;
521 522 523 524
            endianness = G_BYTE_ORDER;
            signedness = TRUE;
            break;
          case CODEC_ID_PCM_U8:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
525 526
            width = 8;
            depth = 8;
527 528 529 530
            endianness = G_BYTE_ORDER;
            signedness = FALSE;
            break;
          default:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
531
            g_assert (0);       /* don't worry, we never get here */
532 533 534
            break;
        }

David Schleef's avatar
David Schleef committed
535
        caps = GST_FF_AUD_CAPS_NEW ("audio/x-raw-int",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
536 537 538 539
            "width", G_TYPE_INT, width,
            "depth", G_TYPE_INT, depth,
            "endianness", G_TYPE_INT, endianness,
            "signed", G_TYPE_BOOLEAN, signedness, NULL);
540 541 542
      } while (0);
      break;

543
    case CODEC_ID_PCM_MULAW:
544
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-mulaw", NULL);
545 546 547
      break;

    case CODEC_ID_PCM_ALAW:
548
      caps = GST_FF_AUD_CAPS_NEW ("audio/x-alaw", NULL);
549 550
      break;

551 552
    case CODEC_ID_ADPCM_IMA_QT:
    case CODEC_ID_ADPCM_IMA_WAV:
553 554 555 556
    case CODEC_ID_ADPCM_IMA_DK3:
    case CODEC_ID_ADPCM_IMA_DK4:
    case CODEC_ID_ADPCM_IMA_WS:
    case CODEC_ID_ADPCM_IMA_SMJPEG:
557
    case CODEC_ID_ADPCM_MS:
558
    case CODEC_ID_ADPCM_4XM:
559 560 561 562
    case CODEC_ID_ADPCM_XA:
    case CODEC_ID_ADPCM_ADX:
    case CODEC_ID_ADPCM_EA:
    case CODEC_ID_ADPCM_G726:
563
    case CODEC_ID_ADPCM_CT:
564 565 566 567 568 569 570 571
      do {
        gchar *layout = NULL;

        switch (codec_id) {
          case CODEC_ID_ADPCM_IMA_QT:
            layout = "quicktime";
            break;
          case CODEC_ID_ADPCM_IMA_WAV:
572
            layout = "dvi";
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
            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;
604 605 606
          case CODEC_ID_ADPCM_CT:
            layout = "ct";
            break;
607
          default:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
608
            g_assert (0);       /* don't worry, we never get here */
609 610 611 612 613 614
            break;
        }

        /* FIXME: someone please check whether we need additional properties
         * in this caps definition. */
        caps = GST_FF_AUD_CAPS_NEW ("audio/x-adpcm",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
615
            "layout", G_TYPE_STRING, layout, NULL);
616 617 618 619
        if (context)
          gst_caps_set_simple (caps,
              "block_align", G_TYPE_INT, context->block_align,
              "bitrate", G_TYPE_INT, context->bit_rate, NULL);
620
      } while (0);
621 622 623
      break;

    case CODEC_ID_AMR_NB:
624 625 626 627
    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... */
628 629
      break;

630
    case CODEC_ID_RA_144:
631
    case CODEC_ID_RA_288:
632 633 634 635 636
      do {
        gint version = (codec_id == CODEC_ID_RA_144) ? 1 : 2;

        /* FIXME: properties? */
        caps = GST_FF_AUD_CAPS_NEW ("audio/x-pn-realaudio",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
637
            "raversion", G_TYPE_INT, version, NULL);
638
      } while (0);
639
      break;
640

641 642 643
    case CODEC_ID_ROQ_DPCM:
    case CODEC_ID_INTERPLAY_DPCM:
    case CODEC_ID_XAN_DPCM:
644
    case CODEC_ID_SOL_DPCM:
645 646 647 648 649 650 651 652 653 654 655 656 657
      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;
658 659 660
          case CODEC_ID_SOL_DPCM:
            layout = "sol";
            break;
661
          default:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
662
            g_assert (0);       /* don't worry, we never get here */
663 664 665 666 667 668
            break;
        }

        /* FIXME: someone please check whether we need additional properties
         * in this caps definition. */
        caps = GST_FF_AUD_CAPS_NEW ("audio/x-dpcm",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
669
            "layout", G_TYPE_STRING, layout, NULL);
670 671 672 673
        if (context)
          gst_caps_set_simple (caps,
              "block_align", G_TYPE_INT, context->block_align,
              "bitrate", G_TYPE_INT, context->bit_rate, NULL);
674 675
      } while (0);
      break;
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
676

677 678 679 680 681 682
    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);
      }
683 684
      break;

685
    default:
686
      g_warning ("Unknown codec ID %d, please add here", codec_id);
687 688 689
      break;
  }

690 691 692 693 694 695 696 697 698 699
  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);
700
          caps = GST_FF_VID_CAPS_NEW (mime, NULL);
701 702 703 704
          g_free (mime);
          break;
        case CODEC_TYPE_AUDIO:
          mime = g_strdup_printf ("audio/x-gst_ff-%s", codec->name);
705
          caps = GST_FF_AUD_CAPS_NEW (mime, NULL);
706
          if (context)
707 708 709
            gst_caps_set_simple (caps,
                "block_align", G_TYPE_INT, context->block_align,
                "bitrate", G_TYPE_INT, context->bit_rate, NULL);
710 711 712 713 714 715 716 717
          g_free (mime);
          break;
        default:
          break;
      }
    }
  }

718
  if (caps != NULL) {
719 720 721 722 723
    char *str;

    /* set private data */
    if (context && context->extradata_size > 0) {
      GstBuffer *data = gst_buffer_new_and_alloc (context->extradata_size);
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
724

725 726 727 728 729 730 731
      memcpy (GST_BUFFER_DATA (data), context->extradata,
          context->extradata_size);
      gst_caps_set_simple (caps,
          "codec_data", GST_TYPE_BUFFER, data, NULL);
      gst_buffer_unref (data);
    }

732 733 734 735 736
    /* palette */
    if (context) {
      gst_ffmpeg_set_palette (caps, context);
    }

737
    str = gst_caps_to_string (caps);
David Schleef's avatar
David Schleef committed
738
    GST_DEBUG ("caps for codec_id=%d: %s", codec_id, str);
739
    g_free (str);
740

741
  } else {
David Schleef's avatar
David Schleef committed
742
    GST_WARNING ("No caps found for codec_id=%d", codec_id);
743 744 745 746 747 748 749 750 751 752 753 754 755
  }

  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 *
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
756
gst_ffmpeg_pixfmt_to_caps (enum PixelFormat pix_fmt, AVCodecContext * context)
757 758 759 760 761 762 763 764 765
{
  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:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
766
      fmt = GST_MAKE_FOURCC ('I', '4', '2', '0');
767 768
      break;
    case PIX_FMT_YUV422:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
769
      fmt = GST_MAKE_FOURCC ('Y', 'U', 'Y', '2');
770 771 772 773
      break;
    case PIX_FMT_RGB24:
      bpp = depth = 24;
      endianness = G_BIG_ENDIAN;
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
774 775 776
      r_mask = 0xff0000;
      g_mask = 0x00ff00;
      b_mask = 0x0000ff;
777 778 779
      break;
    case PIX_FMT_BGR24:
      bpp = depth = 24;
780
      endianness = G_BIG_ENDIAN;
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
781 782 783
      r_mask = 0x0000ff;
      g_mask = 0x00ff00;
      b_mask = 0xff0000;
784 785
      break;
    case PIX_FMT_YUV422P:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
786
      fmt = GST_MAKE_FOURCC ('Y', '4', '2', 'B');
787 788 789 790 791
      break;
    case PIX_FMT_YUV444P:
      /* .. */
      break;
    case PIX_FMT_RGBA32:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
792 793
      bpp = 32;
      depth = 24;
794 795
      endianness = G_BIG_ENDIAN;
#if (G_BYTE_ORDER == G_BIG_ENDIAN)
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
796 797 798 799 800 801 802
      r_mask = 0x00ff0000;
      g_mask = 0x0000ff00;
      b_mask = 0x000000ff;
#else
      r_mask = 0x0000ff00;
      g_mask = 0x00ff0000;
      b_mask = 0xff000000;
803
#endif
804 805
      break;
    case PIX_FMT_YUV410P:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
806
      fmt = GST_MAKE_FOURCC ('Y', 'U', 'V', '9');
807 808
      break;
    case PIX_FMT_YUV411P:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
809
      fmt = GST_MAKE_FOURCC ('Y', '4', '1', 'B');
810 811 812 813
      break;
    case PIX_FMT_RGB565:
      bpp = depth = 16;
      endianness = G_BYTE_ORDER;
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
814 815 816
      r_mask = 0xf800;
      g_mask = 0x07e0;
      b_mask = 0x001f;
817 818
      break;
    case PIX_FMT_RGB555:
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
819 820
      bpp = 16;
      depth = 15;
821
      endianness = G_BYTE_ORDER;
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
822 823 824
      r_mask = 0x7c00;
      g_mask = 0x03e0;
      b_mask = 0x001f;
825
      break;
826 827 828 829
    case PIX_FMT_PAL8:
      bpp = depth = 8;
      endianness = G_BYTE_ORDER;
      break;
830 831 832 833 834 835
    default:
      /* give up ... */
      break;
  }

  if (bpp != 0) {
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
    if (r_mask != 0) {
      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);
    } else {
      caps = GST_FF_VID_CAPS_NEW ("video/x-raw-rgb",
          "bpp", G_TYPE_INT, bpp,
          "depth", G_TYPE_INT, depth,
          "endianness", G_TYPE_INT, endianness, NULL);
      if (context) {
        gst_ffmpeg_set_palette (caps, context);
      }
    }
853
  } else if (fmt) {
David Schleef's avatar
David Schleef committed
854
    caps = GST_FF_VID_CAPS_NEW ("video/x-raw-yuv",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
855
        "format", GST_TYPE_FOURCC, fmt, NULL);
856 857 858
  }

  if (caps != NULL) {
David Schleef's avatar
David Schleef committed
859
    char *str = gst_caps_to_string (caps);
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
860

David Schleef's avatar
David Schleef committed
861
    GST_DEBUG ("caps for pix_fmt=%d: %s", pix_fmt, str);
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
862
    g_free (str);
863
  } else {
David Schleef's avatar
David Schleef committed
864
    GST_WARNING ("No caps found for pix_fmt=%d", pix_fmt);
865 866 867 868 869 870 871 872 873 874 875 876 877
  }

  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 *
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
878 879
gst_ffmpeg_smpfmt_to_caps (enum SampleFormat sample_fmt,
    AVCodecContext * context)
880 881 882 883 884 885 886 887 888 889
{
  GstCaps *caps = NULL;

  int bpp = 0;
  gboolean signedness = FALSE;

  switch (sample_fmt) {
    case SAMPLE_FMT_S16:
      signedness = TRUE;
      bpp = 16;
890
      break;
891

892
    default:
893
      /* .. */
894 895 896
      break;
  }

897
  if (bpp) {
David Schleef's avatar
David Schleef committed
898
    caps = GST_FF_AUD_CAPS_NEW ("audio/x-raw-int",
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
899 900 901
        "signed", G_TYPE_BOOLEAN, signedness,
        "endianness", G_TYPE_INT, G_BYTE_ORDER,
        "width", G_TYPE_INT, bpp, "depth", G_TYPE_INT, bpp, NULL);
902 903 904
  }

  if (caps != NULL) {
David Schleef's avatar
David Schleef committed
905
    char *str = gst_caps_to_string (caps);
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
906

David Schleef's avatar
David Schleef committed
907
    GST_DEBUG ("caps for sample_fmt=%d: %s", sample_fmt, str);
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
908
    g_free (str);
909
  } else {
David Schleef's avatar
David Schleef committed
910
    GST_WARNING ("No caps found for sample_fmt=%d", sample_fmt);
911 912 913 914 915 916 917 918 919 920 921 922 923
  }

  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 *
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
924 925
gst_ffmpeg_codectype_to_caps (enum CodecType codec_type,
    AVCodecContext * context)
926
{
David Schleef's avatar
David Schleef committed
927
  GstCaps *caps;
928 929 930 931

  switch (codec_type) {
    case CODEC_TYPE_VIDEO:
      if (context) {
932 933
        caps = gst_ffmpeg_pixfmt_to_caps (context->pix_fmt,
            context->width == -1 ? NULL : context);
934 935 936 937
      } else {
        GstCaps *temp;
        enum PixelFormat i;

David Schleef's avatar
David Schleef committed
938
        caps = gst_caps_new_empty ();
939 940 941
        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
942
            gst_caps_append (caps, temp);
943 944 945 946 947 948 949 950 951 952 953 954
          }
        }
      }
      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
955
        caps = gst_caps_new_empty ();
956 957 958
        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
959
            gst_caps_append (caps, temp);
960 961 962 963 964 965 966
          }
        }
      }
      break;

    default:
      /* .. */
David Schleef's avatar
David Schleef committed
967
      caps = NULL;
968 969 970 971
      break;
  }

  return caps;
972
}
973 974 975 976 977 978 979 980

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

static void
981 982
gst_ffmpeg_caps_to_smpfmt (const GstCaps * caps,
    AVCodecContext * context, gboolean raw)
983
{
David Schleef's avatar
David Schleef committed
984 985 986 987 988 989
  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);
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
990

991 992 993 994 995 996 997 998
  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);

  if (!raw)
    return;

David Schleef's avatar
David Schleef committed
999 1000 1001 1002
  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)) {
1003 1004 1005 1006 1007 1008 1009
    if (width == 16 && depth == 16 &&
        endianness == G_BYTE_ORDER && signedness == TRUE) {
      context->sample_fmt = SAMPLE_FMT_S16;
    }
  }
}

1010

1011 1012 1013 1014 1015 1016 1017
/* Convert a GstCaps (video/raw) to a FFMPEG PixFmt
 * and other video properties in a AVCodecContext.
 *
 * For usefullness, see below
 */

static void
1018 1019
gst_ffmpeg_caps_to_pixfmt (const GstCaps * caps,
    AVCodecContext * context, gboolean raw)
1020
{
David Schleef's avatar
David Schleef committed
1021 1022
  GstStructure *structure;
  gdouble fps;
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
1023

David Schleef's avatar
David Schleef committed
1024 1025
  g_return_if_fail (gst_caps_get_size (caps) == 1);
  structure = gst_caps_get_structure (caps, 0);
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
1026

David Schleef's avatar
David Schleef committed
1027 1028
  gst_structure_get_int (structure, "width", &context->width);
  gst_structure_get_int (structure, "height", &context->height);
1029

David Schleef's avatar
David Schleef committed
1030
  if (gst_structure_get_double (structure, "framerate", &fps)) {
1031 1032
    context->frame_rate = fps * DEFAULT_FRAME_RATE_BASE;
    context->frame_rate_base = DEFAULT_FRAME_RATE_BASE;
1033 1034
  }

1035 1036 1037
  if (!raw)
    return;

David Schleef's avatar
David Schleef committed
1038 1039
  if (strcmp (gst_structure_get_name (structure), "video/x-raw-yuv") == 0) {
    guint32 fourcc;
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
1040

David Schleef's avatar
David Schleef committed
1041
    if (gst_structure_get_fourcc (structure, "format", &fourcc)) {
Benjamin Otte's avatar
Benjamin Otte committed
1042
      switch (fourcc) {
Thomas Vander Stichele's avatar
indent  
Thomas Vander Stichele committed
1043 1044 1045 1046 1047 1048 1049 1050