2 //This program was created by inverting libbluray, without any docs,
3 // so it is probably got problems. Still, works on my Samsung player.
4 // thanks to: Petri Hintukainen, William Hahne, John Stebbinsm, et.al.
7 // ./bd <tgt_dir_path> <playlist-0> <sep> <playlistp1> <sep> ... <sep> <playlist-n>
8 // <sep> == -<pgm_pid> | --<pgm_pid> | ---<pgm_pid>
9 // <pgm_pid> may be empty string, or a numeric pgm_pid for curr title clip
10 // <pgm_pid> defaults to first pgm probed.
11 // <playlist-x> == <clip-0.m2ts> <clip-1.m2ts> ... <clip-n.m2ts>
13 // ./brwrite /tmp/dir /path/menu.m2ts --- /path/clip0.m2ts /path/clip1.m2ts -- /path/clip2.m2ts
15 // one title is built for each playlist
16 // playlist-0 is used as first-play item
18 // the basic idea is to use playlist-0 as a menu / directions to
19 // use the bluray player remote-control to select the desired title
20 // and start the play, avoiding the need for a menu system.
22 // if the first play item is the main title, that is ok also.
23 // ./brwrite /tmp/dir /path/title.m2ts
26 //To use a bluray bd-re rewriteable: (such as for /dev/sr1)
27 // dvd+rw-format /dev/sr1 (only done once to init the media)
28 // mkudffs /dev/sr1 $((`cat /sys/block/sr1/size`*512/2048-1))
29 // mount /dev/sr1 /mnt1
30 // cp -av <tgd_dir_path>/BDMV /mnt1/.
41 #if !defined (__FreeBSD__)
44 #include <sys/endian.h>
48 // work arounds (centos)
50 #define INT64_MAX 9223372036854775807LL
53 #define INT64_MIN (-INT64_MAX-1)
56 #include "arraylist.h"
58 #define BCTEXTLEN 1024
59 #define BLURAY_TS_PKTSZ 192L
61 static const int bd_sig = 2;
64 #include "libavfilter/buffersrc.h"
65 #include "libavfilter/buffersink.h"
66 #include "libavformat/avformat.h"
67 #include "libavformat/avio.h"
68 #include "libavcodec/avcodec.h"
69 #include "libavfilter/avfilter.h"
70 #include "libavutil/avutil.h"
71 #include "libavutil/opt.h"
72 #include "libavutil/pixdesc.h"
73 #include "libswresample/swresample.h"
74 #include "libswscale/swscale.h"
87 BLURAY_APP_TYPE_MAIN_MOVIE = 1,
88 BLURAY_APP_TYPE_MAIN_TIMED_SLIDE_SHOW = 2,
89 BLURAY_APP_TYPE_MAIN_BROWSED_SLIDE_SHOW = 3,
90 BLURAY_APP_TYPE_SUBPATH_BROWSED_SLIDE_SHOW = 4,
91 BLURAY_APP_TYPE_SUBPATH_IG_MENU = 5,
92 BLURAY_APP_TYPE_SUBPATH_TEXT_SUBTITLE = 6,
93 BLURAY_APP_TYPE_SUBPATH_ELEMENTARY_STREAM = 7,
95 BLURAY_STREAM_TYPE_VIDEO_MPEG1 = 0x01,
96 BLURAY_STREAM_TYPE_VIDEO_MPEG2 = 0x02,
97 BLURAY_STREAM_TYPE_AUDIO_MPEG1 = 0x03,
98 BLURAY_STREAM_TYPE_AUDIO_MPEG2 = 0x04,
99 BLURAY_STREAM_TYPE_AUDIO_LPCM = 0x80,
100 BLURAY_STREAM_TYPE_AUDIO_AC3 = 0x81,
101 BLURAY_STREAM_TYPE_AUDIO_DTS = 0x82,
102 BLURAY_STREAM_TYPE_AUDIO_TRUHD = 0x83,
103 BLURAY_STREAM_TYPE_AUDIO_AC3PLUS = 0x84,
104 BLURAY_STREAM_TYPE_AUDIO_DTSHD = 0x85,
105 BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER = 0x86,
106 BLURAY_STREAM_TYPE_VIDEO_VC1 = 0xea,
107 BLURAY_STREAM_TYPE_VIDEO_H264 = 0x1b,
108 BLURAY_STREAM_TYPE_VIDEO_H264_MVC = 0x20,
109 BLURAY_STREAM_TYPE_VIDEO_HEVC = 0x24,
110 BLURAY_STREAM_TYPE_SUB_PG = 0x90,
111 BLURAY_STREAM_TYPE_SUB_IG = 0x91,
112 BLURAY_STREAM_TYPE_SUB_TEXT = 0x92,
113 BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY = 0xa1,
114 BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY = 0xa2,
116 BLURAY_MARK_TYPE_ENTRY = 0x01,
117 BLURAY_MARK_TYPE_LINK = 0x02,
119 BLURAY_PLAYBACK_TYPE_SEQUENTIAL = 1,
120 BLURAY_PLAYBACK_TYPE_RANDOM = 2,
121 BLURAY_PLAYBACK_TYPE_SHUFFLE = 3,
123 BLURAY_VIDEO_FORMAT_480I = 1, // ITU-R BT.601-5
124 BLURAY_VIDEO_FORMAT_576I = 2, // ITU-R BT.601-4
125 BLURAY_VIDEO_FORMAT_480P = 3, // SMPTE 293M
126 BLURAY_VIDEO_FORMAT_1080I = 4, // SMPTE 274M
127 BLURAY_VIDEO_FORMAT_720P = 5, // SMPTE 296M
128 BLURAY_VIDEO_FORMAT_1080P = 6, // SMPTE 274M
129 BLURAY_VIDEO_FORMAT_576P = 7, // ITU-R BT.1358
130 BLURAY_VIDEO_FORMAT_2160P = 8,
132 BLURAY_VIDEO_RATE_24000_1001 = 1, // 23.976
133 BLURAY_VIDEO_RATE_24 = 2,
134 BLURAY_VIDEO_RATE_25 = 3,
135 BLURAY_VIDEO_RATE_30000_1001 = 4, // 29.97
136 BLURAY_VIDEO_RATE_50 = 6,
137 BLURAY_VIDEO_RATE_60000_1001 = 7, // 59.94
139 BLURAY_ASPECT_RATIO_4_3 = 2,
140 BLURAY_ASPECT_RATIO_16_9 = 3,
142 BLURAY_AUDIO_FORMAT_MONO = 1,
143 BLURAY_AUDIO_FORMAT_STEREO = 3,
144 BLURAY_AUDIO_FORMAT_MULTI_CHAN = 6,
145 BLURAY_AUDIO_FORMAT_COMBO = 12, // Stereo ac3/dts,
147 BLURAY_AUDIO_RATE_48 = 1,
148 BLURAY_AUDIO_RATE_96 = 4,
149 BLURAY_AUDIO_RATE_192 = 5,
150 BLURAY_AUDIO_RATE_192_COMBO = 12, // 48 or 96 ac3/dts, 192 mpl/dts-hd
151 BLURAY_AUDIO_RATE_96_COMBO = 14, // 48 ac3/dts, 96 mpl/dts-hd
152 BLURAY_TEXT_CHAR_CODE_UTF8 = 0x01,
153 BLURAY_TEXT_CHAR_CODE_UTF16BE = 0x02,
154 BLURAY_TEXT_CHAR_CODE_SHIFT_JIS = 0x03,
155 BLURAY_TEXT_CHAR_CODE_EUC_KR = 0x04,
156 BLURAY_TEXT_CHAR_CODE_GB18030_20001 = 0x05,
157 BLURAY_TEXT_CHAR_CODE_CN_GB = 0x06,
158 BLURAY_TEXT_CHAR_CODE_BIG5 = 0x07,
160 BLURAY_STILL_NONE = 0x00,
161 BLURAY_STILL_TIME = 0x01,
162 BLURAY_STILL_INFINITE = 0x02,
164 BLURAY_PG_TEXTST_STREAM = 0x01,
165 BLURAY_SECONDARY_VIDEO_STREAM = 0x02,
166 BLURAY_SECONDARY_AUDIO_STREAM = 0x03,
174 bs_file() { fp = 0; }
178 int open(const char *fn);
180 void write(uint32_t v, int n);
182 void padb(int n) { pad(n*8); }
183 int64_t posb() { return fpos; }
184 void posb(int64_t n);
185 int64_t pos() { return posb()*8 + len; }
186 void writeb(uint8_t * bp, int n);
187 void writeb(const char *cp, int n) {
188 writeb((uint8_t *) cp, n);
195 bs_length() { fpos = len = 0; }
196 int64_t bs_posb(bs_file &bs) { return bs.posb() - fpos; }
197 void bs_len(bs_file &bs, int n) {
198 bs.write(len, n); fpos = bs.posb();
200 void bs_end(bs_file &bs) {
203 void bs_ofs(bs_file &bs, int n) {
204 bs.write(fpos-n/8, n);
206 void bs_zofs(bs_file &bs, int n) {
207 bs.write(!len ? 0 : fpos-n/8, n);
211 class _bd_stream_info {
222 _bd_stream_info() { memset(this, 0, sizeof(*this)); }
223 ~_bd_stream_info() {}
226 class bd_stream_info : public _bd_stream_info {
232 class _bd_clip_info {
236 uint16_t still_time; /* seconds */
241 _bd_clip_info() { memset(this, 0, sizeof(*this)); }
245 class bd_clip_info : public _bd_clip_info {
247 ArrayList<bd_stream_info *> video_streams;
248 ArrayList<bd_stream_info *> audio_streams;
249 ArrayList<bd_stream_info *> pg_streams;
250 ArrayList<bd_stream_info *> ig_streams;
251 ArrayList<bd_stream_info *> sec_audio_streams;
252 ArrayList<bd_stream_info *> sec_video_streams;
256 video_streams.remove_all_objects();
257 audio_streams.remove_all_objects();
258 pg_streams.remove_all_objects();
259 ig_streams.remove_all_objects();
260 sec_audio_streams.remove_all_objects();
261 sec_video_streams.remove_all_objects();
265 class _bd_title_chapter {
273 _bd_title_chapter() { memset(this, 0, sizeof(*this)); }
274 ~_bd_title_chapter() {}
277 class bd_title_chapter : public _bd_title_chapter {
279 bd_title_chapter() {}
280 ~bd_title_chapter() {}
283 class _bd_title_mark {
292 _bd_title_mark() { memset(this, 0, sizeof(*this)); }
296 class bd_title_mark : public _bd_title_mark {
302 class _bd_title_info {
309 _bd_title_info() { memset(this, 0, sizeof(*this)); }
313 class bd_title_info : public _bd_title_info {
315 ArrayList<bd_clip_info *> clips;
316 ArrayList<bd_title_chapter *> chapters;
317 ArrayList<bd_title_mark *> marks;
321 clips.remove_all_objects();
322 chapters.remove_all_objects();
323 marks.remove_all_objects();
327 class _clpi_stc_seq {
330 uint32_t spn_stc_start;
331 uint32_t presentation_start_time;
332 uint32_t presentation_end_time;
334 _clpi_stc_seq() { memset(this, 0, sizeof(*this)); }
338 class clpi_stc_seq : public _clpi_stc_seq {
346 class _clpi_atc_seq {
348 uint32_t spn_atc_start;
349 uint8_t offset_stc_id;
351 _clpi_atc_seq() { memset(this, 0, sizeof(*this)); }
355 class clpi_atc_seq : public _clpi_atc_seq {
357 ArrayList<clpi_stc_seq *> stc_seq;
362 stc_seq.remove_all_objects();
366 class clpi_sequences : public bs_length,
367 public ArrayList<clpi_atc_seq *> {
372 ~clpi_sequences() { remove_all_objects(); }
375 class _clpi_ts_type {
380 _clpi_ts_type() { memset(this, 0, sizeof(*this)); }
384 class clpi_ts_type : public _clpi_ts_type {
390 class _clpi_atc_delta {
396 _clpi_atc_delta() { memset(this, 0, sizeof(*this)); }
397 ~_clpi_atc_delta() {}
400 class clpi_atc_delta : public _clpi_atc_delta {
412 _clpi_font() { memset(this, 0, sizeof(*this)); }
416 class clpi_font : public _clpi_font {
422 class clpi_font_info {
424 ArrayList<clpi_font *> font;
428 font.remove_all_objects();
432 class _clpi_clip_info {
434 uint8_t clip_stream_type;
435 uint8_t application_type;
436 uint8_t is_atc_delta;
437 uint32_t ts_recording_rate;
438 uint32_t num_source_packets;
440 _clpi_clip_info() { memset(this, 0, sizeof(*this)); }
441 ~_clpi_clip_info() {}
444 class clpi_clip_info : public bs_length, public _clpi_clip_info {
446 clpi_ts_type ts_type_info;
447 clpi_font_info font_info;
448 ArrayList<clpi_atc_delta *> atc_delta;
453 atc_delta.remove_all_objects();
457 class _clpi_prog_stream {
468 _clpi_prog_stream() { memset(this, 0, sizeof(*this)); }
469 ~_clpi_prog_stream() {}
472 class clpi_prog_stream : public bs_length, public _clpi_prog_stream {
476 clpi_prog_stream() {}
477 ~clpi_prog_stream() {}
480 class _clpi_ep_coarse {
486 _clpi_ep_coarse() { memset(this, 0, sizeof(*this)); }
487 ~_clpi_ep_coarse() {}
490 class clpi_ep_coarse : public _clpi_ep_coarse {
498 class _clpi_ep_fine {
500 uint8_t is_angle_change_point;
501 uint8_t i_end_position_offset;
505 _clpi_ep_fine() { memset(this, 0, sizeof(*this)); }
509 class clpi_ep_fine : public _clpi_ep_fine {
517 class _clpi_ep_map_entry {
519 uint8_t ep_stream_type;
520 uint32_t ep_map_stream_start_addr;
522 _clpi_ep_map_entry() { memset(this, 0, sizeof(*this)); }
523 ~_clpi_ep_map_entry() {}
526 class clpi_ep_map_entry : public _clpi_ep_map_entry {
530 ArrayList<clpi_ep_coarse *> coarse;
531 ArrayList<clpi_ep_fine *> fine;
532 int write(uint32_t ep_map_pos);
535 clpi_ep_map_entry(int id) { fine_start = 0; pid = id; }
536 ~clpi_ep_map_entry() {
537 coarse.remove_all_objects();
538 fine.remove_all_objects();
544 uint32_t spn_program_sequence_start;
546 _clpi_prog() { memset(this, 0, sizeof(*this)); }
550 class clpi_prog : public _clpi_prog {
552 ArrayList<clpi_prog_stream *> streams;
553 uint16_t program_map_pid;
556 clpi_prog(int pmt_pid) { program_map_pid = pmt_pid; }
557 ~clpi_prog() { streams.remove_all_objects(); }
560 class clpi_programs : public bs_length,
561 public ArrayList<clpi_prog *> {
566 ~clpi_programs() { remove_all_objects(); }
569 class clpi_extents : public bs_length,
570 public ArrayList<uint32_t> {
581 _clpi_cpi() { type = 0; }
585 class clpi_cpi : public bs_length, public _clpi_cpi,
586 public ArrayList<clpi_ep_map_entry *> {
591 ~clpi_cpi() { remove_all_objects(); }
594 class clpi_cmrk : public bs_length {
605 unsigned int menu_call : 1;
606 unsigned int title_search : 1;
607 unsigned int chapter_search : 1;
608 unsigned int time_search : 1;
609 unsigned int skip_to_next_point : 1;
610 unsigned int skip_to_prev_point : 1;
611 unsigned int play_firstplay : 1;
612 unsigned int stop : 1;
613 unsigned int pause_on : 1;
614 unsigned int pause_off : 1;
615 unsigned int still_off : 1;
616 unsigned int forward : 1;
617 unsigned int backward : 1;
618 unsigned int resume : 1;
619 unsigned int move_up : 1;
620 unsigned int move_down : 1;
621 unsigned int move_left : 1;
622 unsigned int move_right : 1;
623 unsigned int select : 1;
624 unsigned int activate : 1;
625 unsigned int select_and_activate : 1;
626 unsigned int primary_audio_change : 1;
627 unsigned int reserved0 : 1;
628 unsigned int angle_change : 1;
629 unsigned int popup_on : 1;
630 unsigned int popup_off : 1;
631 unsigned int pg_enable_disable : 1;
632 unsigned int pg_change : 1;
633 unsigned int secondary_video_enable_disable : 1;
634 unsigned int secondary_video_change : 1;
635 unsigned int secondary_audio_enable_disable : 1;
636 unsigned int secondary_audio_change : 1;
637 unsigned int reserved1 : 1;
638 unsigned int pip_pg_change : 1;
642 memset(this, 0, sizeof(*this));
657 uint8_t dynamic_range_type;
660 uint8_t hdr_plus_flag;
664 _mpls_stream() { memset(this, 0, sizeof(*this)); }
668 class mpls_stream : public _mpls_stream {
670 bs_length strm, code;
686 class mpls_stn : public bs_length, public _mpls_stn {
688 ArrayList<mpls_stream *> video;
689 ArrayList<mpls_stream *> audio;
690 ArrayList<mpls_stream *> pg;
691 ArrayList<mpls_stream *> ig;
692 ArrayList<mpls_stream *> secondary_audio;
693 ArrayList<mpls_stream *> secondary_video;
694 // Secondary audio specific fields
695 ArrayList<uint8_t> sa_primary_audio_ref;
696 // Secondary video specific fields
697 ArrayList<uint8_t> sv_secondary_audio_ref;
698 ArrayList<uint8_t> sv_pip_pg_ref;
703 video.remove_all_objects();
704 audio.remove_all_objects();
705 pg.remove_all_objects();
706 ig.remove_all_objects();
707 secondary_audio.remove_all_objects();
708 secondary_video.remove_all_objects();
718 _mpls_clip() { memset(this, 0, sizeof(*this)); }
722 class mpls_clip : public _mpls_clip {
724 mpls_clip() { strcpy(codec_id, "M2TS"); }
730 uint8_t is_multi_angle;
731 uint8_t connection_condition;
734 uint8_t random_access_flag;
737 uint8_t is_different_audio;
738 uint8_t is_seamless_angle;
740 _mpls_pi() { memset(this, 0, sizeof(*this)); }
744 class mpls_pi : public bs_length, public _mpls_pi {
747 ArrayList<mpls_clip *> clip;
754 clip.remove_all_objects();
762 uint16_t play_item_ref;
764 uint16_t entry_es_pid;
767 _mpls_plm() { memset(this, 0, sizeof(*this)); }
771 class mpls_plm : public _mpls_plm {
781 uint8_t playback_type;
782 uint16_t playback_count;
783 uint8_t random_access_flag;
784 uint8_t audio_mix_flag;
785 uint8_t lossless_bypass_flag;
787 _mpls_ai() { memset(this, 0, sizeof(*this)); }
791 class mpls_ai : public bs_length, public _mpls_ai {
802 uint8_t connection_condition;
803 uint8_t is_multi_clip;
806 uint16_t sync_play_item_id;
809 _mpls_sub_pi() { memset(this, 0, sizeof(*this)); }
813 class mpls_sub_pi : public bs_length, public _mpls_sub_pi {
815 ArrayList<mpls_clip *> clip;
820 clip.remove_all_objects();
829 _mpls_sub() { memset(this, 0, sizeof(*this)); }
833 class mpls_sub : public bs_length, public _mpls_sub {
835 ArrayList<mpls_sub_pi *> sub_play_item;
840 sub_play_item.remove_all_objects();
844 class _mpls_pip_data {
849 uint8_t scale_factor;
851 _mpls_pip_data() { memset(this, 0, sizeof(*this)); }
855 class mpls_pip_data : public _mpls_pip_data {
863 class _mpls_pip_metadata {
866 uint8_t secondary_video_ref;
867 uint8_t timeline_type;
868 uint8_t luma_key_flag;
869 uint8_t upper_limit_luma_key;
870 uint8_t trick_play_flag;
872 _mpls_pip_metadata() { memset(this, 0, sizeof(*this)); }
873 ~_mpls_pip_metadata() {}
876 class mpls_pip_metadata : public _mpls_pip_metadata {
878 ArrayList<mpls_pip_data *> data;
880 int write(uint32_t start_address);
881 mpls_pip_metadata() {}
882 ~mpls_pip_metadata() {
883 data.remove_all_objects();
893 _mpls_pl() { memset(this, 0, sizeof(*this)); }
897 class mpls_pl : public _mpls_pl {
900 bs_length play, mark, subx, pipm;
902 ArrayList<mpls_pi *> play_item;
903 ArrayList<mpls_sub *> sub_path;
904 ArrayList<mpls_plm *> play_mark;
905 // extension data (profile 5, version 2.4)
906 ArrayList<mpls_sub *> ext_sub_path;
907 // extension data (Picture-In-Picture metadata)
908 ArrayList<mpls_pip_metadata *> ext_pip_data;
911 int write_playlist();
912 int write_playlistmark();
913 int write_subpath_extension();
914 int write_pip_metadata_extension();
917 mpls_pl() { sig = bd_sig; }
919 play_item.remove_all_objects();
920 sub_path.remove_all_objects();
921 play_mark.remove_all_objects();
922 ext_sub_path.remove_all_objects();
923 ext_pip_data.remove_all_objects();
929 uint32_t sequence_info_start_addr;
930 uint32_t program_info_start_addr;
931 uint32_t cpi_start_addr;
932 uint32_t clip_mark_start_addr;
933 uint32_t ext_data_start_addr;
935 _clpi_cl() { memset(this, 0, sizeof(*this)); }
939 class clpi_cl : public _clpi_cl {
943 clpi_sequences sequences;
944 clpi_programs programs;
946 clpi_extents extents;
947 clpi_programs programs_ss;
954 int write_clpi_extension(int id1, int id2, void *handle);
955 int write_mpls_extension(int id1, int id2, void *handle);
957 clpi_cl() { sig = bd_sig; }
966 uint32_t sub_grp:3; /* command sub-group */
967 uint32_t grp:2; /* command group */
968 uint32_t op_cnt:3; /* operand count */
969 uint32_t branch_opt:4; /* branch option */
971 uint32_t imm_op2:1; /* I-flag for operand 2 */
972 uint32_t imm_op1:1; /* I-flag for operand 1 */
973 uint32_t cmp_opt:4; /* compare option */
975 uint32_t set_opt:5; /* set option */
982 command_obj() { cmd = dst = src = 0; }
988 int resume_intention_flag;
990 int title_search_mask;
992 _movie_obj() { memset(this, 0, sizeof(*this)); }
996 class movie_obj : public _movie_obj {
998 ArrayList<command_obj *> cmds;
1002 cmds.remove_all_objects();
1006 class movie_file : public bs_length {
1009 ArrayList<movie_obj *> movies;
1011 movie_file() { sig = bd_sig; }
1013 movies.remove_all_objects();
1025 _pb_obj() { memset(this, 0, sizeof(*this)); }
1029 class pb_obj : public _pb_obj {
1043 void set_hdmv(int id, int pt);
1044 void set_bdj(char *nm, int pt);
1045 void write_hdmv_obj(int id_ref);
1046 void write_bdj_obj(char *name);
1052 _title_obj() { acc_typ = 0; }
1056 class title_obj : public pb_obj, public _title_obj {
1067 int initial_output_mode_preference;
1068 int content_exist_flag;
1072 _index_file() { memset(this, 0, sizeof(*this)); }
1076 class index_file : public bs_length, public _index_file {
1078 ArrayList<title_obj *> titles;
1082 bs_length exten, appinf;
1084 void write_hdmv_obj(int hdmv_typ, int id_ref);
1085 void write_bdj_obj(int bdj_typ, char *name);
1086 void write_pb_obj(pb_obj * pb);
1091 memset(user_data, 0, sizeof(user_data));
1094 titles.remove_all_objects();
1100 uint32_t data_start;
1101 uint32_t extension_data_start;
1105 _bdid() { memset(this, 0, sizeof(*this)); }
1109 class bdid : public _bdid {
1114 bdid() { sig = bd_sig; }
1118 class stream : public bd_stream_info {
1123 AVCodecContext *ctx;
1129 stream(AVMediaType ty, int i) {
1130 type = ty; av_idx = i; ctx = 0;
1131 start_pts = INT64_MAX; end_pts = INT64_MIN;
1134 ~stream() { if( ctx ) avcodec_free_context(&ctx); }
1139 static int cmpr(mark **, mark **);
1140 int64_t sample, pos, pts;
1142 mark(int64_t s, int64_t p, int64_t ts, uint32_t pk, int id) {
1143 sample = s; pos = p; pts = ts; pkt = pk; pid = id;
1147 int mark::cmpr(mark **a, mark **b)
1149 mark *ap = *(mark **)a, *bp = *(mark **)b;
1150 return ap->pts > bp->pts ? 1 : ap->pts < bp->pts ? -1 : 0;
1155 int pmt_pid, pcr_pid, ep_pid;
1156 int64_t start_time, end_time;
1159 memset(this, 0, sizeof(*this));
1160 start_time = INT64_MAX; end_time = INT64_MIN;
1165 class program : public _program {
1169 ArrayList<int> strm_idx;
1170 ArrayList<mark*> marks;
1172 int build_toc(clpi_ep_map_entry *map);
1173 void add_label(uint32_t pk, int64_t p, int64_t ts, int id) {
1174 marks.append(new mark(curr_pos, p, ts, pk, id));
1177 program(int i, int id) { idx = i; pid = id; curr_pos = 0; }
1178 ~program() { marks.remove_all_objects(); }
1187 memset(this, 0, sizeof(*this));
1192 class media_info : public _media_info {
1194 static const AVRational clk45k;
1195 char filename[BCTEXTLEN];
1197 int brk, pidx, still;
1198 ArrayList<stream *> streams;
1199 ArrayList<program *> programs;
1200 program *prog() { return programs[pidx]; }
1202 media_info(const char *fn) {
1203 strcpy(filename, fn);
1204 brk = 0; pidx = 0; pgm_pid = -1; still = 0;
1207 streams.remove_all_objects();
1208 programs.remove_all_objects();
1212 int scan(AVFormatContext *fmt_ctx);
1215 class Media : public ArrayList<media_info *> {
1218 char filename[BCTEXTLEN];
1219 int bd_path(const char *bp, const char *fmt, va_list ap);
1220 int bd_open(const char *fmt, ...);
1221 int bd_backup(const char *fmt, ...);
1222 int bd_copy(const char *ifn, const char *fmt, ...);
1226 ArrayList<clpi_cl *> cl;
1227 ArrayList<mpls_pl *> pl;
1229 void add_movie(uint32_t *ops, int n);
1230 int compose(int ch_interval);
1231 int write(char *fn);
1233 Media() { path = 0; filename[0] = 0; }
1235 remove_all_objects();
1236 cl.remove_all_objects();
1237 pl.remove_all_objects();
1243 void bs_file::init()
1251 bs_file::open(const char *fn)
1254 if( !fp ) perror(fn);
1255 return fp != 0 ? 0 : 1;
1261 if( fp ) { fclose(fp); fp = 0; }
1265 bs_file::write(uint32_t v, int n)
1272 if( fp ) fputc((vv >> len), fp);
1273 if( ++fpos > fsz ) fsz = fpos;
1278 bs_file::writeb(uint8_t * bp, int n)
1280 while( --n >= 0 ) write(*bp++, 8);
1286 while( n > 32 ) { write(0, 32); n -= 32; }
1287 if( n > 0 ) write(0, n);
1291 bs_file::posb(int64_t n)
1293 if( fpos == fsz && n > fpos ) {
1298 if( fp ) fseek(fp, fpos, SEEK_SET);
1308 bs.writeb("MOBJ", 4);
1309 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1310 int movie_start = 0x0028;
1311 bs.posb(movie_start);
1314 bs.write(movies.size(), 16);
1316 for (int i = 0; i < movies.size(); ++i) {
1317 movie_obj *mov = movies[i];
1318 bs.write(mov->resume_intention_flag, 1);
1319 bs.write(mov->menu_call_mask, 1);
1320 bs.write(mov->title_search_mask, 1);
1322 ArrayList<command_obj *> &cmds = mov->cmds;
1323 int num_cmds = cmds.size();
1324 bs.write(num_cmds, 16);
1325 for (int j = 0; j < num_cmds; ++j) {
1326 command_obj *cmd = cmds[j];
1327 bs.write(cmd->op_cnt, 3);
1328 bs.write(cmd->grp, 2);
1329 bs.write(cmd->sub_grp, 3);
1330 bs.write(cmd->imm_op1, 1);
1331 bs.write(cmd->imm_op2, 1);
1333 bs.write(cmd->branch_opt, 4);
1335 bs.write(cmd->cmp_opt, 4);
1337 bs.write(cmd->set_opt, 5);
1338 //bs.write(cmd->cmd, 32);
1339 bs.write(cmd->dst, 32);
1340 bs.write(cmd->src, 32);
1344 // bs.write('l', 8);
1345 // bs.writebcd(year, 16);
1346 // bs.writebcd(month, 8);
1347 // bs.writebcd(day, 8);
1348 // bs.writebcd(hour, 8);
1349 // bs.writebcd(min, 8);
1350 // bs.writebcd(sec, 8);
1356 pb_obj::set_hdmv(int id, int pt)
1366 pb_obj::set_bdj(char *nm, int pt)
1372 bdj_name = cstrdup(nm);
1376 pb_obj::write_hdmv_obj(int id_ref)
1378 bs.write(pb_typ, 2);
1380 bs.write(id_ref, 16);
1385 pb_obj::write_bdj_obj(char *name)
1387 bs.write(pb_typ, 2);
1398 write_bdj_obj(bdj_name);
1402 write_hdmv_obj(hdmv_id);
1408 title_obj::write_obj()
1410 bs.write(obj_typ, 2);
1411 bs.write(acc_typ, 2);
1413 pb_obj::write_obj();
1418 index_file::write_pb_obj(pb_obj * pb)
1420 bs.write(pb->obj_typ, 2);
1429 bs.writeb("INDX", 4);
1430 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1432 exten.bs_zofs(bs, 32);
1433 int appinfo_start = 0x28;
1434 bs.posb(appinfo_start);
1435 appinf.bs_len(bs, 32);
1437 bs.write(initial_output_mode_preference, 1);
1438 bs.write(content_exist_flag, 1);
1440 bs.write(video_format, 4);
1441 bs.write(frame_rate, 4);
1442 bs.writeb(user_data, 32);
1447 write_pb_obj(&first_play);
1448 write_pb_obj(&top_menu);
1449 bs.write(titles.size(), 16);
1451 for (int i = 0; i < titles.size(); ++i)
1452 titles[i]->write_obj();
1454 exten.bs_len(bs,32);
1464 bs.writeb("BDID",4);
1465 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1466 bs.write(data_start, 32);
1467 bs.write(extension_data_start, 32);
1469 bs.writeb(org_id, sizeof(org_id));
1470 bs.writeb(disc_id, sizeof(disc_id));
1474 // XXX - not current referenced
1477 bdmv_write_extension_data(int start_address,
1478 int (*handler) (int, int, void *), void *handle)
1483 bs.write(length, 32); /* length of extension data block */
1487 bs.pad(32); /* relative start address of extension data */
1488 bs.pad(24); /* padding */
1489 bs.write(num_entries, 8);
1491 for (n = 0; n < num_entries; n++) {
1494 bs.write(ext_start, 32);
1495 bs.write(ext_len, 32);
1496 saved_pos = bs.pos() >> 3;
1497 bs.posb(start_address + ext_start);
1498 handler(id1, id2, handle);
1507 clpi_prog_stream::write()
1512 bs.write(coding_type, 8);
1513 switch (coding_type) {
1514 case BLURAY_STREAM_TYPE_VIDEO_MPEG1:
1515 case BLURAY_STREAM_TYPE_VIDEO_MPEG2:
1516 case BLURAY_STREAM_TYPE_VIDEO_VC1:
1517 case BLURAY_STREAM_TYPE_VIDEO_H264:
1518 case BLURAY_STREAM_TYPE_VIDEO_HEVC:
1520 bs.write(format, 4);
1522 bs.write(aspect, 4);
1524 bs.write(oc_flag, 1);
1528 case BLURAY_STREAM_TYPE_AUDIO_MPEG1:
1529 case BLURAY_STREAM_TYPE_AUDIO_MPEG2:
1530 case BLURAY_STREAM_TYPE_AUDIO_LPCM:
1531 case BLURAY_STREAM_TYPE_AUDIO_AC3:
1532 case BLURAY_STREAM_TYPE_AUDIO_DTS:
1533 case BLURAY_STREAM_TYPE_AUDIO_TRUHD:
1534 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS:
1535 case BLURAY_STREAM_TYPE_AUDIO_DTSHD:
1536 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER:
1537 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY:
1538 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY:
1539 bs.write(format, 4);
1544 case BLURAY_STREAM_TYPE_SUB_PG:
1545 case BLURAY_STREAM_TYPE_SUB_IG:
1550 case BLURAY_STREAM_TYPE_SUB_TEXT:
1551 bs.write(char_code, 8);
1556 fprintf(stderr, "clpi_prog_stream: unrecognized coding type %02x\n",
1561 bs.padb(0x15 - bs_posb(bs));
1567 clpi_cl::write_header()
1569 bs.writeb("HDMV", 4);
1570 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1571 bs.write(sequence_info_start_addr, 32);
1572 bs.write(program_info_start_addr, 32);
1573 bs.write(cpi_start_addr, 32);
1574 bs.write(clip_mark_start_addr, 32);
1575 bs.write(ext_data_start_addr, 32);
1580 clpi_atc_delta::write()
1582 bs.write(delta, 32);
1583 bs.writeb(file_id, 5);
1584 bs.writeb(file_code, 4);
1590 clpi_clip_info::write()
1595 bs.pad(16); // reserved
1596 bs.write(clip_stream_type, 8);
1597 bs.write(application_type, 8);
1598 bs.pad(31); // skip reserved 31 bits
1599 bs.write(is_atc_delta, 1);
1600 bs.write(ts_recording_rate, 32);
1601 bs.write(num_source_packets, 32);
1603 bs.padb(128); // Skip reserved 128 bytes
1605 // ts type info block
1607 bs.write(ts_len, 16);
1608 int64_t pos = bs.posb();
1610 bs.write(ts_type_info.validity, 8);
1611 bs.writeb(ts_type_info.format_id, 4);
1612 // pad the stuff we don't know anything about
1613 bs.padb(ts_len - (bs.posb() - pos));
1616 if( is_atc_delta ) {
1617 bs.pad(8); // Skip reserved byte
1618 bs.write(atc_delta.size(), 8);
1619 for( int ii=0; ii < atc_delta.size(); ++ii )
1620 if( atc_delta[ii]->write() ) return 1;
1624 if( application_type == 6 /* Sub TS for a sub-path of Text subtitle */ ) {
1626 bs.write(font_info.font.size(), 8);
1627 if( font_info.font.size() ) {
1628 for( int ii=0; ii < font_info.font.size(); ++ii ) {
1629 bs.writeb(font_info.font[ii]->file_id, 5);
1640 clpi_stc_seq::write()
1642 bs.write(pcr_pid, 16);
1643 bs.write(spn_stc_start, 32);
1644 bs.write(presentation_start_time, 32);
1645 bs.write(presentation_end_time, 32);
1650 clpi_atc_seq::write()
1652 bs.write(spn_atc_start, 32);
1653 bs.write(stc_seq.size(), 8);
1654 bs.write(offset_stc_id, 8);
1656 for( int ii=0; ii < stc_seq.size(); ++ii )
1657 if( stc_seq[ii]->write() ) return 1;
1662 clpi_sequences::write()
1665 bs.padb(1); // reserved byte
1666 bs.write(size(), 8);
1667 for( int ii=0; ii < size(); ++ii )
1668 if( get(ii)->write() ) return 1;
1676 bs.write(spn_program_sequence_start, 32);
1677 bs.write(program_map_pid, 16);
1678 bs.write(streams.size(), 8);
1679 bs.write(num_groups, 8);
1680 for( int ii=0; ii < streams.size(); ++ii )
1681 if( streams[ii]->write() ) return 1;
1686 clpi_programs::write()
1689 bs.padb(1); // reserved byte
1690 bs.write(size(), 8);
1691 for( int ii=0; ii < size(); ++ii )
1692 if( get(ii)->write() ) return 1;
1698 clpi_ep_coarse::write()
1700 bs.write(ref_ep_fine_id, 18);
1701 bs.write(pts_ep, 14);
1702 bs.write(spn_ep, 32);
1707 clpi_ep_fine::write()
1709 bs.write(is_angle_change_point, 1);
1710 bs.write(i_end_position_offset, 3);
1711 bs.write(pts_ep, 11);
1712 bs.write(spn_ep, 17);
1717 clpi_ep_map_entry::write(uint32_t ep_map_pos)
1721 bs.write(ep_stream_type, 4);
1722 bs.write(coarse.size(), 16);
1723 bs.write(fine.size(), 18);
1724 bs.write(ep_map_stream_start_addr - ep_map_pos, 32);
1729 clpi_ep_map_entry::write_map()
1731 ep_map_stream_start_addr = bs.posb();
1732 bs.write(fine_start, 32);
1734 for( int ii=0; ii < coarse.size(); ++ii )
1735 if( coarse[ii]->write() ) return 1;
1737 fine_start = bs.posb() - ep_map_stream_start_addr;
1738 for( int ii=0; ii < fine.size(); ++ii )
1739 if( fine[ii]->write() ) return 1;
1750 uint32_t ep_map_pos = bs.posb();
1752 // EP Map starts here
1754 bs.write(size(), 8);
1756 for( int ii=0; ii < size(); ++ii )
1757 if( get(ii)->write(ep_map_pos) ) return 1;
1759 for( int ii=0; ii < size(); ++ii )
1760 if( get(ii)->write_map() ) return 1;
1775 clpi_extents::write()
1778 bs.write(size(), 32);
1779 for( int ii=0; ii < size(); ++ii )
1780 bs.write(get(ii), 32);
1785 clpi_cl::write_clpi_extension(int id1, int id2, void *handle)
1787 clpi_cl *cl = (clpi_cl *) handle;
1791 // LPCM down mix coefficient
1792 //write_lpcm_down_mix_coeff(&cl->lpcm_down_mix_coeff);
1799 // Extent start point
1800 return cl->extents.write();
1804 return cl->programs_ss.write();
1808 return cl->cpi_ss.write();
1812 fprintf(stderr, "write_clpi_extension(): unhandled extension %d.%d\n", id1, id2);
1819 if( write_header() ) return 1;
1820 if( clip.write() ) return 1;
1821 sequence_info_start_addr = bs.posb();
1822 if( sequences.write() ) return 1;
1823 program_info_start_addr = bs.posb();
1824 if( programs.write() ) return 1;
1825 cpi_start_addr = bs.posb();
1826 if( cpi.write() ) return 1;
1827 clip_mark_start_addr = bs.posb();
1828 if( cmrk.write() ) return 1;
1829 //if( has_ext_data ) {
1830 // ext_data_start_addr = bs.pos();
1831 // bdmv_write_extension_data(write_clpi_extension, this);
1839 bs.write(menu_call, 1);
1840 bs.write(title_search, 1);
1841 bs.write(chapter_search, 1);
1842 bs.write(time_search, 1);
1843 bs.write(skip_to_next_point, 1);
1844 bs.write(skip_to_prev_point, 1);
1845 bs.write(play_firstplay, 1);
1847 bs.write(pause_on, 1);
1848 bs.write(pause_off, 1);
1849 bs.write(still_off, 1);
1850 bs.write(forward, 1);
1851 bs.write(backward, 1);
1852 bs.write(resume, 1);
1853 bs.write(move_up, 1);
1854 bs.write(move_down, 1);
1855 bs.write(move_left, 1);
1856 bs.write(move_right, 1);
1857 bs.write(select, 1);
1858 bs.write(activate, 1);
1859 bs.write(select_and_activate, 1);
1860 bs.write(primary_audio_change, 1);
1862 bs.write(angle_change, 1);
1863 bs.write(popup_on, 1);
1864 bs.write(popup_off, 1);
1865 bs.write(pg_enable_disable, 1);
1866 bs.write(pg_change, 1);
1867 bs.write(secondary_video_enable_disable, 1);
1868 bs.write(secondary_video_change, 1);
1869 bs.write(secondary_audio_enable_disable, 1);
1870 bs.write(secondary_audio_change, 1);
1872 bs.write(pip_pg_change, 1);
1881 bs.pad(8); // Reserved
1882 bs.write(playback_type, 8);
1883 if (playback_type == BLURAY_PLAYBACK_TYPE_RANDOM ||
1884 playback_type == BLURAY_PLAYBACK_TYPE_SHUFFLE ) {
1885 bs.write(playback_count, 16);
1888 bs.pad(16); // Reserved
1891 bs.write(random_access_flag, 1);
1892 bs.write(audio_mix_flag, 1);
1893 bs.write(lossless_bypass_flag, 1);
1894 bs.pad(13); // Reserved
1900 mpls_pl::write_header()
1902 bs.writeb("MPLS", 4);
1903 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1904 bs.write(list_pos, 32);
1905 bs.write(mark_pos, 32);
1906 bs.write(ext_pos, 32);
1907 bs.pad(160); // Skip 160 reserved bits
1917 bs.write(stream_type, 8);
1918 switch (stream_type) {
1925 bs.write(subpath_id, 8);
1926 bs.write(subclip_id, 8);
1931 bs.write(subpath_id, 8);
1936 fprintf(stderr, "unrecognized mpls stream type %02x\n", stream_type);
1939 bs.padb(9 - strm.bs_posb(bs));
1943 bs.write(coding_type, 8);
1944 switch (coding_type) {
1945 case BLURAY_STREAM_TYPE_VIDEO_MPEG1:
1946 case BLURAY_STREAM_TYPE_VIDEO_MPEG2:
1947 case BLURAY_STREAM_TYPE_VIDEO_VC1:
1948 case BLURAY_STREAM_TYPE_VIDEO_H264:
1949 case BLURAY_STREAM_TYPE_VIDEO_HEVC:
1950 bs.write(format, 4);
1954 case BLURAY_STREAM_TYPE_AUDIO_MPEG1:
1955 case BLURAY_STREAM_TYPE_AUDIO_MPEG2:
1956 case BLURAY_STREAM_TYPE_AUDIO_LPCM:
1957 case BLURAY_STREAM_TYPE_AUDIO_AC3:
1958 case BLURAY_STREAM_TYPE_AUDIO_DTS:
1959 case BLURAY_STREAM_TYPE_AUDIO_TRUHD:
1960 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS:
1961 case BLURAY_STREAM_TYPE_AUDIO_DTSHD:
1962 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER:
1963 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY:
1964 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY:
1965 bs.write(format, 4);
1970 case BLURAY_STREAM_TYPE_SUB_PG:
1971 case BLURAY_STREAM_TYPE_SUB_IG:
1975 case BLURAY_STREAM_TYPE_SUB_TEXT:
1976 bs.write(char_code, 8);
1981 fprintf(stderr, "mpls_stream: unrecognized coding type %02x\n", coding_type);
1984 bs.padb(5 - code.bs_posb(bs));
1993 bs.pad(16); // Skip 2 reserved bytes
1995 bs.write(video.size(), 8);
1996 bs.write(audio.size(), 8);
1997 bs.write(pg.size() - num_pip_pg, 8);
1998 bs.write(ig.size(), 8);
1999 bs.write(secondary_audio.size(), 8);
2000 bs.write(secondary_video.size(), 8);
2001 bs.write(num_pip_pg, 8);
2006 // Primary Video Streams
2007 for( int ii=0; ii < video.size(); ++ii )
2008 if( video[ii]->write() ) return 1;
2010 // Primary Audio Streams
2011 for( int ii=0; ii < audio.size(); ++ii )
2012 if( audio[ii]->write() ) return 1;
2014 // Presentation Graphic Streams
2015 for( int ii=0; ii < pg.size(); ++ii )
2016 if( pg[ii]->write() ) return 1;
2018 // Interactive Graphic Streams
2019 for( int ii=0; ii < ig.size(); ++ii )
2020 if( ig[ii]->write() ) return 1;
2022 // Secondary Audio Streams
2023 for( int ii=0; ii < secondary_audio.size(); ++ii ) {
2024 if( secondary_audio[ii]->write() ) return 1;
2025 // Read Secondary Audio Extra Attributes
2026 bs.write(sa_primary_audio_ref.size(), 8);
2028 for( int jj=0; jj < sa_primary_audio_ref.size(); ++jj )
2029 bs.write(sa_primary_audio_ref[jj], 8);
2030 if( sa_primary_audio_ref.size() % 2) bs.pad(8 );
2033 // Secondary Video Streams
2034 for( int ii=0; ii < secondary_video.size(); ++ii ) {
2035 if( secondary_video[ii]->write() ) return 1;
2036 // Read Secondary Video Extra Attributes
2037 bs.write(sv_secondary_audio_ref.size(), 8);
2039 for( int jj=0; jj < sv_secondary_audio_ref.size(); ++jj )
2040 bs.write(sv_secondary_audio_ref[jj], 8);
2041 if( sv_secondary_audio_ref.size() % 2) bs.pad(8 );
2042 bs.write(sv_pip_pg_ref.size(), 8);
2044 for( int jj=0; jj < sv_pip_pg_ref.size(); ++jj )
2045 bs.write(sv_pip_pg_ref[jj], 8);
2046 if( sv_pip_pg_ref.size() % 2) bs.pad(8 );
2057 // Primary Clip identifer
2058 bs.writeb(clip[0]->clip_id, 5);
2059 bs.writeb(clip[0]->codec_id, 4); // "M2TS"
2060 bs.pad(11); // Skip reserved 11 bits
2062 bs.write(is_multi_angle, 1);
2063 bs.write(connection_condition, 4); // 0x01, 0x05, 0x06
2065 bs.write(clip[0]->stc_id, 8);
2066 bs.write(in_time, 32);
2067 bs.write(out_time, 32);
2070 bs.write(random_access_flag, 1);
2072 bs.write(still_mode, 8);
2073 if( still_mode == 0x01 ) {
2074 bs.write(still_time, 16);
2080 if( is_multi_angle ) {
2081 bs.write(clip.size(), 8);
2083 bs.write(is_different_audio, 1);
2084 bs.write(is_seamless_angle, 1);
2087 for( int ii=1; ii < clip.size(); ++ii ) {
2088 bs.writeb(clip[ii]->clip_id, 5);
2089 bs.writeb(clip[ii]->codec_id, 4); // "M2TS"
2090 bs.write(clip[ii]->stc_id, 8);
2093 if( stn.write() ) return 1;
2100 mpls_sub_pi::write()
2103 // Primary Clip identifer
2104 bs.writeb(clip[0]->clip_id, 5);
2105 bs.writeb(clip[0]->codec_id, 4); // "M2TS"
2108 bs.write(connection_condition, 4); // 0x01, 0x05, 0x06
2110 bs.write(is_multi_clip, 1);
2111 bs.write(clip[0]->stc_id, 8);
2112 bs.write(in_time, 32);
2113 bs.write(out_time, 32);
2114 bs.write(sync_play_item_id, 16);
2115 bs.write(sync_pts, 32);
2118 bs.write(clip.size(), 8);
2120 for( int ii=1; ii < clip.size(); ++ii ) {
2121 // Primary Clip identifer
2122 bs.writeb(clip[ii]->clip_id, 5);
2123 bs.writeb(clip[ii]->codec_id, 4); // "M2TS"
2124 bs.write(clip[ii]->stc_id, 8);
2138 bs.write(is_repeat, 1);
2140 bs.write(sub_play_item.size(), 8);
2142 for( int ii=0; ii < sub_play_item.size(); ++ii )
2143 if( sub_play_item[ii]->write() ) return 1;
2152 bs.write(mark_id, 8);
2153 bs.write(mark_type, 8);
2154 bs.write(play_item_ref, 16);
2156 bs.write(entry_es_pid, 16);
2157 bs.write(duration, 32);
2162 mpls_pl::write_playlistmark()
2164 mark.bs_len(bs, 32);
2165 // Then get the number of marks
2166 bs.write(play_mark.size(), 16);
2168 for( int ii=0; ii < play_mark.size(); ++ii )
2169 if( play_mark[ii]->write() ) return 1;
2176 mpls_pl::write_playlist()
2180 // Skip reserved bytes
2183 bs.write(play_item.size(), 16);
2184 bs.write(sub_path.size(), 16);
2186 for( int ii=0; ii < play_item.size(); ++ii )
2187 if( play_item[ii]->write() ) return 1;
2189 for( int ii=0; ii < sub_path.size(); ++ii )
2190 if( sub_path[ii]->write() ) return 1;
2197 mpls_pip_data::write()
2202 bs.write(scale_factor, 4);
2208 mpls_pip_metadata::write(uint32_t start_address)
2211 bs.write(clip_ref, 16);
2212 bs.write(secondary_video_ref, 8);
2214 bs.write(timeline_type, 4);
2215 bs.write(luma_key_flag, 1);
2216 bs.write(trick_play_flag, 1);
2218 if( luma_key_flag ) {
2220 bs.write(upper_limit_luma_key, 8);
2227 uint32_t data_address = 0; // XXX
2228 bs.write(data_address, 32);
2230 int64_t pos = bs.pos() / 8;
2231 bs.posb(start_address + data_address);
2233 bs.write(data.size(), 16);
2234 if( data.size() < 1 ) return 1;
2236 for( int ii=0; ii < data.size(); ++ii )
2237 if( data[ii]->write() ) return 1;
2244 mpls_pl::write_pip_metadata_extension()
2246 uint32_t pos = bs.posb();
2247 pipm.bs_len(bs, 32);
2249 bs.write(ext_pip_data.size(), 16);
2250 for( int ii=0; ii < ext_pip_data.size(); ++ii )
2251 if( ext_pip_data[ii]->write(pos) ) return 1;
2258 mpls_pl::write_subpath_extension()
2260 subx.bs_len(bs, 32);
2262 bs.write(ext_sub_path.size(), 16);
2263 for( int ii=0; ii < ext_sub_path.size(); ++ii )
2264 if( ext_sub_path[ii]->write() ) return 1;
2271 clpi_cl::write_mpls_extension(int id1, int id2, void *handle)
2273 mpls_pl *pl = (mpls_pl *) handle;
2277 // PiP metadata extension
2278 return pl->write_pip_metadata_extension();
2287 // SubPath entries extension
2288 return pl->write_subpath_extension();
2298 int ret = write_header();
2299 list_pos = bs.posb();;
2300 if( !ret ) ret = write_playlist();
2301 mark_pos = bs.posb();
2302 if( !ret ) ret = write_playlistmark();
2303 //if( has_pls_extension ) {
2304 // ext_pos = bs.posb();
2305 // bdmv_write_extension_data(write_mpls_extension, pl);
2312 if( !mkdir(path, 0777) )
2319 mk_bdmv_dir(char *bdmv_path)
2321 if( mk_dir(bdmv_path) )
2323 char bdjo_path[BCTEXTLEN];
2324 sprintf(bdjo_path, "%s/BDJO", bdmv_path);
2325 if( mk_dir(bdjo_path) )
2327 char clipinf_path[BCTEXTLEN];
2328 sprintf(clipinf_path, "%s/CLIPINF", bdmv_path);
2329 if( mk_dir(clipinf_path) )
2331 char jar_path[BCTEXTLEN];
2332 sprintf(jar_path, "%s/JAR", bdmv_path);
2333 if( mk_dir(jar_path) )
2335 char playlist_path[BCTEXTLEN];
2336 sprintf(playlist_path, "%s/PLAYLIST", bdmv_path);
2337 if( mk_dir(playlist_path) )
2345 char bdmv_path[BCTEXTLEN];
2346 sprintf(bdmv_path, "%s/BDMV", path);
2347 if( mk_bdmv_dir(bdmv_path) ) return 1;
2348 char cert_path[BCTEXTLEN];
2349 sprintf(cert_path, "%s/CERTIFICATE", path);
2350 if( mk_bdmv_dir(cert_path) ) return 1;
2351 char cert_backup[BCTEXTLEN];
2352 sprintf(cert_backup, "%s/BACKUP", cert_path);
2353 if( mk_bdmv_dir(cert_backup) ) return 1;
2354 char stream_path[BCTEXTLEN];
2355 sprintf(stream_path, "%s/STREAM", bdmv_path);
2356 if( mk_dir(stream_path) ) return 1;
2357 char auxdata_path[BCTEXTLEN];
2358 sprintf(auxdata_path, "%s/AUXDATA", bdmv_path);
2359 if( mk_dir(auxdata_path) ) return 1;
2360 char meta_path[BCTEXTLEN];
2361 sprintf(meta_path, "%s/META", bdmv_path);
2362 if( mk_dir(meta_path) ) return 1;
2363 char backup_path[BCTEXTLEN];
2364 sprintf(backup_path, "%s/BACKUP", bdmv_path);
2365 if( mk_bdmv_dir(backup_path) ) return 1;
2370 build_toc(clpi_ep_map_entry *map)
2372 clpi_ep_coarse *cp = 0;
2373 marks.sort(mark::cmpr);
2374 uint16_t ep_pid = map->pid;
2375 int64_t last_pts = -1, last_pkt = -1;
2376 for( int ii=0; ii<marks.size(); ++ii ) {
2377 mark *mp = marks[ii];
2378 if( mp->pid != ep_pid ) continue;
2379 int64_t pts = mp->pts;
2380 if( last_pts >= pts ) continue;
2382 uint32_t pkt = mp->pos / BLURAY_TS_PKTSZ;
2383 if( last_pkt >= pkt ) continue;
2385 int64_t coarse_pts = (pts >> 18); // & ~0x01;
2386 int64_t fine_pts = (pts & 0x7ffff) >> 8;
2387 uint32_t mpkt = pkt & ~0x1ffff;
2388 if( !cp || cp->pts_ep != coarse_pts || mpkt > cp->spn_ep ) {
2389 cp = new clpi_ep_coarse();
2390 map->coarse.append(cp);
2391 cp->ref_ep_fine_id = map->fine.size();
2392 cp->pts_ep = coarse_pts;
2395 clpi_ep_fine *fp = new clpi_ep_fine();
2396 map->fine.append(fp);
2397 fp->is_angle_change_point = 0;
2398 // XXX - dont know what this is
2399 fp->i_end_position_offset = 1;
2400 fp->pts_ep = fine_pts;
2401 fp->spn_ep = pkt & 0x1ffff;
2406 const AVRational media_info::clk45k = { 1, 45000 };
2408 static int bd_coding_type(AVCodecID codec_id)
2410 int coding_type = 0;
2412 case AV_CODEC_ID_MPEG1VIDEO:
2413 coding_type = BLURAY_STREAM_TYPE_VIDEO_MPEG1;
2415 case AV_CODEC_ID_MPEG2VIDEO:
2416 coding_type = BLURAY_STREAM_TYPE_VIDEO_MPEG2;
2418 case AV_CODEC_ID_H264:
2419 coding_type = BLURAY_STREAM_TYPE_VIDEO_H264;
2421 case AV_CODEC_ID_HEVC:
2422 coding_type = BLURAY_STREAM_TYPE_VIDEO_HEVC;
2424 case AV_CODEC_ID_MP2:
2425 coding_type = BLURAY_STREAM_TYPE_AUDIO_MPEG1;
2427 case AV_CODEC_ID_MP3:
2428 coding_type = BLURAY_STREAM_TYPE_AUDIO_MPEG2;
2430 case AV_CODEC_ID_AC3:
2431 coding_type = BLURAY_STREAM_TYPE_AUDIO_AC3;
2433 case AV_CODEC_ID_EAC3:
2434 coding_type = BLURAY_STREAM_TYPE_AUDIO_AC3PLUS;
2436 case AV_CODEC_ID_DTS:
2437 coding_type = BLURAY_STREAM_TYPE_AUDIO_DTS;
2439 case AV_CODEC_ID_TRUEHD:
2440 coding_type = BLURAY_STREAM_TYPE_AUDIO_TRUHD;
2442 case AV_CODEC_ID_PCM_BLURAY:
2443 coding_type = BLURAY_STREAM_TYPE_AUDIO_LPCM;
2445 case AV_CODEC_ID_HDMV_PGS_SUBTITLE:
2446 coding_type = BLURAY_STREAM_TYPE_SUB_PG;
2449 fprintf(stderr, "unknown bluray codec type %s\n", avcodec_get_name(codec_id));
2455 static int bd_audio_format(int channels)
2457 int audio_format = 0;
2458 switch( channels ) {
2460 audio_format = BLURAY_AUDIO_FORMAT_MONO;
2463 audio_format = BLURAY_AUDIO_FORMAT_STEREO;
2466 audio_format = BLURAY_AUDIO_FORMAT_MULTI_CHAN;
2469 fprintf(stderr, "unknown bluray audio format %d ch\n", channels);
2472 return audio_format;
2475 static int bd_audio_rate(int rate)
2479 case 48000: audio_rate = BLURAY_AUDIO_RATE_48; break;
2480 case 96000: audio_rate = BLURAY_AUDIO_RATE_96; break;
2481 case 192000: audio_rate = BLURAY_AUDIO_RATE_192; break;
2483 fprintf(stderr, "unknown bluray audio rate %d\n", rate);
2489 static int bd_video_format(int w, int h, int ilace)
2491 if( w == 720 && h == 480 && ilace ) return BLURAY_VIDEO_FORMAT_480I;
2492 if( w == 720 && h == 576 && ilace ) return BLURAY_VIDEO_FORMAT_576I;
2493 if( w == 720 && h == 480 && !ilace ) return BLURAY_VIDEO_FORMAT_480P;
2494 if( w == 720 && h == 576 && !ilace ) return BLURAY_VIDEO_FORMAT_576P;
2495 // this seems to be overly restrictive
2496 if( w == 1280 && h == 720 /* && !ilace*/ ) return BLURAY_VIDEO_FORMAT_720P;
2497 if( w == 1440 && h == 1080 /* && ilace*/ ) return BLURAY_VIDEO_FORMAT_1080I;
2498 if( w == 1920 && h == 1080 /* && !ilace*/ ) return BLURAY_VIDEO_FORMAT_1080P;
2499 if( w == 3840 && h == 2160 && !ilace ) return BLURAY_VIDEO_FORMAT_2160P;
2501 fprintf(stderr, "unknown bluray video format %dx%d %silace\n",
2502 w, h, !ilace ? "not " : "");
2506 static int bd_video_rate(double rate)
2508 if( fabs(rate-23.976) < 0.01 ) return BLURAY_VIDEO_RATE_24000_1001;
2509 if( fabs(rate-24.000) < 0.01 ) return BLURAY_VIDEO_RATE_24;
2510 if( fabs(rate-25.000) < 0.01 ) return BLURAY_VIDEO_RATE_25;
2511 if( fabs(rate-29.970) < 0.01 ) return BLURAY_VIDEO_RATE_30000_1001;
2512 if( fabs(rate-50.000) < 0.01 ) return BLURAY_VIDEO_RATE_50;
2513 if( fabs(rate-59.940) < 0.01 ) return BLURAY_VIDEO_RATE_60000_1001;
2514 fprintf(stderr, "unknown bluray video framerate %5.2f\n",rate);
2518 static int bd_aspect_ratio(int w, int h, double ratio)
2520 double aspect = (w * ratio) / h;
2521 if( fabs(aspect-1.333) < 0.01 ) return BLURAY_ASPECT_RATIO_4_3;
2522 if( fabs(aspect-1.777) < 0.01 ) return BLURAY_ASPECT_RATIO_16_9;
2523 return w == 720 ? BLURAY_ASPECT_RATIO_4_3 : BLURAY_ASPECT_RATIO_16_9;
2524 fprintf(stderr, "unknown bluray aspect ratio %5.3f\n",aspect);
2528 static int field_probe(AVFormatContext *fmt_ctx, AVStream *st)
2530 AVDictionary *copts = 0;
2531 //av_dict_copy(&copts, opts, 0);
2532 AVCodecID codec_id = st->codecpar->codec_id;
2533 #if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100)
2534 AVCodec *decoder = avcodec_find_decoder(codec_id);
2536 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
2537 const AVCodec *decoder = avcodec_find_decoder(codec_id);
2539 AVCodecContext *ctx = avcodec_alloc_context3(decoder);
2541 fprintf(stderr,"codec alloc failed\n");
2544 avcodec_parameters_to_context(ctx, st->codecpar);
2545 if( avcodec_open2(ctx, decoder, &copts) < 0 ) {
2546 fprintf(stderr,"codec open failed\n");
2549 av_dict_free(&copts);
2551 AVFrame *ipic = av_frame_alloc();
2553 av_init_packet(&ipkt);
2555 for( int retrys=100; --retrys>=0 && ilaced<0; ) {
2556 av_packet_unref(&ipkt);
2557 int ret = av_read_frame(fmt_ctx, &ipkt);
2558 if( ret == AVERROR_EOF ) break;
2559 if( ret != 0 ) continue;
2560 if( ipkt.stream_index != st->index ) continue;
2561 if( !ipkt.data || !ipkt.size ) continue;
2562 ret = avcodec_send_packet(ctx, &ipkt);
2564 fprintf(stderr, "avcodec_send_packet failed\n");
2567 ret = avcodec_receive_frame(ctx, ipic);
2569 ilaced = ipic->interlaced_frame ? 1 : 0;
2572 if( ret != AVERROR(EAGAIN) )
2573 fprintf(stderr, "avcodec_receive_frame failed %d\n", ret);
2575 av_packet_unref(&ipkt);
2576 av_frame_free(&ipic);
2577 avcodec_free_context(&ctx);
2581 int media_info::scan()
2584 if( stat(filename, &st) ) return 1;
2585 file_size = st.st_size;
2587 AVFormatContext *fmt_ctx = 0;
2588 AVDictionary *fopts = 0;
2589 av_dict_set(&fopts, "formatprobesize", "5000000", 0);
2590 av_dict_set(&fopts, "scan_all_pmts", "1", 0);
2591 av_dict_set(&fopts, "threads", "auto", 0);
2592 int ret = avformat_open_input(&fmt_ctx, filename, NULL, &fopts);
2593 av_dict_free(&fopts);
2594 if( ret < 0 ) return ret;
2595 ret = avformat_find_stream_info(fmt_ctx, NULL);
2597 bit_rate = fmt_ctx->bit_rate;
2600 for( int i=0; ret>=0 && i<(int)fmt_ctx->nb_streams; ++i ) {
2601 AVStream *st = fmt_ctx->streams[i];
2602 AVMediaType type = st->codecpar->codec_type;
2604 case AVMEDIA_TYPE_VIDEO: break;
2605 case AVMEDIA_TYPE_AUDIO: break;
2606 case AVMEDIA_TYPE_SUBTITLE: break;
2609 stream *s = new stream(type, i);
2611 AVCodecID codec_id = st->codecpar->codec_id;
2612 #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(59,16,100)
2613 const AVCodec *decoder = avcodec_find_decoder(codec_id);
2615 #if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58,134,100)
2616 AVCodec *decoder = avcodec_find_decoder(codec_id);
2619 s->ctx = avcodec_alloc_context3(decoder);
2621 fprintf(stderr, "avcodec_alloc_context failed\n");
2625 case AVMEDIA_TYPE_VIDEO: {
2626 if( ep_pid < 0 ) ep_pid = st->id;
2627 s->coding_type = bd_coding_type(codec_id);
2628 int ilace = field_probe(fmt_ctx, st);
2630 fprintf(stderr, "interlace probe failed\n");
2633 s->format = bd_video_format(st->codecpar->width, st->codecpar->height, ilace);
2634 AVRational framerate = av_guess_frame_rate(fmt_ctx, st, 0);
2635 s->rate = bd_video_rate(!framerate.den ? 0 : (double)framerate.num / framerate.den);
2636 s->aspect = bd_aspect_ratio(st->codecpar->width, st->codecpar->height,
2637 !st->sample_aspect_ratio.num || !st->sample_aspect_ratio.den ? 1. :
2638 (double)st->sample_aspect_ratio.num / st->sample_aspect_ratio.den);
2640 case AVMEDIA_TYPE_AUDIO: {
2641 s->coding_type = bd_coding_type(codec_id);
2642 s->format = bd_audio_format(st->codecpar->channels);
2643 s->rate = bd_audio_rate(st->codecpar->sample_rate);
2644 strcpy((char*)s->lang, "eng");
2646 case AVMEDIA_TYPE_SUBTITLE: {
2647 s->coding_type = bd_coding_type(codec_id);
2648 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", 0, 0);
2649 strncpy((char*)s->lang, lang ? lang->value : "und", sizeof(s->lang));
2654 if( bit_rate > 0 && st->duration == AV_NOPTS_VALUE &&
2655 st->time_base.num < INT64_MAX / bit_rate ) {
2656 st->duration = av_rescale(8*file_size, st->time_base.den,
2657 bit_rate * (int64_t) st->time_base.num);
2659 s->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2662 AVDictionary *copts = 0;
2663 ret = avcodec_open2(s->ctx, decoder, &copts);
2666 ep_pid = fmt_ctx->nb_streams > 0 ? fmt_ctx->streams[0]->id : 0;
2668 int npgm = fmt_ctx->nb_programs;
2670 program *pgm = new program(-1, 1);
2671 pgm->ep_pid = ep_pid;
2672 pgm->pmt_pid = 0x1000;
2673 pgm->pcr_pid = 0x1001;
2675 for( int jj=0; jj<streams.size(); ++jj ) {
2676 AVStream *st = fmt_ctx->streams[jj];
2677 AVMediaType type = st->codecpar->codec_type;
2679 case AVMEDIA_TYPE_VIDEO:
2680 case AVMEDIA_TYPE_AUDIO:
2685 pgm->strm_idx.append(jj);
2686 if( !pgm->duration || st->duration < pgm->duration )
2687 pgm->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2689 programs.append(pgm);
2692 for( int ii=0; ii<npgm; ++ii ) {
2693 AVProgram *pgrm = fmt_ctx->programs[ii];
2694 program *pgm = new program(ii, pgrm->id);
2695 pgm->pmt_pid = pgrm->pmt_pid;
2696 pgm->pcr_pid = pgrm->pcr_pid;
2699 for( int jj=0; jj<(int)pgrm->nb_stream_indexes; ++jj ) {
2700 int av_idx = pgrm->stream_index[jj];
2701 AVStream *st = fmt_ctx->streams[av_idx];
2702 AVMediaType type = st->codecpar->codec_type;
2704 case AVMEDIA_TYPE_VIDEO:
2705 if( ep_pid < 0 ) ep_pid = st->id;
2707 case AVMEDIA_TYPE_AUDIO:
2708 case AVMEDIA_TYPE_SUBTITLE:
2713 int sidx = streams.size();
2714 while( --sidx>=0 && streams[sidx]->av_idx!=av_idx );
2716 fprintf(stderr, "bad stream idx %d in pgm %d\n",av_idx, ii);
2719 if( !pgm->duration || st->duration < pgm->duration )
2720 pgm->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2721 pgm->strm_idx.append(sidx);
2724 AVProgram *pgrm = fmt_ctx->programs[0];
2725 ep_pid = pgrm->nb_stream_indexes > 0 ?
2726 fmt_ctx->streams[pgrm->stream_index[0]]->id : 0;
2728 pgm->ep_pid = ep_pid;
2729 programs.append(pgm);
2733 ret = scan(fmt_ctx);
2735 for( int i=0; i<(int)streams.size(); ++i )
2736 avcodec_close(streams[i]->ctx);
2737 avformat_close_input(&fmt_ctx);
2742 int media_info::scan(AVFormatContext *fmt_ctx)
2746 av_init_packet(&ipkt);
2748 // zero pts at pos zero
2749 for( int i=0; i<programs.size(); ++i ) {
2750 program *p = programs[i];
2751 for( int j=0; j<p->strm_idx.size(); ++j ) {
2752 stream *sp = streams[p->strm_idx[j]];
2754 AVStream *st = fmt_ctx->streams[sp->av_idx];
2755 p->add_label(0, 0, 0, st->id);
2759 for( int64_t count=0; ret>=0; ++count ) {
2760 av_packet_unref(&ipkt);
2761 ipkt.data = 0; ipkt.size = 0;
2763 ret = av_read_frame(fmt_ctx, &ipkt);
2765 if( ret == AVERROR_EOF ) break;
2769 if( !ipkt.data ) continue;
2770 if( (ipkt.flags & AV_PKT_FLAG_CORRUPT) ) continue;
2771 if( ipkt.pts == AV_NOPTS_VALUE ) continue;
2772 int i = ipkt.stream_index;
2773 if( i < 0 || i >= streams.size() ) continue;
2777 for( int ii=0; !pgm && ii<programs.size(); ++ii ) {
2778 program *p = programs[ii];
2779 for( int jj=0; jj<p->strm_idx.size(); ++jj ) {
2780 sp = streams[p->strm_idx[jj]];
2781 if( sp->av_idx == i ) { pgm = p; break; }
2784 if( !pgm ) continue;
2785 AVStream *st = fmt_ctx->streams[i];
2786 if( pgm->ep_pid != st->id ) continue;
2787 int64_t pts45k = av_rescale_q(ipkt.pts, st->time_base, clk45k);
2788 if( sp->start_pts > pts45k ) sp->start_pts = pts45k;
2789 if( sp->end_pts < pts45k ) sp->end_pts = pts45k;
2790 if( pgm->start_time > pts45k ) pgm->start_time = pts45k;
2791 if( pgm->end_time < pts45k ) pgm->end_time = pts45k;
2793 if( !(ipkt.flags & AV_PKT_FLAG_KEY) ) continue;
2795 if( sp->last_pts != pts45k ) {
2796 sp->last_pts = pts45k;
2797 pgm->add_label(count, ipkt.pos, pts45k, st->id);
2801 for( int ii=0; ii<programs.size(); ++ii ) {
2802 program *pgm = programs[ii];
2803 if( pgm->end_time > pgm->start_time )
2804 pgm->duration = pgm->end_time - pgm->start_time;
2807 return ret != AVERROR_EOF ? -1 : 0;
2811 Media::add_movie(uint32_t *ops, int n)
2813 movie_obj *mp = new movie_obj();
2814 mp->resume_intention_flag = 1;
2815 uint32_t *eop = ops + n/sizeof(*ops);
2816 while( ops < eop ) {
2817 command_obj *cmd = new command_obj();
2818 cmd->cmd = htobe32(*ops++);
2821 mp->cmds.append(cmd);
2823 mov.movies.append(mp);
2827 Media::compose(int ch_interval)
2833 int top_menu_obj = mov.movies.size();
2834 movie_obj *mp = new movie_obj();
2835 mp->resume_intention_flag = 1;
2836 command_obj *cmd = new command_obj();
2837 cmd->cmd = htobe32(0x21810000); cmd->dst = 1; cmd->src = 0;
2838 mp->cmds.append(cmd); // JUMP_TITLE 1
2839 mov.movies.append(mp);
2842 for( int ii=0; ii<size(); ++ii ) {
2843 mp = new movie_obj();
2844 mp->resume_intention_flag = 1;
2845 cmd = new command_obj();
2846 cmd->cmd = htobe32(0x22800000); cmd->dst = ii; cmd->src = 0;
2847 mp->cmds.append(cmd); // PLAY_PL ii
2848 cmd = new command_obj();
2849 cmd->cmd = htobe32(0x00020000); cmd->dst = 0; cmd->src = 0;
2850 mp->cmds.append(cmd);
2851 mov.movies.append(mp); // BREAK
2855 int first_play_obj = mov.movies.size();
2856 mp = new movie_obj();
2857 mp->resume_intention_flag = 1;
2858 cmd = new command_obj();
2859 cmd->cmd = htobe32(0x21810000); cmd->dst = 0; cmd->src = 0;
2860 mp->cmds.append(cmd); // JUMP_TITLE 0 ; top menu
2861 mov.movies.append(mp);
2865 idx.first_play.set_hdmv(first_play_obj, pb_typ_iactv);
2866 idx.top_menu.set_hdmv(top_menu_obj, pb_typ_iactv);
2870 for( int ii=0; ii<size(); ++ii ) {
2872 tp = new title_obj();
2873 tp->set_hdmv(idx.titles.size()+1, pb_typ_movie);
2874 idx.titles.append(tp);
2877 media_info *ip = get(ii);
2878 // clip program, if specified
2879 int pidx = ip->programs.size();
2880 while( --pidx>=0 && ip->programs[pidx]->pid != ip->pgm_pid );
2881 if( pidx < 0 ) pidx = 0;
2883 ip->pgm_pid = ip->prog()->pid;
2885 clpi_cl *cp = new clpi_cl();
2886 cp->clip.clip_stream_type = 1;
2887 cp->clip.application_type = BLURAY_APP_TYPE_MAIN_MOVIE;
2888 cp->clip.ts_recording_rate = ip->bit_rate;
2889 uint32_t ts_pkt_count = ip->file_size / BLURAY_TS_PKTSZ;
2890 cp->clip.num_source_packets = ts_pkt_count;
2891 cp->clip.ts_type_info.validity = 0x80;
2892 strcpy(cp->clip.ts_type_info.format_id, "HDMV");
2895 for( int jj=0; jj<ip->programs.size(); ++jj ) {
2896 program *pgm = ip->programs[jj];
2897 clpi_prog *p = new clpi_prog(pgm->pmt_pid);
2898 for( int kk=0; kk<pgm->strm_idx.size(); ++kk ) {
2899 int k = pgm->strm_idx[kk];
2900 stream *sp = ip->streams[k];
2901 clpi_prog_stream *s = new clpi_prog_stream();
2903 s->coding_type = sp->coding_type;
2904 s->format = sp->format;
2905 //use unspecified (0)
2906 // if( !idx.video_format ) idx.video_format = s->format;
2908 // if( !idx.frame_rate ) idx.frame_rate = s->rate;
2909 switch( sp->type ) {
2910 case AVMEDIA_TYPE_VIDEO:
2911 s->aspect = sp->aspect;
2913 case AVMEDIA_TYPE_AUDIO:
2914 case AVMEDIA_TYPE_SUBTITLE:
2915 memcpy(s->lang,sp->lang,sizeof(s->lang));
2920 p->streams.append(s);
2922 clpi_ep_map_entry *map = new clpi_ep_map_entry(pgm->ep_pid);
2923 map->ep_stream_type = 1;
2924 pgm->build_toc(map);
2925 cp->cpi.append(map);
2926 cp->programs.append(p);
2928 clpi_atc_seq *atc_seq = new clpi_atc_seq();
2929 clpi_stc_seq *stc_seq = new clpi_stc_seq();
2930 stc_seq->pcr_pid = pgm->pcr_pid;
2931 stc_seq->presentation_start_time = pgm->start_time;
2932 stc_seq->presentation_end_time = pgm->end_time;
2933 atc_seq->stc_seq.append(stc_seq);
2934 cp->sequences.append(atc_seq);
2939 if( ip->brk ) tp = 0;
2942 // playlists, one per title
2943 // one playitem per media clip
2945 for( int ii=0; ii<idx.titles.size(); ++ii ) {
2947 media_info *ip = get(clip_id);
2948 program *pgm = ip->prog();
2949 mpls_pl *pp = new mpls_pl();
2950 pp->app_info.playback_type = BLURAY_PLAYBACK_TYPE_SEQUENTIAL;
2951 // pp->app_info.uo_mask.xxx = 1;
2952 int last = idx.titles[ii]->last;
2953 for( ; clip_id<=last; ++clip_id ) {
2956 mpls_pi *pi = new mpls_pi();
2957 pi->connection_condition = 1; // seamless
2958 // pi->uo_mask.xxx = 1;
2959 pi->in_time = pgm->start_time;
2960 pi->out_time = pgm->end_time;
2962 pi->still_mode = BLURAY_STILL_INFINITE;
2963 int64_t end_time = pgm->start_time + pgm->duration;
2964 if( pi->out_time < end_time ) pi->out_time = end_time;
2965 mpls_clip *cp = new mpls_clip();
2966 sprintf(cp->clip_id,"%05d", clip_id);
2967 pi->clip.append(cp);
2968 for( int kk=0; kk<ip->streams.size(); ++kk ) {
2969 stream *sp = ip->streams[kk];
2970 switch( sp->type ) {
2971 case AVMEDIA_TYPE_VIDEO: break;
2972 case AVMEDIA_TYPE_AUDIO: break;
2973 case AVMEDIA_TYPE_SUBTITLE: break;
2976 mpls_stream *ps = new mpls_stream();
2978 ps->stream_type = BLURAY_PG_TEXTST_STREAM;
2979 ps->coding_type = sp->coding_type;
2980 ps->format = sp->format;
2981 ps->rate = sp->rate;
2982 switch( sp->type ) {
2983 case AVMEDIA_TYPE_VIDEO:
2984 pi->stn.video.append(ps);
2986 case AVMEDIA_TYPE_AUDIO:
2987 memcpy(ps->lang, sp->lang, sizeof(ps->lang));
2988 pi->stn.audio.append(ps);
2990 case AVMEDIA_TYPE_SUBTITLE:
2991 memcpy(ps->lang, sp->lang, sizeof(ps->lang));
2992 pi->stn.pg.append(ps);
2998 pp->play_item.append(pi);
3000 // chapter marks every ch_duration seconds * 45Kticks, default 5 min
3001 int PCR_FREQ = 45000;
3002 if (ch_interval == 0)
3004 int64_t ch_duration = PCR_FREQ * ch_interval;
3005 int64_t mrktm = ch_duration;
3007 int pmark = 0, pitem = 0;
3008 mpls_pi *pi = pp->play_item[pitem];
3009 mpls_plm *pm = new mpls_plm();
3010 pm->mark_id = pmark++;
3011 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
3012 pm->play_item_ref = pitem;
3013 pm->time = pi->in_time;
3014 pm->entry_es_pid = 0;
3015 pp->play_mark.append(pm);
3016 for( int jj=0; jj < pp->play_item.size(); ++jj ) {
3018 pi = pp->play_item[pitem];
3019 int64_t pi_duration = pi->out_time - pi->in_time;
3020 int64_t endtm = plytm + pi_duration;
3021 while( mrktm < endtm ) {
3022 pm = new mpls_plm();
3023 pm->mark_id = pmark++;
3024 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
3025 pm->play_item_ref = pitem;
3026 pm->time = pi->in_time + mrktm - plytm;
3027 pm->entry_es_pid = 0;
3028 pp->play_mark.append(pm);
3029 mrktm += ch_duration;
3033 pm = new mpls_plm();
3034 pm->mark_id = pmark;
3035 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
3036 pm->play_item_ref = pitem;
3037 pm->time = pi->out_time;
3038 pm->entry_es_pid = 0;
3039 pp->play_mark.append(pm);
3047 bd_path(const char *bp, const char *fmt, va_list ap)
3049 int n = sizeof(filename)-1;
3050 char *cp = filename;
3051 const char *pp = path;
3052 while( n>0 && (*cp=*pp)!=0 ) { --n; ++cp; ++pp; }
3053 while( n>0 && (*cp=*bp)!=0 ) { --n; ++cp; ++bp; }
3054 n -= vsnprintf(cp, n, fmt, ap);
3056 return n > 0 ? 0 : 1;
3060 bd_copy(const char *ifn, const char *fmt, ...)
3062 int bfrsz = 0x40000, ret = 1;
3064 FILE *ifp = fopen(ifn,"r");
3068 if( bd_path("/BDMV/", fmt, ap) ) return 1;
3070 FILE *ofp = fopen(filename,"w");
3072 setvbuf(ifp, 0, _IOFBF, 0x80000);
3073 setvbuf(ofp, 0, _IOFBF, 0x80000);
3076 while( !ret && n >= bfrsz ) {
3077 n = fread(bfr,1,bfrsz,ifp);
3078 if( n > 0 && (int)fwrite(bfr,1,n,ofp) != n ) {
3079 fprintf(stderr, "cant write: %s\n",filename);
3084 fprintf(stderr, "read error: %s = %m\n",ifn);
3088 fprintf(stderr, "write error: %s = %m\n",filename);
3092 fprintf(stderr, "close error: %s = %m\n",filename);
3099 fprintf(stderr, "cant copy clip %s\n",ifn);
3104 bd_open(const char *fmt, ...)
3107 if( !path ) return 0;
3110 if( bd_path("/BDMV/", fmt, ap) ) return 1;
3112 if( bs.open(filename) ) {
3113 fprintf(stderr, "cant open file %s\n",filename);
3120 bd_backup(const char *fmt, ...)
3123 if( !path ) return 0;
3126 FILE *ifp = fopen(filename,"r");
3130 if( bd_path("/BDMV/BACKUP/", fmt, ap) ) return 1;
3132 FILE *ofp = fopen(filename,"w");
3134 while( (n=fread(bfr,1,sizeof(bfr),ifp)) > 0 ) fwrite(bfr,1,n,ofp);
3141 fprintf(stderr, "cant backup %s\n",filename);
3145 int Media::write(char *fn)
3149 if( bd_open("index.bdmv") ) return 1;
3150 if( idx.write() ) return 1;
3151 if( bd_backup("index.bdmv") ) return 1;
3153 if( bd_open("MovieObject.bdmv") ) return 1;
3154 if( mov.write() ) return 1;
3155 if( bd_backup("MovieObject.bdmv") ) return 1;
3157 for( int ii=0; ii<cl.size(); ++ii ) {
3158 if( bd_open("CLIPINF/%05d.clpi", ii) ) return 1;
3159 if( cl[ii]->write() ) return 1;
3160 if( bd_backup("CLIPINF/%05d.clpi", ii) ) return 1;
3163 for( int ii=0; ii<pl.size(); ++ii ) {
3164 if( bd_open("PLAYLIST/%05d.mpls", ii) ) return 1;
3165 if( pl[ii]->write() ) return 1;
3166 if( bd_backup("PLAYLIST/%05d.mpls", ii) ) return 1;
3172 main(int ac, char **av)
3176 av_log_set_level(AV_LOG_FATAL);
3177 //av_log_set_level(AV_LOG_VERBOSE);
3178 //av_log_set_level(AV_LOG_DEBUG);
3181 int start = 0, chapter_every_n_sec = 0;
3183 int opt = getopt(ac, av, "c:");
3185 chapter_every_n_sec = atoi(optarg); start = 4;
3186 path = av[3]; printf("Chapter interval: %i \n", chapter_every_n_sec );}
3189 if( mkbdmv(path) ) return 1;
3190 for( int ii=start; ii<ac; ++ii ) {
3192 // any dash seq followed by number sets curr title pgm_pid
3193 // single dash only sets title pgm_pid
3194 // double dash ends curr title, starts a new title
3195 // triple dash ends curr title as infinite still
3198 if( *++ap == '-' ) {
3200 if( *++ap == '-' ) { ++ap; mp->still = 1; }
3202 if( *ap >= '0' && *ap <= '9' )
3203 mp->pgm_pid = strtoul(ap,&ap,0);
3204 if( mp->brk ) mp = 0;
3206 fprintf(stderr, "err arg %d: %s\n",ii,av[ii]);
3211 mp = new media_info(av[ii]);
3214 fprintf(stderr, "cant scan media: %s\n", av[ii]);
3219 if( mp ) mp->brk = 1;
3221 if( media.compose(chapter_every_n_sec) ) {
3222 fprintf(stderr, "cant compose media\n");
3225 if( media.write(0) ) {
3226 fprintf(stderr, "cant prepare media\n");
3229 if( media.write(path) ) {
3230 fprintf(stderr, "cant write media\n");
3234 for( int ii=0; ii<media.size(); ++ii )
3235 if( media.bd_copy(media[ii]->filename, "STREAM/%05d.m2ts", ii) )