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  ~EsParserH264() override;
39 
40  // EsParser implementation overrides.
41  bool Parse(const uint8_t* buf, int size, int64_t pts, int64_t dts) override;
42  void Flush() override;
43  void Reset() override;
44 
45  private:
46  struct TimingDesc {
47  int64_t dts;
48  int64_t pts;
49  };
50 
51  // Find the AUD located at or after |*stream_pos|.
52  // Return true if an AUD is found.
53  // If found, |*stream_pos| corresponds to the position of the AUD start code
54  // in the stream. Otherwise, |*stream_pos| corresponds to the last position
55  // of the start code parser.
56  bool FindAUD(int64_t* stream_pos);
57 
58  // Resumes the H264 ES parsing.
59  // Return true if successful.
60  bool ParseInternal();
61 
62  // Emit a frame whose position in the ES queue starts at |access_unit_pos|.
63  // Returns true if successful, false if no PTS is available for the frame.
64  bool EmitFrame(int64_t access_unit_pos,
65  int access_unit_size,
66  bool is_key_frame,
67  int pps_id);
68 
69  // Update the video decoder config based on an H264 SPS.
70  // Return true if successful.
71  bool UpdateVideoDecoderConfig(const H264SPS* sps);
72 
73  // Callbacks to pass the stream configuration and the frames.
74  NewStreamInfoCB new_stream_info_cb_;
75  EmitSampleCB emit_sample_cb_;
76 
77  // Bytes of the ES stream that have not been emitted yet.
78  scoped_ptr<media::OffsetByteQueue> es_queue_;
79  std::list<std::pair<int64_t, TimingDesc> > timing_desc_list_;
80 
81  // H264 parser state.
82  // - |current_access_unit_pos_| is pointing to an annexB syncword
83  // representing the first NALU of an H264 access unit.
84  scoped_ptr<H264Parser> h264_parser_;
85  int64_t current_access_unit_pos_;
86  int64_t next_access_unit_pos_;
87 
88  // Filter to convert H.264 Annex B byte stream to unit stream.
89  scoped_ptr<H264ByteToUnitStreamConverter> stream_converter_;
90 
91  // Last video decoder config.
92  scoped_refptr<StreamInfo> last_video_decoder_config_;
93  bool decoder_config_check_pending_;
94 
95  // Frame for which we do not yet have a duration.
96  scoped_refptr<MediaSample> pending_sample_;
97  uint64_t pending_sample_duration_;
98 
99  // Indicates whether waiting for first key frame.
100  bool waiting_for_key_frame_;
101 };
102 
103 } // namespace mp2t
104 } // namespace media
105 } // namespace edash_packager
106 
107 #endif