| 1 | |
|---|
| 2 | #ifndef HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_VIDEO_H |
|---|
| 3 | #define HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_VIDEO_H |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | #include "FFmpegHeaders.hpp" |
|---|
| 7 | #include "BoundedMessageQueue.hpp" |
|---|
| 8 | #include "FFmpegClocks.hpp" |
|---|
| 9 | #include "FFmpegPacket.hpp" |
|---|
| 10 | |
|---|
| 11 | #include <boost/shared_ptr.hpp> |
|---|
| 12 | #include <OpenThreads/Thread> |
|---|
| 13 | #include <vector> |
|---|
| 14 | |
|---|
| 15 | namespace osgFFmpeg { |
|---|
| 16 | |
|---|
| 17 | class FramePtr |
|---|
| 18 | { |
|---|
| 19 | public: |
|---|
| 20 | |
|---|
| 21 | typedef AVFrame T; |
|---|
| 22 | |
|---|
| 23 | explicit FramePtr() : _ptr(0) {} |
|---|
| 24 | explicit FramePtr(T* ptr) : _ptr(ptr) {} |
|---|
| 25 | |
|---|
| 26 | ~FramePtr() |
|---|
| 27 | { |
|---|
| 28 | cleanup(); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | T* get() { return _ptr; } |
|---|
| 32 | |
|---|
| 33 | T * operator-> () const |
|---|
| 34 | { |
|---|
| 35 | return _ptr; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | void reset(T* ptr) |
|---|
| 39 | { |
|---|
| 40 | if (ptr==_ptr) return; |
|---|
| 41 | cleanup(); |
|---|
| 42 | _ptr = ptr; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | void cleanup() |
|---|
| 46 | { |
|---|
| 47 | if (_ptr) av_free(_ptr); |
|---|
| 48 | _ptr = 0; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | protected: |
|---|
| 54 | |
|---|
| 55 | T* _ptr; |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | class FFmpegDecoderVideo : public OpenThreads::Thread |
|---|
| 59 | { |
|---|
| 60 | public: |
|---|
| 61 | |
|---|
| 62 | typedef BoundedMessageQueue<FFmpegPacket> PacketQueue; |
|---|
| 63 | typedef void (* PublishFunc) (const FFmpegDecoderVideo & decoder, void * user_data); |
|---|
| 64 | |
|---|
| 65 | FFmpegDecoderVideo(PacketQueue & packets, FFmpegClocks & clocks); |
|---|
| 66 | ~FFmpegDecoderVideo(); |
|---|
| 67 | |
|---|
| 68 | void open(AVStream * stream); |
|---|
| 69 | virtual void run(); |
|---|
| 70 | |
|---|
| 71 | void setUserData(void * user_data); |
|---|
| 72 | void setPublishCallback(PublishFunc function); |
|---|
| 73 | |
|---|
| 74 | int width() const; |
|---|
| 75 | int height() const; |
|---|
| 76 | double aspectRatio() const; |
|---|
| 77 | bool alphaChannel() const; |
|---|
| 78 | double frameRate() const; |
|---|
| 79 | const uint8_t * image() const; |
|---|
| 80 | |
|---|
| 81 | private: |
|---|
| 82 | |
|---|
| 83 | typedef std::vector<uint8_t> Buffer; |
|---|
| 84 | |
|---|
| 85 | void decodeLoop(); |
|---|
| 86 | void findAspectRatio(); |
|---|
| 87 | void publishFrame(double delay); |
|---|
| 88 | void swapBuffers(); |
|---|
| 89 | double synchronizeVideo(double pts); |
|---|
| 90 | void yuva420pToRgba(AVPicture *dst, const AVPicture *src, int width, int height); |
|---|
| 91 | |
|---|
| 92 | int convert(AVPicture *dst, int dst_pix_fmt, const AVPicture *src, |
|---|
| 93 | int src_pix_fmt, int src_width, int src_height); |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | static int getBuffer(AVCodecContext * context, AVFrame * picture); |
|---|
| 97 | static void releaseBuffer(AVCodecContext * context, AVFrame * picture); |
|---|
| 98 | |
|---|
| 99 | PacketQueue & m_packets; |
|---|
| 100 | FFmpegClocks & m_clocks; |
|---|
| 101 | AVStream * m_stream; |
|---|
| 102 | AVCodecContext * m_context; |
|---|
| 103 | AVCodec * m_codec; |
|---|
| 104 | const uint8_t * m_packet_data; |
|---|
| 105 | int m_bytes_remaining; |
|---|
| 106 | int64_t m_packet_pts; |
|---|
| 107 | |
|---|
| 108 | FramePtr m_frame; |
|---|
| 109 | FramePtr m_frame_rgba; |
|---|
| 110 | Buffer m_buffer_rgba[2]; |
|---|
| 111 | int m_writeBuffer; |
|---|
| 112 | |
|---|
| 113 | void * m_user_data; |
|---|
| 114 | PublishFunc m_publish_func; |
|---|
| 115 | |
|---|
| 116 | double m_frame_rate; |
|---|
| 117 | double m_aspect_ratio; |
|---|
| 118 | int m_width; |
|---|
| 119 | int m_height; |
|---|
| 120 | size_t m_next_frame_index; |
|---|
| 121 | bool m_alpha_channel; |
|---|
| 122 | |
|---|
| 123 | volatile bool m_exit; |
|---|
| 124 | |
|---|
| 125 | #ifdef USE_SWSCALE |
|---|
| 126 | struct SwsContext * m_swscale_ctx; |
|---|
| 127 | #endif |
|---|
| 128 | }; |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | inline void FFmpegDecoderVideo::setUserData(void * const user_data) |
|---|
| 135 | { |
|---|
| 136 | m_user_data = user_data; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | inline void FFmpegDecoderVideo::setPublishCallback(const PublishFunc function) |
|---|
| 141 | { |
|---|
| 142 | m_publish_func = function; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | inline int FFmpegDecoderVideo::width() const |
|---|
| 147 | { |
|---|
| 148 | return m_width; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | inline int FFmpegDecoderVideo::height() const |
|---|
| 153 | { |
|---|
| 154 | return m_height; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | inline double FFmpegDecoderVideo::aspectRatio() const |
|---|
| 159 | { |
|---|
| 160 | return m_aspect_ratio; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | |
|---|
| 164 | inline bool FFmpegDecoderVideo::alphaChannel() const |
|---|
| 165 | { |
|---|
| 166 | return m_alpha_channel; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | inline double FFmpegDecoderVideo::frameRate() const |
|---|
| 171 | { |
|---|
| 172 | return m_frame_rate; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | inline const uint8_t * FFmpegDecoderVideo::image() const |
|---|
| 177 | { |
|---|
| 178 | return &((m_buffer_rgba[1-m_writeBuffer])[0]); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | #endif // HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_VIDEO_H |
|---|