CVideoHandler.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. * CVideoHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CVideoHandler.h"
  12. #ifndef DISABLE_VIDEO
  13. #include "ISoundPlayer.h"
  14. #include "../CGameInfo.h"
  15. #include "../CMT.h"
  16. #include "../eventsSDL/InputHandler.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../render/Canvas.h"
  19. #include "../render/IScreenHandler.h"
  20. #include "../renderSDL/SDL_Extensions.h"
  21. #include "../../lib/filesystem/CInputStream.h"
  22. #include "../../lib/filesystem/Filesystem.h"
  23. #include "../../lib/texts/CGeneralTextHandler.h"
  24. #include "../../lib/texts/Languages.h"
  25. #include <SDL_render.h>
  26. extern "C" {
  27. #include <libavformat/avformat.h>
  28. #include <libavcodec/avcodec.h>
  29. #include <libavutil/imgutils.h>
  30. #include <libavutil/opt.h>
  31. #include <libswscale/swscale.h>
  32. #include <libswresample/swresample.h>
  33. }
  34. // Define a set of functions to read data
  35. static int lodRead(void * opaque, uint8_t * buf, int size)
  36. {
  37. auto * data = static_cast<CInputStream *>(opaque);
  38. auto bytesRead = data->read(buf, size);
  39. if(bytesRead == 0)
  40. return AVERROR_EOF;
  41. return bytesRead;
  42. }
  43. static si64 lodSeek(void * opaque, si64 pos, int whence)
  44. {
  45. auto * data = static_cast<CInputStream *>(opaque);
  46. if(whence & AVSEEK_SIZE)
  47. return data->getSize();
  48. return data->seek(pos);
  49. }
  50. [[noreturn]] static void throwFFmpegError(int errorCode)
  51. {
  52. std::array<char, AV_ERROR_MAX_STRING_SIZE> errorMessage{};
  53. av_strerror(errorCode, errorMessage.data(), errorMessage.size());
  54. throw std::runtime_error(errorMessage.data());
  55. }
  56. static std::unique_ptr<CInputStream> findVideoData(const VideoPath & videoToOpen)
  57. {
  58. if(CResourceHandler::get()->existsResource(videoToOpen))
  59. return CResourceHandler::get()->load(videoToOpen);
  60. auto highQualityVideoToOpenWithDir = videoToOpen.addPrefix("VIDEO/");
  61. auto lowQualityVideo = videoToOpen.toType<EResType::VIDEO_LOW_QUALITY>();
  62. auto lowQualityVideoWithDir = highQualityVideoToOpenWithDir.toType<EResType::VIDEO_LOW_QUALITY>();
  63. if(CResourceHandler::get()->existsResource(highQualityVideoToOpenWithDir))
  64. return CResourceHandler::get()->load(highQualityVideoToOpenWithDir);
  65. if(CResourceHandler::get()->existsResource(lowQualityVideo))
  66. return CResourceHandler::get()->load(lowQualityVideo);
  67. if(CResourceHandler::get()->existsResource(lowQualityVideoWithDir))
  68. return CResourceHandler::get()->load(lowQualityVideoWithDir);
  69. return nullptr;
  70. }
  71. bool FFMpegStream::openInput(const VideoPath & videoToOpen)
  72. {
  73. input = findVideoData(videoToOpen);
  74. return input != nullptr;
  75. }
  76. void FFMpegStream::openContext()
  77. {
  78. static const int BUFFER_SIZE = 4096;
  79. input->seek(0);
  80. auto * buffer = static_cast<unsigned char *>(av_malloc(BUFFER_SIZE)); // will be freed by ffmpeg
  81. context = avio_alloc_context(buffer, BUFFER_SIZE, 0, input.get(), lodRead, nullptr, lodSeek);
  82. formatContext = avformat_alloc_context();
  83. formatContext->pb = context;
  84. // filename is not needed - file was already open and stored in this->data;
  85. int avfopen = avformat_open_input(&formatContext, "dummyFilename", nullptr, nullptr);
  86. if(avfopen != 0)
  87. throwFFmpegError(avfopen);
  88. // Retrieve stream information
  89. int findStreamInfo = avformat_find_stream_info(formatContext, nullptr);
  90. if(avfopen < 0)
  91. throwFFmpegError(findStreamInfo);
  92. }
  93. void FFMpegStream::openCodec(int desiredStreamIndex)
  94. {
  95. streamIndex = desiredStreamIndex;
  96. // Find the decoder for the stream
  97. codec = avcodec_find_decoder(formatContext->streams[streamIndex]->codecpar->codec_id);
  98. if(codec == nullptr)
  99. throw std::runtime_error("Unsupported codec");
  100. codecContext = avcodec_alloc_context3(codec);
  101. if(codecContext == nullptr)
  102. throw std::runtime_error("Failed to create codec context");
  103. // Get a pointer to the codec context for the video stream
  104. int ret = avcodec_parameters_to_context(codecContext, formatContext->streams[streamIndex]->codecpar);
  105. if(ret < 0)
  106. {
  107. //We cannot get codec from parameters
  108. avcodec_free_context(&codecContext);
  109. throwFFmpegError(ret);
  110. }
  111. // Open codec
  112. ret = avcodec_open2(codecContext, codec, nullptr);
  113. if(ret < 0)
  114. {
  115. // Could not open codec
  116. codec = nullptr;
  117. throwFFmpegError(ret);
  118. }
  119. // Allocate video frame
  120. frame = av_frame_alloc();
  121. }
  122. const AVCodecParameters * FFMpegStream::getCodecParameters() const
  123. {
  124. return formatContext->streams[streamIndex]->codecpar;
  125. }
  126. const AVCodecContext * FFMpegStream::getCodecContext() const
  127. {
  128. return codecContext;
  129. }
  130. const AVFrame * FFMpegStream::getCurrentFrame() const
  131. {
  132. return frame;
  133. }
  134. void CVideoInstance::openVideo()
  135. {
  136. openContext();
  137. openCodec(findVideoStream());
  138. }
  139. void CVideoInstance::prepareOutput(float scaleFactor, bool useTextureOutput)
  140. {
  141. //setup scaling
  142. dimensions = Point(getCodecContext()->width * scaleFactor, getCodecContext()->height * scaleFactor) * GH.screenHandler().getScalingFactor();
  143. // Allocate a place to put our YUV image on that screen
  144. if (useTextureOutput)
  145. {
  146. std::array potentialFormats = {
  147. AV_PIX_FMT_YUV420P, // -> SDL_PIXELFORMAT_IYUV - most of H3 videos use YUV format, so it is preferred to save some space & conversion time
  148. AV_PIX_FMT_RGB32, // -> SDL_PIXELFORMAT_ARGB8888 - some .smk videos actually use palette, so RGB > YUV. This is also our screen texture format
  149. AV_PIX_FMT_NONE
  150. };
  151. auto preferredFormat = avcodec_find_best_pix_fmt_of_list(potentialFormats.data(), getCodecContext()->pix_fmt, false, nullptr);
  152. if (preferredFormat == AV_PIX_FMT_YUV420P)
  153. textureYUV = SDL_CreateTexture( mainRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, dimensions.x, dimensions.y);
  154. else
  155. textureRGB = SDL_CreateTexture( mainRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, dimensions.x, dimensions.y);
  156. sws = sws_getContext(getCodecContext()->width, getCodecContext()->height, getCodecContext()->pix_fmt,
  157. dimensions.x, dimensions.y, preferredFormat,
  158. SWS_BICUBIC, nullptr, nullptr, nullptr);
  159. }
  160. else
  161. {
  162. surface = CSDL_Ext::newSurface(dimensions);
  163. sws = sws_getContext(getCodecContext()->width, getCodecContext()->height, getCodecContext()->pix_fmt,
  164. dimensions.x, dimensions.y, AV_PIX_FMT_RGB32,
  165. SWS_BICUBIC, nullptr, nullptr, nullptr);
  166. }
  167. if (sws == nullptr)
  168. throw std::runtime_error("Failed to create sws");
  169. }
  170. void FFMpegStream::decodeNextFrame()
  171. {
  172. int rc = avcodec_receive_frame(codecContext, frame);
  173. // frame extracted - data that was sent to codecContext before was sufficient
  174. if (rc == 0)
  175. return;
  176. // returning AVERROR(EAGAIN) is legal - this indicates that codec requires more data from input stream to decode next frame
  177. if(rc != AVERROR(EAGAIN))
  178. throwFFmpegError(rc);
  179. for(;;)
  180. {
  181. AVPacket packet;
  182. // codecContext does not have enough input data - read next packet from input stream
  183. int ret = av_read_frame(formatContext, &packet);
  184. if(ret < 0)
  185. {
  186. if(ret == AVERROR_EOF)
  187. {
  188. av_packet_unref(&packet);
  189. av_frame_free(&frame);
  190. frame = nullptr;
  191. return;
  192. }
  193. throwFFmpegError(ret);
  194. }
  195. // Is this a packet from the stream that needs decoding?
  196. if(packet.stream_index == streamIndex)
  197. {
  198. // Decode read packet
  199. // Note: this method may return AVERROR(EAGAIN). However this should never happen with ffmpeg API
  200. // since there is guaranteed call to avcodec_receive_frame and ffmpeg API promises that *both* of these methods will never return AVERROR(EAGAIN).
  201. int rc = avcodec_send_packet(codecContext, &packet);
  202. if(rc < 0)
  203. throwFFmpegError(rc);
  204. rc = avcodec_receive_frame(codecContext, frame);
  205. if(rc == AVERROR(EAGAIN))
  206. {
  207. // still need more data - read next packet
  208. av_packet_unref(&packet);
  209. continue;
  210. }
  211. else if(rc < 0)
  212. {
  213. throwFFmpegError(rc);
  214. }
  215. else
  216. {
  217. // read succesful. Exit the loop
  218. av_packet_unref(&packet);
  219. return;
  220. }
  221. }
  222. av_packet_unref(&packet);
  223. }
  224. }
  225. bool CVideoInstance::loadNextFrame()
  226. {
  227. decodeNextFrame();
  228. const AVFrame * frame = getCurrentFrame();
  229. if(!frame)
  230. return false;
  231. uint8_t * data[4] = {};
  232. int linesize[4] = {};
  233. if(textureYUV)
  234. {
  235. av_image_alloc(data, linesize, dimensions.x, dimensions.y, AV_PIX_FMT_YUV420P, 1);
  236. sws_scale(sws, frame->data, frame->linesize, 0, getCodecContext()->height, data, linesize);
  237. SDL_UpdateYUVTexture(textureYUV, nullptr, data[0], linesize[0], data[1], linesize[1], data[2], linesize[2]);
  238. av_freep(&data[0]);
  239. }
  240. if(textureRGB)
  241. {
  242. av_image_alloc(data, linesize, dimensions.x, dimensions.y, AV_PIX_FMT_RGB32, 1);
  243. sws_scale(sws, frame->data, frame->linesize, 0, getCodecContext()->height, data, linesize);
  244. SDL_UpdateTexture(textureRGB, nullptr, data[0], linesize[0]);
  245. av_freep(&data[0]);
  246. }
  247. if(surface)
  248. {
  249. // Avoid buffer overflow caused by sws_scale():
  250. // http://trac.ffmpeg.org/ticket/9254
  251. size_t pic_bytes = surface->pitch * surface->h;
  252. size_t ffmped_pad = 1024; /* a few bytes of overflow will go here */
  253. void * for_sws = av_malloc(pic_bytes + ffmped_pad);
  254. data[0] = (ui8 *)for_sws;
  255. linesize[0] = surface->pitch;
  256. sws_scale(sws, frame->data, frame->linesize, 0, getCodecContext()->height, data, linesize);
  257. memcpy(surface->pixels, for_sws, pic_bytes);
  258. av_free(for_sws);
  259. }
  260. return true;
  261. }
  262. double CVideoInstance::timeStamp()
  263. {
  264. return getCurrentFrameEndTime();
  265. }
  266. bool CVideoInstance::videoEnded()
  267. {
  268. return getCurrentFrame() == nullptr;
  269. }
  270. CVideoInstance::~CVideoInstance()
  271. {
  272. sws_freeContext(sws);
  273. SDL_DestroyTexture(textureYUV);
  274. SDL_DestroyTexture(textureRGB);
  275. SDL_FreeSurface(surface);
  276. }
  277. FFMpegStream::~FFMpegStream()
  278. {
  279. av_frame_free(&frame);
  280. #if (LIBAVCODEC_VERSION_MAJOR < 61 )
  281. // deprecated, apparently no longer necessary - avcodec_free_context should suffice
  282. avcodec_close(codecContext);
  283. #endif
  284. avcodec_free_context(&codecContext);
  285. avformat_close_input(&formatContext);
  286. av_free(context);
  287. }
  288. Point CVideoInstance::size()
  289. {
  290. return dimensions / GH.screenHandler().getScalingFactor();
  291. }
  292. void CVideoInstance::show(const Point & position, Canvas & canvas)
  293. {
  294. if(sws == nullptr)
  295. throw std::runtime_error("No video to show!");
  296. CSDL_Ext::blitSurface(surface, canvas.getInternalSurface(), position * GH.screenHandler().getScalingFactor());
  297. }
  298. double FFMpegStream::getCurrentFrameEndTime() const
  299. {
  300. #if(LIBAVUTIL_VERSION_MAJOR < 58)
  301. auto packet_duration = frame->pkt_duration;
  302. #else
  303. auto packet_duration = frame->duration;
  304. #endif
  305. return (frame->pts + packet_duration) * av_q2d(formatContext->streams[streamIndex]->time_base);
  306. }
  307. double FFMpegStream::getCurrentFrameDuration() const
  308. {
  309. #if(LIBAVUTIL_VERSION_MAJOR < 58)
  310. auto packet_duration = frame->pkt_duration;
  311. #else
  312. auto packet_duration = frame->duration;
  313. #endif
  314. return packet_duration * av_q2d(formatContext->streams[streamIndex]->time_base);
  315. }
  316. void CVideoInstance::tick(uint32_t msPassed)
  317. {
  318. if(sws == nullptr)
  319. throw std::runtime_error("No video to show!");
  320. if(videoEnded())
  321. throw std::runtime_error("Video already ended!");
  322. frameTime += msPassed / 1000.0;
  323. if(frameTime >= getCurrentFrameEndTime())
  324. loadNextFrame();
  325. }
  326. struct FFMpegFormatDescription
  327. {
  328. uint8_t sampleSizeBytes;
  329. uint8_t wavFormatID;
  330. bool isPlanar;
  331. };
  332. static FFMpegFormatDescription getAudioFormatProperties(int audioFormat)
  333. {
  334. switch (audioFormat)
  335. {
  336. case AV_SAMPLE_FMT_U8: return { 1, 1, false};
  337. case AV_SAMPLE_FMT_U8P: return { 1, 1, true};
  338. case AV_SAMPLE_FMT_S16: return { 2, 1, false};
  339. case AV_SAMPLE_FMT_S16P: return { 2, 1, true};
  340. case AV_SAMPLE_FMT_S32: return { 4, 1, false};
  341. case AV_SAMPLE_FMT_S32P: return { 4, 1, true};
  342. case AV_SAMPLE_FMT_S64: return { 8, 1, false};
  343. case AV_SAMPLE_FMT_S64P: return { 8, 1, true};
  344. case AV_SAMPLE_FMT_FLT: return { 4, 3, false};
  345. case AV_SAMPLE_FMT_FLTP: return { 4, 3, true};
  346. case AV_SAMPLE_FMT_DBL: return { 8, 3, false};
  347. case AV_SAMPLE_FMT_DBLP: return { 8, 3, true};
  348. }
  349. throw std::runtime_error("Invalid audio format");
  350. }
  351. int FFMpegStream::findAudioStream() const
  352. {
  353. std::vector<int> audioStreamIndices;
  354. for(int i = 0; i < formatContext->nb_streams; i++)
  355. if(formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
  356. audioStreamIndices.push_back(i);
  357. if (audioStreamIndices.empty())
  358. return -1;
  359. if (audioStreamIndices.size() == 1)
  360. return audioStreamIndices.front();
  361. // multiple audio streams - try to pick best one based on language settings
  362. std::map<int, std::string> streamToLanguage;
  363. // Approach 1 - check if stream has language set in metadata
  364. for (auto const & index : audioStreamIndices)
  365. {
  366. const AVDictionaryEntry *e = av_dict_get(formatContext->streams[index]->metadata, "language", nullptr, 0);
  367. if (e)
  368. streamToLanguage[index] = e->value;
  369. }
  370. // Approach 2 - no metadata found. This may be video from Chronicles which have predefined (presumably hardcoded) list of languages
  371. if (streamToLanguage.empty())
  372. {
  373. if (audioStreamIndices.size() == 2)
  374. {
  375. streamToLanguage[audioStreamIndices[0]] = Languages::getLanguageOptions(Languages::ELanguages::ENGLISH).tagISO2;
  376. streamToLanguage[audioStreamIndices[1]] = Languages::getLanguageOptions(Languages::ELanguages::GERMAN).tagISO2;
  377. }
  378. if (audioStreamIndices.size() == 5)
  379. {
  380. streamToLanguage[audioStreamIndices[0]] = Languages::getLanguageOptions(Languages::ELanguages::ENGLISH).tagISO2;
  381. streamToLanguage[audioStreamIndices[1]] = Languages::getLanguageOptions(Languages::ELanguages::FRENCH).tagISO2;
  382. streamToLanguage[audioStreamIndices[2]] = Languages::getLanguageOptions(Languages::ELanguages::GERMAN).tagISO2;
  383. streamToLanguage[audioStreamIndices[3]] = Languages::getLanguageOptions(Languages::ELanguages::ITALIAN).tagISO2;
  384. streamToLanguage[audioStreamIndices[4]] = Languages::getLanguageOptions(Languages::ELanguages::SPANISH).tagISO2;
  385. }
  386. }
  387. std::string preferredLanguageName = CGI->generaltexth->getPreferredLanguage();
  388. std::string preferredTag = Languages::getLanguageOptions(preferredLanguageName).tagISO2;
  389. for (auto const & entry : streamToLanguage)
  390. if (entry.second == preferredTag)
  391. return entry.first;
  392. return audioStreamIndices.front();
  393. }
  394. int FFMpegStream::findVideoStream() const
  395. {
  396. for(int i = 0; i < formatContext->nb_streams; i++)
  397. if(formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  398. return i;
  399. return -1;
  400. }
  401. std::pair<std::unique_ptr<ui8 []>, si64> CAudioInstance::extractAudio(const VideoPath & videoToOpen)
  402. {
  403. if (!openInput(videoToOpen))
  404. return { nullptr, 0};
  405. openContext();
  406. int audioStreamIndex = findAudioStream();
  407. if (audioStreamIndex == -1)
  408. return { nullptr, 0};
  409. openCodec(audioStreamIndex);
  410. const auto * codecpar = getCodecParameters();
  411. std::vector<ui8> samples;
  412. auto formatProperties = getAudioFormatProperties(codecpar->format);
  413. #if(LIBAVUTIL_VERSION_MAJOR < 58)
  414. int numChannels = codecpar->channels;
  415. #else
  416. int numChannels = codecpar->ch_layout.nb_channels;
  417. #endif
  418. samples.reserve(44100 * 5); // arbitrary 5-second buffer to reduce reallocations
  419. if (formatProperties.isPlanar && numChannels > 1)
  420. {
  421. // Format is 'planar', which is not supported by wav / SDL
  422. // Use swresample part of ffmpeg to deplanarize audio into format supported by wav / SDL
  423. auto sourceFormat = static_cast<AVSampleFormat>(codecpar->format);
  424. auto targetFormat = av_get_alt_sample_fmt(sourceFormat, false);
  425. SwrContext * swr_ctx = swr_alloc();
  426. #if (LIBAVUTIL_VERSION_MAJOR < 58)
  427. av_opt_set_channel_layout(swr_ctx, "in_chlayout", codecpar->channel_layout, 0);
  428. av_opt_set_channel_layout(swr_ctx, "out_chlayout", codecpar->channel_layout, 0);
  429. #else
  430. av_opt_set_chlayout(swr_ctx, "in_chlayout", &codecpar->ch_layout, 0);
  431. av_opt_set_chlayout(swr_ctx, "out_chlayout", &codecpar->ch_layout, 0);
  432. #endif
  433. av_opt_set_int(swr_ctx, "in_sample_rate", codecpar->sample_rate, 0);
  434. av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", sourceFormat, 0);
  435. av_opt_set_int(swr_ctx, "out_sample_rate", codecpar->sample_rate, 0);
  436. av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", targetFormat, 0);
  437. int initResult = swr_init(swr_ctx);
  438. if (initResult < 0)
  439. throwFFmpegError(initResult);
  440. std::vector<uint8_t> frameSamplesBuffer;
  441. for (;;)
  442. {
  443. decodeNextFrame();
  444. const AVFrame * frame = getCurrentFrame();
  445. if (!frame)
  446. break;
  447. size_t samplesToRead = frame->nb_samples * numChannels;
  448. size_t bytesToRead = samplesToRead * formatProperties.sampleSizeBytes;
  449. frameSamplesBuffer.resize(std::max(frameSamplesBuffer.size(), bytesToRead));
  450. uint8_t * frameSamplesPtr = frameSamplesBuffer.data();
  451. int result = swr_convert(swr_ctx, &frameSamplesPtr, frame->nb_samples, const_cast<const uint8_t **>(frame->data), frame->nb_samples);
  452. if (result < 0)
  453. throwFFmpegError(result);
  454. size_t samplesToCopy = result * numChannels;
  455. size_t bytesToCopy = samplesToCopy * formatProperties.sampleSizeBytes;
  456. samples.insert(samples.end(), frameSamplesBuffer.begin(), frameSamplesBuffer.begin() + bytesToCopy);
  457. }
  458. swr_free(&swr_ctx);
  459. }
  460. else
  461. {
  462. for (;;)
  463. {
  464. decodeNextFrame();
  465. const AVFrame * frame = getCurrentFrame();
  466. if (!frame)
  467. break;
  468. size_t samplesToRead = frame->nb_samples * numChannels;
  469. size_t bytesToRead = samplesToRead * formatProperties.sampleSizeBytes;
  470. samples.insert(samples.end(), frame->data[0], frame->data[0] + bytesToRead);
  471. }
  472. }
  473. struct WavHeader {
  474. ui8 RIFF[4] = {'R', 'I', 'F', 'F'};
  475. ui32 ChunkSize;
  476. ui8 WAVE[4] = {'W', 'A', 'V', 'E'};
  477. ui8 fmt[4] = {'f', 'm', 't', ' '};
  478. ui32 Subchunk1Size = 16;
  479. ui16 AudioFormat = 1;
  480. ui16 NumOfChan = 2;
  481. ui32 SamplesPerSec = 22050;
  482. ui32 bytesPerSec = 22050 * 2;
  483. ui16 blockAlign = 1;
  484. ui16 bitsPerSample = 32;
  485. ui8 Subchunk2ID[4] = {'d', 'a', 't', 'a'};
  486. ui32 Subchunk2Size;
  487. };
  488. WavHeader wav;
  489. wav.ChunkSize = samples.size() + sizeof(WavHeader) - 8;
  490. wav.AudioFormat = formatProperties.wavFormatID; // 1 = PCM, 3 = IEEE float
  491. wav.NumOfChan = numChannels;
  492. wav.SamplesPerSec = codecpar->sample_rate;
  493. wav.bytesPerSec = codecpar->sample_rate * formatProperties.sampleSizeBytes;
  494. wav.bitsPerSample = formatProperties.sampleSizeBytes * 8;
  495. wav.Subchunk2Size = samples.size() + sizeof(WavHeader) - 44;
  496. auto * wavPtr = reinterpret_cast<ui8*>(&wav);
  497. auto dat = std::make_pair(std::make_unique<ui8[]>(samples.size() + sizeof(WavHeader)), samples.size() + sizeof(WavHeader));
  498. std::copy(wavPtr, wavPtr + sizeof(WavHeader), dat.first.get());
  499. std::copy(samples.begin(), samples.end(), dat.first.get() + sizeof(WavHeader));
  500. return dat;
  501. }
  502. bool CVideoPlayer::openAndPlayVideoImpl(const VideoPath & name, const Point & position, bool useOverlay, bool stopOnKey)
  503. {
  504. CVideoInstance instance;
  505. auto extractedAudio = getAudio(name);
  506. int audioHandle = CCS->soundh->playSound(extractedAudio);
  507. if (!instance.openInput(name))
  508. return true;
  509. instance.openVideo();
  510. instance.prepareOutput(1, true);
  511. auto lastTimePoint = boost::chrono::steady_clock::now();
  512. while(instance.loadNextFrame())
  513. {
  514. if(stopOnKey)
  515. {
  516. GH.input().fetchEvents();
  517. if(GH.input().ignoreEventsUntilInput())
  518. {
  519. CCS->soundh->stopSound(audioHandle);
  520. return false;
  521. }
  522. }
  523. SDL_Rect rect;
  524. rect.x = position.x;
  525. rect.y = position.y;
  526. rect.w = instance.dimensions.x;
  527. rect.h = instance.dimensions.y;
  528. SDL_RenderFillRect(mainRenderer, &rect);
  529. if(instance.textureYUV)
  530. SDL_RenderCopy(mainRenderer, instance.textureYUV, nullptr, &rect);
  531. else
  532. SDL_RenderCopy(mainRenderer, instance.textureRGB, nullptr, &rect);
  533. SDL_RenderPresent(mainRenderer);
  534. // Framerate delay
  535. double targetFrameTimeSeconds = instance.getCurrentFrameDuration();
  536. auto targetFrameTime = boost::chrono::milliseconds(static_cast<int>(1000 * targetFrameTimeSeconds));
  537. auto timePointAfterPresent = boost::chrono::steady_clock::now();
  538. auto timeSpentBusy = boost::chrono::duration_cast<boost::chrono::milliseconds>(timePointAfterPresent - lastTimePoint);
  539. if(targetFrameTime > timeSpentBusy)
  540. boost::this_thread::sleep_for(targetFrameTime - timeSpentBusy);
  541. lastTimePoint = boost::chrono::steady_clock::now();
  542. }
  543. return true;
  544. }
  545. void CVideoPlayer::playSpellbookAnimation(const VideoPath & name, const Point & position)
  546. {
  547. openAndPlayVideoImpl(name, position * GH.screenHandler().getScalingFactor(), false, false);
  548. }
  549. std::unique_ptr<IVideoInstance> CVideoPlayer::open(const VideoPath & name, float scaleFactor)
  550. {
  551. auto result = std::make_unique<CVideoInstance>();
  552. if (!result->openInput(name))
  553. return nullptr;
  554. result->openVideo();
  555. result->prepareOutput(scaleFactor, false);
  556. result->loadNextFrame(); // prepare 1st frame
  557. return result;
  558. }
  559. std::pair<std::unique_ptr<ui8[]>, si64> CVideoPlayer::getAudio(const VideoPath & videoToOpen)
  560. {
  561. AudioPath audioPath = videoToOpen.toType<EResType::SOUND>();
  562. AudioPath audioPathVideoDir = audioPath.addPrefix("VIDEO/");
  563. if(CResourceHandler::get()->existsResource(audioPath))
  564. return CResourceHandler::get()->load(audioPath)->readAll();
  565. if(CResourceHandler::get()->existsResource(audioPathVideoDir))
  566. return CResourceHandler::get()->load(audioPathVideoDir)->readAll();
  567. CAudioInstance audio;
  568. return audio.extractAudio(videoToOpen);
  569. }
  570. #endif