Commit 52b575c8 authored by Janne Grunau's avatar Janne Grunau

cdf: make CdfThreadContext allocation failure safe

parent e72bec43
......@@ -4055,14 +4055,15 @@ static CdfThreadContext cdf_init[4] = {
[3] = { .cdf = NULL },
};
void dav1d_init_states(CdfThreadContext *const cdf, const int qidx) {
int dav1d_init_states(CdfThreadContext *const cdf, const int qidx) {
const int qcat = get_qcat_idx(qidx);
if (cdf_init[qcat].cdf) {
dav1d_cdf_thread_ref(cdf, &cdf_init[qcat]);
return;
return 0;
}
dav1d_cdf_thread_alloc(&cdf_init[qcat], NULL);
int res = dav1d_cdf_thread_alloc(&cdf_init[qcat], NULL);
if (res < 0) return res;
cdf_init[qcat].cdf->m = av1_default_cdf;
memcpy(cdf_init[qcat].cdf->kfym, default_kf_y_mode_cdf,
sizeof(default_kf_y_mode_cdf));
......@@ -4070,6 +4071,7 @@ void dav1d_init_states(CdfThreadContext *const cdf, const int qidx) {
cdf_init[qcat].cdf->mv = default_mv_cdf;
cdf_init[qcat].cdf->dmv = default_mv_cdf;
dav1d_cdf_thread_ref(cdf, &cdf_init[qcat]);
return 0;
}
void dav1d_update_tile_cdf(const Dav1dFrameHeader *const hdr,
......@@ -4211,17 +4213,19 @@ void dav1d_update_tile_cdf(const Dav1dFrameHeader *const hdr,
/*
* CDF threading wrappers.
*/
void dav1d_cdf_thread_alloc(CdfThreadContext *const cdf,
int dav1d_cdf_thread_alloc(CdfThreadContext *const cdf,
struct thread_data *const t)
{
cdf->ref = dav1d_ref_create(sizeof(CdfContext) +
(t != NULL) * sizeof(atomic_uint));
if (!cdf->ref) return -ENOMEM;
cdf->cdf = cdf->ref->data;
if (t) {
cdf->progress = (atomic_uint *) &cdf->cdf[1];
atomic_init(cdf->progress, 0);
cdf->t = t;
}
return 0;
}
void dav1d_cdf_thread_ref(CdfThreadContext *const dst,
......
......@@ -131,11 +131,11 @@ typedef struct CdfThreadContext {
atomic_uint *progress;
} CdfThreadContext;
void dav1d_init_states(CdfThreadContext *cdf, int qidx);
int dav1d_init_states(CdfThreadContext *cdf, int qidx);
void dav1d_update_tile_cdf(const Dav1dFrameHeader *hdr, CdfContext *dst,
const CdfContext *src);
void dav1d_cdf_thread_alloc(CdfThreadContext *cdf, struct thread_data *t);
int dav1d_cdf_thread_alloc(CdfThreadContext *cdf, struct thread_data *t);
void dav1d_cdf_thread_ref(CdfThreadContext *dst, CdfThreadContext *src);
void dav1d_cdf_thread_unref(CdfThreadContext *cdf);
......
......@@ -3103,13 +3103,15 @@ int dav1d_submit_frame(Dav1dContext *const c) {
// setup entropy
if (f->frame_hdr->primary_ref_frame == DAV1D_PRIMARY_REF_NONE) {
dav1d_init_states(&f->in_cdf, f->frame_hdr->quant.yac);
res = dav1d_init_states(&f->in_cdf, f->frame_hdr->quant.yac);
if (res < 0) goto error;
} else {
const int pri_ref = f->frame_hdr->refidx[f->frame_hdr->primary_ref_frame];
dav1d_cdf_thread_ref(&f->in_cdf, &c->cdf[pri_ref]);
}
if (f->frame_hdr->refresh_context) {
dav1d_cdf_thread_alloc(&f->out_cdf, c->n_fc > 1 ? &f->frame_thread.td : NULL);
res = dav1d_cdf_thread_alloc(&f->out_cdf, c->n_fc > 1 ? &f->frame_thread.td : NULL);
if (res < 0) goto error;
}
// FIXME qsort so tiles are in order (for frame threading)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment