theoraenc.c 14.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* GStreamer
 *
 * unit test for theoraenc
 *
 * Copyright (C) 2006 Andy Wingo <wingo at pobox.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <gst/check/gstcheck.h>
24
#include <gst/check/gstbufferstraw.h>
25

26 27
#ifndef GST_DISABLE_PARSE

28
#define TIMESTAMP_OFFSET G_GINT64_CONSTANT(3249870963)
29 30
#define FRAMERATE 10

31 32 33
/* I know all of these have a shift of 6 bits */
#define GRANULEPOS_SHIFT 6

34

35 36 37 38 39
#define check_buffer_is_header(buffer,is_header) \
  fail_unless (GST_BUFFER_FLAG_IS_SET (buffer,   \
          GST_BUFFER_FLAG_IN_CAPS) == is_header, \
      "GST_BUFFER_IN_CAPS is set to %d but expected %d", \
      GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_IN_CAPS), is_header)
40

41 42 43 44 45 46 47 48 49 50 51
#define check_buffer_timestamp(buffer,timestamp) \
  fail_unless (GST_BUFFER_TIMESTAMP (buffer) == timestamp, \
      "expected timestamp %" GST_TIME_FORMAT \
      ", but got timestamp %" GST_TIME_FORMAT, \
      GST_TIME_ARGS (timestamp), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)))

#define check_buffer_duration(buffer,duration) \
  fail_unless (GST_BUFFER_DURATION (buffer) == duration, \
      "expected duration %" GST_TIME_FORMAT \
      ", but got duration %" GST_TIME_FORMAT, \
      GST_TIME_ARGS (duration), GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)))
52 53

static void
54
check_buffer_granulepos (GstBuffer * buffer, gint64 granulepos)
55
{
56
  GstClockTime clocktime;
57
  int framecount;
58

59 60 61 62
  fail_unless (GST_BUFFER_OFFSET_END (buffer) == granulepos,
      "expected granulepos %" G_GUINT64_FORMAT
      ", but got granulepos %" G_GUINT64_FORMAT,
      granulepos, GST_BUFFER_OFFSET_END (buffer));
63 64 65

  /* contrary to what we record as TIMESTAMP, we can use OFFSET to check
   * the granulepos correctly here */
66 67 68 69
  framecount = GST_BUFFER_OFFSET_END (buffer);
  framecount = granulepos >> GRANULEPOS_SHIFT;
  framecount += granulepos & ((1 << GRANULEPOS_SHIFT) - 1);
  clocktime = gst_util_uint64_scale (framecount, GST_SECOND, FRAMERATE);
70 71 72 73 74

  fail_unless (clocktime == GST_BUFFER_OFFSET (buffer),
      "expected OFFSET set to clocktime %" GST_TIME_FORMAT
      ", but got %" GST_TIME_FORMAT,
      GST_TIME_ARGS (clocktime), GST_TIME_ARGS (GST_BUFFER_OFFSET (buffer)));
75 76
}

77 78 79 80 81
/* this check is here to check that the granulepos we derive from the
   timestamp is about correct. This is "about correct" because you can't
   precisely go from timestamp to granulepos due to the downward-rounding
   characteristics of gst_util_uint64_scale, so you check if granulepos is
   equal to the number, or the number plus one. */
82 83 84 85 86
/* should be from_endtime, but theora's granulepos mapping is "special" */
static void
check_buffer_granulepos_from_starttime (GstBuffer * buffer,
    GstClockTime starttime)
{
87
  gint64 granulepos, expected, framecount;
88 89

  granulepos = GST_BUFFER_OFFSET_END (buffer);
90 91
  framecount = granulepos >> GRANULEPOS_SHIFT;
  framecount += granulepos & ((1 << GRANULEPOS_SHIFT) - 1);
92 93
  expected = gst_util_uint64_scale (starttime, FRAMERATE, GST_SECOND);

94 95
  fail_unless (framecount == expected || framecount == expected + 1,
      "expected frame count %" G_GUINT64_FORMAT
96
      " or %" G_GUINT64_FORMAT
97
      ", but got frame count %" G_GUINT64_FORMAT,
98 99 100 101 102 103 104 105 106 107 108 109 110
      expected, expected + 1, granulepos);
}

GST_START_TEST (test_granulepos_offset)
{
  GstElement *bin;
  GstPad *pad;
  gchar *pipe_str;
  GstBuffer *buffer;
  GError *error = NULL;

  pipe_str = g_strdup_printf ("videotestsrc timestamp-offset=%" G_GUINT64_FORMAT
      " ! video/x-raw-yuv,format=(fourcc)I420,framerate=10/1"
111
      " ! theoraenc ! fakesink name=fs0", TIMESTAMP_OFFSET);
112 113 114 115 116 117 118 119

  bin = gst_parse_launch (pipe_str, &error);
  fail_unless (bin != NULL, "Error parsing pipeline: %s",
      error ? error->message : "(invalid error)");
  g_free (pipe_str);

  /* get the pad */
  {
120
    GstElement *sink = gst_bin_get_by_name (GST_BIN (bin), "fs0");
121 122 123 124 125 126 127

    fail_unless (sink != NULL, "Could not get fakesink out of bin");
    pad = gst_element_get_pad (sink, "sink");
    fail_unless (pad != NULL, "Could not get pad out of fakesink");
    gst_object_unref (sink);
  }

128
  gst_buffer_straw_start_pipeline (bin, pad);
129

130
  /* header packets should have timestamp == NONE, granulepos 0, IN_CAPS */
131
  buffer = gst_buffer_straw_get_buffer (bin, pad);
132 133 134
  check_buffer_timestamp (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_duration (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_granulepos (buffer, 0);
135
  check_buffer_is_header (buffer, TRUE);
136 137
  gst_buffer_unref (buffer);

138
  buffer = gst_buffer_straw_get_buffer (bin, pad);
139 140 141
  check_buffer_timestamp (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_duration (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_granulepos (buffer, 0);
142
  check_buffer_is_header (buffer, TRUE);
143 144
  gst_buffer_unref (buffer);

145
  buffer = gst_buffer_straw_get_buffer (bin, pad);
146 147 148
  check_buffer_timestamp (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_duration (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_granulepos (buffer, 0);
149
  check_buffer_is_header (buffer, TRUE);
150 151 152
  gst_buffer_unref (buffer);

  {
153 154
    GstClockTime next_timestamp;
    gint64 last_granulepos;
155 156 157

    /* first buffer should have timestamp of TIMESTAMP_OFFSET, granulepos to
     * match the timestamp of the end of the last sample in the output buffer.
158 159 160 161
     * Note that one cannot go timestamp->granulepos->timestamp and get the
     * same value due to loss of precision with granulepos. theoraenc does
     * take care to timestamp correctly based on the offset of the input data
     * however, so it does do sub-granulepos timestamping. */
162
    buffer = gst_buffer_straw_get_buffer (bin, pad);
163 164 165 166
    last_granulepos = GST_BUFFER_OFFSET_END (buffer);
    check_buffer_timestamp (buffer, TIMESTAMP_OFFSET);
    /* don't really have a good way of checking duration... */
    check_buffer_granulepos_from_starttime (buffer, TIMESTAMP_OFFSET);
167
    check_buffer_is_header (buffer, FALSE);
168 169 170 171 172 173

    next_timestamp = TIMESTAMP_OFFSET + GST_BUFFER_DURATION (buffer);

    gst_buffer_unref (buffer);

    /* check continuity with the next buffer */
174
    buffer = gst_buffer_straw_get_buffer (bin, pad);
175 176 177 178 179 180
    check_buffer_timestamp (buffer, next_timestamp);
    check_buffer_duration (buffer,
        gst_util_uint64_scale (GST_BUFFER_OFFSET_END (buffer), GST_SECOND,
            FRAMERATE)
        - gst_util_uint64_scale (last_granulepos, GST_SECOND, FRAMERATE));
    check_buffer_granulepos_from_starttime (buffer, next_timestamp);
181
    check_buffer_is_header (buffer, FALSE);
182 183 184 185

    gst_buffer_unref (buffer);
  }

186
  gst_buffer_straw_stop_pipeline (bin, pad);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

  gst_object_unref (pad);
  gst_object_unref (bin);
}

GST_END_TEST;

GST_START_TEST (test_continuity)
{
  GstElement *bin;
  GstPad *pad;
  gchar *pipe_str;
  GstBuffer *buffer;
  GError *error = NULL;

  pipe_str = g_strdup_printf ("videotestsrc"
      " ! video/x-raw-yuv,format=(fourcc)I420,framerate=10/1"
204
      " ! theoraenc ! fakesink name=fs0");
205 206 207 208 209 210 211 212

  bin = gst_parse_launch (pipe_str, &error);
  fail_unless (bin != NULL, "Error parsing pipeline: %s",
      error ? error->message : "(invalid error)");
  g_free (pipe_str);

  /* get the pad */
  {
213
    GstElement *sink = gst_bin_get_by_name (GST_BIN (bin), "fs0");
214 215 216 217 218 219 220

    fail_unless (sink != NULL, "Could not get fakesink out of bin");
    pad = gst_element_get_pad (sink, "sink");
    fail_unless (pad != NULL, "Could not get pad out of fakesink");
    gst_object_unref (sink);
  }

221
  gst_buffer_straw_start_pipeline (bin, pad);
222 223

  /* header packets should have timestamp == NONE, granulepos 0 */
224
  buffer = gst_buffer_straw_get_buffer (bin, pad);
225 226 227
  check_buffer_timestamp (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_duration (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_granulepos (buffer, 0);
228
  check_buffer_is_header (buffer, TRUE);
229 230
  gst_buffer_unref (buffer);

231
  buffer = gst_buffer_straw_get_buffer (bin, pad);
232 233 234
  check_buffer_timestamp (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_duration (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_granulepos (buffer, 0);
235
  check_buffer_is_header (buffer, TRUE);
236 237
  gst_buffer_unref (buffer);

238
  buffer = gst_buffer_straw_get_buffer (bin, pad);
239 240 241
  check_buffer_timestamp (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_duration (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_granulepos (buffer, 0);
242
  check_buffer_is_header (buffer, TRUE);
243 244 245
  gst_buffer_unref (buffer);

  {
246 247
    GstClockTime next_timestamp;
    gint64 last_granulepos;
248 249 250

    /* first buffer should have timestamp of TIMESTAMP_OFFSET, granulepos to
     * match the timestamp of the end of the last sample in the output buffer.
251 252 253 254
     * Note that one cannot go timestamp->granulepos->timestamp and get the
     * same value due to loss of precision with granulepos. theoraenc does
     * take care to timestamp correctly based on the offset of the input data
     * however, so it does do sub-granulepos timestamping. */
255
    buffer = gst_buffer_straw_get_buffer (bin, pad);
256 257
    last_granulepos = GST_BUFFER_OFFSET_END (buffer);
    check_buffer_timestamp (buffer, 0);
258 259
    /* plain division because I know the answer is exact */
    check_buffer_duration (buffer, GST_SECOND / 10);
260
    check_buffer_granulepos (buffer, 0);
261
    check_buffer_is_header (buffer, FALSE);
262 263 264 265 266 267

    next_timestamp = GST_BUFFER_DURATION (buffer);

    gst_buffer_unref (buffer);

    /* check continuity with the next buffer */
268
    buffer = gst_buffer_straw_get_buffer (bin, pad);
269 270 271
    check_buffer_timestamp (buffer, next_timestamp);
    check_buffer_duration (buffer, GST_SECOND / 10);
    check_buffer_granulepos (buffer, 1);
272
    check_buffer_is_header (buffer, FALSE);
273 274 275 276

    gst_buffer_unref (buffer);
  }

277
  gst_buffer_straw_stop_pipeline (bin, pad);
278 279 280 281 282 283 284

  gst_object_unref (pad);
  gst_object_unref (bin);
}

GST_END_TEST;

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
static gboolean
drop_second_data_buffer (GstPad * droppad, GstBuffer * buffer, gpointer unused)
{
  return !(GST_BUFFER_OFFSET (buffer) == 1);
}

GST_START_TEST (test_discontinuity)
{
  GstElement *bin;
  GstPad *pad, *droppad;
  gchar *pipe_str;
  GstBuffer *buffer;
  GError *error = NULL;
  guint drop_id;

  pipe_str = g_strdup_printf ("videotestsrc"
      " ! video/x-raw-yuv,format=(fourcc)I420,framerate=10/1"
      " ! theoraenc ! fakesink name=fs0");

  bin = gst_parse_launch (pipe_str, &error);
  fail_unless (bin != NULL, "Error parsing pipeline: %s",
      error ? error->message : "(invalid error)");
  g_free (pipe_str);

  /* the plan: same as test_continuity, but dropping a buffer and seeing if
     theoraenc correctly notes the discontinuity */

  /* get the pad to use to drop buffers */
  {
    GstElement *sink = gst_bin_get_by_name (GST_BIN (bin), "theoraenc0");

    fail_unless (sink != NULL, "Could not get theoraenc out of bin");
    droppad = gst_element_get_pad (sink, "sink");
    fail_unless (droppad != NULL, "Could not get pad out of theoraenc");
    gst_object_unref (sink);
  }

  /* get the pad */
  {
    GstElement *sink = gst_bin_get_by_name (GST_BIN (bin), "fs0");

    fail_unless (sink != NULL, "Could not get fakesink out of bin");
    pad = gst_element_get_pad (sink, "sink");
    fail_unless (pad != NULL, "Could not get pad out of fakesink");
    gst_object_unref (sink);
  }

332 333
  drop_id = gst_pad_add_buffer_probe (droppad,
      G_CALLBACK (drop_second_data_buffer), NULL);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  gst_buffer_straw_start_pipeline (bin, pad);

  /* header packets should have timestamp == NONE, granulepos 0 */
  buffer = gst_buffer_straw_get_buffer (bin, pad);
  check_buffer_timestamp (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_duration (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_granulepos (buffer, 0);
  check_buffer_is_header (buffer, TRUE);
  gst_buffer_unref (buffer);

  buffer = gst_buffer_straw_get_buffer (bin, pad);
  check_buffer_timestamp (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_duration (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_granulepos (buffer, 0);
  check_buffer_is_header (buffer, TRUE);
  gst_buffer_unref (buffer);

  buffer = gst_buffer_straw_get_buffer (bin, pad);
  check_buffer_timestamp (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_duration (buffer, GST_CLOCK_TIME_NONE);
  check_buffer_granulepos (buffer, 0);
  check_buffer_is_header (buffer, TRUE);
  gst_buffer_unref (buffer);

  {
    buffer = gst_buffer_straw_get_buffer (bin, pad);
    check_buffer_timestamp (buffer, 0);
    /* plain division because I know the answer is exact */
    check_buffer_duration (buffer, GST_SECOND / 10);
    check_buffer_granulepos (buffer, 0);
    check_buffer_is_header (buffer, FALSE);
    fail_if (GST_BUFFER_IS_DISCONT (buffer), "expected continuous buffer yo");
    gst_buffer_unref (buffer);

    /* check discontinuity with the next buffer */
    buffer = gst_buffer_straw_get_buffer (bin, pad);
    check_buffer_duration (buffer, GST_SECOND / 10);
371 372 373
    /* After a discont, we'll always get a keyframe, so this one should be 
     * 2<<GRANULEPOS_SHIFT */
    check_buffer_granulepos (buffer, 2 << GRANULEPOS_SHIFT);
374 375 376 377
    check_buffer_is_header (buffer, FALSE);
    fail_unless (GST_BUFFER_IS_DISCONT (buffer),
        "expected discontinuous buffer yo");
    gst_buffer_unref (buffer);
378 379 380 381 382 383 384 385 386

    /* Then the buffer after that should be continuous */
    buffer = gst_buffer_straw_get_buffer (bin, pad);
    fail_if (GST_BUFFER_IS_DISCONT (buffer), "expected continuous buffer yo");
    /* plain division because I know the answer is exact */
    check_buffer_duration (buffer, GST_SECOND / 10);
    check_buffer_granulepos (buffer, (2 << GRANULEPOS_SHIFT) + 1);
    check_buffer_is_header (buffer, FALSE);
    gst_buffer_unref (buffer);
387 388 389 390 391 392 393 394 395 396 397 398
  }

  gst_buffer_straw_stop_pipeline (bin, pad);
  gst_pad_remove_buffer_probe (droppad, drop_id);

  gst_object_unref (droppad);
  gst_object_unref (pad);
  gst_object_unref (bin);
}

GST_END_TEST;

399 400
#endif /* #ifndef GST_DISABLE_PARSE */

401 402 403 404 405 406 407
Suite *
theoraenc_suite (void)
{
  Suite *s = suite_create ("theoraenc");
  TCase *tc_chain = tcase_create ("general");

  suite_add_tcase (s, tc_chain);
408 409

#ifndef GST_DISABLE_PARSE
410 411
  tcase_add_test (tc_chain, test_granulepos_offset);
  tcase_add_test (tc_chain, test_continuity);
412
  tcase_add_test (tc_chain, test_discontinuity);
413
#endif
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433

  return s;
}

int
main (int argc, char **argv)
{
  int nf;

  Suite *s = theoraenc_suite ();
  SRunner *sr = srunner_create (s);

  gst_check_init (&argc, &argv);

  srunner_run_all (sr, CK_NORMAL);
  nf = srunner_ntests_failed (sr);
  srunner_free (sr);

  return nf;
}