%%%%%%%%%%%%% flashmovie.sty
%%%%%%%%%%%%% Copyright 2009 Timo Hartmann
%
% Version 0.0
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
% http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This package allows direct embedding of flash movies into PDF files. It is
% designed for use with pdflatex.
%
% Basically it uses the fact that the Adobe Reader 9 contains an embedded Adobe Flash 
% player which can be invoked with the "rich media annotation" feature which is described 
% in "Adobe Supplement to the ISO 32000 BaseVersion: 1.7 ExtensionLevel: 3".
%
% advantages:
%   - You can directly embed videos into the pdf (external window is optimal and not necessary).
%   - You can play back videos without worrying about platform dependencies.
%
% disadvantages:
%   - You can only play back embedded videos with Adobe Reader 9 and upwards.
%
% example (1)
%   - download a video file to use, for example
%        http://heasarc.gsfc.nasa.gov/Videos/historical/saturn5.avi
%   - transform this video into the flash format with ffmpeg:
%        ffmpeg -i saturn5.avi saturn5.swf
%     (or use any other program captable of this, for example mencoder)
%   - put following line into your tex-file:
%        \flashmovie[width=10cm,heigth=10cm]{saturn5.swf}
%     (both width and height are needed!)
%   - translate with pdflatex
%   - advantages:
%     - easy
%     - does not reley on external compontents
%   - disadvantages:
%     - ffmpeg is incaptable of embedding h.264 into swf-files, so one can only 
%       use inferior video codecs (sorenson h.263 aka flv,...).
%     - no controls (play,pause,stop,seek,...) possible
%
% example (2)
%   - warning: The following example assumes that you have obainted VideoPlayer.swf
%     and SkinOverPlaySeekStop.swf from Acrobat 9 Pro and put it into the working
%     directory.
%   - download a video file to use, for example
%        http://heasarc.gsfc.nasa.gov/Videos/historical/saturn5.avi
%   - transform this video into the mp4 format with ffmpeg:
%        ffmpeg -i saturn5.avi -vcodec libx264 -vpre hq -crf 22 -acodec libfaac -ab 128k -ac 2 saturn5.mp4
%     (or use any other program captable of this, for example mencoder)
%   - alternatively the flv-format with video-codec sorenson h.263 (aka flv) and audio-codec mp3 is also possible:
%       mencoder saturn5.avi -o saturn5.flv -of lavf -ovc lavc -lavcopts vcodec=flv:vbitrate=300:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -srate 22050 -ofps 24 -vf harddup -oac mp3lame -lameopts abr:br=56 )
%     (or use ffmpeg to do the same thing...)
%   - put following line into your tex-file:
%        \flashmoviex[width=10cm,heigth=10cm]{saturn5.mp4}
%     (both width and height are needed! instead of saturn5.mp4 one can also use saturn5.flv but not saturn5.swf)
%   - translate with pdflatex
%   - advantages:
%     - controls (play,pause,stop,seek,...) possible
%     - superior video codec possible (h.264)
%     - one can directly embed mp4 and flv files without conversion
%   - disadvantages:
%     - use of external non-free components.
%
% todo:
%   - replace VideoPlayer.swf and SkinOverPlaySeekStop.swf by some freely available software.
%     it should be possible to program a simple video player with flexsdk and activescript.
%     the videoplayer should play mp4 and flv files and should and be captable of play,pause,seek,stop.
%     controls should be hidable.
%   - make more options of richmedia annotations usable, for example activation by clicking, playcount,...
%   - display a picture when the player is deactivated.
%   - efficient multiple inclusion of video files.
%
% warning:
%   - Because the file names of the movies are directly embedded into the PDFs please use only
%     "normal" file names. More specific use only names compatible with internal PDF strings.
%     There is even a specification what conforming file names are (see the PDF reference).

\RequirePackage{keyval}

\def\flashmovieparamsclear{
  \def\flashmoviewidth {4cm}
  \def\flashmovieheight{4cm}
}

\define@key{flashmovieparams}{width}  {\def\flashmoviewidth  {#1}}
\define@key{flashmovieparams}{heigth} {\def\flashmovieheight {#1}}

\def\flashmovieembedfile#1{
  \immediate
  \pdfobj stream
     attr {  /Type/EmbeddedFile  }
     file {#1}
  \immediate
  \pdfobj { << 
      /Type /Filespec
      /F  (#1)
      /UF (#1)
      /EF << /F \the\pdflastobj\space 0 R >>
    >>}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\flashmovie{\@ifnextchar[\@flashmovie{\@flashmovie[]}}

\def\@flashmovie[#1]#2{\flashmovieparamsclear \setkeys{flashmovieparams}{#1}{
  \newcount\filespecnum
  \newcount\configurationnum
  \newcount\contentnum
  \newcount\settingsnum

  \flashmovieembedfile{#2}
  \filespecnum=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Instances
        [<<
          /Asset \the\filespecnum\space 0 R
          /Params << /Binding /Foreground >>
        >>]
      /Subtype /Flash
    >>}
  \configurationnum=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Assets << /Names [(#2) \the\filespecnum\space 0 R] >>
      /Configurations [\the\configurationnum\space 0 R]
    >>}
  \contentnum=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Activation 
        << /Type /RichMediaActivation 
           /Condition /PO 
           /Configuration \the\configurationnum\space 0 R 
           /Animation 
             << /Subtype /Linear 
                /Speed 1 
                /Playcount 1 
             >> 
           /Presentation
             << /PassContextClick false
                /Style /Embedded 
                /Toolbar false
                /NavigationPane false
                /Transparent true
                /Window 
                  << /Type /RichMediaWindow 
                     /Width  << /Default 100 /Min 100 /Max 100 >> 
                     /Height << /Default 100 /Min 100 /Max 100 >> 
                     /Position 
                       << /Type /RichMediaPosition 
                          /HAlign /Near 
                          /VAlign /Near 
                          /HOffset 0 
                          /VOffset 0 
                       >> 
                  >> 
              >> 
        >> 
      /Deactivation 
        << /Type /RichMediaDeactivation 
           /Condition /XD 
        >> 
    >>}
  \settingsnum=\pdflastobj

  \newlength{\xxwidth}
  \newlength{\xxheight}
  \newsavebox{\xxcontent}

  \sbox{\xxcontent} {{\hbox to \flashmoviewidth {\vbox to \flashmovieheight { }}}}

  \settowidth {\xxwidth} {\usebox{\xxcontent}}%
  \settoheight{\xxheight}{\usebox{\xxcontent}}%
  \usebox{\xxcontent}%

  \pdfannot width \xxwidth height \xxheight depth 0pt {%
       /Subtype /RichMedia
       /RichMediaContent  \the\contentnum\space 0 R 
       /RichMediaSettings \the\settingsnum\space 0 R 
}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\def\flashmoviex{\@ifnextchar[\@flashmoviex{\@flashmoviex[]}}

\def\@flashmoviex[#1]#2{\flashmovieparamsclear \setkeys{flashmovieparams}{#1}{
  \newcount\filespecnum
  \newcount\filespecnumi
  \newcount\filespecnumii
  \newcount\configurationnum
  \newcount\contentnum
  \newcount\settingsnum

  \flashmovieembedfile{#2}
  \filespecnum=\pdflastobj

  \flashmovieembedfile{SkinOverPlaySeekStop.swf}
  \filespecnumi=\pdflastobj

  \flashmovieembedfile{VideoPlayer.swf}
  \filespecnumii=\pdflastobj
  
  \immediate
  \pdfobj
    {<<
      /Instances
        [<<
          /Asset \the\filespecnumii\space 0 R
          /Params 
            << 
               /Binding /Foreground
               /FlashVars (source=#2&skin=SkinOverPlaySeekStop.swf&skinAutoHide=true&skinBackgroundColor=0x00FF00&skinBackgroundAlpha=0.33&volume=1.00)
            >>
        >>]
      /Subtype /Flash
    >>}
  \configurationnum=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Assets << /Names [(#2) \the\filespecnum\space 0 R (skinover.swf) \the\filespecnumi\space 0 R (VideoPlayer.swf) \the\filespecnumii\space 0 R] >>
      /Configurations [\the\configurationnum\space 0 R]
    >>}
  \contentnum=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Activation 
        << /Type /RichMediaActivation 
           /Condition /PO 
           /Configuration \the\configurationnum\space 0 R 
           /Animation 
             << /Subtype /Linear 
                /Speed 1 
                /Playcount 1 
             >> 
           /Presentation
             << /PassContextClick false
                /Style /Embedded 
                /Toolbar false
                /NavigationPane false
                /Transparent true
                /Window 
                  << /Type /RichMediaWindow 
                     /Width  << /Default 100 /Min 100 /Max 100 >> 
                     /Height << /Default 100 /Min 100 /Max 100 >> 
                     /Position 
                       << /Type /RichMediaPosition 
                          /HAlign /Near 
                          /VAlign /Near 
                          /HOffset 0 
                          /VOffset 0 
                       >> 
                  >> 
              >> 
        >> 
      /Deactivation 
        << /Type /RichMediaDeactivation 
           /Condition /XD 
        >> 
    >>}
  \settingsnum=\pdflastobj

  \newlength{\xxwidth}
  \newlength{\xxheight}
  \newsavebox{\xxcontent}

  \sbox{\xxcontent} {{\hbox to \flashmoviewidth {\vbox to \flashmovieheight { }}}}

  \settowidth {\xxwidth} {\usebox{\xxcontent}}%
  \settoheight{\xxheight}{\usebox{\xxcontent}}%
  \usebox{\xxcontent}%

  \pdfannot width \xxwidth height \xxheight depth 0pt {%
       /Subtype /RichMedia
       /RichMediaContent  \the\contentnum\space 0 R 
       /RichMediaSettings \the\settingsnum\space 0 R 
}}}