Multiple-segment Encoding

Multiple-segment encoding is useful in video editing applications during production, for example when the encoder encodes multiple video clips according to their time line. In general, one can define multiple-segment encoding as dividing an input sequence of frames into segments and encoding them in different encoding sessions with the same or different parameter sets. For example:

Segment Already Encoded

Segment in Encoding

Segment to be Encoded

0s

200s

500s

Note

Different encoders can also be used.

The application must be able to:

  • Extract encoding parameters from the bitstream of previously encoded segment.

  • Import these encoding parameters to configure the encoder.

Encoding can then continue on the current segment using either the same or similar encoding parameters.

Extracting the header that contains the encoding parameter set from the encoded bitstream is usually the task of a format splitter (de-multiplexer). Alternatively, the MFXVideoDECODE_DecodeHeader() function can export the raw header if the application attaches the mfxExtCodingOptionSPSPPS structure as part of the parameters.

The encoder can use the mfxExtCodingOptionSPSPPS structure to import the encoding parameters during MFXVideoENCODE_Init(). The encoding parameters are in the encoded bitstream format. Upon a successful import of the header parameters, the encoder will generate bitstreams with a compatible (not necessarily bit-exact) header. The Header Import Functions table shows all functions that can import a header and their error codes if there are unsupported parameters in the header or the encoder is unable to achieve compatibility with the imported header.

Header Import Functions

Function Name

Error Code if Import Fails

MFXVideoENCODE_Init()

MFX_ERR_INCOMPATIBLE_VIDEO_PARAM

MFXVideoENCODE_QueryIOSurf()

MFX_ERR_INCOMPATIBLE_VIDEO_PARAM

MFXVideoENCODE_Reset()

MFX_ERR_INCOMPATIBLE_VIDEO_PARAM

MFXVideoENCODE_Query()

MFX_ERR_UNSUPPORTED

The encoder must encode frames to a GOP sequence starting with an IDR frame for H.264 (or I frame for MPEG-2) to ensure that the current segment encoding does not refer to any frames in the previous segment. This ensures that the encoded segment is self-contained, allowing the application to insert the segment anywhere in the final bitstream. After encoding, each encoded segment is HRD compliant. Concatenated segments may not be HRD compliant.

The following example shows the encoder initialization procedure that imports H.264 sequence and picture parameter sets:

 1mfxStatus init_encoder() {
 2   mfxExtCodingOptionSPSPPS option, *option_array;
 3
 4   /* configure mfxExtCodingOptionSPSPPS */
 5   memset(&option,0,sizeof(option));
 6   option.Header.BufferId=MFX_EXTBUFF_CODING_OPTION_SPSPPS;
 7   option.Header.BufferSz=sizeof(option);
 8   option.SPSBuffer=sps_buffer;
 9   option.SPSBufSize=sps_buffer_length;
10   option.PPSBuffer=pps_buffer;
11   option.PPSBufSize=pps_buffer_length;
12
13   /* configure mfxVideoParam */
14   mfxVideoParam param;
15   //...
16   param.NumExtParam=1;
17   option_array=&option;
18   param.ExtParam=(mfxExtBuffer**)&option_array;
19
20   /* encoder initialization */
21   mfxStatus status;
22   status=MFXVideoENCODE_Init(session, &param);
23   if (status==MFX_ERR_INCOMPATIBLE_VIDEO_PARAM) {
24      printf("Initialization failed.\n");
25   } else {
26      printf("Initialized.\n");
27   }
28   return status;
29}