DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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 <list>
11 
12 #include "packager/base/callback.h"
13 #include "packager/base/compiler_specific.h"
14 #include "packager/base/memory/scoped_ptr.h"
15 #include "packager/media/filters/nalu_reader.h"
16 #include "packager/media/formats/mp2t/es_parser.h"
17 
18 namespace edash_packager {
19 namespace media {
20 
21 class H26xByteToUnitStreamConverter;
22 class OffsetByteQueue;
23 
24 namespace mp2t {
25 
26 // A base class for common code between the H.264/H.265 es parsers.
27 class EsParserH26x : public EsParser {
28  public:
29  EsParserH26x(Nalu::CodecType type,
30  scoped_ptr<H26xByteToUnitStreamConverter> stream_converter,
31  uint32_t pid,
32  const EmitSampleCB& emit_sample_cb);
33  ~EsParserH26x() override;
34 
35  // EsParser implementation overrides.
36  bool Parse(const uint8_t* buf, int size, int64_t pts, int64_t dts) override;
37  void Flush() override;
38  void Reset() override;
39 
40  protected:
41  const H26xByteToUnitStreamConverter* stream_converter() const {
42  return stream_converter_.get();
43  }
44 
45  private:
46  struct TimingDesc {
47  int64_t dts;
48  int64_t pts;
49  };
50 
51  // Processes a NAL unit found in ParseInternal. The @a pps_id_for_access_unit
52  // value will be passed to UpdateVideoDecoderConfig.
53  virtual bool ProcessNalu(const Nalu& nalu,
54  bool* is_key_frame,
55  int* pps_id_for_access_unit) = 0;
56 
57  // Update the video decoder config.
58  // Return true if successful.
59  virtual bool UpdateVideoDecoderConfig(int pps_id) = 0;
60 
61  // Find the start of the next access unit staring at |stream_pos|.
62  // Return true if the end is found.
63  // If found, |*next_unit_start| contains the start of the next access unit.
64  // Otherwise, |*next_unit_start| is unchanged.
65  bool FindNextAccessUnit(int64_t stream_pos, int64_t* next_unit_start);
66 
67  // Resumes the H264 ES parsing.
68  // Return true if successful.
69  bool ParseInternal();
70 
71  // Emit a frame whose position in the ES queue starts at |access_unit_pos|.
72  // Returns true if successful, false if no PTS is available for the frame.
73  bool EmitFrame(int64_t access_unit_pos,
74  int access_unit_size,
75  bool is_key_frame,
76  int pps_id);
77 
78  // Callback to pass the frames.
79  EmitSampleCB emit_sample_cb_;
80 
81  // The type of stream being parsed.
82  Nalu::CodecType type_;
83 
84  // Bytes of the ES stream that have not been emitted yet.
85  scoped_ptr<media::OffsetByteQueue> es_queue_;
86  std::list<std::pair<int64_t, TimingDesc>> timing_desc_list_;
87 
88  // Parser state.
89  // - |current_access_unit_pos_| is pointing to an annexB syncword
90  // representing the first NALU of an access unit.
91  int64_t current_access_unit_pos_;
92  bool found_access_unit_;
93 
94  // Filter to convert H.264/H.265 Annex B byte stream to unit stream.
95  scoped_ptr<H26xByteToUnitStreamConverter> stream_converter_;
96 
97  // Frame for which we do not yet have a duration.
98  scoped_refptr<MediaSample> pending_sample_;
99  uint64_t pending_sample_duration_;
100 
101  // Indicates whether waiting for first key frame.
102  bool waiting_for_key_frame_;
103 };
104 
105 } // namespace mp2t
106 } // namespace media
107 } // namespace edash_packager
108 
109 #endif
A base class that is used to convert H.26x byte streams to NAL unit streams.