[FFmpeg-devel] avcodec/libzvbi-teletextdec: propagate ERASE_PAGE flag for repeated subtitle page headers

Submitted by Marton Balint on June 10, 2018, 11:08 p.m.

Details

Message ID 20180610230837.28726-1-cus@passwd.hu
State Accepted
Commit b1e0e216462a989a39e7b413aef6d32f8cedc154
Headers show

Commit Message

Marton Balint June 10, 2018, 11:08 p.m.
This works around a libzvbi bug (a corner case in the teletext spec):

https://sourceforge.net/p/zapping/bugs/203/
https://sourceforge.net/p/zapping/patches/20/

Fixes samples/ffmpeg-bugs/trac/ticket2086/RBT_20100801_1835.ts.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 libavcodec/libzvbi-teletextdec.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

Comments

Marton Balint June 20, 2018, 8:43 p.m.
On Mon, 11 Jun 2018, Marton Balint wrote:

> This works around a libzvbi bug (a corner case in the teletext spec):
>
> https://sourceforge.net/p/zapping/bugs/203/
> https://sourceforge.net/p/zapping/patches/20/
>
> Fixes samples/ffmpeg-bugs/trac/ticket2086/RBT_20100801_1835.ts.
>
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
> libavcodec/libzvbi-teletextdec.c | 15 +++++++++++++++
> 1 file changed, 15 insertions(+)
>
> diff --git a/libavcodec/libzvbi-teletextdec.c b/libavcodec/libzvbi-teletextdec.c
> index ccded23e7c..809547545b 100644
> --- a/libavcodec/libzvbi-teletextdec.c
> +++ b/libavcodec/libzvbi-teletextdec.c
> @@ -74,6 +74,8 @@ typedef struct TeletextContext
>
>     int             readorder;
>     uint8_t         subtitle_map[2048];
> +    int             last_pgno;
> +    int             last_p5;
> } TeletextContext;
> 
> static int chop_spaces_utf8(const unsigned char* t, int len)
> @@ -372,6 +374,17 @@ static int slice_to_vbi_lines(TeletextContext *ctx, uint8_t* buf, int size)
>                         int pgno = ((pmag & 7) << 8) + page;
>                         // Check for disabled NEWSFLASH flag and enabled SUBTITLE and SUPRESS_HEADER flags
>                         ctx->subtitle_map[pgno] = (!(flags1 & 0x40) && flags1 & 0x80 && flags2 & 0x01);
> +                        // Propagate ERASE_PAGE flag for repeated page headers to work around a libzvbi bug
> +                        if (ctx->subtitle_map[pgno] && pgno == ctx->last_pgno) {
> +                            int last_byte9 = vbi_unham8(ctx->last_p5);
> +                            if (last_byte9 >= 0 && last_byte9 & 0x8) {
> +                                int byte9 = vbi_unham8(p[5]);
> +                                if (byte9 >= 0)
> +                                    p[5] = vbi_ham8(byte9 | 0x8);
> +                            }
> +                        }
> +                        ctx->last_pgno = pgno;
> +                        ctx->last_p5 = p[5];
>                     }
>                 }
>                 lines++;
> @@ -494,6 +507,7 @@ static int teletext_init_decoder(AVCodecContext *avctx)
>
>     ctx->vbi = NULL;
>     ctx->pts = AV_NOPTS_VALUE;
> +    ctx->last_pgno = -1;
>
>     if (ctx->opacity == -1)
>         ctx->opacity = ctx->transparent_bg ? 0 : 255;
> @@ -514,6 +528,7 @@ static int teletext_close_decoder(AVCodecContext *avctx)
>     vbi_decoder_delete(ctx->vbi);
>     ctx->vbi = NULL;
>     ctx->pts = AV_NOPTS_VALUE;
> +    ctx->last_pgno = -1;
>     memset(ctx->subtitle_map, 0, sizeof(ctx->subtitle_map));
>     if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
>         ctx->readorder = 0;
> --

Pushed.

Regards,
Marton

Patch hide | download patch | download mbox

diff --git a/libavcodec/libzvbi-teletextdec.c b/libavcodec/libzvbi-teletextdec.c
index ccded23e7c..809547545b 100644
--- a/libavcodec/libzvbi-teletextdec.c
+++ b/libavcodec/libzvbi-teletextdec.c
@@ -74,6 +74,8 @@  typedef struct TeletextContext
 
     int             readorder;
     uint8_t         subtitle_map[2048];
+    int             last_pgno;
+    int             last_p5;
 } TeletextContext;
 
 static int chop_spaces_utf8(const unsigned char* t, int len)
@@ -372,6 +374,17 @@  static int slice_to_vbi_lines(TeletextContext *ctx, uint8_t* buf, int size)
                         int pgno = ((pmag & 7) << 8) + page;
                         // Check for disabled NEWSFLASH flag and enabled SUBTITLE and SUPRESS_HEADER flags
                         ctx->subtitle_map[pgno] = (!(flags1 & 0x40) && flags1 & 0x80 && flags2 & 0x01);
+                        // Propagate ERASE_PAGE flag for repeated page headers to work around a libzvbi bug
+                        if (ctx->subtitle_map[pgno] && pgno == ctx->last_pgno) {
+                            int last_byte9 = vbi_unham8(ctx->last_p5);
+                            if (last_byte9 >= 0 && last_byte9 & 0x8) {
+                                int byte9 = vbi_unham8(p[5]);
+                                if (byte9 >= 0)
+                                    p[5] = vbi_ham8(byte9 | 0x8);
+                            }
+                        }
+                        ctx->last_pgno = pgno;
+                        ctx->last_p5 = p[5];
                     }
                 }
                 lines++;
@@ -494,6 +507,7 @@  static int teletext_init_decoder(AVCodecContext *avctx)
 
     ctx->vbi = NULL;
     ctx->pts = AV_NOPTS_VALUE;
+    ctx->last_pgno = -1;
 
     if (ctx->opacity == -1)
         ctx->opacity = ctx->transparent_bg ? 0 : 255;
@@ -514,6 +528,7 @@  static int teletext_close_decoder(AVCodecContext *avctx)
     vbi_decoder_delete(ctx->vbi);
     ctx->vbi = NULL;
     ctx->pts = AV_NOPTS_VALUE;
+    ctx->last_pgno = -1;
     memset(ctx->subtitle_map, 0, sizeof(ctx->subtitle_map));
     if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
         ctx->readorder = 0;