video_state.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. #if USE_AVFILTER_AUDIO
  231. is->audio_speed = 1.0;
  232. #endif
  233. return is;
  234. fail:
  235. stream_close(is);
  236. return nullptr;
  237. }
  238. void VideoStateData::threads_setting(VideoState* is, const Threads& threads)
  239. {
  240. if (!is)
  241. return;
  242. assert(!is->threads.read_tid);
  243. assert(!is->threads.video_decode_tid);
  244. assert(!is->threads.audio_decode_tid);
  245. assert(!is->threads.video_play_tid);
  246. assert(!is->threads.audio_play_tid);
  247. assert(!is->threads.subtitle_decode_tid);
  248. is->threads = threads;
  249. }
  250. void VideoStateData::read_thread_exit_wait(VideoState* is)
  251. {
  252. if (!is)
  253. return;
  254. if (is->read_thread_exit != 0)
  255. return;
  256. if (is->threads.read_tid) {
  257. av_log(nullptr, AV_LOG_INFO, "read thread wait before!\n");
  258. is->threads.read_tid->wait();
  259. av_log(nullptr, AV_LOG_INFO, "read thread wait after!\n");
  260. is->threads.read_tid = nullptr;
  261. }
  262. }
  263. void VideoStateData::threads_exit_wait(VideoState* is)
  264. {
  265. if (!is)
  266. return;
  267. if (is->threads.video_play_tid) {
  268. is->threads.video_play_tid->wait();
  269. is->threads.video_play_tid = nullptr;
  270. }
  271. if (is->threads.audio_play_tid) {
  272. is->threads.audio_play_tid->wait();
  273. is->threads.audio_play_tid = nullptr;
  274. }
  275. if (is->threads.video_decode_tid) {
  276. is->threads.video_decode_tid->wait();
  277. is->threads.video_decode_tid = nullptr;
  278. }
  279. if (is->threads.audio_decode_tid) {
  280. is->threads.audio_decode_tid->wait();
  281. is->threads.audio_decode_tid = nullptr;
  282. }
  283. if (is->threads.subtitle_decode_tid) {
  284. is->threads.subtitle_decode_tid->wait();
  285. is->threads.subtitle_decode_tid = nullptr;
  286. }
  287. }
  288. void VideoStateData::stream_close(VideoState* is)
  289. {
  290. assert(is);
  291. is->abort_request = 1;
  292. read_thread_exit_wait(is);
  293. // if (is->read_thread_exit == 0)
  294. //{
  295. // // SDL_WaitThread(is->read_tid, nullptr);
  296. // //((QThread*)(is->read_tid))->wait();
  297. // /*if (m_pReadThreadId)
  298. // m_pReadThreadId->wait();*/
  299. //}
  300. /* close each stream */
  301. if (is->audio_stream >= 0)
  302. stream_component_close(is, is->audio_stream);
  303. if (is->video_stream >= 0)
  304. stream_component_close(is, is->video_stream);
  305. if (is->subtitle_stream >= 0)
  306. stream_component_close(is, is->subtitle_stream);
  307. threads_exit_wait(is); // read and decode threads exit here.
  308. avformat_close_input(&is->ic);
  309. packet_queue_destroy(&is->videoq);
  310. packet_queue_destroy(&is->audioq);
  311. packet_queue_destroy(&is->subtitleq);
  312. /* free all pictures */
  313. frame_queue_destory(&is->pictq);
  314. frame_queue_destory(&is->sampq);
  315. frame_queue_destory(&is->subpq);
  316. if (is->continue_read_thread) {
  317. delete is->continue_read_thread;
  318. is->continue_read_thread = nullptr;
  319. }
  320. // SDL_DestroyCond(is->continue_read_thread);
  321. sws_freeContext(is->img_convert_ctx);
  322. sws_freeContext(is->sub_convert_ctx);
  323. av_free(is->filename);
  324. /*if (is->vis_texture)
  325. SDL_DestroyTexture(is->vis_texture);
  326. if (is->vid_texture)
  327. SDL_DestroyTexture(is->vid_texture);
  328. if (is->sub_texture)
  329. SDL_DestroyTexture(is->sub_texture);*/
  330. av_free(is);
  331. }
  332. static enum AVPixelFormat get_hw_format(AVCodecContext* ctx, const enum AVPixelFormat* pix_fmts)
  333. {
  334. for (const enum AVPixelFormat* p = pix_fmts; *p != -1; p++) {
  335. if (*p == hw_pix_fmt)
  336. return *p;
  337. }
  338. fprintf(stderr, "Failed to get HW surface format, codec_id=%d\n", (int) ctx->codec_id);
  339. return AV_PIX_FMT_NONE;
  340. }
  341. // static int hw_decoder_init(AVCodecContext* ctx, const enum AVHWDeviceType
  342. // type)
  343. //{
  344. // int err = 0;
  345. //
  346. // if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type, nullptr,
  347. //nullptr, 0)) < 0) { fprintf(stderr, "Failed to create specified HW
  348. //device.\n"); return err;
  349. // }
  350. //
  351. // ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
  352. //
  353. // return err;
  354. //}
  355. int VideoStateData::hw_decoder_init(AVCodecContext* ctx, const enum AVHWDeviceType type)
  356. {
  357. int err = 0;
  358. if ((err = av_hwdevice_ctx_create(&m_hw_device_ctx, type, nullptr, nullptr, 0)) < 0) {
  359. fprintf(stderr, "Failed to create specified HW device.\n");
  360. return err;
  361. }
  362. ctx->hw_device_ctx = av_buffer_ref(m_hw_device_ctx);
  363. return err;
  364. }
  365. bool VideoStateData::open_hardware(AVCodecContext* avctx, const AVCodec* codec, const char* device)
  366. {
  367. enum AVHWDeviceType type = get_hwdevice(device);
  368. hw_pix_fmt = get_hwdevice_decoder(codec, type);
  369. avctx->get_format = get_hw_format;
  370. if (hw_decoder_init(avctx, type) < 0)
  371. return false;
  372. return true;
  373. }
  374. void VideoStateData::close_hardware()
  375. {
  376. av_buffer_unref(&m_hw_device_ctx);
  377. }
  378. int VideoStateData::stream_component_open(VideoState* is, int stream_index)
  379. {
  380. assert(is);
  381. AVFormatContext* ic = is->ic;
  382. AVCodecContext* avctx;
  383. const AVCodec* codec;
  384. AVDictionary* opts = nullptr;
  385. // const AVDictionaryEntry* t = nullptr;
  386. int sample_rate, nb_channels;
  387. AVChannelLayout ch_layout = {0};
  388. // int64_t
  389. int format;
  390. int ret = 0;
  391. int stream_lowres = 0;
  392. if (stream_index < 0 || ((unsigned int) stream_index) >= ic->nb_streams)
  393. return -1;
  394. avctx = avcodec_alloc_context3(nullptr);
  395. if (!avctx)
  396. return AVERROR(ENOMEM);
  397. ret = avcodec_parameters_to_context(avctx, ic->streams[stream_index]->codecpar);
  398. if (ret < 0)
  399. goto fail;
  400. avctx->pkt_timebase = ic->streams[stream_index]->time_base;
  401. codec = avcodec_find_decoder(avctx->codec_id);
  402. switch (avctx->codec_type) {
  403. case AVMEDIA_TYPE_AUDIO:
  404. is->last_audio_stream = stream_index;
  405. break;
  406. case AVMEDIA_TYPE_SUBTITLE:
  407. is->last_subtitle_stream = stream_index;
  408. break;
  409. case AVMEDIA_TYPE_VIDEO:
  410. is->last_video_stream = stream_index;
  411. if (m_bUseHardware) {
  412. m_bHardwareSuccess = false;
  413. const char* hardware_device = "dxva2"; // device = <vaapi|vdpau|dxva2|d3d11va>
  414. ret = open_hardware(avctx, codec, hardware_device);
  415. if (!ret) {
  416. qWarning("hardware-accelerated opened failed, device:%s", hardware_device);
  417. goto fail;
  418. }
  419. qInfo("hardware-accelerated opened, device:%s", hardware_device);
  420. m_bHardwareSuccess = true;
  421. }
  422. break;
  423. }
  424. if (!codec) {
  425. av_log(nullptr,
  426. AV_LOG_WARNING,
  427. "No decoder could be found for codec %s\n",
  428. avcodec_get_name(avctx->codec_id));
  429. ret = AVERROR(EINVAL);
  430. goto fail;
  431. }
  432. avctx->codec_id = codec->id;
  433. if (stream_lowres > codec->max_lowres) {
  434. av_log(avctx,
  435. AV_LOG_WARNING,
  436. "The maximum value for lowres supported by the decoder is %d\n",
  437. codec->max_lowres);
  438. stream_lowres = codec->max_lowres;
  439. }
  440. avctx->lowres = stream_lowres;
  441. // avctx->flags2 |= AV_CODEC_FLAG2_FAST;
  442. /*opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], codec);
  443. if (!av_dict_get(opts, "threads", NULL, 0))
  444. av_dict_set(&opts, "threads", "auto", 0);
  445. if (stream_lowres)
  446. av_dict_set_int(&opts, "lowres", stream_lowres, 0);*/
  447. if ((ret = avcodec_open2(avctx, codec, &opts)) < 0) {
  448. goto fail;
  449. }
  450. is->eof = 0;
  451. ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
  452. switch (avctx->codec_type) {
  453. case AVMEDIA_TYPE_AUDIO:
  454. #if USE_AVFILTER_AUDIO
  455. {
  456. AVFilterContext* sink;
  457. // const char* afilters =
  458. // "aresample=8000,aformat=sample_fmts=s16:channel_layouts=mono"; //
  459. // "atempo=2"; const char* afilters = nullptr; const char* afilters =
  460. // "atempo=2.0";
  461. is->audio_filter_src.freq = avctx->sample_rate;
  462. is->audio_filter_src.ch_layout.nb_channels = avctx->ch_layout.nb_channels; // avctx->channels;
  463. is->audio_filter_src.ch_layout = avctx->ch_layout; // avctx->channel_layout
  464. is->audio_filter_src.fmt = avctx->sample_fmt;
  465. if ((ret = configure_audio_filters(is, is->afilters, 0)) < 0)
  466. goto fail;
  467. sink = is->out_audio_filter;
  468. sample_rate = av_buffersink_get_sample_rate(sink);
  469. nb_channels = av_buffersink_get_channels(sink);
  470. // channel_layout = av_buffersink_get_channel_layout(sink);
  471. format = av_buffersink_get_format(sink);
  472. AVChannelLayout chn_layout;
  473. av_buffersink_get_ch_layout(sink, &chn_layout);
  474. qDebug("afilter sink: sample rate:%d, chn:%d, fmt:%d, chn_layout:%d",
  475. sample_rate,
  476. nb_channels,
  477. format,
  478. chn_layout.u);
  479. }
  480. #else
  481. sample_rate = avctx->sample_rate;
  482. ret = av_channel_layout_copy(&ch_layout, &avctx->ch_layout);
  483. if (ret < 0)
  484. goto fail;
  485. #endif
  486. /* prepare audio output */
  487. /*if ((ret = audio_open(is, chn_layout, nb_channels, sample_rate,
  488. &is->audio_tgt)) < 0) goto fail;
  489. is->audio_src = is->audio_tgt;*/
  490. is->audio_stream = stream_index;
  491. is->audio_st = ic->streams[stream_index];
  492. if ((is->ic->iformat->flags
  493. & (AVFMT_NOBINSEARCH | AVFMT_NOGENSEARCH | AVFMT_NO_BYTE_SEEK))) {
  494. is->auddec.start_pts = is->audio_st->start_time;
  495. is->auddec.start_pts_tb = is->audio_st->time_base;
  496. }
  497. m_bHasAudio = true;
  498. m_avctxAudio = avctx;
  499. break;
  500. case AVMEDIA_TYPE_VIDEO:
  501. is->video_stream = stream_index;
  502. is->video_st = ic->streams[stream_index];
  503. m_bHasVideo = true;
  504. m_avctxVideo = avctx;
  505. break;
  506. case AVMEDIA_TYPE_SUBTITLE:
  507. is->subtitle_stream = stream_index;
  508. is->subtitle_st = ic->streams[stream_index];
  509. m_bHasSubtitle = true;
  510. m_avctxSubtitle = avctx;
  511. break;
  512. default:
  513. break;
  514. }
  515. goto out;
  516. fail:
  517. avcodec_free_context(&avctx);
  518. out:
  519. av_dict_free(&opts);
  520. return ret;
  521. }
  522. void VideoStateData::stream_component_close(VideoState* is, int stream_index)
  523. {
  524. assert(is);
  525. AVFormatContext* ic = is->ic;
  526. AVCodecParameters* codecpar;
  527. if (stream_index < 0 || ((unsigned int) stream_index) >= ic->nb_streams)
  528. return;
  529. codecpar = ic->streams[stream_index]->codecpar;
  530. switch (codecpar->codec_type) {
  531. case AVMEDIA_TYPE_AUDIO:
  532. decoder_abort(&is->auddec, &is->sampq);
  533. // SDL_CloseAudioDevice(audio_dev);
  534. decoder_destroy(&is->auddec);
  535. // swr_free(&is->swr_ctx);
  536. // av_freep(&is->audio_buf1);
  537. // is->audio_buf1_size = 0;
  538. // is->audio_buf = nullptr;
  539. /*if (is->rdft) {
  540. av_rdft_end(is->rdft);
  541. av_freep(&is->rdft_data);
  542. is->rdft = nullptr;
  543. is->rdft_bits = 0;
  544. }*/
  545. break;
  546. case AVMEDIA_TYPE_VIDEO:
  547. decoder_abort(&is->viddec, &is->pictq);
  548. decoder_destroy(&is->viddec);
  549. break;
  550. case AVMEDIA_TYPE_SUBTITLE:
  551. decoder_abort(&is->subdec, &is->subpq);
  552. decoder_destroy(&is->subdec);
  553. break;
  554. default:
  555. qDebug("Not handled yet.......code type:%d", codecpar->codec_type);
  556. break;
  557. }
  558. ic->streams[stream_index]->discard = AVDISCARD_ALL;
  559. switch (codecpar->codec_type) {
  560. case AVMEDIA_TYPE_AUDIO:
  561. is->audio_st = nullptr;
  562. is->audio_stream = -1;
  563. break;
  564. case AVMEDIA_TYPE_VIDEO:
  565. is->video_st = nullptr;
  566. is->video_stream = -1;
  567. break;
  568. case AVMEDIA_TYPE_SUBTITLE:
  569. is->subtitle_st = nullptr;
  570. is->subtitle_stream = -1;
  571. break;
  572. default:
  573. break;
  574. }
  575. }
  576. bool VideoStateData::has_video() const
  577. {
  578. return m_bHasVideo;
  579. }
  580. bool VideoStateData::has_audio() const
  581. {
  582. return m_bHasAudio;
  583. }
  584. bool VideoStateData::has_subtitle() const
  585. {
  586. return m_bHasSubtitle;
  587. }
  588. AVCodecContext* VideoStateData::get_contex(AVMediaType type) const
  589. {
  590. AVCodecContext* pCtx = nullptr;
  591. switch (type) {
  592. case AVMEDIA_TYPE_AUDIO:
  593. pCtx = m_avctxAudio;
  594. break;
  595. case AVMEDIA_TYPE_VIDEO:
  596. pCtx = m_avctxVideo;
  597. break;
  598. case AVMEDIA_TYPE_SUBTITLE:
  599. pCtx = m_avctxSubtitle;
  600. break;
  601. default:
  602. break;
  603. }
  604. return pCtx;
  605. }
  606. enum AVHWDeviceType VideoStateData::get_hwdevice(const char* device) const
  607. {
  608. // device = <vaapi|vdpau|dxva2|d3d11va>
  609. enum AVHWDeviceType type = av_hwdevice_find_type_by_name(device);
  610. if (type == AV_HWDEVICE_TYPE_NONE) {
  611. av_log(nullptr, AV_LOG_WARNING, "Device type %s is not supported.\n", device);
  612. av_log(nullptr, AV_LOG_INFO, "Available device types:");
  613. while ((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
  614. av_log(nullptr, AV_LOG_INFO, " %s", av_hwdevice_get_type_name(type));
  615. av_log(nullptr, AV_LOG_INFO, "\n");
  616. return AV_HWDEVICE_TYPE_NONE;
  617. }
  618. return type;
  619. }
  620. enum AVPixelFormat VideoStateData::get_hwdevice_decoder(const AVCodec* decoder,
  621. enum AVHWDeviceType type) const
  622. {
  623. if (!decoder || AV_HWDEVICE_TYPE_NONE == type)
  624. return AV_PIX_FMT_NONE;
  625. for (int i = 0;; i++) {
  626. const AVCodecHWConfig* config = avcodec_get_hw_config(decoder, i);
  627. if (!config) {
  628. av_log(nullptr,
  629. AV_LOG_WARNING,
  630. "Decoder %s does not support device type %s.\n",
  631. decoder->name,
  632. av_hwdevice_get_type_name(type));
  633. return AV_PIX_FMT_NONE;
  634. }
  635. if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX
  636. && config->device_type == type) {
  637. return config->pix_fmt;
  638. }
  639. }
  640. return AV_PIX_FMT_NONE;
  641. }