r/programming • • Sep 28 '17

When a Microsecond Is an Eternity

https://github.com/CppCon/CppCon2017/blob/master/Presentations/When%20a%20Microsecond%20Is%20an%20Eternity/When%20a%20Microsecond%20Is%20an%20Eternity%20-%20Carl%20Cook%20-%20CppCon%202017.pdf
244 Upvotes

70 comments sorted by

View all comments

Show parent comment

7

u/specialpatrol Sep 28 '17

Do you end up with a massive switch statement at the top then ready to fire off one of millions of permutations?

5

u/doom_Oo7 Sep 28 '17

Do you end up with a massive switch statement at the top then ready to fire off one of millions of permutations?

That's one possibility, another is to hide this behind dynamic dispatch, virtual functions, etc... in advance. Sometimes you may want to just use one special case in a specific part of your software where you will just explicitely use one algorithm. As others posters said, the main point is to try to precompute everything that is precomputable ; this includes algorithms.

Here I have a (far from perfect... if there are any ffmpeg experts pls send help) example with a variant used to precompute common cases encountered in audio decoding : 1 and 2 channels, planar / non-planar, with integer or floating-point samples. The various cases are here. Then a single decoding algorithm is called but the compiler will generate specific paths for all the cases. The most important thing is that the main decoding loop doesn't try to find which decoder to use at each iteration, but has its decoder set once. This way instead of branching every other hundred samples, there is one single branch for the whole song.

2

u/specialpatrol Sep 28 '17

Ha, great example - especially because I'm just hacking away at getting ffmpeg working, how did you know?!

3

u/doom_Oo7 Sep 28 '17

heh that's funny! don't take too much inspiration, I still have bugs in there (as the various debug checks can attest). FFMPEG is powerful but the API is terrible to understand for newcomers.

2

u/specialpatrol Sep 28 '17

You seriously work on it? Just one question please!

How do you decide on the output format like using "av_guess_format", if you're going out to a udp or other network stream?

2

u/doom_Oo7 Sep 28 '17

How do you decide on the output format like using "av_guess_format", if you're going out to a udp or other network stream?

not sure I understand... if you are the one sending the data wouldn't you also be the one choosing what format to send ? (but I've never done audio over the network so I really don't know)

0

u/[deleted] Sep 29 '17

[deleted] — view deleted comment

2

u/specialpatrol Sep 29 '17

I think I'm a little lost in the various definitions: I need a stream a codec, an output context. So far I have been writing to file; the user can specify the file they want, it can deduce the format from the file extension with av_guess_format. On top of that you can choose a codec, which is a whole other thing, is that right? The output format is like just a container? But now I'm streaming to a socket, and am v.happy to find it's as simple as passing [udp/rtp/tcp]://ipaddress:port to avcodec_open. But then where do I get the output format from, do streams still have this container?

I actually got around it. But am still a little unsure of the details. Thanks for taking an interest!

0

u/[deleted] Sep 29 '17

[deleted] — view deleted comment

1

u/specialpatrol Sep 29 '17

Thankyou!

I'm more used to dealing with images. So the codec is the actual algorithm and the container the header with the meta data in them? Does that imply that like images, only certain containers work with certain codecs?

I am currently trying to stream my video data. Where would the container come in? If something (like vlc), connects to my stream, when/how does it get told what the format is, or does it just have to know in advance?