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/.
44 // work arounds (centos)
46 #define INT64_MAX 9223372036854775807LL
49 #define INT64_MIN (-INT64_MAX-1)
52 #include "arraylist.h"
54 #define BCTEXTLEN 1024
55 #define BLURAY_TS_PKTSZ 192L
57 static const int bd_sig = 2;
60 #include "libavfilter/buffersrc.h"
61 #include "libavfilter/buffersink.h"
62 #include "libavformat/avformat.h"
63 #include "libavformat/avio.h"
64 #include "libavcodec/avcodec.h"
65 #include "libavfilter/avfilter.h"
66 #include "libavutil/avutil.h"
67 #include "libavutil/opt.h"
68 #include "libavutil/pixdesc.h"
69 #include "libswresample/swresample.h"
70 #include "libswscale/swscale.h"
83 BLURAY_APP_TYPE_MAIN_MOVIE = 1,
84 BLURAY_APP_TYPE_MAIN_TIMED_SLIDE_SHOW = 2,
85 BLURAY_APP_TYPE_MAIN_BROWSED_SLIDE_SHOW = 3,
86 BLURAY_APP_TYPE_SUBPATH_BROWSED_SLIDE_SHOW = 4,
87 BLURAY_APP_TYPE_SUBPATH_IG_MENU = 5,
88 BLURAY_APP_TYPE_SUBPATH_TEXT_SUBTITLE = 6,
89 BLURAY_APP_TYPE_SUBPATH_ELEMENTARY_STREAM = 7,
91 BLURAY_STREAM_TYPE_VIDEO_MPEG1 = 0x01,
92 BLURAY_STREAM_TYPE_VIDEO_MPEG2 = 0x02,
93 BLURAY_STREAM_TYPE_AUDIO_MPEG1 = 0x03,
94 BLURAY_STREAM_TYPE_AUDIO_MPEG2 = 0x04,
95 BLURAY_STREAM_TYPE_AUDIO_LPCM = 0x80,
96 BLURAY_STREAM_TYPE_AUDIO_AC3 = 0x81,
97 BLURAY_STREAM_TYPE_AUDIO_DTS = 0x82,
98 BLURAY_STREAM_TYPE_AUDIO_TRUHD = 0x83,
99 BLURAY_STREAM_TYPE_AUDIO_AC3PLUS = 0x84,
100 BLURAY_STREAM_TYPE_AUDIO_DTSHD = 0x85,
101 BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER = 0x86,
102 BLURAY_STREAM_TYPE_VIDEO_VC1 = 0xea,
103 BLURAY_STREAM_TYPE_VIDEO_H264 = 0x1b,
104 BLURAY_STREAM_TYPE_VIDEO_H264_MVC = 0x20,
105 BLURAY_STREAM_TYPE_SUB_PG = 0x90,
106 BLURAY_STREAM_TYPE_SUB_IG = 0x91,
107 BLURAY_STREAM_TYPE_SUB_TEXT = 0x92,
108 BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY = 0xa1,
109 BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY = 0xa2,
111 BLURAY_MARK_TYPE_ENTRY = 0x01,
112 BLURAY_MARK_TYPE_LINK = 0x02,
114 BLURAY_PLAYBACK_TYPE_SEQUENTIAL = 1,
115 BLURAY_PLAYBACK_TYPE_RANDOM = 2,
116 BLURAY_PLAYBACK_TYPE_SHUFFLE = 3,
118 BLURAY_VIDEO_FORMAT_480I = 1, // ITU-R BT.601-5
119 BLURAY_VIDEO_FORMAT_576I = 2, // ITU-R BT.601-4
120 BLURAY_VIDEO_FORMAT_480P = 3, // SMPTE 293M
121 BLURAY_VIDEO_FORMAT_1080I = 4, // SMPTE 274M
122 BLURAY_VIDEO_FORMAT_720P = 5, // SMPTE 296M
123 BLURAY_VIDEO_FORMAT_1080P = 6, // SMPTE 274M
124 BLURAY_VIDEO_FORMAT_576P = 7, // ITU-R BT.1358
126 BLURAY_VIDEO_RATE_24000_1001 = 1, // 23.976
127 BLURAY_VIDEO_RATE_24 = 2,
128 BLURAY_VIDEO_RATE_25 = 3,
129 BLURAY_VIDEO_RATE_30000_1001 = 4, // 29.97
130 BLURAY_VIDEO_RATE_50 = 6,
131 BLURAY_VIDEO_RATE_60000_1001 = 7, // 59.94
133 BLURAY_ASPECT_RATIO_4_3 = 2,
134 BLURAY_ASPECT_RATIO_16_9 = 3,
136 BLURAY_AUDIO_FORMAT_MONO = 1,
137 BLURAY_AUDIO_FORMAT_STEREO = 3,
138 BLURAY_AUDIO_FORMAT_MULTI_CHAN = 6,
139 BLURAY_AUDIO_FORMAT_COMBO = 12, // Stereo ac3/dts,
141 BLURAY_AUDIO_RATE_48 = 1,
142 BLURAY_AUDIO_RATE_96 = 4,
143 BLURAY_AUDIO_RATE_192 = 5,
144 BLURAY_AUDIO_RATE_192_COMBO = 12, // 48 or 96 ac3/dts, 192 mpl/dts-hd
145 BLURAY_AUDIO_RATE_96_COMBO = 14, // 48 ac3/dts, 96 mpl/dts-hd
146 BLURAY_TEXT_CHAR_CODE_UTF8 = 0x01,
147 BLURAY_TEXT_CHAR_CODE_UTF16BE = 0x02,
148 BLURAY_TEXT_CHAR_CODE_SHIFT_JIS = 0x03,
149 BLURAY_TEXT_CHAR_CODE_EUC_KR = 0x04,
150 BLURAY_TEXT_CHAR_CODE_GB18030_20001 = 0x05,
151 BLURAY_TEXT_CHAR_CODE_CN_GB = 0x06,
152 BLURAY_TEXT_CHAR_CODE_BIG5 = 0x07,
154 BLURAY_STILL_NONE = 0x00,
155 BLURAY_STILL_TIME = 0x01,
156 BLURAY_STILL_INFINITE = 0x02,
158 BLURAY_PG_TEXTST_STREAM = 0x01,
159 BLURAY_SECONDARY_VIDEO_STREAM = 0x02,
160 BLURAY_SECONDARY_AUDIO_STREAM = 0x03,
168 bs_file() { fp = 0; }
172 int open(const char *fn);
174 void write(uint32_t v, int n);
176 void padb(int n) { pad(n*8); }
177 int64_t posb() { return fpos; }
178 void posb(int64_t n);
179 int64_t pos() { return posb()*8 + len; }
180 void writeb(uint8_t * bp, int n);
181 void writeb(const char *cp, int n) {
182 writeb((uint8_t *) cp, n);
189 bs_length() { fpos = len = 0; }
190 int64_t bs_posb(bs_file &bs) { return bs.posb() - fpos; }
191 void bs_len(bs_file &bs, int n) {
192 bs.write(len, n); fpos = bs.posb();
194 void bs_end(bs_file &bs) {
197 void bs_ofs(bs_file &bs, int n) {
198 bs.write(fpos-n/8, n);
200 void bs_zofs(bs_file &bs, int n) {
201 bs.write(!len ? 0 : fpos-n/8, n);
205 class _bd_stream_info {
216 _bd_stream_info() { memset(this, 0, sizeof(*this)); }
217 ~_bd_stream_info() {}
220 class bd_stream_info : public _bd_stream_info {
226 class _bd_clip_info {
230 uint16_t still_time; /* seconds */
235 _bd_clip_info() { memset(this, 0, sizeof(*this)); }
239 class bd_clip_info : public _bd_clip_info {
241 ArrayList<bd_stream_info *> video_streams;
242 ArrayList<bd_stream_info *> audio_streams;
243 ArrayList<bd_stream_info *> pg_streams;
244 ArrayList<bd_stream_info *> ig_streams;
245 ArrayList<bd_stream_info *> sec_audio_streams;
246 ArrayList<bd_stream_info *> sec_video_streams;
250 video_streams.remove_all_objects();
251 audio_streams.remove_all_objects();
252 pg_streams.remove_all_objects();
253 ig_streams.remove_all_objects();
254 sec_audio_streams.remove_all_objects();
255 sec_video_streams.remove_all_objects();
259 class _bd_title_chapter {
267 _bd_title_chapter() { memset(this, 0, sizeof(*this)); }
268 ~_bd_title_chapter() {}
271 class bd_title_chapter : public _bd_title_chapter {
273 bd_title_chapter() {}
274 ~bd_title_chapter() {}
277 class _bd_title_mark {
286 _bd_title_mark() { memset(this, 0, sizeof(*this)); }
290 class bd_title_mark : public _bd_title_mark {
296 class _bd_title_info {
303 _bd_title_info() { memset(this, 0, sizeof(*this)); }
307 class bd_title_info : public _bd_title_info {
309 ArrayList<bd_clip_info *> clips;
310 ArrayList<bd_title_chapter *> chapters;
311 ArrayList<bd_title_mark *> marks;
315 clips.remove_all_objects();
316 chapters.remove_all_objects();
317 marks.remove_all_objects();
321 class _clpi_stc_seq {
324 uint32_t spn_stc_start;
325 uint32_t presentation_start_time;
326 uint32_t presentation_end_time;
328 _clpi_stc_seq() { memset(this, 0, sizeof(*this)); }
332 class clpi_stc_seq : public _clpi_stc_seq {
340 class _clpi_atc_seq {
342 uint32_t spn_atc_start;
343 uint8_t offset_stc_id;
345 _clpi_atc_seq() { memset(this, 0, sizeof(*this)); }
349 class clpi_atc_seq : public _clpi_atc_seq {
351 ArrayList<clpi_stc_seq *> stc_seq;
356 stc_seq.remove_all_objects();
360 class clpi_sequences : public bs_length,
361 public ArrayList<clpi_atc_seq *> {
366 ~clpi_sequences() { remove_all_objects(); }
369 class _clpi_ts_type {
374 _clpi_ts_type() { memset(this, 0, sizeof(*this)); }
378 class clpi_ts_type : public _clpi_ts_type {
384 class _clpi_atc_delta {
390 _clpi_atc_delta() { memset(this, 0, sizeof(*this)); }
391 ~_clpi_atc_delta() {}
394 class clpi_atc_delta : public _clpi_atc_delta {
406 _clpi_font() { memset(this, 0, sizeof(*this)); }
410 class clpi_font : public _clpi_font {
416 class clpi_font_info {
418 ArrayList<clpi_font *> font;
422 font.remove_all_objects();
426 class _clpi_clip_info {
428 uint8_t clip_stream_type;
429 uint8_t application_type;
430 uint8_t is_atc_delta;
431 uint32_t ts_recording_rate;
432 uint32_t num_source_packets;
434 _clpi_clip_info() { memset(this, 0, sizeof(*this)); }
435 ~_clpi_clip_info() {}
438 class clpi_clip_info : public bs_length, public _clpi_clip_info {
440 clpi_ts_type ts_type_info;
441 clpi_font_info font_info;
442 ArrayList<clpi_atc_delta *> atc_delta;
447 atc_delta.remove_all_objects();
451 class _clpi_prog_stream {
462 _clpi_prog_stream() { memset(this, 0, sizeof(*this)); }
463 ~_clpi_prog_stream() {}
466 class clpi_prog_stream : public bs_length, public _clpi_prog_stream {
470 clpi_prog_stream() {}
471 ~clpi_prog_stream() {}
474 class _clpi_ep_coarse {
480 _clpi_ep_coarse() { memset(this, 0, sizeof(*this)); }
481 ~_clpi_ep_coarse() {}
484 class clpi_ep_coarse : public _clpi_ep_coarse {
492 class _clpi_ep_fine {
494 uint8_t is_angle_change_point;
495 uint8_t i_end_position_offset;
499 _clpi_ep_fine() { memset(this, 0, sizeof(*this)); }
503 class clpi_ep_fine : public _clpi_ep_fine {
511 class _clpi_ep_map_entry {
513 uint8_t ep_stream_type;
514 uint32_t ep_map_stream_start_addr;
516 _clpi_ep_map_entry() { memset(this, 0, sizeof(*this)); }
517 ~_clpi_ep_map_entry() {}
520 class clpi_ep_map_entry : public _clpi_ep_map_entry {
524 ArrayList<clpi_ep_coarse *> coarse;
525 ArrayList<clpi_ep_fine *> fine;
526 int write(uint32_t ep_map_pos);
529 clpi_ep_map_entry(int id) { fine_start = 0; pid = id; }
530 ~clpi_ep_map_entry() {
531 coarse.remove_all_objects();
532 fine.remove_all_objects();
538 uint32_t spn_program_sequence_start;
540 _clpi_prog() { memset(this, 0, sizeof(*this)); }
544 class clpi_prog : public _clpi_prog {
546 ArrayList<clpi_prog_stream *> streams;
547 uint16_t program_map_pid;
550 clpi_prog(int pmt_pid) { program_map_pid = pmt_pid; }
551 ~clpi_prog() { streams.remove_all_objects(); }
554 class clpi_programs : public bs_length,
555 public ArrayList<clpi_prog *> {
560 ~clpi_programs() { remove_all_objects(); }
563 class clpi_extents : public bs_length,
564 public ArrayList<uint32_t> {
575 _clpi_cpi() { type = 0; }
579 class clpi_cpi : public bs_length, public _clpi_cpi,
580 public ArrayList<clpi_ep_map_entry *> {
585 ~clpi_cpi() { remove_all_objects(); }
588 class clpi_cmrk : public bs_length {
599 unsigned int menu_call : 1;
600 unsigned int title_search : 1;
601 unsigned int chapter_search : 1;
602 unsigned int time_search : 1;
603 unsigned int skip_to_next_point : 1;
604 unsigned int skip_to_prev_point : 1;
605 unsigned int play_firstplay : 1;
606 unsigned int stop : 1;
607 unsigned int pause_on : 1;
608 unsigned int pause_off : 1;
609 unsigned int still_off : 1;
610 unsigned int forward : 1;
611 unsigned int backward : 1;
612 unsigned int resume : 1;
613 unsigned int move_up : 1;
614 unsigned int move_down : 1;
615 unsigned int move_left : 1;
616 unsigned int move_right : 1;
617 unsigned int select : 1;
618 unsigned int activate : 1;
619 unsigned int select_and_activate : 1;
620 unsigned int primary_audio_change : 1;
621 unsigned int reserved0 : 1;
622 unsigned int angle_change : 1;
623 unsigned int popup_on : 1;
624 unsigned int popup_off : 1;
625 unsigned int pg_enable_disable : 1;
626 unsigned int pg_change : 1;
627 unsigned int secondary_video_enable_disable : 1;
628 unsigned int secondary_video_change : 1;
629 unsigned int secondary_audio_enable_disable : 1;
630 unsigned int secondary_audio_change : 1;
631 unsigned int reserved1 : 1;
632 unsigned int pip_pg_change : 1;
636 memset(this, 0, sizeof(*this));
654 _mpls_stream() { memset(this, 0, sizeof(*this)); }
658 class mpls_stream : public _mpls_stream {
660 bs_length strm, code;
676 class mpls_stn : public bs_length, public _mpls_stn {
678 ArrayList<mpls_stream *> video;
679 ArrayList<mpls_stream *> audio;
680 ArrayList<mpls_stream *> pg;
681 ArrayList<mpls_stream *> ig;
682 ArrayList<mpls_stream *> secondary_audio;
683 ArrayList<mpls_stream *> secondary_video;
684 // Secondary audio specific fields
685 ArrayList<uint8_t> sa_primary_audio_ref;
686 // Secondary video specific fields
687 ArrayList<uint8_t> sv_secondary_audio_ref;
688 ArrayList<uint8_t> sv_pip_pg_ref;
693 video.remove_all_objects();
694 audio.remove_all_objects();
695 pg.remove_all_objects();
696 ig.remove_all_objects();
697 secondary_audio.remove_all_objects();
698 secondary_video.remove_all_objects();
708 _mpls_clip() { memset(this, 0, sizeof(*this)); }
712 class mpls_clip : public _mpls_clip {
714 mpls_clip() { strcpy(codec_id, "M2TS"); }
720 uint8_t is_multi_angle;
721 uint8_t connection_condition;
724 uint8_t random_access_flag;
727 uint8_t is_different_audio;
728 uint8_t is_seamless_angle;
730 _mpls_pi() { memset(this, 0, sizeof(*this)); }
734 class mpls_pi : public bs_length, public _mpls_pi {
737 ArrayList<mpls_clip *> clip;
744 clip.remove_all_objects();
752 uint16_t play_item_ref;
754 uint16_t entry_es_pid;
757 _mpls_plm() { memset(this, 0, sizeof(*this)); }
761 class mpls_plm : public _mpls_plm {
771 uint8_t playback_type;
772 uint16_t playback_count;
773 uint8_t random_access_flag;
774 uint8_t audio_mix_flag;
775 uint8_t lossless_bypass_flag;
777 _mpls_ai() { memset(this, 0, sizeof(*this)); }
781 class mpls_ai : public bs_length, public _mpls_ai {
792 uint8_t connection_condition;
793 uint8_t is_multi_clip;
796 uint16_t sync_play_item_id;
799 _mpls_sub_pi() { memset(this, 0, sizeof(*this)); }
803 class mpls_sub_pi : public bs_length, public _mpls_sub_pi {
805 ArrayList<mpls_clip *> clip;
810 clip.remove_all_objects();
819 _mpls_sub() { memset(this, 0, sizeof(*this)); }
823 class mpls_sub : public bs_length, public _mpls_sub {
825 ArrayList<mpls_sub_pi *> sub_play_item;
830 sub_play_item.remove_all_objects();
834 class _mpls_pip_data {
839 uint8_t scale_factor;
841 _mpls_pip_data() { memset(this, 0, sizeof(*this)); }
845 class mpls_pip_data : public _mpls_pip_data {
853 class _mpls_pip_metadata {
856 uint8_t secondary_video_ref;
857 uint8_t timeline_type;
858 uint8_t luma_key_flag;
859 uint8_t upper_limit_luma_key;
860 uint8_t trick_play_flag;
862 _mpls_pip_metadata() { memset(this, 0, sizeof(*this)); }
863 ~_mpls_pip_metadata() {}
866 class mpls_pip_metadata : public _mpls_pip_metadata {
868 ArrayList<mpls_pip_data *> data;
870 int write(uint32_t start_address);
871 mpls_pip_metadata() {}
872 ~mpls_pip_metadata() {
873 data.remove_all_objects();
883 _mpls_pl() { memset(this, 0, sizeof(*this)); }
887 class mpls_pl : public _mpls_pl {
890 bs_length play, mark, subx, pipm;
892 ArrayList<mpls_pi *> play_item;
893 ArrayList<mpls_sub *> sub_path;
894 ArrayList<mpls_plm *> play_mark;
895 // extension data (profile 5, version 2.4)
896 ArrayList<mpls_sub *> ext_sub_path;
897 // extension data (Picture-In-Picture metadata)
898 ArrayList<mpls_pip_metadata *> ext_pip_data;
901 int write_playlist();
902 int write_playlistmark();
903 int write_subpath_extension();
904 int write_pip_metadata_extension();
907 mpls_pl() { sig = bd_sig; }
909 play_item.remove_all_objects();
910 sub_path.remove_all_objects();
911 play_mark.remove_all_objects();
912 ext_sub_path.remove_all_objects();
913 ext_pip_data.remove_all_objects();
919 uint32_t sequence_info_start_addr;
920 uint32_t program_info_start_addr;
921 uint32_t cpi_start_addr;
922 uint32_t clip_mark_start_addr;
923 uint32_t ext_data_start_addr;
925 _clpi_cl() { memset(this, 0, sizeof(*this)); }
929 class clpi_cl : public _clpi_cl {
933 clpi_sequences sequences;
934 clpi_programs programs;
936 clpi_extents extents;
937 clpi_programs programs_ss;
944 int write_clpi_extension(int id1, int id2, void *handle);
945 int write_mpls_extension(int id1, int id2, void *handle);
947 clpi_cl() { sig = bd_sig; }
956 uint32_t sub_grp:3; /* command sub-group */
957 uint32_t grp:2; /* command group */
958 uint32_t op_cnt:3; /* operand count */
959 uint32_t branch_opt:4; /* branch option */
961 uint32_t imm_op2:1; /* I-flag for operand 2 */
962 uint32_t imm_op1:1; /* I-flag for operand 1 */
963 uint32_t cmp_opt:4; /* compare option */
965 uint32_t set_opt:5; /* set option */
972 command_obj() { cmd = dst = src = 0; }
978 int resume_intention_flag;
980 int title_search_mask;
982 _movie_obj() { memset(this, 0, sizeof(*this)); }
986 class movie_obj : public _movie_obj {
988 ArrayList<command_obj *> cmds;
992 cmds.remove_all_objects();
996 class movie_file : public bs_length {
999 ArrayList<movie_obj *> movies;
1001 movie_file() { sig = bd_sig; }
1003 movies.remove_all_objects();
1015 _pb_obj() { memset(this, 0, sizeof(*this)); }
1019 class pb_obj : public _pb_obj {
1033 void set_hdmv(int id, int pt);
1034 void set_bdj(char *nm, int pt);
1035 void write_hdmv_obj(int id_ref);
1036 void write_bdj_obj(char *name);
1042 _title_obj() { acc_typ = 0; }
1046 class title_obj : public pb_obj, public _title_obj {
1057 int initial_output_mode_preference;
1058 int content_exist_flag;
1062 _index_file() { memset(this, 0, sizeof(*this)); }
1066 class index_file : public bs_length, public _index_file {
1068 ArrayList<title_obj *> titles;
1072 bs_length exten, appinf;
1074 void write_hdmv_obj(int hdmv_typ, int id_ref);
1075 void write_bdj_obj(int bdj_typ, char *name);
1076 void write_pb_obj(pb_obj * pb);
1081 memset(user_data, 0, sizeof(user_data));
1084 titles.remove_all_objects();
1090 uint32_t data_start;
1091 uint32_t extension_data_start;
1095 _bdid() { memset(this, 0, sizeof(*this)); }
1099 class bdid : public _bdid {
1104 bdid() { sig = bd_sig; }
1108 class stream : public bd_stream_info {
1118 stream(AVMediaType ty, int i) {
1119 type = ty; av_idx = i;
1120 start_pts = INT64_MAX; end_pts = INT64_MIN;
1128 static int cmpr(mark **, mark **);
1129 int64_t sample, pos, pts;
1131 mark(int64_t s, int64_t p, int64_t ts, uint32_t pk, int id) {
1132 sample = s; pos = p; pts = ts; pkt = pk; pid = id;
1136 int mark::cmpr(mark **a, mark **b)
1138 mark *ap = *(mark **)a, *bp = *(mark **)b;
1139 return ap->pts > bp->pts ? 1 : ap->pts < bp->pts ? -1 : 0;
1144 int pmt_pid, pcr_pid, ep_pid;
1145 int64_t start_time, end_time;
1148 memset(this, 0, sizeof(*this));
1149 start_time = INT64_MAX; end_time = INT64_MIN;
1154 class program : public _program {
1158 ArrayList<int> strm_idx;
1159 ArrayList<mark*> marks;
1161 int build_toc(clpi_ep_map_entry *map);
1162 void add_label(uint32_t pk, int64_t p, int64_t ts, int id) {
1163 marks.append(new mark(curr_pos, p, ts, pk, id));
1166 program(int i, int id) { idx = i; pid = id; curr_pos = 0; }
1167 ~program() { marks.remove_all_objects(); }
1176 memset(this, 0, sizeof(*this));
1181 class media_info : public _media_info {
1183 static const AVRational clk45k;
1184 char filename[BCTEXTLEN];
1186 int brk, pidx, still;
1187 ArrayList<stream *> streams;
1188 ArrayList<program *> programs;
1189 program *prog() { return programs[pidx]; }
1191 media_info(const char *fn) {
1192 strcpy(filename, fn);
1193 brk = 0; pidx = 0; pgm_pid = -1; still = 0;
1196 streams.remove_all_objects();
1197 programs.remove_all_objects();
1201 int scan(AVFormatContext *fmt_ctx);
1204 class Media : public ArrayList<media_info *> {
1207 char filename[BCTEXTLEN];
1208 int bd_path(const char *bp, const char *fmt, va_list ap);
1209 int bd_open(const char *fmt, ...);
1210 int bd_backup(const char *fmt, ...);
1211 int bd_copy(const char *ifn, const char *fmt, ...);
1215 ArrayList<clpi_cl *> cl;
1216 ArrayList<mpls_pl *> pl;
1218 void add_movie(uint32_t *ops, int n);
1220 int write(char *fn);
1222 Media() { path = 0; filename[0] = 0; }
1224 remove_all_objects();
1225 cl.remove_all_objects();
1226 pl.remove_all_objects();
1232 void bs_file::init()
1240 bs_file::open(const char *fn)
1243 if( !fp ) perror(fn);
1244 return fp != 0 ? 0 : 1;
1250 if( fp ) { fclose(fp); fp = 0; }
1254 bs_file::write(uint32_t v, int n)
1261 if( fp ) fputc((vv >> len), fp);
1262 if( ++fpos > fsz ) fsz = fpos;
1267 bs_file::writeb(uint8_t * bp, int n)
1269 while( --n >= 0 ) write(*bp++, 8);
1275 while( n > 32 ) { write(0, 32); n -= 32; }
1276 if( n > 0 ) write(0, n);
1280 bs_file::posb(int64_t n)
1282 if( fpos == fsz && n > fpos ) {
1287 if( fp ) fseek(fp, fpos, SEEK_SET);
1297 bs.writeb("MOBJ", 4);
1298 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1299 int movie_start = 0x0028;
1300 bs.posb(movie_start);
1303 bs.write(movies.size(), 16);
1305 for (int i = 0; i < movies.size(); ++i) {
1306 movie_obj *mov = movies[i];
1307 bs.write(mov->resume_intention_flag, 1);
1308 bs.write(mov->menu_call_mask, 1);
1309 bs.write(mov->title_search_mask, 1);
1311 ArrayList<command_obj *> &cmds = mov->cmds;
1312 int num_cmds = cmds.size();
1313 bs.write(num_cmds, 16);
1314 for (int j = 0; j < num_cmds; ++j) {
1315 command_obj *cmd = cmds[j];
1316 bs.write(cmd->op_cnt, 3);
1317 bs.write(cmd->grp, 2);
1318 bs.write(cmd->sub_grp, 3);
1319 bs.write(cmd->imm_op1, 1);
1320 bs.write(cmd->imm_op2, 1);
1322 bs.write(cmd->branch_opt, 4);
1324 bs.write(cmd->cmp_opt, 4);
1326 bs.write(cmd->set_opt, 5);
1327 //bs.write(cmd->cmd, 32);
1328 bs.write(cmd->dst, 32);
1329 bs.write(cmd->src, 32);
1333 // bs.write('l', 8);
1334 // bs.writebcd(year, 16);
1335 // bs.writebcd(month, 8);
1336 // bs.writebcd(day, 8);
1337 // bs.writebcd(hour, 8);
1338 // bs.writebcd(min, 8);
1339 // bs.writebcd(sec, 8);
1345 pb_obj::set_hdmv(int id, int pt)
1355 pb_obj::set_bdj(char *nm, int pt)
1361 bdj_name = cstrdup(nm);
1365 pb_obj::write_hdmv_obj(int id_ref)
1367 bs.write(pb_typ, 2);
1369 bs.write(id_ref, 16);
1374 pb_obj::write_bdj_obj(char *name)
1376 bs.write(pb_typ, 2);
1387 write_bdj_obj(bdj_name);
1391 write_hdmv_obj(hdmv_id);
1397 title_obj::write_obj()
1399 bs.write(obj_typ, 2);
1400 bs.write(acc_typ, 2);
1402 pb_obj::write_obj();
1407 index_file::write_pb_obj(pb_obj * pb)
1409 bs.write(pb->obj_typ, 2);
1418 bs.writeb("INDX", 4);
1419 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1421 exten.bs_zofs(bs, 32);
1422 int appinfo_start = 0x28;
1423 bs.posb(appinfo_start);
1424 appinf.bs_len(bs, 32);
1426 bs.write(initial_output_mode_preference, 1);
1427 bs.write(content_exist_flag, 1);
1429 bs.write(video_format, 4);
1430 bs.write(frame_rate, 4);
1431 bs.writeb(user_data, 32);
1436 write_pb_obj(&first_play);
1437 write_pb_obj(&top_menu);
1438 bs.write(titles.size(), 16);
1440 for (int i = 0; i < titles.size(); ++i)
1441 titles[i]->write_obj();
1443 exten.bs_len(bs,32);
1453 bs.writeb("BDID",4);
1454 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1455 bs.write(data_start, 32);
1456 bs.write(extension_data_start, 32);
1458 bs.writeb(org_id, sizeof(org_id));
1459 bs.writeb(disc_id, sizeof(disc_id));
1463 // XXX - not current referenced
1466 bdmv_write_extension_data(int start_address,
1467 int (*handler) (int, int, void *), void *handle)
1472 bs.write(length, 32); /* length of extension data block */
1476 bs.pad(32); /* relative start address of extension data */
1477 bs.pad(24); /* padding */
1478 bs.write(num_entries, 8);
1480 for (n = 0; n < num_entries; n++) {
1483 bs.write(ext_start, 32);
1484 bs.write(ext_len, 32);
1485 saved_pos = bs.pos() >> 3;
1486 bs.posb(start_address + ext_start);
1487 handler(id1, id2, handle);
1496 clpi_prog_stream::write()
1501 bs.write(coding_type, 8);
1502 switch (coding_type) {
1503 case BLURAY_STREAM_TYPE_VIDEO_MPEG1:
1504 case BLURAY_STREAM_TYPE_VIDEO_MPEG2:
1505 case BLURAY_STREAM_TYPE_VIDEO_VC1:
1506 case BLURAY_STREAM_TYPE_VIDEO_H264:
1508 bs.write(format, 4);
1510 bs.write(aspect, 4);
1512 bs.write(oc_flag, 1);
1516 case BLURAY_STREAM_TYPE_AUDIO_MPEG1:
1517 case BLURAY_STREAM_TYPE_AUDIO_MPEG2:
1518 case BLURAY_STREAM_TYPE_AUDIO_LPCM:
1519 case BLURAY_STREAM_TYPE_AUDIO_AC3:
1520 case BLURAY_STREAM_TYPE_AUDIO_DTS:
1521 case BLURAY_STREAM_TYPE_AUDIO_TRUHD:
1522 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS:
1523 case BLURAY_STREAM_TYPE_AUDIO_DTSHD:
1524 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER:
1525 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY:
1526 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY:
1527 bs.write(format, 4);
1532 case BLURAY_STREAM_TYPE_SUB_PG:
1533 case BLURAY_STREAM_TYPE_SUB_IG:
1538 case BLURAY_STREAM_TYPE_SUB_TEXT:
1539 bs.write(char_code, 8);
1544 fprintf(stderr, "clpi_prog_stream: unrecognized coding type %02x\n",
1549 bs.padb(0x15 - bs_posb(bs));
1555 clpi_cl::write_header()
1557 bs.writeb("HDMV", 4);
1558 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1559 bs.write(sequence_info_start_addr, 32);
1560 bs.write(program_info_start_addr, 32);
1561 bs.write(cpi_start_addr, 32);
1562 bs.write(clip_mark_start_addr, 32);
1563 bs.write(ext_data_start_addr, 32);
1568 clpi_atc_delta::write()
1570 bs.write(delta, 32);
1571 bs.writeb(file_id, 5);
1572 bs.writeb(file_code, 4);
1578 clpi_clip_info::write()
1583 bs.pad(16); // reserved
1584 bs.write(clip_stream_type, 8);
1585 bs.write(application_type, 8);
1586 bs.pad(31); // skip reserved 31 bits
1587 bs.write(is_atc_delta, 1);
1588 bs.write(ts_recording_rate, 32);
1589 bs.write(num_source_packets, 32);
1591 bs.padb(128); // Skip reserved 128 bytes
1593 // ts type info block
1595 bs.write(ts_len, 16);
1596 int64_t pos = bs.posb();
1598 bs.write(ts_type_info.validity, 8);
1599 bs.writeb(ts_type_info.format_id, 4);
1600 // pad the stuff we don't know anything about
1601 bs.padb(ts_len - (bs.posb() - pos));
1604 if( is_atc_delta ) {
1605 bs.pad(8); // Skip reserved byte
1606 bs.write(atc_delta.size(), 8);
1607 for( int ii=0; ii < atc_delta.size(); ++ii )
1608 if( atc_delta[ii]->write() ) return 1;
1612 if( application_type == 6 /* Sub TS for a sub-path of Text subtitle */ ) {
1614 bs.write(font_info.font.size(), 8);
1615 if( font_info.font.size() ) {
1616 for( int ii=0; ii < font_info.font.size(); ++ii ) {
1617 bs.writeb(font_info.font[ii]->file_id, 5);
1628 clpi_stc_seq::write()
1630 bs.write(pcr_pid, 16);
1631 bs.write(spn_stc_start, 32);
1632 bs.write(presentation_start_time, 32);
1633 bs.write(presentation_end_time, 32);
1638 clpi_atc_seq::write()
1640 bs.write(spn_atc_start, 32);
1641 bs.write(stc_seq.size(), 8);
1642 bs.write(offset_stc_id, 8);
1644 for( int ii=0; ii < stc_seq.size(); ++ii )
1645 if( stc_seq[ii]->write() ) return 1;
1650 clpi_sequences::write()
1653 bs.padb(1); // reserved byte
1654 bs.write(size(), 8);
1655 for( int ii=0; ii < size(); ++ii )
1656 if( get(ii)->write() ) return 1;
1664 bs.write(spn_program_sequence_start, 32);
1665 bs.write(program_map_pid, 16);
1666 bs.write(streams.size(), 8);
1667 bs.write(num_groups, 8);
1668 for( int ii=0; ii < streams.size(); ++ii )
1669 if( streams[ii]->write() ) return 1;
1674 clpi_programs::write()
1677 bs.padb(1); // reserved byte
1678 bs.write(size(), 8);
1679 for( int ii=0; ii < size(); ++ii )
1680 if( get(ii)->write() ) return 1;
1686 clpi_ep_coarse::write()
1688 bs.write(ref_ep_fine_id, 18);
1689 bs.write(pts_ep, 14);
1690 bs.write(spn_ep, 32);
1695 clpi_ep_fine::write()
1697 bs.write(is_angle_change_point, 1);
1698 bs.write(i_end_position_offset, 3);
1699 bs.write(pts_ep, 11);
1700 bs.write(spn_ep, 17);
1705 clpi_ep_map_entry::write(uint32_t ep_map_pos)
1709 bs.write(ep_stream_type, 4);
1710 bs.write(coarse.size(), 16);
1711 bs.write(fine.size(), 18);
1712 bs.write(ep_map_stream_start_addr - ep_map_pos, 32);
1717 clpi_ep_map_entry::write_map()
1719 ep_map_stream_start_addr = bs.posb();
1720 bs.write(fine_start, 32);
1722 for( int ii=0; ii < coarse.size(); ++ii )
1723 if( coarse[ii]->write() ) return 1;
1725 fine_start = bs.posb() - ep_map_stream_start_addr;
1726 for( int ii=0; ii < fine.size(); ++ii )
1727 if( fine[ii]->write() ) return 1;
1738 uint32_t ep_map_pos = bs.posb();
1740 // EP Map starts here
1742 bs.write(size(), 8);
1744 for( int ii=0; ii < size(); ++ii )
1745 if( get(ii)->write(ep_map_pos) ) return 1;
1747 for( int ii=0; ii < size(); ++ii )
1748 if( get(ii)->write_map() ) return 1;
1763 clpi_extents::write()
1766 bs.write(size(), 32);
1767 for( int ii=0; ii < size(); ++ii )
1768 bs.write(get(ii), 32);
1773 clpi_cl::write_clpi_extension(int id1, int id2, void *handle)
1775 clpi_cl *cl = (clpi_cl *) handle;
1779 // LPCM down mix coefficient
1780 //write_lpcm_down_mix_coeff(&cl->lpcm_down_mix_coeff);
1787 // Extent start point
1788 return cl->extents.write();
1792 return cl->programs_ss.write();
1796 return cl->cpi_ss.write();
1800 fprintf(stderr, "write_clpi_extension(): unhandled extension %d.%d\n", id1, id2);
1807 if( write_header() ) return 1;
1808 if( clip.write() ) return 1;
1809 sequence_info_start_addr = bs.posb();
1810 if( sequences.write() ) return 1;
1811 program_info_start_addr = bs.posb();
1812 if( programs.write() ) return 1;
1813 cpi_start_addr = bs.posb();
1814 if( cpi.write() ) return 1;
1815 clip_mark_start_addr = bs.posb();
1816 if( cmrk.write() ) return 1;
1817 //if( has_ext_data ) {
1818 // ext_data_start_addr = bs.pos();
1819 // bdmv_write_extension_data(write_clpi_extension, this);
1827 bs.write(menu_call, 1);
1828 bs.write(title_search, 1);
1829 bs.write(chapter_search, 1);
1830 bs.write(time_search, 1);
1831 bs.write(skip_to_next_point, 1);
1832 bs.write(skip_to_prev_point, 1);
1833 bs.write(play_firstplay, 1);
1835 bs.write(pause_on, 1);
1836 bs.write(pause_off, 1);
1837 bs.write(still_off, 1);
1838 bs.write(forward, 1);
1839 bs.write(backward, 1);
1840 bs.write(resume, 1);
1841 bs.write(move_up, 1);
1842 bs.write(move_down, 1);
1843 bs.write(move_left, 1);
1844 bs.write(move_right, 1);
1845 bs.write(select, 1);
1846 bs.write(activate, 1);
1847 bs.write(select_and_activate, 1);
1848 bs.write(primary_audio_change, 1);
1850 bs.write(angle_change, 1);
1851 bs.write(popup_on, 1);
1852 bs.write(popup_off, 1);
1853 bs.write(pg_enable_disable, 1);
1854 bs.write(pg_change, 1);
1855 bs.write(secondary_video_enable_disable, 1);
1856 bs.write(secondary_video_change, 1);
1857 bs.write(secondary_audio_enable_disable, 1);
1858 bs.write(secondary_audio_change, 1);
1860 bs.write(pip_pg_change, 1);
1869 bs.pad(8); // Reserved
1870 bs.write(playback_type, 8);
1871 if (playback_type == BLURAY_PLAYBACK_TYPE_RANDOM ||
1872 playback_type == BLURAY_PLAYBACK_TYPE_SHUFFLE ) {
1873 bs.write(playback_count, 16);
1876 bs.pad(16); // Reserved
1879 bs.write(random_access_flag, 1);
1880 bs.write(audio_mix_flag, 1);
1881 bs.write(lossless_bypass_flag, 1);
1882 bs.pad(13); // Reserved
1888 mpls_pl::write_header()
1890 bs.writeb("MPLS", 4);
1891 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1892 bs.write(list_pos, 32);
1893 bs.write(mark_pos, 32);
1894 bs.write(ext_pos, 32);
1895 bs.pad(160); // Skip 160 reserved bits
1905 bs.write(stream_type, 8);
1906 switch (stream_type) {
1913 bs.write(subpath_id, 8);
1914 bs.write(subclip_id, 8);
1919 bs.write(subpath_id, 8);
1924 fprintf(stderr, "unrecognized stream type %02x\n", stream_type);
1927 bs.padb(9 - strm.bs_posb(bs));
1931 bs.write(coding_type, 8);
1932 switch (coding_type) {
1933 case BLURAY_STREAM_TYPE_VIDEO_MPEG1:
1934 case BLURAY_STREAM_TYPE_VIDEO_MPEG2:
1935 case BLURAY_STREAM_TYPE_VIDEO_VC1:
1936 case BLURAY_STREAM_TYPE_VIDEO_H264:
1937 bs.write(format, 4);
1941 case BLURAY_STREAM_TYPE_AUDIO_MPEG1:
1942 case BLURAY_STREAM_TYPE_AUDIO_MPEG2:
1943 case BLURAY_STREAM_TYPE_AUDIO_LPCM:
1944 case BLURAY_STREAM_TYPE_AUDIO_AC3:
1945 case BLURAY_STREAM_TYPE_AUDIO_DTS:
1946 case BLURAY_STREAM_TYPE_AUDIO_TRUHD:
1947 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS:
1948 case BLURAY_STREAM_TYPE_AUDIO_DTSHD:
1949 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER:
1950 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY:
1951 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY:
1952 bs.write(format, 4);
1957 case BLURAY_STREAM_TYPE_SUB_PG:
1958 case BLURAY_STREAM_TYPE_SUB_IG:
1962 case BLURAY_STREAM_TYPE_SUB_TEXT:
1963 bs.write(char_code, 8);
1968 fprintf(stderr, "mpls_stream: unrecognized coding type %02x\n", coding_type);
1971 bs.padb(5 - code.bs_posb(bs));
1980 bs.pad(16); // Skip 2 reserved bytes
1982 bs.write(video.size(), 8);
1983 bs.write(audio.size(), 8);
1984 bs.write(pg.size() - num_pip_pg, 8);
1985 bs.write(ig.size(), 8);
1986 bs.write(secondary_audio.size(), 8);
1987 bs.write(secondary_video.size(), 8);
1988 bs.write(num_pip_pg, 8);
1993 // Primary Video Streams
1994 for( int ii=0; ii < video.size(); ++ii )
1995 if( video[ii]->write() ) return 1;
1997 // Primary Audio Streams
1998 for( int ii=0; ii < audio.size(); ++ii )
1999 if( audio[ii]->write() ) return 1;
2001 // Presentation Graphic Streams
2002 for( int ii=0; ii < pg.size(); ++ii )
2003 if( pg[ii]->write() ) return 1;
2005 // Interactive Graphic Streams
2006 for( int ii=0; ii < ig.size(); ++ii )
2007 if( ig[ii]->write() ) return 1;
2009 // Secondary Audio Streams
2010 for( int ii=0; ii < secondary_audio.size(); ++ii ) {
2011 if( secondary_audio[ii]->write() ) return 1;
2012 // Read Secondary Audio Extra Attributes
2013 bs.write(sa_primary_audio_ref.size(), 8);
2015 for( int jj=0; jj < sa_primary_audio_ref.size(); ++jj )
2016 bs.write(sa_primary_audio_ref[jj], 8);
2017 if( sa_primary_audio_ref.size() % 2) bs.pad(8 );
2020 // Secondary Video Streams
2021 for( int ii=0; ii < secondary_video.size(); ++ii ) {
2022 if( secondary_video[ii]->write() ) return 1;
2023 // Read Secondary Video Extra Attributes
2024 bs.write(sv_secondary_audio_ref.size(), 8);
2026 for( int jj=0; jj < sv_secondary_audio_ref.size(); ++jj )
2027 bs.write(sv_secondary_audio_ref[jj], 8);
2028 if( sv_secondary_audio_ref.size() % 2) bs.pad(8 );
2029 bs.write(sv_pip_pg_ref.size(), 8);
2031 for( int jj=0; jj < sv_pip_pg_ref.size(); ++jj )
2032 bs.write(sv_pip_pg_ref[jj], 8);
2033 if( sv_pip_pg_ref.size() % 2) bs.pad(8 );
2044 // Primary Clip identifer
2045 bs.writeb(clip[0]->clip_id, 5);
2046 bs.writeb(clip[0]->codec_id, 4); // "M2TS"
2047 bs.pad(11); // Skip reserved 11 bits
2049 bs.write(is_multi_angle, 1);
2050 bs.write(connection_condition, 4); // 0x01, 0x05, 0x06
2052 bs.write(clip[0]->stc_id, 8);
2053 bs.write(in_time, 32);
2054 bs.write(out_time, 32);
2057 bs.write(random_access_flag, 1);
2059 bs.write(still_mode, 8);
2060 if( still_mode == 0x01 ) {
2061 bs.write(still_time, 16);
2067 if( is_multi_angle ) {
2068 bs.write(clip.size(), 8);
2070 bs.write(is_different_audio, 1);
2071 bs.write(is_seamless_angle, 1);
2074 for( int ii=1; ii < clip.size(); ++ii ) {
2075 bs.writeb(clip[ii]->clip_id, 5);
2076 bs.writeb(clip[ii]->codec_id, 4); // "M2TS"
2077 bs.write(clip[ii]->stc_id, 8);
2080 if( stn.write() ) return 1;
2087 mpls_sub_pi::write()
2090 // Primary Clip identifer
2091 bs.writeb(clip[0]->clip_id, 5);
2092 bs.writeb(clip[0]->codec_id, 4); // "M2TS"
2095 bs.write(connection_condition, 4); // 0x01, 0x05, 0x06
2097 bs.write(is_multi_clip, 1);
2098 bs.write(clip[0]->stc_id, 8);
2099 bs.write(in_time, 32);
2100 bs.write(out_time, 32);
2101 bs.write(sync_play_item_id, 16);
2102 bs.write(sync_pts, 32);
2105 bs.write(clip.size(), 8);
2107 for( int ii=1; ii < clip.size(); ++ii ) {
2108 // Primary Clip identifer
2109 bs.writeb(clip[ii]->clip_id, 5);
2110 bs.writeb(clip[ii]->codec_id, 4); // "M2TS"
2111 bs.write(clip[ii]->stc_id, 8);
2125 bs.write(is_repeat, 1);
2127 bs.write(sub_play_item.size(), 8);
2129 for( int ii=0; ii < sub_play_item.size(); ++ii )
2130 if( sub_play_item[ii]->write() ) return 1;
2139 bs.write(mark_id, 8);
2140 bs.write(mark_type, 8);
2141 bs.write(play_item_ref, 16);
2143 bs.write(entry_es_pid, 16);
2144 bs.write(duration, 32);
2149 mpls_pl::write_playlistmark()
2151 mark.bs_len(bs, 32);
2152 // Then get the number of marks
2153 bs.write(play_mark.size(), 16);
2155 for( int ii=0; ii < play_mark.size(); ++ii )
2156 if( play_mark[ii]->write() ) return 1;
2163 mpls_pl::write_playlist()
2167 // Skip reserved bytes
2170 bs.write(play_item.size(), 16);
2171 bs.write(sub_path.size(), 16);
2173 for( int ii=0; ii < play_item.size(); ++ii )
2174 if( play_item[ii]->write() ) return 1;
2176 for( int ii=0; ii < sub_path.size(); ++ii )
2177 if( sub_path[ii]->write() ) return 1;
2184 mpls_pip_data::write()
2189 bs.write(scale_factor, 4);
2195 mpls_pip_metadata::write(uint32_t start_address)
2198 bs.write(clip_ref, 16);
2199 bs.write(secondary_video_ref, 8);
2201 bs.write(timeline_type, 4);
2202 bs.write(luma_key_flag, 1);
2203 bs.write(trick_play_flag, 1);
2205 if( luma_key_flag ) {
2207 bs.write(upper_limit_luma_key, 8);
2214 uint32_t data_address = 0; // XXX
2215 bs.write(data_address, 32);
2217 int64_t pos = bs.pos() / 8;
2218 bs.posb(start_address + data_address);
2220 bs.write(data.size(), 16);
2221 if( data.size() < 1 ) return 1;
2223 for( int ii=0; ii < data.size(); ++ii )
2224 if( data[ii]->write() ) return 1;
2231 mpls_pl::write_pip_metadata_extension()
2233 uint32_t pos = bs.posb();
2234 pipm.bs_len(bs, 32);
2236 bs.write(ext_pip_data.size(), 16);
2237 for( int ii=0; ii < ext_pip_data.size(); ++ii )
2238 if( ext_pip_data[ii]->write(pos) ) return 1;
2245 mpls_pl::write_subpath_extension()
2247 subx.bs_len(bs, 32);
2249 bs.write(ext_sub_path.size(), 16);
2250 for( int ii=0; ii < ext_sub_path.size(); ++ii )
2251 if( ext_sub_path[ii]->write() ) return 1;
2258 clpi_cl::write_mpls_extension(int id1, int id2, void *handle)
2260 mpls_pl *pl = (mpls_pl *) handle;
2264 // PiP metadata extension
2265 return pl->write_pip_metadata_extension();
2274 // SubPath entries extension
2275 return pl->write_subpath_extension();
2285 int ret = write_header();
2286 list_pos = bs.posb();;
2287 if( !ret ) ret = write_playlist();
2288 mark_pos = bs.posb();
2289 if( !ret ) ret = write_playlistmark();
2290 //if( has_pls_extension ) {
2291 // ext_pos = bs.posb();
2292 // bdmv_write_extension_data(write_mpls_extension, pl);
2299 if( !mkdir(path, 0777) )
2306 mk_bdmv_dir(char *bdmv_path)
2308 if( mk_dir(bdmv_path) )
2310 char bdjo_path[BCTEXTLEN];
2311 sprintf(bdjo_path, "%s/BDJO", bdmv_path);
2312 if( mk_dir(bdjo_path) )
2314 char clipinf_path[BCTEXTLEN];
2315 sprintf(clipinf_path, "%s/CLIPINF", bdmv_path);
2316 if( mk_dir(clipinf_path) )
2318 char jar_path[BCTEXTLEN];
2319 sprintf(jar_path, "%s/JAR", bdmv_path);
2320 if( mk_dir(jar_path) )
2322 char playlist_path[BCTEXTLEN];
2323 sprintf(playlist_path, "%s/PLAYLIST", bdmv_path);
2324 if( mk_dir(playlist_path) )
2332 char bdmv_path[BCTEXTLEN];
2333 sprintf(bdmv_path, "%s/BDMV", path);
2334 if( mk_bdmv_dir(bdmv_path) ) return 1;
2335 char cert_path[BCTEXTLEN];
2336 sprintf(cert_path, "%s/CERTIFICATE", path);
2337 if( mk_bdmv_dir(cert_path) ) return 1;
2338 char cert_backup[BCTEXTLEN];
2339 sprintf(cert_backup, "%s/BACKUP", cert_path);
2340 if( mk_bdmv_dir(cert_backup) ) return 1;
2341 char stream_path[BCTEXTLEN];
2342 sprintf(stream_path, "%s/STREAM", bdmv_path);
2343 if( mk_dir(stream_path) ) return 1;
2344 char auxdata_path[BCTEXTLEN];
2345 sprintf(auxdata_path, "%s/AUXDATA", bdmv_path);
2346 if( mk_dir(auxdata_path) ) return 1;
2347 char meta_path[BCTEXTLEN];
2348 sprintf(meta_path, "%s/META", bdmv_path);
2349 if( mk_dir(meta_path) ) return 1;
2350 char backup_path[BCTEXTLEN];
2351 sprintf(backup_path, "%s/BACKUP", bdmv_path);
2352 if( mk_bdmv_dir(backup_path) ) return 1;
2357 build_toc(clpi_ep_map_entry *map)
2359 clpi_ep_coarse *cp = 0;
2360 marks.sort(mark::cmpr);
2361 uint16_t ep_pid = map->pid;
2362 int64_t last_pts = -1, last_pkt = -1;
2363 for( int ii=0; ii<marks.size(); ++ii ) {
2364 mark *mp = marks[ii];
2365 if( mp->pid != ep_pid ) continue;
2366 int64_t pts = mp->pts;
2367 if( last_pts >= pts ) continue;
2369 uint32_t pkt = mp->pos / BLURAY_TS_PKTSZ;
2370 if( last_pkt >= pkt ) continue;
2372 int64_t coarse_pts = (pts >> 18); // & ~0x01;
2373 int64_t fine_pts = (pts & 0x7ffff) >> 8;
2374 uint32_t mpkt = pkt & ~0x1ffff;
2375 if( !cp || cp->pts_ep != coarse_pts || mpkt > cp->spn_ep ) {
2376 cp = new clpi_ep_coarse();
2377 map->coarse.append(cp);
2378 cp->ref_ep_fine_id = map->fine.size();
2379 cp->pts_ep = coarse_pts;
2382 clpi_ep_fine *fp = new clpi_ep_fine();
2383 map->fine.append(fp);
2384 fp->is_angle_change_point = 0;
2385 // XXX - dont know what this is
2386 fp->i_end_position_offset = 1;
2387 fp->pts_ep = fine_pts;
2388 fp->spn_ep = pkt & 0x1ffff;
2393 const AVRational media_info::clk45k = { 1, 45000 };
2395 static int bd_stream_type(AVCodecID codec_id)
2397 int stream_type = 0;
2399 case AV_CODEC_ID_MPEG1VIDEO:
2400 stream_type = BLURAY_STREAM_TYPE_VIDEO_MPEG1;
2402 case AV_CODEC_ID_MPEG2VIDEO:
2403 stream_type = BLURAY_STREAM_TYPE_VIDEO_MPEG2;
2405 case AV_CODEC_ID_H264:
2406 stream_type = BLURAY_STREAM_TYPE_VIDEO_H264;
2408 case AV_CODEC_ID_MP2:
2409 stream_type = BLURAY_STREAM_TYPE_AUDIO_MPEG1;
2411 case AV_CODEC_ID_MP3:
2412 stream_type = BLURAY_STREAM_TYPE_AUDIO_MPEG2;
2414 case AV_CODEC_ID_AC3:
2415 stream_type = BLURAY_STREAM_TYPE_AUDIO_AC3;
2417 case AV_CODEC_ID_EAC3:
2418 stream_type = BLURAY_STREAM_TYPE_AUDIO_AC3PLUS;
2420 case AV_CODEC_ID_DTS:
2421 stream_type = BLURAY_STREAM_TYPE_AUDIO_DTS;
2423 case AV_CODEC_ID_TRUEHD:
2424 stream_type = BLURAY_STREAM_TYPE_AUDIO_TRUHD;
2426 case AV_CODEC_ID_HDMV_PGS_SUBTITLE:
2427 stream_type = BLURAY_STREAM_TYPE_SUB_PG;
2430 fprintf(stderr, "unknown bluray stream type %s\n", avcodec_get_name(codec_id));
2436 static int bd_audio_format(int channels)
2438 int audio_format = 0;
2439 switch( channels ) {
2441 audio_format = BLURAY_AUDIO_FORMAT_MONO;
2444 audio_format = BLURAY_AUDIO_FORMAT_STEREO;
2447 audio_format = BLURAY_AUDIO_FORMAT_MULTI_CHAN;
2450 fprintf(stderr, "unknown bluray audio format %d ch\n", channels);
2453 return audio_format;
2456 static int bd_audio_rate(int rate)
2460 case 48000: audio_rate = BLURAY_AUDIO_RATE_48; break;
2461 case 96000: audio_rate = BLURAY_AUDIO_RATE_96; break;
2462 case 192000: audio_rate = BLURAY_AUDIO_RATE_192; break;
2464 fprintf(stderr, "unknown bluray audio rate %d\n", rate);
2470 static int bd_video_format(int w, int h, int ilace)
2472 if( w == 720 && h == 480 && ilace ) return BLURAY_VIDEO_FORMAT_480I;
2473 if( w == 720 && h == 576 && ilace ) return BLURAY_VIDEO_FORMAT_576I;
2474 if( w == 720 && h == 480 && !ilace ) return BLURAY_VIDEO_FORMAT_480P;
2475 if( w == 720 && h == 576 && !ilace ) return BLURAY_VIDEO_FORMAT_576P;
2476 // this seems to be overly restrictive
2477 if( w == 1280 && h == 720 /* && !ilace*/ ) return BLURAY_VIDEO_FORMAT_720P;
2478 if( w == 1440 && h == 1080 /* && ilace*/ ) return BLURAY_VIDEO_FORMAT_1080I;
2479 if( w == 1920 && h == 1080 /* && !ilace*/ ) return BLURAY_VIDEO_FORMAT_1080P;
2480 fprintf(stderr, "unknown bluray video format %dx%d %silace\n",
2481 w, h, !ilace ? "not " : "");
2485 static int bd_video_rate(double rate)
2487 if( fabs(rate-23.976) < 0.01 ) return BLURAY_VIDEO_RATE_24000_1001;
2488 if( fabs(rate-24.000) < 0.01 ) return BLURAY_VIDEO_RATE_24;
2489 if( fabs(rate-25.000) < 0.01 ) return BLURAY_VIDEO_RATE_25;
2490 if( fabs(rate-29.970) < 0.01 ) return BLURAY_VIDEO_RATE_30000_1001;
2491 if( fabs(rate-50.000) < 0.01 ) return BLURAY_VIDEO_RATE_50;
2492 if( fabs(rate-59.940) < 0.01 ) return BLURAY_VIDEO_RATE_60000_1001;
2493 fprintf(stderr, "unknown bluray video framerate %5.2f\n",rate);
2497 static int bd_aspect_ratio(int w, int h, double ratio)
2499 double aspect = (w * ratio) / h;
2500 if( fabs(aspect-1.333) < 0.01 ) return BLURAY_ASPECT_RATIO_4_3;
2501 if( fabs(aspect-1.777) < 0.01 ) return BLURAY_ASPECT_RATIO_16_9;
2502 return w == 720 ? BLURAY_ASPECT_RATIO_4_3 : BLURAY_ASPECT_RATIO_16_9;
2503 fprintf(stderr, "unknown bluray aspect ratio %5.3f\n",aspect);
2507 static int field_probe(AVFormatContext *fmt_ctx, AVStream *st)
2509 AVDictionary *copts = 0;
2510 //av_dict_copy(&copts, opts, 0);
2511 AVCodecID codec_id = st->codec->codec_id;
2512 AVCodec *decoder = avcodec_find_decoder(codec_id);
2513 if( avcodec_open2(st->codec, decoder, &copts) < 0 ) {
2514 fprintf(stderr,"codec open failed\n");
2517 av_dict_free(&copts);
2519 AVFrame *ipic = av_frame_alloc();
2521 av_init_packet(&ipkt);
2523 for( int retrys=100; --retrys>=0 && ilaced<0; ) {
2524 av_packet_unref(&ipkt);
2525 int ret = av_read_frame(fmt_ctx, &ipkt);
2526 if( ret == AVERROR_EOF ) break;
2527 if( ret != 0 ) continue;
2528 if( !ipkt.data ) continue;
2529 if( ipkt.stream_index != st->index ) continue;
2530 while( ipkt.size > 0 ) {
2532 ret = avcodec_decode_video2(st->codec, ipic, &got_frame, &ipkt);
2533 if( ret <= 0 ) break;
2535 ilaced = ipic->interlaced_frame ? 1 : 0;
2542 av_packet_unref(&ipkt);
2543 av_frame_free(&ipic);
2547 int media_info::scan()
2550 if( stat(filename, &st) ) return 1;
2551 file_size = st.st_size;
2553 AVFormatContext *fmt_ctx = 0;
2554 AVDictionary *fopts = 0;
2555 av_dict_set(&fopts, "formatprobesize", "5000000", 0);
2556 av_dict_set(&fopts, "scan_all_pmts", "1", 0);
2557 av_dict_set(&fopts, "threads", "auto", 0);
2558 int ret = avformat_open_input(&fmt_ctx, filename, NULL, &fopts);
2559 av_dict_free(&fopts);
2560 if( ret < 0 ) return ret;
2561 ret = avformat_find_stream_info(fmt_ctx, NULL);
2563 bit_rate = fmt_ctx->bit_rate;
2566 for( int i=0; ret>=0 && i<(int)fmt_ctx->nb_streams; ++i ) {
2567 AVStream *st = fmt_ctx->streams[i];
2568 AVMediaType type = st->codec->codec_type;
2570 case AVMEDIA_TYPE_VIDEO: break;
2571 case AVMEDIA_TYPE_AUDIO: break;
2572 case AVMEDIA_TYPE_SUBTITLE: break;
2575 stream *s = new stream(type, i);
2577 AVCodecID codec_id = st->codec->codec_id;
2579 case AVMEDIA_TYPE_VIDEO: {
2580 if( ep_pid < 0 ) ep_pid = st->id;
2581 s->coding_type = bd_stream_type(codec_id);
2582 int ilace = field_probe(fmt_ctx, st);
2584 fprintf(stderr, "interlace probe failed\n");
2587 s->format = bd_video_format(st->codec->width, st->codec->height, ilace);
2588 s->rate = bd_video_rate(!st->codec->framerate.den ? 0 :
2589 (double)st->codec->framerate.num / st->codec->framerate.den);
2590 s->aspect = bd_aspect_ratio(st->codec->width, st->codec->height,
2591 !st->sample_aspect_ratio.num || !st->sample_aspect_ratio.den ? 1. :
2592 (double)st->sample_aspect_ratio.num / st->sample_aspect_ratio.den);
2594 case AVMEDIA_TYPE_AUDIO: {
2595 s->coding_type = bd_stream_type(codec_id);
2596 s->format = bd_audio_format(st->codec->channels);
2597 s->rate = bd_audio_rate(st->codec->sample_rate);
2598 strcpy((char*)s->lang, "eng");
2600 case AVMEDIA_TYPE_SUBTITLE: {
2601 s->coding_type = bd_stream_type(codec_id);
2602 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", 0, 0);
2603 strncpy((char*)s->lang, lang ? lang->value : "und", sizeof(s->lang));
2608 if( bit_rate > 0 && st->duration == AV_NOPTS_VALUE &&
2609 st->time_base.num < INT64_MAX / bit_rate ) {
2610 st->duration = av_rescale(8*file_size, st->time_base.den,
2611 bit_rate * (int64_t) st->time_base.num);
2613 s->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2616 AVCodec *decoder = avcodec_find_decoder(codec_id);
2617 AVDictionary *copts = 0;
2618 ret = avcodec_open2(st->codec, decoder, &copts);
2621 ep_pid = fmt_ctx->nb_streams > 0 ? fmt_ctx->streams[0]->id : 0;
2623 int npgm = fmt_ctx->nb_programs;
2625 program *pgm = new program(-1, 1);
2626 pgm->ep_pid = ep_pid;
2627 pgm->pmt_pid = 0x1000;
2628 pgm->pcr_pid = 0x1001;
2630 for( int jj=0; jj<streams.size(); ++jj ) {
2631 AVStream *st = fmt_ctx->streams[jj];
2632 AVMediaType type = st->codec->codec_type;
2634 case AVMEDIA_TYPE_VIDEO:
2635 case AVMEDIA_TYPE_AUDIO:
2640 pgm->strm_idx.append(jj);
2641 if( pgm->duration < st->duration )
2642 pgm->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2644 programs.append(pgm);
2647 for( int ii=0; ii<npgm; ++ii ) {
2648 AVProgram *pgrm = fmt_ctx->programs[ii];
2649 program *pgm = new program(ii, pgrm->id);
2650 pgm->pmt_pid = pgrm->pmt_pid;
2651 pgm->pcr_pid = pgrm->pcr_pid;
2654 for( int jj=0; jj<(int)pgrm->nb_stream_indexes; ++jj ) {
2655 int av_idx = pgrm->stream_index[jj];
2656 AVStream *st = fmt_ctx->streams[av_idx];
2657 AVMediaType type = st->codec->codec_type;
2659 case AVMEDIA_TYPE_VIDEO:
2660 if( ep_pid < 0 ) ep_pid = st->id;
2662 case AVMEDIA_TYPE_AUDIO:
2663 case AVMEDIA_TYPE_SUBTITLE:
2668 int sidx = streams.size();
2669 while( --sidx>=0 && streams[sidx]->av_idx!=av_idx );
2671 fprintf(stderr, "bad stream idx %d in pgm %d\n",av_idx, ii);
2674 if( pgm->duration < st->duration )
2675 pgm->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2676 pgm->strm_idx.append(sidx);
2679 AVProgram *pgrm = fmt_ctx->programs[0];
2680 ep_pid = pgrm->nb_stream_indexes > 0 ?
2681 fmt_ctx->streams[pgrm->stream_index[0]]->id : 0;
2683 pgm->ep_pid = ep_pid;
2684 programs.append(pgm);
2688 ret = scan(fmt_ctx);
2690 for( int i=0; i<(int)fmt_ctx->nb_streams; ++i )
2691 avcodec_close(fmt_ctx->streams[i]->codec);
2692 avformat_close_input(&fmt_ctx);
2697 int media_info::scan(AVFormatContext *fmt_ctx)
2701 av_init_packet(&ipkt);
2703 // zero pts at pos zero
2704 for( int i=0; i<programs.size(); ++i ) {
2705 program *p = programs[i];
2706 for( int j=0; j<p->strm_idx.size(); ++j ) {
2707 stream *sp = streams[p->strm_idx[j]];
2709 AVStream *st = fmt_ctx->streams[sp->av_idx];
2710 p->add_label(0, 0, 0, st->id);
2714 for( int64_t count=0; ret>=0; ++count ) {
2715 av_packet_unref(&ipkt);
2716 ipkt.data = 0; ipkt.size = 0;
2718 ret = av_read_frame(fmt_ctx, &ipkt);
2720 if( ret == AVERROR_EOF ) break;
2724 if( !ipkt.data ) continue;
2725 if( (ipkt.flags & AV_PKT_FLAG_CORRUPT) ) continue;
2726 if( ipkt.pts == AV_NOPTS_VALUE ) continue;
2727 int i = ipkt.stream_index;
2728 if( i < 0 || i >= streams.size() ) continue;
2732 for( int ii=0; !pgm && ii<programs.size(); ++ii ) {
2733 program *p = programs[ii];
2734 for( int jj=0; jj<p->strm_idx.size(); ++jj ) {
2735 sp = streams[p->strm_idx[jj]];
2736 if( sp->av_idx == i ) { pgm = p; break; }
2739 if( !pgm ) continue;
2740 AVStream *st = fmt_ctx->streams[i];
2741 if( pgm->ep_pid != st->id ) continue;
2742 int64_t pts45k = av_rescale_q(ipkt.pts, st->time_base, clk45k);
2743 if( sp->start_pts > pts45k ) sp->start_pts = pts45k;
2744 if( sp->end_pts < pts45k ) sp->end_pts = pts45k;
2745 if( pgm->start_time > pts45k ) pgm->start_time = pts45k;
2746 if( pgm->end_time < pts45k ) pgm->end_time = pts45k;
2748 if( !(ipkt.flags & AV_PKT_FLAG_KEY) ) continue;
2750 if( sp->last_pts != pts45k ) {
2751 sp->last_pts = pts45k;
2752 pgm->add_label(count, ipkt.pos, pts45k, st->id);
2756 for( int ii=0; ii<programs.size(); ++ii ) {
2757 program *pgm = programs[ii];
2758 if( pgm->end_time > pgm->start_time )
2759 pgm->duration = pgm->end_time - pgm->start_time;
2762 return ret != AVERROR_EOF ? -1 : 0;
2766 Media::add_movie(uint32_t *ops, int n)
2768 movie_obj *mp = new movie_obj();
2769 mp->resume_intention_flag = 1;
2770 uint32_t *eop = ops + n/sizeof(*ops);
2771 while( ops < eop ) {
2772 command_obj *cmd = new command_obj();
2773 cmd->cmd = htobe32(*ops++);
2776 mp->cmds.append(cmd);
2778 mov.movies.append(mp);
2788 int top_menu_obj = mov.movies.size();
2789 movie_obj *mp = new movie_obj();
2790 mp->resume_intention_flag = 1;
2791 command_obj *cmd = new command_obj();
2792 cmd->cmd = htobe32(0x21810000); cmd->dst = 1; cmd->src = 0;
2793 mp->cmds.append(cmd); // JUMP_TITLE 1
2794 mov.movies.append(mp);
2797 for( int ii=0; ii<size(); ++ii ) {
2798 mp = new movie_obj();
2799 mp->resume_intention_flag = 1;
2800 cmd = new command_obj();
2801 cmd->cmd = htobe32(0x22800000); cmd->dst = ii; cmd->src = 0;
2802 mp->cmds.append(cmd); // PLAY_PL ii
2803 cmd = new command_obj();
2804 cmd->cmd = htobe32(0x00020000); cmd->dst = 0; cmd->src = 0;
2805 mp->cmds.append(cmd);
2806 mov.movies.append(mp); // BREAK
2810 int first_play_obj = mov.movies.size();
2811 mp = new movie_obj();
2812 mp->resume_intention_flag = 1;
2813 cmd = new command_obj();
2814 cmd->cmd = htobe32(0x21810000); cmd->dst = 0; cmd->src = 0;
2815 mp->cmds.append(cmd); // JUMP_TITLE 0 ; top menu
2816 mov.movies.append(mp);
2820 idx.first_play.set_hdmv(first_play_obj, pb_typ_iactv);
2821 idx.top_menu.set_hdmv(top_menu_obj, pb_typ_iactv);
2825 for( int ii=0; ii<size(); ++ii ) {
2827 tp = new title_obj();
2828 tp->set_hdmv(idx.titles.size()+1, pb_typ_movie);
2829 idx.titles.append(tp);
2832 media_info *ip = get(ii);
2833 // clip program, if specified
2834 int pidx = ip->programs.size();
2835 while( --pidx>=0 && ip->programs[pidx]->pid != ip->pgm_pid );
2836 if( pidx < 0 ) pidx = 0;
2838 ip->pgm_pid = ip->prog()->pid;
2840 clpi_cl *cp = new clpi_cl();
2841 cp->clip.clip_stream_type = 1;
2842 cp->clip.application_type = BLURAY_APP_TYPE_MAIN_MOVIE;
2843 cp->clip.ts_recording_rate = ip->bit_rate;
2844 uint32_t ts_pkt_count = ip->file_size / BLURAY_TS_PKTSZ;
2845 cp->clip.num_source_packets = ts_pkt_count;
2846 cp->clip.ts_type_info.validity = 0x80;
2847 strcpy(cp->clip.ts_type_info.format_id, "HDMV");
2850 for( int jj=0; jj<ip->programs.size(); ++jj ) {
2851 program *pgm = ip->programs[jj];
2852 clpi_prog *p = new clpi_prog(pgm->pmt_pid);
2853 for( int kk=0; kk<pgm->strm_idx.size(); ++kk ) {
2854 int k = pgm->strm_idx[kk];
2855 stream *sp = ip->streams[k];
2856 clpi_prog_stream *s = new clpi_prog_stream();
2858 s->coding_type = sp->coding_type;
2859 s->format = sp->format;
2860 //use unspecified (0)
2861 // if( !idx.video_format ) idx.video_format = s->format;
2863 // if( !idx.frame_rate ) idx.frame_rate = s->rate;
2864 switch( sp->type ) {
2865 case AVMEDIA_TYPE_VIDEO:
2866 s->aspect = sp->aspect;
2868 case AVMEDIA_TYPE_AUDIO:
2869 case AVMEDIA_TYPE_SUBTITLE:
2870 memcpy(s->lang,sp->lang,sizeof(s->lang));
2875 p->streams.append(s);
2877 clpi_ep_map_entry *map = new clpi_ep_map_entry(pgm->ep_pid);
2878 map->ep_stream_type = 1;
2879 pgm->build_toc(map);
2880 cp->cpi.append(map);
2881 cp->programs.append(p);
2883 clpi_atc_seq *atc_seq = new clpi_atc_seq();
2884 clpi_stc_seq *stc_seq = new clpi_stc_seq();
2885 stc_seq->pcr_pid = pgm->pcr_pid;
2886 stc_seq->presentation_start_time = pgm->start_time;
2887 stc_seq->presentation_end_time = pgm->end_time;
2888 atc_seq->stc_seq.append(stc_seq);
2889 cp->sequences.append(atc_seq);
2894 if( ip->brk ) tp = 0;
2897 // playlists, one per title
2898 // one playitem per media clip
2900 for( int ii=0; ii<idx.titles.size(); ++ii ) {
2902 media_info *ip = get(clip_id);
2903 program *pgm = ip->prog();
2904 mpls_pl *pp = new mpls_pl();
2905 pp->app_info.playback_type = BLURAY_PLAYBACK_TYPE_SEQUENTIAL;
2906 // pp->app_info.uo_mask.xxx = 1;
2907 int last = idx.titles[ii]->last;
2908 for( ; clip_id<=last; ++clip_id ) {
2911 mpls_pi *pi = new mpls_pi();
2912 pi->connection_condition = 1; // seamless
2913 // pi->uo_mask.xxx = 1;
2914 pi->in_time = pgm->start_time;
2915 pi->out_time = pgm->end_time;
2917 pi->still_mode = BLURAY_STILL_INFINITE;
2918 int64_t end_time = pgm->start_time + pgm->duration;
2919 if( pi->out_time < end_time ) pi->out_time = end_time;
2920 mpls_clip *cp = new mpls_clip();
2921 sprintf(cp->clip_id,"%05d", clip_id);
2922 pi->clip.append(cp);
2923 for( int kk=0; kk<ip->streams.size(); ++kk ) {
2924 stream *sp = ip->streams[kk];
2925 switch( sp->type ) {
2926 case AVMEDIA_TYPE_VIDEO: break;
2927 case AVMEDIA_TYPE_AUDIO: break;
2928 case AVMEDIA_TYPE_SUBTITLE: break;
2931 mpls_stream *ps = new mpls_stream();
2933 ps->stream_type = BLURAY_PG_TEXTST_STREAM;
2934 ps->coding_type = sp->coding_type;
2935 ps->format = sp->format;
2936 ps->rate = sp->rate;
2937 switch( sp->type ) {
2938 case AVMEDIA_TYPE_VIDEO:
2939 pi->stn.video.append(ps);
2941 case AVMEDIA_TYPE_AUDIO:
2942 memcpy(ps->lang, sp->lang, sizeof(ps->lang));
2943 pi->stn.audio.append(ps);
2945 case AVMEDIA_TYPE_SUBTITLE:
2946 memcpy(ps->lang, sp->lang, sizeof(ps->lang));
2947 pi->stn.pg.append(ps);
2953 pp->play_item.append(pi);
2955 // chapter marks every ch_duration ticks
2956 int64_t ch_duration = 45000 * 60*5;
2957 int64_t mrktm = ch_duration;
2959 int pmark = 0, pitem = 0;
2960 mpls_pi *pi = pp->play_item[pitem];
2961 mpls_plm *pm = new mpls_plm();
2962 pm->mark_id = pmark++;
2963 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
2964 pm->play_item_ref = pitem;
2965 pm->time = pi->in_time;
2966 pm->entry_es_pid = 0;
2967 pp->play_mark.append(pm);
2968 for( int jj=0; jj < pp->play_item.size(); ++jj ) {
2970 pi = pp->play_item[pitem];
2971 int64_t pi_duration = pi->out_time - pi->in_time;
2972 int64_t endtm = plytm + pi_duration;
2973 while( mrktm < endtm ) {
2974 pm = new mpls_plm();
2975 pm->mark_id = pmark++;
2976 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
2977 pm->play_item_ref = pitem;
2978 pm->time = pi->in_time + mrktm - plytm;
2979 pm->entry_es_pid = 0;
2980 pp->play_mark.append(pm);
2981 mrktm += ch_duration;
2985 pm = new mpls_plm();
2986 pm->mark_id = pmark;
2987 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
2988 pm->play_item_ref = pitem;
2989 pm->time = pi->out_time;
2990 pm->entry_es_pid = 0;
2991 pp->play_mark.append(pm);
2999 bd_path(const char *bp, const char *fmt, va_list ap)
3001 int n = sizeof(filename)-1;
3002 char *cp = filename;
3003 const char *pp = path;
3004 while( n>0 && (*cp=*pp)!=0 ) { --n; ++cp; ++pp; }
3005 while( n>0 && (*cp=*bp)!=0 ) { --n; ++cp; ++bp; }
3006 n -= vsnprintf(cp, n, fmt, ap);
3008 return n > 0 ? 0 : 1;
3012 bd_copy(const char *ifn, const char *fmt, ...)
3014 int bfrsz = 0x40000, ret = 1;
3016 FILE *ifp = fopen(ifn,"r");
3020 if( bd_path("/BDMV/", fmt, ap) ) return 1;
3022 FILE *ofp = fopen(filename,"w");
3024 setvbuf(ifp, 0, _IOFBF, 0x80000);
3025 setvbuf(ofp, 0, _IOFBF, 0x80000);
3028 while( !ret && n >= bfrsz ) {
3029 n = fread(bfr,1,bfrsz,ifp);
3030 if( n > 0 && (int)fwrite(bfr,1,n,ofp) != n ) {
3031 fprintf(stderr, "cant write: %s\n",filename);
3036 fprintf(stderr, "read error: %s = %m\n",ifn);
3040 fprintf(stderr, "write error: %s = %m\n",filename);
3044 fprintf(stderr, "close error: %s = %m\n",filename);
3051 fprintf(stderr, "cant copy clip %s\n",ifn);
3056 bd_open(const char *fmt, ...)
3059 if( !path ) return 0;
3062 if( bd_path("/BDMV/", fmt, ap) ) return 1;
3064 if( bs.open(filename) ) {
3065 fprintf(stderr, "cant open file %s\n",filename);
3072 bd_backup(const char *fmt, ...)
3075 if( !path ) return 0;
3078 FILE *ifp = fopen(filename,"r");
3082 if( bd_path("/BDMV/BACKUP/", fmt, ap) ) return 1;
3084 FILE *ofp = fopen(filename,"w");
3086 while( (n=fread(bfr,1,sizeof(bfr),ifp)) > 0 ) fwrite(bfr,1,n,ofp);
3093 fprintf(stderr, "cant backup %s\n",filename);
3097 int Media::write(char *fn)
3101 if( bd_open("index.bdmv") ) return 1;
3102 if( idx.write() ) return 1;
3103 if( bd_backup("index.bdmv") ) return 1;
3105 if( bd_open("MovieObject.bdmv") ) return 1;
3106 if( mov.write() ) return 1;
3107 if( bd_backup("MovieObject.bdmv") ) return 1;
3109 for( int ii=0; ii<cl.size(); ++ii ) {
3110 if( bd_open("CLIPINF/%05d.clpi", ii) ) return 1;
3111 if( cl[ii]->write() ) return 1;
3112 if( bd_backup("CLIPINF/%05d.clpi", ii) ) return 1;
3115 for( int ii=0; ii<pl.size(); ++ii ) {
3116 if( bd_open("PLAYLIST/%05d.mpls", ii) ) return 1;
3117 if( pl[ii]->write() ) return 1;
3118 if( bd_backup("PLAYLIST/%05d.mpls", ii) ) return 1;
3124 main(int ac, char **av)
3127 if( mkbdmv(path) ) return 1;
3129 av_log_set_level(AV_LOG_FATAL);
3130 //av_log_set_level(AV_LOG_VERBOSE);
3131 //av_log_set_level(AV_LOG_DEBUG);
3135 for( int ii=2; ii<ac; ++ii ) {
3137 // any dash seq followed by number sets curr title pgm_pid
3138 // single dash only sets title pgm_pid
3139 // double dash ends curr title, starts a new title
3140 // triple dash ends curr title as infinite still
3143 if( *++ap == '-' ) {
3145 if( *++ap == '-' ) { ++ap; mp->still = 1; }
3147 if( *ap >= '0' && *ap <= '9' )
3148 mp->pgm_pid = strtoul(ap,&ap,0);
3149 if( mp->brk ) mp = 0;
3151 fprintf(stderr, "err arg %d: %s\n",ii,av[ii]);
3156 mp = new media_info(av[ii]);
3159 fprintf(stderr, "cant scan media: %s\n", av[ii]);
3164 if( mp ) mp->brk = 1;
3166 if( media.compose() ) {
3167 fprintf(stderr, "cant compose media\n");
3170 if( media.write(0) ) {
3171 fprintf(stderr, "cant prepare media\n");
3174 if( media.write(path) ) {
3175 fprintf(stderr, "cant write media\n");
3179 for( int ii=0; ii<media.size(); ++ii )
3180 if( media.bd_copy(media[ii]->filename, "STREAM/%05d.m2ts", ii) )