| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include <gst/check/gstcheck.h> |
| 24 |
#include <gst/video/video.h> |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
static GstPad *mysrcpad, *mysinkpad; |
| 30 |
|
| 31 |
#define VIDEO_CAPS_STRING "video/x-raw, " \ |
| 32 |
"width = (int) 384, " \ |
| 33 |
"height = (int) 288, " \ |
| 34 |
"framerate = (fraction) 25/1" |
| 35 |
|
| 36 |
#define H264_CAPS_STRING "video/x-h264, " \ |
| 37 |
"width = (int) 384, " \ |
| 38 |
"height = (int) 288, " \ |
| 39 |
"framerate = (fraction) 25/1" |
| 40 |
|
| 41 |
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src", |
| 42 |
GST_PAD_SRC, |
| 43 |
GST_PAD_ALWAYS, |
| 44 |
GST_STATIC_CAPS (VIDEO_CAPS_STRING)); |
| 45 |
|
| 46 |
static void cleanup_x264enc (GstElement * x264enc); |
| 47 |
|
| 48 |
static GstElement * |
| 49 |
setup_x264enc (const gchar * profile, const gchar * stream_format, |
| 50 |
GstVideoFormat input_format) |
| 51 |
{ |
| 52 |
GstPadTemplate *sink_tmpl, *tmpl; |
| 53 |
GstElement *x264enc; |
| 54 |
GstCaps *caps, *tmpl_caps; |
| 55 |
|
| 56 |
GST_DEBUG ("setup_x264enc"); |
| 57 |
|
| 58 |
caps = gst_caps_from_string (H264_CAPS_STRING); |
| 59 |
gst_caps_set_simple (caps, "profile", G_TYPE_STRING, profile, |
| 60 |
"stream-format", G_TYPE_STRING, stream_format, NULL); |
| 61 |
sink_tmpl = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps); |
| 62 |
gst_caps_unref (caps); |
| 63 |
|
| 64 |
x264enc = gst_check_setup_element ("x264enc"); |
| 65 |
mysrcpad = gst_check_setup_src_pad (x264enc, &srctemplate); |
| 66 |
mysinkpad = gst_check_setup_sink_pad_from_template (x264enc, sink_tmpl); |
| 67 |
gst_pad_set_active (mysrcpad, TRUE); |
| 68 |
gst_pad_set_active (mysinkpad, TRUE); |
| 69 |
|
| 70 |
caps = gst_caps_from_string (VIDEO_CAPS_STRING); |
| 71 |
gst_caps_set_simple (caps, "format", G_TYPE_STRING, |
| 72 |
gst_video_format_to_string (input_format), NULL); |
| 73 |
|
| 74 |
tmpl = gst_element_get_pad_template (x264enc, "sink"); |
| 75 |
tmpl_caps = gst_pad_template_get_caps (tmpl); |
| 76 |
|
| 77 |
if (gst_caps_can_intersect (caps, tmpl_caps)) { |
| 78 |
gst_check_setup_events (mysrcpad, x264enc, caps, GST_FORMAT_TIME); |
| 79 |
} else { |
| 80 |
cleanup_x264enc (x264enc); |
| 81 |
x264enc = NULL; |
| 82 |
} |
| 83 |
|
| 84 |
gst_caps_unref (tmpl_caps); |
| 85 |
gst_caps_unref (caps); |
| 86 |
gst_object_unref (sink_tmpl); |
| 87 |
|
| 88 |
return x264enc; |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
cleanup_x264enc (GstElement * x264enc) |
| 93 |
{ |
| 94 |
GST_DEBUG ("cleanup_x264enc"); |
| 95 |
gst_element_set_state (x264enc, GST_STATE_NULL); |
| 96 |
|
| 97 |
gst_pad_set_active (mysrcpad, FALSE); |
| 98 |
gst_pad_set_active (mysinkpad, FALSE); |
| 99 |
gst_check_teardown_src_pad (x264enc); |
| 100 |
gst_check_teardown_sink_pad (x264enc); |
| 101 |
gst_check_teardown_element (x264enc); |
| 102 |
} |
| 103 |
|
| 104 |
static void |
| 105 |
check_caps (GstCaps * caps, const gchar * profile, gint profile_id) |
| 106 |
{ |
| 107 |
GstStructure *s; |
| 108 |
const GValue *sf, *avcc, *pf; |
| 109 |
const gchar *stream_format; |
| 110 |
const gchar *caps_profile; |
| 111 |
|
| 112 |
fail_unless (caps != NULL); |
| 113 |
|
| 114 |
GST_INFO ("caps %" GST_PTR_FORMAT, caps); |
| 115 |
s = gst_caps_get_structure (caps, 0); |
| 116 |
fail_unless (s != NULL); |
| 117 |
fail_if (!gst_structure_has_name (s, "video/x-h264")); |
| 118 |
sf = gst_structure_get_value (s, "stream-format"); |
| 119 |
fail_unless (sf != NULL); |
| 120 |
fail_unless (G_VALUE_HOLDS_STRING (sf)); |
| 121 |
stream_format = g_value_get_string (sf); |
| 122 |
fail_unless (stream_format != NULL); |
| 123 |
if (strcmp (stream_format, "avc") == 0) { |
| 124 |
GstMapInfo map; |
| 125 |
GstBuffer *buf; |
| 126 |
|
| 127 |
avcc = gst_structure_get_value (s, "codec_data"); |
| 128 |
fail_unless (avcc != NULL); |
| 129 |
fail_unless (GST_VALUE_HOLDS_BUFFER (avcc)); |
| 130 |
buf = gst_value_get_buffer (avcc); |
| 131 |
fail_unless (buf != NULL); |
| 132 |
gst_buffer_map (buf, &map, GST_MAP_READ); |
| 133 |
fail_unless_equals_int (map.data[0], 1); |
| 134 |
fail_unless (map.data[1] == profile_id, |
| 135 |
"Expected profile ID %#04x, got %#04x", profile_id, map.data[1]); |
| 136 |
gst_buffer_unmap (buf, &map); |
| 137 |
} else if (strcmp (stream_format, "byte-stream") == 0) { |
| 138 |
fail_if (gst_structure_get_value (s, "codec_data") != NULL); |
| 139 |
} else { |
| 140 |
fail_if (TRUE, "unexpected stream-format in caps: %s", stream_format); |
| 141 |
} |
| 142 |
|
| 143 |
pf = gst_structure_get_value (s, "profile"); |
| 144 |
fail_unless (pf != NULL); |
| 145 |
fail_unless (G_VALUE_HOLDS_STRING (pf)); |
| 146 |
caps_profile = g_value_get_string (pf); |
| 147 |
fail_unless (caps_profile != NULL); |
| 148 |
fail_unless (!strcmp (caps_profile, profile)); |
| 149 |
} |
| 150 |
|
| 151 |
static const GstVideoFormat formats_420_8_and_400_8[] = |
| 152 |
{ GST_VIDEO_FORMAT_I420, GST_VIDEO_FORMAT_YV12, GST_VIDEO_FORMAT_NV12, |
| 153 |
GST_VIDEO_FORMAT_GRAY8, GST_VIDEO_FORMAT_UNKNOWN |
| 154 |
}; |
| 155 |
|
| 156 |
static const GstVideoFormat formats_420_8[] = |
| 157 |
{ GST_VIDEO_FORMAT_I420, GST_VIDEO_FORMAT_YV12, GST_VIDEO_FORMAT_NV12, |
| 158 |
GST_VIDEO_FORMAT_UNKNOWN |
| 159 |
}; |
| 160 |
|
| 161 |
#if G_BYTE_ORDER == G_LITTLE_ENDIAN |
| 162 |
static const GstVideoFormat formats_420_10[] = |
| 163 |
{ GST_VIDEO_FORMAT_I420_10LE, GST_VIDEO_FORMAT_UNKNOWN }; |
| 164 |
static const GstVideoFormat formats_422[] = |
| 165 |
{ GST_VIDEO_FORMAT_Y42B, GST_VIDEO_FORMAT_I422_10LE, |
| 166 |
GST_VIDEO_FORMAT_UNKNOWN |
| 167 |
}; |
| 168 |
|
| 169 |
static const GstVideoFormat formats_444[] = |
| 170 |
{ GST_VIDEO_FORMAT_Y444, GST_VIDEO_FORMAT_Y444_10LE, |
| 171 |
GST_VIDEO_FORMAT_UNKNOWN |
| 172 |
}; |
| 173 |
#else |
| 174 |
static const GstVideoFormat formats_420_10[] = |
| 175 |
{ GST_VIDEO_FORMAT_I420_10BE, GST_VIDEO_FORMAT_UNKNOWN }; |
| 176 |
static const GstVideoFormat formats_422[] = |
| 177 |
{ GST_VIDEO_FORMAT_Y42B, GST_VIDEO_FORMAT_I422_10BE, |
| 178 |
GST_VIDEO_FORMAT_UNKNOWN |
| 179 |
}; |
| 180 |
|
| 181 |
static const GstVideoFormat formats_444[] = |
| 182 |
{ GST_VIDEO_FORMAT_Y444, GST_VIDEO_FORMAT_Y444_10BE, |
| 183 |
GST_VIDEO_FORMAT_UNKNOWN |
| 184 |
}; |
| 185 |
#endif |
| 186 |
|
| 187 |
static void |
| 188 |
test_video_profile (const gchar * profile, gint profile_id, |
| 189 |
const GstVideoFormat input_formats[], gint input_format_index) |
| 190 |
{ |
| 191 |
GstVideoFormat input_format = input_formats[input_format_index]; |
| 192 |
GstElement *x264enc; |
| 193 |
GstBuffer *inbuffer, *outbuffer; |
| 194 |
int i, num_buffers; |
| 195 |
GstVideoInfo vinfo; |
| 196 |
GstCaps *caps; |
| 197 |
|
| 198 |
fail_unless (gst_video_info_set_format (&vinfo, input_format, 384, 288)); |
| 199 |
|
| 200 |
x264enc = setup_x264enc (profile, "avc", input_format); |
| 201 |
if (x264enc == NULL) { |
| 202 |
g_printerr ("WARNING: input format '%s' not supported\n", |
| 203 |
gst_video_format_to_string (input_format)); |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
fail_unless (gst_element_set_state (x264enc, |
| 208 |
GST_STATE_PLAYING) == GST_STATE_CHANGE_SUCCESS, |
| 209 |
"could not set to playing"); |
| 210 |
|
| 211 |
|
| 212 |
caps = gst_pad_peer_query_caps (mysrcpad, NULL); |
| 213 |
for (i = 0; i < gst_caps_get_size (caps); i++) { |
| 214 |
GstStructure *s = gst_caps_get_structure (caps, i); |
| 215 |
const GValue *v, *vi; |
| 216 |
guint vlen, j = 0; |
| 217 |
|
| 218 |
v = gst_structure_get_value (s, "format"); |
| 219 |
|
| 220 |
if (G_VALUE_TYPE (v) == G_TYPE_STRING) { |
| 221 |
vlen = 1; |
| 222 |
vi = v; |
| 223 |
} else if (G_VALUE_TYPE (v) == GST_TYPE_LIST) { |
| 224 |
vlen = gst_value_list_get_size (v); |
| 225 |
fail_unless (vlen > 0, "Got empty format list"); |
| 226 |
vi = gst_value_list_get_value (v, 0); |
| 227 |
} else { |
| 228 |
fail ("Bad format in structure: %" GST_PTR_FORMAT, s); |
| 229 |
g_assert_not_reached (); |
| 230 |
} |
| 231 |
|
| 232 |
while (TRUE) { |
| 233 |
const gchar *str = g_value_get_string (vi); |
| 234 |
GstVideoFormat format = gst_video_format_from_string (str); |
| 235 |
int k; |
| 236 |
|
| 237 |
for (k = 0;; k++) { |
| 238 |
fail_unless (input_formats[k] != GST_VIDEO_FORMAT_UNKNOWN, |
| 239 |
"Bad format: %s", str); |
| 240 |
if (input_formats[k] == format) |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
if (++j < vlen) |
| 245 |
vi = gst_value_list_get_value (v, j); |
| 246 |
else |
| 247 |
break; |
| 248 |
} |
| 249 |
} |
| 250 |
gst_caps_unref (caps); |
| 251 |
|
| 252 |
|
| 253 |
inbuffer = gst_buffer_new_and_alloc (GST_VIDEO_INFO_SIZE (&vinfo)); |
| 254 |
|
| 255 |
|
| 256 |
gst_buffer_memset (inbuffer, 0, 0, -1); |
| 257 |
GST_BUFFER_TIMESTAMP (inbuffer) = 0; |
| 258 |
ASSERT_BUFFER_REFCOUNT (inbuffer, "inbuffer", 1); |
| 259 |
fail_unless (gst_pad_push (mysrcpad, inbuffer) == GST_FLOW_OK); |
| 260 |
|
| 261 |
|
| 262 |
fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()) == TRUE); |
| 263 |
|
| 264 |
num_buffers = g_list_length (buffers); |
| 265 |
fail_unless (num_buffers == 1); |
| 266 |
|
| 267 |
|
| 268 |
{ |
| 269 |
GstCaps *outcaps; |
| 270 |
|
| 271 |
outcaps = gst_pad_get_current_caps (mysinkpad); |
| 272 |
check_caps (outcaps, profile, profile_id); |
| 273 |
gst_caps_unref (outcaps); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
for (i = 0; i < num_buffers; ++i) { |
| 278 |
outbuffer = GST_BUFFER (buffers->data); |
| 279 |
fail_if (outbuffer == NULL); |
| 280 |
|
| 281 |
switch (i) { |
| 282 |
case 0: |
| 283 |
{ |
| 284 |
gint nsize, npos, type, next_type; |
| 285 |
GstMapInfo map; |
| 286 |
const guint8 *data; |
| 287 |
gsize size; |
| 288 |
|
| 289 |
gst_buffer_map (outbuffer, &map, GST_MAP_READ); |
| 290 |
data = map.data; |
| 291 |
size = map.size; |
| 292 |
|
| 293 |
npos = 0; |
| 294 |
|
| 295 |
next_type = 7; |
| 296 |
|
| 297 |
while (npos < size) { |
| 298 |
fail_unless (size - npos >= 4); |
| 299 |
nsize = GST_READ_UINT32_BE (data + npos); |
| 300 |
fail_unless (nsize > 0); |
| 301 |
fail_unless (npos + 4 + nsize <= size); |
| 302 |
type = data[npos + 4] & 0x1F; |
| 303 |
|
| 304 |
if (type != 9 && type != 6) { |
| 305 |
fail_unless (type == next_type); |
| 306 |
switch (type) { |
| 307 |
case 7: |
| 308 |
|
| 309 |
next_type = 8; |
| 310 |
break; |
| 311 |
case 8: |
| 312 |
|
| 313 |
next_type = 5; |
| 314 |
break; |
| 315 |
default: |
| 316 |
break; |
| 317 |
} |
| 318 |
} |
| 319 |
npos += nsize + 4; |
| 320 |
} |
| 321 |
gst_buffer_unmap (outbuffer, &map); |
| 322 |
|
| 323 |
fail_unless (npos == size); |
| 324 |
break; |
| 325 |
} |
| 326 |
default: |
| 327 |
break; |
| 328 |
} |
| 329 |
|
| 330 |
buffers = g_list_remove (buffers, outbuffer); |
| 331 |
|
| 332 |
ASSERT_BUFFER_REFCOUNT (outbuffer, "outbuffer", 1); |
| 333 |
gst_buffer_unref (outbuffer); |
| 334 |
outbuffer = NULL; |
| 335 |
} |
| 336 |
|
| 337 |
cleanup_x264enc (x264enc); |
| 338 |
g_list_free (buffers); |
| 339 |
buffers = NULL; |
| 340 |
} |
| 341 |
|
| 342 |
GST_START_TEST (test_video_baseline) |
| 343 |
{ |
| 344 |
gint i; |
| 345 |
|
| 346 |
for (i = 0; formats_420_8[i] != GST_VIDEO_FORMAT_UNKNOWN; i++) |
| 347 |
test_video_profile ("constrained-baseline", 0x42, formats_420_8, i); |
| 348 |
} |
| 349 |
|
| 350 |
GST_END_TEST; |
| 351 |
|
| 352 |
GST_START_TEST (test_video_main) |
| 353 |
{ |
| 354 |
gint i; |
| 355 |
|
| 356 |
for (i = 0; formats_420_8[i] != GST_VIDEO_FORMAT_UNKNOWN; i++) |
| 357 |
test_video_profile ("main", 0x4d, formats_420_8, i); |
| 358 |
} |
| 359 |
|
| 360 |
GST_END_TEST; |
| 361 |
|
| 362 |
GST_START_TEST (test_video_high) |
| 363 |
{ |
| 364 |
gint i; |
| 365 |
|
| 366 |
for (i = 0; formats_420_8_and_400_8[i] != GST_VIDEO_FORMAT_UNKNOWN; i++) |
| 367 |
test_video_profile ("high", 0x64, formats_420_8_and_400_8, i); |
| 368 |
} |
| 369 |
|
| 370 |
GST_END_TEST; |
| 371 |
|
| 372 |
GST_START_TEST (test_video_high10) |
| 373 |
{ |
| 374 |
gint i; |
| 375 |
|
| 376 |
for (i = 0; formats_420_10[i] != GST_VIDEO_FORMAT_UNKNOWN; i++) |
| 377 |
test_video_profile ("high-10", 0x6e, formats_420_10, i); |
| 378 |
} |
| 379 |
|
| 380 |
GST_END_TEST; |
| 381 |
|
| 382 |
GST_START_TEST (test_video_high422) |
| 383 |
{ |
| 384 |
gint i; |
| 385 |
|
| 386 |
for (i = 0; formats_422[i] != GST_VIDEO_FORMAT_UNKNOWN; i++) |
| 387 |
test_video_profile ("high-4:2:2", 0x7A, formats_422, i); |
| 388 |
} |
| 389 |
|
| 390 |
GST_END_TEST; |
| 391 |
|
| 392 |
GST_START_TEST (test_video_high444) |
| 393 |
{ |
| 394 |
gint i; |
| 395 |
|
| 396 |
for (i = 0; formats_444[i] != GST_VIDEO_FORMAT_UNKNOWN; i++) |
| 397 |
test_video_profile ("high-4:4:4", 0xF4, formats_444, i); |
| 398 |
} |
| 399 |
|
| 400 |
GST_END_TEST; |
| 401 |
|
| 402 |
Suite * |
| 403 |
x264enc_suite (void) |
| 404 |
{ |
| 405 |
Suite *s = suite_create ("x264enc"); |
| 406 |
TCase *tc_chain = tcase_create ("general"); |
| 407 |
|
| 408 |
suite_add_tcase (s, tc_chain); |
| 409 |
tcase_add_test (tc_chain, test_video_baseline); |
| 410 |
tcase_add_test (tc_chain, test_video_main); |
| 411 |
tcase_add_test (tc_chain, test_video_high); |
| 412 |
tcase_add_test (tc_chain, test_video_high10); |
| 413 |
tcase_add_test (tc_chain, test_video_high422); |
| 414 |
tcase_add_test (tc_chain, test_video_high444); |
| 415 |
|
| 416 |
return s; |
| 417 |
} |
| 418 |
|
| 419 |
GST_CHECK_MAIN (x264enc); |