xref: /haiku/headers/os/media/MediaTrack.h (revision 5d9e40fe9252c8f9c5e5e41594545bfa4419fcc7)
1 #ifndef _MEDIA_TRACK_H
2 #define _MEDIA_TRACK_H
3 
4 #include <SupportDefs.h>
5 #include <MediaDefs.h>
6 #include <MediaFormats.h>
7 
8 namespace BPrivate {
9 	class MediaWriter;
10 	namespace media {
11 		class MediaExtractor;
12 		class Decoder;
13 		class Encoder;
14 	}
15 }
16 
17 class BView;
18 class BParameterWeb;
19 
20 enum media_seek_type {
21 	B_MEDIA_SEEK_CLOSEST_FORWARD = 1,
22 	B_MEDIA_SEEK_CLOSEST_BACKWARD = 2,
23 	B_MEDIA_SEEK_DIRECTION_MASK = 3
24 };
25 
26 //
27 // BMediaTrack gives access to a particular media track in a media file
28 // (as represented by BMediaFile).
29 //
30 // You always instantiate a BMediaTrack through BMediaFile::TrackAt()
31 // or BMediaFile::CreateTrack().  When a BMediaTrack object is
32 // constructed it finds the necessary decoder or encoder for the type
33 // of data stored in the track.
34 //
35 // Unless you created the BMediaFile() in B_MEDIA_REPLACE_MODE, you
36 // can only access a track for reading or writing, not both.
37 //
38 // If InitCheck() indicates no errors, then the track is ready to be
39 // used to read and write frames using ReadFrames() and WriteFrames().
40 // For video data you should always only read one frame.
41 //
42 // You can seek a track with SeekToTime() and SeekToFrame().
43 //
44 // If no codec could be found for the track, it is still possible to
45 // access the encoded data using ReadChunk().
46 //
47 class BMediaTrack {
48 
49 protected:
50 
51 	// Use BMediaFile::ReleaseTrack() instead -- or it will go away
52 	// on its own when the MediaFile is deleted.
53 	//
54 	virtual			~BMediaTrack();
55 
56 public:
57 
58 	// for read-only access the BMediaTrack should be instantiated
59 	// through BMediaFile::TrackAt()
60 
61 	// for write-only access the BMediaTrack should be instantiated
62 	// through BMediaFile::CreateTrack()
63 
64 	status_t		InitCheck() const;
65 
66 	// Get information about the codec being used.
67 	status_t		GetCodecInfo(media_codec_info *mci) const;
68 
69 
70 	// EncodedFormat returns information about the track's
71 	// "native" encoded format.
72 
73 	status_t		EncodedFormat(media_format *out_format) const;
74 
75 	// DecodedFormat is used to negotiate the format that the codec will
76 	// use when decoding the track's data.  You pass in the format that
77 	// that you want; the codec will find and return its "best fit"
78 	// format.  (inout_format is used as both the input and the returned
79 	// format.)  The format is typically of the B_MEDIA_RAW_AUDIO or
80 	// B_MEDIA_RAW_VIDEO flavor.
81 	// The data returned through ReadFrames() will be in the format that's
82  	// returned by this function.
83 
84 	status_t		DecodedFormat(media_format *inout_format);
85 
86 	// CountFrames and Duration return the total number of frame and the
87 	// total duration (expressed in microseconds) of a track.
88 
89 	int64			CountFrames() const;
90 	bigtime_t		Duration() const;
91 
92 	// CurrentFrame and CurrentTime return the current position (expressed in
93 	// microseconds) within the track, expressed in frame index and time.
94 
95 	int64			CurrentFrame() const;
96 	bigtime_t		CurrentTime() const;
97 
98 	// ReadFrames() fills a buffer with the next frames/samples. For a video
99 	// track,  it decodes the next frame of video in the passed buffer. For
100 	// an audio track, it fills the buffers with the next N samples, as
101 	// negotiated by DecodedFormat().  However, if it reaches the end of the
102 	// file and was not able to fill the whole  buffer, it returns a partial
103 	// buffer.  Upon return, out_frameCount contains the actual number of
104 	// frame/samples returned, and the start time for the frame, expressed
105 	// in microseconds, is in the media_header structure.
106 
107 	status_t		ReadFrames(void *out_buffer, int64 *out_frameCount,
108 							   media_header *mh = NULL);
109 
110 	status_t		ReadFrames(void *out_buffer, int64 *out_frameCount,
111 							   media_header *mh, media_decode_info *info);
112 
113 	status_t		ReplaceFrames(const void *in_buffer, int64 *io_frameCount,
114 								  const media_header *mh);
115 
116 
117 	// SeekToTime and SeekToFrame are used for seeking to a particular
118 	// position in a track, expressed in either frames or microseconds.
119 	// They return whatever position they were able to seek to. For example,
120 	// a video codec may not be able to seek to arbitrary frames, but only to
121 	// key frames. In this case, it would return the closest key frame before
122 	// the specified seek point.
123 	//
124 	// If you want to explicitly seek to the nearest keyframe _before_ this
125 	// frame or _after_ this frame, pass B_MEDIA_SEEK_CLOSEST_FORWARD or
126 	// B_MEDIA_SEEK_CLOSEST_BACKWARD as the flags field.
127 	//
128 
129 	status_t		SeekToTime(bigtime_t *inout_time, int32 flags=0);
130 	status_t		SeekToFrame(int64 *inout_frame, int32 flags=0);
131 
132 	status_t		FindKeyFrameForTime(bigtime_t *inout_time, int32 flags=0) const;
133 	status_t		FindKeyFrameForFrame(int64 *inout_frame, int32 flags=0) const;
134 
135 	// ReadChunk returns, in out_buffer, the next out_size bytes of
136 	// data from the track.  The data is not decoded -- it will be
137 	// in its native encoded format (as specified by EncodedFormat()).
138 	// You can not mix calling ReadChunk() and ReadFrames() -- either
139 	// you access the track raw (i.e. with ReadChunk) or you access
140 	// it with ReadFrames.
141 
142 	status_t		ReadChunk(char **out_buffer, int32 *out_size,
143 							  media_header *mh = NULL);
144 
145 
146 	//
147 	// Write-only Functions
148 	//
149 	status_t		AddCopyright(const char *data);
150 	status_t		AddTrackInfo(uint32 code, const void *data, size_t size,
151 								 uint32 flags = 0);
152 
153 	//
154 	// Write num_frames of data to the track. This data is passed
155 	// through the encoder that was specified when the MediaTrack
156 	// was constructed.
157 	// Pass B_MEDIA_KEY_FRAME for flags if it is.
158 	//
159 	status_t		WriteFrames(const void *data, int32 num_frames,
160 								int32 flags = 0);
161 	status_t		WriteFrames(const void *data, int64 num_frames,
162 								media_encode_info *info);
163 
164 	//
165 	// Write a raw chunk of (presumably already encoded data) to
166 	// the file.
167 	// Pass B_MEDIA_KEY_FRAME for flags if it is.
168 	//
169 	status_t		WriteChunk(const void *data, size_t size,
170 							   uint32 flags = 0);
171 	status_t		WriteChunk(const void *data, size_t size,
172 							   media_encode_info *info);
173 
174 	// Flush all buffered encoded datas to disk. You should call it after
175 	// writing the last frame to be sure all datas are flushed at the right
176 	// offset into the file.
177 	status_t		Flush();
178 
179 	// These are for controlling the underlying encoder and track parameters
180 	BParameterWeb	*Web();
181 	status_t 		GetParameterValue(int32 id, void *valu, size_t *size);
182 	status_t		SetParameterValue(int32 id, const void *valu, size_t size);
183 	BView			*GetParameterView();
184 
185 	// This is a simplified control API, only one parameter low=0.0, high=1.0
186 	// Return B_ERROR if it's not supported by the current encoder.
187 	status_t		GetQuality(float *quality);
188 	status_t		SetQuality(float quality);
189 
190 	status_t 		GetEncodeParameters(encode_parameters *parameters) const;
191 	status_t 		SetEncodeParameters(encode_parameters *parameters);
192 
193 
194 virtual	status_t Perform(int32 selector, void * data);
195 
196 private:
197 	friend class BMediaFile;
198 
199 					// for read-only access to a track
200 					BMediaTrack(BPrivate::media::MediaExtractor *extractor, int32 stream);
201 
202 					// for write-only access to a BMediaTrack
203 					BMediaTrack(BPrivate::MediaWriter *writer,
204 								int32 stream_num,
205 								media_format *in_format,
206 								BPrivate::media::Encoder *encoder,
207 								media_codec_info *mci);
208 
209 	status_t				fErr;
210 	BPrivate::media::Decoder *fDecoder;
211 	int32					__unused__fDecoderID;
212 	BPrivate::media::MediaExtractor *fExtractor;
213 
214 	int32					fStream;
215 	int64					fCurFrame;
216 	bigtime_t				fCurTime;
217 
218 	media_codec_info		fMCI;
219 
220 	BPrivate::media::Encoder *fEncoder;
221 	int32					fEncoderID;
222 	BPrivate::MediaWriter	*fWriter;
223 	media_format			fWriterFormat;
224 	void					*__unused__fExtractorCookie;
225 
226 protected:
227 	int32			EncoderID() { return fEncoderID; };
228 
229 private:
230 
231 	BMediaTrack();
232 	BMediaTrack(const BMediaTrack&);
233 	BMediaTrack& operator=(const BMediaTrack&);
234 
235 	/* fbc data and virtuals */
236 
237 	uint32 _reserved_BMediaTrack_[31];
238 
239 virtual	status_t _Reserved_BMediaTrack_0(int32 arg, ...);
240 virtual	status_t _Reserved_BMediaTrack_1(int32 arg, ...);
241 virtual	status_t _Reserved_BMediaTrack_2(int32 arg, ...);
242 virtual	status_t _Reserved_BMediaTrack_3(int32 arg, ...);
243 virtual	status_t _Reserved_BMediaTrack_4(int32 arg, ...);
244 virtual	status_t _Reserved_BMediaTrack_5(int32 arg, ...);
245 virtual	status_t _Reserved_BMediaTrack_6(int32 arg, ...);
246 virtual	status_t _Reserved_BMediaTrack_7(int32 arg, ...);
247 virtual	status_t _Reserved_BMediaTrack_8(int32 arg, ...);
248 virtual	status_t _Reserved_BMediaTrack_9(int32 arg, ...);
249 virtual	status_t _Reserved_BMediaTrack_10(int32 arg, ...);
250 virtual	status_t _Reserved_BMediaTrack_11(int32 arg, ...);
251 virtual	status_t _Reserved_BMediaTrack_12(int32 arg, ...);
252 virtual	status_t _Reserved_BMediaTrack_13(int32 arg, ...);
253 virtual	status_t _Reserved_BMediaTrack_14(int32 arg, ...);
254 virtual	status_t _Reserved_BMediaTrack_15(int32 arg, ...);
255 virtual	status_t _Reserved_BMediaTrack_16(int32 arg, ...);
256 virtual	status_t _Reserved_BMediaTrack_17(int32 arg, ...);
257 virtual	status_t _Reserved_BMediaTrack_18(int32 arg, ...);
258 virtual	status_t _Reserved_BMediaTrack_19(int32 arg, ...);
259 virtual	status_t _Reserved_BMediaTrack_20(int32 arg, ...);
260 virtual	status_t _Reserved_BMediaTrack_21(int32 arg, ...);
261 virtual	status_t _Reserved_BMediaTrack_22(int32 arg, ...);
262 virtual	status_t _Reserved_BMediaTrack_23(int32 arg, ...);
263 virtual	status_t _Reserved_BMediaTrack_24(int32 arg, ...);
264 virtual	status_t _Reserved_BMediaTrack_25(int32 arg, ...);
265 virtual	status_t _Reserved_BMediaTrack_26(int32 arg, ...);
266 virtual	status_t _Reserved_BMediaTrack_27(int32 arg, ...);
267 virtual	status_t _Reserved_BMediaTrack_28(int32 arg, ...);
268 virtual	status_t _Reserved_BMediaTrack_29(int32 arg, ...);
269 virtual	status_t _Reserved_BMediaTrack_30(int32 arg, ...);
270 virtual	status_t _Reserved_BMediaTrack_31(int32 arg, ...);
271 virtual	status_t _Reserved_BMediaTrack_32(int32 arg, ...);
272 virtual	status_t _Reserved_BMediaTrack_33(int32 arg, ...);
273 virtual	status_t _Reserved_BMediaTrack_34(int32 arg, ...);
274 virtual	status_t _Reserved_BMediaTrack_35(int32 arg, ...);
275 virtual	status_t _Reserved_BMediaTrack_36(int32 arg, ...);
276 virtual	status_t _Reserved_BMediaTrack_37(int32 arg, ...);
277 virtual	status_t _Reserved_BMediaTrack_38(int32 arg, ...);
278 virtual	status_t _Reserved_BMediaTrack_39(int32 arg, ...);
279 virtual	status_t _Reserved_BMediaTrack_40(int32 arg, ...);
280 virtual	status_t _Reserved_BMediaTrack_41(int32 arg, ...);
281 virtual	status_t _Reserved_BMediaTrack_42(int32 arg, ...);
282 virtual	status_t _Reserved_BMediaTrack_43(int32 arg, ...);
283 virtual	status_t _Reserved_BMediaTrack_44(int32 arg, ...);
284 virtual	status_t _Reserved_BMediaTrack_45(int32 arg, ...);
285 virtual	status_t _Reserved_BMediaTrack_46(int32 arg, ...);
286 virtual	status_t _Reserved_BMediaTrack_47(int32 arg, ...);
287 };
288 
289 #endif
290