Custom Audio Compression Media Foundation Implementation

The idea is to create a new Media Foundation Filter to be used in audio compression/decompression in media foundation MP4 sinkwriter/source reader. I plan to convert Facebook’s Encodec into a MFT.

My problem is the sample descriptor.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> CComPtr<IMFMediaType> ina = ...,myformat = ...;
ina->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM);
ina->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, nch);
ina->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, sr);
int BA = (int)((br / 8) * nch);
ina->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, (UINT32)(sr * BA));
ina->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, BA);
ina->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, br);
ina->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
myformat->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
myformat->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_myformat);
myformat->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, nch);
myformat->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, sr);
hr = myformat->SetBlob(MF_MT_MPEG4_SAMPLE_DESCRIPTION, (UINT8*)cz, sizeof(cz));
hr = myformat->SetUINT32(MF_MT_MPEG4_CURRENT_SAMPLE_ENTRY,0);
hr = wr->AddStream(myformat, &sidx);
LogMediaType(myformat);
hr = wr->SetInputMediaType(sidx, ina, 0);
</code>
<code> CComPtr<IMFMediaType> ina = ...,myformat = ...; ina->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM); ina->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, nch); ina->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, sr); int BA = (int)((br / 8) * nch); ina->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, (UINT32)(sr * BA)); ina->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, BA); ina->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, br); ina->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio); myformat->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio); myformat->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_myformat); myformat->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, nch); myformat->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, sr); hr = myformat->SetBlob(MF_MT_MPEG4_SAMPLE_DESCRIPTION, (UINT8*)cz, sizeof(cz)); hr = myformat->SetUINT32(MF_MT_MPEG4_CURRENT_SAMPLE_ENTRY,0); hr = wr->AddStream(myformat, &sidx); LogMediaType(myformat); hr = wr->SetInputMediaType(sidx, ina, 0); </code>
    CComPtr<IMFMediaType> ina = ...,myformat = ...;
    ina->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM);
    ina->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, nch);
    ina->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, sr);
    int BA = (int)((br / 8) * nch);
    ina->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, (UINT32)(sr * BA));
    ina->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, BA);
    ina->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, br);
    ina->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);

    myformat->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio);
    myformat->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_myformat);
    myformat->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, nch);
    myformat->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, sr);
    hr = myformat->SetBlob(MF_MT_MPEG4_SAMPLE_DESCRIPTION, (UINT8*)cz, sizeof(cz));
    hr = myformat->SetUINT32(MF_MT_MPEG4_CURRENT_SAMPLE_ENTRY,0);

    hr = wr->AddStream(myformat, &sidx);
    LogMediaType(myformat);

    hr = wr->SetInputMediaType(sidx, ina, 0);

This succeeds in the sink writer. After calling BeginWriting, I reset the as I described in this related question:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> CComPtr<IMFMediaSink> pSink;
CComPtr<IMFStreamSink> s2;
CComPtr<IMFMediaTypeHandler> mth;
CComPtr<IMFMediaType> mt2;
wr->GetServiceForStream(MF_SINK_WRITER_MEDIASINK, GUID_NULL, IID_PPV_ARGS(&pSink));
pSink->GetStreamSinkByIndex(0, &s2);
s2->GetMediaTypeHandler(&mth);
mth->GetCurrentMediaType(&mt2);
LogMediaType(mt2);
mt2->SetBlob(MF_MT_MPEG4_SAMPLE_DESCRIPTION, (UINT8*)cz, sizeof(cz));
mt2->SetUINT32(MF_MT_MPEG4_CURRENT_SAMPLE_ENTRY, 0);
mth->SetCurrentMediaType(mt2);
mt2 = 0;
mth->GetCurrentMediaType(&mt2);
LogMediaType(mt2);
</code>
<code> CComPtr<IMFMediaSink> pSink; CComPtr<IMFStreamSink> s2; CComPtr<IMFMediaTypeHandler> mth; CComPtr<IMFMediaType> mt2; wr->GetServiceForStream(MF_SINK_WRITER_MEDIASINK, GUID_NULL, IID_PPV_ARGS(&pSink)); pSink->GetStreamSinkByIndex(0, &s2); s2->GetMediaTypeHandler(&mth); mth->GetCurrentMediaType(&mt2); LogMediaType(mt2); mt2->SetBlob(MF_MT_MPEG4_SAMPLE_DESCRIPTION, (UINT8*)cz, sizeof(cz)); mt2->SetUINT32(MF_MT_MPEG4_CURRENT_SAMPLE_ENTRY, 0); mth->SetCurrentMediaType(mt2); mt2 = 0; mth->GetCurrentMediaType(&mt2); LogMediaType(mt2); </code>
        CComPtr<IMFMediaSink> pSink;
        CComPtr<IMFStreamSink> s2;
        CComPtr<IMFMediaTypeHandler> mth;
        CComPtr<IMFMediaType> mt2;
        wr->GetServiceForStream(MF_SINK_WRITER_MEDIASINK, GUID_NULL, IID_PPV_ARGS(&pSink));
        pSink->GetStreamSinkByIndex(0, &s2);
        s2->GetMediaTypeHandler(&mth);
        mth->GetCurrentMediaType(&mt2);
        LogMediaType(mt2);
        mt2->SetBlob(MF_MT_MPEG4_SAMPLE_DESCRIPTION, (UINT8*)cz, sizeof(cz));
        mt2->SetUINT32(MF_MT_MPEG4_CURRENT_SAMPLE_ENTRY, 0);
        mth->SetCurrentMediaType(mt2);
        mt2 = 0;
        mth->GetCurrentMediaType(&mt2);
        LogMediaType(mt2);

Finalize() succeeds and I have an MP4. Now this MP4 cannot be read back with a source reader (not supported) and ffmpeg -i shows

Stream #0:0(und): Audio: none (ecdc / 0x63646365), 48000 Hz, 0 channels, 288 kb/s (default)
.

This means that my problem is the sample descriptor, which currently is this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>UINT8 cz[100] = {
0x0,0x0,0x0,0x64, // len
0x73,0x74,0x73,0x64, // stsd
0x0,0x0,0x0,0x0, // verflag
0x0,0x0,0x0,0x1, // num
0x0,0x0,0x0,0x54, // len
'e','c','d','c', // code
0x0 (76 bytes)
};
</code>
<code>UINT8 cz[100] = { 0x0,0x0,0x0,0x64, // len 0x73,0x74,0x73,0x64, // stsd 0x0,0x0,0x0,0x0, // verflag 0x0,0x0,0x0,0x1, // num 0x0,0x0,0x0,0x54, // len 'e','c','d','c', // code 0x0 (76 bytes) }; </code>
UINT8 cz[100] = {
0x0,0x0,0x0,0x64, // len
0x73,0x74,0x73,0x64, // stsd
0x0,0x0,0x0,0x0, // verflag
0x0,0x0,0x0,0x1, // num
0x0,0x0,0x0,0x54, // len
'e','c','d','c', // code
0x0 (76 bytes)
    };

So obviously I must put something after the ‘ecdc’ string. What would that be?
Is this a form of, say, WAVEFORMATEX?

When I copy the data of an existing descriptor like AAC, the mp4 can be read and mediainfo/ffmpeg shows AAC/related info, which of course can’t be played back because the stream is not AAC, but the file is parsed.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật