Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// Copyright 2010 Google Inc.
//
// This code is licensed under the same terms as WebM:
//  Software License Agreement:  http://www.webmproject.org/license/software/
//  Additional IP Rights Grant:  http://www.webmproject.org/license/additional/
// -----------------------------------------------------------------------------
//
// Main decoding functions for WEBP images.
//
// Author: Skal (pascal.massimino@gmail.com)

#include <stdlib.h>
#include "vp8i.h"
#include "webpi.h"

#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif

//------------------------------------------------------------------------------
// RIFF layout is:
//   Offset  tag
//   0...3   "RIFF" 4-byte tag
//   4...7   size of image data (including metadata) starting at offset 8
//   8...11  "WEBP"   our form-type signature
// The RIFF container (12 bytes) is followed by appropriate chunks:
//   12..15  "VP8 ": 4-bytes tags, describing the raw video format used
//   16..19  size of the raw VP8 image data, starting at offset 20
//   20....  the VP8 bytes
// Or,
//   12..15  "VP8X": 4-bytes tags, describing the extended-VP8 chunk.
//   16..19  size of the VP8X chunk starting at offset 20.
//   20..23  VP8X flags bit-map corresponding to the chunk-types present.
//   24..27  Width of the Canvas Image.
//   28..31  Height of the Canvas Image.
// There can be extra chunks after the "VP8X" chunk (ICCP, TILE, FRM, VP8,
// META  ...)
// All 32-bits sizes are in little-endian order.
// Note: chunk data must be padded to multiple of 2 in size

static inline uint32_t get_le32(const uint8_t* const data) {
  return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
}

VP8StatusCode WebPParseRIFF(const uint8_t** data, uint32_t* data_size,
                            uint32_t* riff_size) {
  assert(data);
  assert(data_size);
  assert(riff_size);

  if (*data_size >= RIFF_HEADER_SIZE &&
      !memcmp(*data, "RIFF", TAG_SIZE)) {
    if (memcmp(*data + 8, "WEBP", TAG_SIZE)) {
      return VP8_STATUS_BITSTREAM_ERROR;  // Wrong image file signature.
    } else {
      *riff_size = get_le32(*data + TAG_SIZE);
      // Check that we have at least one chunk (i.e "WEBP" + "VP8?nnnn").
      if (*riff_size < TAG_SIZE + CHUNK_HEADER_SIZE) {
        return VP8_STATUS_BITSTREAM_ERROR;
      }
      // We have a RIFF container. Skip it.
      *data += RIFF_HEADER_SIZE;
      *data_size -= RIFF_HEADER_SIZE;
    }
  } else {
    *riff_size = 0;  // Did not get full RIFF Header.
  }
  return VP8_STATUS_OK;
}

VP8StatusCode WebPParseVP8X(const uint8_t** data, uint32_t* data_size,
                            uint32_t* bytes_skipped,
                            int* width, int* height, uint32_t* flags) {
  assert(data);
  assert(data_size);
  assert(bytes_skipped);

  *bytes_skipped = 0;

  if (*data_size < CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE) {
    return VP8_STATUS_NOT_ENOUGH_DATA;  // Insufficient data.
  }

  if (!memcmp(*data, "VP8X", TAG_SIZE)) {
    const uint32_t chunk_size = get_le32(*data + TAG_SIZE);
    if (chunk_size != VP8X_CHUNK_SIZE) {
      return VP8_STATUS_BITSTREAM_ERROR;  // Wrong chunk size.
    }
    if (flags) {
      *flags = get_le32(*data + 8);
    }
    if (width) {
      *width = get_le32(*data + 12);
    }
    if (height) {
      *height = get_le32(*data + 16);
    }
    // We have consumed 20 bytes from VP8X. Skip them.
    *bytes_skipped = CHUNK_HEADER_SIZE + VP8X_CHUNK_SIZE;
    *data += *bytes_skipped;
    *data_size -= *bytes_skipped;
  }
  return VP8_STATUS_OK;
}

VP8StatusCode WebPParseOptionalChunks(const uint8_t** data, uint32_t* data_size,
                                      uint32_t riff_size,
                                      uint32_t* bytes_skipped) {
  const uint8_t* buf;
  uint32_t buf_size;

  assert(data);
  assert(data_size);
  assert(bytes_skipped);

  buf = *data;
  buf_size = *data_size;
  *bytes_skipped = 0;

  while (1) {
    uint32_t chunk_size;
    uint32_t cur_skip_size;
    const uint32_t bytes_skipped_header = TAG_SIZE +           // "WEBP".
                                          CHUNK_HEADER_SIZE +  // "VP8Xnnnn".
                                          VP8X_CHUNK_SIZE;     // Data.
    *data = buf;
    *data_size = buf_size;

    if (buf_size < CHUNK_HEADER_SIZE) {  // Insufficient data.
      return VP8_STATUS_NOT_ENOUGH_DATA;
    }

    chunk_size = get_le32(buf + TAG_SIZE);
    cur_skip_size = CHUNK_HEADER_SIZE + chunk_size;

    // Check that total bytes skipped along with current chunk size
    // does not exceed riff_size.
    if (riff_size > 0 &&
        (bytes_skipped_header + *bytes_skipped + cur_skip_size > riff_size)) {
      return VP8_STATUS_BITSTREAM_ERROR;  // Not a valid chunk size.
    }

    if (buf_size < cur_skip_size) {  // Insufficient data.
      return VP8_STATUS_NOT_ENOUGH_DATA;
    }

    if (!memcmp(buf, "VP8 ", TAG_SIZE)) {  // A valid VP8 header.
      return VP8_STATUS_OK;  // Found.
    }

    // We have a full & valid chunk; skip it.
    buf += cur_skip_size;
    buf_size -= cur_skip_size;
    *bytes_skipped += cur_skip_size;
  }
}

VP8StatusCode WebPParseVP8Header(const uint8_t** data, uint32_t* data_size,
                                 uint32_t riff_size, uint32_t* bytes_skipped,
                                 uint32_t* vp8_chunk_size) {
  assert(data);
  assert(data_size);
  assert(bytes_skipped);
  assert(vp8_chunk_size);

  *bytes_skipped = 0;
  *vp8_chunk_size = 0;

  if (*data_size < CHUNK_HEADER_SIZE) {
    return VP8_STATUS_NOT_ENOUGH_DATA;  // Insufficient data.
  }

  if (!memcmp(*data, "VP8 ", TAG_SIZE)) {
    *vp8_chunk_size = get_le32(*data + TAG_SIZE);
    if (riff_size >= TAG_SIZE + CHUNK_HEADER_SIZE &&  // "WEBP" + "VP8 nnnn".
        (*vp8_chunk_size > riff_size - (TAG_SIZE + CHUNK_HEADER_SIZE))) {
      return VP8_STATUS_BITSTREAM_ERROR;  // Inconsistent size information.
    }
    // We have consumed CHUNK_HEADER_SIZE bytes from VP8 Header. Skip them.
    *bytes_skipped = CHUNK_HEADER_SIZE;
    *data += *bytes_skipped;
    *data_size -= *bytes_skipped;
  }
  return VP8_STATUS_OK;
}

VP8StatusCode WebPParseHeaders(const uint8_t** data, uint32_t* data_size,
                               uint32_t* vp8_size, uint32_t* bytes_skipped) {
  const uint8_t* buf;
  uint32_t buf_size;
  uint32_t riff_size;
  uint32_t vp8_size_tmp;
  uint32_t optional_data_size;
  uint32_t vp8x_skip_size;
  uint32_t vp8_skip_size;
  VP8StatusCode status;

  assert(data);
  assert(data_size);
  assert(vp8_size);
  assert(bytes_skipped);

  buf = *data;
  buf_size = *data_size;

  *vp8_size = 0;
  *bytes_skipped = 0;

  if (buf == NULL || buf_size < RIFF_HEADER_SIZE) {
    return VP8_STATUS_NOT_ENOUGH_DATA;
  }

  // Skip over RIFF header.
  if (WebPParseRIFF(&buf, &buf_size, &riff_size) != VP8_STATUS_OK) {
    return VP8_STATUS_BITSTREAM_ERROR;  // Wrong RIFF Header.
  }

  // Skip over VP8X header.
  status = WebPParseVP8X(&buf, &buf_size, &vp8x_skip_size, NULL, NULL, NULL);
  if (status != VP8_STATUS_OK) {
    return status;  // Wrong VP8X Chunk / Insufficient data.
  }
  if (vp8x_skip_size > 0) {
    // Skip over optional chunks.
    status = WebPParseOptionalChunks(&buf, &buf_size, riff_size,
                                     &optional_data_size);
    if (status != VP8_STATUS_OK) {
      return status;  // Found an invalid chunk size / Insufficient data.
    }
  }

  // Skip over VP8 chunk header.
  status = WebPParseVP8Header(&buf, &buf_size, riff_size, &vp8_skip_size,
                              &vp8_size_tmp);
  if (status != VP8_STATUS_OK) {
    return status;  // Invalid VP8 header / Insufficient data.
  }
  if (vp8_skip_size > 0) {
    *vp8_size = vp8_size_tmp;
  }

  *bytes_skipped = buf - *data;
  assert(*bytes_skipped == *data_size - buf_size);
  *data = buf;
  *data_size = buf_size;
  return VP8_STATUS_OK;
}

//------------------------------------------------------------------------------
// WebPDecParams

void WebPResetDecParams(WebPDecParams* const params) {
  if (params) {
    memset(params, 0, sizeof(*params));
  }
}

//------------------------------------------------------------------------------
// "Into" decoding variants

// Main flow
static VP8StatusCode DecodeInto(const uint8_t* data, uint32_t data_size,
                                WebPDecParams* const params) {
  VP8Decoder* dec = VP8New();
  VP8StatusCode status = VP8_STATUS_OK;
  VP8Io io;

  assert(params);
  if (dec == NULL) {
    return VP8_STATUS_INVALID_PARAM;
  }

  VP8InitIo(&io);
  io.data = data;
  io.data_size = data_size;
  WebPInitCustomIo(params, &io);  // Plug the I/O functions.

#ifdef WEBP_USE_THREAD
  dec->use_threads_ = params->options && (params->options->use_threads > 0);
#else
  dec->use_threads_ = 0;
#endif

  // Decode bitstream header, update io->width/io->height.
  if (!VP8GetHeaders(dec, &io)) {
    status = VP8_STATUS_BITSTREAM_ERROR;
  } else {
    // Allocate/check output buffers.
    status = WebPAllocateDecBuffer(io.width, io.height, params->options,
                                   params->output);
    if (status == VP8_STATUS_OK) {
      // Decode
      if (!VP8Decode(dec, &io)) {
        status = dec->status_;
      }
    }
  }
  VP8Delete(dec);
  if (status != VP8_STATUS_OK) {
    WebPFreeDecBuffer(params->output);
  }
  return status;
}

// Helpers
static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace,
                                     const uint8_t* data, uint32_t data_size,
                                     uint8_t* rgba, int stride, int size) {
  WebPDecParams params;
  WebPDecBuffer buf;
  if (rgba == NULL) {
    return NULL;
  }
  WebPInitDecBuffer(&buf);
  WebPResetDecParams(&params);
  params.output = &buf;
  buf.colorspace    = colorspace;
  buf.u.RGBA.rgba   = rgba;
  buf.u.RGBA.stride = stride;
  buf.u.RGBA.size   = size;
  buf.is_external_memory = 1;
  if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
    return NULL;
  }
  return rgba;
}

uint8_t* WebPDecodeRGBInto(const uint8_t* data, uint32_t data_size,
                           uint8_t* output, int size, int stride) {
  return DecodeIntoRGBABuffer(MODE_RGB, data, data_size, output, stride, size);
}

uint8_t* WebPDecodeRGBAInto(const uint8_t* data, uint32_t data_size,
                            uint8_t* output, int size, int stride) {
  return DecodeIntoRGBABuffer(MODE_RGBA, data, data_size, output, stride, size);
}

uint8_t* WebPDecodeARGBInto(const uint8_t* data, uint32_t data_size,
                            uint8_t* output, int size, int stride) {
  return DecodeIntoRGBABuffer(MODE_ARGB, data, data_size, output, stride, size);
}

uint8_t* WebPDecodeBGRInto(const uint8_t* data, uint32_t data_size,
                           uint8_t* output, int size, int stride) {
  return DecodeIntoRGBABuffer(MODE_BGR, data, data_size, output, stride, size);
}

uint8_t* WebPDecodeBGRAInto(const uint8_t* data, uint32_t data_size,
                            uint8_t* output, int size, int stride) {
  return DecodeIntoRGBABuffer(MODE_BGRA, data, data_size, output, stride, size);
}

uint8_t* WebPDecodeYUVInto(const uint8_t* data, uint32_t data_size,
                           uint8_t* luma, int luma_size, int luma_stride,
                           uint8_t* u, int u_size, int u_stride,
                           uint8_t* v, int v_size, int v_stride) {
  WebPDecParams params;
  WebPDecBuffer output;
  if (luma == NULL) return NULL;
  WebPInitDecBuffer(&output);
  WebPResetDecParams(&params);
  params.output = &output;
  output.colorspace      = MODE_YUV;
  output.u.YUVA.y        = luma;
  output.u.YUVA.y_stride = luma_stride;
  output.u.YUVA.y_size   = luma_size;
  output.u.YUVA.u        = u;
  output.u.YUVA.u_stride = u_stride;
  output.u.YUVA.u_size   = u_size;
  output.u.YUVA.v        = v;
  output.u.YUVA.v_stride = v_stride;
  output.u.YUVA.v_size   = v_size;
  output.is_external_memory = 1;
  if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
    return NULL;
  }
  return luma;
}

//------------------------------------------------------------------------------

static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* data,
                       uint32_t data_size, int* width, int* height,
                       WebPDecBuffer* keep_info) {
  WebPDecParams params;
  WebPDecBuffer output;

  WebPInitDecBuffer(&output);
  WebPResetDecParams(&params);
  params.output = &output;
  output.colorspace = mode;

  // Retrieve (and report back) the required dimensions from bitstream.
  if (!WebPGetInfo(data, data_size, &output.width, &output.height)) {
    return NULL;
  }
  if (width) *width = output.width;
  if (height) *height = output.height;

  // Decode
  if (DecodeInto(data, data_size, &params) != VP8_STATUS_OK) {
    return NULL;
  }
  if (keep_info) {    // keep track of the side-info
    WebPCopyDecBuffer(&output, keep_info);
  }
  // return decoded samples (don't clear 'output'!)
  return (mode >= MODE_YUV) ? output.u.YUVA.y : output.u.RGBA.rgba;
}

uint8_t* WebPDecodeRGB(const uint8_t* data, uint32_t data_size,
                       int* width, int* height) {
  return Decode(MODE_RGB, data, data_size, width, height, NULL);
}

uint8_t* WebPDecodeRGB565(const uint8_t* data, uint32_t data_size,
                       int* width, int* height) {
  return Decode(MODE_RGB_565, data, data_size, width, height, NULL);
}

uint8_t* WebPDecodeRGBA(const uint8_t* data, uint32_t data_size,
                        int* width, int* height) {
  return Decode(MODE_RGBA, data, data_size, width, height, NULL);
}

uint8_t* WebPDecodeARGB(const uint8_t* data, uint32_t data_size,
                        int* width, int* height) {
  return Decode(MODE_ARGB, data, data_size, width, height, NULL);
}

uint8_t* WebPDecodeBGR(const uint8_t* data, uint32_t data_size,
                       int* width, int* height) {
  return Decode(MODE_BGR, data, data_size, width, height, NULL);
}

uint8_t* WebPDecodeBGRA(const uint8_t* data, uint32_t data_size,
                        int* width, int* height) {
  return Decode(MODE_BGRA, data, data_size, width, height, NULL);
}

uint8_t* WebPDecodeYUV(const uint8_t* data, uint32_t data_size,
                       int* width, int* height, uint8_t** u, uint8_t** v,
                       int* stride, int* uv_stride) {
  WebPDecBuffer output;   // only to preserve the side-infos
  uint8_t* const out = Decode(MODE_YUV, data, data_size,
                              width, height, &output);

  if (out) {
    const WebPYUVABuffer* const buf = &output.u.YUVA;
    *u = buf->u;
    *v = buf->v;
    *stride = buf->y_stride;
    *uv_stride = buf->u_stride;
    assert(buf->u_stride == buf->v_stride);
  }
  return out;
}

static void DefaultFeatures(WebPBitstreamFeatures* const features) {
  assert(features);
  memset(features, 0, sizeof(*features));
  features->bitstream_version = 0;
}

static VP8StatusCode GetFeatures(const uint8_t* data, uint32_t data_size,
                                 WebPBitstreamFeatures* const features) {
  uint32_t vp8_chunk_size = 0;
  uint32_t riff_size = 0;
  uint32_t flags = 0;
  uint32_t vp8x_skip_size = 0;
  uint32_t vp8_skip_size = 0;
  VP8StatusCode status;

  if (features == NULL) {
    return VP8_STATUS_INVALID_PARAM;
  }
  DefaultFeatures(features);

  if (data == NULL) {
    return VP8_STATUS_INVALID_PARAM;
  }

  // Skip over RIFF header.
  status = WebPParseRIFF(&data, &data_size, &riff_size);
  if (status != VP8_STATUS_OK) {
    return status;   // Wrong RIFF Header / Insufficient data.
  }

  // Skip over VP8X.
  status = WebPParseVP8X(&data, &data_size, &vp8x_skip_size, &features->width,
                         &features->height, &flags);
  if (status != VP8_STATUS_OK) {
    return status;  // Wrong VP8X / insufficient data.

  }
  if (vp8x_skip_size > 0) {
    return VP8_STATUS_OK;  // Return features from VP8X header.
  }

  // Skip over VP8 header.
  status = WebPParseVP8Header(&data, &data_size, riff_size, &vp8_skip_size,
                              &vp8_chunk_size);
  if (status != VP8_STATUS_OK) {
    return status;  // Wrong VP8 Chunk-header / insufficient data.
  }
  if (vp8_skip_size == 0) {
    vp8_chunk_size = data_size;  // No VP8 chunk wrapper over raw VP8 data.
  }

  // Validates raw VP8 data.
  if (!VP8GetInfo(data, data_size, vp8_chunk_size,
                  &features->width, &features->height, &features->has_alpha)) {
    return VP8_STATUS_BITSTREAM_ERROR;
  }

  return VP8_STATUS_OK;  // Return features from VP8 header.
}

//------------------------------------------------------------------------------
// WebPGetInfo()

int WebPGetInfo(const uint8_t* data, uint32_t data_size,
                int* width, int* height) {
  WebPBitstreamFeatures features;

  if (GetFeatures(data, data_size, &features) != VP8_STATUS_OK) {
    return 0;
  }

  if (width) {
    *width  = features.width;
  }
  if (height) {
    *height = features.height;
  }

  return 1;
}

//------------------------------------------------------------------------------
// Advance decoding API

int WebPInitDecoderConfigInternal(WebPDecoderConfig* const config,
                                  int version) {
  if (version != WEBP_DECODER_ABI_VERSION) {
    return 0;   // version mismatch
  }
  if (config == NULL) {
    return 0;
  }
  memset(config, 0, sizeof(*config));
  DefaultFeatures(&config->input);
  WebPInitDecBuffer(&config->output);
  return 1;
}

VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, uint32_t data_size,
                                      WebPBitstreamFeatures* const features,
                                      int version) {
  VP8StatusCode status;
  if (version != WEBP_DECODER_ABI_VERSION) {
    return VP8_STATUS_INVALID_PARAM;   // version mismatch
  }
  if (features == NULL) {
    return VP8_STATUS_INVALID_PARAM;
  }

  status = GetFeatures(data, data_size, features);
  if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
    return VP8_STATUS_BITSTREAM_ERROR;  // Not enough data treated as error.
  }
  return status;
}

VP8StatusCode WebPDecode(const uint8_t* data, uint32_t data_size,
                         WebPDecoderConfig* const config) {
  WebPDecParams params;
  VP8StatusCode status;

  if (!config) {
    return VP8_STATUS_INVALID_PARAM;
  }

  status = GetFeatures(data, data_size, &config->input);
  if (status != VP8_STATUS_OK) {
    if (status == VP8_STATUS_NOT_ENOUGH_DATA) {
      return VP8_STATUS_BITSTREAM_ERROR;  // Not enough data treated as error.
    }
    return status;
  }

  WebPResetDecParams(&params);
  params.output = &config->output;
  params.options = &config->options;
  status = DecodeInto(data, data_size, &params);

  return status;
}

#if defined(__cplusplus) || defined(c_plusplus)
}    // extern "C"
#endif

Commits for Nextrek/Android/LibrerieNextrek/jni/src/dec/webp.c

Diff revisions: vs.
Revision Author Commited Message
4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000