blob: bdbc8cb7a82e4e3f611f0bdaaa963aab4880665f [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>
33
34#include <cairo.h>
35
36#include "wcap-decode.h"
37
38static void
39write_png(struct wcap_decoder *decoder, const char *filename)
40{
41 cairo_surface_t *surface;
42
43 surface = cairo_image_surface_create_for_data((unsigned char *) decoder->frame,
44 CAIRO_FORMAT_ARGB32,
45 decoder->width,
46 decoder->height,
47 decoder->width * 4);
48 cairo_surface_write_to_png(surface, filename);
49 cairo_surface_destroy(surface);
50}
51
52static inline int
53rgb_to_yuv(uint32_t format, uint32_t p, int *u, int *v)
54{
55 int r, g, b, y;
56
57 switch (format) {
58 case WCAP_FORMAT_XRGB8888:
59 r = (p >> 16) & 0xff;
60 g = (p >> 8) & 0xff;
61 b = (p >> 0) & 0xff;
62 break;
63 case WCAP_FORMAT_XBGR8888:
64 r = (p >> 0) & 0xff;
65 g = (p >> 8) & 0xff;
66 b = (p >> 16) & 0xff;
67 break;
68 }
69
70 y = (19595 * r + 38469 * g + 7472 * b) >> 16;
71 if (y > 255)
72 y = 255;
73
74 *u += 46727 * (r - y);
75 *v += 36962 * (b - y);
76
77 return y;
78}
79
80static inline
81int clamp_uv(int u)
82{
83 int clamp = (u >> 18) + 128;
84
85 if (clamp < 0)
86 return 0;
87 else if (clamp > 255)
88 return 255;
89 else
90 return clamp;
91}
92
93static void
94convert_to_yv12(struct wcap_decoder *decoder, unsigned char *out)
95{
96 unsigned char *y1, *y2, *u, *v;
97 uint32_t *p1, *p2, *end;
98 int i, u_accum, v_accum, stride0, stride1;
99 uint32_t format = decoder->format;
100
101 stride0 = decoder->width;
102 stride1 = decoder->width / 2;
103 for (i = 0; i < decoder->height; i += 2) {
104 y1 = out + stride0 * i;
105 y2 = y1 + stride0;
106 v = out + stride0 * decoder->height + stride1 * i / 2;
107 u = v + stride1 * decoder->height / 2;
108 p1 = decoder->frame + decoder->width * i;
109 p2 = p1 + decoder->width;
110 end = p1 + decoder->width;
111
112 while (p1 < end) {
113 u_accum = 0;
114 v_accum = 0;
115 y1[0] = rgb_to_yuv(format, p1[0], &u_accum, &v_accum);
116 y1[1] = rgb_to_yuv(format, p1[1], &u_accum, &v_accum);
117 y2[0] = rgb_to_yuv(format, p2[0], &u_accum, &v_accum);
118 y2[1] = rgb_to_yuv(format, p2[1], &u_accum, &v_accum);
119 u[0] = clamp_uv(u_accum);
120 v[0] = clamp_uv(v_accum);
121
122 y1 += 2;
123 p1 += 2;
124 y2 += 2;
125 p2 += 2;
126 u++;
127 v++;
128 }
129 }
130}
131
132static void
133output_yuv_frame(struct wcap_decoder *decoder)
134{
135 static char *out;
136 int size;
137
138 size = decoder->width * decoder->height * 3 / 2;
139 if (out == NULL)
140 out = malloc(size);
141
142 convert_to_yv12(decoder, out);
143 printf("FRAME\n");
144 fwrite(out, 1, size, stdout);
145}
146
147static void
148usage(int exit_code)
149{
150 fprintf(stderr, "usage: wcap-snapshot "
151 "[--help] [--yuv4mpeg2] [--frame=<frame>] [--all] \n"
152 "\t[--rate=<num:denom>] <wcap file>\n\n"
153 "\t--help\t\t\tthis help text\n"
154 "\t--yuv2mpeg4\t\tdump wcap file in yuv4mpeg format\n"
155 "\t--frame=<frame>\t\twrite out the given frame number as png\n"
156 "\t--all\t\t\twrite all frames as pngs\n"
157 "\t--rate=<num:denom>\treplay frame rate for yuv4mpeg2,\n"
158 "\t\t\t\tspecified as an integer fraction\n\n");
159
160 exit(exit_code);
161}
162
163int main(int argc, char *argv[])
164{
165 struct wcap_decoder *decoder;
166 int i, j, output_frame = -1, yuv4mpeg2 = 0, all = 0, has_frame;
167 int num = 30, denom = 1;
168 char filename[200];
169 uint32_t msecs, frame_time, *frame, frame_size;
170
171 for (i = 1, j = 1; i < argc; i++) {
172 if (strcmp(argv[i], "--yuv4mpeg2") == 0) {
173 yuv4mpeg2 = 1;
174 } else if (strcmp(argv[i], "--help") == 0) {
175 usage(EXIT_SUCCESS);
176 } else if (strcmp(argv[i], "--all") == 0) {
177 all = 1;
178 } else if (sscanf(argv[i], "--frame=%d", &output_frame) == 1) {
179 ;
180 } else if (sscanf(argv[i], "--rate=%d", &num) == 1) {
181 ;
182 } else if (sscanf(argv[i], "--rate=%d:%d", &num, &denom) == 2) {
183 ;
184 } else if (strcmp(argv[i], "--") == 0) {
185 break;
186 } else if (argv[i][0] == '-') {
187 fprintf(stderr,
188 "unknown option or invalid argument: %s\n", argv[i]);
189 usage(EXIT_FAILURE);
190 } else {
191 argv[j++] = argv[i];
192 }
193 }
194 argc = j;
195
196 if (argc != 2)
197 usage(EXIT_FAILURE);
198 if (denom == 0) {
199 fprintf(stderr, "invalid rate, denom can not be 0\n");
200 exit(EXIT_FAILURE);
201 }
202
203 decoder = wcap_decoder_create(argv[1]);
204
205 if (yuv4mpeg2) {
206 printf("YUV4MPEG2 C420jpeg W%d H%d F%d:%d Ip A0:0\n",
207 decoder->width, decoder->height, num, denom);
208 fflush(stdout);
209 }
210
211 i = 0;
212 has_frame = wcap_decoder_get_frame(decoder);
213 msecs = decoder->msecs;
214 frame_time = 1000 * denom / num;
215 frame_size = decoder->width * decoder->height * 4;
216 frame = malloc(frame_size);
217 while (has_frame) {
218 if (decoder->msecs >= msecs)
219 memcpy(frame, decoder->frame, frame_size);
220 if (all || i == output_frame) {
221 snprintf(filename, sizeof filename,
222 "wcap-frame-%d.png", i);
223 write_png(decoder, filename);
224 fprintf(stderr, "wrote %s\n", filename);
225 }
226 if (yuv4mpeg2)
227 output_yuv_frame(decoder);
228 i++;
229 msecs += frame_time;
230 while (decoder->msecs < msecs && has_frame)
231 has_frame = wcap_decoder_get_frame(decoder);
232 }
233
234 fprintf(stderr, "wcap file: size %dx%d, %d frames\n",
235 decoder->width, decoder->height, i);
236
237 wcap_decoder_destroy(decoder);
238
239 return EXIT_SUCCESS;
240}