OSDN Git Service

- support blu-ray, avchd & dvb x264
[handbrake-jp/handbrake-jp-git.git] / libhb / demuxmpeg.c
1 /* $Id: demuxmpeg.c,v 1.4 2004/10/19 23:11:36 titer Exp $
2
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. */
6
7 #include "hb.h"
8
9 /* Basic MPEG demuxer */
10
11 int hb_demux_ps( hb_buffer_t * buf_ps, hb_list_t * list_es, hb_psdemux_t* state )
12 {
13     hb_buffer_t * buf_es;
14     int           pos = 0;
15
16 #define d (buf_ps->data)
17
18     /* pack_header */
19     if( d[pos] != 0 || d[pos+1] != 0 ||
20         d[pos+2] != 0x1 || d[pos+3] != 0xBA )
21     {
22         hb_log( "hb_demux_ps: not a PS packet (%02x%02x%02x%02x)",
23                 d[pos], d[pos+1], d[pos+2], d[pos+3] );
24         return 0;
25     }
26     pos += 4;                    /* pack_start_code */
27
28     if ( state )
29     {
30         /*
31          * This section of code implements the timing model of
32          * the "Standard Target Decoder" (STD) of the MPEG2 standard
33          * (specified in ISO 13818-1 sections 2.4.2, 2.5.2 & Annex D).
34          * The STD removes and corrects for clock discontinuities so
35          * that the time stamps on the video, audio & other media
36          * streams can be used for cross-media synchronization. To do
37          * this the STD has its own timestamp value, the System Clock
38          * Reference or SCR, in the PACK header. Clock discontinuities
39          * are detected using the SCR & and the adjustment needed
40          * to correct post-discontinuity timestamps to be contiguous
41          * with pre-discontinuity timestamps is computed from pre- and
42          * post-discontinuity values of the SCR. Then this adjustment
43          * is applied to every media timestamp (PTS).
44          *
45          * ISO 13818-1 says there must be an SCR at least every 700ms
46          * (100ms for Transport Streams) so if the difference between
47          * this SCR & the previous is >700ms it's a discontinuity.
48          * If the difference is negative it's non-physical (time doesn't
49          * go backward) and must also be a discontinuity. When we find a
50          * discontinuity we adjust the scr_offset so that the SCR of the
51          * new packet lines up with that of the previous packet.
52          */
53         /* extract the system clock reference (scr) */
54         int64_t scr = ((uint64_t)(d[pos] & 0x38) << 27) |
55                       ((uint64_t)(d[pos] & 0x03) << 28) |
56                       ((uint64_t)(d[pos+1]) << 20) |
57                       ((uint64_t)(d[pos+2] >> 3) << 15) |
58                       ((uint64_t)(d[pos+2] & 3) << 13) |
59                       ((uint64_t)(d[pos+3]) << 5) |
60                       (d[pos+4] >> 3);
61         int64_t scr_delta = scr - state->last_scr;
62         if ( scr_delta > (90*700) || scr_delta < 0 )
63         {
64             ++state->scr_changes;
65             state->scr_offset += scr_delta - 1;
66         }
67         state->last_scr = scr;
68     }
69
70     pos += 9;                    /* pack_header */
71     pos += 1 + ( d[pos] & 0x7 ); /* stuffing bytes */
72
73     /* system_header */
74     if( d[pos] == 0 && d[pos+1] == 0 &&
75         d[pos+2] == 0x1 && d[pos+3] == 0xBB )
76     {
77         int header_length;
78
79         pos           += 4; /* system_header_start_code */
80         header_length  = ( d[pos] << 8 ) + d[pos+1];
81         pos           += 2 + header_length;
82     }
83
84     /* pes */
85     while( pos + 6 < buf_ps->size &&
86            d[pos] == 0 && d[pos+1] == 0 && d[pos+2] == 0x1 )
87     {
88         int      id;
89         int      pes_packet_length;
90         int      pes_packet_end;
91         int      pes_header_d_length;
92         int      pes_header_end;
93         int      has_pts;
94         int64_t  pts = -1, dts = -1;
95
96         pos               += 3;               /* packet_start_code_prefix */
97         id           = d[pos];
98         pos               += 1;
99
100         pes_packet_length  = ( d[pos] << 8 ) + d[pos+1];
101         pos               += 2;               /* pes_packet_length */
102         pes_packet_end     = pos + pes_packet_length;
103
104         if( id != 0xE0 && id != 0xBD &&
105             ( id & 0xC0 ) != 0xC0  )
106         {
107             /* Not interesting */
108             pos = pes_packet_end;
109             continue;
110         }
111
112         has_pts            = d[pos+1] >> 6;
113         pos               += 2;               /* Required headers */
114
115         pes_header_d_length  = d[pos];
116         pos                    += 1;
117         pes_header_end          = pos + pes_header_d_length;
118
119         if( has_pts )
120         {
121             pts = ( (uint64_t)(d[pos] & 0xe ) << 29 ) +
122                   ( d[pos+1] << 22 ) +
123                   ( ( d[pos+2] >> 1 ) << 15 ) +
124                   ( d[pos+3] << 7 ) +
125                   ( d[pos+4] >> 1 );
126             if ( has_pts & 1 )
127             {
128                 dts = ( (uint64_t)(d[pos+5] & 0xe ) << 29 ) +
129                       ( d[pos+6] << 22 ) +
130                       ( ( d[pos+7] >> 1 ) << 15 ) +
131                       ( d[pos+8] << 7 ) +
132                       ( d[pos+9] >> 1 );
133             }
134             else
135             {
136                 dts = pts;
137             }
138         }
139
140         pos = pes_header_end;
141
142         if( id == 0xBD )
143         {
144             id |= ( d[pos] << 8 );
145             if( ( id & 0xF0FF ) == 0x80BD ) /* A52 */
146             {
147                 pos += 4;
148             }
149             else if( ( id & 0xE0FF ) == 0x20BD || /* SPU */
150                      ( id & 0xF0FF ) == 0xA0BD )  /* LPCM */
151             {
152                 pos += 1;
153             }
154         }
155
156         /* Sanity check */
157         if( pos >= pes_packet_end )
158         {
159             pos = pes_packet_end;
160             continue;
161         }
162
163         /* Here we hit we ES payload */
164         buf_es = hb_buffer_init( pes_packet_end - pos );
165
166         buf_es->id       = id;
167         buf_es->start    = pts;
168         buf_es->renderOffset = dts;
169         buf_es->stop     = -1;
170         if (id == 0xE0) {
171             // Consume a chapter break, and apply it to the ES.
172             buf_es->new_chap = buf_ps->new_chap;
173             buf_ps->new_chap = 0;
174         }
175         memcpy( buf_es->data, d + pos, pes_packet_end - pos );
176
177         hb_list_add( list_es, buf_es );
178
179         pos = pes_packet_end;
180     }
181
182 #undef d
183
184     return 1;
185 }
186
187 // "null" demuxer (makes a copy of input buf & returns it in list)
188 // used when the reader for some format includes its own demuxer.
189 // for example, ffmpeg.
190 int hb_demux_null( hb_buffer_t * buf_ps, hb_list_t * list_es, hb_psdemux_t* state )
191 {
192     hb_buffer_t *buf = hb_buffer_init( buf_ps->size );
193
194     // copy everything from the old to the new except the data ptr & alloc
195     uint8_t *data = buf->data;
196     int alloc = buf->alloc;
197     *buf = *buf_ps;
198     buf->data = data;
199     buf->alloc = alloc;
200
201     // now copy the data
202     memcpy( buf->data, buf_ps->data, buf_ps->size );
203     hb_list_add( list_es, buf );
204     return 1;
205 }