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