ass_split.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * SSA/ASS spliting functions
  3. * Copyright (c) 2010 Aurelien Jacobs <aurel@gnuage.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. #ifndef AVCODEC_ASS_SPLIT_H
  22. #define AVCODEC_ASS_SPLIT_H
  23. /**
  24. * fields extracted from the [Script Info] section
  25. */
  26. typedef struct {
  27. char *script_type; /**< SSA script format version (eg. v4.00) */
  28. char *collisions; /**< how subtitles are moved to prevent collisions */
  29. int play_res_x; /**< video width that ASS coords are referring to */
  30. int play_res_y; /**< video height that ASS coords are referring to */
  31. float timer; /**< time multiplier to apply to SSA clock (in %) */
  32. } ASSScriptInfo;
  33. /**
  34. * fields extracted from the [V4(+) Styles] section
  35. */
  36. typedef struct {
  37. char *name; /**< name of the tyle (case sensitive) */
  38. char *font_name; /**< font face (case sensitive) */
  39. int font_size; /**< font height */
  40. int primary_color; /**< color that a subtitle will normally appear in */
  41. int secondary_color;
  42. int outline_color; /**< color for outline in ASS, called tertiary in SSA */
  43. int back_color; /**< color of the subtitle outline or shadow */
  44. int bold; /**< whether text is bold (1) or not (0) */
  45. int italic; /**< whether text is italic (1) or not (0) */
  46. int underline; /**< whether text is underlined (1) or not (0) */
  47. int strikeout;
  48. float scalex;
  49. float scaley;
  50. float spacing;
  51. float angle;
  52. int border_style;
  53. float outline;
  54. float shadow;
  55. int alignment; /**< position of the text (left, center, top...),
  56. defined after the layout of the numpad
  57. (1-3 sub, 4-6 mid, 7-9 top) */
  58. int margin_l;
  59. int margin_r;
  60. int margin_v;
  61. int alpha_level;
  62. int encoding;
  63. } ASSStyle;
  64. /**
  65. * fields extracted from the [Events] section
  66. */
  67. typedef struct {
  68. int readorder;
  69. int layer; /**< higher numbered layers are drawn over lower numbered */
  70. int start; /**< start time of the dialog in centiseconds */
  71. int end; /**< end time of the dialog in centiseconds */
  72. char *style; /**< name of the ASSStyle to use with this dialog */
  73. char *name;
  74. int margin_l;
  75. int margin_r;
  76. int margin_v;
  77. char *effect;
  78. char *text; /**< actual text which will be displayed as a subtitle,
  79. can include style override control codes (see
  80. ff_ass_split_override_codes()) */
  81. } ASSDialog;
  82. /**
  83. * structure containing the whole split ASS data
  84. */
  85. typedef struct {
  86. ASSScriptInfo script_info; /**< general information about the SSA script*/
  87. ASSStyle *styles; /**< array of split out styles */
  88. int styles_count; /**< number of ASSStyle in the styles array */
  89. ASSDialog *dialogs; /**< array of split out dialogs */
  90. int dialogs_count; /**< number of ASSDialog in the dialogs array*/
  91. } ASS;
  92. /**
  93. * This struct can be casted to ASS to access to the split data.
  94. */
  95. typedef struct ASSSplitContext ASSSplitContext;
  96. /**
  97. * Split a full ASS file or a ASS header from a string buffer and store
  98. * the split structure in a newly allocated context.
  99. *
  100. * @param buf String containing the ASS formatted data.
  101. * @return Newly allocated struct containing split data.
  102. */
  103. ASSSplitContext *ff_ass_split(const char *buf);
  104. /**
  105. * Split one or several ASS "Dialogue" lines from a string buffer and store
  106. * them in an already initialized context.
  107. *
  108. * @param ctx Context previously initialized by ff_ass_split().
  109. * @param buf String containing the ASS "Dialogue" lines.
  110. * @param cache Set to 1 to keep all the previously split ASSDialog in
  111. * the context, or set to 0 to free all the previously split
  112. * ASSDialog.
  113. * @param number If not NULL, the pointed integer will be set to the number
  114. * of split ASSDialog.
  115. * @return Pointer to the first split ASSDialog.
  116. */
  117. ASSDialog *ff_ass_split_dialog(ASSSplitContext *ctx, const char *buf,
  118. int cache, int *number);
  119. /**
  120. * Free a dialogue obtained from ff_ass_split_dialog2().
  121. */
  122. void ff_ass_free_dialog(ASSDialog **dialogp);
  123. /**
  124. * Split one ASS Dialogue line from a string buffer.
  125. *
  126. * @param ctx Context previously initialized by ff_ass_split().
  127. * @param buf String containing the ASS "Dialogue" line.
  128. * @return Pointer to the split ASSDialog. Must be freed with ff_ass_free_dialog()
  129. */
  130. ASSDialog *ff_ass_split_dialog2(ASSSplitContext *ctx, const char *buf);
  131. /**
  132. * Free all the memory allocated for an ASSSplitContext.
  133. *
  134. * @param ctx Context previously initialized by ff_ass_split().
  135. */
  136. void ff_ass_split_free(ASSSplitContext *ctx);
  137. /**
  138. * Set of callback functions corresponding to each override codes that can
  139. * be encountered in a "Dialogue" Text field.
  140. */
  141. typedef struct {
  142. /**
  143. * @defgroup ass_styles ASS styles
  144. * @{
  145. */
  146. void (*text)(void *priv, const char *text, int len);
  147. void (*new_line)(void *priv, int forced);
  148. void (*style)(void *priv, char style, int close);
  149. void (*color)(void *priv, unsigned int /* color */, unsigned int color_id);
  150. void (*alpha)(void *priv, int alpha, int alpha_id);
  151. void (*font_name)(void *priv, const char *name);
  152. void (*font_size)(void *priv, int size);
  153. void (*alignment)(void *priv, int alignment);
  154. void (*cancel_overrides)(void *priv, const char *style);
  155. /** @} */
  156. /**
  157. * @defgroup ass_functions ASS functions
  158. * @{
  159. */
  160. void (*move)(void *priv, int x1, int y1, int x2, int y2, int t1, int t2);
  161. void (*origin)(void *priv, int x, int y);
  162. /** @} */
  163. /**
  164. * @defgroup ass_end end of Dialogue Event
  165. * @{
  166. */
  167. void (*end)(void *priv);
  168. /** @} */
  169. } ASSCodesCallbacks;
  170. /**
  171. * Split override codes out of a ASS "Dialogue" Text field.
  172. *
  173. * @param callbacks Set of callback functions called for each override code
  174. * encountered.
  175. * @param priv Opaque pointer passed to the callback functions.
  176. * @param buf The ASS "Dialogue" Text field to split.
  177. * @return >= 0 on success otherwise an error code <0
  178. */
  179. int ff_ass_split_override_codes(const ASSCodesCallbacks *callbacks, void *priv,
  180. const char *buf);
  181. /**
  182. * Find an ASSStyle structure by its name.
  183. *
  184. * @param ctx Context previously initialized by ff_ass_split().
  185. * @param style name of the style to search for.
  186. * @return the ASSStyle corresponding to style, or NULL if style can't be found
  187. */
  188. ASSStyle *ff_ass_style_get(ASSSplitContext *ctx, const char *style);
  189. #endif /* AVCODEC_ASS_SPLIT_H */