After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 768248 - vaapiencode: Add Encoding region-of-interest (ROI) support
vaapiencode: Add Encoding region-of-interest (ROI) support
Status: RESOLVED FIXED
Product: GStreamer
Classification: Platform
Component: gstreamer-vaapi
unspecified
Other Linux
: Normal blocker
: 1.13.90
Assigned To: GStreamer Maintainers
GStreamer Maintainers
Depends on: 793338
Blocks:
 
 
Reported: 2016-06-30 14:42 UTC by sreerenj
Modified: 2018-03-01 23:20 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
libs: encoder/context: query region of interest support (4.37 KB, patch)
2017-01-31 08:02 UTC, Hyunjun Ko
none Details | Review
libs: encoder: add api gst_vaapi_encoder_set_roi (3.60 KB, patch)
2017-01-31 08:03 UTC, Hyunjun Ko
none Details | Review
libs: encoder: h264: set ROI params during encoding (2.57 KB, patch)
2017-01-31 08:04 UTC, Hyunjun Ko
none Details | Review
tests: simple-encoder: add an option to set ROI (2.06 KB, patch)
2017-01-31 08:04 UTC, Hyunjun Ko
none Details | Review
libs: encoder/context: query region of interest support (4.92 KB, patch)
2017-02-23 09:57 UTC, Hyunjun Ko
none Details | Review
libs: encoder: add api gst_vaapi_encoder_add/del_roi (4.45 KB, patch)
2017-02-23 09:57 UTC, Hyunjun Ko
none Details | Review
libs: encoder: h264: set ROI params during encoding (2.66 KB, patch)
2017-02-23 09:58 UTC, Hyunjun Ko
none Details | Review
tests: simple-encoder: add an option to set ROI (2.45 KB, patch)
2017-02-23 09:59 UTC, Hyunjun Ko
none Details | Review
libs: encoder: add api gst_vaapi_encoder_add/del_roi (4.30 KB, patch)
2017-03-17 02:09 UTC, Hyunjun Ko
none Details | Review
libs: encoder: add api gst_vaapi_encoder_add/del_roi (5.40 KB, patch)
2017-03-30 09:25 UTC, Hyunjun Ko
none Details | Review
vaapiencode: handle custom event GstVaapiEncoderRegionOfInterest (4.75 KB, patch)
2017-03-30 09:26 UTC, Hyunjun Ko
needs-work Details | Review
tests: elements: add an example for roi (7.46 KB, patch)
2017-03-30 09:27 UTC, Hyunjun Ko
reviewed Details | Review
libs: encoder/context: query region of interest support (4.75 KB, patch)
2017-05-11 17:24 UTC, Víctor Manuel Jáquez Leal
committed Details | Review
libs: encoder: add api gst_vaapi_encoder_add/del_roi (5.42 KB, patch)
2017-05-11 17:24 UTC, Víctor Manuel Jáquez Leal
committed Details | Review
libs: encoder: h264: set ROI params during encoding (2.87 KB, patch)
2017-05-11 17:24 UTC, Víctor Manuel Jáquez Leal
committed Details | Review
tests: simple-encoder: add an option to set ROI (2.73 KB, patch)
2017-05-11 17:24 UTC, Víctor Manuel Jáquez Leal
committed Details | Review
vaapiencode: handle custom event GstVaapiEncoderRegionOfInterest (5.06 KB, patch)
2017-05-11 17:24 UTC, Víctor Manuel Jáquez Leal
committed Details | Review
tests: elements: add an example for ROI (7.21 KB, patch)
2017-05-11 17:25 UTC, Víctor Manuel Jáquez Leal
committed Details | Review
Revert "vaapiencode: handle custom event GstVaapiEncoderRegionOfInterest" (4.93 KB, patch)
2018-02-23 17:58 UTC, Víctor Manuel Jáquez Leal
none Details | Review
Revert "tests: simple-encoder: add an option to set ROI" (2.59 KB, patch)
2018-02-23 17:59 UTC, Víctor Manuel Jáquez Leal
none Details | Review
Revert "libs: encoder: add api gst_vaapi_encoder_add/del_roi" (5.54 KB, patch)
2018-02-23 17:59 UTC, Víctor Manuel Jáquez Leal
none Details | Review
libs: encoder: reimplement ROI using meta (10.87 KB, patch)
2018-02-23 17:59 UTC, Víctor Manuel Jáquez Leal
none Details | Review
plugins: copy input buffer metas (1.18 KB, patch)
2018-02-23 17:59 UTC, Víctor Manuel Jáquez Leal
committed Details | Review
test: wip: roi test (8.03 KB, patch)
2018-02-23 17:59 UTC, Víctor Manuel Jáquez Leal
none Details | Review
test: wip: roi test (8.15 KB, patch)
2018-02-23 18:20 UTC, Víctor Manuel Jáquez Leal
none Details | Review
Revert "vaapiencode: handle custom event GstVaapiEncoderRegionOfInterest" (4.93 KB, patch)
2018-02-27 21:01 UTC, Víctor Manuel Jáquez Leal
committed Details | Review
Revert "tests: simple-encoder: add an option to set ROI" (2.59 KB, patch)
2018-02-27 21:01 UTC, Víctor Manuel Jáquez Leal
committed Details | Review
Revert "libs: encoder: add api gst_vaapi_encoder_add/del_roi" (5.54 KB, patch)
2018-02-27 21:01 UTC, Víctor Manuel Jáquez Leal
committed Details | Review
libs: encoder: reimplement ROI using meta (10.87 KB, patch)
2018-02-27 21:01 UTC, Víctor Manuel Jáquez Leal
none Details | Review
tests: element: rewrite ROI test (8.59 KB, patch)
2018-02-27 21:02 UTC, Víctor Manuel Jáquez Leal
none Details | Review
postproc: Copy meta data from input to output (2.46 KB, patch)
2018-02-27 21:21 UTC, Nicolas Dufresne (ndufresne)
reviewed Details | Review
libs: encoder: reimplement ROI using meta (10.88 KB, patch)
2018-02-27 22:50 UTC, Víctor Manuel Jáquez Leal
committed Details | Review
tests: element: rewrite ROI test (8.61 KB, patch)
2018-02-27 22:50 UTC, Víctor Manuel Jáquez Leal
committed Details | Review

Description sreerenj 2016-06-30 14:42:14 UTC
Add support for QP adjustments in specific(user provided) areas of the frames when encoding.

ROIs can be set through the VAENCMiscParameterBufferROIs. The ROI set through this structure is applicable only to the current frame/field, so must be sent every frame/field to be applied.  The number of supported ROIs can be queried through the VAConfigAttribEncROI.  The encoder will use the ROI information to adjust the QP values of the MB's that fall within the ROIs.

This feature is for giving more control to the user in order to tune the encode quality.
Comment 1 Hyunjun Ko 2017-01-17 07:15:14 UTC
I've been working for this issue and I found some issues to discuss.

1. Handling region of interest meta?
There's already video meta for this, called "GstVideoRegionOfInterestMeta"
But, afaik, there's no element to use this except for opencv element.(face detection)
Does vaapi encoder need to handle this or just create property like roi-x, roi-y, roi-w, roi-h? Or both?

2. Other property to be created in vaapi encoder.
Maybe these 2 properties are necessary to provide.
- enable-roi : Enable/Disable roi encoding.
- roi-value : a kind of level of roi region 
http://01org.github.io/libva_staging_doxygen/struct__VAEncMiscParameterBufferRoi_1_1VAEncROI.html#ab37eca45463295659565a46430b0b925

And not sure if these attributes are necessary or not as a property.
- max/min delta qp : works only if CQP mode
- num_of_roi : if we supports this, we should support for setting of the number of {x,y,w,h}, corresponding to num_of_roi. but how?

Refer to http://01org.github.io/libva_staging_doxygen/struct__VAEncMiscParameterBufferRoi.html


I think I'm going to start implementation of kinda version 0.1, but need opinions about this :)
Comment 2 Víctor Manuel Jáquez Leal 2017-01-17 17:11:16 UTC
From my point of view, the "gst" way of implementing this, is creating a new GST_EVENT_CUSTOM_DOWNSTREAM (perhaps also upstream) that the application could inject into the pipeline with gst_element_send_event().

The event will have an GstStructure named, for example GstVaapiEncoderRegionOfInterest, with roi-x, roi-y, roi-width, roi-height, etc.

The vaapi encoders will catch this event and will configure themselves accordingly.

That's it, from the top of my head.
Comment 3 Hyunjun Ko 2017-01-31 08:02:43 UTC
Created attachment 344625 [details] [review]
libs: encoder/context: query region of interest support

Query Region of Interest support during config creation.
Comment 4 Hyunjun Ko 2017-01-31 08:03:25 UTC
Created attachment 344626 [details] [review]
libs: encoder: add api gst_vaapi_encoder_set_roi

Implements and exposes new api gst_vaapi_encoder_set_roi to set ROI regions.
Comment 5 Hyunjun Ko 2017-01-31 08:04:01 UTC
Created attachment 344627 [details] [review]
libs: encoder: h264: set ROI params during encoding

Set ROI params during encoding each frame,
which is set via gst_vaapi_encoder_set_roi
Comment 6 Hyunjun Ko 2017-01-31 08:04:42 UTC
Created attachment 344628 [details] [review]
tests: simple-encoder: add an option to set ROI

Adds an option "roi" to test roi support of encoder.
Comment 7 Hyunjun Ko 2017-01-31 08:09:23 UTC
Internally, query/set ROI params are implemented in these patches.
Once these can be acceptable, then we can discuss how to provide the way to set ROI params to users. (GstEvent, or anytihng else)

And we can go further to support for other codecs such as vp8.
Comment 8 Hyunjun Ko 2017-02-17 01:47:51 UTC
Ping. 
Victor, Sree? :)
Comment 9 Víctor Manuel Jáquez Leal 2017-02-17 15:41:20 UTC
Review of attachment 344625 [details] [review]:

::: gst-libs/gst/vaapi/gstvaapicontext.c
@@ +314,3 @@
+          GST_ERROR ("ROI unsupported - number of regions supported: %d"
+              " ROI delta QP: %d", roi_config->bits.num_roi_regions,
+              roi_config->bits.roi_rc_qp_delat_support);

I guess you should improve the error message, imo it is misleading because ROI can be supported but not the number of requested regions. Also, I would keep this as a warning and do not break the encoding.

::: gst-libs/gst/vaapi/gstvaapiencoder.c
@@ +573,3 @@
+    GST_INFO ("ROI unsupported - number of regions supported: %d"
+        " ROI delta QP: %d", roi_config->bits.num_roi_regions,
+        roi_config->bits.roi_rc_qp_delat_support);

If ROI is unsupported, there is no reason to print the number of regions or if delta QP is supported, since any of they are zero.

By the way, I guess we should open a bug in libva because of the typo delat == delta, but that will break the API.
Comment 10 Víctor Manuel Jáquez Leal 2017-02-17 15:48:25 UTC
Review of attachment 344626 [details] [review]:

::: gst-libs/gst/vaapi/gstvaapiencoder.c
@@ +1230,3 @@
+
+  if (!config->roi_capability)
+    return;

Turn the function into boolean to report back if it's possible to execute the operation.

@@ +1240,3 @@
+    g_array_free (encoder->roi_regions, FALSE);
+
+  encoder->roi_regions = g_array_new (FALSE, FALSE, sizeof (GstVaapiROI));

As we know the size, we should use g_array_sized_new ()

::: gst-libs/gst/vaapi/gstvaapiencoder.h
@@ +129,3 @@
+  guint w;
+  guint h;
+} GstVaapiROI;

I would reuse the GstVaapiRectangle nested in GstVaapiROI

@@ +187,3 @@
 
+void
+gst_vaapi_encoder_set_roi (GstVaapiEncoder * encoder, GArray * roi_regions);

What about an API to add and delete single ROIs??

gboolean gst_vaapi_encoder_add_roi (GstVaapiEncoder * encoder, GstVaapiROI * roi);
gboolean gst_vaapi_encoder_del_roi (GstVaapiEncoder * encoder, GstVaapiROI * roi);
Comment 11 Víctor Manuel Jáquez Leal 2017-02-17 15:53:20 UTC
Review of attachment 344627 [details] [review]:

lgtm
Comment 12 Víctor Manuel Jáquez Leal 2017-02-17 16:02:49 UTC
Review of attachment 344628 [details] [review]:

lgtm

::: tests/simple-encoder.c
@@ +197,3 @@
+  }
+
+  gst_vaapi_encoder_set_roi (encoder, roi_regions);

Looking at this, I guess that the approach of {add,del}_roi is the way to go.
Comment 13 Víctor Manuel Jáquez Leal 2017-02-17 16:05:01 UTC
Review of attachment 344626 [details] [review]:

::: gst-libs/gst/vaapi/gstvaapiencoder.c
@@ +1227,3 @@
+  guint roi_num, i;
+
+  g_return_if_fail (roi_regions != NULL);

Is is possible to add/delete ROIs when the encoder is already processing a stream? 

If not, we should also rely on the queued_codedbuf_num
Comment 14 Hyunjun Ko 2017-02-21 04:45:46 UTC
(In reply to Víctor Manuel Jáquez Leal from comment #9)
> Review of attachment 344625 [details] [review] [review]:
> 
> ::: gst-libs/gst/vaapi/gstvaapicontext.c
> @@ +314,3 @@
> +          GST_ERROR ("ROI unsupported - number of regions supported: %d"
> +              " ROI delta QP: %d", roi_config->bits.num_roi_regions,
> +              roi_config->bits.roi_rc_qp_delat_support);
> 
> I guess you should improve the error message, imo it is misleading because
> ROI can be supported but not the number of requested regions. Also, I would
> keep this as a warning and do not break the encoding.
 
I think this should be error since the supported roi thing is already queried and set to config(cip->config.encoder). That's why roi supported_num should be same. If we want to update, we should turn off "static" for cip->config.encoder, but current logic isn't aimed at it, I guess.

> ::: gst-libs/gst/vaapi/gstvaapiencoder.c
> @@ +573,3 @@
> +    GST_INFO ("ROI unsupported - number of regions supported: %d"
> +        " ROI delta QP: %d", roi_config->bits.num_roi_regions,
> +        roi_config->bits.roi_rc_qp_delat_support);
> 
> If ROI is unsupported, there is no reason to print the number of regions or
> if delta QP is supported, since any of they are zero.

Agree, I'll change it.

> 
> By the way, I guess we should open a bug in libva because of the typo delat
> == delta, but that will break the API.

Wow, nice catch. I'm going to raise this issue.
Comment 15 Hyunjun Ko 2017-02-21 04:48:20 UTC
(In reply to Víctor Manuel Jáquez Leal from comment #10)
> Review of attachment 344626 [details] [review] [review]:
> 
> ::: gst-libs/gst/vaapi/gstvaapiencoder.c
> @@ +1230,3 @@
> +
> +  if (!config->roi_capability)
> +    return;
> 
> Turn the function into boolean to report back if it's possible to execute
> the operation.
> 
> @@ +1240,3 @@
> +    g_array_free (encoder->roi_regions, FALSE);
> +
> +  encoder->roi_regions = g_array_new (FALSE, FALSE, sizeof (GstVaapiROI));
> 
> As we know the size, we should use g_array_sized_new ()
> 
> ::: gst-libs/gst/vaapi/gstvaapiencoder.h
> @@ +129,3 @@
> +  guint w;
> +  guint h;
> +} GstVaapiROI;
> 
> I would reuse the GstVaapiRectangle nested in GstVaapiROI
> 
> @@ +187,3 @@
>  
> +void
> +gst_vaapi_encoder_set_roi (GstVaapiEncoder * encoder, GArray * roi_regions);
> 
> What about an API to add and delete single ROIs??
> 
> gboolean gst_vaapi_encoder_add_roi (GstVaapiEncoder * encoder, GstVaapiROI *
> roi);
> gboolean gst_vaapi_encoder_del_roi (GstVaapiEncoder * encoder, GstVaapiROI *
> roi);

Well, I like your approach better than mine.
Let me finish this work according to your suggestion.
Comment 16 Hyunjun Ko 2017-02-21 04:50:14 UTC
(In reply to Víctor Manuel Jáquez Leal from comment #13)
> Review of attachment 344626 [details] [review] [review]:
> 
> ::: gst-libs/gst/vaapi/gstvaapiencoder.c
> @@ +1227,3 @@
> +  guint roi_num, i;
> +
> +  g_return_if_fail (roi_regions != NULL);
> 
> Is is possible to add/delete ROIs when the encoder is already processing a
> stream? 
> 
> If not, we should also rely on the queued_codedbuf_num

Yes, it's possible theoretically though I haven't tested it.
Comment 17 Hyunjun Ko 2017-02-23 09:57:24 UTC
Created attachment 346550 [details] [review]
libs: encoder/context: query region of interest support

Query Region of Interest support during config creation.
Comment 18 Hyunjun Ko 2017-02-23 09:57:59 UTC
Created attachment 346551 [details] [review]
libs: encoder: add api gst_vaapi_encoder_add/del_roi

Implements and exposes new api gst_vaapi_encoder_add/del_roi to set ROI regions.
Comment 19 Hyunjun Ko 2017-02-23 09:58:41 UTC
Created attachment 346552 [details] [review]
libs: encoder: h264: set ROI params during encoding

Set ROI params during encoding each frame, which is set via gst_vaapi_encoder_add_roi
Comment 20 Hyunjun Ko 2017-02-23 09:59:10 UTC
Created attachment 346553 [details] [review]
tests: simple-encoder: add an option to set ROI
Comment 21 Víctor Manuel Jáquez Leal 2017-03-16 12:48:50 UTC
Review of attachment 346551 [details] [review]:

::: gst-libs/gst/vaapi/gstvaapiencoder.c
@@ +1211,3 @@
 }
 
+#if 0

Why is this code disabled? Should it be removed?

@@ +1243,3 @@
+#else
+gboolean
+gst_vaapi_encoder_add_roi (GstVaapiEncoder * encoder, GstVaapiROI * roi)

Document this function. Especially if @roi transfers its ownership.

@@ +1263,3 @@
+
+gboolean
+gst_vaapi_encoder_del_roi (GstVaapiEncoder * encoder, GstVaapiROI * roi)

ditto.
Comment 22 Víctor Manuel Jáquez Leal 2017-03-16 12:49:31 UTC
Review of attachment 346550 [details] [review]:

lgtm
Comment 23 Víctor Manuel Jáquez Leal 2017-03-16 12:51:19 UTC
Review of attachment 346552 [details] [review]:

lgtm
Comment 24 Víctor Manuel Jáquez Leal 2017-03-16 12:51:58 UTC
Review of attachment 346553 [details] [review]:

lgtm
Comment 25 Víctor Manuel Jáquez Leal 2017-03-16 12:54:13 UTC
Almost there. Though I would not push this code until we have a way to use it in a pipeline.
Comment 26 Hyunjun Ko 2017-03-17 02:09:54 UTC
Created attachment 348142 [details] [review]
libs: encoder: add api gst_vaapi_encoder_add/del_roi

Implements and exposes new api gst_vaapi_encoder_add/del_roi to set ROI regions.
Comment 27 Hyunjun Ko 2017-03-17 02:21:12 UTC
(In reply to Víctor Manuel Jáquez Leal from comment #25)
> Almost there. Though I would not push this code until we have a way to use
> it in a pipeline.

I'm going to start working on GstEvent to support this.
Comment 28 Hyunjun Ko 2017-03-29 08:34:08 UTC
Recently, seems ROI is broken in the driver. I filed up this issue to
https://github.com/01org/intel-vaapi-driver/issues/108
Comment 29 Hyunjun Ko 2017-03-30 09:25:45 UTC
Created attachment 348978 [details] [review]
libs: encoder: add api gst_vaapi_encoder_add/del_roi

Implements and exposes new api gst_vaapi_encoder_add/del_roi to set ROI regions.


-----
I revised this patch, especially about memory allocation happens inside api.
Please review this again.
Comment 30 Hyunjun Ko 2017-03-30 09:26:24 UTC
Created attachment 348979 [details] [review]
vaapiencode: handle custom event  GstVaapiEncoderRegionOfInterest

Handles new custom event GstVaapiEncoderRegionOfInterest
to enable/disable a ROI region.

Writes a way to use new event to document.
Comment 31 Hyunjun Ko 2017-03-30 09:27:03 UTC
Created attachment 348980 [details] [review]
tests: elements: add an example for roi

This implements a pipleint to recognize difference between ROI and non-ROI.
See comments in this code in detail.
Comment 32 Hyunjun Ko 2017-03-30 09:28:23 UTC
Even though roi is broken on the current driver,
I would request review for a way to enable/disable roi.
Comment 33 Hyunjun Ko 2017-03-30 09:29:51 UTC
(In reply to Hyunjun Ko from comment #31)
> Created attachment 348980 [details] [review] [review]
> tests: elements: add an example for roi
> 
> This implements a pipleint to recognize difference between ROI and non-ROI.
> See comments in this code in detail.

I've been thinking about how to test and verify roi for a while.
This is an example, but if there's a better way, please let me know.
Comment 34 Víctor Manuel Jáquez Leal 2017-04-07 15:45:07 UTC
Review of attachment 348978 [details] [review]:

this patch needs too be rebased because gstvaapiencoder.h has changed. But still lgtm
Comment 35 Víctor Manuel Jáquez Leal 2017-04-07 15:56:14 UTC
Review of attachment 348979 [details] [review]:

::: gst/vaapi/gstvaapiencode.c
@@ +738,3 @@
           (GstTaskFunction) gst_vaapiencode_buffer_loop, encode, NULL);
       break;
+    case GST_EVENT_CUSTOM_DOWNSTREAM:

I wonder if this event should be GST_EVENT_CUSTOM_DOWNSTREAM_OOB (out of band)

@@ +741,3 @@
+    {
+      const GstStructure *s = gst_event_get_structure (event);
+      if (gst_structure_has_name (s, "GstVaapiEncoderRegionOfInterest")) {

I don't know if GstVaapiEncoderRegionOfInterest is a good name. Starting with the namespace. I am not sure if other encoders could enable ROI.

We need to discuss it with the rest of the community. IMO it should be something more like  "region-of-interest"
Comment 36 Víctor Manuel Jáquez Leal 2017-04-07 15:56:39 UTC
Review of attachment 348980 [details] [review]:

This is beautiful! Great!
Comment 37 Víctor Manuel Jáquez Leal 2017-04-25 09:45:38 UTC
I don't know if this is a good timing to merge these patches, since we are in the code freeze window, and the current version of Intel's driver (the only one supporting this feature) has a regression on it (bug #780881).

Perhaps we might delay it until release 1.13/1.14 :S
Comment 38 Víctor Manuel Jáquez Leal 2017-05-10 14:43:14 UTC
I'm going to push these patches now.

Sree has some doubts about the usage of the GstEvent API for this. He thinks we should find a way to emit it at a specific frame.
Comment 39 Víctor Manuel Jáquez Leal 2017-05-11 16:55:44 UTC
Review of attachment 346550 [details] [review]:

::: gst-libs/gst/vaapi/gstvaapiencoder_priv.h
@@ +231,3 @@
+  /* Region of Interest */
+  gboolean got_roi_capability;
+  gboolean roi_capability;

I feel these two variables are not required at all. The roi_capability is repeated in the GstVaapiConfigInfoEncoder, and got_roi_capability doesn't change much, since the query is fast, there's no need of cache it.
Comment 40 Víctor Manuel Jáquez Leal 2017-05-11 16:57:27 UTC
Review of attachment 346552 [details] [review]:

::: gst-libs/gst/vaapi/gstvaapiencoder_h264.c
@@ +2270,3 @@
+    VAEncMiscParameterBufferROI *roi_param;
+    VAEncROI *region_roi, *region_ptr;
+    gint roi_num = g_list_length (base_encoder->roi_regions);

we are repeting twice the call g_list_length(), it would be better if we just call it once, avoiding traversing the list for the same size.
Comment 41 Víctor Manuel Jáquez Leal 2017-05-11 16:59:14 UTC
Review of attachment 346553 [details] [review]:

::: tests/simple-encoder.c
@@ +34,3 @@
 static char **g_input_files = NULL;
+static guint g_roi_enable = 0;
+static GstVaapiROI *g_roi_region[2] = { NULL, };

There's no need to keep this global, it could bie in the App structure.

@@ +191,3 @@
+
+  for (i = 0; i < 2; i++) {
+    g_roi_region[i] = g_malloc0 (sizeof (GstVaapiROI));

we can save this malloc&free using normal array declared in App structure.
Comment 42 Víctor Manuel Jáquez Leal 2017-05-11 17:00:51 UTC
Review of attachment 348978 [details] [review]:

::: gst-libs/gst/vaapi/gstvaapiencoder.c
@@ +1138,3 @@
 
+  if (encoder->roi_regions)
+    g_list_free (encoder->roi_regions);

here's a memory leak, since the allocated data are not freed.

@@ +1307,3 @@
+        region->rect.width == roi->rect.width &&
+        region->rect.height == roi->rect.height) {
+      encoder->roi_regions = g_list_remove (encoder->roi_regions, region);

here's a memory leak: we are not freeing the memory allocated for the GstVaapiROO in _add_roi()
Comment 43 Víctor Manuel Jáquez Leal 2017-05-11 17:03:02 UTC
Review of attachment 348980 [details] [review]:

::: tests/elements/test-roi.c
@@ +147,3 @@
+      " t. ! queue name=second_q ! " CODEC
+      " ! " VIDEO_ELEMENTS "!" TEXTOVERLAY "ROI "
+      " ! videobox border-alpha=0 left=-640 ! mix.", NULL);

this pipeline can be simplified a lot, avoiding many of those videoconverts, and since it is only taking the snow part of videotestsrc, we can ask it to generate only the snow pattern: pattern=snow
Thus we avoid the extract of that area.
Comment 44 Víctor Manuel Jáquez Leal 2017-05-11 17:06:26 UTC
Review of attachment 348979 [details] [review]:

::: gst/vaapi/gstvaapiencode.c
@@ +767,3 @@
+        }
+      }
+      break;

I would move this above the call to the parent, since this call only is handled by us.
Comment 45 Víctor Manuel Jáquez Leal 2017-05-11 17:24:28 UTC
Created attachment 351651 [details] [review]
libs: encoder/context: query region of interest support

Queries if the driver supports "Region of Interest" (ROI) during the config
creation.

  This attribute conveys whether the driver supports region-of-interest (ROI)
  encoding, based on user provided ROI rectangles.  The attribute value is
  partitioned into fields as defined in the VAConfigAttribValEncROI union.

  If ROI encoding is supported, the ROI information is passed to the driver
  using VAEncMiscParameterTypeROI.

https://bugzilla.gnome.org/show_bug.cgi?id=768248

Signed-off-by: Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
Comment 46 Víctor Manuel Jáquez Leal 2017-05-11 17:24:34 UTC
Created attachment 351652 [details] [review]
libs: encoder: add api gst_vaapi_encoder_add/del_roi

Implements and exposes new api gst_vaapi_encoder_add/del_roi to set ROI regions.
Comment 47 Víctor Manuel Jáquez Leal 2017-05-11 17:24:41 UTC
Created attachment 351653 [details] [review]
libs: encoder: h264: set ROI params during encoding

Set ROI params during encoding each frame, which are set via
gst_vaapi_encoder_add_roi ()

https://bugzilla.gnome.org/show_bug.cgi?id=768248

Signed-off-by: Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
Comment 48 Víctor Manuel Jáquez Leal 2017-05-11 17:24:47 UTC
Created attachment 351654 [details] [review]
tests: simple-encoder: add an option to set ROI

$ simple-encoder -r inputfile.y4m

And you'll got an output file in H264 with two regions of interest.

https://bugzilla.gnome.org/show_bug.cgi?id=768248

Signed-off-by: Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
Comment 49 Víctor Manuel Jáquez Leal 2017-05-11 17:24:54 UTC
Created attachment 351655 [details] [review]
vaapiencode: handle custom event GstVaapiEncoderRegionOfInterest

Handles new custom event GstVaapiEncoderRegionOfInterest
to enable/disable a ROI region.

Writes a way to use new event to document.

https://bugzilla.gnome.org/show_bug.cgi?id=768248

Signed-off-by: Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
Comment 50 Víctor Manuel Jáquez Leal 2017-05-11 17:25:01 UTC
Created attachment 351656 [details] [review]
tests: elements: add an example for ROI

This implements a pipleint to recognize difference between ROI and non-ROI.
See comments in this code in detail.

https://bugzilla.gnome.org/show_bug.cgi?id=768248

Signed-off-by: Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
Comment 51 Víctor Manuel Jáquez Leal 2017-05-11 17:30:20 UTC
@Hyunjun: I'll push these patches tomorrow. Please check the modifications I did.
Comment 52 Hyunjun Ko 2017-05-12 06:03:43 UTC
Review of attachment 351652 [details] [review]:

::: gst-libs/gst/vaapi/gstvaapiencoder.c
@@ +1427,3 @@
+      /* Duplicated region */
+      goto end;
+  GstVaapiROI *region = NULL;

This break can be removed. Sorry for this stupid thing.
Comment 53 Hyunjun Ko 2017-05-12 06:04:33 UTC
Review of attachment 351655 [details] [review]:

::: gst/vaapi/gstvaapiencode.c
@@ +741,3 @@
 
+  switch (GST_EVENT_TYPE (event)) {
+    case GST_EVENT_CUSTOM_DOWNSTREAM:{

Maybe you want GST_EVENT_CUSTOM_DOWNSTREAM_OOB :)
Comment 54 Hyunjun Ko 2017-05-12 06:25:57 UTC
(In reply to Víctor Manuel Jáquez Leal from comment #51)
> @Hyunjun: I'll push these patches tomorrow. Please check the modifications I
> did.
Thanks for supporting this work!
Please see my comments.
Comment 55 Víctor Manuel Jáquez Leal 2017-05-12 09:18:18 UTC
Attachment 351651 [details] pushed as b41d72b - libs: encoder/context: query region of interest support
Attachment 351652 [details] pushed as 7a6f690 - libs: encoder: add api gst_vaapi_encoder_add/del_roi
Attachment 351653 [details] pushed as f3302a0 - libs: encoder: h264: set ROI params during encoding
Attachment 351654 [details] pushed as c21345c - tests: simple-encoder: add an option to set ROI
Attachment 351655 [details] pushed as 8f1b88d - vaapiencode: handle custom event GstVaapiEncoderRegionOfInterest
Attachment 351656 [details] pushed as eb17b71 - tests: elements: add an example for ROI
Comment 56 Nicolas Dufresne (ndufresne) 2018-02-12 16:19:27 UTC
I'm reopening this one since I think the custom event has design issues. It does not support ROI hierarchy, does not have ROI ID (making removal ambiguous), removing ROI requires keeping state and setting again the ROI which is highly error prone.

I believe this should use GstVideoRegionOfInterestMeta with the proposal at https://bugzilla.gnome.org/show_bug.cgi?id=793338 . Just a reminder that as a mainline repository, it's important to think about feature to be generally useful. This custom event was done a bit "downstream" style.
Comment 57 Víctor Manuel Jáquez Leal 2018-02-13 10:30:16 UTC
I'm okay reopening this issue. But is it a blocker?

> Just a reminder that as
> a mainline repository, it's important to think about feature to be generally
> useful. This custom event was done a bit "downstream" style.

Agree, but our intention was to have a proof of concept without modifying the core libraries of gstreamer. The problem would be if there are current users of the downstream event. I'm not aware of any. And of course I celebrate the effort to bring this feature to upstream.
Comment 58 Nicolas Dufresne (ndufresne) 2018-02-13 14:12:16 UTC
I don't want the custom event to become part of the supported API.
Comment 59 Víctor Manuel Jáquez Leal 2018-02-21 16:55:03 UTC
Ok. My work plan is to revert attachment 351655 [details] [review] and later rework the code to match with the meta
Comment 60 Nicolas Dufresne (ndufresne) 2018-02-21 18:12:52 UTC
Or just forward port, I was about to look at, shouldn't be very hard really. It's event less work since you no longer need to keep a state.
Comment 61 Víctor Manuel Jáquez Leal 2018-02-21 18:22:39 UTC
Do you have a test app for the roimeta? An app that injects roimeta through identity or something?
Comment 62 Nicolas Dufresne (ndufresne) 2018-02-21 18:28:10 UTC
(In reply to Víctor Manuel Jáquez Leal from comment #61)
> Do you have a test app for the roimeta? An app that injects roimeta through
> identity or something?

We have one for roi/om-zynq, which I'll try to get ready for 1.14. We use facedetect ! omxh264enc with a pad probe in between. We have other elements (HW accelerated) that started injecting ROI. I think textoverlay should add an ROI too.
Comment 63 Víctor Manuel Jáquez Leal 2018-02-23 17:58:57 UTC
Created attachment 368838 [details] [review]
Revert "vaapiencode: handle custom event GstVaapiEncoderRegionOfInterest"

This reverts commit 8f1b88dac0e64a211325cdcb2cda693b80229bd1.
Comment 64 Víctor Manuel Jáquez Leal 2018-02-23 17:59:04 UTC
Created attachment 368839 [details] [review]
Revert "tests: simple-encoder: add an option to set ROI"

This reverts commit c21345c4787bb6342adddea1190f53fe62abff04.
Comment 65 Víctor Manuel Jáquez Leal 2018-02-23 17:59:21 UTC
Created attachment 368840 [details] [review]
Revert "libs: encoder: add api gst_vaapi_encoder_add/del_roi"

This reverts commit 7a6f690340dcb3b82c59efa777d4453227851de8.
Comment 66 Víctor Manuel Jáquez Leal 2018-02-23 17:59:36 UTC
Created attachment 368841 [details] [review]
libs: encoder: reimplement ROI using meta

Check input buffers for ROI metas and pass them to VA. Also added a
new "default-roi-delta-qp" property in order to tell the encoder what
delta QP should be applied to ROI by default.

Enabled it for H264 and H265 encoders.
Comment 67 Víctor Manuel Jáquez Leal 2018-02-23 17:59:42 UTC
Created attachment 368842 [details] [review]
plugins: copy input buffer metas

When importing buffers to a VA-base buffer, it is required to copy
the metas in the original buffer, otherwise information will be
lost, such as GstVideoRegionOfInterestMeta.
Comment 68 Víctor Manuel Jáquez Leal 2018-02-23 17:59:50 UTC
Created attachment 368843 [details] [review]
test: wip: roi test
Comment 69 Víctor Manuel Jáquez Leal 2018-02-23 18:20:30 UTC
Created attachment 368845 [details] [review]
test: wip: roi test
Comment 70 Víctor Manuel Jáquez Leal 2018-02-23 18:23:59 UTC
with intel-vaapi-driver in kabylake (my laptop) H264 doesn't support ROI anymore, but H265 does. I enabled H265, but my test is very flaky.

but at least it handles the same API.
Comment 71 Nicolas Dufresne (ndufresne) 2018-02-23 18:33:29 UTC
Thanks Victor, I'll review today. Do you have some reference on which HW will support ROI ?
Comment 72 Víctor Manuel Jáquez Leal 2018-02-26 03:57:30 UTC
There's a tool written by Mark Thomson that returns the capabilities of the used driver.

I modified a bit for the libva's ROI typo :)

https://people.igalia.com/vjaquez/vadumpcaps.c
Comment 73 Nicolas Dufresne (ndufresne) 2018-02-27 19:40:21 UTC
Review of attachment 368841 [details] [review]:

Looks good.
Comment 74 Nicolas Dufresne (ndufresne) 2018-02-27 19:41:09 UTC
Review of attachment 368842 [details] [review]:

So familiar ;-P
Comment 75 Nicolas Dufresne (ndufresne) 2018-02-27 19:41:55 UTC
Review of attachment 368845 [details] [review]:

What's needed to finish this ?
Comment 76 Nicolas Dufresne (ndufresne) 2018-02-27 19:43:06 UTC
It looks all good to me, just need to finish the test I guess.
Comment 77 Nicolas Dufresne (ndufresne) 2018-02-27 20:12:42 UTC
I think that the ROI seems to get lost along the way in:

gst-launch-1.0 -e v4l2src ! videoconvert ! facedetect profile=haarcascade_frontalface_default.xml ! videoconvert ! queue ! vaapipostproc ! vaapih264enc default-roi-delta-qp=10 rate-control=cbr bitrate=512 ! ...

like if "plugins: copy input buffer metas" had no effect.
Comment 78 Víctor Manuel Jáquez Leal 2018-02-27 20:31:02 UTC
(In reply to Nicolas Dufresne (stormer) from comment #77)
> I think that the ROI seems to get lost along the way in:
> 
> gst-launch-1.0 -e v4l2src ! videoconvert ! facedetect
> profile=haarcascade_frontalface_default.xml ! videoconvert ! queue !
> vaapipostproc ! vaapih264enc default-roi-delta-qp=10 rate-control=cbr
> bitrate=512 ! ...
> 
> like if "plugins: copy input buffer metas" had no effect.

Did you check with vadumpcaps if your driver/backend supports roi with h264??

In my kabylake it doesn't, but it does for h265
Comment 79 Víctor Manuel Jáquez Leal 2018-02-27 20:37:15 UTC
Comment on attachment 368842 [details] [review]
plugins: copy input buffer metas

Attachment 368842 [details] pushed as ba28c6cf - plugins: copy input buffer metas
Comment 80 Víctor Manuel Jáquez Leal 2018-02-27 21:01:44 UTC
Created attachment 369063 [details] [review]
Revert "vaapiencode: handle custom event GstVaapiEncoderRegionOfInterest"

This reverts commit 8f1b88dac0e64a211325cdcb2cda693b80229bd1.
Comment 81 Víctor Manuel Jáquez Leal 2018-02-27 21:01:49 UTC
Created attachment 369064 [details] [review]
Revert "tests: simple-encoder: add an option to set ROI"

This reverts commit c21345c4787bb6342adddea1190f53fe62abff04.
Comment 82 Víctor Manuel Jáquez Leal 2018-02-27 21:01:53 UTC
Created attachment 369065 [details] [review]
Revert "libs: encoder: add api gst_vaapi_encoder_add/del_roi"

This reverts commit 7a6f690340dcb3b82c59efa777d4453227851de8.
Comment 83 Víctor Manuel Jáquez Leal 2018-02-27 21:01:58 UTC
Created attachment 369066 [details] [review]
libs: encoder: reimplement ROI using meta

Check input buffers for ROI metas and pass them to VA. Also added a
new "default-roi-delta-qp" property in order to tell the encoder what
delta QP should be applied to ROI by default.

Enabled it for H264 and H265 encoders.
Comment 84 Víctor Manuel Jáquez Leal 2018-02-27 21:02:03 UTC
Created attachment 369067 [details] [review]
tests: element: rewrite ROI test

Rewrote the ROI test to use GstVideoRegionOfInterest meta rather
than injecting GstEvents. These meta are added as a pad probe in
the queue src pad.

Also

* Use of navigation messages to control de test
* Use signal watch for processing messages
* Change to H265 rather than H264 since current intel-vaapi-driver
  only supports ROI on kabylake.
  TODO: add a parameter to change the encoder/decoder to test.
Comment 85 Nicolas Dufresne (ndufresne) 2018-02-27 21:09:44 UTC
(In reply to Víctor Manuel Jáquez Leal from comment #78)
> (In reply to Nicolas Dufresne (stormer) from comment #77)
> > I think that the ROI seems to get lost along the way in:
> > 
> > gst-launch-1.0 -e v4l2src ! videoconvert ! facedetect
> > profile=haarcascade_frontalface_default.xml ! videoconvert ! queue !
> > vaapipostproc ! vaapih264enc default-roi-delta-qp=10 rate-control=cbr
> > bitrate=512 ! ...
> > 
> > like if "plugins: copy input buffer metas" had no effect.
> 
> Did you check with vadumpcaps if your driver/backend supports roi with h264??
> 
> In my kabylake it doesn't, but it does for h265

No, but I can confirm that vaapipostproc does not copy the meta, fixing it. I've just validating the ROI makes it to the encoder. The code seems to think that this is supported.
Comment 86 Nicolas Dufresne (ndufresne) 2018-02-27 21:21:16 UTC
Created attachment 369068 [details] [review]
postproc: Copy meta data from input to output

This will ensure that meta data without memory tags will be copied. This
was noticed when testing ROI.
Comment 87 Nicolas Dufresne (ndufresne) 2018-02-27 21:21:47 UTC
I believe this patch is correct, it does copy the ROI, but now it crashes badly.
Comment 88 Nicolas Dufresne (ndufresne) 2018-02-27 21:25:41 UTC
I see this in valgrind (and more):

==421== Invalid write of size 2
==421==    at 0x360DEAD7: gst_vaapi_encoder_ensure_param_roi_regions (gstvaapiencoder.c:341)
==421==    by 0x360E8DC2: ensure_misc_params (gstvaapiencoder_h264.c:2540)
==421==    by 0x360E8DC2: gst_vaapi_encoder_h264_encode (gstvaapiencoder_h264.c:2867)
==421==    by 0x360DED40: gst_vaapi_encoder_put_frame (gstvaapiencoder.c:512)
==421==    by 0x360B3EB9: gst_vaapiencode_handle_frame (gstvaapiencode.c:691)
==421==    by 0xF856F8E: gst_video_encoder_chain (gstvideoencoder.c:1548)
==421==    by 0x4EBA69A: gst_pad_chain_data_unchecked (gstpad.c:4275)
==421==    by 0x4EBA69A: gst_pad_push_data (gstpad.c:4531)
==421==    by 0x4EC2802: gst_pad_push (gstpad.c:4650)
==421==    by 0xF5EDA1F: gst_base_transform_chain (gstbasetransform.c:2321)
==421==    by 0x4EBA69A: gst_pad_chain_data_unchecked (gstpad.c:4275)
==421==    by 0x4EBA69A: gst_pad_push_data (gstpad.c:4531)
==421==    by 0x4EC2802: gst_pad_push (gstpad.c:4650)
==421==    by 0xF5EDA1F: gst_base_transform_chain (gstbasetransform.c:2321)
==421==    by 0x4EBA69A: gst_pad_chain_data_unchecked (gstpad.c:4275)
==421==    by 0x4EBA69A: gst_pad_push_data (gstpad.c:4531)
==421==  Address 0xedc2768 is 168 bytes inside an unallocated block of size 1,072 in arena "client"

Which leads to this (probably just a side effect):
==421== Invalid read of size 2
==421==    at 0x389A3ECA: intel_encoder_check_roi_parameter (i965_encoder.c:671)
==421==    by 0x389A3ECA: intel_encoder_check_brc_parameter (i965_encoder.c:726)
==421==    by 0x389A3ECA: intel_encoder_check_misc_parameter (i965_encoder.c:861)
==421==    by 0x389A3ECA: intel_encoder_sanity_check_input (i965_encoder.c:1335)
==421==    by 0x389A3ECA: intel_encoder_end_picture (i965_encoder.c:1351)
==421==    by 0x3634B28F: vaEndPicture (va.c:1316)
==421==    by 0x360EE4AF: gst_vaapi_enc_picture_encode (gstvaapiencoder_objects.c:590)
==421==    by 0x360E926C: gst_vaapi_encoder_h264_encode (gstvaapiencoder_h264.c:2873)
==421==    by 0x360DED40: gst_vaapi_encoder_put_frame (gstvaapiencoder.c:512)
==421==    by 0x360B3EB9: gst_vaapiencode_handle_frame (gstvaapiencode.c:691)
==421==    by 0xF856F8E: gst_video_encoder_chain (gstvideoencoder.c:1548)
==421==    by 0x4EBA69A: gst_pad_chain_data_unchecked (gstpad.c:4275)
==421==    by 0x4EBA69A: gst_pad_push_data (gstpad.c:4531)
==421==    by 0x4EC2802: gst_pad_push (gstpad.c:4650)
==421==    by 0xF5EDA1F: gst_base_transform_chain (gstbasetransform.c:2321)
==421==    by 0x4EBA69A: gst_pad_chain_data_unchecked (gstpad.c:4275)
==421==    by 0x4EBA69A: gst_pad_push_data (gstpad.c:4531)
==421==    by 0x4EC2802: gst_pad_push (gstpad.c:4650)
==421==  Address 0xedc2768 is 24 bytes after a block of size 32 in arena "client"
Comment 89 Nicolas Dufresne (ndufresne) 2018-02-27 21:34:49 UTC
Review of attachment 369066 [details] [review]:

Found what cause the crashes.

::: gst-libs/gst/vaapi/gstvaapiencoder.c
@@ +311,3 @@
+
+  region_roi = (VAEncROI *) misc->param + sizeof (VAEncMiscParameterBuffer) +
+      sizeof (VAEncMiscParameterBufferROI);

Should be:
  region_roi = (VAEncROI *) ((guint8*) misc->param + sizeof (VAEncMiscParameterBuffer) +
      sizeof (VAEncMiscParameterBufferROI));

Otherwise you are adding VAEncROI instead of bytes. There use to be a guchar* cast in the old code.
Comment 90 Víctor Manuel Jáquez Leal 2018-02-27 21:55:43 UTC
<3!!! :)
Comment 91 Víctor Manuel Jáquez Leal 2018-02-27 22:50:16 UTC
Created attachment 369071 [details] [review]
libs: encoder: reimplement ROI using meta

Check input buffers for ROI metas and pass them to VA. Also added a
new "default-roi-delta-qp" property in order to tell the encoder what
delta QP should be applied to ROI by default.

Enabled it for H264 and H265 encoders.
Comment 92 Víctor Manuel Jáquez Leal 2018-02-27 22:50:25 UTC
Created attachment 369072 [details] [review]
tests: element: rewrite ROI test

Rewrote the ROI test to use GstVideoRegionOfInterest meta rather
than injecting GstEvents. These meta are added as a pad probe in
the queue src pad.

Also

* Use of navigation messages to control de test
* Use signal watch for processing messages
* Change to H265 rather than H264 since current intel-vaapi-driver
  only supports ROI on kabylake.
  TODO: add a parameter to change the encoder/decoder to test.
Comment 93 Nicolas Dufresne (ndufresne) 2018-02-27 22:54:26 UTC
Thanks for porting and fixing the bugs with copying the params. This is greatly
appriciated. So I merged this port now, I think we can forward patch if we find
any issues. So far it does not have any bad effect, which is the goal. My Ivibridge
here pretends to do ROI on H264, but I could got get any visually visible effect
so this might be a lie. But I could calidate that the information seems to travel
to the encoder. I'll try again on kabylake later.

Attachment 369063 [details] pushed as 25c2a0d - Revert "vaapiencode: handle custom event GstVaapiEncoderRegionOfInterest"
Attachment 369064 [details] pushed as 35226db - Revert "tests: simple-encoder: add an option to set ROI"
Attachment 369065 [details] pushed as d311071 - Revert "libs: encoder: add api gst_vaapi_encoder_add/del_roi"
Attachment 369068 [details] pushed as fde5500 - postproc: Copy meta data from input to output
Attachment 369071 [details] pushed as c49a17e - libs: encoder: reimplement ROI using meta
Attachment 369072 [details] pushed as e82fafc - tests: element: rewrite ROI test
Comment 94 Víctor Manuel Jáquez Leal 2018-02-27 22:54:54 UTC
Review of attachment 369068 [details] [review]:

Thanks!

But it is incomplete, deinterlaced buffers are not considered. I can update the patch later :)
Comment 95 Nicolas Dufresne (ndufresne) 2018-02-28 02:38:54 UTC
I'm not sure how, but I guess I'll understand when I see your patch. It is relatively minor, I mainly wanted to restore the feature before today's freeze.  That also makes me wonder if meta traverse the software deinterlacer properly.
Comment 96 Víctor Manuel Jáquez Leal 2018-03-01 23:20:35 UTC
I have pushed a commit refactoring the way how the metadata is copied to vpp output buffers.