[FFmpeg-devel] [PATCH 03/10] libavcodec/ccaption_dec: add calculate_duration option

wm4 nfxjfg at googlemail.com
Wed Jan 6 11:37:22 CET 2016


On Tue,  5 Jan 2016 23:41:35 -0800
Aman Gupta <ffmpeg at tmm1.net> wrote:

> From: Aman Gupta <aman at tmm1.net>
> 
> new option defaults to true, to preserve existing behavior. by flipping
> the option to false, subtitle events are emitted as soon as they are
> received and have an infinite duration.
> 
> this new mode is useful for realtime decoding of closed captions so they
> can be display along with decoded mpeg2 frames.
> ---
>  libavcodec/ccaption_dec.c | 19 ++++++++++++++++---
>  1 file changed, 16 insertions(+), 3 deletions(-)
> 
> diff --git a/libavcodec/ccaption_dec.c b/libavcodec/ccaption_dec.c
> index f651c88..9f17e77 100644
> --- a/libavcodec/ccaption_dec.c
> +++ b/libavcodec/ccaption_dec.c
> @@ -150,6 +150,7 @@ struct Screen {
>  
>  typedef struct CCaptionSubContext {
>      AVClass *class;
> +    int calculate_duration;
>      struct Screen screen[2];
>      int active_screen;
>      uint8_t cursor_row;
> @@ -545,8 +546,12 @@ static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avp
>              continue;
>          else
>              process_cc608(ctx, *(bptr + i + 1) & 0x7f, *(bptr + i + 2) & 0x7f);
> -        if (ctx->screen_changed)
> -        {
> +
> +        if (!ctx->screen_changed)
> +            continue;
> +        ctx->screen_changed = 0;
> +
> +        if (ctx->calculate_duration) {
>              if (ctx->prev_string) {
>                  int start_time = av_rescale_q(ctx->prev_time, avctx->time_base, (AVRational){ 1, 100 });
>                  int end_time = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){ 1, 100 });
> @@ -560,7 +565,12 @@ static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avp
>              av_bprintf(&ctx->buffer, "\r\n");
>              ctx->prev_string = av_strdup(ctx->buffer.str);
>              ctx->prev_time = avpkt->pts;
> -            ctx->screen_changed = 0;
> +        } else {
> +            int start_time = av_rescale_q(avpkt->pts, avctx->time_base, (AVRational){ 1, 100 });
> +            ret = ff_ass_add_rect_bprint(sub, &ctx->buffer, start_time, -1);
> +            if (ret < 0)
> +                return ret;
> +            sub->pts = av_rescale_q(avpkt->pts, avctx->time_base, AV_TIME_BASE_Q);
>          }
>      }
>  
> @@ -568,7 +578,10 @@ static int decode(AVCodecContext *avctx, void *data, int *got_sub, AVPacket *avp
>      return ret;
>  }
>  
> +#define OFFSET(x) offsetof(CCaptionSubContext, x)
> +#define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
>  static const AVOption options[] = {
> +    { "calculate_duration", "Buffer closed captions to calculate durations.", OFFSET(calculate_duration), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, SD },
>      {NULL}
>  };
>  

I want to note that this is actually the only way to get realtime
closed captions (playback, or even just transcoding with rendering
subtitles to the video). Without this, the API user would have to
buffer video and audio frames until the closed caption even is "done".

Returning events with infinite duration is a hack and a weakness in the
API. These events can't be used with libass in a simple way. Instead,
the API user has to adjust or delete previous events manually. Not
really ideal.

I'm not sure what a better API could look like.


More information about the ffmpeg-devel mailing list