video_state.cpp 23 KB

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