Skip to content

Commit a9951a3

Browse files
committed
vcomp/jpegxs: add bitrate option
1 parent 6913850 commit a9951a3

1 file changed

Lines changed: 37 additions & 3 deletions

File tree

src/video_compress/jpegxs.cpp

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@
3535
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3636
*/
3737

38-
#include <string>
38+
#include <cassert> // for assert
3939
#include <cinttypes> // for PRIu32
40+
#include <climits> // for LLONG_MIN
4041
#include <condition_variable>
4142
#include <memory>
4243
#include <mutex>
44+
#include <string>
4345
#include <thread>
4446
#include <vector>
4547
#include <svt-jpegxs/SvtJpegxs.h> // for SvtJxsErrorType
@@ -89,6 +91,8 @@ struct state_video_compress_jpegxs {
8991
svt_jpeg_xs_frame_pool_t *frame_pool{};
9092
int pool_size = DEFAULT_POOL_SIZE;
9193

94+
long long req_bitrate = -1;
95+
9296
bool configured = 0;
9397
bool reconfiguring = 0;
9498
bool stop = 0;
@@ -278,6 +282,23 @@ ColourFormat subsampling_to_jpegxs(int ug_subs) {
278282
}
279283
}
280284

285+
static void
286+
set_bitrate(svt_jpeg_xs_encoder_api_t *encoder, long long req_bitrate,
287+
const struct video_desc *desc)
288+
{
289+
long long numerator = req_bitrate;
290+
long long denominator = desc->width * desc->height * desc->fps;
291+
// reduce numbers to fit uint32_t if num or den is larger
292+
while (numerator > UINT32_MAX || denominator > UINT32_MAX) {
293+
numerator /= 1024;
294+
denominator /= 1024;
295+
}
296+
assert(numerator > 0);
297+
assert(denominator > 0);
298+
encoder->bpp_numerator = numerator;
299+
encoder->bpp_denominator = denominator;
300+
}
301+
281302
static bool configure_with(struct state_video_compress_jpegxs *s, struct video_desc desc)
282303
{
283304
const struct uv_to_jpegxs_conversion *conv = get_uv_to_jpegxs_conversion(desc.color_spec);
@@ -307,6 +328,10 @@ static bool configure_with(struct state_video_compress_jpegxs *s, struct video_d
307328
return false;
308329
}
309330

331+
if (s->req_bitrate != -1) {
332+
set_bitrate(&s->encoder, s->req_bitrate, &desc);
333+
}
334+
310335
err = svt_jpeg_xs_encoder_init(SVT_JPEGXS_API_VER_MAJOR, SVT_JPEGXS_API_VER_MINOR, &s->encoder);
311336
if (err != SvtJxsErrorNone) {
312337
log_msg(LOG_LEVEL_ERROR, MOD_NAME "Failed to initialize JPEG XS encoder: %x\n", err);
@@ -342,7 +367,13 @@ bool state_video_compress_jpegxs::parse_fmt(char *fmt) {
342367
while ((tok = strtok_r(fmt, ":", &save_ptr)) != nullptr) {
343368
const char *const val = strchr(tok, '=') + 1;
344369
const int num = val != nullptr ? atoi(val) : -1;
345-
if (IS_KEY_PREFIX(tok, "bpp")) {
370+
if (IS_KEY_PREFIX(tok, "bitrate")) {
371+
req_bitrate = unit_evaluate(val, nullptr);
372+
if (req_bitrate == LLONG_MIN) {
373+
MSG(ERROR, "Invalid value for bitrate: %s\n", val);
374+
return false;
375+
}
376+
} else if (IS_KEY_PREFIX(tok, "bpp")) {
346377
int num = 0, den = 1;
347378
if (sscanf(val, "%d/%d", &num, &den) < 1 || num <= 0 || den <= 0) {
348379
MSG(ERROR, "Invalid bpp value '%s' (must be a positive integer or fraction, e.g., 2 or 3/4).\n", val);
@@ -423,6 +454,9 @@ static const struct {
423454
bool is_boolean;
424455
const char *placeholder;
425456
} usage_opts[] = {
457+
{"Bit rate", "bitrate", "bitrate", "\t\tbitrate to be used "
458+
"(eg. 50.5M)\n", ":bitrate=", false, "50.5M"
459+
},
426460
{"Bits per pixel", "bpp", "bpp",
427461
"\t\tTarget bits-per-pixel ratio for the encoder. May be given as an\n"
428462
"\t\tinteger (e.g., 2) or as a fraction (e.g., 3/4). Controls the\n"
@@ -485,7 +519,7 @@ static void *jpegxs_compress_init(struct module *parent, const char *opts) {
485519
if (opts && strcmp(opts, "help") == 0) {
486520
color_printf(TBOLD("JPEG XS") " compression usage:\n");
487521
color_printf("\t" TBOLD(
488-
TRED("-c jpegxs") "[:bpp=<ratio>][:decomp_v=<0-2>][:decomp_h=<1-5>]"
522+
TRED("-c jpegxs") "[:bitrate=<br>|:bpp=<ratio>][:decomp_v=<0-2>][:decomp_h=<1-5>]"
489523
"[:quantization=<0-1>][:slice_height=<n>][:rc=<mode>]"
490524
"[:threads=<num_threads>][:pool_size=<n>][:verbose=<n>]") "\n");
491525
color_printf("\t" TBOLD(TRED("-c jpegxs") ":help") "\n");

0 commit comments

Comments
 (0)