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