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
17 #include "libbluray/bluray.h"
20 #define min(a, b) a < b ? a : b
21 #define HB_MAX_PROBE_SIZE (1*1024*1024)
24 * This table defines how ISO MPEG stream type codes map to HandBrake
25 * codecs. It is indexed by the 8 bit stream type and contains the codec
26 * worker object id and a parameter for that worker proc (ignored except
27 * for the ffmpeg-based codecs in which case it is the ffmpeg codec id).
29 * Entries with a worker proc id of 0 or a kind of 'U' indicate that HB
30 * doesn't handle the stream type.
32 * U - Unknown (to be determined by further processing)
38 typedef enum { N
, U
, A
, V
, P
, S
} kind_t
;
40 kind_t kind
; /* not handled / unknown / audio / video */
41 int codec
; /* HB worker object id of codec */
42 int codec_param
; /* param for codec (usually ffmpeg codec id) */
43 const char* name
; /* description of type */
46 #define st(id, kind, codec, codec_param, name) \
47 [id] = { kind, codec, codec_param, name }
49 static const stream2codec_t st2codec
[256] = {
50 st(0x00, U
, 0, 0, NULL
),
51 st(0x01, V
, WORK_DECAVCODECV
, AV_CODEC_ID_MPEG2VIDEO
, "MPEG1"),
52 st(0x02, V
, WORK_DECAVCODECV
, AV_CODEC_ID_MPEG2VIDEO
, "MPEG2"),
53 st(0x03, A
, HB_ACODEC_FFMPEG
, AV_CODEC_ID_MP2
, "MPEG1"),
54 st(0x04, A
, HB_ACODEC_FFMPEG
, AV_CODEC_ID_MP2
, "MPEG2"),
55 st(0x05, N
, 0, 0, "ISO 13818-1 private section"),
56 st(0x06, U
, 0, 0, "ISO 13818-1 PES private data"),
57 st(0x07, N
, 0, 0, "ISO 13522 MHEG"),
58 st(0x08, N
, 0, 0, "ISO 13818-1 DSM-CC"),
59 st(0x09, N
, 0, 0, "ISO 13818-1 auxiliary"),
60 st(0x0a, N
, 0, 0, "ISO 13818-6 encap"),
61 st(0x0b, N
, 0, 0, "ISO 13818-6 DSM-CC U-N msgs"),
62 st(0x0c, N
, 0, 0, "ISO 13818-6 Stream descriptors"),
63 st(0x0d, N
, 0, 0, "ISO 13818-6 Sections"),
64 st(0x0e, N
, 0, 0, "ISO 13818-1 auxiliary"),
65 st(0x0f, A
, HB_ACODEC_FFAAC
, AV_CODEC_ID_AAC
, "AAC"),
66 st(0x10, V
, WORK_DECAVCODECV
, AV_CODEC_ID_MPEG4
, "MPEG4"),
67 st(0x11, A
, HB_ACODEC_FFMPEG
, AV_CODEC_ID_AAC_LATM
, "LATM AAC"),
68 st(0x12, U
, 0, 0, "MPEG4 generic"),
70 st(0x14, N
, 0, 0, "ISO 13818-6 DSM-CC download"),
72 st(0x1b, V
, WORK_DECAVCODECV
, AV_CODEC_ID_H264
, "H.264"),
74 st(0x80, U
, HB_ACODEC_FFMPEG
, AV_CODEC_ID_PCM_BLURAY
, "Digicipher II Video"),
75 st(0x81, A
, HB_ACODEC_AC3
, AV_CODEC_ID_AC3
, "AC3"),
76 st(0x82, A
, HB_ACODEC_DCA
, AV_CODEC_ID_DTS
, "DTS"),
77 // 0x83 can be LPCM or BD TrueHD. Set to 'unknown' till we know more.
78 st(0x83, U
, HB_ACODEC_LPCM
, 0, "LPCM"),
79 // BD E-AC3 Primary audio
80 st(0x84, U
, 0, 0, "SDDS"),
81 st(0x85, U
, 0, 0, "ATSC Program ID"),
82 // 0x86 can be BD DTS-HD/DTS. Set to 'unknown' till we know more.
83 st(0x86, U
, HB_ACODEC_DCA_HD
, AV_CODEC_ID_DTS
, "DTS-HD MA"),
84 st(0x87, A
, HB_ACODEC_FFEAC3
, AV_CODEC_ID_EAC3
, "E-AC3"),
86 st(0x8a, A
, HB_ACODEC_DCA
, AV_CODEC_ID_DTS
, "DTS"),
88 st(0x90, S
, WORK_DECPGSSUB
, 0, "PGS Subtitle"),
89 // 0x91 can be AC3 or BD Interactive Graphics Stream.
90 st(0x91, U
, 0, 0, "AC3/IGS"),
91 st(0x92, N
, 0, 0, "Subtitle"),
93 st(0x94, U
, 0, 0, "SDDS"),
94 st(0xa0, V
, 0, 0, "MSCODEC"),
95 // BD E-AC3 Secondary audio
96 st(0xa1, U
, 0, 0, "E-AC3"),
97 // BD DTS-HD Secondary audio
98 st(0xa2, U
, 0, 0, "DTS-HD LBR"),
100 st(0xea, V
, WORK_DECAVCODECV
, AV_CODEC_ID_VC1
, "VC-1"),
105 hb_stream_type_unknown
= 0,
111 #define MAX_PS_PROBE_SIZE (5*1024*1024)
112 #define kMaxNumberPMTStreams 32
116 uint8_t has_stream_id_ext
;
118 uint8_t stream_id_ext
;
119 uint8_t bd_substream_id
;
129 hb_pes_info_t pes_info
;
130 int8_t pes_info_valid
;
135 uint8_t pkt_summary
[8];
144 uint8_t stream_id_ext
;
149 #define TS_FORMAT_ID_AC3 (('A' << 24) | ('C' << 16) | ('-' << 8) | '3')
150 int codec
; // HB worker object id of codec
151 int codec_param
; // param for codec (usually ffmpeg codec id)
153 int next
; // next pointer for list
154 // hb_ts_stream_t points to a list of
156 hb_buffer_t
*probe_buf
;
165 int frames
; /* video frames so far */
166 int errors
; /* total errors so far */
167 int last_error_frame
; /* frame # at last error message */
168 int last_error_count
; /* # errors at last error message */
169 int packetsize
; /* Transport Stream packet size */
171 int need_keyframe
; // non-zero if want to start at a keyframe
173 int chapter
; /* Chapter that we are currently in */
174 int64_t chapter_end
; /* HB time that the current chapter ends */
180 uint8_t found_pcr
; // non-zero if we've found at least one pcr
181 int64_t pcr
; // most recent input pcr
182 int64_t last_timestamp
; // used for discontinuity detection when
185 uint8_t *packet
; // buffer for one TS packet
186 hb_ts_stream_t
*list
;
193 uint8_t found_scr
; // non-zero if we've found at least one scr
194 int64_t scr
; // most recent input scr
195 hb_pes_stream_t
*list
;
201 * Stuff before this point is dynamic state updated as we read the
202 * stream. Stuff after this point is stream description state that
203 * we learn during the initial scan but cache so it can be
204 * reused during the conversion read.
206 uint8_t has_IDRs
; // # IDRs found during duration scan
207 uint8_t ts_flags
; // stream characteristics:
208 #define TS_HAS_PCR (1 << 0) // at least one PCR seen
209 #define TS_HAS_RAP (1 << 1) // Random Access Point bit seen
210 #define TS_HAS_RSEI (1 << 2) // "Restart point" SEI seen
214 hb_stream_type_t hb_stream_type
;
217 AVFormatContext
*ffmpeg_ic
;
218 AVPacket
*ffmpeg_pkt
;
219 uint8_t ffmpeg_video_id
;
221 uint32_t reg_desc
; // 4 byte registration code that identifies
226 unsigned short program_number
;
227 unsigned short program_map_PID
;
228 } pat_info
[kMaxNumberPMTStreams
];
229 int ts_number_pat_entries
;
234 unsigned char *tablebuf
;
235 unsigned int tablepos
;
236 unsigned char current_continuity_counter
;
238 unsigned int PCR_PID
;
250 /***********************************************************************
252 **********************************************************************/
253 static void hb_stream_duration(hb_stream_t
*stream
, hb_title_t
*inTitle
);
254 static off_t
align_to_next_packet(hb_stream_t
*stream
);
255 static int64_t pes_timestamp( const uint8_t *pes
);
257 static int hb_ts_stream_init(hb_stream_t
*stream
);
258 static hb_buffer_t
* hb_ts_stream_decode(hb_stream_t
*stream
);
259 static void hb_init_audio_list(hb_stream_t
*stream
, hb_title_t
*title
);
260 static void hb_init_subtitle_list(hb_stream_t
*stream
, hb_title_t
*title
);
261 static int hb_ts_stream_find_pids(hb_stream_t
*stream
);
263 static void hb_ps_stream_init(hb_stream_t
*stream
);
264 static hb_buffer_t
* hb_ps_stream_decode(hb_stream_t
*stream
);
265 static void hb_ps_stream_find_streams(hb_stream_t
*stream
);
266 static int hb_ps_read_packet( hb_stream_t
* stream
, hb_buffer_t
*b
);
267 static int update_ps_streams( hb_stream_t
* stream
, int stream_id
, int stream_id_ext
, int stream_type
, int in_kind
);
268 static int update_ts_streams( hb_stream_t
* stream
, int pid
, int stream_id_ext
, int stream_type
, int in_kind
, int *pes_idx
);
269 static void update_pes_kind( hb_stream_t
* stream
, int idx
);
271 static int ffmpeg_open( hb_stream_t
*stream
, hb_title_t
*title
, int scan
);
272 static void ffmpeg_close( hb_stream_t
*d
);
273 static hb_title_t
*ffmpeg_title_scan( hb_stream_t
*stream
, hb_title_t
*title
);
274 hb_buffer_t
*hb_ffmpeg_read( hb_stream_t
*stream
);
275 static int ffmpeg_seek( hb_stream_t
*stream
, float frac
);
276 static int ffmpeg_seek_ts( hb_stream_t
*stream
, int64_t ts
);
277 static inline unsigned int bits_get(bitbuf_t
*bb
, int bits
);
278 static inline void bits_init(bitbuf_t
*bb
, uint8_t* buf
, int bufsize
, int clear
);
279 static inline unsigned int bits_peek(bitbuf_t
*bb
, int bits
);
280 static inline int bits_eob(bitbuf_t
*bb
);
281 static inline int bits_read_ue(bitbuf_t
*bb
);
282 static void pes_add_audio_to_title(hb_stream_t
*s
, int i
, hb_title_t
*t
, int sort
);
283 static int hb_parse_ps( hb_stream_t
*stream
, uint8_t *buf
, int len
, hb_pes_info_t
*pes_info
);
284 static void hb_ts_resolve_pid_types(hb_stream_t
*stream
);
285 static void hb_ps_resolve_stream_types(hb_stream_t
*stream
);
286 void hb_ts_stream_reset(hb_stream_t
*stream
);
287 void hb_ps_stream_reset(hb_stream_t
*stream
);
291 * these frontend hb_log because transport streams can have a lot of errors
292 * so we want to rate limit messages. this routine limits the number of
293 * messages to at most one per minute of video. other errors that occur
294 * during the minute are counted & the count is output with the next
295 * error msg we print.
297 static void ts_warn_helper( hb_stream_t
*stream
, char *log
, va_list args
)
299 // limit error printing to at most one per minute of video (at 30fps)
301 if ( stream
->frames
- stream
->last_error_frame
>= 30*60 )
305 vsnprintf( msg
, sizeof(msg
), log
, args
);
307 if ( stream
->errors
- stream
->last_error_count
< 10 )
309 hb_log( "stream: error near frame %d: %s", stream
->frames
, msg
);
313 int Edelta
= stream
->errors
- stream
->last_error_count
;
314 double Epcnt
= (double)Edelta
* 100. /
315 (stream
->frames
- stream
->last_error_frame
);
316 hb_log( "stream: %d new errors (%.0f%%) up to frame %d: %s",
317 Edelta
, Epcnt
, stream
->frames
, msg
);
319 stream
->last_error_frame
= stream
->frames
;
320 stream
->last_error_count
= stream
->errors
;
324 static void ts_warn( hb_stream_t
*, char*, ... ) HB_WPRINTF(2,3);
325 static void ts_err( hb_stream_t
*, int, char*, ... ) HB_WPRINTF(3,4);
327 static void ts_warn( hb_stream_t
*stream
, char *log
, ... )
330 va_start( args
, log
);
331 ts_warn_helper( stream
, log
, args
);
335 static int get_id(hb_pes_stream_t
*pes
)
337 return ( pes
->stream_id_ext
<< 16 ) + pes
->stream_id
;
340 static int index_of_id(hb_stream_t
*stream
, int id
)
344 for ( i
= 0; i
< stream
->pes
.count
; ++i
)
346 if ( id
== get_id( &stream
->pes
.list
[i
] ) )
353 static int index_of_pid(hb_stream_t
*stream
, int pid
)
357 for ( i
= 0; i
< stream
->ts
.count
; ++i
)
359 if ( pid
== stream
->ts
.list
[i
].pid
)
368 static int index_of_ps_stream(hb_stream_t
*stream
, int id
, int sid
)
372 for ( i
= 0; i
< stream
->pes
.count
; ++i
)
374 if ( id
== stream
->pes
.list
[i
].stream_id
&&
375 sid
== stream
->pes
.list
[i
].stream_id_ext
)
380 // If there is no match on the stream_id_ext, try matching
381 // on only the stream_id.
382 for ( i
= 0; i
< stream
->pes
.count
; ++i
)
384 if ( id
== stream
->pes
.list
[i
].stream_id
&&
385 0 == stream
->pes
.list
[i
].stream_id_ext
)
394 static kind_t
ts_stream_kind( hb_stream_t
* stream
, int idx
)
396 if ( stream
->ts
.list
[idx
].pes_list
!= -1 )
398 // Retuns kind for the first pes substream in the pes list
399 // All substreams in a TS stream are the same kind.
400 return stream
->pes
.list
[stream
->ts
.list
[idx
].pes_list
].stream_kind
;
408 static kind_t
ts_stream_type( hb_stream_t
* stream
, int idx
)
410 if ( stream
->ts
.list
[idx
].pes_list
!= -1 )
412 // Retuns stream type for the first pes substream in the pes list
413 // All substreams in a TS stream are the same stream type.
414 return stream
->pes
.list
[stream
->ts
.list
[idx
].pes_list
].stream_type
;
422 static int pes_index_of_video(hb_stream_t
*stream
)
426 for ( i
= 0; i
< stream
->pes
.count
; ++i
)
427 if ( V
== stream
->pes
.list
[i
].stream_kind
)
433 static int ts_index_of_video(hb_stream_t
*stream
)
437 for ( i
= 0; i
< stream
->ts
.count
; ++i
)
438 if ( V
== ts_stream_kind( stream
, i
) )
444 static void ts_err( hb_stream_t
*stream
, int curstream
, char *log
, ... )
447 va_start( args
, log
);
448 ts_warn_helper( stream
, log
, args
);
451 stream
->ts
.list
[curstream
].skipbad
= 1;
452 stream
->ts
.list
[curstream
].continuity
= -1;
455 static int check_ps_sync(const uint8_t *buf
)
457 // a legal MPEG program stream must start with a Pack header in the
459 return (buf
[0] == 0x00) && (buf
[1] == 0x00) &&
460 (buf
[2] == 0x01) && (buf
[3] == 0xba);
463 static int check_ps_sc(const uint8_t *buf
)
465 // a legal MPEG program stream must start with a Pack followed by a
466 // some other start code. If we've already verified the pack, this skip
467 // it and checks for a start code prefix.
469 int mark
= buf
[4] >> 4;
472 // Check other marker bits to make it less likely
473 // that we are being spoofed.
474 if( ( buf
[4] & 0xf1 ) != 0x21 ||
475 ( buf
[6] & 0x01 ) != 0x01 ||
476 ( buf
[8] & 0x01 ) != 0x01 ||
477 ( buf
[9] & 0x80 ) != 0x80 ||
478 ( buf
[11] & 0x01 ) != 0x01 )
482 // mpeg-1 pack header
483 pos
= 12; // skip over the PACK
487 // Check other marker bits to make it less likely
488 // that we are being spoofed.
489 if( ( buf
[4] & 0xC4 ) != 0x44 ||
490 ( buf
[6] & 0x04 ) != 0x04 ||
491 ( buf
[8] & 0x04 ) != 0x04 ||
492 ( buf
[9] & 0x01 ) != 0x01 ||
493 ( buf
[12] & 0x03 ) != 0x03 )
497 // mpeg-2 pack header
498 pos
= 14 + ( buf
[13] & 0x7 ); // skip over the PACK
500 return (buf
[pos
+0] == 0x00) && (buf
[pos
+1] == 0x00) && (buf
[pos
+2] == 0x01);
503 static int check_ts_sync(const uint8_t *buf
)
505 // must have initial sync byte & a legal adaptation ctrl
506 return (buf
[0] == 0x47) && (((buf
[3] & 0x30) >> 4) > 0);
509 static int have_ts_sync(const uint8_t *buf
, int psize
, int count
)
512 for ( ii
= 0; ii
< count
; ii
++ )
514 if ( !check_ts_sync(&buf
[ii
*psize
]) )
520 static int hb_stream_check_for_ts(const uint8_t *buf
)
522 // transport streams should have a sync byte every 188 bytes.
523 // search the first 8KB of buf looking for at least 8 consecutive
524 // correctly located sync patterns.
528 for ( offset
= 0; offset
< 8*1024-count
*188; ++offset
)
530 if ( have_ts_sync( &buf
[offset
], 188, count
) )
531 return 188 | (offset
<< 8);
532 if ( have_ts_sync( &buf
[offset
], 192, count
) )
533 return 192 | (offset
<< 8);
534 if ( have_ts_sync( &buf
[offset
], 204, count
) )
535 return 204 | (offset
<< 8);
536 if ( have_ts_sync( &buf
[offset
], 208, count
) )
537 return 208 | (offset
<< 8);
542 static int hb_stream_check_for_ps(hb_stream_t
*stream
)
548 fseek(stream
->file_handle
, 0, SEEK_SET
);
550 // program streams should start with a PACK then some other mpeg start
551 // code (usually a SYS but that might be missing if we only have a clip).
552 while (pos
< 512 * 1024)
556 if ( fread(buf
, 1, sizeof(buf
), stream
->file_handle
) != sizeof(buf
) )
559 for ( offset
= 0; offset
< 8*1024-27; ++offset
)
561 if ( check_ps_sync( &buf
[offset
] ) && check_ps_sc( &buf
[offset
] ) )
563 int pes_offset
, prev
, data_len
;
565 uint8_t *b
= buf
+offset
;
567 // Skip the pack header
568 int mark
= buf
[4] >> 4;
571 // mpeg-1 pack header
576 // mpeg-2 pack header
577 pes_offset
= 14 + ( buf
[13] & 0x7 );
581 // Get the next stream id
583 data_len
= (b
[4] << 8) + b
[5];
584 if ( data_len
&& sid
> 0xba && sid
< 0xf9 )
586 prev
= ftell( stream
->file_handle
);
587 pos
= prev
- ( sizeof(buf
) - offset
);
588 pos
+= pes_offset
+ 6 + data_len
;
589 fseek( stream
->file_handle
, pos
, SEEK_SET
);
590 if ( fread(sc_buf
, 1, 4, stream
->file_handle
) != 4 )
592 if (sc_buf
[0] == 0x00 && sc_buf
[1] == 0x00 &&
597 fseek( stream
->file_handle
, prev
, SEEK_SET
);
601 fseek( stream
->file_handle
, -27, SEEK_CUR
);
602 pos
= ftell( stream
->file_handle
);
607 static int hb_stream_get_type(hb_stream_t
*stream
)
611 if ( fread(buf
, 1, sizeof(buf
), stream
->file_handle
) == sizeof(buf
) )
614 if ( ( psize
= hb_stream_check_for_ts(buf
) ) != 0 )
616 int offset
= psize
>> 8;
618 hb_log("file is MPEG Transport Stream with %d byte packets"
619 " offset %d bytes", psize
, offset
);
620 stream
->packetsize
= psize
;
621 stream
->hb_stream_type
= transport
;
622 if (hb_ts_stream_init(stream
) == 0)
625 else if ( hb_stream_check_for_ps(stream
) != 0 )
627 hb_log("file is MPEG Program Stream");
628 stream
->hb_stream_type
= program
;
629 hb_ps_stream_init(stream
);
630 // We default to mpeg codec for ps streams if no
631 // video found in program stream map
638 static void hb_stream_delete_dynamic( hb_stream_t
*d
)
642 fclose( d
->file_handle
);
643 d
->file_handle
= NULL
;
650 free( d
->ts
.packet
);
655 for (i
= 0; i
< d
->ts
.count
; i
++)
657 if (d
->ts
.list
[i
].buf
)
659 hb_buffer_close(&(d
->ts
.list
[i
].buf
));
660 d
->ts
.list
[i
].buf
= NULL
;
666 static void hb_stream_delete( hb_stream_t
*d
)
668 hb_stream_delete_dynamic( d
);
675 static int audio_inactive( hb_stream_t
*stream
, int id
, int stream_id_ext
)
679 // PID declared inactive by hb_stream_title_scan
682 if ( id
== stream
->pmt_info
.PCR_PID
)
684 // PCR PID is always active
689 for ( i
= 0; i
< hb_list_count( stream
->title
->list_audio
); ++i
)
691 hb_audio_t
*audio
= hb_list_item( stream
->title
->list_audio
, i
);
692 if ( audio
->id
== ((stream_id_ext
<< 16) | id
) )
700 /* when the file was first opened we made entries for all the audio elementary
701 * streams we found in it. Streams that were later found during the preview scan
702 * now have an audio codec, type, rate, etc., associated with them. At the end
703 * of the scan we delete all the audio entries that weren't found by the scan
704 * or don't have a format we support. This routine deletes audio entry 'indx'
705 * by setting its PID to an invalid value so no packet will match it. (We can't
706 * move any of the entries since the index of the entry is used as the id
707 * of the media stream for HB. */
708 static void hb_stream_delete_ts_entry(hb_stream_t
*stream
, int indx
)
710 if ( stream
->ts
.list
[indx
].pid
> 0 )
712 stream
->ts
.list
[indx
].pid
= -stream
->ts
.list
[indx
].pid
;
716 static int hb_stream_try_delete_ts_entry(hb_stream_t
*stream
, int indx
)
720 if ( stream
->ts
.list
[indx
].pid
< 0 )
723 for ( ii
= stream
->ts
.list
[indx
].pes_list
; ii
!= -1;
724 ii
= stream
->pes
.list
[ii
].next
)
726 if ( stream
->pes
.list
[ii
].stream_id
>= 0 )
729 stream
->ts
.list
[indx
].pid
= -stream
->ts
.list
[indx
].pid
;
733 static void hb_stream_delete_ps_entry(hb_stream_t
*stream
, int indx
)
735 if ( stream
->pes
.list
[indx
].stream_id
> 0 )
737 stream
->pes
.list
[indx
].stream_id
= -stream
->pes
.list
[indx
].stream_id
;
741 static void prune_streams(hb_stream_t
*d
)
743 if ( d
->hb_stream_type
== transport
)
746 for ( ii
= 0; ii
< d
->ts
.count
; ii
++)
748 // If probing didn't find audio or video, and the pid
749 // is not the PCR, remove the track
750 if ( ts_stream_kind ( d
, ii
) == U
&&
751 !d
->ts
.list
[ii
].is_pcr
)
753 hb_stream_delete_ts_entry(d
, ii
);
757 if ( ts_stream_kind ( d
, ii
) == A
)
759 for ( jj
= d
->ts
.list
[ii
].pes_list
; jj
!= -1;
760 jj
= d
->pes
.list
[jj
].next
)
762 if ( audio_inactive( d
, d
->pes
.list
[jj
].stream_id
,
763 d
->pes
.list
[jj
].stream_id_ext
) )
765 hb_stream_delete_ps_entry(d
, jj
);
768 if ( !d
->ts
.list
[ii
].is_pcr
&&
769 hb_stream_try_delete_ts_entry(d
, ii
) )
775 // reset to beginning of file and reset some stream
777 hb_stream_seek( d
, 0. );
779 else if ( d
->hb_stream_type
== program
)
782 for ( ii
= 0; ii
< d
->pes
.count
; ii
++)
784 // If probing didn't find audio or video, remove the track
785 if ( d
->pes
.list
[ii
].stream_kind
== U
)
787 hb_stream_delete_ps_entry(d
, ii
);
790 if ( d
->pes
.list
[ii
].stream_kind
== A
&&
791 audio_inactive( d
, d
->pes
.list
[ii
].stream_id
,
792 d
->pes
.list
[ii
].stream_id_ext
) )
794 // this PID isn't wanted (we don't have a codec for it
795 // or scan didn't find audio parameters)
796 hb_stream_delete_ps_entry(d
, ii
);
800 // reset to beginning of file and reset some stream
802 hb_stream_seek( d
, 0. );
806 /***********************************************************************
808 ***********************************************************************
810 **********************************************************************/
812 hb_stream_open(hb_handle_t
*h
, char *path
, hb_title_t
*title
, int scan
)
814 FILE *f
= hb_fopen(path
, "rb");
817 hb_log( "hb_stream_open: open %s failed", path
);
821 hb_stream_t
*d
= calloc( sizeof( hb_stream_t
), 1 );
825 hb_log( "hb_stream_open: can't allocate space for %s stream state", path
);
829 if( title
&& !( title
->flags
& HBTF_NO_IDR
) )
835 * If it's something we can deal with (MPEG2 PS or TS) return a stream
836 * reference structure & null otherwise.
842 d
->path
= strdup( path
);
843 if (d
->path
!= NULL
)
845 // XXX: DXVA2 integration code requires an AVFormatContext
846 // use lavf instead of our MPEG demuxer when it's enabled
847 if (!hb_hwd_enabled(d
->h
) && hb_stream_get_type( d
) != 0 )
853 // reset to beginning of file and reset some stream
855 hb_stream_seek( d
, 0. );
858 fclose( d
->file_handle
);
859 d
->file_handle
= NULL
;
860 if ( ffmpeg_open( d
, title
, scan
) )
865 if ( d
->file_handle
)
867 fclose( d
->file_handle
);
873 hb_log( "hb_stream_open: open %s failed", path
);
878 static int new_pid( hb_stream_t
* stream
)
880 int num
= stream
->ts
.alloc
;
882 if ( stream
->ts
.count
== stream
->ts
.alloc
)
884 num
= stream
->ts
.alloc
? stream
->ts
.alloc
* 2 : 32;
885 stream
->ts
.list
= realloc( stream
->ts
.list
,
886 sizeof( hb_ts_stream_t
) * num
);
889 for ( ii
= stream
->ts
.alloc
; ii
< num
; ii
++ )
891 memset(&stream
->ts
.list
[ii
], 0, sizeof( hb_ts_stream_t
));
892 stream
->ts
.list
[ii
].continuity
= -1;
893 stream
->ts
.list
[ii
].pid
= -1;
894 stream
->ts
.list
[ii
].pes_list
= -1;
896 stream
->ts
.alloc
= num
;
897 num
= stream
->ts
.count
;
903 static int new_pes( hb_stream_t
* stream
)
905 int num
= stream
->pes
.alloc
;
907 if ( stream
->pes
.count
== stream
->pes
.alloc
)
909 num
= stream
->pes
.alloc
? stream
->pes
.alloc
* 2 : 32;
910 stream
->pes
.list
= realloc( stream
->pes
.list
,
911 sizeof( hb_pes_stream_t
) * num
);
914 for ( ii
= stream
->pes
.alloc
; ii
< num
; ii
++ )
916 memset(&stream
->pes
.list
[ii
], 0, sizeof( hb_pes_stream_t
));
917 stream
->pes
.list
[ii
].stream_id
= -1;
918 stream
->pes
.list
[ii
].next
= -1;
920 stream
->pes
.alloc
= num
;
921 num
= stream
->pes
.count
;
927 hb_stream_t
* hb_bd_stream_open( hb_handle_t
*h
, hb_title_t
*title
)
931 hb_stream_t
*d
= calloc( sizeof( hb_stream_t
), 1 );
934 hb_error( "hb_bd_stream_open: can't allocate space for stream state" );
939 d
->file_handle
= NULL
;
944 int pid
= title
->video_id
;
945 int stream_type
= title
->video_stream_type
;
946 update_ts_streams( d
, pid
, 0, stream_type
, V
, NULL
);
949 for ( ii
= 0; ( audio
= hb_list_item( title
->list_audio
, ii
) ); ++ii
)
951 int stream_id_ext
= audio
->config
.in
.substream_type
;
952 pid
= audio
->id
& 0xFFFF;
953 stream_type
= audio
->config
.in
.stream_type
;
955 update_ts_streams( d
, pid
, stream_id_ext
, stream_type
, A
, NULL
);
958 hb_subtitle_t
* subtitle
;
959 for ( ii
= 0; ( subtitle
= hb_list_item( title
->list_subtitle
, ii
) ); ++ii
)
961 // If the subtitle track is CC embedded in the video stream, then
962 // it does not have an independent pid. In this case, we assigned
963 // the subtitle->id to 0.
964 if (subtitle
->id
!= 0)
966 pid
= subtitle
->id
& 0xFFFF;
967 stream_type
= subtitle
->stream_type
;
969 update_ts_streams( d
, pid
, 0, stream_type
, S
, NULL
);
973 // We don't need to wait for a PCR when scanning. In fact, it
974 // trips us up on the first preview of every title since we would
975 // have to read quite a lot of data before finding the PCR.
976 if ( title
->flags
& HBTF_SCAN_COMPLETE
)
978 /* BD has PCRs, but the BD index always points to a packet
979 * after a PCR packet, so we will not see the initial PCR
980 * after any seek. So don't set the flag that causes us
981 * to drop packets till we see a PCR. */
982 //d->ts_flags = TS_HAS_RAP | TS_HAS_PCR;
984 // BD PCR PID is specified to always be 0x1001
985 update_ts_streams( d
, 0x1001, 0, -1, P
, NULL
);
989 d
->hb_stream_type
= transport
;
991 for ( ii
= 0; ii
< d
->ts
.count
; ii
++ )
993 d
->ts
.list
[ii
].buf
= hb_buffer_init(d
->packetsize
);
994 d
->ts
.list
[ii
].buf
->size
= 0;
1000 /***********************************************************************
1002 ***********************************************************************
1003 * Closes and frees everything
1004 **********************************************************************/
1005 void hb_stream_close( hb_stream_t
** _d
)
1007 hb_stream_t
*stream
= * _d
;
1009 if ( stream
->hb_stream_type
== ffmpeg
)
1011 ffmpeg_close( stream
);
1012 hb_stream_delete( stream
);
1017 if ( stream
->frames
)
1019 hb_log( "stream: %d good frames, %d errors (%.0f%%)", stream
->frames
,
1020 stream
->errors
, (double)stream
->errors
* 100. /
1021 (double)stream
->frames
);
1024 hb_stream_delete( stream
);
1028 /***********************************************************************
1029 * hb_ps_stream_title_scan
1030 ***********************************************************************
1032 **********************************************************************/
1033 hb_title_t
* hb_stream_title_scan(hb_stream_t
*stream
, hb_title_t
* title
)
1035 if ( stream
->hb_stream_type
== ffmpeg
)
1036 return ffmpeg_title_scan( stream
, title
);
1038 // 'Barebones Title'
1039 title
->type
= HB_STREAM_TYPE
;
1041 // Copy part of the stream path to the title name
1042 char *sep
= hb_strr_dir_sep(stream
->path
);
1044 strcpy(title
->name
, sep
+1);
1045 char *dot_term
= strrchr(title
->name
, '.');
1049 // Figure out how many audio streams we really have:
1050 // - For transport streams, for each PID listed in the PMT (whether
1051 // or not it was an audio stream type) read the bitstream until we
1052 // find an packet from that PID containing a PES header and see if
1053 // the elementary stream is an audio type.
1054 // - For program streams read the first 4MB and take every unique
1055 // audio stream we find.
1056 hb_init_audio_list(stream
, title
);
1057 hb_init_subtitle_list(stream
, title
);
1059 // set the video id, codec & muxer
1060 int idx
= pes_index_of_video( stream
);
1063 hb_title_close( &title
);
1067 title
->video_id
= get_id( &stream
->pes
.list
[idx
] );
1068 title
->video_codec
= stream
->pes
.list
[idx
].codec
;
1069 title
->video_codec_param
= stream
->pes
.list
[idx
].codec_param
;
1071 if (stream
->hb_stream_type
== transport
)
1073 title
->demuxer
= HB_TS_DEMUXER
;
1075 // make sure we're grabbing the PCR PID
1076 update_ts_streams( stream
, stream
->pmt_info
.PCR_PID
, 0, -1, P
, NULL
);
1080 title
->demuxer
= HB_PS_DEMUXER
;
1083 // IDRs will be search for in hb_stream_duration
1084 stream
->has_IDRs
= 0;
1085 hb_stream_duration(stream
, title
);
1088 hb_chapter_t
* chapter
;
1089 chapter
= calloc( sizeof( hb_chapter_t
), 1 );
1090 hb_chapter_set_title( chapter
, "Chapter 1" );
1092 chapter
->duration
= title
->duration
;
1093 chapter
->hours
= title
->hours
;
1094 chapter
->minutes
= title
->minutes
;
1095 chapter
->seconds
= title
->seconds
;
1096 hb_list_add( title
->list_chapter
, chapter
);
1098 if ( stream
->has_IDRs
< 1 )
1100 hb_log( "stream doesn't seem to have video IDR frames" );
1101 title
->flags
|= HBTF_NO_IDR
;
1104 if ( stream
->hb_stream_type
== transport
&&
1105 ( stream
->ts_flags
& TS_HAS_PCR
) == 0 )
1107 hb_log( "transport stream missing PCRs - using video DTS instead" );
1110 // Height, width, rate and aspect ratio information is filled in
1111 // when the previews are built
1116 * read the next transport stream packet from 'stream'. Return NULL if
1117 * we hit eof & a pointer to the sync byte otherwise.
1119 static const uint8_t *next_packet( hb_stream_t
*stream
)
1121 uint8_t *buf
= stream
->ts
.packet
+ stream
->packetsize
- 188;
1125 if ( fread(stream
->ts
.packet
, 1, stream
->packetsize
, stream
->file_handle
) !=
1126 stream
->packetsize
)
1129 if ((err
= ferror(stream
->file_handle
)) != 0)
1131 hb_error("next_packet: error (%d)", err
);
1132 hb_set_work_error(stream
->h
, HB_ERROR_READ
);
1140 // lost sync - back up to where we started then try to re-establish.
1141 off_t pos
= ftello(stream
->file_handle
) - stream
->packetsize
;
1142 off_t pos2
= align_to_next_packet(stream
);
1145 hb_log( "next_packet: eof while re-establishing sync @ %"PRId64
, pos
);
1148 ts_warn( stream
, "next_packet: sync lost @ %"PRId64
", regained after %"PRId64
" bytes",
1154 * skip to the start of the next PACK header in program stream src_stream.
1156 static void skip_to_next_pack( hb_stream_t
*src_stream
)
1158 // scan forward until we find the start of the next pack
1159 uint32_t strt_code
= -1;
1162 flockfile( src_stream
->file_handle
);
1163 while ( ( c
= getc_unlocked( src_stream
->file_handle
) ) != EOF
)
1165 strt_code
= ( strt_code
<< 8 ) | c
;
1166 if ( strt_code
== 0x000001ba )
1167 // we found the start of the next pack
1170 funlockfile( src_stream
->file_handle
);
1172 // if we didn't terminate on an eof back up so the next read
1173 // starts on the pack boundary.
1176 fseeko( src_stream
->file_handle
, -4, SEEK_CUR
);
1180 static void CreateDecodedNAL( uint8_t **dst
, int *dst_len
,
1181 const uint8_t *src
, int src_len
)
1183 const uint8_t *end
= &src
[src_len
];
1184 uint8_t *d
= malloc( src_len
);
1192 if( src
< end
- 3 && src
[0] == 0x00 && src
[1] == 0x00 &&
1195 // Next start code found
1198 if( src
< end
- 3 && src
[0] == 0x00 && src
[1] == 0x00 &&
1210 *dst_len
= d
- *dst
;
1213 static int isRecoveryPoint( const uint8_t *buf
, int len
)
1218 int recovery_frames
= 0;
1220 CreateDecodedNAL( &nal
, &nal_len
, buf
, len
);
1222 for ( ii
= 0; ii
+1 < nal_len
; )
1225 while ( ii
+1 < nal_len
)
1228 if ( nal
[ii
-1] != 0xff )
1232 while ( ii
+1 < nal_len
)
1235 if ( nal
[ii
-1] != 0xff )
1241 recovery_frames
= 1;
1248 return recovery_frames
;
1251 static int isIframe( hb_stream_t
*stream
, const uint8_t *buf
, int len
)
1253 // For mpeg2: look for a gop start or i-frame picture start
1254 // for h.264: look for idr nal type or a slice header for an i-frame
1255 // for vc1: look for a Sequence header
1260 int vid
= pes_index_of_video( stream
);
1261 hb_pes_stream_t
*pes
= &stream
->pes
.list
[vid
];
1262 if ( pes
->stream_type
<= 2 ||
1263 pes
->codec_param
== AV_CODEC_ID_MPEG1VIDEO
||
1264 pes
->codec_param
== AV_CODEC_ID_MPEG2VIDEO
)
1266 // This section of the code handles MPEG-1 and MPEG-2 video streams
1267 for (ii
= 0; ii
< len
; ii
++)
1269 strid
= (strid
<< 8) | buf
[ii
];
1270 if ( ( strid
>> 8 ) == 1 )
1272 // we found a start code
1276 case 0xB8: // group_start_code (GOP header)
1277 case 0xB3: // sequence_header code
1280 case 0x00: // picture_start_code
1281 // picture_header, let's see if it's an I-frame
1284 // check if picture_coding_type == 1
1285 if ((buf
[ii
+2] & (0x7 << 3)) == (1 << 3))
1287 // found an I-frame picture
1295 // didn't find an I-frame
1298 if ( pes
->stream_type
== 0x1b || pes
->codec_param
== AV_CODEC_ID_H264
)
1300 // we have an h.264 stream
1301 for (ii
= 0; ii
< len
; ii
++)
1303 strid
= (strid
<< 8) | buf
[ii
];
1304 if ( ( strid
>> 8 ) == 1 )
1306 // we found a start code - remove the ref_idc from the nal type
1307 uint8_t nal_type
= strid
& 0x1f;
1308 if ( nal_type
== 0x01 )
1310 // Found slice and no recovery point
1313 if ( nal_type
== 0x05 )
1315 // h.264 IDR picture start
1318 else if ( nal_type
== 0x06 )
1321 int recovery_frames
= isRecoveryPoint( buf
+off
, len
-off
);
1322 if ( recovery_frames
)
1324 return recovery_frames
;
1329 // didn't find an I-frame
1332 if ( pes
->stream_type
== 0xea || pes
->codec_param
== AV_CODEC_ID_VC1
)
1334 // we have an vc1 stream
1335 for (ii
= 0; ii
< len
; ii
++)
1337 strid
= (strid
<< 8) | buf
[ii
];
1338 if ( strid
== 0x10f )
1340 // the ffmpeg vc1 decoder requires a seq hdr code in the first
1345 // didn't find an I-frame
1348 if ( pes
->stream_type
== 0x10 || pes
->codec_param
== AV_CODEC_ID_MPEG4
)
1350 // we have an mpeg4 stream
1351 for (ii
= 0; ii
< len
-1; ii
++)
1353 strid
= (strid
<< 8) | buf
[ii
];
1354 if ( strid
== 0x1b6 )
1356 if ((buf
[ii
+1] & 0xC0) == 0)
1360 // didn't find an I-frame
1364 // we don't understand the stream type so just say "yes" otherwise
1365 // we'll discard all the video.
1369 static int ts_isIframe( hb_stream_t
*stream
, const uint8_t *buf
, int adapt_len
)
1371 return isIframe( stream
, buf
+ 13 + adapt_len
, 188 - ( 13 + adapt_len
) );
1375 * scan the next MB of 'stream' to find the next start packet for
1376 * the Packetized Elementary Stream associated with TS PID 'pid'.
1378 static const uint8_t *hb_ts_stream_getPEStype(hb_stream_t
*stream
, uint32_t pid
, int *out_adapt_len
)
1380 int npack
= 300000; // max packets to read
1382 while (--npack
>= 0)
1384 const uint8_t *buf
= next_packet( stream
);
1387 hb_log("hb_ts_stream_getPEStype: EOF while searching for PID 0x%x", pid
);
1391 // while we're reading the stream, check if it has valid PCRs
1392 // and/or random access points.
1393 uint32_t pack_pid
= ( (buf
[1] & 0x1f) << 8 ) | buf
[2];
1394 if ( pack_pid
== stream
->pmt_info
.PCR_PID
)
1396 if ( ( buf
[5] & 0x10 ) &&
1397 ( ( ( buf
[3] & 0x30 ) == 0x20 ) ||
1398 ( ( buf
[3] & 0x30 ) == 0x30 && buf
[4] > 6 ) ) )
1400 stream
->ts_flags
|= TS_HAS_PCR
;
1403 if ( buf
[5] & 0x40 )
1405 stream
->ts_flags
|= TS_HAS_RAP
;
1409 * The PES header is only in TS packets with 'start' set so we check
1410 * that first then check for the right PID.
1412 if ((buf
[1] & 0x40) == 0 || pack_pid
!= pid
)
1414 // not a start packet or not the pid we want
1419 /* skip over the TS hdr to return a pointer to the PES hdr */
1420 switch (buf
[3] & 0x30)
1422 case 0x00: // illegal
1423 case 0x20: // fill packet
1426 case 0x30: // adaptation
1427 adapt_len
= buf
[4] + 1;
1428 if (adapt_len
> 184)
1430 hb_log("hb_ts_stream_getPEStype: invalid adaptation field length %d for PID 0x%x", buf
[4], pid
);
1435 /* PES hdr has to begin with an mpeg start code */
1436 if (buf
[adapt_len
+4] == 0x00 && buf
[adapt_len
+5] == 0x00 && buf
[adapt_len
+6] == 0x01)
1438 *out_adapt_len
= adapt_len
;
1443 /* didn't find it */
1447 static hb_buffer_t
* hb_ps_stream_getVideo(
1448 hb_stream_t
*stream
,
1451 hb_buffer_t
*buf
= hb_buffer_init(HB_DVD_READ_BUFFER_SIZE
);
1452 hb_pes_info_t pes_info
;
1453 // how many blocks we read while searching for a video PES header
1454 int blksleft
= 2048;
1456 while (--blksleft
>= 0)
1459 int len
= hb_ps_read_packet( stream
, buf
);
1465 if ( !hb_parse_ps( stream
, buf
->data
, buf
->size
, &pes_info
) )
1469 if ( pes_info
.stream_id
== 0xbd )
1471 idx
= index_of_ps_stream( stream
, pes_info
.stream_id
,
1472 pes_info
.bd_substream_id
);
1476 idx
= index_of_ps_stream( stream
, pes_info
.stream_id
,
1477 pes_info
.stream_id_ext
);
1479 if ( stream
->pes
.list
[idx
].stream_kind
== V
)
1481 if ( pes_info
.pts
!= AV_NOPTS_VALUE
)
1488 hb_buffer_close( &buf
);
1492 /***********************************************************************
1493 * hb_stream_duration
1494 ***********************************************************************
1496 * Finding stream duration is difficult. One issue is that the video file
1497 * may have chunks from several different program fragments (main feature,
1498 * commercials, station id, trailers, etc.) all with their own base pts
1499 * value. We can't find the piece boundaries without reading the entire
1500 * file but if we compute a rate based on time stamps from two different
1501 * pieces the result will be meaningless. The second issue is that the
1502 * data rate of compressed video normally varies by 5-10x over the length
1503 * of the video. This says that we want to compute the rate over relatively
1504 * long segments to get a representative average but long segments increase
1505 * the likelihood that we'll cross a piece boundary.
1507 * What we do is take time stamp samples at several places in the file
1508 * (currently 16) then compute the average rate (i.e., ticks of video per
1509 * byte of the file) for all pairs of samples (N^2 rates computed for N
1510 * samples). Some of those rates will be absurd because the samples came
1511 * from different segments. Some will be way low or high because the
1512 * samples came from a low or high motion part of the segment. But given
1513 * that we're comparing *all* pairs the majority of the computed rates
1514 * should be near the overall average. So we median filter the computed
1515 * rates to pick the most representative value.
1517 **********************************************************************/
1519 uint64_t pos
; /* file position of this PTS sample */
1520 uint64_t pts
; /* PTS from video stream */
1523 #define NDURSAMPLES 128
1525 // get one (position, timestamp) sampple from a transport or program
1527 static struct pts_pos
hb_sample_pts(hb_stream_t
*stream
, uint64_t fpos
)
1529 struct pts_pos pp
= { 0, 0 };
1531 if ( stream
->hb_stream_type
== transport
)
1535 fseeko( stream
->file_handle
, fpos
, SEEK_SET
);
1536 align_to_next_packet( stream
);
1537 int pid
= stream
->ts
.list
[ts_index_of_video(stream
)].pid
;
1538 buf
= hb_ts_stream_getPEStype( stream
, pid
, &adapt_len
);
1541 hb_log("hb_sample_pts: couldn't find video packet near %"PRIu64
, fpos
);
1544 const uint8_t *pes
= buf
+ 4 + adapt_len
;
1545 if ( ( pes
[7] >> 7 ) != 1 )
1547 hb_log("hb_sample_pts: no PTS in video packet near %"PRIu64
, fpos
);
1550 pp
.pts
= ((((uint64_t)pes
[ 9] >> 1 ) & 7) << 30) |
1551 ( (uint64_t)pes
[10] << 22) |
1552 ( ((uint64_t)pes
[11] >> 1 ) << 15) |
1553 ( (uint64_t)pes
[12] << 7 ) |
1554 ( (uint64_t)pes
[13] >> 1 );
1556 if ( ts_isIframe( stream
, buf
, adapt_len
) )
1558 if ( stream
->has_IDRs
< 255 )
1563 pp
.pos
= ftello(stream
->file_handle
);
1564 if ( !stream
->has_IDRs
)
1566 // Scan a little more to see if we will stumble upon one
1568 for ( ii
= 0; ii
< 10; ii
++ )
1570 buf
= hb_ts_stream_getPEStype( stream
, pid
, &adapt_len
);
1573 if ( ts_isIframe( stream
, buf
, adapt_len
) )
1584 hb_pes_info_t pes_info
;
1586 // round address down to nearest dvd sector start
1587 fpos
&=~ ( HB_DVD_READ_BUFFER_SIZE
- 1 );
1588 fseeko( stream
->file_handle
, fpos
, SEEK_SET
);
1589 if ( stream
->hb_stream_type
== program
)
1591 skip_to_next_pack( stream
);
1593 buf
= hb_ps_stream_getVideo( stream
, &pes_info
);
1596 hb_log("hb_sample_pts: couldn't find video packet near %"PRIu64
, fpos
);
1599 if ( pes_info
.pts
< 0 )
1601 hb_log("hb_sample_pts: no PTS in video packet near %"PRIu64
, fpos
);
1602 hb_buffer_close( &buf
);
1605 if ( isIframe( stream
, buf
->data
, buf
->size
) )
1607 if ( stream
->has_IDRs
< 255 )
1612 hb_buffer_close( &buf
);
1613 if ( !stream
->has_IDRs
)
1615 // Scan a little more to see if we will stumble upon one
1617 for ( ii
= 0; ii
< 10; ii
++ )
1619 buf
= hb_ps_stream_getVideo( stream
, &pes_info
);
1622 if ( isIframe( stream
, buf
->data
, buf
->size
) )
1625 hb_buffer_close( &buf
);
1628 hb_buffer_close( &buf
);
1632 pp
.pts
= pes_info
.pts
;
1633 pp
.pos
= ftello(stream
->file_handle
);
1638 static int dur_compare( const void *a
, const void *b
)
1640 const double *aval
= a
, *bval
= b
;
1641 return ( *aval
< *bval
? -1 : ( *aval
== *bval
? 0 : 1 ) );
1644 // given an array of (position, time) samples, compute a max-likelihood
1645 // estimate of the average rate by computing the rate between all pairs
1646 // of samples then taking the median of those rates.
1647 static double compute_stream_rate( struct pts_pos
*pp
, int n
)
1650 double rates
[NDURSAMPLES
* NDURSAMPLES
/ 8];
1653 // the following nested loops compute the rates between all pairs.
1655 for ( i
= 0; i
< n
-1; ++i
)
1657 // Bias the median filter by not including pairs that are "far"
1658 // from one another. This is to handle cases where the file is
1659 // made of roughly equal size pieces where a symmetric choice of
1660 // pairs results in having the same number of intra-piece &
1661 // inter-piece rate estimates. This would mean that the median
1662 // could easily fall in the inter-piece part of the data which
1663 // would give a bogus estimate. The 'ns' index creates an
1664 // asymmetry that favors locality.
1665 int ns
= i
+ ( n
>> 3 );
1668 for ( j
= i
+1; j
< ns
; ++j
)
1670 if ( (uint64_t)(pp
[j
].pts
- pp
[i
].pts
) > 90000LL*3600*6 )
1672 if ( pp
[j
].pts
!= pp
[i
].pts
&& pp
[j
].pos
> pp
[i
].pos
)
1674 *rp
= ((double)( pp
[j
].pts
- pp
[i
].pts
)) /
1675 ((double)( pp
[j
].pos
- pp
[i
].pos
));
1680 // now compute and return the median of all the (n*n/2) rates we computed
1682 int nrates
= rp
- rates
;
1683 qsort( rates
, nrates
, sizeof (rates
[0] ), dur_compare
);
1684 return rates
[nrates
>> 1];
1687 static void hb_stream_duration(hb_stream_t
*stream
, hb_title_t
*inTitle
)
1689 struct pts_pos ptspos
[NDURSAMPLES
];
1690 struct pts_pos
*pp
= ptspos
;
1693 fseeko(stream
->file_handle
, 0, SEEK_END
);
1694 uint64_t fsize
= ftello(stream
->file_handle
);
1695 uint64_t fincr
= fsize
/ NDURSAMPLES
;
1696 uint64_t fpos
= fincr
/ 2;
1697 for ( i
= NDURSAMPLES
; --i
>= 0; fpos
+= fincr
)
1699 *pp
++ = hb_sample_pts(stream
, fpos
);
1701 uint64_t dur
= compute_stream_rate( ptspos
, pp
- ptspos
) * (double)fsize
;
1702 inTitle
->duration
= dur
;
1704 inTitle
->hours
= dur
/ 3600;
1705 inTitle
->minutes
= ( dur
% 3600 ) / 60;
1706 inTitle
->seconds
= dur
% 60;
1708 rewind(stream
->file_handle
);
1711 /***********************************************************************
1713 ***********************************************************************
1715 **********************************************************************/
1716 hb_buffer_t
* hb_stream_read( hb_stream_t
* src_stream
)
1718 if ( src_stream
->hb_stream_type
== ffmpeg
)
1720 return hb_ffmpeg_read( src_stream
);
1722 if ( src_stream
->hb_stream_type
== program
)
1724 return hb_ps_stream_decode( src_stream
);
1726 return hb_ts_stream_decode( src_stream
);
1729 int64_t ffmpeg_initial_timestamp( hb_stream_t
* stream
)
1731 AVFormatContext
*ic
= stream
->ffmpeg_ic
;
1732 if ( ic
->start_time
!= AV_NOPTS_VALUE
&& ic
->start_time
> 0 )
1733 return ic
->start_time
;
1738 int hb_stream_seek_chapter( hb_stream_t
* stream
, int chapter_num
)
1741 if ( stream
->hb_stream_type
!= ffmpeg
)
1743 // currently meaningliess for transport and program streams
1746 if ( !stream
|| !stream
->title
||
1747 chapter_num
> hb_list_count( stream
->title
->list_chapter
) )
1752 int64_t sum_dur
= 0;
1753 hb_chapter_t
*chapter
= NULL
;
1755 for ( i
= 0; i
< chapter_num
; ++i
)
1757 chapter
= hb_list_item( stream
->title
->list_chapter
, i
);
1758 sum_dur
+= chapter
->duration
;
1760 stream
->chapter
= chapter_num
- 1;
1761 stream
->chapter_end
= sum_dur
;
1763 int64_t pos
= ( ( ( sum_dur
- chapter
->duration
) * AV_TIME_BASE
) / 90000 ) + ffmpeg_initial_timestamp( stream
);
1765 hb_deep_log( 2, "Seeking to chapter %d: starts %"PRId64
", ends %"PRId64
", AV pos %"PRId64
,
1766 chapter_num
, sum_dur
- chapter
->duration
, sum_dur
, pos
);
1768 if ( chapter_num
> 1 && pos
> 0 )
1770 AVStream
*st
= stream
->ffmpeg_ic
->streams
[stream
->ffmpeg_video_id
];
1771 // timebase must be adjusted to match timebase of stream we are
1772 // using for seeking.
1773 pos
= av_rescale(pos
, st
->time_base
.den
, AV_TIME_BASE
* (int64_t)st
->time_base
.num
);
1774 avformat_seek_file( stream
->ffmpeg_ic
, stream
->ffmpeg_video_id
, 0, pos
, pos
, AVSEEK_FLAG_BACKWARD
);
1779 /***********************************************************************
1781 ***********************************************************************
1782 * Return the number of the chapter that we are currently in. We store
1783 * the chapter number starting from 0, so + 1 for the real chpater num.
1784 **********************************************************************/
1785 int hb_stream_chapter( hb_stream_t
* src_stream
)
1787 return( src_stream
->chapter
+ 1 );
1790 /***********************************************************************
1792 ***********************************************************************
1794 **********************************************************************/
1795 int hb_stream_seek( hb_stream_t
* stream
, float f
)
1797 if ( stream
->hb_stream_type
== ffmpeg
)
1799 return ffmpeg_seek( stream
, f
);
1801 off_t stream_size
, cur_pos
, new_pos
;
1802 double pos_ratio
= f
;
1803 cur_pos
= ftello( stream
->file_handle
);
1804 fseeko( stream
->file_handle
, 0, SEEK_END
);
1805 stream_size
= ftello( stream
->file_handle
);
1806 new_pos
= (off_t
) ((double) (stream_size
) * pos_ratio
);
1807 new_pos
&=~ (HB_DVD_READ_BUFFER_SIZE
- 1);
1809 int r
= fseeko( stream
->file_handle
, new_pos
, SEEK_SET
);
1812 fseeko( stream
->file_handle
, cur_pos
, SEEK_SET
);
1816 if ( stream
->hb_stream_type
== transport
)
1818 // We need to drop the current decoder output and move
1819 // forwards to the next transport stream packet.
1820 hb_ts_stream_reset(stream
);
1821 align_to_next_packet(stream
);
1822 if ( !stream
->has_IDRs
)
1824 // the stream has no IDRs so don't look for one.
1825 stream
->need_keyframe
= 0;
1828 else if ( stream
->hb_stream_type
== program
)
1830 hb_ps_stream_reset(stream
);
1831 skip_to_next_pack( stream
);
1832 if ( !stream
->has_IDRs
)
1834 // the stream has no IDRs so don't look for one.
1835 stream
->need_keyframe
= 0;
1842 int hb_stream_seek_ts( hb_stream_t
* stream
, int64_t ts
)
1844 if ( stream
->hb_stream_type
== ffmpeg
)
1846 return ffmpeg_seek_ts( stream
, ts
);
1851 static char* strncpyupper( char *dst
, const char *src
, int len
)
1855 for ( ii
= 0; ii
< len
-1 && src
[ii
]; ii
++ )
1857 dst
[ii
] = islower(src
[ii
]) ? toupper(src
[ii
]) : src
[ii
];
1863 static const char *stream_type_name2(hb_stream_t
*stream
, hb_pes_stream_t
*pes
)
1865 static char codec_name_caps
[80];
1867 if ( stream
->reg_desc
== STR4_TO_UINT32("HDMV") )
1869 // Names for streams we know about.
1870 switch ( pes
->stream_type
)
1882 return "DTS-HD HRA";
1891 if ( st2codec
[pes
->stream_type
].name
)
1893 return st2codec
[pes
->stream_type
].name
;
1895 if ( pes
->codec_name
[0] != 0 )
1897 return pes
->codec_name
;
1899 if ( pes
->codec
& HB_ACODEC_FF_MASK
)
1901 AVCodec
* codec
= avcodec_find_decoder( pes
->codec_param
);
1902 if ( codec
&& codec
->name
&& codec
->name
[0] )
1904 strncpyupper( codec_name_caps
, codec
->name
, 80 );
1905 return codec_name_caps
;
1911 static void set_audio_description(hb_audio_t
*audio
, iso639_lang_t
*lang
)
1913 snprintf( audio
->config
.lang
.simple
,
1914 sizeof( audio
->config
.lang
.simple
), "%s",
1915 strlen( lang
->native_name
) ? lang
->native_name
: lang
->eng_name
);
1916 snprintf( audio
->config
.lang
.iso639_2
,
1917 sizeof( audio
->config
.lang
.iso639_2
), "%s", lang
->iso639_2
);
1918 audio
->config
.lang
.type
= 0;
1921 // Sort specifies the index in the audio list where you would
1922 // like sorted items to begin.
1923 static void pes_add_subtitle_to_title(
1924 hb_stream_t
*stream
,
1929 hb_pes_stream_t
*pes
= &stream
->pes
.list
[idx
];
1931 // Sort by id when adding to the list
1932 // This assures that they are always displayed in the same order
1933 int id
= get_id( pes
);
1935 hb_subtitle_t
*tmp
= NULL
;
1937 int count
= hb_list_count( title
->list_subtitle
);
1939 // Don't add the same audio twice. Search for audio.
1940 for ( i
= 0; i
< count
; i
++ )
1942 tmp
= hb_list_item( title
->list_subtitle
, i
);
1943 if ( id
== tmp
->id
)
1947 hb_subtitle_t
*subtitle
= calloc( sizeof( hb_subtitle_t
), 1 );
1948 iso639_lang_t
* lang
;
1950 subtitle
->track
= idx
;
1952 lang
= lang_for_code( pes
->lang_code
);
1953 snprintf( subtitle
->lang
, sizeof( subtitle
->lang
), "%s",
1954 strlen(lang
->native_name
) ? lang
->native_name
: lang
->eng_name
);
1955 snprintf( subtitle
->iso639_2
, sizeof( subtitle
->iso639_2
), "%s",
1958 switch ( pes
->codec
)
1960 case WORK_DECPGSSUB
:
1961 subtitle
->source
= PGSSUB
;
1962 subtitle
->format
= PICTURESUB
;
1963 subtitle
->config
.dest
= RENDERSUB
;
1965 case WORK_DECVOBSUB
:
1966 subtitle
->source
= VOBSUB
;
1967 subtitle
->format
= PICTURESUB
;
1968 subtitle
->config
.dest
= RENDERSUB
;
1971 // Unrecognized, don't add to list
1972 hb_log("unregonized subtitle!");
1976 subtitle
->reg_desc
= stream
->reg_desc
;
1977 subtitle
->stream_type
= pes
->stream_type
;
1978 subtitle
->substream_type
= pes
->stream_id_ext
;
1979 subtitle
->codec
= pes
->codec
;
1981 // Create a default palette since vob files do not include the
1983 if ( subtitle
->source
== VOBSUB
)
1985 subtitle
->palette
[0] = 0x108080;
1986 subtitle
->palette
[1] = 0x108080;
1987 subtitle
->palette
[2] = 0x108080;
1988 subtitle
->palette
[3] = 0xbff000;
1990 subtitle
->palette
[4] = 0xbff000;
1991 subtitle
->palette
[5] = 0x108080;
1992 subtitle
->palette
[6] = 0x108080;
1993 subtitle
->palette
[7] = 0x108080;
1995 subtitle
->palette
[8] = 0xbff000;
1996 subtitle
->palette
[9] = 0x108080;
1997 subtitle
->palette
[10] = 0x108080;
1998 subtitle
->palette
[11] = 0x108080;
2000 subtitle
->palette
[12] = 0x108080;
2001 subtitle
->palette
[13] = 0xbff000;
2002 subtitle
->palette
[14] = 0x108080;
2003 subtitle
->palette
[15] = 0x108080;
2006 hb_log("stream id 0x%x (type 0x%x substream 0x%x) subtitle 0x%x",
2007 pes
->stream_id
, pes
->stream_type
, pes
->stream_id_ext
, subtitle
->id
);
2009 // Search for the sort position
2012 sort
= sort
< count
? sort
: count
;
2013 for ( i
= sort
; i
< count
; i
++ )
2015 tmp
= hb_list_item( title
->list_subtitle
, i
);
2016 int sid
= tmp
->id
& 0xffff;
2017 int ssid
= tmp
->id
>> 16;
2018 if ( pes
->stream_id
< sid
)
2020 else if ( pes
->stream_id
<= sid
&&
2021 pes
->stream_id_ext
<= ssid
)
2026 hb_list_insert( title
->list_subtitle
, i
, subtitle
);
2030 hb_list_add( title
->list_subtitle
, subtitle
);
2034 // Sort specifies the index in the audio list where you would
2035 // like sorted items to begin.
2036 static void pes_add_audio_to_title(
2037 hb_stream_t
*stream
,
2042 hb_pes_stream_t
*pes
= &stream
->pes
.list
[idx
];
2044 // Sort by id when adding to the list
2045 // This assures that they are always displayed in the same order
2046 int id
= get_id( pes
);
2048 hb_audio_t
*tmp
= NULL
;
2050 int count
= hb_list_count( title
->list_audio
);
2052 // Don't add the same audio twice. Search for audio.
2053 for ( i
= 0; i
< count
; i
++ )
2055 tmp
= hb_list_item( title
->list_audio
, i
);
2056 if ( id
== tmp
->id
)
2060 hb_audio_t
*audio
= calloc( sizeof( hb_audio_t
), 1 );
2063 audio
->config
.in
.reg_desc
= stream
->reg_desc
;
2064 audio
->config
.in
.stream_type
= pes
->stream_type
;
2065 audio
->config
.in
.substream_type
= pes
->stream_id_ext
;
2067 audio
->config
.in
.codec
= pes
->codec
;
2068 audio
->config
.in
.codec_param
= pes
->codec_param
;
2070 set_audio_description(audio
, lang_for_code(pes
->lang_code
));
2072 hb_log("stream id 0x%x (type 0x%x substream 0x%x) audio 0x%x",
2073 pes
->stream_id
, pes
->stream_type
, pes
->stream_id_ext
, audio
->id
);
2075 audio
->config
.in
.track
= idx
;
2077 // Search for the sort position
2080 sort
= sort
< count
? sort
: count
;
2081 for ( i
= sort
; i
< count
; i
++ )
2083 tmp
= hb_list_item( title
->list_audio
, i
);
2084 int sid
= tmp
->id
& 0xffff;
2085 int ssid
= tmp
->id
>> 16;
2086 if ( pes
->stream_id
< sid
)
2088 else if ( pes
->stream_id
<= sid
&&
2089 pes
->stream_id_ext
<= ssid
)
2094 hb_list_insert( title
->list_audio
, i
, audio
);
2098 hb_list_add( title
->list_audio
, audio
);
2102 static void hb_init_subtitle_list(hb_stream_t
*stream
, hb_title_t
*title
)
2108 // First add all that were found in a map.
2109 for ( map_idx
= 0; 1; map_idx
++ )
2111 for ( ii
= 0; ii
< stream
->pes
.count
; ii
++ )
2113 if ( stream
->pes
.list
[ii
].stream_kind
== S
)
2115 if ( stream
->pes
.list
[ii
].map_idx
== map_idx
)
2117 pes_add_subtitle_to_title( stream
, ii
, title
, -1 );
2119 if ( stream
->pes
.list
[ii
].map_idx
> largest
)
2120 largest
= stream
->pes
.list
[ii
].map_idx
;
2123 if ( map_idx
> largest
)
2127 int count
= hb_list_count( title
->list_audio
);
2128 // Now add the reset. Sort them by stream id.
2129 for ( ii
= 0; ii
< stream
->pes
.count
; ii
++ )
2131 if ( stream
->pes
.list
[ii
].stream_kind
== S
)
2133 pes_add_subtitle_to_title( stream
, ii
, title
, count
);
2138 static void hb_init_audio_list(hb_stream_t
*stream
, hb_title_t
*title
)
2144 // First add all that were found in a map.
2145 for ( map_idx
= 0; 1; map_idx
++ )
2147 for ( ii
= 0; ii
< stream
->pes
.count
; ii
++ )
2149 if ( stream
->pes
.list
[ii
].stream_kind
== A
)
2151 if ( stream
->pes
.list
[ii
].map_idx
== map_idx
)
2153 pes_add_audio_to_title( stream
, ii
, title
, -1 );
2155 if ( stream
->pes
.list
[ii
].map_idx
> largest
)
2156 largest
= stream
->pes
.list
[ii
].map_idx
;
2159 if ( map_idx
> largest
)
2163 int count
= hb_list_count( title
->list_audio
);
2164 // Now add the reset. Sort them by stream id.
2165 for ( ii
= 0; ii
< stream
->pes
.count
; ii
++ )
2167 if ( stream
->pes
.list
[ii
].stream_kind
== A
)
2169 pes_add_audio_to_title( stream
, ii
, title
, count
);
2174 /***********************************************************************
2176 ***********************************************************************
2178 **********************************************************************/
2180 static int hb_ts_stream_init(hb_stream_t
*stream
)
2184 if ( stream
->ts
.list
)
2186 for (i
=0; i
< stream
->ts
.alloc
; i
++)
2188 stream
->ts
.list
[i
].continuity
= -1;
2189 stream
->ts
.list
[i
].pid
= -1;
2190 stream
->ts
.list
[i
].pes_list
= -1;
2193 stream
->ts
.count
= 0;
2195 if ( stream
->pes
.list
)
2197 for (i
=0; i
< stream
->pes
.alloc
; i
++)
2199 stream
->pes
.list
[i
].stream_id
= -1;
2200 stream
->pes
.list
[i
].next
= -1;
2203 stream
->pes
.count
= 0;
2205 stream
->ts
.packet
= malloc( stream
->packetsize
);
2207 // Find the audio and video pids in the stream
2208 if (hb_ts_stream_find_pids(stream
) < 0)
2213 // hb_ts_resolve_pid_types reads some data, so the TS buffers
2215 for (i
= 0; i
< stream
->ts
.count
; i
++)
2217 // demuxing buffer for TS to PS conversion
2218 stream
->ts
.list
[i
].buf
= hb_buffer_init(stream
->packetsize
);
2219 stream
->ts
.list
[i
].buf
->size
= 0;
2221 hb_ts_resolve_pid_types(stream
);
2225 hb_log("Found the following PIDS");
2226 hb_log(" Video PIDS : ");
2227 for (i
=0; i
< stream
->ts
.count
; i
++)
2229 if ( ts_stream_kind( stream
, i
) == V
)
2231 hb_log( " 0x%x type %s (0x%x)%s",
2232 stream
->ts
.list
[i
].pid
,
2233 stream_type_name2(stream
,
2234 &stream
->pes
.list
[stream
->ts
.list
[i
].pes_list
]),
2235 ts_stream_type( stream
, i
),
2236 stream
->ts
.list
[i
].is_pcr
? " (PCR)" : "");
2239 hb_log(" Audio PIDS : ");
2240 for (i
= 0; i
< stream
->ts
.count
; i
++)
2242 if ( ts_stream_kind( stream
, i
) == A
)
2244 hb_log( " 0x%x type %s (0x%x)%s",
2245 stream
->ts
.list
[i
].pid
,
2246 stream_type_name2(stream
,
2247 &stream
->pes
.list
[stream
->ts
.list
[i
].pes_list
]),
2248 ts_stream_type( stream
, i
),
2249 stream
->ts
.list
[i
].is_pcr
? " (PCR)" : "");
2252 hb_log(" Subtitle PIDS : ");
2253 for (i
= 0; i
< stream
->ts
.count
; i
++)
2255 if ( ts_stream_kind( stream
, i
) == S
)
2257 hb_log( " 0x%x type %s (0x%x)%s",
2258 stream
->ts
.list
[i
].pid
,
2259 stream_type_name2(stream
,
2260 &stream
->pes
.list
[stream
->ts
.list
[i
].pes_list
]),
2261 ts_stream_type( stream
, i
),
2262 stream
->ts
.list
[i
].is_pcr
? " (PCR)" : "");
2265 hb_log(" Other PIDS : ");
2266 for (i
= 0; i
< stream
->ts
.count
; i
++)
2268 if ( ts_stream_kind( stream
, i
) == N
||
2269 ts_stream_kind( stream
, i
) == P
)
2271 hb_log( " 0x%x type %s (0x%x)%s",
2272 stream
->ts
.list
[i
].pid
,
2273 stream_type_name2(stream
,
2274 &stream
->pes
.list
[stream
->ts
.list
[i
].pes_list
]),
2275 ts_stream_type( stream
, i
),
2276 stream
->ts
.list
[i
].is_pcr
? " (PCR)" : "");
2278 if ( ts_stream_kind( stream
, i
) == N
)
2279 hb_stream_delete_ts_entry(stream
, i
);
2284 for (i
= 0; i
< stream
->ts
.count
; i
++)
2286 if ( ts_stream_kind( stream
, i
) == N
)
2287 hb_stream_delete_ts_entry(stream
, i
);
2293 static void hb_ps_stream_init(hb_stream_t
*stream
)
2297 if ( stream
->pes
.list
)
2299 for (i
=0; i
< stream
->pes
.alloc
; i
++)
2301 stream
->pes
.list
[i
].stream_id
= -1;
2302 stream
->pes
.list
[i
].next
= -1;
2305 stream
->pes
.count
= 0;
2307 // Find the audio and video pids in the stream
2308 hb_ps_stream_find_streams(stream
);
2309 hb_ps_resolve_stream_types(stream
);
2313 hb_log("Found the following streams");
2314 hb_log(" Video Streams : ");
2315 for (i
=0; i
< stream
->pes
.count
; i
++)
2317 if ( stream
->pes
.list
[i
].stream_kind
== V
)
2319 hb_log( " 0x%x-0x%x type %s (0x%x)",
2320 stream
->pes
.list
[i
].stream_id
,
2321 stream
->pes
.list
[i
].stream_id_ext
,
2322 stream_type_name2(stream
,
2323 &stream
->pes
.list
[i
]),
2324 stream
->pes
.list
[i
].stream_type
);
2327 hb_log(" Audio Streams : ");
2328 for (i
= 0; i
< stream
->pes
.count
; i
++)
2330 if ( stream
->pes
.list
[i
].stream_kind
== A
)
2332 hb_log( " 0x%x-0x%x type %s (0x%x)",
2333 stream
->pes
.list
[i
].stream_id
,
2334 stream
->pes
.list
[i
].stream_id_ext
,
2335 stream_type_name2(stream
,
2336 &stream
->pes
.list
[i
]),
2337 stream
->pes
.list
[i
].stream_type
);
2340 hb_log(" Subtitle Streams : ");
2341 for (i
= 0; i
< stream
->pes
.count
; i
++)
2343 if ( stream
->pes
.list
[i
].stream_kind
== S
)
2345 hb_log( " 0x%x-0x%x type %s (0x%x)",
2346 stream
->pes
.list
[i
].stream_id
,
2347 stream
->pes
.list
[i
].stream_id_ext
,
2348 stream_type_name2(stream
,
2349 &stream
->pes
.list
[i
]),
2350 stream
->pes
.list
[i
].stream_type
);
2353 hb_log(" Other Streams : ");
2354 for (i
= 0; i
< stream
->pes
.count
; i
++)
2356 if ( stream
->pes
.list
[i
].stream_kind
== N
)
2358 hb_log( " 0x%x-0x%x type %s (0x%x)",
2359 stream
->pes
.list
[i
].stream_id
,
2360 stream
->pes
.list
[i
].stream_id_ext
,
2361 stream_type_name2(stream
,
2362 &stream
->pes
.list
[i
]),
2363 stream
->pes
.list
[i
].stream_type
);
2364 hb_stream_delete_ps_entry(stream
, i
);
2370 for (i
= 0; i
< stream
->pes
.count
; i
++)
2372 if ( stream
->pes
.list
[i
].stream_kind
== N
)
2373 hb_stream_delete_ps_entry(stream
, i
);
2378 #define MAX_HOLE 208*80
2380 static off_t
align_to_next_packet(hb_stream_t
*stream
)
2382 uint8_t buf
[MAX_HOLE
];
2384 off_t start
= ftello(stream
->file_handle
);
2387 if ( start
>= stream
->packetsize
) {
2388 start
-= stream
->packetsize
;
2389 fseeko(stream
->file_handle
, start
, SEEK_SET
);
2395 if (fread(buf
, sizeof(buf
), 1, stream
->file_handle
) == 1)
2397 const uint8_t *bp
= buf
;
2400 for ( i
= sizeof(buf
) - 8 * stream
->packetsize
; --i
>= 0; ++bp
)
2402 if ( have_ts_sync( bp
, stream
->packetsize
, 8 ) )
2409 pos
= ( bp
- buf
) - stream
->packetsize
+ 188;
2412 fseeko(stream
->file_handle
, -8 * stream
->packetsize
, SEEK_CUR
);
2413 start
= ftello(stream
->file_handle
);
2418 if ((err
= ferror(stream
->file_handle
)) != 0)
2420 hb_error("align_to_next_packet: error (%d)", err
);
2421 hb_set_work_error(stream
->h
, HB_ERROR_READ
);
2426 fseeko(stream
->file_handle
, start
+pos
, SEEK_SET
);
2427 return start
- orig
+ pos
;
2430 static const unsigned int bitmask
[] = {
2431 0x0,0x1,0x3,0x7,0xf,0x1f,0x3f,0x7f,0xff,
2432 0x1ff,0x3ff,0x7ff,0xfff,0x1fff,0x3fff,0x7fff,0xffff,
2433 0x1ffff,0x3ffff,0x7ffff,0xfffff,0x1fffff,0x3fffff,0x7fffff,0xffffff,
2434 0x1ffffff,0x3ffffff,0x7ffffff,0xfffffff,0x1fffffff,0x3fffffff,0x7fffffff,0xffffffff};
2436 static inline void bits_init(bitbuf_t
*bb
, uint8_t* buf
, int bufsize
, int clear
)
2441 bb
->val
= (bb
->buf
[0] << 24) | (bb
->buf
[1] << 16) |
2442 (bb
->buf
[2] << 8) | bb
->buf
[3];
2444 memset(bb
->buf
, 0, bufsize
);
2448 static inline void bits_clone( bitbuf_t
*dst
, bitbuf_t
*src
, int bufsize
)
2451 dst
->size
= (dst
->pos
>> 3) + bufsize
;
2454 static inline int bits_bytes_left(bitbuf_t
*bb
)
2456 return bb
->size
- (bb
->pos
>> 3);
2459 static inline int bits_eob(bitbuf_t
*bb
)
2461 return bb
->pos
>> 3 == bb
->size
;
2464 static inline unsigned int bits_peek(bitbuf_t
*bb
, int bits
)
2467 int left
= 32 - (bb
->pos
& 31);
2471 val
= (bb
->val
>> (left
- bits
)) & bitmask
[bits
];
2475 val
= (bb
->val
& bitmask
[left
]) << (bits
- left
);
2476 int bpos
= bb
->pos
+ left
;
2482 int pos
= bpos
>> 3;
2483 int bval
= (bb
->buf
[pos
] << 24) |
2484 (bb
->buf
[pos
+ 1] << 16) |
2485 (bb
->buf
[pos
+ 2] << 8) |
2487 val
|= (bval
>> (32 - bits
)) & bitmask
[bits
];
2494 static inline unsigned int bits_get(bitbuf_t
*bb
, int bits
)
2497 int left
= 32 - (bb
->pos
& 31);
2501 val
= (bb
->val
>> (left
- bits
)) & bitmask
[bits
];
2506 val
= (bb
->val
& bitmask
[left
]) << (bits
- left
);
2510 int pos
= bb
->pos
>> 3;
2511 bb
->val
= (bb
->buf
[pos
] << 24) | (bb
->buf
[pos
+ 1] << 16) | (bb
->buf
[pos
+ 2] << 8) | bb
->buf
[pos
+ 3];
2515 val
|= (bb
->val
>> (32 - bits
)) & bitmask
[bits
];
2523 static inline int bits_read_ue(bitbuf_t
*bb
)
2527 while( bits_get( bb
, 1 ) == 0 && !bits_eob( bb
) && ii
< 32 )
2531 return( ( 1 << ii
) - 1 + bits_get( bb
, ii
) );
2534 static inline int bits_skip(bitbuf_t
*bb
, int bits
)
2547 // extract what useful information we can from the elementary stream
2548 // descriptor list at 'dp' and add it to the stream at 'esindx'.
2549 // Descriptors with info we don't currently use are ignored.
2550 // The descriptor list & descriptor item formats are defined in
2551 // ISO 13818-1 (2000E) section 2.6 (pg. 62).
2552 static void decode_element_descriptors(
2553 hb_stream_t
*stream
,
2559 while( bits_bytes_left( bb
) > 2 )
2561 uint8_t tag
= bits_get(bb
, 8);
2562 uint8_t len
= bits_get(bb
, 8);
2565 case 5: // Registration descriptor
2566 stream
->pes
.list
[pes_idx
].format_id
= bits_get(bb
, 32);
2567 bits_skip(bb
, 8 * (len
- 4));
2570 case 10: // ISO_639_language descriptor
2573 for (ii
= 0; ii
< 3; ii
++)
2575 code
[ii
] = bits_get(bb
, 8);
2577 stream
->pes
.list
[pes_idx
].lang_code
=
2578 lang_to_code(lang_for_code2(code
));
2579 bits_skip(bb
, 8 * (len
- 3));
2582 case 0x56: // DVB Teletext descriptor
2584 // We don't currently process teletext from
2585 // TS or PS streams. Set stream 'kind' to N
2586 stream
->pes
.list
[pes_idx
].stream_type
= 0x00;
2587 stream
->pes
.list
[pes_idx
].stream_kind
= N
;
2588 strncpy(stream
->pes
.list
[pes_idx
].codec_name
,
2589 "DVB Teletext", 80);
2590 bits_skip(bb
, 8 * len
);
2593 case 0x59: // DVB Subtitleing descriptor
2595 // We don't currently process subtitles from
2596 // TS or PS streams. Set stream 'kind' to N
2597 stream
->pes
.list
[pes_idx
].stream_type
= 0x00;
2598 stream
->pes
.list
[pes_idx
].stream_kind
= N
;
2599 strncpy(stream
->pes
.list
[pes_idx
].codec_name
,
2600 "DVB Subtitling", 80);
2601 bits_skip(bb
, 8 * len
);
2604 case 0x6a: // DVB AC-3 descriptor
2606 stream
->pes
.list
[pes_idx
].stream_type
= 0x81;
2607 update_pes_kind( stream
, pes_idx
);
2608 bits_skip(bb
, 8 * len
);
2611 case 0x7a: // DVB EAC-3 descriptor
2613 stream
->pes
.list
[pes_idx
].stream_type
= 0x87;
2614 update_pes_kind( stream
, pes_idx
);
2615 bits_skip(bb
, 8 * len
);
2619 bits_skip(bb
, 8 * len
);
2625 int decode_program_map(hb_stream_t
* stream
)
2628 bits_init(&bb
, stream
->pmt_info
.tablebuf
, stream
->pmt_info
.tablepos
, 0);
2630 bits_get(&bb
, 8); // table_id
2632 unsigned int section_length
= bits_get(&bb
, 12);
2634 bits_get(&bb
, 16); // program number
2636 bits_get(&bb
, 5); // version_number
2638 bits_get(&bb
, 8); // section_number
2639 bits_get(&bb
, 8); // last_section_number
2641 stream
->pmt_info
.PCR_PID
= bits_get(&bb
, 13);
2643 int program_info_length
= bits_get(&bb
, 12);
2645 for (i
= 0; i
< program_info_length
- 2; )
2648 tag
= bits_get(&bb
, 8);
2649 len
= bits_get(&bb
, 8);
2651 if ( i
+ len
> program_info_length
)
2655 if (tag
== 0x05 && len
>= 4)
2657 // registration descriptor
2658 stream
->reg_desc
= bits_get(&bb
, 32);
2663 for ( j
= 0; j
< len
; j
++ )
2669 for ( ; i
< program_info_length
; i
++ )
2674 int cur_pos
= 9 /* data after the section length field*/ + program_info_length
;
2675 int done_reading_stream_types
= 0;
2677 while (!done_reading_stream_types
)
2679 unsigned char stream_type
= bits_get(&bb
, 8);
2681 unsigned int elementary_PID
= bits_get(&bb
, 13);
2683 unsigned int info_len
= bits_get(&bb
, 12);
2684 // Defined audio stream types are 0x81 for AC-3/A52 audio
2685 // and 0x03 for mpeg audio. But content producers seem to
2686 // use other values (0x04 and 0x06 have both been observed)
2687 // so at this point we say everything that isn't a video
2688 // pid is audio then at the end of hb_stream_title_scan
2689 // we'll figure out which are really audio by looking at
2692 update_ts_streams( stream
, elementary_PID
, 0,
2693 stream_type
, -1, &pes_idx
);
2695 stream
->pes
.list
[pes_idx
].map_idx
= ii
;
2699 bits_clone( &bb_desc
, &bb
, info_len
);
2701 decode_element_descriptors( stream
, pes_idx
, &bb_desc
);
2702 bits_skip(&bb
, 8 * info_len
);
2705 cur_pos
+= 5 /* stream header */ + info_len
;
2707 if (cur_pos
>= section_length
- 4 /* stop before the CRC */)
2708 done_reading_stream_types
= 1;
2715 static int build_program_map(const uint8_t *buf
, hb_stream_t
*stream
)
2717 // Get adaption header info
2719 int adaption
= (buf
[3] & 0x30) >> 4;
2722 else if (adaption
== 0x2)
2724 else if (adaption
== 0x3)
2725 adapt_len
= buf
[4] + 1;
2726 if (adapt_len
> 184)
2729 // Get payload start indicator
2731 start
= (buf
[1] & 0x40) != 0;
2733 // Get pointer length - only valid in packets with a start flag
2734 int pointer_len
= 0;
2738 pointer_len
= buf
[4 + adapt_len
] + 1;
2739 stream
->pmt_info
.tablepos
= 0;
2741 // Get Continuity Counter
2742 int continuity_counter
= buf
[3] & 0x0f;
2743 if (!start
&& (stream
->pmt_info
.current_continuity_counter
+ 1 != continuity_counter
))
2745 hb_log("build_program_map - Continuity Counter %d out of sequence - expected %d", continuity_counter
, stream
->pmt_info
.current_continuity_counter
+1);
2748 stream
->pmt_info
.current_continuity_counter
= continuity_counter
;
2749 stream
->pmt_info
.reading
|= start
;
2751 // Add the payload for this packet to the current buffer
2752 int amount_to_copy
= 184 - adapt_len
- pointer_len
;
2753 if (stream
->pmt_info
.reading
&& (amount_to_copy
> 0))
2755 stream
->pmt_info
.tablebuf
= realloc(stream
->pmt_info
.tablebuf
, stream
->pmt_info
.tablepos
+ amount_to_copy
);
2757 memcpy(stream
->pmt_info
.tablebuf
+ stream
->pmt_info
.tablepos
, buf
+ 4 + adapt_len
+ pointer_len
, amount_to_copy
);
2758 stream
->pmt_info
.tablepos
+= amount_to_copy
;
2760 if (stream
->pmt_info
.tablepos
> 3)
2762 // We have enough to check the section length
2764 length
= ((stream
->pmt_info
.tablebuf
[1] << 8) +
2765 stream
->pmt_info
.tablebuf
[2]) & 0xFFF;
2766 if (stream
->pmt_info
.tablepos
> length
+ 1)
2768 // We just finished a bunch of packets - parse the program map details
2770 if (stream
->pmt_info
.tablebuf
[0] == 0x02)
2771 decode_ok
= decode_program_map(stream
);
2772 free(stream
->pmt_info
.tablebuf
);
2773 stream
->pmt_info
.tablebuf
= NULL
;
2774 stream
->pmt_info
.tablepos
= 0;
2775 stream
->pmt_info
.reading
= 0;
2785 static int decode_PAT(const uint8_t *buf
, hb_stream_t
*stream
)
2787 unsigned char tablebuf
[1024];
2788 unsigned int tablepos
= 0;
2793 // Get adaption header info
2795 int adaption
= (buf
[3] & 0x30) >> 4;
2798 else if (adaption
== 0x2)
2800 else if (adaption
== 0x3)
2801 adapt_len
= buf
[4] + 1;
2802 if (adapt_len
> 184)
2805 // Get pointer length
2806 int pointer_len
= buf
[4 + adapt_len
] + 1;
2808 // Get payload start indicator
2810 start
= (buf
[1] & 0x40) != 0;
2815 // Add the payload for this packet to the current buffer
2816 if (reading
&& (184 - adapt_len
) > 0)
2818 if (tablepos
+ 184 - adapt_len
- pointer_len
> 1024)
2820 hb_log("decode_PAT - Bad program section length (> 1024)");
2823 memcpy(tablebuf
+ tablepos
, buf
+ 4 + adapt_len
+ pointer_len
, 184 - adapt_len
- pointer_len
);
2824 tablepos
+= 184 - adapt_len
- pointer_len
;
2827 if (start
&& reading
)
2829 memcpy(tablebuf
+ tablepos
, buf
+ 4 + adapt_len
+ 1, pointer_len
- 1);
2832 unsigned int pos
= 0;
2833 //while (pos < tablepos)
2836 bits_init(&bb
, tablebuf
+ pos
, tablepos
- pos
, 0);
2838 unsigned char section_id
= bits_get(&bb
, 8);
2840 unsigned int section_len
= bits_get(&bb
, 12);
2841 bits_get(&bb
, 16); // transport_id
2843 bits_get(&bb
, 5); // version_num
2844 bits_get(&bb
, 1); // current_next
2845 bits_get(&bb
, 8); // section_num
2846 bits_get(&bb
, 8); // last_section
2852 // Program Association Section
2853 section_len
-= 5; // Already read transport stream ID, version num, section num, and last section num
2854 section_len
-= 4; // Ignore the CRC
2856 stream
->ts_number_pat_entries
= 0;
2857 while ((curr_pos
< section_len
) && (stream
->ts_number_pat_entries
< kMaxNumberPMTStreams
))
2859 unsigned int pkt_program_num
= bits_get(&bb
, 16);
2860 stream
->pat_info
[stream
->ts_number_pat_entries
].program_number
= pkt_program_num
;
2862 bits_get(&bb
, 3); // Reserved
2863 if (pkt_program_num
== 0)
2865 bits_get(&bb
, 13); // pkt_network_id
2869 unsigned int pkt_program_map_PID
= bits_get(&bb
, 13);
2870 stream
->pat_info
[stream
->ts_number_pat_entries
].program_map_PID
= pkt_program_map_PID
;
2873 stream
->ts_number_pat_entries
++;
2887 pos
+= 3 + section_len
;
2895 // convert a PES PTS or DTS to an int64
2896 static int64_t parse_pes_timestamp( bitbuf_t
*bb
)
2900 ts
= ( (uint64_t) bits_get(bb
, 3) << 30 ) +
2902 ( bits_get(bb
, 15) << 15 ) +
2909 static int parse_pes_header(
2910 hb_stream_t
*stream
,
2912 hb_pes_info_t
*pes_info
)
2914 if ( bits_bytes_left(bb
) < 6 )
2919 bits_skip(bb
, 8 * 4);
2920 pes_info
->packet_len
= bits_get(bb
, 16);
2923 * This would normally be an error. But the decoders can generally
2924 * recover well from missing data. So let the packet pass.
2925 if ( bits_bytes_left(bb) < pes_info->packet_len )
2931 int mark
= bits_peek(bb
, 2);
2935 if ( bits_bytes_left(bb
) < 3 )
2942 bits_get(bb, 2); // scrambling
2943 bits_get(bb, 1); // priority
2944 bits_get(bb, 1); // alignment
2945 bits_get(bb, 1); // copyright
2946 bits_get(bb, 1); // original
2948 bits_get(bb
, 8); // skip all of the above
2950 int has_pts
= bits_get(bb
, 2);
2951 int has_escr
= bits_get(bb
, 1);
2952 int has_esrate
= bits_get(bb
, 1);
2953 int has_dsm
= bits_get(bb
, 1);
2954 int has_copy_info
= bits_get(bb
, 1);
2955 int has_crc
= bits_get(bb
, 1);
2956 int has_ext
= bits_get(bb
, 1);
2957 int hdr_len
= pes_info
->header_len
= bits_get(bb
, 8);
2958 pes_info
->header_len
+= bb
->pos
>> 3;
2961 bits_clone(&bb_hdr
, bb
, hdr_len
);
2963 if ( bits_bytes_left(&bb_hdr
) < hdr_len
)
2968 int expect
= (!!has_pts
) * 5 + (has_pts
& 0x01) * 5 + has_escr
* 6 +
2969 has_esrate
* 3 + has_dsm
+ has_copy_info
+ has_crc
* 2 +
2972 if ( bits_bytes_left(&bb_hdr
) < expect
)
2979 if ( bits_bytes_left(&bb_hdr
) < 5 )
2983 bits_skip(&bb_hdr
, 4);
2984 pes_info
->pts
= parse_pes_timestamp( &bb_hdr
);
2987 if ( bits_bytes_left(&bb_hdr
) < 5 )
2991 bits_skip(&bb_hdr
, 4);
2992 pes_info
->dts
= parse_pes_timestamp( &bb_hdr
);
2996 pes_info
->dts
= pes_info
->pts
;
2999 // A user encountered a stream that has garbage DTS timestamps.
3000 // DTS should never be > PTS. Such broken timestamps leads to
3001 // HandBrake computing negative buffer start times.
3002 if (pes_info
->dts
> pes_info
->pts
)
3004 pes_info
->dts
= pes_info
->pts
;
3008 bits_skip(&bb_hdr
, 8 * 6);
3010 bits_skip(&bb_hdr
, 8 * 3);
3012 bits_skip(&bb_hdr
, 8);
3013 if ( has_copy_info
)
3014 bits_skip(&bb_hdr
, 8);
3016 bits_skip(&bb_hdr
, 8 * 2);
3020 int has_private
= bits_get(&bb_hdr
, 1);
3021 int has_pack
= bits_get(&bb_hdr
, 1);
3022 int has_counter
= bits_get(&bb_hdr
, 1);
3023 int has_pstd
= bits_get(&bb_hdr
, 1);
3024 bits_skip(&bb_hdr
, 3); // reserved bits
3025 int has_ext2
= bits_get(&bb_hdr
, 1);
3027 expect
= (has_private
) * 16 + has_pack
+ has_counter
* 2 +
3028 has_pstd
* 2 + has_ext2
* 2;
3030 if ( bits_bytes_left(&bb_hdr
) < expect
)
3037 bits_skip(&bb_hdr
, 8 * 16);
3042 int len
= bits_get(&bb_hdr
, 8);
3044 if ( bits_bytes_left(&bb_hdr
) < len
+ expect
)
3048 bits_skip(&bb_hdr
, 8 * len
);
3051 bits_skip(&bb_hdr
, 8 * 2);
3053 bits_skip(&bb_hdr
, 8 * 2);
3057 bits_skip(&bb_hdr
, 1); // marker
3058 bits_get(&bb_hdr
, 7); // extension length
3059 pes_info
->has_stream_id_ext
= !bits_get(&bb_hdr
, 1);
3060 if ( pes_info
->has_stream_id_ext
)
3061 pes_info
->stream_id_ext
= bits_get(&bb_hdr
, 7);
3064 // eat header stuffing
3065 bits_skip(bb
, 8 * hdr_len
);
3072 while ( bits_peek(bb
, 1) && bits_bytes_left(bb
) )
3075 if ( !bits_bytes_left(bb
) )
3078 // Skip std buffer info
3079 int mark
= bits_get(bb
, 2);
3082 if ( bits_bytes_left(bb
) < 2 )
3084 bits_skip(bb
, 8 * 2);
3087 int has_pts
= bits_get(bb
, 2);
3088 if( has_pts
== 0x02 )
3090 pes_info
->pts
= parse_pes_timestamp( bb
);
3091 pes_info
->dts
= pes_info
->pts
;
3093 else if( has_pts
== 0x03 )
3095 pes_info
->pts
= parse_pes_timestamp( bb
);
3097 pes_info
->dts
= parse_pes_timestamp( bb
);
3101 bits_skip(bb
, 8); // 0x0f flag
3103 if ( bits_bytes_left(bb
) < 0 )
3105 pes_info
->header_len
= bb
->pos
>> 3;
3107 if ( pes_info
->stream_id
== 0xbd && stream
->hb_stream_type
== program
)
3109 if ( bits_bytes_left(bb
) < 4 )
3113 int ssid
= bits_peek(bb
, 8);
3114 if( ( ssid
>= 0xa0 && ssid
<= 0xaf ) ||
3115 ( ssid
>= 0x20 && ssid
<= 0x2f ) )
3117 // DVD LPCM or DVD SPU (subtitles)
3118 pes_info
->bd_substream_id
= bits_get(bb
, 8);
3119 pes_info
->header_len
+= 1;
3121 else if ( ssid
>= 0xb0 && ssid
<= 0xbf )
3123 // HD-DVD TrueHD has a 4 byte header
3124 pes_info
->bd_substream_id
= bits_get(bb
, 8);
3125 bits_skip(bb
, 8 * 4);
3126 pes_info
->header_len
+= 5;
3128 else if( ( ssid
>= 0x80 && ssid
<= 0x9f ) ||
3129 ( ssid
>= 0xc0 && ssid
<= 0xcf ) )
3131 // AC3, E-AC3, DTS, and DTS-HD has 3 byte header
3132 pes_info
->bd_substream_id
= bits_get(bb
, 8);
3133 bits_skip(bb
, 8 * 3);
3134 pes_info
->header_len
+= 4;
3140 static int parse_pack_header(
3141 hb_stream_t
*stream
,
3143 hb_pes_info_t
*pes_info
)
3145 if ( bits_bytes_left(bb
) < 12)
3150 bits_skip(bb
, 8 * 4);
3151 int mark
= bits_get(bb
, 2);
3156 bits_skip(bb
, 2); // marker
3158 pes_info
->scr
= parse_pes_timestamp( bb
);
3163 pes_info
->header_len
= (bb
->pos
>> 3);
3168 int stuffing
= bits_get(bb
, 3);
3169 pes_info
->header_len
= stuffing
;
3170 pes_info
->header_len
+= (bb
->pos
>> 3);
3175 // Returns the length of the header
3176 static int hb_parse_ps(
3177 hb_stream_t
*stream
,
3180 hb_pes_info_t
*pes_info
)
3182 memset( pes_info
, 0, sizeof( hb_pes_info_t
) );
3183 pes_info
->pts
= AV_NOPTS_VALUE
;
3184 pes_info
->dts
= AV_NOPTS_VALUE
;
3187 bits_init(&bb
, buf
, len
, 0);
3188 bits_clone(&cc
, &bb
, len
);
3190 if ( bits_bytes_left(&bb
) < 4 )
3193 // Validate start code
3194 if ( bits_get(&bb
, 8 * 3) != 0x000001 )
3199 pes_info
->stream_id
= bits_get(&bb
, 8);
3200 if ( pes_info
->stream_id
== 0xb9 )
3202 // Program stream end code
3205 else if ( pes_info
->stream_id
== 0xba )
3207 return parse_pack_header( stream
, &cc
, pes_info
);
3209 else if ( pes_info
->stream_id
>= 0xbd &&
3210 pes_info
->stream_id
!= 0xbe &&
3211 pes_info
->stream_id
!= 0xbf &&
3212 pes_info
->stream_id
!= 0xf0 &&
3213 pes_info
->stream_id
!= 0xf1 &&
3214 pes_info
->stream_id
!= 0xf2 &&
3215 pes_info
->stream_id
!= 0xf8 &&
3216 pes_info
->stream_id
!= 0xff )
3218 return parse_pes_header( stream
, &cc
, pes_info
);
3222 if ( bits_bytes_left(&bb
) < 2 )
3226 pes_info
->packet_len
= bits_get(&bb
, 16);
3227 pes_info
->header_len
= bb
.pos
>> 3;
3232 static int hb_ps_read_packet( hb_stream_t
* stream
, hb_buffer_t
*b
)
3234 // Appends to buffer if size != 0
3235 int start_code
= -1;
3240 #define cp (b->data)
3241 flockfile( stream
->file_handle
);
3242 while ( ( c
= getc_unlocked( stream
->file_handle
) ) != EOF
)
3244 start_code
= ( start_code
<< 8 ) | c
;
3245 if ( ( start_code
>> 8 )== 0x000001 )
3246 // we found the start of the next start
3252 if ( pos
+ 4 > b
->alloc
)
3254 // need to expand the buffer
3255 hb_buffer_realloc( b
, b
->alloc
* 2 );
3257 cp
[pos
++] = ( start_code
>> 24 ) & 0xff;
3258 cp
[pos
++] = ( start_code
>> 16 ) & 0xff;
3259 cp
[pos
++] = ( start_code
>> 8 ) & 0xff;
3260 cp
[pos
++] = ( start_code
) & 0xff;
3261 stream_id
= start_code
& 0xff;
3263 if ( stream_id
== 0xba )
3265 int start
= pos
- 4;
3267 if ( pos
+ 21 >= b
->alloc
)
3269 // need to expand the buffer
3270 hb_buffer_realloc( b
, b
->alloc
* 2 );
3273 // There are at least 8 bytes. More if this is mpeg2 pack.
3274 fread( cp
+pos
, 1, 8, stream
->file_handle
);
3275 int mark
= cp
[pos
] >> 4;
3281 fread( cp
+pos
, 1, 2, stream
->file_handle
);
3283 int len
= cp
[start
+13] & 0x7;
3284 fread( cp
+pos
, 1, len
, stream
->file_handle
);
3288 // Non-video streams can emulate start codes, so we need
3289 // to inspect PES packets and skip over their data
3290 // sections to avoid mis-detection of the next pack or pes start code
3291 else if ( stream_id
>= 0xbb )
3294 c
= getc_unlocked( stream
->file_handle
);
3298 c
= getc_unlocked( stream
->file_handle
);
3302 if ( pos
+ len
+ 2 > b
->alloc
)
3304 if ( b
->alloc
* 2 > pos
+ len
+ 2 )
3305 hb_buffer_realloc( b
, b
->alloc
* 2 );
3307 hb_buffer_realloc( b
, b
->alloc
* 2 + len
+ 2 );
3309 cp
[pos
++] = len
>> 8;
3310 cp
[pos
++] = len
& 0xff;
3313 // Length is non-zero, read the packet all at once
3314 len
= fread( cp
+pos
, 1, len
, stream
->file_handle
);
3319 // Length is zero, read bytes till we find a start code.
3320 // Only video PES packets are allowed to have zero length.
3322 while ( ( c
= getc_unlocked( stream
->file_handle
) ) != EOF
)
3324 start_code
= ( start_code
<< 8 ) | c
;
3325 if ( pos
>= b
->alloc
)
3327 // need to expand the buffer
3328 hb_buffer_realloc( b
, b
->alloc
* 2 );
3331 if ( ( start_code
>> 8 ) == 0x000001 &&
3332 ( start_code
& 0xff ) >= 0xb9 )
3334 // we found the start of the next start
3341 fseeko( stream
->file_handle
, -4, SEEK_CUR
);
3346 // Unknown, find next start code
3348 while ( ( c
= getc_unlocked( stream
->file_handle
) ) != EOF
)
3350 start_code
= ( start_code
<< 8 ) | c
;
3351 if ( pos
>= b
->alloc
)
3353 // need to expand the buffer
3354 hb_buffer_realloc( b
, b
->alloc
* 2 );
3357 if ( ( start_code
>> 8 ) == 0x000001 &&
3358 ( start_code
& 0xff ) >= 0xb9 )
3359 // we found the start of the next start
3365 fseeko( stream
->file_handle
, -4, SEEK_CUR
);
3369 // Parse packet for information we might need
3370 funlockfile( stream
->file_handle
);
3373 if ((err
= ferror(stream
->file_handle
)) != 0)
3375 hb_error("hb_ps_read_packet: error (%d)", err
);
3376 hb_set_work_error(stream
->h
, HB_ERROR_READ
);
3379 int len
= pos
- b
->size
;
3385 static hb_buffer_t
* hb_ps_stream_decode( hb_stream_t
*stream
)
3387 hb_pes_info_t pes_info
;
3388 hb_buffer_t
*buf
= hb_buffer_init(HB_DVD_READ_BUFFER_SIZE
);
3393 int len
= hb_ps_read_packet( stream
, buf
);
3397 hb_buffer_close( &buf
);
3400 if ( !hb_parse_ps( stream
, buf
->data
, buf
->size
, &pes_info
) )
3406 if ( pes_info
.stream_id
== 0xba )
3408 stream
->pes
.found_scr
= 1;
3409 stream
->ts_flags
|= TS_HAS_PCR
;
3410 stream
->pes
.scr
= pes_info
.scr
;
3414 // If we don't have a SCR yet but the stream has SCRs just loop
3415 // so we don't process anything until we have a clock reference.
3416 if ( !stream
->pes
.found_scr
&& ( stream
->ts_flags
& TS_HAS_PCR
) )
3422 if ( pes_info
.stream_id
== 0xbb )
3426 if ( pes_info
.stream_id
== 0xbd )
3428 idx
= index_of_ps_stream( stream
, pes_info
.stream_id
,
3429 pes_info
.bd_substream_id
);
3433 idx
= index_of_ps_stream( stream
, pes_info
.stream_id
,
3434 pes_info
.stream_id_ext
);
3437 // Is this a stream carrying data that we care about?
3441 switch (stream
->pes
.list
[idx
].stream_kind
)
3444 buf
->s
.type
= AUDIO_BUF
;
3448 buf
->s
.type
= VIDEO_BUF
;
3452 buf
->s
.type
= OTHER_BUF
;
3456 if ( stream
->need_keyframe
)
3458 // we're looking for the first video frame because we're
3459 // doing random access during 'scan'
3460 if ( buf
->s
.type
!= VIDEO_BUF
||
3461 !isIframe( stream
, buf
->data
, buf
->size
) )
3463 // not the video stream or didn't find an I frame
3464 // but we'll only wait 600 video frames for an I frame.
3465 if ( buf
->s
.type
!= VIDEO_BUF
|| ++stream
->need_keyframe
< 600 )
3470 stream
->need_keyframe
= 0;
3472 if ( buf
->s
.type
== VIDEO_BUF
)
3475 buf
->s
.id
= get_id( &stream
->pes
.list
[idx
] );
3476 buf
->s
.pcr
= stream
->pes
.scr
;
3477 buf
->s
.start
= pes_info
.pts
;
3478 buf
->s
.renderOffset
= pes_info
.dts
;
3479 memmove( buf
->data
, buf
->data
+ pes_info
.header_len
,
3480 buf
->size
- pes_info
.header_len
);
3481 buf
->size
-= pes_info
.header_len
;
3482 if ( buf
->size
== 0 )
3484 stream
->pes
.scr
= AV_NOPTS_VALUE
;
3489 static int update_ps_streams( hb_stream_t
* stream
, int stream_id
, int stream_id_ext
, int stream_type
, int in_kind
)
3492 int same_stream
= -1;
3493 kind_t kind
= in_kind
== -1 ? st2codec
[stream_type
].kind
: in_kind
;
3495 for ( ii
= 0; ii
< stream
->pes
.count
; ii
++ )
3497 if ( stream
->pes
.list
[ii
].stream_id
== stream_id
)
3500 if ( stream
->pes
.list
[ii
].stream_id
== stream_id
&&
3501 stream
->pes
.list
[ii
].stream_id_ext
== 0 &&
3502 stream
->pes
.list
[ii
].stream_kind
== U
)
3504 // This is an unknown stream type that hasn't been
3505 // given a stream_id_ext. So match only to stream_id
3507 // is the stream_id_ext being updated?
3508 if ( stream_id_ext
!= 0 )
3511 // If stream is already in the list and the new 'kind' is
3512 // PCR, Unknown, or same as before, just return the index
3513 // to the entry found.
3514 if ( kind
== P
|| kind
== U
|| kind
== stream
->pes
.list
[ii
].stream_kind
)
3516 // Update stream_type and kind
3519 if ( stream_id
== stream
->pes
.list
[ii
].stream_id
&&
3520 stream_id_ext
== stream
->pes
.list
[ii
].stream_id_ext
)
3522 // If stream is already in the list and the new 'kind' is
3523 // PCR and the old 'kind' is unknown, set the new 'kind'
3524 if ( kind
== P
&& stream
->pes
.list
[ii
].stream_kind
== U
)
3527 // If stream is already in the list and the new 'kind' is
3528 // PCR, Unknown, or same as before, just return the index
3529 // to the entry found.
3530 if ( kind
== P
|| kind
== U
|| kind
== stream
->pes
.list
[ii
].stream_kind
)
3532 // Replace unknown 'kind' with known 'kind'
3535 // Resolve multiple videos
3536 if ( kind
== V
&& stream
->pes
.list
[ii
].stream_kind
== V
)
3538 if ( stream_id
<= stream
->pes
.list
[ii
].stream_id
&&
3539 stream_id_ext
<= stream
->pes
.list
[ii
].stream_id_ext
)
3541 // Assume primary video stream has the smallest stream id
3542 // and only use the primary. move the current item
3543 // to the end of the list. we want to keep it for
3544 // debug and informational purposes.
3545 int jj
= new_pes( stream
);
3546 memcpy( &stream
->pes
.list
[jj
], &stream
->pes
.list
[ii
],
3547 sizeof( hb_pes_stream_t
) );
3553 if ( ii
== stream
->pes
.count
)
3555 ii
= new_pes( stream
);
3556 if ( same_stream
>= 0 )
3558 memcpy( &stream
->pes
.list
[ii
], &stream
->pes
.list
[same_stream
],
3559 sizeof( hb_pes_stream_t
) );
3563 stream
->pes
.list
[ii
].map_idx
= -1;
3567 stream
->pes
.list
[ii
].stream_id
= stream_id
;
3568 stream
->pes
.list
[ii
].stream_id_ext
= stream_id_ext
;
3569 stream
->pes
.list
[ii
].stream_type
= stream_type
;
3570 stream
->pes
.list
[ii
].stream_kind
= kind
;
3574 static void update_pes_kind( hb_stream_t
* stream
, int idx
)
3576 kind_t kind
= st2codec
[stream
->pes
.list
[idx
].stream_type
].kind
;
3577 if ( kind
!= U
&& kind
!= N
)
3579 stream
->pes
.list
[idx
].stream_kind
= kind
;
3583 static void ts_pes_list_add( hb_stream_t
*stream
, int ts_idx
, int pes_idx
)
3585 int ii
= stream
->ts
.list
[ts_idx
].pes_list
;
3588 stream
->ts
.list
[ts_idx
].pes_list
= pes_idx
;
3595 if ( ii
== pes_idx
) // Already in list
3598 ii
= stream
->pes
.list
[ii
].next
;
3600 stream
->pes
.list
[idx
].next
= pes_idx
;
3603 static int update_ts_streams( hb_stream_t
* stream
, int pid
, int stream_id_ext
, int stream_type
, int in_kind
, int *out_pes_idx
)
3606 int pes_idx
= update_ps_streams( stream
, pid
, stream_id_ext
,
3607 stream_type
, in_kind
);
3610 *out_pes_idx
= pes_idx
;
3615 kind_t kind
= stream
->pes
.list
[pes_idx
].stream_kind
;
3616 for ( ii
= 0; ii
< stream
->ts
.count
; ii
++ )
3618 if ( pid
== stream
->ts
.list
[ii
].pid
)
3622 // Resolve multiple videos
3623 if ( kind
== V
&& ts_stream_kind( stream
, ii
) == V
&&
3624 pes_idx
< stream
->ts
.list
[ii
].pes_list
)
3626 // We have a new candidate for the primary video. Move
3627 // the current video to the end of the list. And put the
3628 // new video in this slot
3629 int jj
= new_pid( stream
);
3630 memcpy( &stream
->ts
.list
[jj
], &stream
->ts
.list
[ii
],
3631 sizeof( hb_ts_stream_t
) );
3635 if ( ii
== stream
->ts
.count
)
3636 ii
= new_pid( stream
);
3638 stream
->ts
.list
[ii
].pid
= pid
;
3639 ts_pes_list_add( stream
, ii
, pes_idx
);
3641 stream
->ts
.list
[ii
].is_pcr
= 1;
3646 static int decode_ps_map( hb_stream_t
* stream
, uint8_t *buf
, int len
)
3650 bits_init(&bb
, buf
, len
, 0);
3652 if ( bits_bytes_left(&bb
) < 10 )
3655 // Skip stuff not needed
3656 bits_skip(&bb
, 8 * 8);
3657 int info_len
= bits_get(&bb
, 16);
3658 if ( bits_bytes_left(&bb
) < info_len
)
3664 bits_clone( &cc
, &bb
, info_len
);
3666 while ( bits_bytes_left(&cc
) >= 2 )
3670 tag
= bits_get(&cc
, 8);
3671 len
= bits_get(&cc
, 8);
3672 if ( bits_bytes_left(&cc
) < len
)
3675 if (tag
== 0x05 && len
>= 4)
3677 // registration descriptor
3678 stream
->reg_desc
= bits_get(&cc
, 32);
3679 bits_skip(&cc
, 8 * (len
- 4));
3683 bits_skip(&cc
, 8 * len
);
3686 bits_skip(&bb
, 8 * info_len
);
3689 int map_len
= bits_get(&bb
, 16);
3690 if ( bits_bytes_left(&bb
) < map_len
)
3695 while ( bits_bytes_left(&bb
) >= 8 )
3698 int stream_type
= bits_get(&bb
, 8);
3699 int stream_id
= bits_get(&bb
, 8);
3700 info_len
= bits_get(&bb
, 16);
3701 if ( info_len
> bits_bytes_left(&bb
) )
3704 int substream_id
= 0;
3705 switch ( stream_type
)
3711 // If the stream_id isn't one of the standard mpeg
3712 // stream ids, assume it is an private stream 1 substream id.
3713 // This is how most PS streams specify this type of audio.
3715 // TiVo sets the stream id to 0xbd and does not
3716 // give a substream id. This limits them to one audio
3717 // stream and differs from how everyone else specifies
3718 // this type of audio.
3719 if ( stream_id
< 0xb9 )
3721 substream_id
= stream_id
;
3729 pes_idx
= update_ps_streams( stream
, stream_id
, substream_id
,
3732 stream
->pes
.list
[pes_idx
].map_idx
= ii
;
3737 bits_clone( &bb_desc
, &bb
, info_len
);
3739 decode_element_descriptors( stream
, pes_idx
, &bb_desc
);
3740 bits_skip(&bb
, 8 * info_len
);
3748 static void hb_ps_stream_find_streams(hb_stream_t
*stream
)
3751 hb_buffer_t
*buf
= hb_buffer_init(HB_DVD_READ_BUFFER_SIZE
);
3753 fseeko( stream
->file_handle
, 0, SEEK_SET
);
3754 // Scan beginning of file, then if no program stream map is found
3755 // seek to 20% and scan again since there's occasionally no
3756 // audio at the beginning (particularly for vobs).
3757 for ( ii
= 0; ii
< 2; ii
++ )
3759 for ( jj
= 0; jj
< MAX_PS_PROBE_SIZE
; jj
+= buf
->size
)
3764 hb_pes_info_t pes_info
;
3766 len
= hb_ps_read_packet( stream
, buf
);
3769 // Must have reached EOF
3772 if ( !hb_parse_ps( stream
, buf
->data
, buf
->size
, &pes_info
) )
3774 hb_deep_log( 2, "hb_ps_stream_find_streams: Error parsing PS packet");
3777 if ( pes_info
.stream_id
== 0xba )
3779 stream
->ts_flags
|= TS_HAS_PCR
;
3781 else if ( pes_info
.stream_id
== 0xbc )
3783 // program stream map
3784 // Note that if there is a program map, any
3785 // extrapolation that is made below based on
3786 // stream id may be overridden by entry in the map.
3787 if ( decode_ps_map( stream
, buf
->data
, buf
->size
) )
3789 hb_log("Found program stream map");
3790 // Normally, we could quit here since the program
3791 // stream map *should* map all streams. But once
3792 // again Tivo breaks things by not always creating
3793 // complete maps. So continue processing...
3797 hb_error("Error parsing program stream map");
3800 else if ( ( pes_info
.stream_id
& 0xe0 ) == 0xc0 )
3802 // MPeg audio (c0 - df)
3804 update_ps_streams( stream
, pes_info
.stream_id
,
3805 pes_info
.stream_id_ext
, stream_type
, -1 );
3807 else if ( pes_info
.stream_id
== 0xbd )
3809 int ssid
= pes_info
.bd_substream_id
;
3810 // Add a potentail audio stream
3811 // Check dvd substream id
3812 if ( ssid
>= 0x20 && ssid
<= 0x37 )
3814 int idx
= update_ps_streams( stream
, pes_info
.stream_id
,
3815 pes_info
.bd_substream_id
, 0, -1 );
3816 stream
->pes
.list
[idx
].stream_kind
= S
;
3817 stream
->pes
.list
[idx
].codec
= WORK_DECVOBSUB
;
3818 strncpy(stream
->pes
.list
[idx
].codec_name
,
3819 "DVD Subtitle", 80);
3822 if ( ssid
>= 0x80 && ssid
<= 0x87 )
3824 stream_type
= 0x81; // ac3
3826 else if ( ( ssid
>= 0x88 && ssid
<= 0x8f ) ||
3827 ( ssid
>= 0x98 && ssid
<= 0x9f ) )
3829 // Could be either dts or dts-hd
3830 // will have to probe to resolve
3831 int idx
= update_ps_streams( stream
, pes_info
.stream_id
,
3832 pes_info
.bd_substream_id
, 0, U
);
3833 stream
->pes
.list
[idx
].codec
= HB_ACODEC_DCA_HD
;
3834 stream
->pes
.list
[idx
].codec_param
= AV_CODEC_ID_DTS
;
3837 else if ( ssid
>= 0xa0 && ssid
<= 0xaf )
3839 stream_type
= 0x83; // lpcm
3840 // This is flagged as an unknown stream type in
3841 // st2codec because it can be either LPCM or
3842 // BD TrueHD. In this case it is LPCM.
3843 update_ps_streams( stream
, pes_info
.stream_id
,
3844 pes_info
.bd_substream_id
, stream_type
, A
);
3847 else if ( ssid
>= 0xb0 && ssid
<= 0xbf )
3850 int idx
= update_ps_streams( stream
, pes_info
.stream_id
,
3851 pes_info
.bd_substream_id
, 0, A
);
3852 stream
->pes
.list
[idx
].codec
= HB_ACODEC_FFTRUEHD
;
3853 stream
->pes
.list
[idx
].codec_param
= AV_CODEC_ID_TRUEHD
;
3856 else if ( ssid
>= 0xc0 && ssid
<= 0xcf )
3858 // HD-DVD uses this for both ac3 and eac3.
3859 // Check ac3 bitstream_id to distinguish between them.
3861 bits_init(&bb
, buf
->data
+ pes_info
.header_len
,
3862 buf
->size
- pes_info
.header_len
, 0);
3863 int sync
= bits_get(&bb
, 16);
3864 if ( sync
== 0x0b77 )
3867 int bsid
= bits_get(&bb
, 5);
3871 stream_type
= 0x81; // ac3
3876 stream_type
= 0x87; // eac3
3881 // Doesn't look like an ac3 stream. Probe it.
3887 // Unknown. Probe it.
3890 update_ps_streams( stream
, pes_info
.stream_id
,
3891 pes_info
.bd_substream_id
, stream_type
, -1 );
3893 else if ( ( pes_info
.stream_id
& 0xf0 ) == 0xe0 )
3895 // Normally this is MPEG video, but MPEG-1 PS streams
3896 // (which do not have a program stream map) may use
3897 // this for other types of video.
3899 // Also, the hddvd tards decided to use 0xe2 and 0xe3 for
3900 // h.264 video :( and the twits decided not to put a
3901 // program stream map in the stream :'(
3903 // So set this to an unknown stream type and probe.
3905 update_ps_streams( stream
, pes_info
.stream_id
,
3906 pes_info
.stream_id_ext
, stream_type
, -1 );
3908 else if ( pes_info
.stream_id
== 0xfd )
3910 if ( pes_info
.stream_id_ext
== 0x55 ||
3911 pes_info
.stream_id_ext
== 0x56 )
3913 // hddvd uses this for vc-1.
3918 // mark as unknown and probe.
3921 update_ps_streams( stream
, pes_info
.stream_id
,
3922 pes_info
.stream_id_ext
, stream_type
, -1 );
3925 hb_stream_seek( stream
, 0.2 );
3927 hb_buffer_close( &buf
);
3930 static int probe_dts_profile( hb_stream_t
*stream
, hb_pes_stream_t
*pes
)
3932 hb_work_info_t info
;
3933 hb_work_object_t
*w
= hb_codec_decoder( stream
->h
, pes
->codec
);
3935 w
->codec_param
= pes
->codec_param
;
3936 int ret
= w
->bsinfo( w
, pes
->probe_buf
, &info
);
3939 hb_log( "probe_dts_profile: no info type %d/0x%x for id 0x%x",
3940 pes
->codec
, pes
->codec_param
, pes
->stream_id
);
3943 switch (info
.profile
)
3945 case FF_PROFILE_DTS
:
3946 case FF_PROFILE_DTS_ES
:
3947 case FF_PROFILE_DTS_96_24
:
3948 pes
->codec
= HB_ACODEC_DCA
;
3949 pes
->stream_type
= 0x82;
3950 pes
->stream_kind
= A
;
3953 case FF_PROFILE_DTS_HD_HRA
:
3954 case FF_PROFILE_DTS_HD_MA
:
3955 pes
->stream_type
= 0;
3956 pes
->stream_kind
= A
;
3962 const char *profile_name
;
3963 AVCodec
*codec
= avcodec_find_decoder( pes
->codec_param
);
3964 profile_name
= av_get_profile_name( codec
, info
.profile
);
3967 strncpy(pes
->codec_name
, profile_name
, 80);
3968 pes
->codec_name
[79] = 0;
3973 static int do_probe(hb_stream_t
*stream
, hb_pes_stream_t
*pes
, hb_buffer_t
*buf
)
3975 // Check upper limit of per stream data to probe
3976 if ( pes
->probe_buf
== NULL
)
3978 pes
->probe_buf
= hb_buffer_init( 0 );
3980 if ( pes
->probe_buf
->size
> HB_MAX_PROBE_SIZE
)
3982 pes
->stream_kind
= N
;
3983 hb_buffer_close( &pes
->probe_buf
);
3987 // Add this stream buffer to probe buffer and perform probe
3988 AVInputFormat
*fmt
= NULL
;
3990 AVProbeData pd
= {0,};
3991 int size
= pes
->probe_buf
->size
+ buf
->size
;
3993 hb_buffer_realloc(pes
->probe_buf
, size
+ AVPROBE_PADDING_SIZE
);
3994 memcpy( pes
->probe_buf
->data
+ pes
->probe_buf
->size
, buf
->data
, buf
->size
);
3995 pes
->probe_buf
->size
= size
;
3997 if ( pes
->codec
== HB_ACODEC_DCA_HD
)
3999 // We need to probe for the profile of DTS audio in this stream.
4000 return probe_dts_profile( stream
, pes
);
4003 // Probing is slow, so we don't want to re-probe the probe
4004 // buffer for every packet we add to it. Grow the buffer
4005 // by a factor of 2 before probing again.
4006 if ( pes
->probe_buf
->size
< pes
->probe_next_size
)
4009 pes
->probe_next_size
= pes
->probe_buf
->size
* 2;
4010 pd
.buf
= pes
->probe_buf
->data
;
4011 pd
.buf_size
= pes
->probe_buf
->size
;
4012 fmt
= av_probe_input_format2( &pd
, 1, &score
);
4013 if ( fmt
&& score
> AVPROBE_SCORE_MAX
/ 2 )
4015 AVCodec
*codec
= avcodec_find_decoder_by_name( fmt
->name
);
4026 { "g722" , AV_CODEC_ID_ADPCM_G722
},
4027 { "mlp" , AV_CODEC_ID_MLP
},
4028 { "truehd" , AV_CODEC_ID_TRUEHD
},
4029 { "shn" , AV_CODEC_ID_SHORTEN
},
4030 { "aac" , AV_CODEC_ID_AAC
},
4031 { "ac3" , AV_CODEC_ID_AC3
},
4032 { "dts" , AV_CODEC_ID_DTS
},
4033 { "eac3" , AV_CODEC_ID_EAC3
},
4034 { "h264" , AV_CODEC_ID_H264
},
4035 { "m4v" , AV_CODEC_ID_MPEG4
},
4036 { "mp3" , AV_CODEC_ID_MP3
},
4037 { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO
},
4038 { "cavsvideo", AV_CODEC_ID_CAVS
},
4039 { "dnxhd" , AV_CODEC_ID_DNXHD
},
4040 { "h261" , AV_CODEC_ID_H261
},
4041 { "h263" , AV_CODEC_ID_H263
},
4042 { "mjpeg" , AV_CODEC_ID_MJPEG
},
4043 { "vc1" , AV_CODEC_ID_VC1
},
4046 for( i
= 0; fmt_id_type
[i
].name
; i
++ )
4048 if( !strcmp(fmt
->name
, fmt_id_type
[i
].name
) )
4050 codec
= avcodec_find_decoder( fmt_id_type
[i
].id
);
4057 pes
->codec_param
= codec
->id
;
4058 if ( codec
->type
== AVMEDIA_TYPE_VIDEO
)
4060 pes
->stream_kind
= V
;
4061 switch ( codec
->id
)
4063 case AV_CODEC_ID_MPEG1VIDEO
:
4064 pes
->codec
= WORK_DECAVCODECV
;
4065 pes
->stream_type
= 0x01;
4068 case AV_CODEC_ID_MPEG2VIDEO
:
4069 pes
->codec
= WORK_DECAVCODECV
;
4070 pes
->stream_type
= 0x02;
4073 case AV_CODEC_ID_H264
:
4074 pes
->codec
= WORK_DECAVCODECV
;
4075 pes
->stream_type
= 0x1b;
4078 case AV_CODEC_ID_VC1
:
4079 pes
->codec
= WORK_DECAVCODECV
;
4080 pes
->stream_type
= 0xea;
4084 pes
->codec
= WORK_DECAVCODECV
;
4087 else if ( codec
->type
== AVMEDIA_TYPE_AUDIO
)
4089 pes
->stream_kind
= A
;
4090 switch ( codec
->id
)
4092 case AV_CODEC_ID_AC3
:
4093 pes
->codec
= HB_ACODEC_AC3
;
4096 pes
->codec
= HB_ACODEC_FFMPEG
;
4101 pes
->stream_kind
= N
;
4103 strncpy(pes
->codec_name
, codec
->name
, 79);
4104 pes
->codec_name
[79] = 0;
4108 pes
->stream_kind
= N
;
4110 hb_buffer_close( &pes
->probe_buf
);
4116 static void hb_ts_resolve_pid_types(hb_stream_t
*stream
)
4120 for ( ii
= 0; ii
< stream
->ts
.count
; ii
++ )
4122 int pid
= stream
->ts
.list
[ii
].pid
;
4123 int stype
= ts_stream_type( stream
, ii
);
4126 if ( stype
== 0x80 &&
4127 stream
->reg_desc
== STR4_TO_UINT32("HDMV") )
4129 // LPCM audio in bluray have an stype of 0x80
4130 // 0x80 is used for other DigiCipher normally
4131 // To distinguish, Bluray streams have a reg_desc of HDMV
4132 update_ts_streams( stream
, pid
, 0, stype
, A
, &pes_idx
);
4133 stream
->pes
.list
[pes_idx
].codec
= HB_ACODEC_FFMPEG
;
4134 stream
->pes
.list
[pes_idx
].codec_param
= AV_CODEC_ID_PCM_BLURAY
;
4138 // The blu ray consortium apparently forgot to read the portion
4139 // of the MPEG spec that says one PID should map to one media
4140 // stream and multiplexed multiple types of audio into one PID
4141 // using the extended stream identifier of the PES header to
4142 // distinguish them. So we have to check if that's happening and
4143 // if so tell the runtime what esid we want.
4144 if ( stype
== 0x83 &&
4145 stream
->reg_desc
== STR4_TO_UINT32("HDMV") )
4147 // This is an interleaved TrueHD/AC-3 stream and the esid of
4149 update_ts_streams( stream
, pid
, HB_SUBSTREAM_BD_AC3
,
4150 stype
, A
, &pes_idx
);
4151 stream
->pes
.list
[pes_idx
].codec
= HB_ACODEC_AC3
;
4152 stream
->pes
.list
[pes_idx
].codec_param
= AV_CODEC_ID_AC3
;
4154 update_ts_streams( stream
, pid
, HB_SUBSTREAM_BD_TRUEHD
,
4155 stype
, A
, &pes_idx
);
4156 stream
->pes
.list
[pes_idx
].codec
= HB_ACODEC_FFTRUEHD
;
4157 stream
->pes
.list
[pes_idx
].codec_param
= AV_CODEC_ID_TRUEHD
;
4160 if ( ( stype
== 0x84 || stype
== 0xa1 ) &&
4161 stream
->reg_desc
== STR4_TO_UINT32("HDMV") )
4163 // EAC3 audio in bluray has an stype of 0x84
4164 // which conflicts with SDDS
4165 // To distinguish, Bluray streams have a reg_desc of HDMV
4166 update_ts_streams( stream
, pid
, 0, stype
, A
, &pes_idx
);
4167 stream
->pes
.list
[pes_idx
].codec
= HB_ACODEC_FFEAC3
;
4168 stream
->pes
.list
[pes_idx
].codec_param
= AV_CODEC_ID_EAC3
;
4171 // 0xa2 is DTS-HD LBR used in HD-DVD and bluray for
4172 // secondary audio streams. Libav can not decode yet.
4173 // Having it in the audio list causes delays during scan
4174 // while we try to get stream parameters. So skip
4175 // this type for now.
4176 if ( stype
== 0x85 &&
4177 stream
->reg_desc
== STR4_TO_UINT32("HDMV") )
4179 // DTS-HD HRA audio in bluray has an stype of 0x85
4180 // which conflicts with ATSC Program ID
4181 // To distinguish, Bluray streams have a reg_desc of HDMV
4182 // This is an interleaved DTS-HD HRA/DTS stream and the
4183 // esid of the DTS is 0x71
4184 update_ts_streams( stream
, pid
, HB_SUBSTREAM_BD_DTS
,
4185 stype
, A
, &pes_idx
);
4186 stream
->pes
.list
[pes_idx
].codec
= HB_ACODEC_DCA
;
4187 stream
->pes
.list
[pes_idx
].codec_param
= AV_CODEC_ID_DTS
;
4189 update_ts_streams( stream
, pid
, 0, stype
, A
, &pes_idx
);
4190 stream
->pes
.list
[pes_idx
].codec
= HB_ACODEC_DCA_HD
;
4191 stream
->pes
.list
[pes_idx
].codec_param
= AV_CODEC_ID_DTS
;
4194 if ( stype
== 0x86 &&
4195 stream
->reg_desc
== STR4_TO_UINT32("HDMV") )
4197 // This is an interleaved DTS-HD MA/DTS stream and the
4198 // esid of the DTS is 0x71
4199 update_ts_streams( stream
, pid
, HB_SUBSTREAM_BD_DTS
,
4200 stype
, A
, &pes_idx
);
4201 stream
->pes
.list
[pes_idx
].codec
= HB_ACODEC_DCA
;
4202 stream
->pes
.list
[pes_idx
].codec_param
= AV_CODEC_ID_DTS
;
4204 update_ts_streams( stream
, pid
, 0, stype
, A
, &pes_idx
);
4205 stream
->pes
.list
[pes_idx
].codec
= HB_ACODEC_DCA_HD
;
4206 stream
->pes
.list
[pes_idx
].codec_param
= AV_CODEC_ID_DTS
;
4210 // stype == 0 indicates a type not in st2codec table
4212 ( ts_stream_kind( stream
, ii
) == A
||
4213 ts_stream_kind( stream
, ii
) == S
||
4214 ts_stream_kind( stream
, ii
) == V
) )
4216 // Assuming there are no substreams.
4217 // This should be true before probing.
4218 // This function is only called before
4220 pes_idx
= stream
->ts
.list
[ii
].pes_list
;
4221 stream
->pes
.list
[pes_idx
].codec
= st2codec
[stype
].codec
;
4222 stream
->pes
.list
[pes_idx
].codec_param
= st2codec
[stype
].codec_param
;
4226 if ( ts_stream_kind( stream
, ii
) == U
)
4232 // Probe remaining unknown streams for stream types
4233 hb_stream_seek( stream
, 0.0 );
4234 stream
->need_keyframe
= 0;
4240 hb_log("Probing %d unknown stream%s", probe
, probe
> 1 ? "s" : "" );
4242 while ( probe
&& ( buf
= hb_ts_stream_decode( stream
) ) != NULL
)
4244 // Check upper limit of total data to probe
4245 total_size
+= buf
->size
;
4247 if ( total_size
> HB_MAX_PROBE_SIZE
* 2 )
4249 hb_buffer_close(&buf
);
4254 idx
= index_of_id( stream
, buf
->s
.id
);
4256 if (idx
< 0 || stream
->pes
.list
[idx
].stream_kind
!= U
)
4258 hb_buffer_close(&buf
);
4262 hb_pes_stream_t
*pes
= &stream
->pes
.list
[idx
];
4264 if ( do_probe( stream
, pes
, buf
) )
4267 if ( pes
->stream_kind
!= N
)
4269 hb_log(" Probe: Found stream %s. stream id 0x%x-0x%x",
4270 pes
->codec_name
, pes
->stream_id
, pes
->stream_id_ext
);
4274 hb_log(" Probe: Unsupported stream %s. stream id 0x%x-0x%x",
4275 pes
->codec_name
, pes
->stream_id
, pes
->stream_id_ext
);
4278 hb_buffer_close(&buf
);
4280 // Clean up any probe buffers and set all remaining unknown
4281 // streams to 'kind' N
4282 for ( ii
= 0; ii
< stream
->pes
.count
; ii
++ )
4284 if ( stream
->pes
.list
[ii
].stream_kind
== U
)
4285 stream
->pes
.list
[ii
].stream_kind
= N
;
4286 hb_buffer_close( &stream
->pes
.list
[ii
].probe_buf
);
4287 stream
->pes
.list
[ii
].probe_next_size
= 0;
4291 static void hb_ps_resolve_stream_types(hb_stream_t
*stream
)
4295 for ( ii
= 0; ii
< stream
->pes
.count
; ii
++ )
4297 int stype
= stream
->pes
.list
[ii
].stream_type
;
4299 // stype == 0 indicates a type not in st2codec table
4301 ( stream
->pes
.list
[ii
].stream_kind
== A
||
4302 stream
->pes
.list
[ii
].stream_kind
== S
||
4303 stream
->pes
.list
[ii
].stream_kind
== V
) )
4305 stream
->pes
.list
[ii
].codec
= st2codec
[stype
].codec
;
4306 stream
->pes
.list
[ii
].codec_param
= st2codec
[stype
].codec_param
;
4310 if ( stream
->pes
.list
[ii
].stream_kind
== U
)
4316 // Probe remaining unknown streams for stream types
4317 hb_stream_seek( stream
, 0.0 );
4318 stream
->need_keyframe
= 0;
4324 hb_log("Probing %d unknown stream%s", probe
, probe
> 1 ? "s" : "" );
4326 while ( probe
&& ( buf
= hb_ps_stream_decode( stream
) ) != NULL
)
4328 // Check upper limit of total data to probe
4329 total_size
+= buf
->size
;
4331 if ( total_size
> HB_MAX_PROBE_SIZE
* 2 )
4335 idx
= index_of_id( stream
, buf
->s
.id
);
4337 if (idx
< 0 || stream
->pes
.list
[idx
].stream_kind
!= U
)
4340 hb_pes_stream_t
*pes
= &stream
->pes
.list
[idx
];
4342 if ( do_probe( stream
, pes
, buf
) )
4345 if ( pes
->stream_kind
!= N
)
4347 hb_log(" Probe: Found stream %s. stream id 0x%x-0x%x",
4348 pes
->codec_name
, pes
->stream_id
, pes
->stream_id_ext
);
4352 hb_log(" Probe: Unsupported stream %s. stream id 0x%x-0x%x",
4353 pes
->codec_name
, pes
->stream_id
, pes
->stream_id_ext
);
4357 // Clean up any probe buffers and set all remaining unknown
4358 // streams to 'kind' N
4359 for ( ii
= 0; ii
< stream
->pes
.count
; ii
++ )
4361 if ( stream
->pes
.list
[ii
].stream_kind
== U
)
4362 stream
->pes
.list
[ii
].stream_kind
= N
;
4363 hb_buffer_close( &stream
->pes
.list
[ii
].probe_buf
);
4364 stream
->pes
.list
[ii
].probe_next_size
= 0;
4369 static int hb_ts_stream_find_pids(hb_stream_t
*stream
)
4371 // To be different from every other broadcaster in the world, New Zealand TV
4372 // changes PMTs (and thus video & audio PIDs) when 'programs' change. Since
4373 // we may have the tail of the previous program at the beginning of this
4374 // file, take our PMT from the middle of the file.
4375 fseeko(stream
->file_handle
, 0, SEEK_END
);
4376 uint64_t fsize
= ftello(stream
->file_handle
);
4377 fseeko(stream
->file_handle
, fsize
>> 1, SEEK_SET
);
4378 align_to_next_packet(stream
);
4380 // Read the Transport Stream Packets (188 bytes each) looking at first for PID 0 (the PAT PID), then decode that
4381 // to find the program map PID and then decode that to get the list of audio and video PIDs
4385 const uint8_t *buf
= next_packet( stream
);
4389 hb_log("hb_ts_stream_find_pids - end of file");
4394 int pid
= (((buf
[1] & 0x1F) << 8) | buf
[2]) & 0x1FFF;
4396 if ((pid
== 0x0000) && (stream
->ts_number_pat_entries
== 0))
4398 decode_PAT(buf
, stream
);
4403 for (pat_index
= 0; pat_index
< stream
->ts_number_pat_entries
; pat_index
++)
4405 // There are some streams where the PAT table has multiple
4406 // entries as if their are multiple programs in the same
4407 // transport stream, and yet there's actually only one
4408 // program really in the stream. This seems to be true for
4409 // transport streams that originate in the HDHomeRun but have
4410 // been output by EyeTV's export utility. What I think is
4411 // happening is that the HDHomeRun is sending the entire
4412 // transport stream as broadcast, but the EyeTV is only
4413 // recording a single (selected) program number and not
4414 // rewriting the PAT info on export to match what's actually
4415 // on the stream. Until we have a way of handling multiple
4416 // programs per transport stream elegantly we'll match on the
4417 // first pat entry for which we find a matching program map PID.
4418 // The ideal solution would be to build a title choice popup
4419 // from the PAT program number details and then select from
4420 // their - but right now the API's not capable of that.
4421 if (stream
->pat_info
[pat_index
].program_number
!= 0 &&
4422 pid
== stream
->pat_info
[pat_index
].program_map_PID
)
4424 if (build_program_map(buf
, stream
) > 0)
4430 // Keep going until we have a complete set of PIDs
4431 if ( ts_index_of_video( stream
) >= 0 )
4434 if ( ts_index_of_video( stream
) < 0 )
4436 update_ts_streams( stream
, stream
->pmt_info
.PCR_PID
, 0, -1, P
, NULL
);
4441 // convert a PES PTS or DTS to an int64
4442 static int64_t pes_timestamp( const uint8_t *buf
)
4446 ts
= ( (uint64_t) ( buf
[0] & 0x0e ) << 29 ) +
4448 ( ( buf
[2] >> 1 ) << 15 ) +
4454 static int stream_kind_to_buf_type(int kind
)
4463 return SUBTITLE_BUF
;
4469 static hb_buffer_t
* generate_output_data(hb_stream_t
*stream
, int curstream
)
4471 hb_buffer_t
*buf
= NULL
, *first
= NULL
;
4473 hb_ts_stream_t
* ts_stream
= &stream
->ts
.list
[curstream
];
4474 hb_buffer_t
* b
= ts_stream
->buf
;
4475 if (!ts_stream
->pes_info_valid
)
4477 if (!hb_parse_ps(stream
, b
->data
, b
->size
, &ts_stream
->pes_info
))
4480 ts_stream
->packet_len
= 0;
4481 ts_stream
->packet_offset
= 0;
4484 ts_stream
->pes_info_valid
= 1;
4485 ts_stream
->packet_offset
= ts_stream
->pes_info
.header_len
;
4488 uint8_t *tdat
= b
->data
+ ts_stream
->packet_offset
;
4489 int es_size
= b
->size
- ts_stream
->packet_offset
;
4497 pes_idx
= ts_stream
->pes_list
;
4498 hb_pes_stream_t
*pes_stream
= &stream
->pes
.list
[pes_idx
];
4499 if (stream
->need_keyframe
)
4501 // we're looking for the first video frame because we're
4502 // doing random access during 'scan'
4503 int kind
= pes_stream
->stream_kind
;
4504 if (kind
!= V
|| !isIframe(stream
, tdat
, es_size
))
4506 // not the video stream or didn't find an I frame
4507 // but we'll only wait 255 video frames for an I frame.
4508 if (kind
!= V
|| ++stream
->need_keyframe
< 512)
4511 ts_stream
->pes_info_valid
= 0;
4512 ts_stream
->packet_len
= 0;
4513 ts_stream
->packet_offset
= 0;
4517 stream
->need_keyframe
= 0;
4520 // Some TS streams carry multiple substreams. E.g. DTS-HD contains
4521 // a core DTS substream. We demux these as separate streams here.
4522 // Check all substreams to see if this packet matches
4523 for (pes_idx
= ts_stream
->pes_list
; pes_idx
!= -1;
4524 pes_idx
= stream
->pes
.list
[pes_idx
].next
)
4526 hb_pes_stream_t
*pes_stream
= &stream
->pes
.list
[pes_idx
];
4527 if (pes_stream
->stream_id_ext
!= ts_stream
->pes_info
.stream_id_ext
&&
4528 pes_stream
->stream_id_ext
!= 0)
4532 // The substreams match.
4533 // Note that when stream->pes.list[pes_idx].stream_id_ext == 0,
4534 // we want the whole TS stream including all substreams.
4535 // DTS-HD is an example of this.
4539 first
= buf
= hb_buffer_init(es_size
);
4543 hb_buffer_t
*tmp
= hb_buffer_init(es_size
);
4548 buf
->s
.id
= get_id(pes_stream
);
4549 buf
->s
.type
= stream_kind_to_buf_type(pes_stream
->stream_kind
);
4550 buf
->s
.discontinuity
= stream
->ts
.discontinuity
;
4551 stream
->ts
.discontinuity
= 0;
4552 buf
->s
.new_chap
= b
->s
.new_chap
;
4555 // put the PTS & possible DTS into 'start' & 'renderOffset'
4556 // only put timestamps on the first output buffer for this PES packet.
4557 if (ts_stream
->packet_offset
> 0)
4559 buf
->s
.pcr
= stream
->ts
.pcr
;
4560 stream
->ts
.pcr
= AV_NOPTS_VALUE
;
4561 buf
->s
.start
= ts_stream
->pes_info
.pts
;
4562 buf
->s
.renderOffset
= ts_stream
->pes_info
.dts
;
4566 buf
->s
.pcr
= AV_NOPTS_VALUE
;
4567 buf
->s
.start
= AV_NOPTS_VALUE
;
4568 buf
->s
.renderOffset
= AV_NOPTS_VALUE
;
4570 // copy the elementary stream data into the buffer
4571 memcpy(buf
->data
, tdat
, es_size
);
4574 if (ts_stream
->pes_info
.packet_len
> 0 &&
4575 ts_stream
->packet_len
>= ts_stream
->pes_info
.packet_len
+ 6)
4577 ts_stream
->pes_info_valid
= 0;
4578 ts_stream
->packet_len
= 0;
4581 ts_stream
->packet_offset
= 0;
4585 static void hb_ts_stream_append_pkt(hb_stream_t
*stream
, int idx
,
4586 const uint8_t *buf
, int len
)
4588 if (stream
->ts
.list
[idx
].skipbad
|| len
<= 0)
4591 if (stream
->ts
.list
[idx
].buf
->size
+ len
> stream
->ts
.list
[idx
].buf
->alloc
)
4595 size
= MAX(stream
->ts
.list
[idx
].buf
->alloc
* 2,
4596 stream
->ts
.list
[idx
].buf
->size
+ len
);
4597 hb_buffer_realloc(stream
->ts
.list
[idx
].buf
, size
);
4599 memcpy(stream
->ts
.list
[idx
].buf
->data
+ stream
->ts
.list
[idx
].buf
->size
,
4601 stream
->ts
.list
[idx
].buf
->size
+= len
;
4602 stream
->ts
.list
[idx
].packet_len
+= len
;
4605 static hb_buffer_t
* flush_ts_streams( hb_stream_t
*stream
)
4607 hb_buffer_t
*out
, **last
;
4611 for (ii
= 0; ii
< stream
->ts
.count
; ii
++)
4613 *last
= generate_output_data(stream
, ii
);
4614 // generate_output_data can generate 0 or multiple output buffers
4615 while (*last
!= NULL
)
4616 last
= &(*last
)->next
;
4621 /***********************************************************************
4622 * hb_ts_stream_decode
4623 ***********************************************************************
4625 **********************************************************************/
4626 hb_buffer_t
* hb_ts_decode_pkt( hb_stream_t
*stream
, const uint8_t * pkt
,
4627 int chapter
, int discontinuity
)
4630 * stash the output buffer pointer in our stream so we don't have to
4631 * pass it & its original value to everything we call.
4633 int video_index
= ts_index_of_video(stream
);
4635 hb_buffer_t
*out
= NULL
;
4642 stream
->chapter
= chapter
;
4646 stream
->ts
.discontinuity
= 1;
4649 /* This next section validates the packet */
4651 // Get pid and use it to find stream state.
4652 int pid
= ((pkt
[1] & 0x1F) << 8) | pkt
[2];
4653 if ( ( curstream
= index_of_pid( stream
, pid
) ) < 0 )
4659 int errorbit
= (pkt
[1] & 0x80) != 0;
4662 ts_err( stream
, curstream
, "packet error bit set");
4666 // Get adaption header info
4667 int adaption
= (pkt
[3] & 0x30) >> 4;
4671 ts_err( stream
, curstream
, "adaptation code 0");
4674 else if (adaption
== 0x2)
4676 else if (adaption
== 0x3)
4678 adapt_len
= pkt
[4] + 1;
4679 if (adapt_len
> 184)
4681 ts_err( stream
, curstream
, "invalid adapt len %d", adapt_len
);
4688 // If there is a discontinuity, flush all data
4689 *last
= flush_ts_streams(stream
);
4690 // flush_ts_streams can generate 0 or multiple output buffers
4691 while (*last
!= NULL
)
4692 last
= &(*last
)->next
;
4698 // found a random access point
4700 // if there's an adaptation header & PCR_flag is set
4701 // get the PCR (Program Clock Reference)
4703 // JAS: I have a badly mastered BD that does adaptation field
4704 // stuffing incorrectly which results in invalid PCRs. Test
4705 // for all 0xff to guard against this.
4706 if (adapt_len
> 7 && (pkt
[5] & 0x10) != 0 &&
4707 !(pkt
[5] == 0xff && pkt
[6] == 0xff && pkt
[7] == 0xff &&
4708 pkt
[8] == 0xff && pkt
[9] == 0xff && pkt
[10] == 0xff))
4710 // When we get a new pcr, we flush all data that was
4711 // referenced to the last pcr. This makes it easier
4712 // for reader to resolve pcr discontinuities.
4713 *last
= flush_ts_streams(stream
);
4714 // flush_ts_streams can generate 0 or multiple output buffers
4715 while (*last
!= NULL
)
4716 last
= &(*last
)->next
;
4719 pcr
= ((uint64_t)pkt
[ 6] << (33 - 8) ) |
4720 ((uint64_t)pkt
[ 7] << (33 - 16) ) |
4721 ((uint64_t)pkt
[ 8] << (33 - 24) ) |
4722 ((uint64_t)pkt
[ 9] << (33 - 32) ) |
4724 stream
->ts
.found_pcr
= 1;
4725 stream
->ts_flags
|= TS_HAS_PCR
;
4726 stream
->ts
.pcr
= pcr
;
4730 // If we don't have a PCR yet but the stream has PCRs just loop
4731 // so we don't process anything until we have a clock reference.
4732 // Unfortunately the HD Home Run appears to null out the PCR so if
4733 // we didn't detect a PCR during scan keep going and we'll use
4734 // the video stream DTS for the PCR.
4735 if (!stream
->ts
.found_pcr
&& (stream
->ts_flags
& TS_HAS_PCR
))
4741 // Continuity only increments for adaption values of 0x3 or 0x01
4742 // and is not checked for start packets.
4743 hb_ts_stream_t
* ts_stream
= &stream
->ts
.list
[curstream
];
4744 int start
= (pkt
[1] & 0x40) != 0;
4746 if ( (adaption
& 0x01) != 0 )
4748 int continuity
= (pkt
[3] & 0xF);
4749 if ( continuity
== ts_stream
->continuity
)
4751 // Spliced transport streams can have duplicate
4752 // continuity counts at the splice boundary.
4753 // Test to see if the packet is really a duplicate
4754 // by comparing packet summaries to see if they
4758 summary
[0] = adaption
;
4759 summary
[1] = adapt_len
;
4760 if (adapt_len
+ 4 + 6 + 9 <= 188)
4762 memcpy(&summary
[2], pkt
+4+adapt_len
+9, 6);
4766 memset(&summary
[2], 0, 6);
4768 if ( memcmp( summary
, ts_stream
->pkt_summary
, 8 ) == 0 )
4770 // we got a duplicate packet (usually used to introduce
4771 // a PCR when one is needed). The only thing that can
4772 // change in the dup is the PCR which we grabbed above
4773 // so ignore the rest.
4777 if ( !start
&& (ts_stream
->continuity
!= -1) &&
4778 !ts_stream
->skipbad
&&
4779 (continuity
!= ( (ts_stream
->continuity
+ 1) & 0xf ) ) )
4781 ts_err( stream
, curstream
, "continuity error: got %d expected %d",
4783 (ts_stream
->continuity
+ 1) & 0xf );
4784 ts_stream
->continuity
= continuity
;
4787 ts_stream
->continuity
= continuity
;
4789 // Save a summary of this packet for later duplicate
4790 // testing. The summary includes some header information
4791 // and payload bytes. Should be enough to detect
4793 ts_stream
->pkt_summary
[0] = adaption
;
4794 ts_stream
->pkt_summary
[1] = adapt_len
;
4795 if (adapt_len
+ 4 + 6 + 9 <= 188)
4797 memcpy(&ts_stream
->pkt_summary
[2],
4798 pkt
+4+adapt_len
+9, 6);
4802 memset(&ts_stream
->pkt_summary
[2], 0, 6);
4806 if (ts_stream_kind( stream
, curstream
) == P
)
4808 // This is a stream that only contains PCRs. No need to process
4809 // the remainder of the packet.
4811 // I ran across a poorly mastered BD that does not properly pad
4812 // the adaptation field and causes parsing errors below if we
4813 // do not exit early here.
4817 /* If we get here the packet is valid - process its data */
4820 // Found the start of a new PES packet.
4821 // If we have previous packet data on this stream,
4822 // output the elementary stream data for that packet.
4823 if (ts_stream
->buf
->size
> 0)
4825 // we have to ship the old packet before updating the pcr
4826 // since the packet we've been accumulating is referenced
4828 *last
= generate_output_data(stream
, curstream
);
4829 // generate_output_data can generate 0 or multiple output buffers
4830 while (*last
!= NULL
)
4831 last
= &(*last
)->next
;
4832 ts_stream
->pes_info_valid
= 0;
4833 ts_stream
->packet_len
= 0;
4836 // PES must begin with an mpeg start code
4837 const uint8_t *pes
= pkt
+ adapt_len
+ 4;
4838 if (pes
[0] != 0x00 || pes
[1] != 0x00 || pes
[2] != 0x01)
4840 ts_err( stream
, curstream
, "missing start code" );
4841 ts_stream
->skipbad
= 1;
4845 // If we were skipping a bad packet, start fresh on this new PES packet
4846 ts_stream
->skipbad
= 0;
4848 if (curstream
== video_index
)
4852 // if we don't have a pcr yet use the dts from this frame
4853 // to attempt to detect discontinuities
4854 if (!stream
->ts
.found_pcr
)
4856 // PES must begin with an mpeg start code & contain
4858 if (stream
->ts
.last_timestamp
< 0 && (pes
[7] >> 6) == 0)
4862 if ((pes
[7] >> 6) != 0)
4864 // if we have a dts use it otherwise use the pts
4865 // We simulate a psuedo-PCR here by sampling a timestamp
4866 // about every 600ms.
4868 timestamp
= pes_timestamp(pes
+ (pes
[7] & 0x40 ? 14 : 9));
4869 if (stream
->ts
.last_timestamp
< 0 ||
4870 timestamp
- stream
->ts
.last_timestamp
> 90 * 600 ||
4871 stream
->ts
.last_timestamp
- timestamp
> 90 * 600)
4873 stream
->ts
.pcr
= timestamp
;
4875 stream
->ts
.last_timestamp
= timestamp
;
4881 // Add the payload for this packet to the current buffer
4882 hb_ts_stream_append_pkt(stream
, curstream
, pkt
+ 4 + adapt_len
,
4884 if (stream
->chapter
> 0 &&
4885 stream
->pes
.list
[ts_stream
->pes_list
].stream_kind
== V
)
4887 ts_stream
->buf
->s
.new_chap
= stream
->chapter
;
4888 stream
->chapter
= 0;
4891 if (!ts_stream
->pes_info_valid
&& ts_stream
->buf
->size
>= 19)
4893 if (hb_parse_ps(stream
, ts_stream
->buf
->data
, ts_stream
->buf
->size
,
4894 &ts_stream
->pes_info
))
4896 ts_stream
->pes_info_valid
= 1;
4897 ts_stream
->packet_offset
= ts_stream
->pes_info
.header_len
;
4901 // see if we've hit the end of this PES packet
4902 if (ts_stream
->pes_info_valid
&&
4903 ts_stream
->pes_info
.packet_len
> 0 &&
4904 ts_stream
->packet_len
>= ts_stream
->pes_info
.packet_len
+ 6)
4906 // generate_output_data can generate 0 or multiple output buffers
4907 *last
= generate_output_data(stream
, curstream
);
4908 while (*last
!= NULL
)
4909 last
= &(*last
)->next
;
4914 static hb_buffer_t
* hb_ts_stream_decode( hb_stream_t
*stream
)
4918 // spin until we get a packet of data from some stream or hit eof
4921 const uint8_t *buf
= next_packet(stream
);
4924 // end of file - we didn't finish filling our ps write buffer
4925 // so just discard the remainder (the partial buffer is useless)
4926 hb_log("hb_ts_stream_decode - eof");
4930 b
= hb_ts_decode_pkt( stream
, buf
, 0, 0 );
4939 void hb_stream_set_need_keyframe(hb_stream_t
*stream
, int need_keyframe
)
4941 if ( stream
->hb_stream_type
== transport
||
4942 stream
->hb_stream_type
== program
)
4944 // Only wait for a keyframe if the stream is known to have IDRs
4945 stream
->need_keyframe
= !!need_keyframe
& !!stream
->has_IDRs
;
4949 stream
->need_keyframe
= need_keyframe
;
4953 void hb_ts_stream_reset(hb_stream_t
*stream
)
4957 for (i
=0; i
< stream
->ts
.count
; i
++)
4959 if ( stream
->ts
.list
[i
].buf
)
4960 stream
->ts
.list
[i
].buf
->size
= 0;
4961 stream
->ts
.list
[i
].skipbad
= 1;
4962 stream
->ts
.list
[i
].continuity
= -1;
4963 stream
->ts
.list
[i
].pes_info_valid
= 0;
4966 stream
->need_keyframe
= 1;
4968 stream
->ts
.found_pcr
= 0;
4969 stream
->ts
.pcr
= AV_NOPTS_VALUE
;
4970 stream
->ts
.last_timestamp
= AV_NOPTS_VALUE
;
4974 stream
->last_error_frame
= -10000;
4975 stream
->last_error_count
= 0;
4978 void hb_ps_stream_reset(hb_stream_t
*stream
)
4980 stream
->need_keyframe
= 1;
4982 stream
->pes
.found_scr
= 0;
4983 stream
->pes
.scr
= AV_NOPTS_VALUE
;
4989 // ------------------------------------------------------------------
4990 // Support for reading media files via the ffmpeg libraries.
4992 static int ffmpeg_open( hb_stream_t
*stream
, hb_title_t
*title
, int scan
)
4994 AVFormatContext
*info_ic
= NULL
;
4996 av_log_set_level( AV_LOG_ERROR
);
4998 // Increase probe buffer size
4999 // The default (5MB) is not big enough to successfully scan
5000 // some files with large PNGs
5001 AVDictionary
* av_opts
= NULL
;
5002 av_dict_set( &av_opts
, "probesize", "15000000", 0 );
5004 // FFMpeg has issues with seeking. After av_find_stream_info, the
5005 // streams are left in an indeterminate position. So a seek is
5006 // necessary to force things back to the beginning of the stream.
5007 // But then the seek fails for some stream types. So the safest thing
5008 // to do seems to be to open 2 AVFormatContext. One for probing info
5009 // and the other for reading.
5010 if ( avformat_open_input( &info_ic
, stream
->path
, NULL
, &av_opts
) < 0 )
5014 // libav populates av_opts with the things it didn't recognize.
5015 AVDictionaryEntry
*t
= NULL
;
5016 while ((t
= av_dict_get(av_opts
, "", t
, AV_DICT_IGNORE_SUFFIX
)) != NULL
)
5018 hb_log("ffmpeg_open: unknown option '%s'", t
->key
);
5020 av_dict_free( &av_opts
);
5022 if ( avformat_find_stream_info( info_ic
, NULL
) < 0 )
5025 title
->opaque_priv
= (void*)info_ic
;
5026 stream
->ffmpeg_ic
= info_ic
;
5027 stream
->hb_stream_type
= ffmpeg
;
5028 stream
->ffmpeg_pkt
= malloc(sizeof(*stream
->ffmpeg_pkt
));
5029 av_init_packet( stream
->ffmpeg_pkt
);
5030 stream
->chapter_end
= INT64_MAX
;
5034 // we're opening for read. scan passed out codec params that
5035 // indexed its stream so we need to remap them so they point
5037 stream
->ffmpeg_video_id
= title
->video_id
;
5038 av_log_set_level( AV_LOG_ERROR
);
5042 // we're opening for scan. let ffmpeg put some info into the
5043 // log about what we've got.
5044 stream
->ffmpeg_video_id
= title
->video_id
;
5045 av_log_set_level( AV_LOG_INFO
);
5046 av_dump_format( info_ic
, 0, stream
->path
, 0 );
5047 av_log_set_level( AV_LOG_ERROR
);
5049 // accept this file if it has at least one video stream we can decode
5051 for (i
= 0; i
< info_ic
->nb_streams
; ++i
)
5053 if ( info_ic
->streams
[i
]->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
)
5058 if ( i
>= info_ic
->nb_streams
)
5064 if ( info_ic
) avformat_close_input( &info_ic
);
5068 static void ffmpeg_close( hb_stream_t
*d
)
5070 avformat_close_input( &d
->ffmpeg_ic
);
5071 if ( d
->ffmpeg_pkt
!= NULL
)
5073 free( d
->ffmpeg_pkt
);
5074 d
->ffmpeg_pkt
= NULL
;
5078 static void add_ffmpeg_audio(hb_title_t
*title
, hb_stream_t
*stream
, int id
)
5080 AVStream
*st
= stream
->ffmpeg_ic
->streams
[id
];
5081 AVCodecContext
*codec
= st
->codec
;
5082 AVDictionaryEntry
*tag
= av_dict_get(st
->metadata
, "language", NULL
, 0);
5084 hb_audio_t
*audio
= calloc(1, sizeof(*audio
));
5086 audio
->config
.in
.track
= id
;
5087 audio
->config
.in
.codec
= HB_ACODEC_FFMPEG
;
5088 audio
->config
.in
.codec_param
= codec
->codec_id
;
5089 // set the bitrate to 0; decavcodecaBSInfo will be called and fill the rest
5090 audio
->config
.in
.bitrate
= 0;
5092 // set the input codec and extradata for Passthru
5093 switch (codec
->codec_id
)
5095 case AV_CODEC_ID_AAC
:
5097 int len
= MIN(codec
->extradata_size
, HB_CONFIG_MAX_SIZE
);
5098 memcpy(audio
->priv
.config
.extradata
.bytes
, codec
->extradata
, len
);
5099 audio
->priv
.config
.extradata
.length
= len
;
5100 audio
->config
.in
.codec
= HB_ACODEC_FFAAC
;
5103 case AV_CODEC_ID_AC3
:
5104 audio
->config
.in
.codec
= HB_ACODEC_AC3
;
5107 case AV_CODEC_ID_EAC3
:
5108 audio
->config
.in
.codec
= HB_ACODEC_FFEAC3
;
5111 case AV_CODEC_ID_TRUEHD
:
5112 audio
->config
.in
.codec
= HB_ACODEC_FFTRUEHD
;
5115 case AV_CODEC_ID_DTS
:
5117 switch (codec
->profile
)
5119 case FF_PROFILE_DTS
:
5120 case FF_PROFILE_DTS_ES
:
5121 case FF_PROFILE_DTS_96_24
:
5122 audio
->config
.in
.codec
= HB_ACODEC_DCA
;
5125 case FF_PROFILE_DTS_HD_MA
:
5126 case FF_PROFILE_DTS_HD_HRA
:
5127 audio
->config
.in
.codec
= HB_ACODEC_DCA_HD
;
5135 case AV_CODEC_ID_FLAC
:
5137 int len
= MIN(codec
->extradata_size
, HB_CONFIG_MAX_SIZE
);
5138 memcpy(audio
->priv
.config
.extradata
.bytes
, codec
->extradata
, len
);
5139 audio
->priv
.config
.extradata
.length
= len
;
5140 audio
->config
.in
.codec
= HB_ACODEC_FFFLAC
;
5143 case AV_CODEC_ID_MP3
:
5144 audio
->config
.in
.codec
= HB_ACODEC_MP3
;
5151 set_audio_description(audio
,
5152 lang_for_code2(tag
!= NULL
? tag
->value
: "und"));
5153 hb_list_add(title
->list_audio
, audio
);
5158 * MkvVobSubtitlePrivateData = ( Line )*
5159 * Line = FieldName ':' ' ' FieldValue '\n'
5161 * FieldValue = [^\n]+
5163 * The line of interest is:
5164 * PaletteLine = "palette" ':' ' ' RRGGBB ( ',' ' ' RRGGBB )*
5166 * More information on the format at:
5167 * http://www.matroska.org/technical/specs/subtitles/images.html
5169 static int ffmpeg_parse_vobsub_extradata_mkv( AVCodecContext
*codec
, hb_subtitle_t
*subtitle
)
5171 // lines = (string) codec->extradata;
5172 char *lines
= malloc( codec
->extradata_size
+ 1 );
5173 if ( lines
== NULL
)
5175 memcpy( lines
, codec
->extradata
, codec
->extradata_size
);
5176 lines
[codec
->extradata_size
] = '\0';
5180 int gotDimensions
= 0;
5182 char *curLine
, *curLine_parserData
;
5183 for ( curLine
= strtok_r( lines
, "\n", &curLine_parserData
);
5185 curLine
= strtok_r( NULL
, "\n", &curLine_parserData
) )
5189 int numElementsRead
= sscanf(curLine
, "palette: "
5190 "%06x, %06x, %06x, %06x, "
5191 "%06x, %06x, %06x, %06x, "
5192 "%06x, %06x, %06x, %06x, "
5193 "%06x, %06x, %06x, %06x",
5194 &rgb
[0], &rgb
[1], &rgb
[2], &rgb
[3],
5195 &rgb
[4], &rgb
[5], &rgb
[6], &rgb
[7],
5196 &rgb
[8], &rgb
[9], &rgb
[10], &rgb
[11],
5197 &rgb
[12], &rgb
[13], &rgb
[14], &rgb
[15]);
5199 if (numElementsRead
== 16) {
5205 int numElementsRead
= sscanf(curLine
, "size: %dx%d",
5206 &subtitle
->width
, &subtitle
->height
);
5208 if (numElementsRead
== 2) {
5212 if (gotPalette
&& gotDimensions
)
5216 if (subtitle
->width
== 0 || subtitle
->height
== 0)
5218 subtitle
->width
= 720;
5219 subtitle
->height
= 480;
5227 for (i
=0; i
<16; i
++)
5228 subtitle
->palette
[i
] = hb_rgb2yuv(rgb
[i
]);
5229 subtitle
->palette_set
= 1;
5239 * Format: 8-bit {0,Y,Cb,Cr} x 16
5241 static int ffmpeg_parse_vobsub_extradata_mp4( AVCodecContext
*codec
, hb_subtitle_t
*subtitle
)
5243 if ( codec
->extradata_size
!= 4*16 )
5247 for ( i
=0, j
=0; i
<16; i
++, j
+=4 )
5249 subtitle
->palette
[i
] =
5250 codec
->extradata
[j
+1] << 16 | // Y
5251 codec
->extradata
[j
+2] << 8 | // Cb
5252 codec
->extradata
[j
+3] << 0; // Cr
5253 subtitle
->palette_set
= 1;
5255 if (codec
->width
<= 0 || codec
->height
<= 0)
5257 subtitle
->width
= 720;
5258 subtitle
->height
= 480;
5262 subtitle
->width
= codec
->width
;
5263 subtitle
->height
= codec
->height
;
5269 * Parses the 'subtitle->palette' information from the specific VOB subtitle track's private data.
5270 * Returns 0 if successful or 1 if parsing failed or was incomplete.
5272 static int ffmpeg_parse_vobsub_extradata( AVCodecContext
*codec
, hb_subtitle_t
*subtitle
)
5274 // XXX: Better if we actually chose the correct parser based on the input container
5276 ffmpeg_parse_vobsub_extradata_mkv( codec
, subtitle
) &&
5277 ffmpeg_parse_vobsub_extradata_mp4( codec
, subtitle
);
5280 static void add_ffmpeg_subtitle( hb_title_t
*title
, hb_stream_t
*stream
, int id
)
5282 AVStream
*st
= stream
->ffmpeg_ic
->streams
[id
];
5283 AVCodecContext
*codec
= st
->codec
;
5285 hb_subtitle_t
*subtitle
= calloc( 1, sizeof(*subtitle
) );
5289 switch ( codec
->codec_id
)
5291 case AV_CODEC_ID_DVD_SUBTITLE
:
5292 subtitle
->format
= PICTURESUB
;
5293 subtitle
->source
= VOBSUB
;
5294 subtitle
->config
.dest
= RENDERSUB
; // By default render (burn-in) the VOBSUB.
5295 subtitle
->codec
= WORK_DECVOBSUB
;
5296 if ( ffmpeg_parse_vobsub_extradata( codec
, subtitle
) )
5297 hb_log( "add_ffmpeg_subtitle: malformed extradata for VOB subtitle track; "
5298 "subtitle colors likely to be wrong" );
5300 case AV_CODEC_ID_TEXT
:
5301 subtitle
->format
= TEXTSUB
;
5302 subtitle
->source
= UTF8SUB
;
5303 subtitle
->config
.dest
= PASSTHRUSUB
;
5304 subtitle
->codec
= WORK_DECUTF8SUB
;
5306 case AV_CODEC_ID_MOV_TEXT
: // TX3G
5307 subtitle
->format
= TEXTSUB
;
5308 subtitle
->source
= TX3GSUB
;
5309 subtitle
->config
.dest
= PASSTHRUSUB
;
5310 subtitle
->codec
= WORK_DECTX3GSUB
;
5312 case AV_CODEC_ID_SSA
:
5313 subtitle
->format
= TEXTSUB
;
5314 subtitle
->source
= SSASUB
;
5315 subtitle
->config
.dest
= PASSTHRUSUB
;
5316 subtitle
->codec
= WORK_DECSSASUB
;
5318 case AV_CODEC_ID_HDMV_PGS_SUBTITLE
:
5319 subtitle
->format
= PICTURESUB
;
5320 subtitle
->source
= PGSSUB
;
5321 subtitle
->config
.dest
= RENDERSUB
;
5322 subtitle
->codec
= WORK_DECPGSSUB
;
5325 hb_log( "add_ffmpeg_subtitle: unknown subtitle stream type: 0x%x", (int) codec
->codec_id
);
5330 AVDictionaryEntry
*tag
;
5331 iso639_lang_t
*language
;
5333 tag
= av_dict_get( st
->metadata
, "language", NULL
, 0 );
5334 language
= lang_for_code2( tag
? tag
->value
: "und" );
5335 strcpy( subtitle
->lang
, language
->eng_name
);
5336 strncpy( subtitle
->iso639_2
, language
->iso639_2
, 4 );
5338 // Copy the extradata for the subtitle track
5339 if (codec
->extradata
!= NULL
)
5341 subtitle
->extradata
= malloc( codec
->extradata_size
);
5342 memcpy( subtitle
->extradata
, codec
->extradata
, codec
->extradata_size
);
5343 subtitle
->extradata_size
= codec
->extradata_size
;
5346 subtitle
->track
= id
;
5347 hb_list_add(title
->list_subtitle
, subtitle
);
5350 static char *get_ffmpeg_metadata_value( AVDictionary
*m
, char *key
)
5352 AVDictionaryEntry
*tag
= NULL
;
5354 while ( (tag
= av_dict_get(m
, "", tag
, AV_DICT_IGNORE_SUFFIX
)) )
5356 if ( !strcmp( key
, tag
->key
) )
5364 static void add_ffmpeg_attachment( hb_title_t
*title
, hb_stream_t
*stream
, int id
)
5366 AVStream
*st
= stream
->ffmpeg_ic
->streams
[id
];
5367 AVCodecContext
*codec
= st
->codec
;
5369 enum attachtype type
;
5370 const char *name
= get_ffmpeg_metadata_value( st
->metadata
, "filename" );
5371 switch ( codec
->codec_id
)
5373 case AV_CODEC_ID_TTF
:
5374 // Libav sets codec ID based on mime type of the attachment
5375 type
= FONT_TTF_ATTACH
;
5379 int len
= name
? strlen( name
) : 0;
5382 // Some attachments don't have the right mime type.
5383 // So also trigger on file name extension.
5384 if( !strcasecmp( name
+ len
- 4, ".ttc" ) ||
5385 !strcasecmp( name
+ len
- 4, ".ttf" ) )
5387 type
= FONT_TTF_ATTACH
;
5390 else if( !strcasecmp( name
+ len
- 4, ".otf" ) )
5392 type
= FONT_OTF_ATTACH
;
5396 // Ignore unrecognized attachment type
5401 hb_attachment_t
*attachment
= calloc( 1, sizeof(*attachment
) );
5403 // Copy the attachment name and data
5404 attachment
->type
= type
;
5405 attachment
->name
= strdup( name
);
5406 attachment
->data
= malloc( codec
->extradata_size
);
5407 memcpy( attachment
->data
, codec
->extradata
, codec
->extradata_size
);
5408 attachment
->size
= codec
->extradata_size
;
5410 hb_list_add(title
->list_attachment
, attachment
);
5413 static int ffmpeg_decmetadata( AVDictionary
*m
, hb_title_t
*title
)
5416 AVDictionaryEntry
*tag
= NULL
;
5417 while ( (tag
= av_dict_get(m
, "", tag
, AV_DICT_IGNORE_SUFFIX
)) )
5419 if ( !strcasecmp( "TITLE", tag
->key
) )
5421 hb_metadata_set_name(title
->metadata
, tag
->value
);
5424 else if ( !strcasecmp( "ARTIST", tag
->key
) )
5426 hb_metadata_set_artist(title
->metadata
, tag
->value
);
5429 else if ( !strcasecmp( "DIRECTOR", tag
->key
) ||
5430 !strcasecmp( "album_artist", tag
->key
) )
5432 hb_metadata_set_album_artist(title
->metadata
, tag
->value
);
5435 else if ( !strcasecmp( "COMPOSER", tag
->key
) )
5437 hb_metadata_set_composer(title
->metadata
, tag
->value
);
5440 else if ( !strcasecmp( "DATE_RELEASED", tag
->key
) ||
5441 !strcasecmp( "date", tag
->key
) )
5443 hb_metadata_set_release_date(title
->metadata
, tag
->value
);
5446 else if ( !strcasecmp( "SUMMARY", tag
->key
) ||
5447 !strcasecmp( "comment", tag
->key
) )
5449 hb_metadata_set_comment(title
->metadata
, tag
->value
);
5452 else if ( !strcasecmp( "GENRE", tag
->key
) )
5454 hb_metadata_set_genre(title
->metadata
, tag
->value
);
5457 else if ( !strcasecmp( "DESCRIPTION", tag
->key
) )
5459 hb_metadata_set_description(title
->metadata
, tag
->value
);
5462 else if ( !strcasecmp( "SYNOPSIS", tag
->key
) )
5464 hb_metadata_set_long_description(title
->metadata
, tag
->value
);
5471 static hb_title_t
*ffmpeg_title_scan( hb_stream_t
*stream
, hb_title_t
*title
)
5473 AVFormatContext
*ic
= stream
->ffmpeg_ic
;
5475 // 'Barebones Title'
5476 title
->type
= HB_FF_STREAM_TYPE
;
5478 // Copy part of the stream path to the title name
5479 char *sep
= hb_strr_dir_sep(stream
->path
);
5481 strcpy(title
->name
, sep
+1);
5482 char *dot_term
= strrchr(title
->name
, '.');
5486 uint64_t dur
= ic
->duration
* 90000 / AV_TIME_BASE
;
5487 title
->duration
= dur
;
5489 title
->hours
= dur
/ 3600;
5490 title
->minutes
= ( dur
% 3600 ) / 60;
5491 title
->seconds
= dur
% 60;
5493 // set the title to decode the first video stream in the file
5494 title
->demuxer
= HB_NULL_DEMUXER
;
5495 title
->video_codec
= 0;
5498 for (i
= 0; i
< ic
->nb_streams
; ++i
)
5500 if ( ic
->streams
[i
]->codec
->codec_type
== AVMEDIA_TYPE_VIDEO
&&
5501 !(ic
->streams
[i
]->disposition
& AV_DISPOSITION_ATTACHED_PIC
) &&
5502 avcodec_find_decoder( ic
->streams
[i
]->codec
->codec_id
) &&
5503 title
->video_codec
== 0 )
5505 AVCodecContext
*context
= ic
->streams
[i
]->codec
;
5506 pix_fmt
= context
->pix_fmt
;
5507 if ( context
->pix_fmt
!= AV_PIX_FMT_YUV420P
&&
5508 !sws_isSupportedInput( context
->pix_fmt
) )
5510 hb_log( "ffmpeg_title_scan: Unsupported color space" );
5513 title
->video_id
= i
;
5514 stream
->ffmpeg_video_id
= i
;
5515 if ( ic
->streams
[i
]->sample_aspect_ratio
.num
&&
5516 ic
->streams
[i
]->sample_aspect_ratio
.den
)
5518 title
->geometry
.par
.num
= ic
->streams
[i
]->sample_aspect_ratio
.num
;
5519 title
->geometry
.par
.den
= ic
->streams
[i
]->sample_aspect_ratio
.den
;
5522 title
->video_codec
= WORK_DECAVCODECV
;
5523 title
->video_codec_param
= context
->codec_id
;
5525 else if ( ic
->streams
[i
]->codec
->codec_type
== AVMEDIA_TYPE_AUDIO
&&
5526 avcodec_find_decoder( ic
->streams
[i
]->codec
->codec_id
) )
5528 add_ffmpeg_audio( title
, stream
, i
);
5530 else if ( ic
->streams
[i
]->codec
->codec_type
== AVMEDIA_TYPE_SUBTITLE
)
5532 add_ffmpeg_subtitle( title
, stream
, i
);
5534 else if ( ic
->streams
[i
]->codec
->codec_type
== AVMEDIA_TYPE_ATTACHMENT
)
5536 add_ffmpeg_attachment( title
, stream
, i
);
5540 title
->container_name
= strdup( ic
->iformat
->name
);
5541 title
->data_rate
= ic
->bit_rate
;
5543 hb_deep_log( 2, "Found ffmpeg %d chapters, container=%s", ic
->nb_chapters
, ic
->iformat
->name
);
5545 if( ic
->nb_chapters
!= 0 )
5548 uint64_t duration_sum
= 0;
5549 for( i
= 0; i
< ic
->nb_chapters
; i
++ )
5550 if( ( m
= ic
->chapters
[i
] ) != NULL
)
5552 AVDictionaryEntry
*tag
;
5553 hb_chapter_t
* chapter
;
5554 chapter
= calloc( sizeof( hb_chapter_t
), 1 );
5555 chapter
->index
= i
+1;
5556 chapter
->duration
= ( m
->end
/ ( (double) m
->time_base
.num
* m
->time_base
.den
) ) * 90000 - duration_sum
;
5557 duration_sum
+= chapter
->duration
;
5559 int seconds
= ( chapter
->duration
+ 45000 ) / 90000;
5560 chapter
->hours
= ( seconds
/ 3600 );
5561 chapter
->minutes
= ( seconds
% 3600 ) / 60;
5562 chapter
->seconds
= ( seconds
% 60 );
5564 tag
= av_dict_get( m
->metadata
, "title", NULL
, 0 );
5565 /* Ignore generic chapter names set by MakeMKV
5566 * ("Chapter 00" etc.).
5567 * Our default chapter names are better. */
5568 if( tag
&& tag
->value
&&
5569 ( strncmp( "Chapter ", tag
->value
, 8 ) ||
5570 strlen( tag
->value
) > 11 ) )
5572 hb_chapter_set_title( chapter
, tag
->value
);
5576 char chapter_title
[80];
5577 sprintf( chapter_title
, "Chapter %d", chapter
->index
);
5578 hb_chapter_set_title( chapter
, chapter_title
);
5581 hb_deep_log( 2, "Added chapter %i, name='%s', dur=%"PRIu64
", (%02i:%02i:%02i)",
5582 chapter
->index
, chapter
->title
, chapter
->duration
,
5583 chapter
->hours
, chapter
->minutes
, chapter
->seconds
);
5585 hb_list_add( title
->list_chapter
, chapter
);
5590 * Fill the metadata.
5592 ffmpeg_decmetadata( ic
->metadata
, title
);
5594 if( hb_list_count( title
->list_chapter
) == 0 )
5596 // Need at least one chapter
5597 hb_chapter_t
* chapter
;
5598 chapter
= calloc( sizeof( hb_chapter_t
), 1 );
5600 chapter
->duration
= title
->duration
;
5601 chapter
->hours
= title
->hours
;
5602 chapter
->minutes
= title
->minutes
;
5603 chapter
->seconds
= title
->seconds
;
5604 hb_list_add( title
->list_chapter
, chapter
);
5610 static int64_t av_to_hb_pts( int64_t pts
, double conv_factor
)
5612 if ( pts
== AV_NOPTS_VALUE
)
5613 return AV_NOPTS_VALUE
;
5614 return (int64_t)( (double)pts
* conv_factor
);
5617 static int ffmpeg_is_keyframe( hb_stream_t
*stream
)
5621 switch ( stream
->ffmpeg_ic
->streams
[stream
->ffmpeg_video_id
]->codec
->codec_id
)
5623 case AV_CODEC_ID_VC1
:
5624 // XXX the VC1 codec doesn't mark key frames so to get previews
5625 // we do it ourselves here. The decoder gets messed up if it
5626 // doesn't get a SEQ header first so we consider that to be a key frame.
5627 pkt
= stream
->ffmpeg_pkt
->data
;
5628 if ( !pkt
[0] && !pkt
[1] && pkt
[2] == 1 && pkt
[3] == 0x0f )
5633 case AV_CODEC_ID_WMV3
:
5634 // XXX the ffmpeg WMV3 codec doesn't mark key frames.
5635 // Only M$ could make I-frame detection this complicated: there
5636 // are two to four bits of unused junk ahead of the frame type
5637 // so we have to look at the sequence header to find out how much
5638 // to skip. Then there are three different ways of coding the type
5639 // depending on whether it's main or advanced profile then whether
5640 // there are bframes or not so we have to look at the sequence
5641 // header to get that.
5642 pkt
= stream
->ffmpeg_pkt
->data
;
5643 uint8_t *seqhdr
= stream
->ffmpeg_ic
->streams
[stream
->ffmpeg_video_id
]->codec
->extradata
;
5645 if ( ( seqhdr
[3] & 0x02 ) == 0 )
5648 if ( ( seqhdr
[3] & 0x80 ) == 0 )
5649 // no RANGEREDUCTION
5651 if ( seqhdr
[3] & 0x70 )
5652 // stream has b-frames
5653 return ( ( pkt
[0] >> pshift
) & 0x3 ) == 0x01;
5655 return ( ( pkt
[0] >> pshift
) & 0x2 ) == 0;
5660 return ( stream
->ffmpeg_pkt
->flags
& AV_PKT_FLAG_KEY
);
5663 hb_buffer_t
* hb_ffmpeg_read( hb_stream_t
*stream
)
5669 if ( ( err
= av_read_frame( stream
->ffmpeg_ic
, stream
->ffmpeg_pkt
)) < 0 )
5671 // av_read_frame can return EAGAIN. In this case, it expects
5672 // to be called again to get more data.
5673 if ( err
== AVERROR(EAGAIN
) )
5677 // XXX the following conditional is to handle avi files that
5678 // use M$ 'packed b-frames' and occasionally have negative
5679 // sizes for the null frames these require.
5680 if ( err
!= AVERROR(ENOMEM
) || stream
->ffmpeg_pkt
->size
>= 0 )
5683 if (err
!= AVERROR_EOF
)
5686 av_strerror(err
, errstr
, 80);
5687 hb_error("av_read_frame error (%d): %s", err
, errstr
);
5688 hb_set_work_error(stream
->h
, HB_ERROR_READ
);
5693 if ( stream
->ffmpeg_pkt
->stream_index
== stream
->ffmpeg_video_id
)
5695 if ( stream
->need_keyframe
)
5697 // we've just done a seek (generally for scan or live preview) and
5698 // want to start at a keyframe. Some ffmpeg codecs seek to a key
5699 // frame but most don't. So we spin until we either get a keyframe
5700 // or we've looked through 50 video frames without finding one.
5701 if ( ! ffmpeg_is_keyframe( stream
) && ++stream
->need_keyframe
< 50 )
5703 av_free_packet( stream
->ffmpeg_pkt
);
5706 stream
->need_keyframe
= 0;
5710 if ( stream
->ffmpeg_pkt
->size
<= 0 )
5712 // M$ "invalid and inefficient" packed b-frames require 'null frames'
5713 // following them to preserve the timing (since the packing puts two
5714 // or more frames in what looks like one avi frame). The contents and
5715 // size of these null frames are ignored by the ff_h263_decode_frame
5716 // as long as they're < 20 bytes. Zero length buffers are also
5717 // use by theora to indicate duplicate frames.
5718 buf
= hb_buffer_init( 0 );
5722 // sometimes we get absurd sizes from ffmpeg
5723 if ( stream
->ffmpeg_pkt
->size
>= (1 << 25) )
5725 hb_log( "ffmpeg_read: pkt too big: %d bytes", stream
->ffmpeg_pkt
->size
);
5726 av_free_packet( stream
->ffmpeg_pkt
);
5727 return hb_ffmpeg_read( stream
);
5729 buf
= hb_buffer_init( stream
->ffmpeg_pkt
->size
);
5730 memcpy( buf
->data
, stream
->ffmpeg_pkt
->data
, stream
->ffmpeg_pkt
->size
);
5732 const uint8_t *palette
;
5734 palette
= av_packet_get_side_data(stream
->ffmpeg_pkt
,
5735 AV_PKT_DATA_PALETTE
, &size
);
5736 if (palette
!= NULL
)
5738 buf
->palette
= hb_buffer_init( size
);
5739 memcpy( buf
->palette
->data
, palette
, size
);
5742 buf
->s
.id
= stream
->ffmpeg_pkt
->stream_index
;
5744 // compute a conversion factor to go from the ffmpeg
5745 // timebase for the stream to HB's 90kHz timebase.
5746 AVStream
*s
= stream
->ffmpeg_ic
->streams
[stream
->ffmpeg_pkt
->stream_index
];
5747 double tsconv
= (double)90000. * s
->time_base
.num
/ s
->time_base
.den
;
5748 int64_t offset
= 90000L * ffmpeg_initial_timestamp(stream
) / AV_TIME_BASE
;
5750 buf
->s
.start
= av_to_hb_pts(stream
->ffmpeg_pkt
->pts
, tsconv
) - offset
;
5751 buf
->s
.renderOffset
= av_to_hb_pts(stream
->ffmpeg_pkt
->dts
, tsconv
) - offset
;
5752 if ( buf
->s
.renderOffset
>= 0 && buf
->s
.start
== AV_NOPTS_VALUE
)
5754 buf
->s
.start
= buf
->s
.renderOffset
;
5756 else if ( buf
->s
.renderOffset
== AV_NOPTS_VALUE
&& buf
->s
.start
>= 0 )
5758 buf
->s
.renderOffset
= buf
->s
.start
;
5762 * Fill out buf->s.stop for subtitle packets
5764 * libavcodec's MKV demuxer stores the duration of UTF-8 subtitles (AV_CODEC_ID_TEXT)
5765 * in the 'convergence_duration' field for some reason.
5767 * Other subtitles' durations are stored in the 'duration' field.
5769 * VOB subtitles (AV_CODEC_ID_DVD_SUBTITLE) do not have their duration stored in
5770 * either field. This is not a problem because the VOB decoder can extract this
5771 * information from the packet payload itself.
5773 * SSA subtitles (AV_CODEC_ID_SSA) do not have their duration stored in
5774 * either field. This is not a problem because the SSA decoder can extract this
5775 * information from the packet payload itself.
5777 enum AVCodecID ffmpeg_pkt_codec
;
5778 enum AVMediaType codec_type
;
5779 ffmpeg_pkt_codec
= stream
->ffmpeg_ic
->streams
[stream
->ffmpeg_pkt
->stream_index
]->codec
->codec_id
;
5780 codec_type
= stream
->ffmpeg_ic
->streams
[stream
->ffmpeg_pkt
->stream_index
]->codec
->codec_type
;
5781 switch ( codec_type
)
5783 case AVMEDIA_TYPE_VIDEO
:
5784 buf
->s
.type
= VIDEO_BUF
;
5786 * libav avcodec_decode_video2() needs AVPacket flagged with AV_PKT_FLAG_KEY
5787 * for some codecs. For example, sequence of PNG in a mov container.
5789 if ( stream
->ffmpeg_pkt
->flags
& AV_PKT_FLAG_KEY
)
5791 buf
->s
.frametype
|= HB_FRAME_KEY
;
5795 case AVMEDIA_TYPE_AUDIO
:
5796 buf
->s
.type
= AUDIO_BUF
;
5799 case AVMEDIA_TYPE_SUBTITLE
:
5800 buf
->s
.type
= SUBTITLE_BUF
;
5804 buf
->s
.type
= OTHER_BUF
;
5807 if ( ffmpeg_pkt_codec
== AV_CODEC_ID_TEXT
) {
5808 int64_t ffmpeg_pkt_duration
= stream
->ffmpeg_pkt
->convergence_duration
;
5809 int64_t buf_duration
= av_to_hb_pts( ffmpeg_pkt_duration
, tsconv
);
5810 buf
->s
.stop
= buf
->s
.start
+ buf_duration
;
5812 if ( ffmpeg_pkt_codec
== AV_CODEC_ID_MOV_TEXT
) {
5813 int64_t ffmpeg_pkt_duration
= stream
->ffmpeg_pkt
->duration
;
5814 int64_t buf_duration
= av_to_hb_pts( ffmpeg_pkt_duration
, tsconv
);
5815 buf
->s
.stop
= buf
->s
.start
+ buf_duration
;
5819 * Check to see whether this buffer is on a chapter
5820 * boundary, if so mark it as such in the buffer then advance
5821 * chapter_end to the end of the next chapter.
5822 * If there are no chapters, chapter_end is always initialized to INT64_MAX
5823 * (roughly 3 million years at our 90KHz clock rate) so the test
5824 * below handles both the chapters & no chapters case.
5826 if ( stream
->ffmpeg_pkt
->stream_index
== stream
->ffmpeg_video_id
&&
5827 buf
->s
.start
>= stream
->chapter_end
)
5829 hb_chapter_t
*chapter
= hb_list_item( stream
->title
->list_chapter
,
5830 stream
->chapter
+1 );
5834 stream
->chapter_end
+= chapter
->duration
;
5835 buf
->s
.new_chap
= stream
->chapter
+ 1;
5836 hb_deep_log( 2, "ffmpeg_read starting chapter %i at %"PRId64
,
5837 stream
->chapter
+ 1, buf
->s
.start
);
5839 // Must have run out of chapters, stop looking.
5840 stream
->chapter_end
= INT64_MAX
;
5841 buf
->s
.new_chap
= 0;
5844 buf
->s
.new_chap
= 0;
5846 av_free_packet( stream
->ffmpeg_pkt
);
5850 static int ffmpeg_seek( hb_stream_t
*stream
, float frac
)
5852 AVFormatContext
*ic
= stream
->ffmpeg_ic
;
5856 int64_t pos
= (double)stream
->ffmpeg_ic
->duration
* (double)frac
+
5857 ffmpeg_initial_timestamp( stream
);
5858 res
= avformat_seek_file( ic
, -1, 0, pos
, pos
, AVSEEK_FLAG_BACKWARD
);
5861 hb_error("avformat_seek_file failed");
5866 int64_t pos
= ffmpeg_initial_timestamp( stream
);
5867 res
= avformat_seek_file( ic
, -1, 0, pos
, pos
, AVSEEK_FLAG_BACKWARD
);
5870 hb_error("avformat_seek_file failed");
5873 stream
->need_keyframe
= 1;
5877 // Assumes that we are always seeking forward
5878 static int ffmpeg_seek_ts( hb_stream_t
*stream
, int64_t ts
)
5880 AVFormatContext
*ic
= stream
->ffmpeg_ic
;
5884 pos
= ts
* AV_TIME_BASE
/ 90000 + ffmpeg_initial_timestamp( stream
);
5885 AVStream
*st
= stream
->ffmpeg_ic
->streams
[stream
->ffmpeg_video_id
];
5886 // timebase must be adjusted to match timebase of stream we are
5887 // using for seeking.
5888 pos
= av_rescale(pos
, st
->time_base
.den
, AV_TIME_BASE
* (int64_t)st
->time_base
.num
);
5889 stream
->need_keyframe
= 1;
5890 // Seek to the nearest timestamp before that requested where
5891 // there is an I-frame
5892 ret
= avformat_seek_file( ic
, stream
->ffmpeg_video_id
, 0, pos
, pos
, 0);