]> git.sesse.net Git - vlc/blobdiff - src/lpcm_decoder/lpcm_decoder_thread.c
* Mandatory step for video output IV and the audio output quality
[vlc] / src / lpcm_decoder / lpcm_decoder_thread.c
index 85c9d28f586bc6aefcd58e369585dabe09071bd4..9a3c3d260c988bf5f699b7bcbfed187cfd2bcccd 100644 (file)
@@ -2,23 +2,23 @@
  * lpcm_decoder_thread.c: lpcm decoder thread
  *****************************************************************************
  * Copyright (C) 1999, 2000 VideoLAN
+ * $Id: lpcm_decoder_thread.c,v 1.14 2001/05/01 04:18:18 sam Exp $
  *
- * Authors:
+ * Authors: Samuel Hocevar <sam@zoy.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- *
+ * 
  * This program 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
- * General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 #include <unistd.h>                                              /* getpid() */
 
 #include <stdio.h>                                           /* "intf_msg.h" */
+#include <string.h>                                    /* memcpy(), memset() */
 #include <stdlib.h>                                      /* malloc(), free() */
-#include <sys/types.h>                        /* on BSD, uio.h needs types.h */
-#include <sys/uio.h>                                            /* "input.h" */
 
 #include "config.h"
 #include "common.h"
 #include "threads.h"
 #include "mtime.h"
-#include "plugins.h"
-#include "debug.h"                                      /* "input_netlist.h" */
 
 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
 
-#include "input.h"                                           /* pes_packet_t */
-#include "input_netlist.h"                         /* input_NetlistFreePES() */
-#include "decoder_fifo.h"         /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
+#include "stream_control.h"
+#include "input_ext-dec.h"
 
 #include "audio_output.h"
 
 #include "lpcm_decoder.h"
 #include "lpcm_decoder_thread.h"
 
-#define LPCMDEC_FRAME_SIZE (2*1536)                    /* May not be usefull */
+#define LPCMDEC_FRAME_SIZE (2*1536)                        /* May be useless */
 
 /*****************************************************************************
  * Local prototypes
@@ -64,74 +60,41 @@ static void     EndThread               (lpcmdec_thread_t * p_adec);
 /*****************************************************************************
  * lpcmdec_CreateThread: creates an lpcm decoder thread
  *****************************************************************************/
-lpcmdec_thread_t * lpcmdec_CreateThread (input_thread_t * p_input)
+vlc_thread_t lpcmdec_CreateThread (adec_config_t * p_config)
 {
     lpcmdec_thread_t *   p_lpcmdec;
-    fprintf (stderr, "LPCM Debug: creating lpcm decoder thread\n");
+    intf_DbgMsg ( "lpcm: creating lpcm decoder thread" );
 
     /* Allocate the memory needed to store the thread's structure */
     if ((p_lpcmdec = (lpcmdec_thread_t *)malloc (sizeof(lpcmdec_thread_t))) == NULL) {
-        fprintf (stderr, "LPCM Error: not enough memory for lpcmdec_CreateThread() to create the new thread\n");
-        return NULL;
+        intf_ErrMsg ( "lpcm error: cannot create lpcmdec_thread_t" );
+        return 0;
     }
 
     /*
      * Initialize the thread properties
      */
-    p_lpcmdec->b_die = 0;
-    p_lpcmdec->b_error = 0;
+    p_lpcmdec->p_config = p_config;
+    p_lpcmdec->p_fifo = p_config->decoder_config.p_decoder_fifo;
 
-    /*
-     * Initialize the input properties
-     */
-    /* Initialize the decoder fifo's data lock and conditional variable and set
-     * its buffer as empty */
-    vlc_mutex_init (&p_lpcmdec->fifo.data_lock);
-    vlc_cond_init (&p_lpcmdec->fifo.data_wait);
-    p_lpcmdec->fifo.i_start = 0;
-    p_lpcmdec->fifo.i_end = 0;
 
     /* Initialize the lpcm decoder structures */
     lpcm_init (&p_lpcmdec->lpcm_decoder);
 
-    /* Initialize the bit stream structure */
-    p_lpcmdec->p_input = p_input;
-
     /*
      * Initialize the output properties
      */
-    p_lpcmdec->p_aout = p_input->p_aout;
     p_lpcmdec->p_aout_fifo = NULL;
 
     /* Spawn the lpcm decoder thread */
     if (vlc_thread_create(&p_lpcmdec->thread_id, "lpcm decoder", (vlc_thread_func_t)RunThread, (void *)p_lpcmdec)) {
-        fprintf  (stderr, "LPCM Error: can't spawn lpcm decoder thread\n");
+        intf_ErrMsg  ( "lpcm error: cannot spawn thread" );
         free (p_lpcmdec);
-        return NULL;
+        return 0;
     }
 
-    fprintf (stderr, "LPCM Debug: lpcm decoder thread (%p) created\n", p_lpcmdec);
-    return p_lpcmdec;
-}
-
-/*****************************************************************************
- * lpcmdec_DestroyThread: destroys an lpcm decoder thread
- *****************************************************************************/
-void lpcmdec_DestroyThread (lpcmdec_thread_t * p_lpcmdec)
-{
-    fprintf (stderr, "LPCM Debug: requesting termination of lpcm decoder thread %p\n", p_lpcmdec);
-
-    /* Ask thread to kill itself */
-    p_lpcmdec->b_die = 1;
-
-    /* Make sure the decoder thread leaves the GetByte() function */
-    vlc_mutex_lock (&(p_lpcmdec->fifo.data_lock));
-    vlc_cond_signal (&(p_lpcmdec->fifo.data_wait));
-    vlc_mutex_unlock (&(p_lpcmdec->fifo.data_lock));
-
-    /* Waiting for the decoder thread to exit */
-    /* Remove this as soon as the "status" flag is implemented */
-    vlc_thread_join (p_lpcmdec->thread_id);
+    intf_DbgMsg ( "LPCM Debug: lpcm decoder thread (%p) created", p_lpcmdec );
+    return p_lpcmdec->thread_id;
 }
 
 /* Following functions are local */
@@ -141,42 +104,36 @@ void lpcmdec_DestroyThread (lpcmdec_thread_t * p_lpcmdec)
  *****************************************************************************/
 static int InitThread (lpcmdec_thread_t * p_lpcmdec)
 {
-    aout_fifo_t         aout_fifo;
     lpcm_byte_stream_t * byte_stream;
 
-    fprintf (stderr, "LPCM Debug: initializing lpcm decoder thread %p\n", p_lpcmdec);
+    intf_DbgMsg ( "LPCM Debug: initializing lpcm decoder thread %p", p_lpcmdec );
 
     /* Our first job is to initialize the bit stream structure with the
      * beginning of the input stream */
-    vlc_mutex_lock (&p_lpcmdec->fifo.data_lock);
-    while (DECODER_FIFO_ISEMPTY(p_lpcmdec->fifo)) {
-        if (p_lpcmdec->b_die) {
-            vlc_mutex_unlock (&p_lpcmdec->fifo.data_lock);
+    vlc_mutex_lock (&p_lpcmdec->p_fifo->data_lock);
+    while (DECODER_FIFO_ISEMPTY(*p_lpcmdec->p_fifo)) {
+        if (p_lpcmdec->p_fifo->b_die) {
+            vlc_mutex_unlock (&p_lpcmdec->p_fifo->data_lock);
             return -1;
         }
-        vlc_cond_wait (&p_lpcmdec->fifo.data_wait, &p_lpcmdec->fifo.data_lock);
+        vlc_cond_wait (&p_lpcmdec->p_fifo->data_wait, &p_lpcmdec->p_fifo->data_lock);
     }
-    p_lpcmdec->p_ts = DECODER_FIFO_START (p_lpcmdec->fifo)->p_first_ts;
+    p_lpcmdec->p_data = DECODER_FIFO_START (*p_lpcmdec->p_fifo)->p_first;
     byte_stream = lpcm_byte_stream (&p_lpcmdec->lpcm_decoder);
-    byte_stream->p_byte =
-       p_lpcmdec->p_ts->buffer + p_lpcmdec->p_ts->i_payload_start;
-    byte_stream->p_end =
-       p_lpcmdec->p_ts->buffer + p_lpcmdec->p_ts->i_payload_end;
+    byte_stream->p_byte = p_lpcmdec->p_data->p_payload_start;
+    byte_stream->p_end = p_lpcmdec->p_data->p_payload_end;
     byte_stream->info = p_lpcmdec;
-    vlc_mutex_unlock (&p_lpcmdec->fifo.data_lock);
-
-    aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
-    aout_fifo.i_channels = 2;
-    aout_fifo.b_stereo = 1;
-
-    aout_fifo.l_frame_size = LPCMDEC_FRAME_SIZE;
+    vlc_mutex_unlock (&p_lpcmdec->p_fifo->data_lock);
 
     /* Creating the audio output fifo */
-    if ((p_lpcmdec->p_aout_fifo = aout_CreateFifo(p_lpcmdec->p_aout, &aout_fifo)) == NULL) {
+    p_lpcmdec->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
+                                              LPCMDEC_FRAME_SIZE, NULL  );
+    if ( p_lpcmdec->p_aout_fifo == NULL )
+    {
         return -1;
     }
 
-    fprintf (stderr,"LPCM Debug: lpcm decoder thread %p initialized\n", p_lpcmdec);
+    intf_DbgMsg ( "LPCM Debug: lpcm decoder thread %p initialized", p_lpcmdec );
     return 0;
 }
 
@@ -187,14 +144,15 @@ static void RunThread (lpcmdec_thread_t * p_lpcmdec)
 {
     int sync;
 
-    fprintf (stderr,"LPCM Debug: running lpcm decoder thread (%p) (pid== %i)\n", p_lpcmdec, getpid());
+    intf_DbgMsg( "LPCM Debug: running lpcm decoder thread (%p) (pid== %i)", p_lpcmdec, getpid() );
 
-    msleep (INPUT_PTS_DELAY);
+    /* Fucking holy piece of shit ! */
+    //msleep (INPUT_PTS_DELAY);
 
     /* Initializing the lpcm decoder thread */
     if (InitThread (p_lpcmdec)) 
     {
-        p_lpcmdec->b_error = 1;
+        p_lpcmdec->p_fifo->b_error = 1;
     }
 
     sync = 0;
@@ -203,47 +161,49 @@ static void RunThread (lpcmdec_thread_t * p_lpcmdec)
     /* lpcm decoder thread's main loop */
     /* FIXME : do we have enough room to store the decoded frames ?? */
    
-    while ((!p_lpcmdec->b_die) && (!p_lpcmdec->b_error))
+    while ((!p_lpcmdec->p_fifo->b_die) && (!p_lpcmdec->p_fifo->b_error))
     {
-           s16 * buffer;
-           lpcm_sync_info_t sync_info;
+        s16 * buffer;
+        lpcm_sync_info_t sync_info;
 
-           if (!sync)
+        if (!sync)
         {
             /* have to find a synchro point */
         }
     
-        if (DECODER_FIFO_START(p_lpcmdec->fifo)->b_has_pts)
+        if (DECODER_FIFO_START(*p_lpcmdec->p_fifo)->i_pts)
         {
-               p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_lpcmdec->fifo)->i_pts;
-               DECODER_FIFO_START(p_lpcmdec->fifo)->b_has_pts = 0;
+            p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(*p_lpcmdec->p_fifo)->i_pts;
+            DECODER_FIFO_START(*p_lpcmdec->p_fifo)->i_pts = 0;
         }
         else
         {
-               p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->l_end_frame] = LAST_MDATE;
+            p_lpcmdec->p_aout_fifo->date[p_lpcmdec->p_aout_fifo->l_end_frame] = LAST_MDATE;
         }
 
-       p_lpcmdec->p_aout_fifo->l_rate = sync_info.sample_rate;
+        p_lpcmdec->p_aout_fifo->l_rate = sync_info.sample_rate;
 
-           buffer = ((s16 *)p_lpcmdec->p_aout_fifo->buffer) + (p_lpcmdec->p_aout_fifo->l_end_frame * LPCMDEC_FRAME_SIZE);
+        buffer = ((s16 *)p_lpcmdec->p_aout_fifo->buffer) + (p_lpcmdec->p_aout_fifo->l_end_frame * LPCMDEC_FRAME_SIZE);
 
-           if (lpcm_decode_frame (&p_lpcmdec->lpcm_decoder, buffer))
+        if (lpcm_decode_frame (&p_lpcmdec->lpcm_decoder, buffer))
         {
-               sync = 0;
-               goto bad_frame;
-           }
+            sync = 0;
+            goto bad_frame;
+        }
+
+        vlc_mutex_lock (&p_lpcmdec->p_aout_fifo->data_lock);
+        p_lpcmdec->p_aout_fifo->l_end_frame = (p_lpcmdec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
+        vlc_cond_signal (&p_lpcmdec->p_aout_fifo->data_wait);
+        vlc_mutex_unlock (&p_lpcmdec->p_aout_fifo->data_lock);
 
-           vlc_mutex_lock (&p_lpcmdec->p_aout_fifo->data_lock);
-           p_lpcmdec->p_aout_fifo->l_end_frame = (p_lpcmdec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
-           vlc_cond_signal (&p_lpcmdec->p_aout_fifo->data_wait);
-           vlc_mutex_unlock (&p_lpcmdec->p_aout_fifo->data_lock);
+        intf_DbgMsg( "LPCM Debug: %x", *buffer );
 
-        fprintf(stderr, "LPCM Debug: %x\n", *buffer);
-        bad_frame:
+    bad_frame:
+        continue;
     }
 
     /* If b_error is set, the lpcm decoder thread enters the error loop */
-    if (p_lpcmdec->b_error)
+    if (p_lpcmdec->p_fifo->b_error)
     {
         ErrorThread (p_lpcmdec);
     }
@@ -259,22 +219,23 @@ static void ErrorThread (lpcmdec_thread_t * p_lpcmdec)
 {
     /* We take the lock, because we are going to read/write the start/end
      * indexes of the decoder fifo */
-    vlc_mutex_lock (&p_lpcmdec->fifo.data_lock);
+    vlc_mutex_lock (&p_lpcmdec->p_fifo->data_lock);
 
     /* Wait until a `die' order is sent */
-    while (!p_lpcmdec->b_die) {
+    while (!p_lpcmdec->p_fifo->b_die) {
         /* Trash all received PES packets */
-        while (!DECODER_FIFO_ISEMPTY(p_lpcmdec->fifo)) {
-            input_NetlistFreePES (p_lpcmdec->p_input, DECODER_FIFO_START(p_lpcmdec->fifo));
-            DECODER_FIFO_INCSTART (p_lpcmdec->fifo);
+        while (!DECODER_FIFO_ISEMPTY(*p_lpcmdec->p_fifo)) {
+            p_lpcmdec->p_fifo->pf_delete_pes(p_lpcmdec->p_fifo->p_packets_mgt,
+                    DECODER_FIFO_START(*p_lpcmdec->p_fifo));
+            DECODER_FIFO_INCSTART (*p_lpcmdec->p_fifo);
         }
 
         /* Waiting for the input thread to put new PES packets in the fifo */
-        vlc_cond_wait (&p_lpcmdec->fifo.data_wait, &p_lpcmdec->fifo.data_lock);
+        vlc_cond_wait (&p_lpcmdec->p_fifo->data_wait, &p_lpcmdec->p_fifo->data_lock);
     }
 
     /* We can release the lock before leaving */
-    vlc_mutex_unlock (&p_lpcmdec->fifo.data_lock);
+    vlc_mutex_unlock (&p_lpcmdec->p_fifo->data_lock);
 }
 
 /*****************************************************************************
@@ -282,7 +243,7 @@ static void ErrorThread (lpcmdec_thread_t * p_lpcmdec)
  *****************************************************************************/
 static void EndThread (lpcmdec_thread_t * p_lpcmdec)
 {
-    fprintf (stderr, "LPCM Debug: destroying lpcm decoder thread %p\n", p_lpcmdec);
+    intf_DbgMsg( "LPCM Debug: destroying lpcm decoder thread %p", p_lpcmdec );
 
     /* If the audio output fifo was created, we destroy it */
     if (p_lpcmdec->p_aout_fifo != NULL) {
@@ -297,7 +258,7 @@ static void EndThread (lpcmdec_thread_t * p_lpcmdec)
     /* Destroy descriptor */
     free (p_lpcmdec);
 
-    fprintf (stderr, "LPCM Debug: lpcm decoder thread %p destroyed\n", p_lpcmdec);
+    intf_DbgMsg( "LPCM Debug: lpcm decoder thread %p destroyed", p_lpcmdec );
 }
 
 void lpcm_byte_stream_next (lpcm_byte_stream_t * p_byte_stream)