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