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 
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/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  scoped_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  const H26xByteToUnitStreamConverter* stream_converter() const {
43  return stream_converter_.get();
44  }
45 
46  private:
47  struct TimingDesc {
48  int64_t dts;
49  int64_t pts;
50  };
51  struct NaluInfo {
52  // NOTE: Nalu does not own the memory pointed by its data pointers. The
53  // caller owns and maintains the memory.
54  Nalu nalu;
55  // The offset of the NALU from the beginning of the stream, usable as an
56  // argument to OffsetByteQueue. This points to the start code.
57  uint64_t position;
58  uint8_t start_code_size;
59  };
60 
61  // Processes a NAL unit found in ParseInternal. The @a pps_id_for_access_unit
62  // value will be passed to UpdateVideoDecoderConfig.
63  virtual bool ProcessNalu(const Nalu& nalu,
64  bool* is_key_frame,
65  int* pps_id_for_access_unit) = 0;
66 
67  // Update the video decoder config.
68  // Return true if successful.
69  virtual bool UpdateVideoDecoderConfig(int pps_id) = 0;
70 
71  // Skips to the first access unit available. Returns whether an access unit
72  // is found.
73  bool SkipToFirstAccessUnit();
74 
75  // Finds the next NAL unit by finding the next start code. This will modify
76  // the search position.
77  // Returns true when it has found the next NALU.
78  bool SearchForNextNalu();
79 
80  // Process an access unit that spans the given NAL units (end is exclusive
81  // and should point to a valid object).
82  bool ProcessAccessUnit(std::deque<NaluInfo>::iterator end);
83 
84  // Resumes the H26x ES parsing.
85  // Return true if successful.
86  bool ParseInternal();
87 
88  // Emit a frame whose position in the ES queue starts at |access_unit_pos|.
89  // Returns true if successful, false if no PTS is available for the frame.
90  bool EmitFrame(int64_t access_unit_pos,
91  int access_unit_size,
92  bool is_key_frame,
93  int pps_id);
94 
95  // Callback to pass the frames.
96  EmitSampleCB emit_sample_cb_;
97 
98  // The type of stream being parsed.
99  Nalu::CodecType type_;
100 
101  // Bytes of the ES stream that have not been emitted yet.
102  scoped_ptr<media::OffsetByteQueue> es_queue_;
103  std::list<std::pair<int64_t, TimingDesc>> timing_desc_list_;
104 
105  // Parser state.
106  // The position of the search head.
107  uint64_t current_search_position_;
108  // The NALU that make up the current access unit. This may include elements
109  // from the next access unit. The last item is the NAL unit currently
110  // being processed.
111  std::deque<NaluInfo> access_unit_nalus_;
112 
113  // Filter to convert H.264/H.265 Annex B byte stream to unit stream.
114  scoped_ptr<H26xByteToUnitStreamConverter> stream_converter_;
115 
116  // Frame for which we do not yet have a duration.
117  scoped_refptr<MediaSample> pending_sample_;
118  uint64_t pending_sample_duration_;
119 
120  // Indicates whether waiting for first key frame.
121  bool waiting_for_key_frame_;
122 };
123 
124 } // namespace mp2t
125 } // namespace media
126 } // namespace shaka
127 
128 #endif
A base class that is used to convert H.26x byte streams to NAL unit streams.