1 /* $Id: demuxmpeg.c,v 1.4 2004/10/19 23:11:36 titer Exp $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.fr/>.
5 It may be used under the terms of the GNU General Public License. */
9 /* Basic MPEG demuxer, only works with DVDs (2048 bytes packets) */
11 int hb_demux_ps( hb_buffer_t * buf_ps, hb_list_t * list_es )
17 #define d (buf_ps->data)
20 if( d[pos] != 0 || d[pos+1] != 0 ||
21 d[pos+2] != 0x1 || d[pos+3] != 0xBA )
23 hb_log( "hb_demux_ps: not a PS packet (%02x%02x%02x%02x)",
24 d[pos], d[pos+1], d[pos+2], d[pos+3] );
27 pos += 4; /* pack_start_code */
28 /* extract the system clock reference (scr) */
29 scr = ((uint64_t)(d[pos] & 0x38) << 27) |
30 ((uint64_t)(d[pos] & 0x03) << 28) |
31 ((uint64_t)(d[pos+1]) << 20) |
32 ((uint64_t)(d[pos+2] >> 3) << 15) |
33 ((uint64_t)(d[pos+2] & 3) << 13) |
34 ((uint64_t)(d[pos+3]) << 5) |
36 pos += 9; /* pack_header */
37 pos += 1 + ( d[pos] & 0x7 ); /* stuffing bytes */
40 if( d[pos] == 0 && d[pos+1] == 0 &&
41 d[pos+2] == 0x1 && d[pos+3] == 0xBB )
45 pos += 4; /* system_header_start_code */
46 header_length = ( d[pos] << 8 ) + d[pos+1];
47 pos += 2 + header_length;
51 while( pos + 6 < buf_ps->size &&
52 d[pos] == 0 && d[pos+1] == 0 && d[pos+2] == 0x1 )
55 int pes_packet_length;
57 int pes_header_d_length;
62 pos += 3; /* packet_start_code_prefix */
66 pes_packet_length = ( d[pos] << 8 ) + d[pos+1];
67 pos += 2; /* pes_packet_length */
68 pes_packet_end = pos + pes_packet_length;
70 if( id != 0xE0 && id != 0xBD &&
71 ( id & 0xC0 ) != 0xC0 )
78 has_pts = ( ( d[pos+1] >> 6 ) & 0x2 ) ? 1 : 0;
79 pos += 2; /* Required headers */
81 pes_header_d_length = d[pos];
83 pes_header_end = pos + pes_header_d_length;
87 pts = ( ( ( (uint64_t) d[pos] >> 1 ) & 0x7 ) << 30 ) +
89 ( ( d[pos+2] >> 1 ) << 15 ) +
98 id |= ( d[pos] << 8 );
99 if( ( id & 0xF0FF ) == 0x80BD ) /* A52 */
103 else if( ( id & 0xE0FF ) == 0x20BD || /* SPU */
104 ( id & 0xF0FF ) == 0xA0BD ) /* LPCM */
111 if( pos >= pes_packet_end )
113 pos = pes_packet_end;
117 /* Here we hit we ES payload */
118 buf_es = hb_buffer_init( pes_packet_end - pos );
124 // Consume a chapter break, and apply it to the ES.
125 buf_es->new_chap = buf_ps->new_chap;
126 buf_ps->new_chap = 0;
128 memcpy( buf_es->data, d + pos, pes_packet_end - pos );
130 hb_list_add( list_es, buf_es );
132 pos = pes_packet_end;