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

Side by Side Diff: media/video/h264_poc.cc

Issue 2661423002: VTVDA: Optimize pic_order_cnt_type == 2. (Closed)
Patch Set: Update unit test. Created 3 years, 9 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
« no previous file with comments | « media/video/h264_poc.h ('k') | media/video/h264_poc_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "media/filters/h264_parser.h" 11 #include "media/filters/h264_parser.h"
12 #include "media/video/h264_poc.h" 12 #include "media/video/h264_poc.h"
13 13
14 namespace media { 14 namespace media {
15 15
16 H264POC::H264POC() { 16 namespace {
17 Reset();
18 }
19
20 H264POC::~H264POC() {
21 }
22
23 void H264POC::Reset() {
24 // It shouldn't be necessary to reset these values, but doing so will improve
25 // reproducibility for buggy streams.
26 ref_pic_order_cnt_msb_ = 0;
27 ref_pic_order_cnt_lsb_ = 0;
28 prev_frame_num_ = 0;
29 prev_frame_num_offset_ = 0;
30 }
31 17
32 // Check if a slice includes memory management control operation 5, which 18 // Check if a slice includes memory management control operation 5, which
33 // results in some |pic_order_cnt| state being cleared. 19 // results in some |pic_order_cnt| state being cleared.
34 static bool HasMMCO5(const media::H264SliceHeader& slice_hdr) { 20 bool HasMMCO5(const media::H264SliceHeader& slice_hdr) {
35 // Require that the frame actually has memory management control operations. 21 // Require that the frame actually has memory management control operations.
36 if (slice_hdr.nal_ref_idc == 0 || 22 if (slice_hdr.nal_ref_idc == 0 ||
37 slice_hdr.idr_pic_flag || 23 slice_hdr.idr_pic_flag ||
38 !slice_hdr.adaptive_ref_pic_marking_mode_flag) { 24 !slice_hdr.adaptive_ref_pic_marking_mode_flag) {
39 return false; 25 return false;
40 } 26 }
41 27
42 for (size_t i = 0; i < arraysize(slice_hdr.ref_pic_marking); i++) { 28 for (size_t i = 0; i < arraysize(slice_hdr.ref_pic_marking); i++) {
43 int32_t op = slice_hdr.ref_pic_marking[i].memory_mgmnt_control_operation; 29 int32_t op = slice_hdr.ref_pic_marking[i].memory_mgmnt_control_operation;
44 if (op == 5) 30 if (op == 5)
45 return true; 31 return true;
46 32
47 // Stop at the end of the list. 33 // Stop at the end of the list.
48 if (op == 0) 34 if (op == 0)
49 return false; 35 return false;
50 } 36 }
51 37
52 // Should not get here, the list is always zero terminated. 38 // Should not get here, the list is always zero terminated.
53 return false; 39 return false;
54 } 40 }
55 41
42 } // namespace
43
44 H264POC::H264POC() {
45 Reset();
46 }
47
48 H264POC::~H264POC() {}
49
50 void H264POC::Reset() {
51 // It shouldn't be necessary to reset these values, but doing so will improve
52 // reproducibility for buggy streams.
53 ref_pic_order_cnt_msb_ = 0;
54 ref_pic_order_cnt_lsb_ = 0;
55 prev_frame_num_ = 0;
56 prev_frame_num_offset_ = 0;
57 pending_mmco5_ = false;
58 }
59
56 bool H264POC::ComputePicOrderCnt( 60 bool H264POC::ComputePicOrderCnt(
57 const H264SPS* sps, 61 const H264SPS* sps,
58 const H264SliceHeader& slice_hdr, 62 const H264SliceHeader& slice_hdr,
59 int32_t *pic_order_cnt) { 63 int32_t *pic_order_cnt) {
60 if (slice_hdr.field_pic_flag) { 64 if (slice_hdr.field_pic_flag) {
61 DLOG(ERROR) << "Interlaced frames are not supported"; 65 DLOG(ERROR) << "Interlaced frames are not supported";
62 return false; 66 return false;
63 } 67 }
64 68
65 bool mmco5 = HasMMCO5(slice_hdr); 69 bool mmco5 = HasMMCO5(slice_hdr);
66 int32_t max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4); 70 int32_t max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4);
67 int32_t max_pic_order_cnt_lsb = 71 int32_t max_pic_order_cnt_lsb =
68 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4); 72 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
69 73
70 // Note: Duplicate frame numbers are ignored. They occur in many videos
71 // despite appearing to be invalid according to the spec.
72 // TODO(sandersd): Check if these videos are using slices or have redundant
73 // streams.
74
75 // Note: Gaps in frame numbers are also ignored. They do not affect POC
76 // computation.
77
78 // Based on T-REC-H.264 8.2.1, "Decoding process for picture order 74 // Based on T-REC-H.264 8.2.1, "Decoding process for picture order
79 // count", available from http://www.itu.int/rec/T-REC-H.264. 75 // count", available from http://www.itu.int/rec/T-REC-H.264.
80 // 76 //
81 // Reorganized slightly from spec pseudocode to handle MMCO5 when storing 77 // Reorganized slightly from spec pseudocode to handle MMCO5 when storing
82 // state instead of when loading it. 78 // state instead of when loading it.
79 //
80 // Note: Gaps in frame numbers are ignored. They do not affect POC
81 // computation.
83 switch (sps->pic_order_cnt_type) { 82 switch (sps->pic_order_cnt_type) {
84 case 0: { 83 case 0: {
85 int32_t prev_pic_order_cnt_msb = ref_pic_order_cnt_msb_; 84 int32_t prev_pic_order_cnt_msb = ref_pic_order_cnt_msb_;
86 int32_t prev_pic_order_cnt_lsb = ref_pic_order_cnt_lsb_; 85 int32_t prev_pic_order_cnt_lsb = ref_pic_order_cnt_lsb_;
87 86
88 // For an IDR picture, clear the state. 87 // For an IDR picture, clear the state.
89 if (slice_hdr.idr_pic_flag) { 88 if (slice_hdr.idr_pic_flag) {
90 prev_pic_order_cnt_msb = 0; 89 prev_pic_order_cnt_msb = 0;
91 prev_pic_order_cnt_lsb = 0; 90 prev_pic_order_cnt_lsb = 0;
92 } 91 }
(...skipping 11 matching lines...) Expand all
104 max_pic_order_cnt_lsb / 2)) { 103 max_pic_order_cnt_lsb / 2)) {
105 pic_order_cnt_msb = prev_pic_order_cnt_msb - max_pic_order_cnt_lsb; 104 pic_order_cnt_msb = prev_pic_order_cnt_msb - max_pic_order_cnt_lsb;
106 } else { 105 } else {
107 pic_order_cnt_msb = prev_pic_order_cnt_msb; 106 pic_order_cnt_msb = prev_pic_order_cnt_msb;
108 } 107 }
109 108
110 // 8-4, 8-5. Derive |top_field_order_count| and |bottom_field_order_cnt| 109 // 8-4, 8-5. Derive |top_field_order_count| and |bottom_field_order_cnt|
111 // (assuming no interlacing). 110 // (assuming no interlacing).
112 int32_t top_foc = pic_order_cnt_msb + slice_hdr.pic_order_cnt_lsb; 111 int32_t top_foc = pic_order_cnt_msb + slice_hdr.pic_order_cnt_lsb;
113 int32_t bottom_foc = top_foc + slice_hdr.delta_pic_order_cnt_bottom; 112 int32_t bottom_foc = top_foc + slice_hdr.delta_pic_order_cnt_bottom;
114 *pic_order_cnt = std::min(top_foc, bottom_foc); 113
114 // Compute POC.
115 //
116 // MMCO5, like IDR, starts a new reordering group. The POC is specified to
117 // change to 0 after decoding; we change it immediately and set the
118 // |pending_mmco5_| flag.
119 if (mmco5)
120 *pic_order_cnt = 0;
121 else
122 *pic_order_cnt = std::min(top_foc, bottom_foc);
115 123
116 // Store state. 124 // Store state.
125 pending_mmco5_ = mmco5;
117 prev_frame_num_ = slice_hdr.frame_num; 126 prev_frame_num_ = slice_hdr.frame_num;
118 if (slice_hdr.nal_ref_idc != 0) { 127 if (slice_hdr.nal_ref_idc != 0) {
119 if (mmco5) { 128 if (mmco5) {
120 ref_pic_order_cnt_msb_ = 0; 129 ref_pic_order_cnt_msb_ = 0;
121 ref_pic_order_cnt_lsb_ = top_foc; 130 ref_pic_order_cnt_lsb_ = top_foc;
122 } else { 131 } else {
123 ref_pic_order_cnt_msb_ = pic_order_cnt_msb; 132 ref_pic_order_cnt_msb_ = pic_order_cnt_msb;
124 ref_pic_order_cnt_lsb_ = slice_hdr.pic_order_cnt_lsb; 133 ref_pic_order_cnt_lsb_ = slice_hdr.pic_order_cnt_lsb;
125 } 134 }
126 } 135 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 expected_pic_order_cnt += sps->offset_for_ref_frame[i]; 182 expected_pic_order_cnt += sps->offset_for_ref_frame[i];
174 } 183 }
175 if (slice_hdr.nal_ref_idc == 0) 184 if (slice_hdr.nal_ref_idc == 0)
176 expected_pic_order_cnt += sps->offset_for_non_ref_pic; 185 expected_pic_order_cnt += sps->offset_for_non_ref_pic;
177 186
178 // 8-10. Derive |top_field_order_cnt| and |bottom_field_order_cnt| 187 // 8-10. Derive |top_field_order_cnt| and |bottom_field_order_cnt|
179 // (assuming no interlacing). 188 // (assuming no interlacing).
180 int32_t top_foc = expected_pic_order_cnt + slice_hdr.delta_pic_order_cnt0; 189 int32_t top_foc = expected_pic_order_cnt + slice_hdr.delta_pic_order_cnt0;
181 int32_t bottom_foc = top_foc + sps->offset_for_top_to_bottom_field + 190 int32_t bottom_foc = top_foc + sps->offset_for_top_to_bottom_field +
182 slice_hdr.delta_pic_order_cnt1; 191 slice_hdr.delta_pic_order_cnt1;
183 *pic_order_cnt = std::min(top_foc, bottom_foc); 192
193 // Compute POC. MMCO5 handling is the same as |pic_order_cnt_type| == 0.
194 if (mmco5)
195 *pic_order_cnt = 0;
196 else
197 *pic_order_cnt = std::min(top_foc, bottom_foc);
184 198
185 // Store state. 199 // Store state.
200 pending_mmco5_ = mmco5;
186 prev_frame_num_ = slice_hdr.frame_num; 201 prev_frame_num_ = slice_hdr.frame_num;
187 prev_frame_num_offset_ = frame_num_offset;
188 if (mmco5) 202 if (mmco5)
189 prev_frame_num_offset_ = 0; 203 prev_frame_num_offset_ = 0;
204 else
205 prev_frame_num_offset_ = frame_num_offset;
190 206
191 break; 207 break;
192 } 208 }
193 209
194 case 2: { 210 case 2: {
195 // 8-11. Derive |frame_num_offset|. 211 // 8-11. Derive |frame_num_offset|.
196 int32_t frame_num_offset; 212 int32_t frame_num_offset;
197 if (slice_hdr.idr_pic_flag) 213 if (slice_hdr.idr_pic_flag)
198 frame_num_offset = 0; 214 frame_num_offset = 0;
199 else if (prev_frame_num_ > slice_hdr.frame_num) 215 else if (prev_frame_num_ > slice_hdr.frame_num)
200 frame_num_offset = prev_frame_num_offset_ + max_frame_num; 216 frame_num_offset = prev_frame_num_offset_ + max_frame_num;
201 else 217 else
202 frame_num_offset = prev_frame_num_offset_; 218 frame_num_offset = prev_frame_num_offset_;
203 219
204 // 8-12, 8-13. Derive |temp_pic_order_count| (it's always the 220 // 8-12, 8-13. Derive |temp_pic_order_count| (it's always the
205 // |pic_order_cnt|, regardless of interlacing). 221 // |pic_order_cnt|, regardless of interlacing).
222 int32_t temp_pic_order_count;
206 if (slice_hdr.idr_pic_flag) 223 if (slice_hdr.idr_pic_flag)
224 temp_pic_order_count = 0;
225 else if (slice_hdr.nal_ref_idc == 0)
226 temp_pic_order_count = 2 * (frame_num_offset + slice_hdr.frame_num) - 1;
227 else
228 temp_pic_order_count = 2 * (frame_num_offset + slice_hdr.frame_num);
229
230 // Compute POC. MMCO5 handling is the same as |pic_order_cnt_type| == 0.
231 if (mmco5)
207 *pic_order_cnt = 0; 232 *pic_order_cnt = 0;
208 else if (slice_hdr.nal_ref_idc == 0)
209 *pic_order_cnt = 2 * (frame_num_offset + slice_hdr.frame_num) - 1;
210 else 233 else
211 *pic_order_cnt = 2 * (frame_num_offset + slice_hdr.frame_num); 234 *pic_order_cnt = temp_pic_order_count;
212 235
213 // Store state. 236 // Store state.
237 pending_mmco5_ = mmco5;
214 prev_frame_num_ = slice_hdr.frame_num; 238 prev_frame_num_ = slice_hdr.frame_num;
215 prev_frame_num_offset_ = frame_num_offset;
216 if (mmco5) 239 if (mmco5)
217 prev_frame_num_offset_ = 0; 240 prev_frame_num_offset_ = 0;
241 else
242 prev_frame_num_offset_ = frame_num_offset;
218 243
219 break; 244 break;
220 } 245 }
221 246
222 default: 247 default:
223 DLOG(ERROR) << "Invalid pic_order_cnt_type: " << sps->pic_order_cnt_type; 248 DLOG(ERROR) << "Invalid pic_order_cnt_type: " << sps->pic_order_cnt_type;
224 return false; 249 return false;
225 } 250 }
226 251
227 return true; 252 return true;
228 } 253 }
229 254
230 } // namespace media 255 } // namespace media
OLDNEW
« no previous file with comments | « media/video/h264_poc.h ('k') | media/video/h264_poc_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698