summaryrefslogtreecommitdiffstats
path: root/media/webm/webm_parser.cc
blob: 083ebffab6c8c3816296db9c59b8611f6761a8ea (plain)
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/webm/webm_parser.h"

// This file contains code to parse WebM file elements. It was created
// from information in the Matroska spec.
// http://www.matroska.org/technical/specs/index.html

#include <iomanip>

#include "base/logging.h"
#include "media/webm/webm_constants.h"

namespace media {

// Maximum depth of WebM elements. Some WebM elements are lists of
// other elements. This limits the number levels of recursion allowed.
static const int kMaxLevelDepth = 6;

enum ElementType {
  LIST,
  UINT,
  FLOAT,
  BINARY,
  STRING,
  SBLOCK,
  SKIP,
};

struct ElementIdInfo {
  int level_;
  ElementType type_;
  int id_;
};

struct ListElementInfo {
  int id_;
  const ElementIdInfo* id_info_;
  int id_info_size_;
};

// The following are tables indicating what IDs are valid sub-elements
// of particular elements. If an element is encountered that doesn't
// appear in the list, a parsing error is signalled. Some elements are
// marked as SKIP because they are valid, but we don't care about them
// right now.
static const ElementIdInfo kClusterIds[] = {
  {2, UINT, kWebMIdTimecode},
  {2, SBLOCK, kWebMIdSimpleBlock},
  {2, LIST, kWebMIdBlockGroup},
};

static const ElementIdInfo kInfoIds[] = {
  {2, SKIP, kWebMIdSegmentUID},
  {2, UINT, kWebMIdTimecodeScale},
  {2, FLOAT, kWebMIdDuration},
  {2, SKIP, kWebMIdDateUTC},
  {2, SKIP, kWebMIdTitle},
  {2, SKIP, kWebMIdMuxingApp},
  {2, SKIP, kWebMIdWritingApp},
};

static const ElementIdInfo kTracksIds[] = {
  {2, LIST, kWebMIdTrackEntry},
};

static const ElementIdInfo kTrackEntryIds[] = {
  {3, UINT, kWebMIdTrackNumber},
  {3, SKIP, kWebMIdTrackUID},
  {3, UINT, kWebMIdTrackType},
  {3, SKIP, kWebMIdFlagEnabled},
  {3, SKIP, kWebMIdFlagDefault},
  {3, SKIP, kWebMIdFlagForced},
  {3, UINT, kWebMIdFlagLacing},
  {3, UINT, kWebMIdDefaultDuration},
  {3, SKIP, kWebMIdName},
  {3, SKIP, kWebMIdLanguage},
  {3, STRING, kWebMIdCodecID},
  {3, BINARY, kWebMIdCodecPrivate},
  {3, SKIP, kWebMIdCodecName},
  {3, LIST, kWebMIdVideo},
  {3, LIST, kWebMIdAudio},
};

static const ElementIdInfo kVideoIds[] = {
  {4, SKIP, kWebMIdFlagInterlaced},
  {4, SKIP, kWebMIdStereoMode},
  {4, UINT, kWebMIdPixelWidth},
  {4, UINT, kWebMIdPixelHeight},
  {4, SKIP, kWebMIdPixelCropBottom},
  {4, SKIP, kWebMIdPixelCropTop},
  {4, SKIP, kWebMIdPixelCropLeft},
  {4, SKIP, kWebMIdPixelCropRight},
  {4, SKIP, kWebMIdDisplayWidth},
  {4, SKIP, kWebMIdDisplayHeight},
  {4, SKIP, kWebMIdDisplayUnit},
  {4, SKIP, kWebMIdAspectRatioType},
};

static const ElementIdInfo kAudioIds[] = {
  {4, SKIP, kWebMIdSamplingFrequency},
  {4, SKIP, kWebMIdOutputSamplingFrequency},
  {4, UINT, kWebMIdChannels},
  {4, SKIP, kWebMIdBitDepth},
};

static const ElementIdInfo kClustersOnly[] = {
  {1, LIST, kWebMIdCluster},
};

static const ListElementInfo kListElementInfo[] = {
  { kWebMIdCluster,    kClusterIds,    sizeof(kClusterIds) },
  { kWebMIdInfo,       kInfoIds,       sizeof(kInfoIds) },
  { kWebMIdTracks,     kTracksIds,     sizeof(kTracksIds) },
  { kWebMIdTrackEntry, kTrackEntryIds, sizeof(kTrackEntryIds) },
  { kWebMIdVideo,      kVideoIds,      sizeof(kVideoIds) },
  { kWebMIdAudio,      kAudioIds,      sizeof(kAudioIds) },
};

// Number of elements in kListElementInfo.
const int kListElementInfoCount =
    sizeof(kListElementInfo) / sizeof(ListElementInfo);

WebMParserClient::~WebMParserClient() {}

// Parses an element header id or size field. These fields are variable length
// encoded. The first byte indicates how many bytes the field occupies.
// |buf|  - The buffer to parse.
// |size| - The number of bytes in |buf|
// |max_bytes| - The maximum number of bytes the field can be. ID fields
//               set this to 4 & element size fields set this to 8. If the
//               first byte indicates a larger field size than this it is a
//               parser error.
// |mask_first_byte| - For element size fields the field length encoding bits
//                     need to be masked off. This parameter is true for
//                     element size fields and is false for ID field values.
//
// Returns: The number of bytes parsed on success. -1 on error.
static int ParseWebMElementHeaderField(const uint8* buf, int size,
                                       int max_bytes, bool mask_first_byte,
                                       int64* num) {
  DCHECK(buf);
  DCHECK(num);

  if (size <= 0)
    return -1;

  int mask = 0x80;
  uint8 ch = buf[0];
  int extra_bytes = -1;
  for (int i = 0; i < max_bytes; ++i) {
    if ((ch & mask) == mask) {
      *num = mask_first_byte ? ch & ~mask : ch;
      extra_bytes = i;
      break;
    }
    mask >>= 1;
  }

  if ((extra_bytes == -1) || ((1 + extra_bytes) > size))
    return -1;

  int bytes_used = 1;

  for (int i = 0; i < extra_bytes; ++i)
    *num = (*num << 8) | (0xff & buf[bytes_used++]);

  return bytes_used;
}

// Parses an element header & returns the ID and element size.
//
// Returns: The number of bytes parsed on success. -1 on error.
// |*id| contains the element ID on success & undefined on error.
// |*element_size| contains the element size on success & undefined on error.
static int ParseWebMElementHeader(const uint8* buf, int size,
                                  int* id, int64* element_size) {
  DCHECK(buf);
  DCHECK_GE(size, 0);
  DCHECK(id);
  DCHECK(element_size);

  if (size == 0)
    return 0;

  int64 tmp = 0;
  int num_id_bytes = ParseWebMElementHeaderField(buf, size, 4, false, &tmp);

  if (num_id_bytes <= 0)
    return num_id_bytes;

  *id = static_cast<int>(tmp);

  int num_size_bytes = ParseWebMElementHeaderField(buf + num_id_bytes,
                                                   size - num_id_bytes,
                                                   8, true, &tmp);

  if (num_size_bytes <= 0)
    return num_size_bytes;

  *element_size = tmp;
  return num_id_bytes + num_size_bytes;
}

// Finds ElementIdInfo for a specific ID.
static const ElementIdInfo* FindIdInfo(int id,
                                       const ElementIdInfo* id_info,
                                       int id_info_size) {
  int count = id_info_size / sizeof(*id_info);
  for (int i = 0; i < count; ++i) {
    if (id == id_info[i].id_)
      return &id_info[i];
  }

  return NULL;
}

// Finds ListElementInfo for a specific ID.
static const ListElementInfo* FindListInfo(int id) {
  for (int i = 0; i < kListElementInfoCount; ++i) {
    if (id == kListElementInfo[i].id_)
      return &kListElementInfo[i];
  }

  return NULL;
}

static int ParseSimpleBlock(const uint8* buf, int size,
                            WebMParserClient* client) {
  if (size < 4)
    return -1;

  // Return an error if the trackNum > 127. We just aren't
  // going to support large track numbers right now.
  if ((buf[0] & 0x80) != 0x80) {
    VLOG(1) << "TrackNumber over 127 not supported";
    return -1;
  }

  int track_num = buf[0] & 0x7f;
  int timecode = buf[1] << 8 | buf[2];
  int flags = buf[3] & 0xff;
  int lacing = (flags >> 1) & 0x3;

  if (lacing != 0) {
    VLOG(1) << "Lacing " << lacing << " not supported yet.";
    return -1;
  }

  // Sign extend negative timecode offsets.
  if (timecode & 0x8000)
    timecode |= (-1 << 16);

  const uint8* frame_data = buf + 4;
  int frame_size = size - (frame_data - buf);
  if (!client->OnSimpleBlock(track_num, timecode, flags,
                             frame_data, frame_size)) {
    return -1;
  }

  return size;
}

static int ParseElements(const ElementIdInfo* id_info,
                         int id_info_size,
                         const uint8* buf, int size, int level,
                         WebMParserClient* client);

static int ParseElementList(const uint8* buf, int size,
                            int id, int level,
                            WebMParserClient* client) {
  const ListElementInfo* list_info = FindListInfo(id);

  if (!list_info) {
    VLOG(1) << "Failed to find list info for ID " << std::hex << id;
    return -1;
  }

  if (!client->OnListStart(id))
    return -1;

  int res = ParseElements(list_info->id_info_,
                          list_info->id_info_size_,
                          buf, size,
                          level + 1,
                          client);

  if (res < 0)
    return -1;

  if (!client->OnListEnd(id))
    return -1;

  DCHECK_EQ(res, size);
  return res;
}

static int ParseUInt(const uint8* buf, int size, int id,
                     WebMParserClient* client) {
  if ((size <= 0) || (size > 8))
    return -1;

  // Read in the big-endian integer.
  int64 value = 0;
  for (int i = 0; i < size; ++i)
    value = (value << 8) | buf[i];

  if (!client->OnUInt(id, value))
    return -1;

  return size;
}

static int ParseFloat(const uint8* buf, int size, int id,
                      WebMParserClient* client) {

  if ((size != 4) && (size != 8))
    return -1;

  double value = -1;

  // Read the bytes from big-endian form into a native endian integer.
  int64 tmp = 0;
  for (int i = 0; i < size; ++i)
    tmp = (tmp << 8) | buf[i];

  // Use a union to convert the integer bit pattern into a floating point
  // number.
  if (size == 4) {
    union {
      int32 src;
      float dst;
    } tmp2;
    tmp2.src = static_cast<int32>(tmp);
    value = tmp2.dst;
  } else if (size == 8) {
    union {
      int64 src;
      double dst;
    } tmp2;
    tmp2.src = tmp;
    value = tmp2.dst;
  } else {
    return -1;
  }

  if (!client->OnFloat(id, value))
    return -1;

  return size;
}

static int ParseElements(const ElementIdInfo* id_info,
                         int id_info_size,
                         const uint8* buf, int size, int level,
                         WebMParserClient* client) {
  DCHECK_GE(id_info_size, 0);
  DCHECK_GE(size, 0);
  DCHECK_GE(level, 0);

  const uint8* cur = buf;
  int cur_size = size;
  int used = 0;

  if (level > kMaxLevelDepth)
    return -1;

  while (cur_size > 0) {
    int id = 0;
    int64 element_size = 0;
    int res = ParseWebMElementHeader(cur, cur_size, &id, &element_size);

    if (res < 0)
      return res;

    if (res == 0)
      break;

    cur += res;
    cur_size -= res;
    used += res;

    // Check to see if the element is larger than the remaining data.
    if (element_size > cur_size)
      return -1;

    const ElementIdInfo* info = FindIdInfo(id, id_info, id_info_size);

    if (info == NULL) {
      VLOG(1) << "No info for ID " << std::hex << id;

      // TODO(acolwell): Change this to return -1 after the API has solidified.
      // We don't want to allow elements we don't recognize.
      cur += element_size;
      cur_size -= element_size;
      used += element_size;
      continue;
    }

    if (info->level_ != level) {
      VLOG(1) << "ID " << std::hex << id << std::dec << " at level "
              << level << " instead of " << info->level_;
      return -1;
    }

    switch(info->type_) {
      case SBLOCK:
        if (ParseSimpleBlock(cur, element_size, client) <= 0)
          return -1;
        break;
      case LIST:
        if (ParseElementList(cur, element_size, id, level, client) < 0)
          return -1;
        break;
      case UINT:
        if (ParseUInt(cur, element_size, id, client) <= 0)
          return -1;
        break;
      case FLOAT:
        if (ParseFloat(cur, element_size, id, client) <= 0)
          return -1;
        break;
      case BINARY:
        if (!client->OnBinary(id, cur, element_size))
          return -1;
        break;
      case STRING:
        if (!client->OnString(id,
                              std::string(reinterpret_cast<const char*>(cur),
                                          element_size)))
          return -1;
        break;
      case SKIP:
        // Do nothing.
        break;
      default:
        VLOG(1) << "Unhandled id type " << info->type_;
        return -1;
    };

    cur += element_size;
    cur_size -= element_size;
    used += element_size;
  }

  return used;
}

// Parses a single list element that matches |id|. This method fails if the
// buffer points to an element that does not match |id|.
int WebMParseListElement(const uint8* buf, int size, int id,
                         int level, WebMParserClient* client) {
  if (size == 0)
    return -1;

  const uint8* cur = buf;
  int cur_size = size;

  int element_id = 0;
  int64 element_size = 0;
  int res = ParseWebMElementHeader(cur, cur_size, &element_id, &element_size);

  if (res <= 0)
    return res;

  cur += res;
  cur_size -= res;

  if (element_id != id || element_size > cur_size)
    return -1;

  res = ParseElementList(cur, element_size, element_id, level, client);

  if (res < 0)
    return -1;

  cur += res;
  cur_size -= res;

  return size - cur_size;
}

}  // namespace media