A method comprising encoding, in a base layer encoder, several versions from an input image block, the encoding of the several versions being adjusted by different values of at least one coding parameter; reconstructing, per each of the several versions, a reconstructed image block from the encoded image block; deriving a first distortion between the reconstructed image block and the input image block; inputting the reconstructed image block into a first neural network arranged to perform at least one of the following tasks: object detection, scene classification, semantic segmentation, video event detection, anomaly detection, pedestrian tracking; characterized by obtaining an output distribution from an output of the first neural network; obtaining a second distortion on the basis of said output distribution of the first neural network; selecting values of the at least one coding parameter for the base layer encoder among the several versions by adjusting a function J= w1×D1 + w2×D2 + λ×K, where w1, w2 and λ are weights, D1 is the first distortion, D2 is the second distortion and R is a number of bits used for encoding the input image block, wherein w1 is adjusted to be smaller than w2; encoding the selected values of the at least one coding parameter for the base layer encoder into a bitstream; inputting at least the input image block and the reconstructed image block into an enhancement layer encoder; encoding, in an enhancement layer encoder, several versions from the input image block, the encoding of the several versions being adjusted by different values of at least one coding parameter; selecting values of the at least one coding parameter for the enhancement layer encoder among the several versions by adjusting said function, wherein the second distortion is discarded; and encoding the selected values of the at least one coding parameter for the enhancement layer encoder into the bitstream. encoding, in a base layer encoder, several versions from an input image block, the encoding of the several versions being adjusted by different values of at least one coding parameter; reconstructing, per each of the several versions, a reconstructed image block from the encoded image block; deriving a first distortion between the reconstructed image block and the input image block; inputting the reconstructed image block into a first neural network arranged to perform at least one of the following tasks: object detection, scene classification, semantic segmentation, video event detection, anomaly detection, pedestrian tracking; characterized by obtaining an output distribution from an output of the first neural network; obtaining a second distortion on the basis of said output distribution of the first neural network; selecting values of the at least one coding parameter for the base layer encoder among the several versions by adjusting a function J= w1×D1 + w2×D2 + λ×K, where w1, w2 and λ are weights, D1 is the first distortion, D2 is the second distortion and R is a number of bits used for encoding the input image block, wherein w1 is adjusted to be smaller than w2; encoding the selected values of the at least one coding parameter for the base layer encoder into a bitstream; inputting at least the input image block and the reconstructed image block into an enhancement layer encoder; encoding, in an enhancement layer encoder, several versions from the input image block, the encoding of the several versions being adjusted by different values of at least one coding parameter; selecting values of the at least one coding parameter for the enhancement layer encoder among the several versions by adjusting said function, wherein the second distortion is discarded; and encoding the selected values of the at least one coding parameter for the enhancement layer encoder into the bitstream.