WinGui: Fix another instance of the Caliburn vs Json.net sillyness where objects...
[HandBrake.git] / libhb / demuxmpeg.c
blobf370caf0461066c73fe5db92d735a0b3ed1e3f62
1 /* demuxmpeg.c
3 Copyright (c) 2003-2015 HandBrake Team
4 This file is part of the HandBrake source code
5 Homepage: <http://handbrake.fr/>.
6 It may be used under the terms of the GNU General Public License v2.
7 For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
8 */
10 #include "hb.h"
12 static inline int check_mpeg_scr( hb_psdemux_t *state, int64_t scr, int tol )
15 * This section of code implements the timing model of
16 * the "Standard Target Decoder" (STD) of the MPEG2 standard
17 * (specified in ISO 13818-1 sections 2.4.2, 2.5.2 & Annex D).
18 * The STD removes and corrects for clock discontinuities so
19 * that the time stamps on the video, audio & other media
20 * streams can be used for cross-media synchronization. To do
21 * this the STD has its own timestamp value, the System Clock
22 * Reference or SCR, in the PACK header. Clock discontinuities
23 * are detected using the SCR & and the adjustment needed
24 * to correct post-discontinuity timestamps to be contiguous
25 * with pre-discontinuity timestamps is computed from pre- and
26 * post-discontinuity values of the SCR. Then this adjustment
27 * is applied to every media timestamp (PTS).
29 * ISO 13818-1 says there must be an SCR at least every 700ms
30 * (100ms for Transport Streams) so if the difference between
31 * this SCR & the previous is >700ms it's a discontinuity.
32 * If the difference is negative it's non-physical (time doesn't
33 * go backward) and must also be a discontinuity. When we find a
34 * discontinuity we adjust the scr_offset so that the SCR of the
35 * new packet lines up with that of the previous packet.
38 // we declare a discontinuity if there's a gap of more than
39 // 'tol'ms between the last scr & this or if this scr goes back
40 // by more than half a frame time.
41 int discontinuity = 0;
42 int64_t scr_delta = scr - state->last_scr;
43 if (state->last_scr == AV_NOPTS_VALUE ||
44 scr_delta > 90*tol || scr_delta < -90*10)
46 ++state->scr_changes;
47 state->last_pts = AV_NOPTS_VALUE;
48 discontinuity = 1;
50 state->last_scr = scr;
51 return discontinuity;
54 static inline void save_chap( hb_psdemux_t *state, hb_buffer_t *buf )
56 if ( state && buf->s.new_chap )
58 state->new_chap = buf->s.new_chap;
59 buf->s.new_chap = 0;
63 static inline void restore_chap( hb_psdemux_t *state, hb_buffer_t *buf )
65 if ( state )
67 buf->s.new_chap = state->new_chap;
68 state->new_chap = 0;
72 /* Basic MPEG demuxer */
74 void hb_demux_dvd_ps( hb_buffer_t * buf, hb_list_t * list_es, hb_psdemux_t* state )
76 hb_buffer_t * buf_es;
77 int pos = 0;
79 while ( buf )
81 save_chap( state, buf );
83 #define d (buf->data)
84 /* pack_header */
85 if( d[pos] != 0 || d[pos+1] != 0 ||
86 d[pos+2] != 0x1 || d[pos+3] != 0xBA )
88 hb_log( "hb_demux_ps: not a PS packet (%02x%02x%02x%02x)",
89 d[pos], d[pos+1], d[pos+2], d[pos+3] );
90 hb_buffer_t *tmp = buf->next;
91 buf->next = NULL;
92 hb_buffer_close( &buf );
93 buf = tmp;
94 continue;
96 pos += 4; /* pack_start_code */
98 if ( state )
100 /* extract the system clock reference (scr) */
101 int64_t scr = ((uint64_t)(d[pos] & 0x38) << 27) |
102 ((uint64_t)(d[pos] & 0x03) << 28) |
103 ((uint64_t)(d[pos+1]) << 20) |
104 ((uint64_t)(d[pos+2] >> 3) << 15) |
105 ((uint64_t)(d[pos+2] & 3) << 13) |
106 ((uint64_t)(d[pos+3]) << 5) |
107 (d[pos+4] >> 3);
108 check_mpeg_scr( state, scr, 700 );
111 pos += 9; /* pack_header */
112 pos += 1 + ( d[pos] & 0x7 ); /* stuffing bytes */
114 /* system_header */
115 if( d[pos] == 0 && d[pos+1] == 0 &&
116 d[pos+2] == 0x1 && d[pos+3] == 0xBB )
118 int header_length;
120 pos += 4; /* system_header_start_code */
121 header_length = ( d[pos] << 8 ) + d[pos+1];
122 pos += 2 + header_length;
125 /* pes */
126 while( pos + 6 < buf->size &&
127 d[pos] == 0 && d[pos+1] == 0 && d[pos+2] == 0x1 )
129 int id;
130 int pes_packet_length;
131 int pes_packet_end;
132 int pes_header_d_length;
133 int pes_header_end;
134 int has_pts;
135 int64_t pts = AV_NOPTS_VALUE, dts = AV_NOPTS_VALUE;
137 pos += 3; /* packet_start_code_prefix */
138 id = d[pos];
139 pos += 1;
141 /* pack_header */
142 if( id == 0xBA)
144 pos += 10 + (d[pos+9] & 7);
145 continue;
148 /* system_header */
149 if( id == 0xBB )
151 int header_length;
153 header_length = ( d[pos] << 8 ) + d[pos+1];
154 pos += 2 + header_length;
155 continue;
158 pes_packet_length = ( d[pos] << 8 ) + d[pos+1];
159 pos += 2; /* pes_packet_length */
160 pes_packet_end = pos + pes_packet_length;
162 if( id != 0xE0 && id != 0xBD &&
163 ( id & 0xC0 ) != 0xC0 )
165 /* Not interesting */
166 pos = pes_packet_end;
167 continue;
170 has_pts = d[pos+1] >> 6;
171 pos += 2; /* Required headers */
173 pes_header_d_length = d[pos];
174 pos += 1;
175 pes_header_end = pos + pes_header_d_length;
177 if( has_pts )
179 pts = ( (uint64_t)(d[pos] & 0xe ) << 29 ) +
180 ( d[pos+1] << 22 ) +
181 ( ( d[pos+2] >> 1 ) << 15 ) +
182 ( d[pos+3] << 7 ) +
183 ( d[pos+4] >> 1 );
184 if ( has_pts & 1 )
186 dts = ( (uint64_t)(d[pos+5] & 0xe ) << 29 ) +
187 ( d[pos+6] << 22 ) +
188 ( ( d[pos+7] >> 1 ) << 15 ) +
189 ( d[pos+8] << 7 ) +
190 ( d[pos+9] >> 1 );
192 else
194 dts = pts;
198 pos = pes_header_end;
200 if( id == 0xBD )
202 id |= ( d[pos] << 8 );
203 if( ( id & 0xF0FF ) == 0x80BD ) /* A52 */
205 pos += 4;
207 else if( ( id & 0xE0FF ) == 0x20BD || /* SPU */
208 ( id & 0xF0FF ) == 0xA0BD ) /* LPCM */
210 pos += 1;
214 /* Sanity check */
215 if( pos >= pes_packet_end )
217 pos = pes_packet_end;
218 continue;
221 /* Here we hit we ES payload */
222 buf_es = hb_buffer_init( pes_packet_end - pos );
224 buf_es->s.id = id;
225 buf_es->s.start = pts;
226 buf_es->s.renderOffset = dts;
227 buf_es->s.stop = AV_NOPTS_VALUE;
228 if ( state && id == 0xE0)
230 // Consume a chapter break, and apply it to the ES.
231 restore_chap( state, buf_es );
233 memcpy( buf_es->data, d + pos, pes_packet_end - pos );
235 hb_list_add( list_es, buf_es );
237 pos = pes_packet_end;
239 hb_buffer_t *tmp = buf->next;
240 buf->next = NULL;
241 hb_buffer_close( &buf );
242 buf = tmp;
244 #undef d
247 // mpeg transport stream demuxer. the elementary stream headers have been
248 // stripped off and buf has all the info gleaned from them: id is set,
249 // start contains the pts (if any), renderOffset contains the dts (if any)
250 // and stop contains the pcr (if it changed).
251 void hb_demux_mpeg(hb_buffer_t *buf, hb_list_t *list_es,
252 hb_psdemux_t *state, int tolerance)
254 while ( buf )
256 save_chap( state, buf );
257 if ( state )
259 int discontinuity = 0;
260 // we're keeping track of timing (i.e., not in scan)
261 // check if there's a new pcr in this packet
262 if ( buf->s.pcr >= 0 )
264 // we have a new pcr
265 discontinuity = check_mpeg_scr( state, buf->s.pcr, tolerance );
266 buf->s.pcr = AV_NOPTS_VALUE;
267 // Some streams have consistantly bad PCRs or SCRs
268 // So filter out the offset
269 if ( buf->s.start >= 0 )
270 state->scr_delta = buf->s.start - state->last_scr;
271 else
272 state->scr_delta = 0;
274 if ( !discontinuity && buf->s.discontinuity )
276 // Buffer has been flagged as a discontinuity. This happens
277 // when a blueray changes clips.
278 ++state->scr_changes;
279 state->last_scr = buf->s.start;
280 state->scr_delta = 0;
283 if ( buf->s.start >= 0 )
285 int64_t fdelta;
286 if (buf->s.type == AUDIO_BUF || buf->s.type == VIDEO_BUF)
288 if ( state->last_pts >= 0 )
290 fdelta = buf->s.start - state->last_pts;
291 if ( fdelta < -5 * 90000LL || fdelta > 5 * 90000LL )
293 // Packet too far from last. This may be a NZ TV
294 // broadcast as they like to change the PCR without
295 // sending a PCR update. Since it may be a while
296 // until they actually tell us the new PCR use the
297 // PTS as the PCR.
298 ++state->scr_changes;
299 state->last_scr = buf->s.start;
300 state->scr_delta = 0;
303 state->last_pts = buf->s.start;
305 if (state->last_scr != AV_NOPTS_VALUE)
307 // Program streams have an SCR in every PACK header so they
308 // can't lose their clock reference. But the PCR in
309 // Transport streams is typically on <.1% of the packets.
310 // If a PCR packet gets lost and it marks a clock
311 // discontinuity then the data following it will be
312 // referenced to the wrong clock & introduce huge gaps or
313 // throw our A/V sync off. We try to protect against that
314 // here by sanity checking timestamps against the current
315 // reference clock and discarding packets where the DTS
316 // is "too far" from its clock.
317 fdelta = buf->s.start - state->last_scr - state->scr_delta;
318 if ( fdelta < -300 * 90000LL || fdelta > 300 * 90000LL )
320 // packet too far behind or ahead of its clock reference
321 buf->s.renderOffset = AV_NOPTS_VALUE;
322 buf->s.start = AV_NOPTS_VALUE;
323 buf->s.stop = AV_NOPTS_VALUE;
325 else
327 // Some streams have no PCRs. In these cases, we
328 // will only get an "PCR" update if a large change
329 // in DTS or PTS is detected. So we need to update
330 // our scr_delta with each valid timestamp so that
331 // fdelta does not continually grow.
332 state->scr_delta = buf->s.start - state->last_scr;
337 if ( buf->s.type == VIDEO_BUF )
339 restore_chap( state, buf );
343 hb_buffer_t *tmp = buf->next;
344 buf->next = NULL;
345 hb_list_add( list_es, buf );
346 buf = tmp;
350 void hb_demux_ts(hb_buffer_t *buf, hb_list_t *list_es, hb_psdemux_t *state)
352 // Distance between PCRs in TS is up to 100ms, but we have seen
353 // streams that exceed this, so allow up to 300ms.
354 hb_demux_mpeg(buf, list_es, state, 300);
357 void hb_demux_ps(hb_buffer_t *buf, hb_list_t *list_es, hb_psdemux_t *state)
359 // Distance between SCRs in PS is up to 700ms
360 hb_demux_mpeg(buf, list_es, state, 700);
363 // "null" demuxer (makes a copy of input buf & returns it in list)
364 // used when the reader for some format includes its own demuxer.
365 // for example, ffmpeg.
366 void hb_demux_null( hb_buffer_t * buf, hb_list_t * list_es, hb_psdemux_t* state )
368 while ( buf )
370 save_chap( state, buf );
371 if ( state )
373 // if we don't have a time offset yet,
374 // use this timestamp as the offset.
375 if (state->scr_changes == 0 &&
376 (buf->s.start != AV_NOPTS_VALUE ||
377 buf->s.renderOffset != AV_NOPTS_VALUE))
379 ++state->scr_changes;
380 state->last_scr = buf->s.start >= 0 ? buf->s.start : buf->s.renderOffset;
383 if ( buf->s.type == VIDEO_BUF )
385 restore_chap( state, buf );
389 hb_buffer_t *tmp = buf->next;
390 buf->next = NULL;
391 hb_list_add( list_es, buf );
392 buf = tmp;
396 const hb_muxer_t hb_demux[] = { hb_demux_dvd_ps, hb_demux_ts, hb_demux_ps, hb_demux_null };