modified mix h264 encoder codes to support shared buffer mode encoding

Change-Id: I9398120697b1e703975482459bbbe0ffdd88f894
Signed-off-by: Yanlong Fang <yanlong.fang@intel.com>
diff --git a/mix_video/src/Android.mk b/mix_video/src/Android.mk
index 718fddf..0f060fc 100644
--- a/mix_video/src/Android.mk
+++ b/mix_video/src/Android.mk
@@ -93,6 +93,7 @@
 	mixvideoconfigparamsdec_vc1.h	\
 	mixvideoconfigparamsenc.h	\
 	mixvideoconfigparamsenc_h264.h	\
+        mixvideoconfigparamsenc_h263.h  \
 	mixvideoconfigparamsenc_mpeg4.h	\
 	mixvideoconfigparamsenc_preview.h \
 	mixvideodecodeparams.h		\
@@ -103,6 +104,7 @@
 	mixvideoformat_vc1.h		\
 	mixvideoformatenc.h		\
 	mixvideoformatenc_h264.h	\
+	mixvideoformatenc_h263.h        \
 	mixvideoformatenc_mpeg4.h	\
 	mixvideoformatenc_preview.h	\
 	mixvideoformatqueue.h		\
diff --git a/mix_video/src/mixvideoformatenc_h263.c b/mix_video/src/mixvideoformatenc_h263.c
index 96f8c9a..5f28682 100644
--- a/mix_video/src/mixvideoformatenc_h263.c
+++ b/mix_video/src/mixvideoformatenc_h263.c
@@ -44,6 +44,7 @@
     self->cur_frame = NULL;
     self->ref_frame = NULL;
     self->rec_frame = NULL;	
+    self->last_mix_buffer = NULL;
 
     self->ci_shared_surfaces = NULL;
     self->surfaces= NULL;
@@ -748,6 +749,11 @@
         mix_videoframe_unref (self->ref_frame);
         self->ref_frame = NULL;       
     }
+
+    if(self->last_mix_buffer) {
+       mix_buffer_unref(self->last_mix_buffer);
+       self->last_mix_buffer = NULL;
+    }
     
     /*reset the properities*/    
     self->encoded_frames = 0;
@@ -1310,7 +1316,13 @@
                     "input buf size = %d\n", bufin->size);			
             
             guint8 *inbuf = bufin->data;      
-            
+           
+#ifndef ANDROID
+#define USE_SRC_FMT_YUV420
+#else
+#define USE_SRC_FMT_NV21
+#endif
+#ifdef USE_SRC_FMT_YUV420
             /*need to convert YUV420 to NV12*/
             dst_y = pvbuf +image->offsets[0];
             
@@ -1329,7 +1341,38 @@
                 }
                 dst_uv += image->pitches[1];
             }
-            
+           
+#else //USE_SRC_FMT_NV12 or USE_SRC_FMT_NV21
+            int offset_uv = width * height;
+            guint8 *inbuf_uv = inbuf + offset_uv;
+            int height_uv = height / 2;
+            int width_uv = width;
+
+            dst_y = pvbuf + image->offsets[0];
+            for (i = 0; i < height; i++) {
+                memcpy (dst_y, inbuf + i * width, width);
+                dst_y += image->pitches[0];
+            }
+
+#ifdef USE_SRC_FMT_NV12
+            dst_uv = pvbuf + image->offsets[1];
+            for (i = 0; i < height_uv; i++) {
+                memcpy(dst_uv, inbuf_uv + i * width_uv, width_uv);
+                dst_uv += image->pitches[1];
+            }
+#else //USE_SRC_FMT_NV21
+            dst_uv = pvbuf + image->offsets[1];
+            for (i = 0; i < height_uv; i ++) {
+                for (j = 0; j < width_uv; j += 2) {
+                    dst_uv[j] = inbuf_uv[j+1];  //u
+                    dst_uv[j+1] = inbuf_uv[j];  //v
+                }
+                dst_uv += image->pitches[1];
+                inbuf_uv += width_uv;
+            }
+#endif
+#endif //USE_SRC_FMT_YUV420
+ 
             va_status = vaUnmapBuffer(va_display, image->buf);	
             if (va_status != VA_STATUS_SUCCESS)	 
             {
@@ -1405,8 +1448,9 @@
             if (mix->cur_frame == NULL)
             {
                 guint ci_idx;
-                memcpy (&ci_idx, bufin->data, bufin->size);
-                
+//                memcpy (&ci_idx, bufin->data, bufin->size);
+                memcpy (&ci_idx, bufin->data, sizeof(unsigned int));
+ 
                 LOG_I( 
                         "surface_num = %d\n", mix->surface_num);			 
                 LOG_I( 
@@ -1536,7 +1580,7 @@
             if (coded_seg->next == NULL)	
                 break;		
 
-            coded_seg ++;
+            coded_seg = coded_seg->next;
             num_seg ++;
         }
 
@@ -1572,7 +1616,7 @@
             if (coded_seg->next == NULL)	
                 break;		
 
-            coded_seg ++;
+            coded_seg = coded_seg->next;
         }        
 
         iovout->buffer_size = iovout->data_size;
@@ -1678,9 +1722,18 @@
         mix->coded_buf_index %=2;
         mix->last_frame = mix->cur_frame;
         
+        if(mix->last_mix_buffer) {
+           LOG_V("calls to mix_buffer_unref \n");
+           LOG_V("refcount = %d\n", MIX_PARAMS(mix->last_mix_buffer)->refcount);
+           mix_buffer_unref(mix->last_mix_buffer);
+        }
+
+        LOG_V("ref the current bufin\n");
+        mix->last_mix_buffer = mix_buffer_ref(bufin);
+
         if (!(parent->need_display)) {
-            mix_videoframe_unref (mix->cur_frame);
-            mix->cur_frame = NULL;
+             mix_videoframe_unref (mix->cur_frame);
+             mix->cur_frame = NULL;
         }
 
 cleanup:
diff --git a/mix_video/src/mixvideoformatenc_h263.h b/mix_video/src/mixvideoformatenc_h263.h
index b4d1f84..ece5fc8 100644
--- a/mix_video/src/mixvideoformatenc_h263.h
+++ b/mix_video/src/mixvideoformatenc_h263.h
@@ -47,8 +47,8 @@
 	MixVideoFrame  *cur_frame;	//current input frame to be encoded;	
 	MixVideoFrame  *ref_frame;  //reference frame
 	MixVideoFrame  *rec_frame;	//reconstructed frame;	
-	MixVideoFrame  *last_frame;	//last frame;	
-	
+	MixVideoFrame  *last_frame;	//last frame;
+        MixBuffer      *last_mix_buffer;
 
 	guint disable_deblocking_filter_idc;
 	guint slice_num;
diff --git a/mix_video/src/mixvideoformatenc_h264.c b/mix_video/src/mixvideoformatenc_h264.c
index 776d1fc..82fad06 100644
--- a/mix_video/src/mixvideoformatenc_h264.c
+++ b/mix_video/src/mixvideoformatenc_h264.c
@@ -43,7 +43,8 @@
     self->is_intra = TRUE;
     self->cur_frame = NULL;
     self->ref_frame = NULL;
-    self->rec_frame = NULL;	
+    self->rec_frame = NULL;
+    self->last_mix_buffer = NULL;
 
     self->ci_shared_surfaces = NULL;
     self->surfaces= NULL;
@@ -775,7 +776,12 @@
         mix_videoframe_unref (self->ref_frame);
         self->ref_frame = NULL;       
     }
-    
+
+    if(self->last_mix_buffer) {
+       mix_buffer_unref(self->last_mix_buffer);
+       self->last_mix_buffer = NULL;
+    }
+   
     /*reset the properities*/    
     self->encoded_frames = 0;
     self->pic_skipped = FALSE;
@@ -1641,7 +1647,7 @@
         if (coded_seg->next == NULL)	
             break;		
         
-        coded_seg ++;
+        coded_seg = coded_seg->next;
         num_seg ++;
     }
 
@@ -1684,7 +1690,7 @@
             if (coded_seg->next == NULL)	
                 break;		            
             
-            coded_seg ++;
+            coded_seg = coded_seg->next;
         }		
 
         //memcpy (iovout->data, buf + 16, iovout->data_size); //parload is started from 17th byte
@@ -1752,7 +1758,7 @@
                     if (coded_seg->next == NULL)	
                         break;		
                     
-                    coded_seg ++;
+                    coded_seg = coded_seg->next;
                 }	
                 
                 ret = mix_videofmtenc_h264_AnnexB_to_length_prefixed (tem_buf, iovout->data_size, iovout->data, &size);
@@ -1870,6 +1876,14 @@
     mix->coded_buf_index %=2;
     mix->last_frame = mix->cur_frame;
 
+    if(mix->last_mix_buffer) {       
+       LOG_V("calls to mix_buffer_unref \n");
+       LOG_V("refcount = %d\n", MIX_PARAMS(mix->last_mix_buffer)->refcount);
+       mix_buffer_unref(mix->last_mix_buffer);
+    }
+
+    LOG_V("ref the current bufin\n");
+    mix->last_mix_buffer = mix_buffer_ref(bufin);
 
     if (!(parent->need_display)) {
         mix_videoframe_unref (mix->cur_frame);
diff --git a/mix_video/src/mixvideoformatenc_h264.h b/mix_video/src/mixvideoformatenc_h264.h
index 0ef0e18..6f470af 100644
--- a/mix_video/src/mixvideoformatenc_h264.h
+++ b/mix_video/src/mixvideoformatenc_h264.h
@@ -47,6 +47,7 @@
     MixVideoFrame  *ref_frame;  //reference frame
     MixVideoFrame  *rec_frame;	//reconstructed frame;	
     MixVideoFrame  *last_frame;	//last frame;	
+    MixBuffer      *last_mix_buffer;
 
     guint basic_unit_size;  //for rate control
     guint disable_deblocking_filter_idc;