Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / media / formats / mp2t / es_parser_h264.cc
blob42d41454daa5d94f79145935b9976c218cbda72f
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 "media/formats/mp2t/es_parser_h264.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "media/base/buffers.h"
10 #include "media/base/stream_parser_buffer.h"
11 #include "media/base/video_frame.h"
12 #include "media/filters/h264_parser.h"
13 #include "media/formats/common/offset_byte_queue.h"
14 #include "media/formats/mp2t/mp2t_common.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/geometry/size.h"
18 namespace media {
19 namespace mp2t {
21 // An AUD NALU is at least 4 bytes:
22 // 3 bytes for the start code + 1 byte for the NALU type.
23 const int kMinAUDSize = 4;
25 EsParserH264::EsParserH264(
26 const NewVideoConfigCB& new_video_config_cb,
27 const EmitBufferCB& emit_buffer_cb)
28 : es_adapter_(new_video_config_cb, emit_buffer_cb),
29 h264_parser_(new H264Parser()),
30 current_access_unit_pos_(0),
31 next_access_unit_pos_(0) {
34 EsParserH264::~EsParserH264() {
37 void EsParserH264::Flush() {
38 DVLOG(1) << __FUNCTION__;
39 if (!FindAUD(&current_access_unit_pos_))
40 return;
42 // Simulate an additional AUD to force emitting the last access unit
43 // which is assumed to be complete at this point.
44 uint8 aud[] = { 0x00, 0x00, 0x01, 0x09 };
45 es_queue_->Push(aud, sizeof(aud));
46 ParseFromEsQueue();
48 es_adapter_.Flush();
51 void EsParserH264::ResetInternal() {
52 DVLOG(1) << __FUNCTION__;
53 h264_parser_.reset(new H264Parser());
54 current_access_unit_pos_ = 0;
55 next_access_unit_pos_ = 0;
56 last_video_decoder_config_ = VideoDecoderConfig();
57 es_adapter_.Reset();
60 bool EsParserH264::FindAUD(int64* stream_pos) {
61 while (true) {
62 const uint8* es;
63 int size;
64 es_queue_->PeekAt(*stream_pos, &es, &size);
66 // Find a start code and move the stream to the start code parser position.
67 off_t start_code_offset;
68 off_t start_code_size;
69 bool start_code_found = H264Parser::FindStartCode(
70 es, size, &start_code_offset, &start_code_size);
71 *stream_pos += start_code_offset;
73 // No H264 start code found or NALU type not available yet.
74 if (!start_code_found || start_code_offset + start_code_size >= size)
75 return false;
77 // Exit the parser loop when an AUD is found.
78 // Note: NALU header for an AUD:
79 // - nal_ref_idc must be 0
80 // - nal_unit_type must be H264NALU::kAUD
81 if (es[start_code_offset + start_code_size] == H264NALU::kAUD)
82 break;
84 // The current NALU is not an AUD, skip the start code
85 // and continue parsing the stream.
86 *stream_pos += start_code_size;
89 return true;
92 bool EsParserH264::ParseFromEsQueue() {
93 DCHECK_LE(es_queue_->head(), current_access_unit_pos_);
94 DCHECK_LE(current_access_unit_pos_, next_access_unit_pos_);
95 DCHECK_LE(next_access_unit_pos_, es_queue_->tail());
97 // Find the next AUD located at or after |current_access_unit_pos_|. This is
98 // needed since initially |current_access_unit_pos_| might not point to
99 // an AUD.
100 // Discard all the data before the updated |current_access_unit_pos_|
101 // since it won't be used again.
102 bool aud_found = FindAUD(&current_access_unit_pos_);
103 es_queue_->Trim(current_access_unit_pos_);
104 if (next_access_unit_pos_ < current_access_unit_pos_)
105 next_access_unit_pos_ = current_access_unit_pos_;
107 // Resume parsing later if no AUD was found.
108 if (!aud_found)
109 return true;
111 // Find the next AUD to make sure we have a complete access unit.
112 if (next_access_unit_pos_ < current_access_unit_pos_ + kMinAUDSize) {
113 next_access_unit_pos_ = current_access_unit_pos_ + kMinAUDSize;
114 DCHECK_LE(next_access_unit_pos_, es_queue_->tail());
116 if (!FindAUD(&next_access_unit_pos_))
117 return true;
119 // At this point, we know we have a full access unit.
120 bool is_key_frame = false;
121 int pps_id_for_access_unit = -1;
123 const uint8* es;
124 int size;
125 es_queue_->PeekAt(current_access_unit_pos_, &es, &size);
126 int access_unit_size = base::checked_cast<int, int64>(
127 next_access_unit_pos_ - current_access_unit_pos_);
128 DCHECK_LE(access_unit_size, size);
129 h264_parser_->SetStream(es, access_unit_size);
131 while (true) {
132 bool is_eos = false;
133 H264NALU nalu;
134 switch (h264_parser_->AdvanceToNextNALU(&nalu)) {
135 case H264Parser::kOk:
136 break;
137 case H264Parser::kInvalidStream:
138 case H264Parser::kUnsupportedStream:
139 return false;
140 case H264Parser::kEOStream:
141 is_eos = true;
142 break;
144 if (is_eos)
145 break;
147 switch (nalu.nal_unit_type) {
148 case H264NALU::kAUD: {
149 DVLOG(LOG_LEVEL_ES) << "NALU: AUD";
150 break;
152 case H264NALU::kSPS: {
153 DVLOG(LOG_LEVEL_ES) << "NALU: SPS";
154 int sps_id;
155 if (h264_parser_->ParseSPS(&sps_id) != H264Parser::kOk)
156 return false;
157 break;
159 case H264NALU::kPPS: {
160 DVLOG(LOG_LEVEL_ES) << "NALU: PPS";
161 int pps_id;
162 if (h264_parser_->ParsePPS(&pps_id) != H264Parser::kOk)
163 return false;
164 break;
166 case H264NALU::kIDRSlice:
167 case H264NALU::kNonIDRSlice: {
168 is_key_frame = (nalu.nal_unit_type == H264NALU::kIDRSlice);
169 DVLOG(LOG_LEVEL_ES) << "NALU: slice IDR=" << is_key_frame;
170 H264SliceHeader shdr;
171 if (h264_parser_->ParseSliceHeader(nalu, &shdr) != H264Parser::kOk) {
172 // Only accept an invalid SPS/PPS at the beginning when the stream
173 // does not necessarily start with an SPS/PPS/IDR.
174 // TODO(damienv): Should be able to differentiate a missing SPS/PPS
175 // from a slice header parsing error.
176 if (last_video_decoder_config_.IsValidConfig())
177 return false;
178 } else {
179 pps_id_for_access_unit = shdr.pic_parameter_set_id;
181 break;
183 default: {
184 DVLOG(LOG_LEVEL_ES) << "NALU: " << nalu.nal_unit_type;
189 // Emit a frame and move the stream to the next AUD position.
190 RCHECK(EmitFrame(current_access_unit_pos_, access_unit_size,
191 is_key_frame, pps_id_for_access_unit));
192 current_access_unit_pos_ = next_access_unit_pos_;
193 es_queue_->Trim(current_access_unit_pos_);
195 return true;
198 bool EsParserH264::EmitFrame(int64 access_unit_pos, int access_unit_size,
199 bool is_key_frame, int pps_id) {
200 // Get the access unit timing info.
201 // Note: |current_timing_desc.pts| might be |kNoTimestamp()| at this point
202 // if:
203 // - the stream is not fully MPEG-2 compliant.
204 // - or if the stream relies on H264 VUI parameters to compute the timestamps.
205 // See H.222 spec: section 2.7.5 "Conditional coding of timestamps".
206 // This part is not yet implemented in EsParserH264.
207 // |es_adapter_| will take care of the missing timestamps.
208 TimingDesc current_timing_desc = GetTimingDescriptor(access_unit_pos);
209 DVLOG_IF(1, current_timing_desc.pts == kNoTimestamp())
210 << "Missing timestamp";
212 // If only the PTS is provided, copy the PTS into the DTS.
213 if (current_timing_desc.dts == kNoDecodeTimestamp()) {
214 current_timing_desc.dts =
215 DecodeTimestamp::FromPresentationTime(current_timing_desc.pts);
218 // Update the video decoder configuration if needed.
219 const H264PPS* pps = h264_parser_->GetPPS(pps_id);
220 if (!pps) {
221 // Only accept an invalid PPS at the beginning when the stream
222 // does not necessarily start with an SPS/PPS/IDR.
223 // In this case, the initial frames are conveyed to the upper layer with
224 // an invalid VideoDecoderConfig and it's up to the upper layer
225 // to process this kind of frame accordingly.
226 if (last_video_decoder_config_.IsValidConfig())
227 return false;
228 } else {
229 const H264SPS* sps = h264_parser_->GetSPS(pps->seq_parameter_set_id);
230 if (!sps)
231 return false;
232 RCHECK(UpdateVideoDecoderConfig(sps));
235 // Emit a frame.
236 DVLOG(LOG_LEVEL_ES) << "Emit frame: stream_pos=" << current_access_unit_pos_
237 << " size=" << access_unit_size;
238 int es_size;
239 const uint8* es;
240 es_queue_->PeekAt(current_access_unit_pos_, &es, &es_size);
241 CHECK_GE(es_size, access_unit_size);
243 // TODO(wolenetz/acolwell): Validate and use a common cross-parser TrackId
244 // type and allow multiple video tracks. See https://crbug.com/341581.
245 scoped_refptr<StreamParserBuffer> stream_parser_buffer =
246 StreamParserBuffer::CopyFrom(
248 access_unit_size,
249 is_key_frame,
250 DemuxerStream::VIDEO,
252 stream_parser_buffer->SetDecodeTimestamp(current_timing_desc.dts);
253 stream_parser_buffer->set_timestamp(current_timing_desc.pts);
254 return es_adapter_.OnNewBuffer(stream_parser_buffer);
257 bool EsParserH264::UpdateVideoDecoderConfig(const H264SPS* sps) {
258 // Set the SAR to 1 when not specified in the H264 stream.
259 int sar_width = (sps->sar_width == 0) ? 1 : sps->sar_width;
260 int sar_height = (sps->sar_height == 0) ? 1 : sps->sar_height;
262 // TODO(damienv): a MAP unit can be either 16 or 32 pixels.
263 // although it's 16 pixels for progressive non MBAFF frames.
264 gfx::Size coded_size((sps->pic_width_in_mbs_minus1 + 1) * 16,
265 (sps->pic_height_in_map_units_minus1 + 1) * 16);
266 gfx::Rect visible_rect(
267 sps->frame_crop_left_offset,
268 sps->frame_crop_top_offset,
269 (coded_size.width() - sps->frame_crop_right_offset) -
270 sps->frame_crop_left_offset,
271 (coded_size.height() - sps->frame_crop_bottom_offset) -
272 sps->frame_crop_top_offset);
273 if (visible_rect.width() <= 0 || visible_rect.height() <= 0)
274 return false;
275 gfx::Size natural_size(
276 (visible_rect.width() * sar_width) / sar_height,
277 visible_rect.height());
278 if (natural_size.width() == 0)
279 return false;
281 VideoDecoderConfig video_decoder_config(
282 kCodecH264, VIDEO_CODEC_PROFILE_UNKNOWN, PIXEL_FORMAT_YV12,
283 COLOR_SPACE_HD_REC709, coded_size, visible_rect, natural_size, NULL, 0,
284 false);
286 if (!video_decoder_config.Matches(last_video_decoder_config_)) {
287 DVLOG(1) << "Profile IDC: " << sps->profile_idc;
288 DVLOG(1) << "Level IDC: " << sps->level_idc;
289 DVLOG(1) << "Pic width: " << coded_size.width();
290 DVLOG(1) << "Pic height: " << coded_size.height();
291 DVLOG(1) << "log2_max_frame_num_minus4: "
292 << sps->log2_max_frame_num_minus4;
293 DVLOG(1) << "SAR: width=" << sps->sar_width
294 << " height=" << sps->sar_height;
295 last_video_decoder_config_ = video_decoder_config;
296 es_adapter_.OnConfigChanged(video_decoder_config);
299 return true;
302 } // namespace mp2t
303 } // namespace media