video_state.cpp 23 KB

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