summaryrefslogtreecommitdiffstats
path: root/video/sws_utils.c
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2012-11-22 13:30:16 +0100
committerwm4 <wm4@nowhere>2012-11-22 15:26:38 +0100
commit86ad77d0db3bb4ab4b3995408d9e0f8ccbdf6a6d (patch)
tree466bea46f053f0ec597d48fdc063ee6497a38a44 /video/sws_utils.c
parent6c1e21e2239ec524358f819114ed9f271ffb7dc7 (diff)
downloadmpv-86ad77d0db3bb4ab4b3995408d9e0f8ccbdf6a6d.tar.bz2
mpv-86ad77d0db3bb4ab4b3995408d9e0f8ccbdf6a6d.tar.xz
draw_bmp: add RGB rendering to fix image quality issues
As pointed out in commit ed01df, the quality loss due to frequent conversion between RGB and YUV is too much when drawing OSD and subtitles. Fix this by staying in the same colorspace when drawing subtitles. Render directly to RGB, without converting to YUV first. The bad thing about packed RGB is that there are many pixel formats, which would all require special code for blending. It's also completely incompatible to planar YUV. Use planar RGB instead, which allows us to reuse all code originally written for planar YUV. The only thing that needs to be changed is the color conversion in the libass case. (In exchange for simpler code, the image has to be copied, but this is still much better than converting to YUV.) Unfortunately, libswscale doesn't support planar RGB output. Add a hack to sws_utils.c to handle conversion to planar RGB. In the common case, when converting 32 bit per pixel RGB, calling swscale can be avoided entirely. The change in mp_image.c is needed to allocate GBRP images correctly. (The issue with vo_x11 could be easily solved by always backing up the same bounding box as the bitmap drawing RGB<->YUV conversion does, but this commit is probably the better fix.)
Diffstat (limited to 'video/sws_utils.c')
-rw-r--r--video/sws_utils.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/video/sws_utils.c b/video/sws_utils.c
index 951e101066..7d1845f431 100644
--- a/video/sws_utils.c
+++ b/video/sws_utils.c
@@ -151,9 +151,54 @@ static int mp_csp_to_sws_colorspace(enum mp_csp csp)
}
}
+// component_offset[]: byte index of each r (0), g (1), b (2), a (3) component
+static void planarize32(struct mp_image *dst, struct mp_image *src,
+ int component_offset[4])
+{
+ for (int y = 0; y < dst->h; y++) {
+ for (int p = 0; p < 3; p++) {
+ uint8_t *d_line = dst->planes[p] + y * dst->stride[p];
+ uint8_t *s_line = src->planes[0] + y * src->stride[0];
+ s_line += component_offset[(p + 1) % 3]; // GBR => RGB
+ for (int x = 0; x < dst->w; x++) {
+ d_line[x] = s_line[x * 4];
+ }
+ }
+ }
+}
+
+#define SET_COMPS(comp, r, g, b, a) \
+ { (comp)[0] = (r); (comp)[1] = (g); (comp)[2] = (b); (comp)[3] = (a); }
+
+static void to_gbrp(struct mp_image *dst, struct mp_image *src,
+ int my_sws_flags)
+{
+ struct mp_image *temp = NULL;
+ int comp[4];
+
+ switch (src->imgfmt) {
+ case IMGFMT_ABGR: SET_COMPS(comp, 3, 2, 1, 0); break;
+ case IMGFMT_BGRA: SET_COMPS(comp, 2, 1, 0, 3); break;
+ case IMGFMT_ARGB: SET_COMPS(comp, 1, 2, 3, 0); break;
+ case IMGFMT_RGBA: SET_COMPS(comp, 0, 1, 2, 3); break;
+ default:
+ temp = alloc_mpi(dst->w, dst->h, IMGFMT_RGBA);
+ mp_image_swscale(temp, src, my_sws_flags);
+ src = temp;
+ SET_COMPS(comp, 0, 1, 2, 3);
+ }
+
+ planarize32(dst, src, comp);
+
+ talloc_free(temp);
+}
+
void mp_image_swscale(struct mp_image *dst, struct mp_image *src,
int my_sws_flags)
{
+ if (dst->imgfmt == IMGFMT_GBRP)
+ return to_gbrp(dst, src, my_sws_flags);
+
enum PixelFormat s_fmt = imgfmt2pixfmt(src->imgfmt);
if (src->imgfmt == IMGFMT_RGB8 || src->imgfmt == IMGFMT_BGR8)
s_fmt = PIX_FMT_PAL8;