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

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

Issue 2661423002: VTVDA: Optimize pic_order_cnt_type == 2. (Closed)
Patch Set: Change POC of MMCO5 frame to 0. Created 3 years, 10 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
« media/video/h264_poc.h ('K') | « media/video/h264_poc.h ('k') | no next file » | 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) {
Pawel Osciak 2017/02/17 04:53:26 On a side note, perhaps we should consider using b
sandersd (OOO until July 31) 2017/02/18 00:46:34 Acknowledged.
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);
Pawel Osciak 2017/02/17 04:53:26 Could we use pending_mmco5_ instead of mmco5 and r
sandersd (OOO until July 31) 2017/02/22 01:39:32 Yes, it's the same. I did it this way so that memb
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
Pawel Osciak 2017/02/17 04:53:26 Should this first note be removed as well?
sandersd (OOO until July 31) 2017/02/18 00:46:34 Yes, it was just plain wrong. (Duplicate frame num
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.
Pawel Osciak 2017/02/17 04:53:26 Perhaps we could replace this comment with an expl
sandersd (OOO until July 31) 2017/02/22 01:39:32 Done. I went with something similar to what the he
115 if (mmco5)
116 *pic_order_cnt = 0;
117 else
118 *pic_order_cnt = std::min(top_foc, bottom_foc);
115 119
116 // Store state. 120 // Store state.
121 pending_mmco5_ = mmco5;
117 prev_frame_num_ = slice_hdr.frame_num; 122 prev_frame_num_ = slice_hdr.frame_num;
118 if (slice_hdr.nal_ref_idc != 0) { 123 if (slice_hdr.nal_ref_idc != 0) {
119 if (mmco5) { 124 if (mmco5) {
120 ref_pic_order_cnt_msb_ = 0; 125 ref_pic_order_cnt_msb_ = 0;
121 ref_pic_order_cnt_lsb_ = top_foc; 126 ref_pic_order_cnt_lsb_ = top_foc;
122 } else { 127 } else {
123 ref_pic_order_cnt_msb_ = pic_order_cnt_msb; 128 ref_pic_order_cnt_msb_ = pic_order_cnt_msb;
124 ref_pic_order_cnt_lsb_ = slice_hdr.pic_order_cnt_lsb; 129 ref_pic_order_cnt_lsb_ = slice_hdr.pic_order_cnt_lsb;
125 } 130 }
126 } 131 }
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 expected_pic_order_cnt += sps->offset_for_ref_frame[i]; 178 expected_pic_order_cnt += sps->offset_for_ref_frame[i];
174 } 179 }
175 if (slice_hdr.nal_ref_idc == 0) 180 if (slice_hdr.nal_ref_idc == 0)
176 expected_pic_order_cnt += sps->offset_for_non_ref_pic; 181 expected_pic_order_cnt += sps->offset_for_non_ref_pic;
177 182
178 // 8-10. Derive |top_field_order_cnt| and |bottom_field_order_cnt| 183 // 8-10. Derive |top_field_order_cnt| and |bottom_field_order_cnt|
179 // (assuming no interlacing). 184 // (assuming no interlacing).
180 int32_t top_foc = expected_pic_order_cnt + slice_hdr.delta_pic_order_cnt0; 185 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 + 186 int32_t bottom_foc = top_foc + sps->offset_for_top_to_bottom_field +
182 slice_hdr.delta_pic_order_cnt1; 187 slice_hdr.delta_pic_order_cnt1;
183 *pic_order_cnt = std::min(top_foc, bottom_foc); 188
189 // Compute POC.
190 if (mmco5)
191 *pic_order_cnt = 0;
192 else
193 *pic_order_cnt = std::min(top_foc, bottom_foc);
184 194
185 // Store state. 195 // Store state.
196 pending_mmco5_ = mmco5;
186 prev_frame_num_ = slice_hdr.frame_num; 197 prev_frame_num_ = slice_hdr.frame_num;
187 prev_frame_num_offset_ = frame_num_offset;
188 if (mmco5) 198 if (mmco5)
189 prev_frame_num_offset_ = 0; 199 prev_frame_num_offset_ = 0;
200 else
201 prev_frame_num_offset_ = frame_num_offset;
190 202
191 break; 203 break;
192 } 204 }
193 205
194 case 2: { 206 case 2: {
195 // 8-11. Derive |frame_num_offset|. 207 // 8-11. Derive |frame_num_offset|.
196 int32_t frame_num_offset; 208 int32_t frame_num_offset;
197 if (slice_hdr.idr_pic_flag) 209 if (slice_hdr.idr_pic_flag)
198 frame_num_offset = 0; 210 frame_num_offset = 0;
199 else if (prev_frame_num_ > slice_hdr.frame_num) 211 else if (prev_frame_num_ > slice_hdr.frame_num)
200 frame_num_offset = prev_frame_num_offset_ + max_frame_num; 212 frame_num_offset = prev_frame_num_offset_ + max_frame_num;
201 else 213 else
202 frame_num_offset = prev_frame_num_offset_; 214 frame_num_offset = prev_frame_num_offset_;
203 215
204 // 8-12, 8-13. Derive |temp_pic_order_count| (it's always the 216 // 8-12, 8-13. Derive |temp_pic_order_count| (it's always the
205 // |pic_order_cnt|, regardless of interlacing). 217 // |pic_order_cnt|, regardless of interlacing).
218 int32_t temp_pic_order_count;
Pawel Osciak 2017/02/17 04:53:26 Perhaps we could say: if (mmco5) *pic_order_cnt
sandersd (OOO until July 31) 2017/02/18 00:46:34 I wrote it that way originally, but ended up switc
206 if (slice_hdr.idr_pic_flag) 219 if (slice_hdr.idr_pic_flag)
220 temp_pic_order_count = 0;
221 else if (slice_hdr.nal_ref_idc == 0)
222 temp_pic_order_count = 2 * (frame_num_offset + slice_hdr.frame_num) - 1;
223 else
224 temp_pic_order_count = 2 * (frame_num_offset + slice_hdr.frame_num);
225
226 // Compute POC.
227 if (mmco5)
207 *pic_order_cnt = 0; 228 *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 229 else
211 *pic_order_cnt = 2 * (frame_num_offset + slice_hdr.frame_num); 230 *pic_order_cnt = temp_pic_order_count;
212 231
213 // Store state. 232 // Store state.
233 pending_mmco5_ = mmco5;
214 prev_frame_num_ = slice_hdr.frame_num; 234 prev_frame_num_ = slice_hdr.frame_num;
215 prev_frame_num_offset_ = frame_num_offset;
216 if (mmco5) 235 if (mmco5)
217 prev_frame_num_offset_ = 0; 236 prev_frame_num_offset_ = 0;
237 else
238 prev_frame_num_offset_ = frame_num_offset;
218 239
219 break; 240 break;
220 } 241 }
221 242
222 default: 243 default:
223 DLOG(ERROR) << "Invalid pic_order_cnt_type: " << sps->pic_order_cnt_type; 244 DLOG(ERROR) << "Invalid pic_order_cnt_type: " << sps->pic_order_cnt_type;
224 return false; 245 return false;
225 } 246 }
226 247
227 return true; 248 return true;
228 } 249 }
229 250
230 } // namespace media 251 } // namespace media
OLDNEW
« media/video/h264_poc.h ('K') | « media/video/h264_poc.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698