Merge master.
[dcpomatic.git] / doc / design / timing.tex
1 \documentclass{article}
2 \begin{document}
3
4 We are trying to implement full-ish playlist based content specification.  The timing is awkward.
5
6 \section{Reference timing}
7
8 Frame rates of things can vary a lot; content can be in pretty much
9 anything, and DCP video and audio frame rates may change on a whim
10 depending on what is best for a given set of content.  This suggests
11 (albeit without strong justification) the need for a frame-rate-independent unit of time.
12
13 So far we've been using a time type called \texttt{Time} expressed in
14 $\mathtt{TIME\_HZ}^{-1}$; e.g. \texttt{TIME\_HZ} units is 1 second.
15 \texttt{TIME\_HZ} is chosen to be divisible by lots of frame and
16 sample rates.
17
18 We express content start time as a \texttt{Time}.
19
20
21 \section{Timing at different stages of the chain}
22
23 Let's try this: decoders produce sequences of (perhaps) video frames
24 and (perhaps) audio frames.  There are no gaps.  They are at the
25 content's native frame rates and are synchronised (meaning that if
26 they are played together, at the content's frame rates, they will be
27 in sync).  The decoders give timestamps for each piece of their
28 output, which are \emph{simple indices} (\texttt{ContentVideoFrame}
29 and \texttt{ContentAudioFrame}).  Decoders know nothing of \texttt{Time}.
30
31
32 \section{Split of stuff between decoders and player}
33
34 In some ways it seems nice to have decoders which produce the rawest
35 possible data and make the player sort it out (e.g.\ cropping and
36 scaling video, resampling audio).  The resampling is awkward, though,
37 as you really need one resampler per source.  So it might make more sense
38 to put stuff in the decoder.  But then, what's one map of resamplers between friends?
39
40 On the other hand, having the resampler in the player is confusing.  Audio comes in
41 at a frame `position', but then it gets resampled and not all of it may emerge from
42 the resampler.  This means that the position is meaningless, and we want a count
43 of samples out from the resampler (which can be done more elegantly by the decoder's
44 \texttt{\_audio\_position}.
45
46
47 \section{Options for what \texttt{Time} is a function of}
48
49 I've been trying for a while with \texttt{Time} as a wall-clock
50 `real-time' unit.  This means that the following is tricky:
51
52 \begin{enumerate}
53 \item Add content at 29.97 fps
54 \item Length of this content is converted to \texttt{Time} using the
55   current DCP frame rate (which will be 29.97).
56 \item Add more content at 25 fps.
57 \item This causes the DCP frame rate to be changed to 25 fps, and so
58   the first piece of content is now being run slower and so its length
59   changes.
60 \end{enumerate}
61
62 I think this is the cause of content being overlapped in this case.
63
64 It is tempting to solve this by making Time a subdivsion of DCP video
65 frame rate.  This makes things nicer in many ways; you get a 1:1
66 mapping of content video frames to Time in most cases, but not when
67 video frames are skipped to halve the frame rate, say.  In this case
68 you could have a piece of content at 50 fps which is some time $T$
69 long at at DCP rate of 50 fps, but half as long at a DCP rate of 25 fps.
70
71 I'm fairly sure that there is inherently not a nice representation which
72 will obviate the need for things to be recalculated when DCP rate changes.
73
74 On the plus side, lengths in \texttt{Time} are computed on-demand from
75 lengths kept as source frames.
76
77 \end{document}