summaryrefslogtreecommitdiffstats
path: root/sub/sd_lavc.c
diff options
context:
space:
mode:
authorPhilip Langdale <philipl@overt.org>2022-11-29 11:15:16 -0800
committerPhilip Langdale <github.philipl@overt.org>2022-12-03 14:44:18 -0800
commit4574dd5dc6ff75b1fc693afceec59fbcd51ccd4c (patch)
tree680ea541592dea8e356809e5dbdd5265781d72fe /sub/sd_lavc.c
parent77e7f5de2c01361a88c501fcd78b51c2fe2d9df0 (diff)
downloadmpv-4574dd5dc6ff75b1fc693afceec59fbcd51ccd4c.tar.bz2
mpv-4574dd5dc6ff75b1fc693afceec59fbcd51ccd4c.tar.xz
ffmpeg: update to handle deprecation of `av_init_packet`
This has been a long standing annoyance - ffmpeg is removing sizeof(AVPacket) from the API which means you cannot stack-allocate AVPacket anymore. However, that is something we take advantage of because we use short-lived AVPackets to bridge from native mpv packets in our main decoding paths. We don't think that switching these to `av_packet_alloc` is desirable, given the cost of heap allocation, so this change takes a different approach - allocating a single packet in the relevant context and reusing it over and over. That's fairly straight-forward, with the main caveat being that re-initialising the packet is unintuitive. There is no function that does exactly what we need (what `av_init_packet` did). The closest is `av_packet_unref`, which additionally frees buffers and side-data. However, we don't copy those things - we just assign them in from our own packet, so we have to explicitly clear the pointers before calling `av_packet_unref`. But at least we can make a wrapper function for that. The weirdest part of the change is the handling of the vtt subtitle conversion. This requires two packets, so I had to pre-allocate two in the context struct. That sounds excessive, but if allocating the primary packet is too expensive, then allocating the secondary one for vtt subtitles must also be too expensive. This change is not conditional as heap allocated AVPackets were available for years and years before the deprecation.
Diffstat (limited to 'sub/sd_lavc.c')
-rw-r--r--sub/sd_lavc.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/sub/sd_lavc.c b/sub/sd_lavc.c
index ce0ef11a0a..7b0d0dfe38 100644
--- a/sub/sd_lavc.c
+++ b/sub/sd_lavc.c
@@ -59,6 +59,7 @@ struct seekpoint {
struct sd_lavc_priv {
AVCodecContext *avctx;
+ AVPacket *avpkt;
AVRational pkt_timebase;
struct sub subs[MAX_QUEUE]; // most recent event first
struct sub_bitmap *outbitmaps;
@@ -97,6 +98,9 @@ static int init(struct sd *sd)
ctx = avcodec_alloc_context3(sub_codec);
if (!ctx)
goto error;
+ priv->avpkt = av_packet_alloc();
+ if (!priv->avpkt)
+ goto error;
mp_lavc_set_extradata(ctx, sd->codec->extradata, sd->codec->extradata_size);
priv->pkt_timebase = mp_get_codec_timebase(sd->codec);
ctx->pkt_timebase = priv->pkt_timebase;
@@ -112,6 +116,7 @@ static int init(struct sd *sd)
error:
MP_FATAL(sd, "Could not open libavcodec subtitle decoder\n");
avcodec_free_context(&ctx);
+ mp_free_av_packet(&priv->avpkt);
talloc_free(priv);
return -1;
}
@@ -298,7 +303,6 @@ static void decode(struct sd *sd, struct demux_packet *packet)
double endpts = MP_NOPTS_VALUE;
double duration = packet->duration;
AVSubtitle sub;
- AVPacket pkt;
// libavformat sets duration==0, even if the duration is unknown. Some files
// also have actually subtitle packets with duration explicitly set to 0
@@ -311,7 +315,7 @@ static void decode(struct sd *sd, struct demux_packet *packet)
if (pts == MP_NOPTS_VALUE)
MP_WARN(sd, "Subtitle with unknown start time.\n");
- mp_set_av_packet(&pkt, packet, &priv->pkt_timebase);
+ mp_set_av_packet(priv->avpkt, packet, &priv->pkt_timebase);
if (ctx->codec_id == AV_CODEC_ID_DVB_TELETEXT) {
char page[4];
@@ -320,7 +324,7 @@ static void decode(struct sd *sd, struct demux_packet *packet)
}
int got_sub;
- int res = avcodec_decode_subtitle2(ctx, &sub, &got_sub, &pkt);
+ int res = avcodec_decode_subtitle2(ctx, &sub, &got_sub, priv->avpkt);
if (res < 0 || !got_sub)
return;
@@ -588,6 +592,7 @@ static void uninit(struct sd *sd)
for (int n = 0; n < MAX_QUEUE; n++)
clear_sub(&priv->subs[n]);
avcodec_free_context(&priv->avctx);
+ mp_free_av_packet(&priv->avpkt);
talloc_free(priv);
}