+}
+
+static inline int have_captions( const uint8_t *user_data, uint32_t len )
+{
+ return len >= 6 &&
+ ( ( user_data[0] == 0x43 && user_data[1] == 0x43 ) ||
+ ( user_data[0] == 0x47 && user_data[1] == 0x41 &&
+ user_data[2] == 0x39 && user_data[3] == 0x34 &&
+ user_data[4] == 3 && (user_data[5] & 0x40) ) );
+}
+
+static void do_one_dvd_cc( hb_libmpeg2_t *m, const uint8_t *header, int field1 )
+{
+ uint8_t data[3];
+
+ data[0] = ( header[0] == 0xff && 0 == field1 )? 0x04 : 0x05;
+ data[1] = header[1];
+ data[2] = header[2];
+ hb_mpeg2_cc( m, data );
+
+ data[0] = ( header[3] == 0xff && 1 == field1 )? 0x04 : 0x05;
+ data[1] = header[4];
+ data[2] = header[5];
+ hb_mpeg2_cc( m, data );
+}
+
+// extract all the captions in the current frame and send them downstream
+// to the decoder.
+//
+// (this routine should only be called if there are captions in the current
+// frame. I.e., only if a call to 'have_captions' returns true.)
+static void extract_mpeg2_captions( hb_libmpeg2_t *m )
+{
+ const uint8_t *user_data = m->info->user_data;
+ int dvd_captions = user_data[0] == 0x43;
+ int capcount, field1packet = 0;
+ const uint8_t *header = &user_data[4];
+ if ( !dvd_captions )
+ {
+ // ATSC encapsulated captions - header starts one byte later
+ // and has an extra unused byte following it.
+ capcount = header[1] & 0x1f;
+ header += 3;
+ }