<commit_before>#include "webmmfsource.hpp"
#include "webmmfstream.hpp"
#include "webmmfstreamaudio.hpp"
//#include "webmtypes.hpp"
#include "vorbistypes.hpp"
#include <mfapi.h>
#include <mferror.h>
#include <cassert>
#include <cmath>
#include <comdef.h>

_COM_SMARTPTR_TYPEDEF(IMFStreamDescriptor, __uuidof(IMFStreamDescriptor));
_COM_SMARTPTR_TYPEDEF(IMFMediaType, __uuidof(IMFMediaType));
_COM_SMARTPTR_TYPEDEF(IMFMediaTypeHandler, __uuidof(IMFMediaTypeHandler));
_COM_SMARTPTR_TYPEDEF(IMFMediaBuffer, __uuidof(IMFMediaBuffer));


namespace WebmMfSourceLib
{


HRESULT WebmMfStreamAudio::CreateStreamDescriptor(
    mkvparser::Track* pTrack_,
    IMFStreamDescriptor*& pDesc)
{
    assert(pTrack_);
    assert(pTrack_->GetType() == 2);  //audio

    using mkvparser::AudioTrack;
    AudioTrack* const pTrack = static_cast<AudioTrack*>(pTrack_);

    const char* const codec = pTrack->GetCodecId();
    assert(codec);

    if (_stricmp(codec, "A_VORBIS") != 0)  //weird
    {
        pDesc = 0;
        return E_FAIL;
    }

    const LONGLONG id_ = pTrack->GetNumber();
    const DWORD id = static_cast<DWORD>(id_);

    IMFMediaTypePtr pmt;

    HRESULT hr = MFCreateMediaType(&pmt);
    assert(SUCCEEDED(hr));
    assert(pmt);

    hr = pmt->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
    assert(SUCCEEDED(hr));

    hr = pmt->SetGUID(MF_MT_SUBTYPE, VorbisTypes::MEDIASUBTYPE_Vorbis2);
    assert(SUCCEEDED(hr));

    //TODO: set this too?
    //hr = pmt->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, FALSE);
    //assert(SUCCEEDED(hr));

    hr = pmt->SetUINT32(MF_MT_COMPRESSED, TRUE);
    assert(SUCCEEDED(hr));

    const __int64 channels_ = pTrack->GetChannels();
    assert(channels_ > 0);

    const UINT32 channels = static_cast<UINT32>(channels_);

    hr = pmt->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, channels);
    assert(SUCCEEDED(hr));

    const double rate = pTrack->GetSamplingRate();
    assert(rate > 0);

    hr = pmt->SetDouble(MF_MT_AUDIO_FLOAT_SAMPLES_PER_SECOND, rate);
    assert(SUCCEEDED(hr));

    double int_rate_;

    const double frac_rate = modf(rate, &int_rate_);
    frac_rate;
    assert(frac_rate == 0);

    const UINT32 int_rate = static_cast<UINT32>(int_rate_);

    hr = pmt->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, int_rate);
    assert(SUCCEEDED(hr));

    const __int64 bits_per_sample_ = pTrack->GetBitDepth();
    UINT32 bits_per_sample;

    if (bits_per_sample_ <= 0)
        bits_per_sample = 0;
    else
    {
        assert((bits_per_sample_ % 8) == 0);
        bits_per_sample = static_cast<UINT32>(bits_per_sample_);

        hr = pmt->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, bits_per_sample);
        assert(SUCCEEDED(hr));
    }

    size_t cp_size;
    const BYTE* const cp = pTrack->GetCodecPrivate(cp_size);
    assert(cp);
    assert(cp_size);

    const BYTE* const begin = cp;
    const BYTE* const end = begin + cp_size;

    const BYTE* p = begin;<commit_msg>webmmfsource: more strict vetting of audio stream descriptor<commit_after>#include "webmmfsource.hpp"
#include "webmmfstream.hpp"
#include "webmmfstreamaudio.hpp"
//#include "webmtypes.hpp"
#include "vorbistypes.hpp"
#include <mfapi.h>
#include <mferror.h>
#include <cassert>
#include <cmath>
#include <comdef.h>

_COM_SMARTPTR_TYPEDEF(IMFStreamDescriptor, __uuidof(IMFStreamDescriptor));
_COM_SMARTPTR_TYPEDEF(IMFMediaType, __uuidof(IMFMediaType));
_COM_SMARTPTR_TYPEDEF(IMFMediaTypeHandler, __uuidof(IMFMediaTypeHandler));
_COM_SMARTPTR_TYPEDEF(IMFMediaBuffer, __uuidof(IMFMediaBuffer));


namespace WebmMfSourceLib
{


HRESULT WebmMfStreamAudio::CreateStreamDescriptor(
    mkvparser::Track* pTrack_,
    IMFStreamDescriptor*& pDesc)
{
    assert(pTrack_);
    assert(pTrack_->GetType() == 2);  //audio

    using mkvparser::AudioTrack;
    AudioTrack* const pTrack = static_cast<AudioTrack*>(pTrack_);

    pDesc = 0;

    const char* const codec = pTrack->GetCodecId();

    if (codec == 0)  //weird
        return E_FAIL;

    if (_stricmp(codec, "A_VORBIS") != 0)  //weird
        return E_FAIL;

    const LONGLONG id_ = pTrack->GetNumber();
    const DWORD id = static_cast<DWORD>(id_);

    IMFMediaTypePtr pmt;

    HRESULT hr = MFCreateMediaType(&pmt);
    assert(SUCCEEDED(hr));
    assert(pmt);

    hr = pmt->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
    assert(SUCCEEDED(hr));

    hr = pmt->SetGUID(MF_MT_SUBTYPE, VorbisTypes::MEDIASUBTYPE_Vorbis2);
    assert(SUCCEEDED(hr));

    //TODO: set this too?
    //hr = pmt->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, FALSE);
    //assert(SUCCEEDED(hr));

    hr = pmt->SetUINT32(MF_MT_COMPRESSED, TRUE);
    assert(SUCCEEDED(hr));

    const __int64 channels_ = pTrack->GetChannels();

    if (channels_ <= 0)  //weird
        return E_FAIL;

    const UINT32 channels = static_cast<UINT32>(channels_);

    hr = pmt->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, channels);
    assert(SUCCEEDED(hr));

    const double rate = pTrack->GetSamplingRate();

    if (rate <= 0)
        return E_FAIL;

    hr = pmt->SetDouble(MF_MT_AUDIO_FLOAT_SAMPLES_PER_SECOND, rate);
    assert(SUCCEEDED(hr));

    double int_rate_;

    const double frac_rate = modf(rate, &int_rate_);
    frac_rate;
    assert(frac_rate == 0);

    const UINT32 int_rate = static_cast<UINT32>(int_rate_);

    hr = pmt->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, int_rate);
    assert(SUCCEEDED(hr));

    const __int64 bits_per_sample_ = pTrack->GetBitDepth();
    UINT32 bits_per_sample;

    if (bits_per_sample_ <= 0)
        bits_per_sample = 0;
    else
    {
        assert((bits_per_sample_ % 8) == 0);
        bits_per_sample = static_cast<UINT32>(bits_per_sample_);

        hr = pmt->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, bits_per_sample);
        assert(SUCCEEDED(hr));
    }

    size_t cp_size;

    const BYTE* const cp = pTrack->GetCodecPrivate(cp_size);

    if ((cp == 0) || (cp_size == 0))
        return E_FAIL;

    const BYTE* const begin = cp;
    const BYTE* const end = begin + cp_size;

    const BYTE* p = begin;
{'fraction_non_alphanumeric': 0.12261944537253591, 'fraction_numerical': 0.012863347811560307, 'mean_word_length': 4.043807919123841, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3395290', 'n_tokens_mistral': 2328, 'n_tokens_neox': 2247, 'n_words': 389}