Live setup

If you want to produce a live feed of tiled video, you can either:

- produce short segments, package them and dash them using `-dash-live`, `dash-ctx` and `-subdur`, see discussion [here](https://github.com/gpac/gpac/issues/1648)
- produce a live session with a [tilesplit](tilesplit) filter.

GPAC does not have a direct wrapper for Kvazaar, but you can either:

- use a FFmpeg build with Kvazaar enabled (`--enable-libkvazaar` in FFmpeg's configure) - check GPAC support using `gpac -h ffenc:libkvazaar`
- use an external grab+Kvazaar encoding and pipe its output into GPAC.

### Piping encoder output
Using external Kvazaar encoding, you will need to pipe its output into GPAC and inject a tilesplit filter before the dasher:
```
MP4Box -dash-live 1000 -profile live -out live.mpd source_pipe:@tilesplit 

gpac -i source_pipe tilesplit -o live.mpd

```
The drawback of this approach is that making multiple quality encodings becomes quite tricky and heavy.

### Kvazaar as a GPAC filter
When using FFmpeg Kvazaar encoding in GPAC you have more flexibility as the encoder is just another filter in the chain. Options are passed through `kvazar-params` option of libavcodec. Your typical options will be, for a 3x3 tiling: `kvazaar-params=tiles=3x3,slices=tiles,mv-constraint=frametilemargin,rc-algorithm=lambda` .

You can then setup a filter chain (here a 1 mbps encoding):
```
gpac -i source
 enc:c=libkvazaar:b=1m::kvazaar-params=tiles=3x3,slices=tiles,mv-constraint=frametilemargin,rc-algorithm=lambda
 -o dest
```

You can split the output of the encoder directly:
```
gpac -i source
 enc:c=libkvazaar:b=1m::kvazaar-params=tiles=3x3,slices=tiles,mv-constraint=frametilemargin,rc-algorithm=lambda
 tilesplit -o dest
```

The following is an example of a live tiled encoding from webcam using 2 qualities. Do not change Kvazaar options of the two instances, this could result in incompatible decoder configurations preventing tile adaptation to work.


```
gpac
  #kvazaar parameters passed as global meta arguments since they apply to all our kvazaar instances
 --kvazaar-params=tiles=3x3,slices=tiles,mv-constraint=frametilemargin,rc-algorithm=lambda 

 -i video:// 
 
 #setup a video rescaler for pixel format conversion (see below)
 ffsws:FID=S
  
 #first encoding at 1 mbps with tile splitter, forcing intra every second (see below)
 enc:SID=S:c=libkvazaar:b=1m:fintra=1:rc @ tilesplit:FID=1
 
 #second encoding at 200 kbps with tile splitter, forcing intra every second (see below)
 enc:SID=S:c=libkvazaar:b=200k:fintra=1:rc @ tilesplit:FID=2

 #dasher in dynamic mode, consuming only PIDs from tile splitters
 -o live.mpd:SID=1,2:dmode=dynamic
```


The resulting filter graph is quite fun (use `-graph` to check it) and shows:

- only one (or 0 depending on your webcam formats) pixel converter filter is used in the chain to feed both Kvazaar instances
- all tile PIDs (and only them) connecting to the dasher filter
- 21 output PIDs of the dasher: one for MPD, 2 x (1+3x3) media PIDs.

_Note_
In the above command, the `ffsws` filter is injected to make sure a single video conversion will be used. If not set, the PID capability negotiation will create a dedicated adaptation chain for each connection, resulting in two `ffsws` instances (working but less efficient).

As usual, the output does not need to be a file, you can output as an HTTP server or as an HTTP PUT sink, as discussed [here](LL-DASH), or even to a [ROUTE](route) session !

__Warning__
- You need to specify `rc-algorithm` option for Kvazaar to handle the `b` option. If not setting it, you will need to pass the `bitrate` option as part of the `kvazar-params` on each encoder instance, but then you won't be able to use global options.
- You MUST specify `rc` option for GOPs to be properly enforced in time, Kvazaar cannot currently close a GOP at any other place than its specified `period` option, it requires encoder reset. If you are sure no source frame drop will happen, you can omit `:rc:fintra=` and add `period=N` to Kvazaar options.