Move film_grain_params_present to SequenceHeader.

Cherry-picked from aom/master:
https://aomedia.googlesource.com/aom/+/8d5a4ba1422cc8835f5d0e81af397deb93c759a2

This matches the spec, and enforces that it's value doesn't change
across multiple sequence headers in the same video.

BUG=aomedia:1875

Change-Id: Ic84da452d8a2ec72891ea0eb582c648ac8cb7d04
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index 32bffea..85298dc 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -255,6 +255,8 @@
   int subsampling_y;          // Chroma subsampling for y
   aom_chroma_sample_position_t chroma_sample_position;
   int separate_uv_delta_q;
+
+  int film_grain_params_present;
 } SequenceHeader;
 
 typedef struct AV1Common {
@@ -494,7 +496,6 @@
   ENTROPY_CONTEXT **above_context[MAX_MB_PLANE];
   TXFM_CONTEXT **above_txfm_context;
   WarpedMotionParams global_motion[REF_FRAMES];
-  int film_grain_params_present;
   aom_film_grain_t film_grain_params;
 
   int cdef_pri_damping;
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index f2e93ba..31e998f 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -3994,7 +3994,8 @@
 }
 
 static void read_film_grain(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
-  if (cm->film_grain_params_present && (cm->show_frame || cm->showable_frame)) {
+  if (cm->seq_params.film_grain_params_present &&
+      (cm->show_frame || cm->showable_frame)) {
     av1_read_film_grain_params(cm, rb);
   } else {
     memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
@@ -4689,8 +4690,7 @@
                                "Failed to allocate frame buffer");
           }
           unlock_buffer_pool(pool);
-          set_planes_to_neutral_grey(&cm->seq_params, &frame_bufs[buf_idx].buf,
-                                     0);
+          set_planes_to_neutral_grey(seq_params, &frame_bufs[buf_idx].buf, 0);
 
           cm->ref_frame_map[ref_idx] = buf_idx;
           frame_bufs[buf_idx].cur_frame_offset = frame_offset;
@@ -4710,7 +4710,8 @@
     cm->allow_ref_frame_mvs = 0;
 
     if (cm->intra_only) {
-      cm->cur_frame->film_grain_params_present = cm->film_grain_params_present;
+      cm->cur_frame->film_grain_params_present =
+          seq_params->film_grain_params_present;
       setup_frame_size(cm, frame_size_override_flag, rb);
       if (cm->allow_screen_content_tools && !av1_superres_scaled(cm))
         cm->allow_intrabc = aom_rb_read_bit(rb);
@@ -5011,7 +5012,8 @@
 
   if (!frame_is_intra_only(cm)) read_global_motion(cm, rb);
 
-  cm->cur_frame->film_grain_params_present = cm->film_grain_params_present;
+  cm->cur_frame->film_grain_params_present =
+      seq_params->film_grain_params_present;
   read_film_grain(cm, rb);
 
 #if EXT_TILE_DEBUG
diff --git a/av1/decoder/obu.c b/av1/decoder/obu.c
index 2c5ec85..03d43a2 100644
--- a/av1/decoder/obu.c
+++ b/av1/decoder/obu.c
@@ -339,7 +339,7 @@
                        seq_params->subsampling_x, seq_params->subsampling_y);
   }
 
-  cm->film_grain_params_present = aom_rb_read_bit(rb);
+  seq_params->film_grain_params_present = aom_rb_read_bit(rb);
 
   if (av1_check_trailing_bits(pbi, rb) != 0) {
     // cm->error.error_code is already set.
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index c246c1a..2070755 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -2941,18 +2941,19 @@
                                           struct aom_write_bit_buffer *saved_wb,
                                           struct aom_write_bit_buffer *wb) {
   AV1_COMMON *const cm = &cpi->common;
+  const SequenceHeader *const seq_params = &cm->seq_params;
   MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
 
   // NOTE: By default all coded frames to be used as a reference
   cm->is_reference_frame = 1;
   cm->frame_type = cm->intra_only ? INTRA_ONLY_FRAME : cm->frame_type;
 
-  if (cm->seq_params.still_picture) {
+  if (seq_params->still_picture) {
     assert(cm->show_existing_frame == 0);
     assert(cm->show_frame == 1);
     assert(cm->frame_type == KEY_FRAME);
   }
-  if (!cm->seq_params.reduced_still_picture_hdr) {
+  if (!seq_params->reduced_still_picture_hdr) {
     if (cm->show_existing_frame) {
       RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
       const int frame_to_show = cm->ref_frame_map[cpi->existing_fb_idx_to_show];
@@ -2967,12 +2968,12 @@
       aom_wb_write_bit(wb, 1);  // show_existing_frame
       aom_wb_write_literal(wb, cpi->existing_fb_idx_to_show, 3);
 
-      if (cm->seq_params.decoder_model_info_present_flag &&
+      if (seq_params->decoder_model_info_present_flag &&
           cm->timing_info.equal_picture_interval == 0) {
         write_tu_pts_info(cm, wb);
       }
-      if (cm->seq_params.frame_id_numbers_present_flag) {
-        int frame_id_len = cm->seq_params.frame_id_length;
+      if (seq_params->frame_id_numbers_present_flag) {
+        int frame_id_len = seq_params->frame_id_length;
         int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
         aom_wb_write_literal(wb, display_frame_id, frame_id_len);
       }
@@ -2993,7 +2994,7 @@
 
     aom_wb_write_bit(wb, cm->show_frame);
     if (cm->show_frame) {
-      if (cm->seq_params.decoder_model_info_present_flag &&
+      if (seq_params->decoder_model_info_present_flag &&
           cm->timing_info.equal_picture_interval == 0)
         write_tu_pts_info(cm, wb);
     } else {
@@ -3007,18 +3008,18 @@
   }
   aom_wb_write_bit(wb, cm->disable_cdf_update);
 
-  if (cm->seq_params.force_screen_content_tools == 2) {
+  if (seq_params->force_screen_content_tools == 2) {
     aom_wb_write_bit(wb, cm->allow_screen_content_tools);
   } else {
     assert(cm->allow_screen_content_tools ==
-           cm->seq_params.force_screen_content_tools);
+           seq_params->force_screen_content_tools);
   }
 
   if (cm->allow_screen_content_tools) {
-    if (cm->seq_params.force_integer_mv == 2) {
+    if (seq_params->force_integer_mv == 2) {
       aom_wb_write_bit(wb, cm->cur_frame_force_integer_mv);
     } else {
-      assert(cm->cur_frame_force_integer_mv == cm->seq_params.force_integer_mv);
+      assert(cm->cur_frame_force_integer_mv == seq_params->force_integer_mv);
     }
   } else {
     assert(cm->cur_frame_force_integer_mv == 0);
@@ -3028,49 +3029,49 @@
   int frame_size_override_flag = 0;
   cm->frame_refs_short_signaling = 0;
 
-  if (cm->seq_params.reduced_still_picture_hdr) {
-    assert(cm->width == cm->seq_params.max_frame_width &&
-           cm->height == cm->seq_params.max_frame_height);
+  if (seq_params->reduced_still_picture_hdr) {
+    assert(cm->width == seq_params->max_frame_width &&
+           cm->height == seq_params->max_frame_height);
   } else {
-    if (cm->seq_params.frame_id_numbers_present_flag) {
-      int frame_id_len = cm->seq_params.frame_id_length;
+    if (seq_params->frame_id_numbers_present_flag) {
+      int frame_id_len = seq_params->frame_id_length;
       aom_wb_write_literal(wb, cm->current_frame_id, frame_id_len);
     }
 
-    if (cm->width > cm->seq_params.max_frame_width ||
-        cm->height > cm->seq_params.max_frame_height) {
+    if (cm->width > seq_params->max_frame_width ||
+        cm->height > seq_params->max_frame_height) {
       aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
                          "Frame dimensions are larger than the maximum values");
     }
 
     frame_size_override_flag =
         frame_is_sframe(cm) ? 1
-                            : (cm->width != cm->seq_params.max_frame_width ||
-                               cm->height != cm->seq_params.max_frame_height);
+                            : (cm->width != seq_params->max_frame_width ||
+                               cm->height != seq_params->max_frame_height);
     if (!frame_is_sframe(cm)) aom_wb_write_bit(wb, frame_size_override_flag);
 
-    if (cm->seq_params.enable_order_hint)
+    if (seq_params->enable_order_hint)
       aom_wb_write_literal(wb, cm->frame_offset,
-                           cm->seq_params.order_hint_bits_minus_1 + 1);
+                           seq_params->order_hint_bits_minus_1 + 1);
 
     if (!cm->error_resilient_mode && !frame_is_intra_only(cm)) {
       aom_wb_write_literal(wb, cm->primary_ref_frame, PRIMARY_REF_BITS);
     }
   }
 
-  if (cm->seq_params.decoder_model_info_present_flag) {
+  if (seq_params->decoder_model_info_present_flag) {
     aom_wb_write_bit(wb, cm->buffer_removal_time_present);
     if (cm->buffer_removal_time_present) {
       for (int op_num = 0;
-           op_num < cm->seq_params.operating_points_cnt_minus_1 + 1; op_num++) {
+           op_num < seq_params->operating_points_cnt_minus_1 + 1; op_num++) {
         if (cm->op_params[op_num].decoder_model_param_present_flag) {
-          if (((cm->seq_params.operating_point_idc[op_num] >>
+          if (((seq_params->operating_point_idc[op_num] >>
                 cm->temporal_layer_id) &
                    0x1 &&
-               (cm->seq_params.operating_point_idc[op_num] >>
+               (seq_params->operating_point_idc[op_num] >>
                 (cm->spatial_layer_id + 8)) &
                    0x1) ||
-              cm->seq_params.operating_point_idc[op_num] == 0) {
+              seq_params->operating_point_idc[op_num] == 0) {
             aom_wb_write_unsigned_literal(
                 wb, cm->op_frame_timing[op_num].buffer_removal_time,
                 cm->buffer_model.buffer_removal_time_length);
@@ -3136,7 +3137,7 @@
 
   if (!frame_is_intra_only(cm) || cpi->refresh_frame_mask != 0xFF) {
     // Write all ref frame order hints if error_resilient_mode == 1
-    if (cm->error_resilient_mode && cm->seq_params.enable_order_hint) {
+    if (cm->error_resilient_mode && seq_params->enable_order_hint) {
       RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
       for (int ref_idx = 0; ref_idx < REF_FRAMES; ref_idx++) {
         // Get buffer index
@@ -3145,7 +3146,7 @@
 
         // Write order hint to bit stream
         aom_wb_write_literal(wb, frame_bufs[buf_idx].cur_frame_offset,
-                             cm->seq_params.order_hint_bits_minus_1 + 1);
+                             seq_params->order_hint_bits_minus_1 + 1);
       }
     }
   }
@@ -3170,7 +3171,7 @@
       //       automatically.
 #define FRAME_REFS_SHORT_SIGNALING 0
 #if FRAME_REFS_SHORT_SIGNALING
-      cm->frame_refs_short_signaling = cm->seq_params.enable_order_hint;
+      cm->frame_refs_short_signaling = seq_params->enable_order_hint;
 #endif  // FRAME_REFS_SHORT_SIGNALING
 
       if (cm->frame_refs_short_signaling) {
@@ -3181,7 +3182,7 @@
         check_frame_refs_short_signaling(cpi);
       }
 
-      if (cm->seq_params.enable_order_hint)
+      if (seq_params->enable_order_hint)
         aom_wb_write_bit(wb, cm->frame_refs_short_signaling);
 
       if (cm->frame_refs_short_signaling) {
@@ -3197,10 +3198,10 @@
         if (!cm->frame_refs_short_signaling)
           aom_wb_write_literal(wb, get_ref_frame_map_idx(cpi, ref_frame),
                                REF_FRAMES_LOG2);
-        if (cm->seq_params.frame_id_numbers_present_flag) {
+        if (seq_params->frame_id_numbers_present_flag) {
           int i = get_ref_frame_map_idx(cpi, ref_frame);
-          int frame_id_len = cm->seq_params.frame_id_length;
-          int diff_len = cm->seq_params.delta_frame_id_length;
+          int frame_id_len = seq_params->frame_id_length;
+          int diff_len = seq_params->delta_frame_id_length;
           int delta_frame_id_minus_1 =
               ((cm->current_frame_id - cm->ref_frame_id[i] +
                 (1 << frame_id_len)) %
@@ -3236,7 +3237,7 @@
   }
 
   const int might_bwd_adapt =
-      !(cm->seq_params.reduced_still_picture_hdr) && !(cm->disable_cdf_update);
+      !(seq_params->reduced_still_picture_hdr) && !(cm->disable_cdf_update);
   if (cm->large_scale_tile)
     cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
 
@@ -3296,7 +3297,8 @@
 
   if (!frame_is_intra_only(cm)) write_global_motion(cpi, wb);
 
-  if (cm->film_grain_params_present && (cm->show_frame || cm->showable_frame)) {
+  if (seq_params->film_grain_params_present &&
+      (cm->show_frame || cm->showable_frame)) {
     int flip_back_update_parameters_flag = 0;
     if (cm->frame_type != INTER_FRAME &&
         cm->film_grain_params.update_parameters == 0) {
@@ -3567,7 +3569,7 @@
 
   write_color_config(&cm->seq_params, &wb);
 
-  aom_wb_write_bit(&wb, cm->film_grain_params_present);
+  aom_wb_write_bit(&wb, cm->seq_params.film_grain_params_present);
 
   add_trailing_bits(&wb);
 
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 14d0412..25b84fc 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -455,7 +455,7 @@
   }
 
   if (oxcf->film_grain_test_vector) {
-    cm->film_grain_params_present = 1;
+    cm->seq_params.film_grain_params_present = 1;
     if (cm->frame_type == KEY_FRAME) {
       memcpy(&cm->film_grain_params,
              film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
@@ -473,7 +473,7 @@
     aom_film_grain_table_read(cpi->film_grain_table,
                               oxcf->film_grain_table_filename, &cm->error);
   } else {
-    cm->film_grain_params_present = 0;
+    cm->seq_params.film_grain_params_present = 0;
     memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
   }
 }
@@ -6081,11 +6081,12 @@
   cm->cur_frame->buf.buf_8bit_valid = 0;
 
   if (cpi->film_grain_table) {
-    cm->film_grain_params_present = aom_film_grain_table_lookup(
+    cm->seq_params.film_grain_params_present = aom_film_grain_table_lookup(
         cpi->film_grain_table, *time_stamp, *time_end, 0 /* =erase */,
         &cm->film_grain_params);
   }
-  cm->cur_frame->film_grain_params_present = cm->film_grain_params_present;
+  cm->cur_frame->film_grain_params_present =
+      cm->seq_params.film_grain_params_present;
 
   // only one operating point supported now
   const int64_t pts64 = ticks_to_timebase_units(timebase, *time_stamp);