| [9816] | 1 | |
|---|
| 2 | #ifndef HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_VIDEO_H |
|---|
| 3 | #define HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_VIDEO_H |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | #include "BoundedMessageQueue.hpp" |
|---|
| 7 | #include "FFmpegClocks.hpp" |
|---|
| 8 | #include "FFmpegPacket.hpp" |
|---|
| 9 | |
|---|
| [9818] | 10 | #include <boost/shared_ptr.hpp> |
|---|
| 11 | #include <OpenThreads/Thread> |
|---|
| 12 | #include <vector> |
|---|
| [9816] | 13 | |
|---|
| 14 | namespace osgFFmpeg { |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | class FFmpegDecoderVideo : public OpenThreads::Thread |
|---|
| 19 | { |
|---|
| 20 | public: |
|---|
| 21 | |
|---|
| 22 | typedef BoundedMessageQueue<FFmpegPacket> PacketQueue; |
|---|
| 23 | typedef void (* PublishFunc) (const FFmpegDecoderVideo & decoder, void * user_data); |
|---|
| 24 | |
|---|
| 25 | FFmpegDecoderVideo(PacketQueue & packets, FFmpegClocks & clocks); |
|---|
| 26 | ~FFmpegDecoderVideo(); |
|---|
| 27 | |
|---|
| 28 | void open(AVStream * stream); |
|---|
| 29 | virtual void run(); |
|---|
| 30 | |
|---|
| 31 | void setUserData(void * user_data); |
|---|
| 32 | void setPublishCallback(PublishFunc function); |
|---|
| 33 | |
|---|
| 34 | int width() const; |
|---|
| 35 | int height() const; |
|---|
| 36 | double aspectRatio() const; |
|---|
| 37 | bool alphaChannel() const; |
|---|
| 38 | double frameRate() const; |
|---|
| 39 | const uint8_t * image() const; |
|---|
| 40 | |
|---|
| 41 | private: |
|---|
| 42 | |
|---|
| 43 | typedef boost::shared_ptr<AVFrame> FramePtr; |
|---|
| 44 | typedef std::vector<uint8_t> Buffer; |
|---|
| 45 | |
|---|
| 46 | void decodeLoop(); |
|---|
| 47 | void findAspectRatio(); |
|---|
| 48 | void publishFrame(double delay); |
|---|
| 49 | void swapBuffers(); |
|---|
| 50 | double synchronizeVideo(double pts); |
|---|
| 51 | void yuva420pToRgba(AVPicture *dst, const AVPicture *src, int width, int height); |
|---|
| 52 | |
|---|
| 53 | static int getBuffer(AVCodecContext * context, AVFrame * picture); |
|---|
| 54 | static void releaseBuffer(AVCodecContext * context, AVFrame * picture); |
|---|
| 55 | |
|---|
| 56 | PacketQueue & m_packets; |
|---|
| 57 | FFmpegClocks & m_clocks; |
|---|
| 58 | AVStream * m_stream; |
|---|
| 59 | AVCodecContext * m_context; |
|---|
| 60 | AVCodec * m_codec; |
|---|
| 61 | const uint8_t * m_packet_data; |
|---|
| 62 | int m_bytes_remaining; |
|---|
| 63 | int64_t m_packet_pts; |
|---|
| 64 | |
|---|
| 65 | FramePtr m_frame; |
|---|
| 66 | FramePtr m_frame_rgba; |
|---|
| 67 | Buffer m_buffer_rgba; |
|---|
| 68 | Buffer m_buffer_rgba_public; |
|---|
| 69 | |
|---|
| 70 | void * m_user_data; |
|---|
| 71 | PublishFunc m_publish_func; |
|---|
| 72 | |
|---|
| 73 | double m_frame_rate; |
|---|
| 74 | double m_aspect_ratio; |
|---|
| 75 | int m_width; |
|---|
| 76 | int m_height; |
|---|
| 77 | size_t m_next_frame_index; |
|---|
| 78 | bool m_alpha_channel; |
|---|
| 79 | |
|---|
| 80 | volatile bool m_exit; |
|---|
| 81 | }; |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | inline void FFmpegDecoderVideo::setUserData(void * const user_data) |
|---|
| 88 | { |
|---|
| 89 | m_user_data = user_data; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | inline void FFmpegDecoderVideo::setPublishCallback(const PublishFunc function) |
|---|
| 94 | { |
|---|
| 95 | m_publish_func = function; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | inline int FFmpegDecoderVideo::width() const |
|---|
| 100 | { |
|---|
| 101 | return m_width; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | inline int FFmpegDecoderVideo::height() const |
|---|
| 106 | { |
|---|
| 107 | return m_height; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | inline double FFmpegDecoderVideo::aspectRatio() const |
|---|
| 112 | { |
|---|
| 113 | return m_aspect_ratio; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | inline bool FFmpegDecoderVideo::alphaChannel() const |
|---|
| 118 | { |
|---|
| 119 | return m_alpha_channel; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | inline double FFmpegDecoderVideo::frameRate() const |
|---|
| 124 | { |
|---|
| 125 | return m_frame_rate; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | inline const uint8_t * FFmpegDecoderVideo::image() const |
|---|
| 130 | { |
|---|
| 131 | return &m_buffer_rgba_public[0]; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | #endif // HEADER_GUARD_OSGFFMPEG_FFMPEG_DECODER_VIDEO_H |
|---|