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