ProfilePolicyConnectorFactory: Refactoring from Profile to BrowserContext.
[chromium-blink-merge.git] / media / video / h264_poc.cc
blobd414b1d84033379e9b084efdd6e02830f12b4834
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 #include <algorithm>
7 #include "base/logging.h"
8 #include "media/filters/h264_parser.h"
9 #include "media/video/h264_poc.h"
11 namespace media {
13 H264POC::H264POC() {
14 Reset();
17 H264POC::~H264POC() {
20 void H264POC::Reset() {
21 // It shouldn't be necessary to reset these values, but doing so will improve
22 // reproducibility for buggy streams.
23 ref_pic_order_cnt_msb_ = 0;
24 ref_pic_order_cnt_lsb_ = 0;
25 prev_frame_num_ = 0;
26 prev_frame_num_offset_ = 0;
29 // Check if a slice includes memory management control operation 5, which
30 // results in some |pic_order_cnt| state being cleared.
31 static bool HasMMCO5(const media::H264SliceHeader& slice_hdr) {
32 // Require that the frame actually has memory management control operations.
33 if (slice_hdr.nal_ref_idc == 0 ||
34 slice_hdr.idr_pic_flag ||
35 !slice_hdr.adaptive_ref_pic_marking_mode_flag) {
36 return false;
39 for (size_t i = 0; i < arraysize(slice_hdr.ref_pic_marking); i++) {
40 int32_t op = slice_hdr.ref_pic_marking[i].memory_mgmnt_control_operation;
41 if (op == 5)
42 return true;
44 // Stop at the end of the list.
45 if (op == 0)
46 return false;
49 // Should not get here, the list is always zero terminated.
50 return false;
53 bool H264POC::ComputePicOrderCnt(
54 const H264SPS* sps,
55 const H264SliceHeader& slice_hdr,
56 int32_t *pic_order_cnt) {
57 if (slice_hdr.field_pic_flag) {
58 DLOG(ERROR) << "Interlaced frames are not supported";
59 return false;
62 // TODO(sandersd): Handle |gaps_in_frame_num_value|.
63 if (prev_frame_num_ > 0 && prev_frame_num_ < slice_hdr.frame_num - 1) {
64 DLOG(ERROR) << "Gaps in frame_num are not supported";
65 return false;
68 bool mmco5 = HasMMCO5(slice_hdr);
69 int32_t max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
70 int32_t max_pic_order_cnt_lsb =
71 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
73 // Based on T-REC-H.264 8.2.1, "Decoding process for picture order
74 // count", available from http://www.itu.int/rec/T-REC-H.264.
76 // Reorganized slightly from spec pseudocode to handle MMCO5 when storing
77 // state instead of when loading it.
78 switch (sps->pic_order_cnt_type) {
79 case 0: {
80 int32_t prev_pic_order_cnt_msb = ref_pic_order_cnt_msb_;
81 int32_t prev_pic_order_cnt_lsb = ref_pic_order_cnt_lsb_;
83 // For an IDR picture, clear the state.
84 if (slice_hdr.idr_pic_flag) {
85 prev_pic_order_cnt_msb = 0;
86 prev_pic_order_cnt_lsb = 0;
89 // 8-3. Derive |pic_order_cnt_msb|, accounting for wrapping which is
90 // detected when |pic_order_cnt_lsb| increases or decreases by at
91 // least half of its maximum.
92 int32_t pic_order_cnt_msb;
93 if ((slice_hdr.pic_order_cnt_lsb < prev_pic_order_cnt_lsb) &&
94 (prev_pic_order_cnt_lsb - slice_hdr.pic_order_cnt_lsb >=
95 max_pic_order_cnt_lsb / 2)) {
96 pic_order_cnt_msb = prev_pic_order_cnt_msb + max_pic_order_cnt_lsb;
97 } else if ((slice_hdr.pic_order_cnt_lsb > prev_pic_order_cnt_lsb) &&
98 (slice_hdr.pic_order_cnt_lsb - prev_pic_order_cnt_lsb >
99 max_pic_order_cnt_lsb / 2)) {
100 pic_order_cnt_msb = prev_pic_order_cnt_msb - max_pic_order_cnt_lsb;
101 } else {
102 pic_order_cnt_msb = prev_pic_order_cnt_msb;
105 // 8-4, 8-5. Derive |top_field_order_count| and |bottom_field_order_cnt|
106 // (assuming no interlacing).
107 int32_t top_foc = pic_order_cnt_msb + slice_hdr.pic_order_cnt_lsb;
108 int32_t bottom_foc = top_foc + slice_hdr.delta_pic_order_cnt_bottom;
109 *pic_order_cnt = std::min(top_foc, bottom_foc);
111 // Store state.
112 prev_frame_num_ = slice_hdr.frame_num;
113 if (slice_hdr.nal_ref_idc != 0) {
114 if (mmco5) {
115 ref_pic_order_cnt_msb_ = 0;
116 ref_pic_order_cnt_lsb_ = top_foc;
117 } else {
118 ref_pic_order_cnt_msb_ = pic_order_cnt_msb;
119 ref_pic_order_cnt_lsb_ = slice_hdr.pic_order_cnt_lsb;
123 break;
126 case 1: {
127 // 8-6. Derive |frame_num_offset|.
128 int32_t frame_num_offset;
129 if (slice_hdr.idr_pic_flag)
130 frame_num_offset = 0;
131 else if (prev_frame_num_ > slice_hdr.frame_num)
132 frame_num_offset = prev_frame_num_offset_ + max_frame_num;
133 else
134 frame_num_offset = prev_frame_num_offset_;
136 // 8-7. Derive |abs_frame_num|.
137 int32_t abs_frame_num;
138 if (sps->num_ref_frames_in_pic_order_cnt_cycle != 0)
139 abs_frame_num = frame_num_offset + slice_hdr.frame_num;
140 else
141 abs_frame_num = 0;
143 if (slice_hdr.nal_ref_idc == 0 && abs_frame_num > 0)
144 abs_frame_num--;
146 // 8-9. Derive |expected_pic_order_cnt| (the |pic_order_cnt| indicated
147 // by the cycle described in the SPS).
148 int32_t expected_pic_order_cnt = 0;
149 if (abs_frame_num > 0) {
150 // 8-8. Derive pic_order_cnt_cycle_cnt and
151 // frame_num_in_pic_order_cnt_cycle.
152 // Moved inside 8-9 to avoid division when this check is not done.
153 if (sps->num_ref_frames_in_pic_order_cnt_cycle == 0) {
154 DLOG(ERROR) << "Invalid num_ref_frames_in_pic_order_cnt_cycle";
155 return false;
158 // H264Parser checks that num_ref_frames_in_pic_order_cnt_cycle < 255.
159 int32_t pic_order_cnt_cycle_cnt =
160 (abs_frame_num - 1) / sps->num_ref_frames_in_pic_order_cnt_cycle;
161 int32_t frame_num_in_pic_order_cnt_cycle =
162 (abs_frame_num - 1) % sps->num_ref_frames_in_pic_order_cnt_cycle;
164 // 8-9 continued.
165 expected_pic_order_cnt = pic_order_cnt_cycle_cnt *
166 sps->expected_delta_per_pic_order_cnt_cycle;
167 for (int32_t i = 0; i <= frame_num_in_pic_order_cnt_cycle; i++)
168 expected_pic_order_cnt += sps->offset_for_ref_frame[i];
170 if (slice_hdr.nal_ref_idc == 0)
171 expected_pic_order_cnt += sps->offset_for_non_ref_pic;
173 // 8-10. Derive |top_field_order_cnt| and |bottom_field_order_cnt|
174 // (assuming no interlacing).
175 int32_t top_foc = expected_pic_order_cnt + slice_hdr.delta_pic_order_cnt0;
176 int32_t bottom_foc = top_foc + sps->offset_for_top_to_bottom_field +
177 slice_hdr.delta_pic_order_cnt1;
178 *pic_order_cnt = std::min(top_foc, bottom_foc);
180 // Store state.
181 prev_frame_num_ = slice_hdr.frame_num;
182 prev_frame_num_offset_ = frame_num_offset;
183 if (mmco5)
184 prev_frame_num_offset_ = 0;
186 break;
189 case 2: {
190 // 8-11. Derive |frame_num_offset|.
191 int32_t frame_num_offset;
192 if (slice_hdr.idr_pic_flag)
193 frame_num_offset = 0;
194 else if (prev_frame_num_ > slice_hdr.frame_num)
195 frame_num_offset = prev_frame_num_offset_ + max_frame_num;
196 else
197 frame_num_offset = prev_frame_num_offset_;
199 // 8-12, 8-13. Derive |temp_pic_order_count| (it's always the
200 // |pic_order_cnt|, regardless of interlacing).
201 if (slice_hdr.idr_pic_flag)
202 *pic_order_cnt = 0;
203 else if (slice_hdr.nal_ref_idc == 0)
204 *pic_order_cnt = 2 * (frame_num_offset + slice_hdr.frame_num) - 1;
205 else
206 *pic_order_cnt = 2 * (frame_num_offset + slice_hdr.frame_num);
208 // Store state.
209 prev_frame_num_ = slice_hdr.frame_num;
210 prev_frame_num_offset_ = frame_num_offset;
211 if (mmco5)
212 prev_frame_num_offset_ = 0;
214 break;
217 default:
218 DLOG(ERROR) << "Invalid pic_order_cnt_type: " << sps->pic_order_cnt_type;
219 return false;
222 return true;
225 } // namespace media