summaryrefslogtreecommitdiff
path: root/gst/mve/mvevideodec16.c
blob: e045a0b11e7ab068149ae30befd35090ef35a5a3 (plain)
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
/*
 * Interplay MVE Video Decoder (16 bit)
 * Copyright (C) 2003 the ffmpeg project, Mike Melanson
 *           (C) 2006 Jens Granseuer
 *
 * 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 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * For more information about the Interplay MVE format, visit:
 *   http://www.pcisys.net/~melanson/codecs/interplay-mve.txt
 */

#include "gstmvedemux.h"
#include <string.h>

#define PIXEL(s) GST_READ_UINT16_LE (s)

#define CHECK_STREAM(l, n) \
  do { \
    if (G_UNLIKELY (*(l) < (n))) { \
      GST_ERROR ("wanted to read %d bytes from stream, %d available", (n), *(l)); \
      return -1; \
    } \
    *(l) -= (n); \
  } while (0)

/* copy an 8x8 block from the stream to the frame buffer */
static int
ipvideo_copy_block (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned short *src, int offset)
{
  int i;
  int frame_offset;

  frame_offset = frame - (unsigned short *) s->back_buf1 + offset;

  if (G_UNLIKELY (frame_offset < 0)) {
    GST_ERROR ("frame offset < 0 (%d)", frame_offset);
    return -1;
  } else if (G_UNLIKELY (frame_offset > s->max_block_offset)) {
    GST_ERROR ("frame offset above limit (%d > %u)",
        frame_offset, s->max_block_offset);
    return -1;
  }

  for (i = 0; i < 8; ++i) {
    memcpy (frame, src, 16);
    frame += s->width;
    src += s->width;
  }

  return 0;
}

static int
ipvideo_decode_0x2 (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  unsigned char B;
  int x, y;
  int offset;

  /* copy block from 2 frames ago using a motion vector */
  CHECK_STREAM (len, 1);
  B = *(*data)++;

  if (B < 56) {
    x = 8 + (B % 7);
    y = B / 7;
  } else {
    x = -14 + ((B - 56) % 29);
    y = 8 + ((B - 56) / 29);
  }
  offset = y * s->width + x;

  return ipvideo_copy_block (s, frame, frame + offset, offset);
}

static int
ipvideo_decode_0x3 (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  unsigned char B;
  int x, y;
  int offset;

  /* copy 8x8 block from current frame from an up/left block */
  CHECK_STREAM (len, 1);
  B = *(*data)++;

  if (B < 56) {
    x = -(8 + (B % 7));
    y = -(B / 7);
  } else {
    x = -(-14 + ((B - 56) % 29));
    y = -(8 + ((B - 56) / 29));
  }
  offset = y * s->width + x;

  return ipvideo_copy_block (s, frame, frame + offset, offset);
}

static int
ipvideo_decode_0x4 (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  int x, y;
  unsigned char B;
  int offset;

  /* copy a block from the previous frame */
  CHECK_STREAM (len, 1);
  B = *(*data)++;
  x = -8 + (B & 0x0F);
  y = -8 + (B >> 4);
  offset = y * s->width + x;

  return ipvideo_copy_block (s, frame, frame +
      ((unsigned short *) s->back_buf2 - (unsigned short *) s->back_buf1) +
      offset, offset);
}

static int
ipvideo_decode_0x5 (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  signed char x, y;
  int offset;

  /* copy a block from the previous frame using an expanded range */
  CHECK_STREAM (len, 2);
  x = (signed char) *(*data)++;
  y = (signed char) *(*data)++;
  offset = y * s->width + x;

  return ipvideo_copy_block (s, frame, frame +
      ((unsigned short *) s->back_buf2 - (unsigned short *) s->back_buf1) +
      offset, offset);
}

static int
ipvideo_decode_0x7 (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  int x, y;
  unsigned short P0, P1;
  unsigned int flags;
  int bitmask;

  /* 2-color encoding */
  CHECK_STREAM (len, 4 + 2);
  P0 = PIXEL (*data);
  (*data) += 2;
  P1 = PIXEL (*data);
  (*data) += 2;

  if (!(P0 & 0x8000)) {

    /* need 8 more bytes from the stream */
    CHECK_STREAM (len, 8 - 2);

    for (y = 0; y < 8; ++y) {
      flags = *(*data)++;
      for (x = 0x01; x <= 0x80; x <<= 1) {
        if (flags & x)
          *frame++ = P1;
        else
          *frame++ = P0;
      }
      frame += s->width - 8;
    }

  } else {
    P0 &= ~0x8000;

    /* need 2 more bytes from the stream */

    flags = ((*data)[1] << 8) | (*data)[0];
    (*data) += 2;
    bitmask = 0x0001;
    for (y = 0; y < 8; y += 2) {
      for (x = 0; x < 8; x += 2, bitmask <<= 1) {
        if (flags & bitmask) {
          *(frame + x) = P1;
          *(frame + x + 1) = P1;
          *(frame + s->width + x) = P1;
          *(frame + s->width + x + 1) = P1;
        } else {
          *(frame + x) = P0;
          *(frame + x + 1) = P0;
          *(frame + s->width + x) = P0;
          *(frame + s->width + x + 1) = P0;
        }
      }
      frame += s->width * 2;
    }
  }

  return 0;
}

static int
ipvideo_decode_0x8 (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  int x, y;
  unsigned short P[8];
  unsigned char B[8];
  unsigned int flags = 0;
  unsigned int bitmask = 0;
  unsigned short P0 = 0, P1 = 0;
  int lower_half = 0;

  /* 2-color encoding for each 4x4 quadrant, or 2-color encoding on
   * either top and bottom or left and right halves */
  CHECK_STREAM (len, 6 + 10);

  P[0] = PIXEL (*data);
  (*data) += 2;
  P[1] = PIXEL (*data);
  (*data) += 2;
  B[0] = *(*data)++;
  B[1] = *(*data)++;

  if (!(P[0] & 0x8000)) {

    /* need 18 more bytes */
    CHECK_STREAM (len, 18 - 10);

    P[2] = PIXEL (*data);
    (*data) += 2;
    P[3] = PIXEL (*data);
    (*data) += 2;
    B[2] = *(*data)++;
    B[3] = *(*data)++;
    P[4] = PIXEL (*data);
    (*data) += 2;
    P[5] = PIXEL (*data);
    (*data) += 2;
    B[4] = *(*data)++;
    B[5] = *(*data)++;
    P[6] = PIXEL (*data);
    (*data) += 2;
    P[7] = PIXEL (*data);
    (*data) += 2;
    B[6] = *(*data)++;
    B[7] = *(*data)++;

    flags =
        ((B[0] & 0xF0) << 4) | ((B[4] & 0xF0) << 8) |
        ((B[0] & 0x0F)) | ((B[4] & 0x0F) << 4) |
        ((B[1] & 0xF0) << 20) | ((B[5] & 0xF0) << 24) |
        ((B[1] & 0x0F) << 16) | ((B[5] & 0x0F) << 20);
    bitmask = 0x00000001;
    lower_half = 0;             /* still on top half */

    for (y = 0; y < 8; ++y) {

      /* time to reload flags? */
      if (y == 4) {
        flags =
            ((B[2] & 0xF0) << 4) | ((B[6] & 0xF0) << 8) |
            ((B[2] & 0x0F)) | ((B[6] & 0x0F) << 4) |
            ((B[3] & 0xF0) << 20) | ((B[7] & 0xF0) << 24) |
            ((B[3] & 0x0F) << 16) | ((B[7] & 0x0F) << 20);
        bitmask = 0x00000001;
        lower_half = 2;
      }

      /* get the pixel values ready for this quadrant */
      P0 = P[lower_half + 0];
      P1 = P[lower_half + 1];

      for (x = 0; x < 8; ++x, bitmask <<= 1) {
        if (x == 4) {
          P0 = P[lower_half + 4];
          P1 = P[lower_half + 5];
        }

        if (flags & bitmask)
          *frame++ = P1;
        else
          *frame++ = P0;
      }
      frame += s->width - 8;
    }

  } else {
    P[0] &= ~0x8000;

    /* need 10 more bytes */
    B[2] = *(*data)++;
    B[3] = *(*data)++;
    P[2] = PIXEL (*data);
    (*data) += 2;
    P[3] = PIXEL (*data);
    (*data) += 2;
    B[4] = *(*data)++;
    B[5] = *(*data)++;
    B[6] = *(*data)++;
    B[7] = *(*data)++;

    if (!(P[2] & 0x8000)) {
      /* vertical split; left & right halves are 2-color encoded */

      flags =
          ((B[0] & 0xF0) << 4) | ((B[4] & 0xF0) << 8) |
          ((B[0] & 0x0F)) | ((B[4] & 0x0F) << 4) |
          ((B[1] & 0xF0) << 20) | ((B[5] & 0xF0) << 24) |
          ((B[1] & 0x0F) << 16) | ((B[5] & 0x0F) << 20);
      bitmask = 0x00000001;

      for (y = 0; y < 8; ++y) {

        /* time to reload flags? */
        if (y == 4) {
          flags =
              ((B[2] & 0xF0) << 4) | ((B[6] & 0xF0) << 8) |
              ((B[2] & 0x0F)) | ((B[6] & 0x0F) << 4) |
              ((B[3] & 0xF0) << 20) | ((B[7] & 0xF0) << 24) |
              ((B[3] & 0x0F) << 16) | ((B[7] & 0x0F) << 20);
          bitmask = 0x00000001;
        }

        /* get the pixel values ready for this half */
        P0 = P[0];
        P1 = P[1];

        for (x = 0; x < 8; ++x, bitmask <<= 1) {
          if (x == 4) {
            P0 = P[2];
            P1 = P[3];
          }

          if (flags & bitmask)
            *frame++ = P1;
          else
            *frame++ = P0;
        }
        frame += s->width - 8;
      }

    } else {
      /* horizontal split; top & bottom halves are 2-color encoded */

      P0 = P[0];
      P1 = P[1];

      for (y = 0; y < 8; ++y) {

        flags = B[y];
        if (y == 4) {
          P0 = P[2] & ~0x8000;
          P1 = P[3];
        }

        for (bitmask = 0x01; bitmask <= 0x80; bitmask <<= 1) {

          if (flags & bitmask)
            *frame++ = P1;
          else
            *frame++ = P0;
        }
        frame += s->width - 8;
      }
    }
  }

  return 0;
}

static int
ipvideo_decode_0x9 (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  int x, y;
  unsigned short P[4];
  unsigned char B[4];
  unsigned int flags = 0;
  int shifter = 0;
  unsigned short pix;

  /* 4-color encoding */
  CHECK_STREAM (len, 8 + 4);

  P[0] = PIXEL (*data);
  (*data) += 2;
  P[1] = PIXEL (*data);
  (*data) += 2;
  P[2] = PIXEL (*data);
  (*data) += 2;
  P[3] = PIXEL (*data);
  (*data) += 2;

  if (!(P[0] & 0x8000) && !(P[2] & 0x8000)) {

    /* 1 of 4 colors for each pixel, need 16 more bytes */
    CHECK_STREAM (len, 16 - 4);

    for (y = 0; y < 8; ++y) {
      /* get the next set of 8 2-bit flags */
      flags = ((*data)[1] << 8) | (*data)[0];
      (*data) += 2;
      for (x = 0, shifter = 0; x < 8; ++x, shifter += 2) {
        *frame++ = P[(flags >> shifter) & 0x03];
      }
      frame += s->width - 8;
    }

  } else if (!(P[0] & 0x8000) && (P[2] & 0x8000)) {
    P[2] &= ~0x8000;

    /* 1 of 4 colors for each 2x2 block, need 4 more bytes */

    B[0] = *(*data)++;
    B[1] = *(*data)++;
    B[2] = *(*data)++;
    B[3] = *(*data)++;
    flags = (B[3] << 24) | (B[2] << 16) | (B[1] << 8) | B[0];
    shifter = 0;

    for (y = 0; y < 8; y += 2) {
      for (x = 0; x < 8; x += 2, shifter += 2) {
        pix = P[(flags >> shifter) & 0x03];
        *(frame + x) = pix;
        *(frame + x + 1) = pix;
        *(frame + s->width + x) = pix;
        *(frame + s->width + x + 1) = pix;
      }
      frame += s->width * 2;
    }

  } else if ((P[0] & 0x8000) && !(P[2] & 0x8000)) {
    P[0] &= ~0x8000;

    /* 1 of 4 colors for each 2x1 block, need 8 more bytes */

    CHECK_STREAM (len, 8 - 4);
    for (y = 0; y < 8; ++y) {
      /* time to reload flags? */
      if ((y == 0) || (y == 4)) {
        B[0] = *(*data)++;
        B[1] = *(*data)++;
        B[2] = *(*data)++;
        B[3] = *(*data)++;
        flags = (B[3] << 24) | (B[2] << 16) | (B[1] << 8) | B[0];
        shifter = 0;
      }
      for (x = 0; x < 8; x += 2, shifter += 2) {
        pix = P[(flags >> shifter) & 0x03];
        *(frame + x) = pix;
        *(frame + x + 1) = pix;
      }
      frame += s->width;
    }

  } else {
    P[0] &= ~0x8000;
    P[2] &= ~0x8000;

    /* 1 of 4 colors for each 1x2 block, need 8 more bytes */
    CHECK_STREAM (len, 8 - 4);

    for (y = 0; y < 8; y += 2) {
      /* time to reload flags? */
      if ((y == 0) || (y == 4)) {
        B[0] = *(*data)++;
        B[1] = *(*data)++;
        B[2] = *(*data)++;
        B[3] = *(*data)++;
        flags = (B[3] << 24) | (B[2] << 16) | (B[1] << 8) | B[0];
        shifter = 0;
      }
      for (x = 0; x < 8; ++x, shifter += 2) {
        pix = P[(flags >> shifter) & 0x03];
        *(frame + x) = pix;
        *(frame + s->width + x) = pix;
      }
      frame += s->width * 2;
    }
  }

  return 0;
}

static int
ipvideo_decode_0xa (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  int x, y;
  unsigned short P[16];
  unsigned char B[16];
  int flags = 0;
  int shifter = 0;
  int index;
  int split;
  int lower_half;

  /* 4-color encoding for each 4x4 quadrant, or 4-color encoding on
   * either top and bottom or left and right halves */
  CHECK_STREAM (len, 8 + 24);

  P[0] = PIXEL (*data);
  (*data) += 2;
  P[1] = PIXEL (*data);
  (*data) += 2;
  P[2] = PIXEL (*data);
  (*data) += 2;
  P[3] = PIXEL (*data);
  (*data) += 2;

  if (!(P[0] & 0x8000)) {

    /* 4-color encoding for each quadrant; need 40 more bytes */
    CHECK_STREAM (len, 40 - 24);

    B[0] = *(*data)++;
    B[1] = *(*data)++;
    B[2] = *(*data)++;
    B[3] = *(*data)++;
    for (y = 4; y < 16; y += 4) {
      for (x = y; x < y + 4; ++x) {
        P[x] = PIXEL (*data);
        (*data) += 2;
      }
      for (x = y; x < y + 4; ++x)
        B[x] = *(*data)++;
    }

    for (y = 0; y < 8; ++y) {

      lower_half = (y >= 4) ? 4 : 0;
      flags = (B[y + 8] << 8) | B[y];

      for (x = 0, shifter = 0; x < 8; ++x, shifter += 2) {
        split = (x >= 4) ? 8 : 0;
        index = split + lower_half + ((flags >> shifter) & 0x03);
        *frame++ = P[index];
      }

      frame += s->width - 8;
    }

  } else {
    P[0] &= ~0x8000;

    /* 4-color encoding for either left and right or top and bottom
     * halves; need 24 more bytes */

    memcpy (&B[0], *data, 8);
    (*data) += 8;
    P[4] = PIXEL (*data);
    (*data) += 2;
    P[5] = PIXEL (*data);
    (*data) += 2;
    P[6] = PIXEL (*data);
    (*data) += 2;
    P[7] = PIXEL (*data);
    (*data) += 2;
    memcpy (&B[8], *data, 8);
    (*data) += 8;

    if (!(P[4] & 0x8000)) {

      /* block is divided into left and right halves */
      for (y = 0; y < 8; ++y) {

        flags = (B[y + 8] << 8) | B[y];
        split = 0;

        for (x = 0, shifter = 0; x < 8; ++x, shifter += 2) {
          if (x == 4)
            split = 4;
          *frame++ = P[split + ((flags >> shifter) & 0x03)];
        }

        frame += s->width - 8;
      }

    } else {
      P[4] &= ~0x8000;

      /* block is divided into top and bottom halves */
      split = 0;
      for (y = 0; y < 8; ++y) {

        flags = (B[y * 2 + 1] << 8) | B[y * 2];
        if (y == 4)
          split = 4;

        for (x = 0, shifter = 0; x < 8; ++x, shifter += 2)
          *frame++ = P[split + ((flags >> shifter) & 0x03)];

        frame += s->width - 8;
      }
    }
  }

  return 0;
}

static int
ipvideo_decode_0xb (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  int x, y;

  /* 64-color encoding (each pixel in block is a different color) */
  CHECK_STREAM (len, 128);

  for (y = 0; y < 8; ++y) {
    for (x = 0; x < 8; ++x) {
      *frame++ = PIXEL (*data);
      (*data) += 2;
    }
    frame += s->width - 8;
  }

  return 0;
}

static int
ipvideo_decode_0xc (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  int x, y;
  unsigned short pix;

  /* 16-color block encoding: each 2x2 block is a different color */
  CHECK_STREAM (len, 32);

  for (y = 0; y < 8; y += 2) {
    for (x = 0; x < 8; x += 2) {
      pix = PIXEL (*data);
      (*data) += 2;
      *(frame + x) = pix;
      *(frame + x + 1) = pix;
      *(frame + s->width + x) = pix;
      *(frame + s->width + x + 1) = pix;
    }
    frame += s->width * 2;
  }

  return 0;
}

static int
ipvideo_decode_0xd (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  int x, y;
  unsigned short P[4];
  unsigned char index = 0;

  /* 4-color block encoding: each 4x4 block is a different color */
  CHECK_STREAM (len, 8);

  P[0] = PIXEL (*data);
  (*data) += 2;
  P[1] = PIXEL (*data);
  (*data) += 2;
  P[2] = PIXEL (*data);
  (*data) += 2;
  P[3] = PIXEL (*data);
  (*data) += 2;

  for (y = 0; y < 8; ++y) {
    if (y < 4)
      index = 0;
    else
      index = 2;

    for (x = 0; x < 8; ++x) {
      if (x == 4)
        ++index;
      *frame++ = P[index];
    }
    frame += s->width - 8;
  }

  return 0;
}

static int
ipvideo_decode_0xe (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  int x, y;
  unsigned short pix;

  /* 1-color encoding: the whole block is 1 solid color */
  CHECK_STREAM (len, 2);

  pix = PIXEL (*data);
  (*data) += 2;

  for (y = 0; y < 8; ++y) {
    for (x = 0; x < 8; ++x) {
      *frame++ = pix;
    }
    frame += s->width - 8;
  }

  return 0;
}

static int
ipvideo_decode_0xf (const GstMveDemuxStream * s, unsigned short *frame,
    const unsigned char **data, unsigned short *len)
{
  int x, y;
  unsigned short P[2];

  /* dithered encoding */
  CHECK_STREAM (len, 4);

  P[0] = PIXEL (*data);
  (*data) += 2;
  P[1] = PIXEL (*data);
  (*data) += 2;

  for (y = 0; y < 8; ++y) {
    for (x = 0; x < 4; ++x) {
      *frame++ = P[y & 1];
      *frame++ = P[(y & 1) ^ 1];
    }
    frame += s->width - 8;
  }

  return 0;
}

int
ipvideo_decode_frame16 (const GstMveDemuxStream * s, const unsigned char *data,
    unsigned short len)
{
  int rc = 0;
  int x, y, xx, yy;
  int index = 0;
  unsigned short offset;
  unsigned char opcode;
  unsigned short *frame;
  const unsigned char *data2;
  unsigned short len2;

  CHECK_STREAM (&len, 2);

  offset = (data[1] << 8) | data[0];
  data2 = data + offset;
  len2 = len - offset + 2;
  data += 2;

  frame = (unsigned short *) s->back_buf1;

  /* decoding is done in 8x8 blocks */
  xx = s->width >> 3;
  yy = s->height >> 3;

  for (y = 0; y < yy; ++y) {
    for (x = 0; x < xx; ++x) {
      /* decoding map contains 4 bits of information per 8x8 block */
      /* bottom nibble first, then top nibble */
      if (index & 1)
        opcode = s->code_map[index >> 1] >> 4;
      else
        opcode = s->code_map[index >> 1] & 0x0F;
      ++index;

      /* GST_DEBUG ("block @ (%3d, %3d): encoding 0x%X, data ptr @ %p",
         x, y, opcode, data); */

      switch (opcode) {
        case 0x0:
          /* copy a block from the previous frame */
          rc = ipvideo_copy_block (s, frame, frame +
              ((unsigned short *) s->back_buf2 -
                  (unsigned short *) s->back_buf1), 0);
          break;
        case 0x1:
          /* copy block from 2 frames ago; since we switched the back
           * buffers we don't actually have to do anything here */
          break;
        case 0x2:
          rc = ipvideo_decode_0x2 (s, frame, &data2, &len2);
          break;
        case 0x3:
          rc = ipvideo_decode_0x3 (s, frame, &data2, &len2);
          break;
        case 0x4:
          rc = ipvideo_decode_0x4 (s, frame, &data2, &len2);
          break;
        case 0x5:
          rc = ipvideo_decode_0x5 (s, frame, &data, &len);
          break;
        case 0x6:
          /* mystery opcode? skip multiple blocks? */
          GST_WARNING ("encountered unsupported opcode 0x6");
          rc = -1;
          break;
        case 0x7:
          rc = ipvideo_decode_0x7 (s, frame, &data, &len);
          break;
        case 0x8:
          rc = ipvideo_decode_0x8 (s, frame, &data, &len);
          break;
        case 0x9:
          rc = ipvideo_decode_0x9 (s, frame, &data, &len);
          break;
        case 0xa:
          rc = ipvideo_decode_0xa (s, frame, &data, &len);
          break;
        case 0xb:
          rc = ipvideo_decode_0xb (s, frame, &data, &len);
          break;
        case 0xc:
          rc = ipvideo_decode_0xc (s, frame, &data, &len);
          break;
        case 0xd:
          rc = ipvideo_decode_0xd (s, frame, &data, &len);
          break;
        case 0xe:
          rc = ipvideo_decode_0xe (s, frame, &data, &len);
          break;
        case 0xf:
          rc = ipvideo_decode_0xf (s, frame, &data, &len);
          break;
      }

      if (rc != 0)
        return rc;

      frame += 8;
    }
    frame += 7 * s->width;
  }

  return 0;
}