|
| 1 | +/* GStreamer |
| 2 | + * |
| 3 | + * Copyright (c) 2010 Sebastian Dröge <[email protected]> |
| 4 | + * Copyright (c) 2010 David Schleef <[email protected]> |
| 5 | + * Copyright (c) 2014 Thijs Vermeir <[email protected]> |
| 6 | + * Copyright (c) 2021 Stéphane Cerveau <[email protected]> |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Library General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Library General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Library General Public |
| 19 | + * License along with this library; if not, write to the |
| 20 | + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 21 | + * Boston, MA 02110-1301, USA. |
| 22 | + */ |
| 23 | + |
| 24 | +#include <gst/check/gstcheck.h> |
| 25 | + |
| 26 | +static GstStaticPadTemplate enc_sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink", |
| 27 | + GST_PAD_SINK, |
| 28 | + GST_PAD_ALWAYS, |
| 29 | + GST_STATIC_CAPS ("image/x-j2c, " |
| 30 | + "width = (int) [16, MAX], " |
| 31 | + "height = (int) [16, MAX], " "framerate = (fraction) [0, MAX]")); |
| 32 | + |
| 33 | +static GstStaticPadTemplate enc_srctemplate = GST_STATIC_PAD_TEMPLATE ("src", |
| 34 | + GST_PAD_SRC, |
| 35 | + GST_PAD_ALWAYS, |
| 36 | + GST_STATIC_CAPS ("video/x-raw, " |
| 37 | + "format = (string) I420, " |
| 38 | + "width = (int) [16, MAX], " |
| 39 | + "height = (int) [16, MAX], " "framerate = (fraction) [0, MAX]")); |
| 40 | + |
| 41 | +#define MAX_THREADS 8 |
| 42 | +#define NUM_BUFFERS 4 |
| 43 | +#define FRAME_RATE 1000 |
| 44 | + |
| 45 | +static GstPad *sinkpad, *srcpad; |
| 46 | + |
| 47 | +typedef struct _OpenJPEGData |
| 48 | +{ |
| 49 | + GMainLoop *loop; |
| 50 | + gboolean failing_pipeline; |
| 51 | +} OpenJPEGData; |
| 52 | + |
| 53 | +static GstElement * |
| 54 | +setup_openjpegenc (const gchar * src_caps_str, gint num_stripes) |
| 55 | +{ |
| 56 | + GstElement *openjpegenc; |
| 57 | + GstCaps *srccaps = NULL; |
| 58 | + GstBus *bus; |
| 59 | + |
| 60 | + if (src_caps_str) { |
| 61 | + srccaps = gst_caps_from_string (src_caps_str); |
| 62 | + fail_unless (srccaps != NULL); |
| 63 | + } |
| 64 | + |
| 65 | + openjpegenc = gst_check_setup_element ("openjpegenc"); |
| 66 | + fail_unless (openjpegenc != NULL); |
| 67 | + g_object_set (openjpegenc, "num-stripes", num_stripes, NULL); |
| 68 | + srcpad = gst_check_setup_src_pad (openjpegenc, &enc_srctemplate); |
| 69 | + sinkpad = gst_check_setup_sink_pad (openjpegenc, &enc_sinktemplate); |
| 70 | + gst_pad_set_active (srcpad, TRUE); |
| 71 | + gst_pad_set_active (sinkpad, TRUE); |
| 72 | + |
| 73 | + gst_check_setup_events (srcpad, openjpegenc, srccaps, GST_FORMAT_TIME); |
| 74 | + |
| 75 | + bus = gst_bus_new (); |
| 76 | + gst_element_set_bus (openjpegenc, bus); |
| 77 | + |
| 78 | + fail_unless (gst_element_set_state (openjpegenc, |
| 79 | + GST_STATE_PLAYING) != GST_STATE_CHANGE_FAILURE, |
| 80 | + "could not set to playing"); |
| 81 | + |
| 82 | + if (srccaps) |
| 83 | + gst_caps_unref (srccaps); |
| 84 | + |
| 85 | + buffers = NULL; |
| 86 | + return openjpegenc; |
| 87 | +} |
| 88 | + |
| 89 | +static void |
| 90 | +cleanup_openjpegenc (GstElement * openjpegenc) |
| 91 | +{ |
| 92 | + GstBus *bus; |
| 93 | + |
| 94 | + /* Free parsed buffers */ |
| 95 | + gst_check_drop_buffers (); |
| 96 | + |
| 97 | + bus = GST_ELEMENT_BUS (openjpegenc); |
| 98 | + gst_bus_set_flushing (bus, TRUE); |
| 99 | + gst_object_unref (bus); |
| 100 | + |
| 101 | + gst_pad_set_active (srcpad, FALSE); |
| 102 | + gst_pad_set_active (sinkpad, FALSE); |
| 103 | + gst_check_teardown_src_pad (openjpegenc); |
| 104 | + gst_check_teardown_sink_pad (openjpegenc); |
| 105 | + gst_check_teardown_element (openjpegenc); |
| 106 | +} |
| 107 | + |
| 108 | +GST_START_TEST (test_openjpeg_encode_simple) |
| 109 | +{ |
| 110 | + GstElement *openjpegenc; |
| 111 | + GstBuffer *buffer; |
| 112 | + gint i; |
| 113 | + GList *l; |
| 114 | + GstCaps *outcaps, *sinkcaps; |
| 115 | + GstSegment seg; |
| 116 | + |
| 117 | + openjpegenc = |
| 118 | + setup_openjpegenc |
| 119 | + ("video/x-raw,format=(string)I420,width=(int)320,height=(int)240,framerate=(fraction)25/1", |
| 120 | + 1); |
| 121 | + |
| 122 | + gst_segment_init (&seg, GST_FORMAT_TIME); |
| 123 | + seg.stop = gst_util_uint64_scale (10, GST_SECOND, 25); |
| 124 | + |
| 125 | + fail_unless (gst_pad_push_event (srcpad, gst_event_new_segment (&seg))); |
| 126 | + |
| 127 | + buffer = gst_buffer_new_allocate (NULL, 320 * 240 + 2 * 160 * 120, NULL); |
| 128 | + gst_buffer_memset (buffer, 0, 0, -1); |
| 129 | + |
| 130 | + for (i = 0; i < 10; i++) { |
| 131 | + GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (i, GST_SECOND, 25); |
| 132 | + GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (1, GST_SECOND, 25); |
| 133 | + fail_unless (gst_pad_push (srcpad, gst_buffer_ref (buffer)) == GST_FLOW_OK); |
| 134 | + } |
| 135 | + |
| 136 | + gst_buffer_unref (buffer); |
| 137 | + |
| 138 | + fail_unless (gst_pad_push_event (srcpad, gst_event_new_eos ())); |
| 139 | + |
| 140 | + /* All buffers must be there now */ |
| 141 | + fail_unless_equals_int (g_list_length (buffers), 10); |
| 142 | + |
| 143 | + outcaps = |
| 144 | + gst_caps_from_string |
| 145 | + ("image/x-j2c,width=(int)320,height=(int)240,framerate=(fraction)25/1"); |
| 146 | + |
| 147 | + for (l = buffers, i = 0; l; l = l->next, i++) { |
| 148 | + buffer = l->data; |
| 149 | + |
| 150 | + fail_unless_equals_uint64 (GST_BUFFER_DURATION (buffer), |
| 151 | + gst_util_uint64_scale (1, GST_SECOND, 25)); |
| 152 | + |
| 153 | + sinkcaps = gst_pad_get_current_caps (sinkpad); |
| 154 | + fail_unless (gst_caps_can_intersect (sinkcaps, outcaps)); |
| 155 | + gst_caps_unref (sinkcaps); |
| 156 | + } |
| 157 | + |
| 158 | + gst_caps_unref (outcaps); |
| 159 | + |
| 160 | + cleanup_openjpegenc (openjpegenc); |
| 161 | +} |
| 162 | + |
| 163 | +GST_END_TEST; |
| 164 | + |
| 165 | +static gboolean |
| 166 | +bus_cb (GstBus * bus, GstMessage * message, gpointer data) |
| 167 | +{ |
| 168 | + OpenJPEGData *opj_data = (OpenJPEGData *) data; |
| 169 | + switch (GST_MESSAGE_TYPE (message)) { |
| 170 | + case GST_MESSAGE_ERROR:{ |
| 171 | + GError *err = NULL; |
| 172 | + gchar *debug = NULL; |
| 173 | + |
| 174 | + gst_message_parse_error (message, &err, &debug); |
| 175 | + |
| 176 | + GST_ERROR ("Error: %s : %s", err->message, debug); |
| 177 | + g_error_free (err); |
| 178 | + g_free (debug); |
| 179 | + fail_if (!opj_data->failing_pipeline, "failed"); |
| 180 | + g_main_loop_quit (opj_data->loop); |
| 181 | + break; |
| 182 | + } |
| 183 | + case GST_MESSAGE_EOS: |
| 184 | + fail_if (opj_data->failing_pipeline, "failed"); |
| 185 | + g_main_loop_quit (opj_data->loop); |
| 186 | + break; |
| 187 | + default: |
| 188 | + break; |
| 189 | + } |
| 190 | + return TRUE; |
| 191 | +} |
| 192 | + |
| 193 | +static void |
| 194 | +run_openjpeg_pipeline (const gchar * in_format, gint width, gint height, |
| 195 | + gint num_stripes, gint enc_threads, gint dec_threads, |
| 196 | + gboolean failing_pipeline) |
| 197 | +{ |
| 198 | + GstBus *bus; |
| 199 | + OpenJPEGData opj_data; |
| 200 | + GstElement *pipeline; |
| 201 | + gchar *pipeline_str = |
| 202 | + g_strdup_printf ("videotestsrc num-buffers=%d ! " |
| 203 | + "video/x-raw,format=%s, width=%d, height=%d, framerate=%d/1 ! openjpegenc num-stripes=%d num-threads=%d ! jpeg2000parse" |
| 204 | + " ! openjpegdec max-threads=%d ! fakevideosink", |
| 205 | + NUM_BUFFERS, in_format, width, height, FRAME_RATE, num_stripes, |
| 206 | + enc_threads, dec_threads); |
| 207 | + GST_LOG ("Running pipeline: %s", pipeline_str); |
| 208 | + pipeline = gst_parse_launch (pipeline_str, NULL); |
| 209 | + fail_unless (pipeline != NULL); |
| 210 | + g_free (pipeline_str); |
| 211 | + |
| 212 | + opj_data.loop = g_main_loop_new (NULL, FALSE); |
| 213 | + opj_data.failing_pipeline = failing_pipeline; |
| 214 | + |
| 215 | + bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); |
| 216 | + gst_bus_add_watch (bus, (GstBusFunc) bus_cb, &opj_data); |
| 217 | + |
| 218 | + gst_element_set_state (pipeline, GST_STATE_PLAYING); |
| 219 | + g_main_loop_run (opj_data.loop); |
| 220 | + gst_element_set_state (pipeline, GST_STATE_NULL); |
| 221 | + |
| 222 | + gst_bus_remove_watch (bus); |
| 223 | + gst_object_unref (bus); |
| 224 | + gst_object_unref (pipeline); |
| 225 | + g_main_loop_unref (opj_data.loop); |
| 226 | +} |
| 227 | + |
| 228 | + |
| 229 | +GST_START_TEST (test_openjpeg_simple) |
| 230 | +{ |
| 231 | + int i; |
| 232 | + const gchar *in_format_list[] = { |
| 233 | + "ARGB64", "ARGB", "xRGB", "AYUV64", "Y444_10LE", "I422_10LE", "I420_10LE", |
| 234 | + "AYUV", "Y444", "Y42B", "Y41B", "YUV9", "I420", "GRAY8", "GRAY16_LE" |
| 235 | + }; |
| 236 | + |
| 237 | + for (i = 0; i < G_N_ELEMENTS (in_format_list); i++) { |
| 238 | + run_openjpeg_pipeline (in_format_list[i], 320, 200, 1, 1, 1, FALSE); |
| 239 | + } |
| 240 | + |
| 241 | + /* Check that the pipeline is failing properly */ |
| 242 | + run_openjpeg_pipeline (in_format_list[0], 16, 16, 1, 0, 0, TRUE); |
| 243 | + run_openjpeg_pipeline (in_format_list[0], 16, 16, 1, 1, 1, TRUE); |
| 244 | + |
| 245 | + for (i = 1; i < 8; i++) { |
| 246 | + run_openjpeg_pipeline (in_format_list[0], 320, 200, i, 0, 0, FALSE); |
| 247 | + run_openjpeg_pipeline (in_format_list[0], 320, 200, i, 1, 0, FALSE); |
| 248 | + run_openjpeg_pipeline (in_format_list[0], 320, 200, i, 0, 1, FALSE); |
| 249 | + run_openjpeg_pipeline (in_format_list[0], 320, 200, i, 0, 4, FALSE); |
| 250 | + run_openjpeg_pipeline (in_format_list[0], 320, 200, i, 5, 3, FALSE); |
| 251 | + run_openjpeg_pipeline (in_format_list[0], 320, 200, i, 8, 8, FALSE); |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | +GST_END_TEST; |
| 256 | + |
| 257 | + |
| 258 | +static Suite * |
| 259 | +openjpeg_suite (void) |
| 260 | +{ |
| 261 | + Suite *s = suite_create ("openjpeg"); |
| 262 | + TCase *tc_chain = tcase_create ("general"); |
| 263 | + |
| 264 | + suite_add_tcase (s, tc_chain); |
| 265 | + |
| 266 | + tcase_add_test (tc_chain, test_openjpeg_encode_simple); |
| 267 | + tcase_add_test (tc_chain, test_openjpeg_simple); |
| 268 | + tcase_set_timeout (tc_chain, 5 * 60); |
| 269 | + return s; |
| 270 | +} |
| 271 | + |
| 272 | +GST_CHECK_MAIN (openjpeg); |
0 commit comments