\section{Using OggzMetric}
\label{group__metric}\index{Using OggzMetric@{Using OggzMetric}}
\subsection*{Typedefs}
\begin{CompactItemize}
\item 
typedef ogg\_\-int64\_\-t($\ast$ {\bf OggzMetric} )({\bf OGGZ} $\ast$oggz, long serialno, ogg\_\-int64\_\-t granulepos, void $\ast$user\_\-data)
\begin{CompactList}\small\item\em This is the signature of a function to correlate Ogg streams. \item\end{CompactList}\end{CompactItemize}
\subsection*{Functions}
\begin{CompactItemize}
\item 
int {\bf oggz\_\-get\_\-preroll} ({\bf OGGZ} $\ast$oggz, long serialno)
\begin{CompactList}\small\item\em Retrieve the preroll of a logical bitstream. \item\end{CompactList}\item 
int {\bf oggz\_\-set\_\-preroll} ({\bf OGGZ} $\ast$oggz, long serialno, int preroll)
\begin{CompactList}\small\item\em Specify the preroll of a logical bitstream. \item\end{CompactList}\item 
int {\bf oggz\_\-get\_\-granuleshift} ({\bf OGGZ} $\ast$oggz, long serialno)
\begin{CompactList}\small\item\em Retrieve the granuleshift of a logical bitstream. \item\end{CompactList}\item 
int {\bf oggz\_\-set\_\-granuleshift} ({\bf OGGZ} $\ast$oggz, long serialno, int granuleshift)
\begin{CompactList}\small\item\em Specify the granuleshift of a logical bitstream. \item\end{CompactList}\item 
int {\bf oggz\_\-get\_\-granulerate} ({\bf OGGZ} $\ast$oggz, long serialno, ogg\_\-int64\_\-t $\ast$granulerate\_\-n, ogg\_\-int64\_\-t $\ast$granulerate\_\-d)
\begin{CompactList}\small\item\em Retrieve the granulerate of a logical bitstream. \item\end{CompactList}\item 
int {\bf oggz\_\-set\_\-granulerate} ({\bf OGGZ} $\ast$oggz, long serialno, ogg\_\-int64\_\-t granule\_\-rate\_\-numerator, ogg\_\-int64\_\-t granule\_\-rate\_\-denominator)
\begin{CompactList}\small\item\em Specify the granulerate of a logical bitstream. \item\end{CompactList}\item 
int {\bf oggz\_\-set\_\-metric} ({\bf OGGZ} $\ast$oggz, long serialno, {\bf OggzMetric} metric, void $\ast$user\_\-data)
\begin{CompactList}\small\item\em Set the OggzMetric to use for an OGGZ handle. \item\end{CompactList}\item 
int {\bf oggz\_\-set\_\-data\_\-start} ({\bf OGGZ} $\ast$oggz, {\bf oggz\_\-off\_\-t} offset)
\begin{CompactList}\small\item\em Tell Oggz to remember the given offset as the start of data. \item\end{CompactList}\end{CompactItemize}


\subsection{Detailed Description}
\subsection{Introduction}\label{group__metric_metric_intro}
An OggzMetric is a helper function for Oggz's seeking mechanism.

If every position in an Ogg stream can be described by a metric such as time, then it is possible to define a function that, given a serialno and granulepos, returns a measurement in units such as milliseconds. Oggz will use this function repeatedly while seeking in order to navigate through the Ogg stream.

The meaning of the units is arbitrary, but must be consistent across all logical bitstreams. This allows Oggz to seek accurately through Ogg bitstreams containing multiple logical bitstreams such as tracks of media.\subsection{How to set metrics}\label{group__metric_setting}
You don't need to set metrics for Speex, Vorbis, FLAC, Theora, CMML or Annodex. These can be handled \doxyref{automatically }{p.}{group__seek__api} by Oggz.

For most others it is simply a matter of providing a \char`\"{}granulerate\char`\"{}: a frame or sampling rate, if each packet's granulepos represents a sample number.

\begin{itemize}
\item Set the {\em granule\_\-rate\_\-numerator\/} and {\em granule\_\-rate\_\-denominator\/} appropriately using \doxyref{oggz\_\-set\_\-granulerate()}{p.}{group__metric_ga2a86ec590161bc2295a2c8e91cefa49}\end{itemize}


Some codecs use a \char`\"{}granuleshift\char`\"{} to divide a granulepos into two halves; the first describing a dependency on a previous packet, the second giving the offset since that packet. This is used to mark keyframes and intermediate frames.

\begin{itemize}
\item Set the {\em granuleshift\/} appropriately using \doxyref{oggz\_\-set\_\-granuleshift()}{p.}{group__metric_g7ca67e0344580bf1541b9fbde39d481b}\end{itemize}
\subsubsection{Custom Metrics}\label{group__metric_custom}
For streams with non-linear granulepos, you need to set a custom metric:

\begin{itemize}
\item Implement an OggzMetric callback\item Set the OggzMetric callback using \doxyref{oggz\_\-set\_\-metric()}{p.}{group__metric_g5a630e8dcd04e1dd6601d8f56f0af3f6}\end{itemize}
\subsection{Seeking with OggzMetrics}\label{group__metric_using}
To seek, use \doxyref{oggz\_\-seek\_\-units()}{p.}{group__seek__api_g60bac88ef3695629efacec43a21927e5}. Oggz will perform a ratio search through the Ogg bitstream, using the OggzMetric callback to determine its position relative to the desired unit.

\begin{Desc}
\item[Note:]\end{Desc}
Many data streams begin with headers describing such things as codec setup parameters. One of the assumptions Monty makes is:

\begin{itemize}
\item Given pre-cached decode headers, a player may seek into a stream at any point and begin decode.\end{itemize}


Thus, the first action taken by applications dealing with such data is to read in and cache the decode headers; thereafter the application can safely seek to arbitrary points in the data.

This impacts seeking because the portion of the bitstream containing decode headers should not be considered part of the metric space. To inform Oggz not to seek earlier than the end of the decode headers, use \doxyref{oggz\_\-set\_\-data\_\-start()}{p.}{group__metric_gaec9a54e9b231797d245042ce23b619a}. 

\subsection{Typedef Documentation}
\index{metric@{metric}!OggzMetric@{OggzMetric}}
\index{OggzMetric@{OggzMetric}!metric@{metric}}
\subsubsection[{OggzMetric}]{\setlength{\rightskip}{0pt plus 5cm}typedef ogg\_\-int64\_\-t($\ast$ {\bf OggzMetric})({\bf OGGZ} $\ast$oggz, long serialno, ogg\_\-int64\_\-t granulepos, void $\ast$user\_\-data)}\label{group__metric_g8803bb406015a32f5172902e3e4e76e8}


This is the signature of a function to correlate Ogg streams. 

If every position in an Ogg stream can be described by a metric (eg. time) then define this function that returns some arbitrary unit value. This is the normal use of Oggz for media streams. The meaning of units is arbitrary, but must be consistent across all logical bitstreams; for example a conversion of the time offset of a given packet into nanoseconds or a similar stream-specific subdivision may be appropriate.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em oggz}]An OGGZ handle \item[{\em serialno}]Identifies a logical bitstream within {\em oggz\/} \item[{\em granulepos}]A granulepos within the logical bitstream identified by {\em serialno\/} \item[{\em user\_\-data}]Arbitrary data you wish to pass to your callback \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]A conversion of the (serialno, granulepos) pair into a measure in units which is consistent across all logical bitstreams within {\em oggz\/} \end{Desc}


\subsection{Function Documentation}
\index{metric@{metric}!oggz\_\-get\_\-granulerate@{oggz\_\-get\_\-granulerate}}
\index{oggz\_\-get\_\-granulerate@{oggz\_\-get\_\-granulerate}!metric@{metric}}
\subsubsection[{oggz\_\-get\_\-granulerate}]{\setlength{\rightskip}{0pt plus 5cm}int oggz\_\-get\_\-granulerate ({\bf OGGZ} $\ast$ {\em oggz}, \/  long {\em serialno}, \/  ogg\_\-int64\_\-t $\ast$ {\em granulerate\_\-n}, \/  ogg\_\-int64\_\-t $\ast$ {\em granulerate\_\-d})}\label{group__metric_g72a9a24e6f8483986859d7697fa611b6}


Retrieve the granulerate of a logical bitstream. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em oggz}]An OGGZ handle \item[{\em serialno}]Identify the logical bitstream in {\em oggz\/} \item[{\em granulerate\_\-n}]Return location for the granulerate numerator \item[{\em granulerate\_\-d}]Return location for the granulerate denominator \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]0 Success \end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em OGGZ\_\-ERR\_\-BAD\_\-SERIALNO}]{\em serialno\/} does not identify an existing logical bitstream in {\em oggz\/}. \item[{\em OGGZ\_\-ERR\_\-BAD\_\-OGGZ}]{\em oggz\/} does not refer to an existing OGGZ \end{description}
\end{Desc}
\index{metric@{metric}!oggz\_\-get\_\-granuleshift@{oggz\_\-get\_\-granuleshift}}
\index{oggz\_\-get\_\-granuleshift@{oggz\_\-get\_\-granuleshift}!metric@{metric}}
\subsubsection[{oggz\_\-get\_\-granuleshift}]{\setlength{\rightskip}{0pt plus 5cm}int oggz\_\-get\_\-granuleshift ({\bf OGGZ} $\ast$ {\em oggz}, \/  long {\em serialno})}\label{group__metric_gd9b55e525a6e0b994a133314fd78e2c0}


Retrieve the granuleshift of a logical bitstream. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em oggz}]An OGGZ handle \item[{\em serialno}]Identify the logical bitstream in {\em oggz\/} \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The granuleshift of the specified logical bitstream. \end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em OGGZ\_\-ERR\_\-BAD\_\-SERIALNO}]{\em serialno\/} does not identify an existing logical bitstream in {\em oggz\/}. \item[{\em OGGZ\_\-ERR\_\-BAD\_\-OGGZ}]{\em oggz\/} does not refer to an existing OGGZ \end{description}
\end{Desc}
\index{metric@{metric}!oggz\_\-get\_\-preroll@{oggz\_\-get\_\-preroll}}
\index{oggz\_\-get\_\-preroll@{oggz\_\-get\_\-preroll}!metric@{metric}}
\subsubsection[{oggz\_\-get\_\-preroll}]{\setlength{\rightskip}{0pt plus 5cm}int oggz\_\-get\_\-preroll ({\bf OGGZ} $\ast$ {\em oggz}, \/  long {\em serialno})}\label{group__metric_gb411ab113d5f0f67bf77506724c25633}


Retrieve the preroll of a logical bitstream. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em oggz}]An OGGZ handle \item[{\em serialno}]Identify the logical bitstream in {\em oggz\/} \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]The preroll of the specified logical bitstream. \end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em OGGZ\_\-ERR\_\-BAD\_\-SERIALNO}]{\em serialno\/} does not identify an existing logical bitstream in {\em oggz\/}. \item[{\em OGGZ\_\-ERR\_\-BAD\_\-OGGZ}]{\em oggz\/} does not refer to an existing OGGZ \end{description}
\end{Desc}
\index{metric@{metric}!oggz\_\-set\_\-data\_\-start@{oggz\_\-set\_\-data\_\-start}}
\index{oggz\_\-set\_\-data\_\-start@{oggz\_\-set\_\-data\_\-start}!metric@{metric}}
\subsubsection[{oggz\_\-set\_\-data\_\-start}]{\setlength{\rightskip}{0pt plus 5cm}int oggz\_\-set\_\-data\_\-start ({\bf OGGZ} $\ast$ {\em oggz}, \/  {\bf oggz\_\-off\_\-t} {\em offset})}\label{group__metric_gaec9a54e9b231797d245042ce23b619a}


Tell Oggz to remember the given offset as the start of data. 

This informs the seeking mechanism that when seeking back to unit 0, go to the given offset, not to the start of the file, which is usually codec headers. The usual usage is: \small\begin{alltt}
    oggz\_set\_data\_start (oggz, oggz\_tell (oggz));
\end{alltt}
\normalsize 
 \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em oggz}]An OGGZ handle previously opened for reading \item[{\em offset}]The offset of the start of data \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]0 on success, -1 on failure. \end{Desc}
\index{metric@{metric}!oggz\_\-set\_\-granulerate@{oggz\_\-set\_\-granulerate}}
\index{oggz\_\-set\_\-granulerate@{oggz\_\-set\_\-granulerate}!metric@{metric}}
\subsubsection[{oggz\_\-set\_\-granulerate}]{\setlength{\rightskip}{0pt plus 5cm}int oggz\_\-set\_\-granulerate ({\bf OGGZ} $\ast$ {\em oggz}, \/  long {\em serialno}, \/  ogg\_\-int64\_\-t {\em granule\_\-rate\_\-numerator}, \/  ogg\_\-int64\_\-t {\em granule\_\-rate\_\-denominator})}\label{group__metric_ga2a86ec590161bc2295a2c8e91cefa49}


Specify the granulerate of a logical bitstream. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em oggz}]An OGGZ handle \item[{\em serialno}]Identify the logical bitstream in {\em oggz\/} to attach this linear metric to. A value of -1 indicates that the metric should be attached to all unattached logical bitstreams in {\em oggz\/}. \item[{\em granule\_\-rate\_\-numerator}]The numerator of the granule rate \item[{\em granule\_\-rate\_\-denominator}]The denominator of the granule rate \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]0 Success \end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em OGGZ\_\-ERR\_\-BAD\_\-SERIALNO}]{\em serialno\/} does not identify an existing logical bitstream in {\em oggz\/}. \item[{\em OGGZ\_\-ERR\_\-BAD\_\-OGGZ}]{\em oggz\/} does not refer to an existing OGGZ \end{description}
\end{Desc}
\index{metric@{metric}!oggz\_\-set\_\-granuleshift@{oggz\_\-set\_\-granuleshift}}
\index{oggz\_\-set\_\-granuleshift@{oggz\_\-set\_\-granuleshift}!metric@{metric}}
\subsubsection[{oggz\_\-set\_\-granuleshift}]{\setlength{\rightskip}{0pt plus 5cm}int oggz\_\-set\_\-granuleshift ({\bf OGGZ} $\ast$ {\em oggz}, \/  long {\em serialno}, \/  int {\em granuleshift})}\label{group__metric_g7ca67e0344580bf1541b9fbde39d481b}


Specify the granuleshift of a logical bitstream. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em oggz}]An OGGZ handle \item[{\em serialno}]Identify the logical bitstream in {\em oggz\/} to attach this granuleshift metric to. A value of -1 indicates that the metric should be attached to all unattached logical bitstreams in {\em oggz\/}. \item[{\em granuleshift}]The granuleshift \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]0 Success \end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em OGGZ\_\-ERR\_\-BAD\_\-SERIALNO}]{\em serialno\/} does not identify an existing logical bitstream in {\em oggz\/}. \item[{\em OGGZ\_\-ERR\_\-BAD\_\-OGGZ}]{\em oggz\/} does not refer to an existing OGGZ \end{description}
\end{Desc}
\index{metric@{metric}!oggz\_\-set\_\-metric@{oggz\_\-set\_\-metric}}
\index{oggz\_\-set\_\-metric@{oggz\_\-set\_\-metric}!metric@{metric}}
\subsubsection[{oggz\_\-set\_\-metric}]{\setlength{\rightskip}{0pt plus 5cm}int oggz\_\-set\_\-metric ({\bf OGGZ} $\ast$ {\em oggz}, \/  long {\em serialno}, \/  {\bf OggzMetric} {\em metric}, \/  void $\ast$ {\em user\_\-data})}\label{group__metric_g5a630e8dcd04e1dd6601d8f56f0af3f6}


Set the OggzMetric to use for an OGGZ handle. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em oggz}]An OGGZ handle \item[{\em serialno}]Identify the logical bitstream in {\em oggz\/} to attach this metric to. A value of -1 indicates that this metric should be attached to all unattached logical bitstreams in {\em oggz\/}. \item[{\em metric}]An OggzMetric callback \item[{\em user\_\-data}]arbitrary data to pass to the metric callback\end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]0 Success \end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em OGGZ\_\-ERR\_\-BAD\_\-SERIALNO}]{\em serialno\/} does not identify an existing logical bitstream in {\em oggz\/}, and is not -1 \item[{\em OGGZ\_\-ERR\_\-BAD\_\-OGGZ}]{\em oggz\/} does not refer to an existing OGGZ\end{description}
\end{Desc}
\begin{Desc}
\item[Note:]Specifying values of {\em serialno\/} other than -1 allows you to pass logical bitstream specific user\_\-data to the same metric. 

Alternatively, you may use a different {\em metric\/} for each {\em serialno\/}, but all metrics used must return mutually consistent unit measurements. \end{Desc}
\index{metric@{metric}!oggz\_\-set\_\-preroll@{oggz\_\-set\_\-preroll}}
\index{oggz\_\-set\_\-preroll@{oggz\_\-set\_\-preroll}!metric@{metric}}
\subsubsection[{oggz\_\-set\_\-preroll}]{\setlength{\rightskip}{0pt plus 5cm}int oggz\_\-set\_\-preroll ({\bf OGGZ} $\ast$ {\em oggz}, \/  long {\em serialno}, \/  int {\em preroll})}\label{group__metric_ga2b4ab5be298a11b5b91147b76996c83}


Specify the preroll of a logical bitstream. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em oggz}]An OGGZ handle \item[{\em serialno}]Identify the logical bitstream in {\em oggz\/} to attach this preroll to. \item[{\em preroll}]The preroll \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]0 Success \end{Desc}
\begin{Desc}
\item[Return values:]
\begin{description}
\item[{\em OGGZ\_\-ERR\_\-BAD\_\-SERIALNO}]{\em serialno\/} does not identify an existing logical bitstream in {\em oggz\/}. \item[{\em OGGZ\_\-ERR\_\-BAD\_\-OGGZ}]{\em oggz\/} does not refer to an existing OGGZ \end{description}
\end{Desc}
