avcodecmap: Simplify raw audio caps
[platform/upstream/gst-libav.git] / ext / libav / gstavcodecmap.c
1 /* GStreamer
2  * 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  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <string.h>
27
28 #include <gst/gst.h>
29 #include <libavcodec/avcodec.h>
30 #include <libavutil/channel_layout.h>
31
32 #include "gstav.h"
33 #include "gstavcodecmap.h"
34
35 #include <gst/video/video.h>
36 #include <gst/audio/audio.h>
37 #include <gst/pbutils/codec-utils.h>
38
39 /* IMPORTANT: Keep this sorted by the ffmpeg channel masks */
40 static const struct
41 {
42   guint64 ff;
43   GstAudioChannelPosition gst;
44 } _ff_to_gst_layout[] = {
45   {
46   AV_CH_FRONT_LEFT, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT}, {
47   AV_CH_FRONT_RIGHT, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}, {
48   AV_CH_FRONT_CENTER, GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER}, {
49   AV_CH_LOW_FREQUENCY, GST_AUDIO_CHANNEL_POSITION_LFE1}, {
50   AV_CH_BACK_LEFT, GST_AUDIO_CHANNEL_POSITION_REAR_LEFT}, {
51   AV_CH_BACK_RIGHT, GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT}, {
52   AV_CH_FRONT_LEFT_OF_CENTER, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER}, {
53   AV_CH_FRONT_RIGHT_OF_CENTER,
54         GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER}, {
55   AV_CH_BACK_CENTER, GST_AUDIO_CHANNEL_POSITION_REAR_CENTER}, {
56   AV_CH_SIDE_LEFT, GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT}, {
57   AV_CH_SIDE_RIGHT, GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT}, {
58   AV_CH_TOP_CENTER, GST_AUDIO_CHANNEL_POSITION_TOP_CENTER}, {
59   AV_CH_TOP_FRONT_LEFT, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_LEFT}, {
60   AV_CH_TOP_FRONT_CENTER, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_CENTER}, {
61   AV_CH_TOP_FRONT_RIGHT, GST_AUDIO_CHANNEL_POSITION_TOP_FRONT_RIGHT}, {
62   AV_CH_TOP_BACK_LEFT, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_LEFT}, {
63   AV_CH_TOP_BACK_CENTER, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_CENTER}, {
64   AV_CH_TOP_BACK_RIGHT, GST_AUDIO_CHANNEL_POSITION_TOP_REAR_RIGHT}, {
65   AV_CH_STEREO_LEFT, GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT}, {
66   AV_CH_STEREO_RIGHT, GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT}
67 };
68
69 gboolean
70 gst_ffmpeg_channel_layout_to_gst (AVCodecContext * context,
71     GstAudioChannelPosition * pos)
72 {
73   guint nchannels = 0, channels = context->channels;
74   guint64 channel_layout = context->channel_layout;
75   gboolean none_layout = FALSE;
76
77   if (channel_layout == 0) {
78     nchannels = channels;
79     none_layout = TRUE;
80   } else {
81     guint i, j;
82
83     for (i = 0; i < 64; i++) {
84       if ((channel_layout & (G_GUINT64_CONSTANT (1) << i)) != 0) {
85         nchannels++;
86       }
87     }
88
89     if (nchannels != channels) {
90       GST_ERROR ("Number of channels is different (%u != %u)", channels,
91           nchannels);
92       nchannels = channels;
93       none_layout = TRUE;
94     } else {
95
96       for (i = 0, j = 0; i < G_N_ELEMENTS (_ff_to_gst_layout); i++) {
97         if ((channel_layout & _ff_to_gst_layout[i].ff) != 0) {
98           pos[j++] = _ff_to_gst_layout[i].gst;
99
100           if (_ff_to_gst_layout[i].gst == GST_AUDIO_CHANNEL_POSITION_NONE)
101             none_layout = TRUE;
102         }
103       }
104
105       if (j != nchannels) {
106         GST_WARNING
107             ("Unknown channels in channel layout - assuming NONE layout");
108         none_layout = TRUE;
109       }
110     }
111   }
112
113   if (!none_layout
114       && !gst_audio_check_valid_channel_positions (pos, nchannels, FALSE)) {
115     GST_ERROR ("Invalid channel layout %" G_GUINT64_FORMAT
116         " - assuming NONE layout", channel_layout);
117     none_layout = TRUE;
118   }
119
120   if (none_layout) {
121     if (nchannels == 1) {
122       pos[0] = GST_AUDIO_CHANNEL_POSITION_MONO;
123     } else if (nchannels == 2) {
124       pos[0] = GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
125       pos[1] = GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
126     } else {
127       guint i;
128
129       for (i = 0; i < nchannels; i++)
130         pos[i] = GST_AUDIO_CHANNEL_POSITION_NONE;
131     }
132   }
133
134   return TRUE;
135 }
136
137 /* this macro makes a caps width fixed or unfixed width/height
138  * properties depending on whether we've got a context.
139  *
140  * See below for why we use this.
141  *
142  * We should actually do this stuff at the end, like in riff-media.c,
143  * but I'm too lazy today. Maybe later.
144  */
145 static GstCaps *
146 gst_ff_vid_caps_new (AVCodecContext * context, enum CodecID codec_id,
147     gboolean encode, const char *mimetype, const char *fieldname, ...)
148 {
149   GstStructure *structure = NULL;
150   GstCaps *caps = NULL;
151   va_list var_args;
152   gint i;
153
154   GST_LOG ("context:%p, codec_id:%d, mimetype:%s", context, codec_id, mimetype);
155
156   /* fixed, non probing context */
157   if (context != NULL && context->width != -1) {
158     gint num, denom;
159
160     caps = gst_caps_new_simple (mimetype,
161         "width", G_TYPE_INT, context->width,
162         "height", G_TYPE_INT, context->height, NULL);
163
164     num = context->time_base.den / context->ticks_per_frame;
165     denom = context->time_base.num;
166
167     if (!denom) {
168       GST_LOG ("invalid framerate: %d/0, -> %d/1", num, num);
169       denom = 1;
170     }
171     if (gst_util_fraction_compare (num, denom, 1000, 1) > 0) {
172       GST_LOG ("excessive framerate: %d/%d, -> 0/1", num, denom);
173       num = 0;
174       denom = 1;
175     }
176     GST_LOG ("setting framerate: %d/%d", num, denom);
177     gst_caps_set_simple (caps,
178         "framerate", GST_TYPE_FRACTION, num, denom, NULL);
179   } else if (encode) {
180     /* so we are after restricted caps in this case */
181     switch (codec_id) {
182       case CODEC_ID_H261:
183       {
184         caps = gst_caps_new_simple (mimetype,
185             "width", G_TYPE_INT, 352,
186             "height", G_TYPE_INT, 288,
187             "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
188         gst_caps_append (caps, gst_caps_new_simple (mimetype,
189                 "width", G_TYPE_INT, 176,
190                 "height", G_TYPE_INT, 144,
191                 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL));
192         break;
193       }
194       case CODEC_ID_H263:
195       {
196         /* 128x96, 176x144, 352x288, 704x576, and 1408x1152. slightly reordered
197          * because we want automatic negotiation to go as close to 320x240 as
198          * possible. */
199         const static gint widths[] = { 352, 704, 176, 1408, 128 };
200         const static gint heights[] = { 288, 576, 144, 1152, 96 };
201         GstCaps *temp;
202         gint n_sizes = G_N_ELEMENTS (widths);
203
204         caps = gst_caps_new_empty ();
205         for (i = 0; i < n_sizes; i++) {
206           temp = gst_caps_new_simple (mimetype,
207               "width", G_TYPE_INT, widths[i],
208               "height", G_TYPE_INT, heights[i],
209               "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
210
211           gst_caps_append (caps, temp);
212         }
213         break;
214       }
215       case CODEC_ID_DVVIDEO:
216       {
217         static struct
218         {
219           guint32 csp;
220           gint width, height;
221           gint par_n, par_d;
222           gint framerate_n, framerate_d;
223         } profiles[] = {
224           {
225           GST_MAKE_FOURCC ('Y', '4', '1', 'B'), 720, 480, 10, 11, 30000, 1001}, {
226           GST_MAKE_FOURCC ('Y', '4', '1', 'B'), 720, 480, 40, 33, 30000, 1001}, {
227           GST_MAKE_FOURCC ('I', '4', '2', '0'), 720, 576, 59, 54, 25, 1}, {
228           GST_MAKE_FOURCC ('I', '4', '2', '0'), 720, 576, 118, 81, 25, 1}, {
229           GST_MAKE_FOURCC ('Y', '4', '1', 'B'), 720, 576, 59, 54, 25, 1}, {
230           GST_MAKE_FOURCC ('Y', '4', '1', 'B'), 720, 576, 118, 81, 25, 1}
231         };
232         GstCaps *temp;
233         gint n_sizes = G_N_ELEMENTS (profiles);
234
235         caps = gst_caps_new_empty ();
236         for (i = 0; i < n_sizes; i++) {
237           temp = gst_caps_new_simple (mimetype,
238               "width", G_TYPE_INT, profiles[i].width,
239               "height", G_TYPE_INT, profiles[i].height,
240               "framerate", GST_TYPE_FRACTION, profiles[i].framerate_n,
241               profiles[i].framerate_d, "pixel-aspect-ratio", GST_TYPE_FRACTION,
242               profiles[i].par_n, profiles[i].par_d, NULL);
243
244           gst_caps_append (caps, temp);
245         }
246         break;
247       }
248       case CODEC_ID_DNXHD:
249       {
250         caps = gst_caps_new_simple (mimetype,
251             "width", G_TYPE_INT, 1920,
252             "height", G_TYPE_INT, 1080,
253             "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
254         gst_caps_append (caps, gst_caps_new_simple (mimetype,
255                 "width", G_TYPE_INT, 1280,
256                 "height", G_TYPE_INT, 720,
257                 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL));
258         break;
259       }
260       default:
261         break;
262     }
263   }
264
265   /* no fixed caps or special restrictions applied;
266    * default unfixed setting */
267   if (!caps) {
268     GST_DEBUG ("Creating default caps");
269     caps = gst_caps_new_simple (mimetype, NULL, NULL, NULL);
270   }
271
272   for (i = 0; i < gst_caps_get_size (caps); i++) {
273     va_start (var_args, fieldname);
274     structure = gst_caps_get_structure (caps, i);
275     gst_structure_set_valist (structure, fieldname, var_args);
276     va_end (var_args);
277   }
278
279   return caps;
280 }
281
282 /* same for audio - now with channels/sample rate
283  */
284 static GstCaps *
285 gst_ff_aud_caps_new (AVCodecContext * context, enum CodecID codec_id,
286     gboolean encode, const char *mimetype, const char *fieldname, ...)
287 {
288   GstCaps *caps = NULL;
289   GstStructure *structure = NULL;
290   gint i;
291   va_list var_args;
292
293   /* fixed, non-probing context */
294   if (context != NULL && context->channels != -1) {
295     GstAudioChannelPosition pos[64];
296
297     caps = gst_caps_new_simple (mimetype,
298         "rate", G_TYPE_INT, context->sample_rate,
299         "channels", G_TYPE_INT, context->channels, NULL);
300
301     if (gst_ffmpeg_channel_layout_to_gst (context, pos)) {
302       guint64 mask;
303
304       if (gst_audio_channel_positions_to_mask (pos, context->channels, FALSE,
305               &mask)) {
306         gst_caps_set_simple (caps, "channel-mask", GST_TYPE_BITMASK, mask,
307             NULL);
308       }
309     }
310   } else if (encode) {
311     gint maxchannels = 2;
312     const gint *rates = NULL;
313     gint n_rates = 0;
314
315     /* so we must be after restricted caps in this case */
316     switch (codec_id) {
317       case CODEC_ID_AAC:
318       case CODEC_ID_AAC_LATM:
319       case CODEC_ID_DTS:
320         maxchannels = 6;
321         break;
322       case CODEC_ID_MP2:
323       {
324         const static gint l_rates[] =
325             { 48000, 44100, 32000, 24000, 22050, 16000 };
326         n_rates = G_N_ELEMENTS (l_rates);
327         rates = l_rates;
328         break;
329       }
330       case CODEC_ID_EAC3:
331       case CODEC_ID_AC3:
332       {
333         const static gint l_rates[] = { 48000, 44100, 32000 };
334         maxchannels = 6;
335         n_rates = G_N_ELEMENTS (l_rates);
336         rates = l_rates;
337         break;
338       }
339       case CODEC_ID_ADPCM_G722:
340       {
341         const static gint l_rates[] = { 16000 };
342         n_rates = G_N_ELEMENTS (l_rates);
343         rates = l_rates;
344         maxchannels = 1;
345         break;
346       }
347       case CODEC_ID_ADPCM_G726:
348       {
349         const static gint l_rates[] = { 8000 };
350         n_rates = G_N_ELEMENTS (l_rates);
351         rates = l_rates;
352         maxchannels = 1;
353         break;
354       }
355       case CODEC_ID_ADPCM_SWF:
356       {
357         const static gint l_rates[] = { 11025, 22050, 44100 };
358         n_rates = G_N_ELEMENTS (l_rates);
359         rates = l_rates;
360         break;
361       }
362       case CODEC_ID_ROQ_DPCM:
363       {
364         const static gint l_rates[] = { 22050 };
365         n_rates = G_N_ELEMENTS (l_rates);
366         rates = l_rates;
367         break;
368       }
369       case CODEC_ID_AMR_NB:
370       {
371         const static gint l_rates[] = { 8000 };
372         maxchannels = 1;
373         n_rates = G_N_ELEMENTS (l_rates);
374         rates = l_rates;
375         break;
376       }
377       case CODEC_ID_AMR_WB:
378       {
379         const static gint l_rates[] = { 16000 };
380         maxchannels = 1;
381         n_rates = G_N_ELEMENTS (l_rates);
382         rates = l_rates;
383         break;
384       }
385       default:
386         break;
387     }
388
389     /* TODO: handle context->channel_layouts here to set
390      * the list of channel layouts supported by the encoder.
391      * Unfortunately no encoder uses this yet....
392      */
393     /* regardless of encode/decode, open up channels if applicable */
394     /* Until decoders/encoders expose the maximum number of channels
395      * they support, we whitelist them here. */
396     switch (codec_id) {
397       case CODEC_ID_WMAPRO:
398       case CODEC_ID_TRUEHD:
399         maxchannels = 8;
400         break;
401       default:
402         break;
403     }
404
405     if (maxchannels == 1)
406       caps = gst_caps_new_simple (mimetype,
407           "channels", G_TYPE_INT, maxchannels, NULL);
408     else
409       caps = gst_caps_new_simple (mimetype,
410           "channels", GST_TYPE_INT_RANGE, 1, maxchannels, NULL);
411     if (n_rates) {
412       GValue list = { 0, };
413       GstStructure *structure;
414
415       g_value_init (&list, GST_TYPE_LIST);
416       for (i = 0; i < n_rates; i++) {
417         GValue v = { 0, };
418
419         g_value_init (&v, G_TYPE_INT);
420         g_value_set_int (&v, rates[i]);
421         gst_value_list_append_value (&list, &v);
422         g_value_unset (&v);
423       }
424       structure = gst_caps_get_structure (caps, 0);
425       gst_structure_set_value (structure, "rate", &list);
426       g_value_unset (&list);
427     } else
428       gst_caps_set_simple (caps, "rate", GST_TYPE_INT_RANGE, 4000, 96000, NULL);
429   } else {
430     caps = gst_caps_new_empty_simple (mimetype);
431   }
432
433   for (i = 0; i < gst_caps_get_size (caps); i++) {
434     va_start (var_args, fieldname);
435     structure = gst_caps_get_structure (caps, i);
436     gst_structure_set_valist (structure, fieldname, var_args);
437     va_end (var_args);
438   }
439
440   return caps;
441 }
442
443 /* Convert a FFMPEG codec ID and optional AVCodecContext
444  * to a GstCaps. If the context is ommitted, no fixed values
445  * for video/audio size will be included in the GstCaps
446  *
447  * CodecID is primarily meant for compressed data GstCaps!
448  *
449  * encode is a special parameter. gstffmpegdec will say
450  * FALSE, gstffmpegenc will say TRUE. The output caps
451  * depends on this, in such a way that it will be very
452  * specific, defined, fixed and correct caps for encoders,
453  * yet very wide, "forgiving" caps for decoders. Example
454  * for mp3: decode: audio/mpeg,mpegversion=1,layer=[1-3]
455  * but encode: audio/mpeg,mpegversion=1,layer=3,bitrate=x,
456  * rate=x,channels=x.
457  */
458
459 GstCaps *
460 gst_ffmpeg_codecid_to_caps (enum CodecID codec_id,
461     AVCodecContext * context, gboolean encode)
462 {
463   GstCaps *caps = NULL;
464   gboolean buildcaps = FALSE;
465
466   GST_LOG ("codec_id:%d, context:%p, encode:%d", codec_id, context, encode);
467
468   switch (codec_id) {
469     case CODEC_ID_MPEG1VIDEO:
470       /* FIXME: bitrate */
471       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/mpeg",
472           "mpegversion", G_TYPE_INT, 1,
473           "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
474       break;
475
476     case CODEC_ID_MPEG2VIDEO:
477       if (encode) {
478         /* FIXME: bitrate */
479         caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/mpeg",
480             "mpegversion", G_TYPE_INT, 2,
481             "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
482       } else {
483         /* decode both MPEG-1 and MPEG-2; width/height/fps are all in
484          * the MPEG video stream headers, so may be omitted from caps. */
485         caps = gst_caps_new_simple ("video/mpeg",
486             "mpegversion", GST_TYPE_INT_RANGE, 1, 2,
487             "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
488       }
489       break;
490
491     case CODEC_ID_MPEG2VIDEO_XVMC:
492       /* this is a special ID - don't need it in GStreamer, I think */
493       break;
494
495     case CODEC_ID_H263:
496       if (encode) {
497         caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-h263",
498             "variant", G_TYPE_STRING, "itu",
499             "h263version", G_TYPE_STRING, "h263", NULL);
500       } else {
501         /* don't pass codec_id, we can decode other variants with the H263
502          * decoder that don't have specific size requirements
503          */
504         caps =
505             gst_ff_vid_caps_new (context, CODEC_ID_NONE, encode, "video/x-h263",
506             "variant", G_TYPE_STRING, "itu", NULL);
507       }
508       break;
509
510     case CODEC_ID_H263P:
511       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-h263",
512           "variant", G_TYPE_STRING, "itu",
513           "h263version", G_TYPE_STRING, "h263p", NULL);
514       if (encode && context) {
515
516         gst_caps_set_simple (caps,
517             "annex-f", G_TYPE_BOOLEAN, context->flags & CODEC_FLAG_4MV,
518             "annex-j", G_TYPE_BOOLEAN, context->flags & CODEC_FLAG_LOOP_FILTER,
519             "annex-i", G_TYPE_BOOLEAN, context->flags & CODEC_FLAG_AC_PRED,
520             "annex-t", G_TYPE_BOOLEAN, context->flags & CODEC_FLAG_AC_PRED,
521             NULL);
522       }
523       break;
524
525     case CODEC_ID_H263I:
526       caps =
527           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-intel-h263",
528           "variant", G_TYPE_STRING, "intel", NULL);
529       break;
530
531     case CODEC_ID_H261:
532       caps =
533           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-h261", NULL);
534       break;
535
536     case CODEC_ID_RV10:
537     case CODEC_ID_RV20:
538     case CODEC_ID_RV30:
539     case CODEC_ID_RV40:
540     {
541       gint version;
542
543       switch (codec_id) {
544         case CODEC_ID_RV40:
545           version = 4;
546           break;
547         case CODEC_ID_RV30:
548           version = 3;
549           break;
550         case CODEC_ID_RV20:
551           version = 2;
552           break;
553         default:
554           version = 1;
555           break;
556       }
557
558       /* FIXME: context->sub_id must be filled in during decoding */
559       caps =
560           gst_ff_vid_caps_new (context, codec_id, encode,
561           "video/x-pn-realvideo", "systemstream", G_TYPE_BOOLEAN, FALSE,
562           "rmversion", G_TYPE_INT, version, NULL);
563       if (context) {
564         gst_caps_set_simple (caps, "format", G_TYPE_INT, context->sub_id, NULL);
565         if (context->extradata_size >= 8) {
566           gst_caps_set_simple (caps,
567               "subformat", G_TYPE_INT, GST_READ_UINT32_BE (context->extradata),
568               NULL);
569         }
570       }
571     }
572       break;
573
574     case CODEC_ID_MP1:
575       /* FIXME: bitrate */
576       caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/mpeg",
577           "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 1, NULL);
578       break;
579
580     case CODEC_ID_MP2:
581       /* FIXME: bitrate */
582       caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/mpeg",
583           "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 2, NULL);
584       break;
585
586     case CODEC_ID_MP3:
587       if (encode) {
588         /* FIXME: bitrate */
589         caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/mpeg",
590             "mpegversion", G_TYPE_INT, 1, "layer", G_TYPE_INT, 3, NULL);
591       } else {
592         /* Decodes MPEG-1 layer 1/2/3. Samplerate, channels et al are
593          * in the MPEG audio header, so may be omitted from caps. */
594         caps = gst_caps_new_simple ("audio/mpeg",
595             "mpegversion", G_TYPE_INT, 1,
596             "layer", GST_TYPE_INT_RANGE, 1, 3, NULL);
597       }
598       break;
599
600     case CODEC_ID_MUSEPACK7:
601       caps =
602           gst_ff_aud_caps_new (context, codec_id, encode,
603           "audio/x-ffmpeg-parsed-musepack", "streamversion", G_TYPE_INT, 7,
604           NULL);
605       break;
606
607     case CODEC_ID_MUSEPACK8:
608       caps =
609           gst_ff_aud_caps_new (context, codec_id, encode,
610           "audio/x-ffmpeg-parsed-musepack", "streamversion", G_TYPE_INT, 8,
611           NULL);
612       break;
613
614     case CODEC_ID_AC3:
615       /* FIXME: bitrate */
616       caps =
617           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-ac3", NULL);
618       break;
619
620     case CODEC_ID_EAC3:
621       /* FIXME: bitrate */
622       caps =
623           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-eac3", NULL);
624       break;
625
626     case CODEC_ID_TRUEHD:
627       caps =
628           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-true-hd",
629           NULL);
630       break;
631
632     case CODEC_ID_ATRAC1:
633       caps =
634           gst_ff_aud_caps_new (context, codec_id, encode,
635           "audio/x-vnd.sony.atrac1", NULL);
636       break;
637
638     case CODEC_ID_ATRAC3:
639       caps =
640           gst_ff_aud_caps_new (context, codec_id, encode,
641           "audio/x-vnd.sony.atrac3", NULL);
642       break;
643
644     case CODEC_ID_DTS:
645       caps =
646           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-dts", NULL);
647       break;
648
649     case CODEC_ID_APE:
650       caps =
651           gst_ff_aud_caps_new (context, codec_id, encode,
652           "audio/x-ffmpeg-parsed-ape", NULL);
653       if (context) {
654         gst_caps_set_simple (caps,
655             "depth", G_TYPE_INT, context->bits_per_coded_sample, NULL);
656       }
657       break;
658
659     case CODEC_ID_MLP:
660       caps =
661           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-mlp", NULL);
662       break;
663
664     case CODEC_ID_IMC:
665       caps =
666           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-imc", NULL);
667       break;
668
669       /* MJPEG is normal JPEG, Motion-JPEG and Quicktime MJPEG-A. MJPEGB
670        * is Quicktime's MJPEG-B. LJPEG is lossless JPEG. I don't know what
671        * sp5x is, but it's apparently something JPEG... We don't separate
672        * between those in GStreamer. Should we (at least between MJPEG,
673        * MJPEG-B and sp5x decoding...)? */
674     case CODEC_ID_MJPEG:
675     case CODEC_ID_LJPEG:
676       caps =
677           gst_ff_vid_caps_new (context, codec_id, encode, "image/jpeg", NULL);
678       break;
679
680     case CODEC_ID_SP5X:
681       caps =
682           gst_ff_vid_caps_new (context, codec_id, encode, "video/sp5x", NULL);
683       break;
684
685     case CODEC_ID_MJPEGB:
686       caps =
687           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-mjpeg-b",
688           NULL);
689       break;
690
691     case CODEC_ID_MPEG4:
692       if (encode && context != NULL) {
693         /* I'm not exactly sure what ffmpeg outputs... ffmpeg itself uses
694          * the AVI fourcc 'DIVX', but 'mp4v' for Quicktime... */
695         switch (context->codec_tag) {
696           case GST_MAKE_FOURCC ('D', 'I', 'V', 'X'):
697             caps =
698                 gst_ff_vid_caps_new (context, codec_id, encode, "video/x-divx",
699                 "divxversion", G_TYPE_INT, 5, NULL);
700             break;
701           case GST_MAKE_FOURCC ('m', 'p', '4', 'v'):
702           default:
703             /* FIXME: bitrate */
704             caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/mpeg",
705                 "systemstream", G_TYPE_BOOLEAN, FALSE,
706                 "mpegversion", G_TYPE_INT, 4, NULL);
707             break;
708         }
709       } else {
710         /* The trick here is to separate xvid, divx, mpeg4, 3ivx et al */
711         caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/mpeg",
712             "mpegversion", G_TYPE_INT, 4,
713             "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
714         if (encode) {
715           gst_caps_append (caps, gst_ff_vid_caps_new (context, codec_id, encode,
716                   "video/x-divx", "divxversion", G_TYPE_INT, 5, NULL));
717         } else {
718           gst_caps_append (caps, gst_ff_vid_caps_new (context, codec_id, encode,
719                   "video/x-divx", "divxversion", GST_TYPE_INT_RANGE, 4, 5,
720                   NULL));
721           gst_caps_append (caps, gst_ff_vid_caps_new (context, codec_id, encode,
722                   "video/x-xvid", NULL));
723           gst_caps_append (caps, gst_ff_vid_caps_new (context, codec_id, encode,
724                   "video/x-3ivx", NULL));
725         }
726       }
727       break;
728
729     case CODEC_ID_RAWVIDEO:
730       caps =
731           gst_ffmpeg_codectype_to_caps (AVMEDIA_TYPE_VIDEO, context, codec_id,
732           encode);
733       break;
734
735     case CODEC_ID_MSMPEG4V1:
736     case CODEC_ID_MSMPEG4V2:
737     case CODEC_ID_MSMPEG4V3:
738     {
739       gint version = 41 + codec_id - CODEC_ID_MSMPEG4V1;
740
741       /* encode-FIXME: bitrate */
742       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-msmpeg",
743           "msmpegversion", G_TYPE_INT, version, NULL);
744       if (!encode && codec_id == CODEC_ID_MSMPEG4V3) {
745         gst_caps_append (caps, gst_ff_vid_caps_new (context, codec_id, encode,
746                 "video/x-divx", "divxversion", G_TYPE_INT, 3, NULL));
747       }
748     }
749       break;
750
751     case CODEC_ID_WMV1:
752     case CODEC_ID_WMV2:
753     {
754       gint version = (codec_id == CODEC_ID_WMV1) ? 1 : 2;
755
756       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-wmv",
757           "wmvversion", G_TYPE_INT, version, NULL);
758     }
759       break;
760
761     case CODEC_ID_FLV1:
762       caps =
763           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-flash-video",
764           "flvversion", G_TYPE_INT, 1, NULL);
765       break;
766
767     case CODEC_ID_SVQ1:
768       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-svq",
769           "svqversion", G_TYPE_INT, 1, NULL);
770       break;
771
772     case CODEC_ID_SVQ3:
773       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-svq",
774           "svqversion", G_TYPE_INT, 3, NULL);
775       break;
776
777     case CODEC_ID_DVAUDIO:
778       caps =
779           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-dv", NULL);
780       break;
781
782     case CODEC_ID_DVVIDEO:
783     {
784       if (encode && context) {
785         const gchar *format;
786
787         switch (context->pix_fmt) {
788           case PIX_FMT_YUYV422:
789             format = "YUY2";
790             break;
791           case PIX_FMT_YUV420P:
792             format = "I420";
793             break;
794           case PIX_FMT_YUVA420P:
795             format = "A420";
796             break;
797           case PIX_FMT_YUV411P:
798             format = "Y41B";
799             break;
800           case PIX_FMT_YUV422P:
801             format = "Y42B";
802             break;
803           case PIX_FMT_YUV410P:
804             format = "YUV9";
805             break;
806           default:
807             GST_WARNING
808                 ("Couldnt' find format for pixfmt %d, defaulting to I420",
809                 context->pix_fmt);
810             format = "I420";
811             break;
812         }
813         caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-dv",
814             "systemstream", G_TYPE_BOOLEAN, FALSE,
815             "format", G_TYPE_STRING, format, NULL);
816       } else {
817         caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-dv",
818             "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
819       }
820     }
821       break;
822
823     case CODEC_ID_WMAV1:
824     case CODEC_ID_WMAV2:
825     {
826       gint version = (codec_id == CODEC_ID_WMAV1) ? 1 : 2;
827
828       if (context) {
829         caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-wma",
830             "wmaversion", G_TYPE_INT, version,
831             "block_align", G_TYPE_INT, context->block_align,
832             "bitrate", G_TYPE_INT, context->bit_rate, NULL);
833       } else {
834         caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-wma",
835             "wmaversion", G_TYPE_INT, version,
836             "block_align", GST_TYPE_INT_RANGE, 0, G_MAXINT,
837             "bitrate", GST_TYPE_INT_RANGE, 0, G_MAXINT, NULL);
838       }
839     }
840       break;
841     case CODEC_ID_WMAPRO:
842     {
843       caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-wma",
844           "wmaversion", G_TYPE_INT, 3, NULL);
845       break;
846     }
847
848     case CODEC_ID_WMAVOICE:
849     {
850       caps =
851           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-wms", NULL);
852       break;
853     }
854
855     case CODEC_ID_MACE3:
856     case CODEC_ID_MACE6:
857     {
858       gint version = (codec_id == CODEC_ID_MACE3) ? 3 : 6;
859
860       caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-mace",
861           "maceversion", G_TYPE_INT, version, NULL);
862     }
863       break;
864
865     case CODEC_ID_HUFFYUV:
866       caps =
867           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-huffyuv",
868           NULL);
869       if (context) {
870         gst_caps_set_simple (caps,
871             "bpp", G_TYPE_INT, context->bits_per_coded_sample, NULL);
872       }
873       break;
874
875     case CODEC_ID_CYUV:
876       caps =
877           gst_ff_vid_caps_new (context, codec_id, encode,
878           "video/x-compressed-yuv", NULL);
879       break;
880
881     case CODEC_ID_H264:
882       caps =
883           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-h264",
884           "alignment", G_TYPE_STRING, "au", NULL);
885       break;
886
887     case CODEC_ID_INDEO5:
888       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-indeo",
889           "indeoversion", G_TYPE_INT, 5, NULL);
890       break;
891
892     case CODEC_ID_INDEO4:
893       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-indeo",
894           "indeoversion", G_TYPE_INT, 4, NULL);
895       break;
896
897     case CODEC_ID_INDEO3:
898       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-indeo",
899           "indeoversion", G_TYPE_INT, 3, NULL);
900       break;
901
902     case CODEC_ID_INDEO2:
903       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-indeo",
904           "indeoversion", G_TYPE_INT, 2, NULL);
905       break;
906
907     case CODEC_ID_FLASHSV:
908       caps =
909           gst_ff_vid_caps_new (context, codec_id, encode,
910           "video/x-flash-screen", NULL);
911       break;
912
913     case CODEC_ID_VP3:
914       caps =
915           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-vp3", NULL);
916       break;
917
918     case CODEC_ID_VP5:
919       caps =
920           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-vp5", NULL);
921       break;
922
923     case CODEC_ID_VP6:
924       caps =
925           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-vp6", NULL);
926       break;
927
928     case CODEC_ID_VP6F:
929       caps =
930           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-vp6-flash",
931           NULL);
932       break;
933
934     case CODEC_ID_VP6A:
935       caps =
936           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-vp6-alpha",
937           NULL);
938       break;
939
940     case CODEC_ID_VP8:
941       caps =
942           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-vp8", NULL);
943       break;
944
945     case CODEC_ID_THEORA:
946       caps =
947           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-theora",
948           NULL);
949       break;
950
951     case CODEC_ID_AAC:
952     {
953       caps =
954           gst_ff_aud_caps_new (context, codec_id, encode, "audio/mpeg", NULL);
955
956       if (!encode) {
957         GValue arr = { 0, };
958         GValue item = { 0, };
959
960         g_value_init (&arr, GST_TYPE_LIST);
961         g_value_init (&item, G_TYPE_INT);
962         g_value_set_int (&item, 2);
963         gst_value_list_append_value (&arr, &item);
964         g_value_set_int (&item, 4);
965         gst_value_list_append_value (&arr, &item);
966         g_value_unset (&item);
967
968         gst_caps_set_value (caps, "mpegversion", &arr);
969         g_value_unset (&arr);
970
971         g_value_init (&arr, GST_TYPE_LIST);
972         g_value_init (&item, G_TYPE_STRING);
973         g_value_set_string (&item, "raw");
974         gst_value_list_append_value (&arr, &item);
975         g_value_set_string (&item, "adts");
976         gst_value_list_append_value (&arr, &item);
977         g_value_set_string (&item, "adif");
978         gst_value_list_append_value (&arr, &item);
979         g_value_unset (&item);
980
981         gst_caps_set_value (caps, "stream-format", &arr);
982         g_value_unset (&arr);
983       } else {
984         gst_caps_set_simple (caps, "mpegversion", G_TYPE_INT, 4,
985             "stream-format", G_TYPE_STRING, "raw",
986             "base-profile", G_TYPE_STRING, "lc", NULL);
987
988         if (context && context->extradata_size > 0)
989           gst_codec_utils_aac_caps_set_level_and_profile (caps,
990               context->extradata, context->extradata_size);
991       }
992
993       break;
994     }
995     case CODEC_ID_AAC_LATM:    /* LATM/LOAS AAC syntax */
996       caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/mpeg",
997           "mpegversion", G_TYPE_INT, 4, "stream-format", G_TYPE_STRING, "loas",
998           NULL);
999       break;
1000
1001     case CODEC_ID_ASV1:
1002       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-asus",
1003           "asusversion", G_TYPE_INT, 1, NULL);
1004       break;
1005     case CODEC_ID_ASV2:
1006       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-asus",
1007           "asusversion", G_TYPE_INT, 2, NULL);
1008       break;
1009
1010     case CODEC_ID_FFV1:
1011       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-ffv",
1012           "ffvversion", G_TYPE_INT, 1, NULL);
1013       break;
1014
1015     case CODEC_ID_4XM:
1016       caps =
1017           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-4xm", NULL);
1018       break;
1019
1020     case CODEC_ID_XAN_WC3:
1021     case CODEC_ID_XAN_WC4:
1022       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-xan",
1023           "wcversion", G_TYPE_INT, 3 - CODEC_ID_XAN_WC3 + codec_id, NULL);
1024       break;
1025
1026     case CODEC_ID_CLJR:
1027       caps =
1028           gst_ff_vid_caps_new (context, codec_id, encode,
1029           "video/x-cirrus-logic-accupak", NULL);
1030       break;
1031
1032     case CODEC_ID_FRAPS:
1033       caps =
1034           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-fraps",
1035           NULL);
1036       break;
1037
1038     case CODEC_ID_MDEC:
1039     case CODEC_ID_ROQ:
1040     case CODEC_ID_INTERPLAY_VIDEO:
1041       buildcaps = TRUE;
1042       break;
1043
1044     case CODEC_ID_VCR1:
1045       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-ati-vcr",
1046           "vcrversion", G_TYPE_INT, 1, NULL);
1047       break;
1048
1049     case CODEC_ID_RPZA:
1050       caps =
1051           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-apple-video",
1052           NULL);
1053       break;
1054
1055     case CODEC_ID_CINEPAK:
1056       caps =
1057           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-cinepak",
1058           NULL);
1059       break;
1060
1061       /* WS_VQA belogns here (order) */
1062
1063     case CODEC_ID_MSRLE:
1064       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-rle",
1065           "layout", G_TYPE_STRING, "microsoft", NULL);
1066       if (context) {
1067         gst_caps_set_simple (caps,
1068             "depth", G_TYPE_INT, (gint) context->bits_per_coded_sample, NULL);
1069       } else {
1070         gst_caps_set_simple (caps, "depth", GST_TYPE_INT_RANGE, 1, 64, NULL);
1071       }
1072       break;
1073
1074     case CODEC_ID_QTRLE:
1075       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-rle",
1076           "layout", G_TYPE_STRING, "quicktime", NULL);
1077       if (context) {
1078         gst_caps_set_simple (caps,
1079             "depth", G_TYPE_INT, (gint) context->bits_per_coded_sample, NULL);
1080       } else {
1081         gst_caps_set_simple (caps, "depth", GST_TYPE_INT_RANGE, 1, 64, NULL);
1082       }
1083       break;
1084
1085     case CODEC_ID_MSVIDEO1:
1086       caps =
1087           gst_ff_vid_caps_new (context, codec_id, encode,
1088           "video/x-msvideocodec", "msvideoversion", G_TYPE_INT, 1, NULL);
1089       break;
1090
1091     case CODEC_ID_WMV3:
1092       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-wmv",
1093           "wmvversion", G_TYPE_INT, 3, NULL);
1094       break;
1095     case CODEC_ID_VC1:
1096       caps = gst_ff_vid_caps_new (context, codec_id, encode, "video/x-wmv",
1097           "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1098       break;
1099     case CODEC_ID_QDM2:
1100       caps =
1101           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-qdm2", NULL);
1102       break;
1103
1104     case CODEC_ID_MSZH:
1105       caps =
1106           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-mszh", NULL);
1107       break;
1108
1109     case CODEC_ID_ZLIB:
1110       caps =
1111           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-zlib", NULL);
1112       break;
1113
1114     case CODEC_ID_TRUEMOTION1:
1115       caps =
1116           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-truemotion",
1117           "trueversion", G_TYPE_INT, 1, NULL);
1118       break;
1119     case CODEC_ID_TRUEMOTION2:
1120       caps =
1121           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-truemotion",
1122           "trueversion", G_TYPE_INT, 2, NULL);
1123       break;
1124
1125     case CODEC_ID_ULTI:
1126       caps =
1127           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-ultimotion",
1128           NULL);
1129       break;
1130
1131     case CODEC_ID_TSCC:
1132       caps =
1133           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-camtasia",
1134           NULL);
1135       if (context) {
1136         gst_caps_set_simple (caps,
1137             "depth", G_TYPE_INT, (gint) context->bits_per_coded_sample, NULL);
1138       } else {
1139         gst_caps_set_simple (caps, "depth", GST_TYPE_INT_RANGE, 8, 32, NULL);
1140       }
1141       break;
1142
1143     case CODEC_ID_KMVC:
1144       caps =
1145           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-kmvc", NULL);
1146       break;
1147
1148     case CODEC_ID_NUV:
1149       caps =
1150           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-nuv", NULL);
1151       break;
1152
1153     case CODEC_ID_GIF:
1154       caps = gst_ff_vid_caps_new (context, codec_id, encode, "image/gif", NULL);
1155       break;
1156
1157     case CODEC_ID_PNG:
1158       caps = gst_ff_vid_caps_new (context, codec_id, encode, "image/png", NULL);
1159       break;
1160
1161     case CODEC_ID_PPM:
1162       caps = gst_ff_vid_caps_new (context, codec_id, encode, "image/ppm", NULL);
1163       break;
1164
1165     case CODEC_ID_PBM:
1166       caps = gst_ff_vid_caps_new (context, codec_id, encode, "image/pbm", NULL);
1167       break;
1168
1169     case CODEC_ID_PAM:
1170       caps =
1171           gst_ff_vid_caps_new (context, codec_id, encode,
1172           "image/x-portable-anymap", NULL);
1173       break;
1174
1175     case CODEC_ID_PGM:
1176       caps =
1177           gst_ff_vid_caps_new (context, codec_id, encode,
1178           "image/x-portable-graymap", NULL);
1179       break;
1180
1181     case CODEC_ID_PCX:
1182       caps =
1183           gst_ff_vid_caps_new (context, codec_id, encode, "image/x-pcx", NULL);
1184       break;
1185
1186     case CODEC_ID_SGI:
1187       caps =
1188           gst_ff_vid_caps_new (context, codec_id, encode, "image/x-sgi", NULL);
1189       break;
1190
1191     case CODEC_ID_TARGA:
1192       caps =
1193           gst_ff_vid_caps_new (context, codec_id, encode, "image/x-tga", NULL);
1194       break;
1195
1196     case CODEC_ID_TIFF:
1197       caps =
1198           gst_ff_vid_caps_new (context, codec_id, encode, "image/tiff", NULL);
1199       break;
1200
1201     case CODEC_ID_SUNRAST:
1202       caps =
1203           gst_ff_vid_caps_new (context, codec_id, encode, "image/x-sun-raster",
1204           NULL);
1205       break;
1206
1207     case CODEC_ID_SMC:
1208       caps =
1209           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-smc", NULL);
1210       break;
1211
1212     case CODEC_ID_QDRAW:
1213       caps =
1214           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-qdrw", NULL);
1215       break;
1216
1217     case CODEC_ID_DNXHD:
1218       caps =
1219           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-dnxhd",
1220           NULL);
1221       break;
1222
1223     case CODEC_ID_PRORES:
1224       caps =
1225           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-prores",
1226           NULL);
1227       break;
1228
1229     case CODEC_ID_MIMIC:
1230       caps =
1231           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-mimic",
1232           NULL);
1233       break;
1234
1235     case CODEC_ID_VMNC:
1236       caps =
1237           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-vmnc", NULL);
1238       break;
1239
1240     case CODEC_ID_TRUESPEECH:
1241       caps =
1242           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-truespeech",
1243           NULL);
1244       break;
1245
1246     case CODEC_ID_QCELP:
1247       caps =
1248           gst_ff_aud_caps_new (context, codec_id, encode, "audio/qcelp", NULL);
1249       break;
1250
1251     case CODEC_ID_AMV:
1252       caps =
1253           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-amv", NULL);
1254       break;
1255
1256     case CODEC_ID_AASC:
1257       caps =
1258           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-aasc", NULL);
1259       break;
1260
1261     case CODEC_ID_LOCO:
1262       caps =
1263           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-loco", NULL);
1264       break;
1265
1266     case CODEC_ID_ZMBV:
1267       caps =
1268           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-zmbv", NULL);
1269       break;
1270
1271     case CODEC_ID_LAGARITH:
1272       caps =
1273           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-lagarith",
1274           NULL);
1275       break;
1276
1277     case CODEC_ID_CSCD:
1278       caps =
1279           gst_ff_vid_caps_new (context, codec_id, encode, "video/x-camstudio",
1280           NULL);
1281       if (context) {
1282         gst_caps_set_simple (caps,
1283             "depth", G_TYPE_INT, (gint) context->bits_per_coded_sample, NULL);
1284       } else {
1285         gst_caps_set_simple (caps, "depth", GST_TYPE_INT_RANGE, 8, 32, NULL);
1286       }
1287       break;
1288
1289     case CODEC_ID_WS_VQA:
1290     case CODEC_ID_IDCIN:
1291     case CODEC_ID_8BPS:
1292     case CODEC_ID_FLIC:
1293     case CODEC_ID_VMDVIDEO:
1294     case CODEC_ID_VMDAUDIO:
1295     case CODEC_ID_SNOW:
1296     case CODEC_ID_VIXL:
1297     case CODEC_ID_QPEG:
1298     case CODEC_ID_PGMYUV:
1299     case CODEC_ID_FFVHUFF:
1300     case CODEC_ID_WNV1:
1301     case CODEC_ID_MP3ADU:
1302     case CODEC_ID_MP3ON4:
1303     case CODEC_ID_WESTWOOD_SND1:
1304     case CODEC_ID_MMVIDEO:
1305     case CODEC_ID_AVS:
1306     case CODEC_ID_CAVS:
1307       buildcaps = TRUE;
1308       break;
1309
1310       /* weird quasi-codecs for the demuxers only */
1311     case CODEC_ID_PCM_S16LE:
1312     case CODEC_ID_PCM_S16BE:
1313     case CODEC_ID_PCM_U16LE:
1314     case CODEC_ID_PCM_U16BE:
1315     case CODEC_ID_PCM_S8:
1316     case CODEC_ID_PCM_U8:
1317     {
1318       GstAudioFormat format;
1319
1320       switch (codec_id) {
1321         case CODEC_ID_PCM_S16LE:
1322           format = GST_AUDIO_FORMAT_S16LE;
1323           break;
1324         case CODEC_ID_PCM_S16BE:
1325           format = GST_AUDIO_FORMAT_S16BE;
1326           break;
1327         case CODEC_ID_PCM_U16LE:
1328           format = GST_AUDIO_FORMAT_U16LE;
1329           break;
1330         case CODEC_ID_PCM_U16BE:
1331           format = GST_AUDIO_FORMAT_U16BE;
1332           break;
1333         case CODEC_ID_PCM_S8:
1334           format = GST_AUDIO_FORMAT_S8;
1335           break;
1336         case CODEC_ID_PCM_U8:
1337           format = GST_AUDIO_FORMAT_U8;
1338           break;
1339         default:
1340           g_assert (0);         /* don't worry, we never get here */
1341           break;
1342       }
1343
1344       caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-raw",
1345           "format", G_TYPE_STRING, gst_audio_format_to_string (format),
1346           "layout", G_TYPE_STRING, "interleaved", NULL);
1347     }
1348       break;
1349
1350     case CODEC_ID_PCM_MULAW:
1351       caps =
1352           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-mulaw",
1353           NULL);
1354       break;
1355
1356     case CODEC_ID_PCM_ALAW:
1357       caps =
1358           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-alaw", NULL);
1359       break;
1360
1361     case CODEC_ID_ADPCM_G722:
1362       caps =
1363           gst_ff_aud_caps_new (context, codec_id, encode, "audio/G722", NULL);
1364       if (context)
1365         gst_caps_set_simple (caps,
1366             "block_align", G_TYPE_INT, context->block_align,
1367             "bitrate", G_TYPE_INT, context->bit_rate, NULL);
1368       break;
1369
1370     case CODEC_ID_ADPCM_G726:
1371     {
1372       /* the G726 decoder can also handle G721 */
1373       caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-adpcm",
1374           "layout", G_TYPE_STRING, "g726", NULL);
1375       if (context)
1376         gst_caps_set_simple (caps,
1377             "block_align", G_TYPE_INT, context->block_align,
1378             "bitrate", G_TYPE_INT, context->bit_rate, NULL);
1379
1380       if (!encode) {
1381         gst_caps_append (caps, gst_caps_new_simple ("audio/x-adpcm",
1382                 "layout", G_TYPE_STRING, "g721",
1383                 "channels", G_TYPE_INT, 1, "rate", G_TYPE_INT, 8000, NULL));
1384       }
1385       break;
1386     }
1387     case CODEC_ID_ADPCM_IMA_QT:
1388     case CODEC_ID_ADPCM_IMA_WAV:
1389     case CODEC_ID_ADPCM_IMA_DK3:
1390     case CODEC_ID_ADPCM_IMA_DK4:
1391     case CODEC_ID_ADPCM_IMA_WS:
1392     case CODEC_ID_ADPCM_IMA_SMJPEG:
1393     case CODEC_ID_ADPCM_IMA_AMV:
1394     case CODEC_ID_ADPCM_IMA_ISS:
1395     case CODEC_ID_ADPCM_IMA_EA_EACS:
1396     case CODEC_ID_ADPCM_IMA_EA_SEAD:
1397     case CODEC_ID_ADPCM_MS:
1398     case CODEC_ID_ADPCM_4XM:
1399     case CODEC_ID_ADPCM_XA:
1400     case CODEC_ID_ADPCM_ADX:
1401     case CODEC_ID_ADPCM_EA:
1402     case CODEC_ID_ADPCM_CT:
1403     case CODEC_ID_ADPCM_SWF:
1404     case CODEC_ID_ADPCM_YAMAHA:
1405     case CODEC_ID_ADPCM_SBPRO_2:
1406     case CODEC_ID_ADPCM_SBPRO_3:
1407     case CODEC_ID_ADPCM_SBPRO_4:
1408     case CODEC_ID_ADPCM_EA_R1:
1409     case CODEC_ID_ADPCM_EA_R2:
1410     case CODEC_ID_ADPCM_EA_R3:
1411     case CODEC_ID_ADPCM_EA_MAXIS_XA:
1412     case CODEC_ID_ADPCM_EA_XAS:
1413     case CODEC_ID_ADPCM_THP:
1414     {
1415       const gchar *layout = NULL;
1416
1417       switch (codec_id) {
1418         case CODEC_ID_ADPCM_IMA_QT:
1419           layout = "quicktime";
1420           break;
1421         case CODEC_ID_ADPCM_IMA_WAV:
1422           layout = "dvi";
1423           break;
1424         case CODEC_ID_ADPCM_IMA_DK3:
1425           layout = "dk3";
1426           break;
1427         case CODEC_ID_ADPCM_IMA_DK4:
1428           layout = "dk4";
1429           break;
1430         case CODEC_ID_ADPCM_IMA_WS:
1431           layout = "westwood";
1432           break;
1433         case CODEC_ID_ADPCM_IMA_SMJPEG:
1434           layout = "smjpeg";
1435           break;
1436         case CODEC_ID_ADPCM_IMA_AMV:
1437           layout = "amv";
1438           break;
1439         case CODEC_ID_ADPCM_IMA_ISS:
1440           layout = "iss";
1441           break;
1442         case CODEC_ID_ADPCM_IMA_EA_EACS:
1443           layout = "ea-eacs";
1444           break;
1445         case CODEC_ID_ADPCM_IMA_EA_SEAD:
1446           layout = "ea-sead";
1447           break;
1448         case CODEC_ID_ADPCM_MS:
1449           layout = "microsoft";
1450           break;
1451         case CODEC_ID_ADPCM_4XM:
1452           layout = "4xm";
1453           break;
1454         case CODEC_ID_ADPCM_XA:
1455           layout = "xa";
1456           break;
1457         case CODEC_ID_ADPCM_ADX:
1458           layout = "adx";
1459           break;
1460         case CODEC_ID_ADPCM_EA:
1461           layout = "ea";
1462           break;
1463         case CODEC_ID_ADPCM_CT:
1464           layout = "ct";
1465           break;
1466         case CODEC_ID_ADPCM_SWF:
1467           layout = "swf";
1468           break;
1469         case CODEC_ID_ADPCM_YAMAHA:
1470           layout = "yamaha";
1471           break;
1472         case CODEC_ID_ADPCM_SBPRO_2:
1473           layout = "sbpro2";
1474           break;
1475         case CODEC_ID_ADPCM_SBPRO_3:
1476           layout = "sbpro3";
1477           break;
1478         case CODEC_ID_ADPCM_SBPRO_4:
1479           layout = "sbpro4";
1480           break;
1481         case CODEC_ID_ADPCM_EA_R1:
1482           layout = "ea-r1";
1483           break;
1484         case CODEC_ID_ADPCM_EA_R2:
1485           layout = "ea-r3";
1486           break;
1487         case CODEC_ID_ADPCM_EA_R3:
1488           layout = "ea-r3";
1489           break;
1490         case CODEC_ID_ADPCM_EA_MAXIS_XA:
1491           layout = "ea-maxis-xa";
1492           break;
1493         case CODEC_ID_ADPCM_EA_XAS:
1494           layout = "ea-xas";
1495           break;
1496         case CODEC_ID_ADPCM_THP:
1497           layout = "thp";
1498           break;
1499         default:
1500           g_assert (0);         /* don't worry, we never get here */
1501           break;
1502       }
1503
1504       /* FIXME: someone please check whether we need additional properties
1505        * in this caps definition. */
1506       caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-adpcm",
1507           "layout", G_TYPE_STRING, layout, NULL);
1508       if (context)
1509         gst_caps_set_simple (caps,
1510             "block_align", G_TYPE_INT, context->block_align,
1511             "bitrate", G_TYPE_INT, context->bit_rate, NULL);
1512     }
1513       break;
1514
1515     case CODEC_ID_AMR_NB:
1516       caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/AMR", NULL);
1517       break;
1518
1519     case CODEC_ID_AMR_WB:
1520       caps =
1521           gst_ff_aud_caps_new (context, codec_id, encode, "audio/AMR-WB", NULL);
1522       break;
1523
1524     case CODEC_ID_GSM:
1525       caps =
1526           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-gsm", NULL);
1527       break;
1528
1529     case CODEC_ID_GSM_MS:
1530       caps =
1531           gst_ff_aud_caps_new (context, codec_id, encode, "audio/ms-gsm", NULL);
1532       break;
1533
1534     case CODEC_ID_NELLYMOSER:
1535       caps =
1536           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-nellymoser",
1537           NULL);
1538       break;
1539
1540     case CODEC_ID_SIPR:
1541     {
1542       caps =
1543           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-sipro",
1544           NULL);
1545       if (context) {
1546         gst_caps_set_simple (caps,
1547             "leaf_size", G_TYPE_INT, context->block_align,
1548             "bitrate", G_TYPE_INT, context->bit_rate, NULL);
1549       }
1550     }
1551       break;
1552
1553     case CODEC_ID_RA_144:
1554     case CODEC_ID_RA_288:
1555     case CODEC_ID_COOK:
1556     {
1557       gint version = 0;
1558
1559       switch (codec_id) {
1560         case CODEC_ID_RA_144:
1561           version = 1;
1562           break;
1563         case CODEC_ID_RA_288:
1564           version = 2;
1565           break;
1566         case CODEC_ID_COOK:
1567           version = 8;
1568           break;
1569         default:
1570           break;
1571       }
1572
1573       /* FIXME: properties? */
1574       caps =
1575           gst_ff_aud_caps_new (context, codec_id, encode,
1576           "audio/x-pn-realaudio", "raversion", G_TYPE_INT, version, NULL);
1577       if (context) {
1578         gst_caps_set_simple (caps,
1579             "leaf_size", G_TYPE_INT, context->block_align,
1580             "bitrate", G_TYPE_INT, context->bit_rate, NULL);
1581       }
1582     }
1583       break;
1584
1585     case CODEC_ID_ROQ_DPCM:
1586     case CODEC_ID_INTERPLAY_DPCM:
1587     case CODEC_ID_XAN_DPCM:
1588     case CODEC_ID_SOL_DPCM:
1589     {
1590       const gchar *layout = NULL;
1591
1592       switch (codec_id) {
1593         case CODEC_ID_ROQ_DPCM:
1594           layout = "roq";
1595           break;
1596         case CODEC_ID_INTERPLAY_DPCM:
1597           layout = "interplay";
1598           break;
1599         case CODEC_ID_XAN_DPCM:
1600           layout = "xan";
1601           break;
1602         case CODEC_ID_SOL_DPCM:
1603           layout = "sol";
1604           break;
1605         default:
1606           g_assert (0);         /* don't worry, we never get here */
1607           break;
1608       }
1609
1610       /* FIXME: someone please check whether we need additional properties
1611        * in this caps definition. */
1612       caps = gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-dpcm",
1613           "layout", G_TYPE_STRING, layout, NULL);
1614       if (context)
1615         gst_caps_set_simple (caps,
1616             "block_align", G_TYPE_INT, context->block_align,
1617             "bitrate", G_TYPE_INT, context->bit_rate, NULL);
1618     }
1619       break;
1620
1621     case CODEC_ID_SHORTEN:
1622       caps = gst_caps_new_empty_simple ("audio/x-shorten");
1623       break;
1624
1625     case CODEC_ID_ALAC:
1626       caps =
1627           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-alac", NULL);
1628       if (context) {
1629         gst_caps_set_simple (caps,
1630             "samplesize", G_TYPE_INT, context->bits_per_coded_sample, NULL);
1631       }
1632       break;
1633
1634     case CODEC_ID_FLAC:
1635       /* Note that ffmpeg has no encoder yet, but just for safety. In the
1636        * encoder case, we want to add things like samplerate, channels... */
1637       if (!encode) {
1638         caps = gst_caps_new_empty_simple ("audio/x-flac");
1639       }
1640       break;
1641
1642     case CODEC_ID_DVD_SUBTITLE:
1643     case CODEC_ID_DVB_SUBTITLE:
1644       caps = NULL;
1645       break;
1646     case CODEC_ID_BMP:
1647       caps = gst_caps_new_empty_simple ("image/bmp");
1648       break;
1649     case CODEC_ID_TTA:
1650       caps =
1651           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-tta", NULL);
1652       if (context) {
1653         gst_caps_set_simple (caps,
1654             "samplesize", G_TYPE_INT, context->bits_per_coded_sample, NULL);
1655       }
1656       break;
1657     case CODEC_ID_TWINVQ:
1658       caps =
1659           gst_ff_aud_caps_new (context, codec_id, encode, "audio/x-twin-vq",
1660           NULL);
1661       break;
1662     default:
1663       GST_DEBUG ("Unknown codec ID %d, please add mapping here", codec_id);
1664       break;
1665   }
1666
1667   if (buildcaps) {
1668     AVCodec *codec;
1669
1670     if ((codec = avcodec_find_decoder (codec_id)) ||
1671         (codec = avcodec_find_encoder (codec_id))) {
1672       gchar *mime = NULL;
1673
1674       GST_LOG ("Could not create stream format caps for %s", codec->name);
1675
1676       switch (codec->type) {
1677         case AVMEDIA_TYPE_VIDEO:
1678           mime = g_strdup_printf ("video/x-gst-av-%s", codec->name);
1679           caps = gst_ff_vid_caps_new (context, codec_id, encode, mime, NULL);
1680           g_free (mime);
1681           break;
1682         case AVMEDIA_TYPE_AUDIO:
1683           mime = g_strdup_printf ("audio/x-gst-av-%s", codec->name);
1684           caps = gst_ff_aud_caps_new (context, codec_id, encode, mime, NULL);
1685           if (context)
1686             gst_caps_set_simple (caps,
1687                 "block_align", G_TYPE_INT, context->block_align,
1688                 "bitrate", G_TYPE_INT, context->bit_rate, NULL);
1689           g_free (mime);
1690           break;
1691         default:
1692           break;
1693       }
1694     }
1695   }
1696
1697   if (caps != NULL) {
1698
1699     /* set private data */
1700     if (context && context->extradata_size > 0) {
1701       GstBuffer *data = gst_buffer_new_and_alloc (context->extradata_size);
1702
1703       gst_buffer_fill (data, 0, context->extradata, context->extradata_size);
1704       gst_caps_set_simple (caps, "codec_data", GST_TYPE_BUFFER, data, NULL);
1705       gst_buffer_unref (data);
1706     }
1707
1708     GST_LOG ("caps for codec_id=%d: %" GST_PTR_FORMAT, codec_id, caps);
1709
1710   } else {
1711     GST_LOG ("No caps found for codec_id=%d", codec_id);
1712   }
1713
1714   return caps;
1715 }
1716
1717 /* Convert a FFMPEG Pixel Format and optional AVCodecContext
1718  * to a GstCaps. If the context is ommitted, no fixed values
1719  * for video/audio size will be included in the GstCaps
1720  *
1721  * See below for usefullness
1722  */
1723
1724 GstCaps *
1725 gst_ffmpeg_pixfmt_to_caps (enum PixelFormat pix_fmt, AVCodecContext * context,
1726     enum CodecID codec_id)
1727 {
1728   GstCaps *caps = NULL;
1729   GstVideoFormat format;
1730
1731   format = gst_ffmpeg_pixfmt_to_videoformat (pix_fmt);
1732
1733   if (format != GST_VIDEO_FORMAT_UNKNOWN) {
1734     caps = gst_ff_vid_caps_new (context, codec_id, TRUE, "video/x-raw",
1735         "format", G_TYPE_STRING, gst_video_format_to_string (format), NULL);
1736   }
1737
1738   if (caps != NULL) {
1739     GST_DEBUG ("caps for pix_fmt=%d: %" GST_PTR_FORMAT, pix_fmt, caps);
1740   } else {
1741     GST_LOG ("No caps found for pix_fmt=%d", pix_fmt);
1742   }
1743
1744   return caps;
1745 }
1746
1747 GstAudioFormat
1748 gst_ffmpeg_smpfmt_to_audioformat (enum AVSampleFormat sample_fmt)
1749 {
1750   switch (sample_fmt) {
1751     case AV_SAMPLE_FMT_U8:
1752     case AV_SAMPLE_FMT_U8P:
1753       return GST_AUDIO_FORMAT_U8;
1754       break;
1755     case AV_SAMPLE_FMT_S16:
1756     case AV_SAMPLE_FMT_S16P:
1757       return GST_AUDIO_FORMAT_S16;
1758       break;
1759     case AV_SAMPLE_FMT_S32:
1760     case AV_SAMPLE_FMT_S32P:
1761       return GST_AUDIO_FORMAT_S32;
1762       break;
1763     case AV_SAMPLE_FMT_FLT:
1764     case AV_SAMPLE_FMT_FLTP:
1765       return GST_AUDIO_FORMAT_F32;
1766       break;
1767     case AV_SAMPLE_FMT_DBL:
1768     case AV_SAMPLE_FMT_DBLP:
1769       return GST_AUDIO_FORMAT_F64;
1770       break;
1771     default:
1772       /* .. */
1773       return GST_AUDIO_FORMAT_UNKNOWN;
1774       break;
1775   }
1776 }
1777
1778 /* Convert a FFMPEG Sample Format and optional AVCodecContext
1779  * to a GstCaps. If the context is ommitted, no fixed values
1780  * for video/audio size will be included in the GstCaps
1781  *
1782  * See below for usefullness
1783  */
1784
1785 static GstCaps *
1786 gst_ffmpeg_smpfmt_to_caps (enum AVSampleFormat sample_fmt,
1787     AVCodecContext * context, enum CodecID codec_id)
1788 {
1789   GstCaps *caps = NULL;
1790   GstAudioFormat format;
1791
1792   format = gst_ffmpeg_smpfmt_to_audioformat (sample_fmt);
1793
1794   if (format != GST_AUDIO_FORMAT_UNKNOWN) {
1795     caps = gst_ff_aud_caps_new (context, codec_id, TRUE, "audio/x-raw",
1796         "format", G_TYPE_STRING, gst_audio_format_to_string (format),
1797         "layout", G_TYPE_STRING, "interleaved", NULL);
1798     GST_LOG ("caps for sample_fmt=%d: %" GST_PTR_FORMAT, sample_fmt, caps);
1799   } else {
1800     GST_LOG ("No caps found for sample_fmt=%d", sample_fmt);
1801   }
1802
1803   return caps;
1804 }
1805
1806 static void
1807 gst_ffmpeg_audio_set_sample_fmts (GstCaps * caps,
1808     const enum AVSampleFormat *fmts)
1809 {
1810   GValue va = { 0, };
1811   GValue v = { 0, };
1812   GstAudioFormat format;
1813
1814   if (!fmts || fmts[0] == -1) {
1815     gint i;
1816
1817     g_value_init (&va, GST_TYPE_LIST);
1818     g_value_init (&v, G_TYPE_STRING);
1819     for (i = 0; i <= AV_SAMPLE_FMT_DBL; i++) {
1820       format = gst_ffmpeg_smpfmt_to_audioformat (i);
1821       g_value_set_string (&v, gst_audio_format_to_string (format));
1822       gst_value_list_append_value (&va, &v);
1823     }
1824     gst_caps_set_value (caps, "format", &va);
1825     g_value_unset (&v);
1826     g_value_unset (&va);
1827     return;
1828   }
1829
1830   /* Only a single rate */
1831   if (fmts[1] == -1) {
1832     format = gst_ffmpeg_smpfmt_to_audioformat (fmts[0]);
1833     gst_caps_set_simple (caps, "format", G_TYPE_STRING,
1834         gst_audio_format_to_string (format), NULL);
1835     return;
1836   }
1837
1838   g_value_init (&va, GST_TYPE_LIST);
1839   g_value_init (&v, G_TYPE_STRING);
1840   while (*fmts != -1) {
1841     format = gst_ffmpeg_smpfmt_to_audioformat (*fmts);
1842     g_value_set_string (&v, gst_audio_format_to_string (format));
1843     gst_value_list_append_value (&va, &v);
1844     fmts++;
1845   }
1846   gst_caps_set_value (caps, "format", &va);
1847   g_value_unset (&v);
1848   g_value_unset (&va);
1849 }
1850
1851 GstCaps *
1852 gst_ffmpeg_codectype_to_audio_caps (AVCodecContext * context,
1853     enum CodecID codec_id, gboolean encode, AVCodec * codec)
1854 {
1855   GstCaps *caps = NULL;
1856
1857   GST_DEBUG ("context:%p, codec_id:%d, encode:%d, codec:%p",
1858       context, codec_id, encode, codec);
1859   if (codec)
1860     GST_DEBUG ("sample_fmts:%p, samplerates:%p",
1861         codec->sample_fmts, codec->supported_samplerates);
1862
1863   if (context) {
1864     /* Specific codec context */
1865     caps = gst_ffmpeg_smpfmt_to_caps (context->sample_fmt, context, codec_id);
1866   } else {
1867     caps = gst_ff_aud_caps_new (context, codec_id, TRUE, "audio/x-raw",
1868         "layout", G_TYPE_STRING, "interleaved", NULL);
1869
1870     gst_ffmpeg_audio_set_sample_fmts (caps, codec ? codec->sample_fmts : NULL);
1871   }
1872
1873   return caps;
1874 }
1875
1876 GstCaps *
1877 gst_ffmpeg_codectype_to_video_caps (AVCodecContext * context,
1878     enum CodecID codec_id, gboolean encode, AVCodec * codec)
1879 {
1880   GstCaps *caps;
1881
1882   GST_LOG ("context:%p, codec_id:%d, encode:%d, codec:%p",
1883       context, codec_id, encode, codec);
1884
1885   if (context) {
1886     caps = gst_ffmpeg_pixfmt_to_caps (context->pix_fmt, context, codec_id);
1887   } else {
1888     GstCaps *temp;
1889     enum PixelFormat i;
1890     AVCodecContext ctx = { 0, };
1891
1892     caps = gst_caps_new_empty ();
1893     for (i = 0; i < PIX_FMT_NB; i++) {
1894       ctx.width = -1;
1895       ctx.pix_fmt = i;
1896       temp = gst_ffmpeg_pixfmt_to_caps (i, encode ? &ctx : NULL, codec_id);
1897       if (temp != NULL) {
1898         gst_caps_append (caps, temp);
1899       }
1900     }
1901   }
1902   return caps;
1903 }
1904
1905 /* Convert a FFMPEG codec Type and optional AVCodecContext
1906  * to a GstCaps. If the context is ommitted, no fixed values
1907  * for video/audio size will be included in the GstCaps
1908  *
1909  * AVMediaType is primarily meant for uncompressed data GstCaps!
1910  */
1911
1912 GstCaps *
1913 gst_ffmpeg_codectype_to_caps (enum AVMediaType codec_type,
1914     AVCodecContext * context, enum CodecID codec_id, gboolean encode)
1915 {
1916   GstCaps *caps;
1917
1918   switch (codec_type) {
1919     case AVMEDIA_TYPE_VIDEO:
1920       caps =
1921           gst_ffmpeg_codectype_to_video_caps (context, codec_id, encode, NULL);
1922       break;
1923     case AVMEDIA_TYPE_AUDIO:
1924       caps =
1925           gst_ffmpeg_codectype_to_audio_caps (context, codec_id, encode, NULL);
1926       break;
1927     default:
1928       caps = NULL;
1929       break;
1930   }
1931
1932   return caps;
1933 }
1934
1935 /* Convert a GstCaps (audio/raw) to a FFMPEG SampleFmt
1936  * and other audio properties in a AVCodecContext.
1937  *
1938  * For usefullness, see below
1939  */
1940
1941 static void
1942 gst_ffmpeg_caps_to_smpfmt (const GstCaps * caps,
1943     AVCodecContext * context, gboolean raw)
1944 {
1945   GstStructure *structure;
1946   const gchar *fmt;
1947   GstAudioFormat format = GST_AUDIO_FORMAT_UNKNOWN;
1948
1949   g_return_if_fail (gst_caps_get_size (caps) == 1);
1950
1951   structure = gst_caps_get_structure (caps, 0);
1952
1953   gst_structure_get_int (structure, "channels", &context->channels);
1954   gst_structure_get_int (structure, "rate", &context->sample_rate);
1955   gst_structure_get_int (structure, "block_align", &context->block_align);
1956   gst_structure_get_int (structure, "bitrate", &context->bit_rate);
1957
1958   if (!raw)
1959     return;
1960
1961   if (gst_structure_has_name (structure, "audio/x-raw")) {
1962     if ((fmt = gst_structure_get_string (structure, "format"))) {
1963       format = gst_audio_format_from_string (fmt);
1964     }
1965   }
1966
1967   switch (format) {
1968     case GST_AUDIO_FORMAT_F32:
1969       context->sample_fmt = AV_SAMPLE_FMT_FLT;
1970       break;
1971     case GST_AUDIO_FORMAT_F64:
1972       context->sample_fmt = AV_SAMPLE_FMT_DBL;
1973       break;
1974     case GST_AUDIO_FORMAT_S32:
1975       context->sample_fmt = AV_SAMPLE_FMT_S32;
1976       break;
1977     case GST_AUDIO_FORMAT_S16:
1978       context->sample_fmt = AV_SAMPLE_FMT_S16;
1979       break;
1980     default:
1981       break;
1982   }
1983 }
1984
1985 /* Convert a GstCaps (video/raw) to a FFMPEG PixFmt
1986  * and other video properties in a AVCodecContext.
1987  *
1988  * For usefullness, see below
1989  */
1990
1991 static void
1992 gst_ffmpeg_caps_to_pixfmt (const GstCaps * caps,
1993     AVCodecContext * context, gboolean raw)
1994 {
1995   GstStructure *structure;
1996   const GValue *fps;
1997   const GValue *par = NULL;
1998   const gchar *fmt;
1999   GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN;
2000
2001   GST_DEBUG ("converting caps %" GST_PTR_FORMAT, caps);
2002   g_return_if_fail (gst_caps_get_size (caps) == 1);
2003   structure = gst_caps_get_structure (caps, 0);
2004
2005   gst_structure_get_int (structure, "width", &context->width);
2006   gst_structure_get_int (structure, "height", &context->height);
2007   gst_structure_get_int (structure, "bpp", &context->bits_per_coded_sample);
2008
2009   fps = gst_structure_get_value (structure, "framerate");
2010   if (fps != NULL && GST_VALUE_HOLDS_FRACTION (fps)) {
2011
2012     /* somehow these seem mixed up.. */
2013     context->time_base.den = gst_value_get_fraction_numerator (fps);
2014     context->time_base.num = gst_value_get_fraction_denominator (fps);
2015     context->ticks_per_frame = 1;
2016
2017     GST_DEBUG ("setting framerate %d/%d = %lf",
2018         context->time_base.den, context->time_base.num,
2019         1. * context->time_base.den / context->time_base.num);
2020   }
2021
2022   par = gst_structure_get_value (structure, "pixel-aspect-ratio");
2023   if (par && GST_VALUE_HOLDS_FRACTION (par)) {
2024
2025     context->sample_aspect_ratio.num = gst_value_get_fraction_numerator (par);
2026     context->sample_aspect_ratio.den = gst_value_get_fraction_denominator (par);
2027
2028     GST_DEBUG ("setting pixel-aspect-ratio %d/%d = %lf",
2029         context->sample_aspect_ratio.den, context->sample_aspect_ratio.num,
2030         1. * context->sample_aspect_ratio.den /
2031         context->sample_aspect_ratio.num);
2032   }
2033
2034   if (!raw)
2035     return;
2036
2037   g_return_if_fail (fps != NULL && GST_VALUE_HOLDS_FRACTION (fps));
2038
2039   if (gst_structure_has_name (structure, "video/x-raw")) {
2040     if ((fmt = gst_structure_get_string (structure, "format"))) {
2041       format = gst_video_format_from_string (fmt);
2042     }
2043   }
2044
2045   switch (format) {
2046     case GST_VIDEO_FORMAT_YUY2:
2047       context->pix_fmt = PIX_FMT_YUYV422;
2048       break;
2049     case GST_VIDEO_FORMAT_I420:
2050       context->pix_fmt = PIX_FMT_YUV420P;
2051       break;
2052     case GST_VIDEO_FORMAT_A420:
2053       context->pix_fmt = PIX_FMT_YUVA420P;
2054       break;
2055     case GST_VIDEO_FORMAT_Y41B:
2056       context->pix_fmt = PIX_FMT_YUV411P;
2057       break;
2058     case GST_VIDEO_FORMAT_Y42B:
2059       context->pix_fmt = PIX_FMT_YUV422P;
2060       break;
2061     case GST_VIDEO_FORMAT_YUV9:
2062       context->pix_fmt = PIX_FMT_YUV410P;
2063       break;
2064     case GST_VIDEO_FORMAT_Y444:
2065       context->pix_fmt = PIX_FMT_YUV444P;
2066       break;
2067     case GST_VIDEO_FORMAT_GRAY8:
2068       context->pix_fmt = PIX_FMT_GRAY8;
2069       break;
2070     case GST_VIDEO_FORMAT_xRGB:
2071 #if (G_BYTE_ORDER == G_BIG_ENDIAN)
2072       context->pix_fmt = PIX_FMT_RGB32;
2073 #endif
2074       break;
2075     case GST_VIDEO_FORMAT_BGRx:
2076 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
2077       context->pix_fmt = PIX_FMT_RGB32;
2078 #endif
2079       break;
2080     case GST_VIDEO_FORMAT_RGB:
2081       context->pix_fmt = PIX_FMT_RGB24;
2082       break;
2083     case GST_VIDEO_FORMAT_BGR:
2084       context->pix_fmt = PIX_FMT_BGR24;
2085       break;
2086     case GST_VIDEO_FORMAT_RGB16:
2087       context->pix_fmt = PIX_FMT_RGB565;
2088       break;
2089     case GST_VIDEO_FORMAT_RGB15:
2090       context->pix_fmt = PIX_FMT_RGB555;
2091       break;
2092     case GST_VIDEO_FORMAT_RGB8P:
2093       context->pix_fmt = PIX_FMT_PAL8;
2094       break;
2095     default:
2096       break;
2097   }
2098 }
2099
2100 typedef struct
2101 {
2102   GstVideoFormat format;
2103   enum PixelFormat pixfmt;
2104 } PixToFmt;
2105
2106 /* FIXME : FILLME */
2107 static const PixToFmt pixtofmttable[] = {
2108   /* GST_VIDEO_FORMAT_I420, */
2109   {GST_VIDEO_FORMAT_I420, PIX_FMT_YUV420P},
2110   /* Note : this should use a different chroma placement */
2111   {GST_VIDEO_FORMAT_I420, PIX_FMT_YUVJ420P},
2112
2113   /* GST_VIDEO_FORMAT_YV12, */
2114   /* GST_VIDEO_FORMAT_YUY2, */
2115   {GST_VIDEO_FORMAT_YUY2, PIX_FMT_YUYV422},
2116   /* GST_VIDEO_FORMAT_UYVY, */
2117   {GST_VIDEO_FORMAT_UYVY, PIX_FMT_UYVY422},
2118   /* GST_VIDEO_FORMAT_AYUV, */
2119   /* GST_VIDEO_FORMAT_RGBx, */
2120   /* GST_VIDEO_FORMAT_BGRx, */
2121   /* GST_VIDEO_FORMAT_xRGB, */
2122   /* GST_VIDEO_FORMAT_xBGR, */
2123   /* GST_VIDEO_FORMAT_RGBA, */
2124   {GST_VIDEO_FORMAT_RGBA, PIX_FMT_RGBA},
2125   /* GST_VIDEO_FORMAT_BGRA, */
2126   {GST_VIDEO_FORMAT_BGRA, PIX_FMT_BGRA},
2127   /* GST_VIDEO_FORMAT_ARGB, */
2128   {GST_VIDEO_FORMAT_ARGB, PIX_FMT_ARGB},
2129   /* GST_VIDEO_FORMAT_ABGR, */
2130   {GST_VIDEO_FORMAT_ABGR, PIX_FMT_ABGR},
2131   /* GST_VIDEO_FORMAT_RGB, */
2132   {GST_VIDEO_FORMAT_RGB, PIX_FMT_RGB24},
2133   /* GST_VIDEO_FORMAT_BGR, */
2134   {GST_VIDEO_FORMAT_BGR, PIX_FMT_BGR24},
2135   /* GST_VIDEO_FORMAT_Y41B, */
2136   {GST_VIDEO_FORMAT_Y41B, PIX_FMT_YUV411P},
2137   /* GST_VIDEO_FORMAT_Y42B, */
2138   {GST_VIDEO_FORMAT_Y42B, PIX_FMT_YUV422P},
2139   {GST_VIDEO_FORMAT_Y42B, PIX_FMT_YUVJ422P},
2140   /* GST_VIDEO_FORMAT_YVYU, */
2141   /* GST_VIDEO_FORMAT_Y444, */
2142   {GST_VIDEO_FORMAT_Y444, PIX_FMT_YUV444P},
2143   {GST_VIDEO_FORMAT_Y444, PIX_FMT_YUVJ444P},
2144   /* GST_VIDEO_FORMAT_v210, */
2145   /* GST_VIDEO_FORMAT_v216, */
2146   /* GST_VIDEO_FORMAT_NV12, */
2147   {GST_VIDEO_FORMAT_NV12, PIX_FMT_NV12},
2148   /* GST_VIDEO_FORMAT_NV21, */
2149   {GST_VIDEO_FORMAT_NV21, PIX_FMT_NV21},
2150   /* GST_VIDEO_FORMAT_GRAY8, */
2151   {GST_VIDEO_FORMAT_GRAY8, PIX_FMT_GRAY8},
2152   /* GST_VIDEO_FORMAT_GRAY16_BE, */
2153   {GST_VIDEO_FORMAT_GRAY16_BE, PIX_FMT_GRAY16BE},
2154   /* GST_VIDEO_FORMAT_GRAY16_LE, */
2155   {GST_VIDEO_FORMAT_GRAY16_LE, PIX_FMT_GRAY16LE},
2156   /* GST_VIDEO_FORMAT_v308, */
2157   /* GST_VIDEO_FORMAT_Y800, */
2158   /* GST_VIDEO_FORMAT_Y16, */
2159   /* GST_VIDEO_FORMAT_RGB16, */
2160   {GST_VIDEO_FORMAT_RGB16, PIX_FMT_RGB565},
2161   /* GST_VIDEO_FORMAT_BGR16, */
2162   /* GST_VIDEO_FORMAT_RGB15, */
2163   {GST_VIDEO_FORMAT_RGB15, PIX_FMT_RGB555},
2164   /* GST_VIDEO_FORMAT_BGR15, */
2165   /* GST_VIDEO_FORMAT_UYVP, */
2166   /* GST_VIDEO_FORMAT_A420, */
2167   {GST_VIDEO_FORMAT_A420, PIX_FMT_YUVA420P},
2168   /* GST_VIDEO_FORMAT_RGB8_PALETTED, */
2169   {GST_VIDEO_FORMAT_RGB8P, PIX_FMT_PAL8},
2170   /* GST_VIDEO_FORMAT_YUV9, */
2171   {GST_VIDEO_FORMAT_YUV9, PIX_FMT_YUV410P},
2172   /* GST_VIDEO_FORMAT_YVU9, */
2173   /* GST_VIDEO_FORMAT_IYU1, */
2174   /* GST_VIDEO_FORMAT_ARGB64, */
2175   /* GST_VIDEO_FORMAT_AYUV64, */
2176   /* GST_VIDEO_FORMAT_r210, */
2177   {GST_VIDEO_FORMAT_I420_10LE, PIX_FMT_YUV420P10LE},
2178   {GST_VIDEO_FORMAT_I420_10BE, PIX_FMT_YUV420P10BE},
2179   {GST_VIDEO_FORMAT_I422_10LE, PIX_FMT_YUV422P10LE},
2180   {GST_VIDEO_FORMAT_I422_10BE, PIX_FMT_YUV422P10BE},
2181   {GST_VIDEO_FORMAT_Y444_10LE, PIX_FMT_YUV444P10LE},
2182   {GST_VIDEO_FORMAT_Y444_10BE, PIX_FMT_YUV444P10BE},
2183   {GST_VIDEO_FORMAT_GBR, PIX_FMT_GBRP},
2184   {GST_VIDEO_FORMAT_GBR_10LE, PIX_FMT_GBRP10LE},
2185   {GST_VIDEO_FORMAT_GBR_10BE, PIX_FMT_GBRP10BE},
2186 };
2187
2188 GstVideoFormat
2189 gst_ffmpeg_pixfmt_to_videoformat (enum PixelFormat pixfmt)
2190 {
2191   guint i;
2192
2193   for (i = 0; i < G_N_ELEMENTS (pixtofmttable); i++)
2194     if (pixtofmttable[i].pixfmt == pixfmt)
2195       return pixtofmttable[i].format;
2196
2197   GST_WARNING ("Unknown pixel format %d", pixfmt);
2198   return GST_VIDEO_FORMAT_UNKNOWN;
2199 }
2200
2201 enum PixelFormat
2202 gst_ffmpeg_videoformat_to_pixfmt (GstVideoFormat format)
2203 {
2204   guint i;
2205
2206   for (i = 0; i < G_N_ELEMENTS (pixtofmttable); i++)
2207     if (pixtofmttable[i].format == format)
2208       return pixtofmttable[i].pixfmt;
2209   return PIX_FMT_NONE;
2210 }
2211
2212 void
2213 gst_ffmpeg_videoinfo_to_context (GstVideoInfo * info, AVCodecContext * context)
2214 {
2215   gint i, bpp = 0;
2216
2217   context->width = GST_VIDEO_INFO_WIDTH (info);
2218   context->height = GST_VIDEO_INFO_HEIGHT (info);
2219   for (i = 0; i < GST_VIDEO_INFO_N_COMPONENTS (info); i++)
2220     bpp += GST_VIDEO_INFO_COMP_DEPTH (info, i);
2221   context->bits_per_coded_sample = bpp;
2222
2223   context->ticks_per_frame = 1;
2224   context->time_base.den = GST_VIDEO_INFO_FPS_N (info);
2225   context->time_base.num = GST_VIDEO_INFO_FPS_D (info);
2226
2227   context->sample_aspect_ratio.num = GST_VIDEO_INFO_PAR_N (info);
2228   context->sample_aspect_ratio.den = GST_VIDEO_INFO_PAR_D (info);
2229
2230   context->pix_fmt =
2231       gst_ffmpeg_videoformat_to_pixfmt (GST_VIDEO_INFO_FORMAT (info));
2232 }
2233
2234 void
2235 gst_ffmpeg_audioinfo_to_context (GstAudioInfo * info, AVCodecContext * context)
2236 {
2237   const AVCodec *codec;
2238   const enum AVSampleFormat *smpl_fmts;
2239   enum AVSampleFormat smpl_fmt = -1;
2240
2241   context->channels = info->channels;
2242   context->sample_rate = info->rate;
2243
2244   codec = context->codec;
2245
2246   smpl_fmts = codec->sample_fmts;
2247
2248   switch (info->finfo->format) {
2249     case GST_AUDIO_FORMAT_F32:
2250       if (smpl_fmts) {
2251         while (*smpl_fmts != -1) {
2252           if (*smpl_fmts == AV_SAMPLE_FMT_FLT) {
2253             smpl_fmt = *smpl_fmts;
2254             break;
2255           } else if (*smpl_fmts == AV_SAMPLE_FMT_FLTP) {
2256             smpl_fmt = *smpl_fmts;
2257           }
2258
2259           smpl_fmts++;
2260         }
2261       } else {
2262         smpl_fmt = AV_SAMPLE_FMT_FLT;
2263       }
2264       break;
2265     case GST_AUDIO_FORMAT_F64:
2266       if (smpl_fmts) {
2267         while (*smpl_fmts != -1) {
2268           if (*smpl_fmts == AV_SAMPLE_FMT_DBL) {
2269             smpl_fmt = *smpl_fmts;
2270             break;
2271           } else if (*smpl_fmts == AV_SAMPLE_FMT_DBLP) {
2272             smpl_fmt = *smpl_fmts;
2273           }
2274
2275           smpl_fmts++;
2276         }
2277       } else {
2278         smpl_fmt = AV_SAMPLE_FMT_DBL;
2279       }
2280       break;
2281     case GST_AUDIO_FORMAT_S32:
2282       if (smpl_fmts) {
2283         while (*smpl_fmts != -1) {
2284           if (*smpl_fmts == AV_SAMPLE_FMT_S32) {
2285             smpl_fmt = *smpl_fmts;
2286             break;
2287           } else if (*smpl_fmts == AV_SAMPLE_FMT_S32P) {
2288             smpl_fmt = *smpl_fmts;
2289           }
2290
2291           smpl_fmts++;
2292         }
2293       } else {
2294         smpl_fmt = AV_SAMPLE_FMT_S32;
2295       }
2296       break;
2297     case GST_AUDIO_FORMAT_S16:
2298       if (smpl_fmts) {
2299         while (*smpl_fmts != -1) {
2300           if (*smpl_fmts == AV_SAMPLE_FMT_S16) {
2301             smpl_fmt = *smpl_fmts;
2302             break;
2303           } else if (*smpl_fmts == AV_SAMPLE_FMT_S16P) {
2304             smpl_fmt = *smpl_fmts;
2305           }
2306
2307           smpl_fmts++;
2308         }
2309       } else {
2310         smpl_fmt = AV_SAMPLE_FMT_S16;
2311       }
2312       break;
2313     case GST_AUDIO_FORMAT_U8:
2314       if (smpl_fmts) {
2315         while (*smpl_fmts != -1) {
2316           if (*smpl_fmts == AV_SAMPLE_FMT_U8) {
2317             smpl_fmt = *smpl_fmts;
2318             break;
2319           } else if (*smpl_fmts == AV_SAMPLE_FMT_U8P) {
2320             smpl_fmt = *smpl_fmts;
2321           }
2322
2323           smpl_fmts++;
2324         }
2325       } else {
2326         smpl_fmt = AV_SAMPLE_FMT_U8;
2327       }
2328       break;
2329     default:
2330       break;
2331   }
2332
2333   g_assert (smpl_fmt != -1);
2334
2335   context->sample_fmt = smpl_fmt;
2336 }
2337
2338 /* Convert a GstCaps and a FFMPEG codec Type to a
2339  * AVCodecContext. If the context is ommitted, no fixed values
2340  * for video/audio size will be included in the context
2341  *
2342  * AVMediaType is primarily meant for uncompressed data GstCaps!
2343  */
2344
2345 void
2346 gst_ffmpeg_caps_with_codectype (enum AVMediaType type,
2347     const GstCaps * caps, AVCodecContext * context)
2348 {
2349   if (context == NULL)
2350     return;
2351
2352   switch (type) {
2353     case AVMEDIA_TYPE_VIDEO:
2354       gst_ffmpeg_caps_to_pixfmt (caps, context, TRUE);
2355       break;
2356
2357     case AVMEDIA_TYPE_AUDIO:
2358       gst_ffmpeg_caps_to_smpfmt (caps, context, TRUE);
2359       break;
2360
2361     default:
2362       /* unknown */
2363       break;
2364   }
2365 }
2366
2367 #if 0
2368 static void
2369 nal_escape (guint8 * dst, guint8 * src, guint size, guint * destsize)
2370 {
2371   guint8 *dstp = dst;
2372   guint8 *srcp = src;
2373   guint8 *end = src + size;
2374   gint count = 0;
2375
2376   while (srcp < end) {
2377     if (count == 2 && *srcp <= 0x03) {
2378       GST_DEBUG ("added escape code");
2379       *dstp++ = 0x03;
2380       count = 0;
2381     }
2382     if (*srcp == 0)
2383       count++;
2384     else
2385       count = 0;
2386
2387     GST_DEBUG ("copy %02x, count %d", *srcp, count);
2388     *dstp++ = *srcp++;
2389   }
2390   *destsize = dstp - dst;
2391 }
2392
2393 /* copy the config, escaping NAL units as we iterate them, if something fails we
2394  * copy everything and hope for the best. */
2395 static void
2396 copy_config (guint8 * dst, guint8 * src, guint size, guint * destsize)
2397 {
2398   guint8 *dstp = dst;
2399   guint8 *srcp = src;
2400   gint cnt, i;
2401   guint nalsize, esize;
2402
2403   /* check size */
2404   if (size < 7)
2405     goto full_copy;
2406
2407   /* check version */
2408   if (*srcp != 1)
2409     goto full_copy;
2410
2411   cnt = *(srcp + 5) & 0x1f;     /* Number of sps */
2412
2413   GST_DEBUG ("num SPS %d", cnt);
2414
2415   memcpy (dstp, srcp, 6);
2416   srcp += 6;
2417   dstp += 6;
2418
2419   for (i = 0; i < cnt; i++) {
2420     GST_DEBUG ("copy SPS %d", i);
2421     nalsize = (srcp[0] << 8) | srcp[1];
2422     nal_escape (dstp + 2, srcp + 2, nalsize, &esize);
2423     dstp[0] = esize >> 8;
2424     dstp[1] = esize & 0xff;
2425     dstp += esize + 2;
2426     srcp += nalsize + 2;
2427   }
2428
2429   cnt = *(dstp++) = *(srcp++);  /* Number of pps */
2430
2431   GST_DEBUG ("num PPS %d", cnt);
2432
2433   for (i = 0; i < cnt; i++) {
2434     GST_DEBUG ("copy PPS %d", i);
2435     nalsize = (srcp[0] << 8) | srcp[1];
2436     nal_escape (dstp + 2, srcp + 2, nalsize, &esize);
2437     dstp[0] = esize >> 8;
2438     dstp[1] = esize & 0xff;
2439     dstp += esize + 2;
2440     srcp += nalsize + 2;
2441   }
2442   *destsize = dstp - dst;
2443
2444   return;
2445
2446 full_copy:
2447   {
2448     GST_DEBUG ("something unexpected, doing full copy");
2449     memcpy (dst, src, size);
2450     *destsize = size;
2451     return;
2452   }
2453 }
2454 #endif
2455
2456 /*
2457  * caps_with_codecid () transforms a GstCaps for a known codec
2458  * ID into a filled-in context.
2459  * codec_data from caps will override possible extradata already in the context
2460  */
2461
2462 void
2463 gst_ffmpeg_caps_with_codecid (enum CodecID codec_id,
2464     enum AVMediaType codec_type, const GstCaps * caps, AVCodecContext * context)
2465 {
2466   GstStructure *str;
2467   const GValue *value;
2468   GstBuffer *buf;
2469
2470   GST_LOG ("codec_id:%d, codec_type:%d, caps:%" GST_PTR_FORMAT " context:%p",
2471       codec_id, codec_type, caps, context);
2472
2473   if (!context || !gst_caps_get_size (caps))
2474     return;
2475
2476   str = gst_caps_get_structure (caps, 0);
2477
2478   /* extradata parsing (esds [mpeg4], wma/wmv, msmpeg4v1/2/3, etc.) */
2479   if ((value = gst_structure_get_value (str, "codec_data"))) {
2480     GstMapInfo map;
2481
2482     buf = gst_value_get_buffer (value);
2483     gst_buffer_map (buf, &map, GST_MAP_READ);
2484
2485     /* free the old one if it is there */
2486     if (context->extradata)
2487       av_free (context->extradata);
2488
2489 #if 0
2490     if (codec_id == CODEC_ID_H264) {
2491       guint extrasize;
2492
2493       GST_DEBUG ("copy, escaping codec_data %d", size);
2494       /* ffmpeg h264 expects the codec_data to be escaped, there is no real
2495        * reason for this but let's just escape it for now. Start by allocating
2496        * enough space, x2 is more than enough.
2497        *
2498        * FIXME, we disabled escaping because some file already contain escaped
2499        * codec_data and then we escape twice and fail. It's better to leave it
2500        * as is, as that is what most players do. */
2501       context->extradata =
2502           av_mallocz (GST_ROUND_UP_16 (size * 2 +
2503               FF_INPUT_BUFFER_PADDING_SIZE));
2504       copy_config (context->extradata, data, size, &extrasize);
2505       GST_DEBUG ("escaped size: %d", extrasize);
2506       context->extradata_size = extrasize;
2507     } else
2508 #endif
2509     {
2510       /* allocate with enough padding */
2511       GST_DEBUG ("copy codec_data");
2512       context->extradata =
2513           av_mallocz (GST_ROUND_UP_16 (map.size +
2514               FF_INPUT_BUFFER_PADDING_SIZE));
2515       memcpy (context->extradata, map.data, map.size);
2516       context->extradata_size = map.size;
2517     }
2518
2519     /* Hack for VC1. Sometimes the first (length) byte is 0 for some files */
2520     if (codec_id == CODEC_ID_VC1 && map.size > 0 && map.data[0] == 0) {
2521       context->extradata[0] = (guint8) map.size;
2522     }
2523
2524     GST_DEBUG ("have codec data of size %" G_GSIZE_FORMAT, map.size);
2525
2526     gst_buffer_unmap (buf, &map);
2527   } else if (context->extradata == NULL && codec_id != CODEC_ID_AAC_LATM &&
2528       codec_id != CODEC_ID_FLAC) {
2529     /* no extradata, alloc dummy with 0 sized, some codecs insist on reading
2530      * extradata anyway which makes then segfault. */
2531     context->extradata =
2532         av_mallocz (GST_ROUND_UP_16 (FF_INPUT_BUFFER_PADDING_SIZE));
2533     context->extradata_size = 0;
2534     GST_DEBUG ("no codec data");
2535   }
2536
2537   switch (codec_id) {
2538     case CODEC_ID_MPEG4:
2539     {
2540       const gchar *mime = gst_structure_get_name (str);
2541
2542       if (!strcmp (mime, "video/x-divx"))
2543         context->codec_tag = GST_MAKE_FOURCC ('D', 'I', 'V', 'X');
2544       else if (!strcmp (mime, "video/x-xvid"))
2545         context->codec_tag = GST_MAKE_FOURCC ('X', 'V', 'I', 'D');
2546       else if (!strcmp (mime, "video/x-3ivx"))
2547         context->codec_tag = GST_MAKE_FOURCC ('3', 'I', 'V', '1');
2548       else if (!strcmp (mime, "video/mpeg"))
2549         context->codec_tag = GST_MAKE_FOURCC ('m', 'p', '4', 'v');
2550     }
2551       break;
2552
2553     case CODEC_ID_SVQ3:
2554       /* FIXME: this is a workaround for older gst-plugins releases
2555        * (<= 0.8.9). This should be removed at some point, because
2556        * it causes wrong decoded frame order. */
2557       if (!context->extradata) {
2558         gint halfpel_flag, thirdpel_flag, low_delay, unknown_svq3_flag;
2559         guint16 flags;
2560
2561         if (gst_structure_get_int (str, "halfpel_flag", &halfpel_flag) ||
2562             gst_structure_get_int (str, "thirdpel_flag", &thirdpel_flag) ||
2563             gst_structure_get_int (str, "low_delay", &low_delay) ||
2564             gst_structure_get_int (str, "unknown_svq3_flag",
2565                 &unknown_svq3_flag)) {
2566           context->extradata = (guint8 *) av_mallocz (0x64);
2567           g_stpcpy ((gchar *) context->extradata, "SVQ3");
2568           flags = 1 << 3;
2569           flags |= low_delay;
2570           flags = flags << 2;
2571           flags |= unknown_svq3_flag;
2572           flags = flags << 6;
2573           flags |= halfpel_flag;
2574           flags = flags << 1;
2575           flags |= thirdpel_flag;
2576           flags = flags << 3;
2577
2578           flags = GUINT16_FROM_LE (flags);
2579
2580           memcpy ((gchar *) context->extradata + 0x62, &flags, 2);
2581           context->extradata_size = 0x64;
2582         }
2583       }
2584       break;
2585
2586     case CODEC_ID_MSRLE:
2587     case CODEC_ID_QTRLE:
2588     case CODEC_ID_TSCC:
2589     case CODEC_ID_CSCD:
2590     case CODEC_ID_APE:
2591     {
2592       gint depth;
2593
2594       if (gst_structure_get_int (str, "depth", &depth)) {
2595         context->bits_per_coded_sample = depth;
2596       } else {
2597         GST_WARNING ("No depth field in caps %" GST_PTR_FORMAT, caps);
2598       }
2599
2600     }
2601       break;
2602
2603     case CODEC_ID_RV10:
2604     case CODEC_ID_RV20:
2605     case CODEC_ID_RV30:
2606     case CODEC_ID_RV40:
2607     {
2608       gint format;
2609
2610       if (gst_structure_get_int (str, "format", &format))
2611         context->sub_id = format;
2612
2613       break;
2614     }
2615     case CODEC_ID_COOK:
2616     case CODEC_ID_RA_288:
2617     case CODEC_ID_RA_144:
2618     case CODEC_ID_SIPR:
2619     {
2620       gint leaf_size;
2621       gint bitrate;
2622
2623       if (gst_structure_get_int (str, "leaf_size", &leaf_size))
2624         context->block_align = leaf_size;
2625       if (gst_structure_get_int (str, "bitrate", &bitrate))
2626         context->bit_rate = bitrate;
2627     }
2628     case CODEC_ID_ALAC:
2629       gst_structure_get_int (str, "samplesize",
2630           &context->bits_per_coded_sample);
2631       break;
2632
2633     case CODEC_ID_DVVIDEO:
2634     {
2635       const gchar *format;
2636
2637       if ((format = gst_structure_get_string (str, "format"))) {
2638
2639         if (g_str_equal (format, "YUY2"))
2640           context->pix_fmt = PIX_FMT_YUYV422;
2641         else if (g_str_equal (format, "I420"))
2642           context->pix_fmt = PIX_FMT_YUV420P;
2643         else if (g_str_equal (format, "A420"))
2644           context->pix_fmt = PIX_FMT_YUVA420P;
2645         else if (g_str_equal (format, "Y41B"))
2646           context->pix_fmt = PIX_FMT_YUV411P;
2647         else if (g_str_equal (format, "Y42B"))
2648           context->pix_fmt = PIX_FMT_YUV422P;
2649         else if (g_str_equal (format, "YUV9"))
2650           context->pix_fmt = PIX_FMT_YUV410P;
2651         else {
2652           GST_WARNING ("couldn't convert format %s" " to a pixel format",
2653               format);
2654         }
2655       } else
2656         GST_WARNING ("No specified format");
2657       break;
2658     }
2659     case CODEC_ID_H263P:
2660     {
2661       gboolean val;
2662
2663       if (!gst_structure_get_boolean (str, "annex-f", &val) || val)
2664         context->flags |= CODEC_FLAG_4MV;
2665       else
2666         context->flags &= ~CODEC_FLAG_4MV;
2667       if ((!gst_structure_get_boolean (str, "annex-i", &val) || val) &&
2668           (!gst_structure_get_boolean (str, "annex-t", &val) || val))
2669         context->flags |= CODEC_FLAG_AC_PRED;
2670       else
2671         context->flags &= ~CODEC_FLAG_AC_PRED;
2672       if (!gst_structure_get_boolean (str, "annex-j", &val) || val)
2673         context->flags |= CODEC_FLAG_LOOP_FILTER;
2674       else
2675         context->flags &= ~CODEC_FLAG_LOOP_FILTER;
2676       break;
2677     }
2678     case CODEC_ID_ADPCM_G726:
2679     {
2680       const gchar *layout;
2681
2682       if ((layout = gst_structure_get_string (str, "layout"))) {
2683         if (!strcmp (layout, "g721")) {
2684           context->sample_rate = 8000;
2685           context->channels = 1;
2686           context->bit_rate = 32000;
2687         }
2688       }
2689       break;
2690     }
2691     default:
2692       break;
2693   }
2694
2695   if (!gst_caps_is_fixed (caps))
2696     return;
2697
2698   /* common properties (width, height, fps) */
2699   switch (codec_type) {
2700     case AVMEDIA_TYPE_VIDEO:
2701       gst_ffmpeg_caps_to_pixfmt (caps, context, codec_id == CODEC_ID_RAWVIDEO);
2702       break;
2703     case AVMEDIA_TYPE_AUDIO:
2704       gst_ffmpeg_caps_to_smpfmt (caps, context, FALSE);
2705       break;
2706     default:
2707       break;
2708   }
2709
2710   /* fixup of default settings */
2711   switch (codec_id) {
2712     case CODEC_ID_QCELP:
2713       /* QCELP is always mono, no matter what the caps say */
2714       context->channels = 1;
2715       break;
2716     default:
2717       break;
2718   }
2719 }
2720
2721 /* _formatid_to_caps () is meant for muxers/demuxers, it
2722  * transforms a name (ffmpeg way of ID'ing these, why don't
2723  * they have unique numerical IDs?) to the corresponding
2724  * caps belonging to that mux-format
2725  *
2726  * Note: we don't need any additional info because the caps
2727  * isn't supposed to contain any useful info besides the
2728  * media type anyway
2729  */
2730
2731 GstCaps *
2732 gst_ffmpeg_formatid_to_caps (const gchar * format_name)
2733 {
2734   GstCaps *caps = NULL;
2735
2736   if (!strcmp (format_name, "mpeg")) {
2737     caps = gst_caps_new_simple ("video/mpeg",
2738         "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
2739   } else if (!strcmp (format_name, "mpegts")) {
2740     caps = gst_caps_new_simple ("video/mpegts",
2741         "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
2742   } else if (!strcmp (format_name, "rm")) {
2743     caps = gst_caps_new_simple ("application/x-pn-realmedia",
2744         "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
2745   } else if (!strcmp (format_name, "asf")) {
2746     caps = gst_caps_new_empty_simple ("video/x-ms-asf");
2747   } else if (!strcmp (format_name, "avi")) {
2748     caps = gst_caps_new_empty_simple ("video/x-msvideo");
2749   } else if (!strcmp (format_name, "wav")) {
2750     caps = gst_caps_new_empty_simple ("audio/x-wav");
2751   } else if (!strcmp (format_name, "ape")) {
2752     caps = gst_caps_new_empty_simple ("application/x-ape");
2753   } else if (!strcmp (format_name, "swf")) {
2754     caps = gst_caps_new_empty_simple ("application/x-shockwave-flash");
2755   } else if (!strcmp (format_name, "au")) {
2756     caps = gst_caps_new_empty_simple ("audio/x-au");
2757   } else if (!strcmp (format_name, "dv")) {
2758     caps = gst_caps_new_simple ("video/x-dv",
2759         "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
2760   } else if (!strcmp (format_name, "4xm")) {
2761     caps = gst_caps_new_empty_simple ("video/x-4xm");
2762   } else if (!strcmp (format_name, "matroska")) {
2763     caps = gst_caps_new_empty_simple ("video/x-matroska");
2764   } else if (!strcmp (format_name, "mp3")) {
2765     caps = gst_caps_new_empty_simple ("application/x-id3");
2766   } else if (!strcmp (format_name, "flic")) {
2767     caps = gst_caps_new_empty_simple ("video/x-fli");
2768   } else if (!strcmp (format_name, "flv")) {
2769     caps = gst_caps_new_empty_simple ("video/x-flv");
2770   } else if (!strcmp (format_name, "tta")) {
2771     caps = gst_caps_new_empty_simple ("audio/x-ttafile");
2772   } else if (!strcmp (format_name, "aiff")) {
2773     caps = gst_caps_new_empty_simple ("audio/x-aiff");
2774   } else if (!strcmp (format_name, "mov_mp4_m4a_3gp_3g2")) {
2775     caps =
2776         gst_caps_from_string
2777         ("application/x-3gp; video/quicktime; audio/x-m4a");
2778   } else if (!strcmp (format_name, "mov")) {
2779     caps = gst_caps_from_string ("video/quicktime,variant=(string)apple");
2780   } else if (!strcmp (format_name, "mp4")) {
2781     caps = gst_caps_from_string ("video/quicktime,variant=(string)iso");
2782   } else if (!strcmp (format_name, "3gp")) {
2783     caps = gst_caps_from_string ("video/quicktime,variant=(string)3gpp");
2784   } else if (!strcmp (format_name, "3g2")) {
2785     caps = gst_caps_from_string ("video/quicktime,variant=(string)3g2");
2786   } else if (!strcmp (format_name, "psp")) {
2787     caps = gst_caps_from_string ("video/quicktime,variant=(string)psp");
2788   } else if (!strcmp (format_name, "ipod")) {
2789     caps = gst_caps_from_string ("video/quicktime,variant=(string)ipod");
2790   } else if (!strcmp (format_name, "aac")) {
2791     caps = gst_caps_new_simple ("audio/mpeg",
2792         "mpegversion", G_TYPE_INT, 4, NULL);
2793   } else if (!strcmp (format_name, "gif")) {
2794     caps = gst_caps_from_string ("image/gif");
2795   } else if (!strcmp (format_name, "ogg")) {
2796     caps = gst_caps_from_string ("application/ogg");
2797   } else if (!strcmp (format_name, "mxf") || !strcmp (format_name, "mxf_d10")) {
2798     caps = gst_caps_from_string ("application/mxf");
2799   } else if (!strcmp (format_name, "gxf")) {
2800     caps = gst_caps_from_string ("application/gxf");
2801   } else if (!strcmp (format_name, "yuv4mpegpipe")) {
2802     caps = gst_caps_new_simple ("application/x-yuv4mpeg",
2803         "y4mversion", G_TYPE_INT, 2, NULL);
2804   } else if (!strcmp (format_name, "mpc")) {
2805     caps = gst_caps_from_string ("audio/x-musepack, streamversion = (int) 7");
2806   } else if (!strcmp (format_name, "vqf")) {
2807     caps = gst_caps_from_string ("audio/x-vqf");
2808   } else if (!strcmp (format_name, "nsv")) {
2809     caps = gst_caps_from_string ("video/x-nsv");
2810   } else if (!strcmp (format_name, "amr")) {
2811     caps = gst_caps_from_string ("audio/x-amr-nb-sh");
2812   } else if (!strcmp (format_name, "webm")) {
2813     caps = gst_caps_from_string ("video/webm");
2814   } else if (!strcmp (format_name, "voc")) {
2815     caps = gst_caps_from_string ("audio/x-voc");
2816   } else {
2817     gchar *name;
2818
2819     GST_LOG ("Could not create stream format caps for %s", format_name);
2820     name = g_strdup_printf ("application/x-gst-av-%s", format_name);
2821     caps = gst_caps_new_empty_simple (name);
2822     g_free (name);
2823   }
2824
2825   return caps;
2826 }
2827
2828 gboolean
2829 gst_ffmpeg_formatid_get_codecids (const gchar * format_name,
2830     enum CodecID ** video_codec_list, enum CodecID ** audio_codec_list,
2831     AVOutputFormat * plugin)
2832 {
2833   static enum CodecID tmp_vlist[] = {
2834     CODEC_ID_NONE,
2835     CODEC_ID_NONE
2836   };
2837   static enum CodecID tmp_alist[] = {
2838     CODEC_ID_NONE,
2839     CODEC_ID_NONE
2840   };
2841
2842   GST_LOG ("format_name : %s", format_name);
2843
2844   if (!strcmp (format_name, "mp4")) {
2845     static enum CodecID mp4_video_list[] = {
2846       CODEC_ID_MPEG4, CODEC_ID_H264,
2847       CODEC_ID_MJPEG,
2848       CODEC_ID_NONE
2849     };
2850     static enum CodecID mp4_audio_list[] = {
2851       CODEC_ID_AAC, CODEC_ID_MP3,
2852       CODEC_ID_NONE
2853     };
2854
2855     *video_codec_list = mp4_video_list;
2856     *audio_codec_list = mp4_audio_list;
2857   } else if (!strcmp (format_name, "mpeg")) {
2858     static enum CodecID mpeg_video_list[] = { CODEC_ID_MPEG1VIDEO,
2859       CODEC_ID_MPEG2VIDEO,
2860       CODEC_ID_H264,
2861       CODEC_ID_NONE
2862     };
2863     static enum CodecID mpeg_audio_list[] = { CODEC_ID_MP1,
2864       CODEC_ID_MP2,
2865       CODEC_ID_MP3,
2866       CODEC_ID_NONE
2867     };
2868
2869     *video_codec_list = mpeg_video_list;
2870     *audio_codec_list = mpeg_audio_list;
2871   } else if (!strcmp (format_name, "dvd")) {
2872     static enum CodecID mpeg_video_list[] = { CODEC_ID_MPEG2VIDEO,
2873       CODEC_ID_NONE
2874     };
2875     static enum CodecID mpeg_audio_list[] = { CODEC_ID_MP2,
2876       CODEC_ID_AC3,
2877       CODEC_ID_DTS,
2878       CODEC_ID_PCM_S16BE,
2879       CODEC_ID_NONE
2880     };
2881
2882     *video_codec_list = mpeg_video_list;
2883     *audio_codec_list = mpeg_audio_list;
2884   } else if (!strcmp (format_name, "mpegts")) {
2885     static enum CodecID mpegts_video_list[] = { CODEC_ID_MPEG1VIDEO,
2886       CODEC_ID_MPEG2VIDEO,
2887       CODEC_ID_H264,
2888       CODEC_ID_NONE
2889     };
2890     static enum CodecID mpegts_audio_list[] = { CODEC_ID_MP2,
2891       CODEC_ID_MP3,
2892       CODEC_ID_AC3,
2893       CODEC_ID_DTS,
2894       CODEC_ID_AAC,
2895       CODEC_ID_NONE
2896     };
2897
2898     *video_codec_list = mpegts_video_list;
2899     *audio_codec_list = mpegts_audio_list;
2900   } else if (!strcmp (format_name, "vob")) {
2901     static enum CodecID vob_video_list[] =
2902         { CODEC_ID_MPEG2VIDEO, CODEC_ID_NONE };
2903     static enum CodecID vob_audio_list[] = { CODEC_ID_MP2, CODEC_ID_AC3,
2904       CODEC_ID_DTS, CODEC_ID_NONE
2905     };
2906
2907     *video_codec_list = vob_video_list;
2908     *audio_codec_list = vob_audio_list;
2909   } else if (!strcmp (format_name, "flv")) {
2910     static enum CodecID flv_video_list[] = { CODEC_ID_FLV1, CODEC_ID_NONE };
2911     static enum CodecID flv_audio_list[] = { CODEC_ID_MP3, CODEC_ID_NONE };
2912
2913     *video_codec_list = flv_video_list;
2914     *audio_codec_list = flv_audio_list;
2915   } else if (!strcmp (format_name, "asf")) {
2916     static enum CodecID asf_video_list[] =
2917         { CODEC_ID_WMV1, CODEC_ID_WMV2, CODEC_ID_MSMPEG4V3, CODEC_ID_NONE };
2918     static enum CodecID asf_audio_list[] =
2919         { CODEC_ID_WMAV1, CODEC_ID_WMAV2, CODEC_ID_MP3, CODEC_ID_NONE };
2920
2921     *video_codec_list = asf_video_list;
2922     *audio_codec_list = asf_audio_list;
2923   } else if (!strcmp (format_name, "dv")) {
2924     static enum CodecID dv_video_list[] = { CODEC_ID_DVVIDEO, CODEC_ID_NONE };
2925     static enum CodecID dv_audio_list[] = { CODEC_ID_PCM_S16LE, CODEC_ID_NONE };
2926
2927     *video_codec_list = dv_video_list;
2928     *audio_codec_list = dv_audio_list;
2929   } else if (!strcmp (format_name, "mov")) {
2930     static enum CodecID mov_video_list[] = {
2931       CODEC_ID_SVQ1, CODEC_ID_SVQ3, CODEC_ID_MPEG4,
2932       CODEC_ID_H263, CODEC_ID_H263P,
2933       CODEC_ID_H264, CODEC_ID_DVVIDEO,
2934       CODEC_ID_MJPEG,
2935       CODEC_ID_NONE
2936     };
2937     static enum CodecID mov_audio_list[] = {
2938       CODEC_ID_PCM_MULAW, CODEC_ID_PCM_ALAW, CODEC_ID_ADPCM_IMA_QT,
2939       CODEC_ID_MACE3, CODEC_ID_MACE6, CODEC_ID_AAC,
2940       CODEC_ID_AMR_NB, CODEC_ID_AMR_WB,
2941       CODEC_ID_PCM_S16BE, CODEC_ID_PCM_S16LE,
2942       CODEC_ID_MP3, CODEC_ID_NONE
2943     };
2944
2945     *video_codec_list = mov_video_list;
2946     *audio_codec_list = mov_audio_list;
2947   } else if ((!strcmp (format_name, "3gp") || !strcmp (format_name, "3g2"))) {
2948     static enum CodecID tgp_video_list[] = {
2949       CODEC_ID_MPEG4, CODEC_ID_H263, CODEC_ID_H263P, CODEC_ID_H264,
2950       CODEC_ID_NONE
2951     };
2952     static enum CodecID tgp_audio_list[] = {
2953       CODEC_ID_AMR_NB, CODEC_ID_AMR_WB,
2954       CODEC_ID_AAC,
2955       CODEC_ID_NONE
2956     };
2957
2958     *video_codec_list = tgp_video_list;
2959     *audio_codec_list = tgp_audio_list;
2960   } else if (!strcmp (format_name, "mmf")) {
2961     static enum CodecID mmf_audio_list[] = {
2962       CODEC_ID_ADPCM_YAMAHA, CODEC_ID_NONE
2963     };
2964     *video_codec_list = NULL;
2965     *audio_codec_list = mmf_audio_list;
2966   } else if (!strcmp (format_name, "amr")) {
2967     static enum CodecID amr_audio_list[] = {
2968       CODEC_ID_AMR_NB, CODEC_ID_AMR_WB,
2969       CODEC_ID_NONE
2970     };
2971     *video_codec_list = NULL;
2972     *audio_codec_list = amr_audio_list;
2973   } else if (!strcmp (format_name, "gif")) {
2974     static enum CodecID gif_image_list[] = {
2975       CODEC_ID_RAWVIDEO, CODEC_ID_NONE
2976     };
2977     *video_codec_list = gif_image_list;
2978     *audio_codec_list = NULL;
2979   } else if ((plugin->audio_codec != CODEC_ID_NONE) ||
2980       (plugin->video_codec != CODEC_ID_NONE)) {
2981     tmp_vlist[0] = plugin->video_codec;
2982     tmp_alist[0] = plugin->audio_codec;
2983
2984     *video_codec_list = tmp_vlist;
2985     *audio_codec_list = tmp_alist;
2986   } else {
2987     GST_LOG ("Format %s not found", format_name);
2988     return FALSE;
2989   }
2990
2991   return TRUE;
2992 }
2993
2994 /* Convert a GstCaps to a FFMPEG codec ID. Size et all
2995  * are omitted, that can be queried by the user itself,
2996  * we're not eating the GstCaps or anything
2997  * A pointer to an allocated context is also needed for
2998  * optional extra info
2999  */
3000
3001 enum CodecID
3002 gst_ffmpeg_caps_to_codecid (const GstCaps * caps, AVCodecContext * context)
3003 {
3004   enum CodecID id = CODEC_ID_NONE;
3005   const gchar *mimetype;
3006   const GstStructure *structure;
3007   gboolean video = FALSE, audio = FALSE;        /* we want to be sure! */
3008
3009   g_return_val_if_fail (caps != NULL, CODEC_ID_NONE);
3010   g_return_val_if_fail (gst_caps_get_size (caps) == 1, CODEC_ID_NONE);
3011   structure = gst_caps_get_structure (caps, 0);
3012
3013   mimetype = gst_structure_get_name (structure);
3014
3015   if (!strcmp (mimetype, "video/x-raw")) {
3016     id = CODEC_ID_RAWVIDEO;
3017     video = TRUE;
3018   } else if (!strcmp (mimetype, "audio/x-raw")) {
3019     GstAudioInfo info;
3020
3021     if (gst_audio_info_from_caps (&info, caps)) {
3022       switch (GST_AUDIO_INFO_FORMAT (&info)) {
3023         case GST_AUDIO_FORMAT_S8:
3024           id = CODEC_ID_PCM_S8;
3025           break;
3026         case GST_AUDIO_FORMAT_U8:
3027           id = CODEC_ID_PCM_U8;
3028           break;
3029         case GST_AUDIO_FORMAT_S16LE:
3030           id = CODEC_ID_PCM_S16LE;
3031           break;
3032         case GST_AUDIO_FORMAT_S16BE:
3033           id = CODEC_ID_PCM_S16BE;
3034           break;
3035         case GST_AUDIO_FORMAT_U16LE:
3036           id = CODEC_ID_PCM_U16LE;
3037           break;
3038         case GST_AUDIO_FORMAT_U16BE:
3039           id = CODEC_ID_PCM_U16BE;
3040           break;
3041         default:
3042           break;
3043       }
3044       if (id != CODEC_ID_NONE)
3045         audio = TRUE;
3046     }
3047   } else if (!strcmp (mimetype, "audio/x-mulaw")) {
3048     id = CODEC_ID_PCM_MULAW;
3049     audio = TRUE;
3050   } else if (!strcmp (mimetype, "audio/x-alaw")) {
3051     id = CODEC_ID_PCM_ALAW;
3052     audio = TRUE;
3053   } else if (!strcmp (mimetype, "video/x-dv")) {
3054     gboolean sys_strm;
3055
3056     if (gst_structure_get_boolean (structure, "systemstream", &sys_strm) &&
3057         !sys_strm) {
3058       id = CODEC_ID_DVVIDEO;
3059       video = TRUE;
3060     }
3061   } else if (!strcmp (mimetype, "audio/x-dv")) {        /* ??? */
3062     id = CODEC_ID_DVAUDIO;
3063     audio = TRUE;
3064   } else if (!strcmp (mimetype, "video/x-h263")) {
3065     const gchar *h263version =
3066         gst_structure_get_string (structure, "h263version");
3067     if (h263version && !strcmp (h263version, "h263p"))
3068       id = CODEC_ID_H263P;
3069     else
3070       id = CODEC_ID_H263;
3071     video = TRUE;
3072   } else if (!strcmp (mimetype, "video/x-intel-h263")) {
3073     id = CODEC_ID_H263I;
3074     video = TRUE;
3075   } else if (!strcmp (mimetype, "video/x-h261")) {
3076     id = CODEC_ID_H261;
3077     video = TRUE;
3078   } else if (!strcmp (mimetype, "video/mpeg")) {
3079     gboolean sys_strm;
3080     gint mpegversion;
3081
3082     if (gst_structure_get_boolean (structure, "systemstream", &sys_strm) &&
3083         gst_structure_get_int (structure, "mpegversion", &mpegversion) &&
3084         !sys_strm) {
3085       switch (mpegversion) {
3086         case 1:
3087           id = CODEC_ID_MPEG1VIDEO;
3088           break;
3089         case 2:
3090           id = CODEC_ID_MPEG2VIDEO;
3091           break;
3092         case 4:
3093           id = CODEC_ID_MPEG4;
3094           break;
3095       }
3096     }
3097     if (id != CODEC_ID_NONE)
3098       video = TRUE;
3099   } else if (!strcmp (mimetype, "image/jpeg")) {
3100     id = CODEC_ID_MJPEG;        /* A... B... */
3101     video = TRUE;
3102   } else if (!strcmp (mimetype, "video/x-jpeg-b")) {
3103     id = CODEC_ID_MJPEGB;
3104     video = TRUE;
3105   } else if (!strcmp (mimetype, "video/x-wmv")) {
3106     gint wmvversion = 0;
3107
3108     if (gst_structure_get_int (structure, "wmvversion", &wmvversion)) {
3109       switch (wmvversion) {
3110         case 1:
3111           id = CODEC_ID_WMV1;
3112           break;
3113         case 2:
3114           id = CODEC_ID_WMV2;
3115           break;
3116         case 3:
3117         {
3118           const gchar *format;
3119
3120           /* WMV3 unless the fourcc exists and says otherwise */
3121           id = CODEC_ID_WMV3;
3122
3123           if ((format = gst_structure_get_string (structure, "format")) &&
3124               (g_str_equal (format, "WVC1") || g_str_equal (format, "WMVA")))
3125             id = CODEC_ID_VC1;
3126
3127           break;
3128         }
3129       }
3130     }
3131     if (id != CODEC_ID_NONE)
3132       video = TRUE;
3133   } else if (!strcmp (mimetype, "audio/x-vorbis")) {
3134     id = CODEC_ID_VORBIS;
3135     audio = TRUE;
3136   } else if (!strcmp (mimetype, "audio/x-qdm2")) {
3137     id = CODEC_ID_QDM2;
3138     audio = TRUE;
3139   } else if (!strcmp (mimetype, "audio/mpeg")) {
3140     gint layer = 0;
3141     gint mpegversion = 0;
3142
3143     if (gst_structure_get_int (structure, "mpegversion", &mpegversion)) {
3144       switch (mpegversion) {
3145         case 2:                /* ffmpeg uses faad for both... */
3146         case 4:
3147           id = CODEC_ID_AAC;
3148           break;
3149         case 1:
3150           if (gst_structure_get_int (structure, "layer", &layer)) {
3151             switch (layer) {
3152               case 1:
3153                 id = CODEC_ID_MP1;
3154                 break;
3155               case 2:
3156                 id = CODEC_ID_MP2;
3157                 break;
3158               case 3:
3159                 id = CODEC_ID_MP3;
3160                 break;
3161             }
3162           }
3163       }
3164     }
3165     if (id != CODEC_ID_NONE)
3166       audio = TRUE;
3167   } else if (!strcmp (mimetype, "audio/x-musepack")) {
3168     gint streamversion = -1;
3169
3170     if (gst_structure_get_int (structure, "streamversion", &streamversion)) {
3171       if (streamversion == 7)
3172         id = CODEC_ID_MUSEPACK7;
3173     } else {
3174       id = CODEC_ID_MUSEPACK7;
3175     }
3176   } else if (!strcmp (mimetype, "audio/x-wma")) {
3177     gint wmaversion = 0;
3178
3179     if (gst_structure_get_int (structure, "wmaversion", &wmaversion)) {
3180       switch (wmaversion) {
3181         case 1:
3182           id = CODEC_ID_WMAV1;
3183           break;
3184         case 2:
3185           id = CODEC_ID_WMAV2;
3186           break;
3187         case 3:
3188           id = CODEC_ID_WMAPRO;
3189           break;
3190       }
3191     }
3192     if (id != CODEC_ID_NONE)
3193       audio = TRUE;
3194   } else if (!strcmp (mimetype, "audio/x-wms")) {
3195     id = CODEC_ID_WMAVOICE;
3196     audio = TRUE;
3197   } else if (!strcmp (mimetype, "audio/x-ac3")) {
3198     id = CODEC_ID_AC3;
3199     audio = TRUE;
3200   } else if (!strcmp (mimetype, "audio/x-eac3")) {
3201     id = CODEC_ID_EAC3;
3202     audio = TRUE;
3203   } else if (!strcmp (mimetype, "audio/x-vnd.sony.atrac3") ||
3204       !strcmp (mimetype, "audio/atrac3")) {
3205     id = CODEC_ID_ATRAC3;
3206     audio = TRUE;
3207   } else if (!strcmp (mimetype, "audio/x-dts")) {
3208     id = CODEC_ID_DTS;
3209     audio = TRUE;
3210   } else if (!strcmp (mimetype, "application/x-ape")) {
3211     id = CODEC_ID_APE;
3212     audio = TRUE;
3213   } else if (!strcmp (mimetype, "video/x-msmpeg")) {
3214     gint msmpegversion = 0;
3215
3216     if (gst_structure_get_int (structure, "msmpegversion", &msmpegversion)) {
3217       switch (msmpegversion) {
3218         case 41:
3219           id = CODEC_ID_MSMPEG4V1;
3220           break;
3221         case 42:
3222           id = CODEC_ID_MSMPEG4V2;
3223           break;
3224         case 43:
3225           id = CODEC_ID_MSMPEG4V3;
3226           break;
3227       }
3228     }
3229     if (id != CODEC_ID_NONE)
3230       video = TRUE;
3231   } else if (!strcmp (mimetype, "video/x-svq")) {
3232     gint svqversion = 0;
3233
3234     if (gst_structure_get_int (structure, "svqversion", &svqversion)) {
3235       switch (svqversion) {
3236         case 1:
3237           id = CODEC_ID_SVQ1;
3238           break;
3239         case 3:
3240           id = CODEC_ID_SVQ3;
3241           break;
3242       }
3243     }
3244     if (id != CODEC_ID_NONE)
3245       video = TRUE;
3246   } else if (!strcmp (mimetype, "video/x-huffyuv")) {
3247     id = CODEC_ID_HUFFYUV;
3248     video = TRUE;
3249   } else if (!strcmp (mimetype, "audio/x-mace")) {
3250     gint maceversion = 0;
3251
3252     if (gst_structure_get_int (structure, "maceversion", &maceversion)) {
3253       switch (maceversion) {
3254         case 3:
3255           id = CODEC_ID_MACE3;
3256           break;
3257         case 6:
3258           id = CODEC_ID_MACE6;
3259           break;
3260       }
3261     }
3262     if (id != CODEC_ID_NONE)
3263       audio = TRUE;
3264   } else if (!strcmp (mimetype, "video/x-theora")) {
3265     id = CODEC_ID_THEORA;
3266     video = TRUE;
3267   } else if (!strcmp (mimetype, "video/x-vp3")) {
3268     id = CODEC_ID_VP3;
3269     video = TRUE;
3270   } else if (!strcmp (mimetype, "video/x-vp5")) {
3271     id = CODEC_ID_VP5;
3272     video = TRUE;
3273   } else if (!strcmp (mimetype, "video/x-vp6")) {
3274     id = CODEC_ID_VP6;
3275     video = TRUE;
3276   } else if (!strcmp (mimetype, "video/x-vp6-flash")) {
3277     id = CODEC_ID_VP6F;
3278     video = TRUE;
3279   } else if (!strcmp (mimetype, "video/x-vp6-alpha")) {
3280     id = CODEC_ID_VP6A;
3281     video = TRUE;
3282   } else if (!strcmp (mimetype, "video/x-vp8")) {
3283     id = CODEC_ID_VP8;
3284     video = TRUE;
3285   } else if (!strcmp (mimetype, "video/x-flash-screen")) {
3286     id = CODEC_ID_FLASHSV;
3287     video = TRUE;
3288   } else if (!strcmp (mimetype, "video/x-indeo")) {
3289     gint indeoversion = 0;
3290
3291     if (gst_structure_get_int (structure, "indeoversion", &indeoversion)) {
3292       switch (indeoversion) {
3293         case 5:
3294           id = CODEC_ID_INDEO5;
3295           break;
3296         case 4:
3297           id = CODEC_ID_INDEO4;
3298           break;
3299         case 3:
3300           id = CODEC_ID_INDEO3;
3301           break;
3302         case 2:
3303           id = CODEC_ID_INDEO2;
3304           break;
3305       }
3306       if (id != CODEC_ID_NONE)
3307         video = TRUE;
3308     }
3309   } else if (!strcmp (mimetype, "video/x-divx")) {
3310     gint divxversion = 0;
3311
3312     if (gst_structure_get_int (structure, "divxversion", &divxversion)) {
3313       switch (divxversion) {
3314         case 3:
3315           id = CODEC_ID_MSMPEG4V3;
3316           break;
3317         case 4:
3318         case 5:
3319           id = CODEC_ID_MPEG4;
3320           break;
3321       }
3322     }
3323     if (id != CODEC_ID_NONE)
3324       video = TRUE;
3325   } else if (!strcmp (mimetype, "video/x-3ivx")) {
3326     id = CODEC_ID_MPEG4;
3327     video = TRUE;
3328   } else if (!strcmp (mimetype, "video/x-xvid")) {
3329     id = CODEC_ID_MPEG4;
3330     video = TRUE;
3331   } else if (!strcmp (mimetype, "video/x-ffv")) {
3332     gint ffvversion = 0;
3333
3334     if (gst_structure_get_int (structure, "ffvversion", &ffvversion) &&
3335         ffvversion == 1) {
3336       id = CODEC_ID_FFV1;
3337       video = TRUE;
3338     }
3339   } else if (!strcmp (mimetype, "audio/x-adpcm")) {
3340     const gchar *layout;
3341
3342     layout = gst_structure_get_string (structure, "layout");
3343     if (layout == NULL) {
3344       /* break */
3345     } else if (!strcmp (layout, "quicktime")) {
3346       id = CODEC_ID_ADPCM_IMA_QT;
3347     } else if (!strcmp (layout, "microsoft")) {
3348       id = CODEC_ID_ADPCM_MS;
3349     } else if (!strcmp (layout, "dvi")) {
3350       id = CODEC_ID_ADPCM_IMA_WAV;
3351     } else if (!strcmp (layout, "4xm")) {
3352       id = CODEC_ID_ADPCM_4XM;
3353     } else if (!strcmp (layout, "smjpeg")) {
3354       id = CODEC_ID_ADPCM_IMA_SMJPEG;
3355     } else if (!strcmp (layout, "dk3")) {
3356       id = CODEC_ID_ADPCM_IMA_DK3;
3357     } else if (!strcmp (layout, "dk4")) {
3358       id = CODEC_ID_ADPCM_IMA_DK4;
3359     } else if (!strcmp (layout, "westwood")) {
3360       id = CODEC_ID_ADPCM_IMA_WS;
3361     } else if (!strcmp (layout, "iss")) {
3362       id = CODEC_ID_ADPCM_IMA_ISS;
3363     } else if (!strcmp (layout, "xa")) {
3364       id = CODEC_ID_ADPCM_XA;
3365     } else if (!strcmp (layout, "adx")) {
3366       id = CODEC_ID_ADPCM_ADX;
3367     } else if (!strcmp (layout, "ea")) {
3368       id = CODEC_ID_ADPCM_EA;
3369     } else if (!strcmp (layout, "g726")) {
3370       id = CODEC_ID_ADPCM_G726;
3371     } else if (!strcmp (layout, "g721")) {
3372       id = CODEC_ID_ADPCM_G726;
3373     } else if (!strcmp (layout, "ct")) {
3374       id = CODEC_ID_ADPCM_CT;
3375     } else if (!strcmp (layout, "swf")) {
3376       id = CODEC_ID_ADPCM_SWF;
3377     } else if (!strcmp (layout, "yamaha")) {
3378       id = CODEC_ID_ADPCM_YAMAHA;
3379     } else if (!strcmp (layout, "sbpro2")) {
3380       id = CODEC_ID_ADPCM_SBPRO_2;
3381     } else if (!strcmp (layout, "sbpro3")) {
3382       id = CODEC_ID_ADPCM_SBPRO_3;
3383     } else if (!strcmp (layout, "sbpro4")) {
3384       id = CODEC_ID_ADPCM_SBPRO_4;
3385     }
3386     if (id != CODEC_ID_NONE)
3387       audio = TRUE;
3388   } else if (!strcmp (mimetype, "video/x-4xm")) {
3389     id = CODEC_ID_4XM;
3390     video = TRUE;
3391   } else if (!strcmp (mimetype, "audio/x-dpcm")) {
3392     const gchar *layout;
3393
3394     layout = gst_structure_get_string (structure, "layout");
3395     if (!layout) {
3396       /* .. */
3397     } else if (!strcmp (layout, "roq")) {
3398       id = CODEC_ID_ROQ_DPCM;
3399     } else if (!strcmp (layout, "interplay")) {
3400       id = CODEC_ID_INTERPLAY_DPCM;
3401     } else if (!strcmp (layout, "xan")) {
3402       id = CODEC_ID_XAN_DPCM;
3403     } else if (!strcmp (layout, "sol")) {
3404       id = CODEC_ID_SOL_DPCM;
3405     }
3406     if (id != CODEC_ID_NONE)
3407       audio = TRUE;
3408   } else if (!strcmp (mimetype, "audio/x-flac")) {
3409     id = CODEC_ID_FLAC;
3410     audio = TRUE;
3411   } else if (!strcmp (mimetype, "audio/x-shorten")) {
3412     id = CODEC_ID_SHORTEN;
3413     audio = TRUE;
3414   } else if (!strcmp (mimetype, "audio/x-alac")) {
3415     id = CODEC_ID_ALAC;
3416     audio = TRUE;
3417   } else if (!strcmp (mimetype, "video/x-cinepak")) {
3418     id = CODEC_ID_CINEPAK;
3419     video = TRUE;
3420   } else if (!strcmp (mimetype, "video/x-pn-realvideo")) {
3421     gint rmversion;
3422
3423     if (gst_structure_get_int (structure, "rmversion", &rmversion)) {
3424       switch (rmversion) {
3425         case 1:
3426           id = CODEC_ID_RV10;
3427           break;
3428         case 2:
3429           id = CODEC_ID_RV20;
3430           break;
3431         case 3:
3432           id = CODEC_ID_RV30;
3433           break;
3434         case 4:
3435           id = CODEC_ID_RV40;
3436           break;
3437       }
3438     }
3439     if (id != CODEC_ID_NONE)
3440       video = TRUE;
3441   } else if (!strcmp (mimetype, "audio/x-sipro")) {
3442     id = CODEC_ID_SIPR;
3443     audio = TRUE;
3444   } else if (!strcmp (mimetype, "audio/x-pn-realaudio")) {
3445     gint raversion;
3446
3447     if (gst_structure_get_int (structure, "raversion", &raversion)) {
3448       switch (raversion) {
3449         case 1:
3450           id = CODEC_ID_RA_144;
3451           break;
3452         case 2:
3453           id = CODEC_ID_RA_288;
3454           break;
3455         case 8:
3456           id = CODEC_ID_COOK;
3457           break;
3458       }
3459     }
3460     if (id != CODEC_ID_NONE)
3461       audio = TRUE;
3462   } else if (!strcmp (mimetype, "video/x-rle")) {
3463     const gchar *layout;
3464
3465     if ((layout = gst_structure_get_string (structure, "layout"))) {
3466       if (!strcmp (layout, "microsoft")) {
3467         id = CODEC_ID_MSRLE;
3468         video = TRUE;
3469       }
3470     }
3471   } else if (!strcmp (mimetype, "video/x-xan")) {
3472     gint wcversion = 0;
3473
3474     if ((gst_structure_get_int (structure, "wcversion", &wcversion))) {
3475       switch (wcversion) {
3476         case 3:
3477           id = CODEC_ID_XAN_WC3;
3478           video = TRUE;
3479           break;
3480         case 4:
3481           id = CODEC_ID_XAN_WC4;
3482           video = TRUE;
3483           break;
3484         default:
3485           break;
3486       }
3487     }
3488   } else if (!strcmp (mimetype, "audio/AMR")) {
3489     audio = TRUE;
3490     id = CODEC_ID_AMR_NB;
3491   } else if (!strcmp (mimetype, "audio/AMR-WB")) {
3492     id = CODEC_ID_AMR_WB;
3493     audio = TRUE;
3494   } else if (!strcmp (mimetype, "audio/qcelp")) {
3495     id = CODEC_ID_QCELP;
3496     audio = TRUE;
3497   } else if (!strcmp (mimetype, "video/x-h264")) {
3498     id = CODEC_ID_H264;
3499     video = TRUE;
3500   } else if (!strcmp (mimetype, "video/x-flash-video")) {
3501     gint flvversion = 0;
3502
3503     if ((gst_structure_get_int (structure, "flvversion", &flvversion))) {
3504       switch (flvversion) {
3505         case 1:
3506           id = CODEC_ID_FLV1;
3507           video = TRUE;
3508           break;
3509         default:
3510           break;
3511       }
3512     }
3513
3514   } else if (!strcmp (mimetype, "audio/x-nellymoser")) {
3515     id = CODEC_ID_NELLYMOSER;
3516     audio = TRUE;
3517   } else if (!strncmp (mimetype, "audio/x-gst-av-", 15)) {
3518     gchar ext[16];
3519     AVCodec *codec;
3520
3521     if (strlen (mimetype) <= 30 &&
3522         sscanf (mimetype, "audio/x-gst-av-%s", ext) == 1) {
3523       if ((codec = avcodec_find_decoder_by_name (ext)) ||
3524           (codec = avcodec_find_encoder_by_name (ext))) {
3525         id = codec->id;
3526         audio = TRUE;
3527       }
3528     }
3529   } else if (!strncmp (mimetype, "video/x-gst-av-", 15)) {
3530     gchar ext[16];
3531     AVCodec *codec;
3532
3533     if (strlen (mimetype) <= 30 &&
3534         sscanf (mimetype, "video/x-gst-av-%s", ext) == 1) {
3535       if ((codec = avcodec_find_decoder_by_name (ext)) ||
3536           (codec = avcodec_find_encoder_by_name (ext))) {
3537         id = codec->id;
3538         video = TRUE;
3539       }
3540     }
3541   }
3542
3543   if (context != NULL) {
3544     if (video == TRUE) {
3545       context->codec_type = AVMEDIA_TYPE_VIDEO;
3546     } else if (audio == TRUE) {
3547       context->codec_type = AVMEDIA_TYPE_AUDIO;
3548     } else {
3549       context->codec_type = AVMEDIA_TYPE_UNKNOWN;
3550     }
3551     context->codec_id = id;
3552     gst_ffmpeg_caps_with_codecid (id, context->codec_type, caps, context);
3553   }
3554
3555   if (id != CODEC_ID_NONE) {
3556     GST_DEBUG ("The id=%d belongs to the caps %" GST_PTR_FORMAT, id, caps);
3557   } else {
3558     GST_WARNING ("Couldn't figure out the id for caps %" GST_PTR_FORMAT, caps);
3559   }
3560
3561   return id;
3562 }