CVideoHandler.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 "../CPlayerInterface.h"
  17. #include "../eventsSDL/InputHandler.h"
  18. #include "../gui/CGuiHandler.h"
  19. #include "../gui/FramerateManager.h"
  20. #include "../render/Canvas.h"
  21. #include "../renderSDL/SDL_Extensions.h"
  22. #include "../../lib/filesystem/CInputStream.h"
  23. #include "../../lib/filesystem/Filesystem.h"
  24. #include <SDL_render.h>
  25. extern "C" {
  26. #include <libavformat/avformat.h>
  27. #include <libavcodec/avcodec.h>
  28. #include <libavutil/imgutils.h>
  29. #include <libswscale/swscale.h>
  30. }
  31. // Define a set of functions to read data
  32. static int lodRead(void * opaque, uint8_t * buf, int size)
  33. {
  34. auto * data = static_cast<CInputStream *>(opaque);
  35. int bytes = static_cast<int>(data->read(buf, size));
  36. if(bytes == 0)
  37. return AVERROR_EOF;
  38. return bytes;
  39. }
  40. static si64 lodSeek(void * opaque, si64 pos, int whence)
  41. {
  42. auto * data = static_cast<CInputStream *>(opaque);
  43. if(whence & AVSEEK_SIZE)
  44. return data->getSize();
  45. return data->seek(pos);
  46. }
  47. [[noreturn]] static void throwFFmpegError(int errorCode)
  48. {
  49. std::array<char, AV_ERROR_MAX_STRING_SIZE> errorMessage{};
  50. av_strerror(errorCode, errorMessage.data(), errorMessage.size());
  51. throw std::runtime_error(errorMessage.data());
  52. }
  53. static std::unique_ptr<CInputStream> findVideoData(const VideoPath & videoToOpen)
  54. {
  55. if(CResourceHandler::get()->existsResource(videoToOpen))
  56. return CResourceHandler::get()->load(videoToOpen);
  57. auto highQualityVideoToOpenWithDir = videoToOpen.addPrefix("VIDEO/");
  58. auto lowQualityVideo = videoToOpen.toType<EResType::VIDEO_LOW_QUALITY>();
  59. auto lowQualityVideoWithDir = highQualityVideoToOpenWithDir.toType<EResType::VIDEO_LOW_QUALITY>();
  60. if(CResourceHandler::get()->existsResource(highQualityVideoToOpenWithDir))
  61. return CResourceHandler::get()->load(highQualityVideoToOpenWithDir);
  62. if(CResourceHandler::get()->existsResource(lowQualityVideo))
  63. return CResourceHandler::get()->load(lowQualityVideo);
  64. if(CResourceHandler::get()->existsResource(lowQualityVideoWithDir))
  65. return CResourceHandler::get()->load(lowQualityVideoWithDir);
  66. return nullptr;
  67. }
  68. bool FFMpegStream::openInput(const VideoPath & videoToOpen)
  69. {
  70. input = findVideoData(videoToOpen);
  71. return input != nullptr;
  72. }
  73. void FFMpegStream::openContext()
  74. {
  75. static const int BUFFER_SIZE = 4096;
  76. input->seek(0);
  77. auto * buffer = static_cast<unsigned char *>(av_malloc(BUFFER_SIZE)); // will be freed by ffmpeg
  78. context = avio_alloc_context(buffer, BUFFER_SIZE, 0, input.get(), lodRead, nullptr, lodSeek);
  79. formatContext = avformat_alloc_context();
  80. formatContext->pb = context;
  81. // filename is not needed - file was already open and stored in this->data;
  82. int avfopen = avformat_open_input(&formatContext, "dummyFilename", nullptr, nullptr);
  83. if(avfopen != 0)
  84. throwFFmpegError(avfopen);
  85. // Retrieve stream information
  86. int findStreamInfo = avformat_find_stream_info(formatContext, nullptr);
  87. if(avfopen < 0)
  88. throwFFmpegError(findStreamInfo);
  89. }
  90. void FFMpegStream::openCodec(int desiredStreamIndex)
  91. {
  92. streamIndex = desiredStreamIndex;
  93. // Find the decoder for the stream
  94. codec = avcodec_find_decoder(formatContext->streams[streamIndex]->codecpar->codec_id);
  95. if(codec == nullptr)
  96. throw std::runtime_error("Unsupported codec");
  97. codecContext = avcodec_alloc_context3(codec);
  98. if(codecContext == nullptr)
  99. throw std::runtime_error("Failed to create codec context");
  100. // Get a pointer to the codec context for the video stream
  101. int ret = avcodec_parameters_to_context(codecContext, formatContext->streams[streamIndex]->codecpar);
  102. if(ret < 0)
  103. {
  104. //We cannot get codec from parameters
  105. avcodec_free_context(&codecContext);
  106. throwFFmpegError(ret);
  107. }
  108. // Open codec
  109. ret = avcodec_open2(codecContext, codec, nullptr);
  110. if(ret < 0)
  111. {
  112. // Could not open codec
  113. codec = nullptr;
  114. throwFFmpegError(ret);
  115. }
  116. // Allocate video frame
  117. frame = av_frame_alloc();
  118. }
  119. const AVCodecParameters * FFMpegStream::getCodecParameters()
  120. {
  121. return formatContext->streams[streamIndex]->codecpar;
  122. }
  123. const AVCodecContext * FFMpegStream::getCodecContext()
  124. {
  125. return codecContext;
  126. }
  127. const AVFrame * FFMpegStream::getCurrentFrame()
  128. {
  129. return frame;
  130. }
  131. void CVideoInstance::openVideo()
  132. {
  133. openContext();
  134. openCodec(findVideoStream());
  135. }
  136. void CVideoInstance::prepareOutput(bool scaleToScreenSize, bool useTextureOutput)
  137. {
  138. //setup scaling
  139. if(scaleToScreenSize)
  140. {
  141. dimensions.x = screen->w;
  142. dimensions.y = screen->h;
  143. }
  144. else
  145. {
  146. dimensions.x = getCodecContext()->width;
  147. dimensions.y = getCodecContext()->height;
  148. }
  149. // Allocate a place to put our YUV image on that screen
  150. if (useTextureOutput)
  151. {
  152. std::array potentialFormats = {
  153. AV_PIX_FMT_YUV420P, // -> SDL_PIXELFORMAT_IYUV - most of H3 videos use YUV format, so it is preferred to save some space & conversion time
  154. AV_PIX_FMT_RGB32, // -> SDL_PIXELFORMAT_ARGB8888 - some .smk videos actually use palette, so RGB > YUV. This is also our screen texture format
  155. AV_PIX_FMT_NONE
  156. };
  157. auto preferredFormat = avcodec_find_best_pix_fmt_of_list(potentialFormats.data(), getCodecContext()->pix_fmt, false, nullptr);
  158. if (preferredFormat == AV_PIX_FMT_YUV420P)
  159. textureYUV = SDL_CreateTexture( mainRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, dimensions.x, dimensions.y);
  160. else
  161. textureRGB = SDL_CreateTexture( mainRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, dimensions.x, dimensions.y);
  162. sws = sws_getContext(getCodecContext()->width, getCodecContext()->height, getCodecContext()->pix_fmt,
  163. dimensions.x, dimensions.y, preferredFormat,
  164. SWS_BICUBIC, nullptr, nullptr, nullptr);
  165. }
  166. else
  167. {
  168. surface = CSDL_Ext::newSurface(dimensions.x, dimensions.y);
  169. sws = sws_getContext(getCodecContext()->width, getCodecContext()->height, getCodecContext()->pix_fmt,
  170. dimensions.x, dimensions.y, AV_PIX_FMT_RGB32,
  171. SWS_BICUBIC, nullptr, nullptr, nullptr);
  172. }
  173. if (sws == nullptr)
  174. throw std::runtime_error("Failed to create sws");
  175. }
  176. void FFMpegStream::decodeNextFrame()
  177. {
  178. AVPacket packet;
  179. for(;;)
  180. {
  181. int rc = avcodec_receive_frame(codecContext, frame);
  182. if(rc == AVERROR(EAGAIN))
  183. break;
  184. if(rc < 0)
  185. throwFFmpegError(rc);
  186. return;
  187. }
  188. for(;;)
  189. {
  190. int ret = av_read_frame(formatContext, &packet);
  191. if(ret < 0)
  192. {
  193. if(ret == AVERROR_EOF)
  194. {
  195. av_packet_unref(&packet);
  196. av_frame_free(&frame);
  197. frame = nullptr;
  198. return;
  199. }
  200. throwFFmpegError(ret);
  201. }
  202. // Is this a packet from the video stream?
  203. if(packet.stream_index == streamIndex)
  204. {
  205. // Decode video frame
  206. int rc = avcodec_send_packet(codecContext, &packet);
  207. if(rc < 0 && rc != AVERROR(EAGAIN))
  208. throwFFmpegError(rc);
  209. rc = avcodec_receive_frame(codecContext, frame);
  210. if(rc == AVERROR(EAGAIN))
  211. {
  212. av_packet_unref(&packet);
  213. continue;
  214. }
  215. if(rc < 0)
  216. throwFFmpegError(rc);
  217. av_packet_unref(&packet);
  218. return;
  219. }
  220. av_packet_unref(&packet);
  221. }
  222. }
  223. bool CVideoInstance::loadNextFrame()
  224. {
  225. decodeNextFrame();
  226. const AVFrame * frame = getCurrentFrame();
  227. if(!frame)
  228. return false;
  229. uint8_t * data[4] = {};
  230. int linesize[4] = {};
  231. if(textureYUV)
  232. {
  233. av_image_alloc(data, linesize, dimensions.x, dimensions.y, AV_PIX_FMT_YUV420P, 1);
  234. sws_scale(sws, frame->data, frame->linesize, 0, getCodecContext()->height, data, linesize);
  235. SDL_UpdateYUVTexture(textureYUV, nullptr, data[0], linesize[0], data[1], linesize[1], data[2], linesize[2]);
  236. av_freep(&data[0]);
  237. }
  238. if(textureRGB)
  239. {
  240. av_image_alloc(data, linesize, dimensions.x, dimensions.y, AV_PIX_FMT_RGB32, 1);
  241. sws_scale(sws, frame->data, frame->linesize, 0, getCodecContext()->height, data, linesize);
  242. SDL_UpdateTexture(textureRGB, nullptr, data[0], linesize[0]);
  243. av_freep(&data[0]);
  244. }
  245. if(surface)
  246. {
  247. // Avoid buffer overflow caused by sws_scale():
  248. // http://trac.ffmpeg.org/ticket/9254
  249. size_t pic_bytes = surface->pitch * surface->h;
  250. size_t ffmped_pad = 1024; /* a few bytes of overflow will go here */
  251. void * for_sws = av_malloc(pic_bytes + ffmped_pad);
  252. data[0] = (ui8 *)for_sws;
  253. linesize[0] = surface->pitch;
  254. sws_scale(sws, frame->data, frame->linesize, 0, getCodecContext()->height, data, linesize);
  255. memcpy(surface->pixels, for_sws, pic_bytes);
  256. av_free(for_sws);
  257. }
  258. return true;
  259. }
  260. bool CVideoInstance::videoEnded()
  261. {
  262. return getCurrentFrame() == nullptr;
  263. }
  264. CVideoInstance::~CVideoInstance()
  265. {
  266. sws_freeContext(sws);
  267. SDL_DestroyTexture(textureYUV);
  268. SDL_DestroyTexture(textureRGB);
  269. SDL_FreeSurface(surface);
  270. }
  271. FFMpegStream::~FFMpegStream()
  272. {
  273. // state.videoStream.codec???
  274. // state.audioStream.codec???
  275. av_frame_free(&frame);
  276. avcodec_close(codecContext);
  277. avcodec_free_context(&codecContext);
  278. avcodec_close(codecContext);
  279. avcodec_free_context(&codecContext);
  280. avformat_close_input(&formatContext);
  281. av_free(context);
  282. }
  283. Point CVideoInstance::size()
  284. {
  285. if(!getCurrentFrame())
  286. throw std::runtime_error("Invalid video frame!");
  287. return Point(getCurrentFrame()->width, getCurrentFrame()->height);
  288. }
  289. void CVideoInstance::show(const Point & position, Canvas & canvas)
  290. {
  291. if(sws == nullptr)
  292. throw std::runtime_error("No video to show!");
  293. CSDL_Ext::blitSurface(surface, canvas.getInternalSurface(), position);
  294. }
  295. double FFMpegStream::getCurrentFrameEndTime()
  296. {
  297. #if(LIBAVUTIL_VERSION_MAJOR < 58)
  298. auto packet_duration = frame->pkt_duration;
  299. #else
  300. auto packet_duration = frame->duration;
  301. #endif
  302. return (frame->pts + packet_duration) * av_q2d(formatContext->streams[streamIndex]->time_base);
  303. }
  304. double FFMpegStream::getCurrentFrameDuration()
  305. {
  306. #if(LIBAVUTIL_VERSION_MAJOR < 58)
  307. auto packet_duration = frame->pkt_duration;
  308. #else
  309. auto packet_duration = frame->duration;
  310. #endif
  311. return (packet_duration) * av_q2d(formatContext->streams[streamIndex]->time_base);
  312. }
  313. void CVideoInstance::tick(uint32_t msPassed)
  314. {
  315. if(sws == nullptr)
  316. throw std::runtime_error("No video to show!");
  317. if(videoEnded())
  318. throw std::runtime_error("Video already ended!");
  319. frameTime += msPassed / 1000.0;
  320. if(frameTime >= getCurrentFrameEndTime())
  321. loadNextFrame();
  322. }
  323. struct FFMpegFormatDescription
  324. {
  325. uint8_t sampleSizeBytes;
  326. uint8_t wavFormatID;
  327. bool isPlanar;
  328. };
  329. static FFMpegFormatDescription getAudioFormatProperties(int audioFormat)
  330. {
  331. switch (audioFormat)
  332. {
  333. case AV_SAMPLE_FMT_U8: return { 1, 1, false};
  334. case AV_SAMPLE_FMT_U8P: return { 1, 1, true};
  335. case AV_SAMPLE_FMT_S16: return { 2, 1, false};
  336. case AV_SAMPLE_FMT_S16P: return { 2, 1, true};
  337. case AV_SAMPLE_FMT_S32: return { 4, 1, false};
  338. case AV_SAMPLE_FMT_S32P: return { 4, 1, true};
  339. case AV_SAMPLE_FMT_S64: return { 8, 1, false};
  340. case AV_SAMPLE_FMT_S64P: return { 8, 1, true};
  341. case AV_SAMPLE_FMT_FLT: return { 4, 3, false};
  342. case AV_SAMPLE_FMT_FLTP: return { 4, 3, true};
  343. case AV_SAMPLE_FMT_DBL: return { 8, 3, false};
  344. case AV_SAMPLE_FMT_DBLP: return { 8, 3, true};
  345. }
  346. throw std::runtime_error("Invalid audio format");
  347. }
  348. int FFMpegStream::findAudioStream()
  349. {
  350. for(int i = 0; i < formatContext->nb_streams; i++)
  351. if(formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
  352. return i;
  353. return -1;
  354. }
  355. int FFMpegStream::findVideoStream()
  356. {
  357. for(int i = 0; i < formatContext->nb_streams; i++)
  358. if(formatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  359. return i;
  360. return -1;
  361. }
  362. std::pair<std::unique_ptr<ui8 []>, si64> CAudioInstance::extractAudio(const VideoPath & videoToOpen)
  363. {
  364. if (!openInput(videoToOpen))
  365. return { nullptr, 0};
  366. openContext();
  367. openCodec(findAudioStream());
  368. const auto * codecpar = getCodecParameters();
  369. std::vector<ui8> samples;
  370. auto formatProperties = getAudioFormatProperties(codecpar->format);
  371. #if(LIBAVUTIL_VERSION_MAJOR < 58)
  372. int numChannels = codecpar->channels;
  373. #else
  374. int numChannels = codecpar->ch_layout.nb_channels;
  375. #endif
  376. samples.reserve(44100 * 5); // arbitrary 5-second buffer
  377. for (;;)
  378. {
  379. decodeNextFrame();
  380. const AVFrame * frame = getCurrentFrame();
  381. if (!frame)
  382. break;
  383. int samplesToRead = frame->nb_samples * numChannels;
  384. int bytesToRead = samplesToRead * formatProperties.sampleSizeBytes;
  385. if (formatProperties.isPlanar && numChannels > 1)
  386. {
  387. // Workaround for lack of resampler
  388. // Currently, ffmpeg on conan systems is built without sws resampler
  389. // Because of that, and because wav format does not supports 'planar' formats from ffmpeg
  390. // we need to de-planarize it and convert to "normal" (non-planar / interleaved) stream
  391. samples.reserve(samples.size() + bytesToRead);
  392. for (int sm = 0; sm < frame->nb_samples; ++sm)
  393. for (int ch = 0; ch < numChannels; ++ch)
  394. samples.insert(samples.end(), frame->data[ch] + sm * formatProperties.sampleSizeBytes, frame->data[ch] + (sm+1) * formatProperties.sampleSizeBytes );
  395. }
  396. else
  397. {
  398. samples.insert(samples.end(), frame->data[0], frame->data[0] + bytesToRead);
  399. }
  400. }
  401. typedef struct WAV_HEADER {
  402. ui8 RIFF[4] = {'R', 'I', 'F', 'F'};
  403. ui32 ChunkSize;
  404. ui8 WAVE[4] = {'W', 'A', 'V', 'E'};
  405. ui8 fmt[4] = {'f', 'm', 't', ' '};
  406. ui32 Subchunk1Size = 16;
  407. ui16 AudioFormat = 1;
  408. ui16 NumOfChan = 2;
  409. ui32 SamplesPerSec = 22050;
  410. ui32 bytesPerSec = 22050 * 2;
  411. ui16 blockAlign = 2;
  412. ui16 bitsPerSample = 32;
  413. ui8 Subchunk2ID[4] = {'d', 'a', 't', 'a'};
  414. ui32 Subchunk2Size;
  415. } wav_hdr;
  416. wav_hdr wav;
  417. wav.ChunkSize = samples.size() + sizeof(wav_hdr) - 8;
  418. wav.AudioFormat = formatProperties.wavFormatID; // 1 = PCM, 3 = IEEE float
  419. wav.NumOfChan = numChannels;
  420. wav.SamplesPerSec = codecpar->sample_rate;
  421. wav.bytesPerSec = codecpar->sample_rate * formatProperties.sampleSizeBytes;
  422. wav.bitsPerSample = formatProperties.sampleSizeBytes * 8;
  423. wav.Subchunk2Size = samples.size() + sizeof(wav_hdr) - 44;
  424. auto wavPtr = reinterpret_cast<ui8*>(&wav);
  425. auto dat = std::make_pair(std::make_unique<ui8[]>(samples.size() + sizeof(wav_hdr)), samples.size() + sizeof(wav_hdr));
  426. std::copy(wavPtr, wavPtr + sizeof(wav_hdr), dat.first.get());
  427. std::copy(samples.begin(), samples.end(), dat.first.get() + sizeof(wav_hdr));
  428. return dat;
  429. //CCS->soundh->playSound(dat);
  430. }
  431. bool CVideoPlayer::openAndPlayVideoImpl(const VideoPath & name, const Point & position, bool useOverlay, bool scale, bool stopOnKey)
  432. {
  433. CVideoInstance instance;
  434. CAudioInstance audio;
  435. auto extractedAudio = audio.extractAudio(name);
  436. int audioHandle = CCS->soundh->playSound(extractedAudio);
  437. if (!instance.openInput(name))
  438. return true;
  439. instance.openVideo();
  440. instance.prepareOutput(scale, useOverlay);
  441. auto lastTimePoint = boost::chrono::steady_clock::now();
  442. while(instance.loadNextFrame())
  443. {
  444. if(stopOnKey)
  445. {
  446. GH.input().fetchEvents();
  447. if(GH.input().ignoreEventsUntilInput())
  448. {
  449. CCS->soundh->stopSound(audioHandle);
  450. return false;
  451. }
  452. }
  453. SDL_Rect rect;
  454. rect.x = position.x;
  455. rect.y = position.y;
  456. rect.w = instance.dimensions.x;
  457. rect.h = instance.dimensions.y;
  458. if(useOverlay)
  459. SDL_RenderFillRect(mainRenderer, &rect);
  460. else
  461. SDL_RenderClear(mainRenderer);
  462. if(instance.textureYUV)
  463. SDL_RenderCopy(mainRenderer, instance.textureYUV, nullptr, &rect);
  464. else
  465. SDL_RenderCopy(mainRenderer, instance.textureRGB, nullptr, &rect);
  466. SDL_RenderPresent(mainRenderer);
  467. // Framerate delay
  468. double targetFrameTimeSeconds = instance.getCurrentFrameDuration();
  469. auto targetFrameTime = boost::chrono::milliseconds(static_cast<int>(1000 * (targetFrameTimeSeconds)));
  470. auto timePointAfterPresent = boost::chrono::steady_clock::now();
  471. auto timeSpentBusy = boost::chrono::duration_cast<boost::chrono::milliseconds>(timePointAfterPresent - lastTimePoint);
  472. logGlobal->info("Sleeping for %d", (targetFrameTime - timeSpentBusy).count());
  473. if(targetFrameTime > timeSpentBusy)
  474. boost::this_thread::sleep_for(targetFrameTime - timeSpentBusy);
  475. lastTimePoint = boost::chrono::steady_clock::now();
  476. }
  477. return true;
  478. }
  479. bool CVideoPlayer::playIntroVideo(const VideoPath & name)
  480. {
  481. return openAndPlayVideoImpl(name, Point(0, 0), true, true, true);
  482. }
  483. void CVideoPlayer::playSpellbookAnimation(const VideoPath & name, const Point & position)
  484. {
  485. openAndPlayVideoImpl(name, position, false, false, false);
  486. }
  487. std::unique_ptr<IVideoInstance> CVideoPlayer::open(const VideoPath & name, bool scaleToScreen)
  488. {
  489. auto result = std::make_unique<CVideoInstance>();
  490. if (!result->openInput(name))
  491. return nullptr;
  492. result->openVideo();
  493. result->prepareOutput(scaleToScreen, false);
  494. result->loadNextFrame(); // prepare 1st frame
  495. return result;
  496. }
  497. std::pair<std::unique_ptr<ui8[]>, si64> CVideoPlayer::getAudio(const VideoPath & videoToOpen)
  498. {
  499. CAudioInstance audio;
  500. return audio.extractAudio(videoToOpen);
  501. }
  502. #endif