home :: overview


MPEG2Event is a C# library intended to facilitate rapid prototyping of MPEG-2 analysis tools. Unlike other MPEG-2 decoding libraries which are designed for performance, MPEG2Event sacrifices parsing speed in order maximize flexibility and expose the coding elements contained within an MPEG-2 stream at a number of different granularities.

MPEG2Event provides an event-based architecture. As the media stream is parsed, the library constructs and publishes an event for each coding element encountered. By using the C# publish/subscribe event model, your code is notified via delegate methods when these events occur. Event types are organized in a hierarchy allowing you to subscribe in a number of different ways including:

  • A very specific atomic coding element (e.g., the horizontal size field of a picture header, the vertical component of a motion vector).
  • A compound coding element comprised of smaller coding elements (e.g., the entire picture header, a motion vector, a macroblock).
  • A general class of coding elements (e.g., all headers, all atomic coding elements, etc.).

Currently, MPEG2Event only works with MPEG-2 elementary video streams. You must use other tools to demultiplex the video stream out from a transport or program stream. The MPEG2Event architecture, however, is fairly general and could be easily extended to work with transport, program, or audio streams.

Basic Usage

For most uses of the library, the following basic approach can be followed:

  • Wrap the source of bits into a "BitStream" object.
    The BitStream class provides a bit parser interface to any C# Stream object. Simply pass the source of the MPEG2 stream data (i.e., file stream, network stream, etc.) as a parameter to the BitStream constructor. See the BitStream class documentation for more details.
  • Register delegate methods as callbacks for the coding elements that you would like to process.
    The callback method signature must match the delegate definition within the coding element event class. Register the callback by adding the delegate to the statically defined "handlers" event for each event class.
  • Create a "VideoParser" object to parse the video.
    The VideoParser does the work of actually parsing the video stream. It does so by keeping track of decoding state and knowing what syntactic structure to expect next. It uses the BitStream object created above to retrieve the MPEG2 stream.
  • Call the parse_picture() method of the VideoParser object in an infinite loop (or for however many pictures you want to parse).
    This method will throw an exception if an error in the bitstream is found or if the end of the stream is reached.
  • As coding elements are parsed, events are constructed and published to your callback methods as appropriate.

Coding Element Hierarchy

Events are generated for each coding element as it is encountered in the bitstream. Each coding element is associated with a class which defines the coding element event generated. These coding element events are organized into a relatively flat hierarchy. This hierarchy allows you to subscribe to more general abstract classes of coding events that may encompass several specific subtypes. The design of the coding element class hierarchy is described here, and the event mechanism is more fully described here.

Library Organization

MPEG2Event is organized into a few different namespaces in anticipation of extensions for other parts of the MPEG-2 standard. Currently, the following namespaces are defined:

  • MPEG2Event
    This namespace contains all of the general classes that are not specific to a particular media type including the BitStream class, the parent coding element classes such as CodingElement, AtomicCodingElement, and Header, as well as various utility classes such as ZigZag.
  • MPEG2Event.Video
    This namespace contains all of the classes that are specific to MPEG2 video including all of the coding elements that comprise an MPEG2 video stream, the video parser class VideoParser, and the motion vector decoder class MotionVectorDecoder.
  • MPEG2Event.Systems
    This namespace will contain all of the classes that are part of the MPEG2 systems layer. This is work in progress and is not very useful just yet.
  • permalink 2004.11.23-09:24.00