docs: gst-launch -> gst-launch-1.0 and ffmpegcolorspace -> videoconvert
[platform/upstream/gstreamer.git] / gst / audioparsers / gstmpegaudioparse.c
index d3dca66..62cc5b4 100644 (file)
@@ -29,7 +29,7 @@
  * <refsect2>
  * <title>Example launch line</title>
  * |[
- * gst-launch filesrc location=test.mp3 ! mpegaudioparse ! mad ! autoaudiosink
+ * gst-launch-1.0 filesrc location=test.mp3 ! mpegaudioparse ! mad ! autoaudiosink
  * ]|
  * </refsect2>
  */
@@ -91,10 +91,8 @@ static void gst_mpeg_audio_parse_finalize (GObject * object);
 
 static gboolean gst_mpeg_audio_parse_start (GstBaseParse * parse);
 static gboolean gst_mpeg_audio_parse_stop (GstBaseParse * parse);
-static gboolean gst_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
-    GstBaseParseFrame * frame, guint * size, gint * skipsize);
-static GstFlowReturn gst_mpeg_audio_parse_parse_frame (GstBaseParse * parse,
-    GstBaseParseFrame * frame);
+static GstFlowReturn gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
+    GstBaseParseFrame * frame, gint * skipsize);
 static GstFlowReturn gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
     GstBaseParseFrame * frame);
 static gboolean gst_mpeg_audio_parse_convert (GstBaseParse * parse,
@@ -103,6 +101,9 @@ static gboolean gst_mpeg_audio_parse_convert (GstBaseParse * parse,
 static GstCaps *gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse,
     GstCaps * filter);
 
+static void gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse *
+    mp3parse, GstBuffer * buf);
+
 #define gst_mpeg_audio_parse_parent_class parent_class
 G_DEFINE_TYPE (GstMpegAudioParse, gst_mpeg_audio_parse, GST_TYPE_BASE_PARSE);
 
@@ -156,10 +157,8 @@ gst_mpeg_audio_parse_class_init (GstMpegAudioParseClass * klass)
 
   parse_class->start = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_start);
   parse_class->stop = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_stop);
-  parse_class->check_valid_frame =
-      GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_check_valid_frame);
-  parse_class->parse_frame =
-      GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_parse_frame);
+  parse_class->handle_frame =
+      GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_handle_frame);
   parse_class->pre_push_frame =
       GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_pre_push_frame);
   parse_class->convert = GST_DEBUG_FUNCPTR (gst_mpeg_audio_parse_convert);
@@ -182,7 +181,7 @@ gst_mpeg_audio_parse_class_init (GstMpegAudioParseClass * klass)
   gst_element_class_add_pad_template (element_class,
       gst_static_pad_template_get (&src_template));
 
-  gst_element_class_set_details_simple (element_class, "MPEG1 Audio Parser",
+  gst_element_class_set_static_metadata (element_class, "MPEG1 Audio Parser",
       "Codec/Parser/Audio",
       "Parses and frames mpeg1 audio streams (levels 1-3), provides seek",
       "Jan Schmidt <thaytan@mad.scientist.com>,"
@@ -197,6 +196,7 @@ gst_mpeg_audio_parse_reset (GstMpegAudioParse * mp3parse)
   mp3parse->sent_codec_tag = FALSE;
   mp3parse->last_posted_crc = CRC_UNKNOWN;
   mp3parse->last_posted_channel_mode = MPEG_AUDIO_CHANNEL_MODE_UNKNOWN;
+  mp3parse->freerate = 0;
 
   mp3parse->hdr_bitrate = 0;
 
@@ -303,14 +303,16 @@ mp3_type_frame_length_from_header (GstMpegAudioParse * mp3parse, guint32 header,
 
   bitrate = (header >> 12) & 0xF;
   bitrate = mp3types_bitrates[lsf][layer - 1][bitrate] * 1000;
-  /* The caller has ensured we have a valid header, so bitrate can't be
-     zero here. */
-  g_assert (bitrate != 0);
+  if (!bitrate) {
+    GST_LOG_OBJECT (mp3parse, "using freeform bitrate");
+    bitrate = mp3parse->freerate;
+  }
 
   samplerate = (header >> 10) & 0x3;
   samplerate = mp3types_freqs[lsf + mpg25][samplerate];
 
-  padding = (header >> 9) & 0x1;
+  /* force 0 length if 0 bitrate */
+  padding = (bitrate > 0) ? (header >> 9) & 0x1 : 0;
 
   mode = (header >> 6) & 0x3;
   channels = (mode == 3) ? 1 : 2;
@@ -415,8 +417,7 @@ gst_mp3parse_validate_extended (GstMpegAudioParse * mp3parse, GstBuffer * buf,
           (guint) next_header & HDRMASK, bpf);
       *valid = FALSE;
       goto cleanup;
-    } else if ((((next_header >> 12) & 0xf) == 0) ||
-        (((next_header >> 12) & 0xf) == 0xf)) {
+    } else if (((next_header >> 12) & 0xf) == 0xf) {
       /* The essential parts were the same, but the bitrate held an
          invalid value - also reject */
       GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
@@ -427,6 +428,13 @@ gst_mp3parse_validate_extended (GstMpegAudioParse * mp3parse, GstBuffer * buf,
     bpf = mp3_type_frame_length_from_header (mp3parse, next_header,
         NULL, NULL, NULL, NULL, NULL, NULL, NULL);
 
+    /* if no bitrate, and no freeform rate known, then fail */
+    if (G_UNLIKELY (!bpf)) {
+      GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate 0)");
+      *valid = FALSE;
+      return TRUE;
+    }
+
     offset += bpf;
     frames_found++;
   }
@@ -460,11 +468,6 @@ gst_mpeg_audio_parse_head_check (GstMpegAudioParse * mp3parse,
     return FALSE;
   }
   /* if it's an invalid bitrate */
-  if (((head >> 12) & 0xf) == 0x0) {
-    GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx."
-        "Free format files are not supported yet", (head >> 12) & 0xf);
-    return FALSE;
-  }
   if (((head >> 12) & 0xf) == 0xf) {
     GST_WARNING_OBJECT (mp3parse, "invalid bitrate: 0x%lx", (head >> 12) & 0xf);
     return FALSE;
@@ -485,9 +488,118 @@ gst_mpeg_audio_parse_head_check (GstMpegAudioParse * mp3parse,
   return TRUE;
 }
 
+/* Determines possible freeform frame rate/size by looking for next
+ * header with valid bitrate (0 or otherwise valid) (and sufficiently
+ * matching current header).
+ *
+ * Returns TRUE if we've found such one, and *rate then contains rate
+ * (or *rate contains 0 if decided no freeframe size could be determined).
+ * If not enough data, returns FALSE.
+ */
 static gboolean
-gst_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
-    GstBaseParseFrame * frame, guint * framesize, gint * skipsize)
+gst_mp3parse_find_freerate (GstMpegAudioParse * mp3parse, GstMapInfo * map,
+    guint32 header, gboolean at_eos, gint * _rate)
+{
+  guint32 next_header;
+  const guint8 *data;
+  guint available;
+  int offset = 4;
+  gulong samplerate, rate, layer, padding;
+  gboolean valid;
+  gint lsf, mpg25;
+
+  available = map->size;
+  data = map->data;
+
+  *_rate = 0;
+
+  /* pick apart header again partially */
+  if (header & (1 << 20)) {
+    lsf = (header & (1 << 19)) ? 0 : 1;
+    mpg25 = 0;
+  } else {
+    lsf = 1;
+    mpg25 = 1;
+  }
+  layer = 4 - ((header >> 17) & 0x3);
+  samplerate = (header >> 10) & 0x3;
+  samplerate = mp3types_freqs[lsf + mpg25][samplerate];
+  padding = (header >> 9) & 0x1;
+
+  for (; offset < available; ++offset) {
+    /* Check if we have enough data for all these frames, plus the next
+       frame header. */
+    if (available < offset + 4) {
+      if (at_eos) {
+        /* Running out of data; failed to determine size */
+        return TRUE;
+      } else {
+        return FALSE;
+      }
+    }
+
+    valid = FALSE;
+    next_header = GST_READ_UINT32_BE (data + offset);
+    if ((next_header & 0xFFE00000) != 0xFFE00000)
+      goto next;
+
+    GST_DEBUG_OBJECT (mp3parse, "At %d: header=%08X, header2=%08X",
+        offset, (unsigned int) header, (unsigned int) next_header);
+
+    if ((next_header & HDRMASK) != (header & HDRMASK)) {
+      /* If any of the unmasked bits don't match, then it's not valid */
+      GST_DEBUG_OBJECT (mp3parse, "next header doesn't match "
+          "(header=%08X (%08X), header2=%08X (%08X))",
+          (guint) header, (guint) header & HDRMASK, (guint) next_header,
+          (guint) next_header & HDRMASK);
+      goto next;
+    } else if (((next_header >> 12) & 0xf) == 0xf) {
+      /* The essential parts were the same, but the bitrate held an
+         invalid value - also reject */
+      GST_DEBUG_OBJECT (mp3parse, "next header invalid (bitrate)");
+      goto next;
+    }
+
+    valid = TRUE;
+
+  next:
+    /* almost accept as free frame */
+    if (layer == 1) {
+      rate = samplerate * (offset - 4 * padding + 4) / 48000;
+    } else {
+      rate = samplerate * (offset - padding + 1) / (144 >> lsf) / 1000;
+    }
+
+    if (valid) {
+      GST_LOG_OBJECT (mp3parse, "calculated rate %lu", rate * 1000);
+      if (rate < 8 || (layer == 3 && rate > 640)) {
+        GST_DEBUG_OBJECT (mp3parse, "rate invalid");
+        if (rate < 8) {
+          /* maybe some hope */
+          continue;
+        } else {
+          GST_DEBUG_OBJECT (mp3parse, "aborting");
+          /* give up */
+          break;
+        }
+      }
+      *_rate = rate * 1000;
+      break;
+    } else {
+      /* avoid indefinite searching */
+      if (rate > 1000) {
+        GST_DEBUG_OBJECT (mp3parse, "exceeded sanity rate; aborting");
+        break;
+      }
+    }
+  }
+
+  return TRUE;
+}
+
+static GstFlowReturn
+gst_mpeg_audio_parse_handle_frame (GstBaseParse * parse,
+    GstBaseParseFrame * frame, gint * skipsize)
 {
   GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
   GstBuffer *buf = frame->buffer;
@@ -500,8 +612,10 @@ gst_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
   gboolean res = FALSE;
 
   gst_buffer_map (buf, &map, GST_MAP_READ);
-  if (G_UNLIKELY (map.size < 6))
+  if (G_UNLIKELY (map.size < 6)) {
+    *skipsize = 1;
     goto cleanup;
+  }
 
   gst_byte_reader_init (&reader, map.data, map.size);
 
@@ -531,9 +645,14 @@ gst_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
 
   GST_LOG_OBJECT (parse, "got frame");
 
+  lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
+  draining = GST_BASE_PARSE_DRAINING (parse);
+
+  if (G_UNLIKELY (lost_sync))
+    mp3parse->freerate = 0;
+
   bpf = mp3_type_frame_length_from_header (mp3parse, header,
       &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
-  g_assert (bpf != 0);
 
   if (channels != mp3parse->channels || rate != mp3parse->rate ||
       layer != mp3parse->layer || version != mp3parse->version)
@@ -541,8 +660,31 @@ gst_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
   else
     caps_change = FALSE;
 
-  lost_sync = GST_BASE_PARSE_LOST_SYNC (parse);
-  draining = GST_BASE_PARSE_DRAINING (parse);
+  /* maybe free format */
+  if (bpf == 0) {
+    GST_LOG_OBJECT (mp3parse, "possibly free format");
+    if (lost_sync || mp3parse->freerate == 0) {
+      GST_DEBUG_OBJECT (mp3parse, "finding free format rate");
+      if (!gst_mp3parse_find_freerate (mp3parse, &map, header, draining,
+              &valid)) {
+        /* not enough data */
+        gst_base_parse_set_min_frame_size (parse, valid);
+        *skipsize = 0;
+        return FALSE;
+      } else {
+        GST_DEBUG_OBJECT (parse, "determined freeform size %d", valid);
+        mp3parse->freerate = valid;
+      }
+    }
+    /* try again */
+    bpf = mp3_type_frame_length_from_header (mp3parse, header,
+        &version, &layer, &channels, &bitrate, &rate, &mode, &crc);
+    if (!bpf) {
+      /* did not come up with valid freeform length, reject after all */
+      *skipsize = 1;
+      return FALSE;
+    }
+  }
 
   if (!draining && (lost_sync || caps_change)) {
     if (!gst_mp3parse_validate_extended (mp3parse, buf, header, bpf, draining,
@@ -566,12 +708,67 @@ gst_mpeg_audio_parse_check_valid_frame (GstBaseParse * parse,
   /* restore default minimum */
   gst_base_parse_set_min_frame_size (parse, MIN_FRAME_SIZE);
 
-  *framesize = bpf;
   res = TRUE;
 
+  /* metadata handling */
+  if (G_UNLIKELY (caps_change)) {
+    GstCaps *caps = gst_caps_new_simple ("audio/mpeg",
+        "mpegversion", G_TYPE_INT, 1,
+        "mpegaudioversion", G_TYPE_INT, version,
+        "layer", G_TYPE_INT, layer,
+        "rate", G_TYPE_INT, rate,
+        "channels", G_TYPE_INT, channels, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
+    gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
+    gst_caps_unref (caps);
+
+    mp3parse->rate = rate;
+    mp3parse->channels = channels;
+    mp3parse->layer = layer;
+    mp3parse->version = version;
+
+    /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
+    if (mp3parse->layer == 1)
+      mp3parse->spf = 384;
+    else if (mp3parse->layer == 2)
+      mp3parse->spf = 1152;
+    else if (mp3parse->version == 1) {
+      mp3parse->spf = 1152;
+    } else {
+      /* MPEG-2 or "2.5" */
+      mp3parse->spf = 576;
+    }
+
+    /* lead_in:
+     * We start pushing 9 frames earlier (29 frames for MPEG2) than
+     * segment start to be able to decode the first frame we want.
+     * 9 (29) frames are the theoretical maximum of frames that contain
+     * data for the current frame (bit reservoir).
+     *
+     * lead_out:
+     * Some mp3 streams have an offset in the timestamps, for which we have to
+     * push the frame *after* the end position in order for the decoder to be
+     * able to decode everything up until the segment.stop position. */
+    gst_base_parse_set_frame_rate (parse, mp3parse->rate, mp3parse->spf,
+        (version == 1) ? 10 : 30, 2);
+  }
+
+  mp3parse->hdr_bitrate = bitrate;
+
+  /* For first frame; check for seek tables and output a codec tag */
+  gst_mpeg_audio_parse_handle_first_frame (mp3parse, buf);
+
+  /* store some frame info for later processing */
+  mp3parse->last_crc = crc;
+  mp3parse->last_mode = mode;
+
 cleanup:
   gst_buffer_unmap (buf, &map);
-  return res;
+
+  if (res && bpf <= map.size) {
+    return gst_base_parse_finish_frame (parse, frame, bpf);
+  }
+
+  return GST_FLOW_OK;
 }
 
 static void
@@ -799,9 +996,7 @@ gst_mpeg_audio_parse_handle_first_frame (GstMpegAudioParse * mp3parse,
       GST_DEBUG_OBJECT (mp3parse, "Encoder delay %u, encoder padding %u",
           encoder_delay, encoder_padding);
     }
-  }
-
-  if (read_id_vbri == vbri_id) {
+  } else if (read_id_vbri == vbri_id) {
     gint64 total_bytes, total_frames;
     GstClockTime total_time;
     guint16 nseek_points;
@@ -977,94 +1172,6 @@ cleanup:
   gst_buffer_unmap (buf, &map);
 }
 
-static GstFlowReturn
-gst_mpeg_audio_parse_parse_frame (GstBaseParse * parse,
-    GstBaseParseFrame * frame)
-{
-  GstMpegAudioParse *mp3parse = GST_MPEG_AUDIO_PARSE (parse);
-  GstBuffer *buf = frame->buffer;
-  GstMapInfo map;
-  guint bitrate, layer, rate, channels, version, mode, crc;
-
-  gst_buffer_map (buf, &map, GST_MAP_READ);
-  if (G_UNLIKELY (map.size < 4))
-    goto short_buffer;
-
-  if (!mp3_type_frame_length_from_header (mp3parse,
-          GST_READ_UINT32_BE (map.data),
-          &version, &layer, &channels, &bitrate, &rate, &mode, &crc))
-    goto broken_header;
-
-  if (G_UNLIKELY (channels != mp3parse->channels || rate != mp3parse->rate ||
-          layer != mp3parse->layer || version != mp3parse->version)) {
-    GstCaps *caps = gst_caps_new_simple ("audio/mpeg",
-        "mpegversion", G_TYPE_INT, 1,
-        "mpegaudioversion", G_TYPE_INT, version,
-        "layer", G_TYPE_INT, layer,
-        "rate", G_TYPE_INT, rate,
-        "channels", G_TYPE_INT, channels, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
-    gst_pad_set_caps (GST_BASE_PARSE_SRC_PAD (parse), caps);
-    gst_caps_unref (caps);
-
-    mp3parse->rate = rate;
-    mp3parse->channels = channels;
-    mp3parse->layer = layer;
-    mp3parse->version = version;
-
-    /* see http://www.codeproject.com/audio/MPEGAudioInfo.asp */
-    if (mp3parse->layer == 1)
-      mp3parse->spf = 384;
-    else if (mp3parse->layer == 2)
-      mp3parse->spf = 1152;
-    else if (mp3parse->version == 1) {
-      mp3parse->spf = 1152;
-    } else {
-      /* MPEG-2 or "2.5" */
-      mp3parse->spf = 576;
-    }
-
-    /* lead_in:
-     * We start pushing 9 frames earlier (29 frames for MPEG2) than
-     * segment start to be able to decode the first frame we want.
-     * 9 (29) frames are the theoretical maximum of frames that contain
-     * data for the current frame (bit reservoir).
-     *
-     * lead_out:
-     * Some mp3 streams have an offset in the timestamps, for which we have to
-     * push the frame *after* the end position in order for the decoder to be
-     * able to decode everything up until the segment.stop position. */
-    gst_base_parse_set_frame_rate (parse, mp3parse->rate, mp3parse->spf,
-        (version == 1) ? 10 : 30, 2);
-  }
-
-  mp3parse->hdr_bitrate = bitrate;
-
-  /* For first frame; check for seek tables and output a codec tag */
-  gst_mpeg_audio_parse_handle_first_frame (mp3parse, buf);
-
-  /* store some frame info for later processing */
-  mp3parse->last_crc = crc;
-  mp3parse->last_mode = mode;
-
-  gst_buffer_unmap (buf, &map);
-  return GST_FLOW_OK;
-
-/* ERRORS */
-broken_header:
-  {
-    /* this really shouldn't ever happen */
-    gst_buffer_unmap (buf, &map);
-    GST_ELEMENT_ERROR (parse, STREAM, DECODE, (NULL), (NULL));
-    return GST_FLOW_ERROR;
-  }
-
-short_buffer:
-  {
-    gst_buffer_unmap (buf, &map);
-    return GST_FLOW_ERROR;
-  }
-}
-
 static gboolean
 gst_mpeg_audio_parse_time_to_bytepos (GstMpegAudioParse * mp3parse,
     GstClockTime ts, gint64 * bytepos)
@@ -1294,12 +1401,12 @@ gst_mpeg_audio_parse_pre_push_frame (GstBaseParse * parse,
 static GstCaps *
 gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
 {
-  GstCaps *peercaps;
+  GstCaps *peercaps, *templ;
   GstCaps *res;
 
-  /* FIXME: handle filter caps */
-
+  templ = gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD (parse));
   peercaps = gst_pad_get_allowed_caps (GST_BASE_PARSE_SRC_PAD (parse));
+
   if (peercaps) {
     guint i, n;
 
@@ -1312,15 +1419,25 @@ gst_mpeg_audio_parse_get_sink_caps (GstBaseParse * parse, GstCaps * filter)
       gst_structure_remove_field (s, "parsed");
     }
 
-    res =
-        gst_caps_intersect_full (peercaps,
-        gst_pad_get_pad_template_caps (GST_BASE_PARSE_SRC_PAD (parse)),
-        GST_CAPS_INTERSECT_FIRST);
+    res = gst_caps_intersect_full (peercaps, templ, GST_CAPS_INTERSECT_FIRST);
     gst_caps_unref (peercaps);
+
+    /* Append the template caps because we still want to accept
+     * caps without any fields in the case upstream does not
+     * know anything.
+     */
+    gst_caps_append (res, templ);
   } else {
-    res =
-        gst_caps_copy (gst_pad_get_pad_template_caps (GST_BASE_PARSE_SINK_PAD
-            (parse)));
+    res = templ;
+  }
+
+  if (filter) {
+    GstCaps *intersection;
+
+    intersection =
+        gst_caps_intersect_full (filter, res, GST_CAPS_INTERSECT_FIRST);
+    gst_caps_unref (res);
+    res = intersection;
   }
 
   return res;