video_state.cpp 23 KB

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