summaryrefslogtreecommitdiffstats
path: root/sub
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-06-17 22:43:43 +0200
committerwm4 <wm4@nowhere>2014-06-17 22:43:43 +0200
commit72aac9ae8a0053e7c30199044cc2c9493a39b793 (patch)
tree90c61f53e20aac949fd4c513267d080814ebf4a8 /sub
parent973c1fa5701366eed3752666d6035454ae37712c (diff)
downloadmpv-72aac9ae8a0053e7c30199044cc2c9493a39b793.tar.bz2
mpv-72aac9ae8a0053e7c30199044cc2c9493a39b793.tar.xz
video: introduce failure path for image allocations
Until now, failure to allocate image data resulted in a crash (i.e. abort() was called). This was intentional, because it's pretty silly to degrade playback, and in almost all situations, the OOM will probably kill you anyway. (And then there's the standard Linux overcommit behavior, which also will kill you at some point.) But I changed my opinion, so here we go. This change does not affect _all_ memory allocations, just image data. Now in most failure cases, the output will just be skipped. For video filters, this coincidentally means that failure is treated as EOF (because the playback core assumes EOF if nothing comes out of the video filter chain). In other situations, output might be in some way degraded, like skipping frames, not scaling OSD, and such. Functions whose return values changed semantics: mp_image_alloc mp_image_new_copy mp_image_new_ref mp_image_make_writeable mp_image_setrefp mp_image_to_av_frame_and_unref mp_image_from_av_frame mp_image_new_external_ref mp_image_new_custom_ref mp_image_pool_make_writeable mp_image_pool_get mp_image_pool_new_copy mp_vdpau_mixed_frame_create vf_alloc_out_image vf_make_out_image_writeable glGetWindowScreenshot
Diffstat (limited to 'sub')
-rw-r--r--sub/draw_bmp.c19
-rw-r--r--sub/img_convert.c34
-rw-r--r--sub/osd.c7
3 files changed, 44 insertions, 16 deletions
diff --git a/sub/draw_bmp.c b/sub/draw_bmp.c
index 0b24689313..02b7f9ec20 100644
--- a/sub/draw_bmp.c
+++ b/sub/draw_bmp.c
@@ -223,12 +223,18 @@ static void scale_sb_rgba(struct sub_bitmap *sb, struct mp_image *dst_format,
sbisrc.planes[0] = sb->bitmap;
sbisrc.stride[0] = sb->stride;
struct mp_image *sbisrc2 = mp_image_alloc(IMGFMT_BGR32, sb->dw, sb->dh);
- mp_image_swscale(sbisrc2, &sbisrc, SWS_BILINEAR);
-
struct mp_image *sba = mp_image_alloc(IMGFMT_Y8, sb->dw, sb->dh);
+ struct mp_image *sbi = mp_image_alloc(dst_format->imgfmt, sb->dw, sb->dh);
+ if (!sbisrc2 || !sba || !sbi) {
+ talloc_free(sbisrc2);
+ talloc_free(sba);
+ talloc_free(sbi);
+ return;
+ }
+
+ mp_image_swscale(sbisrc2, &sbisrc, SWS_BILINEAR);
unpremultiply_and_split_BGR32(sbisrc2, sba);
- struct mp_image *sbi = mp_image_alloc(dst_format->imgfmt, sb->dw, sb->dh);
sbi->params.colorspace = dst_format->params.colorspace;
sbi->params.colorlevels = dst_format->params.colorlevels;
mp_image_swscale(sbi, sbisrc2, SWS_BILINEAR);
@@ -262,6 +268,9 @@ static void draw_rgba(struct mp_draw_sub_cache *cache, struct mp_rect bb,
if (!(sbi && sba))
scale_sb_rgba(sb, temp, &sbi, &sba);
+ // on OOM, skip drawing
+ if (!(sbi && sba))
+ continue;
int bytes = (bits + 7) / 8;
uint8_t *alpha_p = sba->planes[0] + src_y * sba->stride[0] + src_x;
@@ -452,6 +461,8 @@ static struct mp_image *chroma_up(struct mp_draw_sub_cache *cache, int imgfmt,
talloc_free(cache->upsample_img);
cache->upsample_img = mp_image_alloc(imgfmt, src->w, src->h);
talloc_steal(cache, cache->upsample_img);
+ if (!cache->upsample_img)
+ return NULL;
}
cache->upsample_temp = *cache->upsample_img;
@@ -547,6 +558,8 @@ void mp_draw_sub_bitmaps(struct mp_draw_sub_cache **cache, struct mp_image *dst,
struct mp_image dst_region = *dst;
mp_image_crop_rc(&dst_region, bb);
struct mp_image *temp = chroma_up(cache_, format, &dst_region);
+ if (!temp)
+ continue; // on OOM, skip region
if (sbs->format == SUBBITMAP_RGBA) {
draw_rgba(cache_, bb, temp, bits, sbs);
diff --git a/sub/img_convert.c b/sub/img_convert.c
index 8c42c5ed4b..a29db4a6f2 100644
--- a/sub/img_convert.c
+++ b/sub/img_convert.c
@@ -77,6 +77,12 @@ bool osd_conv_idx_to_rgba(struct osd_conv_cache *c, struct sub_bitmaps *imgs)
*d = *s;
struct mp_image *image = mp_image_alloc(IMGFMT_BGRA, s->w, s->h);
talloc_steal(c->parts, image);
+ if (!image) {
+ // on OOM, skip the region by making it 0 sized
+ d->w = d->h = d->dw = d->dh = 0;
+ continue;
+ }
+
d->stride = image->stride[0];
d->bitmap = image->planes[0];
@@ -108,6 +114,8 @@ bool osd_conv_blur_rgba(struct osd_conv_cache *c, struct sub_bitmaps *imgs,
int pad = 5;
struct mp_image *temp = mp_image_alloc(IMGFMT_BGRA, s->w + pad * 2,
s->h + pad * 2);
+ if (!temp)
+ continue; // on OOM, skip region
memset_pic(temp->planes[0], 0, temp->w * 4, temp->h, temp->stride[0]);
uint8_t *p0 = temp->planes[0] + pad * 4 + pad * temp->stride[0];
memcpy_pic(p0, s->bitmap, s->w * 4, s->h, temp->stride[0], s->stride);
@@ -121,10 +129,15 @@ bool osd_conv_blur_rgba(struct osd_conv_cache *c, struct sub_bitmaps *imgs,
d->h = d->dh = s->dh + pad * 2 * sy;
struct mp_image *image = mp_image_alloc(IMGFMT_BGRA, d->w, d->h);
talloc_steal(c->parts, image);
- d->stride = image->stride[0];
- d->bitmap = image->planes[0];
-
- mp_image_sw_blur_scale(image, temp, gblur);
+ if (image) {
+ d->stride = image->stride[0];
+ d->bitmap = image->planes[0];
+
+ mp_image_sw_blur_scale(image, temp, gblur);
+ } else {
+ // on OOM, skip region
+ *d = *s;
+ }
talloc_free(temp);
}
@@ -168,10 +181,15 @@ bool osd_scale_rgba(struct osd_conv_cache *c, struct sub_bitmaps *imgs)
d->h = d->dh = s->dh;
struct mp_image *image = mp_image_alloc(IMGFMT_BGRA, d->w, d->h);
talloc_steal(c->parts, image);
- d->stride = image->stride[0];
- d->bitmap = image->planes[0];
-
- mp_image_swscale(image, &src_image, mp_sws_fast_flags);
+ if (image) {
+ d->stride = image->stride[0];
+ d->bitmap = image->planes[0];
+
+ mp_image_swscale(image, &src_image, mp_sws_fast_flags);
+ } else {
+ // on OOM, skip the region; just don't scale it
+ *d = *s;
+ }
}
return true;
}
diff --git a/sub/osd.c b/sub/osd.c
index 28a98892ce..e88df98afe 100644
--- a/sub/osd.c
+++ b/sub/osd.c
@@ -353,11 +353,8 @@ static void draw_on_image(void *ctx, struct sub_bitmaps *imgs)
{
struct draw_on_image_closure *closure = ctx;
struct osd_state *osd = closure->osd;
- if (closure->pool) {
- mp_image_pool_make_writeable(closure->pool, closure->dest);
- } else {
- mp_image_make_writeable(closure->dest);
- }
+ if (!mp_image_pool_make_writeable(closure->pool, closure->dest))
+ return; // on OOM, skip
mp_draw_sub_bitmaps(&osd->draw_cache, closure->dest, imgs);
talloc_steal(osd, osd->draw_cache);
closure->changed = true;