videodecoder: add API to receive subframes
[platform/upstream/gstreamer.git] / tests / check / libs / videodecoder.c
1 /* GStreamer
2  *
3  * Copyright (C) 2014 Samsung Electronics. All rights reserved.
4  *   Author: Thiago Santos <ts.santos@sisa.samsung.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <gst/gst.h>
26 #include <gst/check/gstcheck.h>
27 #include <gst/check/gstharness.h>
28 #include <gst/video/video.h>
29 #include <gst/app/app.h>
30
31 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
32     GST_PAD_SINK,
33     GST_PAD_ALWAYS,
34     GST_STATIC_CAPS ("video/x-raw")
35     );
36
37 #define RESTRICTED_CAPS_WIDTH 800
38 #define RESTRICTED_CAPS_HEIGHT 600
39 #define RESTRICTED_CAPS_FPS_N 30
40 #define RESTRICTED_CAPS_FPS_D 1
41 static GstStaticPadTemplate sinktemplate_restricted =
42 GST_STATIC_PAD_TEMPLATE ("sink",
43     GST_PAD_SINK,
44     GST_PAD_ALWAYS,
45     GST_STATIC_CAPS ("video/x-raw, width=(int)800, height=(int)600,"
46         " framerate=(fraction)30/1")
47     );
48
49 static GstStaticPadTemplate sinktemplate_with_range =
50 GST_STATIC_PAD_TEMPLATE ("sink",
51     GST_PAD_SINK,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS ("video/x-raw, width=(int)[1,800], height=(int)[1,600],"
54         " framerate=(fraction)[1/1, 30/1]")
55     );
56
57 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
58     GST_PAD_SRC,
59     GST_PAD_ALWAYS,
60     GST_STATIC_CAPS ("video/x-test-custom")
61     );
62
63 static GstPad *mysrcpad, *mysinkpad;
64 static GstElement *dec;
65 static GList *events = NULL;
66
67 #define TEST_VIDEO_WIDTH 640
68 #define TEST_VIDEO_HEIGHT 480
69 #define TEST_VIDEO_FPS_N 30
70 #define TEST_VIDEO_FPS_D 1
71
72 #define GST_VIDEO_DECODER_TESTER_TYPE gst_video_decoder_tester_get_type()
73 static GType gst_video_decoder_tester_get_type (void);
74
75 typedef struct _GstVideoDecoderTester GstVideoDecoderTester;
76 typedef struct _GstVideoDecoderTesterClass GstVideoDecoderTesterClass;
77
78 struct _GstVideoDecoderTester
79 {
80   GstVideoDecoder parent;
81
82   guint64 last_buf_num;
83   guint64 last_kf_num;
84   gboolean set_output_state;
85   gboolean subframe_mode;
86 };
87
88 struct _GstVideoDecoderTesterClass
89 {
90   GstVideoDecoderClass parent_class;
91 };
92
93 G_DEFINE_TYPE (GstVideoDecoderTester, gst_video_decoder_tester,
94     GST_TYPE_VIDEO_DECODER);
95
96 static gboolean
97 gst_video_decoder_tester_start (GstVideoDecoder * dec)
98 {
99   GstVideoDecoderTester *dectester = (GstVideoDecoderTester *) dec;
100
101   dectester->last_buf_num = -1;
102   dectester->last_kf_num = -1;
103   dectester->set_output_state = TRUE;
104
105   return TRUE;
106 }
107
108 static gboolean
109 gst_video_decoder_tester_stop (GstVideoDecoder * dec)
110 {
111   return TRUE;
112 }
113
114 static gboolean
115 gst_video_decoder_tester_flush (GstVideoDecoder * dec)
116 {
117   GstVideoDecoderTester *dectester = (GstVideoDecoderTester *) dec;
118
119   dectester->last_buf_num = -1;
120   dectester->last_kf_num = -1;
121
122   return TRUE;
123 }
124
125 static gboolean
126 gst_video_decoder_tester_set_format (GstVideoDecoder * dec,
127     GstVideoCodecState * state)
128 {
129   GstVideoDecoderTester *dectester = (GstVideoDecoderTester *) dec;
130
131   if (dectester->set_output_state) {
132     GstVideoCodecState *res = gst_video_decoder_set_output_state (dec,
133         GST_VIDEO_FORMAT_GRAY8, TEST_VIDEO_WIDTH, TEST_VIDEO_HEIGHT, NULL);
134     gst_video_codec_state_unref (res);
135   }
136
137   return TRUE;
138 }
139
140 static GstFlowReturn
141 gst_video_decoder_tester_handle_frame (GstVideoDecoder * dec,
142     GstVideoCodecFrame * frame)
143 {
144   GstVideoDecoderTester *dectester = (GstVideoDecoderTester *) dec;
145   guint64 input_num;
146   guint8 *data;
147   gint size;
148   GstMapInfo map;
149   gboolean last_subframe = GST_BUFFER_FLAG_IS_SET (frame->input_buffer,
150       GST_VIDEO_BUFFER_FLAG_MARKER);
151
152   if (gst_video_decoder_get_subframe_mode (dec) && !last_subframe) {
153     if (!GST_CLOCK_TIME_IS_VALID (frame->pts))
154       return gst_video_decoder_drop_subframe (dec, frame);
155     goto done;
156   }
157
158   gst_buffer_map (frame->input_buffer, &map, GST_MAP_READ);
159
160   input_num = *((guint64 *) map.data);
161
162   if ((input_num == dectester->last_buf_num + 1
163           && dectester->last_buf_num != -1)
164       || !GST_BUFFER_FLAG_IS_SET (frame->input_buffer,
165           GST_BUFFER_FLAG_DELTA_UNIT) || last_subframe) {
166
167     /* the output is gray8 */
168     size = TEST_VIDEO_WIDTH * TEST_VIDEO_HEIGHT;
169     data = g_malloc0 (size);
170
171     memcpy (data, map.data, sizeof (guint64));
172
173     frame->output_buffer = gst_buffer_new_wrapped (data, size);
174     frame->pts = GST_BUFFER_PTS (frame->input_buffer);
175     frame->duration = GST_BUFFER_DURATION (frame->input_buffer);
176     dectester->last_buf_num = input_num;
177     if (!GST_BUFFER_FLAG_IS_SET (frame->input_buffer,
178             GST_BUFFER_FLAG_DELTA_UNIT))
179       dectester->last_kf_num = input_num;
180   }
181
182   gst_buffer_unmap (frame->input_buffer, &map);
183   if (GST_CLOCK_TIME_IS_VALID (frame->pts)) {
184
185     if (gst_video_decoder_get_subframe_mode (dec) && last_subframe)
186       gst_video_decoder_have_last_subframe (dec, frame);
187
188     if (frame->output_buffer)
189       return gst_video_decoder_finish_frame (dec, frame);
190   } else {
191     return gst_video_decoder_drop_frame (dec, frame);
192
193   }
194
195
196 done:
197   gst_video_codec_frame_unref (frame);
198
199   return GST_FLOW_OK;
200 }
201
202 static GstFlowReturn
203 gst_video_decoder_tester_parse (GstVideoDecoder * decoder,
204     GstVideoCodecFrame * frame, GstAdapter * adapter, gboolean at_eos)
205 {
206   gint av;
207
208   av = gst_adapter_available (adapter);
209
210   /* and pass along all */
211   gst_video_decoder_add_to_frame (decoder, av);
212   return gst_video_decoder_have_frame (decoder);
213 }
214
215 static void
216 gst_video_decoder_tester_class_init (GstVideoDecoderTesterClass * klass)
217 {
218   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
219   GstVideoDecoderClass *videodecoder_class = GST_VIDEO_DECODER_CLASS (klass);
220
221   static GstStaticPadTemplate sink_templ = GST_STATIC_PAD_TEMPLATE ("sink",
222       GST_PAD_SINK, GST_PAD_ALWAYS,
223       GST_STATIC_CAPS ("video/x-test-custom"));
224
225   static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
226       GST_PAD_SRC, GST_PAD_ALWAYS,
227       GST_STATIC_CAPS ("video/x-raw"));
228
229   gst_element_class_add_static_pad_template (element_class, &sink_templ);
230   gst_element_class_add_static_pad_template (element_class, &src_templ);
231
232   gst_element_class_set_metadata (element_class,
233       "VideoDecoderTester", "Decoder/Video", "yep", "me");
234
235   videodecoder_class->start = gst_video_decoder_tester_start;
236   videodecoder_class->stop = gst_video_decoder_tester_stop;
237   videodecoder_class->flush = gst_video_decoder_tester_flush;
238   videodecoder_class->handle_frame = gst_video_decoder_tester_handle_frame;
239   videodecoder_class->set_format = gst_video_decoder_tester_set_format;
240   videodecoder_class->parse = gst_video_decoder_tester_parse;
241 }
242
243 static void
244 gst_video_decoder_tester_init (GstVideoDecoderTester * tester)
245 {
246 }
247
248 static gboolean
249 _mysinkpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
250 {
251   events = g_list_append (events, event);
252   return TRUE;
253 }
254
255 static void
256 setup_videodecodertester (GstStaticPadTemplate * sinktmpl,
257     GstStaticPadTemplate * srctmpl)
258 {
259   if (sinktmpl == NULL)
260     sinktmpl = &sinktemplate;
261   if (srctmpl == NULL)
262     srctmpl = &srctemplate;
263
264   dec = g_object_new (GST_VIDEO_DECODER_TESTER_TYPE, NULL);
265   mysrcpad = gst_check_setup_src_pad (dec, srctmpl);
266   mysinkpad = gst_check_setup_sink_pad (dec, sinktmpl);
267
268   gst_pad_set_event_function (mysinkpad, _mysinkpad_event);
269 }
270
271 static void
272 cleanup_videodecodertest (void)
273 {
274   gst_pad_set_active (mysrcpad, FALSE);
275   gst_pad_set_active (mysinkpad, FALSE);
276   gst_check_teardown_src_pad (dec);
277   gst_check_teardown_sink_pad (dec);
278   gst_check_teardown_element (dec);
279
280   g_list_free_full (events, (GDestroyNotify) gst_event_unref);
281   events = NULL;
282 }
283
284 static GstBuffer *
285 create_test_buffer (guint64 num)
286 {
287   GstBuffer *buffer;
288   guint64 *data = g_malloc (sizeof (guint64));
289
290   *data = num;
291
292   buffer = gst_buffer_new_wrapped (data, sizeof (guint64));
293
294   GST_BUFFER_PTS (buffer) =
295       gst_util_uint64_scale_round (num, GST_SECOND * TEST_VIDEO_FPS_D,
296       TEST_VIDEO_FPS_N);
297   GST_BUFFER_DURATION (buffer) =
298       gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
299       TEST_VIDEO_FPS_N);
300
301   return buffer;
302 }
303
304 static void
305 send_startup_events (void)
306 {
307   GstCaps *caps;
308
309   fail_unless (gst_pad_push_event (mysrcpad,
310           gst_event_new_stream_start ("randomvalue")));
311
312   /* push caps */
313   caps =
314       gst_caps_new_simple ("video/x-test-custom", "width", G_TYPE_INT,
315       TEST_VIDEO_WIDTH, "height", G_TYPE_INT, TEST_VIDEO_HEIGHT, "framerate",
316       GST_TYPE_FRACTION, TEST_VIDEO_FPS_N, TEST_VIDEO_FPS_D, NULL);
317   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_caps (caps)));
318   gst_caps_unref (caps);
319 }
320
321 #define NUM_BUFFERS 1000
322 #define NUM_SUB_BUFFERS 4
323
324 GST_START_TEST (videodecoder_playback)
325 {
326   GstSegment segment;
327   GstBuffer *buffer;
328   guint64 i;
329   GList *iter;
330
331   setup_videodecodertester (NULL, NULL);
332
333   gst_pad_set_active (mysrcpad, TRUE);
334   gst_element_set_state (dec, GST_STATE_PLAYING);
335   gst_pad_set_active (mysinkpad, TRUE);
336
337   send_startup_events ();
338
339   /* push a new segment */
340   gst_segment_init (&segment, GST_FORMAT_TIME);
341   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
342
343   /* push buffers, the data is actually a number so we can track them */
344   for (i = 0; i < NUM_BUFFERS; i++) {
345     buffer = create_test_buffer (i);
346
347     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
348   }
349
350   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
351
352   /* check that all buffers were received by our source pad */
353   fail_unless (g_list_length (buffers) == NUM_BUFFERS);
354   i = 0;
355   for (iter = buffers; iter; iter = g_list_next (iter)) {
356     GstMapInfo map;
357     guint64 num;
358
359     buffer = iter->data;
360
361     gst_buffer_map (buffer, &map, GST_MAP_READ);
362
363
364     num = *(guint64 *) map.data;
365     fail_unless (i == num);
366
367     fail_unless (GST_BUFFER_PTS (buffer) == gst_util_uint64_scale_round (i,
368             GST_SECOND * TEST_VIDEO_FPS_D, TEST_VIDEO_FPS_N));
369     fail_unless (GST_BUFFER_DURATION (buffer) ==
370         gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
371             TEST_VIDEO_FPS_N));
372
373     gst_buffer_unmap (buffer, &map);
374     i++;
375   }
376
377   g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
378   buffers = NULL;
379
380   cleanup_videodecodertest ();
381 }
382
383 GST_END_TEST;
384
385
386 GST_START_TEST (videodecoder_playback_with_events)
387 {
388   GstSegment segment;
389   GstBuffer *buffer;
390   guint i;
391   GList *iter;
392   GList *events_iter;
393
394   setup_videodecodertester (NULL, NULL);
395
396   gst_pad_set_active (mysrcpad, TRUE);
397   gst_element_set_state (dec, GST_STATE_PLAYING);
398   gst_pad_set_active (mysinkpad, TRUE);
399
400   send_startup_events ();
401
402   /* push a new segment */
403   gst_segment_init (&segment, GST_FORMAT_TIME);
404   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
405
406   /* push buffers, the data is actually a number so we can track them */
407   for (i = 0; i < NUM_BUFFERS; i++) {
408     if (i % 10 == 0) {
409       GstTagList *tags;
410
411       tags = gst_tag_list_new (GST_TAG_TRACK_NUMBER, i, NULL);
412       fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_tag (tags)));
413     } else {
414       buffer = create_test_buffer (i);
415
416       fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
417     }
418   }
419
420   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
421
422   events_iter = events;
423   /* make sure the usual events have been received */
424   {
425     GstEvent *sstart = events_iter->data;
426     fail_unless (GST_EVENT_TYPE (sstart) == GST_EVENT_STREAM_START);
427     events_iter = g_list_next (events_iter);
428   }
429   {
430     GstEvent *caps_event = events_iter->data;
431     fail_unless (GST_EVENT_TYPE (caps_event) == GST_EVENT_CAPS);
432     events_iter = g_list_next (events_iter);
433   }
434   {
435     GstEvent *segment_event = events_iter->data;
436     fail_unless (GST_EVENT_TYPE (segment_event) == GST_EVENT_SEGMENT);
437     events_iter = g_list_next (events_iter);
438   }
439
440   /* check that all buffers were received by our source pad */
441   iter = buffers;
442   for (i = 0; i < NUM_BUFFERS; i++) {
443     if (i % 10 == 0) {
444       guint tag_v;
445       GstEvent *tag_event = events_iter->data;
446       GstTagList *taglist = NULL;
447
448       gst_event_parse_tag (tag_event, &taglist);
449
450       fail_unless (gst_tag_list_get_uint (taglist, GST_TAG_TRACK_NUMBER,
451               &tag_v));
452       fail_unless (tag_v == i);
453
454       events_iter = g_list_next (events_iter);
455     } else {
456       GstMapInfo map;
457       guint64 num;
458
459       buffer = iter->data;
460
461       gst_buffer_map (buffer, &map, GST_MAP_READ);
462
463       num = *(guint64 *) map.data;
464       fail_unless (i == num);
465       fail_unless (GST_BUFFER_PTS (buffer) == gst_util_uint64_scale_round (i,
466               GST_SECOND * TEST_VIDEO_FPS_D, TEST_VIDEO_FPS_N));
467       fail_unless (GST_BUFFER_DURATION (buffer) ==
468           gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
469               TEST_VIDEO_FPS_N));
470
471       gst_buffer_unmap (buffer, &map);
472       iter = g_list_next (iter);
473     }
474   }
475   fail_unless (iter == NULL);
476
477   /* check that EOS was received */
478   {
479     GstEvent *eos = events_iter->data;
480
481     fail_unless (GST_EVENT_TYPE (eos) == GST_EVENT_EOS);
482     events_iter = g_list_next (events_iter);
483   }
484
485   fail_unless (events_iter == NULL);
486
487   g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
488   buffers = NULL;
489
490   cleanup_videodecodertest ();
491 }
492
493 GST_END_TEST;
494
495 GST_START_TEST (videodecoder_flush_events)
496 {
497   GstSegment segment;
498   GstBuffer *buffer;
499   guint i;
500   GList *events_iter;
501
502   setup_videodecodertester (NULL, NULL);
503
504   gst_pad_set_active (mysrcpad, TRUE);
505   gst_element_set_state (dec, GST_STATE_PLAYING);
506   gst_pad_set_active (mysinkpad, TRUE);
507
508   send_startup_events ();
509
510   /* push a new segment */
511   gst_segment_init (&segment, GST_FORMAT_TIME);
512   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
513
514   /* push buffers, the data is actually a number so we can track them */
515   for (i = 0; i < NUM_BUFFERS; i++) {
516     if (i % 10 == 0) {
517       GstTagList *tags;
518
519       tags = gst_tag_list_new (GST_TAG_TRACK_NUMBER, i, NULL);
520       fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_tag (tags)));
521     } else {
522       buffer = create_test_buffer (i);
523
524       fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
525     }
526   }
527
528   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
529
530   events_iter = events;
531   /* make sure the usual events have been received */
532   {
533     GstEvent *sstart = events_iter->data;
534     fail_unless (GST_EVENT_TYPE (sstart) == GST_EVENT_STREAM_START);
535     events_iter = g_list_next (events_iter);
536   }
537   {
538     GstEvent *caps_event = events_iter->data;
539     fail_unless (GST_EVENT_TYPE (caps_event) == GST_EVENT_CAPS);
540     events_iter = g_list_next (events_iter);
541   }
542   {
543     GstEvent *segment_event = events_iter->data;
544     fail_unless (GST_EVENT_TYPE (segment_event) == GST_EVENT_SEGMENT);
545     events_iter = g_list_next (events_iter);
546   }
547
548   /* check that EOS was received */
549   fail_unless (GST_PAD_IS_EOS (mysrcpad));
550   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_flush_start ()));
551   fail_unless (GST_PAD_IS_EOS (mysrcpad));
552
553   /* Check that we have tags */
554   {
555     GstEvent *tags = gst_pad_get_sticky_event (mysrcpad, GST_EVENT_TAG, 0);
556
557     fail_unless (tags != NULL);
558     gst_event_unref (tags);
559   }
560
561   /* Check that we still have a segment set */
562   {
563     GstEvent *segment =
564         gst_pad_get_sticky_event (mysrcpad, GST_EVENT_SEGMENT, 0);
565
566     fail_unless (segment != NULL);
567     gst_event_unref (segment);
568   }
569
570   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_flush_stop (TRUE)));
571   fail_if (GST_PAD_IS_EOS (mysrcpad));
572
573   /* Check that the segment was flushed on FLUSH_STOP */
574   {
575     GstEvent *segment =
576         gst_pad_get_sticky_event (mysrcpad, GST_EVENT_SEGMENT, 0);
577
578     fail_unless (segment == NULL);
579   }
580
581   /* Check the tags were not lost on FLUSH_STOP */
582   {
583     GstEvent *tags = gst_pad_get_sticky_event (mysrcpad, GST_EVENT_TAG, 0);
584
585     fail_unless (tags != NULL);
586     gst_event_unref (tags);
587
588   }
589
590   g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
591   buffers = NULL;
592
593   cleanup_videodecodertest ();
594 }
595
596 GST_END_TEST;
597
598
599 /* Check https://bugzilla.gnome.org/show_bug.cgi?id=721835 */
600 GST_START_TEST (videodecoder_playback_first_frames_not_decoded)
601 {
602   GstSegment segment;
603   GstBuffer *buffer;
604   guint64 i = 0;
605
606   setup_videodecodertester (NULL, NULL);
607
608   gst_pad_set_active (mysrcpad, TRUE);
609   gst_element_set_state (dec, GST_STATE_PLAYING);
610   gst_pad_set_active (mysinkpad, TRUE);
611
612   send_startup_events ();
613
614   /* push a new segment */
615   gst_segment_init (&segment, GST_FORMAT_TIME);
616   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
617
618   /* push a buffer, to have the segment attached to it.
619    * unfortunately this buffer can't be decoded as it isn't a keyframe */
620   buffer = create_test_buffer (i++);
621   GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
622   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
623
624   /* now be evil and ask this frame to be released
625    * this frame has the segment event attached to it, and the
626    * segment shouldn't disappear with it */
627   {
628     GList *l, *ol;
629
630     ol = l = gst_video_decoder_get_frames (GST_VIDEO_DECODER (dec));
631     fail_unless (g_list_length (l) == 1);
632     while (l) {
633       GstVideoCodecFrame *tmp = l->data;
634
635       gst_video_decoder_release_frame (GST_VIDEO_DECODER (dec), tmp);
636
637       l = g_list_next (l);
638     }
639     g_list_free (ol);
640   }
641
642   buffer = create_test_buffer (i++);
643   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
644
645   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
646
647   fail_unless (g_list_length (buffers) == 1);
648
649   g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
650   buffers = NULL;
651
652   cleanup_videodecodertest ();
653 }
654
655 GST_END_TEST;
656
657 GST_START_TEST (videodecoder_buffer_after_segment)
658 {
659   GstSegment segment;
660   GstBuffer *buffer;
661   guint64 i;
662   GstClockTime pos;
663   GList *iter;
664
665   setup_videodecodertester (NULL, NULL);
666
667   gst_pad_set_active (mysrcpad, TRUE);
668   gst_element_set_state (dec, GST_STATE_PLAYING);
669   gst_pad_set_active (mysinkpad, TRUE);
670
671   send_startup_events ();
672
673   /* push a new segment */
674   gst_segment_init (&segment, GST_FORMAT_TIME);
675   segment.stop = GST_SECOND;
676   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
677
678   /* push buffers until we fill our segment */
679   i = 0;
680   pos = 0;
681   while (pos < GST_SECOND) {
682     buffer = create_test_buffer (i++);
683
684     pos = GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer);
685     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
686   }
687
688   /* pushing the next buffer should result in EOS */
689   buffer = create_test_buffer (i);
690   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_EOS);
691
692   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
693
694   /* check that all buffers were received by our source pad */
695   fail_unless (g_list_length (buffers) == i);
696   i = 0;
697   for (iter = buffers; iter; iter = g_list_next (iter)) {
698     GstMapInfo map;
699     guint64 num;
700
701     buffer = iter->data;
702
703     gst_buffer_map (buffer, &map, GST_MAP_READ);
704
705
706     num = *(guint64 *) map.data;
707     fail_unless (i == num);
708     fail_unless (GST_BUFFER_PTS (buffer) == gst_util_uint64_scale_round (i,
709             GST_SECOND * TEST_VIDEO_FPS_D, TEST_VIDEO_FPS_N));
710     fail_unless (GST_BUFFER_DURATION (buffer) ==
711         gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
712             TEST_VIDEO_FPS_N));
713
714     gst_buffer_unmap (buffer, &map);
715     i++;
716   }
717
718   g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
719   buffers = NULL;
720
721   cleanup_videodecodertest ();
722 }
723
724 GST_END_TEST;
725
726 /* make sure that the segment event is pushed before the gap */
727 GST_START_TEST (videodecoder_first_data_is_gap)
728 {
729   GstSegment segment;
730   GList *events_iter;
731
732   setup_videodecodertester (NULL, NULL);
733
734   gst_pad_set_active (mysrcpad, TRUE);
735   gst_element_set_state (dec, GST_STATE_PLAYING);
736   gst_pad_set_active (mysinkpad, TRUE);
737
738   send_startup_events ();
739
740   /* push a new segment */
741   gst_segment_init (&segment, GST_FORMAT_TIME);
742   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
743
744   /* push a gap */
745   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_gap (0,
746               GST_SECOND)));
747   events_iter = events;
748   /* make sure the usual events have been received */
749   {
750     GstEvent *sstart = events_iter->data;
751     fail_unless (GST_EVENT_TYPE (sstart) == GST_EVENT_STREAM_START);
752     events_iter = g_list_next (events_iter);
753   }
754   {
755     GstEvent *caps_event = events_iter->data;
756     fail_unless (GST_EVENT_TYPE (caps_event) == GST_EVENT_CAPS);
757     events_iter = g_list_next (events_iter);
758   }
759   {
760     GstEvent *segment_event = events_iter->data;
761     fail_unless (GST_EVENT_TYPE (segment_event) == GST_EVENT_SEGMENT);
762     events_iter = g_list_next (events_iter);
763   }
764
765   /* Make sure the gap was pushed */
766   {
767     GstEvent *gap = events_iter->data;
768     fail_unless (GST_EVENT_TYPE (gap) == GST_EVENT_GAP);
769     events_iter = g_list_next (events_iter);
770   }
771   fail_unless (events_iter == NULL);
772
773   cleanup_videodecodertest ();
774 }
775
776 GST_END_TEST;
777
778 static void
779 videodecoder_backwards_playback (gboolean subframe)
780 {
781   GstSegment segment;
782   GstBuffer *buffer;
783   guint64 i;
784   GList *iter;
785   guint num_subframes = 1;
786   guint num_buffers;
787
788   if (subframe)
789     num_subframes = 2;
790   num_buffers = NUM_BUFFERS / num_subframes;
791
792   setup_videodecodertester (NULL, NULL);
793
794   if (num_subframes > 1) {
795     gst_video_decoder_set_subframe_mode (GST_VIDEO_DECODER (dec), TRUE);
796   }
797
798   gst_pad_set_active (mysrcpad, TRUE);
799   gst_element_set_state (dec, GST_STATE_PLAYING);
800   gst_pad_set_active (mysinkpad, TRUE);
801
802   send_startup_events ();
803
804   /* push a new segment with -1 rate */
805   gst_segment_init (&segment, GST_FORMAT_TIME);
806   segment.rate = -1.0;
807   segment.stop = (num_buffers + 1) * gst_util_uint64_scale_round (GST_SECOND,
808       TEST_VIDEO_FPS_D, TEST_VIDEO_FPS_N);
809   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
810
811   /* push buffers, the data is actually a number so we can track them */
812   i = num_buffers * num_subframes;
813   while (i > 0) {
814     gint target = i;
815     gint j;
816
817     /* push groups of 10 buffers
818      * every number that is divisible by 10 is set as a discont,
819      * if it is divisible by 20 it is also a keyframe
820      *
821      * The logic here is that the current i is the target, and then
822      * it pushes buffers from 'target - 10' up to target.
823      */
824     for (j = MAX (target - 10, 0); j < target; j++) {
825       GstBuffer *buffer = create_test_buffer (j / num_subframes);
826       if ((j + 1) % num_subframes == 0)
827         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_MARKER);
828       if (j % 10 == 0)
829         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
830       if (j % 20 != 0)
831         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
832
833       fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
834       i--;
835     }
836   }
837
838   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
839
840   /* check that all buffers were received by our source pad */
841   fail_unless (g_list_length (buffers) == num_buffers);
842   i = num_buffers - 1;
843   for (iter = buffers; iter; iter = g_list_next (iter)) {
844     GstMapInfo map;
845     guint64 num;
846
847     buffer = iter->data;
848
849     gst_buffer_map (buffer, &map, GST_MAP_READ);
850
851
852     num = *(guint64 *) map.data;
853     fail_unless (i == num);
854     fail_unless (GST_BUFFER_PTS (buffer) == gst_util_uint64_scale_round (i,
855             GST_SECOND * TEST_VIDEO_FPS_D, TEST_VIDEO_FPS_N));
856     fail_unless (GST_BUFFER_DURATION (buffer) ==
857         gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
858             TEST_VIDEO_FPS_N));
859
860     gst_buffer_unmap (buffer, &map);
861     i--;
862   }
863
864   g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
865   buffers = NULL;
866
867   cleanup_videodecodertest ();
868 }
869
870 GST_START_TEST (videodecoder_backwards_playback_normal)
871 {
872   videodecoder_backwards_playback (FALSE);
873 }
874
875 GST_END_TEST;
876
877 GST_START_TEST (videodecoder_backwards_playback_subframes)
878 {
879   videodecoder_backwards_playback (TRUE);
880 }
881
882 GST_END_TEST;
883
884 GST_START_TEST (videodecoder_backwards_buffer_after_segment)
885 {
886   GstSegment segment;
887   GstBuffer *buffer;
888   guint64 i;
889   GstClockTime pos;
890
891   setup_videodecodertester (NULL, NULL);
892
893   gst_pad_set_active (mysrcpad, TRUE);
894   gst_element_set_state (dec, GST_STATE_PLAYING);
895   gst_pad_set_active (mysinkpad, TRUE);
896
897   send_startup_events ();
898
899   /* push a new segment with -1 rate */
900   gst_segment_init (&segment, GST_FORMAT_TIME);
901   segment.rate = -1.0;
902   segment.start = GST_SECOND;
903   segment.stop = (NUM_BUFFERS + 1) * gst_util_uint64_scale_round (GST_SECOND,
904       TEST_VIDEO_FPS_D, TEST_VIDEO_FPS_N);
905   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
906
907   /* push buffers, the data is actually a number so we can track them */
908   i = NUM_BUFFERS;
909   pos = segment.stop;
910   while (pos >= GST_SECOND) {
911     gint target = i;
912     gint j;
913
914     g_assert (i > 0);
915
916     /* push groups of 10 buffers
917      * every number that is divisible by 10 is set as a discont,
918      * if it is divisible by 20 it is also a keyframe
919      *
920      * The logic here is that the current i is the target, and then
921      * it pushes buffers from 'target - 10' up to target.
922      */
923     for (j = MAX (target - 10, 0); j < target; j++) {
924       buffer = create_test_buffer (j);
925
926       pos = MIN (GST_BUFFER_TIMESTAMP (buffer), pos);
927       if (j % 10 == 0)
928         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
929       if (j % 20 != 0)
930         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
931
932       fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
933       i--;
934     }
935   }
936
937   /* push a discont buffer so it flushes the decoding */
938   buffer = create_test_buffer (i - 10);
939   GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
940   GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
941   fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_EOS);
942
943   /* check that the last received buffer doesn't contain a
944    * timestamp before the segment */
945   buffer = g_list_last (buffers)->data;
946   fail_unless (GST_BUFFER_TIMESTAMP (buffer) <= segment.start
947       && GST_BUFFER_TIMESTAMP (buffer) + GST_BUFFER_DURATION (buffer) >
948       segment.start);
949
950   /* flush our decoded data queue */
951   g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
952   buffers = NULL;
953
954   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
955
956   fail_unless (buffers == NULL);
957
958   cleanup_videodecodertest ();
959 }
960
961 GST_END_TEST;
962
963
964 GST_START_TEST (videodecoder_query_caps_with_fixed_caps_peer)
965 {
966   GstCaps *caps;
967   GstCaps *filter;
968   GstStructure *structure;
969   gint width, height, fps_n, fps_d;
970
971   setup_videodecodertester (&sinktemplate_restricted, NULL);
972
973   gst_pad_set_active (mysrcpad, TRUE);
974   gst_element_set_state (dec, GST_STATE_PLAYING);
975   gst_pad_set_active (mysinkpad, TRUE);
976
977   caps = gst_pad_peer_query_caps (mysrcpad, NULL);
978   fail_unless (caps != NULL);
979
980   structure = gst_caps_get_structure (caps, 0);
981   fail_unless (gst_structure_get_int (structure, "width", &width));
982   fail_unless (gst_structure_get_int (structure, "height", &height));
983   fail_unless (gst_structure_get_fraction (structure, "framerate", &fps_n,
984           &fps_d));
985   /* match our restricted caps values */
986   fail_unless (width == RESTRICTED_CAPS_WIDTH);
987   fail_unless (height == RESTRICTED_CAPS_HEIGHT);
988   fail_unless (fps_n == RESTRICTED_CAPS_FPS_N);
989   fail_unless (fps_d == RESTRICTED_CAPS_FPS_D);
990   gst_caps_unref (caps);
991
992   filter = gst_caps_new_simple ("video/x-custom-test", "width", G_TYPE_INT,
993       1000, "height", G_TYPE_INT, 1000, "framerate", GST_TYPE_FRACTION,
994       1000, 1, NULL);
995   caps = gst_pad_peer_query_caps (mysrcpad, filter);
996   fail_unless (caps != NULL);
997   fail_unless (gst_caps_is_empty (caps));
998   gst_caps_unref (caps);
999   gst_caps_unref (filter);
1000
1001   cleanup_videodecodertest ();
1002 }
1003
1004 GST_END_TEST;
1005
1006 static void
1007 _get_int_range (GstStructure * s, const gchar * field, gint * min_v,
1008     gint * max_v)
1009 {
1010   const GValue *value;
1011
1012   value = gst_structure_get_value (s, field);
1013   fail_unless (value != NULL);
1014   fail_unless (GST_VALUE_HOLDS_INT_RANGE (value));
1015
1016   *min_v = gst_value_get_int_range_min (value);
1017   *max_v = gst_value_get_int_range_max (value);
1018 }
1019
1020 static void
1021 _get_fraction_range (GstStructure * s, const gchar * field, gint * fps_n_min,
1022     gint * fps_d_min, gint * fps_n_max, gint * fps_d_max)
1023 {
1024   const GValue *value;
1025   const GValue *min_v, *max_v;
1026
1027   value = gst_structure_get_value (s, field);
1028   fail_unless (value != NULL);
1029   fail_unless (GST_VALUE_HOLDS_FRACTION_RANGE (value));
1030
1031   min_v = gst_value_get_fraction_range_min (value);
1032   fail_unless (GST_VALUE_HOLDS_FRACTION (min_v));
1033   *fps_n_min = gst_value_get_fraction_numerator (min_v);
1034   *fps_d_min = gst_value_get_fraction_denominator (min_v);
1035
1036   max_v = gst_value_get_fraction_range_max (value);
1037   fail_unless (GST_VALUE_HOLDS_FRACTION (max_v));
1038   *fps_n_max = gst_value_get_fraction_numerator (max_v);
1039   *fps_d_max = gst_value_get_fraction_denominator (max_v);
1040 }
1041
1042 GST_START_TEST (videodecoder_query_caps_with_range_caps_peer)
1043 {
1044   GstCaps *caps;
1045   GstCaps *filter;
1046   GstStructure *structure;
1047   gint width, height, fps_n, fps_d;
1048   gint width_min, height_min, fps_n_min, fps_d_min;
1049   gint width_max, height_max, fps_n_max, fps_d_max;
1050
1051   setup_videodecodertester (&sinktemplate_with_range, NULL);
1052
1053   gst_pad_set_active (mysrcpad, TRUE);
1054   gst_element_set_state (dec, GST_STATE_PLAYING);
1055   gst_pad_set_active (mysinkpad, TRUE);
1056
1057   caps = gst_pad_peer_query_caps (mysrcpad, NULL);
1058   fail_unless (caps != NULL);
1059
1060   structure = gst_caps_get_structure (caps, 0);
1061   _get_int_range (structure, "width", &width_min, &width_max);
1062   _get_int_range (structure, "height", &height_min, &height_max);
1063   _get_fraction_range (structure, "framerate", &fps_n_min, &fps_d_min,
1064       &fps_n_max, &fps_d_max);
1065   fail_unless (width_min == 1);
1066   fail_unless (width_max == RESTRICTED_CAPS_WIDTH);
1067   fail_unless (height_min == 1);
1068   fail_unless (height_max == RESTRICTED_CAPS_HEIGHT);
1069   fail_unless (fps_n_min == 1);
1070   fail_unless (fps_d_min == 1);
1071   fail_unless (fps_n_max == RESTRICTED_CAPS_FPS_N);
1072   fail_unless (fps_d_max == RESTRICTED_CAPS_FPS_D);
1073   gst_caps_unref (caps);
1074
1075   /* query with a fixed filter */
1076   filter = gst_caps_new_simple ("video/x-test-custom", "width", G_TYPE_INT,
1077       RESTRICTED_CAPS_WIDTH, "height", G_TYPE_INT, RESTRICTED_CAPS_HEIGHT,
1078       "framerate", GST_TYPE_FRACTION, RESTRICTED_CAPS_FPS_N,
1079       RESTRICTED_CAPS_FPS_D, NULL);
1080   caps = gst_pad_peer_query_caps (mysrcpad, filter);
1081   fail_unless (caps != NULL);
1082   structure = gst_caps_get_structure (caps, 0);
1083   fail_unless (gst_structure_get_int (structure, "width", &width));
1084   fail_unless (gst_structure_get_int (structure, "height", &height));
1085   fail_unless (gst_structure_get_fraction (structure, "framerate", &fps_n,
1086           &fps_d));
1087   fail_unless (width == RESTRICTED_CAPS_WIDTH);
1088   fail_unless (height == RESTRICTED_CAPS_HEIGHT);
1089   fail_unless (fps_n == RESTRICTED_CAPS_FPS_N);
1090   fail_unless (fps_d == RESTRICTED_CAPS_FPS_D);
1091   gst_caps_unref (caps);
1092   gst_caps_unref (filter);
1093
1094   /* query with a fixed filter that will lead to empty result */
1095   filter = gst_caps_new_simple ("video/x-test-custom", "width", G_TYPE_INT,
1096       1000, "height", G_TYPE_INT, 1000, "framerate", GST_TYPE_FRACTION,
1097       1000, 1, NULL);
1098   caps = gst_pad_peer_query_caps (mysrcpad, filter);
1099   fail_unless (caps != NULL);
1100   fail_unless (gst_caps_is_empty (caps));
1101   gst_caps_unref (caps);
1102   gst_caps_unref (filter);
1103
1104   cleanup_videodecodertest ();
1105 }
1106
1107 GST_END_TEST;
1108
1109 #define GETCAPS_CAPS_STR "video/x-test-custom, somefield=(string)getcaps"
1110 static GstCaps *
1111 _custom_video_decoder_getcaps (GstVideoDecoder * dec, GstCaps * filter)
1112 {
1113   return gst_caps_from_string (GETCAPS_CAPS_STR);
1114 }
1115
1116 GST_START_TEST (videodecoder_query_caps_with_custom_getcaps)
1117 {
1118   GstCaps *caps;
1119   GstVideoDecoderClass *klass;
1120   GstCaps *expected_caps;
1121
1122   setup_videodecodertester (&sinktemplate_restricted, NULL);
1123
1124   klass = GST_VIDEO_DECODER_CLASS (GST_VIDEO_DECODER_GET_CLASS (dec));
1125   klass->getcaps = _custom_video_decoder_getcaps;
1126
1127   gst_pad_set_active (mysrcpad, TRUE);
1128   gst_element_set_state (dec, GST_STATE_PLAYING);
1129   gst_pad_set_active (mysinkpad, TRUE);
1130
1131   caps = gst_pad_peer_query_caps (mysrcpad, NULL);
1132   fail_unless (caps != NULL);
1133
1134   expected_caps = gst_caps_from_string (GETCAPS_CAPS_STR);
1135   fail_unless (gst_caps_is_equal (expected_caps, caps));
1136   gst_caps_unref (expected_caps);
1137   gst_caps_unref (caps);
1138
1139   cleanup_videodecodertest ();
1140 }
1141
1142 GST_END_TEST;
1143
1144 static const gchar *test_default_caps[][2] = {
1145   {
1146         "video/x-test-custom",
1147       "video/x-raw, format=I420, width=1280, height=720, framerate=0/1, multiview-mode=mono"}, {
1148         "video/x-test-custom, width=1000",
1149       "video/x-raw, format=I420, width=1000, height=720, framerate=0/1, multiview-mode=mono"}, {
1150         "video/x-test-custom, height=500",
1151       "video/x-raw, format=I420, width=1280, height=500, framerate=0/1, multiview-mode=mono"}, {
1152         "video/x-test-custom, framerate=10/1",
1153       "video/x-raw, format=I420, width=1280, height=720, framerate=10/1, multiview-mode=mono"}, {
1154         "video/x-test-custom, pixel-aspect-ratio=2/1",
1155       "video/x-raw, format=I420, width=1280, height=720, framerate=0/1,"
1156         "pixel-aspect-ratio=2/1, multiview-mode=mono"}
1157 };
1158
1159 GST_START_TEST (videodecoder_default_caps_on_gap_before_buffer)
1160 {
1161   GstVideoDecoderTester *dec =
1162       g_object_new (GST_VIDEO_DECODER_TESTER_TYPE, NULL);
1163   GstHarness *h =
1164       gst_harness_new_with_element (GST_ELEMENT (dec), "sink", "src");
1165   GstEvent *event;
1166   GstCaps *caps1, *caps2;
1167   GstVideoInfo info1, info2;
1168
1169   /* Don't set output state since we want trigger the default output caps */
1170   dec->set_output_state = FALSE;
1171   gst_harness_set_src_caps_str (h, test_default_caps[__i__][0]);
1172
1173   fail_unless (gst_harness_push_event (h, gst_event_new_gap (0, GST_SECOND)));
1174
1175   fail_unless_equals_int (gst_harness_events_received (h), 4);
1176
1177   event = gst_harness_pull_event (h);
1178   fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_STREAM_START);
1179   gst_event_unref (event);
1180
1181   event = gst_harness_pull_event (h);
1182   fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_CAPS);
1183   gst_event_unref (event);
1184
1185   event = gst_harness_pull_event (h);
1186   fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_SEGMENT);
1187   gst_event_unref (event);
1188
1189   event = gst_harness_pull_event (h);
1190   fail_unless (GST_EVENT_TYPE (event) == GST_EVENT_GAP);
1191   gst_event_unref (event);
1192
1193   caps1 = gst_pad_get_current_caps (h->sinkpad);
1194   caps2 = gst_caps_from_string (test_default_caps[__i__][1]);
1195   gst_video_info_from_caps (&info1, caps1);
1196   gst_video_info_from_caps (&info2, caps2);
1197
1198   gst_caps_unref (caps1);
1199   gst_caps_unref (caps2);
1200
1201   fail_unless (gst_video_info_is_equal (&info1, &info2));
1202
1203   gst_harness_teardown (h);
1204   gst_object_unref (dec);
1205 }
1206
1207 GST_END_TEST;
1208
1209 GST_START_TEST (videodecoder_playback_event_order)
1210 {
1211   GstSegment segment;
1212   GstBuffer *buffer;
1213   guint i = 0;
1214   GList *events_iter;
1215
1216   setup_videodecodertester (NULL, NULL);
1217
1218   gst_pad_set_active (mysrcpad, TRUE);
1219   gst_element_set_state (dec, GST_STATE_PLAYING);
1220   gst_pad_set_active (mysinkpad, TRUE);
1221
1222   send_startup_events ();
1223
1224   /* push a new segment */
1225   gst_segment_init (&segment, GST_FORMAT_TIME);
1226   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
1227
1228   /* push 5 buffer with one event each. All buffers except the last
1229    * one are dropped in some way, so the events are collected in various
1230    * places. The order must be preserved.
1231    * With the first buffer the segment event is added to the pending event
1232    * list to ensure that incorrect ordering can be detected for later
1233    * events.
1234    */
1235   for (i = 0; i < 9; i++) {
1236     if (i % 2 == 0) {
1237       buffer = create_test_buffer (i);
1238       if (i < 8)
1239         GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
1240       fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
1241       if (i < 6) {
1242         GList *l, *ol;
1243
1244         ol = l = gst_video_decoder_get_frames (GST_VIDEO_DECODER (dec));
1245         fail_unless (g_list_length (l) == 1);
1246         while (l) {
1247           GstVideoCodecFrame *tmp = l->data;
1248
1249           if (i < 4)
1250             gst_video_decoder_release_frame (GST_VIDEO_DECODER (dec), tmp);
1251           else
1252             gst_video_decoder_drop_frame (GST_VIDEO_DECODER (dec), tmp);
1253
1254           l = g_list_next (l);
1255         }
1256         g_list_free (ol);
1257       }
1258     } else {
1259       GstTagList *tags;
1260       tags = gst_tag_list_new (GST_TAG_TRACK_NUMBER, i, NULL);
1261       fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_tag (tags)));
1262     }
1263   }
1264
1265   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
1266
1267   events_iter = events;
1268   /* make sure the usual events have been received */
1269   {
1270     GstEvent *sstart = events_iter->data;
1271     fail_unless (GST_EVENT_TYPE (sstart) == GST_EVENT_STREAM_START);
1272     events_iter = g_list_next (events_iter);
1273   }
1274   {
1275     GstEvent *caps_event = events_iter->data;
1276     fail_unless (GST_EVENT_TYPE (caps_event) == GST_EVENT_CAPS);
1277     events_iter = g_list_next (events_iter);
1278   }
1279   {
1280     GstEvent *segment_event = events_iter->data;
1281     fail_unless (GST_EVENT_TYPE (segment_event) == GST_EVENT_SEGMENT);
1282     events_iter = g_list_next (events_iter);
1283   }
1284
1285   /* Check the order of the tag events */
1286   for (i = 1; i < 9; i += 2) {
1287     guint tag_v;
1288     GstEvent *tag_event = events_iter->data;
1289     GstTagList *taglist = NULL;
1290
1291     fail_unless (GST_EVENT_TYPE (tag_event) == GST_EVENT_TAG);
1292     gst_event_parse_tag (tag_event, &taglist);
1293
1294     fail_unless (gst_tag_list_get_uint (taglist, GST_TAG_TRACK_NUMBER, &tag_v));
1295     fail_unless (tag_v == i);
1296
1297     events_iter = g_list_next (events_iter);
1298   }
1299
1300   /* check that EOS was received */
1301   {
1302     GstEvent *eos = events_iter->data;
1303
1304     fail_unless (GST_EVENT_TYPE (eos) == GST_EVENT_EOS);
1305     events_iter = g_list_next (events_iter);
1306   }
1307
1308   fail_unless (events_iter == NULL);
1309
1310   g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
1311   buffers = NULL;
1312
1313   cleanup_videodecodertest ();
1314 }
1315
1316 GST_END_TEST;
1317
1318 typedef enum
1319 {
1320   MODE_NONE = 0,
1321   MODE_SUBFRAMES = 1,
1322   MODE_PACKETIZED = 1 << 1,
1323   MODE_META_ROI = 1 << 2,
1324 } SubframeMode;
1325
1326 static void
1327 videodecoder_playback_subframe_mode (SubframeMode mode)
1328 {
1329   GstSegment segment;
1330   GstBuffer *buffer;
1331   guint i;
1332   GList *iter;
1333   gint num_buffers = NUM_BUFFERS;
1334   gint num_subframes = 1;
1335   GList *list;
1336   gint num_roi_metas = 0;
1337
1338   setup_videodecodertester (NULL, NULL);
1339
1340   /* Allow to test combination of subframes and packetized configuration
1341    * 0-0: no subframes not packetized.
1342    * 0-1: subframes not packetized.
1343    * 1-0: no subframes packetized.
1344    * 1-1: subframes and packetized.
1345    */
1346   if (mode & MODE_SUBFRAMES) {
1347     gst_video_decoder_set_subframe_mode (GST_VIDEO_DECODER (dec), TRUE);
1348     num_subframes = NUM_SUB_BUFFERS;
1349   } else {
1350     gst_video_decoder_set_subframe_mode (GST_VIDEO_DECODER (dec), FALSE);
1351     num_subframes = 1;
1352   }
1353   gst_video_decoder_set_packetized (GST_VIDEO_DECODER (dec),
1354       mode & MODE_PACKETIZED ? TRUE : FALSE);
1355
1356   gst_pad_set_active (mysrcpad, TRUE);
1357   gst_element_set_state (dec, GST_STATE_PLAYING);
1358   gst_pad_set_active (mysinkpad, TRUE);
1359
1360   send_startup_events ();
1361
1362   /* push a new segment */
1363   gst_segment_init (&segment, GST_FORMAT_TIME);
1364   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
1365
1366   /* push header only in packetized subframe mode */
1367   if (mode == (MODE_PACKETIZED | MODE_SUBFRAMES)) {
1368     buffer = gst_buffer_new_and_alloc (0);
1369     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_HEADER);
1370     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
1371   }
1372
1373   /* push buffers, the data is actually a number so we can track them */
1374   for (i = 0; i < num_buffers; i++) {
1375     buffer = create_test_buffer (i / num_subframes);
1376     if ((i + 1) % num_subframes == 0)
1377       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_MARKER);
1378     if (mode & MODE_META_ROI)
1379       gst_buffer_add_video_region_of_interest_meta (buffer, "face", 0, 0, 10,
1380           10);
1381
1382     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
1383     fail_unless (gst_pad_push_event (mysrcpad,
1384             gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
1385                 gst_structure_new_empty ("custom1"))));
1386   }
1387   /* Send EOS */
1388   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
1389
1390   /* Test that no frames or pending events are remaining in the base class */
1391   list = gst_video_decoder_get_frames (GST_VIDEO_DECODER (dec));
1392   fail_unless (g_list_length (list) == 0);
1393   g_list_free_full (list, (GDestroyNotify) gst_video_codec_frame_unref);
1394
1395   /* check that all buffers were received by our source pad 1 output buffer for 4 input buffer */
1396   fail_unless (g_list_length (buffers) == num_buffers / num_subframes);
1397
1398   i = 0;
1399   for (iter = buffers; iter; iter = g_list_next (iter)) {
1400     GstMapInfo map;
1401     guint num;
1402     GstMeta *meta;
1403     gpointer state = NULL;
1404
1405     buffer = iter->data;
1406     while ((meta = gst_buffer_iterate_meta (buffer, &state))) {
1407       if (meta->info->api == GST_VIDEO_REGION_OF_INTEREST_META_API_TYPE)
1408         num_roi_metas++;
1409     }
1410     gst_buffer_map (buffer, &map, GST_MAP_READ);
1411     /* Test that the buffer is carrying the expected value 'num' */
1412     num = *(guint64 *) map.data;
1413
1414     fail_unless (i == num);
1415     /* Test that the buffer metadata are correct */
1416     fail_unless (GST_BUFFER_PTS (buffer) == gst_util_uint64_scale_round (i,
1417             GST_SECOND * TEST_VIDEO_FPS_D, TEST_VIDEO_FPS_N));
1418     fail_unless (GST_BUFFER_DURATION (buffer) ==
1419         gst_util_uint64_scale_round (GST_SECOND, TEST_VIDEO_FPS_D,
1420             TEST_VIDEO_FPS_N));
1421
1422
1423     gst_buffer_unmap (buffer, &map);
1424     i++;
1425   }
1426
1427   if (mode &= MODE_META_ROI)
1428     fail_unless (num_roi_metas == num_buffers);
1429
1430   g_list_free_full (buffers, (GDestroyNotify) gst_buffer_unref);
1431   buffers = NULL;
1432
1433   cleanup_videodecodertest ();
1434 }
1435
1436 static void
1437 videodecoder_playback_invalid_ts_subframe_mode (SubframeMode mode)
1438 {
1439   GstSegment segment;
1440   GstBuffer *buffer;
1441   guint i;
1442   gint num_buffers = NUM_BUFFERS;
1443   gint num_subframes = 1;
1444   GList *list;
1445
1446   setup_videodecodertester (NULL, NULL);
1447
1448   /* Allow to test combination of subframes and packetized configuration
1449    * 0-0: no subframes not packetized.
1450    * 0-1: subframes not packetized.
1451    * 1-0: no subframes packetized.
1452    * 1-1: subframes and packetized.
1453    */
1454   if (mode & MODE_SUBFRAMES) {
1455     gst_video_decoder_set_subframe_mode (GST_VIDEO_DECODER (dec), TRUE);
1456     num_subframes = NUM_SUB_BUFFERS;
1457   }
1458
1459   gst_video_decoder_set_packetized (GST_VIDEO_DECODER (dec),
1460       mode & MODE_PACKETIZED ? TRUE : FALSE);
1461
1462   gst_pad_set_active (mysrcpad, TRUE);
1463   gst_element_set_state (dec, GST_STATE_PLAYING);
1464   gst_pad_set_active (mysinkpad, TRUE);
1465
1466   send_startup_events ();
1467
1468   /* push a new segment */
1469   gst_segment_init (&segment, GST_FORMAT_TIME);
1470
1471   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_segment (&segment)));
1472
1473   /* push header only in packetized subframe mode */
1474   if (mode == (MODE_PACKETIZED | MODE_SUBFRAMES)) {
1475     buffer = gst_buffer_new_and_alloc (0);
1476     GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_HEADER);
1477     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
1478   }
1479
1480   /* push buffers, the data is actually a number so we can track them */
1481   for (i = 0; i < num_buffers; i++) {
1482     buffer = create_test_buffer (i / num_subframes);
1483     GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
1484     if ((i + 1) % num_subframes == 0)
1485       GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_MARKER);
1486
1487     fail_unless (gst_pad_push (mysrcpad, buffer) == GST_FLOW_OK);
1488     fail_unless (gst_pad_push_event (mysrcpad,
1489             gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM,
1490                 gst_structure_new_empty ("custom1"))));
1491   }
1492   /* Send EOS */
1493   fail_unless (gst_pad_push_event (mysrcpad, gst_event_new_eos ()));
1494
1495   /* Test that no frames or pending events are remaining in the base class */
1496   list = gst_video_decoder_get_frames (GST_VIDEO_DECODER (dec));
1497   fail_unless (g_list_length (list) == 0);
1498   g_list_free_full (list, (GDestroyNotify) gst_video_codec_frame_unref);
1499
1500   /* check that all buffers were received by our source pad 1 output buffer for 4 input buffer */
1501   fail_unless (g_list_length (buffers) == 0);
1502
1503
1504   cleanup_videodecodertest ();
1505 }
1506
1507 GST_START_TEST (videodecoder_playback_parsed)
1508 {
1509   videodecoder_playback_subframe_mode (MODE_NONE);
1510 }
1511
1512 GST_END_TEST;
1513
1514 GST_START_TEST (videodecoder_playback_packetized)
1515 {
1516   videodecoder_playback_subframe_mode (MODE_PACKETIZED);
1517 }
1518
1519 GST_END_TEST;
1520
1521 GST_START_TEST (videodecoder_playback_parsed_subframes)
1522 {
1523   videodecoder_playback_subframe_mode (MODE_SUBFRAMES);
1524 }
1525
1526 GST_END_TEST;
1527
1528 GST_START_TEST (videodecoder_playback_packetized_subframes)
1529 {
1530   videodecoder_playback_subframe_mode (MODE_SUBFRAMES | MODE_PACKETIZED);
1531 }
1532
1533 GST_END_TEST;
1534
1535 GST_START_TEST (videodecoder_playback_packetized_subframes_metadata)
1536 {
1537   videodecoder_playback_subframe_mode (MODE_SUBFRAMES |
1538       MODE_PACKETIZED | MODE_META_ROI);
1539 }
1540
1541 GST_END_TEST;
1542
1543 GST_START_TEST (videodecoder_playback_invalid_ts_packetized)
1544 {
1545   videodecoder_playback_invalid_ts_subframe_mode (MODE_PACKETIZED);
1546 }
1547
1548 GST_END_TEST;
1549
1550 GST_START_TEST (videodecoder_playback_invalid_ts_packetized_subframes)
1551 {
1552   videodecoder_playback_invalid_ts_subframe_mode (MODE_SUBFRAMES |
1553       MODE_PACKETIZED);
1554 }
1555
1556 GST_END_TEST;
1557
1558
1559
1560 static Suite *
1561 gst_videodecoder_suite (void)
1562 {
1563   Suite *s = suite_create ("GstVideoDecoder");
1564   TCase *tc = tcase_create ("general");
1565
1566   suite_add_tcase (s, tc);
1567
1568   tcase_add_test (tc, videodecoder_query_caps_with_fixed_caps_peer);
1569   tcase_add_test (tc, videodecoder_query_caps_with_range_caps_peer);
1570   tcase_add_test (tc, videodecoder_query_caps_with_custom_getcaps);
1571
1572   tcase_add_test (tc, videodecoder_playback);
1573   tcase_add_test (tc, videodecoder_playback_with_events);
1574   tcase_add_test (tc, videodecoder_playback_first_frames_not_decoded);
1575   tcase_add_test (tc, videodecoder_buffer_after_segment);
1576   tcase_add_test (tc, videodecoder_first_data_is_gap);
1577
1578   tcase_add_test (tc, videodecoder_backwards_playback_normal);
1579   tcase_add_test (tc, videodecoder_backwards_playback_subframes);
1580   tcase_add_test (tc, videodecoder_backwards_buffer_after_segment);
1581   tcase_add_test (tc, videodecoder_flush_events);
1582
1583   tcase_add_loop_test (tc, videodecoder_default_caps_on_gap_before_buffer, 0,
1584       G_N_ELEMENTS (test_default_caps));
1585
1586   tcase_add_test (tc, videodecoder_playback_event_order);
1587   tcase_add_test (tc, videodecoder_playback_parsed);
1588   tcase_add_test (tc, videodecoder_playback_packetized);
1589   tcase_add_test (tc, videodecoder_playback_parsed_subframes);
1590   tcase_add_test (tc, videodecoder_playback_packetized_subframes);
1591   tcase_add_test (tc, videodecoder_playback_packetized_subframes_metadata);
1592   tcase_add_test (tc, videodecoder_playback_invalid_ts_packetized);
1593   tcase_add_test (tc, videodecoder_playback_invalid_ts_packetized_subframes);
1594
1595   return s;
1596 }
1597
1598 GST_CHECK_MAIN (gst_videodecoder);