aboutsummaryrefslogtreecommitdiff
path: root/remuxer/src/remux.rs
blob: a44c58ba7d48b73b8683c3d8f847905cb4bc35ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2025 metamuffin <metamuffin.org>
*/
use jellybase::common::Node;
use std::{io::Write, ops::Range, path::PathBuf};

// struct ClusterLayout {
//     position: usize,
//     timestamp: u64,
//     source_offsets: Vec<Option<u64>>,
//     blocks: Vec<(usize, BlockIndex)>,
// }

pub fn remux_stream_into(
    _writer: impl Write,
    _range: Range<usize>,
    _path_base: PathBuf,
    _item: &Node,
    _selection: Vec<usize>,
    _webm: bool,
) -> anyhow::Result<()> {
    // info!("remuxing {:?} to have tracks {selection:?}", item.title);
    // let writer = TrimWriter::new(BufWriter::new(writer), range.clone());
    // let mut output = EbmlWriter::new(writer, 0);

    // struct ReaderC {
    //     info: SourceTrack,
    //     reader: EbmlReader,
    //     mapped: u64,
    //     index: Arc<SeekIndex>,
    //     source_track_index: usize,
    //     codec_private: Option<Vec<u8>>,
    //     layouting_progress_index: usize,
    // }

    // let timing_cp = Instant::now();

    // let mut inputs = selection
    //     .iter()
    //     .enumerate()
    //     .map(|(index, sel)| {
    //         let info = item
    //             .media
    //             .as_ref()
    //             .unwrap()
    //             .tracks
    //             .get(*sel)
    //             .ok_or(anyhow!("track not available"))?
    //             .to_owned();
    //         let source_path = path_base.join(&private.path);
    //         let mapped = index as u64 + 1;
    //         info!("\t- {sel} {source_path:?} ({} => {mapped})", private.track);
    //         info!("\t    {}", info);
    //         let file = File::open(&source_path).context("opening source file")?;
    //         let index = get_seek_index(&source_path)?;
    //         let index = index
    //             .get(&(private.track as u64))
    //             .ok_or(anyhow!("track missing 3"))?
    //             .to_owned();
    //         debug!("\t    seek index: {} blocks loaded", index.blocks.len());
    //         let reader = EbmlReader::new(BufReader::new(file));
    //         Ok(ReaderC {
    //             index,
    //             reader,
    //             info,
    //             mapped,
    //             source_track_index: private.track,
    //             codec_private: private.codec_private.clone(),
    //             layouting_progress_index: 0,
    //         })
    //     })
    //     .collect::<anyhow::Result<Vec<_>>>()?;

    // info!("(perf) prepare inputs: {:?}", Instant::now() - timing_cp);
    // let timing_cp = Instant::now();

    // output.write_tag(&ebml_header(webm))?;

    // output.write_tag(&MatroskaTag::Segment(Master::Start))?;
    // let segment_offset = output.position();

    // output.write_tag(&MatroskaTag::Info(Master::Collected(vec![
    //     MatroskaTag::TimestampScale(1_000_000),
    //     MatroskaTag::Duration(item.media.as_ref().unwrap().duration * 1000.0),
    //     MatroskaTag::Title(item.title.clone().unwrap_or_default()),
    //     MatroskaTag::MuxingApp("jellyremux".to_string()),
    //     MatroskaTag::WritingApp("jellything".to_string()),
    // ])))?;

    // let tracks_header = inputs
    //     .iter_mut()
    //     .map(|rc| ebml_track_entry(rc.mapped, rc.mapped, &rc.info, rc.codec_private.take()))
    //     .collect();
    // output.write_tag(&MatroskaTag::Tracks(Master::Collected(tracks_header)))?;

    // let mut segment_layout: Vec<ClusterLayout> = {
    //     let mut cluster_pts = 0;
    //     let mut clusters = vec![];
    //     let mut cluster = vec![];
    //     let mut source_offsets = vec![None; inputs.len()];
    //     let mut gp = 0usize; // cluster position (in the segment)
    //     let mut p = 0usize; // block position (in the cluster)
    //     loop {
    //         let (track, block) = {
    //             let mut best_block = BlockIndex {
    //                 pts: u64::MAX,
    //                 size: 0,
    //                 source_off: 0,
    //             };
    //             let mut best_track = 0;
    //             for (i, r) in inputs.iter().enumerate() {
    //                 if let Some(v) = r.index.blocks.get(r.layouting_progress_index) {
    //                     if v.pts < best_block.pts {
    //                         best_block = v.to_owned();
    //                         best_track = i;
    //                     }
    //                 };
    //             }
    //             (best_track, best_block)
    //         };
    //         inputs[track].layouting_progress_index += 1;
    //         source_offsets[track].get_or_insert(block.source_off);
    //         if block.pts > cluster_pts + 1_000 {
    //             let cluster_content_size = 1 + 1 // timestamp {tag, size}
    //                  + bad_vint_length(cluster_pts) // timestamp tag value
    //                  + p;
    //             let cluster_size = 4 // tag length
    //                   + vint_length(cluster_content_size as u64) // size varint
    //                   + cluster_content_size;
    //             clusters.push(ClusterLayout {
    //                 position: gp, // relative to the first cluster
    //                 timestamp: cluster_pts,
    //                 source_offsets,
    //                 blocks: std::mem::take(&mut cluster),
    //             });

    //             cluster_pts = block.pts;
    //             source_offsets = vec![None; inputs.len()];
    //             gp += cluster_size;
    //             p = 0;
    //         }
    //         if block.pts == u64::MAX {
    //             break;
    //         }

    //         let simpleblock_size = 1 + 2 + 1 // block {tracknum, pts_off, flags}
    //                                         // TODO does not work, if more than 127 tracks are present
    //                                         + block.size; // block payload
    //         p += 1; // simpleblock tag
    //         p += vint_length(simpleblock_size as u64); // simpleblock size vint
    //         p += simpleblock_size;

    //         cluster.push((track, block))
    //     }
    //     info!("segment layout computed ({} clusters)", clusters.len());
    //     clusters
    // };
    // info!(
    //     "(perf) compute segment layout: {:?}",
    //     Instant::now() - timing_cp
    // );
    // let timing_cp = Instant::now();

    // let max_cue_size = 4 // cues id
    //         + 8 // cues len
    //         + ( // cues content
    //             1 // cp id
    //             + 1 // cp len
    //             + ( // cp content
    //                 1 // ctime id,
    //                 + 1 // ctime len
    //                 + 8 // ctime content uint
    //                 + ( // ctps
    //                     1 // ctp id
    //                     + 8 // ctp len
    //                     + (// ctp content
    //                         1 // ctrack id
    //                         + 1 // ctrack size
    //                         + 1 // ctrack content int
    //                             // TODO this breaks if inputs.len() >= 127
    //                         + 1 // ccp id
    //                         + 1 // ccp len
    //                         + 8 // ccp content offset
    //                     )
    //                 )
    //             ) * inputs.len()
    //         ) * segment_layout.len()
    //         + 1 // void id
    //         + 8; // void len

    // let first_cluster_offset_predict = max_cue_size + output.position();

    // // make the cluster position relative to the segment start as they should
    // segment_layout
    //     .iter_mut()
    //     .for_each(|e| e.position += first_cluster_offset_predict - segment_offset);

    // output.write_tag(&MatroskaTag::Cues(Master::Collected(
    //     segment_layout
    //         .iter()
    //         .map(|cluster| {
    //             MatroskaTag::CuePoint(Master::Collected(
    //                 Some(MatroskaTag::CueTime(cluster.timestamp))
    //                     .into_iter()
    //                     // TODO: Subtitles should not have cues for every cluster
    //                     .chain(inputs.iter().map(|i| {
    //                         MatroskaTag::CueTrackPositions(Master::Collected(vec![
    //                             MatroskaTag::CueTrack(i.mapped),
    //                             MatroskaTag::CueClusterPosition(cluster.position as u64),
    //                         ]))
    //                     }))
    //                     .collect(),
    //             ))
    //         })
    //         .collect(),
    // )))?;
    // output.write_padding(first_cluster_offset_predict)?;
    // let first_cluster_offset = output.position();
    // assert_eq!(first_cluster_offset, first_cluster_offset_predict);

    // let mut skip = 0;
    // // TODO binary search
    // for (i, cluster) in segment_layout.iter().enumerate() {
    //     if (cluster.position + segment_offset) >= range.start {
    //         break;
    //     }
    //     skip = i;
    // }

    // if skip != 0 {
    //     info!("skipping {skip} clusters");
    //     output.seek(SeekFrom::Start(
    //         (segment_layout[skip].position + segment_offset) as u64,
    //     ))?;
    // }

    // struct ReaderD<'a> {
    //     stream: SegmentExtractIter<'a>,
    //     mapped: u64,
    // }

    // let mut track_readers = inputs
    //     .iter_mut()
    //     .enumerate()
    //     .map(|(i, inp)| {
    //         inp.reader
    //             .seek(
    //                 // the seek target might be a hole; we continue until the next cluster of that track.
    //                 // this should be fine since tracks are only read according to segment_layout
    //                 find_first_cluster_with_off(&segment_layout, skip, i)
    //                     .ok_or(anyhow!("cluster hole at eof"))?,
    //                 MatroskaTag::Cluster(Master::Start), // TODO shouldn't this be a child of cluster?
    //             )
    //             .context("seeking in input")?;
    //         let stream = SegmentExtractIter::new(&mut inp.reader, inp.source_track_index as u64);

    //         Ok(ReaderD {
    //             mapped: inp.mapped,
    //             stream,
    //         })
    //     })
    //     .collect::<anyhow::Result<Vec<_>>>()?;

    // info!("(perf) seek inputs: {:?}", Instant::now() - timing_cp);

    // for (cluster_index, cluster) in segment_layout.into_iter().enumerate().skip(skip) {
    //     debug!(
    //         "writing cluster {cluster_index} (pts_base={}) with {} blocks",
    //         cluster.timestamp,
    //         cluster.blocks.len()
    //     );
    //     {
    //         let cue_error = cluster.position as i64 - (output.position() - segment_offset) as i64;
    //         if cue_error != 0 {
    //             warn!("calculation was {} bytes off", cue_error);
    //         }
    //     }

    //     let mut cluster_blocks = vec![MatroskaTag::Timestamp(cluster.timestamp)];
    //     for (block_track, index_block) in cluster.blocks {
    //         let track_reader = &mut track_readers[block_track];
    //         // TODO handle duration
    //         let mut block = track_reader.stream.next_block()?.0;

    //         assert_eq!(index_block.size, block.data.len(), "seek index is wrong");

    //         block.track = track_reader.mapped;
    //         block.timestamp_off = (index_block.pts - cluster.timestamp).try_into().unwrap();
    //         trace!("n={} tso={}", block.track, block.timestamp_off);

    //         cluster_blocks.push(MatroskaTag::SimpleBlock(block))
    //     }
    //     output.write_tag(&MatroskaTag::Cluster(Master::Collected(cluster_blocks)))?;
    // }
    // // output.write_tag(&MatroskaTag::Segment(Master::End))?;
    // Ok(())
    todo!()
}

// fn find_first_cluster_with_off(
//     segment_layout: &[ClusterLayout],
//     skip: usize,
//     track: usize,
// ) -> Option<u64> {
//     for cluster in segment_layout.iter().skip(skip) {
//         if let Some(off) = cluster.source_offsets[track] {
//             return Some(off);
//         }
//     }
//     None
// }