You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
11KB

  1. /*
  2. * 3GPP TS 26.245 Timed Text encoder
  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. #include <stdarg.h>
  22. #include "avcodec.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/intreadwrite.h"
  26. #include "libavutil/mem.h"
  27. #include "libavutil/common.h"
  28. #include "ass_split.h"
  29. #include "ass.h"
  30. #define STYLE_FLAG_BOLD (1<<0)
  31. #define STYLE_FLAG_ITALIC (1<<1)
  32. #define STYLE_FLAG_UNDERLINE (1<<2)
  33. #define STYLE_RECORD_SIZE 12
  34. #define SIZE_ADD 10
  35. #define STYL_BOX (1<<0)
  36. #define av_bprint_append_any(buf, data, size) av_bprint_append_data(buf, ((const char*)data), size)
  37. typedef struct {
  38. uint16_t style_start;
  39. uint16_t style_end;
  40. uint8_t style_flag;
  41. } StyleBox;
  42. typedef struct {
  43. ASSSplitContext *ass_ctx;
  44. AVBPrint buffer;
  45. StyleBox **style_attributes;
  46. StyleBox *style_attributes_temp;
  47. int count;
  48. uint8_t box_flags;
  49. uint16_t style_entries;
  50. uint16_t style_fontID;
  51. uint8_t style_fontsize;
  52. uint32_t style_color;
  53. uint16_t text_pos;
  54. } MovTextContext;
  55. typedef struct {
  56. uint32_t type;
  57. void (*encode)(MovTextContext *s, uint32_t tsmb_type);
  58. } Box;
  59. static void mov_text_cleanup(MovTextContext *s)
  60. {
  61. int j;
  62. if (s->box_flags & STYL_BOX) {
  63. for (j = 0; j < s->count; j++) {
  64. av_freep(&s->style_attributes[j]);
  65. }
  66. av_freep(&s->style_attributes);
  67. }
  68. }
  69. static void encode_styl(MovTextContext *s, uint32_t tsmb_type)
  70. {
  71. int j;
  72. uint32_t tsmb_size;
  73. if (s->box_flags & STYL_BOX) {
  74. tsmb_size = s->count * STYLE_RECORD_SIZE + SIZE_ADD;
  75. tsmb_size = AV_RB32(&tsmb_size);
  76. s->style_entries = AV_RB16(&s->count);
  77. s->style_fontID = 0x00 | 0x01<<8;
  78. s->style_fontsize = 0x12;
  79. s->style_color = MKTAG(0xFF, 0xFF, 0xFF, 0xFF);
  80. /*The above three attributes are hard coded for now
  81. but will come from ASS style in the future*/
  82. av_bprint_append_any(&s->buffer, &tsmb_size, 4);
  83. av_bprint_append_any(&s->buffer, &tsmb_type, 4);
  84. av_bprint_append_any(&s->buffer, &s->style_entries, 2);
  85. for (j = 0; j < s->count; j++) {
  86. av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_start, 2);
  87. av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_end, 2);
  88. av_bprint_append_any(&s->buffer, &s->style_fontID, 2);
  89. av_bprint_append_any(&s->buffer, &s->style_attributes[j]->style_flag, 1);
  90. av_bprint_append_any(&s->buffer, &s->style_fontsize, 1);
  91. av_bprint_append_any(&s->buffer, &s->style_color, 4);
  92. }
  93. mov_text_cleanup(s);
  94. }
  95. }
  96. static const Box box_types[] = {
  97. { MKTAG('s','t','y','l'), encode_styl },
  98. };
  99. const static size_t box_count = FF_ARRAY_ELEMS(box_types);
  100. static av_cold int mov_text_encode_init(AVCodecContext *avctx)
  101. {
  102. /*
  103. * For now, we'll use a fixed default style. When we add styling
  104. * support, this will be generated from the ASS style.
  105. */
  106. static const uint8_t text_sample_entry[] = {
  107. 0x00, 0x00, 0x00, 0x00, // uint32_t displayFlags
  108. 0x01, // int8_t horizontal-justification
  109. 0xFF, // int8_t vertical-justification
  110. 0x00, 0x00, 0x00, 0x00, // uint8_t background-color-rgba[4]
  111. // BoxRecord {
  112. 0x00, 0x00, // int16_t top
  113. 0x00, 0x00, // int16_t left
  114. 0x00, 0x00, // int16_t bottom
  115. 0x00, 0x00, // int16_t right
  116. // };
  117. // StyleRecord {
  118. 0x00, 0x00, // uint16_t startChar
  119. 0x00, 0x00, // uint16_t endChar
  120. 0x00, 0x01, // uint16_t font-ID
  121. 0x00, // uint8_t face-style-flags
  122. 0x12, // uint8_t font-size
  123. 0xFF, 0xFF, 0xFF, 0xFF, // uint8_t text-color-rgba[4]
  124. // };
  125. // FontTableBox {
  126. 0x00, 0x00, 0x00, 0x12, // uint32_t size
  127. 'f', 't', 'a', 'b', // uint8_t name[4]
  128. 0x00, 0x01, // uint16_t entry-count
  129. // FontRecord {
  130. 0x00, 0x01, // uint16_t font-ID
  131. 0x05, // uint8_t font-name-length
  132. 'S', 'e', 'r', 'i', 'f',// uint8_t font[font-name-length]
  133. // };
  134. // };
  135. };
  136. MovTextContext *s = avctx->priv_data;
  137. avctx->extradata_size = sizeof text_sample_entry;
  138. avctx->extradata = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
  139. if (!avctx->extradata)
  140. return AVERROR(ENOMEM);
  141. av_bprint_init(&s->buffer, 0, AV_BPRINT_SIZE_UNLIMITED);
  142. memcpy(avctx->extradata, text_sample_entry, avctx->extradata_size);
  143. s->ass_ctx = ff_ass_split(avctx->subtitle_header);
  144. return s->ass_ctx ? 0 : AVERROR_INVALIDDATA;
  145. }
  146. static void mov_text_style_cb(void *priv, const char style, int close)
  147. {
  148. MovTextContext *s = priv;
  149. if (!close) {
  150. if (!(s->box_flags & STYL_BOX)) { //first style entry
  151. s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp));
  152. if (!s->style_attributes_temp) {
  153. av_bprint_clear(&s->buffer);
  154. s->box_flags &= ~STYL_BOX;
  155. return;
  156. }
  157. s->style_attributes_temp->style_flag = 0;
  158. s->style_attributes_temp->style_start = AV_RB16(&s->text_pos);
  159. } else {
  160. if (s->style_attributes_temp->style_flag) { //break the style record here and start a new one
  161. s->style_attributes_temp->style_end = AV_RB16(&s->text_pos);
  162. av_dynarray_add(&s->style_attributes, &s->count, s->style_attributes_temp);
  163. s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp));
  164. if (!s->style_attributes_temp) {
  165. mov_text_cleanup(s);
  166. av_bprint_clear(&s->buffer);
  167. s->box_flags &= ~STYL_BOX;
  168. return;
  169. }
  170. s->style_attributes_temp->style_flag = s->style_attributes[s->count - 1]->style_flag;
  171. s->style_attributes_temp->style_start = AV_RB16(&s->text_pos);
  172. } else {
  173. s->style_attributes_temp->style_flag = 0;
  174. s->style_attributes_temp->style_start = AV_RB16(&s->text_pos);
  175. }
  176. }
  177. switch (style){
  178. case 'b':
  179. s->style_attributes_temp->style_flag |= STYLE_FLAG_BOLD;
  180. break;
  181. case 'i':
  182. s->style_attributes_temp->style_flag |= STYLE_FLAG_ITALIC;
  183. break;
  184. case 'u':
  185. s->style_attributes_temp->style_flag |= STYLE_FLAG_UNDERLINE;
  186. break;
  187. }
  188. } else {
  189. s->style_attributes_temp->style_end = AV_RB16(&s->text_pos);
  190. av_dynarray_add(&s->style_attributes, &s->count, s->style_attributes_temp);
  191. s->style_attributes_temp = av_malloc(sizeof(*s->style_attributes_temp));
  192. if (!s->style_attributes_temp) {
  193. mov_text_cleanup(s);
  194. av_bprint_clear(&s->buffer);
  195. s->box_flags &= ~STYL_BOX;
  196. return;
  197. }
  198. s->style_attributes_temp->style_flag = s->style_attributes[s->count - 1]->style_flag;
  199. switch (style){
  200. case 'b':
  201. s->style_attributes_temp->style_flag &= ~STYLE_FLAG_BOLD;
  202. break;
  203. case 'i':
  204. s->style_attributes_temp->style_flag &= ~STYLE_FLAG_ITALIC;
  205. break;
  206. case 'u':
  207. s->style_attributes_temp->style_flag &= ~STYLE_FLAG_UNDERLINE;
  208. break;
  209. }
  210. if (s->style_attributes_temp->style_flag) { //start of new style record
  211. s->style_attributes_temp->style_start = AV_RB16(&s->text_pos);
  212. }
  213. }
  214. s->box_flags |= STYL_BOX;
  215. }
  216. static void mov_text_text_cb(void *priv, const char *text, int len)
  217. {
  218. MovTextContext *s = priv;
  219. av_bprint_append_data(&s->buffer, text, len);
  220. s->text_pos += len;
  221. }
  222. static void mov_text_new_line_cb(void *priv, int forced)
  223. {
  224. MovTextContext *s = priv;
  225. av_bprint_append_data(&s->buffer, "\n", 1);
  226. s->text_pos += 1;
  227. }
  228. static const ASSCodesCallbacks mov_text_callbacks = {
  229. .text = mov_text_text_cb,
  230. .new_line = mov_text_new_line_cb,
  231. .style = mov_text_style_cb,
  232. };
  233. static int mov_text_encode_frame(AVCodecContext *avctx, unsigned char *buf,
  234. int bufsize, const AVSubtitle *sub)
  235. {
  236. MovTextContext *s = avctx->priv_data;
  237. ASSDialog *dialog;
  238. int i, num, length;
  239. size_t j;
  240. s->text_pos = 0;
  241. s->count = 0;
  242. s->box_flags = 0;
  243. s->style_entries = 0;
  244. for (i = 0; i < sub->num_rects; i++) {
  245. if (sub->rects[i]->type != SUBTITLE_ASS) {
  246. av_log(avctx, AV_LOG_ERROR, "Only SUBTITLE_ASS type supported.\n");
  247. return AVERROR(ENOSYS);
  248. }
  249. dialog = ff_ass_split_dialog(s->ass_ctx, sub->rects[i]->ass, 0, &num);
  250. for (; dialog && num--; dialog++) {
  251. ff_ass_split_override_codes(&mov_text_callbacks, s, dialog->text);
  252. }
  253. for (j = 0; j < box_count; j++) {
  254. box_types[j].encode(s, box_types[j].type);
  255. }
  256. }
  257. AV_WB16(buf, s->text_pos);
  258. buf += 2;
  259. if (!av_bprint_is_complete(&s->buffer)) {
  260. length = AVERROR(ENOMEM);
  261. goto exit;
  262. }
  263. if (!s->buffer.len) {
  264. length = 0;
  265. goto exit;
  266. }
  267. if (s->buffer.len > bufsize - 3) {
  268. av_log(avctx, AV_LOG_ERROR, "Buffer too small for ASS event.\n");
  269. length = AVERROR(EINVAL);
  270. goto exit;
  271. }
  272. memcpy(buf, s->buffer.str, s->buffer.len);
  273. length = s->buffer.len + 2;
  274. exit:
  275. av_bprint_clear(&s->buffer);
  276. return length;
  277. }
  278. static int mov_text_encode_close(AVCodecContext *avctx)
  279. {
  280. MovTextContext *s = avctx->priv_data;
  281. ff_ass_split_free(s->ass_ctx);
  282. av_bprint_finalize(&s->buffer, NULL);
  283. return 0;
  284. }
  285. AVCodec ff_movtext_encoder = {
  286. .name = "mov_text",
  287. .long_name = NULL_IF_CONFIG_SMALL("3GPP Timed Text subtitle"),
  288. .type = AVMEDIA_TYPE_SUBTITLE,
  289. .id = AV_CODEC_ID_MOV_TEXT,
  290. .priv_data_size = sizeof(MovTextContext),
  291. .init = mov_text_encode_init,
  292. .encode_sub = mov_text_encode_frame,
  293. .close = mov_text_encode_close,
  294. };