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
59 #include "libavfilter/buffersrc.h"
60 #include "libavfilter/buffersink.h"
61 #include "libavformat/avformat.h"
62 #include "libavformat/avio.h"
63 #include "libavcodec/avcodec.h"
64 #include "libavfilter/avfilter.h"
65 #include "libavutil/avutil.h"
66 #include "libavutil/opt.h"
67 #include "libavutil/pixdesc.h"
68 #include "libswresample/swresample.h"
69 #include "libswscale/swscale.h"
82 BLURAY_APP_TYPE_MAIN_MOVIE = 1,
83 BLURAY_APP_TYPE_MAIN_TIMED_SLIDE_SHOW = 2,
84 BLURAY_APP_TYPE_MAIN_BROWSED_SLIDE_SHOW = 3,
85 BLURAY_APP_TYPE_SUBPATH_BROWSED_SLIDE_SHOW = 4,
86 BLURAY_APP_TYPE_SUBPATH_IG_MENU = 5,
87 BLURAY_APP_TYPE_SUBPATH_TEXT_SUBTITLE = 6,
88 BLURAY_APP_TYPE_SUBPATH_ELEMENTARY_STREAM = 7,
90 BLURAY_STREAM_TYPE_VIDEO_MPEG1 = 0x01,
91 BLURAY_STREAM_TYPE_VIDEO_MPEG2 = 0x02,
92 BLURAY_STREAM_TYPE_AUDIO_MPEG1 = 0x03,
93 BLURAY_STREAM_TYPE_AUDIO_MPEG2 = 0x04,
94 BLURAY_STREAM_TYPE_AUDIO_LPCM = 0x80,
95 BLURAY_STREAM_TYPE_AUDIO_AC3 = 0x81,
96 BLURAY_STREAM_TYPE_AUDIO_DTS = 0x82,
97 BLURAY_STREAM_TYPE_AUDIO_TRUHD = 0x83,
98 BLURAY_STREAM_TYPE_AUDIO_AC3PLUS = 0x84,
99 BLURAY_STREAM_TYPE_AUDIO_DTSHD = 0x85,
100 BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER = 0x86,
101 BLURAY_STREAM_TYPE_VIDEO_VC1 = 0xea,
102 BLURAY_STREAM_TYPE_VIDEO_H264 = 0x1b,
103 BLURAY_STREAM_TYPE_VIDEO_H264_MVC = 0x20,
104 BLURAY_STREAM_TYPE_SUB_PG = 0x90,
105 BLURAY_STREAM_TYPE_SUB_IG = 0x91,
106 BLURAY_STREAM_TYPE_SUB_TEXT = 0x92,
107 BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY = 0xa1,
108 BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY = 0xa2,
110 BLURAY_MARK_TYPE_ENTRY = 0x01,
111 BLURAY_MARK_TYPE_LINK = 0x02,
113 BLURAY_PLAYBACK_TYPE_SEQUENTIAL = 1,
114 BLURAY_PLAYBACK_TYPE_RANDOM = 2,
115 BLURAY_PLAYBACK_TYPE_SHUFFLE = 3,
117 BLURAY_VIDEO_FORMAT_480I = 1, // ITU-R BT.601-5
118 BLURAY_VIDEO_FORMAT_576I = 2, // ITU-R BT.601-4
119 BLURAY_VIDEO_FORMAT_480P = 3, // SMPTE 293M
120 BLURAY_VIDEO_FORMAT_1080I = 4, // SMPTE 274M
121 BLURAY_VIDEO_FORMAT_720P = 5, // SMPTE 296M
122 BLURAY_VIDEO_FORMAT_1080P = 6, // SMPTE 274M
123 BLURAY_VIDEO_FORMAT_576P = 7, // ITU-R BT.1358
125 BLURAY_VIDEO_RATE_24000_1001 = 1, // 23.976
126 BLURAY_VIDEO_RATE_24 = 2,
127 BLURAY_VIDEO_RATE_25 = 3,
128 BLURAY_VIDEO_RATE_30000_1001 = 4, // 29.97
129 BLURAY_VIDEO_RATE_50 = 6,
130 BLURAY_VIDEO_RATE_60000_1001 = 7, // 59.94
132 BLURAY_ASPECT_RATIO_4_3 = 2,
133 BLURAY_ASPECT_RATIO_16_9 = 3,
135 BLURAY_AUDIO_FORMAT_MONO = 1,
136 BLURAY_AUDIO_FORMAT_STEREO = 3,
137 BLURAY_AUDIO_FORMAT_MULTI_CHAN = 6,
138 BLURAY_AUDIO_FORMAT_COMBO = 12, // Stereo ac3/dts,
140 BLURAY_AUDIO_RATE_48 = 1,
141 BLURAY_AUDIO_RATE_96 = 4,
142 BLURAY_AUDIO_RATE_192 = 5,
143 BLURAY_AUDIO_RATE_192_COMBO = 12, // 48 or 96 ac3/dts, 192 mpl/dts-hd
144 BLURAY_AUDIO_RATE_96_COMBO = 14, // 48 ac3/dts, 96 mpl/dts-hd
145 BLURAY_TEXT_CHAR_CODE_UTF8 = 0x01,
146 BLURAY_TEXT_CHAR_CODE_UTF16BE = 0x02,
147 BLURAY_TEXT_CHAR_CODE_SHIFT_JIS = 0x03,
148 BLURAY_TEXT_CHAR_CODE_EUC_KR = 0x04,
149 BLURAY_TEXT_CHAR_CODE_GB18030_20001 = 0x05,
150 BLURAY_TEXT_CHAR_CODE_CN_GB = 0x06,
151 BLURAY_TEXT_CHAR_CODE_BIG5 = 0x07,
153 BLURAY_STILL_NONE = 0x00,
154 BLURAY_STILL_TIME = 0x01,
155 BLURAY_STILL_INFINITE = 0x02,
157 BLURAY_PG_TEXTST_STREAM = 0x01,
158 BLURAY_SECONDARY_VIDEO_STREAM = 0x02,
159 BLURAY_SECONDARY_AUDIO_STREAM = 0x03,
167 bs_file() { fp = 0; }
171 int open(const char *fn);
173 void write(uint32_t v, int n);
175 void padb(int n) { pad(n*8); }
176 int64_t posb() { return fpos; }
177 void posb(int64_t n);
178 int64_t pos() { return posb()*8 + len; }
179 void writeb(uint8_t * bp, int n);
180 void writeb(const char *cp, int n) {
181 writeb((uint8_t *) cp, n);
188 bs_length() { fpos = len = 0; }
189 void bs_len(bs_file &bs, int n) {
190 bs.write(len, n); fpos = bs.posb();
192 void bs_end(bs_file &bs) {
193 len = bs.posb() - fpos;
195 void bs_ofs(bs_file &bs, int n) {
196 bs.write(fpos-n/8, n);
200 class _bd_stream_info {
211 _bd_stream_info() { memset(this, 0, sizeof(*this)); }
212 ~_bd_stream_info() {}
215 class bd_stream_info : public _bd_stream_info {
221 class _bd_clip_info {
225 uint16_t still_time; /* seconds */
230 _bd_clip_info() { memset(this, 0, sizeof(*this)); }
234 class bd_clip_info : public _bd_clip_info {
236 ArrayList<bd_stream_info *> video_streams;
237 ArrayList<bd_stream_info *> audio_streams;
238 ArrayList<bd_stream_info *> pg_streams;
239 ArrayList<bd_stream_info *> ig_streams;
240 ArrayList<bd_stream_info *> sec_audio_streams;
241 ArrayList<bd_stream_info *> sec_video_streams;
245 video_streams.remove_all_objects();
246 audio_streams.remove_all_objects();
247 pg_streams.remove_all_objects();
248 ig_streams.remove_all_objects();
249 sec_audio_streams.remove_all_objects();
250 sec_video_streams.remove_all_objects();
254 class _bd_title_chapter {
262 _bd_title_chapter() { memset(this, 0, sizeof(*this)); }
263 ~_bd_title_chapter() {}
266 class bd_title_chapter : public _bd_title_chapter {
268 bd_title_chapter() {}
269 ~bd_title_chapter() {}
272 class _bd_title_mark {
281 _bd_title_mark() { memset(this, 0, sizeof(*this)); }
285 class bd_title_mark : public _bd_title_mark {
291 class _bd_title_info {
298 _bd_title_info() { memset(this, 0, sizeof(*this)); }
302 class bd_title_info : public _bd_title_info {
304 ArrayList<bd_clip_info *> clips;
305 ArrayList<bd_title_chapter *> chapters;
306 ArrayList<bd_title_mark *> marks;
310 clips.remove_all_objects();
311 chapters.remove_all_objects();
312 marks.remove_all_objects();
316 class _clpi_stc_seq {
319 uint32_t spn_stc_start;
320 uint32_t presentation_start_time;
321 uint32_t presentation_end_time;
323 _clpi_stc_seq() { memset(this, 0, sizeof(*this)); }
327 class clpi_stc_seq : public _clpi_stc_seq {
335 class _clpi_atc_seq {
337 uint32_t spn_atc_start;
338 uint8_t offset_stc_id;
340 _clpi_atc_seq() { memset(this, 0, sizeof(*this)); }
344 class clpi_atc_seq : public _clpi_atc_seq {
346 ArrayList<clpi_stc_seq *> stc_seq;
351 stc_seq.remove_all_objects();
355 class clpi_sequences : public bs_length,
356 public ArrayList<clpi_atc_seq *> {
361 ~clpi_sequences() { remove_all_objects(); }
364 class _clpi_ts_type {
369 _clpi_ts_type() { memset(this, 0, sizeof(*this)); }
373 class clpi_ts_type : public _clpi_ts_type {
379 class _clpi_atc_delta {
385 _clpi_atc_delta() { memset(this, 0, sizeof(*this)); }
386 ~_clpi_atc_delta() {}
389 class clpi_atc_delta : public _clpi_atc_delta {
401 _clpi_font() { memset(this, 0, sizeof(*this)); }
405 class clpi_font : public _clpi_font {
411 class clpi_font_info {
413 ArrayList<clpi_font *> font;
417 font.remove_all_objects();
421 class _clpi_clip_info {
423 uint8_t clip_stream_type;
424 uint8_t application_type;
425 uint8_t is_atc_delta;
426 uint32_t ts_recording_rate;
427 uint32_t num_source_packets;
429 _clpi_clip_info() { memset(this, 0, sizeof(*this)); }
430 ~_clpi_clip_info() {}
433 class clpi_clip_info : public bs_length, public _clpi_clip_info {
435 clpi_ts_type ts_type_info;
436 clpi_font_info font_info;
437 ArrayList<clpi_atc_delta *> atc_delta;
442 atc_delta.remove_all_objects();
446 class _clpi_prog_stream {
457 _clpi_prog_stream() { memset(this, 0, sizeof(*this)); }
458 ~_clpi_prog_stream() {}
461 class clpi_prog_stream : public bs_length, public _clpi_prog_stream {
465 clpi_prog_stream() {}
466 ~clpi_prog_stream() {}
469 class _clpi_ep_coarse {
475 _clpi_ep_coarse() { memset(this, 0, sizeof(*this)); }
476 ~_clpi_ep_coarse() {}
479 class clpi_ep_coarse : public _clpi_ep_coarse {
487 class _clpi_ep_fine {
489 uint8_t is_angle_change_point;
490 uint8_t i_end_position_offset;
494 _clpi_ep_fine() { memset(this, 0, sizeof(*this)); }
498 class clpi_ep_fine : public _clpi_ep_fine {
506 class _clpi_ep_map_entry {
508 uint8_t ep_stream_type;
509 uint32_t ep_map_stream_start_addr;
511 _clpi_ep_map_entry() { memset(this, 0, sizeof(*this)); }
512 ~_clpi_ep_map_entry() {}
515 class clpi_ep_map_entry : public _clpi_ep_map_entry {
519 ArrayList<clpi_ep_coarse *> coarse;
520 ArrayList<clpi_ep_fine *> fine;
521 int write(uint32_t ep_map_pos);
524 clpi_ep_map_entry(int id) { fine_start = 0; pid = id; }
525 ~clpi_ep_map_entry() {
526 coarse.remove_all_objects();
527 fine.remove_all_objects();
533 uint32_t spn_program_sequence_start;
535 _clpi_prog() { memset(this, 0, sizeof(*this)); }
539 class clpi_prog : public _clpi_prog {
541 ArrayList<clpi_prog_stream *> streams;
542 uint16_t program_map_pid;
545 clpi_prog(int pmt_pid) { program_map_pid = pmt_pid; }
546 ~clpi_prog() { streams.remove_all_objects(); }
549 class clpi_programs : public bs_length,
550 public ArrayList<clpi_prog *> {
555 ~clpi_programs() { remove_all_objects(); }
558 class clpi_extents : public bs_length,
559 public ArrayList<uint32_t> {
570 _clpi_cpi() { type = 0; }
574 class clpi_cpi : public bs_length, public _clpi_cpi,
575 public ArrayList<clpi_ep_map_entry *> {
580 ~clpi_cpi() { remove_all_objects(); }
585 unsigned int menu_call : 1;
586 unsigned int title_search : 1;
587 unsigned int chapter_search : 1;
588 unsigned int time_search : 1;
589 unsigned int skip_to_next_point : 1;
590 unsigned int skip_to_prev_point : 1;
591 unsigned int play_firstplay : 1;
592 unsigned int stop : 1;
593 unsigned int pause_on : 1;
594 unsigned int pause_off : 1;
595 unsigned int still_off : 1;
596 unsigned int forward : 1;
597 unsigned int backward : 1;
598 unsigned int resume : 1;
599 unsigned int move_up : 1;
600 unsigned int move_down : 1;
601 unsigned int move_left : 1;
602 unsigned int move_right : 1;
603 unsigned int select : 1;
604 unsigned int activate : 1;
605 unsigned int select_and_activate : 1;
606 unsigned int primary_audio_change : 1;
607 unsigned int reserved0 : 1;
608 unsigned int angle_change : 1;
609 unsigned int popup_on : 1;
610 unsigned int popup_off : 1;
611 unsigned int pg_enable_disable : 1;
612 unsigned int pg_change : 1;
613 unsigned int secondary_video_enable_disable : 1;
614 unsigned int secondary_video_change : 1;
615 unsigned int secondary_audio_enable_disable : 1;
616 unsigned int secondary_audio_change : 1;
617 unsigned int reserved1 : 1;
618 unsigned int pip_pg_change : 1;
622 memset(this, 0, sizeof(*this));
640 _mpls_stream() { memset(this, 0, sizeof(*this)); }
644 class mpls_stream : public _mpls_stream {
646 bs_length strm, code;
662 class mpls_stn : public bs_length, public _mpls_stn {
664 ArrayList<mpls_stream *> video;
665 ArrayList<mpls_stream *> audio;
666 ArrayList<mpls_stream *> pg;
667 ArrayList<mpls_stream *> ig;
668 ArrayList<mpls_stream *> secondary_audio;
669 ArrayList<mpls_stream *> secondary_video;
670 // Secondary audio specific fields
671 ArrayList<uint8_t> sa_primary_audio_ref;
672 // Secondary video specific fields
673 ArrayList<uint8_t> sv_secondary_audio_ref;
674 ArrayList<uint8_t> sv_pip_pg_ref;
679 video.remove_all_objects();
680 audio.remove_all_objects();
681 pg.remove_all_objects();
682 ig.remove_all_objects();
683 secondary_audio.remove_all_objects();
684 secondary_video.remove_all_objects();
694 _mpls_clip() { memset(this, 0, sizeof(*this)); }
698 class mpls_clip : public _mpls_clip {
700 mpls_clip() { strcpy(codec_id, "M2TS"); }
706 uint8_t is_multi_angle;
707 uint8_t connection_condition;
710 uint8_t random_access_flag;
713 uint8_t is_different_audio;
714 uint8_t is_seamless_angle;
716 _mpls_pi() { memset(this, 0, sizeof(*this)); }
720 class mpls_pi : public bs_length, public _mpls_pi {
723 ArrayList<mpls_clip *> clip;
730 clip.remove_all_objects();
738 uint16_t play_item_ref;
740 uint16_t entry_es_pid;
743 _mpls_plm() { memset(this, 0, sizeof(*this)); }
747 class mpls_plm : public _mpls_plm {
757 uint8_t playback_type;
758 uint16_t playback_count;
759 uint8_t random_access_flag;
760 uint8_t audio_mix_flag;
761 uint8_t lossless_bypass_flag;
763 _mpls_ai() { memset(this, 0, sizeof(*this)); }
767 class mpls_ai : public bs_length, public _mpls_ai {
778 uint8_t connection_condition;
779 uint8_t is_multi_clip;
782 uint16_t sync_play_item_id;
785 _mpls_sub_pi() { memset(this, 0, sizeof(*this)); }
789 class mpls_sub_pi : public bs_length, public _mpls_sub_pi {
791 ArrayList<mpls_clip *> clip;
796 clip.remove_all_objects();
805 _mpls_sub() { memset(this, 0, sizeof(*this)); }
809 class mpls_sub : public bs_length, public _mpls_sub {
811 ArrayList<mpls_sub_pi *> sub_play_item;
816 sub_play_item.remove_all_objects();
820 class _mpls_pip_data {
825 uint8_t scale_factor;
827 _mpls_pip_data() { memset(this, 0, sizeof(*this)); }
831 class mpls_pip_data : public _mpls_pip_data {
839 class _mpls_pip_metadata {
842 uint8_t secondary_video_ref;
843 uint8_t timeline_type;
844 uint8_t luma_key_flag;
845 uint8_t upper_limit_luma_key;
846 uint8_t trick_play_flag;
848 _mpls_pip_metadata() { memset(this, 0, sizeof(*this)); }
849 ~_mpls_pip_metadata() {}
852 class mpls_pip_metadata : public _mpls_pip_metadata {
854 ArrayList<mpls_pip_data *> data;
856 int write(uint32_t start_address);
857 mpls_pip_metadata() {}
858 ~mpls_pip_metadata() {
859 data.remove_all_objects();
869 _mpls_pl() { memset(this, 0, sizeof(*this)); }
873 class mpls_pl : public _mpls_pl {
876 bs_length play, mark, subx, pipm;
878 ArrayList<mpls_pi *> play_item;
879 ArrayList<mpls_sub *> sub_path;
880 ArrayList<mpls_plm *> play_mark;
881 // extension data (profile 5, version 2.4)
882 ArrayList<mpls_sub *> ext_sub_path;
883 // extension data (Picture-In-Picture metadata)
884 ArrayList<mpls_pip_metadata *> ext_pip_data;
887 int write_playlist();
888 int write_playlistmark();
889 int write_subpath_extension();
890 int write_pip_metadata_extension();
893 mpls_pl() { sig = 1; }
895 play_item.remove_all_objects();
896 sub_path.remove_all_objects();
897 play_mark.remove_all_objects();
898 ext_sub_path.remove_all_objects();
899 ext_pip_data.remove_all_objects();
905 uint32_t sequence_info_start_addr;
906 uint32_t program_info_start_addr;
907 uint32_t cpi_start_addr;
908 uint32_t clip_mark_start_addr;
909 uint32_t ext_data_start_addr;
911 _clpi_cl() { memset(this, 0, sizeof(*this)); }
915 class clpi_cl : public _clpi_cl {
919 clpi_sequences sequences;
920 clpi_programs programs;
922 clpi_extents extents;
923 clpi_programs programs_ss;
929 int write_clpi_extension(int id1, int id2, void *handle);
930 int write_mpls_extension(int id1, int id2, void *handle);
932 clpi_cl() { sig = 1; }
941 uint32_t sub_grp:3; /* command sub-group */
942 uint32_t grp:2; /* command group */
943 uint32_t op_cnt:3; /* operand count */
944 uint32_t branch_opt:4; /* branch option */
946 uint32_t imm_op2:1; /* I-flag for operand 2 */
947 uint32_t imm_op1:1; /* I-flag for operand 1 */
948 uint32_t cmp_opt:4; /* compare option */
950 uint32_t set_opt:5; /* set option */
957 command_obj() { cmd = dst = src = 0; }
963 int resume_intention_flag;
965 int title_search_mask;
967 _movie_obj() { memset(this, 0, sizeof(*this)); }
971 class movie_obj : public _movie_obj {
973 ArrayList<command_obj *> cmds;
977 cmds.remove_all_objects();
981 class movie_file : public bs_length {
984 ArrayList<movie_obj *> movies;
986 movie_file() { sig = 1; }
988 movies.remove_all_objects();
1000 _pb_obj() { memset(this, 0, sizeof(*this)); }
1004 class pb_obj : public _pb_obj {
1018 void set_hdmv(int id, int pt);
1019 void set_bdj(char *nm, int pt);
1020 void write_hdmv_obj(int id_ref);
1021 void write_bdj_obj(char *name);
1027 _title_obj() { acc_typ = 0; }
1031 class title_obj : public pb_obj, public _title_obj {
1042 int initial_output_mode_preference;
1043 int content_exist_flag;
1047 _index_file() { memset(this, 0, sizeof(*this)); }
1051 class index_file : public bs_length, public _index_file {
1053 ArrayList<title_obj *> titles;
1057 bs_length exten, appinf;
1059 void write_hdmv_obj(int hdmv_typ, int id_ref);
1060 void write_bdj_obj(int bdj_typ, char *name);
1061 void write_pb_obj(pb_obj * pb);
1066 memset(user_data, 0, sizeof(user_data));
1069 titles.remove_all_objects();
1075 uint32_t data_start;
1076 uint32_t extension_data_start;
1080 _bdid() { memset(this, 0, sizeof(*this)); }
1084 class bdid : public _bdid {
1093 class stream : public bd_stream_info {
1103 stream(AVMediaType ty, int i) {
1104 type = ty; av_idx = i;
1105 start_pts = INT64_MAX; end_pts = INT64_MIN;
1113 static int cmpr(mark **, mark **);
1114 int64_t sample, pos, pts;
1116 mark(int64_t s, int64_t p, int64_t ts, uint32_t pk, int id) {
1117 sample = s; pos = p; pts = ts; pkt = pk; pid = id;
1121 int mark::cmpr(mark **a, mark **b)
1123 mark *ap = *(mark **)a, *bp = *(mark **)b;
1124 return ap->pts > bp->pts ? 1 : ap->pts < bp->pts ? -1 : 0;
1129 int pmt_pid, pcr_pid, ep_pid;
1130 int64_t start_time, end_time;
1133 memset(this, 0, sizeof(*this));
1134 start_time = INT64_MAX; end_time = INT64_MIN;
1139 class program : public _program {
1143 ArrayList<int> strm_idx;
1144 ArrayList<mark*> marks;
1146 int build_toc(clpi_ep_map_entry *map);
1147 void add_label(uint32_t pk, int64_t p, int64_t ts, int id) {
1148 marks.append(new mark(curr_pos, p, ts, pk, id));
1151 program(int i, int id) { idx = i; pid = id; curr_pos = 0; }
1152 ~program() { marks.remove_all_objects(); }
1161 memset(this, 0, sizeof(*this));
1166 class media_info : public _media_info {
1168 static const AVRational clk45k;
1169 char filename[BCTEXTLEN];
1171 int brk, pidx, still;
1172 ArrayList<stream *> streams;
1173 ArrayList<program *> programs;
1174 program *prog() { return programs[pidx]; }
1176 media_info(const char *fn) {
1177 strcpy(filename, fn);
1178 brk = 0; pidx = 0; pgm_pid = -1; still = 0;
1181 streams.remove_all_objects();
1182 programs.remove_all_objects();
1186 int scan(AVFormatContext *fmt_ctx);
1189 class Media : public ArrayList<media_info *> {
1192 char filename[BCTEXTLEN];
1193 int bd_path(const char *bp, const char *fmt, va_list ap);
1194 int bd_open(const char *fmt, ...);
1195 int bd_backup(const char *fmt, ...);
1196 int bd_copy(const char *ifn, const char *fmt, ...);
1200 ArrayList<clpi_cl *> cl;
1201 ArrayList<mpls_pl *> pl;
1204 int write(char *fn);
1206 Media() { path = 0; filename[0] = 0; }
1208 remove_all_objects();
1209 cl.remove_all_objects();
1210 pl.remove_all_objects();
1216 void bs_file::init()
1224 bs_file::open(const char *fn)
1227 if( !fp ) perror(fn);
1228 return fp != 0 ? 0 : 1;
1234 if( fp ) { fclose(fp); fp = 0; }
1238 bs_file::write(uint32_t v, int n)
1245 if( fp ) fputc((vv >> len), fp);
1246 if( ++fpos > fsz ) fsz = fpos;
1251 bs_file::writeb(uint8_t * bp, int n)
1253 while( --n >= 0 ) write(*bp++, 8);
1259 while( n > 32 ) { write(0, 32); n -= 32; }
1260 if( n > 0 ) write(0, n);
1264 bs_file::posb(int64_t n)
1266 if( fpos == fsz && n > fpos ) {
1271 if( fp ) fseek(fp, fpos, SEEK_SET);
1281 bs.writeb("MOBJ", 4);
1282 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1283 int movie_start = 0x0028;
1284 bs.posb(movie_start);
1287 bs.write(movies.size(), 16);
1289 for (int i = 0; i < movies.size(); ++i) {
1290 movie_obj *mov = movies[i];
1291 bs.write(mov->resume_intention_flag, 1);
1292 bs.write(mov->menu_call_mask, 1);
1293 bs.write(mov->title_search_mask, 1);
1295 ArrayList<command_obj *> &cmds = mov->cmds;
1296 int num_cmds = cmds.size();
1297 bs.write(num_cmds, 16);
1298 for (int j = 0; j < num_cmds; ++j) {
1299 command_obj *cmd = cmds[j];
1300 bs.write(cmd->op_cnt, 3);
1301 bs.write(cmd->grp, 2);
1302 bs.write(cmd->sub_grp, 3);
1303 bs.write(cmd->imm_op1, 1);
1304 bs.write(cmd->imm_op2, 1);
1306 bs.write(cmd->branch_opt, 4);
1308 bs.write(cmd->cmp_opt, 4);
1310 bs.write(cmd->set_opt, 5);
1311 //bs.write(cmd->cmd, 32);
1312 bs.write(cmd->dst, 32);
1313 bs.write(cmd->src, 32);
1317 // bs.write('l', 8);
1318 // bs.writebcd(year, 16);
1319 // bs.writebcd(month, 8);
1320 // bs.writebcd(day, 8);
1321 // bs.writebcd(hour, 8);
1322 // bs.writebcd(min, 8);
1323 // bs.writebcd(sec, 8);
1329 pb_obj::set_hdmv(int id, int pt)
1339 pb_obj::set_bdj(char *nm, int pt)
1345 bdj_name = cstrdup(nm);
1349 pb_obj::write_hdmv_obj(int id_ref)
1351 bs.write(pb_typ, 2);
1353 bs.write(id_ref, 16);
1358 pb_obj::write_bdj_obj(char *name)
1360 bs.write(pb_typ, 2);
1371 write_bdj_obj(bdj_name);
1375 write_hdmv_obj(hdmv_id);
1381 title_obj::write_obj()
1383 bs.write(obj_typ, 2);
1384 bs.write(acc_typ, 2);
1386 pb_obj::write_obj();
1391 index_file::write_pb_obj(pb_obj * pb)
1393 bs.write(pb->obj_typ, 2);
1402 bs.writeb("INDX", 4);
1403 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1405 exten.bs_ofs(bs, 32);
1406 int appinfo_start = 0x28;
1407 bs.posb(appinfo_start);
1408 appinf.bs_len(bs, 32);
1410 bs.write(initial_output_mode_preference, 1);
1411 bs.write(content_exist_flag, 1);
1413 bs.write(video_format, 4);
1414 bs.write(frame_rate, 4);
1415 bs.writeb(user_data, 32);
1420 write_pb_obj(&first_play);
1421 write_pb_obj(&top_menu);
1422 bs.write(titles.size(), 16);
1424 for (int i = 0; i < titles.size(); ++i)
1425 titles[i]->write_obj();
1427 exten.bs_len(bs,32);
1437 bs.writeb("BDID",4);
1438 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1439 bs.write(data_start, 32);
1440 bs.write(extension_data_start, 32);
1442 bs.writeb(org_id, sizeof(org_id));
1443 bs.writeb(disc_id, sizeof(disc_id));
1447 // XXX - not current referenced
1450 bdmv_write_extension_data(int start_address,
1451 int (*handler) (int, int, void *), void *handle)
1456 bs.write(length, 32); /* length of extension data block */
1460 bs.pad(32); /* relative start address of extension data */
1461 bs.pad(24); /* padding */
1462 bs.write(num_entries, 8);
1464 for (n = 0; n < num_entries; n++) {
1467 bs.write(ext_start, 32);
1468 bs.write(ext_len, 32);
1469 saved_pos = bs.pos() >> 3;
1470 bs.posb(start_address + ext_start);
1471 handler(id1, id2, handle);
1480 clpi_prog_stream::write()
1485 bs.write(coding_type, 8);
1486 switch (coding_type) {
1487 case BLURAY_STREAM_TYPE_VIDEO_MPEG1:
1488 case BLURAY_STREAM_TYPE_VIDEO_MPEG2:
1489 case BLURAY_STREAM_TYPE_VIDEO_VC1:
1490 case BLURAY_STREAM_TYPE_VIDEO_H264:
1492 bs.write(format, 4);
1494 bs.write(aspect, 4);
1496 bs.write(oc_flag, 1);
1500 case BLURAY_STREAM_TYPE_AUDIO_MPEG1:
1501 case BLURAY_STREAM_TYPE_AUDIO_MPEG2:
1502 case BLURAY_STREAM_TYPE_AUDIO_LPCM:
1503 case BLURAY_STREAM_TYPE_AUDIO_AC3:
1504 case BLURAY_STREAM_TYPE_AUDIO_DTS:
1505 case BLURAY_STREAM_TYPE_AUDIO_TRUHD:
1506 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS:
1507 case BLURAY_STREAM_TYPE_AUDIO_DTSHD:
1508 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER:
1509 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY:
1510 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY:
1511 bs.write(format, 4);
1516 case BLURAY_STREAM_TYPE_SUB_PG:
1517 case BLURAY_STREAM_TYPE_SUB_IG:
1522 case BLURAY_STREAM_TYPE_SUB_TEXT:
1523 bs.write(char_code, 8);
1528 fprintf(stderr, "clpi_prog_stream: unrecognized coding type %02x\n",
1538 clpi_cl::write_header()
1540 bs.writeb("HDMV", 4);
1541 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1542 bs.write(sequence_info_start_addr, 32);
1543 bs.write(program_info_start_addr, 32);
1544 bs.write(cpi_start_addr, 32);
1545 bs.write(clip_mark_start_addr, 32);
1546 bs.write(ext_data_start_addr, 32);
1551 clpi_atc_delta::write()
1553 bs.write(delta, 32);
1554 bs.writeb(file_id, 5);
1555 bs.writeb(file_code, 4);
1561 clpi_clip_info::write()
1566 bs.pad(16); // reserved
1567 bs.write(clip_stream_type, 8);
1568 bs.write(application_type, 8);
1569 bs.pad(31); // skip reserved 31 bits
1570 bs.write(is_atc_delta, 1);
1571 bs.write(ts_recording_rate, 32);
1572 bs.write(num_source_packets, 32);
1574 bs.padb(128); // Skip reserved 128 bytes
1576 // ts type info block
1578 bs.write(ts_len, 16);
1579 int64_t pos = bs.posb();
1581 bs.write(ts_type_info.validity, 8);
1582 bs.writeb(ts_type_info.format_id, 4);
1583 // pad the stuff we don't know anything about
1584 bs.padb(ts_len - (bs.posb() - pos));
1587 if( is_atc_delta ) {
1588 bs.pad(8); // Skip reserved byte
1589 bs.write(atc_delta.size(), 8);
1590 for( int ii=0; ii < atc_delta.size(); ++ii )
1591 if( atc_delta[ii]->write() ) return 1;
1595 if( application_type == 6 /* Sub TS for a sub-path of Text subtitle */ ) {
1597 bs.write(font_info.font.size(), 8);
1598 if( font_info.font.size() ) {
1599 for( int ii=0; ii < font_info.font.size(); ++ii ) {
1600 bs.writeb(font_info.font[ii]->file_id, 5);
1611 clpi_stc_seq::write()
1613 bs.write(pcr_pid, 16);
1614 bs.write(spn_stc_start, 32);
1615 bs.write(presentation_start_time, 32);
1616 bs.write(presentation_end_time, 32);
1621 clpi_atc_seq::write()
1623 bs.write(spn_atc_start, 32);
1624 bs.write(stc_seq.size(), 8);
1625 bs.write(offset_stc_id, 8);
1627 for( int ii=0; ii < stc_seq.size(); ++ii )
1628 if( stc_seq[ii]->write() ) return 1;
1633 clpi_sequences::write()
1636 bs.padb(1); // reserved byte
1637 bs.write(size(), 8);
1638 for( int ii=0; ii < size(); ++ii )
1639 if( get(ii)->write() ) return 1;
1647 bs.write(spn_program_sequence_start, 32);
1648 bs.write(program_map_pid, 16);
1649 bs.write(streams.size(), 8);
1650 bs.write(num_groups, 8);
1651 for( int ii=0; ii < streams.size(); ++ii )
1652 if( streams[ii]->write() ) return 1;
1657 clpi_programs::write()
1660 bs.padb(1); // reserved byte
1661 bs.write(size(), 8);
1662 for( int ii=0; ii < size(); ++ii )
1663 if( get(ii)->write() ) return 1;
1669 clpi_ep_coarse::write()
1671 bs.write(ref_ep_fine_id, 18);
1672 bs.write(pts_ep, 14);
1673 bs.write(spn_ep, 32);
1678 clpi_ep_fine::write()
1680 bs.write(is_angle_change_point, 1);
1681 bs.write(i_end_position_offset, 3);
1682 bs.write(pts_ep, 11);
1683 bs.write(spn_ep, 17);
1688 clpi_ep_map_entry::write(uint32_t ep_map_pos)
1692 bs.write(ep_stream_type, 4);
1693 bs.write(coarse.size(), 16);
1694 bs.write(fine.size(), 18);
1695 bs.write(ep_map_stream_start_addr - ep_map_pos, 32);
1700 clpi_ep_map_entry::write_map()
1702 ep_map_stream_start_addr = bs.posb();
1703 bs.write(fine_start, 32);
1705 for( int ii=0; ii < coarse.size(); ++ii )
1706 if( coarse[ii]->write() ) return 1;
1708 fine_start = bs.posb() - ep_map_stream_start_addr;
1709 for( int ii=0; ii < fine.size(); ++ii )
1710 if( fine[ii]->write() ) return 1;
1721 uint32_t ep_map_pos = bs.posb();
1723 // EP Map starts here
1725 bs.write(size(), 8);
1727 for( int ii=0; ii < size(); ++ii )
1728 if( get(ii)->write(ep_map_pos) ) return 1;
1730 for( int ii=0; ii < size(); ++ii )
1731 if( get(ii)->write_map() ) return 1;
1738 clpi_extents::write()
1741 bs.write(size(), 32);
1742 for( int ii=0; ii < size(); ++ii )
1743 bs.write(get(ii), 32);
1748 clpi_cl::write_clpi_extension(int id1, int id2, void *handle)
1750 clpi_cl *cl = (clpi_cl *) handle;
1754 // LPCM down mix coefficient
1755 //write_lpcm_down_mix_coeff(&cl->lpcm_down_mix_coeff);
1762 // Extent start point
1763 return cl->extents.write();
1767 return cl->programs_ss.write();
1771 return cl->cpi_ss.write();
1775 fprintf(stderr, "write_clpi_extension(): unhandled extension %d.%d\n", id1, id2);
1782 if( write_header() ) return 1;
1783 if( clip.write() ) return 1;
1784 sequence_info_start_addr = bs.posb();
1785 if( sequences.write() ) return 1;
1786 program_info_start_addr = bs.posb();
1787 if( programs.write() ) return 1;
1788 cpi_start_addr = bs.posb();
1789 if( cpi.write() ) return 1;
1790 //if( has_ext_data ) {
1791 // ext_data_start_addr = bs.pos();
1792 // bdmv_write_extension_data(write_clpi_extension, this);
1800 bs.write(menu_call, 1);
1801 bs.write(title_search, 1);
1802 bs.write(chapter_search, 1);
1803 bs.write(time_search, 1);
1804 bs.write(skip_to_next_point, 1);
1805 bs.write(skip_to_prev_point, 1);
1806 bs.write(play_firstplay, 1);
1808 bs.write(pause_on, 1);
1809 bs.write(pause_off, 1);
1810 bs.write(still_off, 1);
1811 bs.write(forward, 1);
1812 bs.write(backward, 1);
1813 bs.write(resume, 1);
1814 bs.write(move_up, 1);
1815 bs.write(move_down, 1);
1816 bs.write(move_left, 1);
1817 bs.write(move_right, 1);
1818 bs.write(select, 1);
1819 bs.write(activate, 1);
1820 bs.write(select_and_activate, 1);
1821 bs.write(primary_audio_change, 1);
1823 bs.write(angle_change, 1);
1824 bs.write(popup_on, 1);
1825 bs.write(popup_off, 1);
1826 bs.write(pg_enable_disable, 1);
1827 bs.write(pg_change, 1);
1828 bs.write(secondary_video_enable_disable, 1);
1829 bs.write(secondary_video_change, 1);
1830 bs.write(secondary_audio_enable_disable, 1);
1831 bs.write(secondary_audio_change, 1);
1833 bs.write(pip_pg_change, 1);
1842 bs.pad(8); // Reserved
1843 bs.write(playback_type, 8);
1844 if (playback_type == BLURAY_PLAYBACK_TYPE_RANDOM ||
1845 playback_type == BLURAY_PLAYBACK_TYPE_SHUFFLE ) {
1846 bs.write(playback_count, 16);
1849 bs.pad(16); // Reserved
1852 bs.write(random_access_flag, 1);
1853 bs.write(audio_mix_flag, 1);
1854 bs.write(lossless_bypass_flag, 1);
1855 bs.pad(13); // Reserved
1861 mpls_pl::write_header()
1863 bs.writeb("MPLS", 4);
1864 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1865 bs.write(list_pos, 32);
1866 bs.write(mark_pos, 32);
1867 bs.write(ext_pos, 32);
1868 bs.pad(160); // Skip 160 reserved bits
1878 bs.write(stream_type, 8);
1879 switch (stream_type) {
1886 bs.write(subpath_id, 8);
1887 bs.write(subclip_id, 8);
1892 bs.write(subpath_id, 8);
1897 fprintf(stderr, "unrecognized stream type %02x\n", stream_type);
1903 bs.write(coding_type, 8);
1904 switch (coding_type) {
1905 case BLURAY_STREAM_TYPE_VIDEO_MPEG1:
1906 case BLURAY_STREAM_TYPE_VIDEO_MPEG2:
1907 case BLURAY_STREAM_TYPE_VIDEO_VC1:
1908 case BLURAY_STREAM_TYPE_VIDEO_H264:
1909 bs.write(format, 4);
1913 case BLURAY_STREAM_TYPE_AUDIO_MPEG1:
1914 case BLURAY_STREAM_TYPE_AUDIO_MPEG2:
1915 case BLURAY_STREAM_TYPE_AUDIO_LPCM:
1916 case BLURAY_STREAM_TYPE_AUDIO_AC3:
1917 case BLURAY_STREAM_TYPE_AUDIO_DTS:
1918 case BLURAY_STREAM_TYPE_AUDIO_TRUHD:
1919 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS:
1920 case BLURAY_STREAM_TYPE_AUDIO_DTSHD:
1921 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER:
1922 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY:
1923 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY:
1924 bs.write(format, 4);
1929 case BLURAY_STREAM_TYPE_SUB_PG:
1930 case BLURAY_STREAM_TYPE_SUB_IG:
1934 case BLURAY_STREAM_TYPE_SUB_TEXT:
1935 bs.write(char_code, 8);
1940 fprintf(stderr, "mpls_stream: unrecognized coding type %02x\n", coding_type);
1951 bs.pad(16); // Skip 2 reserved bytes
1953 bs.write(video.size(), 8);
1954 bs.write(audio.size(), 8);
1955 bs.write(pg.size() - num_pip_pg, 8);
1956 bs.write(ig.size(), 8);
1957 bs.write(secondary_audio.size(), 8);
1958 bs.write(secondary_video.size(), 8);
1959 bs.write(num_pip_pg, 8);
1964 // Primary Video Streams
1965 for( int ii=0; ii < video.size(); ++ii )
1966 if( video[ii]->write() ) return 1;
1968 // Primary Audio Streams
1969 for( int ii=0; ii < audio.size(); ++ii )
1970 if( audio[ii]->write() ) return 1;
1972 // Presentation Graphic Streams
1973 for( int ii=0; ii < pg.size(); ++ii )
1974 if( pg[ii]->write() ) return 1;
1976 // Interactive Graphic Streams
1977 for( int ii=0; ii < ig.size(); ++ii )
1978 if( ig[ii]->write() ) return 1;
1980 // Secondary Audio Streams
1981 for( int ii=0; ii < secondary_audio.size(); ++ii ) {
1982 if( secondary_audio[ii]->write() ) return 1;
1983 // Read Secondary Audio Extra Attributes
1984 bs.write(sa_primary_audio_ref.size(), 8);
1986 for( int jj=0; jj < sa_primary_audio_ref.size(); ++jj )
1987 bs.write(sa_primary_audio_ref[jj], 8);
1988 if( sa_primary_audio_ref.size() % 2) bs.pad(8 );
1991 // Secondary Video Streams
1992 for( int ii=0; ii < secondary_video.size(); ++ii ) {
1993 if( secondary_video[ii]->write() ) return 1;
1994 // Read Secondary Video Extra Attributes
1995 bs.write(sv_secondary_audio_ref.size(), 8);
1997 for( int jj=0; jj < sv_secondary_audio_ref.size(); ++jj )
1998 bs.write(sv_secondary_audio_ref[jj], 8);
1999 if( sv_secondary_audio_ref.size() % 2) bs.pad(8 );
2000 bs.write(sv_pip_pg_ref.size(), 8);
2002 for( int jj=0; jj < sv_pip_pg_ref.size(); ++jj )
2003 bs.write(sv_pip_pg_ref[jj], 8);
2004 if( sv_pip_pg_ref.size() % 2) bs.pad(8 );
2015 // Primary Clip identifer
2016 bs.writeb(clip[0]->clip_id, 5);
2017 bs.writeb(clip[0]->codec_id, 4); // "M2TS"
2018 bs.pad(11); // Skip reserved 11 bits
2020 bs.write(is_multi_angle, 1);
2021 bs.write(connection_condition, 4); // 0x01, 0x05, 0x06
2023 bs.write(clip[0]->stc_id, 8);
2024 bs.write(in_time, 32);
2025 bs.write(out_time, 32);
2028 bs.write(random_access_flag, 1);
2030 bs.write(still_mode, 8);
2031 if( still_mode == 0x01 ) {
2032 bs.write(still_time, 16);
2038 if( is_multi_angle ) {
2039 bs.write(clip.size(), 8);
2041 bs.write(is_different_audio, 1);
2042 bs.write(is_seamless_angle, 1);
2045 for( int ii=1; ii < clip.size(); ++ii ) {
2046 bs.writeb(clip[ii]->clip_id, 5);
2047 bs.writeb(clip[ii]->codec_id, 4); // "M2TS"
2048 bs.write(clip[ii]->stc_id, 8);
2051 if( stn.write() ) return 1;
2058 mpls_sub_pi::write()
2061 // Primary Clip identifer
2062 bs.writeb(clip[0]->clip_id, 5);
2063 bs.writeb(clip[0]->codec_id, 4); // "M2TS"
2066 bs.write(connection_condition, 4); // 0x01, 0x05, 0x06
2068 bs.write(is_multi_clip, 1);
2069 bs.write(clip[0]->stc_id, 8);
2070 bs.write(in_time, 32);
2071 bs.write(out_time, 32);
2072 bs.write(sync_play_item_id, 16);
2073 bs.write(sync_pts, 32);
2076 bs.write(clip.size(), 8);
2078 for( int ii=1; ii < clip.size(); ++ii ) {
2079 // Primary Clip identifer
2080 bs.writeb(clip[ii]->clip_id, 5);
2081 bs.writeb(clip[ii]->codec_id, 4); // "M2TS"
2082 bs.write(clip[ii]->stc_id, 8);
2096 bs.write(is_repeat, 1);
2098 bs.write(sub_play_item.size(), 8);
2100 for( int ii=0; ii < sub_play_item.size(); ++ii )
2101 if( sub_play_item[ii]->write() ) return 1;
2110 bs.write(mark_id, 8);
2111 bs.write(mark_type, 8);
2112 bs.write(play_item_ref, 16);
2114 bs.write(entry_es_pid, 16);
2115 bs.write(duration, 32);
2120 mpls_pl::write_playlistmark()
2122 mark.bs_len(bs, 32);
2123 // Then get the number of marks
2124 bs.write(play_mark.size(), 16);
2126 for( int ii=0; ii < play_mark.size(); ++ii )
2127 if( play_mark[ii]->write() ) return 1;
2134 mpls_pl::write_playlist()
2138 // Skip reserved bytes
2141 bs.write(play_item.size(), 16);
2142 bs.write(sub_path.size(), 16);
2144 for( int ii=0; ii < play_item.size(); ++ii )
2145 if( play_item[ii]->write() ) return 1;
2147 for( int ii=0; ii < sub_path.size(); ++ii )
2148 if( sub_path[ii]->write() ) return 1;
2155 mpls_pip_data::write()
2160 bs.write(scale_factor, 4);
2166 mpls_pip_metadata::write(uint32_t start_address)
2169 bs.write(clip_ref, 16);
2170 bs.write(secondary_video_ref, 8);
2172 bs.write(timeline_type, 4);
2173 bs.write(luma_key_flag, 1);
2174 bs.write(trick_play_flag, 1);
2176 if( luma_key_flag ) {
2178 bs.write(upper_limit_luma_key, 8);
2185 uint32_t data_address = 0; // XXX
2186 bs.write(data_address, 32);
2188 int64_t pos = bs.pos() / 8;
2189 bs.posb(start_address + data_address);
2191 bs.write(data.size(), 16);
2192 if( data.size() < 1 ) return 1;
2194 for( int ii=0; ii < data.size(); ++ii )
2195 if( data[ii]->write() ) return 1;
2202 mpls_pl::write_pip_metadata_extension()
2204 uint32_t pos = bs.posb();
2205 pipm.bs_len(bs, 32);
2207 bs.write(ext_pip_data.size(), 16);
2208 for( int ii=0; ii < ext_pip_data.size(); ++ii )
2209 if( ext_pip_data[ii]->write(pos) ) return 1;
2216 mpls_pl::write_subpath_extension()
2218 subx.bs_len(bs, 32);
2220 bs.write(ext_sub_path.size(), 16);
2221 for( int ii=0; ii < ext_sub_path.size(); ++ii )
2222 if( ext_sub_path[ii]->write() ) return 1;
2229 clpi_cl::write_mpls_extension(int id1, int id2, void *handle)
2231 mpls_pl *pl = (mpls_pl *) handle;
2235 // PiP metadata extension
2236 return pl->write_pip_metadata_extension();
2245 // SubPath entries extension
2246 return pl->write_subpath_extension();
2256 int ret = write_header();
2257 list_pos = bs.posb();;
2258 if( !ret ) ret = write_playlist();
2259 mark_pos = bs.posb();
2260 if( !ret ) ret = write_playlistmark();
2261 //if( has_pls_extension ) {
2262 // ext_pos = bs.posb();
2263 // bdmv_write_extension_data(write_mpls_extension, pl);
2270 if( !mkdir(path, 0777) )
2277 mk_bdmv_dir(char *bdmv_path)
2279 if( mk_dir(bdmv_path) )
2281 char bdjo_path[BCTEXTLEN];
2282 sprintf(bdjo_path, "%s/BDJO", bdmv_path);
2283 if( mk_dir(bdjo_path) )
2285 char clipinf_path[BCTEXTLEN];
2286 sprintf(clipinf_path, "%s/CLIPINF", bdmv_path);
2287 if( mk_dir(clipinf_path) )
2289 char jar_path[BCTEXTLEN];
2290 sprintf(jar_path, "%s/JAR", bdmv_path);
2291 if( mk_dir(jar_path) )
2293 char playlist_path[BCTEXTLEN];
2294 sprintf(playlist_path, "%s/PLAYLIST", bdmv_path);
2295 if( mk_dir(playlist_path) )
2303 char bdmv_path[BCTEXTLEN];
2304 sprintf(bdmv_path, "%s/BDMV", path);
2305 if( mk_bdmv_dir(bdmv_path) ) return 1;
2306 char stream_path[BCTEXTLEN];
2307 sprintf(stream_path, "%s/BDMV/STREAM", path);
2308 if( mk_dir(stream_path) ) return 1;
2309 char auxdata_path[BCTEXTLEN];
2310 sprintf(auxdata_path, "%s/BDMV/AUXDATA", path);
2311 if( mk_dir(auxdata_path) ) return 1;
2312 char meta_path[BCTEXTLEN];
2313 sprintf(meta_path, "%s/BDMV/META", path);
2314 if( mk_dir(meta_path) ) return 1;
2315 char backup_path[BCTEXTLEN];
2316 sprintf(backup_path, "%s/BACKUP", bdmv_path);
2317 if( mk_bdmv_dir(backup_path) ) return 1;
2322 build_toc(clpi_ep_map_entry *map)
2324 clpi_ep_coarse *cp = 0;
2325 marks.sort(mark::cmpr);
2326 uint16_t ep_pid = map->pid;
2327 int64_t last_pts = -1, last_pkt = -1;
2328 for( int ii=0; ii<marks.size(); ++ii ) {
2329 mark *mp = marks[ii];
2330 if( mp->pid != ep_pid ) continue;
2331 int64_t pts = mp->pts;
2332 if( last_pts >= pts ) continue;
2334 uint32_t pkt = mp->pos / BLURAY_TS_PKTSZ;
2335 if( last_pkt >= pkt ) continue;
2337 int64_t coarse_pts = (pts >> 18) & ~0x01;
2338 int64_t fine_pts = (pts & 0x7ffff) >> 8;
2339 uint32_t mpkt = pkt & ~0x1ffff;
2340 if( !cp || cp->pts_ep != coarse_pts || cp->spn_ep != mpkt ) {
2341 cp = new clpi_ep_coarse();
2342 map->coarse.append(cp);
2343 cp->ref_ep_fine_id = map->fine.size();
2344 cp->pts_ep = coarse_pts;
2347 clpi_ep_fine *fp = new clpi_ep_fine();
2348 map->fine.append(fp);
2349 fp->is_angle_change_point = 0;
2350 // XXX - dont know what this is
2351 fp->i_end_position_offset = 1;
2352 fp->pts_ep = fine_pts;
2353 fp->spn_ep = pkt & 0x1ffff;
2358 const AVRational media_info::clk45k = { 1, 45000 };
2360 static int bd_stream_type(AVCodecID codec_id)
2362 int stream_type = 0;
2364 case AV_CODEC_ID_MPEG1VIDEO:
2365 case AV_CODEC_ID_MPEG2VIDEO:
2366 stream_type = BLURAY_STREAM_TYPE_VIDEO_MPEG2;
2368 case AV_CODEC_ID_H264:
2369 stream_type = BLURAY_STREAM_TYPE_VIDEO_H264;
2371 case AV_CODEC_ID_MP2:
2372 case AV_CODEC_ID_MP3:
2373 stream_type = BLURAY_STREAM_TYPE_AUDIO_MPEG1;
2375 case AV_CODEC_ID_AC3:
2376 stream_type = BLURAY_STREAM_TYPE_AUDIO_AC3;
2378 case AV_CODEC_ID_DTS:
2379 stream_type = BLURAY_STREAM_TYPE_AUDIO_DTS;
2381 case AV_CODEC_ID_TRUEHD:
2382 stream_type = BLURAY_STREAM_TYPE_AUDIO_TRUHD;
2385 fprintf(stderr, "unknown bluray stream type %s\n", avcodec_get_name(codec_id));
2391 static int bd_audio_format(int channels)
2393 int audio_format = 0;
2394 switch( channels ) {
2396 audio_format = BLURAY_AUDIO_FORMAT_MONO;
2399 audio_format = BLURAY_AUDIO_FORMAT_STEREO;
2402 audio_format = BLURAY_AUDIO_FORMAT_MULTI_CHAN;
2405 fprintf(stderr, "unknown bluray audio format %d ch\n", channels);
2408 return audio_format;
2411 static int bd_audio_rate(int rate)
2415 case 48000: audio_rate = BLURAY_AUDIO_RATE_48; break;
2416 case 96000: audio_rate = BLURAY_AUDIO_RATE_96; break;
2417 case 192000: audio_rate = BLURAY_AUDIO_RATE_192; break;
2419 fprintf(stderr, "unknown bluray audio rate %d\n", rate);
2425 static int bd_video_format(int w, int h, int ilace)
2427 if( w == 720 && h == 480 && ilace ) return BLURAY_VIDEO_FORMAT_480I;
2428 if( w == 720 && h == 576 && ilace ) return BLURAY_VIDEO_FORMAT_576I;
2429 if( w == 720 && h == 480 && !ilace ) return BLURAY_VIDEO_FORMAT_480P;
2430 if( w == 1920 && h == 1080 && ilace ) return BLURAY_VIDEO_FORMAT_1080I;
2431 if( w == 1280 && h == 720 && !ilace ) return BLURAY_VIDEO_FORMAT_720P;
2432 if( w == 1920 && h == 1080 && !ilace ) return BLURAY_VIDEO_FORMAT_1080P;
2433 if( w == 720 && h == 576 && !ilace ) return BLURAY_VIDEO_FORMAT_576P;
2434 fprintf(stderr, "unknown bluray video format %dx%d %silace\n",
2435 w, h, !ilace ? "not " : "");
2439 static int bd_video_rate(double rate)
2441 if( fabs(rate-23.976) < 0.01 ) return BLURAY_VIDEO_RATE_24000_1001;
2442 if( fabs(rate-24.000) < 0.01 ) return BLURAY_VIDEO_RATE_24;
2443 if( fabs(rate-25.000) < 0.01 ) return BLURAY_VIDEO_RATE_25;
2444 if( fabs(rate-29.970) < 0.01 ) return BLURAY_VIDEO_RATE_30000_1001;
2445 if( fabs(rate-50.000) < 0.01 ) return BLURAY_VIDEO_RATE_50;
2446 if( fabs(rate-59.940) < 0.01 ) return BLURAY_VIDEO_RATE_60000_1001;
2447 fprintf(stderr, "unknown bluray video framerate %5.2f\n",rate);
2451 static int bd_aspect_ratio(int w, int h, double ratio)
2453 double aspect = (w * ratio) / h;
2454 if( fabs(aspect-1.333) < 0.01 ) return BLURAY_ASPECT_RATIO_4_3;
2455 if( fabs(aspect-1.777) < 0.01 ) return BLURAY_ASPECT_RATIO_16_9;
2456 return w == 720 ? BLURAY_ASPECT_RATIO_4_3 : BLURAY_ASPECT_RATIO_16_9;
2457 fprintf(stderr, "unknown bluray aspect ratio %5.3f\n",aspect);
2461 int media_info::scan()
2464 if( stat(filename, &st) ) return 1;
2465 file_size = st.st_size;
2467 AVFormatContext *fmt_ctx = 0;
2468 AVDictionary *fopts = 0;
2469 av_dict_set(&fopts, "formatprobesize", "5000000", 0);
2470 av_dict_set(&fopts, "scan_all_pmts", "1", 0);
2471 av_dict_set(&fopts, "threads", "auto", 0);
2472 int ret = avformat_open_input(&fmt_ctx, filename, NULL, &fopts);
2473 av_dict_free(&fopts);
2474 if( ret < 0 ) return ret;
2475 ret = avformat_find_stream_info(fmt_ctx, NULL);
2477 bit_rate = fmt_ctx->bit_rate;
2480 for( int i=0; ret>=0 && i<(int)fmt_ctx->nb_streams; ++i ) {
2481 AVStream *st = fmt_ctx->streams[i];
2482 AVMediaType type = st->codec->codec_type;
2484 case AVMEDIA_TYPE_VIDEO: break;
2485 case AVMEDIA_TYPE_AUDIO: break;
2488 stream *s = new stream(type, i);
2490 AVCodecID codec_id = st->codec->codec_id;
2492 case AVMEDIA_TYPE_VIDEO: {
2493 if( ep_pid < 0 ) ep_pid = st->id;
2494 s->coding_type = bd_stream_type(codec_id);
2495 s->format = bd_video_format(st->codec->width, st->codec->height,
2496 st->codec->flags & CODEC_FLAG_INTERLACED_ME);
2497 s->rate = bd_video_rate(!st->codec->framerate.den ? 0 :
2498 (double)st->codec->framerate.num / st->codec->framerate.den);
2499 s->aspect = bd_aspect_ratio(st->codec->width, st->codec->height,
2500 !st->sample_aspect_ratio.num || !st->sample_aspect_ratio.den ? 1. :
2501 (double)st->sample_aspect_ratio.num / st->sample_aspect_ratio.den);
2503 case AVMEDIA_TYPE_AUDIO: {
2504 s->coding_type = bd_stream_type(codec_id);
2505 s->format = bd_audio_format(st->codec->channels);
2506 s->rate = bd_audio_rate(st->codec->sample_rate);
2507 strcpy((char*)s->lang, "und");
2512 if( bit_rate > 0 && st->duration == AV_NOPTS_VALUE &&
2513 st->time_base.num < INT64_MAX / bit_rate ) {
2514 st->duration = av_rescale(8*file_size, st->time_base.den,
2515 bit_rate * (int64_t) st->time_base.num);
2517 s->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2520 AVCodec *decoder = avcodec_find_decoder(codec_id);
2521 AVDictionary *copts = 0;
2522 ret = avcodec_open2(st->codec, decoder, &copts);
2525 ep_pid = fmt_ctx->nb_streams > 0 ? fmt_ctx->streams[0]->id : 0;
2527 int npgm = fmt_ctx->nb_programs;
2529 program *pgm = new program(-1, 1);
2530 pgm->ep_pid = ep_pid;
2531 pgm->pmt_pid = 0x1000;
2532 pgm->pcr_pid = 0x100;
2534 for( int jj=0; jj<streams.size(); ++jj ) {
2535 AVStream *st = fmt_ctx->streams[jj];
2536 AVMediaType type = st->codec->codec_type;
2538 case AVMEDIA_TYPE_VIDEO:
2539 case AVMEDIA_TYPE_AUDIO:
2544 pgm->strm_idx.append(jj);
2545 if( pgm->duration < st->duration )
2546 pgm->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2548 programs.append(pgm);
2551 for( int ii=0; ii<npgm; ++ii ) {
2552 AVProgram *pgrm = fmt_ctx->programs[ii];
2553 program *pgm = new program(ii, pgrm->id);
2554 pgm->pmt_pid = pgrm->pmt_pid;
2555 pgm->pcr_pid = pgrm->pcr_pid;
2558 for( int jj=0; jj<(int)pgrm->nb_stream_indexes; ++jj ) {
2559 int av_idx = pgrm->stream_index[jj];
2560 AVStream *st = fmt_ctx->streams[av_idx];
2561 AVMediaType type = st->codec->codec_type;
2563 case AVMEDIA_TYPE_VIDEO:
2564 if( ep_pid < 0 ) ep_pid = st->id;
2566 case AVMEDIA_TYPE_AUDIO:
2571 int sidx = streams.size();
2572 while( --sidx>=0 && streams[sidx]->av_idx!=av_idx );
2574 fprintf(stderr, "bad stream idx %d in pgm %d\n",av_idx, ii);
2577 if( pgm->duration < st->duration )
2578 pgm->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2579 pgm->strm_idx.append(sidx);
2582 AVProgram *pgrm = fmt_ctx->programs[0];
2583 ep_pid = pgrm->nb_stream_indexes > 0 ?
2584 fmt_ctx->streams[pgrm->stream_index[0]]->id : 0;
2586 pgm->ep_pid = ep_pid;
2587 programs.append(pgm);
2591 ret = scan(fmt_ctx);
2593 for( int i=0; i<(int)fmt_ctx->nb_streams; ++i )
2594 avcodec_close(fmt_ctx->streams[i]->codec);
2595 avformat_close_input(&fmt_ctx);
2600 int media_info::scan(AVFormatContext *fmt_ctx)
2604 av_init_packet(&ipkt);
2606 for( int64_t count=0; ret>=0; ++count ) {
2607 av_packet_unref(&ipkt);
2608 ipkt.data = 0; ipkt.size = 0;
2610 ret = av_read_frame(fmt_ctx, &ipkt);
2612 if( ret == AVERROR_EOF ) break;
2616 if( !ipkt.data ) continue;
2617 if( (ipkt.flags & AV_PKT_FLAG_CORRUPT) ) continue;
2618 if( ipkt.pts == AV_NOPTS_VALUE ) continue;
2619 int i = ipkt.stream_index;
2620 if( i < 0 || i >= streams.size() ) continue;
2624 for( int ii=0; !pgm && ii<programs.size(); ++ii ) {
2625 program *p = programs[ii];
2626 for( int jj=0; jj<p->strm_idx.size(); ++jj ) {
2627 sp = streams[p->strm_idx[jj]];
2628 if( sp->av_idx == i ) { pgm = p; break; }
2631 if( !pgm ) continue;
2632 AVStream *st = fmt_ctx->streams[i];
2633 if( pgm->ep_pid != st->id ) continue;
2634 int64_t pts45k = av_rescale_q(ipkt.pts, st->time_base, clk45k);
2635 if( sp->start_pts > pts45k ) sp->start_pts = pts45k;
2636 if( sp->end_pts < pts45k ) sp->end_pts = pts45k;
2637 if( pgm->start_time > pts45k ) pgm->start_time = pts45k;
2638 if( pgm->end_time < pts45k ) pgm->end_time = pts45k;
2640 if( !(ipkt.flags & AV_PKT_FLAG_KEY) ) continue;
2642 if( sp->last_pts != pts45k ) {
2643 sp->last_pts = pts45k;
2644 pgm->add_label(count, ipkt.pos, pts45k, st->id);
2648 for( int ii=0; ii<programs.size(); ++ii ) {
2649 program *pgm = programs[ii];
2650 if( pgm->end_time > pgm->start_time )
2651 pgm->duration = pgm->end_time - pgm->start_time;
2654 return ret != AVERROR_EOF ? -1 : 0;
2663 idx.first_play.set_hdmv(0, pb_typ_movie);
2664 idx.top_menu.set_hdmv(0xffff, pb_typ_iactv);
2669 movie_obj *mp = new movie_obj();
2670 mp->resume_intention_flag = 1;
2671 command_obj *cmd = new command_obj();
2672 cmd->cmd = htobe32(0x21810000); cmd->dst = 1; cmd->src = 0;
2673 mp->cmds.append(cmd); // JUMP_TITLE 1
2674 cmd = new command_obj();
2675 cmd->cmd = htobe32(0x00020000); cmd->dst = 0; cmd->src = 0;
2676 mp->cmds.append(cmd);
2677 mov.movies.append(mp); // BREAK
2680 for( int ii=0; ii<size(); ++ii ) {
2681 mp = new movie_obj();
2682 mp->resume_intention_flag = 1;
2683 cmd = new command_obj();
2684 cmd->cmd = htobe32(0x22800000); cmd->dst = ii; cmd->src = 0;
2685 mp->cmds.append(cmd); // PLAY_PL ii
2686 cmd = new command_obj();
2687 cmd->cmd = htobe32(0x00020000); cmd->dst = 0; cmd->src = 0;
2688 mp->cmds.append(cmd);
2689 mov.movies.append(mp); // BREAK
2692 mp = new movie_obj();
2693 mp->resume_intention_flag = 1;
2694 cmd = new command_obj();
2695 cmd->cmd = htobe32(0x21810000); cmd->dst = 1; cmd->src = 0;
2696 mp->cmds.append(cmd); // JUMP_TITLE 1
2697 cmd = new command_obj();
2698 cmd->cmd = htobe32(0x00020000); cmd->dst = 0; cmd->src = 0;
2699 mp->cmds.append(cmd);
2700 mov.movies.append(mp); // BREAK
2704 for( int ii=0; ii<size(); ++ii ) {
2706 tp = new title_obj();
2707 tp->set_hdmv(idx.titles.size()+1, pb_typ_movie);
2708 idx.titles.append(tp);
2711 media_info *ip = get(ii);
2712 // clip program, if specified
2713 int pidx = ip->programs.size();
2714 while( --pidx>=0 && ip->programs[pidx]->pid != ip->pgm_pid );
2715 if( pidx < 0 ) pidx = 0;
2717 ip->pgm_pid = ip->prog()->pid;
2719 clpi_cl *cp = new clpi_cl();
2720 cp->clip.clip_stream_type = 1;
2721 cp->clip.application_type = BLURAY_APP_TYPE_MAIN_MOVIE;
2722 cp->clip.ts_recording_rate = ip->bit_rate;
2723 uint32_t ts_pkt_count = ip->file_size / BLURAY_TS_PKTSZ + 1;
2724 cp->clip.num_source_packets = ts_pkt_count;
2725 cp->clip.ts_type_info.validity = 0x80;
2726 strcpy(cp->clip.ts_type_info.format_id, "HDMV");
2729 for( int jj=0; jj<ip->programs.size(); ++jj ) {
2730 program *pgm = ip->programs[jj];
2731 clpi_prog *p = new clpi_prog(pgm->pmt_pid);
2732 for( int kk=0; kk<pgm->strm_idx.size(); ++kk ) {
2733 int k = pgm->strm_idx[kk];
2734 stream *sp = ip->streams[k];
2735 clpi_prog_stream *s = new clpi_prog_stream();
2737 s->coding_type = sp->coding_type;
2738 s->format = sp->format;
2739 //use unspecified (0)
2740 // if( !idx.video_format ) idx.video_format = s->format;
2742 // if( !idx.frame_rate ) idx.frame_rate = s->rate;
2743 switch( sp->type ) {
2744 case AVMEDIA_TYPE_VIDEO:
2745 s->aspect = sp->aspect;
2747 case AVMEDIA_TYPE_AUDIO:
2748 memcpy(s->lang,sp->lang,sizeof(s->lang));
2753 p->streams.append(s);
2755 clpi_ep_map_entry *map = new clpi_ep_map_entry(pgm->ep_pid);
2756 pgm->build_toc(map);
2757 cp->cpi.append(map);
2758 cp->programs.append(p);
2760 clpi_atc_seq *atc_seq = new clpi_atc_seq();
2761 clpi_stc_seq *stc_seq = new clpi_stc_seq();
2762 stc_seq->pcr_pid = pgm->pcr_pid;
2763 stc_seq->presentation_start_time = pgm->start_time;
2764 stc_seq->presentation_end_time = pgm->end_time;
2765 atc_seq->stc_seq.append(stc_seq);
2766 cp->sequences.append(atc_seq);
2771 if( ip->brk ) tp = 0;
2774 // playlists, one per title
2775 // one playitem per media clip
2777 for( int ii=0; ii<idx.titles.size(); ++ii ) {
2779 media_info *ip = get(clip_id);
2780 program *pgm = ip->prog();
2781 mpls_pl *pp = new mpls_pl();
2782 pp->app_info.playback_type = BLURAY_PLAYBACK_TYPE_SEQUENTIAL;
2783 // pp->app_info.uo_mask.xxx = 1;
2784 int last = idx.titles[ii]->last;
2785 for( ; clip_id<=last; ++clip_id ) {
2788 mpls_pi *pi = new mpls_pi();
2789 pi->connection_condition = 1; // seamless
2790 pi->in_time = pgm->start_time;
2791 pi->out_time = pgm->end_time;
2793 pi->still_mode = BLURAY_STILL_INFINITE;
2794 int64_t end_time = pgm->start_time + pgm->duration;
2795 if( pi->out_time < end_time ) pi->out_time = end_time;
2796 mpls_clip *cp = new mpls_clip();
2797 sprintf(cp->clip_id,"%05d", clip_id);
2798 pi->clip.append(cp);
2799 for( int kk=0; kk<ip->streams.size(); ++kk ) {
2800 stream *sp = ip->streams[kk];
2801 switch( sp->type ) {
2802 case AVMEDIA_TYPE_VIDEO: break;
2803 case AVMEDIA_TYPE_AUDIO: break;
2806 mpls_stream *ps = new mpls_stream();
2808 ps->stream_type = BLURAY_PG_TEXTST_STREAM;
2809 ps->coding_type = sp->coding_type;
2810 ps->format = sp->format;
2811 ps->rate = sp->rate;
2812 switch( sp->type ) {
2813 case AVMEDIA_TYPE_VIDEO:
2814 pi->stn.video.append(ps);
2816 case AVMEDIA_TYPE_AUDIO:
2817 memcpy(ps->lang, sp->lang, sizeof(ps->lang));
2818 pi->stn.audio.append(ps);
2824 pp->play_item.append(pi);
2826 // chapter marks every ch_duration ticks
2827 int64_t ch_duration = 45000 * 60*10;
2828 int64_t mrktm = ch_duration;
2830 int pmark = 0, pitem = 0;
2831 mpls_pi *pi = pp->play_item[pitem];
2832 mpls_plm *pm = new mpls_plm();
2833 pm->mark_id = pmark++;
2834 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
2835 pm->play_item_ref = pitem;
2836 pm->time = pi->in_time;
2837 pm->entry_es_pid = 0;
2838 pp->play_mark.append(pm);
2839 for( int jj=0; jj < pp->play_item.size(); ++jj ) {
2841 pi = pp->play_item[pitem];
2842 int64_t pi_duration = pi->out_time - pi->in_time;
2843 int64_t endtm = plytm + pi_duration;
2844 while( mrktm < endtm ) {
2845 pm = new mpls_plm();
2846 pm->mark_id = pmark++;
2847 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
2848 pm->play_item_ref = pitem;
2849 pm->time = pi->in_time + mrktm - plytm;
2850 pm->entry_es_pid = 0;
2851 pp->play_mark.append(pm);
2852 mrktm += ch_duration;
2856 pm = new mpls_plm();
2857 pm->mark_id = pmark;
2858 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
2859 pm->play_item_ref = pitem;
2860 pm->time = pi->out_time;
2861 pm->entry_es_pid = 0;
2862 pp->play_mark.append(pm);
2870 bd_path(const char *bp, const char *fmt, va_list ap)
2872 int n = sizeof(filename)-1;
2873 char *cp = filename;
2874 const char *pp = path;
2875 while( n>0 && (*cp=*pp)!=0 ) { --n; ++cp; ++pp; }
2876 while( n>0 && (*cp=*bp)!=0 ) { --n; ++cp; ++bp; }
2877 n -= vsnprintf(cp, n, fmt, ap);
2879 return n > 0 ? 0 : 1;
2883 bd_copy(const char *ifn, const char *fmt, ...)
2887 FILE *ifp = fopen(ifn,"r");
2891 if( bd_path("/BDMV/", fmt, ap) ) return 1;
2893 FILE *ofp = fopen(filename,"w");
2895 setvbuf(ifp, 0, _IOFBF, 0x80000);
2896 setvbuf(ofp, 0, _IOFBF, 0x80000);
2897 while( (n=fread(bfr,1,sizeof(bfr),ifp)) > 0 ) fwrite(bfr,1,n,ofp);
2904 fprintf(stderr, "cant copy clip %s\n",ifn);
2909 bd_open(const char *fmt, ...)
2912 if( !path ) return 0;
2915 if( bd_path("/BDMV/", fmt, ap) ) return 1;
2917 if( bs.open(filename) ) {
2918 fprintf(stderr, "cant open file %s\n",filename);
2925 bd_backup(const char *fmt, ...)
2928 if( !path ) return 0;
2931 FILE *ifp = fopen(filename,"r");
2935 if( bd_path("/BDMV/BACKUP/", fmt, ap) ) return 1;
2937 FILE *ofp = fopen(filename,"w");
2939 while( (n=fread(bfr,1,sizeof(bfr),ifp)) > 0 ) fwrite(bfr,1,n,ofp);
2946 fprintf(stderr, "cant backup %s\n",filename);
2950 int Media::write(char *fn)
2954 if( bd_open("index.bdmv") ) return 1;
2955 if( idx.write() ) return 1;
2956 if( bd_backup("index.bdmv") ) return 1;
2958 if( bd_open("MovieObject.bdmv") ) return 1;
2959 if( mov.write() ) return 1;
2960 if( bd_backup("MovieObject.bdmv") ) return 1;
2962 for( int ii=0; ii<cl.size(); ++ii ) {
2963 if( bd_open("CLIPINF/%05d.clpi", ii) ) return 1;
2964 if( cl[ii]->write() ) return 1;
2965 if( bd_backup("CLIPINF/%05d.clpi", ii) ) return 1;
2968 for( int ii=0; ii<pl.size(); ++ii ) {
2969 if( bd_open("PLAYLIST/%05d.mpls", ii) ) return 1;
2970 if( pl[ii]->write() ) return 1;
2971 if( bd_backup("PLAYLIST/%05d.mpls", ii) ) return 1;
2977 main(int ac, char **av)
2980 if( mkbdmv(path) ) return 1;
2982 av_log_set_level(AV_LOG_FATAL);
2983 //av_log_set_level(AV_LOG_VERBOSE);
2984 //av_log_set_level(AV_LOG_DEBUG);
2988 for( int ii=2; ii<ac; ++ii ) {
2990 // any dash seq followed by number sets curr title pgm_pid
2991 // single dash only sets title pgm_pid
2992 // double dash ends curr title, starts a new title
2993 // triple dash ends curr title as infinite still
2996 if( *++ap == '-' ) {
2998 if( *++ap == '-' ) { ++ap; mp->still = 1; }
3000 if( *ap >= '0' && *ap <= '9' )
3001 mp->pgm_pid = strtoul(ap,&ap,0);
3002 if( mp->brk ) mp = 0;
3004 fprintf(stderr, "err arg %d: %s\n",ii,av[ii]);
3009 mp = new media_info(av[ii]);
3012 fprintf(stderr, "cant scan media: %s\n", av[ii]);
3017 if( mp ) mp->brk = 1;
3019 if( media.compose() ) {
3020 fprintf(stderr, "cant compose media\n");
3023 if( media.write(0) ) {
3024 fprintf(stderr, "cant prepare media\n");
3027 if( media.write(path) ) {
3028 fprintf(stderr, "cant write media\n");
3032 for( int ii=0; ii<media.size(); ++ii )
3033 if( media.bd_copy(media[ii]->filename, "STREAM/%05d.m2ts", ii) )