[revised 2006-10-05]
   Please check the home page at 
   periodically to view/download the latest version of this file.
------------------------------------------------------------------------
                         Lplex v 0.2 beta win32
------------------------------------------------------------------------
           DVD-Video Lpcm multiplex utility - GNU GPL License
------------------------------------------------------------------------
   The purpose of Lplex is to provide a free, quick, and hopefully 
   painless way to 
   -  create a playable full-resolution audio-only dvd-video disc from 
      wave or flac audio filesets, with md5 verification and archival 
      content. 
   -  extract unencrypted lpcm audio streams from dvd-video to wave or 
      flac in an md5-verified process. 
   You're already fully equipped for hi-res audio (up to 24-bit/96khz) 
   if you have a typical dvd-player and digital amp, and Lplex is a way 
   to take advantage of this. 
   
---------------------------- REQUIREMENTS ------------------------------
   - Windows XP, DVD-capable hardware
   - Adequate free space, roughly 
     - 2x wave input size or 3x flac for authoring (cleanup enabled).
     - equal to input size for extraction.
         
============================= QUICK START ==============================
   1. Unzip the folder 'Lplex' to a location of your choice.
   
   2. NTSC USERS: SKIP THIS STEP.
      - your tv system is NTSC if you live in Bahamas, Barbados, Belize, 
        Bermuda, Bolivia, Canada, Chile, Columbia, Cuba, El Salvador, 
        Jamaica, Japan, Mexico, Panama, Peru, Philippines, Puerto Rico, 
        South Korea, Surinam, Taiwan, Trinidad, USA, or Venezuela. 
        Otherwise your system is PAL or SECAM. 
      PAL/SECAM USERS ONLY:
      - double-click on 'Lplex.exe' and then close it; now open 
        'Lplex.ini' in Notepad and change 'video=ntsc' to 'video=pal', 
        and save. 
   3. Just drop a folder (or disc) onto 'Lplex.exe' and it automatically 
      creates a dvd if it finds audio files inside, or extracts audio 
      if it finds a dvd filesystem. That's it (almost). 
      Default authoring is set to continuous audio, i.e. a concert. To 
      author a compilation with breaks between the tracks, you can 
      either drop the input folder onto 'Lplex_discrete.bat' (in the 
      'Util' subfolder), or proceed to the SETTINGS section below to 
      customize Lplex first. 
   4. Press CTRL-C at anytime to stop the program.
   
   5. Burn the resulting .iso image file using Nero, ImgBurn, or similar
      program.
------------------------------ FEATURES --------------------------------
   - easy drag & drop operation
   Authoring
   
   - wave or flac input (16 or 24 bit / 48 or 96 Khz / 1-8 channel)
   - no loss of audio data due to stream truncation (this is a problem 
     with other multiplexors) 
   - md5 monitoring of audio data
   - automatic inclusion of archival data in dvd: info files, art, etc.  
   - partial authoring to generate lpcm, m2v, mpeg files.  
   Extraction
   
   - wave or flac output (16 or 24 bit / 48 or 96 Khz / 1-8 channel)
   - md5 monitoring of audio data
      
   (for Lplex-authored discs)
   - restoration of identical original audio data with md5 checking 
   - restoration of input file/folder structure with original filenames 
     and archival data 
   
------------------------------- SETTINGS -------------------------------
   Open 'Lplex.ini' in any plain text editor (e.g. Notepad) to see 
   the default settings. You can edit these to suit your preferences and 
   save.  The settings are: 
   
   'formatOut=wave' (or 'flac' or 'flac#' [where #=1-8] or 'raw')
      EXTRACTION: format will be wave (or flac# where # specifies 
      compression level, or raw dvd-order lpcm). 'flac' is the same as 
      'flac6'. 
   'video=ntsc' (or 'pal' or 'secam')
      AUTHORING: the TV standard to use; must match your dvd player (for 
      the purposes of dvd-video 'pal' and 'secam' are equivalent).
      
   'md5Aware=true' (or 'false')
      AUTHORING: md5 signatures and restoration tags will be embedded 
      in the dvd for future reference during extraction. 
      EXTRACTION: if the dvd is Lplex-authored, the audio data will be 
      md5-verified and the original filenames and folder structure 
      restored. If not Lplex-authored, the audio files will be named 
      according to their position in the dvd structure, and an md5 value 
      will be calculated. 
   'restore=true' (or 'false')
      EXTRACTION: if the dvd is Lplex-authored, track startpoints will 
      be shifted back to their original pre-authored locations, entirely 
      restoring the original fileset, and if set to 'false', output will 
      correspond to the dvd start and end points as-is, excluding any 
      padding. This setting only takes effect if 'md5Aware=true'. 
   'infoFiles=true' (or 'false')
      AUTHORING: all non-wav/flac files encountered will be copied, with 
      the folder structure intact, to an archive folder named 'XTRA' in 
      the root of the dvd. 
      EXTRACTION: if the dvd is Lplex-authored, the file/folder 
      structure in the 'XTRA' folder will be copied to the output 
      folder. 
   'jpeg=black' (or 'black_x' [where x=XS,S,M,L] or a valid filename)
      AUTHORING: the accompanying video will be a black screen. Default 
      size is 352x240 NTSC or 352x288 PAL. You can substitute your 
      own jpeg image by specifying a valid filename (using double 
      backslashes), e.g. 'jpeg=C:\\myImages\\coolbackground.jpg'. Valid 
      dimensions are: 
                  L        M        S       XS
         NTSC: 720x480  704x480  352x480  352x240
         PAL : 720x576  704x576  352x576  352x288
   'alignment=seamless' (or 'discrete' or 'indiscrete' or 'none' )
   
      [AUTHORING:]
      
      seamless: ("concert" mode - lossless, gapless, unpadded)
         Track startpoints will be shifted to where audio and video 
         frames are in alignment, ensuring gapless playback and no need 
         for any padding between tracks. Shift direction is determined 
         by the 'shift' setting (see below). 
         
      discrete: ("compilation" mode - lossless with gaps and padding)
         Tracks will be padded with silence to the next full audio
         frame in order to prevent stream truncation during multiplexing.
         A playback gap (equal to video duration - audio duration) is 
         introduced. An additional half-second pause will also occur 
         during playback because of stc discontinuity. 
      indiscrete: ("compilation" mode - lossless with padding only)
         Tracks will be padded with silence to the next point of a/v 
         frame alignment. Unlike 'discrete' there is technically neither 
         a playback gap nor a discontinuity pause in this model, though 
         the padding is much greater. 
      none: ("standard" model - lossy, unpadded, with gaps)
         Tracks will be padded to next dvd sample unit if required. 
         Audio will be dropped at the end of each track, unless your 
         files happen to be the exact length to require no truncation. 
      See the ARCANE DISCUSSION below for more on alignment. 
   'shift=backward' (or 'nearest' or 'forward')
      AUTHORING: direction to shift the track startpoints if 'alignment' 
      is 'seamless' (see above). 
   'cleanup=true' (or 'false')
      AUTHORING: all interim files (raw lpcm, m2v, mpg, etc) will be 
      deleted in progress; otherwise these files are left wherever 
      'workPath' points to (your system's temp directory by default) - 
      make sure you have adequate additional free space (roughly an 
      extra 2x input size). 
   'create=iso' (or 'dvd' or 'dvdstyler' or 'mpeg' or 'm2v' or 'lpcm')
      
      [AUTHORING:] process will be completed to the specified stage:
      
      iso: an iso image file ('[output name].iso') suitable for burning
         with ImgBurn, Nero(*) or similar program will be created 
         alongside the output folder. To allow you to further edit the 
         contents of the 'XTRA' archive folder prior to burning, or to 
         test the dvd file structure first on a software player, this 
         step can also be done separately afterwards by dropping the 
         '[output name]_DVD' folder onto the 'mkisofs.bat'(**) utility 
         (in the 'Util' subfolder). I recommend burning using an image, 
         not the dvd file structure, since your burning program will 
         likely reject 24-bit content, or worse yet downsample it by 
         default.         
         (*) my Nero (Express 6) mistakes some DVD iso's for CD images 
            (based on size, I think) but ImgBurn has no trouble with
            them. 
         (**) if using 'mkisofs.bat' please ensure the '_DVD' foldername
            is 32 bytes or less, otherwise mkisofs will fail, reporting
            "Volume ID string too long".
      dvd: a dvd file structure will be created in the dvd folder.
      
      dvdstyler: mpeg file(s) and an xml configuration file suitable for
         loading into dvdStyler for menu-generation will be created in
         the work folder. The dvd folder (including the 'XTRA' info 
         folder) will also be created for later transfer of the 
         'VIDEO_TS' and 'AUDIO_TS' folders output by dvdStyler.
         For details see LIMITATIONS section below. 
      mpeg: mpeg file(s) will be created in the work folder. The dvd
         folder (including the 'XTRA' info folder) will also be
         created.
      m2v: raw lpcm and m2v files only will be created in the work
         folder.
      lpcm: raw dvd-order lpcm file(s) only will be created in the work
         folder.
      Note: there is no need to change the 'cleanup' setting for partial
      authoring.
   'media=dvd+r' (or 'dvd-r' or 'dl' or 'none')
      AUTHORING: will verify that output fits on the given media before 
      authoring. This is only a size check.
      
      Important: for 'dl' (dual-layer) media, see LIMITATIONS section 
      below. 
      
   'verbose=false' (or 'true')
      AUTHORING: if 'verbose=true' all of Lplex's screen messages will 
      be shown in progress. This verbose version is always written to 
      'Lplex.log' in the 'XTRA' folder regardless of this setting. 
      EXTRACTION: same as above, except 'Lplex.log' is written to the 
      output folder. 
   'dvdPath=adjacent' (or any valid path)
      AUTHORING: the output folder is by default right next door to the 
      input folder if input is on your hard drive, and named '[input 
      folder name]_DVD', i.e. dropping 'c:\someAudio' will output to 
      'c:\someAudio_DVD'. You can instead redirect dvd file output 
      globally to any valid path by editing this setting (using double 
      backslashes), e.g. 'dvdPath=C:\\myDVDs\\'.
      Note: unless a name is explicitly given using the -d command line 
      switch when the input folder is either an unnamed dvd disc or the 
      dvd is multi-title, output is named '[YYYY-MM-DD_HHMM]_DVD'. 
   'isoPath=adjacent' (or any valid path)
      AUTHORING: globally redirects iso image output, same as above.
   'extractPath=adjacent' (or any valid path)
      EXTRACTION: globally redirects extraction output, same as above, 
      except the output folder name suffix will be '_UNPACKED'. 
   'workPath=[current user's temp folder]' (or any valid path)
      AUTHORING: globally redirects temporary workspace, same as above.
      
   'dvdDriveTo=[current user's home path]' (or any valid path)
      AUTHORING: - root output folder if input is from a disc in your 
      dvd drive. This initially defaults to your system's home directory 
      for the current user, something like 'dvdDriveTo=C:\\Documents and 
      Settings\\buffy\\', but you can edit it (using double backslashes) 
      to be any valid existing path, e.g. 
      'dvdDriveTo=C:\\myAudio\\Lpcm\\'. 
      EXTRACTION: same as above
----------------------------- LIMITATIONS ------------------------------
   This is a beta release, so the list below will likely grow along with 
   your scrutiny, and then hopefully shrink again with your patience. 
   
   1.  In Drag & drop mode input files will be ordered alphabetically 
       first by folder and then by file name. 
   2.  Dvd size is slightly overestimated (on the order 10-20 MB), so a 
       few borderline cases will end up falsely reported as being too 
       large for the given media. 
   3.  20-bit audio support, though in progress, is currently disabled 
       due to the scarcity of sources for conclusive testing. 
   4.  Multi (6+) channel processing is enabled, though not extensively 
       tested. 
      
   5.  Wave files must have canonical 44-byte headers only. If you're 
       experiencing a problem with wave files, try either stripping the 
       header of any extensions or converting to flac prior to authoring 
       with Lplex. 
   
   6.  There is no internal option for dvd menus per se. You can, of 
       course, navigate by pressing 'next', 'prev' and using the number 
       pad on your remote control. In multi-title dvds, pressing any 
       'menu' or 'disc' button will jump forward to the start of the 
       next titleset. 
      
   7.  You can, at your own discretion, generate menus using dvdStyler
        as follows:
      
       - NTSC users, please read the instructions at 
          regarding the
         dvdStyler ntsc-only player bug.
       - set 'create' to 'dvdstyler' and author the mpeg files using
         Lplex. 
       - open the resulting '..._dvdstyler.xml' file (located in the
         work folder) in dvdstyler, noting that Lplex's mpeg files are
         present in the timeline area at the bottom of the window.
       - design and add your menus, then create the dvd file structure
         with the 'file>burn dvd...>just generate' option checked.
       - after testing, move the resulting 'VIDEO_TS' and 'AUDIO_TS'
         folders to the '_DVD' folder already created by Lplex.
        
       - if applicable, trim the '_DVD' folder name to 32 bytes or less 
         to prevent a "Volume ID string too long" failure in the next 
         step. 
       - drop the '_DVD' folder onto the 'mkisofs.bat' utility (located 
         in Lplex's 'Util' subfolder) to generate the iso image.
   8.  Dual-layer images are not created correctly as yet, lacking a 
       properly defined layer break. You can, at your own discretion, 
       try the following workaround to create and burn a dl iso image
       (I haven't burn-tested this myself as yet): 
       - set 'create' to 'dvd' and 'media' to 'dl' or 'none' and 
         author the dvd file structure using Lplex. 
       - process the resulting 'VIDEO_TS' folder using the free tools
         PgcEdit and ImgBurn as described here: 
         
         (n.b. the 'Seamless layer break' section for continuous
         audio)
   9.  There is no facility for upsampling CD audio. You can, at your 
       own discretion, try the following free resamplers for this 
       purpose: 
       
       - SRCdrop  : 
       - Shibatch : 
       - WaveFS44 : 
       - r8brain  : 
   
   Good luck, and please report back with any results, corrections, or
   refinements regarding the above.
           
------------------------- COMMAND LINE USAGE ---------------------------
   Users familiar with a command-line interface can refer to the help 
   screen (type 'Lplex -h') for direct control over the dvd layout 
   (track order, additional titlesets, different types of lpcm on one 
   disc), and to override program defaults. Filepaths must use single 
   backslashes and should be enclosed in quotes if containing spaces, as 
   usual. 
   
-------------------------- ARCANE DISCUSSION ---------------------------
   PADDING AND PLAYBACK GAPS
   
      Dvd-video imposes a somewhat arbitrary minimum unit of an 'audio 
      frame' onto lpcm data, so unless padded the audio will end up 
      truncated to the nearest full audio frame. Padding ensures that no 
      audio is dropped, though a commensurately tiny (or gigantic 
      depending on your point of view) stretch of silence is introduced 
      as a result. The outside limit for audio frame-completion padding 
      is just short of 150 pts ticks, or 0.00167 seconds. 
      Another constraint is that the last frame of audio should occur 
      within the last frame of video, so that unless the frames happen 
      to be perfectly aligned, a playback gap occurs when audio has run 
      out but video has not, with naturally an outside limit of just 
      under one video frame (NTSC = 0.033 and PAL = 0.04 seconds). For 
      discontinuous audio this is tolerable, since there's a break 
      anyway, but there's an additional wrinkle. This discontinuity, for 
      reasons I confess I don't entirely understand, causes dvd playback 
      to pause for an additional half second over and above the 
      predicted gap. To entirely prevent any "gap" (in a strictly 
      technical sense) then, one would be forced to pad forward to the 
      next 'magic point' of perfect a/v alignment (see below). Although 
      there's no clear advantage to authoring this way as opposed to 
      allowing the combined gap and pause, Lplex will, at your option, 
      author discontinuous audio by either method ('discrete' or 
      'indiscrete' alignment). My own feeling is that it's better to use 
      'alignment=discrete' so as to avoid padding, since it won't be 
      separated out if extraction is done using any other utility. 
      
         NOTE: Lplex discards any padding during extraction, so 
         that audio data is ultimately unaltered.
      
      See the title layout tables section of the verbose output (or 
      'Lplex.log') for the precise gap and/or padding length at the end 
      of any given track. 
   SEAMLESS PLAYBACK
   
      For truly seamless playback of continuous audio Lplex's approach 
      is to recut the tracks at 'magic points' where both audio and 
      video frame boundries coincide (somewhat similar to sector 
      boundary alignment in cd-audio). In dvd time is measured in PTS 
      (Presentation Timestamp) ticks. Here's the math: 
      
         One second           = 90000 PTS ticks
         One lpcm audio frame =   150 PTS ticks
         One NTSC video frame =  3003 PTS ticks
         One PAL  video frame =  3600 PTS ticks
      
      so the system-specific 'magic' cut points are
      
         NTSC: every 50 video frames = 1001 audio frames = 1.66 seconds
         PAL : every  1 video frame  =   24 audio frames = 0.04 seconds
      
      and 'universal magic points' occur (unfortunately for us, 
      otherwise there would be an argument for permanently recutting 
      once and for all and not worrying about restoring later) at 
      
         every 1200 NTSC frames = 1001 PAL frames = 24024 audio frames
            = 40.04 seconds
         
      Lplex's 'seamless' alignment moves the track startpoints to an 
      immediate system-specific 'magic point' when authoring (which, to 
      my mind at least, is a tolerable shift), then restores the 
      original startpoints on extraction. 
   (THEORETICALLY) PERMITTED AUDIO
   
      According to http://www.mpeg.org/MPEG/DVD/Book_B/Audio.html, 
      dvd-video allows the following types of lpcm.  Lplex presently  
      attempts to support the varieties marked '+'. 
      
         Rate  Depth   :   Channels
         (khz) (bits)  :   1     2     5(+1)  8
         -------------------------------------------
         48       16   :   Yes+  Yes+  Yes+   Yes+
                  20   :   Yes   Yes   Yes    -
                  24   :   Yes+  Yes+  Yes+   -
         96       16   :   Yes+  Yes+  -      -
                  20   :   Yes   Yes   -      -
                  24   :   Yes+  Yes+  -      -
   LPLEX TAGS DESCRIPTION
   
      Lplex writes tags at two locations in every dvd program
         - PCI.RECI field (offset 0x343) of first nav packet
           (only if the tag fits within the space 0x400-0x343)
         - as a User Data field in first GOP of video
      
      The tags consist of the following two contiguous pieces:
      
      1. 42 byte flac header (STREAM, METADATA_BLOCK_HEADER and
         METADATA_BLOCK_STREAMINFO) describing the audio data
         extracted to wave-order from the dvd as-is. See
          for header details.
         
      2. Variable-length xml entity of the form
      
         
      
         where
            ver     : version of Lplex which wrote the tag.
            path    : path and filename under root folder to use for
                      output.
            id      : index relative to all authored tracks present on
                      the dvd.
            shift   : number of audio bytes to shift (+)in/(-)out at
                      track end to restore original pre-authored state.
            md5prev : 32 byte text-form md5 signature for track after
                      shift.
      
   
--------------------------- ACKNOWLEDGMENTS ----------------------------
   Lplex uses the open-source Linux authoring utilities
      mjpegtools : 
      dvdauthor  :  
      mkisofs    : 
   in win32 versions which run on the free cygwin POSIX emulator, all of 
   which are included in the 'Bin' program folder. The mjpegtools 
   utility mplex provided here is a recent cvs build, and also contains 
   an entirely inconsequential patch to implement a progress counter 
   (the current official release of mplex in mjpegtools-1.8.0 doesn't 
   handle lpcm data correctly). 
   Lplex was initially modeled on Dave Chapman's excellent open source 
   utility dvda-author , which creates 
   dvd-audio discs, and wav2lpcm, which converts lpcm from wave to dvd 
   order. Dvd-audio is admittedly superior to dvd-video in the sense 
   that it handles a far wider range of lpcm audio (from 16-bit 44khz 
   mono up to 24-bit 96khz 6ch and 24-bit 192khz 2ch), but has the one 
   drawback of requiring a dvd-audio player for playback, which in the 
   real world remains a relatively rare bird. Dvd-video players are 
   everywhere, on the other hand, and offer identical digital quality 
   for their narrower range of supported lpcm audio types, which is 
   where Lplex comes in. Dvd navigation and extraction was modeled 
   primarily on dvdunauthor by Scott Smith and Ralf Engels, another 
   great open source utility. 
   
   Software dependencies:
      libdvdread : 
      flac api   : 
      'md5.c'    : 
      wxwidgets  : 
      libjpeg    : 
      gnugetopt  : The GNU C Library
   
   The executables were compressed using upx : .