blob: 94933042bfee7568d0391c05d12f9e5ce739f926 [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
136output_yuv_frame(struct wcap_decoder *decoder)
137{
Scott Moreau005d8cd2012-07-22 18:23:51 -0600138 static unsigned char *out;
Kristian Høgsbergc12efd02012-07-18 11:39:05 -0400139 int size;
140
141 size = decoder->width * decoder->height * 3 / 2;
142 if (out == NULL)
143 out = malloc(size);
144
145 convert_to_yv12(decoder, out);
146 printf("FRAME\n");
147 fwrite(out, 1, size, stdout);
148}
149
150static void
151usage(int exit_code)
152{
Kristian Høgsbergf32f0962012-07-23 11:10:20 -0400153 fprintf(stderr, "usage: wcap-decode "
Kristian Høgsbergc12efd02012-07-18 11:39:05 -0400154 "[--help] [--yuv4mpeg2] [--frame=<frame>] [--all] \n"
155 "\t[--rate=<num:denom>] <wcap file>\n\n"
156 "\t--help\t\t\tthis help text\n"
Scott Moreau2327d1f2012-07-23 11:53:18 -0600157 "\t--yuv4mpeg2\t\tdump wcap file to stdout in yuv4mpeg2 format\n"
Kristian Høgsbergc12efd02012-07-18 11:39:05 -0400158 "\t--frame=<frame>\t\twrite out the given frame number as png\n"
159 "\t--all\t\t\twrite all frames as pngs\n"
160 "\t--rate=<num:denom>\treplay frame rate for yuv4mpeg2,\n"
161 "\t\t\t\tspecified as an integer fraction\n\n");
162
163 exit(exit_code);
164}
165
166int main(int argc, char *argv[])
167{
168 struct wcap_decoder *decoder;
169 int i, j, output_frame = -1, yuv4mpeg2 = 0, all = 0, has_frame;
170 int num = 30, denom = 1;
171 char filename[200];
172 uint32_t msecs, frame_time, *frame, frame_size;
173
174 for (i = 1, j = 1; i < argc; i++) {
175 if (strcmp(argv[i], "--yuv4mpeg2") == 0) {
176 yuv4mpeg2 = 1;
177 } else if (strcmp(argv[i], "--help") == 0) {
178 usage(EXIT_SUCCESS);
179 } else if (strcmp(argv[i], "--all") == 0) {
180 all = 1;
181 } else if (sscanf(argv[i], "--frame=%d", &output_frame) == 1) {
182 ;
183 } else if (sscanf(argv[i], "--rate=%d", &num) == 1) {
184 ;
185 } else if (sscanf(argv[i], "--rate=%d:%d", &num, &denom) == 2) {
186 ;
187 } else if (strcmp(argv[i], "--") == 0) {
188 break;
189 } else if (argv[i][0] == '-') {
190 fprintf(stderr,
191 "unknown option or invalid argument: %s\n", argv[i]);
192 usage(EXIT_FAILURE);
193 } else {
194 argv[j++] = argv[i];
195 }
196 }
197 argc = j;
198
199 if (argc != 2)
200 usage(EXIT_FAILURE);
201 if (denom == 0) {
202 fprintf(stderr, "invalid rate, denom can not be 0\n");
203 exit(EXIT_FAILURE);
204 }
205
206 decoder = wcap_decoder_create(argv[1]);
207
Kristian Høgsbergf32f0962012-07-23 11:10:20 -0400208 if (yuv4mpeg2 && isatty(1)) {
209 fprintf(stderr, "Not dumping yuv4mpeg2 data to terminal. Pipe output to a file or a process.\n");
210 fprintf(stderr, "For example, to encode to webm, use something like\n\n");
211 fprintf(stderr, "\t$ wcap-decode --yuv4mpeg2 ../capture.wcap |\n"
212 "\t\tvpxenc --target-bitrate=1024 --best -t 4 -o foo.webm -\n\n");
213
214 exit(EXIT_FAILURE);
215 }
216
Kristian Høgsbergc12efd02012-07-18 11:39:05 -0400217 if (yuv4mpeg2) {
218 printf("YUV4MPEG2 C420jpeg W%d H%d F%d:%d Ip A0:0\n",
219 decoder->width, decoder->height, num, denom);
220 fflush(stdout);
221 }
222
223 i = 0;
224 has_frame = wcap_decoder_get_frame(decoder);
225 msecs = decoder->msecs;
226 frame_time = 1000 * denom / num;
227 frame_size = decoder->width * decoder->height * 4;
228 frame = malloc(frame_size);
229 while (has_frame) {
230 if (decoder->msecs >= msecs)
231 memcpy(frame, decoder->frame, frame_size);
232 if (all || i == output_frame) {
233 snprintf(filename, sizeof filename,
234 "wcap-frame-%d.png", i);
235 write_png(decoder, filename);
236 fprintf(stderr, "wrote %s\n", filename);
237 }
238 if (yuv4mpeg2)
239 output_yuv_frame(decoder);
240 i++;
241 msecs += frame_time;
242 while (decoder->msecs < msecs && has_frame)
243 has_frame = wcap_decoder_get_frame(decoder);
244 }
245
246 fprintf(stderr, "wcap file: size %dx%d, %d frames\n",
247 decoder->width, decoder->height, i);
248
249 wcap_decoder_destroy(decoder);
250
251 return EXIT_SUCCESS;
252}