diff --git a/.arcconfig b/.arcconfig --- a/.arcconfig +++ b/.arcconfig @@ -1,5 +1,5 @@ { - "phabricator.uri" : "https://phabricator.services.mozilla.com/", + "phabricator.uri" : "https://phabricator-dev.allizom.org/", "repository.callsign": "MOZILLACENTRAL", "history.immutable": false } diff --git a/media/libdav1d/moz.yaml b/media/libdav1d/moz.yaml --- a/media/libdav1d/moz.yaml +++ b/media/libdav1d/moz.yaml @@ -18,15 +18,15 @@ # Usually different from repository url url: https://code.videolan.org/videolan/dav1d # Human-readable identifier for this version/release # Generally "version NNN", "tag SSS", "bookmark SSS" - release: commit 0243c3ffb644e61848b82f24f5e4a7324669d76e (2020-09-27T15:38:45.000+02:00). + release: commit 3fdf468e51307c3fefabb65dfc39f364edd2ae95 (2020-11-18T13:18:16.000+01:00). # Revision to pull in # Must be a long or short commit SHA (long preferred) - revision: 0243c3ffb644e61848b82f24f5e4a7324669d76e + revision: 3fdf468e51307c3fefabb65dfc39f364edd2ae95 # The package's license, where possible using the mnemonic from # https://spdx.org/licenses/ # Multiple licenses can be specified (as a YAML list) # A "LICENSE" file must exist containing the full license text diff --git a/media/libdav1d/vcs_version.h b/media/libdav1d/vcs_version.h --- a/media/libdav1d/vcs_version.h +++ b/media/libdav1d/vcs_version.h @@ -1,2 +1,2 @@ /* auto-generated, do not edit */ -#define DAV1D_VERSION "0.7.1-81-g0243c3f" +#define DAV1D_VERSION "3fdf468e51307c3fefabb65dfc39f364edd2ae95" diff --git a/third_party/dav1d/src/decode.c b/third_party/dav1d/src/decode.c --- a/third_party/dav1d/src/decode.c +++ b/third_party/dav1d/src/decode.c @@ -2676,21 +2676,21 @@ f->tile_thread.titsati_init[1] != f->frame_hdr->tiling.rows || memcmp(f->frame_hdr->tiling.row_start_sb, f->tile_thread.titsati_index_rows, sizeof(*f->tile_thread.titsati_index_rows) * (f->frame_hdr->tiling.rows + 1))) { - for (int tile_row = 0, tile_idx = 0; + for (int tile_row = 0, task_idx = 0; tile_row < f->frame_hdr->tiling.rows; tile_row++) { for (int sby = f->frame_hdr->tiling.row_start_sb[tile_row]; sby < f->frame_hdr->tiling.row_start_sb[tile_row + 1]; sby++) { for (int tile_col = 0; tile_col < f->frame_hdr->tiling.cols; - tile_col++, tile_idx++) + tile_col++, task_idx++) { - f->tile_thread.task_idx_to_sby_and_tile_idx[tile_idx][0] = sby; - f->tile_thread.task_idx_to_sby_and_tile_idx[tile_idx][1] = + f->tile_thread.task_idx_to_sby_and_tile_idx[task_idx][0] = sby; + f->tile_thread.task_idx_to_sby_and_tile_idx[task_idx][1] = tile_row * f->frame_hdr->tiling.cols + tile_col; } } } f->tile_thread.titsati_init[0] = f->frame_hdr->tiling.cols; @@ -3100,11 +3100,11 @@ if (c->n_fc > 1) for (int n = 0; n < 7; n++) if (dav1d_thread_picture_wait(&f->refp[n], 4 * (t->by + f->sb_step), PLANE_TYPE_BLOCK)) { - return 1; + goto error; } dav1d_refmvs_load_tmvs(&f->rf, tile_row, 0, f->bw >> 1, t->by >> 1, by_end); } for (int tile_col = 0; tile_col < f->frame_hdr->tiling.cols; tile_col++) { diff --git a/third_party/dav1d/src/internal.h b/third_party/dav1d/src/internal.h --- a/third_party/dav1d/src/internal.h +++ b/third_party/dav1d/src/internal.h @@ -133,10 +133,15 @@ int all_layers; unsigned frame_size_limit; int drain; Dav1dLogger logger; + + struct { + pthread_mutex_t lock; + Dav1dPictureBuffer *buf; + } picture_buffer_pool; }; struct Dav1dFrameContext { Dav1dRef *seq_hdr_ref; Dav1dSequenceHeader *seq_hdr; diff --git a/third_party/dav1d/src/lib.c b/third_party/dav1d/src/lib.c --- a/third_party/dav1d/src/lib.c +++ b/third_party/dav1d/src/lib.c @@ -127,10 +127,15 @@ c->apply_grain = s->apply_grain; c->operating_point = s->operating_point; c->all_layers = s->all_layers; c->frame_size_limit = s->frame_size_limit; + if (c->allocator.alloc_picture_callback == dav1d_default_picture_alloc) { + if (pthread_mutex_init(&c->picture_buffer_pool.lock, NULL)) goto error; + c->allocator.cookie = c; + } + /* On 32-bit systems extremely large frame sizes can cause overflows in * dav1d_decode_frame() malloc size calculations. Prevent that from occuring * by enforcing a maximum frame size limit, chosen to roughly correspond to * the largest size possible to decode without exhausting virtual memory. */ if (sizeof(size_t) < 8 && s->frame_size_limit - 1 >= 8192 * 8192) { @@ -570,10 +575,18 @@ dav1d_ref_dec(&c->mastering_display_ref); dav1d_ref_dec(&c->content_light_ref); dav1d_ref_dec(&c->itut_t35_ref); + pthread_mutex_destroy(&c->picture_buffer_pool.lock); + Dav1dPictureBuffer *buf = c->picture_buffer_pool.buf; + while (buf) { + Dav1dPictureBuffer *const next = buf->next; + dav1d_free_aligned(buf->data); + buf = next; + } + dav1d_freep_aligned(c_out); } void dav1d_picture_unref(Dav1dPicture *const p) { dav1d_picture_unref_internal(p); diff --git a/third_party/dav1d/src/looprestoration_tmpl.c b/third_party/dav1d/src/looprestoration_tmpl.c --- a/third_party/dav1d/src/looprestoration_tmpl.c +++ b/third_party/dav1d/src/looprestoration_tmpl.c @@ -206,48 +206,62 @@ // // s: Pixel summed and stored // i: Pixel summed and stored (between loops) // c: Pixel summed not stored // x: Pixel not summed not stored -static void boxsum3(coef *dst, const pixel *src, const int w, const int h) { +static void boxsum3(int32_t *sumsq, coef *sum, const pixel *src, + const int w, const int h) +{ // We skip the first row, as it is never used src += REST_UNIT_STRIDE; - dst += REST_UNIT_STRIDE; // We skip the first and last columns, as they are never used for (int x = 1; x < w - 1; x++) { - coef *ds = dst + x; + coef *sum_v = sum + x; + int32_t *sumsq_v = sumsq + x; const pixel *s = src + x; - int a = s[0], b = s[REST_UNIT_STRIDE]; + int a = s[0], a2 = a * a; + int b = s[REST_UNIT_STRIDE], b2 = b * b; // We skip the first 2 rows, as they are skipped in the next loop and // we don't need the last 2 row as it is skipped in the next loop for (int y = 2; y < h - 2; y++) { s += REST_UNIT_STRIDE; const int c = s[REST_UNIT_STRIDE]; - ds += REST_UNIT_STRIDE; - *ds = a + b + c; + const int c2 = c * c; + sum_v += REST_UNIT_STRIDE; + sumsq_v += REST_UNIT_STRIDE; + *sum_v = a + b + c; + *sumsq_v = a2 + b2 + c2; a = b; + a2 = b2; b = c; + b2 = c2; } } - // We skip the first 2 rows as they are never read - dst += REST_UNIT_STRIDE; + // We skip the first row as it is never read + sum += REST_UNIT_STRIDE; + sumsq += REST_UNIT_STRIDE; // We skip the last 2 rows as it is never read for (int y = 2; y < h - 2; y++) { - int a = dst[1], b = dst[2]; + int a = sum[1], a2 = sumsq[1]; + int b = sum[2], b2 = sumsq[2]; // We don't store the first column as it is never read and // we don't store the last 2 columns as they are never read for (int x = 2; x < w - 2; x++) { - const int c = dst[x + 1]; - dst[x] = a + b + c; + const int c = sum[x + 1], c2 = sumsq[x + 1]; + sum[x] = a + b + c; + sumsq[x] = a2 + b2 + c2; a = b; + a2 = b2; b = c; + b2 = c2; } - dst += REST_UNIT_STRIDE; + sum += REST_UNIT_STRIDE; + sumsq += REST_UNIT_STRIDE; } } // Sum over a 5x5 area // The dst and src pointers are positioned 3 pixels above and 3 pixels to the @@ -269,145 +283,66 @@ // // s: Pixel summed and stored // i: Pixel summed and stored (between loops) // c: Pixel summed not stored // x: Pixel not summed not stored -static void boxsum5(coef *dst, const pixel *const src, const int w, const int h) { - // We skip the first row, as it is never used - dst += REST_UNIT_STRIDE; - +static void boxsum5(int32_t *sumsq, coef *sum, const pixel *const src, + const int w, const int h) +{ for (int x = 0; x < w; x++) { - coef *ds = dst + x; + coef *sum_v = sum + x; + int32_t *sumsq_v = sumsq + x; const pixel *s = src + 3 * REST_UNIT_STRIDE + x; - int a = s[-3 * REST_UNIT_STRIDE]; - int b = s[-2 * REST_UNIT_STRIDE]; - int c = s[-1 * REST_UNIT_STRIDE]; - int d = s[0]; + int a = s[-3 * REST_UNIT_STRIDE], a2 = a * a; + int b = s[-2 * REST_UNIT_STRIDE], b2 = b * b; + int c = s[-1 * REST_UNIT_STRIDE], c2 = c * c; + int d = s[0], d2 = d * d; // We skip the first 2 rows, as they are skipped in the next loop and // we don't need the last 2 row as it is skipped in the next loop for (int y = 2; y < h - 2; y++) { s += REST_UNIT_STRIDE; - const int e = *s; - ds += REST_UNIT_STRIDE; - *ds = a + b + c + d + e; - a = b; - b = c; - c = d; - d = e; - } - } - - // We skip the first 2 rows as they are never read - dst += REST_UNIT_STRIDE; - for (int y = 2; y < h - 2; y++) { - int a = dst[0]; - int b = dst[1]; - int c = dst[2]; - int d = dst[3]; - - for (int x = 2; x < w - 2; x++) { - const int e = dst[x + 2]; - dst[x] = a + b + c + d + e; + const int e = *s, e2 = e * e; + sum_v += REST_UNIT_STRIDE; + sumsq_v += REST_UNIT_STRIDE; + *sum_v = a + b + c + d + e; + *sumsq_v = a2 + b2 + c2 + d2 + e2; a = b; b = c; c = d; d = e; + a2 = b2; + b2 = c2; + c2 = d2; + d2 = e2; } - dst += REST_UNIT_STRIDE; } -} - -// See boxsum3 function comments for details on row and column skipping -static void boxsum3sqr(int32_t *dst, const pixel *src, const int w, const int h) { - // We skip the first row, as it is never used - src += REST_UNIT_STRIDE; - dst += REST_UNIT_STRIDE; - - // We skip the first and last columns, as they are never used - for (int x = 1; x < w - 1; x++) { - int32_t *ds = dst + x; - const pixel *s = src + x; - int a = s[0] * s[0]; - int b = s[REST_UNIT_STRIDE] * s[REST_UNIT_STRIDE]; - - // We skip the first row, as it is skipped in the next loop and - // we don't need the last row as it is skipped in the next loop - for (int y = 2; y < h - 2; y++) { - s += REST_UNIT_STRIDE; - const int c = s[REST_UNIT_STRIDE] * s[REST_UNIT_STRIDE]; - ds += REST_UNIT_STRIDE; - *ds = a + b + c; - a = b; - b = c; - } - } // We skip the first row as it is never read - dst += REST_UNIT_STRIDE; - // We skip the last row as it is never read + sum += REST_UNIT_STRIDE; + sumsq += REST_UNIT_STRIDE; for (int y = 2; y < h - 2; y++) { - int a = dst[1], b = dst[2]; - - // We don't store the first column as it is never read and - // we don't store the last 2 columns as they are never read - for (int x = 2; x < w - 2; x++) { - const int c = dst[x + 1]; - dst[x] = a + b + c; - a = b; - b = c; - } - dst += REST_UNIT_STRIDE; - } -} + int a = sum[0], a2 = sumsq[0]; + int b = sum[1], b2 = sumsq[1]; + int c = sum[2], c2 = sumsq[2]; + int d = sum[3], d2 = sumsq[3]; -// See boxsum5 function comments for details on row and column skipping -static void boxsum5sqr(int32_t *dst, const pixel *const src, const int w, - const int h) -{ - // We skip the first row, as it is never used - dst += REST_UNIT_STRIDE; - - for (int x = 0; x < w; x++) { - int32_t *ds = dst + x; - const pixel *s = src + 3 * REST_UNIT_STRIDE + x; - int a = s[-3 * REST_UNIT_STRIDE] * s[-3 * REST_UNIT_STRIDE]; - int b = s[-2 * REST_UNIT_STRIDE] * s[-2 * REST_UNIT_STRIDE]; - int c = s[-1 * REST_UNIT_STRIDE] * s[-1 * REST_UNIT_STRIDE]; - int d = s[0] * s[0]; - - // We skip the first 2 rows, as they are skipped in the next loop and - // we don't need the last 2 row as it is skipped in the next loop - for (int y = 2; y < h - 2; y++) { - s += REST_UNIT_STRIDE; - const int e = s[0] * s[0]; - ds += REST_UNIT_STRIDE; - *ds = a + b + c + d + e; + for (int x = 2; x < w - 2; x++) { + const int e = sum[x + 2], e2 = sumsq[x + 2]; + sum[x] = a + b + c + d + e; + sumsq[x] = a2 + b2 + c2 + d2 + e2; a = b; b = c; c = d; d = e; + a2 = b2; + b2 = c2; + c2 = d2; + d2 = e2; } - } - - // We skip the first 2 rows as they are never read - dst += REST_UNIT_STRIDE; - for (int y = 2; y < h - 2; y++) { - int a = dst[0]; - int b = dst[1]; - int c = dst[2]; - int d = dst[3]; - - for (int x = 2; x < w - 2; x++) { - const int e = dst[x + 2]; - dst[x] = a + b + c + d + e; - a = b; - b = c; - c = d; - d = e; - } - dst += REST_UNIT_STRIDE; + sum += REST_UNIT_STRIDE; + sumsq += REST_UNIT_STRIDE; } } static void selfguided_filter(coef *dst, const pixel *src, const ptrdiff_t src_stride, const int w, @@ -416,25 +351,22 @@ { const int sgr_one_by_x = n == 25 ? 164 : 455; // Selfguided filter is applied to a maximum stripe height of 64 + 3 pixels // of padding above and below - int32_t A_[70 /*(64 + 3 + 3)*/ * REST_UNIT_STRIDE]; - int32_t *A = A_ + 3 * REST_UNIT_STRIDE + 3; + int32_t sumsq[68 /*(64 + 2 + 2)*/ * REST_UNIT_STRIDE]; + int32_t *A = sumsq + 2 * REST_UNIT_STRIDE + 3; // By inverting A and B after the boxsums, B can be of size coef instead // of int32_t - coef B_[70 /*(64 + 3 + 3)*/ * REST_UNIT_STRIDE]; - coef *B = B_ + 3 * REST_UNIT_STRIDE + 3; + coef sum[68 /*(64 + 2 + 2)*/ * REST_UNIT_STRIDE]; + coef *B = sum + 2 * REST_UNIT_STRIDE + 3; const int step = (n == 25) + 1; - if (n == 25) { - boxsum5(B_, src, w + 6, h + 6); - boxsum5sqr(A_, src, w + 6, h + 6); - } else { - boxsum3(B_, src, w + 6, h + 6); - boxsum3sqr(A_, src, w + 6, h + 6); - } + if (n == 25) + boxsum5(sumsq, sum, src, w + 6, h + 6); + else + boxsum3(sumsq, sum, src, w + 6, h + 6); const int bitdepth_min_8 = bitdepth_from_max(bitdepth_max) - 8; int32_t *AA = A - REST_UNIT_STRIDE; coef *BB = B - REST_UNIT_STRIDE; for (int j = -1; j < h + 1; j+= step) { diff --git a/third_party/dav1d/src/obu.c b/third_party/dav1d/src/obu.c --- a/third_party/dav1d/src/obu.c +++ b/third_party/dav1d/src/obu.c @@ -53,19 +53,19 @@ #endif hdr->profile = dav1d_get_bits(gb, 3); if (hdr->profile > 2) goto error; #if DEBUG_SEQ_HDR - printf("SEQHDR: post-profile: off=%ld\n", + printf("SEQHDR: post-profile: off=%u\n", dav1d_get_bits_pos(gb) - init_bit_pos); #endif hdr->still_picture = dav1d_get_bits(gb, 1); hdr->reduced_still_picture_header = dav1d_get_bits(gb, 1); if (hdr->reduced_still_picture_header && !hdr->still_picture) goto error; #if DEBUG_SEQ_HDR - printf("SEQHDR: post-stillpicture_flags: off=%ld\n", + printf("SEQHDR: post-stillpicture_flags: off=%u\n", dav1d_get_bits_pos(gb) - init_bit_pos); #endif if (hdr->reduced_still_picture_header) { hdr->timing_info_present = 0; @@ -100,11 +100,11 @@ } } else { hdr->decoder_model_info_present = 0; } #if DEBUG_SEQ_HDR - printf("SEQHDR: post-timinginfo: off=%ld\n", + printf("SEQHDR: post-timinginfo: off=%u\n", dav1d_get_bits_pos(gb) - init_bit_pos); #endif hdr->display_model_info_present = dav1d_get_bits(gb, 1); hdr->num_operating_points = dav1d_get_bits(gb, 5) + 1; @@ -136,31 +136,31 @@ } const int op_idx = c->operating_point < hdr->num_operating_points ? c->operating_point : 0; c->operating_point_idc = hdr->operating_points[op_idx].idc; #if DEBUG_SEQ_HDR - printf("SEQHDR: post-operating-points: off=%ld\n", + printf("SEQHDR: post-operating-points: off=%u\n", dav1d_get_bits_pos(gb) - init_bit_pos); #endif } hdr->width_n_bits = dav1d_get_bits(gb, 4) + 1; hdr->height_n_bits = dav1d_get_bits(gb, 4) + 1; hdr->max_width = dav1d_get_bits(gb, hdr->width_n_bits) + 1; hdr->max_height = dav1d_get_bits(gb, hdr->height_n_bits) + 1; #if DEBUG_SEQ_HDR - printf("SEQHDR: post-size: off=%ld\n", + printf("SEQHDR: post-size: off=%u\n", dav1d_get_bits_pos(gb) - init_bit_pos); #endif hdr->frame_id_numbers_present = hdr->reduced_still_picture_header ? 0 : dav1d_get_bits(gb, 1); if (hdr->frame_id_numbers_present) { hdr->delta_frame_id_n_bits = dav1d_get_bits(gb, 4) + 2; hdr->frame_id_n_bits = dav1d_get_bits(gb, 3) + hdr->delta_frame_id_n_bits + 1; } #if DEBUG_SEQ_HDR - printf("SEQHDR: post-frame-id-numbers-present: off=%ld\n", + printf("SEQHDR: post-frame-id-numbers-present: off=%u\n", dav1d_get_bits_pos(gb) - init_bit_pos); #endif hdr->sb128 = dav1d_get_bits(gb, 1); hdr->filter_intra = dav1d_get_bits(gb, 1); @@ -190,11 +190,11 @@ hdr->ref_frame_mvs = 0; hdr->order_hint_n_bits = 0; } hdr->screen_content_tools = dav1d_get_bits(gb, 1) ? DAV1D_ADAPTIVE : dav1d_get_bits(gb, 1); #if DEBUG_SEQ_HDR - printf("SEQHDR: post-screentools: off=%ld\n", + printf("SEQHDR: post-screentools: off=%u\n", dav1d_get_bits_pos(gb) - init_bit_pos); #endif hdr->force_integer_mv = hdr->screen_content_tools ? dav1d_get_bits(gb, 1) ? DAV1D_ADAPTIVE : dav1d_get_bits(gb, 1) : 2; if (hdr->order_hint) @@ -202,11 +202,11 @@ } hdr->super_res = dav1d_get_bits(gb, 1); hdr->cdef = dav1d_get_bits(gb, 1); hdr->restoration = dav1d_get_bits(gb, 1); #if DEBUG_SEQ_HDR - printf("SEQHDR: post-featurebits: off=%ld\n", + printf("SEQHDR: post-featurebits: off=%u\n", dav1d_get_bits_pos(gb) - init_bit_pos); #endif hdr->hbd = dav1d_get_bits(gb, 1); if (hdr->profile == 2 && hdr->hbd) hdr->hbd += dav1d_get_bits(gb, 1); @@ -262,17 +262,17 @@ hdr->chr = hdr->ss_hor == 1 && hdr->ss_ver == 1 ? dav1d_get_bits(gb, 2) : DAV1D_CHR_UNKNOWN; } hdr->separate_uv_delta_q = !hdr->monochrome && dav1d_get_bits(gb, 1); #if DEBUG_SEQ_HDR - printf("SEQHDR: post-colorinfo: off=%ld\n", + printf("SEQHDR: post-colorinfo: off=%u\n", dav1d_get_bits_pos(gb) - init_bit_pos); #endif hdr->film_grain_present = dav1d_get_bits(gb, 1); #if DEBUG_SEQ_HDR - printf("SEQHDR: post-filmgrain: off=%ld\n", + printf("SEQHDR: post-filmgrain: off=%u\n", dav1d_get_bits_pos(gb) - init_bit_pos); #endif dav1d_get_bits(gb, 1); // dummy bit @@ -365,19 +365,22 @@ Dav1dFrameHeader *const hdr = c->frame_hdr; hdr->show_existing_frame = !seqhdr->reduced_still_picture_header && dav1d_get_bits(gb, 1); #if DEBUG_FRAME_HDR - printf("HDR: post-show_existing_frame: off=%ld\n", + printf("HDR: post-show_existing_frame: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif if (hdr->show_existing_frame) { hdr->existing_frame_idx = dav1d_get_bits(gb, 3); if (seqhdr->decoder_model_info_present && !seqhdr->equal_picture_interval) hdr->frame_presentation_delay = dav1d_get_bits(gb, seqhdr->frame_presentation_delay_length); - if (seqhdr->frame_id_numbers_present) + if (seqhdr->frame_id_numbers_present) { hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits); + Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->existing_frame_idx].p.p.frame_hdr; + if (!ref_frame_hdr || ref_frame_hdr->frame_id != hdr->frame_id) return DAV1D_ERR(EINVAL); + } return 0; } hdr->frame_type = seqhdr->reduced_still_picture_header ? DAV1D_FRAME_TYPE_KEY : dav1d_get_bits(gb, 2); hdr->show_frame = seqhdr->reduced_still_picture_header || dav1d_get_bits(gb, 1); @@ -389,11 +392,11 @@ hdr->error_resilient_mode = (hdr->frame_type == DAV1D_FRAME_TYPE_KEY && hdr->show_frame) || hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH || seqhdr->reduced_still_picture_header || dav1d_get_bits(gb, 1); #if DEBUG_FRAME_HDR - printf("HDR: post-frametype_bits: off=%ld\n", + printf("HDR: post-frametype_bits: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif hdr->disable_cdf_update = dav1d_get_bits(gb, 1); hdr->allow_screen_content_tools = seqhdr->screen_content_tools == DAV1D_ADAPTIVE ? dav1d_get_bits(gb, 1) : seqhdr->screen_content_tools; @@ -410,11 +413,11 @@ hdr->frame_id = dav1d_get_bits(gb, seqhdr->frame_id_n_bits); hdr->frame_size_override = seqhdr->reduced_still_picture_header ? 0 : hdr->frame_type == DAV1D_FRAME_TYPE_SWITCH ? 1 : dav1d_get_bits(gb, 1); #if DEBUG_FRAME_HDR - printf("HDR: post-frame_size_override_flag: off=%ld\n", + printf("HDR: post-frame_size_override_flag: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif hdr->frame_offset = seqhdr->order_hint ? dav1d_get_bits(gb, seqhdr->order_hint_n_bits) : 0; hdr->primary_ref_frame = !hdr->error_resilient_mode && hdr->frame_type & 1 ? @@ -548,12 +551,16 @@ } } for (int i = 0; i < 7; i++) { if (!hdr->frame_ref_short_signaling) hdr->refidx[i] = dav1d_get_bits(gb, 3); - if (seqhdr->frame_id_numbers_present) - dav1d_get_bits(gb, seqhdr->delta_frame_id_n_bits); + if (seqhdr->frame_id_numbers_present) { + const int delta_ref_frame_id_minus_1 = dav1d_get_bits(gb, seqhdr->delta_frame_id_n_bits); + const int ref_frame_id = (hdr->frame_id + (1 << seqhdr->frame_id_n_bits) - delta_ref_frame_id_minus_1 - 1) & ((1 << seqhdr->frame_id_n_bits) - 1); + Dav1dFrameHeader *const ref_frame_hdr = c->refs[hdr->refidx[i]].p.p.frame_hdr; + if (!ref_frame_hdr || ref_frame_hdr->frame_id != ref_frame_id) goto error; + } } const int use_ref = !hdr->error_resilient_mode && hdr->frame_size_override; if (read_frame_size(c, gb, use_ref) < 0) goto error; hdr->hp = !hdr->force_integer_mv && dav1d_get_bits(gb, 1); @@ -563,18 +570,18 @@ hdr->use_ref_frame_mvs = !hdr->error_resilient_mode && seqhdr->ref_frame_mvs && seqhdr->order_hint && hdr->frame_type & 1 && dav1d_get_bits(gb, 1); } #if DEBUG_FRAME_HDR - printf("HDR: post-frametype-specific-bits: off=%ld\n", + printf("HDR: post-frametype-specific-bits: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif hdr->refresh_context = !seqhdr->reduced_still_picture_header && !hdr->disable_cdf_update && !dav1d_get_bits(gb, 1); #if DEBUG_FRAME_HDR - printf("HDR: post-refresh_context: off=%ld\n", + printf("HDR: post-refresh_context: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif // tile data hdr->tiling.uniform = dav1d_get_bits(gb, 1); @@ -644,11 +651,11 @@ hdr->tiling.n_bytes = dav1d_get_bits(gb, 2) + 1; } else { hdr->tiling.n_bytes = hdr->tiling.update = 0; } #if DEBUG_FRAME_HDR - printf("HDR: post-tiling: off=%ld\n", + printf("HDR: post-tiling: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif // quant data hdr->quant.yac = dav1d_get_bits(gb, 8); @@ -667,11 +674,11 @@ hdr->quant.vdc_delta = hdr->quant.udc_delta; hdr->quant.vac_delta = hdr->quant.uac_delta; } } #if DEBUG_FRAME_HDR - printf("HDR: post-quant: off=%ld\n", + printf("HDR: post-quant: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif hdr->quant.qm = dav1d_get_bits(gb, 1); if (hdr->quant.qm) { hdr->quant.qm_y = dav1d_get_bits(gb, 4); @@ -679,11 +686,11 @@ hdr->quant.qm_v = seqhdr->separate_uv_delta_q ? (int)dav1d_get_bits(gb, 4) : hdr->quant.qm_u; } #if DEBUG_FRAME_HDR - printf("HDR: post-qm: off=%ld\n", + printf("HDR: post-qm: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif // segmentation data hdr->segmentation.enabled = dav1d_get_bits(gb, 1); @@ -764,11 +771,11 @@ memset(&hdr->segmentation.seg_data, 0, sizeof(Dav1dSegmentationDataSet)); for (int i = 0; i < DAV1D_MAX_SEGMENTS; i++) hdr->segmentation.seg_data.d[i].ref = -1; } #if DEBUG_FRAME_HDR - printf("HDR: post-segmentation: off=%ld\n", + printf("HDR: post-segmentation: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif // delta q hdr->delta.q.present = hdr->quant.yac ? dav1d_get_bits(gb, 1) : 0; @@ -776,11 +783,11 @@ hdr->delta.lf.present = hdr->delta.q.present && !hdr->allow_intrabc && dav1d_get_bits(gb, 1); hdr->delta.lf.res_log2 = hdr->delta.lf.present ? dav1d_get_bits(gb, 2) : 0; hdr->delta.lf.multi = hdr->delta.lf.present ? dav1d_get_bits(gb, 1) : 0; #if DEBUG_FRAME_HDR - printf("HDR: post-delta_q_lf_flags: off=%ld\n", + printf("HDR: post-delta_q_lf_flags: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif // derive lossless flags const int delta_lossless = !hdr->quant.ydc_delta && !hdr->quant.udc_delta && @@ -836,11 +843,11 @@ dav1d_get_sbits(gb, 6); } } } #if DEBUG_FRAME_HDR - printf("HDR: post-lpf: off=%ld\n", + printf("HDR: post-lpf: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif // cdef if (!hdr->all_lossless && seqhdr->cdef && !hdr->allow_intrabc) { @@ -855,11 +862,11 @@ hdr->cdef.n_bits = 0; hdr->cdef.y_strength[0] = 0; hdr->cdef.uv_strength[0] = 0; } #if DEBUG_FRAME_HDR - printf("HDR: post-cdef: off=%ld\n", + printf("HDR: post-cdef: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif // restoration if ((!hdr->all_lossless || hdr->super_res.enabled) && @@ -897,23 +904,23 @@ hdr->restoration.type[0] = DAV1D_RESTORATION_NONE; hdr->restoration.type[1] = DAV1D_RESTORATION_NONE; hdr->restoration.type[2] = DAV1D_RESTORATION_NONE; } #if DEBUG_FRAME_HDR - printf("HDR: post-restoration: off=%ld\n", + printf("HDR: post-restoration: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif hdr->txfm_mode = hdr->all_lossless ? DAV1D_TX_4X4_ONLY : dav1d_get_bits(gb, 1) ? DAV1D_TX_SWITCHABLE : DAV1D_TX_LARGEST; #if DEBUG_FRAME_HDR - printf("HDR: post-txfmmode: off=%ld\n", + printf("HDR: post-txfmmode: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif hdr->switchable_comp_refs = hdr->frame_type & 1 ? dav1d_get_bits(gb, 1) : 0; #if DEBUG_FRAME_HDR - printf("HDR: post-refmode: off=%ld\n", + printf("HDR: post-refmode: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif hdr->skip_mode_allowed = 0; if (hdr->switchable_comp_refs && hdr->frame_type & 1 && seqhdr->order_hint) { const unsigned poc = hdr->frame_offset; @@ -970,22 +977,22 @@ } } } hdr->skip_mode_enabled = hdr->skip_mode_allowed ? dav1d_get_bits(gb, 1) : 0; #if DEBUG_FRAME_HDR - printf("HDR: post-extskip: off=%ld\n", + printf("HDR: post-extskip: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif hdr->warp_motion = !hdr->error_resilient_mode && hdr->frame_type & 1 && seqhdr->warped_motion && dav1d_get_bits(gb, 1); #if DEBUG_FRAME_HDR - printf("HDR: post-warpmotionbit: off=%ld\n", + printf("HDR: post-warpmotionbit: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif hdr->reduced_txtp_set = dav1d_get_bits(gb, 1); #if DEBUG_FRAME_HDR - printf("HDR: post-reducedtxtpset: off=%ld\n", + printf("HDR: post-reducedtxtpset: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif for (int i = 0; i < 7; i++) hdr->gmv[i] = dav1d_default_wm_params; @@ -1035,11 +1042,11 @@ mat[0] = dav1d_get_bits_subexp(gb, ref_mat[0] >> shift, bits) * (1 << shift); mat[1] = dav1d_get_bits_subexp(gb, ref_mat[1] >> shift, bits) * (1 << shift); } } #if DEBUG_FRAME_HDR - printf("HDR: post-gmv: off=%ld\n", + printf("HDR: post-gmv: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif hdr->film_grain.present = seqhdr->film_grain_present && (hdr->show_frame || hdr->showable_frame) && @@ -1119,11 +1126,11 @@ } } else { memset(&hdr->film_grain.data, 0, sizeof(hdr->film_grain.data)); } #if DEBUG_FRAME_HDR - printf("HDR: post-filmgrain: off=%ld\n", + printf("HDR: post-filmgrain: off=%td\n", (gb->ptr - init_ptr) * 8 - gb->bits_left); #endif return 0; @@ -1364,10 +1371,14 @@ c->tile[c->n_tile_data].start; c->n_tile_data++; break; } case DAV1D_OBU_METADATA: { +#define DEBUG_OBU_METADATA 0 +#if DEBUG_OBU_METADATA + const uint8_t *const init_ptr = gb.ptr; +#endif // obu metadta type field const enum ObuMetaType meta_type = dav1d_get_uleb128(&gb); const int meta_type_len = (dav1d_get_bits_pos(&gb) - init_bit_pos) >> 3; if (gb.error) goto error; @@ -1376,11 +1387,21 @@ Dav1dRef *ref = dav1d_ref_create(sizeof(Dav1dContentLightLevel)); if (!ref) return DAV1D_ERR(ENOMEM); Dav1dContentLightLevel *const content_light = ref->data; content_light->max_content_light_level = dav1d_get_bits(&gb, 16); +#if DEBUG_OBU_METADATA + printf("CLLOBU: max-content-light-level: %d [off=%td]\n", + content_light->max_content_light_level, + (gb.ptr - init_ptr) * 8 - gb.bits_left); +#endif content_light->max_frame_average_light_level = dav1d_get_bits(&gb, 16); +#if DEBUG_OBU_METADATA + printf("CLLOBU: max-frame-average-light-level: %d [off=%td]\n", + content_light->max_frame_average_light_level, + (gb.ptr - init_ptr) * 8 - gb.bits_left); +#endif // Skip the trailing bit, align to the next byte boundary and check for overrun. dav1d_get_bits(&gb, 1); dav1d_bytealign_get_bits(&gb); if (check_for_overrun(c, &gb, init_bit_pos, len)) { @@ -1399,17 +1420,41 @@ Dav1dMasteringDisplay *const mastering_display = ref->data; for (int i = 0; i < 3; i++) { mastering_display->primaries[i][0] = dav1d_get_bits(&gb, 16); mastering_display->primaries[i][1] = dav1d_get_bits(&gb, 16); +#if DEBUG_OBU_METADATA + printf("MDCVOBU: primaries[%d]: (%d, %d) [off=%td]\n", i, + mastering_display->primaries[i][0], + mastering_display->primaries[i][1], + (gb.ptr - init_ptr) * 8 - gb.bits_left); +#endif } mastering_display->white_point[0] = dav1d_get_bits(&gb, 16); +#if DEBUG_OBU_METADATA + printf("MDCVOBU: white-point-x: %d [off=%td]\n", + mastering_display->white_point[0], + (gb.ptr - init_ptr) * 8 - gb.bits_left); +#endif mastering_display->white_point[1] = dav1d_get_bits(&gb, 16); - +#if DEBUG_OBU_METADATA + printf("MDCVOBU: white-point-y: %d [off=%td]\n", + mastering_display->white_point[1], + (gb.ptr - init_ptr) * 8 - gb.bits_left); +#endif mastering_display->max_luminance = dav1d_get_bits(&gb, 32); +#if DEBUG_OBU_METADATA + printf("MDCVOBU: max-luminance: %d [off=%td]\n", + mastering_display->max_luminance, + (gb.ptr - init_ptr) * 8 - gb.bits_left); +#endif mastering_display->min_luminance = dav1d_get_bits(&gb, 32); - +#if DEBUG_OBU_METADATA + printf("MDCVOBU: min-luminance: %d [off=%td]\n", + mastering_display->min_luminance, + (gb.ptr - init_ptr) * 8 - gb.bits_left); +#endif // Skip the trailing bit, align to the next byte boundary and check for overrun. dav1d_get_bits(&gb, 1); dav1d_bytealign_get_bits(&gb); if (check_for_overrun(c, &gb, init_bit_pos, len)) { dav1d_ref_dec(&ref); diff --git a/third_party/dav1d/src/picture.h b/third_party/dav1d/src/picture.h --- a/third_party/dav1d/src/picture.h +++ b/third_party/dav1d/src/picture.h @@ -50,10 +50,15 @@ // [0] block data (including segmentation map and motion vectors) // [1] pixel data atomic_uint *progress; } Dav1dThreadPicture; +typedef struct Dav1dPictureBuffer { + void *data; + struct Dav1dPictureBuffer *next; +} Dav1dPictureBuffer; + /* * Allocate a picture with custom border size. */ int dav1d_thread_picture_alloc(Dav1dContext *c, Dav1dFrameContext *f, const int bpc); diff --git a/third_party/dav1d/src/picture.c b/third_party/dav1d/src/picture.c --- a/third_party/dav1d/src/picture.c +++ b/third_party/dav1d/src/picture.c @@ -43,11 +43,11 @@ #include "src/ref.h" #include "src/thread.h" #include "src/thread_task.h" int dav1d_default_picture_alloc(Dav1dPicture *const p, void *const cookie) { - assert(cookie == NULL); + assert(sizeof(Dav1dPictureBuffer) <= DAV1D_PICTURE_ALIGNMENT); const int hbd = p->p.bpc > 8; const int aligned_w = (p->p.w + 127) & ~127; const int aligned_h = (p->p.h + 127) & ~127; const int has_chroma = p->p.layout != DAV1D_PIXEL_LAYOUT_I400; const int ss_ver = p->p.layout == DAV1D_PIXEL_LAYOUT_I420; @@ -65,31 +65,51 @@ uv_stride += DAV1D_PICTURE_ALIGNMENT; p->stride[0] = y_stride; p->stride[1] = uv_stride; const size_t y_sz = y_stride * aligned_h; const size_t uv_sz = uv_stride * (aligned_h >> ss_ver); - const size_t pic_size = y_sz + 2 * uv_sz + DAV1D_PICTURE_ALIGNMENT; - uint8_t *const data = dav1d_alloc_aligned(pic_size, DAV1D_PICTURE_ALIGNMENT); - if (!data) return DAV1D_ERR(ENOMEM); + const size_t pic_size = y_sz + 2 * uv_sz; + + /* Pop buffer from the pool. */ + Dav1dContext *const c = cookie; + pthread_mutex_lock(&c->picture_buffer_pool.lock); + Dav1dPictureBuffer *buf = c->picture_buffer_pool.buf; + uint8_t *data; + if (buf) { + c->picture_buffer_pool.buf = buf->next; + pthread_mutex_unlock(&c->picture_buffer_pool.lock); + data = buf->data; + if ((uintptr_t)buf - (uintptr_t)data != pic_size) { + dav1d_free_aligned(data); + goto alloc; + } + } else { + pthread_mutex_unlock(&c->picture_buffer_pool.lock); +alloc: + data = dav1d_alloc_aligned(pic_size + DAV1D_PICTURE_ALIGNMENT, + DAV1D_PICTURE_ALIGNMENT); + if (!data) return DAV1D_ERR(ENOMEM); + buf = (Dav1dPictureBuffer*)(data + pic_size); + buf->data = data; + } + p->allocator_data = buf; p->data[0] = data; p->data[1] = has_chroma ? data + y_sz : NULL; p->data[2] = has_chroma ? data + y_sz + uv_sz : NULL; -#ifndef NDEBUG /* safety check */ - p->allocator_data = data; -#endif - return 0; } void dav1d_default_picture_release(Dav1dPicture *const p, void *const cookie) { - assert(cookie == NULL); -#ifndef NDEBUG /* safety check */ - assert(p->allocator_data == p->data[0]); -#endif - dav1d_free_aligned(p->data[0]); + /* Push buffer to the pool. */ + Dav1dContext *const c = cookie; + Dav1dPictureBuffer *const buf = p->allocator_data; + pthread_mutex_lock(&c->picture_buffer_pool.lock); + buf->next = c->picture_buffer_pool.buf; + c->picture_buffer_pool.buf = buf; + pthread_mutex_unlock(&c->picture_buffer_pool.lock); } struct pic_ctx_context { Dav1dPicAllocator allocator; Dav1dPicture pic; diff --git a/third_party/dav1d/tests/libfuzzer/meson.build b/third_party/dav1d/tests/libfuzzer/meson.build --- a/third_party/dav1d/tests/libfuzzer/meson.build +++ b/third_party/dav1d/tests/libfuzzer/meson.build @@ -70,12 +70,19 @@ kwargs: fuzzer_link_lang ) objcopy = find_program('objcopy', required: false) + +if meson.version().version_compare('<0.56.99') + lto = get_option('b_lto') ? 'true' : 'false' +else + lto = get_option('b_lto') +endif + if (objcopy.found() and - not get_option('b_lto') and + lto == 'false' and get_option('default_library') == 'static' and cc.has_function('posix_memalign', prefix : '#include ', args : test_args)) libdav1d_af = custom_target('libdav1d_af', input: libdav1d,