Project import generated by Copybara.

GitOrigin-RevId: c234c62b839e3d6bef4195120419ab730afb604d
diff --git a/drivers/frame_provider/decoder/h264_multi/h264_dpb.c b/drivers/frame_provider/decoder/h264_multi/h264_dpb.c
index b583a82..7b0980a 100755
--- a/drivers/frame_provider/decoder/h264_multi/h264_dpb.c
+++ b/drivers/frame_provider/decoder/h264_multi/h264_dpb.c
@@ -2053,7 +2053,7 @@
 		p_Dpb->used_size--;
 }
 
-static int is_used_for_reference(struct FrameStore *fs)
+int is_used_for_reference(struct FrameStore *fs)
 {
 	if (fs->is_reference)
 		return 1;
diff --git a/drivers/frame_provider/decoder/h264_multi/h264_dpb.h b/drivers/frame_provider/decoder/h264_multi/h264_dpb.h
index 8f90961..10b2dba 100755
--- a/drivers/frame_provider/decoder/h264_multi/h264_dpb.h
+++ b/drivers/frame_provider/decoder/h264_multi/h264_dpb.h
@@ -998,4 +998,6 @@
 
 int post_picture_early(struct vdec_s *vdec, int index);
 
+int is_used_for_reference(struct FrameStore *fs);
+
 #endif
diff --git a/drivers/frame_provider/decoder/h264_multi/vmh264.c b/drivers/frame_provider/decoder/h264_multi/vmh264.c
index 04ac980..3274134 100755
--- a/drivers/frame_provider/decoder/h264_multi/vmh264.c
+++ b/drivers/frame_provider/decoder/h264_multi/vmh264.c
@@ -102,6 +102,7 @@
 #define FIX_FRAME_RATE_ON                 1
 #define FIX_FRAME_RATE_SMOOTH_CHECKING    2
 
+#define DEC_CONTROL_FLAG_DISABLE 0x0000
 #define DEC_CONTROL_FLAG_FORCE_2997_1080P_INTERLACE 0x0001
 #define DEC_CONTROL_FLAG_FORCE_2500_576P_INTERLACE  0x0002
 #define DEC_CONTROL_FLAG_FORCE_RATE_2397_FPS_FIX_FRAME_RATE  0x0010
@@ -140,9 +141,7 @@
 
 static int ref_b_frame_error_max_count = 50;
 
-static unsigned int dec_control =
-	DEC_CONTROL_FLAG_FORCE_2997_1080P_INTERLACE |
-	DEC_CONTROL_FLAG_FORCE_2500_576P_INTERLACE;
+static unsigned int dec_control = DEC_CONTROL_FLAG_DISABLE;
 
 static unsigned int force_rate_streambase;
 static unsigned int force_rate_framebase;
@@ -5651,10 +5650,6 @@
 	struct h264_dpb_stru *p_H264_Dpb = &hw->dpb;
 
 	bufmgr_h264_remove_unused_frame(p_H264_Dpb, 2);
-	if (error_proc_policy & 0x20) {
-		if (!hw->is_used_v4l)
-			hw->reset_bufmgr_flag = 1;
-	}
 }
 
 void bufmgr_force_recover(struct h264_dpb_stru *p_H264_Dpb)
@@ -5739,26 +5734,25 @@
 			((p_H264_Dpb->mDPB.used_size >=
 				(p_H264_Dpb->mDPB.size - 1)) ||
 			(!have_free_buf_spec(vdec)))) {
-			bufmgr_h264_remove_unused_frame(p_H264_Dpb, 0);
+			unsigned long flags;
+			spin_lock_irqsave(&hw->bufspec_lock, flags);
 
 			for (i = 0; i < p_Dpb->used_size; i++) {
 				if (p_Dpb->fs[i]->pre_output)
 					frame_outside_count++;
-			}
-
-			inner_size = p_Dpb->size - frame_outside_count;
-
-			if (inner_size >= p_H264_Dpb->dec_dpb_size) {
-				if (p_H264_Dpb->mDPB.used_size >=
-					p_H264_Dpb->mDPB.size) {
-					bufmgr_recover(hw);
-				} else if (p_H264_Dpb->mDPB.used_size >=
-					(p_H264_Dpb->mDPB.size - 1)) {
-					if (inner_size > p_H264_Dpb->dec_dpb_size) {
-						bufmgr_recover(hw);
-					}
+				else if (p_Dpb->fs[i]->is_output && !is_used_for_reference(p_Dpb->fs[i])) {
+					spin_unlock_irqrestore(&hw->bufspec_lock, flags);
+					bufmgr_h264_remove_unused_frame(p_H264_Dpb, 0);
+					return 0;
 				}
 			}
+			spin_unlock_irqrestore(&hw->bufspec_lock, flags);
+			inner_size = p_Dpb->used_size - frame_outside_count;
+
+			if (inner_size >= p_H264_Dpb->dec_dpb_size) {
+				bufmgr_recover(hw);
+			}
+			bufmgr_h264_remove_unused_frame(p_H264_Dpb, 0);
 		} else if ((error_proc_policy & 0x8) &&
 			(p_Dpb->ref_frames_in_buffer >
 			(imax(
@@ -5771,9 +5765,9 @@
 
 		if (hw->reset_bufmgr_flag == 1)
 			buffer_available = 1;
-		else if (hw->is_used_v4l)
-			buffer_available = have_free_buf_spec(vdec);
 	}
+	if (hw->is_used_v4l)
+		buffer_available = have_free_buf_spec(vdec);
 
 	return buffer_available;
 }
@@ -9601,6 +9595,9 @@
 	if (hw->reset_bufmgr_flag ||
 		((error_proc_policy & 0x40) &&
 		p_H264_Dpb->buf_alloc_fail)) {
+		if (p_H264_Dpb->buf_alloc_fail){
+			hw->reset_bufmgr_flag = 1;
+		}
 		h264_reset_bufmgr(vdec);
 		hw->reset_bufmgr_flag = 0;
 	}
@@ -10015,8 +10012,9 @@
 	hw->skip_frame_count);
 
 	/* Only the caller from inside decoder we call flush_dbp */
-	if (!hw->reset_bufmgr_flag)
+	if (!hw->reset_bufmgr_flag){
 		flush_dpb(&hw->dpb);
+	}
 
 	timeout = jiffies + HZ;
 	while (kfifo_len(&hw->display_q) > 0) {