OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file contains an implementation of an H264 Annex-B video stream parser. | 5 // This file contains an implementation of an H264 Annex-B video stream parser. |
6 | 6 |
7 #ifndef CONTENT_COMMON_GPU_MEDIA_H264_PARSER_H_ | 7 #ifndef CONTENT_COMMON_GPU_MEDIA_H264_PARSER_H_ |
8 #define CONTENT_COMMON_GPU_MEDIA_H264_PARSER_H_ | 8 #define CONTENT_COMMON_GPU_MEDIA_H264_PARSER_H_ |
9 | 9 |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 | 11 |
12 #include <map> | 12 #include <map> |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
15 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
| 16 #include "content/common/gpu/media/h264_bit_reader.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 // For explanations of each struct and its members, see H.264 specification | 20 // For explanations of each struct and its members, see H.264 specification |
20 // at http://www.itu.int/rec/T-REC-H.264. | 21 // at http://www.itu.int/rec/T-REC-H.264. |
21 struct CONTENT_EXPORT H264NALU { | 22 struct CONTENT_EXPORT H264NALU { |
22 H264NALU(); | 23 H264NALU(); |
23 | 24 |
24 enum Type { | 25 enum Type { |
25 kUnspecified = 0, | 26 kUnspecified = 0, |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 | 295 |
295 // Parse a slice header, returning it in |*shdr|. |*nalu| must be set to | 296 // Parse a slice header, returning it in |*shdr|. |*nalu| must be set to |
296 // the NALU returned from AdvanceToNextNALU() and corresponding to |*shdr|. | 297 // the NALU returned from AdvanceToNextNALU() and corresponding to |*shdr|. |
297 Result ParseSliceHeader(const H264NALU& nalu, H264SliceHeader* shdr); | 298 Result ParseSliceHeader(const H264NALU& nalu, H264SliceHeader* shdr); |
298 | 299 |
299 // Parse a SEI message, returning it in |*sei_msg|, provided and managed | 300 // Parse a SEI message, returning it in |*sei_msg|, provided and managed |
300 // by the caller. | 301 // by the caller. |
301 Result ParseSEI(H264SEIMessage* sei_msg); | 302 Result ParseSEI(H264SEIMessage* sei_msg); |
302 | 303 |
303 private: | 304 private: |
304 // A class to provide bit-granularity reading of H.264 streams. | |
305 // This is not a generic bit reader class, as it takes into account | |
306 // H.264 stream-specific constraints, such as skipping emulation-prevention | |
307 // bytes and stop bits. See spec for more details. | |
308 // TODO(posciak): need separate unittests for this class. | |
309 class H264BitReader { | |
310 public: | |
311 H264BitReader(); | |
312 ~H264BitReader(); | |
313 | |
314 // Initialize the reader to start reading at |data|, |size| being size | |
315 // of |data| in bytes. | |
316 // Return false on insufficient size of stream.. | |
317 // TODO(posciak,fischman): consider replacing Initialize() with | |
318 // heap-allocating and creating bit readers on demand instead. | |
319 bool Initialize(const uint8* data, off_t size); | |
320 | |
321 // Read |num_bits| next bits from stream and return in |*out|, first bit | |
322 // from the stream starting at |num_bits| position in |*out|. | |
323 // |num_bits| may be 1-32, inclusive. | |
324 // Return false if the given number of bits cannot be read (not enough | |
325 // bits in the stream), true otherwise. | |
326 bool ReadBits(int num_bits, int *out); | |
327 | |
328 // Return the number of bits left in the stream. | |
329 off_t NumBitsLeft(); | |
330 | |
331 // See the definition of more_rbsp_data() in spec. | |
332 bool HasMoreRBSPData(); | |
333 | |
334 private: | |
335 // Advance to the next byte, loading it into curr_byte_. | |
336 // Return false on end of stream. | |
337 bool UpdateCurrByte(); | |
338 | |
339 // Pointer to the next unread (not in curr_byte_) byte in the stream. | |
340 const uint8* data_; | |
341 | |
342 // Bytes left in the stream (without the curr_byte_). | |
343 off_t bytes_left_; | |
344 | |
345 // Contents of the current byte; first unread bit starting at position | |
346 // 8 - num_remaining_bits_in_curr_byte_ from MSB. | |
347 int curr_byte_; | |
348 | |
349 // Number of bits remaining in curr_byte_ | |
350 int num_remaining_bits_in_curr_byte_; | |
351 | |
352 // Used in emulation prevention three byte detection (see spec). | |
353 // Initially set to 0xffff to accept all initial two-byte sequences. | |
354 int prev_two_bytes_; | |
355 | |
356 DISALLOW_COPY_AND_ASSIGN(H264BitReader); | |
357 }; | |
358 | |
359 // Exp-Golomb code parsing as specified in chapter 9.1 of the spec. | 305 // Exp-Golomb code parsing as specified in chapter 9.1 of the spec. |
360 // Read one unsigned exp-Golomb code from the stream and return in |*val|. | 306 // Read one unsigned exp-Golomb code from the stream and return in |*val|. |
361 Result ReadUE(int* val); | 307 Result ReadUE(int* val); |
362 | 308 |
363 // Read one signed exp-Golomb code from the stream and return in |*val|. | 309 // Read one signed exp-Golomb code from the stream and return in |*val|. |
364 Result ReadSE(int* val); | 310 Result ReadSE(int* val); |
365 | 311 |
366 // Parse scaling lists (see spec). | 312 // Parse scaling lists (see spec). |
367 Result ParseScalingList(int size, int* scaling_list, bool* use_default); | 313 Result ParseScalingList(int size, int* scaling_list, bool* use_default); |
368 Result ParseSPSScalingLists(H264SPS* sps); | 314 Result ParseSPSScalingLists(H264SPS* sps); |
(...skipping 30 matching lines...) Expand all Loading... |
399 typedef std::map<int, H264PPS*> PPSById; | 345 typedef std::map<int, H264PPS*> PPSById; |
400 SPSById active_SPSes_; | 346 SPSById active_SPSes_; |
401 PPSById active_PPSes_; | 347 PPSById active_PPSes_; |
402 | 348 |
403 DISALLOW_COPY_AND_ASSIGN(H264Parser); | 349 DISALLOW_COPY_AND_ASSIGN(H264Parser); |
404 }; | 350 }; |
405 | 351 |
406 } // namespace content | 352 } // namespace content |
407 | 353 |
408 #endif // CONTENT_COMMON_GPU_MEDIA_H264_PARSER_H_ | 354 #endif // CONTENT_COMMON_GPU_MEDIA_H264_PARSER_H_ |
OLD | NEW |