Kristian Høgsberg | c12efd0 | 2012-07-18 11:39:05 -0400 | [diff] [blame] | 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 <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øgsberg | 776a563 | 2012-07-23 10:47:34 -0400 | [diff] [blame^] | 33 | #include <assert.h> |
Kristian Høgsberg | c12efd0 | 2012-07-18 11:39:05 -0400 | [diff] [blame] | 34 | |
| 35 | #include <cairo.h> |
| 36 | |
| 37 | #include "wcap-decode.h" |
| 38 | |
| 39 | static void |
| 40 | write_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 | |
| 53 | static inline int |
| 54 | rgb_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øgsberg | 776a563 | 2012-07-23 10:47:34 -0400 | [diff] [blame^] | 69 | default: |
| 70 | assert(0); |
Kristian Høgsberg | c12efd0 | 2012-07-18 11:39:05 -0400 | [diff] [blame] | 71 | } |
| 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 | |
| 83 | static inline |
| 84 | int 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 | |
| 96 | static void |
| 97 | convert_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 | |
| 135 | static void |
| 136 | output_yuv_frame(struct wcap_decoder *decoder) |
| 137 | { |
Scott Moreau | 005d8cd | 2012-07-22 18:23:51 -0600 | [diff] [blame] | 138 | static unsigned char *out; |
Kristian Høgsberg | c12efd0 | 2012-07-18 11:39:05 -0400 | [diff] [blame] | 139 | 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 | |
| 150 | static void |
| 151 | usage(int exit_code) |
| 152 | { |
| 153 | fprintf(stderr, "usage: wcap-snapshot " |
| 154 | "[--help] [--yuv4mpeg2] [--frame=<frame>] [--all] \n" |
| 155 | "\t[--rate=<num:denom>] <wcap file>\n\n" |
| 156 | "\t--help\t\t\tthis help text\n" |
| 157 | "\t--yuv2mpeg4\t\tdump wcap file in yuv4mpeg format\n" |
| 158 | "\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 | |
| 166 | int 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 | |
| 208 | if (yuv4mpeg2) { |
| 209 | printf("YUV4MPEG2 C420jpeg W%d H%d F%d:%d Ip A0:0\n", |
| 210 | decoder->width, decoder->height, num, denom); |
| 211 | fflush(stdout); |
| 212 | } |
| 213 | |
| 214 | i = 0; |
| 215 | has_frame = wcap_decoder_get_frame(decoder); |
| 216 | msecs = decoder->msecs; |
| 217 | frame_time = 1000 * denom / num; |
| 218 | frame_size = decoder->width * decoder->height * 4; |
| 219 | frame = malloc(frame_size); |
| 220 | while (has_frame) { |
| 221 | if (decoder->msecs >= msecs) |
| 222 | memcpy(frame, decoder->frame, frame_size); |
| 223 | if (all || i == output_frame) { |
| 224 | snprintf(filename, sizeof filename, |
| 225 | "wcap-frame-%d.png", i); |
| 226 | write_png(decoder, filename); |
| 227 | fprintf(stderr, "wrote %s\n", filename); |
| 228 | } |
| 229 | if (yuv4mpeg2) |
| 230 | output_yuv_frame(decoder); |
| 231 | i++; |
| 232 | msecs += frame_time; |
| 233 | while (decoder->msecs < msecs && has_frame) |
| 234 | has_frame = wcap_decoder_get_frame(decoder); |
| 235 | } |
| 236 | |
| 237 | fprintf(stderr, "wcap file: size %dx%d, %d frames\n", |
| 238 | decoder->width, decoder->height, i); |
| 239 | |
| 240 | wcap_decoder_destroy(decoder); |
| 241 | |
| 242 | return EXIT_SUCCESS; |
| 243 | } |