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