| [9816] | 1 | #include "FFmpegDecoderVideo.hpp" |
|---|
| 2 | |
|---|
| 3 | #include <osg/Notify> |
|---|
| 4 | |
|---|
| 5 | #include <stdexcept> |
|---|
| 6 | #include <string.h> |
|---|
| 7 | |
|---|
| 8 | namespace osgFFmpeg { |
|---|
| 9 | |
|---|
| 10 | FFmpegDecoderVideo::FFmpegDecoderVideo(PacketQueue & packets, FFmpegClocks & clocks) : |
|---|
| 11 | m_packets(packets), |
|---|
| 12 | m_clocks(clocks), |
|---|
| 13 | m_stream(0), |
|---|
| 14 | m_context(0), |
|---|
| 15 | m_codec(0), |
|---|
| 16 | m_packet_data(0), |
|---|
| 17 | m_bytes_remaining(0), |
|---|
| 18 | m_packet_pts(AV_NOPTS_VALUE), |
|---|
| 19 | m_user_data(0), |
|---|
| 20 | m_publish_func(0), |
|---|
| [9857] | 21 | m_exit(false) |
|---|
| 22 | #ifdef USE_SWSCALE |
|---|
| 23 | ,m_swscale_ctx(0) |
|---|
| 24 | #endif |
|---|
| [9816] | 25 | { |
|---|
| 26 | |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | FFmpegDecoderVideo::~FFmpegDecoderVideo() |
|---|
| 32 | { |
|---|
| 33 | if (isRunning()) |
|---|
| 34 | { |
|---|
| 35 | m_exit = true; |
|---|
| 36 | join(); |
|---|
| 37 | } |
|---|
| [9854] | 38 | |
|---|
| [9857] | 39 | #ifdef USE_SWSCALE |
|---|
| [9854] | 40 | if (m_swscale_ctx) |
|---|
| 41 | { |
|---|
| 42 | sws_freeContext(m_swscale_ctx); |
|---|
| 43 | m_swscale_ctx = 0; |
|---|
| 44 | } |
|---|
| [9857] | 45 | #endif |
|---|
| [9816] | 46 | } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | void FFmpegDecoderVideo::open(AVStream * const stream) |
|---|
| 51 | { |
|---|
| 52 | m_stream = stream; |
|---|
| 53 | m_context = stream->codec; |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | m_width = m_context->width; |
|---|
| 58 | m_height = m_context->height; |
|---|
| 59 | findAspectRatio(); |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | m_alpha_channel = (m_context->pix_fmt == PIX_FMT_YUVA420P); |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | m_frame_rate = av_q2d(stream->r_frame_rate); |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | m_codec = avcodec_find_decoder(m_context->codec_id); |
|---|
| 69 | |
|---|
| 70 | if (m_codec == 0) |
|---|
| 71 | throw std::runtime_error("avcodec_find_decoder() failed"); |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | if (avcodec_open(m_context, m_codec) < 0) |
|---|
| 79 | throw std::runtime_error("avcodec_open() failed"); |
|---|
| 80 | |
|---|
| 81 | |
|---|
| [9826] | 82 | m_frame.reset(avcodec_alloc_frame()); |
|---|
| [9816] | 83 | |
|---|
| 84 | |
|---|
| [9826] | 85 | m_frame_rgba.reset(avcodec_alloc_frame()); |
|---|
| [9816] | 86 | m_buffer_rgba.resize(avpicture_get_size(PIX_FMT_RGB32, width(), height())); |
|---|
| 87 | m_buffer_rgba_public.resize(m_buffer_rgba.size()); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | avpicture_fill((AVPicture *) m_frame_rgba.get(), &m_buffer_rgba[0], PIX_FMT_RGB32, width(), height()); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | m_context->opaque = this; |
|---|
| 94 | m_context->get_buffer = getBuffer; |
|---|
| 95 | m_context->release_buffer = releaseBuffer; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | void FFmpegDecoderVideo::run() |
|---|
| 101 | { |
|---|
| 102 | try |
|---|
| 103 | { |
|---|
| 104 | decodeLoop(); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | catch (const std::exception & error) |
|---|
| 108 | { |
|---|
| 109 | osg::notify(osg::WARN) << "FFmpegDecoderVideo::run : " << error.what() << std::endl; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | catch (...) |
|---|
| 113 | { |
|---|
| 114 | osg::notify(osg::WARN) << "FFmpegDecoderVideo::run : unhandled exception" << std::endl; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | void FFmpegDecoderVideo::decodeLoop() |
|---|
| 121 | { |
|---|
| 122 | FFmpegPacket packet; |
|---|
| 123 | double pts; |
|---|
| 124 | |
|---|
| 125 | while (! m_exit) |
|---|
| 126 | { |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | while (m_bytes_remaining > 0) |
|---|
| 130 | { |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | m_packet_pts = packet.packet.pts; |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | int frame_finished = 0; |
|---|
| 138 | |
|---|
| 139 | const int bytes_decoded = avcodec_decode_video(m_context, m_frame.get(), &frame_finished, m_packet_data, m_bytes_remaining); |
|---|
| 140 | |
|---|
| 141 | if (bytes_decoded < 0) |
|---|
| 142 | throw std::runtime_error("avcodec_decode_video failed()"); |
|---|
| 143 | |
|---|
| 144 | m_bytes_remaining -= bytes_decoded; |
|---|
| 145 | m_packet_data += bytes_decoded; |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | if (packet.packet.dts == AV_NOPTS_VALUE && m_frame->opaque != 0 && *reinterpret_cast<const int64_t*>(m_frame->opaque) != AV_NOPTS_VALUE) |
|---|
| 150 | { |
|---|
| 151 | pts = *reinterpret_cast<const int64_t*>(m_frame->opaque); |
|---|
| 152 | } |
|---|
| 153 | else if (packet.packet.dts != AV_NOPTS_VALUE) |
|---|
| 154 | { |
|---|
| 155 | pts = packet.packet.dts; |
|---|
| 156 | } |
|---|
| 157 | else |
|---|
| 158 | { |
|---|
| 159 | pts = 0; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | pts *= av_q2d(m_stream->time_base); |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | if (frame_finished) |
|---|
| 166 | { |
|---|
| 167 | const double synched_pts = m_clocks.videoSynchClock(m_frame.get(), av_q2d(m_stream->time_base), pts); |
|---|
| 168 | const double frame_delay = m_clocks.videoRefreshSchedule(synched_pts); |
|---|
| 169 | |
|---|
| 170 | publishFrame(frame_delay); |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | pts = 0; |
|---|
| 177 | |
|---|
| 178 | if (packet.valid()) |
|---|
| 179 | packet.clear(); |
|---|
| 180 | |
|---|
| 181 | bool is_empty = true; |
|---|
| 182 | packet = m_packets.timedPop(is_empty, 10); |
|---|
| 183 | |
|---|
| 184 | if (! is_empty) |
|---|
| 185 | { |
|---|
| 186 | if (packet.type == FFmpegPacket::PACKET_DATA) |
|---|
| 187 | { |
|---|
| 188 | m_bytes_remaining = packet.packet.size; |
|---|
| 189 | m_packet_data = packet.packet.data; |
|---|
| 190 | } |
|---|
| 191 | else if (packet.type == FFmpegPacket::PACKET_FLUSH) |
|---|
| 192 | { |
|---|
| 193 | avcodec_flush_buffers(m_context); |
|---|
| 194 | m_clocks.rewindVideo(); |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | |
|---|
| 201 | |
|---|
| 202 | void FFmpegDecoderVideo::findAspectRatio() |
|---|
| 203 | { |
|---|
| 204 | double ratio = 0.0; |
|---|
| 205 | |
|---|
| 206 | if (m_context->sample_aspect_ratio.num != 0) |
|---|
| 207 | ratio = (av_q2d(m_context->sample_aspect_ratio) * m_width) / m_height; |
|---|
| 208 | |
|---|
| 209 | if (ratio <= 0.0) |
|---|
| 210 | ratio = double(m_width) / double(m_height); |
|---|
| 211 | |
|---|
| 212 | m_aspect_ratio = ratio; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| [9854] | 215 | int FFmpegDecoderVideo::convert(AVPicture *dst, int dst_pix_fmt, const AVPicture *src, |
|---|
| 216 | int src_pix_fmt, int src_width, int src_height) |
|---|
| 217 | { |
|---|
| 218 | #ifdef USE_SWSCALE |
|---|
| 219 | if (m_swscale_ctx==0) |
|---|
| 220 | { |
|---|
| 221 | m_swscale_ctx = sws_getContext(src_width, src_height, src_pix_fmt, |
|---|
| 222 | src_width, src_height, dst_pix_fmt, |
|---|
| 223 | SWS_BILINEAR, NULL, NULL, NULL); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| [9857] | 226 | osg::notify(osg::NOTICE)<<"Using sws_scale"<<std::endl; |
|---|
| 227 | |
|---|
| [9854] | 228 | return sws_scale(m_swscale_ctx, |
|---|
| 229 | src->data, src->linesize, 0, src_height, |
|---|
| 230 | dst->data, dst->linesize); |
|---|
| 231 | #else |
|---|
| [9857] | 232 | osg::notify(osg::NOTICE)<<"Using img_convert"<<std::endl; |
|---|
| 233 | |
|---|
| 234 | return img_convert(dst, dst_pix_fmt, src, |
|---|
| 235 | src_pix_fmt, src_width, src_height); |
|---|
| [9854] | 236 | #endif |
|---|
| 237 | } |
|---|
| [9816] | 238 | |
|---|
| 239 | |
|---|
| 240 | void FFmpegDecoderVideo::publishFrame(const double delay) |
|---|
| 241 | { |
|---|
| 242 | |
|---|
| 243 | if (m_publish_func == 0) |
|---|
| 244 | return; |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | if (delay < -0.010) |
|---|
| 248 | return; |
|---|
| 249 | |
|---|
| 250 | const AVPicture * const src = (const AVPicture *) m_frame.get(); |
|---|
| 251 | AVPicture * const dst = (AVPicture *) m_frame_rgba.get(); |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | if (m_context->pix_fmt == PIX_FMT_YUVA420P) |
|---|
| 256 | yuva420pToRgba(dst, src, width(), height()); |
|---|
| 257 | else |
|---|
| [9854] | 258 | convert(dst, PIX_FMT_RGB32, src, m_context->pix_fmt, width(), height()); |
|---|
| [9816] | 259 | |
|---|
| 260 | |
|---|
| 261 | swapBuffers(); |
|---|
| 262 | |
|---|
| 263 | |
|---|
| 264 | int i_delay = static_cast<int>(delay * 1000000 + 0.5); |
|---|
| 265 | |
|---|
| 266 | while (i_delay > 1000) |
|---|
| 267 | { |
|---|
| 268 | |
|---|
| 269 | if (m_exit) |
|---|
| 270 | return; |
|---|
| 271 | |
|---|
| 272 | const int micro_delay = (std::min)(1000000, i_delay); |
|---|
| 273 | |
|---|
| 274 | OpenThreads::Thread::microSleep(micro_delay); |
|---|
| 275 | |
|---|
| 276 | i_delay -= micro_delay; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | m_publish_func(* this, m_user_data); |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | void FFmpegDecoderVideo::swapBuffers() |
|---|
| 285 | { |
|---|
| 286 | for (int h = 0; h < height(); ++h) |
|---|
| 287 | memcpy(&m_buffer_rgba_public[(height() - h - 1) * width() * 4], &m_buffer_rgba[h * width() * 4], width() * 4); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | void FFmpegDecoderVideo::yuva420pToRgba(AVPicture * const dst, const AVPicture * const src, int width, int height) |
|---|
| 293 | { |
|---|
| [9854] | 294 | convert(dst, PIX_FMT_RGB32, src, m_context->pix_fmt, width, height); |
|---|
| [9816] | 295 | |
|---|
| 296 | const size_t bpp = 4; |
|---|
| 297 | |
|---|
| 298 | uint8_t * a_dst = dst->data[0] + 3; |
|---|
| 299 | |
|---|
| 300 | for (int h = 0; h < height; ++h) { |
|---|
| 301 | |
|---|
| 302 | const uint8_t * a_src = src->data[3] + h * src->linesize[3]; |
|---|
| 303 | |
|---|
| 304 | for (int w = 0; w < width; ++w) { |
|---|
| 305 | *a_dst = *a_src; |
|---|
| 306 | a_dst += bpp; |
|---|
| 307 | a_src += 1; |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | int FFmpegDecoderVideo::getBuffer(AVCodecContext * const context, AVFrame * const picture) |
|---|
| 315 | { |
|---|
| 316 | const FFmpegDecoderVideo * const this_ = reinterpret_cast<const FFmpegDecoderVideo*>(context->opaque); |
|---|
| 317 | |
|---|
| 318 | const int result = avcodec_default_get_buffer(context, picture); |
|---|
| 319 | int64_t * p_pts = reinterpret_cast<int64_t*>( av_malloc(sizeof(int64_t)) ); |
|---|
| 320 | |
|---|
| 321 | *p_pts = this_->m_packet_pts; |
|---|
| 322 | picture->opaque = p_pts; |
|---|
| 323 | |
|---|
| 324 | return result; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | void FFmpegDecoderVideo::releaseBuffer(AVCodecContext * const context, AVFrame * const picture) |
|---|
| 330 | { |
|---|
| 331 | if (picture != 0) |
|---|
| 332 | av_freep(&picture->opaque); |
|---|
| 333 | |
|---|
| 334 | avcodec_default_release_buffer(context, picture); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | } |
|---|