mirror of
https://github.com/mpv-player/mpv.git
synced 2024-09-20 12:02:23 +02:00
command: overlay_add: more flexible treatment of offset parameter
Essentially, don't make it the mmap() argument, and just add it to the memory address. This hides tricky things like alignment reequirements from the user. Strictly speaking, this is not entirely backwards compatible: this adds the regression that you can't access past 2 or 4 GB of a file on 32 bit systems anymore. But I doubt anyone cared about this. In theory, we could be clever, and just align the offset manually and pass that to mmap(). This would also be transparent to the user, but minimally more effort, so this is left as exercise to the reader.
This commit is contained in:
parent
3fdb6be316
commit
adf7f0661e
@ -501,13 +501,15 @@ Input Commands that are Possibly Subject to Change
|
||||
|
||||
It is also possible to pass a raw memory address for use as bitmap memory
|
||||
by passing a memory address as integer prefixed with a ``&`` character.
|
||||
Passing the wrong thing here will crash the player. The ``offset`` parameter
|
||||
is not used and must be 0. This mode might be useful for use with libmpv.
|
||||
Passing the wrong thing here will crash the player. This mode might be
|
||||
useful for use with libmpv. The ``offset`` parameter is simply added to the
|
||||
memory address (since mpv 0.8.0, ignored before).
|
||||
|
||||
``offset`` is the offset of the first pixel in the source file. It is
|
||||
passed directly to ``mmap`` and is subject to certain restrictions
|
||||
(see ``man mmap`` for details). In particular, this value has to be a
|
||||
multiple of the system's page size.
|
||||
``offset`` is the byte offset of the first pixel in the source file.
|
||||
(The current implementation always mmap's the whole file from position 0 to
|
||||
the end of the image, so large offsets should be avoided. Before mpv 0.8.0,
|
||||
the offset was actually passed directly to ``mmap``, but it was changed to
|
||||
make using it easier.)
|
||||
|
||||
``fmt`` is a string identifying the image format. Currently, only ``bgra``
|
||||
is defined. This format has 4 bytes per pixels, with 8 bits per component.
|
||||
|
@ -95,7 +95,7 @@ struct command_ctx {
|
||||
};
|
||||
|
||||
struct overlay {
|
||||
bool need_unmap : 1;
|
||||
void *map_start;
|
||||
size_t map_size;
|
||||
struct sub_bitmap osd;
|
||||
};
|
||||
@ -3823,8 +3823,8 @@ static void replace_overlay(struct MPContext *mpctx, int id, struct overlay *new
|
||||
recreate_overlays(mpctx);
|
||||
|
||||
// Do this afterwards, so we never unmap while the OSD is using it.
|
||||
if (old.osd.bitmap && old.map_size)
|
||||
munmap(old.osd.bitmap, old.map_size);
|
||||
if (old.map_start && old.map_size)
|
||||
munmap(old.map_start, old.map_size);
|
||||
}
|
||||
|
||||
static int overlay_add(struct MPContext *mpctx, int id, int x, int y,
|
||||
@ -3854,7 +3854,7 @@ static int overlay_add(struct MPContext *mpctx, int id, int x, int y,
|
||||
};
|
||||
int fd = -1;
|
||||
bool close_fd = true;
|
||||
void *p = (void *)-1;
|
||||
void *p = NULL;
|
||||
if (file[0] == '@') {
|
||||
char *end;
|
||||
fd = strtol(&file[1], &end, 10);
|
||||
@ -3871,16 +3871,20 @@ static int overlay_add(struct MPContext *mpctx, int id, int x, int y,
|
||||
fd = open(file, O_RDONLY | O_BINARY | O_CLOEXEC);
|
||||
}
|
||||
if (fd >= 0) {
|
||||
overlay.map_size = h * stride;
|
||||
p = mmap(NULL, overlay.map_size, PROT_READ, MAP_SHARED, fd, offset);
|
||||
overlay.map_size = offset + h * stride;
|
||||
void *m = mmap(NULL, overlay.map_size, PROT_READ, MAP_SHARED, fd, 0);
|
||||
if (close_fd)
|
||||
close(fd);
|
||||
if (m && m != MAP_FAILED) {
|
||||
overlay.map_start = m;
|
||||
p = m;
|
||||
}
|
||||
}
|
||||
if (!p || p == (void *)-1) {
|
||||
if (!p) {
|
||||
MP_ERR(mpctx, "overlay_add: could not open or map '%s'\n", file);
|
||||
goto error;
|
||||
}
|
||||
overlay.osd.bitmap = p;
|
||||
overlay.osd.bitmap = (char *)p + offset;
|
||||
replace_overlay(mpctx, id, &overlay);
|
||||
r = 0;
|
||||
error:
|
||||
|
Loading…
Reference in New Issue
Block a user