| OLD | NEW |
| 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 H264POC::H264POC() { |
| 17 Reset(); | 17 Reset(); |
| 18 } | 18 } |
| 19 | 19 |
| 20 H264POC::~H264POC() { | 20 H264POC::~H264POC() { |
| 21 } | 21 } |
| 22 | 22 |
| 23 void H264POC::Reset() { | 23 void H264POC::Reset() { |
| 24 // It shouldn't be necessary to reset these values, but doing so will improve | 24 // It shouldn't be necessary to reset these values, but doing so will improve |
| 25 // reproducibility for buggy streams. | 25 // reproducibility for buggy streams. |
| 26 ref_pic_order_cnt_msb_ = 0; | 26 ref_pic_order_cnt_msb_ = 0; |
| 27 ref_pic_order_cnt_lsb_ = 0; | 27 ref_pic_order_cnt_lsb_ = 0; |
| 28 prev_frame_num_ = 0; | 28 prev_frame_num_ = 0; |
| 29 prev_frame_num_offset_ = 0; | 29 prev_frame_num_offset_ = 0; |
| 30 prev_poc_ = 0; | |
| 31 } | 30 } |
| 32 | 31 |
| 33 // Check if a slice includes memory management control operation 5, which | 32 // Check if a slice includes memory management control operation 5, which |
| 34 // results in some |pic_order_cnt| state being cleared. | 33 // results in some |pic_order_cnt| state being cleared. |
| 35 static bool HasMMCO5(const media::H264SliceHeader& slice_hdr) { | 34 static bool HasMMCO5(const media::H264SliceHeader& slice_hdr) { |
| 36 // Require that the frame actually has memory management control operations. | 35 // Require that the frame actually has memory management control operations. |
| 37 if (slice_hdr.nal_ref_idc == 0 || | 36 if (slice_hdr.nal_ref_idc == 0 || |
| 38 slice_hdr.idr_pic_flag || | 37 slice_hdr.idr_pic_flag || |
| 39 !slice_hdr.adaptive_ref_pic_marking_mode_flag) { | 38 !slice_hdr.adaptive_ref_pic_marking_mode_flag) { |
| 40 return false; | 39 return false; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 56 | 55 |
| 57 bool H264POC::ComputePicOrderCnt( | 56 bool H264POC::ComputePicOrderCnt( |
| 58 const H264SPS* sps, | 57 const H264SPS* sps, |
| 59 const H264SliceHeader& slice_hdr, | 58 const H264SliceHeader& slice_hdr, |
| 60 int32_t *pic_order_cnt) { | 59 int32_t *pic_order_cnt) { |
| 61 if (slice_hdr.field_pic_flag) { | 60 if (slice_hdr.field_pic_flag) { |
| 62 DLOG(ERROR) << "Interlaced frames are not supported"; | 61 DLOG(ERROR) << "Interlaced frames are not supported"; |
| 63 return false; | 62 return false; |
| 64 } | 63 } |
| 65 | 64 |
| 66 if (!slice_hdr.idr_pic_flag && slice_hdr.frame_num == prev_frame_num_) { | 65 // TODO(sandersd): Handle |gaps_in_frame_num_value|. |
| 67 DLOG(WARNING) << "Redundant picture passed to H264POC"; | 66 if (prev_frame_num_ > 0 && prev_frame_num_ < slice_hdr.frame_num - 1) { |
| 68 *pic_order_cnt = prev_poc_; | 67 DLOG(ERROR) << "Gaps in frame_num are not supported"; |
| 69 return true; | 68 return false; |
| 70 } | 69 } |
| 71 | 70 |
| 72 bool mmco5 = HasMMCO5(slice_hdr); | 71 bool mmco5 = HasMMCO5(slice_hdr); |
| 73 int32_t max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4); | 72 int32_t max_frame_num = 1 << (sps->log2_max_frame_num_minus4 + 4); |
| 74 int32_t max_pic_order_cnt_lsb = | 73 int32_t max_pic_order_cnt_lsb = |
| 75 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4); | 74 1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4); |
| 76 | 75 |
| 77 if (!slice_hdr.idr_pic_flag && | |
| 78 slice_hdr.frame_num != (prev_frame_num_ + 1) % max_frame_num) { | |
| 79 // We don't do any special handling of gaps in |frame_num|, because the | |
| 80 // computations below are unaffected by them. In particular, wrapping is | |
| 81 // handled the same whether we simulate the missing frames or not. | |
| 82 if (!sps->gaps_in_frame_num_value_allowed_flag) | |
| 83 DLOG(WARNING) << "Invalid gap in frame_num"; | |
| 84 } | |
| 85 | |
| 86 // Based on T-REC-H.264 8.2.1, "Decoding process for picture order | 76 // Based on T-REC-H.264 8.2.1, "Decoding process for picture order |
| 87 // count", available from http://www.itu.int/rec/T-REC-H.264. | 77 // count", available from http://www.itu.int/rec/T-REC-H.264. |
| 88 // | 78 // |
| 89 // Reorganized slightly from spec pseudocode to handle MMCO5 when storing | 79 // Reorganized slightly from spec pseudocode to handle MMCO5 when storing |
| 90 // state instead of when loading it. | 80 // state instead of when loading it. |
| 91 int32_t poc; | |
| 92 switch (sps->pic_order_cnt_type) { | 81 switch (sps->pic_order_cnt_type) { |
| 93 case 0: { | 82 case 0: { |
| 94 int32_t prev_pic_order_cnt_msb = ref_pic_order_cnt_msb_; | 83 int32_t prev_pic_order_cnt_msb = ref_pic_order_cnt_msb_; |
| 95 int32_t prev_pic_order_cnt_lsb = ref_pic_order_cnt_lsb_; | 84 int32_t prev_pic_order_cnt_lsb = ref_pic_order_cnt_lsb_; |
| 96 | 85 |
| 97 // For an IDR picture, clear the state. | 86 // For an IDR picture, clear the state. |
| 98 if (slice_hdr.idr_pic_flag) { | 87 if (slice_hdr.idr_pic_flag) { |
| 99 prev_pic_order_cnt_msb = 0; | 88 prev_pic_order_cnt_msb = 0; |
| 100 prev_pic_order_cnt_lsb = 0; | 89 prev_pic_order_cnt_lsb = 0; |
| 101 } | 90 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 113 max_pic_order_cnt_lsb / 2)) { | 102 max_pic_order_cnt_lsb / 2)) { |
| 114 pic_order_cnt_msb = prev_pic_order_cnt_msb - max_pic_order_cnt_lsb; | 103 pic_order_cnt_msb = prev_pic_order_cnt_msb - max_pic_order_cnt_lsb; |
| 115 } else { | 104 } else { |
| 116 pic_order_cnt_msb = prev_pic_order_cnt_msb; | 105 pic_order_cnt_msb = prev_pic_order_cnt_msb; |
| 117 } | 106 } |
| 118 | 107 |
| 119 // 8-4, 8-5. Derive |top_field_order_count| and |bottom_field_order_cnt| | 108 // 8-4, 8-5. Derive |top_field_order_count| and |bottom_field_order_cnt| |
| 120 // (assuming no interlacing). | 109 // (assuming no interlacing). |
| 121 int32_t top_foc = pic_order_cnt_msb + slice_hdr.pic_order_cnt_lsb; | 110 int32_t top_foc = pic_order_cnt_msb + slice_hdr.pic_order_cnt_lsb; |
| 122 int32_t bottom_foc = top_foc + slice_hdr.delta_pic_order_cnt_bottom; | 111 int32_t bottom_foc = top_foc + slice_hdr.delta_pic_order_cnt_bottom; |
| 123 poc = std::min(top_foc, bottom_foc); | 112 *pic_order_cnt = std::min(top_foc, bottom_foc); |
| 124 | 113 |
| 125 // Store state. | 114 // Store state. |
| 126 prev_frame_num_ = slice_hdr.frame_num; | 115 prev_frame_num_ = slice_hdr.frame_num; |
| 127 if (slice_hdr.nal_ref_idc != 0) { | 116 if (slice_hdr.nal_ref_idc != 0) { |
| 128 if (mmco5) { | 117 if (mmco5) { |
| 129 ref_pic_order_cnt_msb_ = 0; | 118 ref_pic_order_cnt_msb_ = 0; |
| 130 ref_pic_order_cnt_lsb_ = top_foc; | 119 ref_pic_order_cnt_lsb_ = top_foc; |
| 131 } else { | 120 } else { |
| 132 ref_pic_order_cnt_msb_ = pic_order_cnt_msb; | 121 ref_pic_order_cnt_msb_ = pic_order_cnt_msb; |
| 133 ref_pic_order_cnt_lsb_ = slice_hdr.pic_order_cnt_lsb; | 122 ref_pic_order_cnt_lsb_ = slice_hdr.pic_order_cnt_lsb; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 expected_pic_order_cnt += sps->offset_for_ref_frame[i]; | 171 expected_pic_order_cnt += sps->offset_for_ref_frame[i]; |
| 183 } | 172 } |
| 184 if (slice_hdr.nal_ref_idc == 0) | 173 if (slice_hdr.nal_ref_idc == 0) |
| 185 expected_pic_order_cnt += sps->offset_for_non_ref_pic; | 174 expected_pic_order_cnt += sps->offset_for_non_ref_pic; |
| 186 | 175 |
| 187 // 8-10. Derive |top_field_order_cnt| and |bottom_field_order_cnt| | 176 // 8-10. Derive |top_field_order_cnt| and |bottom_field_order_cnt| |
| 188 // (assuming no interlacing). | 177 // (assuming no interlacing). |
| 189 int32_t top_foc = expected_pic_order_cnt + slice_hdr.delta_pic_order_cnt0; | 178 int32_t top_foc = expected_pic_order_cnt + slice_hdr.delta_pic_order_cnt0; |
| 190 int32_t bottom_foc = top_foc + sps->offset_for_top_to_bottom_field + | 179 int32_t bottom_foc = top_foc + sps->offset_for_top_to_bottom_field + |
| 191 slice_hdr.delta_pic_order_cnt1; | 180 slice_hdr.delta_pic_order_cnt1; |
| 192 poc = std::min(top_foc, bottom_foc); | 181 *pic_order_cnt = std::min(top_foc, bottom_foc); |
| 193 | 182 |
| 194 // Store state. | 183 // Store state. |
| 195 prev_frame_num_ = slice_hdr.frame_num; | 184 prev_frame_num_ = slice_hdr.frame_num; |
| 196 prev_frame_num_offset_ = frame_num_offset; | 185 prev_frame_num_offset_ = frame_num_offset; |
| 197 if (mmco5) | 186 if (mmco5) |
| 198 prev_frame_num_offset_ = 0; | 187 prev_frame_num_offset_ = 0; |
| 199 | 188 |
| 200 break; | 189 break; |
| 201 } | 190 } |
| 202 | 191 |
| 203 case 2: { | 192 case 2: { |
| 204 // 8-11. Derive |frame_num_offset|. | 193 // 8-11. Derive |frame_num_offset|. |
| 205 int32_t frame_num_offset; | 194 int32_t frame_num_offset; |
| 206 if (slice_hdr.idr_pic_flag) | 195 if (slice_hdr.idr_pic_flag) |
| 207 frame_num_offset = 0; | 196 frame_num_offset = 0; |
| 208 else if (prev_frame_num_ > slice_hdr.frame_num) | 197 else if (prev_frame_num_ > slice_hdr.frame_num) |
| 209 frame_num_offset = prev_frame_num_offset_ + max_frame_num; | 198 frame_num_offset = prev_frame_num_offset_ + max_frame_num; |
| 210 else | 199 else |
| 211 frame_num_offset = prev_frame_num_offset_; | 200 frame_num_offset = prev_frame_num_offset_; |
| 212 | 201 |
| 213 // 8-12, 8-13. Derive |temp_pic_order_count| (it's always the | 202 // 8-12, 8-13. Derive |temp_pic_order_count| (it's always the |
| 214 // |pic_order_cnt|, regardless of interlacing). | 203 // |pic_order_cnt|, regardless of interlacing). |
| 215 if (slice_hdr.idr_pic_flag) | 204 if (slice_hdr.idr_pic_flag) |
| 216 poc = 0; | 205 *pic_order_cnt = 0; |
| 217 else if (slice_hdr.nal_ref_idc == 0) | 206 else if (slice_hdr.nal_ref_idc == 0) |
| 218 poc = 2 * (frame_num_offset + slice_hdr.frame_num) - 1; | 207 *pic_order_cnt = 2 * (frame_num_offset + slice_hdr.frame_num) - 1; |
| 219 else | 208 else |
| 220 poc = 2 * (frame_num_offset + slice_hdr.frame_num); | 209 *pic_order_cnt = 2 * (frame_num_offset + slice_hdr.frame_num); |
| 221 | 210 |
| 222 // Store state. | 211 // Store state. |
| 223 prev_frame_num_ = slice_hdr.frame_num; | 212 prev_frame_num_ = slice_hdr.frame_num; |
| 224 prev_frame_num_offset_ = frame_num_offset; | 213 prev_frame_num_offset_ = frame_num_offset; |
| 225 if (mmco5) | 214 if (mmco5) |
| 226 prev_frame_num_offset_ = 0; | 215 prev_frame_num_offset_ = 0; |
| 227 | 216 |
| 228 break; | 217 break; |
| 229 } | 218 } |
| 230 | 219 |
| 231 default: | 220 default: |
| 232 DLOG(ERROR) << "Invalid pic_order_cnt_type: " << sps->pic_order_cnt_type; | 221 DLOG(ERROR) << "Invalid pic_order_cnt_type: " << sps->pic_order_cnt_type; |
| 233 return false; | 222 return false; |
| 234 } | 223 } |
| 235 | 224 |
| 236 *pic_order_cnt = prev_poc_ = poc; | |
| 237 return true; | 225 return true; |
| 238 } | 226 } |
| 239 | 227 |
| 240 } // namespace media | 228 } // namespace media |
| OLD | NEW |