Repository navigation
Expand file tree
/
Copy pathMediaEngine.cpp
More file actions
1091 lines (942 loc) · 28.1 KB
/
Copy pathMediaEngine.cpp
File metadata and controls
1091 lines (942 loc) · 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "Common/Serialize/SerializeFuncs.h"
#include "Core/Config.h"
#include "Core/Debugger/MemBlockInfo.h"
#include "Core/HW/MediaEngine.h"
#include "Core/MemMap.h"
#include "Core/MIPS/MIPS.h"
#include "Core/Reporting.h"
#include "GPU/GPUState.h" // Used by TextureDecoder.h when templates get instanced
#include "GPU/Common/TextureDecoder.h"
#include "GPU/GPUInterface.h"
#include "Core/HW/SimpleAudioDec.h"
#include <algorithm>
#ifdef USE_FFMPEG
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/imgutils.h"
#include "libswscale/swscale.h"
}
#endif // USE_FFMPEG
#ifdef USE_FFMPEG
static AVPixelFormat getSwsFormat(int pspFormat)
{
switch (pspFormat)
{
case GE_CMODE_16BIT_BGR5650:
return AV_PIX_FMT_BGR565LE;
case GE_CMODE_16BIT_ABGR5551:
return AV_PIX_FMT_BGR555LE;
case GE_CMODE_16BIT_ABGR4444:
return AV_PIX_FMT_BGR444LE;
case GE_CMODE_32BIT_ABGR8888:
return AV_PIX_FMT_RGBA;
default:
ERROR_LOG(ME, "Unknown pixel format");
return (AVPixelFormat)0;
}
}
void ffmpeg_logger(void *, int level, const char *format, va_list va_args) {
// We're still called even if the level doesn't match.
if (level > av_log_get_level())
return;
char tmp[1024];
vsnprintf(tmp, sizeof(tmp), format, va_args);
tmp[sizeof(tmp) - 1] = '\0';
// Strip off any trailing newline.
size_t len = strlen(tmp);
if (tmp[len - 1] == '\n')
tmp[len - 1] = '\0';
if (!strcmp(tmp, "GHA Phase shifting")) {
Reporting::ReportMessage("Atrac3+: GHA phase shifting");
}
// Let's color the log line appropriately.
if (level <= AV_LOG_PANIC) {
ERROR_LOG(ME, "FF: %s", tmp);
} else if (level >= AV_LOG_VERBOSE) {
DEBUG_LOG(ME, "FF: %s", tmp);
} else {
INFO_LOG(ME, "FF: %s", tmp);
}
}
bool InitFFmpeg() {
#ifdef _DEBUG
av_log_set_level(AV_LOG_VERBOSE);
#else
av_log_set_level(AV_LOG_WARNING);
#endif
av_log_set_callback(&ffmpeg_logger);
return true;
}
#endif
static int getPixelFormatBytes(int pspFormat)
{
switch (pspFormat)
{
case GE_CMODE_16BIT_BGR5650:
case GE_CMODE_16BIT_ABGR5551:
case GE_CMODE_16BIT_ABGR4444:
return 2;
case GE_CMODE_32BIT_ABGR8888:
return 4;
default:
ERROR_LOG(ME, "Unknown pixel format");
return 4;
}
}
MediaEngine::MediaEngine(): m_pdata(0) {
#ifdef USE_FFMPEG
m_pFormatCtx = 0;
m_pCodecCtxs.clear();
m_pFrame = 0;
m_pFrameRGB = 0;
m_pIOContext = 0;
m_sws_ctx = 0;
#endif
m_sws_fmt = 0;
m_buffer = 0;
m_videoStream = -1;
m_audioStream = -1;
m_expectedVideoStreams = 0;
m_desWidth = 0;
m_desHeight = 0;
m_decodingsize = 0;
m_bufSize = 0x2000;
m_pdata = 0;
m_demux = 0;
m_audioContext = 0;
m_audiopts = 0;
m_firstTimeStamp = 0;
m_lastTimeStamp = 0;
m_isVideoEnd = false;
m_ringbuffersize = 0;
m_mpegheaderReadPos = 0;
m_mpegheaderSize = sizeof(m_mpegheader);
m_audioType = PSP_CODEC_AT3PLUS; // in movie, we use only AT3+ audio
}
MediaEngine::~MediaEngine() {
closeMedia();
}
void MediaEngine::closeMedia() {
closeContext();
if (m_pdata)
delete m_pdata;
if (m_demux)
delete m_demux;
m_pdata = 0;
m_demux = 0;
AudioClose(&m_audioContext);
m_isVideoEnd = false;
}
void MediaEngine::DoState(PointerWrap &p) {
auto s = p.Section("MediaEngine", 1, 7);
if (!s)
return;
Do(p, m_videoStream);
Do(p, m_audioStream);
DoArray(p, m_mpegheader, sizeof(m_mpegheader));
if (s >= 4) {
Do(p, m_mpegheaderSize);
} else {
m_mpegheaderSize = sizeof(m_mpegheader);
}
if (s >= 5) {
Do(p, m_mpegheaderReadPos);
} else {
m_mpegheaderReadPos = m_mpegheaderSize;
}
if (s >= 6) {
Do(p, m_expectedVideoStreams);
} else {
m_expectedVideoStreams = 0;
}
Do(p, m_ringbuffersize);
u32 hasloadStream = m_pdata != NULL;
Do(p, hasloadStream);
if (hasloadStream && p.mode == p.MODE_READ)
reloadStream();
#ifdef USE_FFMPEG
u32 hasopencontext = m_pFormatCtx != NULL;
#else
u32 hasopencontext = false;
#endif
Do(p, hasopencontext);
if (m_pdata)
m_pdata->DoState(p);
if (m_demux)
m_demux->DoState(p);
Do(p, m_videopts);
if (s >= 7) {
Do(p, m_lastPts);
} else {
m_lastPts = m_videopts;
}
Do(p, m_audiopts);
if (s >= 2) {
Do(p, m_firstTimeStamp);
Do(p, m_lastTimeStamp);
}
if (hasopencontext && p.mode == p.MODE_READ) {
openContext(true);
}
Do(p, m_isVideoEnd);
bool noAudioDataRemoved;
Do(p, noAudioDataRemoved);
if (s >= 3) {
Do(p, m_audioType);
} else {
m_audioType = PSP_CODEC_AT3PLUS;
}
}
static int MpegReadbuffer(void *opaque, uint8_t *buf, int buf_size) {
MediaEngine *mpeg = (MediaEngine *)opaque;
int size = buf_size;
if (mpeg->m_mpegheaderReadPos < mpeg->m_mpegheaderSize) {
size = std::min(buf_size, mpeg->m_mpegheaderSize - mpeg->m_mpegheaderReadPos);
memcpy(buf, mpeg->m_mpegheader + mpeg->m_mpegheaderReadPos, size);
mpeg->m_mpegheaderReadPos += size;
} else {
size = mpeg->m_pdata->pop_front(buf, buf_size);
if (size > 0)
mpeg->m_decodingsize = size;
}
return size;
}
bool MediaEngine::SetupStreams() {
#ifdef USE_FFMPEG
const u32 magic = *(u32_le *)&m_mpegheader[0];
if (magic != PSMF_MAGIC) {
WARN_LOG_REPORT(ME, "Could not setup streams, bad magic: %08x", magic);
return false;
}
int numStreams = *(u16_be *)&m_mpegheader[0x80];
if (numStreams <= 0 || numStreams > 8) {
// Looks crazy. Let's bail out and let FFmpeg handle it.
WARN_LOG_REPORT(ME, "Could not setup streams, unexpected stream count: %d", numStreams);
return false;
}
// Looking good. Let's add those streams.
int videoStreamNum = -1;
for (int i = 0; i < numStreams; i++) {
const u8 *const currentStreamAddr = m_mpegheader + 0x82 + i * 16;
int streamId = currentStreamAddr[0];
// We only set video streams. We demux the audio stream separately.
if ((streamId & PSMF_VIDEO_STREAM_ID) == PSMF_VIDEO_STREAM_ID) {
++videoStreamNum;
addVideoStream(videoStreamNum, streamId);
}
}
// Add the streams to meet the expectation.
for (int i = videoStreamNum + 1; i < m_expectedVideoStreams; i++) {
addVideoStream(i);
}
#endif
return true;
}
bool MediaEngine::openContext(bool keepReadPos) {
#ifdef USE_FFMPEG
InitFFmpeg();
if (m_pFormatCtx || !m_pdata)
return false;
if (!keepReadPos) {
m_mpegheaderReadPos = 0;
}
m_decodingsize = 0;
m_bufSize = std::max(m_bufSize, m_mpegheaderSize);
u8 *tempbuf = (u8*)av_malloc(m_bufSize);
m_pFormatCtx = avformat_alloc_context();
m_pIOContext = avio_alloc_context(tempbuf, m_bufSize, 0, (void*)this, &MpegReadbuffer, nullptr, nullptr);
m_pFormatCtx->pb = m_pIOContext;
// Open video file
AVDictionary *open_opt = nullptr;
av_dict_set_int(&open_opt, "probesize", m_mpegheaderSize, 0);
if (avformat_open_input((AVFormatContext**)&m_pFormatCtx, nullptr, nullptr, &open_opt) != 0) {
av_dict_free(&open_opt);
return false;
}
av_dict_free(&open_opt);
if (!SetupStreams()) {
// Fallback to old behavior. Reads too much and corrupts when game doesn't read fast enough.
// SetupStreams sometimes work for newer FFmpeg 3.1+ now, but sometimes framerate is missing.
WARN_LOG_REPORT_ONCE(setupStreams, ME, "Failed to read valid video stream data from header");
if (avformat_find_stream_info(m_pFormatCtx, nullptr) < 0) {
closeContext();
return false;
}
}
if (m_videoStream >= (int)m_pFormatCtx->nb_streams) {
WARN_LOG_REPORT(ME, "Bad video stream %d", m_videoStream);
m_videoStream = -1;
}
if (m_videoStream == -1) {
// Find the first video stream
for (int i = 0; i < (int)m_pFormatCtx->nb_streams; i++) {
const AVStream *s = m_pFormatCtx->streams[i];
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)
AVMediaType type = s->codecpar->codec_type;
#else
AVMediaType type = s->codec->codec_type;
#endif
if (type == AVMEDIA_TYPE_VIDEO) {
m_videoStream = i;
break;
}
}
if (m_videoStream == -1)
return false;
}
if (!setVideoStream(m_videoStream, true))
return false;
setVideoDim();
m_audioContext = new SimpleAudio(m_audioType, 44100, 2);
m_isVideoEnd = false;
#endif // USE_FFMPEG
return true;
}
void MediaEngine::closeContext()
{
#ifdef USE_FFMPEG
if (m_buffer)
av_free(m_buffer);
if (m_pFrameRGB)
av_frame_free(&m_pFrameRGB);
if (m_pFrame)
av_frame_free(&m_pFrame);
if (m_pIOContext && m_pIOContext->buffer)
av_free(m_pIOContext->buffer);
if (m_pIOContext)
av_free(m_pIOContext);
for (auto it : m_pCodecCtxs) {
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)
avcodec_free_context(&it.second);
#else
avcodec_close(it.second);
#endif
}
m_pCodecCtxs.clear();
if (m_pFormatCtx)
avformat_close_input(&m_pFormatCtx);
sws_freeContext(m_sws_ctx);
m_sws_ctx = NULL;
m_pIOContext = 0;
#endif
m_buffer = 0;
}
bool MediaEngine::loadStream(const u8 *buffer, int readSize, int RingbufferSize)
{
closeMedia();
m_videopts = 0;
m_lastPts = -1;
m_audiopts = 0;
m_ringbuffersize = RingbufferSize;
m_pdata = new BufferQueue(RingbufferSize + 2048);
m_pdata->push(buffer, readSize);
m_firstTimeStamp = getMpegTimeStamp(buffer + PSMF_FIRST_TIMESTAMP_OFFSET);
m_lastTimeStamp = getMpegTimeStamp(buffer + PSMF_LAST_TIMESTAMP_OFFSET);
int mpegoffset = (int)(*(s32_be*)(buffer + 8));
m_demux = new MpegDemux(RingbufferSize + 2048, mpegoffset);
m_demux->addStreamData(buffer, readSize);
return true;
}
bool MediaEngine::reloadStream()
{
return loadStream(m_mpegheader, 2048, m_ringbuffersize);
}
bool MediaEngine::addVideoStream(int streamNum, int streamId) {
#ifdef USE_FFMPEG
if (m_pFormatCtx) {
// no need to add an existing stream.
if ((u32)streamNum < m_pFormatCtx->nb_streams)
return true;
const AVCodec *h264_codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!h264_codec)
return false;
AVStream *stream = avformat_new_stream(m_pFormatCtx, h264_codec);
if (stream) {
// Reference ISO/IEC 13818-1.
if (streamId == -1)
streamId = PSMF_VIDEO_STREAM_ID | streamNum;
stream->id = 0x00000100 | streamId;
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)
stream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
stream->codecpar->codec_id = AV_CODEC_ID_H264;
#else
stream->request_probe = 0;
#endif
stream->need_parsing = AVSTREAM_PARSE_FULL;
// We could set the width here, but we don't need to.
if (streamNum >= m_expectedVideoStreams) {
++m_expectedVideoStreams;
}
return true;
}
}
#endif
if (streamNum >= m_expectedVideoStreams) {
++m_expectedVideoStreams;
}
return false;
}
int MediaEngine::addStreamData(const u8 *buffer, int addSize) {
int size = addSize;
if (size > 0 && m_pdata) {
if (!m_pdata->push(buffer, size))
size = 0;
if (m_demux) {
m_demux->addStreamData(buffer, addSize);
}
#ifdef USE_FFMPEG
if (!m_pFormatCtx && m_pdata->getQueueSize() >= 2048) {
m_mpegheaderSize = m_pdata->get_front(m_mpegheader, sizeof(m_mpegheader));
int streamOffset = (int)(*(s32_be *)(m_mpegheader + 8));
if (streamOffset <= m_mpegheaderSize) {
m_mpegheaderSize = streamOffset;
m_pdata->pop_front(0, m_mpegheaderSize);
openContext();
}
}
#endif // USE_FFMPEG
// We added data, so... not the end anymore?
m_isVideoEnd = false;
}
return size;
}
bool MediaEngine::seekTo(s64 timestamp, int videoPixelMode) {
if (timestamp <= 0) {
return true;
}
// Just doing it the not so great way to be sure audio is in sync.
int timeout = 1000;
while (getVideoTimeStamp() < timestamp - 3003) {
if (getAudioTimeStamp() < getVideoTimeStamp() - 4180 * 2) {
getNextAudioFrame(NULL, NULL, NULL);
}
if (!stepVideo(videoPixelMode, true)) {
return false;
}
if (--timeout <= 0) {
return true;
}
}
while (getAudioTimeStamp() < getVideoTimeStamp() - 4180 * 2) {
if (getNextAudioFrame(NULL, NULL, NULL) == 0) {
return false;
}
if (--timeout <= 0) {
return true;
}
}
return true;
}
bool MediaEngine::setVideoStream(int streamNum, bool force) {
if (m_videoStream == streamNum && !force) {
// Yay, nothing to do.
return true;
}
#ifdef USE_FFMPEG
if (m_pFormatCtx && m_pCodecCtxs.find(streamNum) == m_pCodecCtxs.end()) {
// Get a pointer to the codec context for the video stream
if ((u32)streamNum >= m_pFormatCtx->nb_streams) {
return false;
}
AVStream *stream = m_pFormatCtx->streams[streamNum];
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)
AVCodec *pCodec = avcodec_find_decoder(stream->codecpar->codec_id);
if (!pCodec) {
WARN_LOG_REPORT(ME, "Could not find decoder for %d", (int)stream->codecpar->codec_id);
return false;
}
AVCodecContext *m_pCodecCtx = avcodec_alloc_context3(pCodec);
int paramResult = avcodec_parameters_to_context(m_pCodecCtx, stream->codecpar);
if (paramResult < 0) {
WARN_LOG_REPORT(ME, "Failed to prepare context parameters: %08x", paramResult);
return false;
}
#else
AVCodecContext *m_pCodecCtx = stream->codec;
// Find the decoder for the video stream
AVCodec *pCodec = avcodec_find_decoder(m_pCodecCtx->codec_id);
if (pCodec == nullptr) {
return false;
}
#endif
m_pCodecCtx->flags |= AV_CODEC_FLAG_OUTPUT_CORRUPT | AV_CODEC_FLAG_LOW_DELAY;
AVDictionary *opt = nullptr;
// Allow ffmpeg to use any number of threads it wants. Without this, it doesn't use threads.
av_dict_set(&opt, "threads", "0", 0);
int openResult = avcodec_open2(m_pCodecCtx, pCodec, &opt);
av_dict_free(&opt);
if (openResult < 0) {
return false;
}
m_pCodecCtxs[streamNum] = m_pCodecCtx;
}
#endif
m_videoStream = streamNum;
return true;
}
bool MediaEngine::setVideoDim(int width, int height)
{
#ifdef USE_FFMPEG
auto codecIter = m_pCodecCtxs.find(m_videoStream);
if (codecIter == m_pCodecCtxs.end())
return false;
AVCodecContext *m_pCodecCtx = codecIter->second;
if (width == 0 && height == 0)
{
// use the orignal video size
m_desWidth = m_pCodecCtx->width;
m_desHeight = m_pCodecCtx->height;
}
else
{
m_desWidth = width;
m_desHeight = height;
}
// Allocate video frame
if (!m_pFrame) {
m_pFrame = av_frame_alloc();
}
sws_freeContext(m_sws_ctx);
m_sws_ctx = NULL;
m_sws_fmt = -1;
if (m_desWidth == 0 || m_desHeight == 0) {
// Can't setup SWS yet, so stop for now.
return false;
}
updateSwsFormat(GE_CMODE_32BIT_ABGR8888);
// Allocate video frame for RGB24
m_pFrameRGB = av_frame_alloc();
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100)
int numBytes = av_image_get_buffer_size((AVPixelFormat)m_sws_fmt, m_desWidth, m_desHeight, 1);
#else
int numBytes = avpicture_get_size((AVPixelFormat)m_sws_fmt, m_desWidth, m_desHeight);
#endif
m_buffer = (u8*)av_malloc(numBytes * sizeof(uint8_t));
// Assign appropriate parts of buffer to image planes in m_pFrameRGB
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100)
av_image_fill_arrays(m_pFrameRGB->data, m_pFrameRGB->linesize, m_buffer, (AVPixelFormat)m_sws_fmt, m_desWidth, m_desHeight, 1);
#else
avpicture_fill((AVPicture *)m_pFrameRGB, m_buffer, (AVPixelFormat)m_sws_fmt, m_desWidth, m_desHeight);
#endif
#endif // USE_FFMPEG
return true;
}
void MediaEngine::updateSwsFormat(int videoPixelMode) {
#ifdef USE_FFMPEG
auto codecIter = m_pCodecCtxs.find(m_videoStream);
AVCodecContext *m_pCodecCtx = codecIter == m_pCodecCtxs.end() ? 0 : codecIter->second;
AVPixelFormat swsDesired = getSwsFormat(videoPixelMode);
if (swsDesired != m_sws_fmt && m_pCodecCtx != 0) {
m_sws_fmt = swsDesired;
m_sws_ctx = sws_getCachedContext
(
m_sws_ctx,
m_pCodecCtx->width,
m_pCodecCtx->height,
m_pCodecCtx->pix_fmt,
m_desWidth,
m_desHeight,
(AVPixelFormat)m_sws_fmt,
SWS_BILINEAR,
NULL,
NULL,
NULL
);
int *inv_coefficients;
int *coefficients;
int srcRange, dstRange;
int brightness, contrast, saturation;
if (sws_getColorspaceDetails(m_sws_ctx, &inv_coefficients, &srcRange, &coefficients, &dstRange, &brightness, &contrast, &saturation) != -1) {
srcRange = 0;
dstRange = 0;
sws_setColorspaceDetails(m_sws_ctx, inv_coefficients, srcRange, coefficients, dstRange, brightness, contrast, saturation);
}
}
#endif
}
bool MediaEngine::stepVideo(int videoPixelMode, bool skipFrame) {
#ifdef USE_FFMPEG
auto codecIter = m_pCodecCtxs.find(m_videoStream);
AVCodecContext *m_pCodecCtx = codecIter == m_pCodecCtxs.end() ? 0 : codecIter->second;
if (!m_pFormatCtx)
return false;
if (!m_pCodecCtx)
return false;
if (!m_pFrame)
return false;
AVPacket packet;
av_init_packet(&packet);
int frameFinished;
bool bGetFrame = false;
while (!bGetFrame) {
bool dataEnd = av_read_frame(m_pFormatCtx, &packet) < 0;
// Even if we've read all frames, some may have been re-ordered frames at the end.
// Still need to decode those, so keep calling avcodec_decode_video2() / avcodec_receive_frame().
if (dataEnd || packet.stream_index == m_videoStream) {
// avcodec_decode_video2() / avcodec_send_packet() gives us the re-ordered frames with a NULL packet.
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100)
if (dataEnd)
av_packet_unref(&packet);
#else
if (dataEnd)
av_free_packet(&packet);
#endif
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
if (packet.size != 0)
avcodec_send_packet(m_pCodecCtx, &packet);
int result = avcodec_receive_frame(m_pCodecCtx, m_pFrame);
if (result == 0) {
result = m_pFrame->pkt_size;
frameFinished = 1;
} else if (result == AVERROR(EAGAIN)) {
result = 0;
frameFinished = 0;
} else {
frameFinished = 0;
}
#else
int result = avcodec_decode_video2(m_pCodecCtx, m_pFrame, &frameFinished, &packet);
#endif
if (frameFinished) {
if (!m_pFrameRGB) {
setVideoDim();
}
if (m_pFrameRGB && !skipFrame) {
updateSwsFormat(videoPixelMode);
// TODO: Technically we could set this to frameWidth instead of m_desWidth for better perf.
// Update the linesize for the new format too. We started with the largest size, so it should fit.
m_pFrameRGB->linesize[0] = getPixelFormatBytes(videoPixelMode) * m_desWidth;
sws_scale(m_sws_ctx, m_pFrame->data, m_pFrame->linesize, 0,
m_pCodecCtx->height, m_pFrameRGB->data, m_pFrameRGB->linesize);
}
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(55, 58, 100)
int64_t bestPts = m_pFrame->best_effort_timestamp;
int64_t ptsDuration = m_pFrame->pkt_duration;
#else
int64_t bestPts = av_frame_get_best_effort_timestamp(m_pFrame);
int64_t ptsDuration = av_frame_get_pkt_duration(m_pFrame);
#endif
if (ptsDuration == 0) {
if (m_lastPts == bestPts - m_firstTimeStamp || bestPts == AV_NOPTS_VALUE) {
// TODO: Assuming 29.97 if missing.
m_videopts += 3003;
} else {
m_videopts = bestPts - m_firstTimeStamp;
m_lastPts = m_videopts;
}
} else if (bestPts != AV_NOPTS_VALUE) {
m_videopts = bestPts + ptsDuration - m_firstTimeStamp;
m_lastPts = m_videopts;
} else {
m_videopts += ptsDuration;
m_lastPts = m_videopts;
}
bGetFrame = true;
}
if (result <= 0 && dataEnd) {
// Sometimes, m_readSize is less than m_streamSize at the end, but not by much.
// This is kinda a hack, but the ringbuffer would have to be prematurely empty too.
m_isVideoEnd = !bGetFrame && (m_pdata->getQueueSize() == 0);
if (m_isVideoEnd)
m_decodingsize = 0;
break;
}
}
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100)
av_packet_unref(&packet);
#else
av_free_packet(&packet);
#endif
}
return bGetFrame;
#else
// If video engine is not available, just add to the timestamp at least.
m_videopts += 3003;
return true;
#endif // USE_FFMPEG
}
// Helpers that null out alpha (which seems to be the case on the PSP.)
// Some games depend on this, for example Sword Art Online (doesn't clear A's from buffer.)
inline void writeVideoLineRGBA(void *destp, const void *srcp, int width) {
// TODO: Use SSE/NEON, investigate why AV_PIX_FMT_RGB0 does not work.
u32_le *dest = (u32_le *)destp;
const u32_le *src = (u32_le *)srcp;
const u32 mask = 0x00FFFFFF;
for (int i = 0; i < width; ++i) {
dest[i] = src[i] & mask;
}
}
inline void writeVideoLineABGR5650(void *destp, const void *srcp, int width) {
memcpy(destp, srcp, width * sizeof(u16));
}
inline void writeVideoLineABGR5551(void *destp, const void *srcp, int width) {
// TODO: Use SSE/NEON.
u16_le *dest = (u16_le *)destp;
const u16_le *src = (u16_le *)srcp;
const u16 mask = 0x7FFF;
for (int i = 0; i < width; ++i) {
dest[i] = src[i] & mask;
}
}
inline void writeVideoLineABGR4444(void *destp, const void *srcp, int width) {
// TODO: Use SSE/NEON.
u16_le *dest = (u16_le *)destp;
const u16_le *src = (u16_le *)srcp;
const u16 mask = 0x0FFF;
for (int i = 0; i < width; ++i) {
dest[i] = src[i] & mask;
}
}
int MediaEngine::writeVideoImage(u32 bufferPtr, int frameWidth, int videoPixelMode) {
if (!Memory::IsValidAddress(bufferPtr) || frameWidth > 2048) {
// Clearly invalid values. Let's just not.
ERROR_LOG_REPORT(ME, "Ignoring invalid video decode address %08x/%x", bufferPtr, frameWidth);
return 0;
}
u8 *buffer = Memory::GetPointer(bufferPtr);
#ifdef USE_FFMPEG
if (!m_pFrame || !m_pFrameRGB)
return 0;
// lock the image size
int height = m_desHeight;
int width = m_desWidth;
u8 *imgbuf = buffer;
const u8 *data = m_pFrameRGB->data[0];
int videoLineSize = 0;
switch (videoPixelMode) {
case GE_CMODE_32BIT_ABGR8888:
videoLineSize = frameWidth * sizeof(u32);
break;
case GE_CMODE_16BIT_BGR5650:
case GE_CMODE_16BIT_ABGR5551:
case GE_CMODE_16BIT_ABGR4444:
videoLineSize = frameWidth * sizeof(u16);
break;
}
int videoImageSize = videoLineSize * height;
bool swizzle = Memory::IsVRAMAddress(bufferPtr) && (bufferPtr & 0x00200000) == 0x00200000;
if (swizzle) {
imgbuf = new u8[videoImageSize];
}
switch (videoPixelMode) {
case GE_CMODE_32BIT_ABGR8888:
for (int y = 0; y < height; y++) {
writeVideoLineRGBA(imgbuf + videoLineSize * y, data, width);
data += width * sizeof(u32);
}
break;
case GE_CMODE_16BIT_BGR5650:
for (int y = 0; y < height; y++) {
writeVideoLineABGR5650(imgbuf + videoLineSize * y, data, width);
data += width * sizeof(u16);
}
break;
case GE_CMODE_16BIT_ABGR5551:
for (int y = 0; y < height; y++) {
writeVideoLineABGR5551(imgbuf + videoLineSize * y, data, width);
data += width * sizeof(u16);
}
break;
case GE_CMODE_16BIT_ABGR4444:
for (int y = 0; y < height; y++) {
writeVideoLineABGR4444(imgbuf + videoLineSize * y, data, width);
data += width * sizeof(u16);
}
break;
default:
ERROR_LOG_REPORT(ME, "Unsupported video pixel format %d", videoPixelMode);
break;
}
if (swizzle) {
const int bxc = videoLineSize / 16;
int byc = (height + 7) / 8;
if (byc == 0)
byc = 1;
DoSwizzleTex16((const u32 *)imgbuf, buffer, bxc, byc, videoLineSize);
delete [] imgbuf;
}
NotifyMemInfo(MemBlockFlags::WRITE, bufferPtr, videoImageSize, "VideoDecode");
return videoImageSize;
#endif // USE_FFMPEG
return 0;
}
int MediaEngine::writeVideoImageWithRange(u32 bufferPtr, int frameWidth, int videoPixelMode,
int xpos, int ypos, int width, int height) {
if (!Memory::IsValidAddress(bufferPtr) || frameWidth > 2048) {
// Clearly invalid values. Let's just not.
ERROR_LOG_REPORT(ME, "Ignoring invalid video decode address %08x/%x", bufferPtr, frameWidth);
return 0;
}
u8 *buffer = Memory::GetPointer(bufferPtr);
#ifdef USE_FFMPEG
if (!m_pFrame || !m_pFrameRGB)
return 0;
// lock the image size
u8 *imgbuf = buffer;
const u8 *data = m_pFrameRGB->data[0];
int videoLineSize = 0;
switch (videoPixelMode) {
case GE_CMODE_32BIT_ABGR8888:
videoLineSize = frameWidth * sizeof(u32);
break;
case GE_CMODE_16BIT_BGR5650:
case GE_CMODE_16BIT_ABGR5551:
case GE_CMODE_16BIT_ABGR4444:
videoLineSize = frameWidth * sizeof(u16);
break;
}
int videoImageSize = videoLineSize * height;
bool swizzle = Memory::IsVRAMAddress(bufferPtr) && (bufferPtr & 0x00200000) == 0x00200000;
if (swizzle) {
imgbuf = new u8[videoImageSize];
}
if (width > m_desWidth - xpos)
width = m_desWidth - xpos;
if (height > m_desHeight - ypos)
height = m_desHeight - ypos;
switch (videoPixelMode) {
case GE_CMODE_32BIT_ABGR8888:
data += (ypos * m_desWidth + xpos) * sizeof(u32);
for (int y = 0; y < height; y++) {
writeVideoLineRGBA(imgbuf, data, width);
data += m_desWidth * sizeof(u32);
imgbuf += videoLineSize;
}
break;
case GE_CMODE_16BIT_BGR5650:
data += (ypos * m_desWidth + xpos) * sizeof(u16);
for (int y = 0; y < height; y++) {
writeVideoLineABGR5650(imgbuf, data, width);
data += m_desWidth * sizeof(u16);
imgbuf += videoLineSize;
}
break;
case GE_CMODE_16BIT_ABGR5551:
data += (ypos * m_desWidth + xpos) * sizeof(u16);
for (int y = 0; y < height; y++) {
writeVideoLineABGR5551(imgbuf, data, width);
data += m_desWidth * sizeof(u16);
imgbuf += videoLineSize;
}
break;
case GE_CMODE_16BIT_ABGR4444:
data += (ypos * m_desWidth + xpos) * sizeof(u16);
for (int y = 0; y < height; y++) {
writeVideoLineABGR4444(imgbuf, data, width);
data += m_desWidth * sizeof(u16);
imgbuf += videoLineSize;
}
break;
default:
ERROR_LOG_REPORT(ME, "Unsupported video pixel format %d", videoPixelMode);
break;
}
if (swizzle) {
WARN_LOG_REPORT_ONCE(vidswizzle, ME, "Swizzling Video with range");
const int bxc = videoLineSize / 16;
int byc = (height + 7) / 8;
if (byc == 0)
byc = 1;
DoSwizzleTex16((const u32 *)imgbuf, buffer, bxc, byc, videoLineSize);
delete [] imgbuf;
}
NotifyMemInfo(MemBlockFlags::WRITE, bufferPtr, videoImageSize, "VideoDecodeRange");
return videoImageSize;
#endif // USE_FFMPEG
return 0;
}
u8 *MediaEngine::getFrameImage() {
#ifdef USE_FFMPEG
return m_pFrameRGB->data[0];
#else
return NULL;
#endif
}
int MediaEngine::getRemainSize() {
if (!m_pdata)