+static int isIframe( hb_stream_t *stream, const uint8_t *buf, int adapt_len )
+{
+ // For mpeg2: look for a gop start or i-frame picture start
+ // for h.264: look for idr nal type or a slice header for an i-frame
+ // for vc1: look for a Sequence header
+ int i;
+ uint32_t strid = 0;
+
+
+ if ( stream->ts_stream_type[0] <= 2 )
+ {
+ // This section of the code handles MPEG-1 and MPEG-2 video streams
+ for (i = 13 + adapt_len; i < 188; i++)
+ {
+ strid = (strid << 8) | buf[i];
+ if ( ( strid >> 8 ) == 1 )
+ {
+ // we found a start code
+ uint8_t id = strid;
+ switch ( id )
+ {
+ case 0xB8: // group_start_code (GOP header)
+ case 0xB3: // sequence_header code
+ return 1;
+
+ case 0x00: // picture_start_code
+ // picture_header, let's see if it's an I-frame
+ if (i<185)
+ {
+ // check if picture_coding_type == 1
+ if ((buf[i+2] & (0x7 << 3)) == (1 << 3))
+ {
+ // found an I-frame picture
+ return 1;
+ }
+ }
+ break;
+ }
+ }
+ }
+ // didn't find an I-frame
+ return 0;
+ }
+ if ( stream->ts_stream_type[0] == 0x1b )
+ {
+ // we have an h.264 stream
+ for (i = 13 + adapt_len; i < 188; i++)
+ {
+ strid = (strid << 8) | buf[i];
+ if ( ( strid >> 8 ) == 1 )
+ {
+ // we found a start code - remove the ref_idc from the nal type
+ uint8_t nal_type = strid & 0x1f;
+ if ( nal_type == 0x05 )
+ // h.264 IDR picture start
+ return 1;
+ }
+ }
+ // didn't find an I-frame
+ return 0;
+ }
+ if ( stream->ts_stream_type[0] == 0xea )
+ {
+ // we have an vc1 stream
+ for (i = 13 + adapt_len; i < 188; i++)
+ {
+ strid = (strid << 8) | buf[i];
+ if ( strid == 0x10f )
+ {
+ // the ffmpeg vc1 decoder requires a seq hdr code in the first
+ // frame.
+ return 1;
+ }
+ }
+ // didn't find an I-frame
+ return 0;
+ }
+
+ // we don't understand the stream type so just say "yes" otherwise
+ // we'll discard all the video.
+ return 1;
+}
+