1 /*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include "config.h"
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <sys/mman.h>
32 #include <sys/mman.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <fcntl.h>
38
39 #include <cairo.h>
40
41 #include "wcap-decode.h"
42
43 static void
wcap_decoder_decode_rectangle(struct wcap_decoder * decoder,struct wcap_rectangle * rect)44 wcap_decoder_decode_rectangle(struct wcap_decoder *decoder,
45 struct wcap_rectangle *rect)
46 {
47 uint32_t v, *p = decoder->p, *d;
48 int width = rect->x2 - rect->x1, height = rect->y2 - rect->y1;
49 int x, i, j, k, l, count = width * height;
50 unsigned char r, g, b, dr, dg, db;
51
52 d = decoder->frame + (rect->y2 - 1) * decoder->width;
53 x = rect->x1;
54 i = 0;
55 while (i < count) {
56 v = *p++;
57 l = v >> 24;
58 if (l < 0xe0) {
59 j = l + 1;
60 } else {
61 j = 1 << (l - 0xe0 + 7);
62 }
63
64 dr = (v >> 16);
65 dg = (v >> 8);
66 db = (v >> 0);
67 for (k = 0; k < j; k++) {
68 r = (d[x] >> 16) + dr;
69 g = (d[x] >> 8) + dg;
70 b = (d[x] >> 0) + db;
71 d[x] = 0xff000000 | (r << 16) | (g << 8) | b;
72 x++;
73 if (x == rect->x2) {
74 x = rect->x1;
75 d -= decoder->width;
76 }
77 }
78 i += j;
79 }
80
81 if (i != count)
82 printf("rle encoding longer than expected (%d expected %d)\n",
83 i, count);
84
85 decoder->p = p;
86 }
87
88 int
wcap_decoder_get_frame(struct wcap_decoder * decoder)89 wcap_decoder_get_frame(struct wcap_decoder *decoder)
90 {
91 struct wcap_rectangle *rects;
92 struct wcap_frame_header *header;
93 uint32_t i;
94
95 if (decoder->p == decoder->end)
96 return 0;
97
98 header = decoder->p;
99 decoder->msecs = header->msecs;
100 decoder->count++;
101
102 rects = (void *) (header + 1);
103 decoder->p = (uint32_t *) (rects + header->nrects);
104 for (i = 0; i < header->nrects; i++)
105 wcap_decoder_decode_rectangle(decoder, &rects[i]);
106
107 return 1;
108 }
109
110 struct wcap_decoder *
wcap_decoder_create(const char * filename)111 wcap_decoder_create(const char *filename)
112 {
113 struct wcap_decoder *decoder;
114 struct wcap_header *header;
115 int frame_size;
116 struct stat buf;
117
118 decoder = malloc(sizeof *decoder);
119 if (decoder == NULL)
120 return NULL;
121
122 decoder->fd = open(filename, O_RDONLY);
123 if (decoder->fd == -1) {
124 free(decoder);
125 return NULL;
126 }
127
128 fstat(decoder->fd, &buf);
129 decoder->size = buf.st_size;
130 decoder->map = mmap(NULL, decoder->size,
131 PROT_READ, MAP_PRIVATE, decoder->fd, 0);
132 if (decoder->map == MAP_FAILED) {
133 fprintf(stderr, "mmap failed\n");
134 close(decoder->fd);
135 free(decoder);
136 return NULL;
137 }
138
139 header = decoder->map;
140 decoder->format = header->format;
141 decoder->count = 0;
142 decoder->width = header->width;
143 decoder->height = header->height;
144 decoder->p = header + 1;
145 decoder->end = decoder->map + decoder->size;
146
147 frame_size = header->width * header->height * 4;
148 decoder->frame = malloc(frame_size);
149 if (decoder->frame == NULL) {
150 close(decoder->fd);
151 free(decoder);
152 return NULL;
153 }
154 memset(decoder->frame, 0, frame_size);
155
156 return decoder;
157 }
158
159 void
wcap_decoder_destroy(struct wcap_decoder * decoder)160 wcap_decoder_destroy(struct wcap_decoder *decoder)
161 {
162 munmap(decoder->map, decoder->size);
163 close(decoder->fd);
164 free(decoder->frame);
165 free(decoder);
166 }
167