DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
es_parser_h26x.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_H26x_H_
6 #define MEDIA_FORMATS_MP2T_ES_PARSER_H26x_H_
7 
8 #include <stdint.h>
9 
10 #include <deque>
11 #include <list>
12 #include <memory>
13 
14 #include "packager/base/callback.h"
15 #include "packager/base/compiler_specific.h"
16 #include "packager/media/codecs/nalu_reader.h"
17 #include "packager/media/formats/mp2t/es_parser.h"
18 
19 namespace shaka {
20 namespace media {
21 
22 class H26xByteToUnitStreamConverter;
23 class OffsetByteQueue;
24 
25 namespace mp2t {
26 
27 // A base class for common code between the H.264/H.265 es parsers.
28 class EsParserH26x : public EsParser {
29  public:
30  EsParserH26x(Nalu::CodecType type,
31  std::unique_ptr<H26xByteToUnitStreamConverter> stream_converter,
32  uint32_t pid,
33  const EmitSampleCB& emit_sample_cb);
34  ~EsParserH26x() override;
35 
36  // EsParser implementation overrides.
37  bool Parse(const uint8_t* buf, int size, int64_t pts, int64_t dts) override;
38  void Flush() override;
39  void Reset() override;
40 
41  protected:
42  struct VideoSliceInfo {
43  bool valid = false;
44  bool is_key_frame = false;
45  // Both pps_id and frame_num are extracted from slice header (frame_num is
46  // only for H.264).
47  int pps_id = 0;
48  int frame_num = 0;
49  };
50 
51  const H26xByteToUnitStreamConverter* stream_converter() const {
52  return stream_converter_.get();
53  }
54 
55  private:
56  struct TimingDesc {
57  int64_t dts;
58  int64_t pts;
59  };
60  struct NaluInfo {
61  // NOTE: Nalu does not own the memory pointed by its data pointers. The
62  // caller owns and maintains the memory.
63  Nalu nalu;
64  // The offset of the NALU from the beginning of the stream, usable as an
65  // argument to OffsetByteQueue. This points to the start code.
66  uint64_t position = 0;
67  uint8_t start_code_size = 0;
68  };
69 
70  // Processes a NAL unit found in ParseInternal. |video_slice_info| should not
71  // be null, it will contain the video slice info if it is a video slice nalu
72  // and it is processed successfully; otherwise the |valid| member will be set
73  // to false with other members untouched.
74  virtual bool ProcessNalu(const Nalu& nalu,
75  VideoSliceInfo* video_slice_info) = 0;
76 
77  // Update the video decoder config.
78  // Return true if successful.
79  virtual bool UpdateVideoDecoderConfig(int pps_id) = 0;
80 
81  // Finds the NAL unit by finding the next start code. This will modify the
82  // search position.
83  // Returns true when it has found the NALU.
84  bool SearchForNalu(uint64_t* position, Nalu* nalu);
85 
86  // Resumes the H26x ES parsing.
87  // Return true if successful.
88  bool ParseInternal();
89 
90  // Emit the current access unit if exists.
91  bool EmitCurrentAccessUnit();
92 
93  // Emit a frame whose position in the ES queue starts at |access_unit_pos|.
94  // Returns true if successful, false if no PTS is available for the frame.
95  bool EmitFrame(int64_t access_unit_pos,
96  int access_unit_size,
97  bool is_key_frame,
98  int pps_id);
99 
100  // Callback to pass the frames.
101  EmitSampleCB emit_sample_cb_;
102 
103  // The type of stream being parsed.
104  Nalu::CodecType type_;
105 
106  // Bytes of the ES stream that have not been emitted yet.
107  std::unique_ptr<media::OffsetByteQueue> es_queue_;
108  std::list<std::pair<int64_t, TimingDesc>> timing_desc_list_;
109 
110  // Parser state.
111  // The position of the search head.
112  uint64_t current_search_position_ = 0;
113  // Current access unit starting position.
114  uint64_t current_access_unit_position_ = 0;
115  // The VideoSliceInfo in the current access unit, useful for first vcl nalu
116  // detection (for H.264).
117  VideoSliceInfo current_video_slice_info_;
118  bool next_access_unit_position_set_ = false;
119  uint64_t next_access_unit_position_ = 0;
120  // Current nalu information.
121  std::unique_ptr<NaluInfo> current_nalu_info_;
122  // This is really a temporary storage for the next nalu information.
123  std::unique_ptr<NaluInfo> next_nalu_info_;
124 
125  // Filter to convert H.264/H.265 Annex B byte stream to unit stream.
126  std::unique_ptr<H26xByteToUnitStreamConverter> stream_converter_;
127 
128  // Frame for which we do not yet have a duration.
129  std::shared_ptr<MediaSample> pending_sample_;
130  uint64_t pending_sample_duration_ = 0;
131 
132  // Indicates whether waiting for first key frame.
133  bool waiting_for_key_frame_ = true;
134 };
135 
136 } // namespace mp2t
137 } // namespace media
138 } // namespace shaka
139 
140 #endif
A base class that is used to convert H.26x byte streams to NAL unit streams.