1227569Sphilip/*
2227569Sphilip * Copyright 2002-2010, Haiku, Inc. All rights reserved.
3227569Sphilip * Distributed under the terms of the MIT License.
4227569Sphilip */
5227569Sphilip#ifndef _MEDIA_TRACK_H
6227569Sphilip#define _MEDIA_TRACK_H
7227569Sphilip
8227569Sphilip
9227569Sphilip#include <MediaFormats.h>
10227569Sphilip
11227569Sphilip
12227569Sphilipnamespace BPrivate { namespace media {
13227569Sphilip	class Decoder;
14227569Sphilip	class Encoder;
15227569Sphilip	class MediaExtractor;
16227569Sphilip	class MediaWriter;
17227569Sphilip} }
18227569Sphilip
19227569Sphilipclass BMessage;
20227569Sphilipclass BView;
21227569Sphilipclass BParameterWeb;
22227569Sphilip
23227569Sphilip
24227569Sphilipenum media_seek_type {
25227569Sphilip	B_MEDIA_SEEK_CLOSEST_FORWARD	= 1,
26228100Sphilip	B_MEDIA_SEEK_CLOSEST_BACKWARD	= 2,
27228100Sphilip	B_MEDIA_SEEK_DIRECTION_MASK		= 3
28228100Sphilip};
29227569Sphilip
30227569Sphilip
31227569Sphilip// BMediaTrack gives access to a particular media track in a media file
32227569Sphilip// (as represented by BMediaFile).
33227569Sphilip//
34227569Sphilip// You always instantiate a BMediaTrack through BMediaFile::TrackAt()
35227569Sphilip// or BMediaFile::CreateTrack().  When a BMediaTrack object is
36227569Sphilip// constructed it finds the necessary decoder or encoder for the type
37227569Sphilip// of data stored in the track.
38227569Sphilip//
39227569Sphilip// Unless you created the BMediaFile() in B_MEDIA_REPLACE_MODE, you
40227569Sphilip// can only access a track for reading or writing, not both.
41227569Sphilip//
42227569Sphilip// If InitCheck() indicates no errors, then the track is ready to be
43227569Sphilip// used to read and write frames using ReadFrames() and WriteFrames().
44227569Sphilip// For video data you should always only read one frame.
45227569Sphilip//
46227569Sphilip// You can seek a track with SeekToTime() and SeekToFrame().
47227569Sphilip//
48227569Sphilip// If no codec could be found for the track, it is still possible to
49227569Sphilip// access the encoded data using ReadChunk().
50227569Sphilipclass BMediaTrack {
51227569Sphilipprotected:
52227569Sphilip	// Use BMediaFile::ReleaseTrack() instead -- or it will go away
53227569Sphilip	// on its own when the MediaFile is deleted.
54227569Sphilip	virtual						~BMediaTrack();
55227569Sphilip
56227569Sphilippublic:
57227569Sphilip
58227569Sphilip	// for read-only access the BMediaTrack should be instantiated
59227569Sphilip	// through BMediaFile::TrackAt()
60227569Sphilip
61227569Sphilip	// for write-only access the BMediaTrack should be instantiated
62227569Sphilip	// through BMediaFile::CreateTrack()
63227569Sphilip
64227569Sphilip			status_t			InitCheck() const;
65227569Sphilip
66227569Sphilip	// Get information about the codec being used.
67227569Sphilip			status_t			GetCodecInfo(
68227569Sphilip									media_codec_info* _codecInfo) const;
69227569Sphilip
70227569Sphilip
71227569Sphilip	// EncodedFormat returns information about the track's
72227569Sphilip	// "native" encoded format.
73227569Sphilip
74227569Sphilip			status_t			EncodedFormat(media_format* _format) const;
75227569Sphilip
76227569Sphilip	// DecodedFormat is used to negotiate the format that the codec will
77227569Sphilip	// use when decoding the track's data.  You pass in the format that
78227569Sphilip	// that you want; the codec will find and return its "best fit"
79227569Sphilip	// format.  (inout_format is used as both the input and the returned
80227569Sphilip	// format.)  The format is typically of the B_MEDIA_RAW_AUDIO or
81227569Sphilip	// B_MEDIA_RAW_VIDEO flavor.
82227569Sphilip	// The data returned through ReadFrames() will be in the format that's
83227569Sphilip 	// returned by this function.
84227569Sphilip
85227569Sphilip			status_t			DecodedFormat(media_format* _inOutFormat,
86227569Sphilip									uint32 flags = 0);
87227569Sphilip
88227569Sphilip	// CountFrames and Duration return the total number of frame and the
89227569Sphilip	// total duration (expressed in microseconds) of a track.
90227569Sphilip
91227569Sphilip			int64				CountFrames() const;
92227569Sphilip			bigtime_t			Duration() const;
93227569Sphilip
94227569Sphilip	// Returns in _data hierarchical meta-data about the track.
95227569Sphilip	// The fields in the message shall follow a defined naming-scheme,
96227569Sphilip	// such that applications can find the same information in different
97227569Sphilip	// types of tracks.
98227569Sphilip			status_t			GetMetaData(BMessage* _data) const;
99227569Sphilip
100227569Sphilip	// CurrentFrame and CurrentTime return the current position (expressed in
101227569Sphilip	// microseconds) within the track, expressed in frame index and time.
102227569Sphilip
103227569Sphilip			int64				CurrentFrame() const;
104227569Sphilip			bigtime_t			CurrentTime() const;
105227569Sphilip
106227569Sphilip	// ReadFrames() fills a buffer with the next frames/samples. For a video
107227569Sphilip	// track,  it decodes the next frame of video in the passed buffer. For
108227569Sphilip	// an audio track, it fills the buffers with the next N samples, as
109227569Sphilip	// negotiated by DecodedFormat().  However, if it reaches the end of the
110227569Sphilip	// file and was not able to fill the whole  buffer, it returns a partial
111227569Sphilip	// buffer.  Upon return, out_frameCount contains the actual number of
112227569Sphilip	// frame/samples returned, and the start time for the frame, expressed
113227569Sphilip	// in microseconds, is in the media_header structure.
114227569Sphilip
115227569Sphilip			status_t			ReadFrames(void* buffer, int64* _frameCount,
116227569Sphilip									media_header* mediaHeader = NULL);
117227569Sphilip
118227569Sphilip			status_t			ReadFrames(void* buffer, int64* _frameCount,
119227569Sphilip									media_header* mediaHeader,
120227569Sphilip									media_decode_info* info);
121227569Sphilip
122227569Sphilip			status_t			ReplaceFrames(const void* buffer,
123227569Sphilip									int64* _inOutFrameCount,
124227569Sphilip									const media_header* mediaHeader);
125227569Sphilip
126227569Sphilip
127227569Sphilip	// SeekToTime and SeekToFrame are used for seeking to a particular
128227569Sphilip	// position in a track, expressed in either frames or microseconds.
129227569Sphilip	// They return whatever position they were able to seek to. For example,
130227569Sphilip	// a video codec may not be able to seek to arbitrary frames, but only to
131227569Sphilip	// key frames. In this case, it would return the closest key frame before
132227569Sphilip	// the specified seek point.
133227569Sphilip	//
134227569Sphilip	// If you want to explicitly seek to the nearest keyframe _before_ this
135227569Sphilip	// frame or _after_ this frame, pass B_MEDIA_SEEK_CLOSEST_FORWARD or
136227569Sphilip	// B_MEDIA_SEEK_CLOSEST_BACKWARD as the flags field.
137227569Sphilip
138227569Sphilip			status_t			SeekToTime(bigtime_t* _inOutTime,
139227569Sphilip									int32 flags = 0);
140227569Sphilip			status_t			SeekToFrame(int64* _inOutFrame,
141227569Sphilip									int32 flags = 0);
142227569Sphilip
143227569Sphilip			status_t			FindKeyFrameForTime(bigtime_t* _inOutTime,
144227569Sphilip									int32 flags = 0) const;
145227569Sphilip			status_t			FindKeyFrameForFrame(int64* _inOutFrame,
146227569Sphilip									int32 flags = 0) const;
147227569Sphilip
148227569Sphilip	// ReadChunk returns, in _buffer, the next _size bytes of
149227569Sphilip	// data from the track.  The data is not decoded -- it will be
150227569Sphilip	// in its native encoded format (as specified by EncodedFormat()).
151227569Sphilip	// You can not mix calling ReadChunk() and ReadFrames() -- either
152227569Sphilip	// you access the track raw (i.e. with ReadChunk) or you access
153227569Sphilip	// it with ReadFrames.
154227569Sphilip
155227569Sphilip			status_t			ReadChunk(char** _buffer, int32* _size,
156227569Sphilip									media_header* mediaHeader = NULL);
157227569Sphilip
158227569Sphilip
159227569Sphilip	//
160227569Sphilip	// Write-only Functions
161227569Sphilip	//
162227569Sphilip			status_t			AddCopyright(const char* copyright);
163227569Sphilip			status_t			AddTrackInfo(uint32 code, const void* data,
164227569Sphilip									size_t size, uint32 flags = 0);
165227569Sphilip
166227569Sphilip	// Write frameCount of data to the track. This data is passed
167227569Sphilip	// through the encoder that was specified when the MediaTrack
168227569Sphilip	// was constructed.
169227569Sphilip	// Pass B_MEDIA_KEY_FRAME for flags if it is.
170227569Sphilip
171227569Sphilip			status_t			WriteFrames(const void* data, int32 frameCount,
172227569Sphilip									int32 flags = 0);
173227569Sphilip			status_t			WriteFrames(const void* data, int64 frameCount,
174227569Sphilip									media_encode_info* info);
175227569Sphilip
176227569Sphilip	// Write a raw chunk of (presumably already encoded data) to
177227569Sphilip	// the file.
178227569Sphilip	// Pass B_MEDIA_KEY_FRAME for flags if it is.
179227569Sphilip
180227569Sphilip			status_t			WriteChunk(const void* data, size_t size,
181227569Sphilip									uint32 flags = 0);
182227569Sphilip			status_t			WriteChunk(const void* data, size_t size,
183227569Sphilip									media_encode_info* info);
184227569Sphilip
185227569Sphilip	// Flush all buffered encoded datas to disk. You should call it after
186227569Sphilip	// writing the last frame to be sure all datas are flushed at the right
187227569Sphilip	// offset into the file.
188227569Sphilip			status_t			Flush();
189227569Sphilip
190227569Sphilip	// These are for controlling the underlying encoder and track parameters
191227569Sphilip	// returns a copy of the parameter web
192227569Sphilip			status_t			GetParameterWeb(BParameterWeb** _web);
193227569Sphilip			status_t 			GetParameterValue(int32 id, void* value,
194227569Sphilip									size_t* size);
195227569Sphilip			status_t			SetParameterValue(int32 id, const void* value,
196227569Sphilip									size_t size);
197227569Sphilip			BView*				GetParameterView();
198227569Sphilip
199227569Sphilip	// This is a simplified control API, only one parameter low=0.0, high=1.0
200227569Sphilip	// Return B_ERROR if it's not supported by the current encoder.
201227569Sphilip			status_t			GetQuality(float* _quality);
202227569Sphilip			status_t			SetQuality(float quality);
203227569Sphilip
204227569Sphilip			status_t 			GetEncodeParameters(
205227569Sphilip									encode_parameters* parameters) const;
206227569Sphilip			status_t 			SetEncodeParameters(
207227569Sphilip									encode_parameters* parameters);
208227569Sphilip
209227569Sphilip
210227569Sphilip	virtual	status_t			Perform(int32 code, void* data);
211227569Sphilip
212227569Sphilipprivate:
213227569Sphilip	friend class BMediaFile;
214227569Sphilip
215227569Sphilip	// deprecated, but for BeOS R5 compatibility
216227569Sphilip			BParameterWeb*	Web();
217227569Sphilip
218227569Sphilip	// Does nothing, returns B_ERROR, for Zeta compatiblity only
219227569Sphilip			status_t			ControlCodec(int32 selector, void* _inOutData,
220227569Sphilip									size_t size);
221227569Sphilip
222227569Sphilip	// For read-only access to a BMediaTrack
223227569Sphilip								BMediaTrack(
224227569Sphilip									BPrivate::media::MediaExtractor* extractor,
225227569Sphilip									int32 streamIndex);
226227569Sphilip
227227569Sphilip	// For write-only access to a BMediaTrack
228227569Sphilip								BMediaTrack(
229227569Sphilip									BPrivate::media::MediaWriter* writer,
230227569Sphilip									int32 streamIndex, media_format* format,
231227569Sphilip									const media_codec_info* codecInfo);
232227569Sphilip
233227569Sphilip			void				SetupWorkaround();
234227569Sphilip			bool				SetupFormatTranslation(
235227569Sphilip									const media_format& from,
236227569Sphilip									media_format* _to);
237227569Sphilip
238227569Sphilipprivate:
239227569Sphilip			status_t			fInitStatus;
240227569Sphilip			BPrivate::media::Decoder* fDecoder;
241227569Sphilip			BPrivate::media::Decoder* fRawDecoder;
242227569Sphilip			BPrivate::media::MediaExtractor* fExtractor;
243227569Sphilip
244227569Sphilip			int32				fStream;
245227569Sphilip			int64				fCurrentFrame;
246227569Sphilip			bigtime_t			fCurrentTime;
247227569Sphilip
248227569Sphilip			media_codec_info	fCodecInfo;
249227569Sphilip
250227569Sphilip			BPrivate::media::Encoder* fEncoder;
251227569Sphilip			int32				fEncoderID;
252227569Sphilip			BPrivate::media::MediaWriter* fWriter;
253227569Sphilip			media_format		fFormat;
254227569Sphilip
255227569Sphilip			uint32				fWorkaroundFlags;
256227569Sphilip
257227569Sphilipprotected:
258227569Sphilip			int32				EncoderID() { return fEncoderID; };
259227569Sphilip
260227569Sphilipprivate:
261227569Sphilip								BMediaTrack();
262227569Sphilip								BMediaTrack(const BMediaTrack&);
263227569Sphilip			BMediaTrack&		operator=(const BMediaTrack&);
264227569Sphilip
265227569Sphilip
266227569Sphilip			double				_FrameRate() const;
267227569Sphilip
268227569Sphilip	// FBC data and virtuals
269227569Sphilip			uint32				_reserved_BMediaTrack_[31];
270227569Sphilip
271227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_0(int32 arg, ...);
272227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_1(int32 arg, ...);
273227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_2(int32 arg, ...);
274227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_3(int32 arg, ...);
275227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_4(int32 arg, ...);
276227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_5(int32 arg, ...);
277227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_6(int32 arg, ...);
278227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_7(int32 arg, ...);
279227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_8(int32 arg, ...);
280227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_9(int32 arg, ...);
281227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_10(int32 arg, ...);
282227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_11(int32 arg, ...);
283227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_12(int32 arg, ...);
284227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_13(int32 arg, ...);
285227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_14(int32 arg, ...);
286227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_15(int32 arg, ...);
287227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_16(int32 arg, ...);
288227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_17(int32 arg, ...);
289227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_18(int32 arg, ...);
290227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_19(int32 arg, ...);
291227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_20(int32 arg, ...);
292227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_21(int32 arg, ...);
293227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_22(int32 arg, ...);
294227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_23(int32 arg, ...);
295227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_24(int32 arg, ...);
296227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_25(int32 arg, ...);
297227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_26(int32 arg, ...);
298227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_27(int32 arg, ...);
299227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_28(int32 arg, ...);
300227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_29(int32 arg, ...);
301227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_30(int32 arg, ...);
302227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_31(int32 arg, ...);
303227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_32(int32 arg, ...);
304227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_33(int32 arg, ...);
305227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_34(int32 arg, ...);
306227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_35(int32 arg, ...);
307227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_36(int32 arg, ...);
308227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_37(int32 arg, ...);
309227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_38(int32 arg, ...);
310227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_39(int32 arg, ...);
311227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_40(int32 arg, ...);
312227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_41(int32 arg, ...);
313227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_42(int32 arg, ...);
314227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_43(int32 arg, ...);
315227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_44(int32 arg, ...);
316227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_45(int32 arg, ...);
317227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_46(int32 arg, ...);
318227569Sphilip	virtual	status_t			_Reserved_BMediaTrack_47(int32 arg, ...);
319227569Sphilip};
320227569Sphilip
321227569Sphilip#endif // _MEDIA_TRACK_H
322227569Sphilip