]> git.sesse.net Git - nageru-docs/blob - hdmisdi.rst
Document the imperative 1.9.0 functions.
[nageru-docs] / hdmisdi.rst
1 HDMI/SDI output
2 ===============
3
4 Sometimes, what you want from a video mixer isn't a stream, just another
5 output that goes to e.g. a projector—or you might want the live stream,
6 but also a monitor output on a separate display. You could of course play
7 the stream on another PC, but for many uses, the end-to-end latency is
8 too high, and you might not want to involve a full extra PC just for this
9 anyway.
10
11 Thus, since 1.5.0, Nageru supports using a spare output card for HDMI/SDI
12 output, turning it into a simple, reasonably low-latency audio/video switcher.
13
14
15 Setting up HDMI/SDI output
16 --------------------------
17
18 Turning on HDMI/SDI output is simple; just right-click on the live view and
19 select the output card. (Equivalently, you can access the same functionality
20 from the *Video* menu in the regular menu bar, or you can give the
21 *--output-card=* parameter on the command line.) Currently, this is supported
22 for DeckLink cards only (PCI/Thunderbolt), as the precise output protocol for
23 the Intensity Shuttle cards is still unknown. The stream and recording will
24 keep running just as before.
25
26 A video mode will automatically be picked for you, favoring 59.94 fps if possible,
27 but you can change the mode on-the-fly to something else if you'd like,
28 as long as the resolution matches with what you've set up at program start.
29 Note that whenever HDMI/SDI output is active, the output card will be the
30 master clock; you cannot change it to any of the input cards. This also means
31 that the frame rate you choose here will determine the frame rate for the
32 stream.
33
34
35 A note on latency
36 -----------------
37
38 For a regular stream, a few seconds of latency is usually acceptable
39 (clients will typically buffer at least a few seconds), and thus,
40 working hard to shave off single frames' worth of latency is not worth it.
41 However, for a live output, every millisecond of latency counts;
42 if you have a stage with a projector behind it, 500 ms latency on
43 the projector looks distinctly out of sync with what's happening
44 on stage. Thus, HDMI/SDI output typically has much stricter
45 latency demands than usual streaming.
46
47 Nageru is capable of low latency operation, but not extremely low latency;
48 for instance, it waits for an entire frame to arrive before processing
49 it. (This is a complexity and flexibility tradeoff; anything else would make
50 e.g. scaling nearly impossible.) Well-designed hardware switcher setups can do
51 **cut-through switching** to get latency down to as little as one frame [#]_ or
52 less, ie. 16.7 ms at 60 fps; Nageru can get down to 2–3 frames (50 ms)
53 given the right hardware, and in general, 100 ms isn't hard. 
54
55 .. [#] Since almost all latency in a realtime video setup is caused by processing of various
56        forms and not by length of the cable, most forms of latency will be
57        proportional to the length of a frame. Thus, one will often see latency
58        calculated in terms of number of frames, not milliseconds, and video at
59        higher frame rates will often see less delay. Networking (and by extension
60        streaming) is different; there, jitter and delay is more often caused
61        by propagation and administrative delays, and latency is more often
62        independent of the frame rate.
63
64
65 Typical sources of latency
66 ..........................
67
68 This section aims to illuminate some of the sources of latency and how to deal
69 with them. Note that often, latency is at odds with throughput, and so,
70 tradeoffs must be made. The most important sources of latency are:
71
72  - **Frame transmission latency:** Unlike computer networks, HDMI and SDI
73    transmit their frames pretty much in real time, ie., sending one frame
74    takes one frame of time. For cut-through switching (which includes
75    HDMI → SDI conversion and the other way around), this doesn't really
76    matter, but Nageru has to receive the entire frame before it can start
77    processing it (and by extension, send the result frame out). Thus, you will
78    typically get one frame of latency just by having Nageru, or really any
79    switcher/mixer with digital effects, in the chain at all.
80
81  - **Jitter and queuing latency:** Unless you are lucky enough to have an
82    all-SDI setup where everything runs off of a shared reference clock,
83    frames on different devices, as well as on the output, will be at random
84    offsets from each other (and also drifting slowly, even if they are at
85    the same frame rate). Thus, some sort of *input queue* is needed for each
86    input card, and the time a frame spends in the queue before being picked
87    out for processing is by definition extra latency. (Note that this means
88    that latency is not a single number for the chain as a whole, but can
89    vary by input.)
90
91  - **Processing latency:** By definition, processing of each frame has to take
92    less than one frame's worth of time, or else the system can't keep up.
93    But if you have a fast GPU and/or do little processing, you can spend
94    significantly less. Thus, if you're after the lowest possible latency,
95    a faster GPU might help you shave off a fraction of a frame here.
96
97  - **Output latency:** Finally, cards have their own output queue,
98    and some will expect there to be multiple frames in it before outputting anything.
99    This is outside Nageru's control, unfortunately, but can easily add 2–3
100    frames of latency. If you want to avoid this, look for Blackmagic's “4K” series of
101    cards, which are of a newer, lower-latency design than the previous cards.
102    The 4K series in this context include everything that have “4K” in their
103    names, plus the Mini Recorder, Duo 2 and Quad 2 devices.
104
105 Controlling latency
106 ...................
107
108 Of the different sources of latency outlined in the previous section,
109 the only one that is really under your control (short of buying faster
110 or better hardware) is the input queue latency. By default, Nageru
111 attempts to strike a balance between reducing latency and having to
112 drop frames due to jitter; by looking at each queue's input length
113 history, it attempts to find a “safe queue limit”, above which it
114 can drop frames without risking underrun (which requires duplicating
115 frames). However, if latency is more important to you than 100% smooth
116 motion, you can override this by using the *--max-input-queue-frames=*
117 flag; this is a hard limit on the number of frames that can be kept
118 in the queue, on top of Nageru's own heuristics. It cannot be set lower
119 than 1, or else all incoming frames would immediately get dropped
120 on arrival.
121
122 However, even though the other factors are largely outside your control,
123 you still have to *account* for them. Nageru needs to know when to begin
124 processing a frame, and it cannot do this adaptively; you need to give
125 Nageru a latency budget for processing and output queueing, which tells it when
126 to start processing a frame (by picking out the input frames available at that
127 time). If a frame isn't processed in time for the output card to pick it up,
128 it will be dropped, which means its effort was wasted. (Nageru will tell you
129 on the terminal if this happens.) The latency budget is set by
130 *--output-buffer-frames=*, where the default is a pretty generous 6.0,
131 or 100 ms at 60 fps; if you want lower latency, this you probably want
132 to adjust this value down to the point where Nageru starts complaining about
133 dropped or late frames, and then a bit up again to get some margin.
134 (But see the part about `audio latency <audio-latency>` below.) Note that
135 the value can be fractional.
136
137 As an exception to the above, Nageru also allows *slop*; if the frame is
138 late but only a little (ie., less than the slop), it will give it on to the
139 output card nevertheless and hope for forgiveness, which may or may not
140 cause it to be displayed. The slop is set with *--output-slop-frames=*,
141 where the default is 0.5 frames.
142
143
144 .. _audio-latency:
145
146 Audio latency
147 .............
148
149 Since Nageru does not require synchronized audio sources, neither to video
150 nor to each other (which would require a common, locked reference clock for all
151 capture and sound cards), it needs to *resample* incoming audio to match
152 the rate of the master video clock. To avoid buffer underruns caused by
153 uneven delivery of input audio, each card needs an audio input queue,
154 just like the video input queue; by default, this is set to 100 ms, which then
155 acts as a lower bound on your latency.
156
157 If you want to reduce video latency, you will probably want to reduce audio
158 latency correspondingly, or audio will arrive too late to be heard. You can
159 adjust the audio latency with the *--audio-queue-length-ms=* flag, but notice
160 that this value is in milliseconds, not in frames.
161
162 Audio and video queue lengths do not need to match exactly; the two streams
163 (audio and video) will be synchronized at playback, both for network streaming
164 and for HDMI/SDI output.
165
166
167 .. _measuring-latency:
168
169 Measuring latency
170 .................
171
172 In order to optimize latency, it can be useful to measure it, but for most
173 people, it's hard to measure delays precisely enough to distinguish reliably
174 between e.g. 70 and 80 milliseconds by eye alone. Nageru gives you some simple
175 tools that will help.
176
177 The most direct is the flag *--print-video-latency*. This samples, for every
178 100th frame, the latency of that frame through Nageru. More precisely,
179 it measures the wall clock time from the point where the frame is received from
180 the input card driver (and put into the input queue) to up to four different
181 points:
182
183  * **Mixer latency:** The frame is done processing on the GPU.
184  * **Quick Sync latency:** The frame is through :ref:`VA-API H.264 encoding <digital-intermediate>`
185    and ready to be muxed to disk. (Note that the mux might still be waiting
186    for audio before actually outputting the frame.)
187  * **x264 latency:** The frame is through :ref:`x264 encoding <transcoded-streaming>`
188    and ready to be muxed to disk and/or the network. (Same caveat about the
189    mux as the previous point.)
190  * **DeckLink output latency:** The HDMI/SDI output card reports that it has
191    shown the frame.
192
193 As every output frame can depend on multiple input frames, each with different
194 input queue latencies, latencies will be measured for each of them, and the
195 lowest and highest will be printed. Do note that the measurement is still done
196 over a single *output* frame; it is *not* a measurement over the last 100
197 output frames, even though the statistics are only printed every 100th.
198
199 For more precise measurements, you can use Prometheus metrics to get percentiles
200 for all of these points, which will measure over all frames (over a one-minute
201 window). This yields more precise information than sampling every 100 frames,
202 but setting up Prometheus and a graphic tool is a bit more work, and usually not
203 worth it for simple measurement. For more information, see :doc:`monitoring`.
204
205 Another trick that can be useful in some situations is *looping* your signal,
206 ie., connecting your output back into your input. This allows you to measure
207 delays that don't happen within Nageru itself, like any external converters,
208 delays in the input driver, etc.. (It can also act as a sanity check to make
209 sure your A/V chain passes the signal through without quality degradation,
210 if you first set up a static picture as a signal and then switch to the loop
211 input to verify that the signal stays stable without color e.g. shifts [#]_.
212 See the section on :doc:`the frame analyzer <analyzer>` for other ways of
213 debugging signal integrity.)
214
215 For this, the *timecode output* is useful; you can turn it on from the Video
216 menu, or through the command-line flag *--timecode-stream*. (You can also
217 output it to standard output with the flag *--timecode-stdout*.) It contains
218 some information about frame numbers and current time of day; if you activate
219 it, switch to the loop input and then deactivate it while still holding the
220 loop input active, the timecode will start repeating with roughly the
221 same length as your latency. (It can't be an exact measurement, as delay is
222 frequently fractional, and a loop length cannot be.) The easiest way to find
223 the actual length is to look at the recorded video file by e.g. dumping each
224 frame to an image file and looking at the sequence.
225
226 In general, using Nageru's own latency measurement is both the simplest and
227 the most precise. However, the timecode is a useful supplement, since it
228 can also test external factors, such as network stream latency.
229
230 .. [#] If you actually try this with Nageru, you will see some
231        dark “specks” slowly appear in the image. This is a consequence of
232        small roundoff errors accumulating over time, combined with Nageru's
233        static dither pattern that causes rounding to happen in the same
234        direction each time. The dithering used by Nageru is a tradeoff between
235        many factors, and overall helps image quality much more than it
236        hurts, but in the specific case of an ever-looping signal, it will
237        cause such artifacts.