Merge "bengal: Update media spec for bengal"
diff --git a/conf_files/bengal/system_properties.xml b/conf_files/bengal/system_properties.xml
index 4b76dab..e93fefc 100644
--- a/conf_files/bengal/system_properties.xml
+++ b/conf_files/bengal/system_properties.xml
@@ -34,9 +34,6 @@
         <property name="vidc_enc_log_in" value="0"/>
         <property name="vidc_enc_log_out" value="0"/>
         <property name="vidc_enc_csc_custom_matrix" value="0"/>
-        <!-- Buffer size : internal : 30 MB (30 X 1), output : 50 MB (3.125 X 16) -->
-        <property name="vidc_dec_sec_prefetch_size_internal" value="31457280"/>
-        <property name="vidc_dec_sec_prefetch_size_output" value="3276800"/>
         <!-- Bitmask for arb mode: 1: AVC, 2: HEVC, 4: MPEG2 -->
         <property name="vidc_dec_arb_mode_override" value="7"/>
 </configs>
diff --git a/conf_files/kona/media_codecs_performance.xml b/conf_files/kona/media_codecs_performance.xml
index 795dd6f..ab033bd 100644
--- a/conf_files/kona/media_codecs_performance.xml
+++ b/conf_files/kona/media_codecs_performance.xml
@@ -176,7 +176,7 @@
             <Limit name="measured-frame-rate-1920x1080" range="12-17" />
         </MediaCodec>
         <MediaCodec name="c2.android.hevc.decoder" type="video/hevc" update="true">
-            <Limit name="measured-frame-rate-352x288" range="179-245" />
+            <Limit name="measured-frame-rate-352x288" range="355-960" />
             <Limit name="measured-frame-rate-640x360" range="120-164" />
             <Limit name="measured-frame-rate-720x480" range="111-152" />
             <Limit name="measured-frame-rate-1280x720" range="61-83" />
diff --git a/conf_files/lito/media_codecs_performance.xml b/conf_files/lito/media_codecs_performance.xml
index 3d8222e..7281b95 100644
--- a/conf_files/lito/media_codecs_performance.xml
+++ b/conf_files/lito/media_codecs_performance.xml
@@ -175,8 +175,8 @@
             <Limit name="measured-frame-rate-1920x1080" range="29-43" />
         </MediaCodec>
         <MediaCodec name="c2.android.h263.decoder" type="video/3gpp" update="true">
-            <Limit name="measured-frame-rate-176x144" range="284-386" />
-            <Limit name="measured-frame-rate-352x288" range="197-284" />
+            <Limit name="measured-frame-rate-176x144" range="450-850" />
+            <Limit name="measured-frame-rate-352x288" range="650-780" />
         </MediaCodec>
         <MediaCodec name="c2.android.mpeg4.decoder" type="video/mp4v-es" update="true">
             <Limit name="measured-frame-rate-176x144" range="374-542" />
diff --git a/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp b/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp
index ab2bbc4..a1a95b3 100644
--- a/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp
+++ b/mm-video-v4l2/vidc/vdec/src/omx_vdec_v4l2.cpp
@@ -8662,8 +8662,13 @@
 {
     bool status = true;
     pthread_mutex_lock(&omx->c_lock);
+     /* Whenever port mode is set to kPortModeDynamicANWBuffer, Video Frameworks
+        always uses VideoNativeMetadata and OMX recives buffer type as
+        grallocsource via storeMetaDataInBuffers_l API. The buffer_size
+        will be communicated to frameworks via IndexParamPortdefinition. */
     if (!enabled)
-        buffer_size = omx->drv_ctx.op_buf.buffer_size;
+        buffer_size = omx->dynamic_buf_mode ? sizeof(struct VideoNativeMetadata) :
+                      omx->drv_ctx.op_buf.buffer_size;
     else {
         buffer_size = c2dcc.getBuffSize(C2D_OUTPUT);
     }
@@ -8674,8 +8679,8 @@
 OMX_ERRORTYPE omx_vdec::allocate_color_convert_buf::set_buffer_req(
         OMX_U32 buffer_size, OMX_U32 actual_count)
 {
-    OMX_U32 expectedSize = enabled ? buffer_size_req : omx->drv_ctx.op_buf.buffer_size;
-
+    OMX_U32 expectedSize = enabled ? buffer_size_req : omx->dynamic_buf_mode ?
+            sizeof(struct VideoDecoderOutputMetaData) : omx->drv_ctx.op_buf.buffer_size;
     if (buffer_size < expectedSize) {
         DEBUG_PRINT_ERROR("OP Requirements: Client size(%u) insufficient v/s requested(%u)",
                 buffer_size, expectedSize);
diff --git a/mm-video-v4l2/vidc/venc/inc/video_encoder_device_v4l2.h b/mm-video-v4l2/vidc/venc/inc/video_encoder_device_v4l2.h
index 122d22e..caae5b2 100644
--- a/mm-video-v4l2/vidc/venc/inc/video_encoder_device_v4l2.h
+++ b/mm-video-v4l2/vidc/venc/inc/video_encoder_device_v4l2.h
@@ -370,7 +370,6 @@
         bool is_csc_custom_matrix_enabled;
         bool is_auto_blur_disabled;
         bool csc_enable;
-        OMX_U32 fd_list[64];
         unsigned long get_media_colorformat(unsigned long);
 
     private:
@@ -440,7 +439,6 @@
         bool venc_set_peak_bitrate(OMX_U32 nPeakBitrate);
         bool venc_set_vpx_error_resilience(OMX_BOOL enable);
         bool venc_set_batch_size(OMX_U32 size);
-        bool venc_get_index_from_fd(OMX_U32 buffer_fd, OMX_U32 *index);
         bool venc_set_hierp_layers(OMX_U32 hierp_layers);
         bool venc_set_baselayerid(OMX_U32 baseid);
         bool venc_set_qp(OMX_U32 i_frame_qp, OMX_U32 p_frame_qp,OMX_U32 b_frame_qp, OMX_U32 enable);
diff --git a/mm-video-v4l2/vidc/venc/src/omx_video_encoder.cpp b/mm-video-v4l2/vidc/venc/src/omx_video_encoder.cpp
index 0967944..83cf11e 100644
--- a/mm-video-v4l2/vidc/venc/src/omx_video_encoder.cpp
+++ b/mm-video-v4l2/vidc/venc/src/omx_video_encoder.cpp
@@ -343,14 +343,8 @@
 
     OMX_INIT_STRUCT(&m_sIntraperiod, QOMX_VIDEO_INTRAPERIODTYPE);
     m_sIntraperiod.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
-    m_sIntraperiod.nPFrames = (m_sConfigFramerate.xEncodeFramerate * 2) - 1;
-    /* Consider a scenario where client does get of this and does not modify this
-       and does a set. Then if by default if this is 0 we assume that client is explicitly
-       requesting disabling of B-Frames and our logic to automatically enable bFrames will
-       fail(We do not enable bframes if there is a set of this param with 0 value). We do
-       not want this to happen(also all our default values support auto enabling of B-Frames).
-       We always take care of scenarios where bframes need to be disabled */
-    m_sIntraperiod.nBFrames = 1;
+    m_sIntraperiod.nPFrames = ((m_sConfigFramerate.xEncodeFramerate >> 16) * 2) - 1;
+    m_sIntraperiod.nBFrames = 0;
 
 
     OMX_INIT_STRUCT(&m_sErrorCorrection, OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE);
diff --git a/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2.cpp b/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2.cpp
index 0e080ba..23b1713 100644
--- a/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2.cpp
+++ b/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2.cpp
@@ -152,7 +152,6 @@
     memset(&voptimecfg, 0, sizeof(voptimecfg));
     memset(&capability, 0, sizeof(capability));
     memset(&m_debug,0,sizeof(m_debug));
-    memset(&fd_list, 0, sizeof(fd_list));
     sess_priority.priority = 1;
     operating_rate = 30;
     memset(&color_space, 0x0, sizeof(color_space));
@@ -645,7 +644,7 @@
 bool venc_dev::handle_input_extradata(struct v4l2_buffer buf)
 {
     unsigned int filled_len = 0;
-    unsigned int index = 0;
+    unsigned int index = buf.index;
     int height = m_sVenc_cfg.input_height;
     int width = m_sVenc_cfg.input_width;
     OMX_TICKS nTimeStamp = static_cast<OMX_TICKS>(buf.timestamp.tv_sec) * 1000000 + buf.timestamp.tv_usec;
@@ -662,11 +661,6 @@
         return true;
     }
 
-    if (!venc_get_index_from_fd(fd, &index)) {
-        DEBUG_PRINT_LOW("Index not found for fd %d", fd);
-        return false;
-    }
-
     if (!input_extradata_info.ion[index].uaddr) {
         DEBUG_PRINT_ERROR("Extradata buffers not allocated\n");
         return true;
@@ -1137,14 +1131,15 @@
         return OMX_ErrorNone;
     }
 
-    if (!extradata_info->buffer_size) {
-        DEBUG_PRINT_ERROR("Invalid extradata buffer size for port %d", extradata_info->port_index);
+    if (!extradata_info->buffer_size || !extradata_info->count) {
+        DEBUG_PRINT_ERROR("Invalid extradata buffer size(%lu) or count(%d) for port %d",
+            extradata_info->buffer_size, extradata_info->count, extradata_info->port_index);
         return OMX_ErrorUndefined;
     }
 
 #ifdef USE_ION
 
-    for (int i = 0; i < VIDEO_MAX_FRAME; i++) {
+    for (int i = 0; i < extradata_info->count; i++) {
         if (extradata_info->ion[i].data_fd != -1) {
             venc_handle->ion_unmap(extradata_info->ion[i].data_fd,
                     (void *)extradata_info->ion[i].uaddr, extradata_info->buffer_size);
@@ -1189,7 +1184,7 @@
         return;
     }
 
-    for (int i = 0; i < VIDEO_MAX_FRAME; i++) {
+    for (int i = 0; i < extradata_info->count; i++) {
         if (extradata_info->ion[i].uaddr) {
             venc_handle->ion_unmap(extradata_info->ion[i].data_fd,
                     (void *)extradata_info->ion[i].uaddr, extradata_info->buffer_size);
@@ -1872,7 +1867,7 @@
             return false;
         }
         input_extradata_info.buffer_size =  ALIGN(extra_data_size, SZ_4K);
-        input_extradata_info.count = MAX_V4L2_BUFS;
+        input_extradata_info.count = m_sInput_buff_property.actualcount;
         venc_handle->m_client_in_extradata_info.set_extradata_info(input_extradata_info.buffer_size,m_sInput_buff_property.actualcount);
     } else {
         unsigned int extra_idx = 0;
@@ -2284,10 +2279,6 @@
     struct v4l2_encoder_cmd enc;
     DEBUG_PRINT_LOW("in %s", __func__);
 
-    for (unsigned int i = 0; i < (sizeof(fd_list)/sizeof(fd_list[0])); i++) {
-        fd_list[i] = 0;
-    }
-
     enc.cmd = V4L2_CMD_FLUSH;
     enc.flags = V4L2_CMD_FLUSH_OUTPUT | V4L2_CMD_FLUSH_CAPTURE;
 
@@ -2464,8 +2455,7 @@
     struct v4l2_buffer buf;
     struct v4l2_requestbuffers bufreq;
     struct v4l2_plane plane[VIDEO_MAX_PLANES];
-    int rc = 0, extra_idx;
-    OMX_U32 extradata_index;
+    int rc = 0, extra_idx, c2d_enabled = 0;
     bool interlace_flag = false;
     struct OMX_BUFFERHEADERTYPE *bufhdr;
     LEGACY_CAM_METADATA_TYPE * meta_buf = NULL;
@@ -2847,6 +2837,7 @@
                 // color_format == 1 ==> RGBA to YUV Color-converted buffer
                 // Buffers color-converted via C2D have 601 color
                 if (!streaming[OUTPUT_PORT]) {
+                    c2d_enabled = 1;
                     DEBUG_PRINT_HIGH("Setting colorspace 601 for Color-converted buffer");
                     venc_set_colorspace(MSM_VIDC_BT601_6_625, color_space.range,
                             MSM_VIDC_TRANSFER_601_6_525, MSM_VIDC_MATRIX_601_6_525);
@@ -2878,6 +2869,18 @@
         buf_type=V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
         int ret;
 
+        // Some 3rd APPs use NativeRecorder to implement their applications
+        // like screenrecorder, implicitly enable B-frame may cause issues.
+        // So disallow implicit B-frame when input format is non-UBWC or RGBA(c2d enabled).
+        if ((m_sVenc_cfg.inputformat != V4L2_PIX_FMT_NV12_TP10_UBWC &&
+             m_sVenc_cfg.inputformat != V4L2_PIX_FMT_NV12_UBWC) || c2d_enabled) {
+            DEBUG_PRINT_HIGH("Disallow implicitly enable B-frames");
+            if (!set_native_recoder(OMX_FALSE)) {
+                DEBUG_PRINT_ERROR("Failed to set Native Recorder");
+                return false;
+            }
+        }
+
         if (!downscalar_enabled) {
             OMX_U32 inp_width = 0, inp_height = 0, out_width = 0, out_height = 0;
 
@@ -2914,16 +2917,11 @@
     extra_idx = EXTRADATA_IDX(num_input_planes);
 
     if (extra_idx && (extra_idx < VIDEO_MAX_PLANES)) {
-        if (!venc_get_index_from_fd(fd, &extradata_index)) {
-            DEBUG_PRINT_ERROR("Extradata index not found for fd: %d\n", fd);
-            return false;
-        }
-
         plane[extra_idx].bytesused = input_extradata_info.buffer_size;
         plane[extra_idx].length = input_extradata_info.buffer_size;
-        plane[extra_idx].m.userptr = (unsigned long)input_extradata_info.ion[extradata_index].uaddr;
+        plane[extra_idx].m.userptr = (unsigned long)input_extradata_info.ion[index].uaddr;
 #ifdef USE_ION
-        plane[extra_idx].reserved[MSM_VIDC_BUFFER_FD] = input_extradata_info.ion[extradata_index].data_fd;
+        plane[extra_idx].reserved[MSM_VIDC_BUFFER_FD] = input_extradata_info.ion[index].data_fd;
 #endif
         plane[extra_idx].reserved[MSM_VIDC_DATA_OFFSET] = 0;
         plane[extra_idx].data_offset = 0;
@@ -2967,8 +2965,8 @@
     }
     if (m_debug.extradata_log && extra_idx && (extra_idx < VIDEO_MAX_PLANES)) {
         DEBUG_PRINT_ERROR("Extradata Addr 0x%llx, Buffer Addr = 0x%x",
-            (OMX_U64)input_extradata_info.ion[extradata_index].uaddr, (unsigned int)plane[extra_idx].m.userptr);
-        venc_extradata_log_buffers((char *)plane[extra_idx].m.userptr, extradata_index, true);
+            (OMX_U64)input_extradata_info.ion[index].uaddr, (unsigned int)plane[index].m.userptr);
+        venc_extradata_log_buffers((char *)plane[extra_idx].m.userptr, index, true);
     }
     rc = ioctl(m_nDriver_fd, VIDIOC_QBUF, &buf);
 
@@ -3094,17 +3092,10 @@
             extra_idx = EXTRADATA_IDX(num_input_planes);
 
             if (extra_idx && (extra_idx < VIDEO_MAX_PLANES)) {
-                int fd = plane[0].reserved[MSM_VIDC_BUFFER_FD];
-                OMX_U32 extradata_index;
-                if (!venc_get_index_from_fd(fd, &extradata_index)) {
-                    DEBUG_PRINT_ERROR("Extradata index not found for fd: %d\n", fd);
-                    return false;
-                }
-
                 plane[extra_idx].bytesused = input_extradata_info.buffer_size;
                 plane[extra_idx].length = input_extradata_info.buffer_size;
-                plane[extra_idx].m.userptr = (unsigned long)input_extradata_info.ion[extradata_index].uaddr;
-                plane[extra_idx].reserved[MSM_VIDC_BUFFER_FD] = input_extradata_info.ion[extradata_index].data_fd;
+                plane[extra_idx].m.userptr = (unsigned long)input_extradata_info.ion[v4l2Id].uaddr;
+                plane[extra_idx].reserved[MSM_VIDC_BUFFER_FD] = input_extradata_info.ion[v4l2Id].data_fd;
                 plane[extra_idx].reserved[MSM_VIDC_DATA_OFFSET] = 0;
                 plane[extra_idx].data_offset = 0;
             } else if (extra_idx >= VIDEO_MAX_PLANES) {
@@ -3259,28 +3250,6 @@
     return true;
 }
 
-bool venc_dev::venc_get_index_from_fd(OMX_U32 buffer_fd, OMX_U32 *index)
-{
-    for (unsigned int i = 0; i < (sizeof(fd_list)/sizeof(fd_list[0])); i++) {
-        if (fd_list[i] == buffer_fd) {
-            DEBUG_PRINT_HIGH("FD : %d found at index = %d", buffer_fd, i);
-            *index = i;
-            return true;
-        }
-    }
-
-    for (unsigned int i = 0; i < (sizeof(fd_list)/sizeof(fd_list[0])); i++) {
-        if (fd_list[i] == 0) {
-            DEBUG_PRINT_HIGH("FD : %d added at index = %d", buffer_fd, i);
-            fd_list[i] = buffer_fd;
-            *index = i;
-            return true;
-        }
-    }
-    DEBUG_PRINT_ERROR("Couldn't get index from fd : %d",buffer_fd);
-    return false;
-}
-
 bool venc_dev::venc_set_colorspace(OMX_U32 primaries, OMX_U32 range,
     OMX_U32 transfer_chars, OMX_U32 matrix_coeffs)
 {
diff --git a/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2_params.cpp b/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2_params.cpp
index 0ddc8ce..19731f8 100644
--- a/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2_params.cpp
+++ b/mm-video-v4l2/vidc/venc/src/video_encoder_device_v4l2_params.cpp
@@ -567,8 +567,10 @@
                             return false;
                         }
 
-                        if (num_input_planes > 1)
-                            input_extradata_info.count = m_sInput_buff_property.actualcount + 1;
+                        if (num_input_planes > 1) {
+                            input_extradata_info.count = m_sInput_buff_property.actualcount;
+                            venc_handle->m_client_in_extradata_info.set_extradata_info(input_extradata_info.buffer_size, input_extradata_info.count);
+                        }
 
                         if (!downscalar_enabled) {
                             m_sVenc_cfg.dvs_height = portDefn->format.video.nFrameHeight;
@@ -648,8 +650,10 @@
                             return false;
                         }
 
-                        if (num_output_planes > 1)
+                        if (num_output_planes > 1) {
                             output_extradata_info.count = m_sOutput_buff_property.actualcount;
+                            venc_handle->m_client_out_extradata_info.set_extradata_info(output_extradata_info.buffer_size, output_extradata_info.count);
+                        }
                     } else {
                         DEBUG_PRINT_LOW("venc_set_param: OMX_IndexParamPortDefinition: parameters not changed on port %d",
                             portDefn->nPortIndex);