pre-release 0.4.2
[Fobs.git] / core / Decoder.h
blob4c23ac1e8a0825a6b6099d1ac4ce0cbf9f198e5f
1 /******************************************************************************
2 * FOBS C++ wrapper Header
3 * Copyright (c) 2004 Omnividea Multimedia S.L
4 * Coded by JosŽ San Pedro Wandelmer
6 * This file is part of FOBS.
8 * FOBS is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1
11 * of the License, or (at your option) any later version.
13 * FOBS is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FOBS; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ******************************************************************************/
23 #ifndef __OMNIVIDEA_FOBS_DECODER_H
24 #define __OMNIVIDEA_FOBS_DECODER_H
26 #include "common.h"
28 extern "C" {
29 #include <inttypes.h>
32 //Forward declaration of ffmpeg structs
33 struct AVPacket;
34 struct ReSampleContext;
35 struct AVPicture;
36 struct AVCodecContext;
37 struct AVFormatContext;
38 struct AVInputFormat;
39 struct AVRational;
40 struct SwsContext;
42 class PacketBuffer;
44 #ifndef AVCODEC_MAX_AUDIO_FRAME_SIZE
45 #define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000
46 #endif
49 namespace omnividea
51 namespace fobs
55 class Decoder
57 friend class Transcoder;
58 friend class Encoder;
59 protected:
61 //Streams
62 int videoStreamIndex;
63 int audioStreamIndex;
64 bool audioEnabledFlag;
66 bool eofReachedFlag; /* true if eof reached */
68 AVFormatContext *inputFile;
69 AVInputFormat *inputFileFormat;
72 bool frameFlag;
73 bool opened;
76 /*Decoded frame in different formats*/
77 TimeStamp currentYuv;
78 bool currentYuvFlag;
79 char *yuvBytes;
80 AVPicture *yuvPicture;
81 uint8_t *yuvBuf;
83 TimeStamp currentRgb;
84 bool currentRgbFlag;
85 AVPicture *rgbPicture;
86 uint8_t *rgbBuf;
88 TimeStamp currentRgba;
89 bool currentRgbaFlag;
90 AVPicture *rgbaPicture;
91 uint8_t *rgbaBuf;
93 AVPicture *decodedPicture; //Format from the video
94 char filename[1024];
97 bool incorrectPts;
99 //Video timing data
100 TimeStamp duration;
101 TimeStamp position;
102 TimeStamp firstVideoPosition;
103 bool firstVideoPositionFlag;
104 TimeStamp firstVideoFramePosition;
106 //Audio frames
107 uint8_t decodedAudioFrame[AVCODEC_MAX_AUDIO_FRAME_SIZE];
108 int decodedAudioFrameSize;
109 double audioTime; //in Seconds
110 TimeStamp firstAudioFramePosition;
111 TimeStamp positionAudio;
112 TimeStamp firstAudioPosition;
113 bool firstAudioPositionFlag;
114 ReSampleContext *audioResampler;
116 //Temporals to change size
117 AVPicture *transitionPicture;
118 uint8_t *transitionPictureBuf;
119 AVPicture *transitionPictureRgb;
120 uint8_t *transitionPictureBufRgb;
122 int transitionPictureWidth;
123 int transitionPictureHeight;
124 //ImgReSampleContext *img_resample_ctx;
125 struct SwsContext *img_resample_ctx; /* for image resampling */
126 int resample_height;
128 //Packet buffers
129 PacketBuffer *videoBuffer;
130 PacketBuffer *audioBuffer;
133 //Private Methods
134 void reset();
135 inline uint _getWidth();
136 inline uint _getHeight();
137 ReturnCode reallocTransitionPicture(int newWidth, int newHeight);
138 bool compareTimeStamps(TimeStamp t1, TimeStamp t2);
140 bool audioResampleFlag;
141 //uint8_t resampledAudioBuffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
143 //unit conversion
144 TimeStamp pts2TimeStamp(int64_t pts, AVRational *pts_timebase);
145 int64_t timeStamp2pts(TimeStamp ts, AVRational *pts_timebase);
147 //Packet reading
148 virtual ReturnCode readNextFrame();
149 virtual ReturnCode placeAtNextFrame(bool videoFrame);
150 virtual ReturnCode decodeFrame();
151 virtual ReturnCode decodeAudioFrame();
153 ReturnCode setFrameFast(TimeStamp newPosition);
154 ReturnCode setFrameClassic(TimeStamp newPosition);
155 ReturnCode setAudioFast(TimeStamp newPosition);
156 ReturnCode setAudioClassic(TimeStamp newPosition);
157 FrameIndex frameIndexFromTimeStamp(TimeStamp t);
158 TimeStamp timeStampFromFrameIndex(FrameIndex f);
160 ReturnCode _open();
161 ReturnCode _setFrame(TimeStamp newPosition);
164 public:
165 Decoder(const char *filename);
166 ~Decoder();
168 ReturnCode testOpen();
169 ReturnCode testClose();
170 void setAudioResampleFlag(bool flag); //cap number of channels to two - Compatibility with JMF
171 ReturnCode open();
172 ReturnCode close();
174 //Video stream management
175 bool isVideoPresent();
176 virtual uint getWidth();
177 virtual uint getHeight();
178 virtual int getBitRate();
179 virtual double getFrameRate();
180 virtual TimeStamp getDurationMilliseconds();
181 virtual double getDurationSeconds();
183 //Audio stream management
184 bool isAudioPresent();
185 void enableAudio(bool flag);
186 uint getAudioSampleRate(); //Samples per second
187 uint getAudioBitRate(); //Kbits per second
188 uint getAudioChannelNumber();
190 TimeStamp getAVPosition();
192 inline virtual char* getFileName();
196 //End of stream query
197 //inline virtual bool moreFrames();
199 //Position query
200 virtual FrameIndex getFrameIndex();
201 inline virtual double getFrameTime();
202 virtual double getNextFrameTime();
203 double getAudioTime();
206 //Next&Prev video frame decoding
207 virtual ReturnCode nextFrame();
208 virtual ReturnCode nextAudioFrame();
209 virtual ReturnCode prevFrame();
210 //Decoded video frame access
211 virtual byte getCrFactor();
212 virtual byte getCbFactor();
213 virtual byte *getLuminance();
214 virtual byte *getCr();
215 virtual byte *getCb();
216 virtual byte *getRGB();
217 virtual byte *getRGBA();
218 virtual byte *getRGBA(char *buf);
219 byte *getRGB(int width, int height);
220 ReturnCode getRawFrame(AVPicture** pict, int* pix_fmt);
224 //Audio decoding
225 uint8_t* getAudioSamples();
226 int getAudioSamplesSize();
229 //seeking functionality (video based)
230 virtual ReturnCode setFrame(FrameIndex frameIndex);
231 virtual ReturnCode setFrameByTime(double seconds);
232 virtual ReturnCode setFrameByTime(TimeStamp milliseconds);
233 virtual ReturnCode setPosition(TimeStamp milliseconds);
235 //Other
236 double getFirstFrameTime();
237 double getFirstAudioSampleTime();
239 AVCodecContext *getAudioCodec();
240 AVCodecContext *getVideoCodec();
242 double getTime();
246 } //namespace fobs
247 } //namespace omnividea
248 #endif