summaryrefslogtreecommitdiff
path: root/tests/examples/test-roi.c
blob: 2ce614efa5dfd7a733d1ecd3e880b694a910ae05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 *  test-roi.c - Testsuite for Region of Interest
 *
 *  Copyright (C) 2017 Intel Corporation
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1
 *  of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301 USA
 */

#include <stdio.h>
#include <gst/gst.h>
#include <gst/video/navigation.h>
#include <gst/video/gstvideometa.h>

typedef struct _CustomData
{
  GstElement *pipeline;
  GMainLoop *loop;
  gboolean roi_enabled;
} AppData;

static void
send_eos_event (AppData * data)
{
  gst_element_send_event (data->pipeline, gst_event_new_eos ());
}

static void
dispatch_keystroke (AppData * app, const gchar * str)
{
  switch (g_ascii_tolower (str[0])) {
    case 'r':
      app->roi_enabled = !app->roi_enabled;
      gst_println ("ROI %s", app->roi_enabled ? "enabled" : "disabled");
      break;
    case 'q':
      send_eos_event (app);
      break;
    default:
      break;
  }

  return;
}

static void
cb_msg (GstBus * bus, GstMessage * msg, gpointer data)
{
  AppData *app = data;
  GstNavigationMessageType mtype = gst_navigation_message_get_type (msg);
  GstEvent *ev = NULL;
  GstNavigationEventType type;
  const gchar *key;

  if (mtype != GST_NAVIGATION_MESSAGE_EVENT)
    return;
  if (!gst_navigation_message_parse_event (msg, &ev))
    goto bail;

  type = gst_navigation_event_get_type (ev);
  if (type != GST_NAVIGATION_EVENT_KEY_PRESS)
    goto bail;
  if (!gst_navigation_event_parse_key_event (ev, &key))
    goto bail;

  dispatch_keystroke (app, key);

bail:
  if (ev)
    gst_event_unref (ev);
}

static void
cb_msg_eos (GstBus * bus, GstMessage * msg, gpointer data)
{
  AppData *app = data;
  g_main_loop_quit (app->loop);
}


static void
cb_msg_error (GstBus * bus, GstMessage * msg, gpointer data)
{
  AppData *app = data;
  gchar *debug = NULL;
  GError *err = NULL;

  gst_message_parse_error (msg, &err, &debug);

  g_print ("Error: %s\n", err->message);
  g_error_free (err);

  if (debug) {
    g_print ("Debug details: %s\n", debug);
    g_free (debug);
  }

  g_main_loop_quit (app->loop);
}

static GstPadProbeReturn
cb_add_roi (GstPad * pad, GstPadProbeInfo * info, gpointer data)
{
  AppData *app = data;
  GstVideoRegionOfInterestMeta *rmeta;
  GstBuffer *buf = GST_PAD_PROBE_INFO_BUFFER (info);
  GstStructure *s;

  if (!app->roi_enabled)
    return GST_PAD_PROBE_OK;

  buf = gst_buffer_make_writable (buf);
  if (!buf)
    return GST_PAD_PROBE_OK;

  rmeta =
      gst_buffer_add_video_region_of_interest_meta (buf, "test", 0, 0, 320,
      240);
  if (!rmeta)
    return GST_PAD_PROBE_OK;

  s = gst_structure_new ("roi/vaapi", "delta-qp", G_TYPE_INT, -10, NULL);
  gst_video_region_of_interest_meta_add_param (rmeta, s);

  GST_PAD_PROBE_INFO_DATA (info) = buf;
  return GST_PAD_PROBE_OK;
}

/* Process keyboard input */
static gboolean
handle_keyboard (GIOChannel * source, GIOCondition cond, gpointer data)
{
  AppData *app = data;
  gchar *str = NULL;

  if (g_io_channel_read_line (source, &str, NULL, NULL,
          NULL) != G_IO_STATUS_NORMAL) {
    return TRUE;
  }

  dispatch_keystroke (app, str);

  g_free (str);
  return TRUE;
}

/*
 * This is an example pipeline to recognize difference between ROI and non-ROI.
 * 1. Produce snow pattern with 320p
 * 2. Encode and decode the raw data with 2 pipelines at same time.
 *    2.1. Insert GstVideoRegionOfInterestMeta to the 2nd pipeline buffers to enable ROI.
 * 3. Mix both streams in videomixer.
 * 5. Output the result in one window.
 *
 * Note that the higher definition of original raw data, the easier we
 * recognize.  So you can replace videotestsrc with your
 * high-definition camera or other src elements.
 */

/*
.----------.  .---.     .--------.  .---.  .---.  .---.  .--------.  .----------.  .-----.
| videosrc |->|tee|->Q->|txtovrly|->|enc|->|dec|->|vpp|->|videobox|->|videomixer|->|vsink|
'----------'  '---'     '--------'  '---'  '---'  '---'  '--------'  '----------'  '-----'
                ^                                                    ^
                |                                                    |
                |       .--------.  .---.  .---.  .---.  .--------.  |
                '--->Q->|txtovrly|->|enc|->|dec|->|vpp|->|videobox|->'
                     ^  '--------'  '---'  '---'  '---'  '--------'
                     |
                     '-- Insert GstVideoRegionOfInterestMeta width roit/vaapi params on buffers
*/

int
main (int argc, char *argv[])
{
  AppData data = { 0, };
  GstStateChangeReturn ret;
  GstElement *el;
  GstPad *pad;
  GError *err = NULL;
  GIOChannel *io_stdin;
  GstBus *bus;

  data.roi_enabled = TRUE;

  /* Initialize GStreamer */
  gst_init (&argc, &argv);

  /* Print usage map */
  g_print ("USAGE: 'r' to enable/disable ROI && 'q' to quit\n");

#define SRC "videotestsrc pattern=snow ! " \
            "video/x-raw, format=NV12, width=320, framerate=5/1"
#define ENCDEC "vaapih265enc rate-control=cbr bitrate=2000 ! vaapih265dec ! " \
               "vaapipostproc ! video/x-raw, width=640"
#define TEXT "textoverlay font-desc=\"Arial Bold 48\" "

  data.pipeline =
      gst_parse_launch
      ("videomixer name=mix ! vaapipostproc ! vaapisink sync=false "
      SRC " ! tee name=t ! queue ! " TEXT " text=\"non-ROI\" ! " ENCDEC
      " ! videobox left=-640 ! mix. "
      " t. ! queue name=roi ! " TEXT " text=\"ROI\" ! " ENCDEC
      " ! videobox ! mix.", &err);

  if (err) {
    g_printerr ("failed to parse pipeline: %s\n", err->message);
    g_error_free (err);
    return -1;
  }

  bus = gst_pipeline_get_bus (GST_PIPELINE (data.pipeline));
  gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
  gst_bus_enable_sync_message_emission (bus);
  g_signal_connect (bus, "message::error", G_CALLBACK (cb_msg_error), &data);
  g_signal_connect (bus, "message::eos", G_CALLBACK (cb_msg_eos), &data);
  g_signal_connect (bus, "message::element", G_CALLBACK (cb_msg), &data);
  gst_object_unref (bus);

  el = gst_bin_get_by_name (GST_BIN (data.pipeline), "roi");
  pad = gst_element_get_static_pad (el, "src");
  gst_object_unref (el);
  gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BUFFER, cb_add_roi, &data, NULL);
  gst_object_unref (pad);

  /* Add a keyboard watch so we get notified of keystrokes */
  io_stdin = g_io_channel_unix_new (fileno (stdin));
  g_io_add_watch (io_stdin, G_IO_IN, handle_keyboard, &data);

  /* Start playing */
  ret = gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
  if (ret == GST_STATE_CHANGE_FAILURE) {
    g_printerr ("Unable to set the pipeline to the playing state.\n");
    gst_object_unref (data.pipeline);
    return -1;
  }

  /* Create a GLib Main Loop and set it to run */
  data.loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (data.loop);

  /* Free resources */
  g_main_loop_unref (data.loop);
  gst_element_set_state (data.pipeline, GST_STATE_NULL);
  gst_object_unref (data.pipeline);
  g_io_channel_unref (io_stdin);

  return 0;
}