omxvideo(enc): use GST_VIDEO_INFO_FIELD_RATE_N()
authorGuillaume Desmottes <guillaume.desmottes@collabora.com>
Thu, 6 Feb 2020 04:27:48 +0000 (09:57 +0530)
committerGuillaume Desmottes <guillaume.desmottes@collabora.com>
Thu, 27 Feb 2020 04:05:48 +0000 (04:05 +0000)
Does not change anything for now but will be needed when we'll support
interlace-mode=alternate as the field rate will be twice the frame rate.

Made the code safe from division by 0 while I was on it.

omx/gstomxvideo.c
omx/gstomxvideoenc.c

index 8bbee9e..28d7bd1 100644 (file)
@@ -238,7 +238,13 @@ gst_omx_video_calculate_framerate_q16 (GstVideoInfo * info)
 {
   g_assert (info);
 
-  return gst_util_uint64_scale_int (1 << 16, info->fps_n, info->fps_d);
+  if (!info->fps_d)
+    return 0;
+
+  /* OMX API expects frame rate to actually be the field rate, so twice
+   * the frame rate in interlace mode. */
+  return gst_util_uint64_scale_int (1 << 16, GST_VIDEO_INFO_FIELD_RATE_N (info),
+      info->fps_d);
 }
 
 gboolean
index 99f5952..03a006e 100644 (file)
@@ -2349,10 +2349,12 @@ gst_omx_video_enc_framerate_changed (GstOMXVideoEnc * self,
 
     GST_OMX_INIT_STRUCT (&config);
     config.nPortIndex = self->enc_in_port->index;
-    if (klass->cdata.hacks & GST_OMX_HACK_VIDEO_FRAMERATE_INTEGER)
-      config.xEncodeFramerate = info->fps_n ? (info->fps_n) / (info->fps_d) : 0;
-    else
+    if (klass->cdata.hacks & GST_OMX_HACK_VIDEO_FRAMERATE_INTEGER) {
+      config.xEncodeFramerate =
+          info->fps_d ? GST_VIDEO_INFO_FIELD_RATE_N (info) / (info->fps_d) : 0;
+    } else {
       config.xEncodeFramerate = gst_omx_video_calculate_framerate_q16 (info);
+    }
 
     err = gst_omx_component_set_config (self->enc,
         OMX_IndexConfigVideoFramerate, &config);
@@ -2456,12 +2458,13 @@ gst_omx_video_enc_set_format (GstVideoEncoder * encoder,
   port_def.format.video.nFrameWidth = info->width;
   port_def.format.video.nFrameHeight = GST_VIDEO_INFO_FIELD_HEIGHT (info);
 
-  if (G_UNLIKELY (klass->cdata.hacks & GST_OMX_HACK_VIDEO_FRAMERATE_INTEGER))
+  if (G_UNLIKELY (klass->cdata.hacks & GST_OMX_HACK_VIDEO_FRAMERATE_INTEGER)) {
     port_def.format.video.xFramerate =
-        info->fps_n ? (info->fps_n) / (info->fps_d) : 0;
-  else
+        info->fps_d ? GST_VIDEO_INFO_FIELD_RATE_N (info) / (info->fps_d) : 0;
+  } else {
     port_def.format.video.xFramerate =
         gst_omx_video_calculate_framerate_q16 (info);
+  }
 
   GST_DEBUG_OBJECT (self, "Setting inport port definition");
   if (gst_omx_port_update_port_definition (self->enc_in_port,