Skip to content

Commit be7623c

Browse files
committed
FFMPEG Core - Video Decoding Modification
I changed the way video decoding works slightly in the FFMPEG core to, hopefully, enable CODECs that don't play well--such as AV1 in my experience--with the current strategy. The changes I've made have been done to more precisely follow the recommendations of the FFMPEG developers with respect to swapping between sending packets and receiving frames.
1 parent 8b53192 commit be7623c

File tree

1 file changed

+57
-45
lines changed

1 file changed

+57
-45
lines changed

cores/libretro-ffmpeg/ffmpeg_core.c

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,11 +1570,12 @@ static void sws_worker_thread(void *arg)
15701570
}
15711571

15721572
#ifdef HAVE_SSA
1573-
static void decode_video(AVCodecContext *ctx, AVPacket *pkt, size_t frame_size, ASS_Track *ass_track_active)
1573+
static int decode_video(AVCodecContext *ctx, AVPacket *pkt, size_t frame_size, ASS_Track *ass_track_active)
15741574
#else
1575-
static void decode_video(AVCodecContext *ctx, AVPacket *pkt, size_t frame_size)
1575+
static int decode_video(AVCodecContext *ctx, AVPacket *pkt, size_t frame_size)
15761576
#endif
15771577
{
1578+
static bool receiving = false;
15781579
int ret = 0;
15791580
video_decoder_context_t *decoder_ctx = NULL;
15801581

@@ -1584,72 +1585,83 @@ static void decode_video(AVCodecContext *ctx, AVPacket *pkt, size_t frame_size)
15841585
if (main_sleeping)
15851586
{
15861587
if (!do_seek)
1588+
{
15871589
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Thread: Video deadlock detected.\n");
1590+
return -1;
1591+
}
15881592
tpool_wait(tpool);
15891593
video_buffer_clear(video_buffer);
1590-
return;
1594+
return 1;
15911595
}
15921596
}
15931597

1594-
if ((ret = avcodec_send_packet(ctx, pkt)) < 0)
1595-
{
1596-
#ifdef __cplusplus
1597-
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Can't decode video packet: %d\n", ret);
1598-
#else
1599-
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Can't decode video packet: %s\n", av_err2str(ret));
1600-
#endif
1601-
return;
1602-
}
1603-
1604-
while (!decode_thread_dead && video_buffer_has_open_slot(video_buffer))
1598+
if (!receiving)
16051599
{
1606-
video_buffer_get_open_slot(video_buffer, &decoder_ctx);
1600+
ret = avcodec_send_packet(ctx, pkt);
16071601

1608-
ret = avcodec_receive_frame(ctx, decoder_ctx->source);
1609-
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
1602+
if (ret == AVERROR(EAGAIN) || ret >= 0)
16101603
{
1611-
ret = -42;
1612-
goto end;
1604+
receiving = true;
1605+
return 1;
16131606
}
16141607
else if (ret < 0)
16151608
{
16161609
#ifdef __cplusplus
1617-
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Error while reading video frame: %d\n", ret);
1610+
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Can't decode video packet: %d\n", ret);
16181611
#else
1619-
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Error while reading video frame: %s\n", av_err2str(ret));
1612+
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Can't decode video packet: %s\n", av_err2str(ret));
16201613
#endif
1621-
goto end;
1614+
return -1;
16221615
}
1616+
}
1617+
else
1618+
{
1619+
if (video_buffer_has_open_slot(video_buffer))
1620+
{
1621+
video_buffer_get_open_slot(video_buffer, &decoder_ctx);
16231622

1624-
#if ENABLE_HW_ACCEL
1625-
if (hw_decoding_enabled)
1626-
/* Copy data from VRAM to RAM */
1627-
if ((ret = av_hwframe_transfer_data(decoder_ctx->hw_source, decoder_ctx->source, 0)) < 0)
1623+
ret = avcodec_receive_frame(ctx, decoder_ctx->source);
1624+
1625+
if (ret >= 0)
1626+
{
1627+
#ifdef HAVE_SSA
1628+
decoder_ctx->ass_track_active = ass_track_active;
1629+
#endif
1630+
tpool_add_work(tpool, sws_worker_thread, decoder_ctx);
1631+
return 1;
1632+
}
1633+
else if (ret == AVERROR(EAGAIN))
16281634
{
1635+
#if ENABLE_HW_ACCEL
1636+
if (hw_decoding_enabled)
1637+
/* Copy data from VRAM to RAM */
1638+
if ((ret = av_hwframe_transfer_data(decoder_ctx->hw_source, decoder_ctx->source, 0)) < 0)
1639+
{
16291640
#ifdef __cplusplus
1630-
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Error transferring the data to system memory: %d\n", ret);
1641+
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Error transferring the data to system memory: %d\n", ret);
16311642
#else
1632-
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Error transferring the data to system memory: %s\n", av_err2str(ret));
1643+
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Error transferring the data to system memory: %s\n", av_err2str(ret));
16331644
#endif
1634-
goto end;
1635-
}
1645+
}
16361646
#endif
16371647

1638-
#ifdef HAVE_SSA
1639-
decoder_ctx->ass_track_active = ass_track_active;
1648+
video_buffer_return_open_slot(video_buffer, decoder_ctx);
1649+
receiving = false;
1650+
return 0;
1651+
}
1652+
else if (ret < 0)
1653+
{
1654+
#ifdef __cplusplus
1655+
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Error while reading video frame: %d\n", ret);
1656+
#else
1657+
log_cb(RETRO_LOG_ERROR, "[FFMPEG] Error while reading video frame: %s\n", av_err2str(ret));
16401658
#endif
1641-
1642-
tpool_add_work(tpool, sws_worker_thread, decoder_ctx);
1643-
1644-
end:
1645-
if (ret < 0)
1646-
{
1647-
video_buffer_return_open_slot(video_buffer, decoder_ctx);
1648-
break;
1659+
return -1;
1660+
}
16491661
}
16501662
}
16511663

1652-
return;
1664+
return 0;
16531665
}
16541666

16551667
static int16_t *decode_audio(AVCodecContext *ctx, AVPacket *pkt,
@@ -1922,9 +1934,9 @@ static void decode_thread(void *data)
19221934
packet_buffer_get_packet(video_packet_buffer, pkt);
19231935

19241936
#ifdef HAVE_SSA
1925-
decode_video(vctx, pkt, frame_size, ass_track_active);
1937+
while(decode_video(vctx, pkt, frame_size, ass_track_active) > 0);
19261938
#else
1927-
decode_video(vctx, pkt, frame_size);
1939+
while(decode_video(vctx, pkt, frame_size) > 0);
19281940
#endif
19291941

19301942
av_packet_unref(pkt);
@@ -2231,8 +2243,8 @@ bool CORE_PREFIX(retro_load_game)(const struct retro_game_info *info)
22312243
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_RIGHT, "Increment Subtitle Index" },
22322244
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_UP, "Increment Audio Index" },
22332245
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_DOWN, "Decrement Audio Index" },
2234-
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "Seek -60 seconds" },
2235-
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Seek +60 seconds" },
2246+
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L, "Seek -30 seconds" },
2247+
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R, "Seek +30 seconds" },
22362248
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_L2, "Seek Decrementally" },
22372249
{ 0, RETRO_DEVICE_JOYPAD, 0, RETRO_DEVICE_ID_JOYPAD_R2, "Seek Incrementally" },
22382250

0 commit comments

Comments
 (0)