Reset decoder state at TD

Combine !3380 and !3397 to resolve unit test failure in each
individual change.
    
Fix #1447
diff --git a/av2/decoder/decodeframe.c b/av2/decoder/decodeframe.c
index 4d144f4..0130bba 100644
--- a/av2/decoder/decodeframe.c
+++ b/av2/decoder/decodeframe.c
@@ -8245,7 +8245,7 @@
       } else {
         // prevent resetting it to -1 when {OLK(s) - OLK(h)} when a hidden OLK
         // comes after a shown OLK
-        if (pbi->this_is_first_keyframe_unit_in_tu ||
+        if (pbi->this_is_first_vcl_obu_in_tu ||
             pbi->last_olk_tu_display_order_hint == -1) {
           pbi->last_olk_tu_display_order_hint = -1;
         }
diff --git a/av2/decoder/decoder.c b/av2/decoder/decoder.c
index 1601b23..0f82106 100644
--- a/av2/decoder/decoder.c
+++ b/av2/decoder/decoder.c
@@ -236,7 +236,6 @@
   memset(&pbi->last_displayable_frame_unit, -1,
          sizeof(pbi->last_displayable_frame_unit));
   memset(pbi->last_output_doh, -1, sizeof(pbi->last_output_doh));
-  pbi->this_is_first_keyframe_unit_in_tu = 0;
   pbi->this_is_first_vcl_obu_in_tu = 0;
   pbi->last_decoded_xlayer_id = -1;
   for (int i = 0; i < MAX_NUM_MLAYERS; i++) {
diff --git a/av2/decoder/decoder.h b/av2/decoder/decoder.h
index 13aff31..bd3001c 100644
--- a/av2/decoder/decoder.h
+++ b/av2/decoder/decoder.h
@@ -315,6 +315,9 @@
   bool mfh_valid_buf[MAX_MFH_NUM];
   int decoding_first_frame;
   int last_olk_tu_display_order_hint;
+  int seen_frame_header_buf;
+  int next_start_tile_buf;
+  int seen_vcl_obu_in_this_tu_buf;
 } StreamInfo;
 
 /*!
@@ -527,11 +530,6 @@
    */
   obu_info last_displayable_frame_unit[MAX_NUM_XLAYERS];
   /*!
-   * Indicates if the current data chunk being decoded in avm_codec_decode()
-   * is the first frame unit of the temporal unit
-   */
-  int this_is_first_keyframe_unit_in_tu;
-  /*!
    * Tracks the xlayer_id of the last decoded frame unit. Reset to -1 when a
    * temporal delimiter is seen. Used to detect combined TU boundaries so that
    * last_frame_unit can be reset per-xlayer when a keyframe is encountered.
diff --git a/av2/decoder/obu.c b/av2/decoder/obu.c
index 1badf36..2e56040 100644
--- a/av2/decoder/obu.c
+++ b/av2/decoder/obu.c
@@ -259,6 +259,10 @@
   pbi->stream_info[stream_idx].decoding_first_frame = pbi->decoding_first_frame;
   pbi->stream_info[stream_idx].last_olk_tu_display_order_hint =
       pbi->last_olk_tu_display_order_hint;
+  pbi->stream_info[stream_idx].seen_frame_header_buf = pbi->seen_frame_header;
+  pbi->stream_info[stream_idx].next_start_tile_buf = pbi->next_start_tile;
+  pbi->stream_info[stream_idx].seen_vcl_obu_in_this_tu_buf =
+      pbi->seen_vcl_obu_in_this_tu;
 }
 
 // Helper function to restore xlayer context
@@ -323,6 +327,10 @@
   pbi->decoding_first_frame = pbi->stream_info[stream_idx].decoding_first_frame;
   pbi->last_olk_tu_display_order_hint =
       pbi->stream_info[stream_idx].last_olk_tu_display_order_hint;
+  pbi->seen_frame_header = pbi->stream_info[stream_idx].seen_frame_header_buf;
+  pbi->next_start_tile = pbi->stream_info[stream_idx].next_start_tile_buf;
+  pbi->seen_vcl_obu_in_this_tu =
+      pbi->stream_info[stream_idx].seen_vcl_obu_in_this_tu_buf;
 }
 
 static void init_stream_info(StreamInfo *stream_info) {
@@ -2519,22 +2527,6 @@
       }
     }
 
-    if (is_single_tile_vcl_obu(obu_header.type) ||
-        is_multi_tile_vcl_obu(obu_header.type)) {
-      if (!pbi->seen_vcl_obu_in_this_tu) {
-        pbi->this_is_first_vcl_obu_in_tu = 1;
-        pbi->seen_vcl_obu_in_this_tu = 1;
-      } else {
-        pbi->this_is_first_vcl_obu_in_tu = 0;
-      }
-    }
-
-    if (obu_header.type == OBU_CLOSED_LOOP_KEY ||
-        obu_header.type == OBU_OPEN_LOOP_KEY) {
-      if (pbi->this_is_first_vcl_obu_in_tu)
-        pbi->this_is_first_keyframe_unit_in_tu = 1;
-    }
-
     if (av2_is_leading_vcl_obu(obu_header.type))
       cm->is_leading_picture = 1;
     else if (av2_is_regular_vcl_obu(obu_header.type))
@@ -2638,13 +2630,23 @@
       cm->bridge_frame_info.is_bridge_frame = 0;
     }
 
+    if (is_single_tile_vcl_obu(obu_header.type) ||
+        is_multi_tile_vcl_obu(obu_header.type)) {
+      if (!pbi->seen_vcl_obu_in_this_tu) {
+        pbi->this_is_first_vcl_obu_in_tu = 1;
+        pbi->seen_vcl_obu_in_this_tu = 1;
+      } else {
+        pbi->this_is_first_vcl_obu_in_tu = 0;
+      }
+    }
+
     // Flush remaining frames after xlayer context is correctly set.
     // This must happen after xlayer switching but before processing frame OBUs.
     // Skip flush while in GLOBAL_XLAYER_ID context -- the target layer's
     // context has not been restored yet; flush will run once a non-global
     // OBU restores it.
-    if (pbi->this_is_first_keyframe_unit_in_tu &&
-        cm->xlayer_id != GLOBAL_XLAYER_ID &&
+    if (pbi->this_is_first_vcl_obu_in_tu && cm->xlayer_id != GLOBAL_XLAYER_ID &&
+        obu_header.type == OBU_CLOSED_LOOP_KEY &&
         pbi->obus_in_frame_unit_data[cm->tlayer_id][cm->mlayer_id]
                                     [OBU_CLOSED_LOOP_KEY]) {
       flush_remaining_frames(pbi, INT_MAX);
@@ -2666,13 +2668,19 @@
     av2_init_read_bit_buffer(pbi, &rb, data, data + payload_size);
     switch (obu_header.type) {
       case OBU_TEMPORAL_DELIMITER:
-        decoded_payload_size = read_temporal_delimiter_obu();
-        pbi->seen_frame_header = 0;
-        pbi->next_start_tile = 0;
-        pbi->seen_vcl_obu_in_this_tu = 0;
-        pbi->this_is_first_vcl_obu_in_tu = 0;
-        pbi->doh_tu_order_hint_bits_set = 0;
-        for (int i = 0; i < NUM_CUSTOM_QMS; i++) pbi->qm_protected[i] = 0;
+        for (int xlayer = 0; xlayer < AVM_MAX_NUM_STREAMS - 1; xlayer++) {
+          if (pbi->xlayer_id_map[xlayer] > 0) {
+            av2_store_xlayer_context(pbi, cm, cm->xlayer_id);
+            cm->xlayer_id = xlayer;
+            av2_restore_xlayer_context(pbi, cm, xlayer);
+            decoded_payload_size = read_temporal_delimiter_obu();
+            pbi->seen_frame_header = 0;
+            pbi->next_start_tile = 0;
+            pbi->seen_vcl_obu_in_this_tu = 0;
+            pbi->doh_tu_order_hint_bits_set = 0;
+            for (int i = 0; i < NUM_CUSTOM_QMS; i++) pbi->qm_protected[i] = 0;
+          }
+        }
         break;
       case OBU_MULTI_STREAM_DECODER_OPERATION:
         decoded_payload_size =