aboutsummaryrefslogtreecommitdiffhomepage
path: root/msvidc.c
blob: d02e9d593222e0807799b63fba4e53fd137f450c (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

#define LE_16(x) *(unsigned short *)(x)

#define DECODE_BGR555_TO_BGR888(x) \
        x.c1_b = (x.c1 >> 7) & 0xF8; \
        x.c1_g = (x.c1 >> 2) & 0xF8; \
        x.c1_r = (x.c1 << 3) & 0xF8; \
        x.c2_b = (x.c2 >> 7) & 0xF8; \
        x.c2_g = (x.c2 >> 2) & 0xF8; \
        x.c2_r = (x.c2 << 3) & 0xF8;

#define DECODE_PALETTE_TO_BGR888(x) \
        x.c1_b = palette_map[x.c1 * 4 + 2]; \
        x.c1_g = palette_map[x.c1 * 4 + 1]; \
        x.c1_r = palette_map[x.c1 * 4 + 0]; \
        x.c2_b = palette_map[x.c2 * 4 + 2]; \
        x.c2_g = palette_map[x.c2 * 4 + 1]; \
        x.c2_r = palette_map[x.c2 * 4 + 0];

struct
{
  unsigned short c1, c2;
  unsigned char c1_r, c1_g, c1_b;
  unsigned char c2_r, c2_g, c2_b;
} quad[2][2];

void AVI_Decode_Video1_16(
  char *encoded,
  int encoded_size,
  char *decoded,
  int width,
  int height,
  int bytes_per_pixel)
{
  int block_ptr, pixel_ptr;
  int pixel_x, pixel_y;  // pixel width and height iterators
  int block_x, block_y;  // block width and height iterators
  int blocks_wide, blocks_high;  // width and height in 4x4 blocks
  int block_inc;
  int row_dec;

  // decoding parameters
  int stream_ptr;
  unsigned char byte_a, byte_b;
  unsigned short flags;
  int skip_blocks;

  stream_ptr = 0;
  skip_blocks = 0;
  blocks_wide = width / 4;
  blocks_high = height / 4;
  block_inc = 4 * bytes_per_pixel;
  row_dec = (width + 4) * bytes_per_pixel;

  for (block_y = blocks_high; block_y > 0; block_y--)
  {
    block_ptr = ((block_y * 4) - 1) * (width * bytes_per_pixel);
    for (block_x = blocks_wide; block_x > 0; block_x--)
    {
      // check if this block should be skipped
      if (skip_blocks)
      {
        block_ptr += block_inc;
        skip_blocks--;
        continue;
      }

      pixel_ptr = block_ptr;

      // get the next two bytes in the encoded data stream
      byte_a = encoded[stream_ptr++];
      byte_b = encoded[stream_ptr++];

      // check if the decode is finished
      if ((byte_a == 0) && (byte_b == 0))
        return;

      // check if this is a skip code
      else if ((byte_b & 0xFC) == 0x84)
      {
        // but don't count the current block
        skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
      }

      // check if this is in the 2- or 8-color classes
      else if (byte_b < 0x80)
      {
        flags = (byte_b << 8) | byte_a;

        quad[0][0].c1 = LE_16(&encoded[stream_ptr]);
        stream_ptr += 2;
        quad[0][0].c2 = LE_16(&encoded[stream_ptr]);
        stream_ptr += 2;

        DECODE_BGR555_TO_BGR888(quad[0][0]);

        if (quad[0][0].c1 & 0x8000)
        {
          // 8-color encoding
          quad[1][0].c1 = LE_16(&encoded[stream_ptr]);
          stream_ptr += 2;
          quad[1][0].c2 = LE_16(&encoded[stream_ptr]);
          stream_ptr += 2;
          quad[0][1].c1 = LE_16(&encoded[stream_ptr]);
          stream_ptr += 2;
          quad[0][1].c2 = LE_16(&encoded[stream_ptr]);
          stream_ptr += 2;
          quad[1][1].c1 = LE_16(&encoded[stream_ptr]);
          stream_ptr += 2;
          quad[1][1].c2 = LE_16(&encoded[stream_ptr]);
          stream_ptr += 2;

          DECODE_BGR555_TO_BGR888(quad[0][1]);
          DECODE_BGR555_TO_BGR888(quad[1][0]);
          DECODE_BGR555_TO_BGR888(quad[1][1]);

          for (pixel_y = 0; pixel_y < 4; pixel_y++)
          {
            for (pixel_x = 0; pixel_x < 4; pixel_x++)
            {
              if (flags & 1)
              {
                decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_r;
                decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_g;
                decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_b;
              }
              else
              {
                decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_r;
                decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_g;
                decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_b;
              }

              // get the next flag ready to go
              flags >>= 1;
            }
            pixel_ptr -= row_dec;
          }
        }
        else
        {
          // 2-color encoding
          for (pixel_y = 0; pixel_y < 4; pixel_y++)
          {
            for (pixel_x = 0; pixel_x < 4; pixel_x++)
            {
              if (flags & 1)
              {
                decoded[pixel_ptr++] = quad[0][0].c1_r;
                decoded[pixel_ptr++] = quad[0][0].c1_g;
                decoded[pixel_ptr++] = quad[0][0].c1_b;
              }
              else
              {
                decoded[pixel_ptr++] = quad[0][0].c2_r;
                decoded[pixel_ptr++] = quad[0][0].c2_g;
                decoded[pixel_ptr++] = quad[0][0].c2_b;
              }

              // get the next flag ready to go
              flags >>= 1;
            }
            pixel_ptr -= row_dec;
          }
        }
      }

      // otherwise, it's a 1-color block
      else
      {
        quad[0][0].c1 = (byte_b << 8) | byte_a;
        DECODE_BGR555_TO_BGR888(quad[0][0]);

        for (pixel_y = 0; pixel_y < 4; pixel_y++)
        {
          for (pixel_x = 0; pixel_x < 4; pixel_x++)
          {
            decoded[pixel_ptr++] = quad[0][0].c1_r;
            decoded[pixel_ptr++] = quad[0][0].c1_g;
            decoded[pixel_ptr++] = quad[0][0].c1_b;
          }
          pixel_ptr -= row_dec;
        }
      }

      block_ptr += block_inc;
    }
  }
}

void AVI_Decode_Video1_8(
  char *encoded,
  int encoded_size,
  char *decoded,
  int width,
  int height,
  unsigned char *palette_map,
  int bytes_per_pixel)
{
  int block_ptr, pixel_ptr;
  int pixel_x, pixel_y;  // pixel width and height iterators
  int block_x, block_y;  // block width and height iterators
  int blocks_wide, blocks_high;  // width and height in 4x4 blocks
  int block_inc;
  int row_dec;

  // decoding parameters
  int stream_ptr;
  unsigned char byte_a, byte_b;
  unsigned short flags;
  int skip_blocks;

  stream_ptr = 0;
  skip_blocks = 0;
  blocks_wide = width / 4;
  blocks_high = height / 4;
  block_inc = 4 * bytes_per_pixel;
  row_dec = (width + 4) * bytes_per_pixel;

  for (block_y = blocks_high; block_y > 0; block_y--)
  {
    block_ptr = ((block_y * 4) - 1) * (width * bytes_per_pixel);
    for (block_x = blocks_wide; block_x > 0; block_x--)
    {
      // check if this block should be skipped
      if (skip_blocks)
      {
        block_ptr += block_inc;
        skip_blocks--;
        continue;
      }

      pixel_ptr = block_ptr;

      // get the next two bytes in the encoded data stream
      byte_a = encoded[stream_ptr++];
      byte_b = encoded[stream_ptr++];

      // check if the decode is finished
      if ((byte_a == 0) && (byte_b == 0))
        return;

      // check if this is a skip code
      else if ((byte_b & 0xFC) == 0x84)
      {
        // but don't count the current block
        skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
      }

      // check if this is a 2-color block
      else if (byte_b < 0x80)
      {
        flags = (byte_b << 8) | byte_a;

        quad[0][0].c1 = (unsigned char)encoded[stream_ptr++];
        quad[0][0].c2 = (unsigned char)encoded[stream_ptr++];

        DECODE_PALETTE_TO_BGR888(quad[0][0]);

        // 2-color encoding
        for (pixel_y = 0; pixel_y < 4; pixel_y++)
        {
          for (pixel_x = 0; pixel_x < 4; pixel_x++)
          {
            if (flags & 1)
            {
              decoded[pixel_ptr++] = quad[0][0].c1_r;
              decoded[pixel_ptr++] = quad[0][0].c1_g;
              decoded[pixel_ptr++] = quad[0][0].c1_b;
            }
            else
            {
              decoded[pixel_ptr++] = quad[0][0].c2_r;
              decoded[pixel_ptr++] = quad[0][0].c2_g;
              decoded[pixel_ptr++] = quad[0][0].c2_b;
            }

            // get the next flag ready to go
            flags >>= 1;
          }
          pixel_ptr -= row_dec;
        }
      }

      // check if it's an 8-color block
      else if (byte_b >= 0x90)
      {
        // 8-color encoding
        quad[0][0].c1 = (unsigned char)encoded[stream_ptr++];
        quad[0][0].c2 = (unsigned char)encoded[stream_ptr++];
        quad[1][0].c1 = (unsigned char)encoded[stream_ptr++];
        quad[1][0].c2 = (unsigned char)encoded[stream_ptr++];

        quad[0][1].c1 = (unsigned char)encoded[stream_ptr++];
        quad[0][1].c2 = (unsigned char)encoded[stream_ptr++];
        quad[1][1].c1 = (unsigned char)encoded[stream_ptr++];
        quad[1][1].c2 = (unsigned char)encoded[stream_ptr++];

        DECODE_PALETTE_TO_BGR888(quad[0][0]);
        DECODE_PALETTE_TO_BGR888(quad[0][1]);
        DECODE_PALETTE_TO_BGR888(quad[1][0]);
        DECODE_PALETTE_TO_BGR888(quad[1][1]);

        for (pixel_y = 0; pixel_y < 4; pixel_y++)
        {
          for (pixel_x = 0; pixel_x < 4; pixel_x++)
          {
            if (flags & 1)
            {
              decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_r;
              decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_g;
              decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c1_b;
            }
            else
            {
              decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_r;
              decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_g;
              decoded[pixel_ptr++] = quad[pixel_x >> 1][pixel_y >> 1].c2_b;
            }

            // get the next flag ready to go
            flags >>= 1;
          }
          pixel_ptr -= row_dec;
        }
      }

      // otherwise, it's a 1-color block
      else
      {
        // init c2 along with c1 just so c2 is a known value for macro
        quad[0][0].c1 = quad[0][0].c2 = byte_a;
        DECODE_PALETTE_TO_BGR888(quad[0][0]);

        for (pixel_y = 0; pixel_y < 4; pixel_y++)
        {
          for (pixel_x = 0; pixel_x < 4; pixel_x++)
          {
            decoded[pixel_ptr++] = quad[0][0].c1_r;
            decoded[pixel_ptr++] = quad[0][0].c1_g;
            decoded[pixel_ptr++] = quad[0][0].c1_b;
          }
          pixel_ptr -= row_dec;
        }
      }

      block_ptr += block_inc;
    }
  }
}