Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license |
michael@0 | 5 | * that can be found in the LICENSE file in the root of the source |
michael@0 | 6 | * tree. An additional intellectual property rights grant can be found |
michael@0 | 7 | * in the file PATENTS. All contributing project authors may |
michael@0 | 8 | * be found in the AUTHORS file in the root of the source tree. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | |
michael@0 | 12 | #include <stdlib.h> |
michael@0 | 13 | #include <string.h> |
michael@0 | 14 | #include "vpx/vpx_image.h" |
michael@0 | 15 | |
michael@0 | 16 | #define ADDRESS_STORAGE_SIZE sizeof(size_t) |
michael@0 | 17 | /*returns an addr aligned to the byte boundary specified by align*/ |
michael@0 | 18 | #define align_addr(addr,align) (void*)(((size_t)(addr) + ((align) - 1)) & (size_t)-(align)) |
michael@0 | 19 | |
michael@0 | 20 | /* Memalign code is copied from vpx_mem.c */ |
michael@0 | 21 | static void *img_buf_memalign(size_t align, size_t size) { |
michael@0 | 22 | void *addr, |
michael@0 | 23 | * x = NULL; |
michael@0 | 24 | |
michael@0 | 25 | addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE); |
michael@0 | 26 | |
michael@0 | 27 | if (addr) { |
michael@0 | 28 | x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align); |
michael@0 | 29 | /* save the actual malloc address */ |
michael@0 | 30 | ((size_t *)x)[-1] = (size_t)addr; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | return x; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | static void img_buf_free(void *memblk) { |
michael@0 | 37 | if (memblk) { |
michael@0 | 38 | void *addr = (void *)(((size_t *)memblk)[-1]); |
michael@0 | 39 | free(addr); |
michael@0 | 40 | } |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | static vpx_image_t *img_alloc_helper(vpx_image_t *img, |
michael@0 | 44 | vpx_img_fmt_t fmt, |
michael@0 | 45 | unsigned int d_w, |
michael@0 | 46 | unsigned int d_h, |
michael@0 | 47 | unsigned int buf_align, |
michael@0 | 48 | unsigned int stride_align, |
michael@0 | 49 | unsigned char *img_data) { |
michael@0 | 50 | |
michael@0 | 51 | unsigned int h, w, s, xcs, ycs, bps; |
michael@0 | 52 | int align; |
michael@0 | 53 | |
michael@0 | 54 | /* Treat align==0 like align==1 */ |
michael@0 | 55 | if (!buf_align) |
michael@0 | 56 | buf_align = 1; |
michael@0 | 57 | |
michael@0 | 58 | /* Validate alignment (must be power of 2) */ |
michael@0 | 59 | if (buf_align & (buf_align - 1)) |
michael@0 | 60 | goto fail; |
michael@0 | 61 | |
michael@0 | 62 | /* Treat align==0 like align==1 */ |
michael@0 | 63 | if (!stride_align) |
michael@0 | 64 | stride_align = 1; |
michael@0 | 65 | |
michael@0 | 66 | /* Validate alignment (must be power of 2) */ |
michael@0 | 67 | if (stride_align & (stride_align - 1)) |
michael@0 | 68 | goto fail; |
michael@0 | 69 | |
michael@0 | 70 | /* Get sample size for this format */ |
michael@0 | 71 | switch (fmt) { |
michael@0 | 72 | case VPX_IMG_FMT_RGB32: |
michael@0 | 73 | case VPX_IMG_FMT_RGB32_LE: |
michael@0 | 74 | case VPX_IMG_FMT_ARGB: |
michael@0 | 75 | case VPX_IMG_FMT_ARGB_LE: |
michael@0 | 76 | bps = 32; |
michael@0 | 77 | break; |
michael@0 | 78 | case VPX_IMG_FMT_RGB24: |
michael@0 | 79 | case VPX_IMG_FMT_BGR24: |
michael@0 | 80 | bps = 24; |
michael@0 | 81 | break; |
michael@0 | 82 | case VPX_IMG_FMT_RGB565: |
michael@0 | 83 | case VPX_IMG_FMT_RGB565_LE: |
michael@0 | 84 | case VPX_IMG_FMT_RGB555: |
michael@0 | 85 | case VPX_IMG_FMT_RGB555_LE: |
michael@0 | 86 | case VPX_IMG_FMT_UYVY: |
michael@0 | 87 | case VPX_IMG_FMT_YUY2: |
michael@0 | 88 | case VPX_IMG_FMT_YVYU: |
michael@0 | 89 | bps = 16; |
michael@0 | 90 | break; |
michael@0 | 91 | case VPX_IMG_FMT_I420: |
michael@0 | 92 | case VPX_IMG_FMT_YV12: |
michael@0 | 93 | case VPX_IMG_FMT_VPXI420: |
michael@0 | 94 | case VPX_IMG_FMT_VPXYV12: |
michael@0 | 95 | bps = 12; |
michael@0 | 96 | break; |
michael@0 | 97 | default: |
michael@0 | 98 | bps = 16; |
michael@0 | 99 | break; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | /* Get chroma shift values for this format */ |
michael@0 | 103 | switch (fmt) { |
michael@0 | 104 | case VPX_IMG_FMT_I420: |
michael@0 | 105 | case VPX_IMG_FMT_YV12: |
michael@0 | 106 | case VPX_IMG_FMT_VPXI420: |
michael@0 | 107 | case VPX_IMG_FMT_VPXYV12: |
michael@0 | 108 | xcs = 1; |
michael@0 | 109 | break; |
michael@0 | 110 | default: |
michael@0 | 111 | xcs = 0; |
michael@0 | 112 | break; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | switch (fmt) { |
michael@0 | 116 | case VPX_IMG_FMT_I420: |
michael@0 | 117 | case VPX_IMG_FMT_YV12: |
michael@0 | 118 | case VPX_IMG_FMT_VPXI420: |
michael@0 | 119 | case VPX_IMG_FMT_VPXYV12: |
michael@0 | 120 | ycs = 1; |
michael@0 | 121 | break; |
michael@0 | 122 | default: |
michael@0 | 123 | ycs = 0; |
michael@0 | 124 | break; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | /* Calculate storage sizes given the chroma subsampling */ |
michael@0 | 128 | align = (1 << xcs) - 1; |
michael@0 | 129 | w = (d_w + align) & ~align; |
michael@0 | 130 | align = (1 << ycs) - 1; |
michael@0 | 131 | h = (d_h + align) & ~align; |
michael@0 | 132 | s = (fmt & VPX_IMG_FMT_PLANAR) ? w : bps * w / 8; |
michael@0 | 133 | s = (s + stride_align - 1) & ~(stride_align - 1); |
michael@0 | 134 | |
michael@0 | 135 | /* Allocate the new image */ |
michael@0 | 136 | if (!img) { |
michael@0 | 137 | img = (vpx_image_t *)calloc(1, sizeof(vpx_image_t)); |
michael@0 | 138 | |
michael@0 | 139 | if (!img) |
michael@0 | 140 | goto fail; |
michael@0 | 141 | |
michael@0 | 142 | img->self_allocd = 1; |
michael@0 | 143 | } else { |
michael@0 | 144 | memset(img, 0, sizeof(vpx_image_t)); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | img->img_data = img_data; |
michael@0 | 148 | |
michael@0 | 149 | if (!img_data) { |
michael@0 | 150 | img->img_data = img_buf_memalign(buf_align, ((fmt & VPX_IMG_FMT_PLANAR) ? |
michael@0 | 151 | h * s * bps / 8 : h * s)); |
michael@0 | 152 | img->img_data_owner = 1; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | if (!img->img_data) |
michael@0 | 156 | goto fail; |
michael@0 | 157 | |
michael@0 | 158 | img->fmt = fmt; |
michael@0 | 159 | img->w = w; |
michael@0 | 160 | img->h = h; |
michael@0 | 161 | img->x_chroma_shift = xcs; |
michael@0 | 162 | img->y_chroma_shift = ycs; |
michael@0 | 163 | img->bps = bps; |
michael@0 | 164 | |
michael@0 | 165 | /* Calculate strides */ |
michael@0 | 166 | img->stride[VPX_PLANE_Y] = img->stride[VPX_PLANE_ALPHA] = s; |
michael@0 | 167 | img->stride[VPX_PLANE_U] = img->stride[VPX_PLANE_V] = s >> xcs; |
michael@0 | 168 | |
michael@0 | 169 | /* Default viewport to entire image */ |
michael@0 | 170 | if (!vpx_img_set_rect(img, 0, 0, d_w, d_h)) |
michael@0 | 171 | return img; |
michael@0 | 172 | |
michael@0 | 173 | fail: |
michael@0 | 174 | vpx_img_free(img); |
michael@0 | 175 | return NULL; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | vpx_image_t *vpx_img_alloc(vpx_image_t *img, |
michael@0 | 179 | vpx_img_fmt_t fmt, |
michael@0 | 180 | unsigned int d_w, |
michael@0 | 181 | unsigned int d_h, |
michael@0 | 182 | unsigned int align) { |
michael@0 | 183 | return img_alloc_helper(img, fmt, d_w, d_h, align, align, NULL); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | vpx_image_t *vpx_img_wrap(vpx_image_t *img, |
michael@0 | 187 | vpx_img_fmt_t fmt, |
michael@0 | 188 | unsigned int d_w, |
michael@0 | 189 | unsigned int d_h, |
michael@0 | 190 | unsigned int stride_align, |
michael@0 | 191 | unsigned char *img_data) { |
michael@0 | 192 | /* By setting buf_align = 1, we don't change buffer alignment in this |
michael@0 | 193 | * function. */ |
michael@0 | 194 | return img_alloc_helper(img, fmt, d_w, d_h, 1, stride_align, img_data); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | int vpx_img_set_rect(vpx_image_t *img, |
michael@0 | 198 | unsigned int x, |
michael@0 | 199 | unsigned int y, |
michael@0 | 200 | unsigned int w, |
michael@0 | 201 | unsigned int h) { |
michael@0 | 202 | unsigned char *data; |
michael@0 | 203 | |
michael@0 | 204 | if (x + w <= img->w && y + h <= img->h) { |
michael@0 | 205 | img->d_w = w; |
michael@0 | 206 | img->d_h = h; |
michael@0 | 207 | |
michael@0 | 208 | /* Calculate plane pointers */ |
michael@0 | 209 | if (!(img->fmt & VPX_IMG_FMT_PLANAR)) { |
michael@0 | 210 | img->planes[VPX_PLANE_PACKED] = |
michael@0 | 211 | img->img_data + x * img->bps / 8 + y * img->stride[VPX_PLANE_PACKED]; |
michael@0 | 212 | } else { |
michael@0 | 213 | data = img->img_data; |
michael@0 | 214 | |
michael@0 | 215 | if (img->fmt & VPX_IMG_FMT_HAS_ALPHA) { |
michael@0 | 216 | img->planes[VPX_PLANE_ALPHA] = |
michael@0 | 217 | data + x + y * img->stride[VPX_PLANE_ALPHA]; |
michael@0 | 218 | data += img->h * img->stride[VPX_PLANE_ALPHA]; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | img->planes[VPX_PLANE_Y] = data + x + y * img->stride[VPX_PLANE_Y]; |
michael@0 | 222 | data += img->h * img->stride[VPX_PLANE_Y]; |
michael@0 | 223 | |
michael@0 | 224 | if (!(img->fmt & VPX_IMG_FMT_UV_FLIP)) { |
michael@0 | 225 | img->planes[VPX_PLANE_U] = data |
michael@0 | 226 | + (x >> img->x_chroma_shift) |
michael@0 | 227 | + (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_U]; |
michael@0 | 228 | data += (img->h >> img->y_chroma_shift) * img->stride[VPX_PLANE_U]; |
michael@0 | 229 | img->planes[VPX_PLANE_V] = data |
michael@0 | 230 | + (x >> img->x_chroma_shift) |
michael@0 | 231 | + (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_V]; |
michael@0 | 232 | } else { |
michael@0 | 233 | img->planes[VPX_PLANE_V] = data |
michael@0 | 234 | + (x >> img->x_chroma_shift) |
michael@0 | 235 | + (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_V]; |
michael@0 | 236 | data += (img->h >> img->y_chroma_shift) * img->stride[VPX_PLANE_V]; |
michael@0 | 237 | img->planes[VPX_PLANE_U] = data |
michael@0 | 238 | + (x >> img->x_chroma_shift) |
michael@0 | 239 | + (y >> img->y_chroma_shift) * img->stride[VPX_PLANE_U]; |
michael@0 | 240 | } |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | return 0; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | return -1; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | void vpx_img_flip(vpx_image_t *img) { |
michael@0 | 250 | /* Note: In the calculation pointer adjustment calculation, we want the |
michael@0 | 251 | * rhs to be promoted to a signed type. Section 6.3.1.8 of the ISO C99 |
michael@0 | 252 | * standard indicates that if the adjustment parameter is unsigned, the |
michael@0 | 253 | * stride parameter will be promoted to unsigned, causing errors when |
michael@0 | 254 | * the lhs is a larger type than the rhs. |
michael@0 | 255 | */ |
michael@0 | 256 | img->planes[VPX_PLANE_Y] += (signed)(img->d_h - 1) * img->stride[VPX_PLANE_Y]; |
michael@0 | 257 | img->stride[VPX_PLANE_Y] = -img->stride[VPX_PLANE_Y]; |
michael@0 | 258 | |
michael@0 | 259 | img->planes[VPX_PLANE_U] += (signed)((img->d_h >> img->y_chroma_shift) - 1) |
michael@0 | 260 | * img->stride[VPX_PLANE_U]; |
michael@0 | 261 | img->stride[VPX_PLANE_U] = -img->stride[VPX_PLANE_U]; |
michael@0 | 262 | |
michael@0 | 263 | img->planes[VPX_PLANE_V] += (signed)((img->d_h >> img->y_chroma_shift) - 1) |
michael@0 | 264 | * img->stride[VPX_PLANE_V]; |
michael@0 | 265 | img->stride[VPX_PLANE_V] = -img->stride[VPX_PLANE_V]; |
michael@0 | 266 | |
michael@0 | 267 | img->planes[VPX_PLANE_ALPHA] += (signed)(img->d_h - 1) * img->stride[VPX_PLANE_ALPHA]; |
michael@0 | 268 | img->stride[VPX_PLANE_ALPHA] = -img->stride[VPX_PLANE_ALPHA]; |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | void vpx_img_free(vpx_image_t *img) { |
michael@0 | 272 | if (img) { |
michael@0 | 273 | if (img->img_data && img->img_data_owner) |
michael@0 | 274 | img_buf_free(img->img_data); |
michael@0 | 275 | |
michael@0 | 276 | if (img->self_allocd) |
michael@0 | 277 | free(img); |
michael@0 | 278 | } |
michael@0 | 279 | } |