Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: webrtc/modules/rtp_rtcp/source/h264_sps_parser_unittest.cc

Issue 1979443004: Add H264 bitstream rewriting to limit frame reordering marker in header (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: GN Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/modules/rtp_rtcp/source/h264_sps_parser.h"
12
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 #include "webrtc/base/arraysize.h"
16 #include "webrtc/base/bitbuffer.h"
17
18 namespace webrtc {
19
20 // Example SPS can be generated with ffmpeg. Here's an example set of commands,
21 // runnable on OS X:
22 // 1) Generate a video, from the camera:
23 // ffmpeg -f avfoundation -i "0" -video_size 640x360 camera.mov
24 //
25 // 2) Scale the video to the desired size:
26 // ffmpeg -i camera.mov -vf scale=640x360 scaled.mov
27 //
28 // 3) Get just the H.264 bitstream in AnnexB:
29 // ffmpeg -i scaled.mov -vcodec copy -vbsf h264_mp4toannexb -an out.h264
30 //
31 // 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,
33 // which should be stripped out before being passed to the parser.
34
35 static const size_t kSpsBufferMaxSize = 256;
36
37 // Generates a fake SPS with basically everything empty but the width/height.
38 // Pass in a buffer of at least kSpsBufferMaxSize.
39 // 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
41 // 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[]) {
43 uint8_t rbsp[kSpsBufferMaxSize] = {0};
44 rtc::BitBufferWriter writer(rbsp, kSpsBufferMaxSize);
45 // Profile byte.
46 writer.WriteUInt8(0);
47 // Constraint sets and reserved zero bits.
48 writer.WriteUInt8(0);
49 // level_idc.
50 writer.WriteUInt8(0x3u);
51 // seq_paramter_set_id.
52 writer.WriteExponentialGolomb(0);
53 // Profile is not special, so we skip all the chroma format settings.
54
55 // Now some bit magic.
56 // log2_max_frame_num_minus4: ue(v). 0 is fine.
57 writer.WriteExponentialGolomb(0);
58 // pic_order_cnt_type: ue(v). 0 is the type we want.
59 writer.WriteExponentialGolomb(0);
60 // log2_max_pic_order_cnt_lsb_minus4: ue(v). 0 is fine.
61 writer.WriteExponentialGolomb(0);
62 // max_num_ref_frames: ue(v). 0 is fine.
63 writer.WriteExponentialGolomb(0);
64 // gaps_in_frame_num_value_allowed_flag: u(1).
65 writer.WriteBits(0, 1);
66 // Next are width/height. First, calculate the mbs/map_units versions.
67 uint16_t width_in_mbs_minus1 = (width + 15) / 16 - 1;
68
69 // For the height, we're going to define frame_mbs_only_flag, so we need to
70 // divide by 2. See the parser for the full calculation.
71 uint16_t height_in_map_units_minus1 = ((height + 15) / 16 - 1) / 2;
72 // Write each as ue(v).
73 writer.WriteExponentialGolomb(width_in_mbs_minus1);
74 writer.WriteExponentialGolomb(height_in_map_units_minus1);
75 // frame_mbs_only_flag: u(1). Needs to be false.
76 writer.WriteBits(0, 1);
77 // mb_adaptive_frame_field_flag: u(1).
78 writer.WriteBits(0, 1);
79 // direct_8x8_inferene_flag: u(1).
80 writer.WriteBits(0, 1);
81 // frame_cropping_flag: u(1). 1, so we can supply crop.
82 writer.WriteBits(1, 1);
83 // Now we write the left/right/top/bottom crop. For simplicity, we'll put all
84 // the crop at the left/top.
85 // We picked a 4:2:0 format, so the crops are 1/2 the pixel crop values.
86 // Left/right.
87 writer.WriteExponentialGolomb(((16 - (width % 16)) % 16) / 2);
88 writer.WriteExponentialGolomb(0);
89 // Top/bottom.
90 writer.WriteExponentialGolomb(((16 - (height % 16)) % 16) / 2);
91 writer.WriteExponentialGolomb(0);
92
93 // Get the number of bytes written (including the last partial byte).
94 size_t byte_count, bit_offset;
95 writer.GetCurrentOffset(&byte_count, &bit_offset);
96 if (bit_offset > 0) {
97 byte_count++;
98 }
99
100 // Now, we need to write the rbsp into bytes. To do that, we'll need to add
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 }
118
119 TEST(H264SpsParserTest, TestSampleSPSHdLandscape) {
120 // SPS for a 1280x720 camera capture from ffmpeg on osx. Contains
121 // emulation bytes but no cropping.
122 const uint8_t buffer[] = {0x7A, 0x00, 0x1F, 0xBC, 0xD9, 0x40, 0x50, 0x05,
123 0xBA, 0x10, 0x00, 0x00, 0x03, 0x00, 0xC0, 0x00,
124 0x00, 0x2A, 0xE0, 0xF1, 0x83, 0x19, 0x60};
125 H264SpsParser parser = H264SpsParser(buffer, arraysize(buffer));
126 EXPECT_TRUE(parser.Parse());
127 EXPECT_EQ(1280u, parser.width());
128 EXPECT_EQ(720u, parser.height());
129 }
130
131 TEST(H264SpsParserTest, TestSampleSPSVgaLandscape) {
132 // SPS for a 640x360 camera capture from ffmpeg on osx. Contains emulation
133 // bytes and cropping (360 isn't divisible by 16).
134 const uint8_t buffer[] = {0x7A, 0x00, 0x1E, 0xBC, 0xD9, 0x40, 0xA0, 0x2F,
135 0xF8, 0x98, 0x40, 0x00, 0x00, 0x03, 0x01, 0x80,
136 0x00, 0x00, 0x56, 0x83, 0xC5, 0x8B, 0x65, 0x80};
137 H264SpsParser parser = H264SpsParser(buffer, arraysize(buffer));
138 EXPECT_TRUE(parser.Parse());
139 EXPECT_EQ(640u, parser.width());
140 EXPECT_EQ(360u, parser.height());
141 }
142
143 TEST(H264SpsParserTest, TestSampleSPSWeirdResolution) {
144 // SPS for a 200x400 camera capture from ffmpeg on osx. Horizontal and
145 // veritcal crop (neither dimension is divisible by 16).
146 const uint8_t buffer[] = {0x7A, 0x00, 0x0D, 0xBC, 0xD9, 0x43, 0x43, 0x3E,
147 0x5E, 0x10, 0x00, 0x00, 0x03, 0x00, 0x60, 0x00,
148 0x00, 0x15, 0xA0, 0xF1, 0x42, 0x99, 0x60};
149 H264SpsParser parser = H264SpsParser(buffer, arraysize(buffer));
150 EXPECT_TRUE(parser.Parse());
151 EXPECT_EQ(200u, parser.width());
152 EXPECT_EQ(400u, parser.height());
153 }
154
155 TEST(H264SpsParserTest, TestSyntheticSPSQvgaLandscape) {
156 uint8_t buffer[kSpsBufferMaxSize] = {0};
157 GenerateFakeSps(320u, 180u, buffer);
158 H264SpsParser parser = H264SpsParser(buffer, arraysize(buffer));
159 EXPECT_TRUE(parser.Parse());
160 EXPECT_EQ(320u, parser.width());
161 EXPECT_EQ(180u, parser.height());
162 }
163
164 TEST(H264SpsParserTest, TestSyntheticSPSWeirdResolution) {
165 uint8_t buffer[kSpsBufferMaxSize] = {0};
166 GenerateFakeSps(156u, 122u, buffer);
167 H264SpsParser parser = H264SpsParser(buffer, arraysize(buffer));
168 EXPECT_TRUE(parser.Parse());
169 EXPECT_EQ(156u, parser.width());
170 EXPECT_EQ(122u, parser.height());
171 }
172
173 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698