Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * 3GPP TS 26.245 Timed Text decoder
  3.  * Copyright (c) 2012  Philip Langdale <philipl@overt.org>
  4.  *
  5.  * This file is part of FFmpeg.
  6.  *
  7.  * FFmpeg is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * FFmpeg is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with FFmpeg; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20.  */
  21.  
  22. #include "avcodec.h"
  23. #include "ass.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/bprint.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/mem.h"
  29.  
  30. #define STYLE_FLAG_BOLD         (1<<0)
  31. #define STYLE_FLAG_ITALIC       (1<<1)
  32. #define STYLE_FLAG_UNDERLINE    (1<<2)
  33.  
  34. #define BOX_SIZE_INITIAL    40
  35.  
  36. #define STYL_BOX   (1<<0)
  37. #define HLIT_BOX   (1<<1)
  38. #define HCLR_BOX   (1<<2)
  39. #define TWRP_BOX   (1<<3)
  40.  
  41. #define BOTTOM_LEFT     1
  42. #define BOTTOM_CENTER   2
  43. #define BOTTOM_RIGHT    3
  44. #define MIDDLE_LEFT     4
  45. #define MIDDLE_CENTER   5
  46. #define MIDDLE_RIGHT    6
  47. #define TOP_LEFT        7
  48. #define TOP_CENTER      8
  49. #define TOP_RIGHT       9
  50.  
  51. typedef struct {
  52.     char *font;
  53.     int fontsize;
  54.     int color;
  55.     int back_color;
  56.     int bold;
  57.     int italic;
  58.     int underline;
  59.     int alignment;
  60. } MovTextDefault;
  61.  
  62. typedef struct {
  63.     uint16_t fontID;
  64.     char *font;
  65. } FontRecord;
  66.  
  67. typedef struct {
  68.     uint16_t style_start;
  69.     uint16_t style_end;
  70.     uint8_t style_flag;
  71.     uint8_t fontsize;
  72.     uint16_t style_fontID;
  73. } StyleBox;
  74.  
  75. typedef struct {
  76.     uint16_t hlit_start;
  77.     uint16_t hlit_end;
  78. } HighlightBox;
  79.  
  80. typedef struct {
  81.    uint8_t hlit_color[4];
  82. } HilightcolorBox;
  83.  
  84. typedef struct {
  85.     uint8_t wrap_flag;
  86. } TextWrapBox;
  87.  
  88. typedef struct {
  89.     StyleBox **s;
  90.     StyleBox *s_temp;
  91.     HighlightBox h;
  92.     HilightcolorBox c;
  93.     FontRecord **ftab;
  94.     FontRecord *ftab_temp;
  95.     TextWrapBox w;
  96.     MovTextDefault d;
  97.     uint8_t box_flags;
  98.     uint16_t style_entries, ftab_entries;
  99.     uint64_t tracksize;
  100.     int size_var;
  101.     int count_s, count_f;
  102. } MovTextContext;
  103.  
  104. typedef struct {
  105.     uint32_t type;
  106.     size_t base_size;
  107.     int (*decode)(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt);
  108. } Box;
  109.  
  110. static void mov_text_cleanup(MovTextContext *m)
  111. {
  112.     int i;
  113.     if (m->box_flags & STYL_BOX) {
  114.         for(i = 0; i < m->count_s; i++) {
  115.             av_freep(&m->s[i]);
  116.         }
  117.         av_freep(&m->s);
  118.     }
  119. }
  120.  
  121. static void mov_text_cleanup_ftab(MovTextContext *m)
  122. {
  123.     int i;
  124.     if (m->ftab_temp)
  125.         av_freep(&m->ftab_temp->font);
  126.     av_freep(&m->ftab_temp);
  127.     if (m->ftab) {
  128.         for(i = 0; i < m->count_f; i++) {
  129.             av_freep(&m->ftab[i]->font);
  130.             av_freep(&m->ftab[i]);
  131.         }
  132.     }
  133.     av_freep(&m->ftab);
  134. }
  135.  
  136. static int mov_text_tx3g(AVCodecContext *avctx, MovTextContext *m)
  137. {
  138.     uint8_t *tx3g_ptr = avctx->extradata;
  139.     int i, box_size, font_length;
  140.     int8_t v_align, h_align;
  141.     int style_fontID;
  142.     StyleBox s_default;
  143.  
  144.     m->count_f = 0;
  145.     m->ftab_entries = 0;
  146.     box_size = BOX_SIZE_INITIAL; /* Size till ftab_entries */
  147.     if (avctx->extradata_size < box_size)
  148.         return -1;
  149.  
  150.     // Display Flags
  151.     tx3g_ptr += 4;
  152.     // Alignment
  153.     h_align = *tx3g_ptr++;
  154.     v_align = *tx3g_ptr++;
  155.     if (h_align == 0) {
  156.         if (v_align == 0)
  157.             m->d.alignment = TOP_LEFT;
  158.         if (v_align == 1)
  159.             m->d.alignment = MIDDLE_LEFT;
  160.         if (v_align == -1)
  161.             m->d.alignment = BOTTOM_LEFT;
  162.     }
  163.     if (h_align == 1) {
  164.         if (v_align == 0)
  165.             m->d.alignment = TOP_CENTER;
  166.         if (v_align == 1)
  167.             m->d.alignment = MIDDLE_CENTER;
  168.         if (v_align == -1)
  169.             m->d.alignment = BOTTOM_CENTER;
  170.     }
  171.     if (h_align == -1) {
  172.         if (v_align == 0)
  173.             m->d.alignment = TOP_RIGHT;
  174.         if (v_align == 1)
  175.             m->d.alignment = MIDDLE_RIGHT;
  176.         if (v_align == -1)
  177.             m->d.alignment = BOTTOM_RIGHT;
  178.     }
  179.     // Background Color
  180.     m->d.back_color = AV_RB24(tx3g_ptr);
  181.     tx3g_ptr += 4;
  182.     // BoxRecord
  183.     tx3g_ptr += 8;
  184.     // StyleRecord
  185.     tx3g_ptr += 4;
  186.     // fontID
  187.     style_fontID = AV_RB16(tx3g_ptr);
  188.     tx3g_ptr += 2;
  189.     // face-style-flags
  190.     s_default.style_flag = *tx3g_ptr++;
  191.     m->d.bold = s_default.style_flag & STYLE_FLAG_BOLD;
  192.     m->d.italic = s_default.style_flag & STYLE_FLAG_ITALIC;
  193.     m->d.underline = s_default.style_flag & STYLE_FLAG_UNDERLINE;
  194.     // fontsize
  195.     m->d.fontsize = *tx3g_ptr++;
  196.     // Primary color
  197.     m->d.color = AV_RB24(tx3g_ptr);
  198.     tx3g_ptr += 4;
  199.     // FontRecord
  200.     // FontRecord Size
  201.     tx3g_ptr += 4;
  202.     // ftab
  203.     tx3g_ptr += 4;
  204.  
  205.     m->ftab_entries = AV_RB16(tx3g_ptr);
  206.     tx3g_ptr += 2;
  207.  
  208.     for (i = 0; i < m->ftab_entries; i++) {
  209.  
  210.         box_size += 3;
  211.         if (avctx->extradata_size < box_size) {
  212.             mov_text_cleanup_ftab(m);
  213.             m->ftab_entries = 0;
  214.             return -1;
  215.         }
  216.         m->ftab_temp = av_mallocz(sizeof(*m->ftab_temp));
  217.         if (!m->ftab_temp) {
  218.             mov_text_cleanup_ftab(m);
  219.             return AVERROR(ENOMEM);
  220.         }
  221.         m->ftab_temp->fontID = AV_RB16(tx3g_ptr);
  222.         tx3g_ptr += 2;
  223.         font_length = *tx3g_ptr++;
  224.  
  225.         box_size = box_size + font_length;
  226.         if (avctx->extradata_size < box_size) {
  227.             mov_text_cleanup_ftab(m);
  228.             m->ftab_entries = 0;
  229.             return -1;
  230.         }
  231.         m->ftab_temp->font = av_malloc(font_length + 1);
  232.         if (!m->ftab_temp->font) {
  233.             mov_text_cleanup_ftab(m);
  234.             return AVERROR(ENOMEM);
  235.         }
  236.         memcpy(m->ftab_temp->font, tx3g_ptr, font_length);
  237.         m->ftab_temp->font[font_length] = '\0';
  238.         av_dynarray_add(&m->ftab, &m->count_f, m->ftab_temp);
  239.         if (!m->ftab) {
  240.             mov_text_cleanup_ftab(m);
  241.             return AVERROR(ENOMEM);
  242.         }
  243.         m->ftab_temp = NULL;
  244.         tx3g_ptr = tx3g_ptr + font_length;
  245.     }
  246.     for (i = 0; i < m->ftab_entries; i++) {
  247.         if (style_fontID == m->ftab[i]->fontID)
  248.             m->d.font = m->ftab[i]->font;
  249.     }
  250.     return 0;
  251. }
  252.  
  253. static int decode_twrp(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
  254. {
  255.     m->box_flags |= TWRP_BOX;
  256.     m->w.wrap_flag = *tsmb++;
  257.     return 0;
  258. }
  259.  
  260. static int decode_hlit(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
  261. {
  262.     m->box_flags |= HLIT_BOX;
  263.     m->h.hlit_start = AV_RB16(tsmb);
  264.     tsmb += 2;
  265.     m->h.hlit_end = AV_RB16(tsmb);
  266.     tsmb += 2;
  267.     return 0;
  268. }
  269.  
  270. static int decode_hclr(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
  271. {
  272.     m->box_flags |= HCLR_BOX;
  273.     memcpy(m->c.hlit_color, tsmb, 4);
  274.     tsmb += 4;
  275.     return 0;
  276. }
  277.  
  278. static int decode_styl(const uint8_t *tsmb, MovTextContext *m, AVPacket *avpkt)
  279. {
  280.     int i;
  281.     m->style_entries = AV_RB16(tsmb);
  282.     tsmb += 2;
  283.     // A single style record is of length 12 bytes.
  284.     if (m->tracksize + m->size_var + 2 + m->style_entries * 12 > avpkt->size)
  285.         return -1;
  286.  
  287.     m->box_flags |= STYL_BOX;
  288.     for(i = 0; i < m->style_entries; i++) {
  289.         m->s_temp = av_malloc(sizeof(*m->s_temp));
  290.         if (!m->s_temp) {
  291.             mov_text_cleanup(m);
  292.             return AVERROR(ENOMEM);
  293.         }
  294.         m->s_temp->style_start = AV_RB16(tsmb);
  295.         tsmb += 2;
  296.         m->s_temp->style_end = AV_RB16(tsmb);
  297.         tsmb += 2;
  298.         m->s_temp->style_fontID = AV_RB16(tsmb);
  299.         tsmb += 2;
  300.         m->s_temp->style_flag = AV_RB8(tsmb);
  301.         tsmb++;
  302.         m->s_temp->fontsize = AV_RB8(tsmb);
  303.         av_dynarray_add(&m->s, &m->count_s, m->s_temp);
  304.         if(!m->s) {
  305.             mov_text_cleanup(m);
  306.             return AVERROR(ENOMEM);
  307.         }
  308.         tsmb++;
  309.         // text-color-rgba
  310.         tsmb += 4;
  311.     }
  312.     return 0;
  313. }
  314.  
  315. static const Box box_types[] = {
  316.     { MKBETAG('s','t','y','l'), 2, decode_styl },
  317.     { MKBETAG('h','l','i','t'), 4, decode_hlit },
  318.     { MKBETAG('h','c','l','r'), 4, decode_hclr },
  319.     { MKBETAG('t','w','r','p'), 1, decode_twrp }
  320. };
  321.  
  322. const static size_t box_count = FF_ARRAY_ELEMS(box_types);
  323.  
  324. static int text_to_ass(AVBPrint *buf, const char *text, const char *text_end,
  325.                         MovTextContext *m)
  326. {
  327.     int i = 0;
  328.     int j = 0;
  329.     int text_pos = 0;
  330.  
  331.     if (text < text_end && m->box_flags & TWRP_BOX) {
  332.         if (m->w.wrap_flag == 1) {
  333.             av_bprintf(buf, "{\\q1}"); /* End of line wrap */
  334.         } else {
  335.             av_bprintf(buf, "{\\q2}"); /* No wrap */
  336.         }
  337.     }
  338.  
  339.     while (text < text_end) {
  340.         if (m->box_flags & STYL_BOX) {
  341.             for (i = 0; i < m->style_entries; i++) {
  342.                 if (m->s[i]->style_flag && text_pos == m->s[i]->style_end) {
  343.                     av_bprintf(buf, "{\\r}");
  344.                 }
  345.             }
  346.             for (i = 0; i < m->style_entries; i++) {
  347.                 if (m->s[i]->style_flag && text_pos == m->s[i]->style_start) {
  348.                     if (m->s[i]->style_flag & STYLE_FLAG_BOLD)
  349.                         av_bprintf(buf, "{\\b1}");
  350.                     if (m->s[i]->style_flag & STYLE_FLAG_ITALIC)
  351.                         av_bprintf(buf, "{\\i1}");
  352.                     if (m->s[i]->style_flag & STYLE_FLAG_UNDERLINE)
  353.                         av_bprintf(buf, "{\\u1}");
  354.                     av_bprintf(buf, "{\\fs%d}", m->s[i]->fontsize);
  355.                     for (j = 0; j < m->ftab_entries; j++) {
  356.                         if (m->s[i]->style_fontID == m->ftab[j]->fontID)
  357.                             av_bprintf(buf, "{\\fn%s}", m->ftab[j]->font);
  358.                     }
  359.                 }
  360.             }
  361.         }
  362.         if (m->box_flags & HLIT_BOX) {
  363.             if (text_pos == m->h.hlit_start) {
  364.                 /* If hclr box is present, set the secondary color to the color
  365.                  * specified. Otherwise, set primary color to white and secondary
  366.                  * color to black. These colors will come from TextSampleModifier
  367.                  * boxes in future and inverse video technique for highlight will
  368.                  * be implemented.
  369.                  */
  370.                 if (m->box_flags & HCLR_BOX) {
  371.                     av_bprintf(buf, "{\\2c&H%02x%02x%02x&}", m->c.hlit_color[2],
  372.                                 m->c.hlit_color[1], m->c.hlit_color[0]);
  373.                 } else {
  374.                     av_bprintf(buf, "{\\1c&H000000&}{\\2c&HFFFFFF&}");
  375.                 }
  376.             }
  377.             if (text_pos == m->h.hlit_end) {
  378.                 if (m->box_flags & HCLR_BOX) {
  379.                     av_bprintf(buf, "{\\2c&H000000&}");
  380.                 } else {
  381.                     av_bprintf(buf, "{\\1c&HFFFFFF&}{\\2c&H000000&}");
  382.                 }
  383.             }
  384.         }
  385.  
  386.         switch (*text) {
  387.         case '\r':
  388.             break;
  389.         case '\n':
  390.             av_bprintf(buf, "\\N");
  391.             break;
  392.         default:
  393.             av_bprint_chars(buf, *text, 1);
  394.             break;
  395.         }
  396.         text++;
  397.         text_pos++;
  398.     }
  399.  
  400.     return 0;
  401. }
  402.  
  403. static int mov_text_init(AVCodecContext *avctx) {
  404.     /*
  405.      * TODO: Handle the default text style.
  406.      * NB: Most players ignore styles completely, with the result that
  407.      * it's very common to find files where the default style is broken
  408.      * and respecting it results in a worse experience than ignoring it.
  409.      */
  410.     int ret;
  411.     MovTextContext *m = avctx->priv_data;
  412.     ret = mov_text_tx3g(avctx, m);
  413.     if (ret == 0) {
  414.         return ff_ass_subtitle_header(avctx, m->d.font, m->d.fontsize, m->d.color,
  415.                                 m->d.back_color, m->d.bold, m->d.italic,
  416.                                 m->d.underline, m->d.alignment);
  417.     } else
  418.         return ff_ass_subtitle_header_default(avctx);
  419. }
  420.  
  421. static int mov_text_decode_frame(AVCodecContext *avctx,
  422.                             void *data, int *got_sub_ptr, AVPacket *avpkt)
  423. {
  424.     AVSubtitle *sub = data;
  425.     MovTextContext *m = avctx->priv_data;
  426.     int ret, ts_start, ts_end;
  427.     AVBPrint buf;
  428.     char *ptr = avpkt->data;
  429.     char *end;
  430.     int text_length, tsmb_type, ret_tsmb;
  431.     uint64_t tsmb_size;
  432.     const uint8_t *tsmb;
  433.  
  434.     if (!ptr || avpkt->size < 2)
  435.         return AVERROR_INVALIDDATA;
  436.  
  437.     /*
  438.      * A packet of size two with value zero is an empty subtitle
  439.      * used to mark the end of the previous non-empty subtitle.
  440.      * We can just drop them here as we have duration information
  441.      * already. If the value is non-zero, then it's technically a
  442.      * bad packet.
  443.      */
  444.     if (avpkt->size == 2)
  445.         return AV_RB16(ptr) == 0 ? 0 : AVERROR_INVALIDDATA;
  446.  
  447.     /*
  448.      * The first two bytes of the packet are the length of the text string
  449.      * In complex cases, there are style descriptors appended to the string
  450.      * so we can't just assume the packet size is the string size.
  451.      */
  452.     text_length = AV_RB16(ptr);
  453.     end = ptr + FFMIN(2 + text_length, avpkt->size);
  454.     ptr += 2;
  455.  
  456.     ts_start = av_rescale_q(avpkt->pts,
  457.                             avctx->time_base,
  458.                             (AVRational){1,100});
  459.     ts_end   = av_rescale_q(avpkt->pts + avpkt->duration,
  460.                             avctx->time_base,
  461.                             (AVRational){1,100});
  462.  
  463.     tsmb_size = 0;
  464.     m->tracksize = 2 + text_length;
  465.     m->style_entries = 0;
  466.     m->box_flags = 0;
  467.     m->count_s = 0;
  468.     // Note that the spec recommends lines be no longer than 2048 characters.
  469.     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  470.     if (text_length + 2 != avpkt->size) {
  471.         while (m->tracksize + 8 <= avpkt->size) {
  472.             // A box is a minimum of 8 bytes.
  473.             tsmb = ptr + m->tracksize - 2;
  474.             tsmb_size = AV_RB32(tsmb);
  475.             tsmb += 4;
  476.             tsmb_type = AV_RB32(tsmb);
  477.             tsmb += 4;
  478.  
  479.             if (tsmb_size == 1) {
  480.                 if (m->tracksize + 16 > avpkt->size)
  481.                     break;
  482.                 tsmb_size = AV_RB64(tsmb);
  483.                 tsmb += 8;
  484.                 m->size_var = 16;
  485.             } else
  486.                 m->size_var = 8;
  487.             //size_var is equal to 8 or 16 depending on the size of box
  488.  
  489.             if (m->tracksize + tsmb_size > avpkt->size)
  490.                 break;
  491.  
  492.             for (size_t i = 0; i < box_count; i++) {
  493.                 if (tsmb_type == box_types[i].type) {
  494.                     if (m->tracksize + m->size_var + box_types[i].base_size > avpkt->size)
  495.                         break;
  496.                     ret_tsmb = box_types[i].decode(tsmb, m, avpkt);
  497.                     if (ret_tsmb == -1)
  498.                         break;
  499.                 }
  500.             }
  501.             m->tracksize = m->tracksize + tsmb_size;
  502.         }
  503.         text_to_ass(&buf, ptr, end, m);
  504.         mov_text_cleanup(m);
  505.     } else
  506.         text_to_ass(&buf, ptr, end, m);
  507.  
  508.     ret = ff_ass_add_rect_bprint(sub, &buf, ts_start, ts_end - ts_start);
  509.     av_bprint_finalize(&buf, NULL);
  510.     if (ret < 0)
  511.         return ret;
  512.     *got_sub_ptr = sub->num_rects > 0;
  513.     return avpkt->size;
  514. }
  515.  
  516. static int mov_text_decode_close(AVCodecContext *avctx)
  517. {
  518.     MovTextContext *m = avctx->priv_data;
  519.     mov_text_cleanup_ftab(m);
  520.     return 0;
  521. }
  522.  
  523. AVCodec ff_movtext_decoder = {
  524.     .name         = "mov_text",
  525.     .long_name    = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
  526.     .type         = AVMEDIA_TYPE_SUBTITLE,
  527.     .id           = AV_CODEC_ID_MOV_TEXT,
  528.     .priv_data_size = sizeof(MovTextContext),
  529.     .init         = mov_text_init,
  530.     .decode       = mov_text_decode_frame,
  531.     .close        = mov_text_decode_close,
  532. };
  533.