Repository: LadybirdBrowser/ladybird
File: Libraries/LibMedia/FFmpeg/FFmpegAudioDecoder.cpp (C++)

Code before:
void FFmpegAudioDecoder::signal_end_of_stream()
{
    m_packet->data = nullptr;
    m_packet->size = 0;
    m_packet->pts = 0;
    m_packet->dts = 0;

    auto result = avcodec_send_packet(m_codec_context, m_packet);
    VERIFY(result == 0 || result == AVERROR_EOF);
}

template<typename T>
static float float_sample_from_frame_data(u8** data, size_t plane, size_t index);

template<>
float float_sample_from_frame_data<u8>(u8** data, size_t plane, size_t index)
{
    return static_cast<float>(data[plane][index] - 128) / 128;
}

template<typename T>
requires(IsSigned<T>)
static float float_sample_from_frame_data(u8** data, size_t plane, size_t index)
{
    auto* pointer = reinterpret_cast<T*>(data[plane]);
    i64 full_scale = 1LL << ((sizeof(T) * 8) - 1);
    return static_cast<float>(pointer[index]) / static_cast<float>(full_scale);
}

template<typename T>
requires(IsFloatingPoint<T>)
static float float_sample_from_frame_data(u8** data, size_t plane, size_t index)
{
    auto* pointer = reinterpret_cast<T*>(data[plane]);
    return pointer[index];
}

DecoderErrorOr<void> FFmpegAudioDecoder::write_next_block(AudioBlock& block)
{
    auto result = avcodec_receive_frame(m_codec_context, m_frame);

    switch (result) {
    case 0: {
        if (m_frame->sample_rate <= 0)
            return DecoderError::corrupted("FFmpeg decoder created a packet with an invalid sample rate"sv);

        auto timestamp = AK::Duration::from_microseconds(m_frame->pts);

        auto channel_map_result = av_channel_layout_to_channel_map(m_frame->ch_layout);
        if (channel_map_result.is_error())
            return DecoderError::with_description(DecoderErrorCategory::NotImplemented, channel_map_result.error().string_literal());

Review comment (suggestion):
```suggestion
    constexpr float full_scale = NumericLimits<MakeUnsigned<T>>::max() / 2;
```

I think this should work, right? constexpr allows the compiler to verify the cast at compile time, so I think it shouldn't result in any warnings.

Code after:
void FFmpegAudioDecoder::signal_end_of_stream()
{
    m_packet->data = nullptr;
    m_packet->size = 0;
    m_packet->pts = 0;
    m_packet->dts = 0;

    auto result = avcodec_send_packet(m_codec_context, m_packet);
    VERIFY(result == 0 || result == AVERROR_EOF);
}

template<typename T>
static float float_sample_from_frame_data(u8** data, size_t plane, size_t index);

template<>
float float_sample_from_frame_data<u8>(u8** data, size_t plane, size_t index)
{
    return static_cast<float>(data[plane][index] - 128) / 128;
}

template<typename T>
requires(IsSigned<T>)
static float float_sample_from_frame_data(u8** data, size_t plane, size_t index)
{
    auto* pointer = reinterpret_cast<T*>(data[plane]);
    constexpr float full_scale = NumericLimits<MakeUnsigned<T>>::max() / 2;
    return static_cast<float>(pointer[index]) / static_cast<float>(full_scale);
}

template<typename T>
requires(IsFloatingPoint<T>)
static float float_sample_from_frame_data(u8** data, size_t plane, size_t index)
{
    auto* pointer = reinterpret_cast<T*>(data[plane]);
    return pointer[index];
}

DecoderErrorOr<void> FFmpegAudioDecoder::write_next_block(AudioBlock& block)
{
    auto result = avcodec_receive_frame(m_codec_context, m_frame);

    switch (result) {
    case 0: {
        if (m_frame->sample_rate <= 0)
            return DecoderError::corrupted("FFmpeg decoder created a packet with an invalid sample rate"sv);

        auto timestamp = AK::Duration::from_microseconds(m_frame->pts);

        auto channel_map_result = av_channel_layout_to_channel_map(m_frame->ch_layout);
        if (channel_map_result.is_error())
            return DecoderError::with_description(DecoderErrorCategory::NotImplemented, channel_map_result.error().string_literal());