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