Advertisement
Guest User

Untitled

a guest
Oct 14th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | None | 0 0
  1. void VideoStream::play_proc()
  2. {
  3.     AVPacket packet;
  4.     int skiped_frames = 0;
  5.     uint64_t frame_delay_i = 0;
  6.     m_current_dts = 0;
  7.     m_frameIdx = 0;
  8.     uint64_t next_ts;
  9.     m_thread_runned = true;
  10.     #if LIBAVCODEC_VERSION_MAJOR > 56
  11.     int ret;
  12.     #else
  13.     int frameFinished = false;
  14.     #endif
  15.  
  16.     InitNullPacket(&packet);
  17.  
  18.     do
  19.     {
  20.         if (m_seek_target)
  21.         {
  22.             if (!av_seek_frame(m_av_ctx, m_video_stream_idx, m_seek_target / (av_q2d(m_videoStream->time_base) * 1000.d),
  23.                                AVSEEK_FLAG_BACKWARD))
  24.                 avcodec_flush_buffers(m_codec_ctx);
  25.             m_paused = false;
  26.         }
  27.         while (m_paused)
  28.             std::this_thread::sleep_for(std::chrono::milliseconds(100));
  29.  
  30.         while ((!m_stoped) && (!m_paused) && (m_frameIdx < m_frames_count))
  31.         {
  32.             bool readed = (av_read_frame(m_av_ctx, &packet)>=0);
  33.             if (m_pause_start)
  34.             {
  35.                 m_start_ts += m_pause_start;
  36.                 m_pause_start = 0;
  37.             }
  38.  
  39.             if(packet.stream_index==m_video_stream_idx)
  40.             {
  41.                 m_frame_mtx.lock();
  42.  
  43.                 #if LIBAVCODEC_VERSION_MAJOR > 56
  44.                 if (avcodec_send_packet(m_codec_ctx, &packet) < 0)
  45.                 {
  46.                     m_frame_mtx.unlock();
  47.                     break;
  48.                 }
  49.  
  50.                 ret = avcodec_receive_frame(m_codec_ctx, m_frame);
  51.                 if (ret == AVERROR(EAGAIN))
  52.                 {
  53.                     av_packet_unref(&packet);
  54.                     m_frame_mtx.unlock();
  55.                     continue;
  56.                 }
  57.                 if ((ret < 0) && (ret != AVERROR_EOF))
  58.                 {
  59.                     m_frame_mtx.unlock();
  60.                     break;
  61.                 }
  62.                 #else
  63.                 avcodec_decode_video2(m_codec_ctx, m_frame, &frameFinished, &packet);
  64.                 #endif
  65.  
  66.  
  67.                 #if LIBAVCODEC_VERSION_MAJOR > 56
  68.                 if ((!readed) && (m_codec_ctx->has_b_frames))
  69.                 #else
  70.                 if ((!readed) && (!frameFinished) && (m_codec_ctx->has_b_frames))
  71.                 #endif
  72.                 {
  73.                     AVPacket NullPacket;
  74.                     InitNullPacket(&NullPacket);
  75.                     #if LIBAVCODEC_VERSION_MAJOR > 56
  76.                     if (avcodec_send_packet(m_codec_ctx, &NullPacket) < 0)
  77.                     {
  78.                         m_frame_mtx.unlock();
  79.                         break;
  80.                     }
  81.  
  82.                     ret = avcodec_receive_frame(m_codec_ctx, m_frame);
  83.                     if ((ret < 0) && (ret != AVERROR(EAGAIN)) && (ret != AVERROR_EOF))
  84.                     {
  85.                         m_frame_mtx.unlock();
  86.                         break;
  87.                     }
  88.                     #else
  89.                     avcodec_decode_video2(m_codec_ctx, m_frame, &frameFinished, &NullPacket);
  90.                     #endif
  91.                 }
  92.                 int64_t frame_dts = m_frame->pkt_dts;
  93.                 m_frame_mtx.unlock();
  94.  
  95.                 if (is_preview)
  96.                     pause();
  97.  
  98.                 if (m_stoped)
  99.                     break;
  100.  
  101.                 m_frameIdx++;
  102.  
  103.                 if (m_speed != 1)
  104.                 {
  105.                     if (skiped_frames < (m_speed - 1))
  106.                     {
  107.                         skiped_frames++;
  108.                         av_packet_unref(&packet);
  109.                         continue;
  110.                     }
  111.                     skiped_frames = 0;
  112.                 }
  113.  
  114.                 double frame_delay = 1.0f / av_q2d(m_videoStream->avg_frame_rate);
  115.                 if (frame_delay == 0.0d)
  116.                     frame_delay_i =  m_frameIdx ? frame_dts / m_frameIdx : 0;
  117.                 else
  118.                     frame_delay_i =  frame_delay * 1000;
  119.                 m_current_dts += frame_delay_i;
  120.  
  121.                 if (m_seek_target)
  122.                 {
  123.                     m_frameIdx = frame_dts * m_frames_count / m_duration_dts;
  124.                     if ((m_start_ts + frame_delay_i * m_frameIdx) < m_seek_target)
  125.                         continue;
  126.                     else
  127.                     {
  128.                         if (m_current_dts > m_seek_target)
  129.                             m_start_ts += m_current_dts;
  130.                         else
  131.                             m_start_ts -= m_seek_target;
  132.                         m_current_dts = m_seek_target;
  133.                         m_seek_target = 0;
  134.                     }
  135.                 }
  136.  
  137.                 next_ts = m_start_ts + m_current_dts;
  138.  
  139.                 frame_present = (m_frameIdx < m_frames_count);
  140.                 first_frame_present = true;
  141.  
  142.                 while ((!m_stoped) && (next_ts > Utils::get_ts()))
  143.                     std::this_thread::sleep_for(std::chrono::milliseconds(frame_delay_i / 10));
  144.             }
  145.             av_packet_unref(&packet);
  146.         }
  147.         if ((!m_stoped) && (m_looped))
  148.         {
  149.             seek_to_ms(1);
  150.             m_frameIdx = 0;
  151.         }
  152.         av_packet_unref(&packet);
  153.     } while (!m_stoped);
  154.     frame_present = false;
  155.     ///av_packet_unref(&packet);
  156.     m_thread_runned = false;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement