1 /*
2    mkvmerge -- utility for splicing together matroska files
3    from component media subtypes
4 
5    Distributed under the GPL v2
6    see the file COPYING for details
7    or visit https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
8 
9    class definitions for the RealMedia demultiplexer module
10 
11    Written by Moritz Bunkus <moritz@bunkus.org>.
12 */
13 
14 #pragma once
15 
16 #include "common/common_pch.h"
17 
18 #include "common/error.h"
19 #include "librmff/librmff.h"
20 #include "output/p_video_for_windows.h"
21 #include "merge/generic_reader.h"
22 
23 struct rv_segment_t {
24   memory_cptr data;
25   uint64_t flags;
26 };
27 
28 using rv_segment_cptr = std::shared_ptr<rv_segment_t>;
29 
30 struct real_demuxer_t {
31   int ptzr;
32   rmff_track_t *track;
33 
34   int bsid;
35   unsigned int channels, samples_per_second, bits_per_sample;
36   unsigned int width, height;
37   char fourcc[5];
38   bool is_aac;
39   bool rv_dimensions;
40   bool force_keyframe_flag;
41   bool cook_audio_fix;
42   double fps;
43 
44   real_video_props_t *rvp;
45   real_audio_v4_props_t *ra4p;
46   real_audio_v5_props_t *ra5p;
47 
48   memory_cptr private_data, extra_data;
49 
50   bool first_frame;
51   int num_packets;
52   uint64_t last_timestamp;
53   int64_t ref_timestamp;         // can be negative
54 
55   std::vector<rv_segment_cptr> segments;
56 
real_demuxer_treal_demuxer_t57   real_demuxer_t(rmff_track_t *n_track):
58     ptzr(-1),
59     track(n_track),
60     bsid(0),
61     channels(0),
62     samples_per_second(0),
63     bits_per_sample(0),
64     width(0),
65     height(0),
66     is_aac(false),
67     rv_dimensions(false),
68     force_keyframe_flag(false),
69     cook_audio_fix(false),
70     fps(0.0),
71     rvp(nullptr),
72     ra4p(nullptr),
73     ra5p(nullptr),
74     first_frame(true),
75     num_packets(0),
76     last_timestamp(0),
77     ref_timestamp(0) {
78 
79     memset(fourcc, 0, 5);
80   };
81 };
82 
83 using real_demuxer_cptr = std::shared_ptr<real_demuxer_t>;
84 
85 class real_reader_c: public generic_reader_c {
86 private:
87   rmff_file_t *file;
88   std::vector<std::shared_ptr<real_demuxer_t> > demuxers;
89   bool done;
90 
91 public:
92   virtual ~real_reader_c();
93 
get_format_type()94   virtual mtx::file_type_e get_format_type() const {
95     return mtx::file_type_e::real;
96   }
97 
98   virtual void read_headers();
99   virtual void identify();
100   virtual void create_packetizers();
101   virtual void create_packetizer(int64_t tid);
102   virtual void add_available_track_ids();
103 
104   virtual bool probe_file() override;
105 
106 protected:
107   virtual file_status_e read(generic_packetizer_c *packetizer, bool force = false) override;
108 
109   virtual void parse_headers();
110   virtual real_demuxer_cptr find_demuxer(unsigned int id);
111   virtual void assemble_video_packet(real_demuxer_cptr dmx, rmff_frame_t *frame);
112   virtual file_status_e finish();
113   virtual bool get_rv_dimensions(unsigned char *buf, int size, uint32_t &width, uint32_t &height);
114   virtual void set_dimensions(real_demuxer_cptr dmx, unsigned char *buffer, int size);
115   virtual void get_information_from_data();
116   virtual void deliver_aac_frames(real_demuxer_cptr dmx, memory_c &mem);
117   virtual void queue_audio_frames(real_demuxer_cptr dmx, memory_cptr const &mem, uint64_t timestamp, uint32_t flags);
118   virtual void queue_one_audio_frame(real_demuxer_cptr dmx, memory_cptr const &mem, uint64_t timestamp, uint32_t flags);
119   virtual void deliver_audio_frames(real_demuxer_cptr dmx, uint64_t duration);
120 
121   virtual void create_audio_packetizer(real_demuxer_cptr dmx);
122   virtual void create_aac_audio_packetizer(real_demuxer_cptr dmx);
123   virtual void create_dnet_audio_packetizer(real_demuxer_cptr dmx);
124   virtual void create_video_packetizer(real_demuxer_cptr dmx);
125 };
126