| [9816] | 1 | |
|---|
| 2 | #ifndef HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_VIDEO_H |
|---|
| 3 | #define HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_VIDEO_H |
|---|
| 4 | |
|---|
| 5 | |
|---|
| [9840] | 6 | #include "FFmpegHeaders.hpp" |
|---|
| [9816] | 7 | #include "BoundedMessageQueue.hpp" |
|---|
| 8 | #include "FFmpegClocks.hpp" |
|---|
| 9 | #include "FFmpegPacket.hpp" |
|---|
| 10 | |
|---|
| [9818] | 11 | #include <OpenThreads/Thread> |
|---|
| 12 | #include <vector> |
|---|
| [9816] | 13 | |
|---|
| 14 | namespace osgFFmpeg { |
|---|
| 15 | |
|---|
| [9826] | 16 | class FramePtr |
|---|
| 17 | { |
|---|
| 18 | public: |
|---|
| 19 | |
|---|
| 20 | typedef AVFrame T; |
|---|
| 21 | |
|---|
| 22 | explicit FramePtr() : _ptr(0) {} |
|---|
| 23 | explicit FramePtr(T* ptr) : _ptr(ptr) {} |
|---|
| 24 | |
|---|
| 25 | ~FramePtr() |
|---|
| 26 | { |
|---|
| 27 | cleanup(); |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | T* get() { return _ptr; } |
|---|
| [9816] | 31 | |
|---|
| [9826] | 32 | T * operator-> () const |
|---|
| 33 | { |
|---|
| 34 | return _ptr; |
|---|
| 35 | } |
|---|
| [9816] | 36 | |
|---|
| [9826] | 37 | void reset(T* ptr) |
|---|
| 38 | { |
|---|
| 39 | if (ptr==_ptr) return; |
|---|
| 40 | cleanup(); |
|---|
| 41 | _ptr = ptr; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | void cleanup() |
|---|
| 45 | { |
|---|
| 46 | if (_ptr) av_free(_ptr); |
|---|
| 47 | _ptr = 0; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | protected: |
|---|
| 53 | |
|---|
| 54 | T* _ptr; |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| [9816] | 57 | class FFmpegDecoderVideo : public OpenThreads::Thread |
|---|
| 58 | { |
|---|
| 59 | public: |
|---|
| 60 | |
|---|
| 61 | typedef BoundedMessageQueue<FFmpegPacket> PacketQueue; |
|---|
| 62 | typedef void (* PublishFunc) (const FFmpegDecoderVideo & decoder, void * user_data); |
|---|
| 63 | |
|---|
| 64 | FFmpegDecoderVideo(PacketQueue & packets, FFmpegClocks & clocks); |
|---|
| 65 | ~FFmpegDecoderVideo(); |
|---|
| 66 | |
|---|
| 67 | void open(AVStream * stream); |
|---|
| [9869] | 68 | void close(bool waitForThreadToExit); |
|---|
| 69 | |
|---|
| [9816] | 70 | virtual void run(); |
|---|
| 71 | |
|---|
| 72 | void setUserData(void * user_data); |
|---|
| 73 | void setPublishCallback(PublishFunc function); |
|---|
| 74 | |
|---|
| 75 | int width() const; |
|---|
| 76 | int height() const; |
|---|
| [9910] | 77 | float pixelAspectRatio() const; |
|---|
| [9816] | 78 | bool alphaChannel() const; |
|---|
| 79 | double frameRate() const; |
|---|
| 80 | const uint8_t * image() const; |
|---|
| 81 | |
|---|
| 82 | private: |
|---|
| 83 | |
|---|
| 84 | typedef std::vector<uint8_t> Buffer; |
|---|
| 85 | |
|---|
| 86 | void decodeLoop(); |
|---|
| 87 | void findAspectRatio(); |
|---|
| [10161] | 88 | void publishFrame(double delay, bool audio_disabled); |
|---|
| [9816] | 89 | double synchronizeVideo(double pts); |
|---|
| [9960] | 90 | void yuva420pToRgba(AVPicture *dst, AVPicture *src, int width, int height); |
|---|
| [9816] | 91 | |
|---|
| [9960] | 92 | int convert(AVPicture *dst, int dst_pix_fmt, AVPicture *src, |
|---|
| [9854] | 93 | int src_pix_fmt, int src_width, int src_height); |
|---|
| 94 | |
|---|
| 95 | |
|---|
| [9816] | 96 | static int getBuffer(AVCodecContext * context, AVFrame * picture); |
|---|
| 97 | static void releaseBuffer(AVCodecContext * context, AVFrame * picture); |
|---|
| 98 | |
|---|
| [9854] | 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; |
|---|
| [9816] | 107 | |
|---|
| [9854] | 108 | FramePtr m_frame; |
|---|
| 109 | FramePtr m_frame_rgba; |
|---|
| [9860] | 110 | Buffer m_buffer_rgba[2]; |
|---|
| 111 | int m_writeBuffer; |
|---|
| [9816] | 112 | |
|---|
| [9854] | 113 | void * m_user_data; |
|---|
| 114 | PublishFunc m_publish_func; |
|---|
| [9816] | 115 | |
|---|
| [9854] | 116 | double m_frame_rate; |
|---|
| [9910] | 117 | float m_pixel_aspect_ratio; |
|---|
| [9854] | 118 | int m_width; |
|---|
| 119 | int m_height; |
|---|
| 120 | size_t m_next_frame_index; |
|---|
| 121 | bool m_alpha_channel; |
|---|
| [9816] | 122 | |
|---|
| [9854] | 123 | volatile bool m_exit; |
|---|
| 124 | |
|---|
| [9856] | 125 | #ifdef USE_SWSCALE |
|---|
| [9854] | 126 | struct SwsContext * m_swscale_ctx; |
|---|
| 127 | #endif |
|---|
| [9816] | 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 | |
|---|
| [9910] | 158 | inline float FFmpegDecoderVideo::pixelAspectRatio() const |
|---|
| [9816] | 159 | { |
|---|
| [9910] | 160 | return m_pixel_aspect_ratio; |
|---|
| [9816] | 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 | { |
|---|
| [9860] | 178 | return &((m_buffer_rgba[1-m_writeBuffer])[0]); |
|---|
| [9816] | 179 | } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | #endif // HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_VIDEO_H |
|---|