History

player/screenshot: support more formats for screenshot-raw command

This adds a format parameter to screenshot-raw command which controls the format of the returned image data. In addition to the bgr0 format, this adds support for 3 other formats: bgra, rgba, rgba64. bgra is used by overlay-add command and several window systems. rgba and rgba64 are used by several GPU APIs and are the formats returned by the gpu and gpu-next screenshot code, which avoid the need for further format conversion (see convert_image function). rgba64 also adds the ability to acquire high bit depth screenshots for screenshot-raw command.

nanahi committed 1 year ago

Commit ab47e951 parent c1237c14 files · +42 −12Browse files

Unified · Split

4 files
  1. DOCS/interface-changes/screenshot-raw-format.txt +1 −0
  2. DOCS/man/input.rst +27 −10
  3. player/command.c +6 −0
  4. player/screenshot.c +8 −2
DOCS/interface-changes/screenshot-raw-format.txt+1−0
@@ -0,0 +1 @@
1+add `format` argument to `screenshot-raw` command
DOCS/man/input.rst+27−10
@@ -1395,21 +1395,38 @@ Screenshot Commands · 1394 unmodified lines
13951395 Like all input command parameters, the filename is subject to property
13961396 expansion as described in `Property Expansion`_.
13971397
1398−``screenshot-raw [<flags>]``
1398+``screenshot-raw [<flags> [<format>]]``
13991399 Return a screenshot in memory. This can be used only through the client
14001400 API. The MPV_FORMAT_NODE_MAP returned by this command has the ``w``, ``h``,
1401− ``stride`` fields set to obvious contents. The ``format`` field is set to
1402− ``bgr0`` by default. This format is organized as ``B8G8R8X8`` (where ``B``
1403− is the LSB). The contents of the padding ``X`` are undefined. The ``data``
1404− field is of type MPV_FORMAT_BYTE_ARRAY with the actual image data. The image
1405− is freed as soon as the result mpv_node is freed. As usual with client API
1406− semantics, you are not allowed to write to the image data.
1401+ ``stride`` fields set to obvious contents.
1402+
1403+ The ``format`` field is set to the format of the screenshot image data.
1404+ This can be controlled by the ``format`` argument. The format can be one of
1405+ the following:
1406+
1407+ bgr0 (default)
1408+ This format is organized as ``B8G8R8X8`` (where ``B`` is the LSB).
1409+ The contents of the padding ``X`` are undefined.
1410+ bgra
1411+ This format is organized as ``B8G8R8A8`` (where ``B`` is the LSB).
1412+ rgba
1413+ This format is organized as ``R8G8B8A8`` (where ``R`` is the LSB).
1414+ rgba64
1415+ This format is organized as ``R16G16B16A16`` (where ``R`` is the LSB).
1416+ Each component occupies 2 bytes per pixel.
1417+ When this format is used, the image data will be high bit depth, and
1418+ ``--screenshot-high-bit-depth`` is ignored.
1419+
1420+ The ``data`` field is of type MPV_FORMAT_BYTE_ARRAY with the actual image
1421+ data. The image is freed as soon as the result mpv_node is freed. As usual
1422+ with client API semantics, you are not allowed to write to the image data.
14071423
14081424 The ``stride`` is the number of bytes from a pixel at ``(x0, y0)`` to the
1409− pixel at ``(x0, y0 + 1)``. This can be larger than ``w * 4`` if the image
1425+ pixel at ``(x0, y0 + 1)``. This can be larger than ``w * bpp`` if the image
14101426 was cropped, or if there is padding. This number can be negative as well.
1411− You access a pixel with ``byte_index = y * stride + x * 4`` (assuming the
1412− ``bgr0`` format).
1427+ You access a pixel with ``byte_index = y * stride + x * bpp``.
1428+ Here, ``bpp`` is the number of bytes per pixel, which is 8 for ``rgba64``
1429+ format and 4 for other formats.
14131430
14141431 The ``flags`` argument is like the first argument to ``screenshot`` and
14151432 supports ``subtitles``, ``video``, ``window``.
player/command.c+6−0
@@ -7176,6 +7176,12 @@ const struct mp_cmd_def mp_cmds[] = { · 7175 unmodified lines
71767176 {"window", 1},
71777177 {"subtitles", 2}),
71787178 OPTDEF_INT(2)},
7179+ {"format", OPT_CHOICE(v.i,
7180+ {"bgr0", 0},
7181+ {"bgra", 1},
7182+ {"rgba", 2},
7183+ {"rgba64", 3}),
7184+ OPTDEF_INT(0)},
71797185 },
71807186 },
71817187 { "loadfile", cmd_loadfile,
player/screenshot.c+8−2
@@ -558,8 +558,14 @@ void cmd_screenshot_raw(void *p) · 557 unmodified lines
558558 struct MPContext *mpctx = cmd->mpctx;
559559 struct mpv_node *res = &cmd->result;
560560
561+ const enum mp_imgfmt formats[] = {IMGFMT_BGR0, IMGFMT_BGRA, IMGFMT_RGBA, IMGFMT_RGBA64};
562+ const char *format_names[] = {"bgr0", "bgra", "rgba", "rgba64"};
563+ int idx = cmd->args[1].v.i;
564+ assert(idx >= 0 && idx <= 3);
565+
566+ bool high_depth = formats[idx] == IMGFMT_RGBA64;
561567 struct mp_image *img = screenshot_get_rgb(mpctx, cmd->args[0].v.i,
562− false, IMGFMT_BGR0);
568+ high_depth, formats[idx]);
563569 if (!img) {
564570 cmd->success = false;
565571 return;
@@ -569,7 +575,7 @@ void cmd_screenshot_raw(void *p) · 3 unmodified lines
569575 node_map_add_int64(res, "w", img->w);
570576 node_map_add_int64(res, "h", img->h);
571577 node_map_add_int64(res, "stride", img->stride[0]);
572− node_map_add_string(res, "format", "bgr0");
578+ node_map_add_string(res, "format", format_names[idx]);
573579 struct mpv_byte_array *ba =
574580 node_map_add(res, "data", MPV_FORMAT_BYTE_ARRAY)->u.ba;
575581 *ba = (struct mpv_byte_array){

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t