%%%%%%%%%%%%% flashmovie.sty
%%%%%%%%%%%%% Copyright 2010 Timo Hartmann (thartmann15 at googlemail.com)
%
% Version 0.4
%
% changelog:
%   0.4 : 2010-09-13 : better support for jw-player, minor bug fixes
%   0.3 : 2010-09-09 : better examples,minor bug fixes, issues with beamer explained
%   0.2 : 2010-04-21 : major overhaul: keyval 'engine', flv-player-engine,
%                                      efficient multiple embedding of files,...
%   0.1 : 2010-04-13 : a bunch of bugfixes
%   0.0 : 2009-12-06 : initial release
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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 license applies to following files:
%   flashmovie.sty,test.tex,test-flv.tex,README
%
%
%
% The file "player_flv_maxi.swf" is NOT from me. This is the original flv-player 
% from http://flv-player.net whose developer is neolao. 
% This file is distributed under the MPL-license. See the directory 
% "flv-player-license" for more information. The file "license.txt" in that directory
% is from the original source-code of the flv-player. A copy of the MPL-license is also
% included there.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% special thanks to following people for providing support, tips and improvements:
%  - Elie Roux
%  - Marco Aur��lio Graciotto Silva
%  - neolao
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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.
%
% usage:
%   There are two fundamental different ways to embed videos via the
%   flash-component of the Adobe Reader:
%     - One can create a ".swf"-file and insert this directly
%       into the pdf. See example (1) below.
%     - One can create a ".mp4"-file and use that file as an input
%       to a flash-video-player which renders the videos.
%       Both files (the video and the flash-video-player) will be
%       embedded into the pdf. See example(2) below.
%       Different video formats (mp4/flv/whatever-the-flash-video-player-accepts/...)
%       can be used. Controls are available (depending on the player).
%       See example (2) below.
%   Using the keyword "engine" one can select the method of embedding.
%   If you omit it or if you specify "engine=pure-swf" then the file will be
%   directly embedded. This works only for swf-files.
%   For other possible values of "engine" see the chapter "flash-video-players".
%
% supported file formats:
%   - flash animations ( ".swf" files) via direct embedding
%   - mp4 movies (".mp4" files) via an embedded flash-video-player
%   - flv movies (".flv" files) via an embedded flash-video-player
%
%   attention:
%     - avi videos (".avi" files) don't work !
%
% flash-video-players:
%   There are several flash-video-players available which fit the needs
%   of flashmovie.sty. The different players can be selected with the key "engine",
%   for example: "\flashmovie[width=10cm,height=10cm,engine=flv-player]{saturn5.mp4}".
%
%   The use of "engine=flv-player" is recommended, because it is supported best and
%   the flv-player is included in this distribution. But there seems to be some
%   incompatibilities of flv-player and beamer. See troubleshooting below.
%
%   List of flash-video-players:
%
%    - "engine=flv-player". This is an open-source player from http://flv-player.net.
%      It is included with this distribution (the file player_flv_maxi.swf).
%      The developer is neolao. See the directory flv-player-license for the license of it.
%      This is the player which flashmovie.sty supports best.
%
%      supported parameters:
%        auto=0                   --> auto-play off (default)
%        auto=1                   --> auto-play on
%        image=someimage.jpeg     --> start-image (not necessary)
%
%    - "engine=jw-player". This is the JW player from http://www.longtailvideo.com/players/jw-flv-player.
%      You have to obtain player.swf from that site and put it in the working directory in order
%      to use this module. Its released under a creative common license which means its free
%      for non-commercial use.
%      It is NOT included in this distribution.
%
%      supported parameters:
%        auto=0                   --> auto-play off (default)
%        auto=1                   --> auto-play on
%        image=someimage.jpeg     --> start-image (not necessary)
%        controlbar=0             --> no control bar shown
%        controlbar=1             --> control bar shown (default)
%        loop=0                   --> do not loop the video (default)
%        loop=1                   --> loop the video
%
%    - "engine=acropro". This is an commercial player which is distributed with
%      Acrobat 9 Pro. You have to obtain VideoPlayer.swf and SkinOverPlaySeekStop.swf 
%      from Acrobat 9 Pro and put them into the working directory in order to use that module.
%      It is NOT included in this distribution.
%      For this player flashmovie.sty only provides rudimentary support.
%    - "engine=osplayer". This is an open-source player from http://www.osflv.com
%      You have to obtain OSplayer.swf from that site and put it in the working directory in order
%      to use this module.
%      It is NOT included in this distribution.
%      For this player flashmovie.sty only provides rudimentary support.
%
%   other options for the "engine"-keyword:
%    - "engine=pure-swf" : This directly embedds the file into the PDF without 
%      any flash-video-player. This is the default option when you omit the 
%      "engine"-keyword. This works only for swf-files.
%    - "engine=blank" : This simply produces an empty rectangle instead of an
%      flash animation. This is useful during the creation process of the 
%      PDF because flash animations inside a PDF can easily crash 
%      the Adobe reader with the reload (CTRL-R) feature after recompiling
%      the tex-file. 
%      The same effect can be produced if uses "blank=1" as an option 
%      regardless what the value of "engine" is.
%      example: \flashmovie[...,engine=flv-player,blank=1,...]{...}
%
%
% 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 capable of this, for example mencoder)
%   - put following line into your tex-file:
%        \flashmovie[width=10cm,height=10cm]{saturn5.swf}
%     (both width and height are needed!)
%   - translate with pdflatex
%   - advantages:
%     - easy
%     - does not rely on external components
%   - disadvantages:
%     - ffmpeg is incapable 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)
%   - 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 capable 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:
%        \flashmovie[width=10cm,height=10cm,engine=flv-player,auto=1]{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
%
% how to produce swf-files:
%   - ffmpeg or mencoder : transform a video file into a flash animation (example: .mp4 --> .swf)
%   - png2swf : transform a sequence of png files into a flash animation (example: .png --> .swf)
%   - flex sdk : use action script to program your own flash applet
%   - mtasc and swfmill : use action script to program your own flash applet (outdated, should not be used)
%
% todo:
%   - make more options of rich-media annotations usable, for example activation by clicking, play-count,...
%
% tips:
%   - It is in principle possible to insert any flash animation into a PDF.
%     One is not restricted to videos.
%   - During development of the PDF one can switch of the flash animations which
%     is sometimes useful. See "engine=blank" or "blank=1" for more information.
%
% troubleshooting:
%   - If you encounter the error message "\pdfminorversion cannot be changed after data is written to the PDF file.",
%     then another package is also setting the variable "\pdfminorversion".
%     For example the package "beamer" does so.
%     There are two solutions: 
%      - Include the package with the highest "\pdfminorversion" first.
%        For example put "\RequirePackage{flashmovie}" on the very first line 
%        of your file instead of using "\usepackage{flashmovie}" later on.
%        Or rearrange the usepackage-commands in the right order.
%      - Comment the line "\ifnum\pdfminorversion<7 \pdfminorversion=7\fi" in
%        "flashmovie.sty". This will probably leave your PDF with the wrong 
%        version information, but the Adobe Reader will take it anyway.
%   - There seems to be some trouble with the latex package beamer. You can't use
%     the flv-player reliably with it. It is prone to crash the acrobat reader while
%     changing pages.
%     The only reliable way to use beamer seems to be directly embedding the videos as
%     flash animations or to use the JW player.
%
% warning:
%   - It is recommended to use always the latest available version of the Adobe Reader. 
%     This should be mandatory because in the past many attacks on computers were based
%     on 'evil' PDFs which exploited security holes in this software.
%   - 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}
\RequirePackage{ifthen}

\ifnum\pdfminorversion<7 \pdfminorversion=7\fi

\def\flashmovieempty{}
\def\flashmoviezero{0}
\def\flashmovieembedded{}

\def\flashmovieparamsclear{
  \def\flashmoviewidth {4cm}
  \def\flashmovieheight{4cm}
  \def\flashmovieimage{}
  \def\flashmovieauto{0}
  \def\flashmovieengine{}
  \def\flashmoviekblank{0}
  \def\flashmoviecontrolbar{1}
  \def\flashmovieloop{0}
}

\define@key{flashmovieparams}{width}      {\def\flashmoviewidth  {#1}}
\define@key{flashmovieparams}{height}     {\def\flashmovieheight {#1}}
\define@key{flashmovieparams}{image}      {\def\flashmovieimage  {#1}}
\define@key{flashmovieparams}{auto}       {\def\flashmovieauto   {#1}}
\define@key{flashmovieparams}{engine}     {\def\flashmovieengine {#1}}
\define@key{flashmovieparams}{blank}      {\def\flashmoviekblank {#1}}
\define@key{flashmovieparams}{controlbar} {\def\flashmoviecontrolbar{#1}}
\define@key{flashmovieparams}{loop}       {\def\flashmovieloop{#1}}

\newlength{\flashmovie@width}
\newlength{\flashmovie@height}
\newsavebox{\flashmovie@content}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% variable module %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \flashmovieset{a}{b}   is equivalent to \def\a{b}
% \flashmovieget{a}      is equivalent to \a if a is defined and otherwise it is empty
%
% example:
%   \flashmovieset{aaa}{bla} \flashmovieget{aaa}     ------> produces bla
%                            \flashmovieget{bbb}     ------> produces nothing if bbb is not defined

\def\flashmovieset#1#2{{\expandafter\xdef\csname flashmovie@f@#1\endcsname{#2}}}

% warning: command must be on one line so that there are no spaces generated !
\def\flashmovieget#1{\expandafter\ifx\csname flashmovie@f@#1\endcsname\relax\expandafter\flashmovieempty\else\csname flashmovie@f@#1\endcsname\fi}

%simpler:
%  \def\flashmovieset#1#2{{\expandafter\xdef\csname#1\endcsname{#2}}}
%  \def\flashmovieget#1{\expandafter\ifx\csname#1\endcsname\relax\expandafter\flashmovieempty\else\csname#1\endcsname\fi}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%% file embedding module %%%%%%%%%%%%%%%%%%%%%%%%
%
% usage:
%    \flashmovieembedfile{somefile}
%    \edef\flashmovie@e@somefile{\flashmovieembedded}

\def\flashmovieembedfileinternal#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\flashmovieembedfile#1{%
  \ifthenelse{\equal{\flashmovieget{\pdfmdfivesum file {#1}}}{\flashmovieempty}}{%
    \flashmovieembedfileinternal{#1}%
    \def\flashmovieembedded{\the\pdflastobj}%
    \flashmovieset{\pdfmdfivesum file {#1}}{\flashmovieembedded}%
  }{%
    \def\flashmovieembedded{\flashmovieget{\pdfmdfivesum file {#1}}}%
  }%
}

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

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

\def\@flashmovie[#1]#2{\flashmovieparamsclear \setkeys{flashmovieparams}{#1}{

  \ifthenelse{\equal{\flashmoviekblank}{\flashmoviezero}}
   {}{\def\flashmovieengine{blank}}

  \ifx\flashmovieengine\flashmovieempty
    \flashmovie@pureswf[#1]{#2}
  \else

    \ifthenelse{\equal{\flashmovieengine}{pure-swf}}
    {\flashmovie@pureswf[#1]{#2}}{}

    \ifthenelse{\equal{\flashmovieengine}{flv-player}}
    {\flashmovie@flvplayer[#1]{#2}}{}

    \ifthenelse{\equal{\flashmovieengine}{acropro}}
    {\flashmovie@acropro[#1]{#2}}{}

    \ifthenelse{\equal{\flashmovieengine}{jw-player}}
    {\flashmovie@jwplayer[#1]{#2}}{}

    \ifthenelse{\equal{\flashmovieengine}{osplayer}}
    {\flashmovie@osplayer[#1]{#2}}{}

    \ifthenelse{\equal{\flashmovieengine}{blank}}
    {\flashmovie@blank[#1]{#2}}{}

  \fi
}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pure swf

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

\def\@flashmovie@pureswf[#1]#2{\flashmovieparamsclear \setkeys{flashmovieparams}{#1}{
  \newcount\flashmovie@n@config
  \newcount\flashmovie@n@content
  \newcount\flashmovie@n@settings

  \flashmovieembedfile{#2}
  \edef\flashmovie@e@video{\flashmovieembedded}

  \immediate
  \pdfobj
    {<<
      /Instances
        [<<
          /Asset \flashmovie@e@video\space 0 R
          /Params << /Binding /Foreground >>
        >>]
      /Subtype /Flash
    >>}
  \flashmovie@n@config=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Assets << /Names [(#2) \flashmovie@e@video\space 0 R] >>
      /Configurations [\the\flashmovie@n@config\space 0 R]
    >>}
  \flashmovie@n@content=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Activation 
        << /Type /RichMediaActivation 
           /Condition /PV
           /Configuration \the\flashmovie@n@config\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 /PI
        >> 
    >>}
  \flashmovie@n@settings=\pdflastobj

  \sbox{\flashmovie@content} {{\hbox to \flashmoviewidth {\vbox to \flashmovieheight { }}}}

  \settowidth {\flashmovie@width} {\usebox{\flashmovie@content}}
  \settoheight{\flashmovie@height}{\usebox{\flashmovie@content}}
  \usebox{\flashmovie@content}

  \pdfannot width \flashmovie@width height \flashmovie@height depth 0pt {
       /Subtype /RichMedia
       /RichMediaContent  \the\flashmovie@n@content\space 0 R 
       /RichMediaSettings \the\flashmovie@n@settings\space 0 R 
}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% blank

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

\def\@flashmovie@blank[#1]#2{\flashmovieparamsclear \setkeys{flashmovieparams}{#1}{
  \sbox{\flashmovie@content} {{\hbox to \flashmoviewidth {\vbox to \flashmovieheight { }}}}
  \settowidth {\flashmovie@width} {\usebox{\flashmovie@content}}
  \settoheight{\flashmovie@height}{\usebox{\flashmovie@content}}
  \usebox{\flashmovie@content}
}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% player from adobe acrobat pro

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

\def\@flashmovie@acropro[#1]#2{\flashmovieparamsclear \setkeys{flashmovieparams}{#1}{
  \newcount\flashmovie@n@config
  \newcount\flashmovie@n@content
  \newcount\flashmovie@n@settings

  \flashmovieembedfile{#2}
  \edef\flashmovie@e@video{\flashmovieembedded}

  \flashmovieembedfile{SkinOverPlaySeekStop.swf}
  \edef\flashmovie@e@skin{\flashmovieembedded}

  \flashmovieembedfile{VideoPlayer.swf}
  \edef\flashmovie@e@player{\flashmovieembedded}
  
  \immediate
  \pdfobj
    {<<
      /Instances
        [<<
          /Asset \flashmovie@e@player\space 0 R
          /Params 
            << 
               /Binding /Foreground
               /FlashVars (source=#2&skin=SkinOverPlaySeekStop.swf&skinAutoHide=true&skinBackgroundColor=0x00FF00&skinBackgroundAlpha=0.33&volume=1.00)
            >>
        >>]
      /Subtype /Flash
    >>}
  \flashmovie@n@config=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Assets << /Names [(#2) \flashmovie@e@video\space 0 R (skinover.swf) \flashmovie@e@skin\space 0 R (VideoPlayer.swf) \flashmovie@e@player\space 0 R] >>
      /Configurations [\the\flashmovie@n@config\space 0 R]
    >>}
  \flashmovie@n@content=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Activation 
        << /Type /RichMediaActivation 
           /Condition /PV
           /Configuration \the\flashmovie@n@config\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 /PI
        >> 
    >>}
  \flashmovie@n@settings=\pdflastobj

  \sbox{\flashmovie@content} {{\hbox to \flashmoviewidth {\vbox to \flashmovieheight { }}}}

  \settowidth {\flashmovie@width} {\usebox{\flashmovie@content}}
  \settoheight{\flashmovie@height}{\usebox{\flashmovie@content}}
  \usebox{\flashmovie@content}

  \pdfannot width \flashmovie@width height \flashmovie@height depth 0pt {
       /Subtype /RichMedia
       /RichMediaContent  \the\flashmovie@n@content\space 0 R 
       /RichMediaSettings \the\flashmovie@n@settings\space 0 R 
}}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% flv-player from http://flv-player.net/players/maxi

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

\def\@flashmovie@flvplayer[#1]#2{\flashmovieparamsclear \setkeys{flashmovieparams}{#1}{
  \newcount\flashmovie@n@config
  \newcount\flashmovie@n@content
  \newcount\flashmovie@n@settings

  \flashmovieembedfile{#2}
  \edef\flashmovie@e@video{\flashmovieembedded}

  \flashmovieembedfile{player_flv_maxi.swf}
  \edef\flashmovie@e@player{\flashmovieembedded}

  % startimage is initialized here
  \ifx\flashmovieimage\flashmovieempty
    \def\flashmovieimagevari{}
    \def\flashmovieimagevarii{}
  \else
    \flashmovieembedfile{\flashmovieimage}
    \edef\flashmovie@e@image{\flashmovieembedded}
    \def\flashmovieimagevari{&startimage=\flashmovieimage}
    \def\flashmovieimagevarii{(\flashmovieimage) \flashmovie@e@image\space 0 R}
  \fi

  % autoplay is initialized here
  \ifx\flashmovieauto\flashmoviezero
    \def\flashmovieautovari{}
  \else
    \def\flashmovieautovari{&autoplay=1}
  \fi

  % looping is initialized here
  \ifx\flashmovieloop\flashmoviezero
    \def\flashmovieloopvari{&loop=0}
  \else
    \def\flashmovieloopvari{&loop=1}
  \fi

  \immediate
  \pdfobj
    {<<
      /Instances
        [<<
          /Asset \flashmovie@e@player\space 0 R
          /Params 
            << 
               /Binding /Foreground
               /FlashVars (flv=#2&margin=0&showvolume=1&showstop=1&showtime=1&videobgcolor=0xFFFFFF\flashmovieautovari\flashmovieimagevari\flashmovieloopvari)
            >>
        >>]
      /Subtype /Flash
    >>}
  \flashmovie@n@config=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Assets << /Names 
       [(#2) \flashmovie@e@video\space 0 R (player_flv_maxi.swf) \flashmovie@e@player\space 0 R \flashmovieimagevarii] >>
      /Configurations [\the\flashmovie@n@config\space 0 R]
    >>}
  \flashmovie@n@content=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Activation 
        << /Type /RichMediaActivation 
           /Condition /PV
           /Configuration \the\flashmovie@n@config\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 /PI
        >> 
    >>}
  \flashmovie@n@settings=\pdflastobj

  \sbox{\flashmovie@content} {{\hbox to \flashmoviewidth {\vbox to \flashmovieheight { }}}}

  \settowidth {\flashmovie@width} {\usebox{\flashmovie@content}}
  \settoheight{\flashmovie@height}{\usebox{\flashmovie@content}}
  \usebox{\flashmovie@content}

  \pdfannot width \flashmovie@width height \flashmovie@height depth 0pt {
       /Subtype /RichMedia
       /RichMediaContent  \the\flashmovie@n@content\space 0 R 
       /RichMediaSettings \the\flashmovie@n@settings\space 0 R }
}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% JW Player from http://www.longtailvideo.com/players/jw-flv-player/

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

\def\@flashmovie@jwplayer[#1]#2{\flashmovieparamsclear \setkeys{flashmovieparams}{#1}{
  \newcount\flashmovie@n@config
  \newcount\flashmovie@n@content
  \newcount\flashmovie@n@settings

  \flashmovieembedfile{#2}
  \edef\flashmovie@e@video{\flashmovieembedded}

  \flashmovieembedfile{player.swf}
  \edef\flashmovie@e@player{\flashmovieembedded}

  % startimage is initialized here
  \ifx\flashmovieimage\flashmovieempty
    \def\flashmovieimagevari{}
    \def\flashmovieimagevarii{}
  \else
    \flashmovieembedfile{\flashmovieimage}
    \edef\flashmovie@e@image{\flashmovieembedded}
    \def\flashmovieimagevari{&image=\flashmovieimage}
    \def\flashmovieimagevarii{(\flashmovieimage) \flashmovie@e@image\space 0 R}
  \fi

  % autoplay is initialized here
  \ifx\flashmovieauto\flashmoviezero
    \def\flashmovieautovari{}
  \else
    \def\flashmovieautovari{&autostart=true}
  \fi

  % controlbar is initialized here
  \ifx\flashmoviecontrolbar\flashmoviezero
    \def\flashmoviecontrolbarvari{&controlbar=none}
  \else
    \def\flashmoviecontrolbarvari{}
  \fi

  % looping is initialized here
  \ifx\flashmovieloop\flashmoviezero
    \def\flashmovieloopvari{}
  \else
    \def\flashmovieloopvari{&repeat=always}
  \fi

  \immediate
  \pdfobj
    {<<
      /Instances
        [<<
          /Asset \flashmovie@e@player\space 0 R
          /Params 
            << 
               /Binding /Foreground
               /FlashVars (file=#2\flashmovieimagevari\flashmovieautovari\flashmoviecontrolbarvari\flashmovieloopvari)
            >>
        >>]
      /Subtype /Flash
    >>}
  \flashmovie@n@config=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Assets << /Names 
       [(#2) \flashmovie@e@video\space 0 R (player.swf) \flashmovie@e@player\space 0 R \flashmovieimagevarii] >>
      /Configurations [\the\flashmovie@n@config\space 0 R]
    >>}
  \flashmovie@n@content=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Activation 
        << /Type /RichMediaActivation 
           /Condition /PV
           /Configuration \the\flashmovie@n@config\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 /PI
        >> 
    >>}
  \flashmovie@n@settings=\pdflastobj

  \sbox{\flashmovie@content} {{\hbox to \flashmoviewidth {\vbox to \flashmovieheight { }}}}

  \settowidth {\flashmovie@width} {\usebox{\flashmovie@content}}
  \settoheight{\flashmovie@height}{\usebox{\flashmovie@content}}
  \usebox{\flashmovie@content}

  \pdfannot width \flashmovie@width height \flashmovie@height depth 0pt {
       /Subtype /RichMedia
       /RichMediaContent  \the\flashmovie@n@content\space 0 R 
       /RichMediaSettings \the\flashmovie@n@settings\space 0 R }
}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% OSFlvPlayer from http://www.osflv.com

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

\def\@flashmovie@osplayer[#1]#2{\flashmovieparamsclear \setkeys{flashmovieparams}{#1}{
  \newcount\flashmovie@n@config
  \newcount\flashmovie@n@content
  \newcount\flashmovie@n@settings

  \flashmovieembedfile{#2}
  \edef\flashmovie@e@video{\flashmovieembedded}

  \flashmovieembedfile{OSplayer.swf}
  \edef\flashmovie@e@player{\flashmovieembedded}

  \immediate
  \pdfobj
    {<<
      /Instances
        [<<
          /Asset \flashmovie@e@player\space 0 R
          /Params 
            << 
               /Binding /Foreground
               /FlashVars (movie=#2)
            >>
        >>]
      /Subtype /Flash
    >>}
  \flashmovie@n@config=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Assets << /Names 
       [(#2) \flashmovie@e@video\space 0 R (OSplayer.swf) \flashmovie@e@player\space 0 R] >>
      /Configurations [\the\flashmovie@n@config\space 0 R]
    >>}
  \flashmovie@n@content=\pdflastobj

  \immediate
  \pdfobj
    {<<
      /Activation 
        << /Type /RichMediaActivation 
           /Condition /PV
           /Configuration \the\flashmovie@n@config\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 /PI
        >> 
    >>}
  \flashmovie@n@settings=\pdflastobj

  \sbox{\flashmovie@content} {{\hbox to \flashmoviewidth {\vbox to \flashmovieheight { }}}}

  \settowidth {\flashmovie@width} {\usebox{\flashmovie@content}}
  \settoheight{\flashmovie@height}{\usebox{\flashmovie@content}}
  \usebox{\flashmovie@content}

  \pdfannot width \flashmovie@width height \flashmovie@height depth 0pt {
       /Subtype /RichMedia
       /RichMediaContent  \the\flashmovie@n@content\space 0 R 
       /RichMediaSettings \the\flashmovie@n@settings\space 0 R }
}}