[FFmpeg-devel] [PATCH] movtextdec.c: Add support for highlight and hilightcolor box

Clément Bœsch u at pkh.me
Mon Jul 6 23:42:39 CEST 2015


On Mon, Jul 06, 2015 at 10:27:47PM +0530, Niklesh Lalwani wrote:
> From: Niklesh <niklesh.lalwani at iitb.ac.in>
> 
> Signed-off-by: Niklesh <niklesh.lalwani at iitb.ac.in>
> ---
>  libavcodec/movtextdec.c | 254 +++++++++++++++++++++++++++++++++---------------
>  1 file changed, 173 insertions(+), 81 deletions(-)
> 
> diff --git a/libavcodec/movtextdec.c b/libavcodec/movtextdec.c
> index a3afd91..7e9e4f5 100644
> --- a/libavcodec/movtextdec.c
> +++ b/libavcodec/movtextdec.c
> @@ -31,37 +31,166 @@
>  #define STYLE_FLAG_ITALIC       (1<<1)
>  #define STYLE_FLAG_UNDERLINE    (1<<2)
>  
> +#define STYL_BOX   (1<<0)
> +#define HLIT_BOX   (1<<1)
> +#define HCLR_BOX   (1<<2)
> +
>  typedef struct {
>      uint16_t style_start;
>      uint16_t style_end;
>      uint8_t style_flag;
>  } StyleBox;
>  
> +typedef struct {
> +    uint16_t hlit_start;
> +    uint16_t hlit_end;
> +} HighlightBox;
> +
> +typedef struct {
> +   uint8_t hlit_color[4];
> +} HilightcolorBox;
> +
> +typedef struct {
> +    StyleBox **s;
> +    StyleBox *s_temp;
> +    HighlightBox h;
> +    HilightcolorBox c;
> +    uint8_t box_flags;
> +    uint16_t style_entries;
> +    uint64_t tracksize;
> +    int size_var;
> +    int count_s;
> +} MovTextContext;
> +

> +struct Box
> +{

style

> +    uint32_t type;
> +    size_t base_size;
> +    int (*decode)(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt);
> +};
> +
> +static void mov_text_cleanup(MovTextContext *m)
> +{
> +    int i;
> +    if (m->box_flags & STYL_BOX) {
> +        for(i = 0; i < m->count_s; i++) {
> +            av_freep(&m->s[i]);
> +        }
> +        av_freep(&m->s);
> +    }
> +}
> +
> +static int decode_hlit(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
> +{
> +    m->box_flags |= HLIT_BOX;
> +    m->h.hlit_start = AV_RB16(tsmb);
> +    tsmb += 2;
> +    m->h.hlit_end = AV_RB16(tsmb);
> +    tsmb += 2;
> +    return 0;
> +}
> +
> +static int decode_hclr(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
> +{
> +    m->box_flags |= HCLR_BOX;

> +    m->c.hlit_color[0] = *tsmb++;
> +    m->c.hlit_color[1] = *tsmb++;
> +    m->c.hlit_color[2] = *tsmb++;
> +    m->c.hlit_color[3] = *tsmb++;

you can use memcpy here it seems

> +    return 0;
> +}
> +
> +static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
> +{
> +    int i;
> +    m->style_entries = AV_RB16(tsmb);
> +    tsmb += 2;
> +    // A single style record is of length 12 bytes.
> +    if (m->tracksize + m->size_var + 2 + m->style_entries * 12 > avpkt->size)
> +        return -1;
> +
> +    m->box_flags |= STYL_BOX;
> +    for(i = 0; i < m->style_entries; i++) {

> +        m->s_temp = av_malloc(sizeof(StyleBox));

sizeof(*m->s_temp)

> +        if (!m->s_temp) {
> +            mov_text_cleanup(m);
> +            return AVERROR(ENOMEM);
> +        }
> +        m->s_temp->style_start = AV_RB16(tsmb);
> +        tsmb += 2;
> +        m->s_temp->style_end = AV_RB16(tsmb);
> +        tsmb += 2;
> +        // fontID = AV_RB16(tsmb);
> +        tsmb += 2;
> +        m->s_temp->style_flag = AV_RB8(tsmb);
> +        av_dynarray_add(&m->s, &m->count_s, m->s_temp);
> +        if(!m->s) {
> +            mov_text_cleanup(m);
> +            return AVERROR(ENOMEM);
> +        }
> +        // fontsize = AV_RB8(tsmb);
> +        tsmb += 2;
> +        // text-color-rgba
> +        tsmb += 4;
> +    }
> +    return 0;
> +}
> +
> +struct Box box_types[] = {

static const

> +    { MKBETAG('s','t','y','l'), 2, decode_styl },
> +    { MKBETAG('h','l','i','t'), 4, decode_hlit },
> +    { MKBETAG('h','c','l','r'), 4, decode_hclr }
> +};
> +

> +const static size_t box_count = sizeof(box_types) / sizeof(struct Box);

use FF_ARRAY_ELEMS() in the only place it's used instead

> +
>  static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
> -                        StyleBox **s, int style_entries)
> +                        MovTextContext *m)

nit: vertical align

>  {
>      int i = 0;
>      int text_pos = 0;
>      while (text < text_end) {
> -        for (i = 0; i < style_entries; i++) {
> -            if (s[i]->style_flag && text_pos == s[i]->style_end) {
> -                if (s[i]->style_flag & STYLE_FLAG_BOLD)
> -                    av_bprintf(buf, "{\\b0}");
> -                if (s[i]->style_flag & STYLE_FLAG_ITALIC)
> -                    av_bprintf(buf, "{\\i0}");
> -                if (s[i]->style_flag & STYLE_FLAG_UNDERLINE)
> -                    av_bprintf(buf, "{\\u0}");
> +        if (m->box_flags & STYL_BOX) {

> +            for (i = 0; i < m->style_entries; i++) {
> +                if (m->s[i]->style_flag && text_pos == m->s[i]->style_end) {
> +                    if (m->s[i]->style_flag & STYLE_FLAG_BOLD)
> +                        av_bprintf(buf, "{\\b0}");
> +                    if (m->s[i]->style_flag & STYLE_FLAG_ITALIC)
> +                        av_bprintf(buf, "{\\i0}");
> +                    if (m->s[i]->style_flag & STYLE_FLAG_UNDERLINE)
> +                        av_bprintf(buf, "{\\u0}");
> +                }
> +            }

keep this block not re-idented (and eventually add a /* TODO: reindent */
above) to keep the diff small and ease review. You can re-indent in a
later commit.

ditto below if applicable.

> +            for (i = 0; i < m->style_entries; i++) {
> +                if (m->s[i]->style_flag && text_pos == m->s[i]->style_start) {
> +                    if (m->s[i]->style_flag & STYLE_FLAG_BOLD)
> +                        av_bprintf(buf, "{\\b1}");
> +                    if (m->s[i]->style_flag & STYLE_FLAG_ITALIC)
> +                        av_bprintf(buf, "{\\i1}");
> +                    if (m->s[i]->style_flag & STYLE_FLAG_UNDERLINE)
> +                        av_bprintf(buf, "{\\u1}");
> +                }
>              }
>          }
> -
> -        for (i = 0; i < style_entries; i++) {
> -            if (s[i]->style_flag && text_pos == s[i]->style_start) {
> -                if (s[i]->style_flag & STYLE_FLAG_BOLD)
> -                    av_bprintf(buf, "{\\b1}");
> -                if (s[i]->style_flag & STYLE_FLAG_ITALIC)
> -                    av_bprintf(buf, "{\\i1}");
> -                if (s[i]->style_flag & STYLE_FLAG_UNDERLINE)
> -                    av_bprintf(buf, "{\\u1}");
> +        if (m->box_flags & HLIT_BOX) {
> +            if (text_pos == m->h.hlit_start) {
> +                /* If hclr box is present, set the secondary color to the color specified.
> +                 * Otherwise, set primary color to white and secondary color to black.
> +                 * These colors will come from TextSampleModifier boxes in future
> +                 * and inverse video technique for highlight will be implemented.
> +                */

> +                if (m->box_flags & HCLR_BOX) {
> +                    av_bprintf(buf, "{\\2c&H%02x%02x%02x&}", m->c.hlit_color[2], m->c.hlit_color[1], m->c.hlit_color[0]);
> +                } else {
> +                        av_bprintf(buf, "{\\1c&H000000&}{\\2c&HFFFFFF}");

wrong indent

> +                }
> +            }
> +            if (text_pos == m->h.hlit_end) {
> +                if (m->box_flags & HCLR_BOX) {
> +                    av_bprintf(buf, "{\\2c&H000000}");
> +                } else {
> +                    av_bprintf(buf, "{\\1c&HFFFFFF&}{\\2c&H000000}");
> +                }
>              }
>          }
>  
> @@ -78,7 +207,6 @@ static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
>          text++;
>          text_pos++;
>      }

> -

nit: unrelated changee

>      return 0;
>  }
>  
[...]

-- 
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 473 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20150706/af8e6292/attachment.sig>


More information about the ffmpeg-devel mailing list