media/libyuv/source/convert_jpeg.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright 2011 The LibYuv 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 #include "libyuv/convert.h"
michael@0 12
michael@0 13 #ifdef HAVE_JPEG
michael@0 14 #include "libyuv/mjpeg_decoder.h"
michael@0 15 #endif
michael@0 16
michael@0 17 #ifdef __cplusplus
michael@0 18 namespace libyuv {
michael@0 19 extern "C" {
michael@0 20 #endif
michael@0 21
michael@0 22 #ifdef HAVE_JPEG
michael@0 23 struct I420Buffers {
michael@0 24 uint8* y;
michael@0 25 int y_stride;
michael@0 26 uint8* u;
michael@0 27 int u_stride;
michael@0 28 uint8* v;
michael@0 29 int v_stride;
michael@0 30 int w;
michael@0 31 int h;
michael@0 32 };
michael@0 33
michael@0 34 static void JpegCopyI420(void* opaque,
michael@0 35 const uint8* const* data,
michael@0 36 const int* strides,
michael@0 37 int rows) {
michael@0 38 I420Buffers* dest = (I420Buffers*)(opaque);
michael@0 39 I420Copy(data[0], strides[0],
michael@0 40 data[1], strides[1],
michael@0 41 data[2], strides[2],
michael@0 42 dest->y, dest->y_stride,
michael@0 43 dest->u, dest->u_stride,
michael@0 44 dest->v, dest->v_stride,
michael@0 45 dest->w, rows);
michael@0 46 dest->y += rows * dest->y_stride;
michael@0 47 dest->u += ((rows + 1) >> 1) * dest->u_stride;
michael@0 48 dest->v += ((rows + 1) >> 1) * dest->v_stride;
michael@0 49 dest->h -= rows;
michael@0 50 }
michael@0 51
michael@0 52 static void JpegI422ToI420(void* opaque,
michael@0 53 const uint8* const* data,
michael@0 54 const int* strides,
michael@0 55 int rows) {
michael@0 56 I420Buffers* dest = (I420Buffers*)(opaque);
michael@0 57 I422ToI420(data[0], strides[0],
michael@0 58 data[1], strides[1],
michael@0 59 data[2], strides[2],
michael@0 60 dest->y, dest->y_stride,
michael@0 61 dest->u, dest->u_stride,
michael@0 62 dest->v, dest->v_stride,
michael@0 63 dest->w, rows);
michael@0 64 dest->y += rows * dest->y_stride;
michael@0 65 dest->u += ((rows + 1) >> 1) * dest->u_stride;
michael@0 66 dest->v += ((rows + 1) >> 1) * dest->v_stride;
michael@0 67 dest->h -= rows;
michael@0 68 }
michael@0 69
michael@0 70 static void JpegI444ToI420(void* opaque,
michael@0 71 const uint8* const* data,
michael@0 72 const int* strides,
michael@0 73 int rows) {
michael@0 74 I420Buffers* dest = (I420Buffers*)(opaque);
michael@0 75 I444ToI420(data[0], strides[0],
michael@0 76 data[1], strides[1],
michael@0 77 data[2], strides[2],
michael@0 78 dest->y, dest->y_stride,
michael@0 79 dest->u, dest->u_stride,
michael@0 80 dest->v, dest->v_stride,
michael@0 81 dest->w, rows);
michael@0 82 dest->y += rows * dest->y_stride;
michael@0 83 dest->u += ((rows + 1) >> 1) * dest->u_stride;
michael@0 84 dest->v += ((rows + 1) >> 1) * dest->v_stride;
michael@0 85 dest->h -= rows;
michael@0 86 }
michael@0 87
michael@0 88 static void JpegI411ToI420(void* opaque,
michael@0 89 const uint8* const* data,
michael@0 90 const int* strides,
michael@0 91 int rows) {
michael@0 92 I420Buffers* dest = (I420Buffers*)(opaque);
michael@0 93 I411ToI420(data[0], strides[0],
michael@0 94 data[1], strides[1],
michael@0 95 data[2], strides[2],
michael@0 96 dest->y, dest->y_stride,
michael@0 97 dest->u, dest->u_stride,
michael@0 98 dest->v, dest->v_stride,
michael@0 99 dest->w, rows);
michael@0 100 dest->y += rows * dest->y_stride;
michael@0 101 dest->u += ((rows + 1) >> 1) * dest->u_stride;
michael@0 102 dest->v += ((rows + 1) >> 1) * dest->v_stride;
michael@0 103 dest->h -= rows;
michael@0 104 }
michael@0 105
michael@0 106 static void JpegI400ToI420(void* opaque,
michael@0 107 const uint8* const* data,
michael@0 108 const int* strides,
michael@0 109 int rows) {
michael@0 110 I420Buffers* dest = (I420Buffers*)(opaque);
michael@0 111 I400ToI420(data[0], strides[0],
michael@0 112 dest->y, dest->y_stride,
michael@0 113 dest->u, dest->u_stride,
michael@0 114 dest->v, dest->v_stride,
michael@0 115 dest->w, rows);
michael@0 116 dest->y += rows * dest->y_stride;
michael@0 117 dest->u += ((rows + 1) >> 1) * dest->u_stride;
michael@0 118 dest->v += ((rows + 1) >> 1) * dest->v_stride;
michael@0 119 dest->h -= rows;
michael@0 120 }
michael@0 121
michael@0 122 // Query size of MJPG in pixels.
michael@0 123 LIBYUV_API
michael@0 124 int MJPGSize(const uint8* sample, size_t sample_size,
michael@0 125 int* width, int* height) {
michael@0 126 MJpegDecoder mjpeg_decoder;
michael@0 127 LIBYUV_BOOL ret = mjpeg_decoder.LoadFrame(sample, sample_size);
michael@0 128 if (ret) {
michael@0 129 *width = mjpeg_decoder.GetWidth();
michael@0 130 *height = mjpeg_decoder.GetHeight();
michael@0 131 }
michael@0 132 mjpeg_decoder.UnloadFrame();
michael@0 133 return ret ? 0 : -1; // -1 for runtime failure.
michael@0 134 }
michael@0 135
michael@0 136 // MJPG (Motion JPeg) to I420
michael@0 137 // TODO(fbarchard): review w and h requirement. dw and dh may be enough.
michael@0 138 LIBYUV_API
michael@0 139 int MJPGToI420(const uint8* sample,
michael@0 140 size_t sample_size,
michael@0 141 uint8* y, int y_stride,
michael@0 142 uint8* u, int u_stride,
michael@0 143 uint8* v, int v_stride,
michael@0 144 int w, int h,
michael@0 145 int dw, int dh) {
michael@0 146 if (sample_size == kUnknownDataSize) {
michael@0 147 // ERROR: MJPEG frame size unknown
michael@0 148 return -1;
michael@0 149 }
michael@0 150
michael@0 151 // TODO(fbarchard): Port MJpeg to C.
michael@0 152 MJpegDecoder mjpeg_decoder;
michael@0 153 LIBYUV_BOOL ret = mjpeg_decoder.LoadFrame(sample, sample_size);
michael@0 154 if (ret && (mjpeg_decoder.GetWidth() != w ||
michael@0 155 mjpeg_decoder.GetHeight() != h)) {
michael@0 156 // ERROR: MJPEG frame has unexpected dimensions
michael@0 157 mjpeg_decoder.UnloadFrame();
michael@0 158 return 1; // runtime failure
michael@0 159 }
michael@0 160 if (ret) {
michael@0 161 I420Buffers bufs = { y, y_stride, u, u_stride, v, v_stride, dw, dh };
michael@0 162 // YUV420
michael@0 163 if (mjpeg_decoder.GetColorSpace() ==
michael@0 164 MJpegDecoder::kColorSpaceYCbCr &&
michael@0 165 mjpeg_decoder.GetNumComponents() == 3 &&
michael@0 166 mjpeg_decoder.GetVertSampFactor(0) == 2 &&
michael@0 167 mjpeg_decoder.GetHorizSampFactor(0) == 2 &&
michael@0 168 mjpeg_decoder.GetVertSampFactor(1) == 1 &&
michael@0 169 mjpeg_decoder.GetHorizSampFactor(1) == 1 &&
michael@0 170 mjpeg_decoder.GetVertSampFactor(2) == 1 &&
michael@0 171 mjpeg_decoder.GetHorizSampFactor(2) == 1) {
michael@0 172 ret = mjpeg_decoder.DecodeToCallback(&JpegCopyI420, &bufs, dw, dh);
michael@0 173 // YUV422
michael@0 174 } else if (mjpeg_decoder.GetColorSpace() ==
michael@0 175 MJpegDecoder::kColorSpaceYCbCr &&
michael@0 176 mjpeg_decoder.GetNumComponents() == 3 &&
michael@0 177 mjpeg_decoder.GetVertSampFactor(0) == 1 &&
michael@0 178 mjpeg_decoder.GetHorizSampFactor(0) == 2 &&
michael@0 179 mjpeg_decoder.GetVertSampFactor(1) == 1 &&
michael@0 180 mjpeg_decoder.GetHorizSampFactor(1) == 1 &&
michael@0 181 mjpeg_decoder.GetVertSampFactor(2) == 1 &&
michael@0 182 mjpeg_decoder.GetHorizSampFactor(2) == 1) {
michael@0 183 ret = mjpeg_decoder.DecodeToCallback(&JpegI422ToI420, &bufs, dw, dh);
michael@0 184 // YUV444
michael@0 185 } else if (mjpeg_decoder.GetColorSpace() ==
michael@0 186 MJpegDecoder::kColorSpaceYCbCr &&
michael@0 187 mjpeg_decoder.GetNumComponents() == 3 &&
michael@0 188 mjpeg_decoder.GetVertSampFactor(0) == 1 &&
michael@0 189 mjpeg_decoder.GetHorizSampFactor(0) == 1 &&
michael@0 190 mjpeg_decoder.GetVertSampFactor(1) == 1 &&
michael@0 191 mjpeg_decoder.GetHorizSampFactor(1) == 1 &&
michael@0 192 mjpeg_decoder.GetVertSampFactor(2) == 1 &&
michael@0 193 mjpeg_decoder.GetHorizSampFactor(2) == 1) {
michael@0 194 ret = mjpeg_decoder.DecodeToCallback(&JpegI444ToI420, &bufs, dw, dh);
michael@0 195 // YUV411
michael@0 196 } else if (mjpeg_decoder.GetColorSpace() ==
michael@0 197 MJpegDecoder::kColorSpaceYCbCr &&
michael@0 198 mjpeg_decoder.GetNumComponents() == 3 &&
michael@0 199 mjpeg_decoder.GetVertSampFactor(0) == 1 &&
michael@0 200 mjpeg_decoder.GetHorizSampFactor(0) == 4 &&
michael@0 201 mjpeg_decoder.GetVertSampFactor(1) == 1 &&
michael@0 202 mjpeg_decoder.GetHorizSampFactor(1) == 1 &&
michael@0 203 mjpeg_decoder.GetVertSampFactor(2) == 1 &&
michael@0 204 mjpeg_decoder.GetHorizSampFactor(2) == 1) {
michael@0 205 ret = mjpeg_decoder.DecodeToCallback(&JpegI411ToI420, &bufs, dw, dh);
michael@0 206 // YUV400
michael@0 207 } else if (mjpeg_decoder.GetColorSpace() ==
michael@0 208 MJpegDecoder::kColorSpaceGrayscale &&
michael@0 209 mjpeg_decoder.GetNumComponents() == 1 &&
michael@0 210 mjpeg_decoder.GetVertSampFactor(0) == 1 &&
michael@0 211 mjpeg_decoder.GetHorizSampFactor(0) == 1) {
michael@0 212 ret = mjpeg_decoder.DecodeToCallback(&JpegI400ToI420, &bufs, dw, dh);
michael@0 213 } else {
michael@0 214 // TODO(fbarchard): Implement conversion for any other colorspace/sample
michael@0 215 // factors that occur in practice. 411 is supported by libjpeg
michael@0 216 // ERROR: Unable to convert MJPEG frame because format is not supported
michael@0 217 mjpeg_decoder.UnloadFrame();
michael@0 218 return 1;
michael@0 219 }
michael@0 220 }
michael@0 221 return 0;
michael@0 222 }
michael@0 223
michael@0 224 #ifdef HAVE_JPEG
michael@0 225 struct ARGBBuffers {
michael@0 226 uint8* argb;
michael@0 227 int argb_stride;
michael@0 228 int w;
michael@0 229 int h;
michael@0 230 };
michael@0 231
michael@0 232 static void JpegI420ToARGB(void* opaque,
michael@0 233 const uint8* const* data,
michael@0 234 const int* strides,
michael@0 235 int rows) {
michael@0 236 ARGBBuffers* dest = (ARGBBuffers*)(opaque);
michael@0 237 I420ToARGB(data[0], strides[0],
michael@0 238 data[1], strides[1],
michael@0 239 data[2], strides[2],
michael@0 240 dest->argb, dest->argb_stride,
michael@0 241 dest->w, rows);
michael@0 242 dest->argb += rows * dest->argb_stride;
michael@0 243 dest->h -= rows;
michael@0 244 }
michael@0 245
michael@0 246 static void JpegI422ToARGB(void* opaque,
michael@0 247 const uint8* const* data,
michael@0 248 const int* strides,
michael@0 249 int rows) {
michael@0 250 ARGBBuffers* dest = (ARGBBuffers*)(opaque);
michael@0 251 I422ToARGB(data[0], strides[0],
michael@0 252 data[1], strides[1],
michael@0 253 data[2], strides[2],
michael@0 254 dest->argb, dest->argb_stride,
michael@0 255 dest->w, rows);
michael@0 256 dest->argb += rows * dest->argb_stride;
michael@0 257 dest->h -= rows;
michael@0 258 }
michael@0 259
michael@0 260 static void JpegI444ToARGB(void* opaque,
michael@0 261 const uint8* const* data,
michael@0 262 const int* strides,
michael@0 263 int rows) {
michael@0 264 ARGBBuffers* dest = (ARGBBuffers*)(opaque);
michael@0 265 I444ToARGB(data[0], strides[0],
michael@0 266 data[1], strides[1],
michael@0 267 data[2], strides[2],
michael@0 268 dest->argb, dest->argb_stride,
michael@0 269 dest->w, rows);
michael@0 270 dest->argb += rows * dest->argb_stride;
michael@0 271 dest->h -= rows;
michael@0 272 }
michael@0 273
michael@0 274 static void JpegI411ToARGB(void* opaque,
michael@0 275 const uint8* const* data,
michael@0 276 const int* strides,
michael@0 277 int rows) {
michael@0 278 ARGBBuffers* dest = (ARGBBuffers*)(opaque);
michael@0 279 I411ToARGB(data[0], strides[0],
michael@0 280 data[1], strides[1],
michael@0 281 data[2], strides[2],
michael@0 282 dest->argb, dest->argb_stride,
michael@0 283 dest->w, rows);
michael@0 284 dest->argb += rows * dest->argb_stride;
michael@0 285 dest->h -= rows;
michael@0 286 }
michael@0 287
michael@0 288 static void JpegI400ToARGB(void* opaque,
michael@0 289 const uint8* const* data,
michael@0 290 const int* strides,
michael@0 291 int rows) {
michael@0 292 ARGBBuffers* dest = (ARGBBuffers*)(opaque);
michael@0 293 I400ToARGB(data[0], strides[0],
michael@0 294 dest->argb, dest->argb_stride,
michael@0 295 dest->w, rows);
michael@0 296 dest->argb += rows * dest->argb_stride;
michael@0 297 dest->h -= rows;
michael@0 298 }
michael@0 299
michael@0 300 // MJPG (Motion JPeg) to ARGB
michael@0 301 // TODO(fbarchard): review w and h requirement. dw and dh may be enough.
michael@0 302 LIBYUV_API
michael@0 303 int MJPGToARGB(const uint8* sample,
michael@0 304 size_t sample_size,
michael@0 305 uint8* argb, int argb_stride,
michael@0 306 int w, int h,
michael@0 307 int dw, int dh) {
michael@0 308 if (sample_size == kUnknownDataSize) {
michael@0 309 // ERROR: MJPEG frame size unknown
michael@0 310 return -1;
michael@0 311 }
michael@0 312
michael@0 313 // TODO(fbarchard): Port MJpeg to C.
michael@0 314 MJpegDecoder mjpeg_decoder;
michael@0 315 LIBYUV_BOOL ret = mjpeg_decoder.LoadFrame(sample, sample_size);
michael@0 316 if (ret && (mjpeg_decoder.GetWidth() != w ||
michael@0 317 mjpeg_decoder.GetHeight() != h)) {
michael@0 318 // ERROR: MJPEG frame has unexpected dimensions
michael@0 319 mjpeg_decoder.UnloadFrame();
michael@0 320 return 1; // runtime failure
michael@0 321 }
michael@0 322 if (ret) {
michael@0 323 ARGBBuffers bufs = { argb, argb_stride, dw, dh };
michael@0 324 // YUV420
michael@0 325 if (mjpeg_decoder.GetColorSpace() ==
michael@0 326 MJpegDecoder::kColorSpaceYCbCr &&
michael@0 327 mjpeg_decoder.GetNumComponents() == 3 &&
michael@0 328 mjpeg_decoder.GetVertSampFactor(0) == 2 &&
michael@0 329 mjpeg_decoder.GetHorizSampFactor(0) == 2 &&
michael@0 330 mjpeg_decoder.GetVertSampFactor(1) == 1 &&
michael@0 331 mjpeg_decoder.GetHorizSampFactor(1) == 1 &&
michael@0 332 mjpeg_decoder.GetVertSampFactor(2) == 1 &&
michael@0 333 mjpeg_decoder.GetHorizSampFactor(2) == 1) {
michael@0 334 ret = mjpeg_decoder.DecodeToCallback(&JpegI420ToARGB, &bufs, dw, dh);
michael@0 335 // YUV422
michael@0 336 } else if (mjpeg_decoder.GetColorSpace() ==
michael@0 337 MJpegDecoder::kColorSpaceYCbCr &&
michael@0 338 mjpeg_decoder.GetNumComponents() == 3 &&
michael@0 339 mjpeg_decoder.GetVertSampFactor(0) == 1 &&
michael@0 340 mjpeg_decoder.GetHorizSampFactor(0) == 2 &&
michael@0 341 mjpeg_decoder.GetVertSampFactor(1) == 1 &&
michael@0 342 mjpeg_decoder.GetHorizSampFactor(1) == 1 &&
michael@0 343 mjpeg_decoder.GetVertSampFactor(2) == 1 &&
michael@0 344 mjpeg_decoder.GetHorizSampFactor(2) == 1) {
michael@0 345 ret = mjpeg_decoder.DecodeToCallback(&JpegI422ToARGB, &bufs, dw, dh);
michael@0 346 // YUV444
michael@0 347 } else if (mjpeg_decoder.GetColorSpace() ==
michael@0 348 MJpegDecoder::kColorSpaceYCbCr &&
michael@0 349 mjpeg_decoder.GetNumComponents() == 3 &&
michael@0 350 mjpeg_decoder.GetVertSampFactor(0) == 1 &&
michael@0 351 mjpeg_decoder.GetHorizSampFactor(0) == 1 &&
michael@0 352 mjpeg_decoder.GetVertSampFactor(1) == 1 &&
michael@0 353 mjpeg_decoder.GetHorizSampFactor(1) == 1 &&
michael@0 354 mjpeg_decoder.GetVertSampFactor(2) == 1 &&
michael@0 355 mjpeg_decoder.GetHorizSampFactor(2) == 1) {
michael@0 356 ret = mjpeg_decoder.DecodeToCallback(&JpegI444ToARGB, &bufs, dw, dh);
michael@0 357 // YUV411
michael@0 358 } else if (mjpeg_decoder.GetColorSpace() ==
michael@0 359 MJpegDecoder::kColorSpaceYCbCr &&
michael@0 360 mjpeg_decoder.GetNumComponents() == 3 &&
michael@0 361 mjpeg_decoder.GetVertSampFactor(0) == 1 &&
michael@0 362 mjpeg_decoder.GetHorizSampFactor(0) == 4 &&
michael@0 363 mjpeg_decoder.GetVertSampFactor(1) == 1 &&
michael@0 364 mjpeg_decoder.GetHorizSampFactor(1) == 1 &&
michael@0 365 mjpeg_decoder.GetVertSampFactor(2) == 1 &&
michael@0 366 mjpeg_decoder.GetHorizSampFactor(2) == 1) {
michael@0 367 ret = mjpeg_decoder.DecodeToCallback(&JpegI411ToARGB, &bufs, dw, dh);
michael@0 368 // YUV400
michael@0 369 } else if (mjpeg_decoder.GetColorSpace() ==
michael@0 370 MJpegDecoder::kColorSpaceGrayscale &&
michael@0 371 mjpeg_decoder.GetNumComponents() == 1 &&
michael@0 372 mjpeg_decoder.GetVertSampFactor(0) == 1 &&
michael@0 373 mjpeg_decoder.GetHorizSampFactor(0) == 1) {
michael@0 374 ret = mjpeg_decoder.DecodeToCallback(&JpegI400ToARGB, &bufs, dw, dh);
michael@0 375 } else {
michael@0 376 // TODO(fbarchard): Implement conversion for any other colorspace/sample
michael@0 377 // factors that occur in practice. 411 is supported by libjpeg
michael@0 378 // ERROR: Unable to convert MJPEG frame because format is not supported
michael@0 379 mjpeg_decoder.UnloadFrame();
michael@0 380 return 1;
michael@0 381 }
michael@0 382 }
michael@0 383 return 0;
michael@0 384 }
michael@0 385 #endif
michael@0 386
michael@0 387 #endif
michael@0 388
michael@0 389 #ifdef __cplusplus
michael@0 390 } // extern "C"
michael@0 391 } // namespace libyuv
michael@0 392 #endif

mercurial