gst_plugins_good/gst/mpegaudioparse/gstxingmux.c
changeset 26 69c7080681bf
parent 24 bc39b352897e
child 28 4ed5253bb6ba
equal deleted inserted replaced
24:bc39b352897e 26:69c7080681bf
     1 /*
       
     2  * Copyright (c) 2006 Christophe Fergeau  <teuf@gnome.org>
       
     3  * Copyright (c) 2008 Sebastian Dröge  <slomo@circular-chaos.org>
       
     4  *
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Library General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Library General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU Library General Public
       
    16  * License along with this library; if not, write to the
       
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    18  * Boston, MA 02111-1307, USA.
       
    19  */
       
    20 
       
    21 /* Xing SDK: http://www.mp3-tech.org/programmer/sources/vbrheadersdk.zip */
       
    22 
       
    23 
       
    24 /**
       
    25  * SECTION:element-xingmux
       
    26  *
       
    27  * xingmux adds a Xing header to MP3 files. This contains information about the duration and size
       
    28  * of the file and a seek table and is very useful for getting an almost correct duration and better
       
    29  * seeking on VBR MP3 files.
       
    30  * 
       
    31  * This element will remove any existing Xing, LAME or VBRI headers from the beginning of the file.
       
    32  *
       
    33  * <refsect2>
       
    34  * <title>Example launch line</title>
       
    35  * |[
       
    36  * gst-launch audiotestsrc num-buffers=1000 ! audioconvert ! lame ! xingmux ! filesink location=test.mp3
       
    37  * gst-launch filesrc location=test.mp3 ! xingmux ! filesink location=test2.mp3
       
    38  * gst-launch filesrc location=test.mp3 ! mp3parse ! xingmux ! filesink location=test2.mp3
       
    39  * ]|
       
    40  * </refsect2>
       
    41  */
       
    42 
       
    43 #ifdef HAVE_CONFIG_H
       
    44 #include "../../config.h"
       
    45 #endif
       
    46 
       
    47 #include <string.h>
       
    48 #include "gstxingmux.h"
       
    49 
       
    50 GST_DEBUG_CATEGORY_STATIC (xing_mux_debug);
       
    51 #define GST_CAT_DEFAULT xing_mux_debug
       
    52 
       
    53 GST_BOILERPLATE (GstXingMux, gst_xing_mux, GstElement, GST_TYPE_ELEMENT);
       
    54 
       
    55 /* Xing Header stuff */
       
    56 #define GST_XING_FRAME_FIELD   (1 << 0)
       
    57 #define GST_XING_BYTES_FIELD   (1 << 1)
       
    58 #define GST_XING_TOC_FIELD     (1 << 2)
       
    59 #define GST_XING_QUALITY_FIELD (1 << 3)
       
    60 
       
    61 typedef struct _GstXingSeekEntry
       
    62 {
       
    63   gint64 timestamp;
       
    64   gint byte;
       
    65 } GstXingSeekEntry;
       
    66 
       
    67 static inline GstXingSeekEntry *
       
    68 gst_xing_seek_entry_new ()
       
    69 {
       
    70   return g_slice_new (GstXingSeekEntry);
       
    71 }
       
    72 
       
    73 static inline void
       
    74 gst_xing_seek_entry_free (GstXingSeekEntry * entry)
       
    75 {
       
    76   g_slice_free (GstXingSeekEntry, entry);
       
    77 }
       
    78 
       
    79 static void gst_xing_mux_finalize (GObject * obj);
       
    80 static GstStateChangeReturn
       
    81 gst_xing_mux_change_state (GstElement * element, GstStateChange transition);
       
    82 static GstFlowReturn gst_xing_mux_chain (GstPad * pad, GstBuffer * buffer);
       
    83 static gboolean gst_xing_mux_sink_event (GstPad * pad, GstEvent * event);
       
    84 
       
    85 static GstStaticPadTemplate gst_xing_mux_sink_template =
       
    86 GST_STATIC_PAD_TEMPLATE ("sink",
       
    87     GST_PAD_SINK,
       
    88     GST_PAD_ALWAYS,
       
    89     GST_STATIC_CAPS ("audio/mpeg, "
       
    90         "mpegversion = (int) 1, " "layer = (int) [ 1, 3 ]"));
       
    91 
       
    92 
       
    93 static GstStaticPadTemplate gst_xing_mux_src_template =
       
    94 GST_STATIC_PAD_TEMPLATE ("src",
       
    95     GST_PAD_SRC,
       
    96     GST_PAD_ALWAYS,
       
    97     GST_STATIC_CAPS ("audio/mpeg, "
       
    98         "mpegversion = (int) 1, " "layer = (int) [ 1, 3 ]"));
       
    99 static const guint mp3types_bitrates[2][3][16] = {
       
   100   {
       
   101         {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
       
   102         {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
       
   103         {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
       
   104       },
       
   105   {
       
   106         {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
       
   107         {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
       
   108         {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
       
   109       },
       
   110 };
       
   111 
       
   112 static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
       
   113 {22050, 24000, 16000},
       
   114 {11025, 12000, 8000}
       
   115 };
       
   116 
       
   117 static gboolean
       
   118 parse_header (guint32 header, guint * ret_size, guint * ret_spf,
       
   119     gulong * ret_rate)
       
   120 {
       
   121   guint length, spf;
       
   122   gulong samplerate, bitrate, layer, padding;
       
   123   gint lsf, mpg25;
       
   124 
       
   125   if ((header & 0xffe00000) != 0xffe00000) {
       
   126     g_warning ("invalid sync");
       
   127     return FALSE;
       
   128   }
       
   129 
       
   130   if (((header >> 19) & 3) == 0x01) {
       
   131     g_warning ("invalid MPEG version");
       
   132     return FALSE;
       
   133   }
       
   134 
       
   135   if (((header >> 17) & 3) == 0x00) {
       
   136     g_warning ("invalid MPEG layer");
       
   137     return FALSE;
       
   138   }
       
   139 
       
   140   if (((header >> 12) & 0xf) == 0xf || ((header >> 12) & 0xf) == 0x0) {
       
   141     g_warning ("invalid bitrate");
       
   142     return FALSE;
       
   143   }
       
   144 
       
   145   if (((header >> 10) & 0x3) == 0x3) {
       
   146     g_warning ("invalid sampling rate");
       
   147     return FALSE;
       
   148   }
       
   149 
       
   150   if (header & 0x00000002) {
       
   151     g_warning ("invalid emphasis");
       
   152     return FALSE;
       
   153   }
       
   154 
       
   155   if (header & (1 << 20)) {
       
   156     lsf = (header & (1 << 19)) ? 0 : 1;
       
   157     mpg25 = 0;
       
   158   } else {
       
   159     lsf = 1;
       
   160     mpg25 = 1;
       
   161   }
       
   162 
       
   163   layer = 4 - ((header >> 17) & 0x3);
       
   164 
       
   165   bitrate = (header >> 12) & 0xF;
       
   166   bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
       
   167   if (bitrate == 0)
       
   168     return 0;
       
   169 
       
   170   samplerate = (header >> 10) & 0x3;
       
   171   samplerate = mp3types_freqs[lsf + mpg25][samplerate];
       
   172 
       
   173   padding = (header >> 9) & 0x1;
       
   174 
       
   175   switch (layer) {
       
   176     case 1:
       
   177       length = 4 * ((bitrate * 12) / samplerate + padding);
       
   178       break;
       
   179     case 2:
       
   180       length = (bitrate * 144) / samplerate + padding;
       
   181       break;
       
   182     default:
       
   183     case 3:
       
   184       length = (bitrate * 144) / (samplerate << lsf) + padding;
       
   185       break;
       
   186   }
       
   187 
       
   188   if (layer == 1)
       
   189     spf = 384;
       
   190   else if (layer == 2 || lsf == 0)
       
   191     spf = 1152;
       
   192   else
       
   193     spf = 576;
       
   194 
       
   195   if (ret_size)
       
   196     *ret_size = length;
       
   197   if (ret_spf)
       
   198     *ret_spf = spf;
       
   199   if (ret_rate)
       
   200     *ret_rate = samplerate;
       
   201 
       
   202   return TRUE;
       
   203 }
       
   204 
       
   205 static guint
       
   206 get_xing_offset (guint32 header)
       
   207 {
       
   208   guint mpeg_version = (header >> 19) & 0x3;
       
   209   guint channel_mode = (header >> 6) & 0x3;
       
   210 
       
   211   if (mpeg_version == 0x3) {
       
   212     if (channel_mode == 0x3) {
       
   213       return 0x11;
       
   214     } else {
       
   215       return 0x20;
       
   216     }
       
   217   } else {
       
   218     if (channel_mode == 0x3) {
       
   219       return 0x09;
       
   220     } else {
       
   221       return 0x11;
       
   222     }
       
   223   }
       
   224 }
       
   225 
       
   226 static gboolean
       
   227 has_xing_header (guint32 header, guchar * data, gsize size)
       
   228 {
       
   229   data += 4;
       
   230   data += get_xing_offset (header);
       
   231 
       
   232   if (memcmp (data, "Xing", 4) == 0 ||
       
   233       memcmp (data, "Info", 4) == 0 || memcmp (data, "VBRI", 4) == 0)
       
   234     return TRUE;
       
   235   else
       
   236     return FALSE;
       
   237 }
       
   238 
       
   239 static GstBuffer *
       
   240 generate_xing_header (GstXingMux * xing)
       
   241 {
       
   242   guint8 *xing_flags;
       
   243   guint32 xing_flags_tmp = 0;
       
   244   GstBuffer *xing_header;
       
   245   guchar *data;
       
   246 
       
   247   guint32 header;
       
   248   guint32 header_be;
       
   249   guint size, spf, xing_offset;
       
   250   gulong rate;
       
   251   guint bitrate = 0x00;
       
   252 
       
   253   gint64 duration;
       
   254   gint64 byte_count;
       
   255 
       
   256   header = xing->first_header;
       
   257 
       
   258   /* Set bitrate and choose lowest possible size */
       
   259   do {
       
   260     bitrate++;
       
   261 
       
   262     header &= 0xffff0fff;
       
   263     header |= bitrate << 12;
       
   264 
       
   265     parse_header (header, &size, &spf, &rate);
       
   266     xing_offset = get_xing_offset (header);
       
   267   } while (size < (4 + xing_offset + 4 + 4 + 4 + 4 + 100) && bitrate < 0xe);
       
   268 
       
   269   if (bitrate == 0xe) {
       
   270     GST_ERROR ("No usable bitrate found!");
       
   271     return NULL;
       
   272   }
       
   273 
       
   274   if (gst_pad_alloc_buffer_and_set_caps (xing->srcpad, 0, size,
       
   275           GST_PAD_CAPS (xing->srcpad), &xing_header) != GST_FLOW_OK) {
       
   276     xing_header = gst_buffer_new_and_alloc (size);
       
   277     gst_buffer_set_caps (xing_header, GST_PAD_CAPS (xing->srcpad));
       
   278   }
       
   279 
       
   280   data = GST_BUFFER_DATA (xing_header);
       
   281   memset (data, 0, size);
       
   282   header_be = GUINT32_TO_BE (header);
       
   283   memcpy (data, &header_be, 4);
       
   284 
       
   285   data += 4;
       
   286   data += xing_offset;
       
   287 
       
   288   memcpy (data, "Xing", 4);
       
   289   data += 4;
       
   290 
       
   291   xing_flags = data;
       
   292   data += 4;
       
   293 
       
   294   if (xing->duration != GST_CLOCK_TIME_NONE) {
       
   295     duration = xing->duration;
       
   296   } else {
       
   297     GstFormat fmt = GST_FORMAT_TIME;
       
   298 
       
   299     if (!gst_pad_query_peer_duration (xing->sinkpad, &fmt, &duration))
       
   300       duration = GST_CLOCK_TIME_NONE;
       
   301   }
       
   302 
       
   303   if (duration != GST_CLOCK_TIME_NONE) {
       
   304     guint32 number_of_frames;
       
   305 
       
   306     /* The Xing Header contains a NumberOfFrames field, which verifies to:
       
   307      * Duration = NumberOfFrames *SamplesPerFrame/SamplingRate
       
   308      * SamplesPerFrame and SamplingRate are values for the current frame. 
       
   309      */
       
   310     number_of_frames = gst_util_uint64_scale (duration, rate, GST_SECOND) / spf;
       
   311     GST_DEBUG ("Setting number of frames to %u", number_of_frames);
       
   312     number_of_frames = GUINT32_TO_BE (number_of_frames);
       
   313     memcpy (data, &number_of_frames, 4);
       
   314     xing_flags_tmp |= GST_XING_FRAME_FIELD;
       
   315     data += 4;
       
   316   }
       
   317 
       
   318   if (xing->byte_count != 0) {
       
   319     byte_count = xing->byte_count;
       
   320   } else {
       
   321     GstFormat fmt = GST_FORMAT_BYTES;
       
   322 
       
   323     if (!gst_pad_query_peer_duration (xing->sinkpad, &fmt, &byte_count))
       
   324       byte_count = 0;
       
   325     if (byte_count == -1)
       
   326       byte_count = 0;
       
   327   }
       
   328 
       
   329   if (byte_count != 0) {
       
   330     guint32 nbytes;
       
   331 
       
   332     if (byte_count > G_MAXUINT32) {
       
   333       GST_DEBUG ("Too large stream: %" G_GINT64_FORMAT " > %u bytes",
       
   334           byte_count, G_MAXUINT32);
       
   335     } else {
       
   336       nbytes = byte_count;
       
   337       GST_DEBUG ("Setting number of bytes to %u", nbytes);
       
   338       nbytes = GUINT32_TO_BE (nbytes);
       
   339       memcpy (data, &nbytes, 4);
       
   340       xing_flags_tmp |= GST_XING_BYTES_FIELD;
       
   341       data += 4;
       
   342     }
       
   343   }
       
   344 
       
   345   if (xing->seek_table != NULL && byte_count != 0
       
   346       && duration != GST_CLOCK_TIME_NONE) {
       
   347     GList *it;
       
   348     gint percent = 0;
       
   349 
       
   350     xing_flags_tmp |= GST_XING_TOC_FIELD;
       
   351 
       
   352     GST_DEBUG ("Writing seek table");
       
   353     for (it = xing->seek_table; it != NULL && percent < 100; it = it->next) {
       
   354       GstXingSeekEntry *entry = (GstXingSeekEntry *) it->data;
       
   355       gint64 pos;
       
   356       guchar byte;
       
   357 
       
   358       while ((entry->timestamp * 100) / duration >= percent) {
       
   359         pos = (entry->byte * 256) / byte_count;
       
   360         GST_DEBUG ("  %d %% -- %" G_GINT64_FORMAT " 1/256", percent, pos);
       
   361         byte = (guchar) pos;
       
   362         memcpy (data, &byte, 1);
       
   363         data++;
       
   364         percent++;
       
   365       }
       
   366     }
       
   367 
       
   368     if (percent < 100) {
       
   369       guchar b;
       
   370       gint i;
       
   371 
       
   372       memcpy (&b, data - 1, 1);
       
   373 
       
   374       for (i = percent; i < 100; i++) {
       
   375         GST_DEBUG ("  %d %% -- %d 1/256", i, b);
       
   376         memcpy (data, &b, 1);
       
   377         data++;
       
   378       }
       
   379     }
       
   380   }
       
   381 
       
   382   GST_DEBUG ("Setting Xing flags to 0x%x\n", xing_flags_tmp);
       
   383   xing_flags_tmp = GUINT32_TO_BE (xing_flags_tmp);
       
   384   memcpy (xing_flags, &xing_flags_tmp, 4);
       
   385   return xing_header;
       
   386 }
       
   387 
       
   388 static void
       
   389 gst_xing_mux_base_init (gpointer g_class)
       
   390 {
       
   391   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
       
   392 
       
   393   static const GstElementDetails gst_xing_mux_details =
       
   394       GST_ELEMENT_DETAILS ("MP3 Xing muxer",
       
   395       "Formatter/Metadata",
       
   396       "Adds a Xing header to the beginning of a VBR MP3 file",
       
   397       "Christophe Fergeau <teuf@gnome.org>");
       
   398 
       
   399   gst_element_class_add_pad_template (element_class,
       
   400       gst_static_pad_template_get (&gst_xing_mux_src_template));
       
   401   gst_element_class_add_pad_template (element_class,
       
   402       gst_static_pad_template_get (&gst_xing_mux_sink_template));
       
   403 
       
   404   GST_DEBUG_CATEGORY_INIT (xing_mux_debug, "xingmux", 0, "Xing Header Muxer");
       
   405 
       
   406   gst_element_class_set_details (element_class, &gst_xing_mux_details);
       
   407 }
       
   408 
       
   409 static void
       
   410 gst_xing_mux_class_init (GstXingMuxClass * klass)
       
   411 {
       
   412   GObjectClass *gobject_class;
       
   413   GstElementClass *gstelement_class;
       
   414 
       
   415   gobject_class = (GObjectClass *) klass;
       
   416   gstelement_class = (GstElementClass *) klass;
       
   417 
       
   418   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_xing_mux_finalize);
       
   419   gstelement_class->change_state =
       
   420       GST_DEBUG_FUNCPTR (gst_xing_mux_change_state);
       
   421 }
       
   422 
       
   423 static void
       
   424 gst_xing_mux_finalize (GObject * obj)
       
   425 {
       
   426   GstXingMux *xing = GST_XING_MUX (obj);
       
   427 
       
   428   if (xing->adapter) {
       
   429     g_object_unref (xing->adapter);
       
   430     xing->adapter = NULL;
       
   431   }
       
   432 
       
   433   if (xing->seek_table) {
       
   434     g_list_foreach (xing->seek_table, (GFunc) gst_xing_seek_entry_free, NULL);
       
   435     g_list_free (xing->seek_table);
       
   436     xing->seek_table = NULL;
       
   437   }
       
   438 
       
   439   G_OBJECT_CLASS (parent_class)->finalize (obj);
       
   440 }
       
   441 
       
   442 static void
       
   443 xing_reset (GstXingMux * xing)
       
   444 {
       
   445   xing->duration = GST_CLOCK_TIME_NONE;
       
   446   xing->byte_count = 0;
       
   447 
       
   448   gst_adapter_clear (xing->adapter);
       
   449 
       
   450   if (xing->seek_table) {
       
   451     g_list_foreach (xing->seek_table, (GFunc) gst_xing_seek_entry_free, NULL);
       
   452     g_list_free (xing->seek_table);
       
   453     xing->seek_table = NULL;
       
   454   }
       
   455 
       
   456   xing->sent_xing = FALSE;
       
   457 }
       
   458 
       
   459 
       
   460 static void
       
   461 gst_xing_mux_init (GstXingMux * xing, GstXingMuxClass * xingmux_class)
       
   462 {
       
   463   GstElementClass *klass = GST_ELEMENT_CLASS (xingmux_class);
       
   464 
       
   465   /* pad through which data comes in to the element */
       
   466   xing->sinkpad =
       
   467       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
       
   468           "sink"), "sink");
       
   469   gst_pad_set_setcaps_function (xing->sinkpad,
       
   470       GST_DEBUG_FUNCPTR (gst_pad_proxy_setcaps));
       
   471   gst_pad_set_chain_function (xing->sinkpad,
       
   472       GST_DEBUG_FUNCPTR (gst_xing_mux_chain));
       
   473   gst_pad_set_event_function (xing->sinkpad,
       
   474       GST_DEBUG_FUNCPTR (gst_xing_mux_sink_event));
       
   475   gst_element_add_pad (GST_ELEMENT (xing), xing->sinkpad);
       
   476 
       
   477   /* pad through which data goes out of the element */
       
   478   xing->srcpad =
       
   479       gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
       
   480           "src"), "src");
       
   481   gst_element_add_pad (GST_ELEMENT (xing), xing->srcpad);
       
   482 
       
   483   xing->adapter = gst_adapter_new ();
       
   484 
       
   485   xing_reset (xing);
       
   486 }
       
   487 
       
   488 static GstFlowReturn
       
   489 gst_xing_mux_chain (GstPad * pad, GstBuffer * buffer)
       
   490 {
       
   491   GstXingMux *xing = GST_XING_MUX (GST_PAD_PARENT (pad));
       
   492   GstFlowReturn ret = GST_FLOW_OK;
       
   493 
       
   494   gst_adapter_push (xing->adapter, buffer);
       
   495 
       
   496   while (gst_adapter_available (xing->adapter) >= 4) {
       
   497     const guchar *data = gst_adapter_peek (xing->adapter, 4);
       
   498     guint32 header;
       
   499     GstBuffer *outbuf;
       
   500     GstClockTime duration;
       
   501     guint size, spf;
       
   502     gulong rate;
       
   503     GstXingSeekEntry *seek_entry;
       
   504 
       
   505     header = GST_READ_UINT32_BE (data);
       
   506 
       
   507     if (!parse_header (header, &size, &spf, &rate)) {
       
   508       GST_DEBUG ("Lost sync, resyncing");
       
   509       gst_adapter_flush (xing->adapter, 1);
       
   510       continue;
       
   511     }
       
   512 
       
   513     if (gst_adapter_available (xing->adapter) < size)
       
   514       break;
       
   515 
       
   516     outbuf = gst_adapter_take_buffer (xing->adapter, size);
       
   517     gst_buffer_set_caps (outbuf, GST_PAD_CAPS (xing->srcpad));
       
   518 
       
   519     if (!xing->sent_xing) {
       
   520       if (has_xing_header (header, GST_BUFFER_DATA (outbuf), size)) {
       
   521         GST_LOG_OBJECT (xing, "Dropping old Xing header");
       
   522         gst_buffer_unref (outbuf);
       
   523         continue;
       
   524       } else {
       
   525         GstBuffer *xing_header;
       
   526         guint64 xing_header_size;
       
   527 
       
   528         xing->first_header = header;
       
   529 
       
   530         xing_header = generate_xing_header (xing);
       
   531 
       
   532         if (xing_header == NULL) {
       
   533           GST_ERROR ("Can't generate Xing header");
       
   534           gst_buffer_unref (outbuf);
       
   535           return GST_FLOW_ERROR;
       
   536         }
       
   537 
       
   538         xing_header_size = GST_BUFFER_SIZE (xing_header);
       
   539 
       
   540         if (GST_FLOW_IS_FATAL (ret = gst_pad_push (xing->srcpad, xing_header))) {
       
   541           GST_ERROR_OBJECT (xing, "Failed to push Xing header: %s",
       
   542               gst_flow_get_name (ret));
       
   543           gst_buffer_unref (xing_header);
       
   544           gst_buffer_unref (outbuf);
       
   545           return ret;
       
   546         }
       
   547 
       
   548         xing->byte_count += xing_header_size;
       
   549         xing->sent_xing = TRUE;
       
   550       }
       
   551     }
       
   552 
       
   553     seek_entry = gst_xing_seek_entry_new ();
       
   554     seek_entry->timestamp =
       
   555         (xing->duration == GST_CLOCK_TIME_NONE) ? 0 : xing->duration;
       
   556     /* Workaround for parsers checking that the first seek table entry is 0 */
       
   557     seek_entry->byte = (seek_entry->timestamp == 0) ? 0 : xing->byte_count;
       
   558     xing->seek_table = g_list_append (xing->seek_table, seek_entry);
       
   559 
       
   560     duration = gst_util_uint64_scale (spf, GST_SECOND, rate);
       
   561 
       
   562     GST_BUFFER_TIMESTAMP (outbuf) =
       
   563         (xing->duration == GST_CLOCK_TIME_NONE) ? 0 : xing->duration;
       
   564     GST_BUFFER_DURATION (outbuf) = duration;
       
   565     GST_BUFFER_OFFSET (outbuf) = xing->byte_count;
       
   566     GST_BUFFER_OFFSET_END (outbuf) =
       
   567         xing->byte_count + GST_BUFFER_SIZE (outbuf);
       
   568 
       
   569     xing->byte_count += GST_BUFFER_SIZE (outbuf);
       
   570 
       
   571     if (xing->duration == GST_CLOCK_TIME_NONE)
       
   572       xing->duration = duration;
       
   573     else
       
   574       xing->duration += duration;
       
   575 
       
   576     if (GST_FLOW_IS_FATAL (ret = gst_pad_push (xing->srcpad, outbuf))) {
       
   577       GST_ERROR_OBJECT (xing, "Failed to push MP3 frame: %s",
       
   578           gst_flow_get_name (ret));
       
   579       return ret;
       
   580     }
       
   581   }
       
   582 
       
   583   return ret;
       
   584 }
       
   585 
       
   586 static gboolean
       
   587 gst_xing_mux_sink_event (GstPad * pad, GstEvent * event)
       
   588 {
       
   589   GstXingMux *xing;
       
   590   gboolean result;
       
   591 
       
   592   xing = GST_XING_MUX (gst_pad_get_parent (pad));
       
   593 
       
   594   switch (GST_EVENT_TYPE (event)) {
       
   595     case GST_EVENT_NEWSEGMENT:
       
   596       if (xing->sent_xing) {
       
   597         GST_ERROR ("Already sent Xing header, dropping NEWSEGMENT event!");
       
   598         gst_event_unref (event);
       
   599         result = FALSE;
       
   600       } else {
       
   601         GstFormat fmt;
       
   602 
       
   603         gst_event_parse_new_segment (event, NULL, NULL, &fmt, NULL, NULL, NULL);
       
   604 
       
   605         if (fmt == GST_FORMAT_BYTES) {
       
   606           result = gst_pad_push_event (xing->srcpad, event);
       
   607         } else {
       
   608           gst_event_unref (event);
       
   609 
       
   610           event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
       
   611               0, GST_CLOCK_TIME_NONE, 0);
       
   612 
       
   613           result = gst_pad_push_event (xing->srcpad, event);
       
   614         }
       
   615       }
       
   616       break;
       
   617 
       
   618     case GST_EVENT_EOS:{
       
   619       GstEvent *n_event;
       
   620 
       
   621       GST_DEBUG_OBJECT (xing, "handling EOS event");
       
   622 
       
   623       if (xing->sent_xing) {
       
   624 
       
   625         n_event = gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES,
       
   626             0, GST_CLOCK_TIME_NONE, 0);
       
   627 
       
   628         if (G_UNLIKELY (!gst_pad_push_event (xing->srcpad, n_event))) {
       
   629           GST_WARNING
       
   630               ("Failed to seek to position 0 for pushing the Xing header");
       
   631         } else {
       
   632           GstBuffer *header;
       
   633           GstFlowReturn ret;
       
   634 
       
   635           header = generate_xing_header (xing);
       
   636 
       
   637           if (header == NULL) {
       
   638             GST_ERROR ("Can't generate Xing header");
       
   639           } else {
       
   640 
       
   641             GST_INFO ("Writing real Xing header to beginning of stream");
       
   642 
       
   643             if (GST_FLOW_IS_FATAL (ret = gst_pad_push (xing->srcpad, header)))
       
   644               GST_WARNING ("Failed to push updated Xing header: %s\n",
       
   645                   gst_flow_get_name (ret));
       
   646           }
       
   647         }
       
   648       }
       
   649       result = gst_pad_push_event (xing->srcpad, event);
       
   650       break;
       
   651     }
       
   652     default:
       
   653       result = gst_pad_event_default (pad, event);
       
   654       break;
       
   655   }
       
   656   gst_object_unref (GST_OBJECT (xing));
       
   657 
       
   658   return result;
       
   659 }
       
   660 
       
   661 
       
   662 static GstStateChangeReturn
       
   663 gst_xing_mux_change_state (GstElement * element, GstStateChange transition)
       
   664 {
       
   665   GstXingMux *xing;
       
   666   GstStateChangeReturn result;
       
   667 
       
   668   xing = GST_XING_MUX (element);
       
   669 
       
   670   result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
       
   671 
       
   672   switch (transition) {
       
   673     case GST_STATE_CHANGE_PAUSED_TO_READY:
       
   674       xing_reset (xing);
       
   675       break;
       
   676     default:
       
   677       break;
       
   678   }
       
   679 
       
   680   return result;
       
   681 }