<commit_before>/* Swfdec
 * Copyright (C) 2007 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include "swfdec_audio_flv.h"
#include "swfdec_debug.h"
#include "swfdec_sound.h"


G_DEFINE_TYPE (SwfdecAudioFlv, swfdec_audio_flv, SWFDEC_TYPE_AUDIO)

static void
swfdec_audio_flv_dispose (GObject *object)
{
  SwfdecAudioFlv *flv = SWFDEC_AUDIO_FLV (object);

  if (flv->decoder != NULL) {
    g_object_unref (flv->decoder);
    flv->decoder = NULL;
  }
  g_queue_foreach (flv->playback_queue, (GFunc) swfdec_buffer_unref, NULL);
  g_queue_free (flv->playback_queue);
  g_object_unref (flv->flvdecoder);

  G_OBJECT_CLASS (swfdec_audio_flv_parent_class)->dispose (object);
}

static SwfdecBuffer *
swfdec_audio_flv_decode_one (SwfdecAudioFlv *flv)
{
  SwfdecBuffer *buffer;
  guint format;
  SwfdecAudioFormat in;
  guint now, soon;

  if  (g_queue_is_empty (flv->playback_queue)) {
    /* sync */
    guint last;
    swfdec_flv_decoder_get_audio (flv->flvdecoder, 
	SWFDEC_TICKS_TO_MSECS (flv->timestamp),
	NULL, NULL, &last, NULL);
    flv->playback_skip = SWFDEC_TICKS_TO_SAMPLES (
	flv->timestamp - SWFDEC_MSECS_TO_TICKS (last));
    flv->next_timestamp = last;
    SWFDEC_DEBUG ("syncing to %ums: next timestamp to decode is %ums, skipping %u samples", 
	(guint) SWFDEC_TICKS_TO_MSECS (flv->timestamp), 
	flv->next_timestamp, flv->playback_skip);
  }
  if (flv->decoder)
    buffer = swfdec_audio_decoder_pull (flv->decoder);
  else
    buffer = NULL;
  while (buffer == NULL) {
    if (flv->decoder && flv->next_timestamp == 0)
      return NULL;
    buffer = swfdec_flv_decoder_get_audio (flv->flvdecoder, flv->next_timestamp,
      &format, &in, &now, &soon);

    if (flv->next_timestamp != now) {
      /* FIXME: do sync on first frame here */
      SWFDEC_WARNING ("FIXME: didn't get requested timestamp - still loading?");
    }
    /* FIXME FIXME FIXME: This avoids decoding the last frame forever, however it ensures sync */
    if (soon == 0)
      return NULL;
    flv->next_timestamp = soon;
    if (flv->in == 0) {
      /* init */
      if (flv->decoder) {
	g_object_unref (flv->decoder);
	flv->decoder = NULL;
      }
      flv->format = format;
      flv->in = in;
      flv->decoder = swfdec_audio_decoder_new (flv->format, flv->in);
      if (flv->decoder == NULL)
	return NULL;
    } else if (format != flv->format ||
	in != flv->in) {
      SWFDEC_ERROR ("FIXME: format change not implemented");
      return NULL;
    } else if (flv->decoder == NULL) {
      return NULL;
    }
    if (format == SWFDEC_AUDIO_CODEC_AAC) {
      SwfdecBuffer *data;
      SwfdecBits bits;
      guint type;
      swfdec_bits_init (&bits, buffer);
      type = swfdec_bits_get_u8 (&bits);
      switch (type) {
	case 0:
	  data = swfdec_bits_get_buffer (&bits, -1);
	  if (data) {
	    swfdec_audio_decoder_set_codec_data (flv->decoder, data);
	    swfdec_buffer_unref (data);
	  }
	  break;
	case 1:
	  data = swfdec_bits_get_buffer (&bits, -1);
	  if (data) {
	    swfdec_audio_decoder_push (flv->decoder, data);<commit_msg>add a hack that makes codec setup data survive seeks<commit_after>/* Swfdec
 * Copyright (C) 2007 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include "swfdec_audio_flv.h"
#include "swfdec_debug.h"
#include "swfdec_sound.h"


G_DEFINE_TYPE (SwfdecAudioFlv, swfdec_audio_flv, SWFDEC_TYPE_AUDIO)

static void
swfdec_audio_flv_dispose (GObject *object)
{
  SwfdecAudioFlv *flv = SWFDEC_AUDIO_FLV (object);

  if (flv->decoder != NULL) {
    g_object_unref (flv->decoder);
    flv->decoder = NULL;
  }
  g_queue_foreach (flv->playback_queue, (GFunc) swfdec_buffer_unref, NULL);
  g_queue_free (flv->playback_queue);
  g_object_unref (flv->flvdecoder);

  G_OBJECT_CLASS (swfdec_audio_flv_parent_class)->dispose (object);
}

static SwfdecBuffer *
swfdec_audio_flv_decode_one (SwfdecAudioFlv *flv)
{
  SwfdecBuffer *buffer;
  guint format;
  SwfdecAudioFormat in;
  guint now, soon;

  if  (g_queue_is_empty (flv->playback_queue)) {
    /* sync */
    guint last;
    swfdec_flv_decoder_get_audio (flv->flvdecoder, 
	SWFDEC_TICKS_TO_MSECS (flv->timestamp),
	NULL, NULL, &last, NULL);
    flv->playback_skip = SWFDEC_TICKS_TO_SAMPLES (
	flv->timestamp - SWFDEC_MSECS_TO_TICKS (last));
    flv->next_timestamp = last;
    SWFDEC_DEBUG ("syncing to %ums: next timestamp to decode is %ums, skipping %u samples", 
	(guint) SWFDEC_TICKS_TO_MSECS (flv->timestamp), 
	flv->next_timestamp, flv->playback_skip);
  }
  if (flv->decoder)
    buffer = swfdec_audio_decoder_pull (flv->decoder);
  else
    buffer = NULL;
  while (buffer == NULL) {
    if (flv->decoder && flv->next_timestamp == 0)
      return NULL;
    buffer = swfdec_flv_decoder_get_audio (flv->flvdecoder, flv->next_timestamp,
      &format, &in, &now, &soon);

    if (flv->next_timestamp != now) {
      /* FIXME: do sync on first frame here */
      SWFDEC_WARNING ("FIXME: didn't get requested timestamp - still loading?");
    }
    /* FIXME FIXME FIXME: This avoids decoding the last frame forever, however it ensures sync */
    if (soon == 0)
      return NULL;
    flv->next_timestamp = soon;
    if (flv->in == 0) {
      /* init */
      if (flv->decoder) {
	g_object_unref (flv->decoder);
	flv->decoder = NULL;
      }
      flv->format = format;
      flv->in = in;
      flv->decoder = swfdec_audio_decoder_new (flv->format, flv->in);
      if (flv->decoder == NULL)
	return NULL;
      /* This is a hack that ensures AAC codec data is always present, even if
       * the decoder gets initialized in the middle of the stream */
      if (format == SWFDEC_AUDIO_CODEC_AAC) {
	SwfdecBuffer *tmp = swfdec_flv_decoder_get_audio (flv->flvdecoder,
	    0, &format, NULL, NULL, NULL);
	if (format == SWFDEC_AUDIO_CODEC_AAC && tmp->data[0] == 0 &&
	    tmp->length > 1) {
	  tmp = swfdec_buffer_new_subbuffer (tmp, 1, tmp->length - 1);
	  swfdec_audio_decoder_set_codec_data (flv->decoder, tmp);
	  swfdec_buffer_unref (tmp);
	}
      }
    } else if (format != flv->format ||
	in != flv->in) {
      SWFDEC_ERROR ("FIXME: format change not implemented");
      return NULL;
    } else if (flv->decoder == NULL) {
      return NULL;
    }
    if (format == SWFDEC_AUDIO_CODEC_AAC) {
      SwfdecBuffer *data;
      SwfdecBits bits;
      guint type;
      swfdec_bits_init (&bits, buffer);
      type = swfdec_bits_get_u8 (&bits);
      switch (type) {
	case 0:
	  data = swfdec_bits_get_buffer (&bits, -1);
	  if (data) {
	    swfdec_audio_decoder_set_codec_data (flv->decoder, data);
	    swfdec_buffer_unref (data);
	  }
	  break;
	case 1:
	  data = swfdec_bits_get_buffer (&bits, -1);
	  if (data) {
	    swfdec_audio_decoder_push (flv->decoder, data);
{'fraction_non_alphanumeric': 0.11294632013602138, 'fraction_numerical': 0.00680106873937333, 'mean_word_length': 3.532195927352779, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2612561', 'n_tokens_mistral': 3069, 'n_tokens_neox': 2926, 'n_words': 802}