gstflac.c 4.91 KB
Newer Older
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
1
/* GStreamer
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#include "gstflacenc.h"
#include "gstflacdec.h"

24 25
#include "flac_compat.h"

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
26 27 28
extern GstElementDetails flacenc_details;
extern GstElementDetails flacdec_details;

29
static GstCaps* 	flac_type_find 	(GstBuffer *buf, gpointer private);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
30

Wim Taymans's avatar
Wim Taymans committed
31 32
GstPadTemplate *gst_flacdec_src_template, *gst_flacdec_sink_template; 
GstPadTemplate *gst_flacenc_src_template, *gst_flacenc_sink_template;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
33 34 35 36 37 38 39

static GstCaps*
flac_caps_factory (void)
{
  return
   gst_caps_new (
  	"flac_flac",
40
  	"application/x-flac",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  	NULL);
}

static GstCaps*
raw_caps_factory (void)
{
  return
   gst_caps_new (
  	"flac_raw",
  	"audio/raw",
	gst_props_new (
  	  "format",   		GST_PROPS_STRING ("int"),
    	    "law",   		GST_PROPS_INT (0),
    	    "endianness", 	GST_PROPS_INT (G_BYTE_ORDER),
    	    "signed", 		GST_PROPS_BOOLEAN (TRUE),
    	    "width", 		GST_PROPS_INT (16),
    	    "depth",    	GST_PROPS_INT (16),
    	    "rate",     	GST_PROPS_INT_RANGE (11025, 48000),
    	    "channels", 	GST_PROPS_INT_RANGE (1, 2),
	    NULL));
}

static GstTypeDefinition flacdefinition = {
64 65
  "flac_application/x-flac",
  "application/x-flac",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
66
  ".flac",
67
  flac_type_find,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
68 69 70
};

static GstCaps* 
71
flac_type_find (GstBuffer *buf, gpointer private) 
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
72
{
73 74 75
  gint offset;
  guint8 *data;
  gint size;
Ronald S. Bultje's avatar
Ronald S. Bultje committed
76
  guint32 head = GUINT32_FROM_BE (*((guint32 *)GST_BUFFER_DATA (buf)));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
77

78 79 80
  if (GST_BUFFER_SIZE (buf) < 4)
    return NULL;

81 82
  data = GST_BUFFER_DATA (buf);
  size = GST_BUFFER_SIZE (buf);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  if (head  == 0x664C6143)
    return gst_caps_new ("flac_type_find", "application/x-flac", NULL);
  else {
    /* checks for existance of flac identification header in case
     * there's an ID3 tag */
    for (offset = 0; offset < size-4; offset++) {
      if (data[offset]   == 'f' && 
          data[offset+1] == 'L' && 
          data[offset+2] == 'a' &&
          data[offset+3] == 'C' ) {
        return gst_caps_new ("flac_type_find", "application/x-flac", NULL);
      }
    }
  }

  return NULL;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
100 101 102 103 104 105 106 107 108 109
}


static gboolean
plugin_init (GModule *module, GstPlugin *plugin)
{
  GstElementFactory *enc, *dec;
  GstTypeFactory *type;
  GstCaps *raw_caps, *flac_caps;

110
  /* this filter needs the bytestream package */
111
  if (!gst_library_load ("gstbytestream"))
112 113
    return FALSE;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
114 115 116
  gst_plugin_set_longname (plugin, "The FLAC Lossless compressor Codec");

  /* create an elementfactory for the flacenc element */
117
  enc = gst_element_factory_new ("flacenc", GST_TYPE_FLACENC,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
118 119 120 121 122 123 124
                                &flacenc_details);
  g_return_val_if_fail (enc != NULL, FALSE);

  raw_caps = raw_caps_factory ();
  flac_caps = flac_caps_factory ();

  /* register sink pads */
Wim Taymans's avatar
Wim Taymans committed
125
  gst_flacenc_sink_template = gst_pad_template_new ("sink", GST_PAD_SINK, 
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
126 127
		                              GST_PAD_ALWAYS, 
					      raw_caps, NULL);
Wim Taymans's avatar
Wim Taymans committed
128
  gst_element_factory_add_pad_template (enc, gst_flacenc_sink_template);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
129 130

  /* register src pads */
Wim Taymans's avatar
Wim Taymans committed
131
  gst_flacenc_src_template = gst_pad_template_new ("src", GST_PAD_SRC, 
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
132 133
		                             GST_PAD_ALWAYS, 
					     flac_caps, NULL);
Wim Taymans's avatar
Wim Taymans committed
134
  gst_element_factory_add_pad_template (enc, gst_flacenc_src_template);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
135 136 137 138

  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (enc));

  /* create an elementfactory for the flacdec element */
139
  dec = gst_element_factory_new("flacdec",GST_TYPE_FLACDEC,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
140 141
                               &flacdec_details);
  g_return_val_if_fail(dec != NULL, FALSE);
142
  gst_element_factory_set_rank (dec, GST_ELEMENT_RANK_PRIMARY);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
143 144
 
  /* register sink pads */
Wim Taymans's avatar
Wim Taymans committed
145
  gst_flacdec_sink_template = gst_pad_template_new ("sink", GST_PAD_SINK, 
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
146 147
		                              GST_PAD_ALWAYS, 
					      flac_caps, NULL);
Wim Taymans's avatar
Wim Taymans committed
148
  gst_element_factory_add_pad_template (dec, gst_flacdec_sink_template);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
149 150

  /* register src pads */
Wim Taymans's avatar
Wim Taymans committed
151
  gst_flacdec_src_template = gst_pad_template_new ("src", GST_PAD_SRC, 
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
152 153
		                             GST_PAD_ALWAYS, 
					     raw_caps, NULL);
Wim Taymans's avatar
Wim Taymans committed
154
  gst_element_factory_add_pad_template (dec, gst_flacdec_src_template);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
155 156 157
  
  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (dec));

158
  type = gst_type_factory_new (&flacdefinition);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
159 160 161 162 163 164 165 166 167 168 169
  gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (type));

  return TRUE;
}

GstPluginDesc plugin_desc = {
  GST_VERSION_MAJOR,
  GST_VERSION_MINOR,
  "flac",
  plugin_init
};