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