Shaka Packager SDK
Loading...
Searching...
No Matches
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 PACKAGER_MEDIA_FORMATS_MP2T_ES_PARSER_H26x_H_
6#define PACKAGER_MEDIA_FORMATS_MP2T_ES_PARSER_H26x_H_
7
8#include <cstdint>
9#include <deque>
10#include <functional>
11#include <list>
12#include <memory>
13
14#include <packager/macros/classes.h>
15#include <packager/media/codecs/nalu_reader.h>
16#include <packager/media/formats/mp2t/es_parser.h>
17#include <functional>
18
19namespace shaka {
20namespace media {
21
22class H26xByteToUnitStreamConverter;
23class OffsetByteQueue;
24
25namespace mp2t {
26
27// A base class for common code between the H.264/H.265 es parsers.
28class 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 bool Flush() override;
39 void Reset() override;
40
41 protected:
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 // Calculates frame duration based on SPS frame data
100 virtual int64_t CalculateSampleDuration(int pps_id) = 0;
101
102 // Callback to pass the frames.
103 EmitSampleCB emit_sample_cb_;
104
105 // The type of stream being parsed.
106 Nalu::CodecType type_;
107
108 // Bytes of the ES stream that have not been emitted yet.
109 std::unique_ptr<media::OffsetByteQueue> es_queue_;
110 std::list<std::pair<int64_t, TimingDesc>> timing_desc_list_;
111
112 // Parser state.
113 // The position of the search head.
114 uint64_t current_search_position_ = 0;
115 // Current access unit starting position.
116 uint64_t current_access_unit_position_ = 0;
117 // The VideoSliceInfo in the current access unit, useful for first vcl nalu
118 // detection (for H.264).
119 VideoSliceInfo current_video_slice_info_;
120 bool next_access_unit_position_set_ = false;
121 uint64_t next_access_unit_position_ = 0;
122 // Current nalu information.
123 std::unique_ptr<NaluInfo> current_nalu_info_;
124 // This is really a temporary storage for the next nalu information.
125 std::unique_ptr<NaluInfo> next_nalu_info_;
126
127 // Filter to convert H.264/H.265 Annex B byte stream to unit stream.
128 std::unique_ptr<H26xByteToUnitStreamConverter> stream_converter_;
129
130 // Frame for which we do not yet have a duration.
131 std::shared_ptr<MediaSample> pending_sample_;
132 int pending_sample_pps_id_ = -1;
133 int64_t pending_sample_duration_ = 0;
134
135 // Indicates whether waiting for first key frame.
136 bool waiting_for_key_frame_ = true;
137};
138
139} // namespace mp2t
140} // namespace media
141} // namespace shaka
142
143#endif
A base class that is used to convert H.26x byte streams to NAL unit streams.
All the methods that are virtual are virtual for mocking.