video_state.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. // ***********************************************************/
  2. // video_state.cpp
  3. //
  4. // Copy Right @ Steven Huang. All rights reserved.
  5. //
  6. // A/V synchronization state. This code is referenced
  7. // from ffplay.c in Ffmpeg library.
  8. // ***********************************************************/
  9. #include "video_state.h"
  10. int infinite_buffer = -1;
  11. int64_t start_time = AV_NOPTS_VALUE;
  12. static enum AVPixelFormat hw_pix_fmt;
  13. VideoStateData::VideoStateData(bool use_hardware, bool loop_play)
  14. : m_bUseHardware(use_hardware)
  15. , m_bLoopPlay(loop_play)
  16. {}
  17. VideoStateData::~VideoStateData()
  18. {
  19. close_hardware();
  20. delete_video_state();
  21. }
  22. void VideoStateData::delete_video_state()
  23. {
  24. if (m_pState) {
  25. stream_close(m_pState);
  26. m_pState = nullptr;
  27. }
  28. }
  29. VideoState* VideoStateData::get_state() const
  30. {
  31. return m_pState;
  32. }
  33. bool VideoStateData::is_hardware_decode() const
  34. {
  35. return m_bHardwareSuccess;
  36. }
  37. int VideoStateData::create_video_state(const char* filename)
  38. {
  39. int ret = -1;
  40. if (!filename || !filename[0]) {
  41. qDebug("filename is invalid, please select a valid media file.");
  42. return ret;
  43. }
  44. m_pState = stream_open(filename);
  45. if (!m_pState) {
  46. qDebug("stream_open failed!");
  47. return ret;
  48. }
  49. return open_media(m_pState);
  50. }
  51. void VideoStateData::print_state() const
  52. {
  53. if (const auto is = m_pState) {
  54. qDebug("[VideoState]PacketQueue(v:%p,a:%p,s:%p)", &is->videoq, &is->audioq, &is->subtitleq);
  55. qDebug("[VideoState]FrameQueue(v:%p,a:%p,s:%p)", &is->pictq, &is->sampq, &is->subpq);
  56. qDebug("[VideoState]Decoder(v:%p,a:%p,s:%p)", &is->viddec, &is->auddec, &is->subdec);
  57. qDebug("[VideoState]Clock(v:%p,a:%p,s:%p)", &is->vidclk, &is->audclk, &is->extclk);
  58. }
  59. }
  60. int decode_interrupt_cb(void* ctx)
  61. {
  62. VideoState* is = (VideoState*) ctx;
  63. return is->abort_request;
  64. }
  65. int VideoStateData::open_media(VideoState* is)
  66. {
  67. assert(is);
  68. int err;
  69. uint i;
  70. int ret = -1;
  71. int st_index[AVMEDIA_TYPE_NB];
  72. AVFormatContext* ic = nullptr;
  73. const char* wanted_stream_spec[AVMEDIA_TYPE_NB] = {0};
  74. memset(st_index, -1, sizeof(st_index));
  75. is->eof = 0;
  76. ic = avformat_alloc_context();
  77. if (!ic) {
  78. av_log(nullptr, AV_LOG_FATAL, "Could not allocate context.\n");
  79. ret = AVERROR(ENOMEM);
  80. goto fail;
  81. }
  82. ic->interrupt_callback.callback = decode_interrupt_cb;
  83. ic->interrupt_callback.opaque = is;
  84. err = avformat_open_input(&ic, is->filename, is->iformat, nullptr);
  85. if (err < 0) {
  86. av_log(nullptr, AV_LOG_FATAL, "failed to open %s: %d", is->filename, err);
  87. ret = -1;
  88. goto fail;
  89. }
  90. // if (!av_dict_get(format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
  91. // av_dict_set(&format_opts, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
  92. // scan_all_pmts_set = 1;
  93. // }
  94. // err = avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
  95. // if (err < 0) {
  96. // print_error(is->filename, err);
  97. // ret = -1;
  98. // goto fail;
  99. // }
  100. // if (scan_all_pmts_set)
  101. // av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
  102. // remove_avoptions(&format_opts, codec_opts);
  103. // ret = check_avoptions(format_opts);
  104. // if (ret < 0)
  105. // goto fail;
  106. is->ic = ic;
  107. // if (genpts)
  108. // ic->flags |= AVFMT_FLAG_GENPTS;
  109. // if (find_stream_info) {
  110. // AVDictionary **opts;
  111. // int orig_nb_streams = ic->nb_streams;
  112. // err = setup_find_stream_info_opts(ic, codec_opts, &opts);
  113. // if (err < 0) {
  114. // av_log(NULL, AV_LOG_ERROR,
  115. // "Error setting up avformat_find_stream_info() options\n");
  116. // ret = err;
  117. // goto fail;
  118. // }
  119. // err = avformat_find_stream_info(ic, opts);
  120. // for (i = 0; i < orig_nb_streams; i++)
  121. // av_dict_free(&opts[i]);
  122. // av_freep(&opts);
  123. // if (err < 0) {
  124. // av_log(NULL, AV_LOG_WARNING,
  125. // "%s: could not find codec parameters\n", is->filename);
  126. // ret = -1;
  127. // goto fail;
  128. // }
  129. // }
  130. av_format_inject_global_side_data(ic);
  131. err = avformat_find_stream_info(ic, nullptr);
  132. if (err < 0) {
  133. av_log(nullptr, AV_LOG_WARNING, "%s: could not find codec parameters\n", is->filename);
  134. ret = -1;
  135. goto fail;
  136. }
  137. if (ic->pb) {
  138. // FIXME hack, ffplay maybe should not use avio_feof() to test for the end
  139. ic->pb->eof_reached = 0;
  140. }
  141. if (seek_by_bytes < 0)
  142. seek_by_bytes = !(ic->iformat->flags & AVFMT_NO_BYTE_SEEK)
  143. && !!(ic->iformat->flags & AVFMT_TS_DISCONT)
  144. && strcmp("ogg", ic->iformat->name);
  145. // is->max_frame_duration = (ic->iformat->flags & AVFMT_TS_DISCONT) ? 10.0 : 3600.0;
  146. is->max_frame_duration = 2.0;
  147. /* if seeking requested, we execute it */
  148. if (start_time != AV_NOPTS_VALUE) {
  149. int64_t timestamp;
  150. timestamp = start_time;
  151. /* add the stream start time */
  152. if (ic->start_time != AV_NOPTS_VALUE)
  153. timestamp += ic->start_time;
  154. ret = avformat_seek_file(ic, -1, INT64_MIN, timestamp, INT64_MAX, 0);
  155. if (ret < 0) {
  156. av_log(NULL,
  157. AV_LOG_WARNING,
  158. "%s: could not seek to position %0.3f\n",
  159. is->filename,
  160. (double) timestamp / AV_TIME_BASE);
  161. }
  162. }
  163. is->realtime = is_realtime(ic);
  164. // if (show_status)
  165. av_dump_format(ic, 0, is->filename, 0);
  166. for (i = 0; i < ic->nb_streams; i++) {
  167. AVStream* st = ic->streams[i];
  168. enum AVMediaType type = st->codecpar->codec_type;
  169. st->discard = AVDISCARD_ALL;
  170. if (type >= 0 && wanted_stream_spec[type] && st_index[type] == -1)
  171. if (avformat_match_stream_specifier(ic, st, wanted_stream_spec[type]) > 0)
  172. st_index[type] = i;
  173. }
  174. for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
  175. if (wanted_stream_spec[i] && st_index[i] == -1) {
  176. av_log(nullptr,
  177. AV_LOG_ERROR,
  178. "Stream specifier %s does not match any %s stream\n",
  179. wanted_stream_spec[i],
  180. av_get_media_type_string(AVMediaType(i)));
  181. st_index[i] = INT_MAX;
  182. }
  183. }
  184. // if (!video_disable)
  185. st_index[AVMEDIA_TYPE_VIDEO]
  186. = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, st_index[AVMEDIA_TYPE_VIDEO], -1, NULL, 0);
  187. // if (!audio_disable)
  188. st_index[AVMEDIA_TYPE_AUDIO] = av_find_best_stream(ic,
  189. AVMEDIA_TYPE_AUDIO,
  190. st_index[AVMEDIA_TYPE_AUDIO],
  191. st_index[AVMEDIA_TYPE_VIDEO],
  192. NULL,
  193. 0);
  194. // if (!video_disable && !subtitle_disable)
  195. st_index[AVMEDIA_TYPE_SUBTITLE] = av_find_best_stream(ic,
  196. AVMEDIA_TYPE_SUBTITLE,
  197. st_index[AVMEDIA_TYPE_SUBTITLE],
  198. (st_index[AVMEDIA_TYPE_AUDIO] >= 0
  199. ? st_index[AVMEDIA_TYPE_AUDIO]
  200. : st_index[AVMEDIA_TYPE_VIDEO]),
  201. NULL,
  202. 0);
  203. /* open the streams */
  204. if (st_index[AVMEDIA_TYPE_VIDEO] >= 0) {
  205. stream_component_open(is, st_index[AVMEDIA_TYPE_VIDEO]);
  206. }
  207. if (st_index[AVMEDIA_TYPE_AUDIO] >= 0) {
  208. stream_component_open(is, st_index[AVMEDIA_TYPE_AUDIO]);
  209. }
  210. if (st_index[AVMEDIA_TYPE_SUBTITLE] >= 0) {
  211. stream_component_open(is, st_index[AVMEDIA_TYPE_SUBTITLE]);
  212. }
  213. if (is->video_stream < 0 && is->audio_stream < 0) {
  214. av_log(nullptr,
  215. AV_LOG_FATAL,
  216. "Failed to open file '%s' or configure filtergraph\n",
  217. is->filename);
  218. ret = -1;
  219. goto fail;
  220. }
  221. if (infinite_buffer < 0 && is->realtime)
  222. infinite_buffer = 1;
  223. return 0;
  224. fail:
  225. if (ic && !is->ic)
  226. avformat_close_input(&ic);
  227. return ret;
  228. }
  229. VideoState* VideoStateData::stream_open(const char* filename, const AVInputFormat* iformat)
  230. {
  231. VideoState* is = nullptr;
  232. int startup_volume = 100;
  233. int av_sync_type = AV_SYNC_AUDIO_MASTER;
  234. is = (VideoState*) av_mallocz(sizeof(VideoState));
  235. if (!is)
  236. return nullptr;
  237. is->last_video_stream = is->video_stream = -1;
  238. is->last_audio_stream = is->audio_stream = -1;
  239. is->last_subtitle_stream = is->subtitle_stream = -1;
  240. is->filename = av_strdup(filename);
  241. if (!is->filename)
  242. goto fail;
  243. is->iformat = iformat;
  244. is->ytop = 0;
  245. is->xleft = 0;
  246. /* start video display */
  247. if (frame_queue_init(&is->pictq, &is->videoq, VIDEO_PICTURE_QUEUE_SIZE, 1) < 0)
  248. goto fail;
  249. if (frame_queue_init(&is->subpq, &is->subtitleq, SUBPICTURE_QUEUE_SIZE, 0) < 0)
  250. goto fail;
  251. if (frame_queue_init(&is->sampq, &is->audioq, SAMPLE_QUEUE_SIZE, 1) < 0)
  252. goto fail;
  253. if (packet_queue_init(&is->videoq) < 0 || packet_queue_init(&is->audioq) < 0
  254. || packet_queue_init(&is->subtitleq) < 0)
  255. goto fail;
  256. if (!(is->continue_read_thread = new QWaitCondition())) {
  257. av_log(nullptr, AV_LOG_FATAL, "new QWaitCondition() failed!\n");
  258. goto fail;
  259. }
  260. init_clock(&is->vidclk, &is->videoq.serial);
  261. init_clock(&is->audclk, &is->audioq.serial);
  262. init_clock(&is->extclk, &is->extclk.serial);
  263. is->audio_clock_serial = -1;
  264. if (startup_volume < 0)
  265. av_log(nullptr, AV_LOG_WARNING, "-volume=%d < 0, setting to 0\n", startup_volume);
  266. if (startup_volume > 100)
  267. av_log(nullptr, AV_LOG_WARNING, "-volume=%d > 100, setting to 100\n", startup_volume);
  268. startup_volume = av_clip(startup_volume, 0, 100);
  269. startup_volume = av_clip(SDL_MIX_MAXVOLUME * startup_volume / 100, 0, SDL_MIX_MAXVOLUME);
  270. is->audio_volume = startup_volume;
  271. is->muted = 0;
  272. is->av_sync_type = av_sync_type;
  273. // is->read_tid = SDL_CreateThread(read_thread, "read_thread", is);;
  274. is->read_thread_exit = -1;
  275. is->loop = int(m_bLoopPlay);
  276. // is->threads = {nullptr};
  277. is->threads.read_tid = nullptr;
  278. is->threads.video_decode_tid = nullptr;
  279. is->threads.audio_decode_tid = nullptr;
  280. is->threads.video_play_tid = nullptr;
  281. is->threads.audio_play_tid = nullptr;
  282. is->threads.subtitle_decode_tid = nullptr;
  283. #if USE_AVFILTER_AUDIO
  284. is->audio_speed = 1.0;
  285. #endif
  286. return is;
  287. fail:
  288. stream_close(is);
  289. return nullptr;
  290. }
  291. void VideoStateData::threads_setting(VideoState* is, const Threads& threads)
  292. {
  293. if (!is)
  294. return;
  295. assert(!is->threads.read_tid);
  296. assert(!is->threads.video_decode_tid);
  297. assert(!is->threads.audio_decode_tid);
  298. assert(!is->threads.video_play_tid);
  299. assert(!is->threads.audio_play_tid);
  300. assert(!is->threads.subtitle_decode_tid);
  301. // is->threads = threads;
  302. is->threads.read_tid = threads.read_tid;
  303. is->threads.video_decode_tid = threads.video_decode_tid;
  304. is->threads.audio_decode_tid = threads.audio_decode_tid;
  305. is->threads.video_play_tid = threads.video_play_tid;
  306. is->threads.audio_play_tid = threads.audio_play_tid;
  307. is->threads.subtitle_decode_tid = threads.subtitle_decode_tid;
  308. }
  309. void VideoStateData::threads_exit_wait(VideoState* is)
  310. {
  311. if (!is)
  312. return;
  313. auto try_stop_and_join = [](ThreadBase* t) {
  314. if (t) {
  315. t->stop(); // 通知线程退出
  316. t->join(); // 等待线程结束
  317. // 注意:不要delete,线程对象由PlayerController的unique_ptr管理
  318. }
  319. };
  320. try_stop_and_join(is->threads.video_play_tid);
  321. try_stop_and_join(is->threads.audio_play_tid);
  322. try_stop_and_join(is->threads.video_decode_tid);
  323. try_stop_and_join(is->threads.audio_decode_tid);
  324. try_stop_and_join(is->threads.subtitle_decode_tid);
  325. }
  326. void VideoStateData::stream_close(VideoState* is)
  327. {
  328. assert(is);
  329. is->abort_request = 1;
  330. // SDL_WaitThread(is->read_tid, NULL);
  331. {
  332. if (is->read_thread_exit == 0) {
  333. if (is->threads.read_tid) {
  334. is->threads.read_tid->stop();
  335. av_log(nullptr, AV_LOG_INFO, "read thread wait before!\n");
  336. is->threads.read_tid->join();
  337. av_log(nullptr, AV_LOG_INFO, "read thread wait after!\n");
  338. is->threads.read_tid = nullptr;
  339. }
  340. }
  341. }
  342. /* close each stream */
  343. if (is->audio_stream >= 0)
  344. stream_component_close(is, is->audio_stream);
  345. if (is->video_stream >= 0)
  346. stream_component_close(is, is->video_stream);
  347. if (is->subtitle_stream >= 0)
  348. stream_component_close(is, is->subtitle_stream);
  349. //threads_exit_wait(is); // 等待所有线程退出
  350. avformat_close_input(&is->ic);
  351. packet_queue_destroy(&is->videoq);
  352. packet_queue_destroy(&is->audioq);
  353. packet_queue_destroy(&is->subtitleq);
  354. /* free all pictures */
  355. frame_queue_destory(&is->pictq);
  356. frame_queue_destory(&is->sampq);
  357. frame_queue_destory(&is->subpq);
  358. // SDL_DestroyCond(is->continue_read_thread);
  359. if (is->continue_read_thread) {
  360. delete is->continue_read_thread;
  361. is->continue_read_thread = nullptr;
  362. }
  363. sws_freeContext(is->img_convert_ctx);
  364. sws_freeContext(is->sub_convert_ctx);
  365. av_free(is->filename);
  366. // if (is->vis_texture)
  367. // SDL_DestroyTexture(is->vis_texture);
  368. // if (is->vid_texture)
  369. // SDL_DestroyTexture(is->vid_texture);
  370. // if (is->sub_texture)
  371. // SDL_DestroyTexture(is->sub_texture);
  372. av_free(is);
  373. }
  374. static enum AVPixelFormat get_hw_format(AVCodecContext* ctx, const enum AVPixelFormat* pix_fmts)
  375. {
  376. for (const enum AVPixelFormat* p = pix_fmts; *p != -1; p++) {
  377. if (*p == hw_pix_fmt)
  378. return *p;
  379. }
  380. fprintf(stderr, "Failed to get HW surface format, codec_id=%d\n", (int) ctx->codec_id);
  381. return AV_PIX_FMT_NONE;
  382. }
  383. // static int hw_decoder_init(AVCodecContext* ctx, const enum AVHWDeviceType
  384. // type)
  385. //{
  386. // int err = 0;
  387. //
  388. // if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type, nullptr,
  389. //nullptr, 0)) < 0) { fprintf(stderr, "Failed to create specified HW
  390. //device.\n"); return err;
  391. // }
  392. //
  393. // ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
  394. //
  395. // return err;
  396. //}
  397. int VideoStateData::hw_decoder_init(AVCodecContext* ctx, const enum AVHWDeviceType type)
  398. {
  399. int err = 0;
  400. if ((err = av_hwdevice_ctx_create(&m_hw_device_ctx, type, nullptr, nullptr, 0)) < 0) {
  401. fprintf(stderr, "Failed to create specified HW device.\n");
  402. return err;
  403. }
  404. ctx->hw_device_ctx = av_buffer_ref(m_hw_device_ctx);
  405. return err;
  406. }
  407. bool VideoStateData::open_hardware(AVCodecContext* avctx, const AVCodec* codec, const char* device)
  408. {
  409. enum AVHWDeviceType type = get_hwdevice(device);
  410. hw_pix_fmt = get_hwdevice_decoder(codec, type);
  411. avctx->get_format = get_hw_format;
  412. if (hw_decoder_init(avctx, type) < 0)
  413. return false;
  414. return true;
  415. }
  416. void VideoStateData::close_hardware()
  417. {
  418. av_buffer_unref(&m_hw_device_ctx);
  419. }
  420. int VideoStateData::stream_component_open(VideoState* is, int stream_index)
  421. {
  422. assert(is);
  423. AVFormatContext* ic = is->ic;
  424. AVCodecContext* avctx;
  425. const AVCodec* codec;
  426. AVDictionary* opts = nullptr;
  427. // const AVDictionaryEntry* t = nullptr;
  428. int sample_rate, nb_channels;
  429. AVChannelLayout ch_layout = {0};
  430. // int64_t
  431. int format;
  432. int ret = 0;
  433. int stream_lowres = 0;
  434. if (stream_index < 0 || ((unsigned int) stream_index) >= ic->nb_streams)
  435. return -1;
  436. avctx = avcodec_alloc_context3(nullptr);
  437. if (!avctx)
  438. return AVERROR(ENOMEM);
  439. ret = avcodec_parameters_to_context(avctx, ic->streams[stream_index]->codecpar);
  440. if (ret < 0)
  441. goto fail;
  442. avctx->pkt_timebase = ic->streams[stream_index]->time_base;
  443. codec = avcodec_find_decoder(avctx->codec_id);
  444. switch (avctx->codec_type) {
  445. case AVMEDIA_TYPE_AUDIO:
  446. is->last_audio_stream = stream_index;
  447. break;
  448. case AVMEDIA_TYPE_SUBTITLE:
  449. is->last_subtitle_stream = stream_index;
  450. break;
  451. case AVMEDIA_TYPE_VIDEO:
  452. is->last_video_stream = stream_index;
  453. if (m_bUseHardware) {
  454. m_bHardwareSuccess = false;
  455. const char* hardware_device = "dxva2"; // device = <vaapi|vdpau|dxva2|d3d11va>
  456. ret = open_hardware(avctx, codec, hardware_device);
  457. if (!ret) {
  458. qWarning("hardware-accelerated opened failed, device:%s", hardware_device);
  459. goto fail;
  460. }
  461. qInfo("hardware-accelerated opened, device:%s", hardware_device);
  462. m_bHardwareSuccess = true;
  463. }
  464. break;
  465. }
  466. if (!codec) {
  467. av_log(nullptr,
  468. AV_LOG_WARNING,
  469. "No decoder could be found for codec %s\n",
  470. avcodec_get_name(avctx->codec_id));
  471. ret = AVERROR(EINVAL);
  472. goto fail;
  473. }
  474. avctx->codec_id = codec->id;
  475. if (stream_lowres > codec->max_lowres) {
  476. av_log(avctx,
  477. AV_LOG_WARNING,
  478. "The maximum value for lowres supported by the decoder is %d\n",
  479. codec->max_lowres);
  480. stream_lowres = codec->max_lowres;
  481. }
  482. avctx->lowres = stream_lowres;
  483. // avctx->flags2 |= AV_CODEC_FLAG2_FAST;
  484. /*opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
  485. if (!av_dict_get(opts, "threads", NULL, 0))
  486. av_dict_set(&opts, "threads", "auto", 0);
  487. if (stream_lowres)
  488. av_dict_set_int(&opts, "lowres", stream_lowres, 0);*/
  489. if ((ret = avcodec_open2(avctx, codec, &opts)) < 0) {
  490. goto fail;
  491. }
  492. is->eof = 0;
  493. ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
  494. switch (avctx->codec_type) {
  495. case AVMEDIA_TYPE_AUDIO:
  496. #if USE_AVFILTER_AUDIO
  497. {
  498. AVFilterContext* sink;
  499. // const char* afilters =
  500. // "aresample=8000,aformat=sample_fmts=s16:channel_layouts=mono"; //
  501. // "atempo=2"; const char* afilters = nullptr; const char* afilters =
  502. // "atempo=2.0";
  503. is->audio_filter_src.freq = avctx->sample_rate;
  504. is->audio_filter_src.ch_layout.nb_channels = avctx->ch_layout.nb_channels; // avctx->channels;
  505. is->audio_filter_src.ch_layout = avctx->ch_layout; // avctx->channel_layout
  506. is->audio_filter_src.fmt = avctx->sample_fmt;
  507. if ((ret = configure_audio_filters(is, is->afilters, 0)) < 0)
  508. goto fail;
  509. sink = is->out_audio_filter;
  510. sample_rate = av_buffersink_get_sample_rate(sink);
  511. nb_channels = av_buffersink_get_channels(sink);
  512. // channel_layout = av_buffersink_get_channel_layout(sink);
  513. format = av_buffersink_get_format(sink);
  514. AVChannelLayout chn_layout;
  515. av_buffersink_get_ch_layout(sink, &chn_layout);
  516. qDebug("afilter sink: sample rate:%d, chn:%d, fmt:%d, chn_layout:%d",
  517. sample_rate,
  518. nb_channels,
  519. format,
  520. chn_layout.u);
  521. }
  522. #else
  523. sample_rate = avctx->sample_rate;
  524. ret = av_channel_layout_copy(&ch_layout, &avctx->ch_layout);
  525. if (ret < 0)
  526. goto fail;
  527. #endif
  528. /* prepare audio output */
  529. /*if ((ret = audio_open(is, chn_layout, nb_channels, sample_rate,
  530. &is->audio_tgt)) < 0) goto fail;
  531. is->audio_src = is->audio_tgt;*/
  532. is->audio_stream = stream_index;
  533. is->audio_st = ic->streams[stream_index];
  534. if ((is->ic->iformat->flags
  535. & (AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK))) {
  536. is->auddec.start_pts = is->audio_st->start_time;
  537. is->auddec.start_pts_tb = is->audio_st->time_base;
  538. }
  539. m_bHasAudio = true;
  540. m_avctxAudio = avctx;
  541. break;
  542. case AVMEDIA_TYPE_VIDEO:
  543. is->video_stream = stream_index;
  544. is->video_st = ic->streams[stream_index];
  545. // 设置视频水印
  546. set_video_watermark(is, "Watermark");
  547. m_bHasVideo = true;
  548. m_avctxVideo = avctx;
  549. break;
  550. case AVMEDIA_TYPE_SUBTITLE:
  551. is->subtitle_stream = stream_index;
  552. is->subtitle_st = ic->streams[stream_index];
  553. m_bHasSubtitle = true;
  554. m_avctxSubtitle = avctx;
  555. break;
  556. default:
  557. break;
  558. }
  559. goto out;
  560. fail:
  561. avcodec_free_context(&avctx);
  562. out:
  563. av_dict_free(&opts);
  564. return ret;
  565. }
  566. void VideoStateData::stream_component_close(VideoState* is, int stream_index)
  567. {
  568. assert(is);
  569. AVFormatContext* ic = is->ic;
  570. AVCodecParameters* codecpar;
  571. if (stream_index < 0 || ((unsigned int) stream_index) >= ic->nb_streams)
  572. return;
  573. codecpar = ic->streams[stream_index]->codecpar;
  574. switch (codecpar->codec_type) {
  575. case AVMEDIA_TYPE_AUDIO:
  576. decoder_abort(&is->auddec, &is->sampq);
  577. // SDL_CloseAudioDevice(audio_dev);
  578. decoder_destroy(&is->auddec);
  579. // swr_free(&is->swr_ctx);
  580. // av_freep(&is->audio_buf1);
  581. // is->audio_buf1_size = 0;
  582. // is->audio_buf = nullptr;
  583. /*if (is->rdft) {
  584. av_rdft_end(is->rdft);
  585. av_freep(&is->rdft_data);
  586. is->rdft = nullptr;
  587. is->rdft_bits = 0;
  588. }*/
  589. break;
  590. case AVMEDIA_TYPE_VIDEO:
  591. decoder_abort(&is->viddec, &is->pictq);
  592. decoder_destroy(&is->viddec);
  593. break;
  594. case AVMEDIA_TYPE_SUBTITLE:
  595. decoder_abort(&is->subdec, &is->subpq);
  596. decoder_destroy(&is->subdec);
  597. break;
  598. default:
  599. qDebug("Not handled yet.......code type:%d", codecpar->codec_type);
  600. break;
  601. }
  602. ic->streams[stream_index]->discard = AVDISCARD_ALL;
  603. switch (codecpar->codec_type) {
  604. case AVMEDIA_TYPE_AUDIO:
  605. is->audio_st = nullptr;
  606. is->audio_stream = -1;
  607. break;
  608. case AVMEDIA_TYPE_VIDEO:
  609. is->video_st = nullptr;
  610. is->video_stream = -1;
  611. break;
  612. case AVMEDIA_TYPE_SUBTITLE:
  613. is->subtitle_st = nullptr;
  614. is->subtitle_stream = -1;
  615. break;
  616. default:
  617. break;
  618. }
  619. }
  620. bool VideoStateData::has_video() const
  621. {
  622. return m_bHasVideo;
  623. }
  624. bool VideoStateData::has_audio() const
  625. {
  626. return m_bHasAudio;
  627. }
  628. bool VideoStateData::has_subtitle() const
  629. {
  630. return m_bHasSubtitle;
  631. }
  632. AVCodecContext* VideoStateData::get_contex(AVMediaType type) const
  633. {
  634. AVCodecContext* pCtx = nullptr;
  635. switch (type) {
  636. case AVMEDIA_TYPE_AUDIO:
  637. pCtx = m_avctxAudio;
  638. break;
  639. case AVMEDIA_TYPE_VIDEO:
  640. pCtx = m_avctxVideo;
  641. break;
  642. case AVMEDIA_TYPE_SUBTITLE:
  643. pCtx = m_avctxSubtitle;
  644. break;
  645. default:
  646. break;
  647. }
  648. return pCtx;
  649. }
  650. enum AVHWDeviceType VideoStateData::get_hwdevice(const char* device) const
  651. {
  652. // device = <vaapi|vdpau|dxva2|d3d11va>
  653. enum AVHWDeviceType type = av_hwdevice_find_type_by_name(device);
  654. if (type == AV_HWDEVICE_TYPE_NONE) {
  655. av_log(nullptr, AV_LOG_WARNING, "Device type %s is not supported.\n", device);
  656. av_log(nullptr, AV_LOG_INFO, "Available device types:");
  657. while ((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
  658. av_log(nullptr, AV_LOG_INFO, " %s", av_hwdevice_get_type_name(type));
  659. av_log(nullptr, AV_LOG_INFO, "\n");
  660. return AV_HWDEVICE_TYPE_NONE;
  661. }
  662. return type;
  663. }
  664. enum AVPixelFormat VideoStateData::get_hwdevice_decoder(const AVCodec* decoder,
  665. enum AVHWDeviceType type) const
  666. {
  667. if (!decoder || AV_HWDEVICE_TYPE_NONE == type)
  668. return AV_PIX_FMT_NONE;
  669. for (int i = 0;; i++) {
  670. const AVCodecHWConfig* config = avcodec_get_hw_config(decoder, i);
  671. if (!config) {
  672. av_log(nullptr,
  673. AV_LOG_WARNING,
  674. "Decoder %s does not support device type %s.\n",
  675. decoder->name,
  676. av_hwdevice_get_type_name(type));
  677. return AV_PIX_FMT_NONE;
  678. }
  679. if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
  680. && config->device_type == type) {
  681. return config->pix_fmt;
  682. }
  683. }
  684. return AV_PIX_FMT_NONE;
  685. }