ao_lavc: fix crash with interleaved audio outputs.
ao_lavc: write the final audio chunks from uninit()

These must be written even if there was no "final frame", e.g. due to
the player being exited with "q".

Although the issue is mostly of theoretical nature, as most audio codecs
don't need the final encoding calls with NULL data. Maybe will be more
relevant in the future.
audio/format: fix doublep sample format

This was accidentally equivalent to floatp.
audio/format: add heuristic to estimate loss on format conversion

The added function af_format_conversion_score() can be used to select
the best sample format to convert to in order to reduce loss and extra
conversion work.

It calculates a "loss" score when going from one format to another, and
for each conversion that needs to be done a certain score is subtracted.
Thus, if you have to convert from one format to a set of other formats,
you can calculate the score for each conversion, and pick the one with
the highest score.

Conversion between int and float is considered the worst case. One odd
consequence is that when converting from s32 to u8 or float, u8 will be
picked.

Test program used to develop this follows:

#define MAX_FMT 200
struct entry {
    const char *name;
    int score;
};

static int compentry(const void *px1, const void *px2)
{
    const struct entry *x1 = px1;
    const struct entry *x2 = px2;
    if (x1->score > x2->score)
        return 1;
    if (x1->score < x2->score)
        return -1;
    return 0;
}

int main(int argc, char *argv[])
{
    for (int n = 0; af_fmtstr_table[n].name; n++) {
        struct entry entry[MAX_FMT];
        int entries = 0;
        for (int i = 0; af_fmtstr_table[i].name; i++) {
            assert(i < MAX_FMT);
            entry[entries].name = af_fmtstr_table[i].name;
            entry[entries].score =
                af_format_conversion_score(af_fmtstr_table[i].format,
                                           af_fmtstr_table[n].format);
            entries++;
        }
        qsort(&entry[0], entries, sizeof(entry[0]), compentry);
        for (int i = 0; i < entries; i++) {
            printf("%s -> %s: %d \n", af_fmtstr_table[n].name,
                   entry[i].name, entry[i].score);
        }
    }
}
ao_lavc: use af_format_conversion_score()

This should allow it to select better fallback formats, instead of
picking the first encoder sample format if ao->format is not equal to
any of the encoder sample formats.

Not sure what is supposed to happen if the encoder provides no
compatible sample format (or no sample format list at all), but in this
case ao_lavc.c still fails gracefully.
encode_lavc: add missing newline in error message
demux: reset EOF flag differently

This should be almost equivalent, but is slightly better because the
EOF flag is reset earlier.
demux_lavf: remove broken and commented byte based seeks

This was originally added for better seeking where libavformat's seek
function won't work well: files with timestamp resets. In these cases,
the code tried to calculate an average bitrate, and then do byte based
seeks by multiplying the seek target time with the bitrate.

Apparently this was unreliable enough that the code was just commented
(and other parts became inactive). Get rid of it.

Note that the player still does byte based seeks in these cases when
doing percent-seeks.
demux: simplify handling of filepos field

demuxer->filepos contains the byte offset of the last read packet. This
is so that the player can estimate the current playback position, if no
proper timestamps are available. Simplify it to use demux_packet->pos in
the generic demuxer code, instead of bothering every demuxer
implementation about it.

(Note that this is still a bit incorrect: it relfects the position of
the last packet read by the demuxer, not that returned to the user. But
that was already broken, and is not that trivial to fix.)
demux: remove unused commands

These were replaced with DEMUXER_CTRL_SWITCHED_TRACKS a while ago.
demux: update a comment
demux_packet: add source stream index

Might be helpful later.
player: don't remove playback status when reinitializing DVB

Also break that line a bit.
ao_null: properly simulate final chunk, add buffer options

Simulate proper handling of AOPLAY_FINAL_CHUNK. Print when underruns
occur (i.e. running out of data). Add some options that control
simulated buffer and outburst sizes.

All this is useful for debugging and self-documentation. (Note that
ao_null always was supposed to simulate an ideal AO, which is the reason
why it fools people who try to use it for benchmarking video.)
player: write final audo chunk only if there is audio left

Don't call ao_play() if there's nothing left. Of course this still asks
the AO to play internally buffered audio by setting drain=true.
af_lavrresample: set cutoff as double, not int

Regression introduced with commit a89549e8.
stream: split out pthread helper function

Also split the function itself into 3.
audio: fix mid-stream audio reconfiguration

Commit 22b3f522 not only redid major aspects of audio decoding, but also
attempted to fix audio format change handling. Before that commit, data
that was already decoded but not yet filtered was thrown away on a
format change. After that commit, data was supposed to finish playing
before rebuilding filters and so on.

It was still buggy, though: the decoder buffer was initialized to the
new format too early, triggering an assertion failure. Move the reinit
call below filtering to fix this.

ad_mpg123.c needs to be adjusted so that it doesn't decode new data
before the format change is actually executed.

Add some more assertions to af_play() (audio filtering) to make sure
input data and configured format don't mismatch. This will also catch
filters which don't set the format on their output data correctly.

Regression due to planar_audio branch.
audio: use the decoder buffer's format, not sh_audio

When the decoder detects a format change, it overwrites the values
stored in sh_audio (this affects the members sample_format, samplerate,
channels). In the case when the old audio data still needs to be
played/filtered, the audio format as identified by sh_audio and the
format used for the decoder buffer can mismatch. In particular, they
will mismatch in the very unlikely but possible case the audio chain is
reinitialized while old data is draining during a format change.

Or in other words, sh_audio might contain the new format, while the
audio chain is still configured to use the old format.

Currently, the audio code (player/audio.c and init_audio_filters) access
sh_audio to get the current format. This is in theory incorrect for the
reasons mentioned above. Use the decoder buffer's format instead, which
should be correct at any point.
vo_vdpau: don't calculate destination alpha when drawing OSD

Same as MPlayer svn commit r36515 "Chose cheaper alpha blend equation."

No idea if this is actually faster, but can't hurt.