Make decoder_model_info match the spec better.

Rename the following struct members and function to match their names in
the spec:
  buffer_removal_delay_present => buffer_removal_time_present
  buffer_removal_delay => buffer_removal_time
  tu_presentation_delay => frame_presentation_time
  frame_presentation_delay_length => frame_presentation_time_length
  av1_read_tu_pts_info => av1_read_temporal_point_info

Declare the frame_presentation_time member of struct AV1Common as
uint32_t because it is at most 32 bits long. For the same reason,
declare the buffer_removal_time member of struct aom_op_timing_info_t as
uint32_t. This allows us to remove the uint32_t casts on these members
when we pass them to aom_wb_write_unsigned_literal(). Add checks to
ensure the values of these members do not overflow uint32_t.

Remove the unused tu_presentation_delay_flag member of struct AV1Common.

Change-Id: Id50dd3dd2892d1a9a99b7d469c244a99141e19e7
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index a0af8b8..65949c9 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -464,7 +464,7 @@
     oxcf->buffer_model.num_units_in_decoding_tick = cfg->g_timebase.num;
     oxcf->timing_info.equal_picture_interval = 0;
     oxcf->decoder_model_info_present_flag = 1;
-    oxcf->buffer_removal_delay_present = 1;
+    oxcf->buffer_removal_time_present = 1;
     oxcf->display_model_info_present_flag = 1;
   }
   if (oxcf->init_framerate > 180) {
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index 20229bd..b618fd8 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -253,12 +253,11 @@
   int last_height;
   int timing_info_present;
   aom_timing_info_t timing_info;
-  int buffer_removal_delay_present;
+  int buffer_removal_time_present;
   aom_dec_model_info_t buffer_model;
   aom_dec_model_op_parameters_t op_params[MAX_NUM_OPERATING_POINTS + 1];
   aom_op_timing_info_t op_frame_timing[MAX_NUM_OPERATING_POINTS + 1];
-  int tu_presentation_delay_flag;
-  int64_t tu_presentation_delay;
+  uint32_t frame_presentation_time;
 
   // TODO(jkoleszar): this implies chroma ss right now, but could vary per
   // plane. Revisit as part of the future change to YV12_BUFFER_CONFIG to
diff --git a/av1/common/timing.c b/av1/common/timing.c
index 5ff538a..49dbde7 100644
--- a/av1/common/timing.c
+++ b/av1/common/timing.c
@@ -53,8 +53,8 @@
 
 void set_aom_dec_model_info(aom_dec_model_info_t *decoder_model) {
   decoder_model->encoder_decoder_buffer_delay_length = 16;
-  decoder_model->buffer_removal_delay_length = 10;
-  decoder_model->frame_presentation_delay_length = 10;
+  decoder_model->buffer_removal_time_length = 10;
+  decoder_model->frame_presentation_time_length = 10;
 }
 
 void set_dec_model_op_parameters(aom_dec_model_op_parameters_t *op_params) {
diff --git a/av1/common/timing.h b/av1/common/timing.h
index 2562951..1749baa 100644
--- a/av1/common/timing.h
+++ b/av1/common/timing.h
@@ -27,8 +27,8 @@
 typedef struct aom_dec_model_info {
   uint32_t num_units_in_decoding_tick;
   int encoder_decoder_buffer_delay_length;
-  int buffer_removal_delay_length;
-  int frame_presentation_delay_length;
+  int buffer_removal_time_length;
+  int frame_presentation_time_length;
 } aom_dec_model_info_t;
 
 typedef struct aom_dec_model_op_parameters {
@@ -43,7 +43,7 @@
 } aom_dec_model_op_parameters_t;
 
 typedef struct aom_op_timing_info_t {
-  int64_t buffer_removal_delay;
+  uint32_t buffer_removal_time;
 } aom_op_timing_info_t;
 
 void set_aom_dec_model_info(aom_dec_model_info_t *decoder_model);
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 4c9120b..3fc9168 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -3375,8 +3375,8 @@
       aom_rb_read_literal(rb, 5) + 1;
   cm->buffer_model.num_units_in_decoding_tick = aom_rb_read_unsigned_literal(
       rb, 32);  // Number of units in a decoding tick
-  cm->buffer_model.buffer_removal_delay_length = aom_rb_read_literal(rb, 5) + 1;
-  cm->buffer_model.frame_presentation_delay_length =
+  cm->buffer_model.buffer_removal_time_length = aom_rb_read_literal(rb, 5) + 1;
+  cm->buffer_model.frame_presentation_time_length =
       aom_rb_read_literal(rb, 5) + 1;
 }
 
@@ -3398,10 +3398,10 @@
   cm->op_params[op_num].low_delay_mode_flag = aom_rb_read_bit(rb);
 }
 
-static void av1_read_tu_pts_info(AV1_COMMON *const cm,
-                                 struct aom_read_bit_buffer *rb) {
-  cm->tu_presentation_delay = aom_rb_read_unsigned_literal(
-      rb, cm->buffer_model.frame_presentation_delay_length);
+static void av1_read_temporal_point_info(AV1_COMMON *const cm,
+                                         struct aom_read_bit_buffer *rb) {
+  cm->frame_presentation_time = aom_rb_read_unsigned_literal(
+      rb, cm->buffer_model.frame_presentation_time_length);
 }
 
 void read_sequence_header(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
@@ -3710,7 +3710,7 @@
       const int frame_to_show = cm->ref_frame_map[existing_frame_idx];
       if (cm->seq_params.decoder_model_info_present_flag &&
           cm->timing_info.equal_picture_interval == 0) {
-        av1_read_tu_pts_info(cm, rb);
+        av1_read_temporal_point_info(cm, rb);
       }
       if (cm->seq_params.frame_id_numbers_present_flag) {
         int frame_id_length = cm->seq_params.frame_id_length;
@@ -3765,7 +3765,7 @@
     if (cm->show_frame) {
       if (cm->seq_params.decoder_model_info_present_flag &&
           cm->timing_info.equal_picture_interval == 0)
-        av1_read_tu_pts_info(cm, rb);
+        av1_read_temporal_point_info(cm, rb);
     } else {
       // See if this frame can be used as show_existing_frame in future
       cm->showable_frame = aom_rb_read_bit(rb);
@@ -3857,8 +3857,8 @@
   }
 
   if (cm->seq_params.decoder_model_info_present_flag) {
-    cm->buffer_removal_delay_present = aom_rb_read_bit(rb);
-    if (cm->buffer_removal_delay_present) {
+    cm->buffer_removal_time_present = aom_rb_read_bit(rb);
+    if (cm->buffer_removal_time_present) {
       for (int op_num = 0;
            op_num < cm->seq_params.operating_points_cnt_minus_1 + 1; op_num++) {
         if (cm->op_params[op_num].decoder_model_param_present_flag) {
@@ -3869,14 +3869,14 @@
                  (cm->spatial_layer_id + 8)) &
                 0x1)) ||
               cm->seq_params.operating_point_idc[op_num] == 0) {
-            cm->op_frame_timing[op_num].buffer_removal_delay =
+            cm->op_frame_timing[op_num].buffer_removal_time =
                 aom_rb_read_unsigned_literal(
-                    rb, cm->buffer_model.buffer_removal_delay_length);
+                    rb, cm->buffer_model.buffer_removal_time_length);
           } else {
-            cm->op_frame_timing[op_num].buffer_removal_delay = 0;
+            cm->op_frame_timing[op_num].buffer_removal_time = 0;
           }
         } else {
-          cm->op_frame_timing[op_num].buffer_removal_delay = 0;
+          cm->op_frame_timing[op_num].buffer_removal_time = 0;
         }
       }
     }
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 4435909..16fada3 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -2517,8 +2517,8 @@
       wb, cm->buffer_model.encoder_decoder_buffer_delay_length - 1, 5);
   aom_wb_write_unsigned_literal(wb, cm->buffer_model.num_units_in_decoding_tick,
                                 32);  // Number of units in decoding tick
-  aom_wb_write_literal(wb, cm->buffer_model.buffer_removal_delay_length - 1, 5);
-  aom_wb_write_literal(wb, cm->buffer_model.frame_presentation_delay_length - 1,
+  aom_wb_write_literal(wb, cm->buffer_model.buffer_removal_time_length - 1, 5);
+  aom_wb_write_literal(wb, cm->buffer_model.frame_presentation_time_length - 1,
                        5);
 }
 
@@ -2543,15 +2543,15 @@
 
   aom_wb_write_bit(wb, cm->op_params[op_num].low_delay_mode_flag);
 
-  cm->op_frame_timing[op_num].buffer_removal_delay =
+  cm->op_frame_timing[op_num].buffer_removal_time =
       0;  // reset the decoded frame counter
 }
 
 static void write_tu_pts_info(AV1_COMMON *const cm,
                               struct aom_write_bit_buffer *wb) {
   aom_wb_write_unsigned_literal(
-      wb, (uint32_t)cm->tu_presentation_delay,
-      cm->buffer_model.frame_presentation_delay_length);
+      wb, cm->frame_presentation_time,
+      cm->buffer_model.frame_presentation_time_length);
 }
 
 static void write_film_grain_params(AV1_COMP *cpi,
@@ -3051,8 +3051,8 @@
   }
 
   if (cm->seq_params.decoder_model_info_present_flag) {
-    aom_wb_write_bit(wb, cm->buffer_removal_delay_present);
-    if (cm->buffer_removal_delay_present) {
+    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++) {
         if (cm->op_params[op_num].decoder_model_param_present_flag) {
@@ -3063,10 +3063,14 @@
                 (cm->spatial_layer_id + 8)) &
                    0x1) ||
               cm->seq_params.operating_point_idc[op_num] == 0) {
-            aom_wb_write_literal(
-                wb, (uint32_t)cm->op_frame_timing[op_num].buffer_removal_delay,
-                cm->buffer_model.buffer_removal_delay_length);
-            cm->op_frame_timing[op_num].buffer_removal_delay++;
+            aom_wb_write_unsigned_literal(
+                wb, cm->op_frame_timing[op_num].buffer_removal_time,
+                cm->buffer_model.buffer_removal_time_length);
+            cm->op_frame_timing[op_num].buffer_removal_time++;
+            if (cm->op_frame_timing[op_num].buffer_removal_time == 0) {
+              aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
+                                 "buffer_removal_time overflowed");
+            }
           }
         }
       }
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 878fb7e..3d7f0a3 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -1033,7 +1033,7 @@
     // set the decoder model parameters in schedule mode
     cm->buffer_model.num_units_in_decoding_tick =
         oxcf->buffer_model.num_units_in_decoding_tick;
-    cm->buffer_removal_delay_present = 1;
+    cm->buffer_removal_time_present = 1;
     set_aom_dec_model_info(&cm->buffer_model);
     set_dec_model_op_parameters(&cm->op_params[0]);
   } else if (cm->timing_info_present &&
@@ -2286,7 +2286,7 @@
     // set the decoder model parameters in schedule mode
     cm->buffer_model.num_units_in_decoding_tick =
         oxcf->buffer_model.num_units_in_decoding_tick;
-    cm->buffer_removal_delay_present = 1;
+    cm->buffer_removal_time_present = 1;
     set_aom_dec_model_info(&cm->buffer_model);
     set_dec_model_op_parameters(&cm->op_params[0]);
   } else if (cm->timing_info_present &&
@@ -5911,8 +5911,9 @@
   cm->cur_frame->film_grain_params_present = cm->film_grain_params_present;
 
   // only one operating point supported now
-  cpi->common.tu_presentation_delay =
-      ticks_to_timebase_units(timebase, *time_stamp);
+  const int64_t pts64 = ticks_to_timebase_units(timebase, *time_stamp);
+  if (pts64 < 0 || pts64 > UINT32_MAX) return AOM_CODEC_ERROR;
+  cpi->common.frame_presentation_time = (uint32_t)pts64;
 
   // Start with a 0 size frame.
   *size = 0;
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 2c018fb..7e2e583 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -277,7 +277,7 @@
   aom_timing_info_t timing_info;
   int decoder_model_info_present_flag;
   int display_model_info_present_flag;
-  int buffer_removal_delay_present;
+  int buffer_removal_time_present;
   aom_dec_model_info_t buffer_model;
   aom_dec_model_op_parameters_t op_params[MAX_NUM_OPERATING_POINTS + 1];
   aom_op_timing_info_t op_frame_timing[MAX_NUM_OPERATING_POINTS + 1];