diff options
Diffstat (limited to 'sub')
-rw-r--r-- | sub/lavc_conv.c | 28 | ||||
-rw-r--r-- | sub/sd_lavc.c | 11 |
2 files changed, 27 insertions, 12 deletions
diff --git a/sub/lavc_conv.c b/sub/lavc_conv.c index d9f628492c..8e26c41237 100644 --- a/sub/lavc_conv.c +++ b/sub/lavc_conv.c @@ -34,6 +34,8 @@ struct lavc_conv { struct mp_log *log; AVCodecContext *avctx; + AVPacket *avpkt; + AVPacket *avpkt_vtt; char *codec; char *extradata; AVSubtitle cur; @@ -84,6 +86,11 @@ struct lavc_conv *lavc_conv_create(struct mp_log *log, const char *codec_name, if (mp_lavc_set_extradata(avctx, extradata, extradata_len) < 0) goto error; + priv->avpkt = av_packet_alloc(); + priv->avpkt_vtt = av_packet_alloc(); + if (!priv->avpkt || !priv->avpkt_vtt) + goto error; + #if LIBAVCODEC_VERSION_MAJOR < 59 av_dict_set(&opts, "sub_text_format", "ass", 0); #endif @@ -107,6 +114,8 @@ struct lavc_conv *lavc_conv_create(struct mp_log *log, const char *codec_name, MP_FATAL(priv, "Could not open libavcodec subtitle converter\n"); av_dict_free(&opts); av_free(avctx); + mp_free_av_packet(&priv->avpkt); + mp_free_av_packet(&priv->avpkt_vtt); talloc_free(priv); return NULL; } @@ -224,26 +233,25 @@ char **lavc_conv_decode(struct lavc_conv *priv, struct demux_packet *packet, double *sub_pts, double *sub_duration) { AVCodecContext *avctx = priv->avctx; - AVPacket pkt; - AVPacket parsed_pkt = {0}; + AVPacket *curr_pkt = priv->avpkt; int ret, got_sub; int num_cur = 0; avsubtitle_free(&priv->cur); - mp_set_av_packet(&pkt, packet, &avctx->time_base); - if (pkt.pts < 0) - pkt.pts = 0; + mp_set_av_packet(priv->avpkt, packet, &avctx->time_base); + if (priv->avpkt->pts < 0) + priv->avpkt->pts = 0; if (strcmp(priv->codec, "webvtt-webm") == 0) { - if (parse_webvtt(&pkt, &parsed_pkt) < 0) { + if (parse_webvtt(priv->avpkt, priv->avpkt_vtt) < 0) { MP_ERR(priv, "Error parsing subtitle\n"); goto done; } - pkt = parsed_pkt; + curr_pkt = priv->avpkt_vtt; } - ret = avcodec_decode_subtitle2(avctx, &priv->cur, &got_sub, &pkt); + ret = avcodec_decode_subtitle2(avctx, &priv->cur, &got_sub, curr_pkt); if (ret < 0) { MP_ERR(priv, "Error decoding subtitle\n"); } else if (got_sub) { @@ -266,7 +274,7 @@ char **lavc_conv_decode(struct lavc_conv *priv, struct demux_packet *packet, } done: - av_packet_unref(&parsed_pkt); + av_packet_unref(priv->avpkt_vtt); MP_TARRAY_APPEND(priv, priv->cur_list, num_cur, NULL); return priv->cur_list; } @@ -280,5 +288,7 @@ void lavc_conv_uninit(struct lavc_conv *priv) { avsubtitle_free(&priv->cur); avcodec_free_context(&priv->avctx); + mp_free_av_packet(&priv->avpkt); + mp_free_av_packet(&priv->avpkt_vtt); talloc_free(priv); } 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); } |