diff options
Diffstat (limited to 'third_party/jpeg-xl/lib/jxl/encode.cc')
-rw-r--r-- | third_party/jpeg-xl/lib/jxl/encode.cc | 123 |
1 files changed, 69 insertions, 54 deletions
diff --git a/third_party/jpeg-xl/lib/jxl/encode.cc b/third_party/jpeg-xl/lib/jxl/encode.cc index 76f2148d62..4dbbeba4e7 100644 --- a/third_party/jpeg-xl/lib/jxl/encode.cc +++ b/third_party/jpeg-xl/lib/jxl/encode.cc @@ -595,21 +595,21 @@ JxlEncoderStatus VerifyInputBitDepth(JxlBitDepth bit_depth, return JxlErrorOrStatus::Success(); } -static inline bool EncodeVarInt(uint64_t value, size_t output_size, - size_t* output_pos, uint8_t* output) { +inline bool EncodeVarInt(uint64_t value, size_t output_size, size_t* output_pos, + uint8_t* output) { // While more than 7 bits of data are left, // store 7 bits and set the next byte flag while (value > 127) { // TODO(eustas): should it be `>=` ? if (*output_pos > output_size) return false; // |128: Set the next byte flag - output[(*output_pos)++] = ((uint8_t)(value & 127)) | 128; + output[(*output_pos)++] = (static_cast<uint8_t>(value & 127)) | 128; // Remove the seven bits we just wrote value >>= 7; } // TODO(eustas): should it be `>=` ? if (*output_pos > output_size) return false; - output[(*output_pos)++] = ((uint8_t)value) & 127; + output[(*output_pos)++] = static_cast<uint8_t>(value & 127); return true; } @@ -854,7 +854,7 @@ jxl::Status JxlEncoderStruct::ProcessOneEnqueuedInput() { timecode = 0; } - const bool last_frame = frames_closed && !num_queued_frames; + const bool last_frame = frames_closed && (num_queued_frames == 0); uint32_t max_bits_per_sample = metadata.m.bit_depth.bits_per_sample; for (const auto& info : metadata.m.extra_channel_info) { @@ -901,7 +901,7 @@ jxl::Status JxlEncoderStruct::ProcessOneEnqueuedInput() { return JXL_API_ERROR( this, JXL_ENC_ERR_API_USAGE, "Cannot use save_as_reference values >=3 (found: %d)", - (int)save_as_reference); + static_cast<int>(save_as_reference)); } jxl::FrameInfo frame_info; @@ -909,8 +909,8 @@ jxl::Status JxlEncoderStruct::ProcessOneEnqueuedInput() { frame_info.save_as_reference = save_as_reference; frame_info.source = input_frame->option_values.header.layer_info.blend_info.source; - frame_info.clamp = - input_frame->option_values.header.layer_info.blend_info.clamp; + frame_info.clamp = FROM_JXL_BOOL( + input_frame->option_values.header.layer_info.blend_info.clamp); frame_info.alpha_channel = input_frame->option_values.header.layer_info.blend_info.alpha; frame_info.extra_channel_blending_info.resize( @@ -989,7 +989,7 @@ jxl::Status JxlEncoderStruct::ProcessOneEnqueuedInput() { #endif jxl::WriteBoxHeader(jxl::MakeBoxType("jxlc"), frame_codestream_size, /*unbounded=*/false, use_large_box, - &box_header[0]); + box_header.data()); JXL_ASSERT(n == box_header_size); } else { #if JXL_ENABLE_ASSERT @@ -997,7 +997,7 @@ jxl::Status JxlEncoderStruct::ProcessOneEnqueuedInput() { #endif jxl::WriteBoxHeader( jxl::MakeBoxType("jxlp"), frame_codestream_size + 4, - /*unbounded=*/false, use_large_box, &box_header[0]); + /*unbounded=*/false, use_large_box, box_header.data()); JXL_ASSERT(n == box_header_size - 4); WriteJxlpBoxCounter(jxlp_counter++, last_frame, &box_header[box_header_size - 4]); @@ -1061,15 +1061,17 @@ JxlEncoderStatus JxlEncoderSetColorEncoding(JxlEncoder* enc, } if (enc->metadata.m.color_encoding.GetColorSpace() == jxl::ColorSpace::kGray) { - if (enc->basic_info.num_color_channels != 1) + if (enc->basic_info.num_color_channels != 1) { return JXL_API_ERROR( enc, JXL_ENC_ERR_API_USAGE, "Cannot use grayscale color encoding with num_color_channels != 1"); + } } else { - if (enc->basic_info.num_color_channels != 3) + if (enc->basic_info.num_color_channels != 3) { return JXL_API_ERROR( enc, JXL_ENC_ERR_API_USAGE, "Cannot use RGB color encoding with num_color_channels != 3"); + } } enc->color_encoding_set = true; if (!enc->intensity_target_set) { @@ -1103,15 +1105,17 @@ JxlEncoderStatus JxlEncoderSetICCProfile(JxlEncoder* enc, } if (enc->metadata.m.color_encoding.GetColorSpace() == jxl::ColorSpace::kGray) { - if (enc->basic_info.num_color_channels != 1) + if (enc->basic_info.num_color_channels != 1) { return JXL_API_ERROR( enc, JXL_ENC_ERR_BAD_INPUT, "Cannot use grayscale ICC profile with num_color_channels != 1"); + } } else { - if (enc->basic_info.num_color_channels != 3) + if (enc->basic_info.num_color_channels != 3) { return JXL_API_ERROR( enc, JXL_ENC_ERR_BAD_INPUT, "Cannot use RGB ICC profile with num_color_channels != 3"); + } // TODO(jon): also check that a kBlack extra channel is provided in the CMYK // case } @@ -1210,7 +1214,8 @@ JxlEncoderStatus JxlEncoderSetBasicInfo(JxlEncoder* enc, enc->metadata.m.bit_depth.floating_point_sample = (info->exponent_bits_per_sample != 0u); enc->metadata.m.modular_16_bit_buffer_sufficient = - (!info->uses_original_profile || info->bits_per_sample <= 12) && + (!FROM_JXL_BOOL(info->uses_original_profile) || + info->bits_per_sample <= 12) && info->alpha_bits <= 12; if ((info->intrinsic_xsize > 0 || info->intrinsic_ysize > 0) && (info->intrinsic_xsize != info->xsize || @@ -1248,7 +1253,7 @@ JxlEncoderStatus JxlEncoderSetBasicInfo(JxlEncoder* enc, } } - enc->metadata.m.xyb_encoded = !info->uses_original_profile; + enc->metadata.m.xyb_encoded = !FROM_JXL_BOOL(info->uses_original_profile); if (info->orientation > 0 && info->orientation <= 8) { enc->metadata.m.orientation = info->orientation; } else { @@ -1271,12 +1276,12 @@ JxlEncoderStatus JxlEncoderSetBasicInfo(JxlEncoder* enc, } enc->metadata.m.tone_mapping.min_nits = info->min_nits; enc->metadata.m.tone_mapping.relative_to_max_display = - info->relative_to_max_display; + FROM_JXL_BOOL(info->relative_to_max_display); enc->metadata.m.tone_mapping.linear_below = info->linear_below; enc->basic_info = *info; enc->basic_info_set = true; - enc->metadata.m.have_animation = info->have_animation; + enc->metadata.m.have_animation = FROM_JXL_BOOL(info->have_animation); if (info->have_animation) { if (info->animation.tps_denominator < 1) { return JXL_API_ERROR( @@ -1290,7 +1295,8 @@ JxlEncoderStatus JxlEncoderSetBasicInfo(JxlEncoder* enc, enc->metadata.m.animation.tps_numerator = info->animation.tps_numerator; enc->metadata.m.animation.tps_denominator = info->animation.tps_denominator; enc->metadata.m.animation.num_loops = info->animation.num_loops; - enc->metadata.m.animation.have_timecodes = info->animation.have_timecodes; + enc->metadata.m.animation.have_timecodes = + FROM_JXL_BOOL(info->animation.have_timecodes); } std::string level_message; int required_level = VerifyLevelSettings(enc, &level_message); @@ -1326,14 +1332,16 @@ JXL_EXPORT JxlEncoderStatus JxlEncoderSetUpsamplingMode(JxlEncoder* enc, const int64_t mode) { // for convenience, allow calling this with factor 1 and just make it a no-op if (factor == 1) return JxlErrorOrStatus::Success(); - if (factor != 2 && factor != 4 && factor != 8) + if (factor != 2 && factor != 4 && factor != 8) { return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE, "Invalid upsampling factor"); + } if (mode < -1) return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE, "Invalid upsampling mode"); - if (mode > 1) + if (mode > 1) { return JXL_API_ERROR(enc, JXL_ENC_ERR_NOT_SUPPORTED, "Unsupported upsampling mode"); + } const size_t count = (factor == 2 ? 15 : (factor == 4 ? 55 : 210)); auto& td = enc->metadata.transform_data; @@ -1436,7 +1444,7 @@ JxlEncoderFrameSettings* JxlEncoderFrameSettingsCreate( } opts->values.cparams.level = enc->codestream_level; opts->values.cparams.ec_distance.resize(enc->metadata.m.num_extra_channels, - -1); + 0); JxlEncoderFrameSettings* ret = opts.get(); enc->encoder_options.emplace_back(std::move(opts)); @@ -1451,7 +1459,7 @@ JxlEncoderStatus JxlEncoderSetFrameLossless( frame_settings->enc, JXL_ENC_ERR_API_USAGE, "Set uses_original_profile=true for lossless encoding"); } - frame_settings->values.lossless = lossless; + frame_settings->values.lossless = FROM_JXL_BOOL(lossless); return JxlErrorOrStatus::Success(); } @@ -1489,7 +1497,7 @@ JxlEncoderStatus JxlEncoderSetExtraChannelDistance( // This can only happen if JxlEncoderFrameSettingsCreate() was called before // JxlEncoderSetBasicInfo(). frame_settings->values.cparams.ec_distance.resize( - frame_settings->enc->metadata.m.num_extra_channels, -1); + frame_settings->enc->metadata.m.num_extra_channels, 0); } frame_settings->values.cparams.ec_distance[index] = distance; @@ -1537,14 +1545,14 @@ JxlEncoderStatus JxlEncoderFrameSettingsSetOption( switch (option) { case JXL_ENC_FRAME_SETTING_EFFORT: if (frame_settings->enc->allow_expert_options) { - if (value < 1 || value > 10) { + if (value < 1 || value > 11) { return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED, - "Encode effort has to be in [1..10]"); + "Encode effort has to be in [1..11]"); } } else { - if (value < 1 || value > 9) { + if (value < 1 || value > 10) { return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED, - "Encode effort has to be in [1..9]"); + "Encode effort has to be in [1..10]"); } } frame_settings->values.cparams.speed_tier = @@ -1747,9 +1755,9 @@ JxlEncoderStatus JxlEncoderFrameSettingsSetOption( frame_settings->values.cparams.jpeg_compress_boxes = value; break; case JXL_ENC_FRAME_SETTING_BUFFERING: - if (value < 0 || value > 3) { + if (value < -1 || value > 3) { return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_NOT_SUPPORTED, - "Buffering has to be in [0..3]"); + "Buffering has to be in [-1..3]"); } frame_settings->values.cparams.buffering = value; break; @@ -1934,7 +1942,7 @@ JxlEncoderStatus JxlEncoderUseContainer(JxlEncoder* enc, return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE, "this setting can only be set at the beginning"); } - enc->use_container = static_cast<bool>(use_container); + enc->use_container = FROM_JXL_BOOL(use_container); return JxlErrorOrStatus::Success(); } @@ -1944,7 +1952,7 @@ JxlEncoderStatus JxlEncoderStoreJPEGMetadata(JxlEncoder* enc, return JXL_API_ERROR(enc, JXL_ENC_ERR_API_USAGE, "this setting can only be set at the beginning"); } - enc->store_jpeg_metadata = static_cast<bool>(store_jpeg_metadata); + enc->store_jpeg_metadata = FROM_JXL_BOOL(store_jpeg_metadata); return JxlErrorOrStatus::Success(); } @@ -2034,7 +2042,7 @@ JxlEncoderStatus JxlEncoderAddJPEGFrame( JxlEncoderInitBasicInfo(&basic_info); basic_info.xsize = io.Main().jpeg_data->width; basic_info.ysize = io.Main().jpeg_data->height; - basic_info.uses_original_profile = true; + basic_info.uses_original_profile = JXL_TRUE; if (JxlEncoderSetBasicInfo(frame_settings->enc, &basic_info) != JXL_ENC_SUCCESS) { return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC, @@ -2042,7 +2050,8 @@ JxlEncoderStatus JxlEncoderAddJPEGFrame( } } - size_t xsize, ysize; + size_t xsize; + size_t ysize; if (GetCurrentDimensions(frame_settings, xsize, ysize) != JXL_ENC_SUCCESS) { return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC, "bad dimensions"); @@ -2074,21 +2083,23 @@ JxlEncoderStatus JxlEncoderAddJPEGFrame( std::vector<uint8_t> exif(exif_size); memcpy(exif.data() + 4, io.blobs.exif.data(), io.blobs.exif.size()); JxlEncoderUseBoxes(frame_settings->enc); - JxlEncoderAddBox(frame_settings->enc, "Exif", exif.data(), exif_size, - frame_settings->values.cparams.jpeg_compress_boxes); + JxlEncoderAddBox( + frame_settings->enc, "Exif", exif.data(), exif_size, + TO_JXL_BOOL(frame_settings->values.cparams.jpeg_compress_boxes)); } if (!io.blobs.xmp.empty() && frame_settings->values.cparams.jpeg_keep_xmp) { JxlEncoderUseBoxes(frame_settings->enc); - JxlEncoderAddBox(frame_settings->enc, "xml ", io.blobs.xmp.data(), - io.blobs.xmp.size(), - frame_settings->values.cparams.jpeg_compress_boxes); + JxlEncoderAddBox( + frame_settings->enc, "xml ", io.blobs.xmp.data(), io.blobs.xmp.size(), + TO_JXL_BOOL(frame_settings->values.cparams.jpeg_compress_boxes)); } if (!io.blobs.jumbf.empty() && frame_settings->values.cparams.jpeg_keep_jumbf) { JxlEncoderUseBoxes(frame_settings->enc); - JxlEncoderAddBox(frame_settings->enc, "jumb", io.blobs.jumbf.data(), - io.blobs.jumbf.size(), - frame_settings->values.cparams.jpeg_compress_boxes); + JxlEncoderAddBox( + frame_settings->enc, "jumb", io.blobs.jumbf.data(), + io.blobs.jumbf.size(), + TO_JXL_BOOL(frame_settings->values.cparams.jpeg_compress_boxes)); } if (frame_settings->enc->store_jpeg_metadata) { if (!frame_settings->values.cparams.jpeg_keep_exif || @@ -2259,10 +2270,12 @@ JxlEncoderStatus JxlEncoderAddImageFrameInternal( pool, 0, count, jxl::ThreadPool::NoInit, [&](size_t i, size_t) { fun(opaque, i); }, "Encode fast lossless")); }; - auto frame_state = JxlFastLosslessPrepareFrame( + JXL_BOOL oneshot = TO_JXL_BOOL(!frame_data.StreamingInput()); + auto* frame_state = JxlFastLosslessPrepareFrame( frame_data.GetInputSource(), xsize, ysize, num_channels, - frame_settings->enc->metadata.m.bit_depth.bits_per_sample, big_endian, - /*effort=*/2, /*oneshot=*/!frame_data.StreamingInput()); + frame_settings->enc->metadata.m.bit_depth.bits_per_sample, + TO_JXL_BOOL(big_endian), + /*effort=*/2, oneshot); if (!streaming) { JxlFastLosslessProcessFrame(frame_state, /*is_last=*/false, frame_settings->enc->thread_pool.get(), @@ -2325,7 +2338,8 @@ JxlEncoderStatus JxlEncoderAddImageFrameInternal( JxlEncoderStatus JxlEncoderAddImageFrame( const JxlEncoderFrameSettings* frame_settings, const JxlPixelFormat* pixel_format, const void* buffer, size_t size) { - size_t xsize, ysize; + size_t xsize; + size_t ysize; if (GetCurrentDimensions(frame_settings, xsize, ysize) != JXL_ENC_SUCCESS) { return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC, "bad dimensions"); @@ -2345,7 +2359,8 @@ JxlEncoderStatus JxlEncoderAddImageFrame( JxlEncoderStatus JxlEncoderAddChunkedFrame( const JxlEncoderFrameSettings* frame_settings, JXL_BOOL is_last_frame, JxlChunkedFrameInputSource chunked_frame_input) { - size_t xsize, ysize; + size_t xsize; + size_t ysize; if (GetCurrentDimensions(frame_settings, xsize, ysize) != JXL_ENC_SUCCESS) { return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_GENERIC, "bad dimensions"); @@ -2417,7 +2432,7 @@ JxlEncoderStatus JxlEncoderAddBox(JxlEncoder* enc, const JxlBoxType type, box->type = jxl::MakeBoxType(type); box->contents.assign(contents, contents + size); - box->compress_box = !!compress_box; + box->compress_box = FROM_JXL_BOOL(compress_box); QueueBox(enc, box); return JxlErrorOrStatus::Success(); } @@ -2450,7 +2465,7 @@ JXL_EXPORT JxlEncoderStatus JxlEncoderSetExtraChannelBuffer( return JXL_API_ERROR_NOSET("Invalid input bit depth"); } const uint8_t* uint8_buffer = reinterpret_cast<const uint8_t*>(buffer); - auto queued_frame = frame_settings->enc->input_queue.back().frame.get(); + auto* queued_frame = frame_settings->enc->input_queue.back().frame.get(); if (!queued_frame->frame_data.SetFromBuffer(1 + index, uint8_buffer, size, ec_format)) { return JXL_API_ERROR(frame_settings->enc, JXL_ENC_ERR_API_USAGE, @@ -2588,12 +2603,14 @@ JxlEncoderStatus JxlEncoderSetFrameBitDepth( void JxlColorEncodingSetToSRGB(JxlColorEncoding* color_encoding, JXL_BOOL is_gray) { - *color_encoding = jxl::ColorEncoding::SRGB(is_gray).ToExternal(); + *color_encoding = + jxl::ColorEncoding::SRGB(FROM_JXL_BOOL(is_gray)).ToExternal(); } void JxlColorEncodingSetToLinearSRGB(JxlColorEncoding* color_encoding, JXL_BOOL is_gray) { - *color_encoding = jxl::ColorEncoding::LinearSRGB(is_gray).ToExternal(); + *color_encoding = + jxl::ColorEncoding::LinearSRGB(FROM_JXL_BOOL(is_gray)).ToExternal(); } void JxlEncoderAllowExpertOptions(JxlEncoder* enc) { @@ -2611,9 +2628,7 @@ JXL_EXPORT JxlEncoderStats* JxlEncoderStatsCreate() { return new JxlEncoderStats(); } -JXL_EXPORT void JxlEncoderStatsDestroy(JxlEncoderStats* stats) { - if (stats) delete stats; -} +JXL_EXPORT void JxlEncoderStatsDestroy(JxlEncoderStats* stats) { delete stats; } JXL_EXPORT void JxlEncoderCollectStats(JxlEncoderFrameSettings* frame_settings, JxlEncoderStats* stats) { |