File: test-gst.c

package info (click to toggle)
spice 0.14.0-1.3%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,008 kB
  • sloc: ansic: 74,895; sh: 4,580; python: 3,025; makefile: 629
file content (1071 lines) | stat: -rw-r--r-- 32,589 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
332
333
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2016 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/* Utility to check video encoder code
 * (see program_description below)
 */
#undef NDEBUG
#include <config.h>
#include <stdio.h>
#include <glib.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <common/log.h>
#include <gst/gst.h>
#include <gst/app/gstappsrc.h>
#include <gst/app/gstappsink.h>

#include "video-encoder.h"
#include "spice-bitmap-utils.h"
#include "reds.h" // reds_get_mm_time

static const char program_description[] =
    "2 GStreamer plugins are used in a chain like this:\n"
    "    (1) input pipeline -> (2) video encoder -> (3) output pipeline\n"
    "While converting output from (1) is compared with output of (3)\n"
    "making sure the streaming is working correctly.\n"
    "\n"
    "As an example you can use a command like\n"
    "\n"
    "  $ ./test-gst -e gstreamer:vp8 -i \\\n"
    "    'filesrc location=bbb_sunflower_1080p_30fps_normal.mp4 \\\n"
    "    ! decodebin ! videoconvert'\n"
    "\n"
    "to check vp8 encoding.";

// clipping informations passed in command line
typedef enum {
    COORDS_INVALID,
    COORDS_NUMBER,
    COORDS_PERCENT,
} CoordsUnit;
static struct {
    unsigned int value;
    CoordsUnit unit;
} clipping_coords[4];
enum {
    COORDS_BOX,
    COORDS_SIZE
} clipping_type;

typedef struct {
    gint refs;
    SpiceBitmap *bitmap;
} TestFrame;

#ifdef HAVE_GSTREAMER_0_10

#define VIDEOCONVERT "ffmpegcolorspace"
#define BGRx_CAPS "caps=video/x-raw-rgb,bpp=32,depth=24,blue_mask=-16777216,green_mask=16711680,red_mask=65280"

typedef GstBuffer GstSample;
#define gst_sample_get_buffer(s) (s)
#define gst_sample_get_caps(s) GST_BUFFER_CAPS(s)
#define gst_sample_unref(s) gst_buffer_unref(s)
#define gst_app_sink_pull_sample(s) gst_app_sink_pull_buffer(s)
typedef struct {
    uint8_t *data;
} GstMapInfo;
#define GST_MAP_READ 1
static void
gst_buffer_unmap(GstBuffer *buffer, GstMapInfo *mapinfo)
{ }

static gboolean
gst_buffer_map(GstBuffer *buffer, GstMapInfo *mapinfo, int flags)
{
    mapinfo->data = GST_BUFFER_DATA(buffer);
    return mapinfo->data != NULL;
}

static GstBuffer*
gst_buffer_new_wrapped_full(int flags SPICE_GNUC_UNUSED, gpointer data, gsize maxsize,
                            gsize offset, gsize size,
                            gpointer user_data, GDestroyNotify notify)
{
    GstBuffer *buffer = gst_buffer_new();

    buffer->malloc_data = user_data;
    GST_BUFFER_FREE_FUNC(buffer) = notify;
    GST_BUFFER_DATA(buffer) = (uint8_t *) data + offset;
    GST_BUFFER_SIZE(buffer) = size;

    return buffer;
}

#define GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS 0

#define gst_bus_set_sync_handler(bus, proc, param, destroy) G_STMT_START {\
    SPICE_VERIFY(destroy == NULL); \
    gst_bus_set_sync_handler(bus, proc, param); \
} G_STMT_END
#else
#define VIDEOCONVERT "videoconvert"
#define BGRx_CAPS "caps=video/x-raw,format=BGRx"
#endif

typedef void (*SampleProc)(GstSample *sample, void *param);

typedef struct {
    GstAppSrc *appsrc;
    GstAppSink *appsink;
    GstElement *gst_pipeline;
    SampleProc sample_proc;
    void *sample_param;
    gboolean got_eos;
} TestPipeline;

typedef struct {
    const char *name;
    new_video_encoder_t new_encoder;
    SpiceVideoCodecType coded_type;
    // see spice-gtk channel-display-gst.c
    const char *caps;
    const char *decoder;
} EncoderInfo;

// our video encoder we are testing
static VideoEncoder *video_encoder = NULL;

// image settings
static gboolean top_down = FALSE;
static SpiceBitmapFmt bitmap_format = SPICE_BITMAP_FMT_32BIT;
static gint image_split_lines = 60000;

static gboolean clipping_type_computed = FALSE;
static SpiceRect clipping_rect;
static pthread_mutex_t frame_queue_mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t frame_queue_cond = PTHREAD_COND_INITIALIZER;
static GQueue frame_queue = G_QUEUE_INIT;
// input frames are counted
static unsigned input_frame_index = 0;
// file output for report informations like
// frame output size
static FILE *file_report;
static TestPipeline *input_pipeline, *output_pipeline;
static pthread_mutex_t eos_mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t eos_cond = PTHREAD_COND_INITIALIZER;
// minimum image different expected, depends on quality
// and encoder
static gdouble minimum_psnr = 25;
static uint64_t starting_bit_rate = 3000000;

static void compute_clipping_rect(GstSample *sample);
static void parse_clipping(const char *clipping);
static TestFrame *gst_to_spice_frame(GstSample *sample);
static void bitmap_free(SpiceBitmap *bitmap);
static void frame_ref(TestFrame *frame);
static void frame_unref(TestFrame *frame);
static void pipeline_free(TestPipeline *pipeline);
static void pipeline_send_raw_data(TestPipeline *pipeline, VideoBuffer *buffer);
static void pipeline_wait_eos(TestPipeline *pipeline);
static void create_input_pipeline(const char *input_pipeline,
                                  SampleProc sample_proc, void *param);
static void create_output_pipeline(const EncoderInfo *encoder,
                                   SampleProc output_frames, void *param);
static void create_video_encoder(const EncoderInfo *encoder);
static const EncoderInfo *get_encoder_info(const char *encoder_name);
static SpiceBitmapFmt get_bitmap_format(const char *format);
static double compute_psnr(SpiceBitmap *bitmap1, int32_t x1, int32_t y1,
                           SpiceBitmap *bitmap2, int32_t x2, int32_t y2,
                           int32_t w, int32_t h);

// handle output frames from input pipeline
static void
input_frames(GstSample *sample, void *param)
{
    unsigned curr_frame_index = input_frame_index++;

    spice_assert(video_encoder && sample);

    if (SPICE_UNLIKELY(!clipping_type_computed)) {
        compute_clipping_rect(sample);
    }

    VideoBuffer *p_outbuf = NULL;
    // TODO correct ?? emulate another timer ??
    uint32_t frame_mm_time = reds_get_mm_time();

    // convert frame to SpiceBitmap/DRM prime
    TestFrame *frame = gst_to_spice_frame(sample);

    // send frame to our video encoder (must be from a single thread)
    int res = video_encoder->encode_frame(video_encoder, frame_mm_time, frame->bitmap,
                                          &clipping_rect, top_down, frame,
                                          &p_outbuf);
    switch (res) {
    case VIDEO_ENCODER_FRAME_ENCODE_DONE:
        // save frame into queue for comparison later
        frame_ref(frame);
        pthread_mutex_lock(&frame_queue_mtx);
        g_queue_push_tail(&frame_queue, frame);
        while (g_queue_get_length(&frame_queue) >= 16) {
            pthread_cond_wait(&frame_queue_cond, &frame_queue_mtx);
        }
        pthread_mutex_unlock(&frame_queue_mtx);
        spice_assert(p_outbuf);
        pipeline_send_raw_data(output_pipeline, p_outbuf);
        if (file_report) {
            fprintf(file_report,
                    "Frame: %u\n"
                    "Output size: %u\n",
                    curr_frame_index,
                    (unsigned) p_outbuf->size);
        }
        break;
    case VIDEO_ENCODER_FRAME_UNSUPPORTED:
        // ?? what to do ??
        // should not happen, format passed should be supported
        // could happen for serious problems and encoder gave up
        spice_assert(0);
        break;
    case VIDEO_ENCODER_FRAME_DROP:
        if (file_report) {
            fprintf(file_report,
                    "Frame: %u\n"
                    "Output size: 0\n",
                    curr_frame_index);
        }
        break;
    default:
        // invalid value returned
        spice_assert(0);
    }

    // TODO call client_stream_report to simulate this report from the client

    frame_unref(frame);
}

// handle output frames from output pipeline
static void
output_frames(GstSample *sample, void *param)
{
    TestFrame *curr_frame = gst_to_spice_frame(sample);

    // get first frame queued
    pthread_mutex_lock(&frame_queue_mtx);
    TestFrame *expected_frame = g_queue_pop_head(&frame_queue);
    pthread_cond_signal(&frame_queue_cond);
    pthread_mutex_unlock(&frame_queue_mtx);
    if (!expected_frame) {
        g_printerr("Frame not present in the queue but arrived in output!\n");
        exit(1);
    }

    // TODO try to understand if this is correct
    if (!top_down) {
        curr_frame->bitmap->flags ^= SPICE_BITMAP_FLAGS_TOP_DOWN;
    }
#ifdef DUMP_BITMAP
    dump_bitmap(expected_frame->bitmap);
    dump_bitmap(curr_frame->bitmap);
#endif

    // compute difference
    double psnr = compute_psnr(expected_frame->bitmap, clipping_rect.left, clipping_rect.top,
                               curr_frame->bitmap, 0, 0,
                               clipping_rect.right - clipping_rect.left,
                               clipping_rect.bottom - clipping_rect.top);

    // check is more or less the same
    if (psnr < minimum_psnr) {
        g_printerr("Frame PSNR too low, got %g minimum %g\n", psnr, minimum_psnr);
        exit(1);
    }

    frame_unref(expected_frame);
    frame_unref(curr_frame);
}

static const EncoderInfo encoder_infos[] = {
    { "mjpeg", mjpeg_encoder_new, SPICE_VIDEO_CODEC_TYPE_MJPEG,
      "caps=image/jpeg", "jpegdec" },
#define encoder_info_mjpeg (&encoder_infos[0])
    { "gstreamer:mjpeg", gstreamer_encoder_new, SPICE_VIDEO_CODEC_TYPE_MJPEG,
      "caps=image/jpeg", "jpegdec" },
    { "gstreamer:vp8",   gstreamer_encoder_new, SPICE_VIDEO_CODEC_TYPE_VP8,
      "caps=video/x-vp8", "vp8dec" },
    { "gstreamer:vp9",   gstreamer_encoder_new, SPICE_VIDEO_CODEC_TYPE_VP9,
      "caps=video/x-vp9", "vp9dec" },
    { "gstreamer:h264",  gstreamer_encoder_new, SPICE_VIDEO_CODEC_TYPE_H264,
#ifdef HAVE_GSTREAMER_0_10
      "", "h264parse ! ffdec_h264" },
#else
      "", "h264parse ! avdec_h264" },
#endif
    { NULL, NULL, SPICE_VIDEO_CODEC_TYPE_ENUM_END, NULL, NULL }
};

int main(int argc, char *argv[])
{
    gchar *input_pipeline_desc = NULL;
    gchar *image_format = NULL;
    gchar *encoder_name = NULL;
    gchar *file_report_name = NULL;
    gboolean use_hw_encoder = FALSE; // TODO use
    gchar *clipping = NULL;

    // - input pipeline
    // - top/down
    // - format for video encoder input (bits, rgb/bgr)
    // - encoder (mjpeg/vp8/h264)
    // - use h/w acceleration (if available)
    // - clipping (part of the source)
    // - TODO bandwidth changes?
    // - TODO fps ??
    GOptionEntry entries[] = {
        { "input-pipeline", 'i', 0, G_OPTION_ARG_STRING, &input_pipeline_desc,
          "GStreamer input pipeline", "PIPELINE" },
        { "top-down", 0, 0, G_OPTION_ARG_NONE, &top_down,
          "Image encoded as top-down", NULL },
        { "format", 'f', 0, G_OPTION_ARG_STRING, &image_format,
          "Image format (16BIT/24BIT/32BIT/RGBA)", "FMT" },
        { "encoder", 'e', 0, G_OPTION_ARG_STRING, &encoder_name,
          "Encoder to use", "ENC" },
        { "use-hw-encoder", 0, 0, G_OPTION_ARG_NONE, &use_hw_encoder,
          "Use H/W encoders if possible", NULL },
        { "clipping", 0, 0, G_OPTION_ARG_STRING, &clipping,
          "Clipping region (x1,y1)-(x2,y2) or (x,y)x(w,h). "
          "You can specify coordinates using pixel or percentage.", "STRING" },
        { "starting-bitrate", 0, 0, G_OPTION_ARG_INT64, &starting_bit_rate,
          "Initial bitrate", "BITRATE" },
        { "min-psnr", 0, 0, G_OPTION_ARG_DOUBLE, &minimum_psnr,
          "Minimum PSNR accepted", "PSNR" },
        { "split-lines", 0, 0, G_OPTION_ARG_INT, &image_split_lines,
          "Split image into different chunks every LINES lines", "LINES" },
        { "report", 0, 0, G_OPTION_ARG_FILENAME, &file_report_name,
          "Report statistics to file", "FILENAME" },
        { NULL }
    };

    GOptionContext *context = NULL;
    GError *error = NULL;
    context = g_option_context_new("- helper for testing VideoEncoder");
    g_option_context_set_description(context, program_description);
    g_option_context_add_main_entries(context, entries, NULL);
    if (!g_option_context_parse(context, &argc, &argv, &error)) {
        g_printerr("Option parsing failed: %s\n", error->message);
        exit(1);
    }

    if (!input_pipeline_desc) {
        g_printerr("Input pipeline option missing\n");
        exit(1);
    }

    const EncoderInfo *encoder = encoder_info_mjpeg;
    if (encoder_name != NULL) {
        encoder = get_encoder_info(encoder_name);
        if (!encoder) {
            g_printerr("Encoder name unsupported: %s\n", encoder_name);
            exit(1);
        }
    }

    if (image_format != NULL) {
        bitmap_format = get_bitmap_format(image_format);
        if (bitmap_format == SPICE_BITMAP_FMT_INVALID) {
            g_printerr("Invalid image format: %s\n", image_format);
            exit(1);
        }
    }

    parse_clipping(clipping ? clipping : "(0,0)x(100%,100%)");

    if (minimum_psnr < 0) {
        g_printerr("Invalid PSNR specified %f\n", minimum_psnr);
        exit(1);
    }

    if (image_split_lines < 1) {
        g_printerr("Invalid --split-lines option: %d\n", image_split_lines);
        exit(1);
    }

    if (file_report_name) {
        file_report = fopen(file_report_name, "w");
        if (!file_report) {
            g_printerr("Error opening file %s for report\n", file_report_name);
            exit(1);
        }
    }

    gst_init(&argc, &argv);

    // TODO give particular error if pipeline fails to be created

    create_output_pipeline(encoder, output_frames, NULL);

    create_video_encoder(encoder);

    create_input_pipeline(input_pipeline_desc, input_frames, NULL);

    // run all input streaming
    pipeline_wait_eos(input_pipeline);

    video_encoder->destroy(video_encoder);

    // send EOS to output and wait
    // this assure we processed all frames sent from input pipeline
    if (gst_app_src_end_of_stream(output_pipeline->appsrc) != GST_FLOW_OK) {
        g_printerr("gst_app_src_end_of_stream failed\n");
        exit(1);
    }
    pipeline_wait_eos(output_pipeline);

    // check queue is now empty
    pthread_mutex_lock(&frame_queue_mtx);
    TestFrame *frame = g_queue_pop_head(&frame_queue);
    pthread_mutex_unlock(&frame_queue_mtx);
    if (frame) {
        g_printerr("Queue not empty at the end\n");
        exit(1);
    }

    pipeline_free(input_pipeline);
    pipeline_free(output_pipeline);

    g_free(encoder_name);
    g_free(image_format);
    g_free(input_pipeline_desc);
    g_free(clipping);
    g_option_context_free(context);

    return 0;
}

static void
parse_clipping(const char *clipping)
{
    spice_assert(clipping);

#define NUM_FMT "%31[^,)]"
#define NUM(n) coords[n]
    char coords[4][32];
    char clipping_type_sign[2];
    int i;

    if (sscanf(clipping, "(" NUM_FMT "," NUM_FMT ")%1[x-](" NUM_FMT "," NUM_FMT ")",
               NUM(0), NUM(1), clipping_type_sign, NUM(2), NUM(3)) < 5) {
        goto format_error;
    }
    for (i = 0; i < 4; ++i) {
        char *end = NULL;
        errno = 0;
        clipping_coords[i].unit = COORDS_NUMBER;
        clipping_coords[i].value = strtoul(coords[i], &end, 10);
        if (errno || !end || (strcmp(end, "") != 0 && strcmp(end, "%") != 0)) {
            goto format_error;
        }
        if (strcmp(end, "%") == 0) {
            clipping_coords[i].unit = COORDS_PERCENT;
            if (clipping_coords[i].value > 100) {
                goto format_error;
            }
        }
    }
    if (clipping_type_sign[0] == 'x') {
        clipping_type = COORDS_SIZE;
    } else {
        clipping_type = COORDS_BOX;
    }
    return;

format_error:
    g_printerr("Invalid clipping format: %s\n", clipping);
    exit(1);

}

static void
compute_clipping_rect(GstSample *sample)
{
    GstCaps *caps = gst_sample_get_caps(sample);
    spice_assert(caps);

    GstStructure *s = gst_caps_get_structure(caps, 0);
    spice_assert(s);

    gint width, height;
    spice_assert(gst_structure_get_int(s, "width", &width) &&
                 gst_structure_get_int(s, "height", &height));

    // transform from percent to pixel values
    int i;
    unsigned int coords[4];
    for (i = 0; i < 4; ++i) {
        unsigned int coord = coords[i] = clipping_coords[i].value;
        if (clipping_coords[i].unit != COORDS_PERCENT) {
            spice_assert(clipping_coords[i].unit == COORDS_NUMBER);
            continue;
        }
        coords[i] = coord * ((i&1) ? height : width) / 100;
    }

    // transform from sized to box
    if (clipping_type == COORDS_SIZE) {
        coords[2] += coords[0];
        coords[3] += coords[1];
    }

    // clip to sample
    coords[0] = MIN(coords[0], width);
    coords[1] = MIN(coords[1], height);
    coords[2] = MIN(coords[2], width);
    coords[3] = MIN(coords[3], height);

    // check coordinated are valid
    spice_assert(coords[0] < coords[2]);
    spice_assert(coords[1] < coords[3]);

    // set
    clipping_rect.left = coords[0];
    clipping_rect.top = coords[1];
    clipping_rect.right = coords[2];
    clipping_rect.bottom = coords[3];
    clipping_type_computed = TRUE;
}

static const EncoderInfo *
get_encoder_info(const char *encoder_name)
{
    const EncoderInfo *info;
    for (info = encoder_infos; info->name; ++info) {
        if (strcmp(info->name, encoder_name) == 0) {
            return info;
        }
    }
    return NULL;
}

static GstFlowReturn
new_sample(GstAppSink *gstappsink, gpointer test_pipeline)
{
    TestPipeline *pipeline = test_pipeline;

    GstSample *sample = gst_app_sink_pull_sample(pipeline->appsink);
    if (sample) {
        pipeline->sample_proc(sample, pipeline->sample_param);
        gst_sample_unref(sample);
    }
    return GST_FLOW_OK;
}

static GstBusSyncReply
handle_pipeline_message(GstBus *bus, GstMessage *msg, gpointer test_pipeline)
{
    TestPipeline *pipeline = (TestPipeline *) test_pipeline;

    if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_EOS) {
        pthread_mutex_lock(&eos_mtx);
        pipeline->got_eos = TRUE;
        pthread_cond_signal(&eos_cond);
        pthread_mutex_unlock(&eos_mtx);
    }
    return GST_BUS_PASS;
}

static TestPipeline*
create_pipeline(const char *desc, SampleProc sample_proc, void *param)
{
    TestPipeline *pipeline = spice_new0(TestPipeline, 1);

    pipeline->sample_proc = sample_proc;
    pipeline->sample_param = param;

    GError *err = NULL;
    pipeline->gst_pipeline = gst_parse_launch_full(desc, NULL, GST_PARSE_FLAG_FATAL_ERRORS, &err);
    if (!pipeline->gst_pipeline) {
        g_printerr("GStreamer error: %s\n", err->message);
        return NULL;
    }

    pipeline->appsrc = GST_APP_SRC(gst_bin_get_by_name(GST_BIN(pipeline->gst_pipeline), "src"));
    pipeline->appsink = GST_APP_SINK(gst_bin_get_by_name(GST_BIN(pipeline->gst_pipeline), "sink"));
    if (!pipeline->appsink) {
        g_printerr("Appsync not found in pipeline: %s\n", desc);
        return NULL;
    }

    static const GstAppSinkCallbacks appsink_cbs_template =
        { NULL, NULL, new_sample, ._gst_reserved={NULL} };
    GstAppSinkCallbacks appsink_cbs = appsink_cbs_template;
    gst_app_sink_set_callbacks(pipeline->appsink, &appsink_cbs, pipeline, NULL);

    GstBus *bus = gst_element_get_bus(pipeline->gst_pipeline);
    gst_bus_set_sync_handler(bus, handle_pipeline_message, pipeline, NULL);
    gst_object_unref(bus);

    if (gst_element_set_state(pipeline->gst_pipeline, GST_STATE_PLAYING) ==
        GST_STATE_CHANGE_FAILURE) {
        g_printerr("GStreamer error: Unable to set the pipeline to the playing state.\n");
        exit(1);
    }

    return pipeline;
}

static void
pipeline_free(TestPipeline *pipeline)
{
    if (pipeline->appsrc) {
        gst_object_unref(pipeline->appsrc);
    }
    gst_object_unref(pipeline->appsink);
    gst_object_unref(pipeline->gst_pipeline);
    free(pipeline);
}

static void
create_output_pipeline(const EncoderInfo *encoder, SampleProc sample_proc, void *param)
{
    gchar *desc =
        g_strdup_printf("appsrc name=src is-live=true format=time max-bytes=0 block=true "
                        "%s ! %s ! " VIDEOCONVERT " ! appsink name=sink " BGRx_CAPS
                        " sync=false drop=false", encoder->caps, encoder->decoder);

    TestPipeline *pipeline = create_pipeline(desc, sample_proc, param);
    if (!pipeline) {
        g_printerr("Error creating output pipeline: %s\n", desc);
        exit(1);
    }
    g_free(desc);

    output_pipeline = pipeline;
}

static void
create_input_pipeline(const char *input_pipeline_desc, SampleProc sample_proc, void *param)
{
    gchar *desc =
        g_strdup_printf("%s ! appsink name=sink " BGRx_CAPS
                        " sync=false drop=false", input_pipeline_desc);

    TestPipeline *pipeline = create_pipeline(desc, sample_proc, param);
    g_free(desc);
    if (!pipeline) {
        // TODO specific error
        g_printerr("Error creating input pipeline\n");
        exit(1);
    }

    input_pipeline = pipeline;
}

static void
video_buffer_release(VideoBuffer *video_buffer)
{
    video_buffer->free(video_buffer);
}

static void
pipeline_send_raw_data(TestPipeline *pipeline, VideoBuffer *video_buffer)
{
    GstBuffer *buffer =
        gst_buffer_new_wrapped_full(GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS,
                                    video_buffer->data, video_buffer->size,
                                    0, video_buffer->size,
                                    video_buffer, (void (*)(void*)) video_buffer_release);

    GST_BUFFER_DURATION(buffer) = GST_CLOCK_TIME_NONE;
#ifndef HAVE_GSTREAMER_0_10
    GST_BUFFER_DTS(buffer) = GST_CLOCK_TIME_NONE;
#endif

    if (gst_app_src_push_buffer(pipeline->appsrc, buffer) != GST_FLOW_OK) {
        g_printerr("GStreamer error: unable to push frame of size %u\n", video_buffer->size);
        exit(1);
    }
}

static void
pipeline_wait_eos(TestPipeline *pipeline)
{
    pthread_mutex_lock(&eos_mtx);
    while (!pipeline->got_eos) {
        pthread_cond_wait(&eos_cond, &eos_mtx);
    }
    pthread_mutex_unlock(&eos_mtx);
}

static uint32_t
mock_get_roundtrip_ms(void *opaque)
{
    // TODO
    return 20;
}

static uint32_t
mock_get_source_fps(void *opaque)
{
    // TODO
    return 10;
}

static void
mock_update_client_playback_delay(void *opaque, uint32_t delay_ms)
{
    // TODO
}

static VideoEncoderRateControlCbs rate_control_cbs = {
    .get_roundtrip_ms = mock_get_roundtrip_ms,
    .get_source_fps = mock_get_source_fps,
    .update_client_playback_delay = mock_update_client_playback_delay,
};

static void
create_video_encoder(const EncoderInfo *encoder)
{
    spice_assert(encoder);

    video_encoder = encoder->new_encoder(encoder->coded_type, starting_bit_rate, &rate_control_cbs,
                                         (bitmap_ref_t) frame_ref, (bitmap_unref_t) frame_unref);
    // TODO return not supported error
    spice_assert(video_encoder);
}

static void
frame_ref(TestFrame *frame)
{
    g_atomic_int_inc(&frame->refs);
}

static void
frame_unref(TestFrame *frame)
{
    if (!g_atomic_int_dec_and_test(&frame->refs)) {
        return;
    }
    bitmap_free(frame->bitmap);
    free(frame);
}

static void
bitmap_free(SpiceBitmap *bitmap)
{
    if (!bitmap) {
        return;
    }
    spice_assert(!bitmap->palette);
    spice_assert(bitmap->data);
    spice_chunks_destroy(bitmap->data);
    free(bitmap);
}

static SpiceChunks* chunks_alloc(uint32_t stride, uint32_t height, uint32_t split);
static uint8_t *bitmap_get_line(SpiceBitmap *bitmap, int y);
static uint32_t compute_stride(int width, SpiceBitmapFmt format);
typedef void convert_line_t(uint8_t *dest, const uint8_t *src, uint32_t width);
static convert_line_t convert_line16;
static convert_line_t convert_line24;
static convert_line_t convert_line32;
static convert_line_t *get_convert_line(SpiceBitmapFmt format);

static SpiceBitmap *
gst_to_spice_bitmap(GstSample *sample)
{
    GstCaps *caps = gst_sample_get_caps(sample);
    spice_assert(caps);

    GstStructure *s = gst_caps_get_structure(caps, 0);
    spice_assert(s);

    gint width, height;
    spice_assert(gst_structure_get_int(s, "width", &width) &&
                 gst_structure_get_int(s, "height", &height));

    SpiceBitmap *bitmap = spice_new0(SpiceBitmap, 1);
    bitmap->format = bitmap_format;
    bitmap->flags = top_down ? SPICE_BITMAP_FLAGS_TOP_DOWN : 0;
    bitmap->x = width;
    bitmap->y = height;
    bitmap->stride = compute_stride(width, bitmap->format);
    bitmap->data = chunks_alloc(bitmap->stride, height, image_split_lines);

    GstBuffer *buffer = gst_sample_get_buffer(sample);
    GstMapInfo mapinfo;
    if (!gst_buffer_map(buffer, &mapinfo, GST_MAP_READ)) {
        spice_error("GStreamer error: could not map the buffer");
    }

    // convert image
    gint y;
    convert_line_t *convert_line = get_convert_line(bitmap->format);
    for (y = 0; y < height; ++y) {
        convert_line(bitmap_get_line(bitmap, y),
                     mapinfo.data + y * width * 4,
                     width);
    }
    gst_buffer_unmap(buffer, &mapinfo);
    // TODO should we unref buffer ??

    return bitmap;
}

static uint32_t
compute_stride(int width, SpiceBitmapFmt format)
{
    spice_assert(width > 0);

    switch (format) {
    case SPICE_BITMAP_FMT_16BIT:
        return width * 2;
    case SPICE_BITMAP_FMT_24BIT:
        return width * 3;
    case SPICE_BITMAP_FMT_32BIT:
    case SPICE_BITMAP_FMT_RGBA:
        return width * 4;
    default:
        break;
    }
    spice_assert(0);
    return 0;
}

static SpiceChunks*
chunks_alloc(uint32_t stride, uint32_t height, uint32_t split)
{
    spice_assert(stride && height && split);
    const uint32_t num_chunks = (height + split - 1u) / split;
    SpiceChunks *chunks = spice_malloc0(sizeof(SpiceChunks) + sizeof(SpiceChunk) * num_chunks);

    chunks->data_size = stride * height;
    chunks->num_chunks = num_chunks;
    chunks->flags = SPICE_CHUNKS_FLAGS_FREE;
    unsigned n;
    uint32_t allocated = 0;
    for (n = 0; n < num_chunks; ++n) {
        SpiceChunk *chunk = &chunks->chunk[n];
        uint32_t len = stride * split;
        spice_assert(chunks->data_size > allocated);
        len = MIN(len, chunks->data_size - allocated);
        chunk->data = spice_malloc0(len);
        chunk->len = len;
        allocated += len;
    }
    spice_assert(chunks->data_size == allocated);
    return chunks;
}

static uint8_t *
bitmap_get_line(SpiceBitmap *bitmap, int y)
{
    spice_assert(bitmap && y >= 0 && y < bitmap->y);
    if (!(bitmap->flags & SPICE_BITMAP_FLAGS_TOP_DOWN)) {
        y = bitmap->y - y - 1;
    }
    const uint32_t stride = bitmap->stride;
    uint32_t pos = stride * y;
    SpiceChunk *chunk = &bitmap->data->chunk[0];
    while (pos >= chunk->len) {
        pos -= chunk->len;
        ++chunk;
        spice_assert(chunk < &bitmap->data->chunk[bitmap->data->num_chunks]);
    }
    spice_assert(pos + stride <= chunk->len);
    return chunk->data + pos;
}

static convert_line_t *
get_convert_line(SpiceBitmapFmt format)
{
    switch (format) {
    case SPICE_BITMAP_FMT_16BIT:
        return convert_line16;
    case SPICE_BITMAP_FMT_24BIT:
        return convert_line24;
    case SPICE_BITMAP_FMT_32BIT:
    case SPICE_BITMAP_FMT_RGBA:
        return convert_line32;
    default:
        break;
    }
    spice_assert(0);
    return 0;
}

static void
convert_line16(uint8_t *dest, const uint8_t *src, uint32_t width)
{
    uint16_t *dest16 = (uint16_t *) dest;
    for (; width; --width) {
        *dest16++ = (src[0] >> 3) | ((src[1] & 0xf8) << 2) | ((src[2] & 0xf8) << 7);
        src += 4;
    }
}

static void
convert_line24(uint8_t *dest, const uint8_t *src, uint32_t width)
{
    for (; width; --width) {
        *dest++ = *src++;
        *dest++ = *src++;
        *dest++ = *src++;
        ++src;
    }
}

static void
convert_line32(uint8_t *dest, const uint8_t *src, uint32_t width)
{
    for (; width; --width) {
        *dest++ = *src++;
        *dest++ = *src++;
        *dest++ = *src++;
        *dest++ = 0;
        ++src;
    }
}

static SpiceBitmapFmt
get_bitmap_format(const char *format)
{
    if (strcmp(format, "32BIT") == 0) {
        return SPICE_BITMAP_FMT_32BIT;
    } else if (strcmp(format, "24BIT") == 0) {
        return SPICE_BITMAP_FMT_24BIT;
    } else if (strcmp(format, "16BIT") == 0) {
        return SPICE_BITMAP_FMT_16BIT;
    } else if (strcmp(format, "RGBA") == 0) {
        return SPICE_BITMAP_FMT_RGBA;
    }
    return SPICE_BITMAP_FMT_INVALID;
}

static TestFrame *
gst_to_spice_frame(GstSample *sample)
{
    TestFrame *frame = spice_new0(TestFrame, 1);
    frame->refs = 1;
    frame->bitmap = gst_to_spice_bitmap(sample);
    return frame;
}

static uint32_t
line_diff_rgb(const uint8_t *pixel1, const uint8_t *pixel2, uint32_t w)
{
    uint32_t diff_sum = 0;
    for (w *= 3; w; --w) {
        int diff = *pixel1 - *pixel2;
        diff_sum += diff * diff;
        ++pixel1;
        ++pixel2;
    }
    return diff_sum;
}

typedef uint8_t *bitmap_extract_rgb_line_t(SpiceBitmap *bitmap, uint8_t *buf,
                                           int32_t x, int32_t y, int32_t w);
static bitmap_extract_rgb_line_t *get_bitmap_extract(SpiceBitmapFmt format);
static bitmap_extract_rgb_line_t bitmap_extract16;
static bitmap_extract_rgb_line_t bitmap_extract24;
static bitmap_extract_rgb_line_t bitmap_extract32;

// compute PSNR
// see https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
// higher is better (less data loosed)
// typical are 30-50
static double
compute_psnr(SpiceBitmap *bitmap1, int32_t x1, int32_t y1,
             SpiceBitmap *bitmap2, int32_t x2, int32_t y2,
             int32_t w, int32_t h)
{
    spice_assert(x1 >= 0 && y1 >= 0);
    spice_assert(x2 >= 0 && y2 >= 0);
    spice_assert(w > 0 && h > 0);
    spice_assert(x1 + w <= bitmap1->x);
    spice_assert(y1 + h <= bitmap1->y);
    spice_assert(x2 + w <= bitmap2->x);
    spice_assert(y2 + h <= bitmap2->y);

    int y;
    uint64_t diff_sum = 0;
    uint8_t pixels[2][w*3];
    bitmap_extract_rgb_line_t *extract1 = get_bitmap_extract(bitmap1->format);
    bitmap_extract_rgb_line_t *extract2 = get_bitmap_extract(bitmap2->format);
    for (y = 0; y < h; ++y) {
        uint8_t *line1 = extract1(bitmap1, pixels[0], x1, y1 + y, w);
        uint8_t *line2 = extract2(bitmap2, pixels[1], x2, y2 + y, w);
        diff_sum += line_diff_rgb(line1, line2, w);
    }

    double mse = (double) diff_sum / (w*h*3);
    double psnr = 10 * log10(255*255/mse);

    return psnr;
}

static bitmap_extract_rgb_line_t *
get_bitmap_extract(SpiceBitmapFmt format)
{
    switch (format) {
    case SPICE_BITMAP_FMT_16BIT:
        return bitmap_extract16;
    case SPICE_BITMAP_FMT_24BIT:
        return bitmap_extract24;
    case SPICE_BITMAP_FMT_32BIT:
    case SPICE_BITMAP_FMT_RGBA:
        return bitmap_extract32;
    default:
        break;
    }
    spice_assert(0);
    return 0;
}

static uint8_t *
bitmap_extract24(SpiceBitmap *bitmap, uint8_t *buf, int32_t x, int32_t y, int32_t w)
{
    uint8_t *line = bitmap_get_line(bitmap, y) + x * 3;
    return line;
}

static uint8_t *
bitmap_extract32(SpiceBitmap *bitmap, uint8_t *buf, int32_t x, int32_t y, int32_t w)
{
    const uint8_t *line = bitmap_get_line(bitmap, y) + x * 4;
    uint8_t *dest = buf;
    for (; w; --w) {
        *dest++ = *line++;
        *dest++ = *line++;
        *dest++ = *line++;
        ++line;
    }
    return buf;
}

static uint8_t *
bitmap_extract16(SpiceBitmap *bitmap, uint8_t *buf, int32_t x, int32_t y, int32_t w)
{
    const uint16_t *line = (const uint16_t *)(bitmap_get_line(bitmap, y) + x * 2);
    uint8_t *dest = buf;
    for (; w; --w) {
        uint16_t pixel = *line++;
        uint8_t comp;
        comp = (pixel >> 0) & 0x1f;
        *dest++ = (comp << 3) | (comp >> 2);
        comp = (pixel >> 5) & 0x1f;
        *dest++ = (comp << 3) | (comp >> 2);
        comp = (pixel >> 10) & 0x1f;
        *dest++ = (comp << 3) | (comp >> 2);
    }
    return buf;
}