Blame gst/audioparsers/gstmpegaudioparse.c

Packit 1f69a5
/* GStreamer MPEG audio parser
Packit 1f69a5
 * Copyright (C) 2006-2007 Jan Schmidt <thaytan@mad.scientist.com>
Packit 1f69a5
 * Copyright (C) 2010 Mark Nauwelaerts <mnauw users sf net>
Packit 1f69a5
 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
Packit 1f69a5
 *   Contact: Stefan Kost <stefan.kost@nokia.com>
Packit 1f69a5
 *
Packit 1f69a5
 * This library is free software; you can redistribute it and/or
Packit 1f69a5
 * modify it under the terms of the GNU Library General Public
Packit 1f69a5
 * License as published by the Free Software Foundation; either
Packit 1f69a5
 * version 2 of the License, or (at your option) any later version.
Packit 1f69a5
 *
Packit 1f69a5
 * This library is distributed in the hope that it will be useful,
Packit 1f69a5
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 1f69a5
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 1f69a5
 * Library General Public License for more details.
Packit 1f69a5
 *
Packit 1f69a5
 * You should have received a copy of the GNU Library General Public
Packit 1f69a5
 * License along with this library; if not, write to the
Packit 1f69a5
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 1f69a5
 * Boston, MA 02110-1301, USA.
Packit 1f69a5
 */
Packit 1f69a5
/**
Packit 1f69a5
 * SECTION:element-mpegaudioparse
Packit 1f69a5
 * @short_description: MPEG audio parser
Packit 1f69a5
 * @see_also: #GstAmrParse, #GstAACParse
Packit 1f69a5
 *
Packit 1f69a5
 * Parses and frames mpeg1 audio streams. Provides seeking.
Packit 1f69a5
 *
Packit 1f69a5
 * <refsect2>
Packit 1f69a5
 * <title>Example launch line</title>
Packit 1f69a5
 * |[
Packit 1f69a5
 * gst-launch-1.0 filesrc location=test.mp3 ! mpegaudioparse ! mpg123audiodec
Packit 1f69a5
 *  ! audioconvert ! audioresample ! autoaudiosink
Packit 1f69a5
 * ]|
Packit 1f69a5
 * </refsect2>
Packit 1f69a5
 */
Packit 1f69a5
Packit 1f69a5
/* FIXME: we should make the base class (GstBaseParse) aware of the
Packit 1f69a5
 * XING seek table somehow, so it can use it properly for things like
Packit 1f69a5
 * accurate seeks. Currently it can only do a lookup via the convert function,
Packit 1f69a5
 * but then doesn't know what the result represents exactly. One could either
Packit 1f69a5
 * add a vfunc for index lookup, or just make mpegaudioparse populate the
Packit 1f69a5
 * base class's index via the API provided.
Packit 1f69a5
 */
Packit 1f69a5
#ifdef HAVE_CONFIG_H
Packit 1f69a5
#include "config.h"
Packit 1f69a5
#endif
Packit 1f69a5
Packit 1f69a5
#include <string.h>
Packit 1f69a5
Packit 1f69a5
#include "gstmpegaudioparse.h"
Packit 1f69a5
#include <gst/base/gstbytereader.h>
Packit 1f69a5
#include <gst/pbutils/pbutils.h>
Packit 1f69a5
Packit 1f69a5
GST_DEBUG_CATEGORY_STATIC (mpeg_audio_parse_debug);
Packit 1f69a5
#define GST_CAT_DEFAULT mpeg_audio_parse_debug
Packit 1f69a5
Packit 1f69a5
#define MPEG_AUDIO_CHANNEL_MODE_UNKNOWN -1
Packit 1f69a5
#define MPEG_AUDIO_CHANNEL_MODE_STEREO 0
Packit 1f69a5
#define MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO 1
Packit 1f69a5
#define MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL 2
Packit 1f69a5
#define MPEG_AUDIO_CHANNEL_MODE_MONO 3
Packit 1f69a5
Packit 1f69a5
#define CRC_UNKNOWN -1
Packit 1f69a5
#define CRC_PROTECTED 0
Packit 1f69a5
#define CRC_NOT_PROTECTED 1
Packit 1f69a5
Packit 1f69a5
#define XING_FRAMES_FLAG     0x0001
Packit 1f69a5
#define XING_BYTES_FLAG      0x0002
Packit 1f69a5
#define XING_TOC_FLAG        0x0004
Packit 1f69a5
#define XING_VBR_SCALE_FLAG  0x0008
Packit 1f69a5
Packit 1f69a5
#define MIN_FRAME_SIZE       6
Packit 1f69a5
Packit 1f69a5
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
Packit 1f69a5
    GST_PAD_SRC,
Packit 1f69a5
    GST_PAD_ALWAYS,
Packit 1f69a5
    GST_STATIC_CAPS ("audio/mpeg, "
Packit 1f69a5
        "mpegversion = (int) 1, "
Packit 1f69a5
        "layer = (int) [ 1, 3 ], "
Packit 1f69a5
        "mpegaudioversion = (int) [ 1, 3], "
Packit 1f69a5
        "rate = (int) [ 8000, 48000 ], "
Packit 1f69a5
        "channels = (int) [ 1, 2 ], " "parsed=(boolean) true")
Packit 1f69a5
    );
Packit 1f69a5
Packit 1f69a5
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
Packit 1f69a5
    GST_PAD_SINK,
Packit 1f69a5
    GST_PAD_ALWAYS,
Packit 1f69a5
    GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1")
Packit 1f69a5
    );
Packit 1f69a5
Packit 1f69a5
static void gst_mpeg_audio_parse_finalize (GObject * object);
Packit 1f69a5
Packit 1f69a5
static gboolean gst_mpeg_audio_parse_start (GstBaseParse * parse);
Packit 1f69a5
static gboolean gst_mpeg_audio_parse_stop (GstBaseParse * parse);
Packit 1f69a5
static GstFlowReturn gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
Packit 1f69a5
    GstBaseParseFrame * frame, gint * skipsize);
Packit 1f69a5
static GstFlowReturn gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
Packit 1f69a5
    GstBaseParseFrame * frame);
Packit 1f69a5
static gboolean gst_mpeg_audio_parse_convert (GstBaseParse * parse,
Packit 1f69a5
    GstFormat src_format, gint64 src_value,
Packit 1f69a5
    GstFormat dest_format, gint64 * dest_value);
Packit 1f69a5
static GstCaps *gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse,
Packit 1f69a5
    GstCaps * filter);
Packit 1f69a5
Packit 1f69a5
static void gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse *
Packit 1f69a5
    mp3parse, GstBuffer * buf);
Packit 1f69a5
Packit 1f69a5
#define gst_mpeg_audio_parse_parent_class parent_class
Packit 1f69a5
G_DEFINE_TYPE (GstMpegAudioParse, gst_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
Packit 1f69a5
Packit 1f69a5
#define GST_TYPE_MPEG_AUDIO_CHANNEL_MODE  \
Packit 1f69a5
    (gst_mpeg_audio_channel_mode_get_type())
Packit 1f69a5
Packit 1f69a5
static const GEnumValue mpeg_audio_channel_mode[] = {
Packit 1f69a5
  {MPEG_AUDIO_CHANNEL_MODE_UNKNOWN, "Unknown", "unknown"},
Packit 1f69a5
  {MPEG_AUDIO_CHANNEL_MODE_MONO, "Mono", "mono"},
Packit 1f69a5
  {MPEG_AUDIO_CHANNEL_MODE_DUAL_CHANNEL, "Dual Channel", "dual-channel"},
Packit 1f69a5
  {MPEG_AUDIO_CHANNEL_MODE_JOINT_STEREO, "Joint Stereo", "joint-stereo"},
Packit 1f69a5
  {MPEG_AUDIO_CHANNEL_MODE_STEREO, "Stereo", "stereo"},
Packit 1f69a5
  {0, NULL, NULL},
Packit 1f69a5
};
Packit 1f69a5
Packit 1f69a5
static GType
Packit 1f69a5
gst_mpeg_audio_channel_mode_get_type (void)
Packit 1f69a5
{
Packit 1f69a5
  static GType mpeg_audio_channel_mode_type = 0;
Packit 1f69a5
Packit 1f69a5
  if (!mpeg_audio_channel_mode_type) {
Packit 1f69a5
    mpeg_audio_channel_mode_type =
Packit 1f69a5
        g_enum_register_static ("GstMpegAudioChannelMode",
Packit 1f69a5
        mpeg_audio_channel_mode);
Packit 1f69a5
  }
Packit 1f69a5
  return mpeg_audio_channel_mode_type;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static const gchar *
Packit 1f69a5
gst_mpeg_audio_channel_mode_get_nick (gint mode)
Packit 1f69a5
{
Packit 1f69a5
  guint i;
Packit 1f69a5
  for (i = 0; i < G_N_ELEMENTS (mpeg_audio_channel_mode); i++) {
Packit 1f69a5
    if (mpeg_audio_channel_mode[i].value == mode)
Packit 1f69a5
      return mpeg_audio_channel_mode[i].value_nick;
Packit 1f69a5
  }
Packit 1f69a5
  return NULL;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static void
Packit 1f69a5
gst_mpeg_audio_parse_class_init (GstMpegAudioParseClass * klass)
Packit 1f69a5
{
Packit 1f69a5
  GstBaseParseClass *parse_class = GST_BASE_PARSE_CLASS (klass);
Packit 1f69a5
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
Packit 1f69a5
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Packit 1f69a5
Packit 1f69a5
  GST_DEBUG_CATEGORY_INIT (mpeg_audio_parse_debug, "mpegaudioparse", 0,
Packit 1f69a5
      "MPEG1 audio stream parser");
Packit 1f69a5
Packit 1f69a5
  object_class->finalize = gst_mpeg_audio_parse_finalize;
Packit 1f69a5
Packit 1f69a5
  parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_start);
Packit 1f69a5
  parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_stop);
Packit 1f69a5
  parse_class->handle_frame =
Packit 1f69a5
      GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_handle_frame);
Packit 1f69a5
  parse_class->pre_push_frame =
Packit 1f69a5
      GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_pre_push_frame);
Packit 1f69a5
  parse_class->convert = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_convert);
Packit 1f69a5
  parse_class->get_sink_caps =
Packit 1f69a5
      GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_get_sink_caps);
Packit 1f69a5
Packit 1f69a5
  /* register tags */
Packit 1f69a5
#define GST_TAG_CRC      "has-crc"
Packit 1f69a5
#define GST_TAG_MODE     "channel-mode"
Packit 1f69a5
Packit 1f69a5
  gst_tag_register (GST_TAG_CRC, GST_TAG_FLAG_META, G_TYPE_BOOLEAN,
Packit 1f69a5
      "has crc", "Using CRC", NULL);
Packit 1f69a5
  gst_tag_register (GST_TAG_MODE, GST_TAG_FLAG_ENCODED, G_TYPE_STRING,
Packit 1f69a5
      "channel mode", "MPEG audio channel mode", NULL);
Packit 1f69a5
Packit 1f69a5
  g_type_class_ref (GST_TYPE_MPEG_AUDIO_CHANNEL_MODE);
Packit 1f69a5
Packit 1f69a5
  gst_element_class_add_static_pad_template (element_class, &sink_template);
Packit 1f69a5
  gst_element_class_add_static_pad_template (element_class, &src_template);
Packit 1f69a5
Packit 1f69a5
  gst_element_class_set_static_metadata (element_class, "MPEG1 Audio Parser",
Packit 1f69a5
      "Codec/Parser/Audio",
Packit 1f69a5
      "Parses and frames mpeg1 audio streams (levels 1-3), provides seek",
Packit 1f69a5
      "Jan Schmidt <thaytan@mad.scientist.com>,"
Packit 1f69a5
      "Mark Nauwelaerts <mark.nauwelaerts@collabora.co.uk>");
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static void
Packit 1f69a5
gst_mpeg_audio_parse_reset (GstMpegAudioParse * mp3parse)
Packit 1f69a5
{
Packit 1f69a5
  mp3parse->channels = -1;
Packit 1f69a5
  mp3parse->rate = -1;
Packit 1f69a5
  mp3parse->sent_codec_tag = FALSE;
Packit 1f69a5
  mp3parse->last_posted_crc = CRC_UNKNOWN;
Packit 1f69a5
  mp3parse->last_posted_channel_mode = MPEG_AUDIO_CHANNEL_MODE_UNKNOWN;
Packit 1f69a5
  mp3parse->freerate = 0;
Packit 1f69a5
Packit 1f69a5
  mp3parse->hdr_bitrate = 0;
Packit 1f69a5
Packit 1f69a5
  mp3parse->xing_flags = 0;
Packit 1f69a5
  mp3parse->xing_bitrate = 0;
Packit 1f69a5
  mp3parse->xing_frames = 0;
Packit 1f69a5
  mp3parse->xing_total_time = 0;
Packit 1f69a5
  mp3parse->xing_bytes = 0;
Packit 1f69a5
  mp3parse->xing_vbr_scale = 0;
Packit 1f69a5
  memset (mp3parse->xing_seek_table, 0, sizeof (mp3parse->xing_seek_table));
Packit 1f69a5
  memset (mp3parse->xing_seek_table_inverse, 0,
Packit 1f69a5
      sizeof (mp3parse->xing_seek_table_inverse));
Packit 1f69a5
Packit 1f69a5
  mp3parse->vbri_bitrate = 0;
Packit 1f69a5
  mp3parse->vbri_frames = 0;
Packit 1f69a5
  mp3parse->vbri_total_time = 0;
Packit 1f69a5
  mp3parse->vbri_bytes = 0;
Packit 1f69a5
  mp3parse->vbri_seek_points = 0;
Packit 1f69a5
  g_free (mp3parse->vbri_seek_table);
Packit 1f69a5
  mp3parse->vbri_seek_table = NULL;
Packit 1f69a5
Packit 1f69a5
  mp3parse->encoder_delay = 0;
Packit 1f69a5
  mp3parse->encoder_padding = 0;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static void
Packit 1f69a5
gst_mpeg_audio_parse_init (GstMpegAudioParse * mp3parse)
Packit 1f69a5
{
Packit 1f69a5
  gst_mpeg_audio_parse_reset (mp3parse);
Packit 1f69a5
  GST_PAD_SET_ACCEPT_INTERSECT (GST_BASE_PARSE_SINK_PAD (mp3parse));
Packit 1f69a5
  GST_PAD_SET_ACCEPT_TEMPLATE (GST_BASE_PARSE_SINK_PAD (mp3parse));
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static void
Packit 1f69a5
gst_mpeg_audio_parse_finalize (GObject * object)
Packit 1f69a5
{
Packit 1f69a5
  G_OBJECT_CLASS (parent_class)->finalize (object);
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static gboolean
Packit 1f69a5
gst_mpeg_audio_parse_start (GstBaseParse * parse)
Packit 1f69a5
{
Packit 1f69a5
  GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
Packit 1f69a5
Packit 1f69a5
  gst_base_parse_set_min_frame_size (GST_BASE_PARSE (mp3parse), MIN_FRAME_SIZE);
Packit 1f69a5
  GST_DEBUG_OBJECT (parse, "starting");
Packit 1f69a5
Packit 1f69a5
  gst_mpeg_audio_parse_reset (mp3parse);
Packit 1f69a5
Packit 1f69a5
  return TRUE;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static gboolean
Packit 1f69a5
gst_mpeg_audio_parse_stop (GstBaseParse * parse)
Packit 1f69a5
{
Packit 1f69a5
  GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
Packit 1f69a5
Packit 1f69a5
  GST_DEBUG_OBJECT (parse, "stopping");
Packit 1f69a5
Packit 1f69a5
  gst_mpeg_audio_parse_reset (mp3parse);
Packit 1f69a5
Packit 1f69a5
  return TRUE;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static const guint mp3types_bitrates[2][3][16] = {
Packit 1f69a5
  {
Packit 1f69a5
        {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448,},
Packit 1f69a5
        {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384,},
Packit 1f69a5
        {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,}
Packit 1f69a5
      },
Packit 1f69a5
  {
Packit 1f69a5
        {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256,},
Packit 1f69a5
        {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,},
Packit 1f69a5
        {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,}
Packit 1f69a5
      },
Packit 1f69a5
};
Packit 1f69a5
Packit 1f69a5
static const guint mp3types_freqs[3][3] = { {44100, 48000, 32000},
Packit 1f69a5
{22050, 24000, 16000},
Packit 1f69a5
{11025, 12000, 8000}
Packit 1f69a5
};
Packit 1f69a5
Packit 1f69a5
static inline guint
Packit 1f69a5
mp3_type_frame_length_from_header (GstMpegAudioParse * mp3parse, guint32 header,
Packit 1f69a5
    guint * put_version, guint * put_layer, guint * put_channels,
Packit 1f69a5
    guint * put_bitrate, guint * put_samplerate, guint * put_mode,
Packit 1f69a5
    guint * put_crc)
Packit 1f69a5
{
Packit 1f69a5
  guint length;
Packit 1f69a5
  gulong mode, samplerate, bitrate, layer, channels, padding, crc;
Packit 1f69a5
  gulong version;
Packit 1f69a5
  gint lsf, mpg25;
Packit 1f69a5
Packit 1f69a5
  if (header & (1 << 20)) {
Packit 1f69a5
    lsf = (header & (1 << 19)) ? 0 : 1;
Packit 1f69a5
    mpg25 = 0;
Packit 1f69a5
  } else {
Packit 1f69a5
    lsf = 1;
Packit 1f69a5
    mpg25 = 1;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  version = 1 + lsf + mpg25;
Packit 1f69a5
Packit 1f69a5
  layer = 4 - ((header >> 17) & 0x3);
Packit 1f69a5
Packit 1f69a5
  crc = (header >> 16) & 0x1;
Packit 1f69a5
Packit 1f69a5
  bitrate = (header >> 12) & 0xF;
Packit 1f69a5
  bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
Packit 1f69a5
  if (!bitrate) {
Packit 1f69a5
    GST_LOG_OBJECT (mp3parse, "using freeform bitrate");
Packit 1f69a5
    bitrate = mp3parse->freerate;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  samplerate = (header >> 10) & 0x3;
Packit 1f69a5
  samplerate = mp3types_freqs[lsf + mpg25][samplerate];
Packit 1f69a5
Packit 1f69a5
  /* force 0 length if 0 bitrate */
Packit 1f69a5
  padding = (bitrate > 0) ? (header >> 9) & 0x1 : 0;
Packit 1f69a5
Packit 1f69a5
  mode = (header >> 6) & 0x3;
Packit 1f69a5
  channels = (mode == 3) ? 1 : 2;
Packit 1f69a5
Packit 1f69a5
  switch (layer) {
Packit 1f69a5
    case 1:
Packit 1f69a5
      length = 4 * ((bitrate * 12) / samplerate + padding);
Packit 1f69a5
      break;
Packit 1f69a5
    case 2:
Packit 1f69a5
      length = (bitrate * 144) / samplerate + padding;
Packit 1f69a5
      break;
Packit 1f69a5
    default:
Packit 1f69a5
    case 3:
Packit 1f69a5
      length = (bitrate * 144) / (samplerate << lsf) + padding;
Packit 1f69a5
      break;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  GST_DEBUG_OBJECT (mp3parse, "Calculated mp3 frame length of %u bytes",
Packit 1f69a5
      length);
Packit 1f69a5
  GST_DEBUG_OBJECT (mp3parse, "samplerate = %lu, bitrate = %lu, version = %lu, "
Packit 1f69a5
      "layer = %lu, channels = %lu, mode = %s", samplerate, bitrate, version,
Packit 1f69a5
      layer, channels, gst_mpeg_audio_channel_mode_get_nick (mode));
Packit 1f69a5
Packit 1f69a5
  if (put_version)
Packit 1f69a5
    *put_version = version;
Packit 1f69a5
  if (put_layer)
Packit 1f69a5
    *put_layer = layer;
Packit 1f69a5
  if (put_channels)
Packit 1f69a5
    *put_channels = channels;
Packit 1f69a5
  if (put_bitrate)
Packit 1f69a5
    *put_bitrate = bitrate;
Packit 1f69a5
  if (put_samplerate)
Packit 1f69a5
    *put_samplerate = samplerate;
Packit 1f69a5
  if (put_mode)
Packit 1f69a5
    *put_mode = mode;
Packit 1f69a5
  if (put_crc)
Packit 1f69a5
    *put_crc = crc;
Packit 1f69a5
Packit 1f69a5
  return length;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
/* Minimum number of consecutive, valid-looking frames to consider
Packit 1f69a5
 * for resyncing */
Packit 1f69a5
#define MIN_RESYNC_FRAMES 3
Packit 1f69a5
Packit 1f69a5
/* Perform extended validation to check that subsequent headers match
Packit 1f69a5
 * the first header given here in important characteristics, to avoid
Packit 1f69a5
 * false sync. We look for a minimum of MIN_RESYNC_FRAMES consecutive
Packit 1f69a5
 * frames to match their major characteristics.
Packit 1f69a5
 *
Packit 1f69a5
 * If at_eos is set to TRUE, we just check that we don't find any invalid
Packit 1f69a5
 * frames in whatever data is available, rather than requiring a full
Packit 1f69a5
 * MIN_RESYNC_FRAMES of data.
Packit 1f69a5
 *
Packit 1f69a5
 * Returns TRUE if we've seen enough data to validate or reject the frame.
Packit 1f69a5
 * If TRUE is returned, then *valid contains TRUE if it validated, or false
Packit 1f69a5
 * if we decided it was false sync.
Packit 1f69a5
 * If FALSE is returned, then *valid contains minimum needed data.
Packit 1f69a5
 */
Packit 1f69a5
static gboolean
Packit 1f69a5
gst_mp3parse_validate_extended (GstMpegAudioParse * mp3parse, GstBuffer * buf,
Packit 1f69a5
    guint32 header, int bpf, gboolean at_eos, gint * valid)
Packit 1f69a5
{
Packit 1f69a5
  guint32 next_header;
Packit 1f69a5
  GstMapInfo map;
Packit 1f69a5
  gboolean res = TRUE;
Packit 1f69a5
  int frames_found = 1;
Packit 1f69a5
  int offset = bpf;
Packit 1f69a5
Packit 1f69a5
  gst_buffer_map (buf, &map, GST_MAP_READ);
Packit 1f69a5
Packit 1f69a5
  while (frames_found < MIN_RESYNC_FRAMES) {
Packit 1f69a5
    /* Check if we have enough data for all these frames, plus the next
Packit 1f69a5
       frame header. */
Packit 1f69a5
    if (map.size < offset + 4) {
Packit 1f69a5
      if (at_eos) {
Packit 1f69a5
        /* Running out of data at EOS is fine; just accept it */
Packit 1f69a5
        *valid = TRUE;
Packit 1f69a5
        goto cleanup;
Packit 1f69a5
      } else {
Packit 1f69a5
        *valid = offset + 4;
Packit 1f69a5
        res = FALSE;
Packit 1f69a5
        goto cleanup;
Packit 1f69a5
      }
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    next_header = GST_READ_UINT32_BE (map.data + offset);
Packit 1f69a5
    GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X, bpf=%d",
Packit 1f69a5
        offset, (unsigned int) header, (unsigned int) next_header, bpf);
Packit 1f69a5
Packit 1f69a5
/* mask the bits which are allowed to differ between frames */
Packit 1f69a5
#define HDRMASK ~((0xF << 12)  /* bitrate */ | \
Packit 1f69a5
                  (0x1 <<  9)  /* padding */ | \
Packit 1f69a5
                  (0xf <<  4)  /* mode|mode extension */ | \
Packit 1f69a5
                  (0xf))        /* copyright|emphasis */
Packit 1f69a5
Packit 1f69a5
    if ((next_header & HDRMASK) != (header & HDRMASK)) {
Packit 1f69a5
      /* If any of the unmasked bits don't match, then it's not valid */
Packit 1f69a5
      GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
Packit 1f69a5
          "(header=%08X (%08X), header2=%08X (%08X), bpf=%d)",
Packit 1f69a5
          (guint) header, (guint) header & HDRMASK, (guint) next_header,
Packit 1f69a5
          (guint) next_header & HDRMASK, bpf);
Packit 1f69a5
      *valid = FALSE;
Packit 1f69a5
      goto cleanup;
Packit 1f69a5
    } else if (((next_header >> 12) & 0xf) == 0xf) {
Packit 1f69a5
      /* The essential parts were the same, but the bitrate held an
Packit 1f69a5
         invalid value - also reject */
Packit 1f69a5
      GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
Packit 1f69a5
      *valid = FALSE;
Packit 1f69a5
      goto cleanup;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    bpf = mp3_type_frame_length_from_header (mp3parse, next_header,
Packit 1f69a5
        NULL, NULL, NULL, NULL, NULL, NULL, NULL);
Packit 1f69a5
Packit 1f69a5
    /* if no bitrate, and no freeform rate known, then fail */
Packit 1f69a5
    if (G_UNLIKELY (!bpf)) {
Packit 1f69a5
      GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate 0)");
Packit 1f69a5
      *valid = FALSE;
Packit 1f69a5
      goto cleanup;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    offset += bpf;
Packit 1f69a5
    frames_found++;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  *valid = TRUE;
Packit 1f69a5
Packit 1f69a5
cleanup:
Packit 1f69a5
  gst_buffer_unmap (buf, &map);
Packit 1f69a5
  return res;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static gboolean
Packit 1f69a5
gst_mpeg_audio_parse_head_check (GstMpegAudioParse * mp3parse,
Packit 1f69a5
    unsigned long head)
Packit 1f69a5
{
Packit 1f69a5
  GST_DEBUG_OBJECT (mp3parse, "checking mp3 header 0x%08lx", head);
Packit 1f69a5
  /* if it's not a valid sync */
Packit 1f69a5
  if ((head & 0xffe00000) != 0xffe00000) {
Packit 1f69a5
    GST_WARNING_OBJECT (mp3parse, "invalid sync");
Packit 1f69a5
    return FALSE;
Packit 1f69a5
  }
Packit 1f69a5
  /* if it's an invalid MPEG version */
Packit 1f69a5
  if (((head >> 19) & 3) == 0x1) {
Packit 1f69a5
    GST_WARNING_OBJECT (mp3parse, "invalid MPEG version: 0x%lx",
Packit 1f69a5
        (head >> 19) & 3);
Packit 1f69a5
    return FALSE;
Packit 1f69a5
  }
Packit 1f69a5
  /* if it's an invalid layer */
Packit 1f69a5
  if (!((head >> 17) & 3)) {
Packit 1f69a5
    GST_WARNING_OBJECT (mp3parse, "invalid layer: 0x%lx", (head >> 17) & 3);
Packit 1f69a5
    return FALSE;
Packit 1f69a5
  }
Packit 1f69a5
  /* if it's an invalid bitrate */
Packit 1f69a5
  if (((head >> 12) & 0xf) == 0xf) {
Packit 1f69a5
    GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx", (head >> 12) & 0xf);
Packit 1f69a5
    return FALSE;
Packit 1f69a5
  }
Packit 1f69a5
  /* if it's an invalid samplerate */
Packit 1f69a5
  if (((head >> 10) & 0x3) == 0x3) {
Packit 1f69a5
    GST_WARNING_OBJECT (mp3parse, "invalid samplerate: 0x%lx",
Packit 1f69a5
        (head >> 10) & 0x3);
Packit 1f69a5
    return FALSE;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  if ((head & 0x3) == 0x2) {
Packit 1f69a5
    /* Ignore this as there are some files with emphasis 0x2 that can
Packit 1f69a5
     * be played fine. See BGO #537235 */
Packit 1f69a5
    GST_WARNING_OBJECT (mp3parse, "invalid emphasis: 0x%lx", head & 0x3);
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  return TRUE;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
/* Determines possible freeform frame rate/size by looking for next
Packit 1f69a5
 * header with valid bitrate (0 or otherwise valid) (and sufficiently
Packit 1f69a5
 * matching current header).
Packit 1f69a5
 *
Packit 1f69a5
 * Returns TRUE if we've found such one, and *rate then contains rate
Packit 1f69a5
 * (or *rate contains 0 if decided no freeframe size could be determined).
Packit 1f69a5
 * If not enough data, returns FALSE.
Packit 1f69a5
 */
Packit 1f69a5
static gboolean
Packit 1f69a5
gst_mp3parse_find_freerate (GstMpegAudioParse * mp3parse, GstMapInfo * map,
Packit 1f69a5
    guint32 header, gboolean at_eos, gint * _rate)
Packit 1f69a5
{
Packit 1f69a5
  guint32 next_header;
Packit 1f69a5
  const guint8 *data;
Packit 1f69a5
  guint available;
Packit 1f69a5
  int offset = 4;
Packit 1f69a5
  gulong samplerate, rate, layer, padding;
Packit 1f69a5
  gboolean valid;
Packit 1f69a5
  gint lsf, mpg25;
Packit 1f69a5
Packit 1f69a5
  available = map->size;
Packit 1f69a5
  data = map->data;
Packit 1f69a5
Packit 1f69a5
  *_rate = 0;
Packit 1f69a5
Packit 1f69a5
  /* pick apart header again partially */
Packit 1f69a5
  if (header & (1 << 20)) {
Packit 1f69a5
    lsf = (header & (1 << 19)) ? 0 : 1;
Packit 1f69a5
    mpg25 = 0;
Packit 1f69a5
  } else {
Packit 1f69a5
    lsf = 1;
Packit 1f69a5
    mpg25 = 1;
Packit 1f69a5
  }
Packit 1f69a5
  layer = 4 - ((header >> 17) & 0x3);
Packit 1f69a5
  samplerate = (header >> 10) & 0x3;
Packit 1f69a5
  samplerate = mp3types_freqs[lsf + mpg25][samplerate];
Packit 1f69a5
  padding = (header >> 9) & 0x1;
Packit 1f69a5
Packit 1f69a5
  for (; offset < available; ++offset) {
Packit 1f69a5
    /* Check if we have enough data for all these frames, plus the next
Packit 1f69a5
       frame header. */
Packit 1f69a5
    if (available < offset + 4) {
Packit 1f69a5
      if (at_eos) {
Packit 1f69a5
        /* Running out of data; failed to determine size */
Packit 1f69a5
        return TRUE;
Packit 1f69a5
      } else {
Packit 1f69a5
        return FALSE;
Packit 1f69a5
      }
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    valid = FALSE;
Packit 1f69a5
    next_header = GST_READ_UINT32_BE (data + offset);
Packit 1f69a5
    if ((next_header & 0xFFE00000) != 0xFFE00000)
Packit 1f69a5
      goto next;
Packit 1f69a5
Packit 1f69a5
    GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X",
Packit 1f69a5
        offset, (unsigned int) header, (unsigned int) next_header);
Packit 1f69a5
Packit 1f69a5
    if ((next_header & HDRMASK) != (header & HDRMASK)) {
Packit 1f69a5
      /* If any of the unmasked bits don't match, then it's not valid */
Packit 1f69a5
      GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
Packit 1f69a5
          "(header=%08X (%08X), header2=%08X (%08X))",
Packit 1f69a5
          (guint) header, (guint) header & HDRMASK, (guint) next_header,
Packit 1f69a5
          (guint) next_header & HDRMASK);
Packit 1f69a5
      goto next;
Packit 1f69a5
    } else if (((next_header >> 12) & 0xf) == 0xf) {
Packit 1f69a5
      /* The essential parts were the same, but the bitrate held an
Packit 1f69a5
         invalid value - also reject */
Packit 1f69a5
      GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
Packit 1f69a5
      goto next;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    valid = TRUE;
Packit 1f69a5
Packit 1f69a5
  next:
Packit 1f69a5
    /* almost accept as free frame */
Packit 1f69a5
    if (layer == 1) {
Packit 1f69a5
      rate = samplerate * (offset - 4 * padding + 4) / 48000;
Packit 1f69a5
    } else {
Packit 1f69a5
      rate = samplerate * (offset - padding + 1) / (144 >> lsf) / 1000;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    if (valid) {
Packit 1f69a5
      GST_LOG_OBJECT (mp3parse, "calculated rate %lu", rate * 1000);
Packit 1f69a5
      if (rate < 8 || (layer == 3 && rate > 640)) {
Packit 1f69a5
        GST_DEBUG_OBJECT (mp3parse, "rate invalid");
Packit 1f69a5
        if (rate < 8) {
Packit 1f69a5
          /* maybe some hope */
Packit 1f69a5
          continue;
Packit 1f69a5
        } else {
Packit 1f69a5
          GST_DEBUG_OBJECT (mp3parse, "aborting");
Packit 1f69a5
          /* give up */
Packit 1f69a5
          break;
Packit 1f69a5
        }
Packit 1f69a5
      }
Packit 1f69a5
      *_rate = rate * 1000;
Packit 1f69a5
      break;
Packit 1f69a5
    } else {
Packit 1f69a5
      /* avoid indefinite searching */
Packit 1f69a5
      if (rate > 1000) {
Packit 1f69a5
        GST_DEBUG_OBJECT (mp3parse, "exceeded sanity rate; aborting");
Packit 1f69a5
        break;
Packit 1f69a5
      }
Packit 1f69a5
    }
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  return TRUE;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static GstFlowReturn
Packit 1f69a5
gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
Packit 1f69a5
    GstBaseParseFrame * frame, gint * skipsize)
Packit 1f69a5
{
Packit 1f69a5
  GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
Packit 1f69a5
  GstBuffer *buf = frame->buffer;
Packit 1f69a5
  GstByteReader reader;
Packit 1f69a5
  gint off, bpf = 0;
Packit 1f69a5
  gboolean lost_sync, draining, valid, caps_change;
Packit 1f69a5
  guint32 header;
Packit 1f69a5
  guint bitrate, layer, rate, channels, version, mode, crc;
Packit 1f69a5
  GstMapInfo map;
Packit 1f69a5
  gboolean res = FALSE;
Packit 1f69a5
Packit 1f69a5
  gst_buffer_map (buf, &map, GST_MAP_READ);
Packit 1f69a5
  if (G_UNLIKELY (map.size < 6)) {
Packit 1f69a5
    *skipsize = 1;
Packit 1f69a5
    goto cleanup;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  gst_byte_reader_init (&reader, map.data, map.size);
Packit 1f69a5
Packit 1f69a5
  off = gst_byte_reader_masked_scan_uint32 (&reader, 0xffe00000, 0xffe00000,
Packit 1f69a5
      0, map.size);
Packit 1f69a5
Packit 1f69a5
  GST_LOG_OBJECT (parse, "possible sync at buffer offset %d", off);
Packit 1f69a5
Packit 1f69a5
  /* didn't find anything that looks like a sync word, skip */
Packit 1f69a5
  if (off < 0) {
Packit 1f69a5
    *skipsize = map.size - 3;
Packit 1f69a5
    goto cleanup;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  /* possible frame header, but not at offset 0? skip bytes before sync */
Packit 1f69a5
  if (off > 0) {
Packit 1f69a5
    *skipsize = off;
Packit 1f69a5
    goto cleanup;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  /* make sure the values in the frame header look sane */
Packit 1f69a5
  header = GST_READ_UINT32_BE (map.data);
Packit 1f69a5
  if (!gst_mpeg_audio_parse_head_check (mp3parse, header)) {
Packit 1f69a5
    *skipsize = 1;
Packit 1f69a5
    goto cleanup;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  GST_LOG_OBJECT (parse, "got frame");
Packit 1f69a5
Packit 1f69a5
  lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
Packit 1f69a5
  draining = GST_BASE_PARSE_DRAINING (parse);
Packit 1f69a5
Packit 1f69a5
  if (G_UNLIKELY (lost_sync))
Packit 1f69a5
    mp3parse->freerate = 0;
Packit 1f69a5
Packit 1f69a5
  bpf = mp3_type_frame_length_from_header (mp3parse, header,
Packit 1f69a5
      &version, &layer, &channels, &bitrate, &rate, &mode, &crc;;
Packit 1f69a5
Packit 1f69a5
  if (channels != mp3parse->channels || rate != mp3parse->rate ||
Packit 1f69a5
      layer != mp3parse->layer || version != mp3parse->version)
Packit 1f69a5
    caps_change = TRUE;
Packit 1f69a5
  else
Packit 1f69a5
    caps_change = FALSE;
Packit 1f69a5
Packit 1f69a5
  /* maybe free format */
Packit 1f69a5
  if (bpf == 0) {
Packit 1f69a5
    GST_LOG_OBJECT (mp3parse, "possibly free format");
Packit 1f69a5
    if (lost_sync || mp3parse->freerate == 0) {
Packit 1f69a5
      GST_DEBUG_OBJECT (mp3parse, "finding free format rate");
Packit 1f69a5
      if (!gst_mp3parse_find_freerate (mp3parse, &map, header, draining,
Packit 1f69a5
              &valid)) {
Packit 1f69a5
        /* not enough data */
Packit 1f69a5
        gst_base_parse_set_min_frame_size (parse, valid);
Packit 1f69a5
        *skipsize = 0;
Packit 1f69a5
        goto cleanup;
Packit 1f69a5
      } else {
Packit 1f69a5
        GST_DEBUG_OBJECT (parse, "determined freeform size %d", valid);
Packit 1f69a5
        mp3parse->freerate = valid;
Packit 1f69a5
      }
Packit 1f69a5
    }
Packit 1f69a5
    /* try again */
Packit 1f69a5
    bpf = mp3_type_frame_length_from_header (mp3parse, header,
Packit 1f69a5
        &version, &layer, &channels, &bitrate, &rate, &mode, &crc;;
Packit 1f69a5
    if (!bpf) {
Packit 1f69a5
      /* did not come up with valid freeform length, reject after all */
Packit 1f69a5
      *skipsize = 1;
Packit 1f69a5
      goto cleanup;
Packit 1f69a5
    }
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  if (!draining && (lost_sync || caps_change)) {
Packit 1f69a5
    if (!gst_mp3parse_validate_extended (mp3parse, buf, header, bpf, draining,
Packit 1f69a5
            &valid)) {
Packit 1f69a5
      /* not enough data */
Packit 1f69a5
      gst_base_parse_set_min_frame_size (parse, valid);
Packit 1f69a5
      *skipsize = 0;
Packit 1f69a5
      goto cleanup;
Packit 1f69a5
    } else {
Packit 1f69a5
      if (!valid) {
Packit 1f69a5
        *skipsize = off + 2;
Packit 1f69a5
        goto cleanup;
Packit 1f69a5
      }
Packit 1f69a5
    }
Packit 1f69a5
  } else if (draining && lost_sync && caps_change && mp3parse->rate > 0) {
Packit 1f69a5
    /* avoid caps jitter that we can't be sure of */
Packit 1f69a5
    *skipsize = off + 2;
Packit 1f69a5
    goto cleanup;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  /* restore default minimum */
Packit 1f69a5
  gst_base_parse_set_min_frame_size (parse, MIN_FRAME_SIZE);
Packit 1f69a5
Packit 1f69a5
  res = TRUE;
Packit 1f69a5
Packit 1f69a5
  /* metadata handling */
Packit 1f69a5
  if (G_UNLIKELY (caps_change)) {
Packit 1f69a5
    GstCaps *caps = gst_caps_new_simple ("audio/mpeg",
Packit 1f69a5
        "mpegversion", G_TYPE_INT, 1,
Packit 1f69a5
        "mpegaudioversion", G_TYPE_INT, version,
Packit 1f69a5
        "layer", G_TYPE_INT, layer,
Packit 1f69a5
        "rate", G_TYPE_INT, rate,
Packit 1f69a5
        "channels", G_TYPE_INT, channels, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
Packit 1f69a5
    gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
Packit 1f69a5
    gst_caps_unref (caps);
Packit 1f69a5
Packit 1f69a5
    mp3parse->rate = rate;
Packit 1f69a5
    mp3parse->channels = channels;
Packit 1f69a5
    mp3parse->layer = layer;
Packit 1f69a5
    mp3parse->version = version;
Packit 1f69a5
Packit 1f69a5
    /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
Packit 1f69a5
    if (mp3parse->layer == 1)
Packit 1f69a5
      mp3parse->spf = 384;
Packit 1f69a5
    else if (mp3parse->layer == 2)
Packit 1f69a5
      mp3parse->spf = 1152;
Packit 1f69a5
    else if (mp3parse->version == 1) {
Packit 1f69a5
      mp3parse->spf = 1152;
Packit 1f69a5
    } else {
Packit 1f69a5
      /* MPEG-2 or "2.5" */
Packit 1f69a5
      mp3parse->spf = 576;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    /* lead_in:
Packit 1f69a5
     * We start pushing 9 frames earlier (29 frames for MPEG2) than
Packit 1f69a5
     * segment start to be able to decode the first frame we want.
Packit 1f69a5
     * 9 (29) frames are the theoretical maximum of frames that contain
Packit 1f69a5
     * data for the current frame (bit reservoir).
Packit 1f69a5
     *
Packit 1f69a5
     * lead_out:
Packit 1f69a5
     * Some mp3 streams have an offset in the timestamps, for which we have to
Packit 1f69a5
     * push the frame *after* the end position in order for the decoder to be
Packit 1f69a5
     * able to decode everything up until the segment.stop position. */
Packit 1f69a5
    gst_base_parse_set_frame_rate (parse, mp3parse->rate, mp3parse->spf,
Packit 1f69a5
        (version == 1) ? 10 : 30, 2);
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  mp3parse->hdr_bitrate = bitrate;
Packit 1f69a5
Packit 1f69a5
  /* For first frame; check for seek tables and output a codec tag */
Packit 1f69a5
  gst_mpeg_audio_parse_handle_first_frame (mp3parse, buf);
Packit 1f69a5
Packit 1f69a5
  /* store some frame info for later processing */
Packit 1f69a5
  mp3parse->last_crc = crc;
Packit 1f69a5
  mp3parse->last_mode = mode;
Packit 1f69a5
Packit 1f69a5
cleanup:
Packit 1f69a5
  gst_buffer_unmap (buf, &map);
Packit 1f69a5
Packit 1f69a5
  if (res && bpf <= map.size) {
Packit 1f69a5
    return gst_base_parse_finish_frame (parse, frame, bpf);
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  return GST_FLOW_OK;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static void
Packit 1f69a5
gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse * mp3parse,
Packit 1f69a5
    GstBuffer * buf)
Packit 1f69a5
{
Packit 1f69a5
  const guint32 xing_id = 0x58696e67;   /* 'Xing' in hex */
Packit 1f69a5
  const guint32 info_id = 0x496e666f;   /* 'Info' in hex - found in LAME CBR files */
Packit 1f69a5
  const guint32 vbri_id = 0x56425249;   /* 'VBRI' in hex */
Packit 1f69a5
  const guint32 lame_id = 0x4c414d45;   /* 'LAME' in hex */
Packit 1f69a5
  gint offset_xing, offset_vbri;
Packit 1f69a5
  guint64 avail;
Packit 1f69a5
  gint64 upstream_total_bytes = 0;
Packit 1f69a5
  guint32 read_id_xing = 0, read_id_vbri = 0;
Packit 1f69a5
  GstMapInfo map;
Packit 1f69a5
  guint8 *data;
Packit 1f69a5
  guint bitrate;
Packit 1f69a5
Packit 1f69a5
  if (mp3parse->sent_codec_tag)
Packit 1f69a5
    return;
Packit 1f69a5
Packit 1f69a5
  /* Check first frame for Xing info */
Packit 1f69a5
  if (mp3parse->version == 1) { /* MPEG-1 file */
Packit 1f69a5
    if (mp3parse->channels == 1)
Packit 1f69a5
      offset_xing = 0x11;
Packit 1f69a5
    else
Packit 1f69a5
      offset_xing = 0x20;
Packit 1f69a5
  } else {                      /* MPEG-2 header */
Packit 1f69a5
    if (mp3parse->channels == 1)
Packit 1f69a5
      offset_xing = 0x09;
Packit 1f69a5
    else
Packit 1f69a5
      offset_xing = 0x11;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  /* The VBRI tag is always at offset 0x20 */
Packit 1f69a5
  offset_vbri = 0x20;
Packit 1f69a5
Packit 1f69a5
  /* Skip the 4 bytes of the MP3 header too */
Packit 1f69a5
  offset_xing += 4;
Packit 1f69a5
  offset_vbri += 4;
Packit 1f69a5
Packit 1f69a5
  /* Check if we have enough data to read the Xing header */
Packit 1f69a5
  gst_buffer_map (buf, &map, GST_MAP_READ);
Packit 1f69a5
  data = map.data;
Packit 1f69a5
  avail = map.size;
Packit 1f69a5
Packit 1f69a5
  if (avail >= offset_xing + 4) {
Packit 1f69a5
    read_id_xing = GST_READ_UINT32_BE (data + offset_xing);
Packit 1f69a5
  }
Packit 1f69a5
  if (avail >= offset_vbri + 4) {
Packit 1f69a5
    read_id_vbri = GST_READ_UINT32_BE (data + offset_vbri);
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  /* obtain real upstream total bytes */
Packit 1f69a5
  if (!gst_pad_peer_query_duration (GST_BASE_PARSE_SINK_PAD (mp3parse),
Packit 1f69a5
          GST_FORMAT_BYTES, &upstream_total_bytes))
Packit 1f69a5
    upstream_total_bytes = 0;
Packit 1f69a5
Packit 1f69a5
  if (read_id_xing == xing_id || read_id_xing == info_id) {
Packit 1f69a5
    guint32 xing_flags;
Packit 1f69a5
    guint bytes_needed = offset_xing + 8;
Packit 1f69a5
    gint64 total_bytes;
Packit 1f69a5
    GstClockTime total_time;
Packit 1f69a5
Packit 1f69a5
    GST_DEBUG_OBJECT (mp3parse, "Found Xing header marker 0x%x", xing_id);
Packit 1f69a5
Packit 1f69a5
    /* Move data after Xing header */
Packit 1f69a5
    data += offset_xing + 4;
Packit 1f69a5
Packit 1f69a5
    /* Read 4 base bytes of flags, big-endian */
Packit 1f69a5
    xing_flags = GST_READ_UINT32_BE (data);
Packit 1f69a5
    data += 4;
Packit 1f69a5
    if (xing_flags & XING_FRAMES_FLAG)
Packit 1f69a5
      bytes_needed += 4;
Packit 1f69a5
    if (xing_flags & XING_BYTES_FLAG)
Packit 1f69a5
      bytes_needed += 4;
Packit 1f69a5
    if (xing_flags & XING_TOC_FLAG)
Packit 1f69a5
      bytes_needed += 100;
Packit 1f69a5
    if (xing_flags & XING_VBR_SCALE_FLAG)
Packit 1f69a5
      bytes_needed += 4;
Packit 1f69a5
    if (avail < bytes_needed) {
Packit 1f69a5
      GST_DEBUG_OBJECT (mp3parse,
Packit 1f69a5
          "Not enough data to read Xing header (need %d)", bytes_needed);
Packit 1f69a5
      goto cleanup;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    GST_DEBUG_OBJECT (mp3parse, "Reading Xing header");
Packit 1f69a5
    mp3parse->xing_flags = xing_flags;
Packit 1f69a5
Packit 1f69a5
    if (xing_flags & XING_FRAMES_FLAG) {
Packit 1f69a5
      mp3parse->xing_frames = GST_READ_UINT32_BE (data);
Packit 1f69a5
      if (mp3parse->xing_frames == 0) {
Packit 1f69a5
        GST_WARNING_OBJECT (mp3parse,
Packit 1f69a5
            "Invalid number of frames in Xing header");
Packit 1f69a5
        mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
Packit 1f69a5
      } else {
Packit 1f69a5
        mp3parse->xing_total_time = gst_util_uint64_scale (GST_SECOND,
Packit 1f69a5
            (guint64) (mp3parse->xing_frames) * (mp3parse->spf),
Packit 1f69a5
            mp3parse->rate);
Packit 1f69a5
      }
Packit 1f69a5
Packit 1f69a5
      data += 4;
Packit 1f69a5
    } else {
Packit 1f69a5
      mp3parse->xing_frames = 0;
Packit 1f69a5
      mp3parse->xing_total_time = 0;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    if (xing_flags & XING_BYTES_FLAG) {
Packit 1f69a5
      mp3parse->xing_bytes = GST_READ_UINT32_BE (data);
Packit 1f69a5
      if (mp3parse->xing_bytes == 0) {
Packit 1f69a5
        GST_WARNING_OBJECT (mp3parse, "Invalid number of bytes in Xing header");
Packit 1f69a5
        mp3parse->xing_flags &= ~XING_BYTES_FLAG;
Packit 1f69a5
      }
Packit 1f69a5
      data += 4;
Packit 1f69a5
    } else {
Packit 1f69a5
      mp3parse->xing_bytes = 0;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    /* If we know the upstream size and duration, compute the
Packit 1f69a5
     * total bitrate, rounded up to the nearest kbit/sec */
Packit 1f69a5
    if ((total_time = mp3parse->xing_total_time) &&
Packit 1f69a5
        (total_bytes = mp3parse->xing_bytes)) {
Packit 1f69a5
      mp3parse->xing_bitrate = gst_util_uint64_scale (total_bytes,
Packit 1f69a5
          8 * GST_SECOND, total_time);
Packit 1f69a5
      mp3parse->xing_bitrate += 500;
Packit 1f69a5
      mp3parse->xing_bitrate -= mp3parse->xing_bitrate % 1000;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    if (xing_flags & XING_TOC_FLAG) {
Packit 1f69a5
      int i, percent = 0;
Packit 1f69a5
      guchar *table = mp3parse->xing_seek_table;
Packit 1f69a5
      guchar old = 0, new;
Packit 1f69a5
      guint first;
Packit 1f69a5
Packit 1f69a5
      first = data[0];
Packit 1f69a5
      GST_DEBUG_OBJECT (mp3parse,
Packit 1f69a5
          "Subtracting initial offset of %d bytes from Xing TOC", first);
Packit 1f69a5
Packit 1f69a5
      /* xing seek table: percent time -> 1/256 bytepos */
Packit 1f69a5
      for (i = 0; i < 100; i++) {
Packit 1f69a5
        new = data[i] - first;
Packit 1f69a5
        if (old > new) {
Packit 1f69a5
          GST_WARNING_OBJECT (mp3parse, "Skipping broken Xing TOC");
Packit 1f69a5
          mp3parse->xing_flags &= ~XING_TOC_FLAG;
Packit 1f69a5
          goto skip_toc;
Packit 1f69a5
        }
Packit 1f69a5
        mp3parse->xing_seek_table[i] = old = new;
Packit 1f69a5
      }
Packit 1f69a5
Packit 1f69a5
      /* build inverse table: 1/256 bytepos -> 1/100 percent time */
Packit 1f69a5
      for (i = 0; i < 256; i++) {
Packit 1f69a5
        while (percent < 99 && table[percent + 1] <= i)
Packit 1f69a5
          percent++;
Packit 1f69a5
Packit 1f69a5
        if (table[percent] == i) {
Packit 1f69a5
          mp3parse->xing_seek_table_inverse[i] = percent * 100;
Packit 1f69a5
        } else if (percent < 99 && table[percent]) {
Packit 1f69a5
          gdouble fa, fb, fx;
Packit 1f69a5
          gint a = percent, b = percent + 1;
Packit 1f69a5
Packit 1f69a5
          fa = table[a];
Packit 1f69a5
          fb = table[b];
Packit 1f69a5
          fx = (b - a) / (fb - fa) * (i - fa) + a;
Packit 1f69a5
          mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
Packit 1f69a5
        } else if (percent == 99) {
Packit 1f69a5
          gdouble fa, fb, fx;
Packit 1f69a5
          gint a = percent, b = 100;
Packit 1f69a5
Packit 1f69a5
          fa = table[a];
Packit 1f69a5
          fb = 256.0;
Packit 1f69a5
          fx = (b - a) / (fb - fa) * (i - fa) + a;
Packit 1f69a5
          mp3parse->xing_seek_table_inverse[i] = (guint16) (fx * 100);
Packit 1f69a5
        }
Packit 1f69a5
      }
Packit 1f69a5
    skip_toc:
Packit 1f69a5
      data += 100;
Packit 1f69a5
    } else {
Packit 1f69a5
      memset (mp3parse->xing_seek_table, 0, sizeof (mp3parse->xing_seek_table));
Packit 1f69a5
      memset (mp3parse->xing_seek_table_inverse, 0,
Packit 1f69a5
          sizeof (mp3parse->xing_seek_table_inverse));
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    if (xing_flags & XING_VBR_SCALE_FLAG) {
Packit 1f69a5
      mp3parse->xing_vbr_scale = GST_READ_UINT32_BE (data);
Packit 1f69a5
      data += 4;
Packit 1f69a5
    } else
Packit 1f69a5
      mp3parse->xing_vbr_scale = 0;
Packit 1f69a5
Packit 1f69a5
    GST_DEBUG_OBJECT (mp3parse, "Xing header reported %u frames, time %"
Packit 1f69a5
        GST_TIME_FORMAT ", %u bytes, vbr scale %u", mp3parse->xing_frames,
Packit 1f69a5
        GST_TIME_ARGS (mp3parse->xing_total_time), mp3parse->xing_bytes,
Packit 1f69a5
        mp3parse->xing_vbr_scale);
Packit 1f69a5
Packit 1f69a5
    /* check for truncated file */
Packit 1f69a5
    if (upstream_total_bytes && mp3parse->xing_bytes &&
Packit 1f69a5
        mp3parse->xing_bytes * 0.8 > upstream_total_bytes) {
Packit 1f69a5
      GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
Packit 1f69a5
          "invalidating Xing header duration and size");
Packit 1f69a5
      mp3parse->xing_flags &= ~XING_BYTES_FLAG;
Packit 1f69a5
      mp3parse->xing_flags &= ~XING_FRAMES_FLAG;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    /* Optional LAME tag? */
Packit 1f69a5
    if (avail - bytes_needed >= 36 && GST_READ_UINT32_BE (data) == lame_id) {
Packit 1f69a5
      gchar lame_version[10] = { 0, };
Packit 1f69a5
      guint tag_rev;
Packit 1f69a5
      guint32 encoder_delay, encoder_padding;
Packit 1f69a5
Packit 1f69a5
      memcpy (lame_version, data, 9);
Packit 1f69a5
      data += 9;
Packit 1f69a5
      tag_rev = data[0] >> 4;
Packit 1f69a5
      GST_DEBUG_OBJECT (mp3parse, "Found LAME tag revision %d created by '%s'",
Packit 1f69a5
          tag_rev, lame_version);
Packit 1f69a5
Packit 1f69a5
      /* Skip all the information we're not interested in */
Packit 1f69a5
      data += 12;
Packit 1f69a5
      /* Encoder delay and end padding */
Packit 1f69a5
      encoder_delay = GST_READ_UINT24_BE (data);
Packit 1f69a5
      encoder_delay >>= 12;
Packit 1f69a5
      encoder_padding = GST_READ_UINT24_BE (data);
Packit 1f69a5
      encoder_padding &= 0x000fff;
Packit 1f69a5
Packit 1f69a5
      mp3parse->encoder_delay = encoder_delay;
Packit 1f69a5
      mp3parse->encoder_padding = encoder_padding;
Packit 1f69a5
Packit 1f69a5
      GST_DEBUG_OBJECT (mp3parse, "Encoder delay %u, encoder padding %u",
Packit 1f69a5
          encoder_delay, encoder_padding);
Packit 1f69a5
    }
Packit 1f69a5
  } else if (read_id_vbri == vbri_id) {
Packit 1f69a5
    gint64 total_bytes, total_frames;
Packit 1f69a5
    GstClockTime total_time;
Packit 1f69a5
    guint16 nseek_points;
Packit 1f69a5
Packit 1f69a5
    GST_DEBUG_OBJECT (mp3parse, "Found VBRI header marker 0x%x", vbri_id);
Packit 1f69a5
Packit 1f69a5
    if (avail < offset_vbri + 26) {
Packit 1f69a5
      GST_DEBUG_OBJECT (mp3parse,
Packit 1f69a5
          "Not enough data to read VBRI header (need %d)", offset_vbri + 26);
Packit 1f69a5
      goto cleanup;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    GST_DEBUG_OBJECT (mp3parse, "Reading VBRI header");
Packit 1f69a5
Packit 1f69a5
    /* Move data after VBRI header */
Packit 1f69a5
    data += offset_vbri + 4;
Packit 1f69a5
Packit 1f69a5
    if (GST_READ_UINT16_BE (data) != 0x0001) {
Packit 1f69a5
      GST_WARNING_OBJECT (mp3parse,
Packit 1f69a5
          "Unsupported VBRI version 0x%x", GST_READ_UINT16_BE (data));
Packit 1f69a5
      goto cleanup;
Packit 1f69a5
    }
Packit 1f69a5
    data += 2;
Packit 1f69a5
Packit 1f69a5
    /* Skip encoder delay */
Packit 1f69a5
    data += 2;
Packit 1f69a5
Packit 1f69a5
    /* Skip quality */
Packit 1f69a5
    data += 2;
Packit 1f69a5
Packit 1f69a5
    total_bytes = GST_READ_UINT32_BE (data);
Packit 1f69a5
    if (total_bytes != 0)
Packit 1f69a5
      mp3parse->vbri_bytes = total_bytes;
Packit 1f69a5
    data += 4;
Packit 1f69a5
Packit 1f69a5
    total_frames = GST_READ_UINT32_BE (data);
Packit 1f69a5
    if (total_frames != 0) {
Packit 1f69a5
      mp3parse->vbri_frames = total_frames;
Packit 1f69a5
      mp3parse->vbri_total_time = gst_util_uint64_scale (GST_SECOND,
Packit 1f69a5
          (guint64) (mp3parse->vbri_frames) * (mp3parse->spf), mp3parse->rate);
Packit 1f69a5
    }
Packit 1f69a5
    data += 4;
Packit 1f69a5
Packit 1f69a5
    /* If we know the upstream size and duration, compute the
Packit 1f69a5
     * total bitrate, rounded up to the nearest kbit/sec */
Packit 1f69a5
    if ((total_time = mp3parse->vbri_total_time) &&
Packit 1f69a5
        (total_bytes = mp3parse->vbri_bytes)) {
Packit 1f69a5
      mp3parse->vbri_bitrate = gst_util_uint64_scale (total_bytes,
Packit 1f69a5
          8 * GST_SECOND, total_time);
Packit 1f69a5
      mp3parse->vbri_bitrate += 500;
Packit 1f69a5
      mp3parse->vbri_bitrate -= mp3parse->vbri_bitrate % 1000;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    nseek_points = GST_READ_UINT16_BE (data);
Packit 1f69a5
    data += 2;
Packit 1f69a5
Packit 1f69a5
    if (nseek_points > 0) {
Packit 1f69a5
      guint scale, seek_bytes, seek_frames;
Packit 1f69a5
      gint i;
Packit 1f69a5
Packit 1f69a5
      mp3parse->vbri_seek_points = nseek_points;
Packit 1f69a5
Packit 1f69a5
      scale = GST_READ_UINT16_BE (data);
Packit 1f69a5
      data += 2;
Packit 1f69a5
Packit 1f69a5
      seek_bytes = GST_READ_UINT16_BE (data);
Packit 1f69a5
      data += 2;
Packit 1f69a5
Packit 1f69a5
      seek_frames = GST_READ_UINT16_BE (data);
Packit 1f69a5
Packit 1f69a5
      if (scale == 0 || seek_bytes == 0 || seek_bytes > 4 || seek_frames == 0) {
Packit 1f69a5
        GST_WARNING_OBJECT (mp3parse, "Unsupported VBRI seek table");
Packit 1f69a5
        goto out_vbri;
Packit 1f69a5
      }
Packit 1f69a5
Packit 1f69a5
      if (avail < offset_vbri + 26 + nseek_points * seek_bytes) {
Packit 1f69a5
        GST_WARNING_OBJECT (mp3parse,
Packit 1f69a5
            "Not enough data to read VBRI seek table (need %d)",
Packit 1f69a5
            offset_vbri + 26 + nseek_points * seek_bytes);
Packit 1f69a5
        goto out_vbri;
Packit 1f69a5
      }
Packit 1f69a5
Packit 1f69a5
      if (seek_frames * nseek_points < total_frames - seek_frames ||
Packit 1f69a5
          seek_frames * nseek_points > total_frames + seek_frames) {
Packit 1f69a5
        GST_WARNING_OBJECT (mp3parse,
Packit 1f69a5
            "VBRI seek table doesn't cover the complete file");
Packit 1f69a5
        goto out_vbri;
Packit 1f69a5
      }
Packit 1f69a5
Packit 1f69a5
      data = map.data;
Packit 1f69a5
      data += offset_vbri + 26;
Packit 1f69a5
Packit 1f69a5
      /* VBRI seek table: frame/seek_frames -> byte */
Packit 1f69a5
      mp3parse->vbri_seek_table = g_new (guint32, nseek_points);
Packit 1f69a5
      if (seek_bytes == 4)
Packit 1f69a5
        for (i = 0; i < nseek_points; i++) {
Packit 1f69a5
          mp3parse->vbri_seek_table[i] = GST_READ_UINT32_BE (data) * scale;
Packit 1f69a5
          data += 4;
Packit 1f69a5
      } else if (seek_bytes == 3)
Packit 1f69a5
        for (i = 0; i < nseek_points; i++) {
Packit 1f69a5
          mp3parse->vbri_seek_table[i] = GST_READ_UINT24_BE (data) * scale;
Packit 1f69a5
          data += 3;
Packit 1f69a5
      } else if (seek_bytes == 2)
Packit 1f69a5
        for (i = 0; i < nseek_points; i++) {
Packit 1f69a5
          mp3parse->vbri_seek_table[i] = GST_READ_UINT16_BE (data) * scale;
Packit 1f69a5
          data += 2;
Packit 1f69a5
      } else                    /* seek_bytes == 1 */
Packit 1f69a5
        for (i = 0; i < nseek_points; i++) {
Packit 1f69a5
          mp3parse->vbri_seek_table[i] = GST_READ_UINT8 (data) * scale;
Packit 1f69a5
          data += 1;
Packit 1f69a5
        }
Packit 1f69a5
    }
Packit 1f69a5
  out_vbri:
Packit 1f69a5
Packit 1f69a5
    GST_DEBUG_OBJECT (mp3parse, "VBRI header reported %u frames, time %"
Packit 1f69a5
        GST_TIME_FORMAT ", bytes %u", mp3parse->vbri_frames,
Packit 1f69a5
        GST_TIME_ARGS (mp3parse->vbri_total_time), mp3parse->vbri_bytes);
Packit 1f69a5
Packit 1f69a5
    /* check for truncated file */
Packit 1f69a5
    if (upstream_total_bytes && mp3parse->vbri_bytes &&
Packit 1f69a5
        mp3parse->vbri_bytes * 0.8 > upstream_total_bytes) {
Packit 1f69a5
      GST_WARNING_OBJECT (mp3parse, "File appears to have been truncated; "
Packit 1f69a5
          "invalidating VBRI header duration and size");
Packit 1f69a5
      mp3parse->vbri_valid = FALSE;
Packit 1f69a5
    } else {
Packit 1f69a5
      mp3parse->vbri_valid = TRUE;
Packit 1f69a5
    }
Packit 1f69a5
  } else {
Packit 1f69a5
    GST_DEBUG_OBJECT (mp3parse,
Packit 1f69a5
        "Xing, LAME or VBRI header not found in first frame");
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  /* set duration if tables provided a valid one */
Packit 1f69a5
  if (mp3parse->xing_flags & XING_FRAMES_FLAG) {
Packit 1f69a5
    gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
Packit 1f69a5
        mp3parse->xing_total_time, 0);
Packit 1f69a5
  }
Packit 1f69a5
  if (mp3parse->vbri_total_time != 0 && mp3parse->vbri_valid) {
Packit 1f69a5
    gst_base_parse_set_duration (GST_BASE_PARSE (mp3parse), GST_FORMAT_TIME,
Packit 1f69a5
        mp3parse->vbri_total_time, 0);
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  /* tell baseclass how nicely we can seek, and a bitrate if one found */
Packit 1f69a5
  /* FIXME: fill index with seek table */
Packit 1f69a5
#if 0
Packit 1f69a5
  seekable = GST_BASE_PARSE_SEEK_DEFAULT;
Packit 1f69a5
  if ((mp3parse->xing_flags & XING_TOC_FLAG) && mp3parse->xing_bytes &&
Packit 1f69a5
      mp3parse->xing_total_time)
Packit 1f69a5
    seekable = GST_BASE_PARSE_SEEK_TABLE;
Packit 1f69a5
Packit 1f69a5
  if (mp3parse->vbri_seek_table && mp3parse->vbri_bytes &&
Packit 1f69a5
      mp3parse->vbri_total_time)
Packit 1f69a5
    seekable = GST_BASE_PARSE_SEEK_TABLE;
Packit 1f69a5
#endif
Packit 1f69a5
Packit 1f69a5
  if (mp3parse->xing_bitrate)
Packit 1f69a5
    bitrate = mp3parse->xing_bitrate;
Packit 1f69a5
  else if (mp3parse->vbri_bitrate)
Packit 1f69a5
    bitrate = mp3parse->vbri_bitrate;
Packit 1f69a5
  else
Packit 1f69a5
    bitrate = 0;
Packit 1f69a5
Packit 1f69a5
  gst_base_parse_set_average_bitrate (GST_BASE_PARSE (mp3parse), bitrate);
Packit 1f69a5
Packit 1f69a5
cleanup:
Packit 1f69a5
  gst_buffer_unmap (buf, &map);
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static gboolean
Packit 1f69a5
gst_mpeg_audio_parse_time_to_bytepos (GstMpegAudioParse * mp3parse,
Packit 1f69a5
    GstClockTime ts, gint64 * bytepos)
Packit 1f69a5
{
Packit 1f69a5
  gint64 total_bytes;
Packit 1f69a5
  GstClockTime total_time;
Packit 1f69a5
Packit 1f69a5
  /* If XING seek table exists use this for time->byte conversion */
Packit 1f69a5
  if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
Packit 1f69a5
      (total_bytes = mp3parse->xing_bytes) &&
Packit 1f69a5
      (total_time = mp3parse->xing_total_time)) {
Packit 1f69a5
    gdouble fa, fb, fx;
Packit 1f69a5
    gdouble percent =
Packit 1f69a5
        CLAMP ((100.0 * gst_util_guint64_to_gdouble (ts)) /
Packit 1f69a5
        gst_util_guint64_to_gdouble (total_time), 0.0, 100.0);
Packit 1f69a5
    gint index = CLAMP (percent, 0, 99);
Packit 1f69a5
Packit 1f69a5
    fa = mp3parse->xing_seek_table[index];
Packit 1f69a5
    if (index < 99)
Packit 1f69a5
      fb = mp3parse->xing_seek_table[index + 1];
Packit 1f69a5
    else
Packit 1f69a5
      fb = 256.0;
Packit 1f69a5
Packit 1f69a5
    fx = fa + (fb - fa) * (percent - index);
Packit 1f69a5
Packit 1f69a5
    *bytepos = (1.0 / 256.0) * fx * total_bytes;
Packit 1f69a5
Packit 1f69a5
    return TRUE;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  if (mp3parse->vbri_seek_table && (total_bytes = mp3parse->vbri_bytes) &&
Packit 1f69a5
      (total_time = mp3parse->vbri_total_time)) {
Packit 1f69a5
    gint i, j;
Packit 1f69a5
    gdouble a, b, fa, fb;
Packit 1f69a5
Packit 1f69a5
    i = gst_util_uint64_scale (ts, mp3parse->vbri_seek_points - 1, total_time);
Packit 1f69a5
    i = CLAMP (i, 0, mp3parse->vbri_seek_points - 1);
Packit 1f69a5
Packit 1f69a5
    a = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
Packit 1f69a5
            mp3parse->vbri_seek_points));
Packit 1f69a5
    fa = 0.0;
Packit 1f69a5
    for (j = i; j >= 0; j--)
Packit 1f69a5
      fa += mp3parse->vbri_seek_table[j];
Packit 1f69a5
Packit 1f69a5
    if (i + 1 < mp3parse->vbri_seek_points) {
Packit 1f69a5
      b = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
Packit 1f69a5
              mp3parse->vbri_seek_points));
Packit 1f69a5
      fb = fa + mp3parse->vbri_seek_table[i + 1];
Packit 1f69a5
    } else {
Packit 1f69a5
      b = gst_guint64_to_gdouble (total_time);
Packit 1f69a5
      fb = total_bytes;
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    *bytepos = fa + ((fb - fa) / (b - a)) * (gst_guint64_to_gdouble (ts) - a);
Packit 1f69a5
Packit 1f69a5
    return TRUE;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  return FALSE;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static gboolean
Packit 1f69a5
gst_mpeg_audio_parse_bytepos_to_time (GstMpegAudioParse * mp3parse,
Packit 1f69a5
    gint64 bytepos, GstClockTime * ts)
Packit 1f69a5
{
Packit 1f69a5
  gint64 total_bytes;
Packit 1f69a5
  GstClockTime total_time;
Packit 1f69a5
Packit 1f69a5
  /* If XING seek table exists use this for byte->time conversion */
Packit 1f69a5
  if ((mp3parse->xing_flags & XING_TOC_FLAG) &&
Packit 1f69a5
      (total_bytes = mp3parse->xing_bytes) &&
Packit 1f69a5
      (total_time = mp3parse->xing_total_time)) {
Packit 1f69a5
    gdouble fa, fb, fx;
Packit 1f69a5
    gdouble pos;
Packit 1f69a5
    gint index;
Packit 1f69a5
Packit 1f69a5
    pos = CLAMP ((bytepos * 256.0) / total_bytes, 0.0, 256.0);
Packit 1f69a5
    index = CLAMP (pos, 0, 255);
Packit 1f69a5
    fa = mp3parse->xing_seek_table_inverse[index];
Packit 1f69a5
    if (index < 255)
Packit 1f69a5
      fb = mp3parse->xing_seek_table_inverse[index + 1];
Packit 1f69a5
    else
Packit 1f69a5
      fb = 10000.0;
Packit 1f69a5
Packit 1f69a5
    fx = fa + (fb - fa) * (pos - index);
Packit 1f69a5
Packit 1f69a5
    *ts = (1.0 / 10000.0) * fx * gst_util_guint64_to_gdouble (total_time);
Packit 1f69a5
Packit 1f69a5
    return TRUE;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  if (mp3parse->vbri_seek_table &&
Packit 1f69a5
      (total_bytes = mp3parse->vbri_bytes) &&
Packit 1f69a5
      (total_time = mp3parse->vbri_total_time)) {
Packit 1f69a5
    gint i = 0;
Packit 1f69a5
    guint64 sum = 0;
Packit 1f69a5
    gdouble a, b, fa, fb;
Packit 1f69a5
Packit 1f69a5
    do {
Packit 1f69a5
      sum += mp3parse->vbri_seek_table[i];
Packit 1f69a5
      i++;
Packit 1f69a5
    } while (i + 1 < mp3parse->vbri_seek_points
Packit 1f69a5
        && sum + mp3parse->vbri_seek_table[i] < bytepos);
Packit 1f69a5
    i--;
Packit 1f69a5
Packit 1f69a5
    a = gst_guint64_to_gdouble (sum);
Packit 1f69a5
    fa = gst_guint64_to_gdouble (gst_util_uint64_scale (i, total_time,
Packit 1f69a5
            mp3parse->vbri_seek_points));
Packit 1f69a5
Packit 1f69a5
    if (i + 1 < mp3parse->vbri_seek_points) {
Packit 1f69a5
      b = a + mp3parse->vbri_seek_table[i + 1];
Packit 1f69a5
      fb = gst_guint64_to_gdouble (gst_util_uint64_scale (i + 1, total_time,
Packit 1f69a5
              mp3parse->vbri_seek_points));
Packit 1f69a5
    } else {
Packit 1f69a5
      b = total_bytes;
Packit 1f69a5
      fb = gst_guint64_to_gdouble (total_time);
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    *ts = gst_gdouble_to_guint64 (fa + ((fb - fa) / (b - a)) * (bytepos - a));
Packit 1f69a5
Packit 1f69a5
    return TRUE;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  return FALSE;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static gboolean
Packit 1f69a5
gst_mpeg_audio_parse_convert (GstBaseParse * parse, GstFormat src_format,
Packit 1f69a5
    gint64 src_value, GstFormat dest_format, gint64 * dest_value)
Packit 1f69a5
{
Packit 1f69a5
  GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
Packit 1f69a5
  gboolean res = FALSE;
Packit 1f69a5
Packit 1f69a5
  if (src_format == GST_FORMAT_TIME && dest_format == GST_FORMAT_BYTES)
Packit 1f69a5
    res =
Packit 1f69a5
        gst_mpeg_audio_parse_time_to_bytepos (mp3parse, src_value, dest_value);
Packit 1f69a5
  else if (src_format == GST_FORMAT_BYTES && dest_format == GST_FORMAT_TIME)
Packit 1f69a5
    res = gst_mpeg_audio_parse_bytepos_to_time (mp3parse, src_value,
Packit 1f69a5
        (GstClockTime *) dest_value);
Packit 1f69a5
Packit 1f69a5
  /* if no tables, fall back to default estimated rate based conversion */
Packit 1f69a5
  if (!res)
Packit 1f69a5
    return gst_base_parse_convert_default (parse, src_format, src_value,
Packit 1f69a5
        dest_format, dest_value);
Packit 1f69a5
Packit 1f69a5
  return res;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static GstFlowReturn
Packit 1f69a5
gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
Packit 1f69a5
    GstBaseParseFrame * frame)
Packit 1f69a5
{
Packit 1f69a5
  GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
Packit 1f69a5
  GstTagList *taglist = NULL;
Packit 1f69a5
Packit 1f69a5
  /* we will create a taglist (if any of the parameters has changed)
Packit 1f69a5
   * to add the tags that changed */
Packit 1f69a5
  if (mp3parse->last_posted_crc != mp3parse->last_crc) {
Packit 1f69a5
    gboolean using_crc;
Packit 1f69a5
Packit 1f69a5
    if (!taglist)
Packit 1f69a5
      taglist = gst_tag_list_new_empty ();
Packit 1f69a5
Packit 1f69a5
    mp3parse->last_posted_crc = mp3parse->last_crc;
Packit 1f69a5
    if (mp3parse->last_posted_crc == CRC_PROTECTED) {
Packit 1f69a5
      using_crc = TRUE;
Packit 1f69a5
    } else {
Packit 1f69a5
      using_crc = FALSE;
Packit 1f69a5
    }
Packit 1f69a5
    gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CRC,
Packit 1f69a5
        using_crc, NULL);
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  if (mp3parse->last_posted_channel_mode != mp3parse->last_mode) {
Packit 1f69a5
    if (!taglist)
Packit 1f69a5
      taglist = gst_tag_list_new_empty ();
Packit 1f69a5
Packit 1f69a5
    mp3parse->last_posted_channel_mode = mp3parse->last_mode;
Packit 1f69a5
Packit 1f69a5
    gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_MODE,
Packit 1f69a5
        gst_mpeg_audio_channel_mode_get_nick (mp3parse->last_mode), NULL);
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  /* tag sending done late enough in hook to ensure pending events
Packit 1f69a5
   * have already been sent */
Packit 1f69a5
  if (taglist != NULL || !mp3parse->sent_codec_tag) {
Packit 1f69a5
    GstCaps *caps;
Packit 1f69a5
Packit 1f69a5
    if (taglist == NULL)
Packit 1f69a5
      taglist = gst_tag_list_new_empty ();
Packit 1f69a5
Packit 1f69a5
    /* codec tag */
Packit 1f69a5
    caps = gst_pad_get_current_caps (GST_BASE_PARSE_SRC_PAD (parse));
Packit 1f69a5
    if (G_UNLIKELY (caps == NULL)) {
Packit 1f69a5
      gst_tag_list_unref (taglist);
Packit 1f69a5
Packit 1f69a5
      if (GST_PAD_IS_FLUSHING (GST_BASE_PARSE_SRC_PAD (parse))) {
Packit 1f69a5
        GST_INFO_OBJECT (parse, "Src pad is flushing");
Packit 1f69a5
        return GST_FLOW_FLUSHING;
Packit 1f69a5
      } else {
Packit 1f69a5
        GST_INFO_OBJECT (parse, "Src pad is not negotiated!");
Packit 1f69a5
        return GST_FLOW_NOT_NEGOTIATED;
Packit 1f69a5
      }
Packit 1f69a5
    }
Packit 1f69a5
    gst_pb_utils_add_codec_description_to_tag_list (taglist,
Packit 1f69a5
        GST_TAG_AUDIO_CODEC, caps);
Packit 1f69a5
    gst_caps_unref (caps);
Packit 1f69a5
Packit 1f69a5
    if (mp3parse->hdr_bitrate > 0 && mp3parse->xing_bitrate == 0 &&
Packit 1f69a5
        mp3parse->vbri_bitrate == 0) {
Packit 1f69a5
      /* We don't have a VBR bitrate, so post the available bitrate as
Packit 1f69a5
       * nominal and let baseparse calculate the real bitrate */
Packit 1f69a5
      gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE,
Packit 1f69a5
          GST_TAG_NOMINAL_BITRATE, mp3parse->hdr_bitrate, NULL);
Packit 1f69a5
    }
Packit 1f69a5
Packit 1f69a5
    /* also signals the end of first-frame processing */
Packit 1f69a5
    mp3parse->sent_codec_tag = TRUE;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  /* if the taglist exists, we need to update it so it gets sent out */
Packit 1f69a5
  if (taglist) {
Packit 1f69a5
    gst_base_parse_merge_tags (parse, taglist, GST_TAG_MERGE_REPLACE);
Packit 1f69a5
    gst_tag_list_unref (taglist);
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  /* usual clipping applies */
Packit 1f69a5
  frame->flags |= GST_BASE_PARSE_FRAME_FLAG_CLIP;
Packit 1f69a5
Packit 1f69a5
  return GST_FLOW_OK;
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static void
Packit 1f69a5
remove_fields (GstCaps * caps)
Packit 1f69a5
{
Packit 1f69a5
  guint i, n;
Packit 1f69a5
Packit 1f69a5
  n = gst_caps_get_size (caps);
Packit 1f69a5
  for (i = 0; i < n; i++) {
Packit 1f69a5
    GstStructure *s = gst_caps_get_structure (caps, i);
Packit 1f69a5
Packit 1f69a5
    gst_structure_remove_field (s, "parsed");
Packit 1f69a5
  }
Packit 1f69a5
}
Packit 1f69a5
Packit 1f69a5
static GstCaps *
Packit 1f69a5
gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
Packit 1f69a5
{
Packit 1f69a5
  GstCaps *peercaps, *templ;
Packit 1f69a5
  GstCaps *res;
Packit 1f69a5
Packit 1f69a5
  templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
Packit 1f69a5
  if (filter) {
Packit 1f69a5
    GstCaps *fcopy = gst_caps_copy (filter);
Packit 1f69a5
    /* Remove the fields we convert */
Packit 1f69a5
    remove_fields (fcopy);
Packit 1f69a5
    peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), fcopy);
Packit 1f69a5
    gst_caps_unref (fcopy);
Packit 1f69a5
  } else
Packit 1f69a5
    peercaps = gst_pad_peer_query_caps (GST_BASE_PARSE_SRC_PAD (parse), NULL);
Packit 1f69a5
Packit 1f69a5
  if (peercaps) {
Packit 1f69a5
    /* Remove the parsed field */
Packit 1f69a5
    peercaps = gst_caps_make_writable (peercaps);
Packit 1f69a5
    remove_fields (peercaps);
Packit 1f69a5
Packit 1f69a5
    res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
Packit 1f69a5
    gst_caps_unref (peercaps);
Packit 1f69a5
    gst_caps_unref (templ);
Packit 1f69a5
  } else {
Packit 1f69a5
    res = templ;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  if (filter) {
Packit 1f69a5
    GstCaps *intersection;
Packit 1f69a5
Packit 1f69a5
    intersection =
Packit 1f69a5
        gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
Packit 1f69a5
    gst_caps_unref (res);
Packit 1f69a5
    res = intersection;
Packit 1f69a5
  }
Packit 1f69a5
Packit 1f69a5
  return res;
Packit 1f69a5
}