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