video_play_thread.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. // ***********************************************************/
  2. // video_play_thread.cpp
  3. //
  4. // Copy Right @ Steven Huang. All rights reserved.
  5. //
  6. // Video play thread. This section includes key code for
  7. // synchronizing video frames and audio using pts/dts,
  8. // as well as subtitle processing.
  9. // ***********************************************************/
  10. #include "video_play_thread.h"
  11. extern int framedrop;
  12. const QRegularExpression VideoPlayThread::m_assFilter = QRegularExpression("{\\\\.*?}");
  13. const QRegularExpression VideoPlayThread::m_assNewLineReplacer = QRegularExpression("\\\\n|\\\\N");
  14. Q_DECLARE_METATYPE(AVFrame*)
  15. VideoPlayThread::VideoPlayThread(QObject* parent, VideoState* pState)
  16. : QThread(parent)
  17. , m_pState(pState)
  18. {
  19. qRegisterMetaType<AVFrame*>();
  20. }
  21. VideoPlayThread::~VideoPlayThread()
  22. {
  23. stop_thread();
  24. final_resample_param();
  25. }
  26. void VideoPlayThread::run()
  27. {
  28. assert(m_pState);
  29. VideoState* is = m_pState;
  30. double remaining_time = 0.0;
  31. for (;;) {
  32. if (m_bExitThread)
  33. break;
  34. if (is->abort_request)
  35. break;
  36. if (is->paused) {
  37. msleep(10);
  38. continue;
  39. }
  40. if (remaining_time > 0.0)
  41. av_usleep((int64_t) (remaining_time * 1000000.0));
  42. remaining_time = REFRESH_RATE;
  43. if ((!is->paused || is->force_refresh))
  44. video_refresh(is, &remaining_time);
  45. }
  46. qDebug("-------- Video play thread exit.");
  47. }
  48. void VideoPlayThread::video_refresh(VideoState* is, double* remaining_time)
  49. {
  50. double time;
  51. Frame *sp, *sp2;
  52. if (!is->paused && get_master_sync_type(is) == AV_SYNC_EXTERNAL_CLOCK && is->realtime)
  53. check_external_clock_speed(is);
  54. if (is->video_st) {
  55. retry:
  56. if (frame_queue_nb_remaining(&is->pictq) == 0) {
  57. // nothing to do, no picture to display in the queue
  58. *remaining_time = REFRESH_RATE;
  59. } else {
  60. double last_duration, duration, delay;
  61. Frame *vp, *lastvp;
  62. /* dequeue the picture */
  63. lastvp = frame_queue_peek_last(&is->pictq);
  64. vp = frame_queue_peek(&is->pictq);
  65. if (vp->serial != is->videoq.serial) {
  66. frame_queue_next(&is->pictq);
  67. goto retry;
  68. }
  69. if (lastvp->serial != vp->serial)
  70. is->frame_timer = av_gettime_relative() / 1000000.0;
  71. if (is->paused)
  72. goto display;
  73. /* compute nominal last_duration */
  74. last_duration = vp_duration(is, lastvp, vp);
  75. delay = compute_target_delay(last_duration, is);
  76. time = av_gettime_relative() / 1000000.0;
  77. if (time < is->frame_timer + delay) {
  78. *remaining_time = FFMIN(is->frame_timer + delay - time, *remaining_time);
  79. goto display;
  80. }
  81. is->frame_timer += delay;
  82. if (delay > 0 && time - is->frame_timer > AV_SYNC_THRESHOLD_MAX)
  83. is->frame_timer = time;
  84. is->pictq.mutex->lock();
  85. if (!isnan(vp->pts))
  86. update_video_pts(is, vp->pts, vp->pos, vp->serial);
  87. is->pictq.mutex->unlock();
  88. if (frame_queue_nb_remaining(&is->pictq) > 1) {
  89. Frame* nextvp = frame_queue_peek_next(&is->pictq);
  90. duration = vp_duration(is, vp, nextvp);
  91. if (!is->step
  92. && (framedrop > 0
  93. || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER))
  94. && time > is->frame_timer + duration) {
  95. is->frame_drops_late++;
  96. frame_queue_next(&is->pictq);
  97. goto retry;
  98. }
  99. }
  100. if (is->subtitle_st) {
  101. while (frame_queue_nb_remaining(&is->subpq) > 0) {
  102. sp = frame_queue_peek(&is->subpq);
  103. if (frame_queue_nb_remaining(&is->subpq) > 1)
  104. sp2 = frame_queue_peek_next(&is->subpq);
  105. else
  106. sp2 = nullptr;
  107. if (sp->serial != is->subtitleq.serial
  108. || (is->vidclk.pts > (sp->pts + ((float) sp->sub.end_display_time / 1000)))
  109. || (sp2
  110. && is->vidclk.pts
  111. > (sp2->pts + ((float) sp2->sub.start_display_time / 1000)))) {
  112. #if 0
  113. if (sp->uploaded) {
  114. int i;
  115. for (i = 0; i < sp->sub.num_rects; i++) {
  116. AVSubtitleRect* sub_rect = sp->sub.rects[i];
  117. /*uint8_t* pixels;
  118. int pitch, j;
  119. if (!SDL_LockTexture(is->sub_texture, (SDL_Rect*)sub_rect, (void**)&pixels, &pitch)) {
  120. for (j = 0; j < sub_rect->h; j++, pixels += pitch)
  121. memset(pixels, 0, sub_rect->w << 2);
  122. SDL_UnlockTexture(is->sub_texture);
  123. }*/
  124. }
  125. }
  126. #endif
  127. frame_queue_next(&is->subpq);
  128. } else {
  129. break;
  130. }
  131. }
  132. }
  133. frame_queue_next(&is->pictq);
  134. is->force_refresh = 1;
  135. if (is->step && !is->paused)
  136. toggle_pause(is, !is->step);
  137. }
  138. display:
  139. /* display picture */
  140. if (is->force_refresh && is->pictq.rindex_shown)
  141. video_display(is);
  142. }
  143. is->force_refresh = 0;
  144. }
  145. void VideoPlayThread::video_display(VideoState* is)
  146. {
  147. if (is->audio_st && false) {
  148. // video_audio_display(is);
  149. } else if (is->video_st) {
  150. video_image_display(is);
  151. }
  152. }
  153. #if 0
  154. void VideoPlayThread::video_audio_display(VideoState* s)
  155. {
  156. int64_t audio_callback_time = 0;
  157. int i, i_start, x, y1, y, ys, delay, n, nb_display_channels;
  158. int ch, channels, h, h2;
  159. int64_t time_diff;
  160. int rdft_bits, nb_freq;
  161. for (rdft_bits = 1; (1 << rdft_bits) < 2 * s->height; rdft_bits++)
  162. ;
  163. nb_freq = 1 << (rdft_bits - 1);
  164. /* compute display index : center on currently output samples */
  165. channels = s->audio_tgt.channels;
  166. nb_display_channels = channels;
  167. if (!s->paused) {
  168. int data_used = (2 * nb_freq);
  169. n = 2 * channels;
  170. delay = s->audio_write_buf_size;
  171. delay /= n;
  172. /* to be more precise, we take into account the time spent since
  173. the last buffer computation */
  174. if (audio_callback_time) {
  175. time_diff = av_gettime_relative() - audio_callback_time;
  176. delay -= (time_diff * s->audio_tgt.freq) / 1000000;
  177. }
  178. delay += 2 * data_used;
  179. if (delay < data_used)
  180. delay = data_used;
  181. i_start = x = compute_mod(s->sample_array_index - delay * channels, SAMPLE_ARRAY_SIZE);
  182. /*if (s->show_mode == SHOW_MODE_WAVES) {
  183. h = INT_MIN;
  184. for (i = 0; i < 1000; i += channels) {
  185. int idx = (SAMPLE_ARRAY_SIZE + x - i) % SAMPLE_ARRAY_SIZE;
  186. int a = s->sample_array[idx];
  187. int b = s->sample_array[(idx + 4 * channels) % SAMPLE_ARRAY_SIZE];
  188. int c = s->sample_array[(idx + 5 * channels) % SAMPLE_ARRAY_SIZE];
  189. int d = s->sample_array[(idx + 9 * channels) % SAMPLE_ARRAY_SIZE];
  190. int score = a - d;
  191. if (h < score && (b ^ c) < 0) {
  192. h = score;
  193. i_start = idx;
  194. }
  195. }
  196. }*/
  197. s->last_i_start = i_start;
  198. }
  199. else {
  200. i_start = s->last_i_start;
  201. }
  202. #if 0
  203. if (s->show_mode == SHOW_MODE_WAVES) {
  204. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  205. /* total height for one channel */
  206. h = s->height / nb_display_channels;
  207. /* graph height / 2 */
  208. h2 = (h * 9) / 20;
  209. for (ch = 0; ch < nb_display_channels; ch++) {
  210. i = i_start + ch;
  211. y1 = s->ytop + ch * h + (h / 2); /* position of center line */
  212. for (x = 0; x < s->width; x++) {
  213. y = (s->sample_array[i] * h2) >> 15;
  214. if (y < 0) {
  215. y = -y;
  216. ys = y1 - y;
  217. }
  218. else {
  219. ys = y1;
  220. }
  221. fill_rectangle(s->xleft + x, ys, 1, y);
  222. i += channels;
  223. if (i >= SAMPLE_ARRAY_SIZE)
  224. i -= SAMPLE_ARRAY_SIZE;
  225. }
  226. }
  227. SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
  228. for (ch = 1; ch < nb_display_channels; ch++) {
  229. y = s->ytop + ch * h;
  230. fill_rectangle(s->xleft, y, s->width, 1);
  231. }
  232. }
  233. else {
  234. if (realloc_texture(&s->vis_texture, SDL_PIXELFORMAT_ARGB8888, s->width, s->height, SDL_BLENDMODE_NONE, 1) < 0)
  235. return;
  236. if (s->xpos >= s->width)
  237. s->xpos = 0;
  238. nb_display_channels = FFMIN(nb_display_channels, 2);
  239. if (rdft_bits != s->rdft_bits) {
  240. av_rdft_end(s->rdft);
  241. av_free(s->rdft_data);
  242. s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
  243. s->rdft_bits = rdft_bits;
  244. s->rdft_data = av_malloc_array(nb_freq, 4 * sizeof(*s->rdft_data));
  245. }
  246. if (!s->rdft || !s->rdft_data) {
  247. av_log(nullptr, AV_LOG_ERROR, "Failed to allocate buffers for RDFT, switching to waves display\n");
  248. s->show_mode = SHOW_MODE_WAVES;
  249. }
  250. else {
  251. FFTSample* data[2];
  252. SDL_Rect rect = { .x = s->xpos, .y = 0, .w = 1, .h = s->height };
  253. uint32_t* pixels;
  254. int pitch;
  255. for (ch = 0; ch < nb_display_channels; ch++) {
  256. data[ch] = s->rdft_data + 2 * nb_freq * ch;
  257. i = i_start + ch;
  258. for (x = 0; x < 2 * nb_freq; x++) {
  259. double w = (x - nb_freq) * (1.0 / nb_freq);
  260. data[ch][x] = s->sample_array[i] * (1.0 - w * w);
  261. i += channels;
  262. if (i >= SAMPLE_ARRAY_SIZE)
  263. i -= SAMPLE_ARRAY_SIZE;
  264. }
  265. av_rdft_calc(s->rdft, data[ch]);
  266. }
  267. /* Least efficient way to do this, we should of course
  268. * directly access it but it is more than fast enough. */
  269. if (!SDL_LockTexture(s->vis_texture, &rect, (void**)&pixels, &pitch)) {
  270. pitch >>= 2;
  271. pixels += pitch * s->height;
  272. for (y = 0; y < s->height; y++) {
  273. double w = 1 / sqrt(nb_freq);
  274. int a = sqrt(w * sqrt(data[0][2 * y + 0] * data[0][2 * y + 0] + data[0][2 * y + 1] * data[0][2 * y + 1]));
  275. int b = (nb_display_channels == 2) ? sqrt(w * hypot(data[1][2 * y + 0], data[1][2 * y + 1]))
  276. : a;
  277. a = FFMIN(a, 255);
  278. b = FFMIN(b, 255);
  279. pixels -= pitch;
  280. *pixels = (a << 16) + (b << 8) + ((a + b) >> 1);
  281. }
  282. SDL_UnlockTexture(s->vis_texture);
  283. }
  284. SDL_RenderCopy(renderer, s->vis_texture, nullptr, nullptr);
  285. }
  286. if (!s->paused)
  287. s->xpos++;
  288. }
  289. #endif
  290. }
  291. #endif
  292. void VideoPlayThread::video_image_display(VideoState* is)
  293. {
  294. Frame* sp = nullptr;
  295. Frame* vp = frame_queue_peek_last(&is->pictq);
  296. Video_Resample* pResample = &m_Resample;
  297. if (frame_queue_nb_remaining(&is->subpq) > 0) {
  298. sp = frame_queue_peek(&is->subpq);
  299. if (vp->pts >= sp->pts + ((float) sp->sub.start_display_time / 1000)) {
  300. if (!sp->uploaded) {
  301. // uint8_t* pixels[4];
  302. // int pitch[4];
  303. if (!sp->width || !sp->height) {
  304. sp->width = vp->width;
  305. sp->height = vp->height;
  306. }
  307. // if (realloc_texture(&is->sub_texture, SDL_PIXELFORMAT_ARGB8888,
  308. // sp->width, sp->height, SDL_BLENDMODE_BLEND, 1) < 0) return;
  309. #if 1
  310. for (unsigned int i = 0; i < sp->sub.num_rects; i++) {
  311. AVSubtitleRect* sub_rect = sp->sub.rects[i];
  312. if (sub_rect->type == SUBTITLE_ASS) {
  313. qDebug("subtitle[%d], format:%d, type:%d, text:%s, flags:%d",
  314. i,
  315. sp->sub.format,
  316. sub_rect->type,
  317. sub_rect->text,
  318. sub_rect->flags);
  319. // QString ass = QString::fromUtf8(sub_rect->ass);
  320. QString ass = QString::fromLocal8Bit(
  321. QString::fromStdString(sub_rect->ass).toUtf8());
  322. QStringList assList = ass.split(",");
  323. if (assList.size() > 8) {
  324. ass = assList[8];
  325. qDebug("ass: %s", qUtf8Printable(ass));
  326. parse_subtitle_ass(ass);
  327. }
  328. } else {
  329. qWarning("not handled yet, type:%d", sub_rect->type);
  330. }
  331. }
  332. #else
  333. for (i = 0; i < sp->sub.num_rects; i++) {
  334. AVSubtitleRect* sub_rect = sp->sub.rects[i];
  335. sub_rect->x = av_clip(sub_rect->x, 0, sp->width);
  336. sub_rect->y = av_clip(sub_rect->y, 0, sp->height);
  337. sub_rect->w = av_clip(sub_rect->w, 0, sp->width - sub_rect->x);
  338. sub_rect->h = av_clip(sub_rect->h, 0, sp->height - sub_rect->y);
  339. is->sub_convert_ctx = sws_getCachedContext(is->sub_convert_ctx,
  340. sub_rect->w,
  341. sub_rect->h,
  342. AV_PIX_FMT_PAL8,
  343. sub_rect->w,
  344. sub_rect->h,
  345. AV_PIX_FMT_BGRA,
  346. 0,
  347. nullptr,
  348. nullptr,
  349. nullptr);
  350. if (!is->sub_convert_ctx) {
  351. av_log(nullptr, AV_LOG_FATAL, "Cannot initialize the conversion context\n");
  352. return;
  353. }
  354. #if 1
  355. sws_scale(is->sub_convert_ctx,
  356. (const uint8_t* const*) sub_rect->data,
  357. sub_rect->linesize,
  358. 0,
  359. sub_rect->h,
  360. pixels,
  361. pitch);
  362. #else
  363. if (!SDL_LockTexture(is->sub_texture,
  364. (SDL_Rect*) sub_rect,
  365. (void**) pixels,
  366. pitch)) {
  367. sws_scale(is->sub_convert_ctx,
  368. (const uint8_t* const*) sub_rect->data,
  369. sub_rect->linesize,
  370. 0,
  371. sub_rect->h,
  372. pixels,
  373. pitch);
  374. SDL_UnlockTexture(is->sub_texture);
  375. }
  376. #endif
  377. }
  378. #endif
  379. sp->uploaded = 1;
  380. }
  381. } else {
  382. sp = nullptr;
  383. }
  384. }
  385. AVFrame* pFrameRGB = pResample->pFrameRGB; // dst
  386. AVCodecContext* pVideoCtx = is->viddec.avctx;
  387. AVFrame* pFrame = vp->frame;
  388. // AVPixelFormat fmt = (AVPixelFormat)pFrame->format; // 0
  389. // const char* fmt_name = av_get_pix_fmt_name(fmt);
  390. // AVHWFramesContext* ctx =
  391. // (AVHWFramesContext*)pVideoCtx->hw_frames_ctx->data; AVPixelFormat sw_fmt =
  392. // ctx->sw_format;
  393. // qDebug("frame w:%d,h:%d, pts:%lld, dts:%lld", pVideoCtx->width,
  394. // pVideoCtx->height, pFrame->pts, pFrame->pkt_dts);
  395. // TODO: 不转换
  396. sws_scale(pResample->sws_ctx,
  397. (uint8_t const* const*) pFrame->data,
  398. pFrame->linesize,
  399. 0,
  400. pVideoCtx->height,
  401. pFrameRGB->data,
  402. pFrameRGB->linesize);
  403. // QImage img(pVideoCtx->width, pVideoCtx->height, QImage::Format_RGB888);
  404. // for (int y = 0; y < pVideoCtx->height; ++y) {
  405. // memcpy(img.scanLine(y),
  406. // pFrameRGB->data[0] + y * pFrameRGB->linesize[0],
  407. // pVideoCtx->width * 3);
  408. // }
  409. // emit frame_ready(img);
  410. emit frameReady(pFrameRGB);
  411. }
  412. bool VideoPlayThread::init_resample_param(AVCodecContext* pVideo, bool bHardware)
  413. {
  414. Video_Resample* pResample = &m_Resample;
  415. if (pVideo) {
  416. enum AVPixelFormat pix_fmt = pVideo->pix_fmt; // frame format after decode
  417. if (bHardware)
  418. pix_fmt = AV_PIX_FMT_NV12;
  419. struct SwsContext* sws_ctx
  420. = sws_getContext(pVideo->width,
  421. pVideo->height,
  422. pix_fmt, // AV_PIX_FMT_YUV420P
  423. pVideo->width,
  424. pVideo->height,
  425. AV_PIX_FMT_RGB24, // sws_scale destination color scheme
  426. SWS_BILINEAR,
  427. nullptr,
  428. nullptr,
  429. nullptr);
  430. AVFrame* pFrameRGB = av_frame_alloc();
  431. if (!pFrameRGB) {
  432. printf("Could not allocate rgb frame.\n");
  433. return false;
  434. }
  435. pFrameRGB->width = pVideo->width;
  436. pFrameRGB->height = pVideo->height;
  437. pFrameRGB->format = AV_PIX_FMT_RGB24;
  438. int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, pVideo->width, pVideo->height, 32);
  439. uint8_t* const buffer_RGB = (uint8_t*) av_malloc(numBytes * sizeof(uint8_t));
  440. if (!buffer_RGB) {
  441. printf("Could not allocate buffer.\n");
  442. return false;
  443. }
  444. av_image_fill_arrays(pFrameRGB->data,
  445. pFrameRGB->linesize,
  446. buffer_RGB,
  447. AV_PIX_FMT_RGB24,
  448. pVideo->width,
  449. pVideo->height,
  450. 32);
  451. pResample->sws_ctx = sws_ctx;
  452. pResample->pFrameRGB = pFrameRGB;
  453. pResample->buffer_RGB = buffer_RGB;
  454. return true;
  455. }
  456. return false;
  457. }
  458. void VideoPlayThread::final_resample_param()
  459. {
  460. Video_Resample* pResample = &m_Resample;
  461. // Free video resample context
  462. sws_freeContext(pResample->sws_ctx);
  463. // Free the RGB image
  464. av_free(pResample->buffer_RGB);
  465. av_frame_free(&pResample->pFrameRGB);
  466. av_free(pResample->pFrameRGB);
  467. }
  468. void VideoPlayThread::stop_thread()
  469. {
  470. m_bExitThread = true;
  471. wait();
  472. }
  473. void VideoPlayThread::parse_subtitle_ass(const QString& text)
  474. {
  475. QString str = text;
  476. str.remove(m_assFilter);
  477. str.replace(m_assNewLineReplacer, "\n");
  478. str = str.trimmed();
  479. emit subtitle_ready(str);
  480. }