blob: 425ccff11fa2a25a7e95447064949606b179f753 [file] [log] [blame]
Kristian Høgsberg89060c02012-05-25 11:59:53 -04001/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#include <stdlib.h>
24#include <stdio.h>
25#include <stdint.h>
26#include <sys/mman.h>
27#include <sys/mman.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <unistd.h>
31#include <string.h>
32#include <fcntl.h>
33
34#include <cairo.h>
35
Kristian Høgsberg894e0b52012-05-25 17:55:20 -040036#include "wcap-decode.h"
Kristian Høgsberg89060c02012-05-25 11:59:53 -040037
38static void
39wcap_decoder_decode_rectangle(struct wcap_decoder *decoder,
40 struct wcap_rectangle *rect)
41{
42 uint32_t v, *p = decoder->p, *d;
43 int width = rect->x2 - rect->x1, height = rect->y2 - rect->y1;
44 int x, i, j, k, l, count = width * height;
Kristian Høgsberg053be422012-05-29 12:15:47 -040045 unsigned char r, g, b, dr, dg, db;
46
Kristian Høgsberg89060c02012-05-25 11:59:53 -040047 d = decoder->frame + (rect->y2 - 1) * decoder->width;
48 x = rect->x1;
49 i = 0;
50 while (i < count) {
51 v = *p++;
52 l = v >> 24;
53 if (l < 0xe0) {
54 j = l + 1;
55 } else {
56 j = 1 << (l - 0xe0 + 7);
57 }
58
Kristian Høgsberg053be422012-05-29 12:15:47 -040059 dr = (v >> 16);
60 dg = (v >> 8);
61 db = (v >> 0);
Kristian Høgsberg89060c02012-05-25 11:59:53 -040062 for (k = 0; k < j; k++) {
Kristian Høgsberg053be422012-05-29 12:15:47 -040063 r = (d[x] >> 16) + dr;
64 g = (d[x] >> 8) + dg;
65 b = (d[x] >> 0) + db;
66 d[x] = 0xff000000 | (r << 16) | (g << 8) | b;
Kristian Høgsberg89060c02012-05-25 11:59:53 -040067 x++;
68 if (x == rect->x2) {
69 x = rect->x1;
70 d -= decoder->width;
71 }
72 }
73 i += j;
74 }
75
76 if (i != count)
77 printf("rle encoding longer than expected (%d expected %d)\n",
78 i, count);
79
80 decoder->p = p;
81}
82
Kristian Høgsberg894e0b52012-05-25 17:55:20 -040083int
Kristian Høgsberg89060c02012-05-25 11:59:53 -040084wcap_decoder_get_frame(struct wcap_decoder *decoder)
85{
86 struct wcap_rectangle *rects;
87 struct wcap_frame_header *header;
88 uint32_t *s;
89 uint32_t i;
90 int width, height;
91
92 if (decoder->p == decoder->end)
93 return 0;
94
95 header = decoder->p;
Kristian Høgsberg11374d22012-05-25 22:33:35 -040096 decoder->msecs = header->msecs;
97 decoder->count++;
Kristian Høgsberg89060c02012-05-25 11:59:53 -040098
99 rects = (void *) (header + 1);
100 decoder->p = (uint32_t *) (rects + header->nrects);
101 for (i = 0; i < header->nrects; i++) {
102 width = rects[i].x2 - rects[i].x1;
103 height = rects[i].y2 - rects[i].y1;
104 wcap_decoder_decode_rectangle(decoder, &rects[i]);
105 }
106
107 return 1;
108}
109
110struct wcap_decoder *
111wcap_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 return NULL;
125
126 fstat(decoder->fd, &buf);
127 decoder->size = buf.st_size;
128 decoder->map = mmap(NULL, decoder->size,
129 PROT_READ, MAP_PRIVATE, decoder->fd, 0);
130
131 header = decoder->map;
Kristian Høgsberg2255eb02012-05-25 21:51:25 -0400132 decoder->format = header->format;
Kristian Høgsberg11374d22012-05-25 22:33:35 -0400133 decoder->count = 0;
Kristian Høgsberg89060c02012-05-25 11:59:53 -0400134 decoder->width = header->width;
135 decoder->height = header->height;
136 decoder->p = header + 1;
137 decoder->end = decoder->map + decoder->size;
138
139 frame_size = header->width * header->height * 4;
140 decoder->frame = malloc(frame_size);
141 memset(decoder->frame, 0, frame_size);
142
143 return decoder;
144}
145
146void
147wcap_decoder_destroy(struct wcap_decoder *decoder)
148{
149 munmap(decoder->map, decoder->size);
150 free(decoder->frame);
151 free(decoder);
152}