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.

385 lines
13KB

  1. /*
  2. * ARIB STD-B24 caption decoder using the libaribb24 library
  3. * Copyright (c) 2019 Jan Ekström
  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 "avcodec.h"
  22. #include "libavcodec/ass.h"
  23. #include "libavutil/log.h"
  24. #include "libavutil/opt.h"
  25. #include <aribb24/aribb24.h>
  26. #include <aribb24/parser.h>
  27. #include <aribb24/decoder.h>
  28. typedef struct Libaribb24Context {
  29. AVClass *class;
  30. arib_instance_t *lib_instance;
  31. arib_parser_t *parser;
  32. arib_decoder_t *decoder;
  33. int read_order;
  34. char *aribb24_base_path;
  35. unsigned int aribb24_skip_ruby;
  36. } Libaribb24Context;
  37. static unsigned int get_profile_font_size(int profile)
  38. {
  39. switch (profile) {
  40. case FF_PROFILE_ARIB_PROFILE_A:
  41. return 36;
  42. case FF_PROFILE_ARIB_PROFILE_C:
  43. return 18;
  44. default:
  45. return 0;
  46. }
  47. }
  48. static void libaribb24_log(void *p, const char *msg)
  49. {
  50. av_log((AVCodecContext *)p, AV_LOG_INFO, "%s\n", msg);
  51. }
  52. static int libaribb24_generate_ass_header(AVCodecContext *avctx)
  53. {
  54. unsigned int plane_width = 0;
  55. unsigned int plane_height = 0;
  56. unsigned int font_size = 0;
  57. switch (avctx->profile) {
  58. case FF_PROFILE_ARIB_PROFILE_A:
  59. plane_width = 960;
  60. plane_height = 540;
  61. font_size = get_profile_font_size(avctx->profile);
  62. break;
  63. case FF_PROFILE_ARIB_PROFILE_C:
  64. plane_width = 320;
  65. plane_height = 180;
  66. font_size = get_profile_font_size(avctx->profile);
  67. break;
  68. default:
  69. av_log(avctx, AV_LOG_ERROR, "Unknown or unsupported profile set!\n");
  70. return AVERROR(EINVAL);
  71. }
  72. avctx->subtitle_header = av_asprintf(
  73. "[Script Info]\r\n"
  74. "; Script generated by FFmpeg/Lavc%s\r\n"
  75. "ScriptType: v4.00+\r\n"
  76. "PlayResX: %d\r\n"
  77. "PlayResY: %d\r\n"
  78. "\r\n"
  79. "[V4+ Styles]\r\n"
  80. /* ASSv4 header */
  81. "Format: Name, "
  82. "Fontname, Fontsize, "
  83. "PrimaryColour, SecondaryColour, OutlineColour, BackColour, "
  84. "Bold, Italic, Underline, StrikeOut, "
  85. "ScaleX, ScaleY, "
  86. "Spacing, Angle, "
  87. "BorderStyle, Outline, Shadow, "
  88. "Alignment, MarginL, MarginR, MarginV, "
  89. "Encoding\r\n"
  90. "Style: "
  91. "Default," /* Name */
  92. "%s,%d," /* Font{name,size} */
  93. "&H%x,&H%x,&H%x,&H%x," /* {Primary,Secondary,Outline,Back}Colour */
  94. "%d,%d,%d,0," /* Bold, Italic, Underline, StrikeOut */
  95. "100,100," /* Scale{X,Y} */
  96. "0,0," /* Spacing, Angle */
  97. "%d,1,0," /* BorderStyle, Outline, Shadow */
  98. "%d,10,10,10," /* Alignment, Margin[LRV] */
  99. "0\r\n" /* Encoding */
  100. "\r\n"
  101. "[Events]\r\n"
  102. "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\r\n",
  103. !(avctx->flags & AV_CODEC_FLAG_BITEXACT) ? AV_STRINGIFY(LIBAVCODEC_VERSION) : "",
  104. plane_width, plane_height,
  105. ASS_DEFAULT_FONT, font_size, ASS_DEFAULT_COLOR,
  106. ASS_DEFAULT_COLOR, ASS_DEFAULT_BACK_COLOR, ASS_DEFAULT_BACK_COLOR,
  107. -ASS_DEFAULT_BOLD, -ASS_DEFAULT_ITALIC, -ASS_DEFAULT_UNDERLINE,
  108. ASS_DEFAULT_BORDERSTYLE, ASS_DEFAULT_ALIGNMENT);
  109. if (!avctx->subtitle_header)
  110. return AVERROR(ENOMEM);
  111. avctx->subtitle_header_size = strlen(avctx->subtitle_header);
  112. return 0;
  113. }
  114. static int libaribb24_init(AVCodecContext *avctx)
  115. {
  116. Libaribb24Context *b24 = avctx->priv_data;
  117. void(* arib_dec_init)(arib_decoder_t* decoder) = NULL;
  118. int ret_code = AVERROR_EXTERNAL;
  119. if (!(b24->lib_instance = arib_instance_new(avctx))) {
  120. av_log(avctx, AV_LOG_ERROR, "Failed to initialize libaribb24!\n");
  121. goto init_fail;
  122. }
  123. if (b24->aribb24_base_path) {
  124. av_log(avctx, AV_LOG_INFO, "Setting the libaribb24 base path to '%s'\n",
  125. b24->aribb24_base_path);
  126. arib_set_base_path(b24->lib_instance, b24->aribb24_base_path);
  127. }
  128. arib_register_messages_callback(b24->lib_instance, libaribb24_log);
  129. if (!(b24->parser = arib_get_parser(b24->lib_instance))) {
  130. av_log(avctx, AV_LOG_ERROR, "Failed to initialize libaribb24 PES parser!\n");
  131. goto init_fail;
  132. }
  133. if (!(b24->decoder = arib_get_decoder(b24->lib_instance))) {
  134. av_log(avctx, AV_LOG_ERROR, "Failed to initialize libaribb24 decoder!\n");
  135. goto init_fail;
  136. }
  137. switch (avctx->profile) {
  138. case FF_PROFILE_ARIB_PROFILE_A:
  139. arib_dec_init = arib_initialize_decoder_a_profile;
  140. break;
  141. case FF_PROFILE_ARIB_PROFILE_C:
  142. arib_dec_init = arib_initialize_decoder_c_profile;
  143. break;
  144. default:
  145. av_log(avctx, AV_LOG_ERROR, "Unknown or unsupported profile set!\n");
  146. ret_code = AVERROR(EINVAL);
  147. goto init_fail;
  148. }
  149. arib_dec_init(b24->decoder);
  150. if (libaribb24_generate_ass_header(avctx) < 0) {
  151. ret_code = AVERROR(ENOMEM);
  152. goto init_fail;
  153. }
  154. return 0;
  155. init_fail:
  156. if (b24->decoder)
  157. arib_finalize_decoder(b24->decoder);
  158. if (b24->lib_instance)
  159. arib_instance_destroy(b24->lib_instance);
  160. return ret_code;
  161. }
  162. static int libaribb24_close(AVCodecContext *avctx)
  163. {
  164. Libaribb24Context *b24 = avctx->priv_data;
  165. if (b24->decoder)
  166. arib_finalize_decoder(b24->decoder);
  167. if (b24->lib_instance)
  168. arib_instance_destroy(b24->lib_instance);
  169. return 0;
  170. }
  171. #define RGB_TO_BGR(c) ((c & 0xff) << 16 | (c & 0xff00) | ((c >> 16) & 0xff))
  172. static void libaribb24_handle_regions(AVCodecContext *avctx, AVSubtitle *sub)
  173. {
  174. Libaribb24Context *b24 = avctx->priv_data;
  175. const arib_buf_region_t *region = arib_decoder_get_regions(b24->decoder);
  176. unsigned int profile_font_size = get_profile_font_size(avctx->profile);
  177. AVBPrint buf = { 0 };
  178. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  179. while (region) {
  180. ptrdiff_t region_length = region->p_end - region->p_start;
  181. unsigned int ruby_region =
  182. region->i_fontheight == (profile_font_size / 2);
  183. // ASS requires us to make the colors BGR, so we convert here
  184. int foreground_bgr_color = RGB_TO_BGR(region->i_foreground_color);
  185. int background_bgr_color = RGB_TO_BGR(region->i_background_color);
  186. if (region_length < 0) {
  187. av_log(avctx, AV_LOG_ERROR, "Invalid negative region length!\n");
  188. break;
  189. }
  190. if (region_length == 0 || (ruby_region && b24->aribb24_skip_ruby)) {
  191. goto next_region;
  192. }
  193. // color and alpha
  194. if (foreground_bgr_color != ASS_DEFAULT_COLOR)
  195. av_bprintf(&buf, "{\\1c&H%06x&}", foreground_bgr_color);
  196. if (region->i_foreground_alpha != 0)
  197. av_bprintf(&buf, "{\\1a&H%02x&}", region->i_foreground_alpha);
  198. if (background_bgr_color != ASS_DEFAULT_BACK_COLOR)
  199. av_bprintf(&buf, "{\\3c&H%06x&}", background_bgr_color);
  200. if (region->i_background_alpha != 0)
  201. av_bprintf(&buf, "{\\3a&H%02x&}", region->i_background_alpha);
  202. // font size
  203. if (region->i_fontwidth != profile_font_size ||
  204. region->i_fontheight != profile_font_size) {
  205. av_bprintf(&buf, "{\\fscx%d\\fscy%d}",
  206. (int)round(((double)region->i_fontwidth /
  207. (double)profile_font_size) * 100),
  208. (int)round(((double)region->i_fontheight /
  209. (double)profile_font_size) * 100));
  210. }
  211. // TODO: positioning
  212. av_bprint_append_data(&buf, region->p_start, region_length);
  213. av_bprintf(&buf, "{\\r}");
  214. next_region:
  215. region = region->p_next;
  216. }
  217. av_log(avctx, AV_LOG_DEBUG, "Styled ASS line: %s\n",
  218. buf.str);
  219. ff_ass_add_rect(sub, buf.str, b24->read_order++,
  220. 0, NULL, NULL);
  221. av_bprint_finalize(&buf, NULL);
  222. }
  223. static int libaribb24_decode(AVCodecContext *avctx, void *data, int *got_sub_ptr, AVPacket *pkt)
  224. {
  225. Libaribb24Context *b24 = avctx->priv_data;
  226. AVSubtitle *sub = data;
  227. size_t parsed_data_size = 0;
  228. size_t decoded_subtitle_size = 0;
  229. const unsigned char *parsed_data = NULL;
  230. char *decoded_subtitle = NULL;
  231. time_t subtitle_duration = 0;
  232. if (pkt->size <= 0)
  233. return pkt->size;
  234. arib_parse_pes(b24->parser, pkt->data, pkt->size);
  235. parsed_data = arib_parser_get_data(b24->parser,
  236. &parsed_data_size);
  237. if (!parsed_data || !parsed_data_size) {
  238. av_log(avctx, AV_LOG_DEBUG, "No decode'able data was received from "
  239. "packet (dts: %"PRId64", pts: %"PRId64").\n",
  240. pkt->dts, pkt->pts);
  241. return pkt->size;
  242. }
  243. decoded_subtitle_size = parsed_data_size * 4;
  244. if (!(decoded_subtitle = av_mallocz(decoded_subtitle_size + 1))) {
  245. av_log(avctx, AV_LOG_ERROR,
  246. "Failed to allocate buffer for decoded subtitle!\n");
  247. return AVERROR(ENOMEM);
  248. }
  249. decoded_subtitle_size = arib_decode_buffer(b24->decoder,
  250. parsed_data,
  251. parsed_data_size,
  252. decoded_subtitle,
  253. decoded_subtitle_size);
  254. subtitle_duration = arib_decoder_get_time(b24->decoder);
  255. if (avctx->pkt_timebase.num && pkt->pts != AV_NOPTS_VALUE)
  256. sub->pts = av_rescale_q(pkt->pts,
  257. avctx->pkt_timebase, AV_TIME_BASE_Q);
  258. sub->end_display_time = subtitle_duration ?
  259. av_rescale_q(subtitle_duration,
  260. AV_TIME_BASE_Q,
  261. (AVRational){1, 1000}) :
  262. UINT32_MAX;
  263. av_log(avctx, AV_LOG_DEBUG,
  264. "Result: '%s' (size: %zu, pkt_pts: %"PRId64", sub_pts: %"PRId64" "
  265. "duration: %"PRIu32", pkt_timebase: %d/%d, time_base: %d/%d')\n",
  266. decoded_subtitle ? decoded_subtitle : "<no subtitle>",
  267. decoded_subtitle_size,
  268. pkt->pts, sub->pts,
  269. sub->end_display_time,
  270. avctx->pkt_timebase.num, avctx->pkt_timebase.den,
  271. avctx->time_base.num, avctx->time_base.den);
  272. if (decoded_subtitle)
  273. libaribb24_handle_regions(avctx, sub);
  274. *got_sub_ptr = sub->num_rects > 0;
  275. av_free(decoded_subtitle);
  276. // flush the region buffers, otherwise the linked list keeps getting
  277. // longer and longer...
  278. arib_finalize_decoder(b24->decoder);
  279. return pkt->size;
  280. }
  281. static void libaribb24_flush(AVCodecContext *avctx)
  282. {
  283. Libaribb24Context *b24 = avctx->priv_data;
  284. if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
  285. b24->read_order = 0;
  286. }
  287. #define OFFSET(x) offsetof(Libaribb24Context, x)
  288. #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
  289. static const AVOption options[] = {
  290. { "aribb24-base-path", "set the base path for the libaribb24 library",
  291. OFFSET(aribb24_base_path), AV_OPT_TYPE_STRING, { 0 }, 0, 0, SD },
  292. { "aribb24-skip-ruby-text", "skip ruby text blocks during decoding",
  293. OFFSET(aribb24_skip_ruby), AV_OPT_TYPE_BOOL, { 1 }, 0, 1, SD },
  294. { NULL }
  295. };
  296. static const AVClass aribb24_class = {
  297. .class_name = "libaribb24 decoder",
  298. .item_name = av_default_item_name,
  299. .option = options,
  300. .version = LIBAVUTIL_VERSION_INT,
  301. };
  302. AVCodec ff_libaribb24_decoder = {
  303. .name = "libaribb24",
  304. .long_name = NULL_IF_CONFIG_SMALL("libaribb24 ARIB STD-B24 caption decoder"),
  305. .type = AVMEDIA_TYPE_SUBTITLE,
  306. .id = AV_CODEC_ID_ARIB_CAPTION,
  307. .priv_data_size = sizeof(Libaribb24Context),
  308. .init = libaribb24_init,
  309. .close = libaribb24_close,
  310. .decode = libaribb24_decode,
  311. .flush = libaribb24_flush,
  312. .priv_class= &aribb24_class,
  313. .wrapper_name = "libaribb24",
  314. };