OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/rtp_rtcp/source/h264_sps_parser.h" | 11 #include "webrtc/common_video/h264/sps_parser.h" |
12 | 12 |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 #include "webrtc/base/arraysize.h" | 15 #include "webrtc/base/arraysize.h" |
16 #include "webrtc/base/bitbuffer.h" | 16 #include "webrtc/base/bitbuffer.h" |
| 17 #include "webrtc/base/buffer.h" |
| 18 #include "webrtc/common_video/h264/h264_common.h" |
17 | 19 |
18 namespace webrtc { | 20 namespace webrtc { |
19 | 21 |
20 // Example SPS can be generated with ffmpeg. Here's an example set of commands, | 22 // Example SPS can be generated with ffmpeg. Here's an example set of commands, |
21 // runnable on OS X: | 23 // runnable on OS X: |
22 // 1) Generate a video, from the camera: | 24 // 1) Generate a video, from the camera: |
23 // ffmpeg -f avfoundation -i "0" -video_size 640x360 camera.mov | 25 // ffmpeg -f avfoundation -i "0" -video_size 640x360 camera.mov |
24 // | 26 // |
25 // 2) Scale the video to the desired size: | 27 // 2) Scale the video to the desired size: |
26 // ffmpeg -i camera.mov -vf scale=640x360 scaled.mov | 28 // ffmpeg -i camera.mov -vf scale=640x360 scaled.mov |
27 // | 29 // |
28 // 3) Get just the H.264 bitstream in AnnexB: | 30 // 3) Get just the H.264 bitstream in AnnexB: |
29 // ffmpeg -i scaled.mov -vcodec copy -vbsf h264_mp4toannexb -an out.h264 | 31 // ffmpeg -i scaled.mov -vcodec copy -vbsf h264_mp4toannexb -an out.h264 |
30 // | 32 // |
31 // 4) Open out.h264 and find the SPS, generally everything between the first | 33 // 4) Open out.h264 and find the SPS, generally everything between the first |
32 // two start codes (0 0 0 1 or 0 0 1). The first byte should be 0x67, | 34 // two start codes (0 0 0 1 or 0 0 1). The first byte should be 0x67, |
33 // which should be stripped out before being passed to the parser. | 35 // which should be stripped out before being passed to the parser. |
34 | 36 |
35 static const size_t kSpsBufferMaxSize = 256; | 37 static const size_t kSpsBufferMaxSize = 256; |
36 | 38 |
37 // Generates a fake SPS with basically everything empty but the width/height. | 39 // Generates a fake SPS with basically everything empty but the width/height. |
38 // Pass in a buffer of at least kSpsBufferMaxSize. | 40 // Pass in a buffer of at least kSpsBufferMaxSize. |
39 // The fake SPS that this generates also always has at least one emulation byte | 41 // The fake SPS that this generates also always has at least one emulation byte |
40 // at offset 2, since the first two bytes are always 0, and has a 0x3 as the | 42 // at offset 2, since the first two bytes are always 0, and has a 0x3 as the |
41 // level_idc, to make sure the parser doesn't eat all 0x3 bytes. | 43 // level_idc, to make sure the parser doesn't eat all 0x3 bytes. |
42 void GenerateFakeSps(uint16_t width, uint16_t height, uint8_t buffer[]) { | 44 void GenerateFakeSps(uint16_t width, uint16_t height, rtc::Buffer* out_buffer) { |
43 uint8_t rbsp[kSpsBufferMaxSize] = {0}; | 45 uint8_t rbsp[kSpsBufferMaxSize] = {0}; |
44 rtc::BitBufferWriter writer(rbsp, kSpsBufferMaxSize); | 46 rtc::BitBufferWriter writer(rbsp, kSpsBufferMaxSize); |
45 // Profile byte. | 47 // Profile byte. |
46 writer.WriteUInt8(0); | 48 writer.WriteUInt8(0); |
47 // Constraint sets and reserved zero bits. | 49 // Constraint sets and reserved zero bits. |
48 writer.WriteUInt8(0); | 50 writer.WriteUInt8(0); |
49 // level_idc. | 51 // level_idc. |
50 writer.WriteUInt8(0x3u); | 52 writer.WriteUInt8(0x3u); |
51 // seq_paramter_set_id. | 53 // seq_paramter_set_id. |
52 writer.WriteExponentialGolomb(0); | 54 writer.WriteExponentialGolomb(0); |
(...skipping 30 matching lines...) Expand all Loading... |
83 // Now we write the left/right/top/bottom crop. For simplicity, we'll put all | 85 // Now we write the left/right/top/bottom crop. For simplicity, we'll put all |
84 // the crop at the left/top. | 86 // the crop at the left/top. |
85 // We picked a 4:2:0 format, so the crops are 1/2 the pixel crop values. | 87 // We picked a 4:2:0 format, so the crops are 1/2 the pixel crop values. |
86 // Left/right. | 88 // Left/right. |
87 writer.WriteExponentialGolomb(((16 - (width % 16)) % 16) / 2); | 89 writer.WriteExponentialGolomb(((16 - (width % 16)) % 16) / 2); |
88 writer.WriteExponentialGolomb(0); | 90 writer.WriteExponentialGolomb(0); |
89 // Top/bottom. | 91 // Top/bottom. |
90 writer.WriteExponentialGolomb(((16 - (height % 16)) % 16) / 2); | 92 writer.WriteExponentialGolomb(((16 - (height % 16)) % 16) / 2); |
91 writer.WriteExponentialGolomb(0); | 93 writer.WriteExponentialGolomb(0); |
92 | 94 |
| 95 // vui_parameters_present_flag: u(1) |
| 96 writer.WriteBits(0, 1); |
| 97 |
93 // Get the number of bytes written (including the last partial byte). | 98 // Get the number of bytes written (including the last partial byte). |
94 size_t byte_count, bit_offset; | 99 size_t byte_count, bit_offset; |
95 writer.GetCurrentOffset(&byte_count, &bit_offset); | 100 writer.GetCurrentOffset(&byte_count, &bit_offset); |
96 if (bit_offset > 0) { | 101 if (bit_offset > 0) { |
97 byte_count++; | 102 byte_count++; |
98 } | 103 } |
99 | 104 |
100 // Now, we need to write the rbsp into bytes. To do that, we'll need to add | 105 H264::WriteRbsp(rbsp, byte_count, out_buffer); |
101 // emulation 0x03 bytes if there's ever a sequence of 00 00 01 or 00 00 00 01. | |
102 // To be simple, just add a 0x03 after every 0x00. Extra emulation doesn't | |
103 // hurt. | |
104 for (size_t i = 0; i < byte_count;) { | |
105 // The -3 is intentional; we never need to write an emulation byte if the 00 | |
106 // is at the end. | |
107 if (i < byte_count - 3 && rbsp[i] == 0 && rbsp[i + 1] == 0) { | |
108 *buffer++ = rbsp[i]; | |
109 *buffer++ = rbsp[i + 1]; | |
110 *buffer++ = 0x3u; | |
111 i += 2; | |
112 } else { | |
113 *buffer++ = rbsp[i]; | |
114 ++i; | |
115 } | |
116 } | |
117 } | 106 } |
118 | 107 |
119 TEST(H264SpsParserTest, TestSampleSPSHdLandscape) { | 108 class H264SpsParserTest : public ::testing::Test { |
| 109 public: |
| 110 H264SpsParserTest() {} |
| 111 virtual ~H264SpsParserTest() {} |
| 112 |
| 113 rtc::Optional<SpsParser::SpsState> sps_; |
| 114 }; |
| 115 |
| 116 TEST_F(H264SpsParserTest, TestSampleSPSHdLandscape) { |
120 // SPS for a 1280x720 camera capture from ffmpeg on osx. Contains | 117 // SPS for a 1280x720 camera capture from ffmpeg on osx. Contains |
121 // emulation bytes but no cropping. | 118 // emulation bytes but no cropping. |
122 const uint8_t buffer[] = {0x7A, 0x00, 0x1F, 0xBC, 0xD9, 0x40, 0x50, 0x05, | 119 const uint8_t buffer[] = {0x7A, 0x00, 0x1F, 0xBC, 0xD9, 0x40, 0x50, 0x05, |
123 0xBA, 0x10, 0x00, 0x00, 0x03, 0x00, 0xC0, 0x00, | 120 0xBA, 0x10, 0x00, 0x00, 0x03, 0x00, 0xC0, 0x00, |
124 0x00, 0x2A, 0xE0, 0xF1, 0x83, 0x19, 0x60}; | 121 0x00, 0x2A, 0xE0, 0xF1, 0x83, 0x19, 0x60}; |
125 H264SpsParser parser = H264SpsParser(buffer, arraysize(buffer)); | 122 EXPECT_TRUE( |
126 EXPECT_TRUE(parser.Parse()); | 123 static_cast<bool>(sps_ = SpsParser::ParseSps(buffer, arraysize(buffer)))); |
127 EXPECT_EQ(1280u, parser.width()); | 124 EXPECT_EQ(1280u, sps_->width); |
128 EXPECT_EQ(720u, parser.height()); | 125 EXPECT_EQ(720u, sps_->height); |
129 } | 126 } |
130 | 127 |
131 TEST(H264SpsParserTest, TestSampleSPSVgaLandscape) { | 128 TEST_F(H264SpsParserTest, TestSampleSPSVgaLandscape) { |
132 // SPS for a 640x360 camera capture from ffmpeg on osx. Contains emulation | 129 // SPS for a 640x360 camera capture from ffmpeg on osx. Contains emulation |
133 // bytes and cropping (360 isn't divisible by 16). | 130 // bytes and cropping (360 isn't divisible by 16). |
134 const uint8_t buffer[] = {0x7A, 0x00, 0x1E, 0xBC, 0xD9, 0x40, 0xA0, 0x2F, | 131 const uint8_t buffer[] = {0x7A, 0x00, 0x1E, 0xBC, 0xD9, 0x40, 0xA0, 0x2F, |
135 0xF8, 0x98, 0x40, 0x00, 0x00, 0x03, 0x01, 0x80, | 132 0xF8, 0x98, 0x40, 0x00, 0x00, 0x03, 0x01, 0x80, |
136 0x00, 0x00, 0x56, 0x83, 0xC5, 0x8B, 0x65, 0x80}; | 133 0x00, 0x00, 0x56, 0x83, 0xC5, 0x8B, 0x65, 0x80}; |
137 H264SpsParser parser = H264SpsParser(buffer, arraysize(buffer)); | 134 EXPECT_TRUE( |
138 EXPECT_TRUE(parser.Parse()); | 135 static_cast<bool>(sps_ = SpsParser::ParseSps(buffer, arraysize(buffer)))); |
139 EXPECT_EQ(640u, parser.width()); | 136 EXPECT_EQ(640u, sps_->width); |
140 EXPECT_EQ(360u, parser.height()); | 137 EXPECT_EQ(360u, sps_->height); |
141 } | 138 } |
142 | 139 |
143 TEST(H264SpsParserTest, TestSampleSPSWeirdResolution) { | 140 TEST_F(H264SpsParserTest, TestSampleSPSWeirdResolution) { |
144 // SPS for a 200x400 camera capture from ffmpeg on osx. Horizontal and | 141 // SPS for a 200x400 camera capture from ffmpeg on osx. Horizontal and |
145 // veritcal crop (neither dimension is divisible by 16). | 142 // veritcal crop (neither dimension is divisible by 16). |
146 const uint8_t buffer[] = {0x7A, 0x00, 0x0D, 0xBC, 0xD9, 0x43, 0x43, 0x3E, | 143 const uint8_t buffer[] = {0x7A, 0x00, 0x0D, 0xBC, 0xD9, 0x43, 0x43, 0x3E, |
147 0x5E, 0x10, 0x00, 0x00, 0x03, 0x00, 0x60, 0x00, | 144 0x5E, 0x10, 0x00, 0x00, 0x03, 0x00, 0x60, 0x00, |
148 0x00, 0x15, 0xA0, 0xF1, 0x42, 0x99, 0x60}; | 145 0x00, 0x15, 0xA0, 0xF1, 0x42, 0x99, 0x60}; |
149 H264SpsParser parser = H264SpsParser(buffer, arraysize(buffer)); | 146 EXPECT_TRUE( |
150 EXPECT_TRUE(parser.Parse()); | 147 static_cast<bool>(sps_ = SpsParser::ParseSps(buffer, arraysize(buffer)))); |
151 EXPECT_EQ(200u, parser.width()); | 148 EXPECT_EQ(200u, sps_->width); |
152 EXPECT_EQ(400u, parser.height()); | 149 EXPECT_EQ(400u, sps_->height); |
153 } | 150 } |
154 | 151 |
155 TEST(H264SpsParserTest, TestSyntheticSPSQvgaLandscape) { | 152 TEST_F(H264SpsParserTest, TestSyntheticSPSQvgaLandscape) { |
156 uint8_t buffer[kSpsBufferMaxSize] = {0}; | 153 rtc::Buffer buffer; |
157 GenerateFakeSps(320u, 180u, buffer); | 154 GenerateFakeSps(320u, 180u, &buffer); |
158 H264SpsParser parser = H264SpsParser(buffer, arraysize(buffer)); | 155 EXPECT_TRUE(static_cast<bool>( |
159 EXPECT_TRUE(parser.Parse()); | 156 sps_ = SpsParser::ParseSps(buffer.data(), buffer.size()))); |
160 EXPECT_EQ(320u, parser.width()); | 157 EXPECT_EQ(320u, sps_->width); |
161 EXPECT_EQ(180u, parser.height()); | 158 EXPECT_EQ(180u, sps_->height); |
162 } | 159 } |
163 | 160 |
164 TEST(H264SpsParserTest, TestSyntheticSPSWeirdResolution) { | 161 TEST_F(H264SpsParserTest, TestSyntheticSPSWeirdResolution) { |
165 uint8_t buffer[kSpsBufferMaxSize] = {0}; | 162 rtc::Buffer buffer; |
166 GenerateFakeSps(156u, 122u, buffer); | 163 GenerateFakeSps(156u, 122u, &buffer); |
167 H264SpsParser parser = H264SpsParser(buffer, arraysize(buffer)); | 164 EXPECT_TRUE(static_cast<bool>( |
168 EXPECT_TRUE(parser.Parse()); | 165 sps_ = SpsParser::ParseSps(buffer.data(), buffer.size()))); |
169 EXPECT_EQ(156u, parser.width()); | 166 EXPECT_EQ(156u, sps_->width); |
170 EXPECT_EQ(122u, parser.height()); | 167 EXPECT_EQ(122u, sps_->height); |
171 } | 168 } |
172 | 169 |
173 } // namespace webrtc | 170 } // namespace webrtc |
OLD | NEW |