shithub: dav1d

Download patch

ref: 9f6767199177c554ae05ae4dcbdd9cd5d7cada3c
parent: 7cd946937ae499ba3a966f19c7c4d4544154623a
author: Luc Trudeau <ltrudeau@twoorioles.com>
date: Fri Mar 27 18:48:11 EDT 2020

const correctness in obu.c

--- a/src/obu.c
+++ b/src/obu.c
@@ -85,7 +85,7 @@
             hdr->time_scale = dav1d_get_bits(gb, 32);
             hdr->equal_picture_interval = dav1d_get_bits(gb, 1);
             if (hdr->equal_picture_interval) {
-                unsigned num_ticks_per_picture = dav1d_get_vlc(gb);
+                const unsigned num_ticks_per_picture = dav1d_get_vlc(gb);
                 if (num_ticks_per_picture == 0xFFFFFFFFU)
                     goto error;
                 hdr->num_ticks_per_picture = num_ticks_per_picture + 1;
@@ -111,8 +111,6 @@
         for (int i = 0; i < hdr->num_operating_points; i++) {
             struct Dav1dSequenceHeaderOperatingPoint *const op =
                 &hdr->operating_points[i];
-            struct Dav1dSequenceHeaderOperatingParameterInfo *const opi =
-                &hdr->operating_parameter_info[i];
             op->idc = dav1d_get_bits(gb, 12);
             op->major_level = 2 + dav1d_get_bits(gb, 3);
             op->minor_level = dav1d_get_bits(gb, 2);
@@ -120,6 +118,8 @@
             op->decoder_model_param_present =
                 hdr->decoder_model_info_present && dav1d_get_bits(gb, 1);
             if (op->decoder_model_param_present) {
+                struct Dav1dSequenceHeaderOperatingParameterInfo *const opi =
+                    &hdr->operating_parameter_info[i];
                 opi->decoder_buffer_delay =
                     dav1d_get_bits(gb, hdr->encoder_decoder_buffer_delay_length);
                 opi->encoder_buffer_delay =
@@ -132,10 +132,9 @@
                 op->initial_display_delay = dav1d_get_bits(gb, 4) + 1;
             }
         }
-        if (c->operating_point < hdr->num_operating_points)
-            c->operating_point_idc = hdr->operating_points[c->operating_point].idc;
-        else
-            c->operating_point_idc = hdr->operating_points[0].idc;
+        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",
                dav1d_get_bits_pos(gb) - init_bit_pos);
@@ -295,7 +294,7 @@
     if (use_ref) {
         for (int i = 0; i < 7; i++) {
             if (dav1d_get_bits(gb, 1)) {
-                Dav1dThreadPicture *const ref =
+                const Dav1dThreadPicture *const ref =
                     &c->refs[c->frame_hdr->refidx[i]].p;
                 if (!ref->p.data[0]) return -1;
                 hdr->width[1] = ref->p.p.w;
@@ -343,7 +342,7 @@
     return 0;
 }
 
-static inline int tile_log2(int sz, int tgt) {
+static inline int tile_log2(const int sz, const int tgt) {
     int k;
     for (k = 0; (sz << k) < tgt; k++) ;
     return k;
@@ -362,7 +361,6 @@
 #endif
     const Dav1dSequenceHeader *const seqhdr = c->seq_hdr;
     Dav1dFrameHeader *const hdr = c->frame_hdr;
-    int res;
 
     hdr->show_existing_frame =
         !seqhdr->reduced_still_picture_header && dav1d_get_bits(gb, 1);
@@ -444,7 +442,7 @@
         if (hdr->refresh_frame_flags != 0xff && hdr->error_resilient_mode && seqhdr->order_hint)
             for (int i = 0; i < 8; i++)
                 dav1d_get_bits(gb, seqhdr->order_hint_n_bits);
-        if ((res = read_frame_size(c, gb, 0)) < 0) goto error;
+        if (read_frame_size(c, gb, 0) < 0) goto error;
         hdr->allow_intrabc = hdr->allow_screen_content_tools &&
                              !hdr->super_res.enabled && dav1d_get_bits(gb, 1);
         hdr->use_ref_frame_mvs = 0;
@@ -479,7 +477,7 @@
 
             int latest_frame_offset = -1;
             for (int i = 0; i < 8; i++) {
-                int hint = shifted_frame_offset[i];
+                const int hint = shifted_frame_offset[i];
                 if (!used_frame[i] && hint >= current_frame_offset &&
                     hint >= latest_frame_offset)
                 {
@@ -492,7 +490,7 @@
 
             int earliest_frame_offset = INT_MAX;
             for (int i = 0; i < 8; i++) {
-                int hint = shifted_frame_offset[i];
+                const int hint = shifted_frame_offset[i];
                 if (!used_frame[i] && hint >= current_frame_offset &&
                     hint < earliest_frame_offset)
                 {
@@ -505,7 +503,7 @@
 
             earliest_frame_offset = INT_MAX;
             for (int i = 0; i < 8; i++) {
-                int hint = shifted_frame_offset[i];
+                const int hint = shifted_frame_offset[i];
                 if (!used_frame[i] && hint >= current_frame_offset &&
                     (hint < earliest_frame_offset))
                 {
@@ -520,7 +518,7 @@
                 if (hdr->refidx[i] < 0) {
                     latest_frame_offset = -1;
                     for (int j = 0; j < 8; j++) {
-                        int hint = shifted_frame_offset[j];
+                        const int hint = shifted_frame_offset[j];
                         if (!used_frame[j] && hint < current_frame_offset &&
                             hint >= latest_frame_offset)
                         {
@@ -536,7 +534,7 @@
             earliest_frame_offset = INT_MAX;
             int ref = -1;
             for (int i = 0; i < 8; i++) {
-                int hint = shifted_frame_offset[i];
+                const int hint = shifted_frame_offset[i];
                 if (hint < earliest_frame_offset) {
                     ref = i;
                     earliest_frame_offset = hint;
@@ -555,7 +553,7 @@
         }
         const int use_ref = !hdr->error_resilient_mode &&
                             hdr->frame_size_override;
-        if ((res = read_frame_size(c, gb, use_ref)) < 0) goto error;
+        if (read_frame_size(c, gb, use_ref) < 0) goto error;
         hdr->hp = !hdr->force_integer_mv && dav1d_get_bits(gb, 1);
         hdr->subpel_filter_mode = dav1d_get_bits(gb, 1) ? DAV1D_FILTER_SWITCHABLE :
                                                           dav1d_get_bits(gb, 2);
@@ -579,15 +577,15 @@
     // tile data
     hdr->tiling.uniform = dav1d_get_bits(gb, 1);
     const int sbsz_min1 = (64 << seqhdr->sb128) - 1;
-    int sbsz_log2 = 6 + seqhdr->sb128;
-    int sbw = (hdr->width[0] + sbsz_min1) >> sbsz_log2;
-    int sbh = (hdr->height + sbsz_min1) >> sbsz_log2;
-    int max_tile_width_sb = 4096 >> sbsz_log2;
-    int max_tile_area_sb = 4096 * 2304 >> (2 * sbsz_log2);
+    const int sbsz_log2 = 6 + seqhdr->sb128;
+    const int sbw = (hdr->width[0] + sbsz_min1) >> sbsz_log2;
+    const int sbh = (hdr->height + sbsz_min1) >> sbsz_log2;
+    const int max_tile_width_sb = 4096 >> sbsz_log2;
+    const int max_tile_area_sb = 4096 * 2304 >> (2 * sbsz_log2);
     hdr->tiling.min_log2_cols = tile_log2(max_tile_width_sb, sbw);
     hdr->tiling.max_log2_cols = tile_log2(1, imin(sbw, DAV1D_MAX_TILE_COLS));
     hdr->tiling.max_log2_rows = tile_log2(1, imin(sbh, DAV1D_MAX_TILE_ROWS));
-    int min_log2_tiles = imax(tile_log2(max_tile_area_sb, sbw * sbh),
+    const int min_log2_tiles = imax(tile_log2(max_tile_area_sb, sbw * sbh),
                               hdr->tiling.min_log2_cols);
     if (hdr->tiling.uniform) {
         for (hdr->tiling.log2_cols = hdr->tiling.min_log2_cols;
@@ -621,7 +619,7 @@
         }
         hdr->tiling.log2_cols = tile_log2(1, hdr->tiling.cols);
         if (min_log2_tiles) max_tile_area_sb >>= min_log2_tiles + 1;
-        int max_tile_height_sb = imax(max_tile_area_sb / widest_tile, 1);
+        const int max_tile_height_sb = imax(max_tile_area_sb / widest_tile, 1);
 
         hdr->tiling.rows = 0;
         for (int sby = 0; sby < sbh && hdr->tiling.rows < DAV1D_MAX_TILE_ROWS; hdr->tiling.rows++) {
@@ -657,7 +655,7 @@
         // If the sequence header says that delta_q might be different
         // for U, V, we must check whether it actually is for this
         // frame.
-        int diff_uv_delta = seqhdr->separate_uv_delta_q ? dav1d_get_bits(gb, 1) : 0;
+        const int diff_uv_delta = seqhdr->separate_uv_delta_q ? dav1d_get_bits(gb, 1) : 0;
         hdr->quant.udc_delta = dav1d_get_bits(gb, 1) ? dav1d_get_sbits(gb, 6) : 0;
         hdr->quant.uac_delta = dav1d_get_bits(gb, 1) ? dav1d_get_sbits(gb, 6) : 0;
         if (diff_uv_delta) {
@@ -1053,7 +1051,7 @@
             for (i = 0; i < 7; i++)
                 if (hdr->refidx[i] == refidx)
                     break;
-            if (i == 7 || !c->refs[refidx].p.p.frame_hdr)  goto error;
+            if (i == 7 || !c->refs[refidx].p.p.frame_hdr) goto error;
             hdr->film_grain.data = c->refs[refidx].p.p.frame_hdr->film_grain.data;
             hdr->film_grain.data.seed = seed;
         } else {
@@ -1133,10 +1131,8 @@
 }
 
 static void parse_tile_hdr(Dav1dContext *const c, GetBits *const gb) {
-    int have_tile_pos = 0;
     const int n_tiles = c->frame_hdr->tiling.cols * c->frame_hdr->tiling.rows;
-    if (n_tiles > 1)
-        have_tile_pos = dav1d_get_bits(gb, 1);
+    const int have_tile_pos = n_tiles > 1 ? dav1d_get_bits(gb, 1) : 0;
 
     if (have_tile_pos) {
         const int n_bits = c->frame_hdr->tiling.log2_cols +
@@ -1151,9 +1147,9 @@
 
 // Check that we haven't read more than obu_len bytes from the buffer
 // since init_bit_pos.
-static int
-check_for_overrun(Dav1dContext *const c, GetBits *const gb,
-                  unsigned init_bit_pos, unsigned obu_len)
+static int check_for_overrun(Dav1dContext *const c, GetBits *const gb,
+                             const unsigned init_bit_pos,
+                             const unsigned obu_len)
 {
     // Make sure we haven't actually read past the end of the gb buffer
     if (gb->error) {
@@ -1161,7 +1157,7 @@
         return 1;
     }
 
-    unsigned pos = dav1d_get_bits_pos(gb);
+    const unsigned pos = dav1d_get_bits_pos(gb);
 
     // We assume that init_bit_pos was the bit position of the buffer
     // at some point in the past, so cannot be smaller than pos.
@@ -1175,7 +1171,7 @@
     return 0;
 }
 
-int dav1d_parse_obus(Dav1dContext *const c, Dav1dData *const in, int global) {
+int dav1d_parse_obus(Dav1dContext *const c, Dav1dData *const in, const int global) {
     GetBits gb;
     int res;
 
@@ -1196,11 +1192,8 @@
     }
 
     // obu length field
-    unsigned len = 0;
-    if (has_length_field)
-        len = dav1d_get_uleb128(&gb);
-    else
-        len = (int) in->sz - 1 - has_extension;
+    const unsigned len = has_length_field ?
+        dav1d_get_uleb128(&gb) : (unsigned) in->sz - 1 - has_extension;
     if (gb.error) goto error;
 
     const unsigned init_bit_pos = dav1d_get_bits_pos(&gb);
@@ -1442,7 +1435,7 @@
             payload_size -= meta_type_len;
 
             int country_code_extension_byte = 0;
-            int country_code = dav1d_get_bits(&gb, 8);
+            const int country_code = dav1d_get_bits(&gb, 8);
             payload_size--;
             if (country_code == 0xFF) {
                 country_code_extension_byte = dav1d_get_bits(&gb, 8);