]> git.sesse.net Git - vlc/blobdiff - modules/access/cdda/access.c
A few string fixes
[vlc] / modules / access / cdda / access.c
index 4cb26cc2c95b0ef63b0deeee42f6ce4996074990..f16183bb21373b305b8f87e3702d4cb610306c7e 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
+#include "callback.h"      /* FIXME - reorganize callback.h, cdda.h better */
+#include "cdda.h"          /* private structures. Also #includes vlc things */
+#include <vlc_playlist.h>  /* Has to come *after* cdda.h */
+#include "vlc_keys.h"
+
+#include <cdio/cdio.h>
+#include <cdio/logging.h>
+#include <cdio/cd_types.h>
+
 #include <stdio.h>
-#include <stdlib.h>
 
-#include <vlc/vlc.h>
-#include <vlc/intf.h>
+/* #ifdef variables below are defined via config.h via #include vlc above. */
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
 
+#ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
-#include <cdio/cdio.h>
-#include <cdio/cd_types.h>
+#endif
 
-#include "codecs.h"
-#include "vlc_keys.h"
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
 
 #ifdef HAVE_UNISTD_H
 #   include <unistd.h>
 #   include <errno.h>
 #endif
 
-#include <string.h>
-
-#include "cdda.h"
-
-/* how many blocks Open will read in each loop */
-#define CDDA_BLOCKS_ONCE 20
-#define CDDA_DATA_ONCE   (CDDA_BLOCKS_ONCE * CDIO_CD_FRAMESIZE_RAW)
-
 #define CDDA_MRL_PREFIX "cddax://"
 
+/* Frequency of sample in bits per second. */
+#define CDDA_FREQUENCY_SAMPLE 44100
+
 /* FIXME: This variable is a hack. Would be nice to eliminate. */
-static input_thread_t *p_cdda_input = NULL;
+access_t *p_cdda_input = NULL;
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static int  CDDARead         ( input_thread_t *, byte_t *, size_t );
-static void CDDASeek         ( input_thread_t *, off_t );
-static int  CDDASetArea      ( input_thread_t *, input_area_t * );
-static int  CDDASetProgram   ( input_thread_t *, pgrm_descriptor_t * );
-
-static int  CDDAFixupPlayList( input_thread_t *p_input,
-                              cdda_data_t *p_cdda, const char *psz_source,
-                              bool play_single_track);
-
-#if NEED_UPDATE_VAR
-static void CDDAUpdateVar( input_thread_t *p_input, int i_entry, int i_action,
-                          const char *p_varname, char *p_label,
-                          const char *p_debug_label );
-#endif
+static block_t *CDDAReadBlocks( access_t * p_access );
+static int      CDDASeek( access_t * p_access, int64_t i_pos );
+static int      CDDAControl( access_t *p_access, int i_query, 
+                            va_list args );
+static void     CDDAMetaInfo( access_t *p_access  );
+static int      CDDAFixupPlaylist( access_t *p_access, cdda_data_t *p_cdda, 
+                                  const char *psz_source, 
+                                  vlc_bool_t b_single_track );
+static void     CDDACreatePlaylistItem(const access_t *p_access, 
+                                      cdda_data_t *p_cdda,
+                                      playlist_t *p_playlist, 
+                                      track_t i_track,
+                                      char *psz_mrl, int psz_mrl_max,
+                                      const char *psz_source, 
+                                      int playlist_operation,
+                                      int i_pos);
+
+static int      GetCDInfo( access_t *p_access, cdda_data_t *p_cdda ) ;
+
+
 
 
 /****************************************************************************
@@ -87,7 +99,11 @@ static void CDDAUpdateVar( input_thread_t *p_input, int i_entry, int i_action,
 static void
 cdio_log_handler (cdio_log_level_t level, const char message[])
 {
-  cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
+  cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
+
+  if( p_cdda == NULL )
+      return;
+
   switch (level) {
   case CDIO_LOG_DEBUG:
   case CDIO_LOG_INFO:
@@ -103,7 +119,7 @@ cdio_log_handler (cdio_log_level_t level, const char message[])
     break;
   default:
     msg_Warn( p_cdda_input, message,
-            _("The above message had unknown vcdimager log level"),
+            "The above message had unknown cdio log level",
             level);
   }
   return;
@@ -117,7 +133,7 @@ cdio_log_handler (cdio_log_level_t level, const char message[])
 static void
 cddb_log_handler (cddb_log_level_t level, const char message[])
 {
-  cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
+  cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
   switch (level) {
   case CDDB_LOG_DEBUG:
   case CDDB_LOG_INFO:
@@ -136,11 +152,15 @@ cddb_log_handler (cddb_log_level_t level, const char message[])
 static void
 uninit_log_handler (cdio_log_level_t level, const char message[])
 {
-  cdda_data_t *p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
+  cdda_data_t *p_cdda = NULL;
+
+  if (p_cdda_input)
+    p_cdda = (cdda_data_t *)p_cdda_input->p_sys;
+
   switch (level) {
   case CDIO_LOG_DEBUG:
   case CDIO_LOG_INFO:
-    if (!(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
+    if (!p_cdda || !(p_cdda->i_debug & (INPUT_DBG_CDIO|INPUT_DBG_CDDB)))
       return;
     /* Fall through if to warn case */
   case CDIO_LOG_WARN:
@@ -155,7 +175,7 @@ uninit_log_handler (cdio_log_level_t level, const char message[])
   default:
     fprintf(stderr, "UNKNOWN ERROR: %s\n%s %d\n",
             message,
-            _("The above message had unknown cdio log level"),
+            "The above message had unknown cdio log level",
             level);
   }
 
@@ -163,287 +183,108 @@ uninit_log_handler (cdio_log_level_t level, const char message[])
 }
 
 /*****************************************************************************
- * CDDAPlay: Arrange things so we play the specified track.
- * VLC_TRUE is returned if there was no error.
+ * CDDAReadBlocks: reads a group of blocks from the CD-DA and returns
+ * an allocated pointer to the data. NULL is returned if no data
+ * read. It is also possible if we haven't read a RIFF header in which
+ * case one that we creaded during Open/Initialization is returned.
  *****************************************************************************/
-vlc_bool_t
-CDDAPlay( input_thread_t *p_input, int i_track )
+static block_t *
+CDDAReadBlocks( access_t * p_access )
 {
-  cdda_data_t *p_cdda = (cdda_data_t *) p_input->p_access_data;
+    block_t     *p_block;
+    cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
+    int          i_blocks = p_cdda->i_blocks_per_read;
 
-  if( i_track > p_cdda->i_nb_tracks || i_track < 1 )
-    return VLC_FALSE;
+    dbg_print((INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_LSN), "called %d", 
+             p_cdda->i_lsn);
 
-  CDDASetArea( p_input, p_input->stream.pp_areas[i_track] );
-  return VLC_TRUE;
-}
+    /* Check end of file */
+    if( p_access->info.b_eof ) return NULL;
 
-/*****************************************************************************
- * CDDARead: reads from the CDDA into PES packets.
- *****************************************************************************
- * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
- * bytes.
- *****************************************************************************/
-static int CDDARead( input_thread_t * p_input, byte_t * p_buffer,
-                     size_t i_len )
-{
-    cdda_data_t *           p_cdda;
-    int                     i_blocks;
-    int                     i_index;
-    int                     i_read;
-
-    p_cdda = (cdda_data_t *)p_input->p_access_data;
-
-    /* Compute the number of blocks we have to read */
-    i_blocks = i_len / CDIO_CD_FRAMESIZE_RAW;
-    i_read = 0;
-
-    if( !p_cdda->i_header_pos )
-    {
-        p_cdda->i_header_pos = sizeof(WAVEHEADER);
-        i_blocks = (i_len - sizeof(WAVEHEADER)) / CDIO_CD_FRAMESIZE_RAW;
-        memcpy( p_buffer, &p_cdda->waveheader, sizeof(WAVEHEADER) );
-        p_buffer += sizeof(WAVEHEADER);
-        i_read += sizeof(WAVEHEADER);
+    if( !p_cdda->b_header )
+      {
+        /* Return only the dummy RIFF header we created in Open/Init */
+        p_block = block_New( p_access, sizeof( WAVEHEADER ) );
+        memcpy( p_block->p_buffer, &p_cdda->waveheader, sizeof(WAVEHEADER) );
+        p_cdda->b_header = VLC_TRUE;
+        return p_block;
     }
 
-    for( i_index = 0; i_index < i_blocks; i_index++ )
+    /* Check end of track */
+    while( p_cdda->i_lsn >= p_cdda->p_lsns[p_access->info.i_title + 1] )
     {
-
-        if( cdio_read_audio_sector( p_cdda->p_cddev->cdio, p_buffer,
-                                    p_cdda->i_sector) != 0 )
+        if( p_access->info.i_title + 1 >= p_cdda->i_tracks )
         {
-            msg_Err( p_input, "could not read sector %lu", 
-                    (long unsigned int) p_cdda->i_sector );
-            return -1;
+            p_access->info.b_eof = VLC_TRUE;
+            return NULL;
         }
 
-        p_cdda->i_sector ++;
-        if( p_cdda->i_sector == p_cdda->p_sectors[p_cdda->i_track + 1] )
-        {
-            input_area_t *p_area;
-
-            dbg_print( (INPUT_DBG_LSN|INPUT_DBG_CALL),
-                       "end of track, cur: %lu", 
-                      (long unsigned int) p_cdda->i_sector );
-
-            /*???? if ( p_cdda->i_track >= p_cdda->i_nb_tracks - 1 )*/
-                return 0; /* EOF */
-
-            vlc_mutex_lock( &p_input->stream.stream_lock );
-            p_area = p_input->stream.pp_areas[
-                    p_input->stream.p_selected_area->i_id + 1 ];
-
-            p_area->i_part = 1;
-            CDDASetArea( p_input, p_area );
-            vlc_mutex_unlock( &p_input->stream.stream_lock );
-        }
-        i_read += CDIO_CD_FRAMESIZE_RAW;
-        p_buffer += CDIO_CD_FRAMESIZE_RAW;
+        p_access->info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SIZE;
+        p_access->info.i_title++;
+        p_access->info.i_size = 
+         p_cdda->p_title[p_access->info.i_title]->i_size;
+        p_access->info.i_pos = 0;
+       p_cdda->i_track++;
     }
 
-    if ( i_len % CDIO_CD_FRAMESIZE_RAW ) /* this should not happen */
+    /* Possibly adjust i_blocks so we don't read past the end of a track. */
+    if( p_cdda->i_lsn + i_blocks >=
+        p_cdda->p_lsns[p_access->info.i_title + 1] )
     {
-        msg_Err( p_input, "must read full sectors" );
+        i_blocks = p_cdda->p_lsns[p_access->info.i_title + 1 ] -
+                   p_cdda->i_lsn;
     }
 
-    return i_read;
-}
-
-/*****************************************************************************
- * CDDASetProgram: Does nothing since a CDDA is mono_program
- *****************************************************************************/
-static int CDDASetProgram( input_thread_t * p_input,
-                           pgrm_descriptor_t * p_program)
-{
-    cdda_data_t * p_cdda= (cdda_data_t *) p_input->p_access_data;
-    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
-    return 0;
-}
-
-/*****************************************************************************
- * CDDASetArea: initialize input data for title x.
- * It should be called for each user navigation request.
- ****************************************************************************/
-static int CDDASetArea( input_thread_t * p_input, input_area_t * p_area )
-{
-    cdda_data_t *p_cdda = (cdda_data_t*) p_input->p_access_data;
-    vlc_value_t val;
-    vlc_value_t text;
-
-    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "");
-
-    text.psz_string = _("Track");
-    var_Change( p_input, "title", VLC_VAR_SETTEXT, &text, NULL );
-
-    /* we can't use the interface slider until initilization is complete */
-    p_input->stream.b_seekable = 0;
-
-    if( p_area != p_input->stream.p_selected_area )
+    /* Do the actual reading */
+    p_block = block_New( p_access, i_blocks * CDIO_CD_FRAMESIZE_RAW );
+    if( !p_block)
     {
-        /* Change the default area */
-        p_input->stream.p_selected_area = p_area;
-
-        /* Change the current track */
-        p_cdda->i_track = p_area->i_id - 1;
-        p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
-
-        /* Update the navigation variables without triggering a callback */
-        val.i_int = p_area->i_id;
-        var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
+      msg_Err( p_access, "Cannot get a new block of size: %i",
+              i_blocks * CDIO_CD_FRAMESIZE_RAW );
+      return NULL;
     }
 
-    p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track];
-
-    p_input->stream.p_selected_area->i_tell =
-        (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
-         - p_input->stream.p_selected_area->i_start;
-
-    /* warn interface that something has changed */
-    p_input->stream.b_seekable = 1;
-    p_input->stream.b_changed = 1;
+    if( cdio_read_audio_sectors( p_cdda->p_cdio, p_block->p_buffer,
+                                 p_cdda->i_lsn, i_blocks) != 0 )
+        {
+         msg_Err( p_access, "could not read sector %lu",
+                  (long unsigned int) p_cdda->i_lsn );
+         block_Release( p_block );
+
+         /* If we had problems above, assume the problem is with
+            the first sector of the read and set to skip it.  In
+            the future libcdio may have cdparanoia support.
+         */
+         p_cdda->i_lsn++;
+         p_access->info.i_pos += CDIO_CD_FRAMESIZE_RAW;
+         return NULL;
+        }
+    
+    p_cdda->i_lsn     += i_blocks;
+    p_access->info.i_pos += p_block->i_buffer;
 
-    return 0;
+    return p_block;
 }
 
 /****************************************************************************
- * CDDASeek
+ * CDDASeek - change position for subsequent reads. For example, this
+ * can happen if the user moves a position slider bar in a GUI.
  ****************************************************************************/
-static void CDDASeek( input_thread_t * p_input, off_t i_off )
+static int 
+CDDASeek( access_t * p_access, int64_t i_pos )
 {
-    cdda_data_t * p_cdda;
-
-    p_cdda = (cdda_data_t *) p_input->p_access_data;
-
-    p_cdda->i_sector = p_cdda->p_sectors[p_cdda->i_track]
-                       + i_off / (off_t)CDIO_CD_FRAMESIZE_RAW;
+    cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
 
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    p_input->stream.p_selected_area->i_tell =
-        (off_t)p_cdda->i_sector * (off_t)CDIO_CD_FRAMESIZE_RAW
-         - p_input->stream.p_selected_area->i_start;
-
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
+    p_cdda->i_lsn = p_cdda->p_lsns[p_access->info.i_title]
+                  + (i_pos / CDIO_CD_FRAMESIZE_RAW);
+    p_access->info.i_pos = i_pos;
 
     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_SEEK),
-               "sector %lu, offset: %lld, i_tell: %lld",  
-              (long unsigned int) p_cdda->i_sector, i_off,
-               p_input->stream.p_selected_area->i_tell );
-
-}
-
-#if NEED_UPDATE_VAR
-/****************************************************************************
- Update the "varname" variable to i_num without triggering a callback.
-****************************************************************************/
-static void
-CDDAUpdateVar( input_thread_t *p_input, int i_num, int i_action,
-              const char *p_varname, char *p_label, 
-             const char *p_debug_label)
-{
-  vlc_value_t val;
-  val.i_int = i_num;
-  if (p_label) {
-    vlc_value_t text;
-    text.psz_string = p_label;
-    var_Change( p_input, p_varname, VLC_VAR_SETTEXT, &text, NULL );
-  }
-  var_Change( p_input, p_varname, i_action, &val, NULL );
-}
-#endif
-
-#define meta_info_add_str(title, str)                          \
-  if ( str ) {                                                 \
-    dbg_print( INPUT_DBG_META, "field %s: %s\n", title, str);  \
-    input_AddInfo( p_cat, _(title), "%s", str );               \
-    vlc_mutex_lock( &p_playlist->object_lock );                \
-    p_item = playlist_ItemGetByPos( p_playlist, -1 );          \
-    vlc_mutex_unlock( &p_playlist->object_lock );              \
-    vlc_mutex_lock( &p_item->lock );                           \
-    playlist_ItemAddInfo( p_item, p_cat->psz_name,             \
-                      _(title), "%s" , str );                  \
-    vlc_mutex_unlock( &p_item->lock );                         \
-  }
-
-
-static void InformationCreate( input_thread_t *p_input  )
-{
-  cdda_data_t *p_cdda = (cdda_data_t *) p_input->p_access_data;
-  playlist_t *p_playlist = vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
-                                            FIND_PARENT );
-  input_info_category_t *p_cat;
-
-  p_cat = input_InfoCategory( p_input, "General" );
-
-
-#ifdef HAVE_LIBCDDB
-  if (p_cdda->i_cddb_enabled) {
-    playlist_item_t *p_item;
-
-    meta_info_add_str( "Title", p_cdda->cddb.disc->title );
-    meta_info_add_str( "Artist", p_cdda->cddb.disc->artist );
-    meta_info_add_str( "Genre", p_cdda->cddb.disc->genre );
-    meta_info_add_str( "Extended Data", p_cdda->cddb.disc->ext_data );
-    {
-      char year[5];
-      if (p_cdda->cddb.disc->year != 0) {
-        snprintf(year, 5, "%d", p_cdda->cddb.disc->year);
-        meta_info_add_str( "Year", year );
-      }
-      if ( p_cdda->cddb.disc->discid ) {
-        input_AddInfo( p_cat, _("CDDB Disc ID"), "%x",
-                       p_cdda->cddb.disc->discid );
-      }
-
-      if ( p_cdda->cddb.disc->category != CDDB_CAT_INVALID ) {
-        input_AddInfo( p_cat, _("CDDB Disc Category"), "%s",
-                       CDDB_CATEGORY[p_cdda->cddb.disc->category] );
-      }
-
-    }
-  }
-
-#endif /*HAVE_LIBCDDB*/
-#define TITLE_MAX 30
-
-  {
-    track_t i_track = p_cdda->i_nb_tracks;
-    char psz_buffer[MSTRTIME_MAX_SIZE];
-    mtime_t i_duration =
-      (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[0])
-      / CDIO_CD_FRAMES_PER_SEC;
-
-    dbg_print( INPUT_DBG_META, "Duration %ld", (long int) i_duration );
-    input_AddInfo( p_cat, _("Duration"), "%s",
-                   secstotimestr( psz_buffer, i_duration ) );
-
-    for( i_track = 0 ; i_track < p_cdda->i_nb_tracks ; i_track++ ) {
-      char track_str[TITLE_MAX];
-      mtime_t i_duration =
-       (p_cdda->p_sectors[i_track+1] - p_cdda->p_sectors[i_track])
-       / CDIO_CD_FRAMES_PER_SEC;
-      snprintf(track_str, TITLE_MAX, "%s %02d", _("Track"), i_track+1);
-      p_cat = input_InfoCategory( p_input, track_str );
-      input_AddInfo( p_cat, _("Duration"), "%s",
-                    secstotimestr( psz_buffer, i_duration ) );
-
-#ifdef HAVE_LIBCDDB
-      if (p_cdda->i_cddb_enabled) {
-        cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
-                                            i_track);
-        if (t != NULL && t->artist != NULL) {
-         input_AddInfo( p_cat, _("Artist"), "%s", t->artist );
-         input_AddInfo( p_cat, _("Title"), "%s",  t->title );
-       }
-      }
-#endif
-    }
-  }
-
-  if( p_playlist ) vlc_object_release( p_playlist );
+               "lsn %lu, offset: %lld",  
+              (long unsigned int) p_cdda->i_lsn, i_pos );
+    return VLC_SUCCESS;
 }
 
-
 #ifdef HAVE_LIBCDDB
 
 #define free_and_dup(var, val) \
@@ -452,80 +293,82 @@ static void InformationCreate( input_thread_t *p_input  )
 
 
 static void
-GetCDDBInfo( const input_thread_t *p_input, cdda_data_t *p_cdda )
+GetCDDBInfo( access_t *p_access, cdda_data_t *p_cdda )
 {
 
   dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
 
-  if (config_GetInt( p_input, MODULE_STRING "-cddb-enabled" )) {
+  if (config_GetInt( p_access, MODULE_STRING "-cddb-enabled" )) {
     int i, i_matches;
     cddb_conn_t  *conn = cddb_new();
-    const CdIo *cdio = p_cdda->p_cddev->cdio;
+    const CdIo *p_cdio = p_cdda->p_cdio;
 
 
     cddb_log_set_handler (uninit_log_handler);
 
     if (!conn) {
-      msg_Warn( p_input, "unable to initialize libcddb" );
+      msg_Warn( p_access, "Unable to initialize libcddb" );
       goto cddb_destroy;
     }
 
     cddb_set_email_address( conn,
-                            config_GetPsz( p_input,
+                            config_GetPsz( p_access,
                                            MODULE_STRING "-cddb-email") );
 
     cddb_set_server_name( conn,
-                          config_GetPsz( p_input,
+                          config_GetPsz( p_access,
                                          MODULE_STRING "-cddb-server") );
 
     cddb_set_server_port(conn,
-                          config_GetInt( p_input,
+                          config_GetInt( p_access,
                                          MODULE_STRING "-cddb-port") );
 
     /* Set the location of the local CDDB cache directory.
        The default location of this directory is */
 
-    if (!config_GetInt( p_input, MODULE_STRING "-cddb-enable-cache" ))
+    if (!config_GetInt( p_access, MODULE_STRING "-cddb-enable-cache" ))
       cddb_cache_disable(conn);
 
     cddb_cache_set_dir(conn,
-                       config_GetPsz( p_input,
+                       config_GetPsz( p_access,
                                       MODULE_STRING "-cddb-cachedir") );
 
     cddb_set_timeout(conn,
-                     config_GetInt( p_input, MODULE_STRING "-cddb-timeout") );
+                     config_GetInt( p_access, MODULE_STRING "-cddb-timeout") );
 
 
-    if (config_GetInt( p_input, MODULE_STRING "-cddb-httpd" )) {
+    if (config_GetInt( p_access, MODULE_STRING "-cddb-httpd" )) {
       cddb_http_enable(conn);
     } else
       cddb_http_disable(conn);
 
     p_cdda->cddb.disc = cddb_disc_new();
     if (!p_cdda->cddb.disc) {
-      msg_Err( p_input, "Unable to create CDDB disc structure." );
+      msg_Err( p_access, "Unable to create CDDB disc structure." );
       goto cddb_end;
     }
 
-    for(i = 1; i <= p_cdda->i_nb_tracks; i++) {
+    p_cdda->psz_mcn = cdio_get_mcn(p_cdio);
+
+    for(i = 1; i <= p_cdda->i_tracks; i++) {
       cddb_track_t *t = cddb_track_new();
-      t->frame_offset = cdio_get_track_lba(cdio, i);
+      t->frame_offset = cdio_get_track_lba(p_cdio, i);
       cddb_disc_add_track(p_cdda->cddb.disc, t);
     }
 
     p_cdda->cddb.disc->length =
-      cdio_get_track_lba(cdio, CDIO_CDROM_LEADOUT_TRACK)
+      cdio_get_track_lba(p_cdio, CDIO_CDROM_LEADOUT_TRACK)
       / CDIO_CD_FRAMES_PER_SEC;
 
     if (!cddb_disc_calc_discid(p_cdda->cddb.disc)) {
-      msg_Err( p_input, "CDDB disc calc failed" );
+      msg_Err( p_access, "CDDB disc ID calculation failed" );
       goto cddb_destroy;
     }
 
     i_matches = cddb_query(conn, p_cdda->cddb.disc);
     if (i_matches > 0) {
       if (i_matches > 1)
-        msg_Warn( p_input, "Found %d matches in CDDB. Using first one.",
+        msg_Warn( p_access, "Found %d matches in CDDB. Using first one.",
                   i_matches);
       cddb_read(conn, p_cdda->cddb.disc);
 
@@ -533,7 +376,7 @@ GetCDDBInfo( const input_thread_t *p_input, cdda_data_t *p_cdda )
         cddb_disc_print(p_cdda->cddb.disc);
 
     } else {
-      msg_Warn( p_input, "CDDB error: %s", cddb_error_str(errno));
+      msg_Warn( p_access, "CDDB error: %s", cddb_error_str(errno));
     }
 
   cddb_destroy:
@@ -543,6 +386,101 @@ GetCDDBInfo( const input_thread_t *p_input, cdda_data_t *p_cdda )
 }
 #endif /*HAVE_LIBCDDB*/
 
+#define add_meta_val(FIELD, VLC_META, VAL)                             \
+  if ( p_cdda->p_meta && VAL) {                                                \
+    vlc_meta_Add( p_cdda->p_meta, VLC_META, VAL );                     \
+    dbg_print( INPUT_DBG_META, "field %s: %s\n", VLC_META, VAL );      \
+  }                                                                    \
+    
+#define add_cddb_meta(FIELD, VLC_META)                                 \
+  add_meta_val(FIELD, VLC_META, p_cdda->cddb.disc->FIELD);
+    
+#define add_cddb_meta_fmt(FIELD, FORMAT_SPEC, VLC_META)                        \
+  {                                                                    \
+    char psz_buf[100];                                                 \
+    snprintf( psz_buf, sizeof(psz_buf)-1, FORMAT_SPEC,                 \
+             p_cdda->cddb.disc->FIELD );                               \
+    psz_buf[sizeof(psz_buf)-1] = '\0';                                 \
+    add_meta_val(FIELD, VLC_META, psz_buf);                            \
+  }    
+
+/*
+ Gets and saves CDDA Meta Information. In the Control routine, 
+ we handle Meta Information requests and basically copy what we've
+ saved here. 
+ */    
+static void CDDAMetaInfo( access_t *p_access  )
+{
+  cdda_data_t *p_cdda = (cdda_data_t *) p_access->p_sys;
+
+#ifdef HAVE_LIBCDDB
+  if ( p_cdda && p_cdda->i_cddb_enabled ) {
+
+    GetCDDBInfo(p_access, p_cdda);
+
+    if ( p_cdda->cddb.disc ) {
+
+      p_cdda->p_meta = vlc_meta_New();
+
+      add_cddb_meta(title,    VLC_META_CDDB_TITLE);
+      add_cddb_meta(artist,   VLC_META_CDDB_ARTIST);
+      add_cddb_meta(genre,    VLC_META_CDDB_GENRE);
+      add_cddb_meta(ext_data, VLC_META_CDDB_EXT_DATA);
+
+      add_cddb_meta_fmt(year,   "%d", VLC_META_CDDB_YEAR);
+      add_cddb_meta_fmt(discid, "%x", VLC_META_CDDB_DISCID);
+    }
+  }
+
+#endif /*HAVE_LIBCDDB*/
+#define TITLE_MAX 30
+
+#if UPDATE_TRACK_INFORMATION_FINISHED
+  {
+    track_t i_track = p_cdda->i_tracks;
+    char psz_buffer[MSTRTIME_MAX_SIZE];
+    mtime_t i_duration =
+      (p_cdda->p_lsns[i_track] - p_cdda->p_lsns[0])
+      / CDIO_CD_FRAMES_PER_SEC;
+
+    dbg_print( INPUT_DBG_META, "Duration %ld", (long int) i_duration );
+    input_Control( p_access, INPUT_ADD_INFO, _("General"), _("Duration"), "%s",
+                  secstotimestr( psz_buffer, i_duration ) );
+
+    for( i_track = 0 ; i_track < p_cdda->i_tracks ; i_track++ ) {
+      char track_str[TITLE_MAX];
+      mtime_t i_duration =
+        (p_cdda->p_lsns[i_track+1] - p_cdda->p_lsns[i_track])
+        / CDIO_CD_FRAMES_PER_SEC;
+      snprintf(track_str, TITLE_MAX, "%s %02d", _("Track"), i_track+1);
+      input_Control( p_access, INPUT_ADD_INFO, track_str, _("Duration"), "%s",
+                     secstotimestr( psz_buffer, i_duration ) );
+
+#ifdef HAVE_LIBCDDB
+      if (p_cdda->i_cddb_enabled) {
+        cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
+                                            i_track);
+        if (t != NULL) {
+          if ( t->artist != NULL && strlen(t->artist) ) {
+            input_Control( p_access, INPUT_ADD_INFO, track_str,
+                           _("Artist"), "%s", t->artist );
+          }
+          if ( t->title != NULL && strlen(t->title) ) {
+            input_Control( p_access, INPUT_ADD_INFO, track_str,
+                           _("Title"), "%s",  t->title );
+          }
+          if ( t->ext_data != NULL && strlen(t->ext_data) ) {
+            input_Control( p_access, INPUT_ADD_INFO, track_str,
+                           _("Extended Data"), "%s",  t->ext_data );
+          }
+        }
+      }
+#endif /*HAVE_LIBCDDB*/
+    }
+  }
+#endif /* UPDATE_TRACK_INFORMATION_FINISHED */
+}
+
 #define add_format_str_info(val)                         \
   {                                                      \
     const char *str = val;                               \
@@ -591,18 +529,18 @@ GetCDDBInfo( const input_thread_t *p_input, cdda_data_t *p_cdda )
    %% : a %
 */
 static char *
-CDDAFormatStr(const input_thread_t *p_input, cdda_data_t *p_cdda,
-              const char format_str[], const char *mrl, int i_track)
+CDDAFormatStr( const access_t *p_access, cdda_data_t *p_cdda,
+              const char format_str[], const char *mrl, int i_track)
 {
 #define TEMP_STR_SIZE 256
 #define TEMP_STR_LEN (TEMP_STR_SIZE-1)
   static char    temp_str[TEMP_STR_SIZE];
   size_t i;
   char * tp = temp_str;
-  bool saw_control_prefix = false;
+  vlc_bool_t saw_control_prefix = false;
   size_t format_len = strlen(format_str);
 
-  bzero(temp_str, TEMP_STR_SIZE);
+  memset(temp_str, 0, TEMP_STR_SIZE);
 
   for (i=0; i<format_len; i++) {
 
@@ -622,30 +560,36 @@ CDDAFormatStr(const input_thread_t *p_input, cdda_data_t *p_cdda,
 #ifdef HAVE_LIBCDDB
     case 'a':
       if (!p_cdda->i_cddb_enabled) goto not_special;
-      add_format_str_info(p_cdda->cddb.disc->artist);
+      if (p_cdda->cddb.disc)
+       add_format_str_info(p_cdda->cddb.disc->artist);
       break;
     case 'A':
       if (!p_cdda->i_cddb_enabled) goto not_special;
-      add_format_str_info(p_cdda->cddb.disc->title);
+      if (p_cdda->cddb.disc)
+       add_format_str_info(p_cdda->cddb.disc->title);
       break;
     case 'C':
       if (!p_cdda->i_cddb_enabled) goto not_special;
-      add_format_str_info(CDDB_CATEGORY[p_cdda->cddb.disc->category]);
+      if (p_cdda->cddb.disc)
+       add_format_str_info(CDDB_CATEGORY[p_cdda->cddb.disc->category]);
       break;
     case 'G':
       if (!p_cdda->i_cddb_enabled) goto not_special;
-      add_format_str_info(p_cdda->cddb.disc->genre);
+      if (p_cdda->cddb.disc)
+       add_format_str_info(p_cdda->cddb.disc->genre);
       break;
     case 'I':
       if (!p_cdda->i_cddb_enabled) goto not_special;
-      add_format_num_info(p_cdda->cddb.disc->discid, "%x");
+      if (p_cdda->cddb.disc)
+       add_format_num_info(p_cdda->cddb.disc->discid, "%x");
       break;
     case 'Y':
       if (!p_cdda->i_cddb_enabled) goto not_special;
-      add_format_num_info(p_cdda->cddb.disc->year, "%5d");
+      if (p_cdda->cddb.disc)
+       add_format_num_info(p_cdda->cddb.disc->year, "%5d");
       break;
     case 't':
-      if (p_cdda->i_cddb_enabled) {
+      if (p_cdda && p_cdda->i_cddb_enabled && p_cdda->cddb.disc) {
         cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
                                             i_track-1);
         if (t != NULL && t->title != NULL)
@@ -653,40 +597,44 @@ CDDAFormatStr(const input_thread_t *p_input, cdda_data_t *p_cdda,
       } else goto not_special;
       break;
     case 'p':
-      if (p_cdda->i_cddb_enabled) {
+      if (p_cdda->i_cddb_enabled && p_cdda->cddb.disc) {
         cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
                                             i_track-1);
         if (t != NULL && t->artist != NULL)
           add_format_str_info(t->artist);
       } else goto not_special;
       break;
+    case 'e':
+      if (p_cdda->i_cddb_enabled && p_cdda->cddb.disc) {
+        cddb_track_t *t=cddb_disc_get_track(p_cdda->cddb.disc,
+                                            i_track-1);
+        if (t != NULL && t->ext_data != NULL)
+          add_format_str_info(t->ext_data);
+      } else goto not_special;
+      break;
 #endif
 
     case 'M':
       add_format_str_info(mrl);
       break;
 
-#if FINISHED
     case 'm':
-      add_format_str_info(p_cdda->mcn);
+      add_format_str_info(p_cdda->psz_mcn);
       break;
-#endif
 
     case 'n':
-      add_format_num_info(p_cdda->i_nb_tracks, "%d");
+      add_format_num_info(p_cdda->i_tracks, "%d");
       break;
 
-#ifdef HAVE_LIBCDDB
     case 's':
       if (p_cdda->i_cddb_enabled) {
         char psz_buffer[MSTRTIME_MAX_SIZE];
         mtime_t i_duration =
-          (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1])
+          (p_cdda->p_lsns[i_track] - p_cdda->p_lsns[i_track-1])
           / CDIO_CD_FRAMES_PER_SEC;
         add_format_str_info(secstotimestr( psz_buffer, i_duration ) );
       } else goto not_special;
       break;
-#endif
 
     case 'T':
       add_format_num_info(i_track, "%02d");
@@ -704,14 +652,14 @@ CDDAFormatStr(const input_thread_t *p_input, cdda_data_t *p_cdda,
 }
 
 static void
-CDDACreatePlayListItem(const input_thread_t *p_input, cdda_data_t *p_cdda,
-                       playlist_t *p_playlist, unsigned int i_track,
+CDDACreatePlaylistItem(const access_t *p_access, cdda_data_t *p_cdda,
+                       playlist_t *p_playlist, track_t i_track,
                        char *psz_mrl, int psz_mrl_max,
                        const char *psz_source, int playlist_operation,
                        int i_pos)
 {
   mtime_t i_duration =
-    (p_cdda->p_sectors[i_track] - p_cdda->p_sectors[i_track-1])
+    (p_cdda->p_lsns[i_track] - p_cdda->p_lsns[i_track-1])
     * (1000000 / CDIO_CD_FRAMES_PER_SEC) ;
   char *p_author;
   char *p_title;
@@ -727,8 +675,8 @@ CDDACreatePlayListItem(const input_thread_t *p_input, cdda_data_t *p_cdda,
   snprintf(psz_mrl, psz_mrl_max, "%s%s@T%u",
            CDDA_MRL_PREFIX, psz_source, i_track);
 
-  p_title = CDDAFormatStr(p_input, p_cdda,
-                          config_GetPsz( p_input, config_varname ),
+  p_title = CDDAFormatStr(p_access, p_cdda,
+                          config_GetPsz( p_access, config_varname ),
                           psz_mrl, i_track);
 
   dbg_print( INPUT_DBG_META, "mrl: %s, title: %s, duration, %ld, pos %d",
@@ -744,11 +692,11 @@ CDDACreatePlayListItem(const input_thread_t *p_input, cdda_data_t *p_cdda,
   if( !p_item )
       return;
 
-  vlc_mutex_lock( &p_item->lock );
+  vlc_mutex_lock( &p_item->input.lock );
 
   p_author =
-    CDDAFormatStr( p_input, p_cdda,
-                   config_GetPsz( p_input, MODULE_STRING "-author-format" ),
+    CDDAFormatStr( p_access, p_cdda,
+                   config_GetPsz( p_access, MODULE_STRING "-author-format" ),
                    psz_mrl, i_track );
 
   playlist_ItemAddInfo( p_item ,  _("General"),_("Author"), p_author);
@@ -789,12 +737,12 @@ CDDACreatePlayListItem(const input_thread_t *p_input, cdda_data_t *p_cdda,
   }
 #endif /*HAVE_LIBCDDB*/
 
-  vlc_mutex_unlock( &p_item->lock );
+  vlc_mutex_unlock( &p_item->input.lock );
 }
 
 static int
-CDDAFixupPlayList( input_thread_t *p_input, cdda_data_t *p_cdda,
-                   const char *psz_source, bool play_single_track)
+CDDAFixupPlaylist( access_t *p_access, cdda_data_t *p_cdda, 
+                  const char *psz_source, vlc_bool_t b_single_track )
 {
   int i;
   playlist_t * p_playlist;
@@ -804,292 +752,206 @@ CDDAFixupPlayList( input_thread_t *p_input, cdda_data_t *p_cdda,
 
 #ifdef HAVE_LIBCDDB
   p_cdda->i_cddb_enabled =
-    config_GetInt( p_input, MODULE_STRING "-cddb-enabled" );
-  if( play_single_track && !p_cdda->i_cddb_enabled ) return 0;
+    config_GetInt( p_access, MODULE_STRING "-cddb-enabled" );
+  if( b_single_track && !p_cdda->i_cddb_enabled ) return 0;
 #else
-  if( play_single_track ) return 0;
+  if( b_single_track ) return VLC_SUCCESS;
 #endif
 
-
   psz_mrl = malloc( psz_mrl_max );
 
   if( psz_mrl == NULL )
     {
-      msg_Warn( p_input, "out of memory" );
-      return -1;
+      msg_Warn( p_access, "out of memory" );
+      return VLC_ENOMEM;
     }
 
-  p_playlist = (playlist_t *) vlc_object_find( p_input, VLC_OBJECT_PLAYLIST,
+  p_playlist = (playlist_t *) vlc_object_find( p_access, VLC_OBJECT_PLAYLIST,
                                                FIND_ANYWHERE );
   if( !p_playlist )
     {
-      msg_Warn( p_input, "can't find playlist" );
+      msg_Warn( p_access, "can't find playlist" );
       free(psz_mrl);
-      return -1;
+      return VLC_EGENERIC;
     }
 
-#ifdef HAVE_LIBCDDB
-  if (p_cdda->i_cddb_enabled)
-    GetCDDBInfo(p_input, p_cdda);
-  else
-    p_cdda->cddb.disc = NULL;
-#endif
+  CDDAMetaInfo(p_access);
 
-  InformationCreate(p_input);
-
-  if (play_single_track) {
+  if (b_single_track) {
     /* May fill out more information when the playlist user interface becomes
        more mature.
      */
-    CDDACreatePlayListItem(p_input, p_cdda, p_playlist, p_cdda->i_track+1,
+    CDDACreatePlaylistItem(p_access, p_cdda, p_playlist, p_cdda->i_track,
                            psz_mrl, psz_mrl_max, psz_source, PLAYLIST_REPLACE,
                            p_playlist->i_index);
   } else {
 
-    playlist_Delete( p_playlist, p_playlist->i_index);
-
-    for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
+    for( i = 1 ; i <= p_cdda->i_tracks ; i++ )
       {
-        CDDACreatePlayListItem(p_input, p_cdda, p_playlist, i, psz_mrl,
-                               psz_mrl_max, psz_source, PLAYLIST_APPEND,
-                               PLAYLIST_END);
+       input_title_t *t = p_cdda->p_title[i-1] = vlc_input_title_New();
+       
+       asprintf( &t->psz_name, _("Track %i"), i );
+       t->i_size = ( p_cdda->p_lsns[i] - p_cdda->p_lsns[i-1] ) *
+         (int64_t)CDIO_CD_FRAMESIZE_RAW;
+       
+       t->i_length = I64C(1000000) * t->i_size / CDDA_FREQUENCY_SAMPLE / 4;
+       CDDACreatePlaylistItem(p_access, p_cdda, p_playlist, i, psz_mrl,
+                              psz_mrl_max, psz_source, PLAYLIST_APPEND,
+                              PLAYLIST_END);
       }
-
-    playlist_Command( p_playlist, PLAYLIST_GOTO, 0 );
-
   }
 
-  vlc_object_release( p_playlist );
-  free(psz_mrl);
-  return 0;
+  return VLC_SUCCESS;
 }
 
 /****************************************************************************
  * Public functions
  ****************************************************************************/
-int
-E_(DebugCB)   ( vlc_object_t *p_this, const char *psz_name,
-                vlc_value_t oldval, vlc_value_t val, void *p_data )
-{
-  cdda_data_t *p_cdda;
-
-  if (NULL == p_cdda_input) return VLC_EGENERIC;
-
-  p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
-
-  if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
-    msg_Dbg( p_cdda_input, "Old debug (x%0x) %d, new debug (x%0x) %d",
-             p_cdda->i_debug, p_cdda->i_debug, val.i_int, val.i_int);
-  }
-  p_cdda->i_debug = val.i_int;
-  return VLC_SUCCESS;
-}
-
-int
-E_(CDDBEnabledCB)   ( vlc_object_t *p_this, const char *psz_name,
-                      vlc_value_t oldval, vlc_value_t val, void *p_data )
-{
-  cdda_data_t *p_cdda;
-
-  if (NULL == p_cdda_input) return VLC_EGENERIC;
-
-  p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
-
-#ifdef HAVE_LIBCDDB
-  if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
-    msg_Dbg( p_cdda_input, "Old CDDB Enabled (x%0x) %d, new (x%0x) %d",
-             p_cdda->i_cddb_enabled, p_cdda->i_cddb_enabled,
-             val.i_int, val.i_int);
-  }
-  p_cdda->i_cddb_enabled = val.i_int;
-#endif
-  return VLC_SUCCESS;
-}
-
-/*FIXME*/
-#if PLAYLIST_INTERFACE_IS_FIXED
-int
-E_(TitleFormatCB)   ( vlc_object_t *p_this, const char *psz_name,
-                      vlc_value_t oldval, vlc_value_t val, void *p_data )
-{
-  cdda_data_t *p_cdda;
-
-  if (NULL == p_cdda_input) return VLC_EGENERIC;
-
-  p_cdda = (cdda_data_t *)p_cdda_input->p_access_data;
-
-  if (p_cdda->i_debug & (INPUT_DBG_CALL|INPUT_DBG_EXT)) {
-    msg_Dbg( p_cdda_input, "Old CDDB Enabled (%s), new (%s)",
-             oldval.psz_string, val.psz_string);
-  }
-  ????
-  return VLC_SUCCESS;
-}
-#endif
 
 /*****************************************************************************
- * Open: open cdda
+ * Open: open cdda device or image file and initialize structures 
+ * for subsequent operations.
  *****************************************************************************/
 int
-E_(Open)( vlc_object_t *p_this )
+E_(CDDAOpen)( vlc_object_t *p_this )
 {
-    input_thread_t *        p_input = (input_thread_t *)p_this;
-    char *                  psz_orig;
-    char *                  psz_parser;
-    char *                  psz_source;
-    cdda_data_t *           p_cdda;
-    int                     i;
-    int                     i_track = 1;
-    cddev_t                 *p_cddev;
-    vlc_value_t             val;
-    bool                    play_single_track = false;
+    access_t    *p_access = (access_t*)p_this;
+    char *      psz_source = NULL;
+    cdda_data_t *p_cdda;
+    CdIo        *p_cdio;
+    track_t     i_track = 1;
+    vlc_bool_t  b_single_track = false;
+    int         i_rc = VLC_EGENERIC;
+
+    p_access->p_sys = NULL;
 
     /* Set where to log errors messages from libcdio. */
-    p_cdda_input = (input_thread_t *)p_this;
+    p_cdda_input = p_access;
 
     /* parse the options passed in command line : */
-    psz_orig = psz_parser = psz_source = strdup( p_input->psz_name );
-
-    if( !psz_orig )
-    {
-        return( -1 );
-    }
-
-    while( *psz_parser && *psz_parser != '@' )
-    {
-        psz_parser++;
-    }
 
-    if( *psz_parser == '@' )
+    if( p_access->psz_path && *p_access->psz_path )
     {
-        /* Found options */
-        *psz_parser = '\0';
-        ++psz_parser;
+      char *psz_parser = psz_source = strdup( p_access->psz_path );
 
-        if ('T' == *psz_parser || 't' == *psz_parser )
+      while( *psz_parser && *psz_parser != '@' )
+       {
+         psz_parser++;
+       }
+      
+      if( *psz_parser == '@' )
+       {
+         /* Found options */
+         *psz_parser = '\0';
+         ++psz_parser;
+         
+         if ('T' == *psz_parser || 't' == *psz_parser )
             ++psz_parser;
+         
+         i_track = (int)strtol( psz_parser, NULL, 10 );
+         i_track = i_track ? i_track : 1;
+         b_single_track = true;
+       }
+    } 
 
-        i_track = (int)strtol( psz_parser, NULL, 10 );
-        i_track = i_track ? i_track : 1;
-        play_single_track = true;
-    }
+    if (!psz_source || !*psz_source)
+      {
+        /* No device/track given. Continue only when this plugin was 
+          selected */
+        if( !p_this->b_force ) return VLC_EGENERIC;
 
-    if( !*psz_source ) {
-      /* No source specified, so figure it out. */
-      if( !p_input->psz_access ) {
-        free( psz_orig );
-        return -1;
-      }
-      psz_source = config_GetPsz( p_input, "cd-audio" );
+        psz_source = var_CreateGetString( p_this, "cd-audio" );
 
-      if( !psz_source || 0==strlen(psz_source) ) {
+       if( !psz_source || !*psz_source ) {
         /* Scan for a CD-ROM drive with a CD-DA in it. */
         char **cd_drives =
           cdio_get_devices_with_cap(NULL,  CDIO_FS_AUDIO, false);
-        if (NULL == cd_drives) return -1;
-        if (cd_drives[0] == NULL) {
-          cdio_free_device_list(cd_drives);
-          return -1;
-        }
+
+        if (NULL == cd_drives || NULL == cd_drives[0] ) {
+         msg_Err( p_access, 
+                  "libcdio couldn't find something with a CD-DA in it" );
+          if (cd_drives) cdio_free_device_list(cd_drives);
+         return VLC_EGENERIC;
+       }
+       
         psz_source = strdup(cd_drives[0]);
         cdio_free_device_list(cd_drives);
       }
     }
 
-    /* Open CDDA */
     cdio_log_set_handler ( cdio_log_handler );
-#ifdef HAVE_LIBCDDB
-    cddb_log_set_handler ( cddb_log_handler );
-#endif
 
-    if( !(p_cddev = ioctl_Open( p_this, psz_source )) )
+    /* Open CDDA */
+    if( !(p_cdio = cdio_open( psz_source, DRIVER_UNKNOWN )) )
     {
-        msg_Warn( p_input, "could not open %s", psz_source );
-        free( psz_source );
-        return VLC_EGENERIC;
+        msg_Warn( p_access, "could not open %s", psz_source );
+       goto error2;
     }
 
     p_cdda = malloc( sizeof(cdda_data_t) );
     if( p_cdda == NULL )
     {
-        msg_Err( p_input, "out of memory" );
+        msg_Err( p_access, "out of memory" );
         free( psz_source );
         return VLC_ENOMEM;
     }
+    memset( p_cdda, 0, sizeof(cdda_data_t) );
 
-    p_cdda->p_cddev        = p_cddev;
-    p_cdda->i_debug        = config_GetInt( p_this, MODULE_STRING "-debug" );
-    p_input->p_access_data = (void *)p_cdda;
-
-    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
-
-    p_input->i_mtu = CDDA_DATA_ONCE;
+#ifdef HAVE_LIBCDDB
+    cddb_log_set_handler ( cddb_log_handler );
+    p_cdda->cddb.disc = NULL;
+    p_cdda->i_cddb_enabled =
+      config_GetInt( p_access, MODULE_STRING "-cddb-enabled" );
+#endif
 
-    /* We read the Table Of Content information */
-    p_cdda->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
-                              p_cdda->p_cddev->cdio, &p_cdda->p_sectors );
-    if( p_cdda->i_nb_tracks < 0 )
-        msg_Err( p_input, "unable to count tracks" );
-    else if( p_cdda->i_nb_tracks <= 0 )
-        msg_Err( p_input, "no audio tracks found" );
-
-    if( p_cdda->i_nb_tracks <= 0 )
-    {
-        ioctl_Close( p_cdda->p_cddev );
-        free( p_cdda );
-        free( psz_source );
-        return VLC_EGENERIC;
+    p_cdda->b_header = VLC_FALSE;
+    p_cdda->p_cdio   = p_cdio;
+    p_cdda->i_track  = i_track;
+    p_cdda->i_debug  = config_GetInt(p_this, MODULE_STRING "-debug");
+    p_cdda->i_blocks_per_read
+                     = config_GetInt(p_this, MODULE_STRING "-blocks-per-read");
+
+    if (0 == p_cdda->i_blocks_per_read)
+      p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
+    
+    if ( p_cdda->i_blocks_per_read < MIN_BLOCKS_PER_READ 
+        || p_cdda->i_blocks_per_read > MAX_BLOCKS_PER_READ ) {
+      msg_Warn( p_cdda_input, 
+               "Number of blocks (%d) has to be between %d and %d. "
+               "Using %d.", 
+               p_cdda->i_blocks_per_read, 
+               MIN_BLOCKS_PER_READ, MAX_BLOCKS_PER_READ,
+               DEFAULT_BLOCKS_PER_READ );
+      p_cdda->i_blocks_per_read = DEFAULT_BLOCKS_PER_READ;
     }
 
-    if( i_track > p_cdda->i_nb_tracks || i_track < 1 )
-        i_track = 1;
-
-    /* Set stream and area data */
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-
-    /* Initialize ES structures */
-    input_InitStream( p_input, 0 );
 
-    /* cdda input method */
-    p_input->stream.i_method = INPUT_METHOD_CDDA;
-
-    p_input->stream.b_pace_control = 1;
-    p_input->stream.b_seekable = 1;
-    p_input->stream.i_mux_rate = 44100 * 4 / 50;
-
-#define area p_input->stream.pp_areas
-    for( i = 1 ; i <= p_cdda->i_nb_tracks ; i++ )
-    {
-        input_AddArea( p_input, i, 1 );
-
-        /* Absolute start offset and size */
-        area[i]->i_start =
-            (off_t)p_cdda->p_sectors[i-1] * (off_t)CDIO_CD_FRAMESIZE_RAW;
-        area[i]->i_size =
-            (off_t)(p_cdda->p_sectors[i] - p_cdda->p_sectors[i-1])
-            * (off_t)CDIO_CD_FRAMESIZE_RAW;
-    }
-#undef area
-
-    CDDAPlay( p_input, i_track);
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
+    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "%s", psz_source );
 
-    CDDAFixupPlayList(p_input, p_cdda, psz_source, play_single_track);
+    /* Set up p_access */
+    p_access->pf_read    = NULL;
+    p_access->pf_block   = CDDAReadBlocks;
+    p_access->pf_control = CDDAControl;
+    p_access->pf_seek    = CDDASeek;
 
-    p_input->pf_read = CDDARead;
-    p_input->pf_seek = CDDASeek;
-    p_input->pf_set_area = CDDASetArea;
-    p_input->pf_set_program = CDDASetProgram;
+    p_access->info.i_update    = 0;
+    p_access->info.i_size      = 0;
+    p_access->info.i_pos       = 0;
+    p_access->info.b_eof       = VLC_FALSE;
+    p_access->info.i_title     = 0;
+    p_access->info.i_seekpoint = 0;
 
-    /* Update default_pts to a suitable value for cdda access */
-    var_Create( p_input, MODULE_STRING "-caching",
-                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
-    var_Get( p_input, MODULE_STRING "-caching", &val );
-    p_input->i_pts_delay = val.i_int * 1000;
+    p_access->p_sys     = (access_sys_t *) p_cdda;
 
-    free( psz_source );
+    /* We read the Table Of Content information */
+    i_rc = GetCDInfo( p_access, p_cdda );
+    if ( VLC_SUCCESS != i_rc ) goto error;
 
-    /* Build a WAV header for the output data */
+    CDDAFixupPlaylist( p_access, p_cdda, psz_source, b_single_track );
+    
+    /* Build a WAV header to put in front of the output data. 
+       This gets sent back in the Block (read) routine.
+     */
     memset( &p_cdda->waveheader, 0, sizeof(WAVEHEADER) );
     SetWLE( &p_cdda->waveheader.Format, 1 ); /*WAVE_FORMAT_PCM*/
     SetWLE( &p_cdda->waveheader.BitsPerSample, 16);
@@ -1099,29 +961,47 @@ E_(Open)( vlc_object_t *p_this )
     p_cdda->waveheader.SubChunkID  = VLC_FOURCC('f', 'm', 't', ' ');
     SetDWLE( &p_cdda->waveheader.SubChunkLength, 16);
     SetWLE( &p_cdda->waveheader.Modus, 2);
-    SetDWLE( &p_cdda->waveheader.SampleFreq, 44100);
+    SetDWLE( &p_cdda->waveheader.SampleFreq, CDDA_FREQUENCY_SAMPLE);
     SetWLE( &p_cdda->waveheader.BytesPerSample,
             2 /*Modus*/ * 16 /*BitsPerSample*/ / 8 );
     SetDWLE( &p_cdda->waveheader.BytesPerSec,
-             16 /*BytesPerSample*/ * 44100 /*SampleFreq*/ );
+            2*16/8 /*BytesPerSample*/ * CDDA_FREQUENCY_SAMPLE );
     p_cdda->waveheader.DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
-    p_cdda->waveheader.DataLength = 0;                 /* we just don't know */
-    p_cdda->i_header_pos = 0;
+    p_cdda->waveheader.DataLength  = 0;    /* we just don't know */
 
+    /* PTS delay */
+    var_Create( p_access, MODULE_STRING "-caching", 
+               VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
     return VLC_SUCCESS;
+
+ error:
+    cdio_destroy( p_cdda->p_cdio );
+    free( p_cdda );
+ error2:
+    free( psz_source );
+    return i_rc;
+
 }
 
 /*****************************************************************************
- * CDDAClose: closes cdda
+ * CDDAClose: closes cdda and frees any resources associded with it.
  *****************************************************************************/
 void
-E_(Close)( vlc_object_t *p_this )
+E_(CDDAClose)( vlc_object_t *p_this )
 {
-    input_thread_t *   p_input = (input_thread_t *)p_this;
-    cdda_data_t *p_cdda = (cdda_data_t *)p_input->p_access_data;
+    access_t    *p_access = (access_t *) p_this;
+    cdda_data_t *p_cdda   = (cdda_data_t *) p_access->p_sys;
+    track_t      i;
 
     dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT), "" );
-    ioctl_Close( p_cdda->p_cddev );
+
+    /* Remove playlist titles */
+    for( i = 0; i < p_cdda->i_tracks; i++ )
+    {
+        vlc_input_title_Delete( p_cdda->p_title[i] );
+    }
+
+    cdio_destroy( p_cdda->p_cdio );
 
     cdio_log_set_handler (uninit_log_handler);
 
@@ -1131,7 +1011,178 @@ E_(Close)( vlc_object_t *p_this )
       cddb_disc_destroy(p_cdda->cddb.disc);
 #endif
 
-    free( p_cdda->p_sectors );
+    free( p_cdda->p_lsns );
+    if (p_cdda->psz_mcn) free( p_cdda->psz_mcn );
     free( p_cdda );
     p_cdda_input = NULL;
 }
+
+/*****************************************************************************
+ * Control: The front-end or vlc engine calls here to ether get
+ * information such as meta information or plugin capabilities or to
+ * issue miscellaneous "set" requests.
+ *****************************************************************************/
+static int CDDAControl( access_t *p_access, int i_query, va_list args )
+{
+    cdda_data_t  *p_cdda = (cdda_data_t *) p_access->p_sys;
+    int          *pi_int;
+    int i;
+
+    dbg_print( (INPUT_DBG_CALL|INPUT_DBG_EXT|INPUT_DBG_EVENT),
+               "query %d", i_query );
+
+    switch( i_query )
+    {
+        /* Pass back a copy of meta information that was gathered when we
+          during the Open/Initialize call.
+        */
+        case ACCESS_GET_META:
+         { 
+           vlc_meta_t **pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
+           if ( p_cdda->p_meta ) {
+             *pp_meta = vlc_meta_Duplicate( p_cdda->p_meta );
+             dbg_print( INPUT_DBG_META, "%s", "Meta copied" );
+           } else 
+             msg_Warn( p_access, "tried to copy NULL meta info" );
+           
+           return VLC_SUCCESS;
+         }
+         return VLC_EGENERIC;
+
+        case ACCESS_CAN_SEEK:
+        case ACCESS_CAN_FASTSEEK:
+        case ACCESS_CAN_PAUSE:
+        case ACCESS_CAN_CONTROL_PACE: 
+         {
+            vlc_bool_t *pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
+            *pb_bool = VLC_TRUE;
+            break;
+         }
+
+        /* */
+        case ACCESS_GET_MTU:
+            pi_int = (int*)va_arg( args, int * );
+            *pi_int = p_cdda-> i_blocks_per_read * CDIO_CD_FRAMESIZE_RAW;
+            break;
+
+        case ACCESS_GET_PTS_DELAY:
+         { 
+           int64_t *pi_64 = (int64_t*)va_arg( args, int64_t * );
+            *pi_64 = var_GetInteger( p_access, MODULE_STRING "-caching" )
+             * MILLISECONDS_PER_SEC;
+            break;
+         }
+
+        /* */
+        case ACCESS_SET_PAUSE_STATE:
+            break;
+
+        case ACCESS_GET_TITLE_INFO:
+         { input_title_t ***ppp_title;
+            ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
+            pi_int    = (int*)va_arg( args, int* );
+           *((int*)va_arg( args, int* )) = 1; /* Title offset */
+
+            /* Duplicate track info */
+            *pi_int = p_cdda->i_tracks;
+            *ppp_title = malloc(sizeof( input_title_t **) * p_cdda->i_tracks );
+
+           if (!*ppp_title) return VLC_ENOMEM;
+
+            for( i = 0; i < p_cdda->i_tracks; i++ )
+            {
+             if ( p_cdda->p_title[i] )
+                (*ppp_title)[i] = 
+                 vlc_input_title_Duplicate( p_cdda->p_title[i] );
+            }
+         }
+         break;
+
+        case ACCESS_SET_TITLE:
+            i = (int)va_arg( args, int );
+            if( i != p_access->info.i_title )
+            {
+                /* Update info */
+                p_access->info.i_update |=
+                    INPUT_UPDATE_TITLE|INPUT_UPDATE_SIZE;
+                p_access->info.i_title = i;
+                p_access->info.i_size = p_cdda->p_title[i]->i_size;
+                p_access->info.i_pos = 0;
+
+                /* Next sector to read */
+                p_cdda->i_lsn = p_cdda->p_lsns[i];
+            }
+            break;
+
+        case ACCESS_SET_SEEKPOINT:
+        case ACCESS_SET_PRIVATE_ID_STATE:
+            return VLC_EGENERIC;
+        default:
+         msg_Warn( p_access, "unimplemented query in control" );
+            return VLC_EGENERIC;
+
+    }
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+  GetCDInfo: 
+
+ Initialize information pertaining to the CD: the number of tracks,
+ first track number, LSNs for each track and the leadout. The leadout
+ information is stored after the last track. The LSN array is
+ 0-origin, same as p_access->info.  Add first_track to get what track
+ number this is on the CD. Note: libcdio uses the real track number.
+
+ On input we assume p_cdda->p_cdio and p_cdda->i_track have been set.
+
+ We return the VLC-type status, e.g. VLC_SUCCESS, VLC_ENOMEM, etc.
+ *****************************************************************************/
+static int
+GetCDInfo( access_t *p_access, cdda_data_t *p_cdda ) 
+{
+    track_t i;
+    discmode_t  discmode = CDIO_DISC_MODE_NO_INFO;
+
+    p_cdda->i_tracks       = cdio_get_num_tracks(p_cdda->p_cdio);
+    p_cdda->i_first_track  = cdio_get_first_track_num(p_cdda->p_cdio);
+
+    discmode = cdio_get_discmode(p_cdda->p_cdio);
+    switch(discmode) {
+    case CDIO_DISC_MODE_CD_DA:
+    case CDIO_DISC_MODE_CD_MIXED:
+      /* These are possible for CD-DA */
+      break;
+    default:
+      /* These are not possible for CD-DA */
+      msg_Err( p_access, 
+              "Disc seems not to be CD-DA. libcdio reports it is %s",
+              discmode2str[discmode]
+              );
+      return VLC_EGENERIC;
+    }
+    
+    p_cdda->p_lsns = malloc( (p_cdda->i_tracks + 1) * sizeof(lsn_t) );
+
+    if( p_cdda->p_lsns == NULL )
+      {
+        msg_Err( p_access, "out of memory" );
+        return VLC_ENOMEM;
+      }
+
+
+    /* Fill the p_lsns structure with the track/sector matches.
+       Note cdio_get_track_lsn when given num_tracks + 1 will return
+       the leadout LSN.
+     */
+    for( i = 0 ; i <= p_cdda->i_tracks ; i++ )
+      {
+        (p_cdda->p_lsns)[ i ] = 
+         cdio_get_track_lsn(p_cdda->p_cdio, p_cdda->i_first_track+i);
+      }
+
+    /* Set reading start LSN. */
+    p_cdda->i_lsn = p_cdda->p_lsns[p_cdda->i_track - p_cdda->i_first_track];
+
+    return VLC_SUCCESS;
+}