annotate cyuv.c @ 5520:25c43bd75e4c

small bugfixes...
author pontscho
date Sun, 07 Apr 2002 18:00:41 +0000
parents eea0213d64c8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3969
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
1 /* ------------------------------------------------------------------------
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
2 * Creative YUV Video Decoder
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
3 *
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
4 * Dr. Tim Ferguson, 2001.
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
5 * For more details on the algorithm:
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
6 * http://www.csse.monash.edu.au/~timf/videocodec.html
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
7 *
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
8 * This is a very simple predictive coder. A video frame is coded in YUV411
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
9 * format. The first pixel of each scanline is coded using the upper four
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
10 * bits of its absolute value. Subsequent pixels for the scanline are coded
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
11 * using the difference between the last pixel and the current pixel (DPCM).
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
12 * The DPCM values are coded using a 16 entry table found at the start of the
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
13 * frame. Thus four bits per component are used and are as follows:
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
14 * UY VY YY UY VY YY UY VY...
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
15 * This code assumes the frame width will be a multiple of four pixels. This
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
16 * should probably be fixed.
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
17 * ------------------------------------------------------------------------ */
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
18 #include <stdio.h>
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
19 #include <stdlib.h>
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
20 #include <string.h>
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
21 #include <sys/types.h>
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
22 #include <unistd.h>
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
23
5351
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
24 #include "loader/wine/avifmt.h" // for mmioFOURCC macro
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
25
3969
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
26 /* ------------------------------------------------------------------------
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
27 * This function decodes a buffer containing a CYUV encoded frame.
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
28 *
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
29 * buf - the input buffer to be decoded
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
30 * size - the size of the input buffer
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
31 * frame - the output frame buffer (UYVY format)
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
32 * width - the width of the output frame
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
33 * height - the height of the output frame
5351
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
34 * format - the requested output format
3969
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
35 */
5351
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
36 void decode_cyuv(unsigned char *buf, int size, unsigned char *frame, int width, int height, int format)
3969
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
37 {
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
38 int i, xpos, ypos, cur_Y = 0, cur_U = 0, cur_V = 0;
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
39 char *delta_y_tbl, *delta_c_tbl, *ptr;
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
40
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
41 delta_y_tbl = buf + 16;
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
42 delta_c_tbl = buf + 32;
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
43 ptr = buf + (16 * 3);
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
44
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
45 for(ypos = 0; ypos < height; ypos++)
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
46 for(xpos = 0; xpos < width; xpos += 4)
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
47 {
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
48 if(xpos == 0) /* first pixels in scanline */
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
49 {
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
50 cur_U = *(ptr++);
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
51 cur_Y = (cur_U & 0x0f) << 4;
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
52 cur_U = cur_U & 0xf0;
5351
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
53 if (format == mmioFOURCC('Y','U','Y','2'))
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
54 {
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
55 *frame++ = cur_Y;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
56 *frame++ = cur_U;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
57 }
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
58 else
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
59 {
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
60 *frame++ = cur_U;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
61 *frame++ = cur_Y;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
62 }
3969
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
63
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
64 cur_V = *(ptr++);
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
65 cur_Y = (cur_Y + delta_y_tbl[cur_V & 0x0f]) & 0xff;
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
66 cur_V = cur_V & 0xf0;
5351
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
67 if (format == mmioFOURCC('Y','U','Y','2'))
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
68 {
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
69 *frame++ = cur_Y;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
70 *frame++ = cur_V;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
71 }
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
72 else
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
73 {
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
74 *frame++ = cur_V;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
75 *frame++ = cur_Y;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
76 }
3969
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
77 }
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
78 else /* subsequent pixels in scanline */
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
79 {
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
80 i = *(ptr++);
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
81 cur_U = (cur_U + delta_c_tbl[i >> 4]) & 0xff;
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
82 cur_Y = (cur_Y + delta_y_tbl[i & 0x0f]) & 0xff;
5351
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
83 if (format == mmioFOURCC('Y','U','Y','2'))
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
84 {
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
85 *frame++ = cur_Y;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
86 *frame++ = cur_U;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
87 }
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
88 else
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
89 {
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
90 *frame++ = cur_U;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
91 *frame++ = cur_Y;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
92 }
3969
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
93
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
94 i = *(ptr++);
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
95 cur_V = (cur_V + delta_c_tbl[i >> 4]) & 0xff;
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
96 cur_Y = (cur_Y + delta_y_tbl[i & 0x0f]) & 0xff;
5351
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
97 if (format == mmioFOURCC('Y','U','Y','2'))
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
98 {
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
99 *frame++ = cur_Y;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
100 *frame++ = cur_V;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
101 }
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
102 else
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
103 {
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
104 *frame++ = cur_V;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
105 *frame++ = cur_Y;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
106 }
3969
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
107 }
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
108
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
109 i = *(ptr++);
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
110 cur_Y = (cur_Y + delta_y_tbl[i & 0x0f]) & 0xff;
5351
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
111 if (format == mmioFOURCC('Y','U','Y','2'))
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
112 {
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
113 *frame++ = cur_Y;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
114 *frame++ = cur_U;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
115 }
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
116 else
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
117 {
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
118 *frame++ = cur_U;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
119 *frame++ = cur_Y;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
120 }
3969
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
121
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
122 cur_Y = (cur_Y + delta_y_tbl[i >> 4]) & 0xff;
5351
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
123 if (format == mmioFOURCC('Y','U','Y','2'))
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
124 {
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
125 *frame++ = cur_Y;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
126 *frame++ = cur_V;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
127 }
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
128 else
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
129 {
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
130 *frame++ = cur_V;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
131 *frame++ = cur_Y;
eea0213d64c8 added YUY2 output to the widely used (heh) CYUV decoder because it seemed
melanson
parents: 3969
diff changeset
132 }
3969
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
133 }
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
134 }
c4c3f32dae47 integrated Tim Ferguson's native CYUV decoder
melanson
parents:
diff changeset
135