|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html. |
| 4 | + |
| 5 | +#ifndef CONTAINER_AVI_HPP |
| 6 | +#define CONTAINER_AVI_HPP |
| 7 | + |
| 8 | +#ifndef __OPENCV_BUILD |
| 9 | +# error this is a private header which should not be used from outside of the OpenCV library |
| 10 | +#endif |
| 11 | + |
| 12 | +#include "opencv2/core/cvdef.h" |
| 13 | +#include "opencv2/videoio/videoio_c.h" |
| 14 | +#include <deque> |
| 15 | + |
| 16 | +namespace cv |
| 17 | +{ |
| 18 | + |
| 19 | +/* |
| 20 | +AVI struct: |
| 21 | +
|
| 22 | +RIFF ('AVI ' |
| 23 | + LIST ('hdrl' |
| 24 | + 'avih'(<Main AVI Header>) |
| 25 | + LIST ('strl' |
| 26 | + 'strh'(<Stream header>) |
| 27 | + 'strf'(<Stream format>) |
| 28 | + [ 'strd'(<Additional header data>) ] |
| 29 | + [ 'strn'(<Stream name>) ] |
| 30 | + [ 'indx'(<Odml index data>) ] |
| 31 | + ... |
| 32 | + ) |
| 33 | + [LIST ('strl' ...)] |
| 34 | + [LIST ('strl' ...)] |
| 35 | + ... |
| 36 | + [LIST ('odml' |
| 37 | + 'dmlh'(<ODML header data>) |
| 38 | + ... |
| 39 | + ) |
| 40 | + ] |
| 41 | + ... |
| 42 | + ) |
| 43 | + [LIST ('INFO' ...)] |
| 44 | + [JUNK] |
| 45 | + LIST ('movi' |
| 46 | + {{xxdb|xxdc|xxpc|xxwb}(<Data>) | LIST ('rec ' |
| 47 | + {xxdb|xxdc|xxpc|xxwb}(<Data>) |
| 48 | + {xxdb|xxdc|xxpc|xxwb}(<Data>) |
| 49 | + ... |
| 50 | + ) |
| 51 | + ... |
| 52 | + } |
| 53 | + ... |
| 54 | + ) |
| 55 | + ['idx1' (<AVI Index>) ] |
| 56 | + ) |
| 57 | +
|
| 58 | + {xxdb|xxdc|xxpc|xxwb} |
| 59 | + xx - stream number: 00, 01, 02, ... |
| 60 | + db - uncompressed video frame |
| 61 | + dc - commpressed video frame |
| 62 | + pc - palette change |
| 63 | + wb - audio frame |
| 64 | +
|
| 65 | + JUNK section may pad any data section and must be ignored |
| 66 | +*/ |
| 67 | + |
| 68 | +typedef std::deque< std::pair<uint64_t, uint32_t> > frame_list; |
| 69 | +typedef frame_list::iterator frame_iterator; |
| 70 | +struct RiffChunk; |
| 71 | +struct RiffList; |
| 72 | +class VideoInputStream; |
| 73 | +enum Codecs { MJPEG }; |
| 74 | + |
| 75 | +//Represents single MJPEG video stream within single AVI/AVIX entry |
| 76 | +//Multiple video streams within single AVI/AVIX entry are not supported |
| 77 | +//ODML index is not supported |
| 78 | +class CV_EXPORTS AVIReadContainer |
| 79 | +{ |
| 80 | +public: |
| 81 | + AVIReadContainer(); |
| 82 | + |
| 83 | + void initStream(const String& filename); |
| 84 | + void initStream(Ptr<VideoInputStream> m_file_stream_); |
| 85 | + |
| 86 | + void close(); |
| 87 | + //stores founded frames in m_frame_list which can be accessed via getFrames |
| 88 | + bool parseAvi(Codecs codec_) { return parseAviWithFrameList(m_frame_list, codec_); } |
| 89 | + //stores founded frames in in_frame_list. getFrames() would return empty list |
| 90 | + bool parseAvi(frame_list& in_frame_list, Codecs codec_) { return parseAviWithFrameList(in_frame_list, codec_); } |
| 91 | + size_t getFramesCount() { return m_frame_list.size(); } |
| 92 | + frame_list& getFrames() { return m_frame_list; } |
| 93 | + unsigned int getWidth() { return m_width; } |
| 94 | + unsigned int getHeight() { return m_height; } |
| 95 | + double getFps() { return m_fps; } |
| 96 | + std::vector<char> readFrame(frame_iterator it); |
| 97 | + bool parseRiff(frame_list &m_mjpeg_frames); |
| 98 | + |
| 99 | +protected: |
| 100 | + |
| 101 | + bool parseAviWithFrameList(frame_list& in_frame_list, Codecs codec_); |
| 102 | + void skipJunk(RiffChunk& chunk); |
| 103 | + void skipJunk(RiffList& list); |
| 104 | + bool parseHdrlList(Codecs codec_); |
| 105 | + bool parseIndex(unsigned int index_size, frame_list& in_frame_list); |
| 106 | + bool parseMovi(frame_list& in_frame_list) |
| 107 | + { |
| 108 | + //not implemented |
| 109 | + in_frame_list.empty(); |
| 110 | + return true; |
| 111 | + } |
| 112 | + bool parseStrl(char stream_id, Codecs codec_); |
| 113 | + bool parseInfo() |
| 114 | + { |
| 115 | + //not implemented |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + void printError(RiffList& list, unsigned int expected_fourcc); |
| 120 | + |
| 121 | + void printError(RiffChunk& chunk, unsigned int expected_fourcc); |
| 122 | + |
| 123 | + Ptr<VideoInputStream> m_file_stream; |
| 124 | + unsigned int m_stream_id; |
| 125 | + unsigned long long int m_movi_start; |
| 126 | + unsigned long long int m_movi_end; |
| 127 | + frame_list m_frame_list; |
| 128 | + unsigned int m_width; |
| 129 | + unsigned int m_height; |
| 130 | + double m_fps; |
| 131 | + bool m_is_indx_present; |
| 132 | +}; |
| 133 | + |
| 134 | +enum { COLORSPACE_GRAY=0, COLORSPACE_RGBA=1, COLORSPACE_BGR=2, COLORSPACE_YUV444P=3 }; |
| 135 | +enum StreamType { db, dc, pc, wb }; |
| 136 | +class BitStream; |
| 137 | + |
| 138 | +// {xxdb|xxdc|xxpc|xxwb} |
| 139 | +// xx - stream number: 00, 01, 02, ... |
| 140 | +// db - uncompressed video frame |
| 141 | +// dc - commpressed video frame |
| 142 | +// pc - palette change |
| 143 | +// wb - audio frame |
| 144 | + |
| 145 | + |
| 146 | +class CV_EXPORTS AVIWriteContainer |
| 147 | +{ |
| 148 | +public: |
| 149 | + AVIWriteContainer(); |
| 150 | + ~AVIWriteContainer(); |
| 151 | + |
| 152 | + bool initContainer(const String& filename, double fps, Size size, bool iscolor); |
| 153 | + void startWriteAVI(int stream_count); |
| 154 | + void writeStreamHeader(Codecs codec_); |
| 155 | + void startWriteChunk(int fourcc); |
| 156 | + void endWriteChunk(); |
| 157 | + |
| 158 | + int getAVIIndex(int stream_number, StreamType strm_type); |
| 159 | + void writeIndex(int stream_number, StreamType strm_type); |
| 160 | + void finishWriteAVI(); |
| 161 | + |
| 162 | + bool isOpenedStream() const; |
| 163 | + bool isEmptyFrameOffset() const { return frameOffset.empty(); } |
| 164 | + int getWidth() const { return width; } |
| 165 | + int getHeight() const { return height; } |
| 166 | + int getChannels() const { return channels; } |
| 167 | + size_t getMoviPointer() const { return moviPointer; } |
| 168 | + size_t getStreamPos() const; |
| 169 | + |
| 170 | + void pushFrameOffset(size_t elem) { frameOffset.push_back(elem); } |
| 171 | + void pushFrameSize(size_t elem) { frameSize.push_back(elem); } |
| 172 | + bool isEmptyFrameSize() const { return frameSize.empty(); } |
| 173 | + size_t atFrameSize(size_t i) const { return frameSize[i]; } |
| 174 | + size_t countFrameSize() const { return frameSize.size(); } |
| 175 | + void jputStreamShort(int val); |
| 176 | + void putStreamBytes(const uchar* buf, int count); |
| 177 | + void putStreamByte(int val); |
| 178 | + void jputStream(unsigned currval); |
| 179 | + void jflushStream(unsigned currval, int bitIdx); |
| 180 | + |
| 181 | +private: |
| 182 | + Ptr<BitStream> strm; |
| 183 | + int outfps; |
| 184 | + int width, height, channels; |
| 185 | + size_t moviPointer; |
| 186 | + std::vector<size_t> frameOffset, frameSize, AVIChunkSizeIndex, frameNumIndexes; |
| 187 | +}; |
| 188 | + |
| 189 | +} |
| 190 | + |
| 191 | +#endif //CONTAINER_AVI_HPP |
0 commit comments