xbmc
DVDDemuxFFmpeg.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "DVDDemux.h"
12 #include "threads/CriticalSection.h"
13 #include "threads/SystemClock.h"
14 #include <map>
15 #include <memory>
16 #include <vector>
17 
18 extern "C" {
19 #include <libavformat/avformat.h>
20 }
21 
22 class CDVDDemuxFFmpeg;
23 class CURL;
24 
25 enum class TRANSPORT_STREAM_STATE
26 {
27  NONE,
28  READY,
29  NOTREADY,
30 };
31 
33 {
34 public:
35  explicit CDemuxStreamVideoFFmpeg(AVStream* stream) : m_stream(stream) {}
36  std::string GetStreamName() override;
37 
38  std::string m_description;
39 protected:
40  AVStream* m_stream = nullptr;
41 };
42 
44 {
45 public:
46  explicit CDemuxStreamAudioFFmpeg(AVStream* stream) : m_stream(stream) {}
47  std::string GetStreamName() override;
48 
49  std::string m_description;
50 protected:
51  CDVDDemuxFFmpeg* m_parent;
52  AVStream* m_stream = nullptr;
53 };
54 
56  : public CDemuxStreamSubtitle
57 {
58 public:
59  explicit CDemuxStreamSubtitleFFmpeg(AVStream* stream) : m_stream(stream) {}
60  std::string GetStreamName() override;
61 
62  std::string m_description;
63 protected:
64  CDVDDemuxFFmpeg* m_parent;
65  AVStream* m_stream = nullptr;
66 };
67 
69 {
70 public:
72  AVCodecParserContext* m_parserCtx = nullptr;
73  AVCodecContext* m_codecCtx = nullptr;
74 };
75 
76 #define FFMPEG_DVDNAV_BUFFER_SIZE 2048 // for dvd's
77 
79 
80 class CDVDDemuxFFmpeg : public CDVDDemux
81 {
82 public:
84  ~CDVDDemuxFFmpeg() override;
85 
86  bool Open(const std::shared_ptr<CDVDInputStream>& pInput, bool fileinfo);
87  void Dispose();
88  bool Reset() override ;
89  void Flush() override;
90  void Abort() override;
91  void SetSpeed(int iSpeed) override;
92  std::string GetFileName() override;
93 
94  DemuxPacket* Read() override;
95  DemuxPacket* ReadInternal(bool keep);
96 
97  bool SeekTime(double time, bool backwards = false, double* startpts = NULL) override;
98  bool SeekByte(int64_t pos);
99  int GetStreamLength() override;
100  CDemuxStream* GetStream(int iStreamId) const override;
101  std::vector<CDemuxStream*> GetStreams() const override;
102  int GetNrOfStreams() const override;
103  int GetPrograms(std::vector<ProgramInfo>& programs) override;
104  void SetProgram(int progId) override;
105 
106  bool SeekChapter(int chapter, double* startpts = NULL) override;
107  int GetChapterCount() override;
108  int GetChapter() override;
109  void GetChapterName(std::string& strChapterName, int chapterIdx=-1) override;
110  int64_t GetChapterPos(int chapterIdx = -1) override;
111  std::string GetStreamCodecName(int iStreamId) override;
112 
113  bool Aborted();
114 
115  AVFormatContext* m_pFormatContext;
116  std::shared_ptr<CDVDInputStream> m_pInput;
117 
118 protected:
119  friend class CDemuxStreamAudioFFmpeg;
120  friend class CDemuxStreamVideoFFmpeg;
121  friend class CDemuxStreamSubtitleFFmpeg;
122 
123  CDemuxStream* AddStream(int streamIdx);
124  void AddStream(int streamIdx, CDemuxStream* stream);
125  void CreateStreams(unsigned int program = UINT_MAX);
126  void DisposeStreams();
127  void ParsePacket(AVPacket* pkt);
128  TRANSPORT_STREAM_STATE TransportStreamAudioState();
129  TRANSPORT_STREAM_STATE TransportStreamVideoState();
130  bool IsTransportStreamReady();
131  void ResetVideoStreams();
132  AVDictionary* GetFFMpegOptionsFromInput();
133  double ConvertTimestamp(int64_t pts, int den, int num);
134  bool IsProgramChange();
135  unsigned int HLSSelectProgram();
136 
137  std::string GetStereoModeFromMetadata(AVDictionary* pMetadata);
138  std::string ConvertCodecToInternalStereoMode(const std::string& mode, const StereoModeConversionMap* conversionMap);
139 
140  void GetL16Parameters(int& channels, int& samplerate);
141  double SelectAspect(AVStream* st, bool& forced);
142 
143  StreamHdrType DetermineHdrType(AVStream* pStream);
144 
145  CCriticalSection m_critSection;
146  std::map<int, CDemuxStream*> m_streams;
147  std::map<int, std::unique_ptr<CDemuxParserFFmpeg>> m_parsers;
148 
149  AVIOContext* m_ioContext;
150 
151  double m_currentPts; // used for stream length estimation
152  bool m_bMatroska;
153  bool m_bAVI;
154  bool m_bSup;
155  int m_speed;
156  unsigned int m_program;
157  unsigned int m_streamsInProgram;
158  unsigned int m_newProgram;
159  unsigned int m_initialProgramNumber;
160  int m_seekStream;
161 
162  XbmcThreads::EndTime<> m_timeout;
163 
164  // Due to limitations of ffmpeg, we only can detect a program change
165  // with a packet. This struct saves the packet for the next read and
166  // signals STREAMCHANGE to player
167  struct
168  {
169  AVPacket pkt; // packet ffmpeg returned
170  int result; // result from av_read_packet
171  }m_pkt;
172 
173  bool m_streaminfo;
174  bool m_reopen = false;
175  bool m_checkTransportStream;
176  int m_displayTime = 0;
177  double m_dtsAtDisplayTime;
178  bool m_seekToKeyFrame = false;
179  double m_startTime = 0;
180 };
181 
Definition: DVDDemux.h:220
Definition: DVDDemuxFFmpeg.h:68
Definition: URL.h:21
Definition: SystemClock.h:31
Definition: DVDDemux.h:156
Definition: DVDDemuxFFmpeg.h:80
Definition: DVDDemux.h:124
Definition: DVDDemuxFFmpeg.h:43
Definition: DVDDemuxFFmpeg.cpp:66
Definition: DVDDemuxFFmpeg.h:55
Definition: DVDDemux.h:184
Definition: LibInputPointer.h:13
Definition: DVDDemuxFFmpeg.h:32
Definition: DVDDemux.h:72
Definition: DemuxPacket.h:22