enable random access at the decoder

set random_access true when random_access_point_index = random-access-point-index.
the first CLK is considered as random_access_point_index=0.

1. Fixes encoder not encoding the sequence header for OLKs
2. Fixes encoder not encoding all the frames when --enable-fwd-kf=1

Fixes #1444
diff --git a/apps/avmdec.c b/apps/avmdec.c
index 177f4f1..1e77641 100644
--- a/apps/avmdec.c
+++ b/apps/avmdec.c
@@ -130,8 +130,8 @@
 static const arg_def_t skipfilmgrain =
     ARG_DEF(NULL, "skip-film-grain", 0, "Skip film grain application");
 static const arg_def_t randomaccess =
-    ARG_DEF(NULL, "random-access", 0,
-            "Random access to random-access-th random access point");
+    ARG_DEF(NULL, "random-access-point-index", 1,
+            "Start decoding at the N-th random access point");
 static const arg_def_t bruoptmodearg =
     ARG_DEF(NULL, "bru-opt-mode", 0, "Use BRU optimized decode mode");
 static const arg_def_t icc_file =
diff --git a/av2/av2_dx_iface.c b/av2/av2_dx_iface.c
index 7252352..54108a5 100644
--- a/av2/av2_dx_iface.c
+++ b/av2/av2_dx_iface.c
@@ -112,6 +112,7 @@
 
     priv->selected_ops_id = -1;
     priv->selected_op_index = -1;
+    priv->random_access_point_index = -1;
     priv->enable_sub_bitstream_extraction = 0;
     priv->num_local_ops_selections = 0;
 
@@ -845,6 +846,7 @@
   // loop with while() to make the test decoder in the encoder works same as a
   // standalone decoder.
   struct AV2Decoder *pbi = frame_worker_data->pbi;
+  bool ra_counted_in_tu = false;
   while (data_start < data_end) {
     int xlayer_id = -1;
     int mlayer_id = -1;  // vcl obu's mlayer_id
@@ -855,32 +857,58 @@
     if (frame_unit_size == 0 || frame_unit_size == SIZE_MAX) {
       return AVM_CODEC_ERROR;
     }
-    if (pbi->obus_in_frame_unit_data[0][0][OBU_TEMPORAL_DELIMITER])
+    if (pbi->obus_in_frame_unit_data[0][0][OBU_TEMPORAL_DELIMITER]) {
       pbi->last_decoded_xlayer_id = -1;
+      ra_counted_in_tu = false;
+    }
     bool has_key_obu =
         pbi->obus_in_frame_unit_data[tlayer_id][mlayer_id]
                                     [OBU_CLOSED_LOOP_KEY] ||
-        pbi->obus_in_frame_unit_data[tlayer_id][mlayer_id][OBU_OPEN_LOOP_KEY];
+        pbi->obus_in_frame_unit_data[tlayer_id][mlayer_id][OBU_OPEN_LOOP_KEY] ||
+        pbi->obus_in_frame_unit_data[tlayer_id][mlayer_id][OBU_RAS_FRAME];
     pbi->current_mlayer_id = mlayer_id;
     pbi->current_tlayer_id = tlayer_id;
 
     // droping leading obus if neccessary
-    // pbi->random_accessed is set in main_loop() when parsing begins at a
-    // random access point
+    // pbi->random_accessed is set when random_access_point_count reaches
+    // random_access_point_index
     bool has_leading_frame =
         pbi->obus_in_frame_unit_data[tlayer_id][mlayer_id]
                                     [OBU_LEADING_TILE_GROUP] ||
         pbi->obus_in_frame_unit_data[tlayer_id][mlayer_id][OBU_LEADING_SEF] ||
         pbi->obus_in_frame_unit_data[tlayer_id][mlayer_id][OBU_LEADING_TIP];
-    if (pbi->random_accessed && has_leading_frame) {
+
+    bool is_target_rap =
+        (pbi->random_access_point_count - 1 == pbi->random_access_point_index);
+
+    if (pbi->random_accessed && has_leading_frame && is_target_rap) {
       data_start += frame_unit_size;
       continue;
-    } else if ((pbi->random_accessed && !has_key_obu) ||
-               pbi->obus_in_frame_unit_data[tlayer_id][mlayer_id]
+    } else if (pbi->random_accessed && !has_key_obu && !has_leading_frame) {
+      pbi->random_accessed = false;
+    } else if (pbi->obus_in_frame_unit_data[tlayer_id][mlayer_id]
                                            [OBU_CLOSED_LOOP_KEY]) {
       pbi->random_accessed = false;
     }
 
+    // Count random access points and derive random_accessed (0-based).
+    // This runs after the reset logic above so that random_accessed
+    // persists from the key OBU into subsequent frame units.
+    if (has_key_obu && !ra_counted_in_tu) {
+      ra_counted_in_tu = true;
+      if (pbi->random_access_point_count == pbi->random_access_point_index) {
+        pbi->random_accessed = true;
+      }
+      pbi->random_access_point_count++;
+    }
+
+    // Skip all frame units before the target random access point
+    if (pbi->random_access_point_index >= 0 &&
+        pbi->random_access_point_count <= pbi->random_access_point_index) {
+      data_start += frame_unit_size;
+      continue;
+    }
+
     // Reset last_frame_unit and last_displayable_frame_unit for this xlayer
     // when a keyframe is present in a TU (identified by the TD). In a combined
     // TU the TD is only in the first frame unit, so last_decoded_xlayer_id
diff --git a/av2/decoder/decoder.h b/av2/decoder/decoder.h
index bd3001c..242c1cf 100644
--- a/av2/decoder/decoder.h
+++ b/av2/decoder/decoder.h
@@ -606,8 +606,8 @@
   /*!
    * A decoder option to start decoding from the random_access_point_index-th
    * random access point. random_accessed is set to be true when
-   * random_access_point_index is equal to random_access_point_count   * when
-   * not used, the default value is -1
+   * random_access_point_index is equal to random_access_point_count.
+   * When not used, the default value is -1
    */
   int64_t random_access_point_index;
   /*!
diff --git a/av2/encoder/bitstream.c b/av2/encoder/bitstream.c
index ba3bdf9..dd2c4ac 100644
--- a/av2/encoder/bitstream.c
+++ b/av2/encoder/bitstream.c
@@ -7017,10 +7017,10 @@
     }
   }
 
-  // write sequence header obu if obu type is CLK, preceded by 4-byte size
-  // NOTE: by signalling a SH only with CLK, bitstreams generated by the AVM s/w
-  // may not be random accessed to an OBU_OPEN_LOOP_KEY
-  if (cm->current_frame.cm_obu_type == OBU_CLOSED_LOOP_KEY) {
+  // write sequence header obu if obu type is CLK or OLK, preceded by 4-byte
+  // size
+  if (cm->current_frame.cm_obu_type == OBU_CLOSED_LOOP_KEY ||
+      cm->current_frame.cm_obu_type == OBU_OPEN_LOOP_KEY) {
     obu_header_size =
         av2_write_obu_header(level_params, OBU_SEQUENCE_HEADER, 0, 0, data);
     if (cm->seq_params.seq_seg_info_present_flag)
@@ -7035,7 +7035,10 @@
     }
 
     data += obu_header_size + obu_payload_size + length_field_size;
+  }
 
+  if (cm->current_frame.cm_obu_type == OBU_CLOSED_LOOP_KEY) {
+    size_t length_field_size;
     if (cm->current_frame.frame_type == KEY_FRAME && !cpi->no_show_fwd_kf &&
         cpi->write_ci_obu_flag) {
       obu_header_size = av2_write_obu_header(
diff --git a/av2/encoder/pass2_strategy.c b/av2/encoder/pass2_strategy.c
index 4aeb154..8daece2 100644
--- a/av2/encoder/pass2_strategy.c
+++ b/av2/encoder/pass2_strategy.c
@@ -1575,6 +1575,18 @@
   } else {
     rc->baseline_gf_interval = arf_position;
   }
+
+  // In LAP mode, is_last_kf may fail to detect the last KF interval because
+  // the stats buffer doesn't represent the true end of the clip. Cap
+  // baseline_gf_interval against the actual lookahead depth to prevent
+  // arf_src_offset from exceeding available frames.
+  if (cpi->lap_enabled) {
+    const int la_depth =
+        av2_lookahead_depth(cpi->lookahead, cpi->compressor_stage);
+    if (rc->baseline_gf_interval > la_depth)
+      rc->baseline_gf_interval = la_depth;
+  }
+
   assert(rc->baseline_gf_interval > 0);
 }