DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs
es_parser_h264.h
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.
4 
5 #ifndef MEDIA_FORMATS_MP2T_ES_PARSER_H264_H_
6 #define MEDIA_FORMATS_MP2T_ES_PARSER_H264_H_
7 
8 #include <stdint.h>
9 
10 #include <list>
11 #include <utility>
12 
13 #include "packager/base/callback.h"
14 #include "packager/base/compiler_specific.h"
15 #include "packager/base/memory/scoped_ptr.h"
16 #include "packager/media/formats/mp2t/es_parser.h"
17 
18 namespace edash_packager {
19 namespace media {
20 
21 class H264ByteToUnitStreamConverter;
22 class H264Parser;
23 class OffsetByteQueue;
24 struct H264SPS;
25 
26 namespace mp2t {
27 
28 // Remark:
29 // In this h264 parser, frame splitting is based on AUD nals.
30 // Mpeg2 TS spec: "2.14 Carriage of Rec. ITU-T H.264 | ISO/IEC 14496-10 video"
31 // "Each AVC access unit shall contain an access unit delimiter NAL Unit;"
32 //
33 class EsParserH264 : public EsParser {
34  public:
35  EsParserH264(uint32_t pid,
36  const NewStreamInfoCB& new_stream_info_cb,
37  const EmitSampleCB& emit_sample_cb);
38  virtual ~EsParserH264();
39 
40  // EsParser implementation overrides.
41  virtual bool Parse(const uint8_t* buf,
42  int size,
43  int64_t pts,
44  int64_t dts) OVERRIDE;
45  virtual void Flush() OVERRIDE;
46  virtual void Reset() OVERRIDE;
47 
48  private:
49  struct TimingDesc {
50  int64_t dts;
51  int64_t pts;
52  };
53 
54  // Find the AUD located at or after |*stream_pos|.
55  // Return true if an AUD is found.
56  // If found, |*stream_pos| corresponds to the position of the AUD start code
57  // in the stream. Otherwise, |*stream_pos| corresponds to the last position
58  // of the start code parser.
59  bool FindAUD(int64_t* stream_pos);
60 
61  // Resumes the H264 ES parsing.
62  // Return true if successful.
63  bool ParseInternal();
64 
65  // Emit a frame whose position in the ES queue starts at |access_unit_pos|.
66  // Returns true if successful, false if no PTS is available for the frame.
67  bool EmitFrame(int64_t access_unit_pos,
68  int access_unit_size,
69  bool is_key_frame,
70  int pps_id);
71 
72  // Update the video decoder config based on an H264 SPS.
73  // Return true if successful.
74  bool UpdateVideoDecoderConfig(const H264SPS* sps);
75 
76  // Callbacks to pass the stream configuration and the frames.
77  NewStreamInfoCB new_stream_info_cb_;
78  EmitSampleCB emit_sample_cb_;
79 
80  // Bytes of the ES stream that have not been emitted yet.
81  scoped_ptr<media::OffsetByteQueue> es_queue_;
82  std::list<std::pair<int64_t, TimingDesc> > timing_desc_list_;
83 
84  // H264 parser state.
85  // - |current_access_unit_pos_| is pointing to an annexB syncword
86  // representing the first NALU of an H264 access unit.
87  scoped_ptr<H264Parser> h264_parser_;
88  int64_t current_access_unit_pos_;
89  int64_t next_access_unit_pos_;
90 
91  // Filter to convert H.264 Annex B byte stream to unit stream.
92  scoped_ptr<H264ByteToUnitStreamConverter> stream_converter_;
93 
94  // Last video decoder config.
95  scoped_refptr<StreamInfo> last_video_decoder_config_;
96  bool decoder_config_check_pending_;
97 
98  // Frame for which we do not yet have a duration.
99  scoped_refptr<MediaSample> pending_sample_;
100  uint64_t pending_sample_duration_;
101 
102  // Indicates whether waiting for first key frame.
103  bool waiting_for_key_frame_;
104 };
105 
106 } // namespace mp2t
107 } // namespace media
108 } // namespace edash_packager
109 
110 #endif