2016-03-31 19:48:16 +00:00
|
|
|
// Copyright 2014 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_FORMATS_MP2T_ES_PARSER_H26x_H_
|
|
|
|
#define PACKAGER_MEDIA_FORMATS_MP2T_ES_PARSER_H26x_H_
|
2016-03-31 19:48:16 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <cstdint>
|
2016-05-03 22:05:45 +00:00
|
|
|
#include <deque>
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <functional>
|
2016-03-31 19:48:16 +00:00
|
|
|
#include <list>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2016-03-31 19:48:16 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/macros/classes.h>
|
|
|
|
#include <packager/media/codecs/nalu_reader.h>
|
|
|
|
#include <packager/media/formats/mp2t/es_parser.h>
|
|
|
|
#include <functional>
|
2016-03-31 19:48:16 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-03-31 19:48:16 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
class H26xByteToUnitStreamConverter;
|
|
|
|
class OffsetByteQueue;
|
|
|
|
|
|
|
|
namespace mp2t {
|
|
|
|
|
|
|
|
// A base class for common code between the H.264/H.265 es parsers.
|
|
|
|
class EsParserH26x : public EsParser {
|
|
|
|
public:
|
|
|
|
EsParserH26x(Nalu::CodecType type,
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<H26xByteToUnitStreamConverter> stream_converter,
|
2016-03-31 19:48:16 +00:00
|
|
|
uint32_t pid,
|
|
|
|
const EmitSampleCB& emit_sample_cb);
|
|
|
|
~EsParserH26x() override;
|
|
|
|
|
|
|
|
// EsParser implementation overrides.
|
|
|
|
bool Parse(const uint8_t* buf, int size, int64_t pts, int64_t dts) override;
|
2020-12-10 23:03:41 +00:00
|
|
|
bool Flush() override;
|
2016-03-31 19:48:16 +00:00
|
|
|
void Reset() override;
|
|
|
|
|
|
|
|
protected:
|
2016-08-25 22:44:25 +00:00
|
|
|
struct VideoSliceInfo {
|
|
|
|
bool valid = false;
|
|
|
|
bool is_key_frame = false;
|
|
|
|
// Both pps_id and frame_num are extracted from slice header (frame_num is
|
|
|
|
// only for H.264).
|
|
|
|
int pps_id = 0;
|
|
|
|
int frame_num = 0;
|
|
|
|
};
|
|
|
|
|
2016-03-31 19:48:16 +00:00
|
|
|
const H26xByteToUnitStreamConverter* stream_converter() const {
|
|
|
|
return stream_converter_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct TimingDesc {
|
|
|
|
int64_t dts;
|
|
|
|
int64_t pts;
|
|
|
|
};
|
2016-05-03 22:05:45 +00:00
|
|
|
struct NaluInfo {
|
|
|
|
// NOTE: Nalu does not own the memory pointed by its data pointers. The
|
|
|
|
// caller owns and maintains the memory.
|
|
|
|
Nalu nalu;
|
|
|
|
// The offset of the NALU from the beginning of the stream, usable as an
|
|
|
|
// argument to OffsetByteQueue. This points to the start code.
|
2016-08-25 22:44:25 +00:00
|
|
|
uint64_t position = 0;
|
|
|
|
uint8_t start_code_size = 0;
|
2016-05-03 22:05:45 +00:00
|
|
|
};
|
2016-03-31 19:48:16 +00:00
|
|
|
|
2016-08-25 22:44:25 +00:00
|
|
|
// Processes a NAL unit found in ParseInternal. |video_slice_info| should not
|
|
|
|
// be null, it will contain the video slice info if it is a video slice nalu
|
|
|
|
// and it is processed successfully; otherwise the |valid| member will be set
|
|
|
|
// to false with other members untouched.
|
2016-03-31 19:48:16 +00:00
|
|
|
virtual bool ProcessNalu(const Nalu& nalu,
|
2016-08-25 22:44:25 +00:00
|
|
|
VideoSliceInfo* video_slice_info) = 0;
|
2016-03-31 19:48:16 +00:00
|
|
|
|
|
|
|
// Update the video decoder config.
|
|
|
|
// Return true if successful.
|
|
|
|
virtual bool UpdateVideoDecoderConfig(int pps_id) = 0;
|
|
|
|
|
2016-08-25 22:44:25 +00:00
|
|
|
// Finds the NAL unit by finding the next start code. This will modify the
|
|
|
|
// search position.
|
|
|
|
// Returns true when it has found the NALU.
|
|
|
|
bool SearchForNalu(uint64_t* position, Nalu* nalu);
|
2016-03-31 19:48:16 +00:00
|
|
|
|
2016-05-03 22:05:45 +00:00
|
|
|
// Resumes the H26x ES parsing.
|
2016-03-31 19:48:16 +00:00
|
|
|
// Return true if successful.
|
|
|
|
bool ParseInternal();
|
|
|
|
|
2016-08-25 22:44:25 +00:00
|
|
|
// Emit the current access unit if exists.
|
|
|
|
bool EmitCurrentAccessUnit();
|
|
|
|
|
2016-03-31 19:48:16 +00:00
|
|
|
// Emit a frame whose position in the ES queue starts at |access_unit_pos|.
|
|
|
|
// Returns true if successful, false if no PTS is available for the frame.
|
|
|
|
bool EmitFrame(int64_t access_unit_pos,
|
|
|
|
int access_unit_size,
|
|
|
|
bool is_key_frame,
|
|
|
|
int pps_id);
|
2022-10-28 03:22:17 +00:00
|
|
|
// Calculates frame duration based on SPS frame data
|
|
|
|
virtual int64_t CalculateSampleDuration(int pps_id) = 0;
|
2016-03-31 19:48:16 +00:00
|
|
|
|
|
|
|
// Callback to pass the frames.
|
|
|
|
EmitSampleCB emit_sample_cb_;
|
|
|
|
|
|
|
|
// The type of stream being parsed.
|
|
|
|
Nalu::CodecType type_;
|
|
|
|
|
|
|
|
// Bytes of the ES stream that have not been emitted yet.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<media::OffsetByteQueue> es_queue_;
|
2016-03-31 19:48:16 +00:00
|
|
|
std::list<std::pair<int64_t, TimingDesc>> timing_desc_list_;
|
|
|
|
|
|
|
|
// Parser state.
|
2016-05-03 22:05:45 +00:00
|
|
|
// The position of the search head.
|
2016-08-25 22:44:25 +00:00
|
|
|
uint64_t current_search_position_ = 0;
|
|
|
|
// Current access unit starting position.
|
|
|
|
uint64_t current_access_unit_position_ = 0;
|
|
|
|
// The VideoSliceInfo in the current access unit, useful for first vcl nalu
|
|
|
|
// detection (for H.264).
|
|
|
|
VideoSliceInfo current_video_slice_info_;
|
|
|
|
bool next_access_unit_position_set_ = false;
|
|
|
|
uint64_t next_access_unit_position_ = 0;
|
|
|
|
// Current nalu information.
|
|
|
|
std::unique_ptr<NaluInfo> current_nalu_info_;
|
|
|
|
// This is really a temporary storage for the next nalu information.
|
|
|
|
std::unique_ptr<NaluInfo> next_nalu_info_;
|
2016-03-31 19:48:16 +00:00
|
|
|
|
|
|
|
// Filter to convert H.264/H.265 Annex B byte stream to unit stream.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<H26xByteToUnitStreamConverter> stream_converter_;
|
2016-03-31 19:48:16 +00:00
|
|
|
|
|
|
|
// Frame for which we do not yet have a duration.
|
2017-01-24 00:55:02 +00:00
|
|
|
std::shared_ptr<MediaSample> pending_sample_;
|
2022-10-28 03:22:17 +00:00
|
|
|
int pending_sample_pps_id_ = -1;
|
2021-08-04 18:56:44 +00:00
|
|
|
int64_t pending_sample_duration_ = 0;
|
2016-03-31 19:48:16 +00:00
|
|
|
|
|
|
|
// Indicates whether waiting for first key frame.
|
2016-08-25 22:44:25 +00:00
|
|
|
bool waiting_for_key_frame_ = true;
|
2016-03-31 19:48:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mp2t
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2016-03-31 19:48:16 +00:00
|
|
|
|
|
|
|
#endif
|