j2k-jpeg 0.7.2

JPEG inspect/decode and fallback encode support for j2k
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// SPDX-License-Identifier: MIT OR Apache-2.0

use alloc::vec::Vec;
use core::mem::size_of;

use j2k_core::{try_host_vec_with_capacity, BatchInfrastructureError, HostAllocationError};

use crate::decoder::DEFAULT_MAX_DECODE_BYTES;
use crate::error::JpegError;

/// Shared allowance for every batch-owned vector and deep warning owner.
pub(super) const JPEG_BATCH_METADATA_ALLOWANCE_BYTES: usize = 64 * 1024 * 1024;

/// Authoritative codec-domain allowance used by one planning decoder or by
/// all concurrently active worker claims in aggregate.
pub(super) const JPEG_CODEC_HOST_CAP_BYTES: usize = DEFAULT_MAX_DECODE_BYTES;

/// Maximum checked JPEG batch live set: one codec domain plus metadata.
const _: [(); 1] =
    [(); (JPEG_CODEC_HOST_CAP_BYTES <= usize::MAX - JPEG_BATCH_METADATA_ALLOWANCE_BYTES) as usize];
pub(super) const JPEG_BATCH_HOST_CAP_BYTES: usize =
    JPEG_CODEC_HOST_CAP_BYTES + JPEG_BATCH_METADATA_ALLOWANCE_BYTES;

#[derive(Debug, Clone)]
pub(super) enum PlannedJob {
    Decode {
        worker_live_bytes: usize,
        retained_result_bytes: usize,
    },
    Reject(JpegError),
}

impl PlannedJob {
    pub(super) const fn live_bytes(&self) -> usize {
        match self {
            Self::Decode {
                worker_live_bytes, ..
            } => *worker_live_bytes,
            Self::Reject(_) => 0,
        }
    }

    pub(super) const fn retained_result_bytes(&self) -> usize {
        match self {
            Self::Decode {
                retained_result_bytes,
                ..
            } => *retained_result_bytes,
            Self::Reject(_) => 0,
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub(super) struct BatchMetadataLayout {
    pub(super) fixed_bytes: usize,
    pub(super) worker_slot_capacity: usize,
    pub(super) worker_slot_bytes: usize,
    pub(super) worker_result_bytes: usize,
    pub(super) ordered_result_bytes: usize,
    pub(super) handle_bytes: usize,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) struct BatchPlan {
    pub(super) worker_count: usize,
    pub(super) chunk_size: usize,
    pub(super) live_bytes: usize,
    pub(super) metadata_bytes: usize,
    pub(super) codec_bytes: usize,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct PlannedLiveBytes {
    metadata: usize,
    codec: usize,
    aggregate: usize,
}

#[derive(Clone, Copy, Debug)]
struct BatchPlanLimits {
    metadata: usize,
    codec: usize,
    aggregate: usize,
}

pub(super) fn select_batch_plan(
    jobs: &[PlannedJob],
    desired_workers: usize,
    metadata: BatchMetadataLayout,
    retained_worker_bytes: impl Fn(usize) -> usize,
) -> Result<BatchPlan, BatchInfrastructureError> {
    select_batch_plan_with_limits(
        jobs,
        desired_workers,
        metadata,
        retained_worker_bytes,
        BatchPlanLimits {
            metadata: JPEG_BATCH_METADATA_ALLOWANCE_BYTES,
            codec: JPEG_CODEC_HOST_CAP_BYTES,
            aggregate: JPEG_BATCH_HOST_CAP_BYTES,
        },
    )
}

fn select_batch_plan_with_limits(
    jobs: &[PlannedJob],
    desired_workers: usize,
    metadata: BatchMetadataLayout,
    retained_worker_bytes: impl Fn(usize) -> usize,
    limits: BatchPlanLimits,
) -> Result<BatchPlan, BatchInfrastructureError> {
    if jobs.is_empty() {
        return Err(BatchInfrastructureError::EmptyBatchPlan);
    }
    let desired_workers = desired_workers.max(1).min(jobs.len());
    let mut final_rejection = BatchInfrastructureError::AllocationTooLarge {
        what: "JPEG batch live set",
        requested: usize::MAX,
        cap: limits.aggregate,
    };

    for requested_workers in (1..=desired_workers).rev() {
        let chunk_size = jobs.len().div_ceil(requested_workers);
        let worker_count = jobs.len().div_ceil(chunk_size);
        let live = match planned_live_bytes(
            jobs,
            worker_count,
            chunk_size,
            metadata,
            &retained_worker_bytes,
            limits,
        ) {
            Ok(live) => live,
            Err(error @ BatchInfrastructureError::AllocationTooLarge { .. }) => {
                final_rejection = error;
                continue;
            }
            Err(error) => return Err(error),
        };
        return Ok(BatchPlan {
            worker_count,
            chunk_size,
            live_bytes: live.aggregate,
            metadata_bytes: live.metadata,
            codec_bytes: live.codec,
        });
    }

    Err(final_rejection)
}

fn planned_live_bytes(
    jobs: &[PlannedJob],
    worker_count: usize,
    chunk_size: usize,
    metadata: BatchMetadataLayout,
    retained_worker_bytes: &impl Fn(usize) -> usize,
    limits: BatchPlanLimits,
) -> Result<PlannedLiveBytes, BatchInfrastructureError> {
    let mut metadata_bytes = metadata.fixed_bytes;
    for job in jobs {
        metadata_bytes = checked_add(
            metadata_bytes,
            job.retained_result_bytes(),
            "JPEG retained batch outcomes",
            limits.metadata,
        )?;
    }
    metadata_bytes = checked_add(
        metadata_bytes,
        checked_mul(
            metadata.worker_slot_capacity.max(worker_count),
            metadata.worker_slot_bytes,
            "JPEG batch worker slots",
            limits.metadata,
        )?,
        "JPEG batch metadata",
        limits.metadata,
    )?;
    metadata_bytes = checked_add(
        metadata_bytes,
        checked_mul(
            jobs.len(),
            metadata.worker_result_bytes,
            "JPEG ordered worker result slots",
            limits.metadata,
        )?,
        "JPEG batch metadata",
        limits.metadata,
    )?;
    metadata_bytes = checked_add(
        metadata_bytes,
        checked_mul(
            jobs.len(),
            metadata.ordered_result_bytes,
            "JPEG ordered batch results",
            limits.metadata,
        )?,
        "JPEG batch metadata",
        limits.metadata,
    )?;
    metadata_bytes = checked_add(
        metadata_bytes,
        checked_mul(
            worker_count,
            metadata.handle_bytes,
            "JPEG scoped worker handles",
            limits.metadata,
        )?,
        "JPEG batch metadata",
        limits.metadata,
    )?;
    ensure_within(metadata_bytes, limits.metadata, "JPEG batch metadata")?;

    let mut codec_bytes = 0usize;
    for (worker_index, chunk) in jobs.chunks(chunk_size).enumerate() {
        if worker_index >= worker_count {
            return Err(BatchInfrastructureError::WorkerSlotMissing {
                worker: worker_index,
                available: worker_count,
            });
        }
        let operation_bytes = chunk.iter().map(PlannedJob::live_bytes).max().unwrap_or(0);
        // A stale pool can remain live while the next decoder constructs its
        // prepared metadata. Count both rather than assuming the operation
        // claim replaces every retained owner; `prepare_batch` may explicitly
        // release stale slots and retry when this conservative sum does not fit.
        let worker_bytes = checked_add(
            operation_bytes,
            retained_worker_bytes(worker_index),
            "JPEG retained worker live set",
            limits.codec,
        )?;
        codec_bytes = checked_add(
            codec_bytes,
            worker_bytes,
            "JPEG batch codec claims",
            limits.codec,
        )?;
    }
    ensure_within(codec_bytes, limits.codec, "JPEG batch codec claims")?;
    let aggregate = checked_add(
        metadata_bytes,
        codec_bytes,
        "JPEG batch live set",
        limits.aggregate,
    )?;
    ensure_within(aggregate, limits.aggregate, "JPEG batch live set")?;
    Ok(PlannedLiveBytes {
        metadata: metadata_bytes,
        codec: codec_bytes,
        aggregate,
    })
}

pub(super) fn try_vec_with_capacity<T>(
    capacity: usize,
    what: &'static str,
) -> Result<Vec<T>, BatchInfrastructureError> {
    try_vec_with_retained_metadata(capacity, 0, what)
}

pub(super) fn try_vec_with_retained_metadata<T>(
    capacity: usize,
    retained_metadata_bytes: usize,
    what: &'static str,
) -> Result<Vec<T>, BatchInfrastructureError> {
    let requested = checked_mul(
        capacity,
        size_of::<T>(),
        what,
        JPEG_BATCH_METADATA_ALLOWANCE_BYTES,
    )?;
    ensure_metadata_bytes(retained_metadata_bytes, requested, what)?;
    let values =
        try_host_vec_with_capacity(capacity).map_err(|error| host_allocation_error(what, error))?;
    let actual = checked_mul(
        values.capacity(),
        size_of::<T>(),
        what,
        JPEG_BATCH_METADATA_ALLOWANCE_BYTES,
    )?;
    ensure_metadata_bytes(retained_metadata_bytes, actual, what)?;
    Ok(values)
}

pub(super) fn ensure_metadata_bytes(
    retained: usize,
    additional: usize,
    what: &'static str,
) -> Result<usize, BatchInfrastructureError> {
    let requested = checked_add(
        retained,
        additional,
        what,
        JPEG_BATCH_METADATA_ALLOWANCE_BYTES,
    )?;
    if requested > JPEG_BATCH_METADATA_ALLOWANCE_BYTES {
        return Err(BatchInfrastructureError::AllocationTooLarge {
            what,
            requested,
            cap: JPEG_BATCH_METADATA_ALLOWANCE_BYTES,
        });
    }
    Ok(requested)
}

pub(super) fn ensure_live_domains(
    codec_bytes: usize,
    metadata_bytes: usize,
    what: &'static str,
) -> Result<usize, BatchInfrastructureError> {
    if codec_bytes > JPEG_CODEC_HOST_CAP_BYTES {
        return Err(BatchInfrastructureError::AllocationTooLarge {
            what: "JPEG batch codec claims",
            requested: codec_bytes,
            cap: JPEG_CODEC_HOST_CAP_BYTES,
        });
    }
    if metadata_bytes > JPEG_BATCH_METADATA_ALLOWANCE_BYTES {
        return Err(BatchInfrastructureError::AllocationTooLarge {
            what: "JPEG batch metadata",
            requested: metadata_bytes,
            cap: JPEG_BATCH_METADATA_ALLOWANCE_BYTES,
        });
    }
    let requested = checked_add(codec_bytes, metadata_bytes, what, JPEG_BATCH_HOST_CAP_BYTES)?;
    if requested > JPEG_BATCH_HOST_CAP_BYTES {
        return Err(BatchInfrastructureError::AllocationTooLarge {
            what,
            requested,
            cap: JPEG_BATCH_HOST_CAP_BYTES,
        });
    }
    Ok(requested)
}

pub(super) fn ensure_planning_phase(
    metadata_bytes: usize,
) -> Result<usize, BatchInfrastructureError> {
    ensure_live_domains(
        JPEG_CODEC_HOST_CAP_BYTES,
        metadata_bytes,
        "JPEG batch planning phase",
    )
}

pub(super) fn vec_capacity_bytes<T>(values: &Vec<T>) -> Result<usize, BatchInfrastructureError> {
    checked_mul(
        values.capacity(),
        size_of::<T>(),
        "JPEG batch vector",
        JPEG_BATCH_METADATA_ALLOWANCE_BYTES,
    )
}

fn ensure_within(
    requested: usize,
    cap: usize,
    what: &'static str,
) -> Result<(), BatchInfrastructureError> {
    if requested > cap {
        return Err(BatchInfrastructureError::AllocationTooLarge {
            what,
            requested,
            cap,
        });
    }
    Ok(())
}

fn checked_add(
    left: usize,
    right: usize,
    what: &'static str,
    cap: usize,
) -> Result<usize, BatchInfrastructureError> {
    left.checked_add(right)
        .ok_or(BatchInfrastructureError::AllocationTooLarge {
            what,
            requested: usize::MAX,
            cap,
        })
}

fn checked_mul(
    left: usize,
    right: usize,
    what: &'static str,
    cap: usize,
) -> Result<usize, BatchInfrastructureError> {
    left.checked_mul(right)
        .ok_or(BatchInfrastructureError::AllocationTooLarge {
            what,
            requested: usize::MAX,
            cap,
        })
}

fn host_allocation_error(
    what: &'static str,
    error: HostAllocationError,
) -> BatchInfrastructureError {
    BatchInfrastructureError::HostAllocationFailed {
        what,
        bytes: error.requested_bytes(),
    }
}

#[cfg(test)]
mod tests;