desktop-shell: properly set background widget as opaque
[profile/ivi/weston-ivi-shell.git] / wcap / main.c
1 /*
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 <config.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <sys/mman.h>
29 #include <sys/mman.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <assert.h>
36
37 #include <cairo.h>
38
39 #include "wcap-decode.h"
40
41 static void
42 write_png(struct wcap_decoder *decoder, const char *filename)
43 {
44         cairo_surface_t *surface;
45
46         surface = cairo_image_surface_create_for_data((unsigned char *) decoder->frame,
47                                                       CAIRO_FORMAT_ARGB32,
48                                                       decoder->width,
49                                                       decoder->height,
50                                                       decoder->width * 4);
51         cairo_surface_write_to_png(surface, filename);
52         cairo_surface_destroy(surface);
53 }
54
55 static inline int
56 rgb_to_yuv(uint32_t format, uint32_t p, int *u, int *v)
57 {
58         int r, g, b, y;
59
60         switch (format) {
61         case WCAP_FORMAT_XRGB8888:
62                 r = (p >> 16) & 0xff;
63                 g = (p >> 8) & 0xff;
64                 b = (p >> 0) & 0xff;
65                 break;
66         case WCAP_FORMAT_XBGR8888:
67                 r = (p >> 0) & 0xff;
68                 g = (p >> 8) & 0xff;
69                 b = (p >> 16) & 0xff;
70                 break;
71         default:
72                 assert(0);
73         }
74
75         y = (19595 * r + 38469 * g + 7472 * b) >> 16;
76         if (y > 255)
77                 y = 255;
78
79         *u += 46727 * (r - y);
80         *v += 36962 * (b - y);
81
82         return y;
83 }
84
85 static inline
86 int clamp_uv(int u)
87 {
88         int clamp = (u >> 18) + 128;
89
90         if (clamp < 0)
91                 return 0;
92         else if (clamp > 255)
93                 return 255;
94         else
95                 return clamp;
96 }
97
98 static void
99 convert_to_yv12(struct wcap_decoder *decoder, unsigned char *out)
100 {
101         unsigned char *y1, *y2, *u, *v;
102         uint32_t *p1, *p2, *end;
103         int i, u_accum, v_accum, stride0, stride1;
104         uint32_t format = decoder->format;
105
106         stride0 = decoder->width;
107         stride1 = decoder->width / 2;
108         for (i = 0; i < decoder->height; i += 2) {
109                 y1 = out + stride0 * i;
110                 y2 = y1 + stride0;
111                 v = out + stride0 * decoder->height + stride1 * i / 2;
112                 u = v + stride1 * decoder->height / 2;
113                 p1 = decoder->frame + decoder->width * i;
114                 p2 = p1 + decoder->width;
115                 end = p1 + decoder->width;
116
117                 while (p1 < end) {
118                         u_accum = 0;
119                         v_accum = 0;
120                         y1[0] = rgb_to_yuv(format, p1[0], &u_accum, &v_accum);
121                         y1[1] = rgb_to_yuv(format, p1[1], &u_accum, &v_accum);
122                         y2[0] = rgb_to_yuv(format, p2[0], &u_accum, &v_accum);
123                         y2[1] = rgb_to_yuv(format, p2[1], &u_accum, &v_accum);
124                         u[0] = clamp_uv(u_accum);
125                         v[0] = clamp_uv(v_accum);
126
127                         y1 += 2;
128                         p1 += 2;
129                         y2 += 2;
130                         p2 += 2;
131                         u++;
132                         v++;
133                 }
134         }
135 }
136
137 static void
138 convert_to_yuv444(struct wcap_decoder *decoder, unsigned char *out)
139 {
140
141         unsigned char *yp, *up, *vp;
142         uint32_t *rp, *end;
143         int u, v;
144         int i, stride, psize;
145         uint32_t format = decoder->format;
146
147         stride = decoder->width;
148         psize = stride * decoder->height;
149         for (i = 0; i < decoder->height; i++) {
150                 yp = out + stride * i;
151                 up = yp + (psize * 2);
152                 vp = yp + (psize * 1);
153                 rp = decoder->frame + decoder->width * i;
154                 end = rp + decoder->width;      
155                 while (rp < end) {
156                         u = 0;
157                         v = 0;
158                         yp[0] = rgb_to_yuv(format, rp[0], &u, &v);
159                         up[0] = clamp_uv(u/.3);
160                         vp[0] = clamp_uv(v/.3);
161                         up++;
162                         vp++;
163                         yp++;
164                         rp++;
165                 }
166         }
167 }
168
169 static void
170 output_yuv_frame(struct wcap_decoder *decoder, int depth)
171 {
172         static unsigned char *out;
173         int size;
174
175         if (depth == 444) {
176                 size = decoder->width * decoder->height * 3;
177         } else {
178                 size = decoder->width * decoder->height * 3 / 2;
179         }
180         if (out == NULL)
181                 out = malloc(size);
182
183         if (depth == 444) {
184                 convert_to_yuv444(decoder, out);
185         } else {
186                 convert_to_yv12(decoder, out);
187         }
188
189         printf("FRAME\n");
190         fwrite(out, 1, size, stdout);
191 }
192
193 static void
194 usage(int exit_code)
195 {
196         fprintf(stderr, "usage: wcap-decode "
197                 "[--help] [--yuv4mpeg2] [--frame=<frame>] [--all] \n"
198                 "\t[--rate=<num:denom>] <wcap file>\n\n"
199                 "\t--help\t\t\tthis help text\n"
200                 "\t--yuv4mpeg2\t\tdump wcap file to stdout in yuv4mpeg2 format\n"
201                 "\t--yuv4mpeg2-444\t\tdump wcap file to stdout in yuv4mpeg2 444 format\n"
202                 "\t--frame=<frame>\t\twrite out the given frame number as png\n"
203                 "\t--all\t\t\twrite all frames as pngs\n"
204                 "\t--rate=<num:denom>\treplay frame rate for yuv4mpeg2,\n"
205                 "\t\t\t\tspecified as an integer fraction\n\n");
206
207         exit(exit_code);
208 }
209
210 int main(int argc, char *argv[])
211 {
212         struct wcap_decoder *decoder;
213         int i, j, output_frame = -1, yuv4mpeg2 = 0, all = 0, has_frame;
214         int num = 30, denom = 1;
215         char filename[200];
216         char *mode;
217         uint32_t msecs, frame_time;
218
219         for (i = 1, j = 1; i < argc; i++) {
220                 if (strcmp(argv[i], "--yuv4mpeg2-444") == 0) {
221                         yuv4mpeg2 = 444;
222                 } else if (strcmp(argv[i], "--yuv4mpeg2") == 0) {
223                         yuv4mpeg2 = 420;
224                 } else if (strcmp(argv[i], "--help") == 0) {
225                         usage(EXIT_SUCCESS);
226                 } else if (strcmp(argv[i], "--all") == 0) {
227                         all = 1;
228                 } else if (sscanf(argv[i], "--frame=%d", &output_frame) == 1) {
229                         ;
230                 } else if (sscanf(argv[i], "--rate=%d", &num) == 1) {
231                         ;
232                 } else if (sscanf(argv[i], "--rate=%d:%d", &num, &denom) == 2) {
233                         ;
234                 } else if (strcmp(argv[i], "--") == 0) {
235                         break;
236                 } else if (argv[i][0] == '-') {
237                         fprintf(stderr,
238                                 "unknown option or invalid argument: %s\n", argv[i]);
239                         usage(EXIT_FAILURE);
240                 } else {
241                         argv[j++] = argv[i];
242                 }
243         }
244         argc = j;
245
246         if (argc != 2)
247                 usage(EXIT_FAILURE);
248         if (denom == 0) {
249                 fprintf(stderr, "invalid rate, denom can not be 0\n");
250                 exit(EXIT_FAILURE);
251         }
252
253         decoder = wcap_decoder_create(argv[1]);
254         if (decoder == NULL) {
255                 fprintf(stderr, "Creating wcap decoder failed\n");
256                 exit(EXIT_FAILURE);
257         }
258
259         if (yuv4mpeg2 && isatty(1)) {
260                 fprintf(stderr, "Not dumping yuv4mpeg2 data to terminal.  Pipe output to a file or a process.\n");
261                 fprintf(stderr, "For example, to encode to webm, use something like\n\n");
262                 fprintf(stderr, "\t$ wcap-decode  --yuv4mpeg2 ../capture.wcap |\n"
263                         "\t\tvpxenc --target-bitrate=1024 --best -t 4 -o foo.webm -\n\n");
264
265                 exit(EXIT_FAILURE);
266         }
267
268         if (yuv4mpeg2) {
269                 if (yuv4mpeg2 == 444) {
270                         mode = "C444";
271                 } else {
272                         mode = "C420jpeg";
273                 }
274                 printf("YUV4MPEG2 %s W%d H%d F%d:%d Ip A0:0\n",
275                                          mode, decoder->width, decoder->height, num, denom);
276                 fflush(stdout);
277         }
278
279         i = 0;
280         has_frame = wcap_decoder_get_frame(decoder);
281         msecs = decoder->msecs;
282         frame_time = 1000 * denom / num;
283         while (has_frame) {
284                 if (all || i == output_frame) {
285                         snprintf(filename, sizeof filename,
286                                  "wcap-frame-%d.png", i);
287                         write_png(decoder, filename);
288                         fprintf(stderr, "wrote %s\n", filename);
289                 }
290                 if (yuv4mpeg2)
291                         output_yuv_frame(decoder, yuv4mpeg2);
292                 i++;
293                 msecs += frame_time;
294                 while (decoder->msecs < msecs && has_frame)
295                         has_frame = wcap_decoder_get_frame(decoder);
296         }
297
298         fprintf(stderr, "wcap file: size %dx%d, %d frames\n",
299                 decoder->width, decoder->height, i);
300
301         wcap_decoder_destroy(decoder);
302
303         return EXIT_SUCCESS;
304 }