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/.
45 #include "arraylist.h"
47 #define BCTEXTLEN 1024
48 #define BLURAY_TS_PKTSZ 192L
52 #include "libavfilter/buffersrc.h"
53 #include "libavfilter/buffersink.h"
54 #include "libavformat/avformat.h"
55 #include "libavformat/avio.h"
56 #include "libavcodec/avcodec.h"
57 #include "libavfilter/avfilter.h"
58 #include "libavutil/avutil.h"
59 #include "libavutil/opt.h"
60 #include "libavutil/pixdesc.h"
61 #include "libswresample/swresample.h"
62 #include "libswscale/swscale.h"
75 BLURAY_APP_TYPE_MAIN_MOVIE = 1,
76 BLURAY_APP_TYPE_MAIN_TIMED_SLIDE_SHOW = 2,
77 BLURAY_APP_TYPE_MAIN_BROWSED_SLIDE_SHOW = 3,
78 BLURAY_APP_TYPE_SUBPATH_BROWSED_SLIDE_SHOW = 4,
79 BLURAY_APP_TYPE_SUBPATH_IG_MENU = 5,
80 BLURAY_APP_TYPE_SUBPATH_TEXT_SUBTITLE = 6,
81 BLURAY_APP_TYPE_SUBPATH_ELEMENTARY_STREAM = 7,
83 BLURAY_STREAM_TYPE_VIDEO_MPEG1 = 0x01,
84 BLURAY_STREAM_TYPE_VIDEO_MPEG2 = 0x02,
85 BLURAY_STREAM_TYPE_AUDIO_MPEG1 = 0x03,
86 BLURAY_STREAM_TYPE_AUDIO_MPEG2 = 0x04,
87 BLURAY_STREAM_TYPE_AUDIO_LPCM = 0x80,
88 BLURAY_STREAM_TYPE_AUDIO_AC3 = 0x81,
89 BLURAY_STREAM_TYPE_AUDIO_DTS = 0x82,
90 BLURAY_STREAM_TYPE_AUDIO_TRUHD = 0x83,
91 BLURAY_STREAM_TYPE_AUDIO_AC3PLUS = 0x84,
92 BLURAY_STREAM_TYPE_AUDIO_DTSHD = 0x85,
93 BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER = 0x86,
94 BLURAY_STREAM_TYPE_VIDEO_VC1 = 0xea,
95 BLURAY_STREAM_TYPE_VIDEO_H264 = 0x1b,
96 BLURAY_STREAM_TYPE_VIDEO_H264_MVC = 0x20,
97 BLURAY_STREAM_TYPE_SUB_PG = 0x90,
98 BLURAY_STREAM_TYPE_SUB_IG = 0x91,
99 BLURAY_STREAM_TYPE_SUB_TEXT = 0x92,
100 BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY = 0xa1,
101 BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY = 0xa2,
103 BLURAY_MARK_TYPE_ENTRY = 0x01,
104 BLURAY_MARK_TYPE_LINK = 0x02,
106 BLURAY_PLAYBACK_TYPE_SEQUENTIAL = 1,
107 BLURAY_PLAYBACK_TYPE_RANDOM = 2,
108 BLURAY_PLAYBACK_TYPE_SHUFFLE = 3,
110 BLURAY_VIDEO_FORMAT_480I = 1, // ITU-R BT.601-5
111 BLURAY_VIDEO_FORMAT_576I = 2, // ITU-R BT.601-4
112 BLURAY_VIDEO_FORMAT_480P = 3, // SMPTE 293M
113 BLURAY_VIDEO_FORMAT_1080I = 4, // SMPTE 274M
114 BLURAY_VIDEO_FORMAT_720P = 5, // SMPTE 296M
115 BLURAY_VIDEO_FORMAT_1080P = 6, // SMPTE 274M
116 BLURAY_VIDEO_FORMAT_576P = 7, // ITU-R BT.1358
118 BLURAY_VIDEO_RATE_24000_1001 = 1, // 23.976
119 BLURAY_VIDEO_RATE_24 = 2,
120 BLURAY_VIDEO_RATE_25 = 3,
121 BLURAY_VIDEO_RATE_30000_1001 = 4, // 29.97
122 BLURAY_VIDEO_RATE_50 = 6,
123 BLURAY_VIDEO_RATE_60000_1001 = 7, // 59.94
125 BLURAY_ASPECT_RATIO_4_3 = 2,
126 BLURAY_ASPECT_RATIO_16_9 = 3,
128 BLURAY_AUDIO_FORMAT_MONO = 1,
129 BLURAY_AUDIO_FORMAT_STEREO = 3,
130 BLURAY_AUDIO_FORMAT_MULTI_CHAN = 6,
131 BLURAY_AUDIO_FORMAT_COMBO = 12, // Stereo ac3/dts,
133 BLURAY_AUDIO_RATE_48 = 1,
134 BLURAY_AUDIO_RATE_96 = 4,
135 BLURAY_AUDIO_RATE_192 = 5,
136 BLURAY_AUDIO_RATE_192_COMBO = 12, // 48 or 96 ac3/dts, 192 mpl/dts-hd
137 BLURAY_AUDIO_RATE_96_COMBO = 14, // 48 ac3/dts, 96 mpl/dts-hd
138 BLURAY_TEXT_CHAR_CODE_UTF8 = 0x01,
139 BLURAY_TEXT_CHAR_CODE_UTF16BE = 0x02,
140 BLURAY_TEXT_CHAR_CODE_SHIFT_JIS = 0x03,
141 BLURAY_TEXT_CHAR_CODE_EUC_KR = 0x04,
142 BLURAY_TEXT_CHAR_CODE_GB18030_20001 = 0x05,
143 BLURAY_TEXT_CHAR_CODE_CN_GB = 0x06,
144 BLURAY_TEXT_CHAR_CODE_BIG5 = 0x07,
146 BLURAY_STILL_NONE = 0x00,
147 BLURAY_STILL_TIME = 0x01,
148 BLURAY_STILL_INFINITE = 0x02,
150 BLURAY_PG_TEXTST_STREAM = 0x01,
151 BLURAY_SECONDARY_VIDEO_STREAM = 0x02,
152 BLURAY_SECONDARY_AUDIO_STREAM = 0x03,
160 bs_file() { fp = 0; }
164 int open(const char *fn);
166 void write(uint32_t v, int n);
168 void padb(int n) { pad(n*8); }
169 int64_t posb() { return fpos; }
170 void posb(int64_t n);
171 int64_t pos() { return posb()*8 + len; }
172 void writeb(uint8_t * bp, int n);
173 void writeb(const char *cp, int n) {
174 writeb((uint8_t *) cp, n);
181 bs_length() { fpos = len = 0; }
182 void bs_len(bs_file &bs, int n) {
183 bs.write(len, n); fpos = bs.posb();
185 void bs_end(bs_file &bs) {
186 len = bs.posb() - fpos;
188 void bs_ofs(bs_file &bs, int n) {
189 bs.write(fpos-n/8, n);
193 class _bd_stream_info {
204 _bd_stream_info() { memset(this, 0, sizeof(*this)); }
205 ~_bd_stream_info() {}
208 class bd_stream_info : public _bd_stream_info {
214 class _bd_clip_info {
218 uint16_t still_time; /* seconds */
223 _bd_clip_info() { memset(this, 0, sizeof(*this)); }
227 class bd_clip_info : public _bd_clip_info {
229 ArrayList<bd_stream_info *> video_streams;
230 ArrayList<bd_stream_info *> audio_streams;
231 ArrayList<bd_stream_info *> pg_streams;
232 ArrayList<bd_stream_info *> ig_streams;
233 ArrayList<bd_stream_info *> sec_audio_streams;
234 ArrayList<bd_stream_info *> sec_video_streams;
238 video_streams.remove_all_objects();
239 audio_streams.remove_all_objects();
240 pg_streams.remove_all_objects();
241 ig_streams.remove_all_objects();
242 sec_audio_streams.remove_all_objects();
243 sec_video_streams.remove_all_objects();
247 class _bd_title_chapter {
255 _bd_title_chapter() { memset(this, 0, sizeof(*this)); }
256 ~_bd_title_chapter() {}
259 class bd_title_chapter : public _bd_title_chapter {
261 bd_title_chapter() {}
262 ~bd_title_chapter() {}
265 class _bd_title_mark {
274 _bd_title_mark() { memset(this, 0, sizeof(*this)); }
278 class bd_title_mark : public _bd_title_mark {
284 class _bd_title_info {
291 _bd_title_info() { memset(this, 0, sizeof(*this)); }
295 class bd_title_info : public _bd_title_info {
297 ArrayList<bd_clip_info *> clips;
298 ArrayList<bd_title_chapter *> chapters;
299 ArrayList<bd_title_mark *> marks;
303 clips.remove_all_objects();
304 chapters.remove_all_objects();
305 marks.remove_all_objects();
309 class _clpi_stc_seq {
312 uint32_t spn_stc_start;
313 uint32_t presentation_start_time;
314 uint32_t presentation_end_time;
316 _clpi_stc_seq() { memset(this, 0, sizeof(*this)); }
320 class clpi_stc_seq : public _clpi_stc_seq {
328 class _clpi_atc_seq {
330 uint32_t spn_atc_start;
331 uint8_t offset_stc_id;
333 _clpi_atc_seq() { memset(this, 0, sizeof(*this)); }
337 class clpi_atc_seq : public _clpi_atc_seq {
339 ArrayList<clpi_stc_seq *> stc_seq;
344 stc_seq.remove_all_objects();
348 class clpi_sequences : public bs_length,
349 public ArrayList<clpi_atc_seq *> {
354 ~clpi_sequences() { remove_all_objects(); }
357 class _clpi_ts_type {
362 _clpi_ts_type() { memset(this, 0, sizeof(*this)); }
366 class clpi_ts_type : public _clpi_ts_type {
372 class _clpi_atc_delta {
378 _clpi_atc_delta() { memset(this, 0, sizeof(*this)); }
379 ~_clpi_atc_delta() {}
382 class clpi_atc_delta : public _clpi_atc_delta {
394 _clpi_font() { memset(this, 0, sizeof(*this)); }
398 class clpi_font : public _clpi_font {
404 class clpi_font_info {
406 ArrayList<clpi_font *> font;
410 font.remove_all_objects();
414 class _clpi_clip_info {
416 uint8_t clip_stream_type;
417 uint8_t application_type;
418 uint8_t is_atc_delta;
419 uint32_t ts_recording_rate;
420 uint32_t num_source_packets;
422 _clpi_clip_info() { memset(this, 0, sizeof(*this)); }
423 ~_clpi_clip_info() {}
426 class clpi_clip_info : public bs_length, public _clpi_clip_info {
428 clpi_ts_type ts_type_info;
429 clpi_font_info font_info;
430 ArrayList<clpi_atc_delta *> atc_delta;
435 atc_delta.remove_all_objects();
439 class _clpi_prog_stream {
450 _clpi_prog_stream() { memset(this, 0, sizeof(*this)); }
451 ~_clpi_prog_stream() {}
454 class clpi_prog_stream : public bs_length, public _clpi_prog_stream {
458 clpi_prog_stream() {}
459 ~clpi_prog_stream() {}
462 class _clpi_ep_coarse {
468 _clpi_ep_coarse() { memset(this, 0, sizeof(*this)); }
469 ~_clpi_ep_coarse() {}
472 class clpi_ep_coarse : public _clpi_ep_coarse {
480 class _clpi_ep_fine {
482 uint8_t is_angle_change_point;
483 uint8_t i_end_position_offset;
487 _clpi_ep_fine() { memset(this, 0, sizeof(*this)); }
491 class clpi_ep_fine : public _clpi_ep_fine {
499 class _clpi_ep_map_entry {
501 uint8_t ep_stream_type;
502 uint32_t ep_map_stream_start_addr;
504 _clpi_ep_map_entry() { memset(this, 0, sizeof(*this)); }
505 ~_clpi_ep_map_entry() {}
508 class clpi_ep_map_entry : public _clpi_ep_map_entry {
512 ArrayList<clpi_ep_coarse *> coarse;
513 ArrayList<clpi_ep_fine *> fine;
514 int write(uint32_t ep_map_pos);
517 clpi_ep_map_entry(int id) { fine_start = 0; pid = id; }
518 ~clpi_ep_map_entry() {
519 coarse.remove_all_objects();
520 fine.remove_all_objects();
526 uint32_t spn_program_sequence_start;
528 _clpi_prog() { memset(this, 0, sizeof(*this)); }
532 class clpi_prog : public _clpi_prog {
534 ArrayList<clpi_prog_stream *> streams;
535 uint16_t program_map_pid;
538 clpi_prog(int pmt_pid) { program_map_pid = pmt_pid; }
539 ~clpi_prog() { streams.remove_all_objects(); }
542 class clpi_programs : public bs_length,
543 public ArrayList<clpi_prog *> {
548 ~clpi_programs() { remove_all_objects(); }
551 class clpi_extents : public bs_length,
552 public ArrayList<uint32_t> {
563 _clpi_cpi() { type = 0; }
567 class clpi_cpi : public bs_length, public _clpi_cpi,
568 public ArrayList<clpi_ep_map_entry *> {
573 ~clpi_cpi() { remove_all_objects(); }
578 unsigned int menu_call : 1;
579 unsigned int title_search : 1;
580 unsigned int chapter_search : 1;
581 unsigned int time_search : 1;
582 unsigned int skip_to_next_point : 1;
583 unsigned int skip_to_prev_point : 1;
584 unsigned int play_firstplay : 1;
585 unsigned int stop : 1;
586 unsigned int pause_on : 1;
587 unsigned int pause_off : 1;
588 unsigned int still_off : 1;
589 unsigned int forward : 1;
590 unsigned int backward : 1;
591 unsigned int resume : 1;
592 unsigned int move_up : 1;
593 unsigned int move_down : 1;
594 unsigned int move_left : 1;
595 unsigned int move_right : 1;
596 unsigned int select : 1;
597 unsigned int activate : 1;
598 unsigned int select_and_activate : 1;
599 unsigned int primary_audio_change : 1;
600 unsigned int reserved0 : 1;
601 unsigned int angle_change : 1;
602 unsigned int popup_on : 1;
603 unsigned int popup_off : 1;
604 unsigned int pg_enable_disable : 1;
605 unsigned int pg_change : 1;
606 unsigned int secondary_video_enable_disable : 1;
607 unsigned int secondary_video_change : 1;
608 unsigned int secondary_audio_enable_disable : 1;
609 unsigned int secondary_audio_change : 1;
610 unsigned int reserved1 : 1;
611 unsigned int pip_pg_change : 1;
615 memset(this, 0, sizeof(*this));
633 _mpls_stream() { memset(this, 0, sizeof(*this)); }
637 class mpls_stream : public _mpls_stream {
639 bs_length strm, code;
655 class mpls_stn : public bs_length, public _mpls_stn {
657 ArrayList<mpls_stream *> video;
658 ArrayList<mpls_stream *> audio;
659 ArrayList<mpls_stream *> pg;
660 ArrayList<mpls_stream *> ig;
661 ArrayList<mpls_stream *> secondary_audio;
662 ArrayList<mpls_stream *> secondary_video;
663 // Secondary audio specific fields
664 ArrayList<uint8_t> sa_primary_audio_ref;
665 // Secondary video specific fields
666 ArrayList<uint8_t> sv_secondary_audio_ref;
667 ArrayList<uint8_t> sv_pip_pg_ref;
672 video.remove_all_objects();
673 audio.remove_all_objects();
674 pg.remove_all_objects();
675 ig.remove_all_objects();
676 secondary_audio.remove_all_objects();
677 secondary_video.remove_all_objects();
687 _mpls_clip() { memset(this, 0, sizeof(*this)); }
691 class mpls_clip : public _mpls_clip {
693 mpls_clip() { strcpy(codec_id, "M2TS"); }
699 uint8_t is_multi_angle;
700 uint8_t connection_condition;
703 uint8_t random_access_flag;
706 uint8_t is_different_audio;
707 uint8_t is_seamless_angle;
709 _mpls_pi() { memset(this, 0, sizeof(*this)); }
713 class mpls_pi : public bs_length, public _mpls_pi {
716 ArrayList<mpls_clip *> clip;
729 uint16_t play_item_ref;
731 uint16_t entry_es_pid;
734 _mpls_plm() { memset(this, 0, sizeof(*this)); }
738 class mpls_plm : public _mpls_plm {
748 uint8_t playback_type;
749 uint16_t playback_count;
750 uint8_t random_access_flag;
751 uint8_t audio_mix_flag;
752 uint8_t lossless_bypass_flag;
754 _mpls_ai() { memset(this, 0, sizeof(*this)); }
758 class mpls_ai : public bs_length, public _mpls_ai {
769 uint8_t connection_condition;
770 uint8_t is_multi_clip;
773 uint16_t sync_play_item_id;
776 _mpls_sub_pi() { memset(this, 0, sizeof(*this)); }
780 class mpls_sub_pi : public bs_length, public _mpls_sub_pi {
782 ArrayList<mpls_clip *> clip;
787 clip.remove_all_objects();
796 _mpls_sub() { memset(this, 0, sizeof(*this)); }
800 class mpls_sub : public bs_length, public _mpls_sub {
802 ArrayList<mpls_sub_pi *> sub_play_item;
807 sub_play_item.remove_all_objects();
811 class _mpls_pip_data {
816 uint8_t scale_factor;
818 _mpls_pip_data() { memset(this, 0, sizeof(*this)); }
822 class mpls_pip_data : public _mpls_pip_data {
830 class _mpls_pip_metadata {
833 uint8_t secondary_video_ref;
834 uint8_t timeline_type;
835 uint8_t luma_key_flag;
836 uint8_t upper_limit_luma_key;
837 uint8_t trick_play_flag;
839 _mpls_pip_metadata() { memset(this, 0, sizeof(*this)); }
840 ~_mpls_pip_metadata() {}
843 class mpls_pip_metadata : public _mpls_pip_metadata {
845 ArrayList<mpls_pip_data *> data;
847 int write(uint32_t start_address);
848 mpls_pip_metadata() {}
849 ~mpls_pip_metadata() {
850 data.remove_all_objects();
860 _mpls_pl() { memset(this, 0, sizeof(*this)); }
864 class mpls_pl : public _mpls_pl {
867 bs_length play, mark, subx, pipm;
869 ArrayList<mpls_pi *> play_item;
870 ArrayList<mpls_sub *> sub_path;
871 ArrayList<mpls_plm *> play_mark;
872 // extension data (profile 5, version 2.4)
873 ArrayList<mpls_sub *> ext_sub_path;
874 // extension data (Picture-In-Picture metadata)
875 ArrayList<mpls_pip_metadata *> ext_pip_data;
878 int write_playlist();
879 int write_playlistmark();
880 int write_subpath_extension();
881 int write_pip_metadata_extension();
884 mpls_pl() { sig = 1; }
886 play_item.remove_all_objects();
887 sub_path.remove_all_objects();
888 play_mark.remove_all_objects();
889 ext_sub_path.remove_all_objects();
890 ext_pip_data.remove_all_objects();
896 uint32_t sequence_info_start_addr;
897 uint32_t program_info_start_addr;
898 uint32_t cpi_start_addr;
899 uint32_t clip_mark_start_addr;
900 uint32_t ext_data_start_addr;
902 _clpi_cl() { memset(this, 0, sizeof(*this)); }
906 class clpi_cl : public _clpi_cl {
910 clpi_sequences sequences;
911 clpi_programs programs;
913 clpi_extents extents;
914 clpi_programs programs_ss;
920 int write_clpi_extension(int id1, int id2, void *handle);
921 int write_mpls_extension(int id1, int id2, void *handle);
923 clpi_cl() { sig = 1; }
932 uint32_t sub_grp:3; /* command sub-group */
933 uint32_t grp:2; /* command group */
934 uint32_t op_cnt:3; /* operand count */
935 uint32_t branch_opt:4; /* branch option */
937 uint32_t imm_op2:1; /* I-flag for operand 2 */
938 uint32_t imm_op1:1; /* I-flag for operand 1 */
939 uint32_t cmp_opt:4; /* compare option */
941 uint32_t set_opt:5; /* set option */
948 command_obj() { cmd = dst = src = 0; }
954 int resume_intention_flag;
956 int title_search_mask;
958 _movie_obj() { memset(this, 0, sizeof(*this)); }
962 class movie_obj : public _movie_obj {
964 ArrayList<command_obj *> cmds;
968 cmds.remove_all_objects();
972 class movie_file : public bs_length {
975 ArrayList<movie_obj *> movies;
977 movie_file() { sig = 1; }
979 movies.remove_all_objects();
991 _pb_obj() { memset(this, 0, sizeof(*this)); }
995 class pb_obj : public _pb_obj {
1009 void set_hdmv(int id, int pt);
1010 void set_bdj(char *nm, int pt);
1011 void write_hdmv_obj(int id_ref);
1012 void write_bdj_obj(char *name);
1018 _title_obj() { acc_typ = 0; }
1022 class title_obj : public pb_obj, public _title_obj {
1033 int initial_output_mode_preference;
1034 int content_exist_flag;
1038 _index_file() { memset(this, 0, sizeof(*this)); }
1042 class index_file : public bs_length, public _index_file {
1044 ArrayList<title_obj *> titles;
1048 bs_length exten, appinf;
1050 void write_hdmv_obj(int hdmv_typ, int id_ref);
1051 void write_bdj_obj(int bdj_typ, char *name);
1052 void write_pb_obj(pb_obj * pb);
1057 memset(user_data, 0, sizeof(user_data));
1060 titles.remove_all_objects();
1066 uint32_t data_start;
1067 uint32_t extension_data_start;
1071 _bdid() { memset(this, 0, sizeof(*this)); }
1075 class bdid : public _bdid {
1084 class stream : public bd_stream_info {
1094 stream(AVMediaType ty, int i) {
1095 type = ty; av_idx = i;
1096 start_pts = INT64_MAX; end_pts = INT64_MIN;
1104 static int cmpr(mark **, mark **);
1105 int64_t sample, pos, pts;
1107 mark(int64_t s, int64_t p, int64_t ts, uint32_t pk, int id) {
1108 sample = s; pos = p; pts = ts; pkt = pk; pid = id;
1112 int mark::cmpr(mark **a, mark **b)
1114 mark *ap = *(mark **)a, *bp = *(mark **)b;
1115 return ap->pts > bp->pts ? 1 : ap->pts < bp->pts ? -1 : 0;
1120 int pmt_pid, pcr_pid, ep_pid;
1121 int64_t start_time, end_time;
1124 memset(this, 0, sizeof(*this));
1125 start_time = INT64_MAX; end_time = INT64_MIN;
1130 class program : public _program {
1134 ArrayList<int> strm_idx;
1135 ArrayList<mark*> marks;
1137 int build_toc(clpi_ep_map_entry *map);
1138 void add_label(uint32_t pk, int64_t p, int64_t ts, int id) {
1139 marks.append(new mark(curr_pos, p, ts, pk, id));
1142 program(int i, int id) { idx = i; pid = id; curr_pos = 0; }
1143 ~program() { marks.remove_all_objects(); }
1152 memset(this, 0, sizeof(*this));
1157 class media_info : public _media_info {
1159 static const AVRational clk45k;
1160 char filename[BCTEXTLEN];
1162 int brk, pidx, still;
1163 ArrayList<stream *> streams;
1164 ArrayList<program *> programs;
1165 program *prog() { return programs[pidx]; }
1167 media_info(const char *fn) {
1168 strcpy(filename, fn);
1169 brk = 0; pidx = 0; pgm_pid = -1; still = 0;
1171 ~media_info() { streams.remove_all_objects(); }
1174 int scan(AVFormatContext *fmt_ctx);
1177 class Media : public ArrayList<media_info *> {
1180 char filename[BCTEXTLEN];
1181 int bd_path(const char *bp, const char *fmt, va_list ap);
1182 int bd_open(const char *fmt, ...);
1183 int bd_backup(const char *fmt, ...);
1184 int bd_copy(const char *ifn, const char *fmt, ...);
1188 ArrayList<clpi_cl *> cl;
1189 ArrayList<mpls_pl *> pl;
1192 int write(char *fn);
1194 Media() { path = 0; filename[0] = 0; }
1196 remove_all_objects();
1197 cl.remove_all_objects();
1198 pl.remove_all_objects();
1204 void bs_file::init()
1212 bs_file::open(const char *fn)
1215 if( !fp ) perror(fn);
1216 return fp != 0 ? 0 : 1;
1222 if( fp ) { fclose(fp); fp = 0; }
1226 bs_file::write(uint32_t v, int n)
1233 if( fp ) fputc((vv >> len), fp);
1234 if( ++fpos > fsz ) fsz = fpos;
1239 bs_file::writeb(uint8_t * bp, int n)
1241 while( --n >= 0 ) write(*bp++, 8);
1247 while( n > 32 ) { write(0, 32); n -= 32; }
1248 if( n > 0 ) write(0, n);
1252 bs_file::posb(int64_t n)
1254 if( fpos == fsz && n > fpos ) {
1259 if( fp ) fseek(fp, fpos, SEEK_SET);
1269 bs.writeb("MOBJ", 4);
1270 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1271 int movie_start = 0x0028;
1272 bs.posb(movie_start);
1275 bs.write(movies.size(), 16);
1277 for (int i = 0; i < movies.size(); ++i) {
1278 movie_obj *mov = movies[i];
1279 bs.write(mov->resume_intention_flag, 1);
1280 bs.write(mov->menu_call_mask, 1);
1281 bs.write(mov->title_search_mask, 1);
1283 ArrayList<command_obj *> &cmds = mov->cmds;
1284 int num_cmds = cmds.size();
1285 bs.write(num_cmds, 16);
1286 for (int j = 0; j < num_cmds; ++j) {
1287 command_obj *cmd = cmds[j];
1288 bs.write(cmd->op_cnt, 3);
1289 bs.write(cmd->grp, 2);
1290 bs.write(cmd->sub_grp, 3);
1291 bs.write(cmd->imm_op1, 1);
1292 bs.write(cmd->imm_op2, 1);
1294 bs.write(cmd->branch_opt, 4);
1296 bs.write(cmd->cmp_opt, 4);
1298 bs.write(cmd->set_opt, 5);
1299 //bs.write(cmd->cmd, 32);
1300 bs.write(cmd->dst, 32);
1301 bs.write(cmd->src, 32);
1305 // bs.write('l', 8);
1306 // bs.writebcd(year, 16);
1307 // bs.writebcd(month, 8);
1308 // bs.writebcd(day, 8);
1309 // bs.writebcd(hour, 8);
1310 // bs.writebcd(min, 8);
1311 // bs.writebcd(sec, 8);
1317 pb_obj::set_hdmv(int id, int pt)
1327 pb_obj::set_bdj(char *nm, int pt)
1333 bdj_name = cstrdup(nm);
1337 pb_obj::write_hdmv_obj(int id_ref)
1339 bs.write(pb_typ, 2);
1341 bs.write(id_ref, 16);
1346 pb_obj::write_bdj_obj(char *name)
1348 bs.write(pb_typ, 2);
1359 write_bdj_obj(bdj_name);
1363 write_hdmv_obj(hdmv_id);
1369 title_obj::write_obj()
1371 bs.write(obj_typ, 2);
1372 bs.write(acc_typ, 2);
1374 pb_obj::write_obj();
1379 index_file::write_pb_obj(pb_obj * pb)
1381 bs.write(pb->obj_typ, 2);
1390 bs.writeb("INDX", 4);
1391 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1393 exten.bs_ofs(bs, 32);
1394 int appinfo_start = 0x28;
1395 bs.posb(appinfo_start);
1396 appinf.bs_len(bs, 32);
1398 bs.write(initial_output_mode_preference, 1);
1399 bs.write(content_exist_flag, 1);
1401 bs.write(video_format, 4);
1402 bs.write(frame_rate, 4);
1403 bs.writeb(user_data, 32);
1408 write_pb_obj(&first_play);
1409 write_pb_obj(&top_menu);
1410 bs.write(titles.size(), 16);
1412 for (int i = 0; i < titles.size(); ++i)
1413 titles[i]->write_obj();
1415 exten.bs_len(bs,32);
1425 bs.writeb("BDID",4);
1426 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1427 bs.write(data_start, 32);
1428 bs.write(extension_data_start, 32);
1430 bs.writeb(org_id, sizeof(org_id));
1431 bs.writeb(disc_id, sizeof(disc_id));
1435 // XXX - not current referenced
1438 bdmv_write_extension_data(int start_address,
1439 int (*handler) (int, int, void *), void *handle)
1444 bs.write(length, 32); /* length of extension data block */
1448 bs.pad(32); /* relative start address of extension data */
1449 bs.pad(24); /* padding */
1450 bs.write(num_entries, 8);
1452 for (n = 0; n < num_entries; n++) {
1455 bs.write(ext_start, 32);
1456 bs.write(ext_len, 32);
1457 saved_pos = bs.pos() >> 3;
1458 bs.posb(start_address + ext_start);
1459 handler(id1, id2, handle);
1468 clpi_prog_stream::write()
1473 bs.write(coding_type, 8);
1474 switch (coding_type) {
1475 case BLURAY_STREAM_TYPE_VIDEO_MPEG1:
1476 case BLURAY_STREAM_TYPE_VIDEO_MPEG2:
1477 case BLURAY_STREAM_TYPE_VIDEO_VC1:
1478 case BLURAY_STREAM_TYPE_VIDEO_H264:
1480 bs.write(format, 4);
1482 bs.write(aspect, 4);
1484 bs.write(oc_flag, 1);
1488 case BLURAY_STREAM_TYPE_AUDIO_MPEG1:
1489 case BLURAY_STREAM_TYPE_AUDIO_MPEG2:
1490 case BLURAY_STREAM_TYPE_AUDIO_LPCM:
1491 case BLURAY_STREAM_TYPE_AUDIO_AC3:
1492 case BLURAY_STREAM_TYPE_AUDIO_DTS:
1493 case BLURAY_STREAM_TYPE_AUDIO_TRUHD:
1494 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS:
1495 case BLURAY_STREAM_TYPE_AUDIO_DTSHD:
1496 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER:
1497 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY:
1498 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY:
1499 bs.write(format, 4);
1504 case BLURAY_STREAM_TYPE_SUB_PG:
1505 case BLURAY_STREAM_TYPE_SUB_IG:
1510 case BLURAY_STREAM_TYPE_SUB_TEXT:
1511 bs.write(char_code, 8);
1516 fprintf(stderr, "clpi_prog_stream: unrecognized coding type %02x\n",
1526 clpi_cl::write_header()
1528 bs.writeb("HDMV", 4);
1529 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1530 bs.write(sequence_info_start_addr, 32);
1531 bs.write(program_info_start_addr, 32);
1532 bs.write(cpi_start_addr, 32);
1533 bs.write(clip_mark_start_addr, 32);
1534 bs.write(ext_data_start_addr, 32);
1539 clpi_atc_delta::write()
1541 bs.write(delta, 32);
1542 bs.writeb(file_id, 5);
1543 bs.writeb(file_code, 4);
1549 clpi_clip_info::write()
1554 bs.pad(16); // reserved
1555 bs.write(clip_stream_type, 8);
1556 bs.write(application_type, 8);
1557 bs.pad(31); // skip reserved 31 bits
1558 bs.write(is_atc_delta, 1);
1559 bs.write(ts_recording_rate, 32);
1560 bs.write(num_source_packets, 32);
1562 bs.padb(128); // Skip reserved 128 bytes
1564 // ts type info block
1566 bs.write(ts_len, 16);
1567 int64_t pos = bs.posb();
1569 bs.write(ts_type_info.validity, 8);
1570 bs.writeb(ts_type_info.format_id, 4);
1571 // pad the stuff we don't know anything about
1572 bs.padb(ts_len - (bs.posb() - pos));
1575 if( is_atc_delta ) {
1576 bs.pad(8); // Skip reserved byte
1577 bs.write(atc_delta.size(), 8);
1578 for( int ii=0; ii < atc_delta.size(); ++ii )
1579 if( atc_delta[ii]->write() ) return 1;
1583 if( application_type == 6 /* Sub TS for a sub-path of Text subtitle */ ) {
1585 bs.write(font_info.font.size(), 8);
1586 if( font_info.font.size() ) {
1587 for( int ii=0; ii < font_info.font.size(); ++ii ) {
1588 bs.writeb(font_info.font[ii]->file_id, 5);
1599 clpi_stc_seq::write()
1601 bs.write(pcr_pid, 16);
1602 bs.write(spn_stc_start, 32);
1603 bs.write(presentation_start_time, 32);
1604 bs.write(presentation_end_time, 32);
1609 clpi_atc_seq::write()
1611 bs.write(spn_atc_start, 32);
1612 bs.write(stc_seq.size(), 8);
1613 bs.write(offset_stc_id, 8);
1615 for( int ii=0; ii < stc_seq.size(); ++ii )
1616 if( stc_seq[ii]->write() ) return 1;
1621 clpi_sequences::write()
1624 bs.padb(1); // reserved byte
1625 bs.write(size(), 8);
1626 for( int ii=0; ii < size(); ++ii )
1627 if( get(ii)->write() ) return 1;
1635 bs.write(spn_program_sequence_start, 32);
1636 bs.write(program_map_pid, 16);
1637 bs.write(streams.size(), 8);
1638 bs.write(num_groups, 8);
1639 for( int ii=0; ii < streams.size(); ++ii )
1640 if( streams[ii]->write() ) return 1;
1645 clpi_programs::write()
1648 bs.padb(1); // reserved byte
1649 bs.write(size(), 8);
1650 for( int ii=0; ii < size(); ++ii )
1651 if( get(ii)->write() ) return 1;
1657 clpi_ep_coarse::write()
1659 bs.write(ref_ep_fine_id, 18);
1660 bs.write(pts_ep, 14);
1661 bs.write(spn_ep, 32);
1666 clpi_ep_fine::write()
1668 bs.write(is_angle_change_point, 1);
1669 bs.write(i_end_position_offset, 3);
1670 bs.write(pts_ep, 11);
1671 bs.write(spn_ep, 17);
1676 clpi_ep_map_entry::write(uint32_t ep_map_pos)
1680 bs.write(ep_stream_type, 4);
1681 bs.write(coarse.size(), 16);
1682 bs.write(fine.size(), 18);
1683 bs.write(ep_map_stream_start_addr - ep_map_pos, 32);
1688 clpi_ep_map_entry::write_map()
1690 ep_map_stream_start_addr = bs.posb();
1691 bs.write(fine_start, 32);
1693 for( int ii=0; ii < coarse.size(); ++ii )
1694 if( coarse[ii]->write() ) return 1;
1696 fine_start = bs.posb() - ep_map_stream_start_addr;
1697 for( int ii=0; ii < fine.size(); ++ii )
1698 if( fine[ii]->write() ) return 1;
1709 uint32_t ep_map_pos = bs.posb();
1711 // EP Map starts here
1713 bs.write(size(), 8);
1715 for( int ii=0; ii < size(); ++ii )
1716 if( get(ii)->write(ep_map_pos) ) return 1;
1718 for( int ii=0; ii < size(); ++ii )
1719 if( get(ii)->write_map() ) return 1;
1726 clpi_extents::write()
1729 bs.write(size(), 32);
1730 for( int ii=0; ii < size(); ++ii )
1731 bs.write(get(ii), 32);
1736 clpi_cl::write_clpi_extension(int id1, int id2, void *handle)
1738 clpi_cl *cl = (clpi_cl *) handle;
1742 // LPCM down mix coefficient
1743 //write_lpcm_down_mix_coeff(&cl->lpcm_down_mix_coeff);
1750 // Extent start point
1751 return cl->extents.write();
1755 return cl->programs_ss.write();
1759 return cl->cpi_ss.write();
1763 fprintf(stderr, "write_clpi_extension(): unhandled extension %d.%d\n", id1, id2);
1770 if( write_header() ) return 1;
1771 if( clip.write() ) return 1;
1772 sequence_info_start_addr = bs.posb();
1773 if( sequences.write() ) return 1;
1774 program_info_start_addr = bs.posb();
1775 if( programs.write() ) return 1;
1776 cpi_start_addr = bs.posb();
1777 if( cpi.write() ) return 1;
1778 //if( has_ext_data ) {
1779 // ext_data_start_addr = bs.pos();
1780 // bdmv_write_extension_data(write_clpi_extension, this);
1788 bs.write(menu_call, 1);
1789 bs.write(title_search, 1);
1790 bs.write(chapter_search, 1);
1791 bs.write(time_search, 1);
1792 bs.write(skip_to_next_point, 1);
1793 bs.write(skip_to_prev_point, 1);
1794 bs.write(play_firstplay, 1);
1796 bs.write(pause_on, 1);
1797 bs.write(pause_off, 1);
1798 bs.write(still_off, 1);
1799 bs.write(forward, 1);
1800 bs.write(backward, 1);
1801 bs.write(resume, 1);
1802 bs.write(move_up, 1);
1803 bs.write(move_down, 1);
1804 bs.write(move_left, 1);
1805 bs.write(move_right, 1);
1806 bs.write(select, 1);
1807 bs.write(activate, 1);
1808 bs.write(select_and_activate, 1);
1809 bs.write(primary_audio_change, 1);
1811 bs.write(angle_change, 1);
1812 bs.write(popup_on, 1);
1813 bs.write(popup_off, 1);
1814 bs.write(pg_enable_disable, 1);
1815 bs.write(pg_change, 1);
1816 bs.write(secondary_video_enable_disable, 1);
1817 bs.write(secondary_video_change, 1);
1818 bs.write(secondary_audio_enable_disable, 1);
1819 bs.write(secondary_audio_change, 1);
1821 bs.write(pip_pg_change, 1);
1830 bs.pad(8); // Reserved
1831 bs.write(playback_type, 8);
1832 if (playback_type == BLURAY_PLAYBACK_TYPE_RANDOM ||
1833 playback_type == BLURAY_PLAYBACK_TYPE_SHUFFLE ) {
1834 bs.write(playback_count, 16);
1837 bs.pad(16); // Reserved
1840 bs.write(random_access_flag, 1);
1841 bs.write(audio_mix_flag, 1);
1842 bs.write(lossless_bypass_flag, 1);
1843 bs.pad(13); // Reserved
1849 mpls_pl::write_header()
1851 bs.writeb("MPLS", 4);
1852 bs.writeb(sig == 1 ? "0100" : "0200", 4);
1853 bs.write(list_pos, 32);
1854 bs.write(mark_pos, 32);
1855 bs.write(ext_pos, 32);
1856 bs.pad(160); // Skip 160 reserved bits
1866 bs.write(stream_type, 8);
1867 switch (stream_type) {
1874 bs.write(subpath_id, 8);
1875 bs.write(subclip_id, 8);
1880 bs.write(subpath_id, 8);
1885 fprintf(stderr, "unrecognized stream type %02x\n", stream_type);
1891 bs.write(coding_type, 8);
1892 switch (coding_type) {
1893 case BLURAY_STREAM_TYPE_VIDEO_MPEG1:
1894 case BLURAY_STREAM_TYPE_VIDEO_MPEG2:
1895 case BLURAY_STREAM_TYPE_VIDEO_VC1:
1896 case BLURAY_STREAM_TYPE_VIDEO_H264:
1897 bs.write(format, 4);
1901 case BLURAY_STREAM_TYPE_AUDIO_MPEG1:
1902 case BLURAY_STREAM_TYPE_AUDIO_MPEG2:
1903 case BLURAY_STREAM_TYPE_AUDIO_LPCM:
1904 case BLURAY_STREAM_TYPE_AUDIO_AC3:
1905 case BLURAY_STREAM_TYPE_AUDIO_DTS:
1906 case BLURAY_STREAM_TYPE_AUDIO_TRUHD:
1907 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS:
1908 case BLURAY_STREAM_TYPE_AUDIO_DTSHD:
1909 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_MASTER:
1910 case BLURAY_STREAM_TYPE_AUDIO_AC3PLUS_SECONDARY:
1911 case BLURAY_STREAM_TYPE_AUDIO_DTSHD_SECONDARY:
1912 bs.write(format, 4);
1917 case BLURAY_STREAM_TYPE_SUB_PG:
1918 case BLURAY_STREAM_TYPE_SUB_IG:
1922 case BLURAY_STREAM_TYPE_SUB_TEXT:
1923 bs.write(char_code, 8);
1928 fprintf(stderr, "mpls_stream: unrecognized coding type %02x\n", coding_type);
1939 bs.pad(16); // Skip 2 reserved bytes
1941 bs.write(video.size(), 8);
1942 bs.write(audio.size(), 8);
1943 bs.write(pg.size() - num_pip_pg, 8);
1944 bs.write(ig.size(), 8);
1945 bs.write(secondary_audio.size(), 8);
1946 bs.write(secondary_video.size(), 8);
1947 bs.write(num_pip_pg, 8);
1952 // Primary Video Streams
1953 for( int ii=0; ii < video.size(); ++ii )
1954 if( video[ii]->write() ) return 1;
1956 // Primary Audio Streams
1957 for( int ii=0; ii < audio.size(); ++ii )
1958 if( audio[ii]->write() ) return 1;
1960 // Presentation Graphic Streams
1961 for( int ii=0; ii < pg.size(); ++ii )
1962 if( pg[ii]->write() ) return 1;
1964 // Interactive Graphic Streams
1965 for( int ii=0; ii < ig.size(); ++ii )
1966 if( ig[ii]->write() ) return 1;
1968 // Secondary Audio Streams
1969 for( int ii=0; ii < secondary_audio.size(); ++ii ) {
1970 if( secondary_audio[ii]->write() ) return 1;
1971 // Read Secondary Audio Extra Attributes
1972 bs.write(sa_primary_audio_ref.size(), 8);
1974 for( int jj=0; jj < sa_primary_audio_ref.size(); ++jj )
1975 bs.write(sa_primary_audio_ref[jj], 8);
1976 if( sa_primary_audio_ref.size() % 2) bs.pad(8 );
1979 // Secondary Video Streams
1980 for( int ii=0; ii < secondary_video.size(); ++ii ) {
1981 if( secondary_video[ii]->write() ) return 1;
1982 // Read Secondary Video Extra Attributes
1983 bs.write(sv_secondary_audio_ref.size(), 8);
1985 for( int jj=0; jj < sv_secondary_audio_ref.size(); ++jj )
1986 bs.write(sv_secondary_audio_ref[jj], 8);
1987 if( sv_secondary_audio_ref.size() % 2) bs.pad(8 );
1988 bs.write(sv_pip_pg_ref.size(), 8);
1990 for( int jj=0; jj < sv_pip_pg_ref.size(); ++jj )
1991 bs.write(sv_pip_pg_ref[jj], 8);
1992 if( sv_pip_pg_ref.size() % 2) bs.pad(8 );
2003 // Primary Clip identifer
2004 bs.writeb(clip[0]->clip_id, 5);
2005 bs.writeb(clip[0]->codec_id, 4); // "M2TS"
2006 bs.pad(11); // Skip reserved 11 bits
2008 bs.write(is_multi_angle, 1);
2009 bs.write(connection_condition, 4); // 0x01, 0x05, 0x06
2011 bs.write(clip[0]->stc_id, 8);
2012 bs.write(in_time, 32);
2013 bs.write(out_time, 32);
2016 bs.write(random_access_flag, 1);
2018 bs.write(still_mode, 8);
2019 if( still_mode == 0x01 ) {
2020 bs.write(still_time, 16);
2026 if( is_multi_angle ) {
2027 bs.write(clip.size(), 8);
2029 bs.write(is_different_audio, 1);
2030 bs.write(is_seamless_angle, 1);
2033 for( int ii=1; ii < clip.size(); ++ii ) {
2034 bs.writeb(clip[ii]->clip_id, 5);
2035 bs.writeb(clip[ii]->codec_id, 4); // "M2TS"
2036 bs.write(clip[ii]->stc_id, 8);
2039 if( stn.write() ) return 1;
2046 mpls_sub_pi::write()
2049 // Primary Clip identifer
2050 bs.writeb(clip[0]->clip_id, 5);
2051 bs.writeb(clip[0]->codec_id, 4); // "M2TS"
2054 bs.write(connection_condition, 4); // 0x01, 0x05, 0x06
2056 bs.write(is_multi_clip, 1);
2057 bs.write(clip[0]->stc_id, 8);
2058 bs.write(in_time, 32);
2059 bs.write(out_time, 32);
2060 bs.write(sync_play_item_id, 16);
2061 bs.write(sync_pts, 32);
2064 bs.write(clip.size(), 8);
2066 for( int ii=1; ii < clip.size(); ++ii ) {
2067 // Primary Clip identifer
2068 bs.writeb(clip[ii]->clip_id, 5);
2069 bs.writeb(clip[ii]->codec_id, 4); // "M2TS"
2070 bs.write(clip[ii]->stc_id, 8);
2084 bs.write(is_repeat, 1);
2086 bs.write(sub_play_item.size(), 8);
2088 for( int ii=0; ii < sub_play_item.size(); ++ii )
2089 if( sub_play_item[ii]->write() ) return 1;
2098 bs.write(mark_id, 8);
2099 bs.write(mark_type, 8);
2100 bs.write(play_item_ref, 16);
2102 bs.write(entry_es_pid, 16);
2103 bs.write(duration, 32);
2108 mpls_pl::write_playlistmark()
2110 mark.bs_len(bs, 32);
2111 // Then get the number of marks
2112 bs.write(play_mark.size(), 16);
2114 for( int ii=0; ii < play_mark.size(); ++ii )
2115 if( play_mark[ii]->write() ) return 1;
2122 mpls_pl::write_playlist()
2126 // Skip reserved bytes
2129 bs.write(play_item.size(), 16);
2130 bs.write(sub_path.size(), 16);
2132 for( int ii=0; ii < play_item.size(); ++ii )
2133 if( play_item[ii]->write() ) return 1;
2135 for( int ii=0; ii < sub_path.size(); ++ii )
2136 if( sub_path[ii]->write() ) return 1;
2143 mpls_pip_data::write()
2148 bs.write(scale_factor, 4);
2154 mpls_pip_metadata::write(uint32_t start_address)
2157 bs.write(clip_ref, 16);
2158 bs.write(secondary_video_ref, 8);
2160 bs.write(timeline_type, 4);
2161 bs.write(luma_key_flag, 1);
2162 bs.write(trick_play_flag, 1);
2164 if( luma_key_flag ) {
2166 bs.write(upper_limit_luma_key, 8);
2173 uint32_t data_address = 0; // XXX
2174 bs.write(data_address, 32);
2176 int64_t pos = bs.pos() / 8;
2177 bs.posb(start_address + data_address);
2179 bs.write(data.size(), 16);
2180 if( data.size() < 1 ) return 1;
2182 for( int ii=0; ii < data.size(); ++ii )
2183 if( data[ii]->write() ) return 1;
2190 mpls_pl::write_pip_metadata_extension()
2192 uint32_t pos = bs.posb();
2193 pipm.bs_len(bs, 32);
2195 bs.write(ext_pip_data.size(), 16);
2196 for( int ii=0; ii < ext_pip_data.size(); ++ii )
2197 if( ext_pip_data[ii]->write(pos) ) return 1;
2204 mpls_pl::write_subpath_extension()
2206 subx.bs_len(bs, 32);
2208 bs.write(ext_sub_path.size(), 16);
2209 for( int ii=0; ii < ext_sub_path.size(); ++ii )
2210 if( ext_sub_path[ii]->write() ) return 1;
2217 clpi_cl::write_mpls_extension(int id1, int id2, void *handle)
2219 mpls_pl *pl = (mpls_pl *) handle;
2223 // PiP metadata extension
2224 return pl->write_pip_metadata_extension();
2233 // SubPath entries extension
2234 return pl->write_subpath_extension();
2244 int ret = write_header();
2245 list_pos = bs.posb();;
2246 if( !ret ) ret = write_playlist();
2247 mark_pos = bs.posb();
2248 if( !ret ) ret = write_playlistmark();
2249 //if( has_pls_extension ) {
2250 // ext_pos = bs.posb();
2251 // bdmv_write_extension_data(write_mpls_extension, pl);
2258 if( !mkdir(path, 0777) )
2265 mk_bdmv_dir(char *bdmv_path)
2267 if( mk_dir(bdmv_path) )
2269 char bdjo_path[BCTEXTLEN];
2270 sprintf(bdjo_path, "%s/BDJO", bdmv_path);
2271 if( mk_dir(bdjo_path) )
2273 char clipinf_path[BCTEXTLEN];
2274 sprintf(clipinf_path, "%s/CLIPINF", bdmv_path);
2275 if( mk_dir(clipinf_path) )
2277 char jar_path[BCTEXTLEN];
2278 sprintf(jar_path, "%s/JAR", bdmv_path);
2279 if( mk_dir(jar_path) )
2281 char playlist_path[BCTEXTLEN];
2282 sprintf(playlist_path, "%s/PLAYLIST", bdmv_path);
2283 if( mk_dir(playlist_path) )
2291 char bdmv_path[BCTEXTLEN];
2292 sprintf(bdmv_path, "%s/BDMV", path);
2293 if( mk_bdmv_dir(bdmv_path) ) return 1;
2294 char stream_path[BCTEXTLEN];
2295 sprintf(stream_path, "%s/BDMV/STREAM", path);
2296 if( mk_dir(stream_path) ) return 1;
2297 char auxdata_path[BCTEXTLEN];
2298 sprintf(auxdata_path, "%s/BDMV/AUXDATA", path);
2299 if( mk_dir(auxdata_path) ) return 1;
2300 char meta_path[BCTEXTLEN];
2301 sprintf(meta_path, "%s/BDMV/META", path);
2302 if( mk_dir(meta_path) ) return 1;
2303 char backup_path[BCTEXTLEN];
2304 sprintf(backup_path, "%s/BACKUP", bdmv_path);
2305 if( mk_bdmv_dir(backup_path) ) return 1;
2310 build_toc(clpi_ep_map_entry *map)
2312 clpi_ep_coarse *cp = 0;
2313 marks.sort(mark::cmpr);
2314 uint16_t ep_pid = map->pid;
2315 int64_t last_pts = -1, last_pkt = -1;
2316 for( int ii=0; ii<marks.size(); ++ii ) {
2317 mark *mp = marks[ii];
2318 if( mp->pid != ep_pid ) continue;
2319 int64_t pts = mp->pts;
2320 if( last_pts >= pts ) continue;
2322 uint32_t pkt = mp->pos / BLURAY_TS_PKTSZ;
2323 if( last_pkt >= pkt ) continue;
2325 int64_t coarse_pts = (pts >> 18) & ~0x01;
2326 int64_t fine_pts = (pts & 0x7ffff) >> 8;
2327 uint32_t mpkt = pkt & ~0x1ffff;
2328 if( !cp || cp->pts_ep != coarse_pts || cp->spn_ep != mpkt ) {
2329 cp = new clpi_ep_coarse();
2330 map->coarse.append(cp);
2331 cp->ref_ep_fine_id = map->fine.size();
2332 cp->pts_ep = coarse_pts;
2335 clpi_ep_fine *fp = new clpi_ep_fine();
2336 map->fine.append(fp);
2337 fp->is_angle_change_point = 0;
2338 // XXX - dont know what this is
2339 fp->i_end_position_offset = 1;
2340 fp->pts_ep = fine_pts;
2341 fp->spn_ep = pkt & 0x1ffff;
2346 const AVRational media_info::clk45k = { 1, 45000 };
2348 static int bd_stream_type(AVCodecID codec_id)
2350 int stream_type = 0;
2352 case AV_CODEC_ID_MPEG1VIDEO:
2353 case AV_CODEC_ID_MPEG2VIDEO:
2354 stream_type = BLURAY_STREAM_TYPE_VIDEO_MPEG2;
2356 case AV_CODEC_ID_H264:
2357 stream_type = BLURAY_STREAM_TYPE_VIDEO_H264;
2359 case AV_CODEC_ID_MP2:
2360 case AV_CODEC_ID_MP3:
2361 stream_type = BLURAY_STREAM_TYPE_AUDIO_MPEG1;
2363 case AV_CODEC_ID_AC3:
2364 stream_type = BLURAY_STREAM_TYPE_AUDIO_AC3;
2366 case AV_CODEC_ID_DTS:
2367 stream_type = BLURAY_STREAM_TYPE_AUDIO_DTS;
2369 case AV_CODEC_ID_TRUEHD:
2370 stream_type = BLURAY_STREAM_TYPE_AUDIO_TRUHD;
2373 fprintf(stderr, "unknown bluray stream type %s\n", avcodec_get_name(codec_id));
2379 static int bd_audio_format(int channels)
2381 int audio_format = 0;
2382 switch( channels ) {
2384 audio_format = BLURAY_AUDIO_FORMAT_MONO;
2387 audio_format = BLURAY_AUDIO_FORMAT_STEREO;
2390 audio_format = BLURAY_AUDIO_FORMAT_MULTI_CHAN;
2393 fprintf(stderr, "unknown bluray audio format %d ch\n", channels);
2396 return audio_format;
2399 static int bd_audio_rate(int rate)
2403 case 48000: audio_rate = BLURAY_AUDIO_RATE_48; break;
2404 case 96000: audio_rate = BLURAY_AUDIO_RATE_96; break;
2405 case 192000: audio_rate = BLURAY_AUDIO_RATE_192; break;
2407 fprintf(stderr, "unknown bluray audio rate %d\n", rate);
2413 static int bd_video_format(int w, int h, int ilace)
2415 if( w == 720 && h == 480 && ilace ) return BLURAY_VIDEO_FORMAT_480I;
2416 if( w == 720 && h == 576 && ilace ) return BLURAY_VIDEO_FORMAT_576I;
2417 if( w == 720 && h == 480 && !ilace ) return BLURAY_VIDEO_FORMAT_480P;
2418 if( w == 1920 && h == 1080 && ilace ) return BLURAY_VIDEO_FORMAT_1080I;
2419 if( w == 1280 && h == 720 && !ilace ) return BLURAY_VIDEO_FORMAT_720P;
2420 if( w == 1920 && h == 1080 && !ilace ) return BLURAY_VIDEO_FORMAT_1080P;
2421 if( w == 720 && h == 576 && !ilace ) return BLURAY_VIDEO_FORMAT_576P;
2422 fprintf(stderr, "unknown bluray video format %dx%d %silace\n",
2423 w, h, !ilace ? "not " : "");
2427 static int bd_video_rate(double rate)
2429 if( fabs(rate-23.976) < 0.01 ) return BLURAY_VIDEO_RATE_24000_1001;
2430 if( fabs(rate-24.000) < 0.01 ) return BLURAY_VIDEO_RATE_24;
2431 if( fabs(rate-25.000) < 0.01 ) return BLURAY_VIDEO_RATE_25;
2432 if( fabs(rate-29.970) < 0.01 ) return BLURAY_VIDEO_RATE_30000_1001;
2433 if( fabs(rate-50.000) < 0.01 ) return BLURAY_VIDEO_RATE_50;
2434 if( fabs(rate-59.940) < 0.01 ) return BLURAY_VIDEO_RATE_60000_1001;
2435 fprintf(stderr, "unknown bluray video framerate %5.2f\n",rate);
2439 static int bd_aspect_ratio(int w, double ratio)
2441 if( fabs(ratio-1.333) < 0.01 ) return BLURAY_ASPECT_RATIO_4_3;
2442 if( fabs(ratio-1.777) < 0.01 ) return BLURAY_ASPECT_RATIO_16_9;
2443 return w == 720 ? BLURAY_ASPECT_RATIO_4_3 : BLURAY_ASPECT_RATIO_16_9;
2444 fprintf(stderr, "unknown bluray aspect ratio %5.3f\n",ratio);
2448 int media_info::scan()
2451 if( stat(filename, &st) ) return 1;
2452 file_size = st.st_size;
2454 AVFormatContext *fmt_ctx = 0;
2455 AVDictionary *fopts = 0;
2456 av_dict_set(&fopts, "formatprobesize", "5000000", 0);
2457 av_dict_set(&fopts, "scan_all_pmts", "1", 0);
2458 av_dict_set(&fopts, "threads", "auto", 0);
2459 int ret = avformat_open_input(&fmt_ctx, filename, NULL, &fopts);
2460 av_dict_free(&fopts);
2463 ret = avformat_find_stream_info(fmt_ctx, NULL);
2466 bit_rate = fmt_ctx->bit_rate;
2470 for( int i=0; ret>=0 && i<(int)fmt_ctx->nb_streams; ++i ) {
2471 AVStream *st = fmt_ctx->streams[i];
2472 AVMediaType type = st->codec->codec_type;
2474 case AVMEDIA_TYPE_VIDEO: break;
2475 case AVMEDIA_TYPE_AUDIO: break;
2478 stream *s = new stream(type, i);
2480 AVCodecID codec_id = st->codec->codec_id;
2482 case AVMEDIA_TYPE_VIDEO: {
2483 if( ep_pid < 0 ) ep_pid = st->id;
2484 s->coding_type = bd_stream_type(codec_id);
2485 s->format = bd_video_format(st->codec->width, st->codec->height,
2486 st->codec->flags & CODEC_FLAG_INTERLACED_ME);
2487 s->rate = bd_video_rate(!st->codec->framerate.den ? 0 :
2488 (double)st->codec->framerate.num / st->codec->framerate.den);
2489 s->aspect = bd_aspect_ratio(st->codec->width,!st->sample_aspect_ratio.den ? 0 :
2490 (double)st->sample_aspect_ratio.num / st->sample_aspect_ratio.den);
2492 case AVMEDIA_TYPE_AUDIO: {
2493 s->coding_type = bd_stream_type(codec_id);
2494 s->format = bd_audio_format(st->codec->channels);
2495 s->rate = bd_audio_rate(st->codec->sample_rate);
2496 strcpy((char*)s->lang, "und");
2501 if( bit_rate > 0 && st->duration == AV_NOPTS_VALUE &&
2502 st->time_base.num < INT64_MAX / bit_rate ) {
2503 st->duration = av_rescale(8*file_size, st->time_base.den,
2504 bit_rate * (int64_t) st->time_base.num);
2506 s->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2509 AVCodec *decoder = avcodec_find_decoder(codec_id);
2510 AVDictionary *copts = 0;
2511 ret = avcodec_open2(st->codec, decoder, &copts);
2514 ep_pid = fmt_ctx->nb_streams > 0 ? fmt_ctx->streams[0]->id : 0;
2516 int npgm = fmt_ctx->nb_programs;
2518 program *pgm = new program(-1, 1);
2519 pgm->ep_pid = ep_pid;
2520 pgm->pmt_pid = 0x1000;
2521 pgm->pcr_pid = 0x100;
2522 for( int jj=0; jj<streams.size(); ++jj )
2523 pgm->strm_idx.append(jj);
2526 for( int ii=0; ii<npgm; ++ii ) {
2527 AVProgram *pgrm = fmt_ctx->programs[ii];
2528 program *pgm = new program(ii, pgrm->id);
2529 pgm->pmt_pid = pgrm->pmt_pid;
2530 pgm->pcr_pid = pgrm->pcr_pid;
2533 for( int jj=0; jj<(int)pgrm->nb_stream_indexes; ++jj ) {
2534 int av_idx = pgrm->stream_index[jj];
2535 AVStream *st = fmt_ctx->streams[av_idx];
2536 AVMediaType type = st->codec->codec_type;
2538 case AVMEDIA_TYPE_VIDEO:
2539 if( ep_pid < 0 ) ep_pid = st->id;
2541 case AVMEDIA_TYPE_AUDIO:
2546 int sidx = streams.size();
2547 while( --sidx>=0 && streams[sidx]->av_idx!=av_idx );
2549 fprintf(stderr, "bad stream idx %d in pgm %d\n",av_idx, ii);
2552 if( pgm->duration < st->duration )
2553 pgm->duration = av_rescale_q(st->duration, st->time_base, clk45k);
2554 pgm->strm_idx.append(sidx);
2557 AVProgram *pgrm = fmt_ctx->programs[0];
2558 ep_pid = pgrm->nb_stream_indexes > 0 ?
2559 fmt_ctx->streams[pgrm->stream_index[0]]->id : 0;
2561 pgm->ep_pid = ep_pid;
2562 programs.append(pgm);
2566 ret = scan(fmt_ctx);
2569 avformat_close_input(&fmt_ctx);
2574 int media_info::scan(AVFormatContext *fmt_ctx)
2578 av_init_packet(&ipkt);
2580 for( int64_t count=0; ret>=0; ++count ) {
2581 av_packet_unref(&ipkt);
2582 ipkt.data = 0; ipkt.size = 0;
2584 ret = av_read_frame(fmt_ctx, &ipkt);
2586 if( ret == AVERROR_EOF ) break;
2590 if( !ipkt.data ) continue;
2591 if( (ipkt.flags & AV_PKT_FLAG_CORRUPT) ) continue;
2592 if( ipkt.pts == AV_NOPTS_VALUE ) continue;
2593 int i = ipkt.stream_index;
2594 if( i < 0 || i >= streams.size() ) continue;
2598 for( int ii=0; !pgm && ii<programs.size(); ++ii ) {
2599 program *p = programs[ii];
2600 for( int jj=0; jj<p->strm_idx.size(); ++jj ) {
2601 sp = streams[p->strm_idx[jj]];
2602 if( sp->av_idx == i ) { pgm = p; break; }
2605 if( !pgm ) continue;
2606 AVStream *st = fmt_ctx->streams[i];
2607 if( pgm->ep_pid != st->id ) continue;
2608 int64_t pts45k = av_rescale_q(ipkt.pts, st->time_base, clk45k);
2609 if( sp->start_pts > pts45k ) sp->start_pts = pts45k;
2610 if( sp->end_pts < pts45k ) sp->end_pts = pts45k;
2611 if( pgm->start_time > pts45k ) pgm->start_time = pts45k;
2612 if( pgm->end_time < pts45k ) pgm->end_time = pts45k;
2614 if( !(ipkt.flags & AV_PKT_FLAG_KEY) ) continue;
2616 if( sp->last_pts != pts45k ) {
2617 sp->last_pts = pts45k;
2618 pgm->add_label(count, ipkt.pos, pts45k, st->id);
2622 for( int ii=0; ii<programs.size(); ++ii ) {
2623 program *pgm = programs[ii];
2624 if( pgm->end_time > pgm->start_time )
2625 pgm->duration = pgm->end_time - pgm->start_time;
2628 return ret != AVERROR_EOF ? -1 : 0;
2637 idx.first_play.set_hdmv(0, pb_typ_movie);
2638 idx.top_menu.set_hdmv(0xffff, pb_typ_iactv);
2643 movie_obj *mp = new movie_obj();
2644 mp->resume_intention_flag = 1;
2645 command_obj *cmd = new command_obj();
2646 cmd->cmd = htobe32(0x21810000); cmd->dst = 1; cmd->src = 0;
2647 mp->cmds.append(cmd); // JUMP_TITLE 1
2648 cmd = new command_obj();
2649 cmd->cmd = htobe32(0x00020000); cmd->dst = 0; cmd->src = 0;
2650 mp->cmds.append(cmd);
2651 mov.movies.append(mp); // BREAK
2654 for( int ii=0; ii<size(); ++ii ) {
2655 mp = new movie_obj();
2656 mp->resume_intention_flag = 1;
2657 cmd = new command_obj();
2658 cmd->cmd = htobe32(0x22800000); cmd->dst = ii; cmd->src = 0;
2659 mp->cmds.append(cmd); // PLAY_PL ii
2660 cmd = new command_obj();
2661 cmd->cmd = htobe32(0x00020000); cmd->dst = 0; cmd->src = 0;
2662 mp->cmds.append(cmd);
2663 mov.movies.append(mp); // BREAK
2666 mp = new movie_obj();
2667 mp->resume_intention_flag = 1;
2668 cmd = new command_obj();
2669 cmd->cmd = htobe32(0x21810000); cmd->dst = 1; cmd->src = 0;
2670 mp->cmds.append(cmd); // JUMP_TITLE 1
2671 cmd = new command_obj();
2672 cmd->cmd = htobe32(0x00020000); cmd->dst = 0; cmd->src = 0;
2673 mp->cmds.append(cmd);
2674 mov.movies.append(mp); // BREAK
2678 for( int ii=0; ii<size(); ++ii ) {
2680 tp = new title_obj();
2681 tp->set_hdmv(idx.titles.size()+1, pb_typ_movie);
2682 idx.titles.append(tp);
2685 media_info *ip = get(ii);
2686 // clip program, if specified
2687 int pidx = ip->programs.size();
2688 while( --pidx>=0 && ip->programs[pidx]->pid != ip->pgm_pid );
2689 if( pidx < 0 ) pidx = 0;
2691 ip->pgm_pid = ip->prog()->pid;
2693 clpi_cl *cp = new clpi_cl();
2694 cp->clip.clip_stream_type = 1;
2695 cp->clip.application_type = BLURAY_APP_TYPE_MAIN_MOVIE;
2696 cp->clip.ts_recording_rate = ip->bit_rate;
2697 uint32_t ts_pkt_count = ip->file_size / BLURAY_TS_PKTSZ + 1;
2698 cp->clip.num_source_packets = ts_pkt_count;
2699 cp->clip.ts_type_info.validity = 0x80;
2700 strcpy(cp->clip.ts_type_info.format_id, "HDMV");
2703 for( int jj=0; jj<ip->programs.size(); ++jj ) {
2704 program *pgm = ip->programs[jj];
2705 clpi_prog *p = new clpi_prog(pgm->pmt_pid);
2706 for( int kk=0; kk<pgm->strm_idx.size(); ++kk ) {
2707 int k = pgm->strm_idx[kk];
2708 stream *sp = ip->streams[k];
2709 clpi_prog_stream *s = new clpi_prog_stream();
2711 s->coding_type = sp->coding_type;
2712 s->format = sp->format;
2713 //use unspecified (0)
2714 // if( !idx.video_format ) idx.video_format = s->format;
2716 // if( !idx.frame_rate ) idx.frame_rate = s->rate;
2717 switch( sp->type ) {
2718 case AVMEDIA_TYPE_VIDEO:
2719 s->aspect = sp->aspect;
2721 case AVMEDIA_TYPE_AUDIO:
2722 memcpy(s->lang,sp->lang,sizeof(s->lang));
2727 p->streams.append(s);
2729 clpi_ep_map_entry *map = new clpi_ep_map_entry(pgm->ep_pid);
2730 pgm->build_toc(map);
2731 cp->cpi.append(map);
2732 cp->programs.append(p);
2734 clpi_atc_seq *atc_seq = new clpi_atc_seq();
2735 clpi_stc_seq *stc_seq = new clpi_stc_seq();
2736 stc_seq->pcr_pid = pgm->pcr_pid;
2737 stc_seq->presentation_start_time = pgm->start_time;
2738 stc_seq->presentation_end_time = pgm->end_time;
2739 atc_seq->stc_seq.append(stc_seq);
2740 cp->sequences.append(atc_seq);
2745 if( ip->brk ) tp = 0;
2748 // playlists, one per title
2749 // one playitem per media clip
2751 for( int ii=0; ii<idx.titles.size(); ++ii ) {
2753 media_info *ip = get(clip_id);
2754 program *pgm = ip->prog();
2755 mpls_pl *pp = new mpls_pl();
2756 pp->app_info.playback_type = BLURAY_PLAYBACK_TYPE_SEQUENTIAL;
2757 // pp->app_info.uo_mask.xxx = 1;
2758 int last = idx.titles[ii]->last;
2759 for( ; clip_id<=last; ++clip_id ) {
2762 mpls_pi *pi = new mpls_pi();
2763 pi->connection_condition = 1; // seamless
2764 pi->in_time = pgm->start_time;
2765 pi->out_time = pgm->end_time;
2767 pi->still_mode = BLURAY_STILL_INFINITE;
2768 int64_t end_time = pgm->start_time + pgm->duration;
2769 if( pi->out_time < end_time ) pi->out_time = end_time;
2770 mpls_clip *cp = new mpls_clip();
2771 sprintf(cp->clip_id,"%05d", clip_id);
2772 pi->clip.append(cp);
2773 for( int kk=0; kk<ip->streams.size(); ++kk ) {
2774 stream *sp = ip->streams[kk];
2775 switch( sp->type ) {
2776 case AVMEDIA_TYPE_VIDEO: break;
2777 case AVMEDIA_TYPE_AUDIO: break;
2780 mpls_stream *ps = new mpls_stream();
2782 ps->stream_type = BLURAY_PG_TEXTST_STREAM;
2783 ps->coding_type = sp->coding_type;
2784 ps->format = sp->format;
2785 ps->rate = sp->rate;
2786 switch( sp->type ) {
2787 case AVMEDIA_TYPE_VIDEO:
2788 pi->stn.video.append(ps);
2790 case AVMEDIA_TYPE_AUDIO:
2791 memcpy(ps->lang, sp->lang, sizeof(ps->lang));
2792 pi->stn.audio.append(ps);
2798 pp->play_item.append(pi);
2800 // chapter marks every ch_duration ticks
2801 int64_t ch_duration = 45000 * 60*10;
2802 int64_t mrktm = ch_duration;
2804 int pmark = 0, pitem = 0;
2805 mpls_pi *pi = pp->play_item[pitem];
2806 mpls_plm *pm = new mpls_plm();
2807 pm->mark_id = pmark++;
2808 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
2809 pm->play_item_ref = pitem;
2810 pm->time = pi->in_time;
2811 pm->entry_es_pid = 0;
2812 pp->play_mark.append(pm);
2813 for( int jj=0; jj < pp->play_item.size(); ++jj ) {
2815 pi = pp->play_item[pitem];
2816 int64_t pi_duration = pi->out_time - pi->in_time;
2817 int64_t endtm = plytm + pi_duration;
2818 while( mrktm < endtm ) {
2819 pm = new mpls_plm();
2820 pm->mark_id = pmark++;
2821 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
2822 pm->play_item_ref = pitem;
2823 pm->time = pi->in_time + mrktm - plytm;
2824 pm->entry_es_pid = 0;
2825 pp->play_mark.append(pm);
2826 mrktm += ch_duration;
2830 pm = new mpls_plm();
2831 pm->mark_id = pmark;
2832 pm->mark_type = BLURAY_MARK_TYPE_ENTRY;
2833 pm->play_item_ref = pitem;
2834 pm->time = pi->out_time;
2835 pm->entry_es_pid = 0;
2836 pp->play_mark.append(pm);
2844 bd_path(const char *bp, const char *fmt, va_list ap)
2846 int n = sizeof(filename)-1;
2847 char *cp = filename;
2848 const char *pp = path;
2849 while( n>0 && (*cp=*pp)!=0 ) { --n; ++cp; ++pp; }
2850 while( n>0 && (*cp=*bp)!=0 ) { --n; ++cp; ++bp; }
2851 n -= vsnprintf(cp, n, fmt, ap);
2853 return n > 0 ? 0 : 1;
2857 bd_copy(const char *ifn, const char *fmt, ...)
2861 FILE *ifp = fopen(ifn,"r");
2865 if( bd_path("/BDMV/", fmt, ap) ) return 1;
2867 FILE *ofp = fopen(filename,"w");
2869 setvbuf(ifp, 0, _IOFBF, 0x80000);
2870 setvbuf(ofp, 0, _IOFBF, 0x80000);
2871 while( (n=fread(bfr,1,sizeof(bfr),ifp)) > 0 ) fwrite(bfr,1,n,ofp);
2878 fprintf(stderr, "cant copy clip %s\n",ifn);
2883 bd_open(const char *fmt, ...)
2886 if( !path ) return 0;
2889 if( bd_path("/BDMV/", fmt, ap) ) return 1;
2891 if( bs.open(filename) ) {
2892 fprintf(stderr, "cant open file %s\n",filename);
2899 bd_backup(const char *fmt, ...)
2902 if( !path ) return 0;
2905 FILE *ifp = fopen(filename,"r");
2909 if( bd_path("/BDMV/BACKUP/", fmt, ap) ) return 1;
2911 FILE *ofp = fopen(filename,"w");
2913 while( (n=fread(bfr,1,sizeof(bfr),ifp)) > 0 ) fwrite(bfr,1,n,ofp);
2920 fprintf(stderr, "cant backup %s\n",filename);
2924 int Media::write(char *fn)
2928 if( bd_open("index.bdmv") ) return 1;
2929 if( idx.write() ) return 1;
2930 if( bd_backup("index.bdmv") ) return 1;
2932 if( bd_open("MovieObject.bdmv") ) return 1;
2933 if( mov.write() ) return 1;
2934 if( bd_backup("MovieObject.bdmv") ) return 1;
2936 for( int ii=0; ii<cl.size(); ++ii ) {
2937 if( bd_open("CLIPINF/%05d.clpi", ii) ) return 1;
2938 if( cl[ii]->write() ) return 1;
2939 if( bd_backup("CLIPINF/%05d.clpi", ii) ) return 1;
2942 for( int ii=0; ii<pl.size(); ++ii ) {
2943 if( bd_open("PLAYLIST/%05d.mpls", ii) ) return 1;
2944 if( pl[ii]->write() ) return 1;
2945 if( bd_backup("PLAYLIST/%05d.mpls", ii) ) return 1;
2951 main(int ac, char **av)
2954 if( mkbdmv(path) ) return 1;
2956 av_log_set_level(AV_LOG_FATAL);
2957 //av_log_set_level(AV_LOG_VERBOSE);
2958 //av_log_set_level(AV_LOG_DEBUG);
2962 for( int ii=2; ii<ac; ++ii ) {
2964 // any dash seq followed by number sets curr title pgm_pid
2965 // single dash only sets title pgm_pid
2966 // double dash ends curr title, starts a new title
2967 // triple dash ends curr title as infinite still
2970 if( *++ap == '-' ) {
2972 if( *++ap == '-' ) { ++ap; mp->still = 1; }
2974 if( *ap >= '0' && *ap <= '9' )
2975 mp->pgm_pid = strtoul(ap,&ap,0);
2976 if( mp->brk ) mp = 0;
2978 fprintf(stderr, "err arg %d: %s\n",ii,av[ii]);
2983 mp = new media_info(av[ii]);
2986 fprintf(stderr, "cant scan media: %s\n", av[ii]);
2991 if( mp ) mp->brk = 1;
2993 if( media.compose() ) {
2994 fprintf(stderr, "cant compose media\n");
2997 if( media.write(0) ) {
2998 fprintf(stderr, "cant prepare media\n");
3001 if( media.write(path) ) {
3002 fprintf(stderr, "cant write media\n");
3006 for( int ii=0; ii<media.size(); ++ii )
3007 if( media.bd_copy(media[ii]->filename, "STREAM/%05d.m2ts", ii) )