Skip to content

Commit bfa2002

Browse files
committed
video/image_writer: tag bits_per_raw_sample when pixfmt changes
This populates the AVCodecContext->bits_per_raw_sample field value when the pixel format of the screenshot being written doesn't match the pixel format of the original video. For example, if the original video was 10-bit and the screenshot is 16-bit, it populates this field so the encoder can make use of it. Currently libjxl and png encoders are known to accept this tag and set the corresponding metadata (via BitDepthHeader, or sBIT). This feature is only enabled if screenshot-sw=yes is enabled, because screenshots from a hardware buffer are always natively at max-depth. Signed-off-by: Leo Izen <[email protected]>
1 parent 0757185 commit bfa2002

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

video/image_writer.c

+16
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,22 @@ static bool write_lavc(struct image_writer_ctx *ctx, mp_image_t *image, FILE *fp
179179
avctx->width = image->w;
180180
avctx->height = image->h;
181181
avctx->pix_fmt = imgfmt2pixfmt(image->imgfmt);
182+
183+
/*
184+
* tagging avctx->bits_per_raw_sample indicates the number of significant
185+
* bits. For example, if the original video was 10-bit, and the GPU buffer is
186+
* 16-bit, this tells lavc that only 10 bits are significant. lavc encoders may
187+
* ignore this value, but some codecs can make use of it (for example, PNG's
188+
* sBIT chunk or JXL's bit depth header)
189+
*/
190+
if (memcmp(image->fmt.bpp, ctx->original_format.bpp, sizeof(image->fmt.bpp))) {
191+
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(imgfmt2pixfmt(ctx->original_format.id));
192+
int depth = 0;
193+
for (int i = 0; i < desc->nb_components; i++)
194+
depth = MPMAX(depth, desc->comp[i].depth);
195+
avctx->bits_per_raw_sample = depth;
196+
}
197+
182198
if (codec->id == AV_CODEC_ID_MJPEG) {
183199
// Annoying deprecated garbage for the jpg encoder.
184200
if (image->params.repr.levels == PL_COLOR_LEVELS_FULL)

0 commit comments

Comments
 (0)