Allocating Buffers for File Reading

[The feature associated with this page, Windows Media Format 11 SDK, is a legacy feature. It has been superseded by Source Reader and Sink Writer. Source Reader and Sink Writer have been optimized for Windows 10 and Windows 11. Microsoft strongly recommends that new code use Source Reader and Sink Writer instead of Windows Media Format 11 SDK, when possible. Microsoft suggests that existing code that uses the legacy APIs be rewritten to use the new APIs if possible.]

In the most basic file-reading scenario, the buffers used to deliver samples are allocated by the reading object (the reader object or the synchronous reader object). You can, however, allocate buffers yourself. For more information about the benefits of allocating your own buffers, see User Allocated Sample Support.

To use your own buffers for file reading, perform the following steps.

  1. Implement a callback or callbacks for the reader to call when it needs a buffer. If you are reading output samples, use IWMReaderAllocatorEx::AllocateForOutputEx. If you are reading stream samples, use IWMReaderAllocatorEx::AllocateForStreamEx. Include whatever logic for managing buffers that suits your application.
  2. Allocate a pool of buffers that you will use for file reading.
  3. Set up the reader or synchronous reader for reading. For more information see Reading Files with the Asynchronous Reader or Reading Files with the Synchronous Reader.
  4. Before beginning writing, call IWMReaderAdvanced::SetAllocateForOutput or IWMReaderAdvanced::SetAllocateForStream for each output and stream for which you are allocating buffers using the reader object. For the synchronous reader, call IWMSyncReader2::SetAllocateForOutput or IWMSyncReader2::SetAllocateForStream instead.
  5. Begin reading the file.

The reading object will make calls to the appropriate allocator callback and get samples from your application. Your buffer management logic must include a way to signal that a buffer is free to be used again. Typically, a buffer is put back into the pool when its contents are rendered. Depending upon your application, you may need just a few buffers in the pool or many.

Reading ASF Files