summaryrefslogtreecommitdiffstats
path: root/dom/media/webrtc/common/YuvStamper.cpp
blob: b0c84269831f49d59a3e3fd1b97c1139d06a3692 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifdef HAVE_NETINET_IN_H
#  include <netinet/in.h>
#elif defined XP_WIN
#  include <winsock2.h>
#endif
#include <string.h>

#include "nspr.h"
#include "YuvStamper.h"
#include "mozilla/Sprintf.h"

typedef uint32_t UINT4;  // Needed for r_crc32() call
extern "C" {
#include "r_crc32.h"
}

namespace mozilla {

#define ON_5 0x20
#define ON_4 0x10
#define ON_3 0x08
#define ON_2 0x04
#define ON_1 0x02
#define ON_0 0x01

/*
  0, 0, 1, 1, 0, 0,
  0, 1, 0, 0, 1, 0,
  1, 0, 0, 0, 0, 1,
  1, 0, 0, 0, 0, 1,
  1, 0, 0, 0, 0, 1,
  0, 1, 0, 0, 1, 0,
  0, 0, 1, 1, 0, 0
*/
static unsigned char DIGIT_0[] = {ON_3 | ON_2, ON_4 | ON_1, ON_5 | ON_0,
                                  ON_5 | ON_0, ON_5 | ON_0, ON_4 | ON_1,
                                  ON_3 | ON_2};

/*
  0, 0, 0, 1, 0, 0,
  0, 0, 0, 1, 0, 0,
  0, 0, 0, 1, 0, 0,
  0, 0, 0, 1, 0, 0,
  0, 0, 0, 1, 0, 0,
  0, 0, 0, 1, 0, 0,
  0, 0, 0, 1, 0, 0,
*/
static unsigned char DIGIT_1[] = {ON_2, ON_2, ON_2, ON_2, ON_2, ON_2, ON_2};

/*
  1, 1, 1, 1, 1, 0,
  0, 0, 0, 0, 0, 1,
  0, 0, 0, 0, 0, 1,
  0, 1, 1, 1, 1, 0,
  1, 0, 0, 0, 0, 0,
  1, 0, 0, 0, 0, 0,
  0, 1, 1, 1, 1, 1,
*/
static unsigned char DIGIT_2[] = {
    ON_5 | ON_4 | ON_3 | ON_2 | ON_1, ON_0, ON_0,
    ON_4 | ON_3 | ON_2 | ON_1,        ON_5, ON_5,
    ON_4 | ON_3 | ON_2 | ON_1 | ON_0,
};

/*
  1, 1, 1, 1, 1, 0,
  0, 0, 0, 0, 0, 1,
  0, 0, 0, 0, 0, 1,
  0, 1, 1, 1, 1, 1,
  0, 0, 0, 0, 0, 1,
  0, 0, 0, 0, 0, 1,
  1, 1, 1, 1, 1, 0,
*/
static unsigned char DIGIT_3[] = {
    ON_5 | ON_4 | ON_3 | ON_2 | ON_1, ON_0, ON_0,
    ON_4 | ON_3 | ON_2 | ON_1 | ON_0, ON_0, ON_0,
    ON_5 | ON_4 | ON_3 | ON_2 | ON_1,
};

/*
  0, 1, 0, 0, 0, 1,
  0, 1, 0, 0, 0, 1,
  0, 1, 0, 0, 0, 1,
  0, 1, 1, 1, 1, 1,
  0, 0, 0, 0, 0, 1,
  0, 0, 0, 0, 0, 1,
  0, 0, 0, 0, 0, 1
*/
static unsigned char DIGIT_4[] = {
    ON_4 | ON_0, ON_4 | ON_0, ON_4 | ON_0, ON_4 | ON_3 | ON_2 | ON_1 | ON_0,
    ON_0,        ON_0,        ON_0,
};

/*
  0, 1, 1, 1, 1, 1,
  1, 0, 0, 0, 0, 0,
  1, 0, 0, 0, 0, 0,
  0, 1, 1, 1, 1, 0,
  0, 0, 0, 0, 0, 1,
  0, 0, 0, 0, 0, 1,
  1, 1, 1, 1, 1, 0,
*/
static unsigned char DIGIT_5[] = {
    ON_4 | ON_3 | ON_2 | ON_1 | ON_0, ON_5, ON_5,
    ON_4 | ON_3 | ON_2 | ON_1,        ON_0, ON_0,
    ON_5 | ON_4 | ON_3 | ON_2 | ON_1,
};

/*
  0, 1, 1, 1, 1, 1,
  1, 0, 0, 0, 0, 0,
  1, 0, 0, 0, 0, 0,
  1, 1, 1, 1, 1, 0,
  1, 0, 0, 0, 0, 1,
  1, 0, 0, 0, 0, 1,
  0, 1, 1, 1, 1, 0,
*/
static unsigned char DIGIT_6[] = {
    ON_4 | ON_3 | ON_2 | ON_1 | ON_0, ON_5,        ON_5,
    ON_4 | ON_3 | ON_2 | ON_1,        ON_5 | ON_0, ON_5 | ON_0,
    ON_4 | ON_3 | ON_2 | ON_1,
};

/*
  1, 1, 1, 1, 1, 1,
  0, 0, 0, 0, 0, 1,
  0, 0, 0, 0, 1, 0,
  0, 0, 0, 1, 0, 0,
  0, 0, 1, 0, 0, 0,
  0, 1, 0, 0, 0, 0,
  1, 0, 0, 0, 0, 0
*/
static unsigned char DIGIT_7[] = {ON_5 | ON_4 | ON_3 | ON_2 | ON_1 | ON_0,
                                  ON_0,
                                  ON_1,
                                  ON_2,
                                  ON_3,
                                  ON_4,
                                  ON_5};

/*
  0, 1, 1, 1, 1, 1,
  1, 0, 0, 0, 0, 1,
  1, 0, 0, 0, 0, 1,
  0, 1, 1, 1, 1, 0,
  1, 0, 0, 0, 0, 1,
  1, 0, 0, 0, 0, 1,
  0, 1, 1, 1, 1, 0
*/
static unsigned char DIGIT_8[] = {
    ON_4 | ON_3 | ON_2 | ON_1, ON_5 | ON_0, ON_5 | ON_0,
    ON_4 | ON_3 | ON_2 | ON_1, ON_5 | ON_0, ON_5 | ON_0,
    ON_4 | ON_3 | ON_2 | ON_1,
};

/*
  0, 1, 1, 1, 1, 1,
  1, 0, 0, 0, 0, 1,
  1, 0, 0, 0, 0, 1,
  0, 1, 1, 1, 1, 1,
  0, 0, 0, 0, 0, 1,
  0, 0, 0, 0, 0, 1,
  0, 1, 1, 1, 1, 0
*/
static unsigned char DIGIT_9[] = {
    ON_4 | ON_3 | ON_2 | ON_1 | ON_0, ON_5 | ON_0, ON_5 | ON_0,
    ON_4 | ON_3 | ON_2 | ON_1 | ON_0, ON_0,        ON_0,
    ON_4 | ON_3 | ON_2 | ON_1,
};

static unsigned char* DIGITS[] = {DIGIT_0, DIGIT_1, DIGIT_2, DIGIT_3, DIGIT_4,
                                  DIGIT_5, DIGIT_6, DIGIT_7, DIGIT_8, DIGIT_9};

YuvStamper::YuvStamper(unsigned char* pYData, uint32_t width, uint32_t height,
                       uint32_t stride, uint32_t x, uint32_t y,
                       unsigned char symbol_width, unsigned char symbol_height)
    : pYData(pYData),
      mStride(stride),
      mWidth(width),
      mHeight(height),
      mSymbolWidth(symbol_width),
      mSymbolHeight(symbol_height),
      mCursor(x, y) {}

bool YuvStamper::Encode(uint32_t width, uint32_t height, uint32_t stride,
                        unsigned char* pYData, unsigned char* pMsg,
                        size_t msg_len, uint32_t x, uint32_t y) {
  YuvStamper stamper(pYData, width, height, stride, x, y, sBitSize, sBitSize);

  // Reserve space for a checksum.
  if (stamper.Capacity() < 8 * (msg_len + sizeof(uint32_t))) {
    return false;
  }

  bool ok = false;
  uint32_t crc;
  unsigned char* pCrc = reinterpret_cast<unsigned char*>(&crc);
  r_crc32(reinterpret_cast<char*>(pMsg), (int)msg_len, &crc);
  crc = htonl(crc);

  while (msg_len-- > 0) {
    if (!stamper.Write8(*pMsg++)) {
      return false;
    }
  }

  // Add checksum after the message.
  ok = stamper.Write8(*pCrc++) && stamper.Write8(*pCrc++) &&
       stamper.Write8(*pCrc++) && stamper.Write8(*pCrc++);

  return ok;
}

bool YuvStamper::Decode(uint32_t width, uint32_t height, uint32_t stride,
                        unsigned char* pYData, unsigned char* pMsg,
                        size_t msg_len, uint32_t x, uint32_t y) {
  YuvStamper stamper(pYData, width, height, stride, x, y, sBitSize, sBitSize);

  unsigned char* ptr = pMsg;
  size_t len = msg_len;
  uint32_t crc, msg_crc;
  unsigned char* pCrc = reinterpret_cast<unsigned char*>(&crc);

  // Account for space reserved for the checksum
  if (stamper.Capacity() < 8 * (len + sizeof(uint32_t))) {
    return false;
  }

  while (len-- > 0) {
    if (!stamper.Read8(*ptr++)) {
      return false;
    }
  }

  if (!(stamper.Read8(*pCrc++) && stamper.Read8(*pCrc++) &&
        stamper.Read8(*pCrc++) && stamper.Read8(*pCrc++))) {
    return false;
  }

  r_crc32(reinterpret_cast<char*>(pMsg), (int)msg_len, &msg_crc);
  return crc == htonl(msg_crc);
}

inline uint32_t YuvStamper::Capacity() {
  // Enforce at least a symbol width and height offset from outer edges.
  if (mCursor.y + mSymbolHeight > mHeight) {
    return 0;
  }

  if (mCursor.x + mSymbolWidth > mWidth && !AdvanceCursor()) {
    return 0;
  }

  // Normalize frame integral to mSymbolWidth x mSymbolHeight
  uint32_t width = mWidth / mSymbolWidth;
  uint32_t height = mHeight / mSymbolHeight;
  uint32_t x = mCursor.x / mSymbolWidth;
  uint32_t y = mCursor.y / mSymbolHeight;

  return (width * height - width * y) - x;
}

bool YuvStamper::Write8(unsigned char value) {
  // Encode MSB to LSB.
  unsigned char mask = 0x80;
  while (mask) {
    if (!WriteBit(!!(value & mask))) {
      return false;
    }
    mask >>= 1;
  }
  return true;
}

bool YuvStamper::WriteBit(bool one) {
  // A bit is mapped to a mSymbolWidth x mSymbolHeight square of luma data
  // points. Don't use ternary op.:
  // https://bugzilla.mozilla.org/show_bug.cgi?id=1001708
  unsigned char value;
  if (one)
    value = sYOn;
  else
    value = sYOff;

  for (uint32_t y = 0; y < mSymbolHeight; y++) {
    for (uint32_t x = 0; x < mSymbolWidth; x++) {
      *(pYData + (mCursor.x + x) + ((mCursor.y + y) * mStride)) = value;
    }
  }

  return AdvanceCursor();
}

bool YuvStamper::AdvanceCursor() {
  mCursor.x += mSymbolWidth;
  if (mCursor.x + mSymbolWidth > mWidth) {
    // move to the start of the next row if possible.
    mCursor.y += mSymbolHeight;
    if (mCursor.y + mSymbolHeight > mHeight) {
      // end of frame, do not advance
      mCursor.y -= mSymbolHeight;
      mCursor.x -= mSymbolWidth;
      return false;
    } else {
      mCursor.x = 0;
    }
  }

  return true;
}

bool YuvStamper::Read8(unsigned char& value) {
  unsigned char octet = 0;
  unsigned char bit = 0;

  for (int i = 8; i > 0; --i) {
    if (!ReadBit(bit)) {
      return false;
    }
    octet <<= 1;
    octet |= bit;
  }

  value = octet;
  return true;
}

bool YuvStamper::ReadBit(unsigned char& bit) {
  uint32_t sum = 0;
  for (uint32_t y = 0; y < mSymbolHeight; y++) {
    for (uint32_t x = 0; x < mSymbolWidth; x++) {
      sum += *(pYData + mStride * (mCursor.y + y) + mCursor.x + x);
    }
  }

  // apply threshold to collected bit square
  bit = (sum > (sBitThreshold * mSymbolWidth * mSymbolHeight)) ? 1 : 0;
  return AdvanceCursor();
}

bool YuvStamper::WriteDigits(uint32_t value) {
  char buf[20];
  SprintfLiteral(buf, "%.5u", value);
  size_t size = strlen(buf);

  if (Capacity() < size) {
    return false;
  }

  for (size_t i = 0; i < size; ++i) {
    if (!WriteDigit(buf[i] - '0')) return false;
    if (!AdvanceCursor()) {
      return false;
    }
  }

  return true;
}

bool YuvStamper::WriteDigit(unsigned char digit) {
  if (digit > sizeof(DIGITS) / sizeof(DIGITS[0])) return false;

  unsigned char* dig = DIGITS[digit];
  for (uint32_t row = 0; row < sDigitHeight; ++row) {
    unsigned char mask = 0x01 << (sDigitWidth - 1);
    for (uint32_t col = 0; col < sDigitWidth; ++col, mask >>= 1) {
      if (dig[row] & mask) {
        for (uint32_t xx = 0; xx < sPixelSize; ++xx) {
          for (uint32_t yy = 0; yy < sPixelSize; ++yy) {
            WritePixel(pYData, mCursor.x + (col * sPixelSize) + xx,
                       mCursor.y + (row * sPixelSize) + yy);
          }
        }
      }
    }
  }

  return true;
}

void YuvStamper::WritePixel(unsigned char* data, uint32_t x, uint32_t y) {
  unsigned char* ptr = &data[y * mStride + x];
  // Don't use ternary op.: https://bugzilla.mozilla.org/show_bug.cgi?id=1001708
  if (*ptr > sLumaThreshold)
    *ptr = sLumaMin;
  else
    *ptr = sLumaMax;
}

}  // namespace mozilla.