{-# LINE 1 "src/Codec/FFmpeg/Probe.hsc" #-}
{-# LANGUAGE
    ForeignFunctionInterface,
    GeneralizedNewtypeDeriving
    #-}

module Codec.FFmpeg.Probe (
    -- * Files
    withAvFile, nbStreams, formatName, formatMetadata, duration,

    -- * Streams
    AvStreamT, withStream, codecContext, codecName,
    codecMediaTypeName, streamBitrate, streamMetadata,
    codec, streamImageSize,

    -- * Dictionaries
    dictFoldM_
    ) where

import Control.Applicative ( Applicative )
import Control.Monad.Catch ( MonadMask, finally )
import Control.Monad.Reader
import Control.Monad.Trans.Except
import Data.Int ( Int64 )
import Foreign.C.String ( CString, peekCString, withCString )
import Foreign.C.Types ( CInt(..) )
import Foreign.Marshal.Utils ( with )
import Foreign.Ptr ( Ptr, nullPtr )
import Foreign.Storable

import Codec.FFmpeg.Enums
import Codec.FFmpeg.Decode
import Codec.FFmpeg.Types



-------------------------------------------------------------------------------
-- avformat - level stuff
-------------------------------------------------------------------------------

newtype AvFormat m a = AvFormat { AvFormat m a -> ReaderT AVFormatContext m a
unAvFormat :: ReaderT AVFormatContext m a }
    deriving
        ( Functor (AvFormat m)
a -> AvFormat m a
Functor (AvFormat m) =>
(forall a. a -> AvFormat m a)
-> (forall a b.
    AvFormat m (a -> b) -> AvFormat m a -> AvFormat m b)
-> (forall a b c.
    (a -> b -> c) -> AvFormat m a -> AvFormat m b -> AvFormat m c)
-> (forall a b. AvFormat m a -> AvFormat m b -> AvFormat m b)
-> (forall a b. AvFormat m a -> AvFormat m b -> AvFormat m a)
-> Applicative (AvFormat m)
AvFormat m a -> AvFormat m b -> AvFormat m b
AvFormat m a -> AvFormat m b -> AvFormat m a
AvFormat m (a -> b) -> AvFormat m a -> AvFormat m b
(a -> b -> c) -> AvFormat m a -> AvFormat m b -> AvFormat m c
forall a. a -> AvFormat m a
forall a b. AvFormat m a -> AvFormat m b -> AvFormat m a
forall a b. AvFormat m a -> AvFormat m b -> AvFormat m b
forall a b. AvFormat m (a -> b) -> AvFormat m a -> AvFormat m b
forall a b c.
(a -> b -> c) -> AvFormat m a -> AvFormat m b -> AvFormat m c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
forall (m :: * -> *). Applicative m => Functor (AvFormat m)
forall (m :: * -> *) a. Applicative m => a -> AvFormat m a
forall (m :: * -> *) a b.
Applicative m =>
AvFormat m a -> AvFormat m b -> AvFormat m a
forall (m :: * -> *) a b.
Applicative m =>
AvFormat m a -> AvFormat m b -> AvFormat m b
forall (m :: * -> *) a b.
Applicative m =>
AvFormat m (a -> b) -> AvFormat m a -> AvFormat m b
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> AvFormat m a -> AvFormat m b -> AvFormat m c
<* :: AvFormat m a -> AvFormat m b -> AvFormat m a
$c<* :: forall (m :: * -> *) a b.
Applicative m =>
AvFormat m a -> AvFormat m b -> AvFormat m a
*> :: AvFormat m a -> AvFormat m b -> AvFormat m b
$c*> :: forall (m :: * -> *) a b.
Applicative m =>
AvFormat m a -> AvFormat m b -> AvFormat m b
liftA2 :: (a -> b -> c) -> AvFormat m a -> AvFormat m b -> AvFormat m c
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> AvFormat m a -> AvFormat m b -> AvFormat m c
<*> :: AvFormat m (a -> b) -> AvFormat m a -> AvFormat m b
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
AvFormat m (a -> b) -> AvFormat m a -> AvFormat m b
pure :: a -> AvFormat m a
$cpure :: forall (m :: * -> *) a. Applicative m => a -> AvFormat m a
$cp1Applicative :: forall (m :: * -> *). Applicative m => Functor (AvFormat m)
Applicative
        , a -> AvFormat m b -> AvFormat m a
(a -> b) -> AvFormat m a -> AvFormat m b
(forall a b. (a -> b) -> AvFormat m a -> AvFormat m b)
-> (forall a b. a -> AvFormat m b -> AvFormat m a)
-> Functor (AvFormat m)
forall a b. a -> AvFormat m b -> AvFormat m a
forall a b. (a -> b) -> AvFormat m a -> AvFormat m b
forall (m :: * -> *) a b.
Functor m =>
a -> AvFormat m b -> AvFormat m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> AvFormat m a -> AvFormat m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> AvFormat m b -> AvFormat m a
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> AvFormat m b -> AvFormat m a
fmap :: (a -> b) -> AvFormat m a -> AvFormat m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> AvFormat m a -> AvFormat m b
Functor
        , Applicative (AvFormat m)
a -> AvFormat m a
Applicative (AvFormat m) =>
(forall a b. AvFormat m a -> (a -> AvFormat m b) -> AvFormat m b)
-> (forall a b. AvFormat m a -> AvFormat m b -> AvFormat m b)
-> (forall a. a -> AvFormat m a)
-> Monad (AvFormat m)
AvFormat m a -> (a -> AvFormat m b) -> AvFormat m b
AvFormat m a -> AvFormat m b -> AvFormat m b
forall a. a -> AvFormat m a
forall a b. AvFormat m a -> AvFormat m b -> AvFormat m b
forall a b. AvFormat m a -> (a -> AvFormat m b) -> AvFormat m b
forall (m :: * -> *). Monad m => Applicative (AvFormat m)
forall (m :: * -> *) a. Monad m => a -> AvFormat m a
forall (m :: * -> *) a b.
Monad m =>
AvFormat m a -> AvFormat m b -> AvFormat m b
forall (m :: * -> *) a b.
Monad m =>
AvFormat m a -> (a -> AvFormat m b) -> AvFormat m b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> AvFormat m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> AvFormat m a
>> :: AvFormat m a -> AvFormat m b -> AvFormat m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
AvFormat m a -> AvFormat m b -> AvFormat m b
>>= :: AvFormat m a -> (a -> AvFormat m b) -> AvFormat m b
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
AvFormat m a -> (a -> AvFormat m b) -> AvFormat m b
$cp1Monad :: forall (m :: * -> *). Monad m => Applicative (AvFormat m)
Monad
        , Monad (AvFormat m)
Monad (AvFormat m) =>
(forall a. IO a -> AvFormat m a) -> MonadIO (AvFormat m)
IO a -> AvFormat m a
forall a. IO a -> AvFormat m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
forall (m :: * -> *). MonadIO m => Monad (AvFormat m)
forall (m :: * -> *) a. MonadIO m => IO a -> AvFormat m a
liftIO :: IO a -> AvFormat m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> AvFormat m a
$cp1MonadIO :: forall (m :: * -> *). MonadIO m => Monad (AvFormat m)
MonadIO
        , MonadReader AVFormatContext
        , m a -> AvFormat m a
(forall (m :: * -> *) a. Monad m => m a -> AvFormat m a)
-> MonadTrans AvFormat
forall (m :: * -> *) a. Monad m => m a -> AvFormat m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> AvFormat m a
$clift :: forall (m :: * -> *) a. Monad m => m a -> AvFormat m a
MonadTrans
        )

withAvFile :: (MonadMask m, MonadIO m) => String -> AvFormat m a -> m a
withAvFile :: String -> AvFormat m a -> m a
withAvFile fn :: String
fn f :: AvFormat m a
f = do
    Either String AVFormatContext
ectx <- ExceptT String m AVFormatContext
-> m (Either String AVFormatContext)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT (ExceptT String m AVFormatContext
 -> m (Either String AVFormatContext))
-> ExceptT String m AVFormatContext
-> m (Either String AVFormatContext)
forall a b. (a -> b) -> a -> b
$ String -> ExceptT String m AVFormatContext
forall (m :: * -> *).
(MonadIO m, MonadError String m) =>
String -> m AVFormatContext
openFile String
fn
    case Either String AVFormatContext
ectx of
        Left e :: String
e    -> IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ String -> IO a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
e
        Right ctx :: AVFormatContext
ctx -> m a -> m () -> m a
forall (m :: * -> *) a b. MonadMask m => m a -> m b -> m a
finally
            ((IO CInt -> m CInt
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO CInt -> m CInt) -> IO CInt -> m CInt
forall a b. (a -> b) -> a -> b
$ AVFormatContext -> Ptr () -> IO CInt
avformat_find_stream_info AVFormatContext
ctx Ptr ()
forall a. Ptr a
nullPtr) m CInt -> m a -> m a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ReaderT AVFormatContext m a -> AVFormatContext -> m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (AvFormat m a -> ReaderT AVFormatContext m a
forall (m :: * -> *) a. AvFormat m a -> ReaderT AVFormatContext m a
unAvFormat AvFormat m a
f) AVFormatContext
ctx)
            (IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ AVFormatContext -> (Ptr AVFormatContext -> IO ()) -> IO ()
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with AVFormatContext
ctx Ptr AVFormatContext -> IO ()
close_input)

nbStreams :: MonadIO m => AvFormat m Int
nbStreams :: AvFormat m Int
nbStreams = AvFormat m CInt -> AvFormat m Int
forall (m :: * -> *). Monad m => AvFormat m CInt -> AvFormat m Int
avToInt (AvFormat m CInt -> AvFormat m Int)
-> AvFormat m CInt -> AvFormat m Int
forall a b. (a -> b) -> a -> b
$ AvFormat m AVFormatContext
forall r (m :: * -> *). MonadReader r m => m r
ask AvFormat m AVFormatContext
-> (AVFormatContext -> AvFormat m CInt) -> AvFormat m CInt
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ctx :: AVFormatContext
ctx ->
    IO CInt -> AvFormat m CInt
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO CInt -> AvFormat m CInt) -> IO CInt -> AvFormat m CInt
forall a b. (a -> b) -> a -> b
$ ((\hsc_ptr :: Ptr ()
hsc_ptr -> Ptr () -> Int -> IO CInt
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr ()
hsc_ptr 44)) (AVFormatContext -> Ptr ()
forall a. HasPtr a => a -> Ptr ()
getPtr AVFormatContext
ctx)
{-# LINE 62 "src/Codec/FFmpeg/Probe.hsc" #-}

formatName :: MonadIO m => AvFormat m String
formatName :: AvFormat m String
formatName = AvFormat m AVFormatContext
forall r (m :: * -> *). MonadReader r m => m r
ask AvFormat m AVFormatContext
-> (AVFormatContext -> AvFormat m String) -> AvFormat m String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ctx :: AVFormatContext
ctx -> IO String -> AvFormat m String
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO String -> AvFormat m String) -> IO String -> AvFormat m String
forall a b. (a -> b) -> a -> b
$
    ((\hsc_ptr :: Ptr ()
hsc_ptr -> Ptr () -> Int -> IO (Ptr Any)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr ()
hsc_ptr 8)) (AVFormatContext -> Ptr ()
forall a. HasPtr a => a -> Ptr ()
getPtr AVFormatContext
ctx) IO (Ptr Any) -> (Ptr Any -> IO CString) -> IO CString
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
{-# LINE 66 "src/Codec/FFmpeg/Probe.hsc" #-}
    ((\hsc_ptr -> peekByteOff hsc_ptr 0)) IO CString -> (CString -> IO String) -> IO String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
{-# LINE 67 "src/Codec/FFmpeg/Probe.hsc" #-}
    peekCString

duration :: MonadIO m => AvFormat m Int64
duration :: AvFormat m Int64
duration = AvFormat m AVFormatContext
forall r (m :: * -> *). MonadReader r m => m r
ask AvFormat m AVFormatContext
-> (AVFormatContext -> AvFormat m Int64) -> AvFormat m Int64
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ctx :: AVFormatContext
ctx -> IO Int64 -> AvFormat m Int64
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Int64 -> AvFormat m Int64) -> IO Int64 -> AvFormat m Int64
forall a b. (a -> b) -> a -> b
$ ((\hsc_ptr :: Ptr ()
hsc_ptr -> Ptr () -> Int -> IO Int64
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr ()
hsc_ptr 1088)) (AVFormatContext -> Ptr ()
forall a. HasPtr a => a -> Ptr ()
getPtr AVFormatContext
ctx)
{-# LINE 71 "src/Codec/FFmpeg/Probe.hsc" #-}

formatMetadata :: MonadIO m => AvFormat m AVDictionary
formatMetadata :: AvFormat m AVDictionary
formatMetadata = AvFormat m AVFormatContext
forall r (m :: * -> *). MonadReader r m => m r
ask AvFormat m AVFormatContext
-> (AVFormatContext -> AvFormat m AVDictionary)
-> AvFormat m AVDictionary
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO AVDictionary -> AvFormat m AVDictionary
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO AVDictionary -> AvFormat m AVDictionary)
-> (AVFormatContext -> IO AVDictionary)
-> AVFormatContext
-> AvFormat m AVDictionary
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((\hsc_ptr :: Ptr ()
hsc_ptr -> Ptr () -> Int -> IO AVDictionary
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr ()
hsc_ptr 1192)) (Ptr () -> IO AVDictionary)
-> (AVFormatContext -> Ptr ())
-> AVFormatContext
-> IO AVDictionary
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AVFormatContext -> Ptr ()
forall a. HasPtr a => a -> Ptr ()
getPtr
{-# LINE 74 "src/Codec/FFmpeg/Probe.hsc" #-}

-------------------------------------------------------------------------------
-- stream - level stuff
-------------------------------------------------------------------------------

newtype AvStreamT m a = AvStreamT { AvStreamT m a -> ReaderT AVStream m a
unAvStreamT :: ReaderT AVStream (m) a }
    deriving
        ( Functor (AvStreamT m)
a -> AvStreamT m a
Functor (AvStreamT m) =>
(forall a. a -> AvStreamT m a)
-> (forall a b.
    AvStreamT m (a -> b) -> AvStreamT m a -> AvStreamT m b)
-> (forall a b c.
    (a -> b -> c) -> AvStreamT m a -> AvStreamT m b -> AvStreamT m c)
-> (forall a b. AvStreamT m a -> AvStreamT m b -> AvStreamT m b)
-> (forall a b. AvStreamT m a -> AvStreamT m b -> AvStreamT m a)
-> Applicative (AvStreamT m)
AvStreamT m a -> AvStreamT m b -> AvStreamT m b
AvStreamT m a -> AvStreamT m b -> AvStreamT m a
AvStreamT m (a -> b) -> AvStreamT m a -> AvStreamT m b
(a -> b -> c) -> AvStreamT m a -> AvStreamT m b -> AvStreamT m c
forall a. a -> AvStreamT m a
forall a b. AvStreamT m a -> AvStreamT m b -> AvStreamT m a
forall a b. AvStreamT m a -> AvStreamT m b -> AvStreamT m b
forall a b. AvStreamT m (a -> b) -> AvStreamT m a -> AvStreamT m b
forall a b c.
(a -> b -> c) -> AvStreamT m a -> AvStreamT m b -> AvStreamT m c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
forall (m :: * -> *). Applicative m => Functor (AvStreamT m)
forall (m :: * -> *) a. Applicative m => a -> AvStreamT m a
forall (m :: * -> *) a b.
Applicative m =>
AvStreamT m a -> AvStreamT m b -> AvStreamT m a
forall (m :: * -> *) a b.
Applicative m =>
AvStreamT m a -> AvStreamT m b -> AvStreamT m b
forall (m :: * -> *) a b.
Applicative m =>
AvStreamT m (a -> b) -> AvStreamT m a -> AvStreamT m b
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> AvStreamT m a -> AvStreamT m b -> AvStreamT m c
<* :: AvStreamT m a -> AvStreamT m b -> AvStreamT m a
$c<* :: forall (m :: * -> *) a b.
Applicative m =>
AvStreamT m a -> AvStreamT m b -> AvStreamT m a
*> :: AvStreamT m a -> AvStreamT m b -> AvStreamT m b
$c*> :: forall (m :: * -> *) a b.
Applicative m =>
AvStreamT m a -> AvStreamT m b -> AvStreamT m b
liftA2 :: (a -> b -> c) -> AvStreamT m a -> AvStreamT m b -> AvStreamT m c
$cliftA2 :: forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> c) -> AvStreamT m a -> AvStreamT m b -> AvStreamT m c
<*> :: AvStreamT m (a -> b) -> AvStreamT m a -> AvStreamT m b
$c<*> :: forall (m :: * -> *) a b.
Applicative m =>
AvStreamT m (a -> b) -> AvStreamT m a -> AvStreamT m b
pure :: a -> AvStreamT m a
$cpure :: forall (m :: * -> *) a. Applicative m => a -> AvStreamT m a
$cp1Applicative :: forall (m :: * -> *). Applicative m => Functor (AvStreamT m)
Applicative
        , a -> AvStreamT m b -> AvStreamT m a
(a -> b) -> AvStreamT m a -> AvStreamT m b
(forall a b. (a -> b) -> AvStreamT m a -> AvStreamT m b)
-> (forall a b. a -> AvStreamT m b -> AvStreamT m a)
-> Functor (AvStreamT m)
forall a b. a -> AvStreamT m b -> AvStreamT m a
forall a b. (a -> b) -> AvStreamT m a -> AvStreamT m b
forall (m :: * -> *) a b.
Functor m =>
a -> AvStreamT m b -> AvStreamT m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> AvStreamT m a -> AvStreamT m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> AvStreamT m b -> AvStreamT m a
$c<$ :: forall (m :: * -> *) a b.
Functor m =>
a -> AvStreamT m b -> AvStreamT m a
fmap :: (a -> b) -> AvStreamT m a -> AvStreamT m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> AvStreamT m a -> AvStreamT m b
Functor
        , Applicative (AvStreamT m)
a -> AvStreamT m a
Applicative (AvStreamT m) =>
(forall a b.
 AvStreamT m a -> (a -> AvStreamT m b) -> AvStreamT m b)
-> (forall a b. AvStreamT m a -> AvStreamT m b -> AvStreamT m b)
-> (forall a. a -> AvStreamT m a)
-> Monad (AvStreamT m)
AvStreamT m a -> (a -> AvStreamT m b) -> AvStreamT m b
AvStreamT m a -> AvStreamT m b -> AvStreamT m b
forall a. a -> AvStreamT m a
forall a b. AvStreamT m a -> AvStreamT m b -> AvStreamT m b
forall a b. AvStreamT m a -> (a -> AvStreamT m b) -> AvStreamT m b
forall (m :: * -> *). Monad m => Applicative (AvStreamT m)
forall (m :: * -> *) a. Monad m => a -> AvStreamT m a
forall (m :: * -> *) a b.
Monad m =>
AvStreamT m a -> AvStreamT m b -> AvStreamT m b
forall (m :: * -> *) a b.
Monad m =>
AvStreamT m a -> (a -> AvStreamT m b) -> AvStreamT m b
forall (m :: * -> *).
Applicative m =>
(forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> AvStreamT m a
$creturn :: forall (m :: * -> *) a. Monad m => a -> AvStreamT m a
>> :: AvStreamT m a -> AvStreamT m b -> AvStreamT m b
$c>> :: forall (m :: * -> *) a b.
Monad m =>
AvStreamT m a -> AvStreamT m b -> AvStreamT m b
>>= :: AvStreamT m a -> (a -> AvStreamT m b) -> AvStreamT m b
$c>>= :: forall (m :: * -> *) a b.
Monad m =>
AvStreamT m a -> (a -> AvStreamT m b) -> AvStreamT m b
$cp1Monad :: forall (m :: * -> *). Monad m => Applicative (AvStreamT m)
Monad
        , Monad (AvStreamT m)
Monad (AvStreamT m) =>
(forall a. IO a -> AvStreamT m a) -> MonadIO (AvStreamT m)
IO a -> AvStreamT m a
forall a. IO a -> AvStreamT m a
forall (m :: * -> *).
Monad m =>
(forall a. IO a -> m a) -> MonadIO m
forall (m :: * -> *). MonadIO m => Monad (AvStreamT m)
forall (m :: * -> *) a. MonadIO m => IO a -> AvStreamT m a
liftIO :: IO a -> AvStreamT m a
$cliftIO :: forall (m :: * -> *) a. MonadIO m => IO a -> AvStreamT m a
$cp1MonadIO :: forall (m :: * -> *). MonadIO m => Monad (AvStreamT m)
MonadIO
        , MonadReader AVStream
        , m a -> AvStreamT m a
(forall (m :: * -> *) a. Monad m => m a -> AvStreamT m a)
-> MonadTrans AvStreamT
forall (m :: * -> *) a. Monad m => m a -> AvStreamT m a
forall (t :: (* -> *) -> * -> *).
(forall (m :: * -> *) a. Monad m => m a -> t m a) -> MonadTrans t
lift :: m a -> AvStreamT m a
$clift :: forall (m :: * -> *) a. Monad m => m a -> AvStreamT m a
MonadTrans
        )

withStream :: (MonadIO m) => Int -> AvStreamT (AvFormat m) a -> AvFormat m a
withStream :: Int -> AvStreamT (AvFormat m) a -> AvFormat m a
withStream sid :: Int
sid f :: AvStreamT (AvFormat m) a
f = AvFormat m Int
forall (m :: * -> *). MonadIO m => AvFormat m Int
nbStreams AvFormat m Int -> (Int -> AvFormat m a) -> AvFormat m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ns :: Int
ns -> if Int
sid Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
ns
    then String -> AvFormat m a
forall a. HasCallStack => String -> a
error (String -> AvFormat m a) -> String -> AvFormat m a
forall a b. (a -> b) -> a -> b
$ Int -> String
forall a. Show a => a -> String
show Int
sid String -> String -> String
forall a. [a] -> [a] -> [a]
++ " >= " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
ns
    else do
        AVFormatContext
ctx <- AvFormat m AVFormatContext
forall r (m :: * -> *). MonadReader r m => m r
ask
        Ptr AVStream
streams <- IO (Ptr AVStream) -> AvFormat m (Ptr AVStream)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Ptr AVStream) -> AvFormat m (Ptr AVStream))
-> IO (Ptr AVStream) -> AvFormat m (Ptr AVStream)
forall a b. (a -> b) -> a -> b
$ ((\hsc_ptr :: Ptr ()
hsc_ptr -> Ptr () -> Int -> IO (Ptr AVStream)
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr ()
hsc_ptr 48)) (AVFormatContext -> Ptr ()
forall a. HasPtr a => a -> Ptr ()
getPtr AVFormatContext
ctx)
{-# LINE 95 "src/Codec/FFmpeg/Probe.hsc" #-}
        IO AVStream -> AvFormat m AVStream
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Ptr AVStream -> Int -> IO AVStream
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff Ptr AVStream
streams Int
sid) AvFormat m AVStream -> (AVStream -> AvFormat m a) -> AvFormat m a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ReaderT AVStream (AvFormat m) a -> AVStream -> AvFormat m a
forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (AvStreamT (AvFormat m) a -> ReaderT AVStream (AvFormat m) a
forall (m :: * -> *) a. AvStreamT m a -> ReaderT AVStream m a
unAvStreamT AvStreamT (AvFormat m) a
f)

codecContext :: MonadIO m => AvStreamT m (Maybe AVCodecContext)
codecContext :: AvStreamT m (Maybe AVCodecContext)
codecContext = do
    Ptr ()
p <- AvStreamT m AVStream
forall r (m :: * -> *). MonadReader r m => m r
ask AvStreamT m AVStream
-> (AVStream -> AvStreamT m (Ptr ())) -> AvStreamT m (Ptr ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (IO (Ptr ()) -> AvStreamT m (Ptr ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Ptr ()) -> AvStreamT m (Ptr ()))
-> (AVStream -> IO (Ptr ())) -> AVStream -> AvStreamT m (Ptr ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((\hsc_ptr :: Ptr ()
hsc_ptr -> Ptr () -> Int -> IO (Ptr ())
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr ()
hsc_ptr 8)) (Ptr () -> IO (Ptr ()))
-> (AVStream -> Ptr ()) -> AVStream -> IO (Ptr ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AVStream -> Ptr ()
forall a. HasPtr a => a -> Ptr ()
getPtr)
{-# LINE 100 "src/Codec/FFmpeg/Probe.hsc" #-}
    if (p /= nullPtr)
        then return $ Just $ AVCodecContext p
        else return Nothing

codecMediaTypeName :: MonadIO m => AVCodecContext -> AvStreamT m String
codecMediaTypeName :: AVCodecContext -> AvStreamT m String
codecMediaTypeName cctx :: AVCodecContext
cctx = IO String -> AvStreamT m String
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO String -> AvStreamT m String)
-> IO String -> AvStreamT m String
forall a b. (a -> b) -> a -> b
$
    ((\hsc_ptr :: Ptr ()
hsc_ptr -> Ptr () -> Int -> IO AVMediaType
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr ()
hsc_ptr 12)) (AVCodecContext -> Ptr ()
forall a. HasPtr a => a -> Ptr ()
getPtr AVCodecContext
cctx) IO AVMediaType -> (AVMediaType -> IO CString) -> IO CString
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
{-# LINE 107 "src/Codec/FFmpeg/Probe.hsc" #-}
    av_get_media_type_string IO CString -> (CString -> IO String) -> IO String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
    CString -> IO String
peekCString

codec :: MonadIO m => AVCodecContext -> AvStreamT m (Maybe AVCodec)
codec :: AVCodecContext -> AvStreamT m (Maybe AVCodec)
codec cctx :: AVCodecContext
cctx = (IO (Ptr ()) -> AvStreamT m (Ptr ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Ptr ()) -> AvStreamT m (Ptr ()))
-> (AVCodecContext -> IO (Ptr ()))
-> AVCodecContext
-> AvStreamT m (Ptr ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((\hsc_ptr :: Ptr ()
hsc_ptr -> Ptr () -> Int -> IO (Ptr ())
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr ()
hsc_ptr 16)) (Ptr () -> IO (Ptr ()))
-> (AVCodecContext -> Ptr ()) -> AVCodecContext -> IO (Ptr ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AVCodecContext -> Ptr ()
forall a. HasPtr a => a -> Ptr ()
getPtr) AVCodecContext
cctx AvStreamT m (Ptr ())
-> (Ptr () -> AvStreamT m (Maybe AVCodec))
-> AvStreamT m (Maybe AVCodec)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
{-# LINE 112 "src/Codec/FFmpeg/Probe.hsc" #-}
    \mc -> if mc == nullPtr
        then return Nothing
        else return $ Just $ AVCodec mc

codecName :: MonadIO m => AVCodecContext -> AvStreamT m String
codecName :: AVCodecContext -> AvStreamT m String
codecName cctx :: AVCodecContext
cctx = IO String -> AvStreamT m String
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO String -> AvStreamT m String)
-> IO String -> AvStreamT m String
forall a b. (a -> b) -> a -> b
$ AVCodecContext -> IO AVCodecID
forall t. HasCodecID t => t -> IO AVCodecID
getCodecID AVCodecContext
cctx IO AVCodecID -> (AVCodecID -> IO CString) -> IO CString
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= AVCodecID -> IO CString
avcodec_get_name IO CString -> (CString -> IO String) -> IO String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO String
peekCString

streamBitrate :: MonadIO m => AVCodecContext -> AvStreamT m Int
streamBitrate :: AVCodecContext -> AvStreamT m Int
streamBitrate cctx :: AVCodecContext
cctx = IO Int -> AvStreamT m Int
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Int -> AvStreamT m Int) -> IO Int -> AvStreamT m Int
forall a b. (a -> b) -> a -> b
$ AVCodecContext -> IO CInt
forall t. HasBitRate t => t -> IO CInt
getBitRate AVCodecContext
cctx IO CInt -> (CInt -> IO Int) -> IO Int
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> IO Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> IO Int) -> (CInt -> Int) -> CInt -> IO Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral

-- |
-- Gives the (width, height) of a video stream in pixels, not accounting for the pixel aspect ratio.
streamImageSize :: MonadIO m => AVCodecContext -> AvStreamT m (Int, Int)
streamImageSize :: AVCodecContext -> AvStreamT m (Int, Int)
streamImageSize cctx :: AVCodecContext
cctx = IO (Int, Int) -> AvStreamT m (Int, Int)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Int, Int) -> AvStreamT m (Int, Int))
-> IO (Int, Int) -> AvStreamT m (Int, Int)
forall a b. (a -> b) -> a -> b
$ (,)
    (Int -> Int -> (Int, Int)) -> IO Int -> IO (Int -> (Int, Int))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (CInt -> Int) -> IO CInt -> IO Int
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (AVCodecContext -> IO CInt
forall t. HasWidth t => t -> IO CInt
getWidth AVCodecContext
cctx)
    IO (Int -> (Int, Int)) -> IO Int -> IO (Int, Int)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (CInt -> Int) -> IO CInt -> IO Int
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (AVCodecContext -> IO CInt
forall t. HasHeight t => t -> IO CInt
getHeight AVCodecContext
cctx)

streamMetadata :: MonadIO m => AvStreamT m AVDictionary
streamMetadata :: AvStreamT m AVDictionary
streamMetadata = AvStreamT m AVStream
forall r (m :: * -> *). MonadReader r m => m r
ask AvStreamT m AVStream
-> (AVStream -> AvStreamT m AVDictionary)
-> AvStreamT m AVDictionary
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= IO AVDictionary -> AvStreamT m AVDictionary
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO AVDictionary -> AvStreamT m AVDictionary)
-> (AVStream -> IO AVDictionary)
-> AVStream
-> AvStreamT m AVDictionary
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((\hsc_ptr :: Ptr ()
hsc_ptr -> Ptr () -> Int -> IO AVDictionary
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr ()
hsc_ptr 96)) (Ptr () -> IO AVDictionary)
-> (AVStream -> Ptr ()) -> AVStream -> IO AVDictionary
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AVStream -> Ptr ()
forall a. HasPtr a => a -> Ptr ()
getPtr
{-# LINE 131 "src/Codec/FFmpeg/Probe.hsc" #-}

-------------------------------------------------------------------------------
-- dictionaries
-------------------------------------------------------------------------------

dictFoldM_
    :: MonadIO m
    => ((String, String) -> m ())
    -> AVDictionary
    -> m ()
dictFoldM_ :: ((String, String) -> m ()) -> AVDictionary -> m ()
dictFoldM_ f :: (String, String) -> m ()
f d :: AVDictionary
d =
    let
        flags :: CInt
flags = (14)
{-# LINE 144 "src/Codec/FFmpeg/Probe.hsc" #-}
        next :: Ptr () -> m ()
next ep :: Ptr ()
ep = do
            Ptr ()
e' <- IO (Ptr ()) -> m (Ptr ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Ptr ()) -> m (Ptr ())) -> IO (Ptr ()) -> m (Ptr ())
forall a b. (a -> b) -> a -> b
$ String -> (CString -> IO (Ptr ())) -> IO (Ptr ())
forall a. String -> (CString -> IO a) -> IO a
withCString "" ((CString -> IO (Ptr ())) -> IO (Ptr ()))
-> (CString -> IO (Ptr ())) -> IO (Ptr ())
forall a b. (a -> b) -> a -> b
$ \s :: CString
s -> AVDictionary -> CString -> Ptr () -> CInt -> IO (Ptr ())
av_dict_get AVDictionary
d CString
s Ptr ()
ep CInt
flags
            if (Ptr ()
e' Ptr () -> Ptr () -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr ()
forall a. Ptr a
nullPtr)
                then () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
                else do
                    String
k <- IO String -> m String
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO String -> m String) -> IO String -> m String
forall a b. (a -> b) -> a -> b
$ ((\hsc_ptr :: Ptr ()
hsc_ptr -> Ptr () -> Int -> IO CString
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr ()
hsc_ptr 0)) Ptr ()
e' IO CString -> (CString -> IO String) -> IO String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO String
peekCString
{-# LINE 150 "src/Codec/FFmpeg/Probe.hsc" #-}
                    String
v <- IO String -> m String
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO String -> m String) -> IO String -> m String
forall a b. (a -> b) -> a -> b
$ ((\hsc_ptr :: Ptr ()
hsc_ptr -> Ptr () -> Int -> IO CString
forall a b. Storable a => Ptr b -> Int -> IO a
peekByteOff Ptr ()
hsc_ptr 8)) Ptr ()
e' IO CString -> (CString -> IO String) -> IO String
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO String
peekCString
{-# LINE 151 "src/Codec/FFmpeg/Probe.hsc" #-}
                    (String, String) -> m ()
f (String
k, String
v)
                    Ptr () -> m ()
next Ptr ()
e'
    in do
        -- e <- liftIO $ malloc >>= \m -> poke m nullPtr >> return m
        Ptr () -> m ()
next Ptr ()
forall a. Ptr a
nullPtr

-------------------------------------------------------------------------------
-- helpers
-------------------------------------------------------------------------------

avToInt :: Monad m => AvFormat m CInt -> AvFormat m Int
avToInt :: AvFormat m CInt -> AvFormat m Int
avToInt f :: AvFormat m CInt
f = AvFormat m CInt
f AvFormat m CInt -> (CInt -> AvFormat m Int) -> AvFormat m Int
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> AvFormat m Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> AvFormat m Int) -> (CInt -> Int) -> CInt -> AvFormat m Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral

-------------------------------------------------------------------------------
-- FFI imports
-------------------------------------------------------------------------------

foreign import ccall "av_get_media_type_string"
  av_get_media_type_string :: AVMediaType -> IO CString

foreign import ccall "avcodec_get_name"
  avcodec_get_name :: AVCodecID -> IO CString

foreign import ccall "av_dict_get"
  av_dict_get :: AVDictionary -> CString -> Ptr () -> CInt -> IO (Ptr ())