Debian Codegraph @

CorsixTH/Src/th_movie.cpp

raw
1 /*
2 Copyright (c) 2012 Stephen Baker
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #include "th_movie.h"
24
25 #include "config.h"
26
27 #include "lua_sdl.h"
28 #ifdef CORSIX_TH_USE_FFMPEG
29
30 extern "C" {
31 #include <libavcodec/avcodec.h>
32 #include <libavutil/avutil.h>
33 #include <libavutil/channel_layout.h>
34 #include <libavutil/error.h>
35 #include <libavutil/imgutils.h>
36 #include <libavutil/rational.h>
37 #include <libavutil/samplefmt.h>
38 #include <libswresample/version.h>
39 #include <libswscale/swscale.h>
40 }
41 #include <SDL_error.h>
42 #include <SDL_events.h>
43 #include <SDL_mixer.h>
44 #include <SDL_pixels.h>
45 #include <SDL_rect.h>
46 #include <SDL_render.h>
47 #include <SDL_timer.h>
48
49 #include <cerrno>
50 #include <chrono>
51 #include <cstring>
52 #include <iostream>
53 #include <stdexcept>
54 #include <utility>
55
56 namespace {
57
58 void th_movie_audio_callback(int iChannel, void* pStream, int iStreamSize,
59 void* pUserData) {
60 movie_player* pMovie = static_cast<movie_player*>(pUserData);
61 pMovie->copy_audio_to_stream(static_cast<uint8_t*>(pStream), iStreamSize);
62 }
63
64 } // namespace
65
66 movie_picture::movie_picture()
67 : buffer(nullptr),
68 pixel_format(AV_PIX_FMT_RGB24),
69 width(0),
70 height(0),
71 pts(0) {}
72
73 movie_picture::~movie_picture() { av_freep(&buffer); }
74
75 void movie_picture::allocate(int iWidth, int iHeight) {
76 int numBytes = av_image_get_buffer_size(pixel_format, iWidth, iHeight, 1);
77 if (numBytes < 0) {
78 throw std::runtime_error(
79 "problem calculating size of buffer for movie_picture");
80 }
81 width = iWidth;
82 height = iHeight;
83 av_freep(&buffer);
84 buffer =
85 static_cast<uint8_t*>(av_mallocz(static_cast<std::size_t>(numBytes)));
86 }
87
88 void movie_picture::deallocate() { av_freep(&buffer); }
89
90 movie_picture_buffer::movie_picture_buffer()
91 : aborting(false),
92 allocated(false),
93 picture_count(0),
94 read_index(0),
95 write_index(0),
96 sws_context(nullptr),
97 texture(nullptr) {}
98
99 movie_picture_buffer::~movie_picture_buffer() {
100 sws_freeContext(sws_context);
101 if (texture) {
102 SDL_DestroyTexture(texture);
103 texture = nullptr;
104 }
105 }
106
107 void movie_picture_buffer::abort() {
108 aborting = true;
109 std::scoped_lock lock(mutex);
110 cond.notify_all();
111 }
112
113 void movie_picture_buffer::reset() { aborting = false; }
114
115 void movie_picture_buffer::allocate(SDL_Renderer* pRenderer, int iWidth,
116 int iHeight) {
117 if (texture) {
118 SDL_DestroyTexture(texture);
119 std::cerr << "movie_player overlay should be deallocated before being "
120 "allocated!\n";
121 }
122 texture = SDL_CreateTexture(pRenderer, SDL_PIXELFORMAT_RGB24,
123 SDL_TEXTUREACCESS_STREAMING, iWidth, iHeight);
124 if (texture == nullptr) {
125 std::cerr << "Problem creating overlay: " << SDL_GetError() << "\n";
126 return;
127 }
128 for (movie_picture& picture : picture_queue) {
129 picture.allocate(iWidth, iHeight);
130 }
131 // Do not change write_index, it's used by the other thread.
132 // read_index is only used in this thread.
133 read_index = write_index;
134
135 std::scoped_lock lock(mutex);
136 picture_count = 0;
137 allocated = true;
138 cond.notify_one();
139 }
140
141 void movie_picture_buffer::deallocate() {
142 {
143 std::scoped_lock lock(mutex);
144 allocated = false;
145 }
146
147 for (movie_picture& picture : picture_queue) {
148 std::scoped_lock pictureLock(picture.mutex);
149 picture.deallocate();
150 }
151
152 if (texture) {
153 SDL_DestroyTexture(texture);
154 texture = nullptr;
155 }
156 }
157
158 bool movie_picture_buffer::advance() {
159 if (empty()) {
160 return false;
161 }
162
163 read_index++;
164 if (read_index == picture_buffer_size) {
165 read_index = 0;
166 }
167
168 std::scoped_lock lock(mutex);
169 picture_count--;
170 cond.notify_one();
171
172 return true;
173 }
174
175 void movie_picture_buffer::draw(SDL_Renderer* pRenderer,
176 const SDL_Rect& dstrect) {
177 if (!empty()) {
178 auto& cur_pic = picture_queue[read_index];
179
180 std::scoped_lock pictureLock(cur_pic.mutex);
181 if (cur_pic.buffer) {
182 SDL_UpdateTexture(texture, nullptr, cur_pic.buffer, cur_pic.width * 3);
183 int iError = SDL_RenderCopy(pRenderer, texture, nullptr, &dstrect);
184 if (iError < 0) {
185 std::cerr << "Error displaying movie frame: " << SDL_GetError() << "\n";
186 }
187 }
188 }
189 }
190
191 double movie_picture_buffer::get_next_pts() {
192 double nextPts;
193 std::scoped_lock lock(mutex);
194 if (!allocated || picture_count < 2) {
195 nextPts = 0;
196 } else {
197 nextPts = picture_queue[(read_index + 1UL) % picture_buffer_size].pts;
198 }
199 return nextPts;
200 }
201
202 bool movie_picture_buffer::empty() {
203 std::scoped_lock lock(mutex);
204 return (!allocated || picture_count == 0);
205 }
206
207 bool movie_picture_buffer::full() {
208 std::scoped_lock lock(mutex);
209 return unsafe_full();
210 }
211
212 bool movie_picture_buffer::unsafe_full() const {
213 return (!allocated || picture_count == picture_buffer_size);
214 }
215
216 int movie_picture_buffer::write(AVFrame* pFrame, double dPts) {
217 std::unique_lock<std::mutex> picBufLock(mutex);
218 while (unsafe_full() && !aborting) {
219 cond.wait(picBufLock);
220 }
221 picBufLock.unlock();
222
223 if (aborting) {
224 return -1;
225 }
226
227 auto& picture = picture_queue[write_index];
228 std::unique_lock<std::mutex> pictureLock(picture.mutex);
229
230 if (picture.buffer) {
231 sws_context = sws_getCachedContext(
232 sws_context, pFrame->width, pFrame->height,
233 static_cast<AVPixelFormat>(pFrame->format), picture.width,
234 picture.height, picture.pixel_format, SWS_BICUBIC, nullptr, nullptr,
235 nullptr);
236 if (sws_context == nullptr) {
237 std::cerr << "Failed to initialize SwsContext\n";
238 return 1;
239 }
240
241 /* Allocate a new frame and buffer for the destination RGB24 data. */
242 av_frame_unique_ptr pFrameRGB(av_frame_alloc());
243 av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, picture.buffer,
244 picture.pixel_format, picture.width, picture.height,
245 1);
246
247 /* Rescale the frame data and convert it to RGB24. */
248 sws_scale(sws_context, pFrame->data, pFrame->linesize, 0, pFrame->height,
249 pFrameRGB->data, pFrameRGB->linesize);
250
251 picture.pts = dPts;
252
253 pictureLock.unlock();
254 write_index++;
255 if (write_index == picture_buffer_size) {
256 write_index = 0;
257 }
258 picBufLock.lock();
259 picture_count++;
260 picBufLock.unlock();
261 }
262
263 return 0;
264 }
265
266 av_packet_queue::av_packet_queue() = default;
267
268 std::size_t av_packet_queue::get_count() const { return data.size(); }
269
270 void av_packet_queue::push(av_packet_unique_ptr pPacket) {
271 std::scoped_lock lock(mutex);
272 data.push(std::move(pPacket));
273
274 cond.notify_one();
275 }
276
277 av_packet_unique_ptr av_packet_queue::pull(bool block) {
278 std::unique_lock lock(mutex);
279
280 if (data.empty() && block) {
281 cond.wait(lock);
282 }
283
284 av_packet_unique_ptr pPacket(nullptr);
285 if (!data.empty()) {
286 pPacket.swap(data.front());
287 data.pop();
288 }
289
290 return pPacket;
291 }
292
293 void av_packet_queue::release() {
294 std::scoped_lock lock(mutex);
295 cond.notify_all();
296 }
297
298 void av_packet_queue::clear() {
299 while (get_count() > 0) {
300 av_packet_unique_ptr p = pull(false);
301 }
302 }
303
304 movie_player::movie_player()
305 : renderer(nullptr),
306 format_context(nullptr),
307 video_codec_context(nullptr),
308 audio_codec_context(nullptr),
309 audio_resample_context(nullptr),
310 empty_audio_chunk(nullptr),
311 audio_chunk_buffer{},
312 audio_channel(-1),
313 error_buffer{},
314 aborting(false),
315 video_stream_index(-1),
316 audio_stream_index(-1),
317 current_sync_pts(0.0),
318 current_sync_pts_system_time(0),
319 mixer_channels(0),
320 mixer_frequency(0) {
321 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
322 av_register_all();
323 #endif
324 }
325
326 movie_player::~movie_player() { unload(); }
327
328 void movie_player::set_renderer(SDL_Renderer* pRenderer) {
329 renderer = pRenderer;
330 }
331
332 bool movie_player::movies_enabled() const { return true; }
333
334 bool movie_player::load(const char* szFilepath) {
335 int iError = 0;
336
337 unload(); // Unload any currently loaded video to free memory
338 aborting = false;
339
340 iError = avformat_open_input(&format_context, szFilepath, nullptr, nullptr);
341 if (iError < 0) {
342 av_strerror(iError, error_buffer, movie_error_buffer_capacity);
343 last_error = std::string(error_buffer);
344 return false;
345 }
346
347 iError = avformat_find_stream_info(format_context, nullptr);
348 if (iError < 0) {
349 av_strerror(iError, error_buffer, movie_error_buffer_capacity);
350 last_error = std::string(error_buffer);
351 return false;
352 }
353
354 av_codec_ptr video_decoder; // unowned, do not free
355 video_stream_index = av_find_best_stream(format_context, AVMEDIA_TYPE_VIDEO,
356 -1, -1, &video_decoder, 0);
357 if (video_stream_index < 0) {
358 av_strerror(video_stream_index, error_buffer, movie_error_buffer_capacity);
359 last_error = std::string(error_buffer);
360 return false;
361 }
362 video_codec_context = get_codec_context_for_stream(
363 video_decoder, format_context->streams[video_stream_index]);
364 avcodec_open2(video_codec_context.get(), video_decoder, nullptr);
365
366 av_codec_ptr audio_decoder; // unowned, do not free
367 audio_stream_index = av_find_best_stream(format_context, AVMEDIA_TYPE_AUDIO,
368 -1, -1, &audio_decoder, 0);
369 if (audio_stream_index >= 0) {
370 audio_codec_context = get_codec_context_for_stream(
371 audio_decoder, format_context->streams[audio_stream_index]);
372 avcodec_open2(audio_codec_context.get(), audio_decoder, nullptr);
373 }
374
375 return true;
376 }
377
378 av_codec_context_unique_ptr movie_player::get_codec_context_for_stream(
379 av_codec_ptr codec, AVStream* stream) const {
380 av_codec_context_unique_ptr ctx(avcodec_alloc_context3(codec));
381 avcodec_parameters_to_context(ctx.get(), stream->codecpar);
382 return ctx;
383 }
384
385 void movie_player::unload() {
386 aborting = true;
387
388 audio_queue.release();
389 video_queue.release();
390 movie_picture_buffer.abort();
391
392 if (stream_thread.joinable()) {
393 stream_thread.join();
394 }
395 if (video_thread.joinable()) {
396 video_thread.join();
397 }
398
399 // wait until after other threads are closed to clear the packet queues
400 // so we don't free something being used.
401 audio_queue.clear();
402 video_queue.clear();
403 movie_picture_buffer.deallocate();
404
405 video_codec_context.reset();
406
407 if (audio_channel >= 0) {
408 Mix_UnregisterAllEffects(audio_channel);
409 Mix_HaltChannel(audio_channel);
410 empty_audio_chunk.reset();
411 audio_channel = -1;
412 }
413
414 std::scoped_lock audioLock(decoding_audio_mutex);
415
416 audio_codec_context.reset();
417
418 swr_free(&audio_resample_context);
419
420 if (format_context) {
421 avformat_close_input(&format_context);
422 }
423 }
424
425 void movie_player::play(int requested_audio_channel) {
426 if (!renderer) {
427 last_error = std::string("Cannot play before setting the renderer");
428 return;
429 }
430
431 audio_queue.clear();
432 video_queue.clear();
433 movie_picture_buffer.reset();
434 movie_picture_buffer.allocate(renderer, video_codec_context->width,
435 video_codec_context->height);
436
437 paused = false;
438 current_sync_pts = 0;
439 current_sync_pts_system_time = SDL_GetTicks();
440
441 play_audio(requested_audio_channel);
442
443 stream_thread = std::thread(&movie_player::read_streams, this);
444 video_thread = std::thread(&movie_player::run_video, this);
445 }
446
447 void movie_player::play_audio(int requested_audio_channel) {
448 if (audio_stream_index < 0) {
449 return;
450 }
451
452 int opened = Mix_QuerySpec(&mixer_frequency, nullptr, &mixer_channels);
453
454 if (opened == 0 || mixer_channels == 0) {
455 return;
456 }
457
458 std::int64_t target_channel_layout;
459 switch (mixer_channels) {
460 case 1:
461 target_channel_layout = AV_CH_LAYOUT_MONO;
462 break;
463 case 2:
464 target_channel_layout = AV_CH_LAYOUT_STEREO;
465 break;
466 case 4:
467 target_channel_layout = AV_CH_LAYOUT_QUAD;
468 break;
469 case 6:
470 target_channel_layout = AV_CH_LAYOUT_5POINT1;
471 break;
472 case 8:
473 target_channel_layout = AV_CH_LAYOUT_7POINT1;
474 break;
475 default:
476 std::cerr << "WARN: unsupported channel layout " << mixer_channels
477 << ". Please report issue.";
478 target_channel_layout = 0;
479 }
480
481 #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 24, 100) && \
482 LIBSWRESAMPLE_VERSION_INT >= AV_VERSION_INT(4, 5, 100)
483 av_channel_layout_unique_ptr ch_layout(new AVChannelLayout{});
484
485 if (target_channel_layout == 0) {
486 av_channel_layout_default(ch_layout.get(), mixer_channels);
487 } else {
488 av_channel_layout_from_mask(ch_layout.get(), target_channel_layout);
489 }
490
491 swr_alloc_set_opts2(&audio_resample_context, ch_layout.get(),
492 AV_SAMPLE_FMT_S16, mixer_frequency,
493 &(audio_codec_context->ch_layout),
494 audio_codec_context->sample_fmt,
495 audio_codec_context->sample_rate, 0, nullptr);
496 #else
497 if (target_channel_layout == 0) {
498 target_channel_layout = av_get_default_channel_layout(mixer_channels);
499 }
500
501 audio_resample_context = swr_alloc_set_opts(
502 audio_resample_context, static_cast<std::int64_t>(target_channel_layout),
503 AV_SAMPLE_FMT_S16, mixer_frequency,
504 static_cast<std::int64_t>(audio_codec_context->channel_layout),
505 audio_codec_context->sample_fmt, audio_codec_context->sample_rate, 0,
506 nullptr);
507 #endif
508 swr_init(audio_resample_context);
509 empty_audio_chunk.reset(
510 Mix_QuickLoad_RAW(audio_chunk_buffer.data(),
511 static_cast<uint32_t>(audio_chunk_buffer.size())));
512
513 audio_channel =
514 Mix_PlayChannel(requested_audio_channel, empty_audio_chunk.get(), -1);
515 if (audio_channel < 0) {
516 audio_channel = -1;
517 last_error = std::string(Mix_GetError());
518 empty_audio_chunk.reset();
519 } else {
520 Mix_RegisterEffect(audio_channel, th_movie_audio_callback, nullptr, this);
521 }
522 }
523
524 void movie_player::stop() { aborting = true; }
525
526 void movie_player::togglePause() {
527 // This is so close to thread safe. Note the potential race between !paused
528 // and paused.exchange. If c++ had an atomic not, or if fetch_xor
529 // worked on bools in c++ 17 I would have done it. As is I'd need a mutex or a
530 // compare_exchange loop, which didn't seem worth it since we only call from
531 // the event thread anyway.
532 bool wasPaused = paused.exchange(!paused);
533
534 if (!wasPaused) {
535 pause_start_time = SDL_GetTicks();
536 } else {
537 uint32_t pauseDuration = SDL_GetTicks() - pause_start_time;
538 current_sync_pts_system_time += pauseDuration;
539 }
540 }
541
542 int movie_player::get_native_height() const {
543 int iHeight = 0;
544
545 if (video_codec_context) {
546 iHeight = video_codec_context->height;
547 }
548 return iHeight;
549 }
550
551 int movie_player::get_native_width() const {
552 int iWidth = 0;
553
554 if (video_codec_context) {
555 iWidth = video_codec_context->width;
556 }
557 return iWidth;
558 }
559
560 bool movie_player::has_audio_track() const { return (audio_stream_index >= 0); }
561
562 double movie_player::get_movie_length() const {
563 if (format_context && video_stream_index >= 0) {
564 AVStream* stream = format_context->streams[video_stream_index];
565 return static_cast<double>(stream->duration) * av_q2d(stream->time_base) *
566 1000.0;
567 }
568 return 0;
569 }
570
571 const char* movie_player::get_last_error() const { return last_error.c_str(); }
572
573 void movie_player::clear_last_error() { last_error.clear(); }
574
575 double movie_player::refresh(const SDL_Rect& destination_rect) {
576 SDL_Rect dest_rect = SDL_Rect{destination_rect.x, destination_rect.y,
577 destination_rect.w, destination_rect.h};
578
579 double dCurTime = (paused.load() ? pause_start_time : SDL_GetTicks()) -
580 current_sync_pts_system_time + current_sync_pts * 1000.0;
581
582 if (!movie_picture_buffer.empty()) {
583 double dNextPts = movie_picture_buffer.get_next_pts();
584
585 if (dNextPts > 0 && dNextPts * 1000.0 <= dCurTime) {
586 movie_picture_buffer.advance();
587 }
588 movie_picture_buffer.draw(renderer, dest_rect);
589 }
590
591 return dCurTime;
592 }
593
594 void movie_player::allocate_picture_buffer() {
595 if (!video_codec_context) {
596 return;
597 }
598 movie_picture_buffer.allocate(renderer, get_native_width(),
599 get_native_height());
600 }
601
602 void movie_player::deallocate_picture_buffer() {
603 movie_picture_buffer.deallocate();
604 }
605
606 void movie_player::read_streams() {
607 while (!aborting) {
608 av_packet_unique_ptr packet(
609 static_cast<AVPacket*>(av_malloc(sizeof(AVPacket))));
610 int iError = av_read_frame(format_context, packet.get());
611 if (iError < 0) {
612 if (iError == AVERROR_EOF || format_context->pb->error ||
613 format_context->pb->eof_reached) {
614 break;
615 }
616 } else {
617 if (packet->stream_index == video_stream_index) {
618 video_queue.push(std::move(packet));
619 } else if (packet->stream_index == audio_stream_index &&
620 audio_channel >= 0) {
621 audio_queue.push(std::move(packet));
622 }
623 }
624 }
625
626 while (!aborting) {
627 if (video_queue.get_count() == 0 && audio_queue.get_count() == 0 &&
628 movie_picture_buffer.get_next_pts() == 0) {
629 break;
630 }
631 std::this_thread::sleep_for(std::chrono::milliseconds(10));
632 }
633
634 SDL_Event endEvent;
635 endEvent.type = SDL_USEREVENT_MOVIE_OVER;
636 SDL_PushEvent(&endEvent);
637 aborting = true;
638 }
639
640 void movie_player::run_video() {
641 av_frame_unique_ptr pFrame(av_frame_alloc());
642 double dClockPts;
643 int iError;
644
645 while (!aborting) {
646 av_frame_unref(pFrame.get());
647
648 iError = populate_frame(video_stream_index, *pFrame);
649
650 if (iError == AVERROR_EOF) {
651 break;
652 } else if (iError < 0) {
653 std::cerr << "Unexpected error " << iError
654 << " while decoding video packet\n";
655 break;
656 }
657
658 dClockPts = get_presentation_time_for_frame(*pFrame, video_stream_index);
659 iError = movie_picture_buffer.write(pFrame.get(), dClockPts);
660
661 if (iError < 0) {
662 break;
663 }
664 }
665
666 avcodec_flush_buffers(video_codec_context.get());
667 }
668
669 double movie_player::get_presentation_time_for_frame(const AVFrame& frame,
670 int streamIndex) const {
671 int64_t pts;
672 #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 18, 100)
673 pts = av_frame_get_best_effort_timestamp(&frame);
674 #else
675 pts = frame.best_effort_timestamp;
676 #endif // LIBAVCODEC_VERSION_INT
677
678 if (pts == AV_NOPTS_VALUE) {
679 pts = 0;
680 }
681
682 return static_cast<double>(pts) *
683 av_q2d(format_context->streams[streamIndex]->time_base);
684 }
685
686 int movie_player::populate_frame(int stream, AVFrame& frame) {
687 if (stream == video_stream_index) {
688 return populate_frame(*video_codec_context, video_queue, frame);
689 } else if (stream == audio_stream_index) {
690 return populate_frame(*audio_codec_context, audio_queue, frame);
691 } else {
692 throw std::invalid_argument("Invalid value provided for stream");
693 }
694 }
695
696 int movie_player::populate_frame(AVCodecContext& ctx, av_packet_queue& pq,
697 AVFrame& frame) {
698 int iError = AVERROR(EAGAIN);
699 while (iError == AVERROR(EAGAIN)) {
700 iError = avcodec_receive_frame(&ctx, &frame);
701
702 if (iError == AVERROR(EAGAIN)) {
703 av_packet_unique_ptr pkt = pq.pull(true);
704 int res = avcodec_send_packet(&ctx, pkt.get());
705
706 if (res == AVERROR(EAGAIN)) {
707 throw std::runtime_error(
708 "avcodec_receive_frame and avcodec_send_packet should "
709 "not return EAGAIN at the same time");
710 }
711 }
712 }
713
714 return iError;
715 }
716
717 void movie_player::copy_audio_to_stream(uint8_t* pbStream, int iStreamSize) {
718 std::scoped_lock audioLock(decoding_audio_mutex);
719
720 while (iStreamSize > 0 && !aborting) {
721 int iAudioSize = decode_audio_frame(pbStream, iStreamSize);
722
723 if (iAudioSize <= 0) {
724 std::memset(pbStream, 0, static_cast<std::size_t>(iStreamSize));
725 return;
726 } else {
727 iStreamSize -= iAudioSize;
728 pbStream += iAudioSize;
729 }
730 }
731 }
732
733 int movie_player::decode_audio_frame(uint8_t* stream, int stream_size) {
734 // If we are paused, return silence
735 if (paused.load()) {
736 std::memset(stream, 0, static_cast<std::size_t>(stream_size));
737 return stream_size;
738 }
739
740 int iOutSamples = stream_size / (av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) *
741 mixer_channels);
742
743 int actual_samples =
744 swr_convert(audio_resample_context, &stream, iOutSamples, nullptr, 0);
745 if (actual_samples < 0) {
746 std::cerr << "WARN: Unexpected error " << actual_samples
747 << " while converting audio\n";
748 return 0;
749 } else if (actual_samples > 0) {
750 return actual_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) *
751 mixer_channels;
752 }
753
754 av_frame_unique_ptr audio_frame(av_frame_alloc());
755 int iError = populate_frame(audio_stream_index, *audio_frame);
756
757 if (iError == AVERROR_EOF) {
758 return 0;
759 } else if (iError < 0) {
760 std::cerr << "WARN: Unexpected error " << iError
761 << " while decoding audio packet\n";
762 return 0;
763 }
764
765 double dClockPts =
766 get_presentation_time_for_frame(*audio_frame, audio_stream_index);
767 current_sync_pts = dClockPts;
768 current_sync_pts_system_time = SDL_GetTicks();
769
770 actual_samples =
771 swr_convert(audio_resample_context, &stream, iOutSamples,
772 const_cast<const uint8_t**>(&audio_frame->data[0]),
773 audio_frame->nb_samples);
774 if (actual_samples < 0) {
775 std::cerr << "WARN: Unexpected error " << actual_samples
776 << " while converting audio\n";
777 return 0;
778 }
779 return actual_samples * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) *
780 mixer_channels;
781 }
782 #else // CORSIX_TH_USE_FFMPEG
783 movie_player::movie_player() {}
784 movie_player::~movie_player() {}
785 void movie_player::set_renderer(SDL_Renderer* renderer) {}
786 bool movie_player::movies_enabled() const { return false; }
787 bool movie_player::load(const char* file_path) { return true; }
788 void movie_player::unload() {}
789 void movie_player::play(int requested_audio_channel) {
790 SDL_Event endEvent;
791 endEvent.type = SDL_USEREVENT_MOVIE_OVER;
792 SDL_PushEvent(&endEvent);
793 }
794 void movie_player::stop() {}
795 void movie_player::togglePause() {}
796 int movie_player::get_native_height() const { return 0; }
797 int movie_player::get_native_width() const { return 0; }
798 bool movie_player::has_audio_track() const { return false; }
799 double movie_player::get_movie_length() const { return 0; }
800 const char* movie_player::get_last_error() const { return nullptr; }
801 void movie_player::clear_last_error() {}
802 double movie_player::refresh(const SDL_Rect& destination_rect) { return 0; }
803 void movie_player::allocate_picture_buffer() {}
804 void movie_player::deallocate_picture_buffer() {}
805 void movie_player::read_streams() {}
806 void movie_player::run_video() {}
807 void movie_player::copy_audio_to_stream(uint8_t* stream, int length) {}
808 #endif // CORSIX_TH_USE_FFMPEG