1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Routines for encoding and decoding a small number of bits into an image
6 // in a way that is decodable even after scaling/encoding/cropping.
8 // The encoding is very simple:
10 // #### #### ######## #### #### ####
11 // #### #### ######## #### #### ####
12 // #### #### ######## #### #### ####
13 // #### #### ######## #### #### ####
14 // 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 // <-----start----><--one-bit-><-zero bit-><----stop---->
17 // We use a basic unit, depicted here as four characters wide.
18 // We start with 1u black 1u white 1u black 1u white. (1-4 above)
19 // From there on, a "one" bit is encoded as 2u black and 1u white,
20 // and a zero bit is encoded as 1u black and 2u white. After
21 // all the bits we end the pattern with the same pattern as the
22 // start of the pattern.
27 #include "base/logging.h"
28 #include "media/base/video_frame.h"
29 #include "media/cast/test/utility/barcode.h"
35 const int kBlackThreshold
= 256 * 2 / 3;
36 const int kWhiteThreshold
= 256 / 3;
38 bool EncodeBarcode(const std::vector
<bool>& bits
,
39 scoped_refptr
<VideoFrame
> output_frame
) {
40 DCHECK(output_frame
->format() == PIXEL_FORMAT_YV12
||
41 output_frame
->format() == PIXEL_FORMAT_YV16
||
42 output_frame
->format() == PIXEL_FORMAT_I420
);
43 int row_bytes
= output_frame
->row_bytes(VideoFrame::kYPlane
);
44 std::vector
<unsigned char> bytes(row_bytes
);
45 for (int i
= 0; i
< row_bytes
; i
++) {
48 size_t units
= bits
.size() * 3 + 7; // White or black bar where size matters.
49 // We only use 60% of the image to make sure it works even if
50 // the image gets cropped.
51 size_t unit_size
= row_bytes
* 6 / 10 / units
;
52 if (unit_size
< 1) return false;
53 size_t bytes_required
= unit_size
* units
;
54 size_t padding
= (row_bytes
- bytes_required
) / 2;
55 unsigned char *pos
= &bytes
[padding
];
56 // Two leading black bars.
57 memset(pos
, 0, unit_size
);
59 memset(pos
, 0, unit_size
);
61 for (size_t bit
= 0; bit
< bits
.size(); bit
++) {
62 memset(pos
, 0, bits
[bit
] ? unit_size
* 2: unit_size
);
65 memset(pos
, 0, unit_size
);
67 memset(pos
, 0, unit_size
);
69 DCHECK_LE(pos
- &bytes
.front(), row_bytes
);
71 // Now replicate this one row into all rows in kYPlane.
72 for (int row
= 0; row
< output_frame
->rows(VideoFrame::kYPlane
); row
++) {
73 memcpy(output_frame
->data(VideoFrame::kYPlane
) +
74 output_frame
->stride(VideoFrame::kYPlane
) * row
,
82 bool DecodeBarCodeRows(const scoped_refptr
<VideoFrame
>& frame
,
83 std::vector
<bool>* output
,
86 // Do a basic run-length encoding
90 for (int pos
= 0; pos
< frame
->row_bytes(VideoFrame::kYPlane
); pos
++) {
92 for (int row
= min_row
; row
< max_row
; row
++) {
93 value
+= frame
->data(VideoFrame::kYPlane
)[
94 frame
->stride(VideoFrame::kYPlane
) * row
+ pos
];
96 value
/= max_row
- min_row
;
97 if (is_black
? value
> kBlackThreshold
: value
< kWhiteThreshold
) {
99 runs
.push_back(length
);
105 runs
.push_back(length
);
107 // Try decoding starting at each white-black transition.
108 while (runs
.size() >= output
->size() * 2 + 7) {
109 std::deque
<int>::const_iterator i
= runs
.begin();
110 double unit_size
= (i
[1] + i
[2] + i
[3] + i
[4]) / 4;
112 if (i
[0] > unit_size
* 2 || i
[0] < unit_size
/ 2) valid
= false;
113 if (i
[1] > unit_size
* 2 || i
[1] < unit_size
/ 2) valid
= false;
114 if (i
[2] > unit_size
* 2 || i
[2] < unit_size
/ 2) valid
= false;
115 if (i
[3] > unit_size
* 2 || i
[3] < unit_size
/ 2) valid
= false;
117 for (size_t bit
= 0; valid
&& bit
< output
->size(); bit
++) {
118 if (i
[0] > unit_size
/ 2 && i
[0] <= unit_size
* 1.5 &&
119 i
[1] > unit_size
* 1.5 && i
[1] <= unit_size
* 3) {
120 (*output
)[bit
] = false;
121 } else if (i
[1] > unit_size
/ 2 && i
[1] <= unit_size
* 1.5 &&
122 i
[0] > unit_size
* 1.5 && i
[0] <= unit_size
* 3) {
123 (*output
)[bit
] = true;
130 if (i
[0] > unit_size
* 2 || i
[0] < unit_size
/ 2) valid
= false;
131 if (i
[1] > unit_size
* 2 || i
[1] < unit_size
/ 2) valid
= false;
132 if (i
[2] > unit_size
* 2 || i
[2] < unit_size
/ 2) valid
= false;
134 DCHECK(i
<= runs
.end());
136 // Decoding successful, return true
147 // Note that "output" is assumed to be the right size already. This
148 // could be inferred from the data, but the decoding is more robust
149 // if we can assume that we know how many bits we want.
150 bool DecodeBarcode(const scoped_refptr
<VideoFrame
>& frame
,
151 std::vector
<bool>* output
) {
152 DCHECK(frame
->format() == PIXEL_FORMAT_YV12
||
153 frame
->format() == PIXEL_FORMAT_YV16
||
154 frame
->format() == PIXEL_FORMAT_I420
);
155 int rows
= frame
->rows(VideoFrame::kYPlane
);
157 if (DecodeBarCodeRows(frame
,
159 std::max(0, rows
/ 2 - 5),
160 std::min(rows
, rows
/ 2 + 5))) {
165 if (DecodeBarCodeRows(frame
, output
, 0, std::min(5, rows
))) {