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 {
1113 AVCodecContext *ctx;
1119 stream(AVMediaType ty, int i) {
1120 type = ty; av_idx = i; ctx = 0;
1121 start_pts = INT64_MAX; end_pts = INT64_MIN;
1124 ~stream() { if( ctx ) avcodec_free_context(&ctx); }
1129 static int cmpr(mark **, mark **);
1130 int64_t sample, pos, pts;
1132 mark(int64_t s, int64_t p, int64_t ts, uint32_t pk, int id) {
1133 sample = s; pos = p; pts = ts; pkt = pk; pid = id;
1137 int mark::cmpr(mark **a, mark **b)
1139 mark *ap = *(mark **)a, *bp = *(mark **)b;
1140 return ap->pts > bp->pts ? 1 : ap->pts < bp->pts ? -1 : 0;
1145 int pmt_pid, pcr_pid, ep_pid;
1146 int64_t start_time, end_time;
1149 memset(this, 0, sizeof(*this));
1150 start_time = INT64_MAX; end_time = INT64_MIN;
1155 class program : public _program {
1159 ArrayList<int> strm_idx;
1160 ArrayList<mark*> marks;
1162 int build_toc(clpi_ep_map_entry *map);
1163 void add_label(uint32_t pk, int64_t p, int64_t ts, int id) {
1164 marks.append(new mark(curr_pos, p, ts, pk, id));
1167 program(int i, int id) { idx = i; pid = id; curr_pos = 0; }
1168 ~program() { marks.remove_all_objects(); }
1177 memset(this, 0, sizeof(*this));
1182 class media_info : public _media_info {
1184 static const AVRational clk45k;
1185 char filename[BCTEXTLEN];
1187 int brk, pidx, still;
1188 ArrayList<stream *> streams;
1189 ArrayList<program *> programs;
1190 program *prog() { return programs[pidx]; }
1192 media_info(const char *fn) {
1193 strcpy(filename, fn);
1194 brk = 0; pidx = 0; pgm_pid = -1; still = 0;
1197 streams.remove_all_objects();
1198 programs.remove_all_objects();
1202 int scan(AVFormatContext *fmt_ctx);
1205 class Media : public ArrayList<media_info *> {
1208 char filename[BCTEXTLEN];
1209 int bd_path(const char *bp, const char *fmt, va_list ap);
1210 int bd_open(const char *fmt, ...);
1211 int bd_backup(const char *fmt, ...);
1212 int bd_copy(const char *ifn, const char *fmt, ...);
1216 ArrayList<clpi_cl *> cl;
1217 ArrayList<mpls_pl *> pl;
1219 void add_movie(uint32_t *ops, int n);
1221 int write(char *fn);
1223 Media() { path = 0; filename[0] = 0; }
1225 remove_all_objects();
1226 cl.remove_all_objects();
1227 pl.remove_all_objects();
1233 void bs_file::init()
1241 bs_file::open(const char *fn)
1244 if( !fp ) perror(fn);
1245 return fp != 0 ? 0 : 1;
1251 if( fp ) { fclose(fp); fp = 0; }
1255 bs_file::write(uint32_t v, int n)
1262 if( fp ) fputc((vv >> len), fp);
1263 if( ++fpos > fsz ) fsz = fpos;
1268 bs_file::writeb(uint8_t * bp, int n)
1270 while( --n >= 0 ) write(*bp++, 8);
1276 while( n > 32 ) { write(0, 32); n -= 32; }
1277 if( n > 0 ) write(0, n);
1281 bs_file::posb(int64_t n)
1283 if( fpos == fsz && n > fpos ) {
1288 if( fp ) fseek(fp, fpos, SEEK_SET);
1298 bs.writeb("MOBJ", 4);
1299 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1300 int movie_start = 0x0028;
1301 bs.posb(movie_start);
1304 bs.write(movies.size(), 16);
1306 for (int i = 0; i < movies.size(); ++i) {
1307 movie_obj *mov = movies[i];
1308 bs.write(mov->resume_intention_flag, 1);
1309 bs.write(mov->menu_call_mask, 1);
1310 bs.write(mov->title_search_mask, 1);
1312 ArrayList<command_obj *> &cmds = mov->cmds;
1313 int num_cmds = cmds.size();
1314 bs.write(num_cmds, 16);
1315 for (int j = 0; j < num_cmds; ++j) {
1316 command_obj *cmd = cmds[j];
1317 bs.write(cmd->op_cnt, 3);
1318 bs.write(cmd->grp, 2);
1319 bs.write(cmd->sub_grp, 3);
1320 bs.write(cmd->imm_op1, 1);
1321 bs.write(cmd->imm_op2, 1);
1323 bs.write(cmd->branch_opt, 4);
1325 bs.write(cmd->cmp_opt, 4);
1327 bs.write(cmd->set_opt, 5);
1328 //bs.write(cmd->cmd, 32);
1329 bs.write(cmd->dst, 32);
1330 bs.write(cmd->src, 32);
1334 // bs.write('l', 8);
1335 // bs.writebcd(year, 16);
1336 // bs.writebcd(month, 8);
1337 // bs.writebcd(day, 8);
1338 // bs.writebcd(hour, 8);
1339 // bs.writebcd(min, 8);
1340 // bs.writebcd(sec, 8);
1346 pb_obj::set_hdmv(int id, int pt)
1356 pb_obj::set_bdj(char *nm, int pt)
1362 bdj_name = cstrdup(nm);
1366 pb_obj::write_hdmv_obj(int id_ref)
1368 bs.write(pb_typ, 2);
1370 bs.write(id_ref, 16);
1375 pb_obj::write_bdj_obj(char *name)
1377 bs.write(pb_typ, 2);
1388 write_bdj_obj(bdj_name);
1392 write_hdmv_obj(hdmv_id);
1398 title_obj::write_obj()
1400 bs.write(obj_typ, 2);
1401 bs.write(acc_typ, 2);
1403 pb_obj::write_obj();
1408 index_file::write_pb_obj(pb_obj * pb)
1410 bs.write(pb->obj_typ, 2);
1419 bs.writeb("INDX", 4);
1420 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1422 exten.bs_zofs(bs, 32);
1423 int appinfo_start = 0x28;
1424 bs.posb(appinfo_start);
1425 appinf.bs_len(bs, 32);
1427 bs.write(initial_output_mode_preference, 1);
1428 bs.write(content_exist_flag, 1);
1430 bs.write(video_format, 4);
1431 bs.write(frame_rate, 4);
1432 bs.writeb(user_data, 32);
1437 write_pb_obj(&first_play);
1438 write_pb_obj(&top_menu);
1439 bs.write(titles.size(), 16);
1441 for (int i = 0; i < titles.size(); ++i)
1442 titles[i]->write_obj();
1444 exten.bs_len(bs,32);
1454 bs.writeb("BDID",4);
1455 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1456 bs.write(data_start, 32);
1457 bs.write(extension_data_start, 32);
1459 bs.writeb(org_id, sizeof(org_id));
1460 bs.writeb(disc_id, sizeof(disc_id));
1464 // XXX - not current referenced
1467 bdmv_write_extension_data(int start_address,
1468 int (*handler) (int, int, void *), void *handle)
1473 bs.write(length, 32); /* length of extension data block */
1477 bs.pad(32); /* relative start address of extension data */
1478 bs.pad(24); /* padding */
1479 bs.write(num_entries, 8);
1481 for (n = 0; n < num_entries; n++) {
1484 bs.write(ext_start, 32);
1485 bs.write(ext_len, 32);
1486 saved_pos = bs.pos() >> 3;
1487 bs.posb(start_address + ext_start);
1488 handler(id1, id2, handle);
1497 clpi_prog_stream::write()
1502 bs.write(coding_type, 8);
1503 switch (coding_type) {
1504 case BLURAY_STREAM_TYPE_VIDEO_MPEG1:
1505 case BLURAY_STREAM_TYPE_VIDEO_MPEG2:
1506 case BLURAY_STREAM_TYPE_VIDEO_VC1:
1507 case BLURAY_STREAM_TYPE_VIDEO_H264:
1509 bs.write(format, 4);
1511 bs.write(aspect, 4);
1513 bs.write(oc_flag, 1);
1517 case BLURAY_STREAM_TYPE_AUDIO_MPEG1:
1518 case BLURAY_STREAM_TYPE_AUDIO_MPEG2:
1519 case BLURAY_STREAM_TYPE_AUDIO_LPCM:
1520 case BLURAY_STREAM_TYPE_AUDIO_AC3:
1521 case BLURAY_STREAM_TYPE_AUDIO_DTS:
1522 case BLURAY_STREAM_TYPE_AUDIO_TRUHD:
1523 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS:
1524 case BLURAY_STREAM_TYPE_AUDIO_DTSHD:
1525 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER:
1526 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY:
1527 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY:
1528 bs.write(format, 4);
1533 case BLURAY_STREAM_TYPE_SUB_PG:
1534 case BLURAY_STREAM_TYPE_SUB_IG:
1539 case BLURAY_STREAM_TYPE_SUB_TEXT:
1540 bs.write(char_code, 8);
1545 fprintf(stderr, "clpi_prog_stream: unrecognized coding type %02x\n",
1550 bs.padb(0x15 - bs_posb(bs));
1556 clpi_cl::write_header()
1558 bs.writeb("HDMV", 4);
1559 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1560 bs.write(sequence_info_start_addr, 32);
1561 bs.write(program_info_start_addr, 32);
1562 bs.write(cpi_start_addr, 32);
1563 bs.write(clip_mark_start_addr, 32);
1564 bs.write(ext_data_start_addr, 32);
1569 clpi_atc_delta::write()
1571 bs.write(delta, 32);
1572 bs.writeb(file_id, 5);
1573 bs.writeb(file_code, 4);
1579 clpi_clip_info::write()
1584 bs.pad(16); // reserved
1585 bs.write(clip_stream_type, 8);
1586 bs.write(application_type, 8);
1587 bs.pad(31); // skip reserved 31 bits
1588 bs.write(is_atc_delta, 1);
1589 bs.write(ts_recording_rate, 32);
1590 bs.write(num_source_packets, 32);
1592 bs.padb(128); // Skip reserved 128 bytes
1594 // ts type info block
1596 bs.write(ts_len, 16);
1597 int64_t pos = bs.posb();
1599 bs.write(ts_type_info.validity, 8);
1600 bs.writeb(ts_type_info.format_id, 4);
1601 // pad the stuff we don't know anything about
1602 bs.padb(ts_len - (bs.posb() - pos));
1605 if( is_atc_delta ) {
1606 bs.pad(8); // Skip reserved byte
1607 bs.write(atc_delta.size(), 8);
1608 for( int ii=0; ii < atc_delta.size(); ++ii )
1609 if( atc_delta[ii]->write() ) return 1;
1613 if( application_type == 6 /* Sub TS for a sub-path of Text subtitle */ ) {
1615 bs.write(font_info.font.size(), 8);
1616 if( font_info.font.size() ) {
1617 for( int ii=0; ii < font_info.font.size(); ++ii ) {
1618 bs.writeb(font_info.font[ii]->file_id, 5);
1629 clpi_stc_seq::write()
1631 bs.write(pcr_pid, 16);
1632 bs.write(spn_stc_start, 32);
1633 bs.write(presentation_start_time, 32);
1634 bs.write(presentation_end_time, 32);
1639 clpi_atc_seq::write()
1641 bs.write(spn_atc_start, 32);
1642 bs.write(stc_seq.size(), 8);
1643 bs.write(offset_stc_id, 8);
1645 for( int ii=0; ii < stc_seq.size(); ++ii )
1646 if( stc_seq[ii]->write() ) return 1;
1651 clpi_sequences::write()
1654 bs.padb(1); // reserved byte
1655 bs.write(size(), 8);
1656 for( int ii=0; ii < size(); ++ii )
1657 if( get(ii)->write() ) return 1;
1665 bs.write(spn_program_sequence_start, 32);
1666 bs.write(program_map_pid, 16);
1667 bs.write(streams.size(), 8);
1668 bs.write(num_groups, 8);
1669 for( int ii=0; ii < streams.size(); ++ii )
1670 if( streams[ii]->write() ) return 1;
1675 clpi_programs::write()
1678 bs.padb(1); // reserved byte
1679 bs.write(size(), 8);
1680 for( int ii=0; ii < size(); ++ii )
1681 if( get(ii)->write() ) return 1;
1687 clpi_ep_coarse::write()
1689 bs.write(ref_ep_fine_id, 18);
1690 bs.write(pts_ep, 14);
1691 bs.write(spn_ep, 32);
1696 clpi_ep_fine::write()
1698 bs.write(is_angle_change_point, 1);
1699 bs.write(i_end_position_offset, 3);
1700 bs.write(pts_ep, 11);
1701 bs.write(spn_ep, 17);
1706 clpi_ep_map_entry::write(uint32_t ep_map_pos)
1710 bs.write(ep_stream_type, 4);
1711 bs.write(coarse.size(), 16);
1712 bs.write(fine.size(), 18);
1713 bs.write(ep_map_stream_start_addr - ep_map_pos, 32);
1718 clpi_ep_map_entry::write_map()
1720 ep_map_stream_start_addr = bs.posb();
1721 bs.write(fine_start, 32);
1723 for( int ii=0; ii < coarse.size(); ++ii )
1724 if( coarse[ii]->write() ) return 1;
1726 fine_start = bs.posb() - ep_map_stream_start_addr;
1727 for( int ii=0; ii < fine.size(); ++ii )
1728 if( fine[ii]->write() ) return 1;
1739 uint32_t ep_map_pos = bs.posb();
1741 // EP Map starts here
1743 bs.write(size(), 8);
1745 for( int ii=0; ii < size(); ++ii )
1746 if( get(ii)->write(ep_map_pos) ) return 1;
1748 for( int ii=0; ii < size(); ++ii )
1749 if( get(ii)->write_map() ) return 1;
1764 clpi_extents::write()
1767 bs.write(size(), 32);
1768 for( int ii=0; ii < size(); ++ii )
1769 bs.write(get(ii), 32);
1774 clpi_cl::write_clpi_extension(int id1, int id2, void *handle)
1776 clpi_cl *cl = (clpi_cl *) handle;
1780 // LPCM down mix coefficient
1781 //write_lpcm_down_mix_coeff(&cl->lpcm_down_mix_coeff);
1788 // Extent start point
1789 return cl->extents.write();
1793 return cl->programs_ss.write();
1797 return cl->cpi_ss.write();
1801 fprintf(stderr, "write_clpi_extension(): unhandled extension %d.%d\n", id1, id2);
1808 if( write_header() ) return 1;
1809 if( clip.write() ) return 1;
1810 sequence_info_start_addr = bs.posb();
1811 if( sequences.write() ) return 1;
1812 program_info_start_addr = bs.posb();
1813 if( programs.write() ) return 1;
1814 cpi_start_addr = bs.posb();
1815 if( cpi.write() ) return 1;
1816 clip_mark_start_addr = bs.posb();
1817 if( cmrk.write() ) return 1;
1818 //if( has_ext_data ) {
1819 // ext_data_start_addr = bs.pos();
1820 // bdmv_write_extension_data(write_clpi_extension, this);
1828 bs.write(menu_call, 1);
1829 bs.write(title_search, 1);
1830 bs.write(chapter_search, 1);
1831 bs.write(time_search, 1);
1832 bs.write(skip_to_next_point, 1);
1833 bs.write(skip_to_prev_point, 1);
1834 bs.write(play_firstplay, 1);
1836 bs.write(pause_on, 1);
1837 bs.write(pause_off, 1);
1838 bs.write(still_off, 1);
1839 bs.write(forward, 1);
1840 bs.write(backward, 1);
1841 bs.write(resume, 1);
1842 bs.write(move_up, 1);
1843 bs.write(move_down, 1);
1844 bs.write(move_left, 1);
1845 bs.write(move_right, 1);
1846 bs.write(select, 1);
1847 bs.write(activate, 1);
1848 bs.write(select_and_activate, 1);
1849 bs.write(primary_audio_change, 1);
1851 bs.write(angle_change, 1);
1852 bs.write(popup_on, 1);
1853 bs.write(popup_off, 1);
1854 bs.write(pg_enable_disable, 1);
1855 bs.write(pg_change, 1);
1856 bs.write(secondary_video_enable_disable, 1);
1857 bs.write(secondary_video_change, 1);
1858 bs.write(secondary_audio_enable_disable, 1);
1859 bs.write(secondary_audio_change, 1);
1861 bs.write(pip_pg_change, 1);
1870 bs.pad(8); // Reserved
1871 bs.write(playback_type, 8);
1872 if (playback_type == BLURAY_PLAYBACK_TYPE_RANDOM ||
1873 playback_type == BLURAY_PLAYBACK_TYPE_SHUFFLE ) {
1874 bs.write(playback_count, 16);
1877 bs.pad(16); // Reserved
1880 bs.write(random_access_flag, 1);
1881 bs.write(audio_mix_flag, 1);
1882 bs.write(lossless_bypass_flag, 1);
1883 bs.pad(13); // Reserved
1889 mpls_pl::write_header()
1891 bs.writeb("MPLS", 4);
1892 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1893 bs.write(list_pos, 32);
1894 bs.write(mark_pos, 32);
1895 bs.write(ext_pos, 32);
1896 bs.pad(160); // Skip 160 reserved bits
1906 bs.write(stream_type, 8);
1907 switch (stream_type) {
1914 bs.write(subpath_id, 8);
1915 bs.write(subclip_id, 8);
1920 bs.write(subpath_id, 8);
1925 fprintf(stderr, "unrecognized stream type %02x\n", stream_type);
1928 bs.padb(9 - strm.bs_posb(bs));
1932 bs.write(coding_type, 8);
1933 switch (coding_type) {
1934 case BLURAY_STREAM_TYPE_VIDEO_MPEG1:
1935 case BLURAY_STREAM_TYPE_VIDEO_MPEG2:
1936 case BLURAY_STREAM_TYPE_VIDEO_VC1:
1937 case BLURAY_STREAM_TYPE_VIDEO_H264:
1938 bs.write(format, 4);
1942 case BLURAY_STREAM_TYPE_AUDIO_MPEG1:
1943 case BLURAY_STREAM_TYPE_AUDIO_MPEG2:
1944 case BLURAY_STREAM_TYPE_AUDIO_LPCM:
1945 case BLURAY_STREAM_TYPE_AUDIO_AC3:
1946 case BLURAY_STREAM_TYPE_AUDIO_DTS:
1947 case BLURAY_STREAM_TYPE_AUDIO_TRUHD:
1948 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS:
1949 case BLURAY_STREAM_TYPE_AUDIO_DTSHD:
1950 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER:
1951 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY:
1952 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY:
1953 bs.write(format, 4);
1958 case BLURAY_STREAM_TYPE_SUB_PG:
1959 case BLURAY_STREAM_TYPE_SUB_IG:
1963 case BLURAY_STREAM_TYPE_SUB_TEXT:
1964 bs.write(char_code, 8);
1969 fprintf(stderr, "mpls_stream: unrecognized coding type %02x\n", coding_type);
1972 bs.padb(5 - code.bs_posb(bs));
1981 bs.pad(16); // Skip 2 reserved bytes
1983 bs.write(video.size(), 8);
1984 bs.write(audio.size(), 8);
1985 bs.write(pg.size() - num_pip_pg, 8);
1986 bs.write(ig.size(), 8);
1987 bs.write(secondary_audio.size(), 8);
1988 bs.write(secondary_video.size(), 8);
1989 bs.write(num_pip_pg, 8);
1994 // Primary Video Streams
1995 for( int ii=0; ii < video.size(); ++ii )
1996 if( video[ii]->write() ) return 1;
1998 // Primary Audio Streams
1999 for( int ii=0; ii < audio.size(); ++ii )
2000 if( audio[ii]->write() ) return 1;
2002 // Presentation Graphic Streams
2003 for( int ii=0; ii < pg.size(); ++ii )
2004 if( pg[ii]->write() ) return 1;
2006 // Interactive Graphic Streams
2007 for( int ii=0; ii < ig.size(); ++ii )
2008 if( ig[ii]->write() ) return 1;
2010 // Secondary Audio Streams
2011 for( int ii=0; ii < secondary_audio.size(); ++ii ) {
2012 if( secondary_audio[ii]->write() ) return 1;
2013 // Read Secondary Audio Extra Attributes
2014 bs.write(sa_primary_audio_ref.size(), 8);
2016 for( int jj=0; jj < sa_primary_audio_ref.size(); ++jj )
2017 bs.write(sa_primary_audio_ref[jj], 8);
2018 if( sa_primary_audio_ref.size() % 2) bs.pad(8 );
2021 // Secondary Video Streams
2022 for( int ii=0; ii < secondary_video.size(); ++ii ) {
2023 if( secondary_video[ii]->write() ) return 1;
2024 // Read Secondary Video Extra Attributes
2025 bs.write(sv_secondary_audio_ref.size(), 8);
2027 for( int jj=0; jj < sv_secondary_audio_ref.size(); ++jj )
2028 bs.write(sv_secondary_audio_ref[jj], 8);
2029 if( sv_secondary_audio_ref.size() % 2) bs.pad(8 );
2030 bs.write(sv_pip_pg_ref.size(), 8);
2032 for( int jj=0; jj < sv_pip_pg_ref.size(); ++jj )
2033 bs.write(sv_pip_pg_ref[jj], 8);
2034 if( sv_pip_pg_ref.size() % 2) bs.pad(8 );
2045 // Primary Clip identifer
2046 bs.writeb(clip[0]->clip_id, 5);
2047 bs.writeb(clip[0]->codec_id, 4); // "M2TS"
2048 bs.pad(11); // Skip reserved 11 bits
2050 bs.write(is_multi_angle, 1);
2051 bs.write(connection_condition, 4); // 0x01, 0x05, 0x06
2053 bs.write(clip[0]->stc_id, 8);
2054 bs.write(in_time, 32);
2055 bs.write(out_time, 32);
2058 bs.write(random_access_flag, 1);
2060 bs.write(still_mode, 8);
2061 if( still_mode == 0x01 ) {
2062 bs.write(still_time, 16);
2068 if( is_multi_angle ) {
2069 bs.write(clip.size(), 8);
2071 bs.write(is_different_audio, 1);
2072 bs.write(is_seamless_angle, 1);
2075 for( int ii=1; ii < clip.size(); ++ii ) {
2076 bs.writeb(clip[ii]->clip_id, 5);
2077 bs.writeb(clip[ii]->codec_id, 4); // "M2TS"
2078 bs.write(clip[ii]->stc_id, 8);
2081 if( stn.write() ) return 1;
2088 mpls_sub_pi::write()
2091 // Primary Clip identifer
2092 bs.writeb(clip[0]->clip_id, 5);
2093 bs.writeb(clip[0]->codec_id, 4); // "M2TS"
2096 bs.write(connection_condition, 4); // 0x01, 0x05, 0x06
2098 bs.write(is_multi_clip, 1);
2099 bs.write(clip[0]->stc_id, 8);
2100 bs.write(in_time, 32);
2101 bs.write(out_time, 32);
2102 bs.write(sync_play_item_id, 16);
2103 bs.write(sync_pts, 32);
2106 bs.write(clip.size(), 8);
2108 for( int ii=1; ii < clip.size(); ++ii ) {
2109 // Primary Clip identifer
2110 bs.writeb(clip[ii]->clip_id, 5);
2111 bs.writeb(clip[ii]->codec_id, 4); // "M2TS"
2112 bs.write(clip[ii]->stc_id, 8);
2126 bs.write(is_repeat, 1);
2128 bs.write(sub_play_item.size(), 8);
2130 for( int ii=0; ii < sub_play_item.size(); ++ii )
2131 if( sub_play_item[ii]->write() ) return 1;
2140 bs.write(mark_id, 8);
2141 bs.write(mark_type, 8);
2142 bs.write(play_item_ref, 16);
2144 bs.write(entry_es_pid, 16);
2145 bs.write(duration, 32);
2150 mpls_pl::write_playlistmark()
2152 mark.bs_len(bs, 32);
2153 // Then get the number of marks
2154 bs.write(play_mark.size(), 16);
2156 for( int ii=0; ii < play_mark.size(); ++ii )
2157 if( play_mark[ii]->write() ) return 1;
2164 mpls_pl::write_playlist()
2168 // Skip reserved bytes
2171 bs.write(play_item.size(), 16);
2172 bs.write(sub_path.size(), 16);
2174 for( int ii=0; ii < play_item.size(); ++ii )
2175 if( play_item[ii]->write() ) return 1;
2177 for( int ii=0; ii < sub_path.size(); ++ii )
2178 if( sub_path[ii]->write() ) return 1;
2185 mpls_pip_data::write()
2190 bs.write(scale_factor, 4);
2196 mpls_pip_metadata::write(uint32_t start_address)
2199 bs.write(clip_ref, 16);
2200 bs.write(secondary_video_ref, 8);
2202 bs.write(timeline_type, 4);
2203 bs.write(luma_key_flag, 1);
2204 bs.write(trick_play_flag, 1);
2206 if( luma_key_flag ) {
2208 bs.write(upper_limit_luma_key, 8);
2215 uint32_t data_address = 0; // XXX
2216 bs.write(data_address, 32);
2218 int64_t pos = bs.pos() / 8;
2219 bs.posb(start_address + data_address);
2221 bs.write(data.size(), 16);
2222 if( data.size() < 1 ) return 1;
2224 for( int ii=0; ii < data.size(); ++ii )
2225 if( data[ii]->write() ) return 1;
2232 mpls_pl::write_pip_metadata_extension()
2234 uint32_t pos = bs.posb();
2235 pipm.bs_len(bs, 32);
2237 bs.write(ext_pip_data.size(), 16);
2238 for( int ii=0; ii < ext_pip_data.size(); ++ii )
2239 if( ext_pip_data[ii]->write(pos) ) return 1;
2246 mpls_pl::write_subpath_extension()
2248 subx.bs_len(bs, 32);
2250 bs.write(ext_sub_path.size(), 16);
2251 for( int ii=0; ii < ext_sub_path.size(); ++ii )
2252 if( ext_sub_path[ii]->write() ) return 1;
2259 clpi_cl::write_mpls_extension(int id1, int id2, void *handle)
2261 mpls_pl *pl = (mpls_pl *) handle;
2265 // PiP metadata extension
2266 return pl->write_pip_metadata_extension();
2275 // SubPath entries extension
2276 return pl->write_subpath_extension();
2286 int ret = write_header();
2287 list_pos = bs.posb();;
2288 if( !ret ) ret = write_playlist();
2289 mark_pos = bs.posb();
2290 if( !ret ) ret = write_playlistmark();
2291 //if( has_pls_extension ) {
2292 // ext_pos = bs.posb();
2293 // bdmv_write_extension_data(write_mpls_extension, pl);
2300 if( !mkdir(path, 0777) )
2307 mk_bdmv_dir(char *bdmv_path)
2309 if( mk_dir(bdmv_path) )
2311 char bdjo_path[BCTEXTLEN];
2312 sprintf(bdjo_path, "%s/BDJO", bdmv_path);
2313 if( mk_dir(bdjo_path) )
2315 char clipinf_path[BCTEXTLEN];
2316 sprintf(clipinf_path, "%s/CLIPINF", bdmv_path);
2317 if( mk_dir(clipinf_path) )
2319 char jar_path[BCTEXTLEN];
2320 sprintf(jar_path, "%s/JAR", bdmv_path);
2321 if( mk_dir(jar_path) )
2323 char playlist_path[BCTEXTLEN];
2324 sprintf(playlist_path, "%s/PLAYLIST", bdmv_path);
2325 if( mk_dir(playlist_path) )
2333 char bdmv_path[BCTEXTLEN];
2334 sprintf(bdmv_path, "%s/BDMV", path);
2335 if( mk_bdmv_dir(bdmv_path) ) return 1;
2336 char cert_path[BCTEXTLEN];
2337 sprintf(cert_path, "%s/CERTIFICATE", path);
2338 if( mk_bdmv_dir(cert_path) ) return 1;
2339 char cert_backup[BCTEXTLEN];
2340 sprintf(cert_backup, "%s/BACKUP", cert_path);
2341 if( mk_bdmv_dir(cert_backup) ) return 1;
2342 char stream_path[BCTEXTLEN];
2343 sprintf(stream_path, "%s/STREAM", bdmv_path);
2344 if( mk_dir(stream_path) ) return 1;
2345 char auxdata_path[BCTEXTLEN];
2346 sprintf(auxdata_path, "%s/AUXDATA", bdmv_path);
2347 if( mk_dir(auxdata_path) ) return 1;
2348 char meta_path[BCTEXTLEN];
2349 sprintf(meta_path, "%s/META", bdmv_path);
2350 if( mk_dir(meta_path) ) return 1;
2351 char backup_path[BCTEXTLEN];
2352 sprintf(backup_path, "%s/BACKUP", bdmv_path);
2353 if( mk_bdmv_dir(backup_path) ) return 1;
2358 build_toc(clpi_ep_map_entry *map)
2360 clpi_ep_coarse *cp = 0;
2361 marks.sort(mark::cmpr);
2362 uint16_t ep_pid = map->pid;
2363 int64_t last_pts = -1, last_pkt = -1;
2364 for( int ii=0; ii<marks.size(); ++ii ) {
2365 mark *mp = marks[ii];
2366 if( mp->pid != ep_pid ) continue;
2367 int64_t pts = mp->pts;
2368 if( last_pts >= pts ) continue;
2370 uint32_t pkt = mp->pos / BLURAY_TS_PKTSZ;
2371 if( last_pkt >= pkt ) continue;
2373 int64_t coarse_pts = (pts >> 18); // & ~0x01;
2374 int64_t fine_pts = (pts & 0x7ffff) >> 8;
2375 uint32_t mpkt = pkt & ~0x1ffff;
2376 if( !cp || cp->pts_ep != coarse_pts || mpkt > cp->spn_ep ) {
2377 cp = new clpi_ep_coarse();
2378 map->coarse.append(cp);
2379 cp->ref_ep_fine_id = map->fine.size();
2380 cp->pts_ep = coarse_pts;
2383 clpi_ep_fine *fp = new clpi_ep_fine();
2384 map->fine.append(fp);
2385 fp->is_angle_change_point = 0;
2386 // XXX - dont know what this is
2387 fp->i_end_position_offset = 1;
2388 fp->pts_ep = fine_pts;
2389 fp->spn_ep = pkt & 0x1ffff;
2394 const AVRational media_info::clk45k = { 1, 45000 };
2396 static int bd_stream_type(AVCodecID codec_id)
2398 int stream_type = 0;
2400 case AV_CODEC_ID_MPEG1VIDEO:
2401 stream_type = BLURAY_STREAM_TYPE_VIDEO_MPEG1;
2403 case AV_CODEC_ID_MPEG2VIDEO:
2404 stream_type = BLURAY_STREAM_TYPE_VIDEO_MPEG2;
2406 case AV_CODEC_ID_H264:
2407 stream_type = BLURAY_STREAM_TYPE_VIDEO_H264;
2409 case AV_CODEC_ID_MP2:
2410 stream_type = BLURAY_STREAM_TYPE_AUDIO_MPEG1;
2412 case AV_CODEC_ID_MP3:
2413 stream_type = BLURAY_STREAM_TYPE_AUDIO_MPEG2;
2415 case AV_CODEC_ID_AC3:
2416 stream_type = BLURAY_STREAM_TYPE_AUDIO_AC3;
2418 case AV_CODEC_ID_EAC3:
2419 stream_type = BLURAY_STREAM_TYPE_AUDIO_AC3PLUS;
2421 case AV_CODEC_ID_DTS:
2422 stream_type = BLURAY_STREAM_TYPE_AUDIO_DTS;
2424 case AV_CODEC_ID_TRUEHD:
2425 stream_type = BLURAY_STREAM_TYPE_AUDIO_TRUHD;
2427 case AV_CODEC_ID_HDMV_PGS_SUBTITLE:
2428 stream_type = BLURAY_STREAM_TYPE_SUB_PG;
2431 fprintf(stderr, "unknown bluray stream type %s\n", avcodec_get_name(codec_id));
2437 static int bd_audio_format(int channels)
2439 int audio_format = 0;
2440 switch( channels ) {
2442 audio_format = BLURAY_AUDIO_FORMAT_MONO;
2445 audio_format = BLURAY_AUDIO_FORMAT_STEREO;
2448 audio_format = BLURAY_AUDIO_FORMAT_MULTI_CHAN;
2451 fprintf(stderr, "unknown bluray audio format %d ch\n", channels);
2454 return audio_format;
2457 static int bd_audio_rate(int rate)
2461 case 48000: audio_rate = BLURAY_AUDIO_RATE_48; break;
2462 case 96000: audio_rate = BLURAY_AUDIO_RATE_96; break;
2463 case 192000: audio_rate = BLURAY_AUDIO_RATE_192; break;
2465 fprintf(stderr, "unknown bluray audio rate %d\n", rate);
2471 static int bd_video_format(int w, int h, int ilace)
2473 if( w == 720 && h == 480 && ilace ) return BLURAY_VIDEO_FORMAT_480I;
2474 if( w == 720 && h == 576 && ilace ) return BLURAY_VIDEO_FORMAT_576I;
2475 if( w == 720 && h == 480 && !ilace ) return BLURAY_VIDEO_FORMAT_480P;
2476 if( w == 720 && h == 576 && !ilace ) return BLURAY_VIDEO_FORMAT_576P;
2477 // this seems to be overly restrictive
2478 if( w == 1280 && h == 720 /* && !ilace*/ ) return BLURAY_VIDEO_FORMAT_720P;
2479 if( w == 1440 && h == 1080 /* && ilace*/ ) return BLURAY_VIDEO_FORMAT_1080I;
2480 if( w == 1920 && h == 1080 /* && !ilace*/ ) return BLURAY_VIDEO_FORMAT_1080P;
2481 fprintf(stderr, "unknown bluray video format %dx%d %silace\n",
2482 w, h, !ilace ? "not " : "");
2486 static int bd_video_rate(double rate)
2488 if( fabs(rate-23.976) < 0.01 ) return BLURAY_VIDEO_RATE_24000_1001;
2489 if( fabs(rate-24.000) < 0.01 ) return BLURAY_VIDEO_RATE_24;
2490 if( fabs(rate-25.000) < 0.01 ) return BLURAY_VIDEO_RATE_25;
2491 if( fabs(rate-29.970) < 0.01 ) return BLURAY_VIDEO_RATE_30000_1001;
2492 if( fabs(rate-50.000) < 0.01 ) return BLURAY_VIDEO_RATE_50;
2493 if( fabs(rate-59.940) < 0.01 ) return BLURAY_VIDEO_RATE_60000_1001;
2494 fprintf(stderr, "unknown bluray video framerate %5.2f\n",rate);
2498 static int bd_aspect_ratio(int w, int h, double ratio)
2500 double aspect = (w * ratio) / h;
2501 if( fabs(aspect-1.333) < 0.01 ) return BLURAY_ASPECT_RATIO_4_3;
2502 if( fabs(aspect-1.777) < 0.01 ) return BLURAY_ASPECT_RATIO_16_9;
2503 return w == 720 ? BLURAY_ASPECT_RATIO_4_3 : BLURAY_ASPECT_RATIO_16_9;
2504 fprintf(stderr, "unknown bluray aspect ratio %5.3f\n",aspect);
2508 static int field_probe(AVFormatContext *fmt_ctx, AVStream *st)
2510 AVDictionary *copts = 0;
2511 //av_dict_copy(&copts, opts, 0);
2512 AVCodecID codec_id = st->codecpar->codec_id;
2513 AVCodec *decoder = avcodec_find_decoder(codec_id);
2514 AVCodecContext *ctx = avcodec_alloc_context3(decoder);
2516 fprintf(stderr,"codec alloc failed\n");
2519 avcodec_parameters_to_context(ctx, st->codecpar);
2520 if( avcodec_open2(ctx, decoder, &copts) < 0 ) {
2521 fprintf(stderr,"codec open failed\n");
2524 av_dict_free(&copts);
2526 AVFrame *ipic = av_frame_alloc();
2528 av_init_packet(&ipkt);
2530 for( int retrys=100; --retrys>=0 && ilaced<0; ) {
2531 av_packet_unref(&ipkt);
2532 int ret = av_read_frame(fmt_ctx, &ipkt);
2533 if( ret == AVERROR_EOF ) break;
2534 if( ret != 0 ) continue;
2535 if( ipkt.stream_index != st->index ) continue;
2536 if( !ipkt.data || !ipkt.size ) continue;
2537 ret = avcodec_send_packet(ctx, &ipkt);
2539 fprintf(stderr, "avcodec_send_packet failed\n");
2542 ret = avcodec_receive_frame(ctx, ipic);
2544 ilaced = ipic->interlaced_frame ? 1 : 0;
2547 if( ret != AVERROR(EAGAIN) )
2548 fprintf(stderr, "avcodec_receive_frame failed %d\n", ret);
2550 av_packet_unref(&ipkt);
2551 av_frame_free(&ipic);
2552 avcodec_free_context(&ctx);
2556 int media_info::scan()
2559 if( stat(filename, &st) ) return 1;
2560 file_size = st.st_size;
2562 AVFormatContext *fmt_ctx = 0;
2563 AVDictionary *fopts = 0;
2564 av_dict_set(&fopts, "formatprobesize", "5000000", 0);
2565 av_dict_set(&fopts, "scan_all_pmts", "1", 0);
2566 av_dict_set(&fopts, "threads", "auto", 0);
2567 int ret = avformat_open_input(&fmt_ctx, filename, NULL, &fopts);
2568 av_dict_free(&fopts);
2569 if( ret < 0 ) return ret;
2570 ret = avformat_find_stream_info(fmt_ctx, NULL);
2572 bit_rate = fmt_ctx->bit_rate;
2575 for( int i=0; ret>=0 && i<(int)fmt_ctx->nb_streams; ++i ) {
2576 AVStream *st = fmt_ctx->streams[i];
2577 AVMediaType type = st->codecpar->codec_type;
2579 case AVMEDIA_TYPE_VIDEO: break;
2580 case AVMEDIA_TYPE_AUDIO: break;
2581 case AVMEDIA_TYPE_SUBTITLE: break;
2584 stream *s = new stream(type, i);
2586 AVCodecID codec_id = st->codecpar->codec_id;
2587 AVCodec *decoder = avcodec_find_decoder(codec_id);
2588 s->ctx = avcodec_alloc_context3(decoder);
2590 fprintf(stderr, "avcodec_alloc_context failed\n");
2594 case AVMEDIA_TYPE_VIDEO: {
2595 if( ep_pid < 0 ) ep_pid = st->id;
2596 s->coding_type = bd_stream_type(codec_id);
2597 int ilace = field_probe(fmt_ctx, st);
2599 fprintf(stderr, "interlace probe failed\n");
2602 s->format = bd_video_format(st->codecpar->width, st->codecpar->height, ilace);
2603 AVRational framerate = av_guess_frame_rate(fmt_ctx, st, 0);
2604 s->rate = bd_video_rate(!framerate.den ? 0 : (double)framerate.num / framerate.den);
2605 s->aspect = bd_aspect_ratio(st->codecpar->width, st->codecpar->height,
2606 !st->sample_aspect_ratio.num || !st->sample_aspect_ratio.den ? 1. :
2607 (double)st->sample_aspect_ratio.num / st->sample_aspect_ratio.den);
2609 case AVMEDIA_TYPE_AUDIO: {
2610 s->coding_type = bd_stream_type(codec_id);
2611 s->format = bd_audio_format(st->codecpar->channels);
2612 s->rate = bd_audio_rate(st->codecpar->sample_rate);
2613 strcpy((char*)s->lang, "eng");
2615 case AVMEDIA_TYPE_SUBTITLE: {
2616 s->coding_type = bd_stream_type(codec_id);
2617 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", 0, 0);
2618 strncpy((char*)s->lang, lang ? lang->value : "und", sizeof(s->lang));
2623 if( bit_rate > 0 && st->duration == AV_NOPTS_VALUE &&
2624 st->time_base.num < INT64_MAX / bit_rate ) {
2625 st->duration = av_rescale(8*file_size, st->time_base.den,
2626 bit_rate * (int64_t) st->time_base.num);
2628 s->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2631 AVDictionary *copts = 0;
2632 ret = avcodec_open2(s->ctx, decoder, &copts);
2635 ep_pid = fmt_ctx->nb_streams > 0 ? fmt_ctx->streams[0]->id : 0;
2637 int npgm = fmt_ctx->nb_programs;
2639 program *pgm = new program(-1, 1);
2640 pgm->ep_pid = ep_pid;
2641 pgm->pmt_pid = 0x1000;
2642 pgm->pcr_pid = 0x1001;
2644 for( int jj=0; jj<streams.size(); ++jj ) {
2645 AVStream *st = fmt_ctx->streams[jj];
2646 AVMediaType type = st->codecpar->codec_type;
2648 case AVMEDIA_TYPE_VIDEO:
2649 case AVMEDIA_TYPE_AUDIO:
2654 pgm->strm_idx.append(jj);
2655 if( !pgm->duration || st->duration < pgm->duration )
2656 pgm->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2658 programs.append(pgm);
2661 for( int ii=0; ii<npgm; ++ii ) {
2662 AVProgram *pgrm = fmt_ctx->programs[ii];
2663 program *pgm = new program(ii, pgrm->id);
2664 pgm->pmt_pid = pgrm->pmt_pid;
2665 pgm->pcr_pid = pgrm->pcr_pid;
2668 for( int jj=0; jj<(int)pgrm->nb_stream_indexes; ++jj ) {
2669 int av_idx = pgrm->stream_index[jj];
2670 AVStream *st = fmt_ctx->streams[av_idx];
2671 AVMediaType type = st->codecpar->codec_type;
2673 case AVMEDIA_TYPE_VIDEO:
2674 if( ep_pid < 0 ) ep_pid = st->id;
2676 case AVMEDIA_TYPE_AUDIO:
2677 case AVMEDIA_TYPE_SUBTITLE:
2682 int sidx = streams.size();
2683 while( --sidx>=0 && streams[sidx]->av_idx!=av_idx );
2685 fprintf(stderr, "bad stream idx %d in pgm %d\n",av_idx, ii);
2688 if( !pgm->duration || st->duration < pgm->duration )
2689 pgm->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2690 pgm->strm_idx.append(sidx);
2693 AVProgram *pgrm = fmt_ctx->programs[0];
2694 ep_pid = pgrm->nb_stream_indexes > 0 ?
2695 fmt_ctx->streams[pgrm->stream_index[0]]->id : 0;
2697 pgm->ep_pid = ep_pid;
2698 programs.append(pgm);
2702 ret = scan(fmt_ctx);
2704 for( int i=0; i<(int)streams.size(); ++i )
2705 avcodec_close(streams[i]->ctx);
2706 avformat_close_input(&fmt_ctx);
2711 int media_info::scan(AVFormatContext *fmt_ctx)
2715 av_init_packet(&ipkt);
2717 // zero pts at pos zero
2718 for( int i=0; i<programs.size(); ++i ) {
2719 program *p = programs[i];
2720 for( int j=0; j<p->strm_idx.size(); ++j ) {
2721 stream *sp = streams[p->strm_idx[j]];
2723 AVStream *st = fmt_ctx->streams[sp->av_idx];
2724 p->add_label(0, 0, 0, st->id);
2728 for( int64_t count=0; ret>=0; ++count ) {
2729 av_packet_unref(&ipkt);
2730 ipkt.data = 0; ipkt.size = 0;
2732 ret = av_read_frame(fmt_ctx, &ipkt);
2734 if( ret == AVERROR_EOF ) break;
2738 if( !ipkt.data ) continue;
2739 if( (ipkt.flags & AV_PKT_FLAG_CORRUPT) ) continue;
2740 if( ipkt.pts == AV_NOPTS_VALUE ) continue;
2741 int i = ipkt.stream_index;
2742 if( i < 0 || i >= streams.size() ) continue;
2746 for( int ii=0; !pgm && ii<programs.size(); ++ii ) {
2747 program *p = programs[ii];
2748 for( int jj=0; jj<p->strm_idx.size(); ++jj ) {
2749 sp = streams[p->strm_idx[jj]];
2750 if( sp->av_idx == i ) { pgm = p; break; }
2753 if( !pgm ) continue;
2754 AVStream *st = fmt_ctx->streams[i];
2755 if( pgm->ep_pid != st->id ) continue;
2756 int64_t pts45k = av_rescale_q(ipkt.pts, st->time_base, clk45k);
2757 if( sp->start_pts > pts45k ) sp->start_pts = pts45k;
2758 if( sp->end_pts < pts45k ) sp->end_pts = pts45k;
2759 if( pgm->start_time > pts45k ) pgm->start_time = pts45k;
2760 if( pgm->end_time < pts45k ) pgm->end_time = pts45k;
2762 if( !(ipkt.flags & AV_PKT_FLAG_KEY) ) continue;
2764 if( sp->last_pts != pts45k ) {
2765 sp->last_pts = pts45k;
2766 pgm->add_label(count, ipkt.pos, pts45k, st->id);
2770 for( int ii=0; ii<programs.size(); ++ii ) {
2771 program *pgm = programs[ii];
2772 if( pgm->end_time > pgm->start_time )
2773 pgm->duration = pgm->end_time - pgm->start_time;
2776 return ret != AVERROR_EOF ? -1 : 0;
2780 Media::add_movie(uint32_t *ops, int n)
2782 movie_obj *mp = new movie_obj();
2783 mp->resume_intention_flag = 1;
2784 uint32_t *eop = ops + n/sizeof(*ops);
2785 while( ops < eop ) {
2786 command_obj *cmd = new command_obj();
2787 cmd->cmd = htobe32(*ops++);
2790 mp->cmds.append(cmd);
2792 mov.movies.append(mp);
2802 int top_menu_obj = mov.movies.size();
2803 movie_obj *mp = new movie_obj();
2804 mp->resume_intention_flag = 1;
2805 command_obj *cmd = new command_obj();
2806 cmd->cmd = htobe32(0x21810000); cmd->dst = 1; cmd->src = 0;
2807 mp->cmds.append(cmd); // JUMP_TITLE 1
2808 mov.movies.append(mp);
2811 for( int ii=0; ii<size(); ++ii ) {
2812 mp = new movie_obj();
2813 mp->resume_intention_flag = 1;
2814 cmd = new command_obj();
2815 cmd->cmd = htobe32(0x22800000); cmd->dst = ii; cmd->src = 0;
2816 mp->cmds.append(cmd); // PLAY_PL ii
2817 cmd = new command_obj();
2818 cmd->cmd = htobe32(0x00020000); cmd->dst = 0; cmd->src = 0;
2819 mp->cmds.append(cmd);
2820 mov.movies.append(mp); // BREAK
2824 int first_play_obj = mov.movies.size();
2825 mp = new movie_obj();
2826 mp->resume_intention_flag = 1;
2827 cmd = new command_obj();
2828 cmd->cmd = htobe32(0x21810000); cmd->dst = 0; cmd->src = 0;
2829 mp->cmds.append(cmd); // JUMP_TITLE 0 ; top menu
2830 mov.movies.append(mp);
2834 idx.first_play.set_hdmv(first_play_obj, pb_typ_iactv);
2835 idx.top_menu.set_hdmv(top_menu_obj, pb_typ_iactv);
2839 for( int ii=0; ii<size(); ++ii ) {
2841 tp = new title_obj();
2842 tp->set_hdmv(idx.titles.size()+1, pb_typ_movie);
2843 idx.titles.append(tp);
2846 media_info *ip = get(ii);
2847 // clip program, if specified
2848 int pidx = ip->programs.size();
2849 while( --pidx>=0 && ip->programs[pidx]->pid != ip->pgm_pid );
2850 if( pidx < 0 ) pidx = 0;
2852 ip->pgm_pid = ip->prog()->pid;
2854 clpi_cl *cp = new clpi_cl();
2855 cp->clip.clip_stream_type = 1;
2856 cp->clip.application_type = BLURAY_APP_TYPE_MAIN_MOVIE;
2857 cp->clip.ts_recording_rate = ip->bit_rate;
2858 uint32_t ts_pkt_count = ip->file_size / BLURAY_TS_PKTSZ;
2859 cp->clip.num_source_packets = ts_pkt_count;
2860 cp->clip.ts_type_info.validity = 0x80;
2861 strcpy(cp->clip.ts_type_info.format_id, "HDMV");
2864 for( int jj=0; jj<ip->programs.size(); ++jj ) {
2865 program *pgm = ip->programs[jj];
2866 clpi_prog *p = new clpi_prog(pgm->pmt_pid);
2867 for( int kk=0; kk<pgm->strm_idx.size(); ++kk ) {
2868 int k = pgm->strm_idx[kk];
2869 stream *sp = ip->streams[k];
2870 clpi_prog_stream *s = new clpi_prog_stream();
2872 s->coding_type = sp->coding_type;
2873 s->format = sp->format;
2874 //use unspecified (0)
2875 // if( !idx.video_format ) idx.video_format = s->format;
2877 // if( !idx.frame_rate ) idx.frame_rate = s->rate;
2878 switch( sp->type ) {
2879 case AVMEDIA_TYPE_VIDEO:
2880 s->aspect = sp->aspect;
2882 case AVMEDIA_TYPE_AUDIO:
2883 case AVMEDIA_TYPE_SUBTITLE:
2884 memcpy(s->lang,sp->lang,sizeof(s->lang));
2889 p->streams.append(s);
2891 clpi_ep_map_entry *map = new clpi_ep_map_entry(pgm->ep_pid);
2892 map->ep_stream_type = 1;
2893 pgm->build_toc(map);
2894 cp->cpi.append(map);
2895 cp->programs.append(p);
2897 clpi_atc_seq *atc_seq = new clpi_atc_seq();
2898 clpi_stc_seq *stc_seq = new clpi_stc_seq();
2899 stc_seq->pcr_pid = pgm->pcr_pid;
2900 stc_seq->presentation_start_time = pgm->start_time;
2901 stc_seq->presentation_end_time = pgm->end_time;
2902 atc_seq->stc_seq.append(stc_seq);
2903 cp->sequences.append(atc_seq);
2908 if( ip->brk ) tp = 0;
2911 // playlists, one per title
2912 // one playitem per media clip
2914 for( int ii=0; ii<idx.titles.size(); ++ii ) {
2916 media_info *ip = get(clip_id);
2917 program *pgm = ip->prog();
2918 mpls_pl *pp = new mpls_pl();
2919 pp->app_info.playback_type = BLURAY_PLAYBACK_TYPE_SEQUENTIAL;
2920 // pp->app_info.uo_mask.xxx = 1;
2921 int last = idx.titles[ii]->last;
2922 for( ; clip_id<=last; ++clip_id ) {
2925 mpls_pi *pi = new mpls_pi();
2926 pi->connection_condition = 1; // seamless
2927 // pi->uo_mask.xxx = 1;
2928 pi->in_time = pgm->start_time;
2929 pi->out_time = pgm->end_time;
2931 pi->still_mode = BLURAY_STILL_INFINITE;
2932 int64_t end_time = pgm->start_time + pgm->duration;
2933 if( pi->out_time < end_time ) pi->out_time = end_time;
2934 mpls_clip *cp = new mpls_clip();
2935 sprintf(cp->clip_id,"%05d", clip_id);
2936 pi->clip.append(cp);
2937 for( int kk=0; kk<ip->streams.size(); ++kk ) {
2938 stream *sp = ip->streams[kk];
2939 switch( sp->type ) {
2940 case AVMEDIA_TYPE_VIDEO: break;
2941 case AVMEDIA_TYPE_AUDIO: break;
2942 case AVMEDIA_TYPE_SUBTITLE: break;
2945 mpls_stream *ps = new mpls_stream();
2947 ps->stream_type = BLURAY_PG_TEXTST_STREAM;
2948 ps->coding_type = sp->coding_type;
2949 ps->format = sp->format;
2950 ps->rate = sp->rate;
2951 switch( sp->type ) {
2952 case AVMEDIA_TYPE_VIDEO:
2953 pi->stn.video.append(ps);
2955 case AVMEDIA_TYPE_AUDIO:
2956 memcpy(ps->lang, sp->lang, sizeof(ps->lang));
2957 pi->stn.audio.append(ps);
2959 case AVMEDIA_TYPE_SUBTITLE:
2960 memcpy(ps->lang, sp->lang, sizeof(ps->lang));
2961 pi->stn.pg.append(ps);
2967 pp->play_item.append(pi);
2969 // chapter marks every ch_duration ticks
2970 int64_t ch_duration = 45000 * 60*5;
2971 int64_t mrktm = ch_duration;
2973 int pmark = 0, pitem = 0;
2974 mpls_pi *pi = pp->play_item[pitem];
2975 mpls_plm *pm = new mpls_plm();
2976 pm->mark_id = pmark++;
2977 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
2978 pm->play_item_ref = pitem;
2979 pm->time = pi->in_time;
2980 pm->entry_es_pid = 0;
2981 pp->play_mark.append(pm);
2982 for( int jj=0; jj < pp->play_item.size(); ++jj ) {
2984 pi = pp->play_item[pitem];
2985 int64_t pi_duration = pi->out_time - pi->in_time;
2986 int64_t endtm = plytm + pi_duration;
2987 while( mrktm < endtm ) {
2988 pm = new mpls_plm();
2989 pm->mark_id = pmark++;
2990 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
2991 pm->play_item_ref = pitem;
2992 pm->time = pi->in_time + mrktm - plytm;
2993 pm->entry_es_pid = 0;
2994 pp->play_mark.append(pm);
2995 mrktm += ch_duration;
2999 pm = new mpls_plm();
3000 pm->mark_id = pmark;
3001 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
3002 pm->play_item_ref = pitem;
3003 pm->time = pi->out_time;
3004 pm->entry_es_pid = 0;
3005 pp->play_mark.append(pm);
3013 bd_path(const char *bp, const char *fmt, va_list ap)
3015 int n = sizeof(filename)-1;
3016 char *cp = filename;
3017 const char *pp = path;
3018 while( n>0 && (*cp=*pp)!=0 ) { --n; ++cp; ++pp; }
3019 while( n>0 && (*cp=*bp)!=0 ) { --n; ++cp; ++bp; }
3020 n -= vsnprintf(cp, n, fmt, ap);
3022 return n > 0 ? 0 : 1;
3026 bd_copy(const char *ifn, const char *fmt, ...)
3028 int bfrsz = 0x40000, ret = 1;
3030 FILE *ifp = fopen(ifn,"r");
3034 if( bd_path("/BDMV/", fmt, ap) ) return 1;
3036 FILE *ofp = fopen(filename,"w");
3038 setvbuf(ifp, 0, _IOFBF, 0x80000);
3039 setvbuf(ofp, 0, _IOFBF, 0x80000);
3042 while( !ret && n >= bfrsz ) {
3043 n = fread(bfr,1,bfrsz,ifp);
3044 if( n > 0 && (int)fwrite(bfr,1,n,ofp) != n ) {
3045 fprintf(stderr, "cant write: %s\n",filename);
3050 fprintf(stderr, "read error: %s = %m\n",ifn);
3054 fprintf(stderr, "write error: %s = %m\n",filename);
3058 fprintf(stderr, "close error: %s = %m\n",filename);
3065 fprintf(stderr, "cant copy clip %s\n",ifn);
3070 bd_open(const char *fmt, ...)
3073 if( !path ) return 0;
3076 if( bd_path("/BDMV/", fmt, ap) ) return 1;
3078 if( bs.open(filename) ) {
3079 fprintf(stderr, "cant open file %s\n",filename);
3086 bd_backup(const char *fmt, ...)
3089 if( !path ) return 0;
3092 FILE *ifp = fopen(filename,"r");
3096 if( bd_path("/BDMV/BACKUP/", fmt, ap) ) return 1;
3098 FILE *ofp = fopen(filename,"w");
3100 while( (n=fread(bfr,1,sizeof(bfr),ifp)) > 0 ) fwrite(bfr,1,n,ofp);
3107 fprintf(stderr, "cant backup %s\n",filename);
3111 int Media::write(char *fn)
3115 if( bd_open("index.bdmv") ) return 1;
3116 if( idx.write() ) return 1;
3117 if( bd_backup("index.bdmv") ) return 1;
3119 if( bd_open("MovieObject.bdmv") ) return 1;
3120 if( mov.write() ) return 1;
3121 if( bd_backup("MovieObject.bdmv") ) return 1;
3123 for( int ii=0; ii<cl.size(); ++ii ) {
3124 if( bd_open("CLIPINF/%05d.clpi", ii) ) return 1;
3125 if( cl[ii]->write() ) return 1;
3126 if( bd_backup("CLIPINF/%05d.clpi", ii) ) return 1;
3129 for( int ii=0; ii<pl.size(); ++ii ) {
3130 if( bd_open("PLAYLIST/%05d.mpls", ii) ) return 1;
3131 if( pl[ii]->write() ) return 1;
3132 if( bd_backup("PLAYLIST/%05d.mpls", ii) ) return 1;
3138 main(int ac, char **av)
3141 if( mkbdmv(path) ) return 1;
3142 av_log_set_level(AV_LOG_FATAL);
3143 //av_log_set_level(AV_LOG_VERBOSE);
3144 //av_log_set_level(AV_LOG_DEBUG);
3148 for( int ii=2; ii<ac; ++ii ) {
3150 // any dash seq followed by number sets curr title pgm_pid
3151 // single dash only sets title pgm_pid
3152 // double dash ends curr title, starts a new title
3153 // triple dash ends curr title as infinite still
3156 if( *++ap == '-' ) {
3158 if( *++ap == '-' ) { ++ap; mp->still = 1; }
3160 if( *ap >= '0' && *ap <= '9' )
3161 mp->pgm_pid = strtoul(ap,&ap,0);
3162 if( mp->brk ) mp = 0;
3164 fprintf(stderr, "err arg %d: %s\n",ii,av[ii]);
3169 mp = new media_info(av[ii]);
3172 fprintf(stderr, "cant scan media: %s\n", av[ii]);
3177 if( mp ) mp->brk = 1;
3179 if( media.compose() ) {
3180 fprintf(stderr, "cant compose media\n");
3183 if( media.write(0) ) {
3184 fprintf(stderr, "cant prepare media\n");
3187 if( media.write(path) ) {
3188 fprintf(stderr, "cant write media\n");
3192 for( int ii=0; ii<media.size(); ++ii )
3193 if( media.bd_copy(media[ii]->filename, "STREAM/%05d.m2ts", ii) )