FFmpeg
Loading...
Searching...
No Matches
libaribcaption.c
Go to the documentation of this file.
1/*
2 * ARIB STD-B24 caption decoder using the libaribcaption library
3 * Copyright (c) 2022 TADANO Tokumei
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 "codec_internal.h"
24#include "internal.h"
25#include "libavcodec/ass.h"
26#include "libavutil/avstring.h"
27#include "libavutil/avutil.h"
28#include "libavutil/mem.h"
29#include "libavutil/thread.h"
30#include "libavutil/log.h"
31#include "libavutil/opt.h"
32
33#include <aribcaption/aribcaption.h>
34
35#if !defined(DEFAULT_FONT_ASS)
36# define DEFAULT_FONT_ASS "sans-serif"
37#endif
38
39#define ARIBC_BPRINT_SIZE_INIT 64
40#define ARIBC_BPRINT_SIZE_MAX (8 * 1024)
41#define ARIBC_ALPHA_MAX_NUM 4
42#define ARIBC_ALPHA_DEFAULT_FRONT 0xFFu
43#define ARIBC_ALPHA_DEFAULT_BACK 0x80u
44
45#define ARIBCC_COLOR_RGB(c) ((c) & 0xFFFFFF)
46#define ARIBCC_COLOR_DIFF_RGB(c1,c2) (((c1) ^ (c2)) & 0x00FFFFFF)
47#define ARIBCC_COLOR_DIFF_A(c1,c2) (((c1) ^ (c2)) & 0xFF000000)
48
49#define CLUT_RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
50#define CLUT_A(c) (((c) >> 24) & 0xFF)
51#define CLUT_R(c) (((c) >> 16) & 0xFF)
52#define CLUT_G(c) (((c) >> 8) & 0xFF)
53#define CLUT_B(c) ( (c) & 0xFF)
54
55#define ARIBCC_COLOR_TO_CLUT_RGBA(c,a) (((ARIBCC_COLOR_A(c) ? ARIBCC_COLOR_A(c) : (a)) << 24) | \
56 (ARIBCC_COLOR_R(c) << 16) | \
57 (ARIBCC_COLOR_G(c) << 8) | \
58 (ARIBCC_COLOR_B(c)))
59
105
106static void hex_dump_debug(void *ctx, const char *buf, int buf_size)
107{
108 int i;
109
110 for (i = 0; i < buf_size; i++) {
111 ff_dlog(ctx, "%02hhx ", buf[i]);
112 if (i % 16 == 15)
113 ff_dlog(ctx, "\n");
114 }
115 if (i % 16)
116 ff_dlog(ctx, "\n");
117}
118
119static void logcat_callback(aribcc_loglevel_t level, const char* message, void* userdata)
120{
121 ARIBCaptionContext *ctx = userdata;
122 int lvl;
123
124 if (ctx->decoder != NULL) {
125 switch (level) {
126 case ARIBCC_LOGLEVEL_ERROR:
127 lvl = AV_LOG_ERROR;
128 break;
129 case ARIBCC_LOGLEVEL_WARNING:
130 lvl = AV_LOG_WARNING;
131 break;
132 default:
133 lvl = AV_LOG_INFO;
134 }
135
136 av_log(ctx, lvl, "%s\n", message);
137 }
138}
139
141{
142 if (ctx->avctx->width > 0 && ctx->avctx->height > 0) {
143 /* input video size specified by -canvas_size option */
144 ctx->bitmap_plane_width = ctx->avctx->width;
145 ctx->bitmap_plane_height = ctx->avctx->height;
146 } else if (ctx->plane_width == 960) {
147 /* ARIB TR-B14 Fascicle 2 Volume 3 [Section 2] 4.3.1 */
148 /* ARIB TR-B14 Fascicle 2 Volume 3 [Section 2] Appendix-4 */
149 ctx->bitmap_plane_width = 1440;
150 ctx->bitmap_plane_height = 1080;
151 } else {
152 ctx->bitmap_plane_width = ctx->plane_width;
153 ctx->bitmap_plane_height = ctx->plane_height;
154 }
155 /* Expand either width or height */
156 if (ctx->bitmap_plane_height * ctx->plane_width > ctx->bitmap_plane_width * ctx->plane_height) {
157 ctx->frame_height = ctx->bitmap_plane_height;
158 ctx->frame_width = ctx->frame_height * ctx->plane_width / ctx->plane_height;
159 } else {
160 ctx->frame_width = ctx->bitmap_plane_width;
161 ctx->frame_height = ctx->frame_width * ctx->plane_height / ctx->plane_width;
162 }
163}
164
166{
167 int i;
168
169 for (i = 0; i < ARIBC_ALPHA_MAX_NUM; i++) {
170 if (ctx->clut_alpha[i] == 0) {
171 ctx->clut_alpha[i] = a;
172 return;
173 }
174 if (ctx->clut_alpha[i] == a)
175 return;
176 }
177 return;
178}
179
181{
182 int i, j, d;
183
184 if (a == 0)
185 return a;
186 d = 256;
187 j = 0;
188 for (i = 0; i < ARIBC_ALPHA_MAX_NUM; i++) {
189 if (ctx->clut_alpha[i] == a)
190 return a;
191 if (ctx->clut_alpha[i] == 0)
192 break;
193 if (abs((int)a - (int)ctx->clut_alpha[i]) < d) {
194 d = abs((int)a - (int)ctx->clut_alpha[i]);
195 j = i;
196 }
197 }
198 return ctx->clut_alpha[j];
199}
200
201static int clut_find(ARIBCaptionContext *ctx, uint32_t rgba)
202{
203 int i;
204
205 for (i = 0; i < ctx->clut_idx; i++) {
206 if (ctx->clut[i] == rgba)
207 return i;
208 }
209 return -1;
210}
211
212static inline int clut_color_distance(uint32_t rgba1, uint32_t rgba2)
213{
214 return abs((int)CLUT_R(rgba1) - (int)CLUT_R(rgba2)) +
215 abs((int)CLUT_G(rgba1) - (int)CLUT_G(rgba2)) +
216 abs((int)CLUT_B(rgba1) - (int)CLUT_B(rgba2));
217}
218
219static uint8_t clut_pick_or_set(ARIBCaptionContext *ctx, int r, int g, int b, int a)
220{
221 int c, i, d, d_min;
222 uint32_t rgba;
223
225 if (a == 0)
226 return 0; /* transparent */
227 rgba = CLUT_RGBA(r,g,b,a);
228
229 d_min = 256 * 3;
230 c = 0;
231 for (i = 0; i < ctx->clut_idx; i++) {
232 if (ctx->clut[i] == rgba)
233 return i;
234 if (CLUT_A(ctx->clut[i]) != a)
235 continue;
236 d = clut_color_distance(ctx->clut[i], rgba);
237 if (d < d_min) {
238 d_min = d;
239 c = i;
240 }
241 }
242 if (d_min > 3) {
243 if (ctx->clut_idx >= AVPALETTE_COUNT)
244 ctx->clut_overflow++;
245 else {
246 c = ctx->clut_idx;
247 ctx->clut[ctx->clut_idx++] = rgba;
248 }
249 }
250 return c;
251}
252
253/* initialize CLUT with only the transparent color */
255{
256 ctx->clut[0] = CLUT_RGBA(0,0,0,0); /* transparent */
257 memset(ctx->clut_alpha, 0, sizeof(ctx->clut_alpha));
258 ctx->clut_alpha[0] = 0xFF;
259 ctx->clut_idx = 1;
260 ctx->clut_overflow = 0;
261}
262
263/* add a color to the CLUT unless it is already there or the CLUT is full */
264static void clut_add(ARIBCaptionContext *ctx, uint32_t rgba)
265{
266 if (clut_find(ctx, rgba) >= 0)
267 return;
268 if (ctx->clut_idx >= AVPALETTE_COUNT) {
269 ctx->clut_overflow++;
270 return;
271 }
272 ctx->clut[ctx->clut_idx++] = rgba;
273 clut_set_alpha(ctx, CLUT_A(rgba));
274}
275
276/* whether a region is left out: no character, or ruby when ruby is ignored */
277static int region_left_out(const aribcc_caption_region_t *region, int ignore_ruby)
278{
279 return !region->char_count || (region->is_ruby && ignore_ruby);
280}
281
283 const aribcc_caption_region_t *region)
284{
285 return region_left_out(region, ctx->ignore_ruby);
286}
287
288/* add the character colors of a region to the CLUT */
289static void clut_add_region(ARIBCaptionContext *ctx, aribcc_caption_region_t *region)
290{
291 for (int i = 0; i < region->char_count; i++) {
292 aribcc_caption_char_t *ch = &region->chars[i];
296 }
297}
298
299/**
300 * aribcaption_trans_{bitmap|ass|text}_subtitle()
301 *
302 * Transfer decoded subtitle to AVSubtitle with corresponding subtitle type.
303 *
304 * @param ctx pointer to the ARIBCaptionContext
305 * @return > 0 subtitle event produced (possibly with zero rectangles for clear)
306 * = 0 no subtitle
307 * < 0 error code
308 */
310{
311 int ret = 0;
312 AVSubtitle *sub = ctx->sub;
313 int status, rect_idx, image_count = 0, seed_idx, seed_overflow;
314 int old_width = ctx->frame_width;
315 int old_height = ctx->frame_height;
316
317 if (ctx->caption.plane_width > 0 && ctx->caption.plane_height > 0) {
318 ctx->plane_width = ctx->caption.plane_width;
319 ctx->plane_height = ctx->caption.plane_height;
320 }
322 if (ctx->frame_width != old_width || ctx->frame_height != old_height) {
323 ff_dlog(ctx, "canvas: %dx%d plane: %dx%d bitmap: %dx%d frame: %dx%d\n",
324 ctx->avctx->width, ctx->avctx->height,
325 ctx->plane_width, ctx->plane_height,
326 ctx->bitmap_plane_width, ctx->bitmap_plane_height,
327 ctx->frame_width, ctx->frame_height);
328 if (!aribcc_renderer_set_frame_size(ctx->renderer,
329 ctx->frame_width, ctx->frame_height)) {
331 "aribcc_renderer_set_frame_size() returned with error.\n");
332 return AVERROR_EXTERNAL;
333 }
334 }
335
336 status = aribcc_renderer_append_caption(ctx->renderer, &ctx->caption);
337 if (!status) {
339 "aribcc_renderer_append_caption() returned with error.\n");
340 return AVERROR_EXTERNAL;
341 }
342
343 status = aribcc_renderer_render(ctx->renderer, ctx->pts, &ctx->render_result);
344 switch (status) {
345 case ARIBCC_RENDER_STATUS_GOT_IMAGE:
346 break;
347
348 case ARIBCC_RENDER_STATUS_GOT_IMAGE_UNCHANGED:
349 aribcc_render_result_cleanup(&ctx->render_result);
350 ff_dlog(ctx, "got image unchanged\n");
351 return 0;
352
353 case ARIBCC_RENDER_STATUS_NO_IMAGE:
354 /* the result holds no image, handled below */
355 break;
356
357 case ARIBCC_RENDER_STATUS_ERROR:
359 "aribcc_renderer_render() returned with error.\n");
360 return AVERROR_EXTERNAL;
361
362 default:
363 aribcc_render_result_cleanup(&ctx->render_result);
365 "aribcc_renderer_render() returned unknown status: %d\n", status);
366 return AVERROR_EXTERNAL;
367 }
368
369 if (!ctx->render_result.image_count || ctx->render_result.images == NULL) {
370 ff_dlog(ctx, "no image (%d)\n", ctx->render_result.image_count);
371 aribcc_render_result_cleanup(&ctx->render_result);
372 /* A caption carrying an explicit clear screen (CS) may produce no
373 image. Report an empty subtitle so that the previously displayed
374 bitmap is erased; the TEXT path already does this by emitting "".
375 Do not treat every caption without image as a clear: a statement
376 containing only TIME also decodes to zero regions but must not
377 erase the display. */
378 if (ctx->caption.flags & ARIBCC_CAPTIONFLAGS_CLEARSCREEN) {
379 sub->format = 0; /* graphic */
380 sub->num_rects = 0;
381 return 1;
382 }
383 return 0;
384 }
385
386 image_count = ctx->render_result.image_count;
387 sub->format = 0; /* graphic */
388 sub->rects = av_calloc(image_count, sizeof(*sub->rects));
389 if (!sub->rects) {
390 ret = AVERROR(ENOMEM);
391 goto fail;
392 }
393 for (int i = 0; i < image_count; i++) {
394 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
395 if (!sub->rects[i]) {
396 ret = AVERROR(ENOMEM);
397 goto fail;
398 }
399 }
400
401 /* The renderer drops ruby regions with ignore_ruby and regions too small
402 to draw, without telling which region an image comes from. Seed the
403 CLUT with the colors of every region that may be drawn, once. Each
404 image only appends colors past the seed, so dropping them restores it. */
406 for (int i = 0; i < ctx->caption.region_count; i++)
407 if (!region_skipped(ctx, &ctx->caption.regions[i]))
408 clut_add_region(ctx, &ctx->caption.regions[i]);
409 seed_idx = ctx->clut_idx;
410 seed_overflow = ctx->clut_overflow;
411
412 for (rect_idx = 0; rect_idx < image_count; rect_idx++) {
413 AVSubtitleRect *rect = sub->rects[rect_idx];
414 aribcc_image_t *image = &ctx->render_result.images[rect_idx];
415 int w, h, shrink_height, dst_idx;
416
417 ctx->clut_idx = seed_idx;
418 ctx->clut_overflow = seed_overflow;
419
420 rect->w = image->width * ctx->bitmap_plane_width / ctx->frame_width;
421 rect->h = image->height * ctx->bitmap_plane_height / ctx->frame_height;
422 rect->data[0] = av_mallocz(rect->w * rect->h);
423 if (!rect->data[0]) {
424 ret = AVERROR(ENOMEM);
425 goto fail;
426 }
427 if ((image->height != rect->h && image->width != rect->w) ||
428 image->stride < image->width * 4 ||
429 image->stride * image->height > image->bitmap_size) {
430 av_log(ctx, AV_LOG_ERROR, "Bug: unexpected rendered image: %d(%d)x%d -> %dx%d\n",
431 image->width, image->stride / 4, image->height, rect->w, rect->h);
433 goto fail;
434 }
435
436 shrink_height = image->height != rect->h;
437 dst_idx = 0;
438 for (h = 0; h < rect->h; h++) {
439 for (w = 0; w < rect->w; w++) {
440 /* Bi-linear interpolation */
441 int n, m, idx0, idx1, r, g, b, a;
442 if (shrink_height) {
443 int div_a, y0, y1;
444 div_a = h * ctx->frame_height;
445 n = ctx->bitmap_plane_height;
446 y0 = div_a / n;
447 y1 = FFMIN(y0 + 1, image->height - 1);
448 m = div_a - n * y0;
449 idx0 = image->stride * y0 + w * 4;
450 idx1 = image->stride * y1 + w * 4;
451 } else {
452 int div_a, x0, x1;
453 div_a = w * ctx->frame_width;
454 n = ctx->bitmap_plane_width;
455 x0 = div_a / n;
456 x1 = FFMIN(x0 + 1, image->width - 1);
457 m = div_a - n * x0;
458 idx0 = image->stride * h + x0 * 4;
459 idx1 = image->stride * h + x1 * 4;
460 }
461 r = (image->bitmap[idx0++] * (n - m) + image->bitmap[idx1++] * m) / n;
462 g = (image->bitmap[idx0++] * (n - m) + image->bitmap[idx1++] * m) / n;
463 b = (image->bitmap[idx0++] * (n - m) + image->bitmap[idx1++] * m) / n;
464 a = (image->bitmap[idx0++] * (n - m) + image->bitmap[idx1++] * m) / n;
465 rect->data[0][dst_idx++] = clut_pick_or_set(ctx, r, g, b, a);
466 }
467 }
468 rect->data[1] = av_memdup(ctx->clut, AVPALETTE_SIZE);
469 if (!rect->data[1]) {
470 ret = AVERROR(ENOMEM);
471 goto fail;
472 }
473
474 if (ctx->avctx->profile == AV_PROFILE_ARIB_PROFILE_C) {
475 /* ARIB TR-B14 version 3.8 Fascicle 1-(2/2) Volume 3 [Section 4] */
476 /* No position information is provided for profile C */
477 rect->x = (ctx->frame_width - rect->w) / 2;
478 rect->y = ctx->frame_height - rect->h * (image_count - rect_idx);
479 } else {
480 rect->x = image->dst_x * ctx->bitmap_plane_width / ctx->frame_width;
481 rect->y = image->dst_y * ctx->bitmap_plane_height / ctx->frame_height;
482 }
483 rect->type = SUBTITLE_BITMAP;
484 rect->linesize[0] = rect->w;
485 rect->nb_colors = 256;
486
487 ff_dlog(ctx, "BITMAP subtitle (%d,%d) %dx%d -> (%d,%d) %dx%d [%d]: %d colors\n",
488 image->dst_x, image->dst_y, image->width, image->height,
489 rect->x, rect->y, rect->w, rect->h,
490 rect_idx, ctx->clut_idx);
491 if (ctx->clut_overflow)
492 av_log(ctx, AV_LOG_WARNING, "CLUT overflow (%d).\n", ctx->clut_overflow);
493 }
494 sub->num_rects = rect_idx;
495 aribcc_render_result_cleanup(&ctx->render_result);
496
497 return rect_idx;
498
499fail:
500 if (sub->rects) {
501 for (int i = 0; i < image_count; i++) {
502 if (sub->rects[i]) {
503 av_freep(&sub->rects[i]->data[0]);
504 av_freep(&sub->rects[i]->data[1]);
505 av_freep(&sub->rects[i]);
506 }
507 }
508 av_freep(&sub->rects);
509 }
510 sub->num_rects = 0;
511 aribcc_render_result_cleanup(&ctx->render_result);
512
513 return ret;
514}
515
517{
518 AVCodecContext *avctx = ctx->avctx;
519 int outline, shadow;
520 const char *font_name;
521 const char *fonts = ctx->font;
522
523 if (ctx->border_style == 4) {
524 outline = 0;
525 shadow = 4;
526 } else {
527 outline = 1;
528 shadow = 0;
529 }
530 if (ctx->force_stroke_text)
531 outline = (int)(ctx->stroke_width * 4.0 / 3.0);
532
533 if (fonts && *fonts)
534 font_name = av_get_token(&fonts, ",");
535 else
536 font_name = av_strdup(DEFAULT_FONT_ASS);
537 if (!font_name)
538 return AVERROR(ENOMEM);
539
540 av_freep(&avctx->subtitle_header);
542 "[Script Info]\n"
543 "ScriptType: v4.00+\n"
544 "PlayResX: %d\n"
545 "PlayResY: %d\n"
546 "WrapStyle: 2\n" /* 2: no word wrapping */
547 "\n"
548
549 "[V4+ Styles]\n"
550 "Format: Name, "
551 "Fontname, Fontsize, "
552 "PrimaryColour, SecondaryColour, OutlineColour, BackColour, "
553 "Bold, Italic, Underline, StrikeOut, "
554 "ScaleX, ScaleY, "
555 "Spacing, Angle, "
556 "BorderStyle, Outline, Shadow, "
557 "Alignment, MarginL, MarginR, MarginV, "
558 "Encoding\n"
559
560 "Style: "
561 "Default," /* Name */
562 "%s,%d," /* Font{name,size} */
563 "&H%x,&H%x,&H%x,&H%x," /* {Primary,Secondary,Outline,Back}Colour */
564 "%d,%d,%d,0," /* Bold, Italic, Underline, StrikeOut */
565 "100,100," /* Scale{X,Y} */
566 "0,0," /* Spacing, Angle */
567 "%d,%d,%d," /* BorderStyle, Outline, Shadow */
568 "%d,10,10,10," /* Alignment, Margin[LRV] */
569 "0\n" /* Encoding */
570 "\n"
571
572 "[Events]\n"
573 "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n",
574 ctx->plane_width, ctx->plane_height,
575 font_name, ctx->font_size,
579 ctx->border_style, outline, shadow, ASS_DEFAULT_ALIGNMENT);
580
581 av_freep(&font_name);
582 if (!avctx->subtitle_header)
583 return AVERROR(ENOMEM);
584 avctx->subtitle_header_size = strlen(avctx->subtitle_header);
585 return 0;
586}
587
588static void set_ass_color(AVBPrint *buf, int color_num,
589 aribcc_color_t new_color, aribcc_color_t old_color)
590{
591 if (ARIBCC_COLOR_DIFF_RGB(new_color, old_color))
592 av_bprintf(buf, "{\\%dc&H%06x&}", color_num,
593 ARIBCC_COLOR_RGB(new_color));
594 if (ARIBCC_COLOR_DIFF_A(new_color, old_color))
595 av_bprintf(buf, "{\\%da&H%02x&}", color_num,
596 0xFF - ARIBCC_COLOR_A(new_color));
597}
598
599/* position of a region, keeping the existing wrapping of negative values
600 in ass mode */
602 const aribcc_caption_region_t *region, int *x, int *y)
603{
604 *x = region->x < 0 ? region->x + ctx->plane_width : region->x;
605 *y = region->y < 0 ? region->y + ctx->plane_height : region->y;
606}
607
609{
610 AVSubtitle *sub = ctx->sub;
611 AVBPrint buf;
612 bool single_rect = ctx->ass_single_rect;
613 bool emitted = false;
614 int ret = 0, rect_idx;
615
616 if (ctx->caption.plane_width > 0 && ctx->caption.plane_height > 0 &&
617 (ctx->caption.plane_width != ctx->plane_width ||
618 ctx->caption.plane_height != ctx->plane_height)) {
619 ctx->plane_width = ctx->caption.plane_width;
620 ctx->plane_height = ctx->caption.plane_height;
621 if ((ret = set_ass_header(ctx)) < 0)
622 return ret;
623 }
624
625 /* ARIB TR-B14 version 3.8 Fascicle 1-(2/2) Volume 3 [Section 4] */
626 /* No position information is provided for profile C */
627 if (ctx->avctx->profile == AV_PROFILE_ARIB_PROFILE_C)
628 single_rect = true;
629
630 sub->format = 1; /* text */
631
633
634 if (single_rect && ctx->avctx->profile != AV_PROFILE_ARIB_PROFILE_C) {
635 int x, y, rx, ry;
636 x = ctx->plane_width;
637 y = ctx->plane_height;
638 for (int i = 0; i < ctx->caption.region_count; i++) {
639 if (region_skipped(ctx, &ctx->caption.regions[i]))
640 continue;
641 region_position(ctx, &ctx->caption.regions[i], &rx, &ry);
642 if (rx < x)
643 x = rx;
644 if (ry < y)
645 y = ry;
646 }
647 av_bprintf(&buf, "{\\an7}");
648 if (x > 0 || y > 0)
649 av_bprintf(&buf, "{\\pos(%d,%d)}", x, y);
650 }
651
652 rect_idx = 0;
653 for (int i = 0; i < ctx->caption.region_count; i++) {
654 aribcc_caption_region_t *region = &ctx->caption.regions[i];
655 aribcc_color_t text_color = ARIBCC_MAKE_RGBA(0xFF, 0xFF, 0xFF,
657 aribcc_color_t stroke_color = ARIBCC_MAKE_RGBA(0, 0, 0,
659 aribcc_color_t back_color = ARIBCC_MAKE_RGBA(0, 0, 0,
661 aribcc_charstyle_t charstyle = ctx->charstyle;
662 int char_width = ctx->font_size;
663 int char_height = ctx->font_size;
664 int char_horizontal_spacing = 0;
665
666 if (region_skipped(ctx, region))
667 continue;
668
669 if (single_rect && emitted)
670 av_bprintf(&buf, "{\\r}\\N");
671 emitted = true;
672
673 if (!single_rect) {
674 int x, y;
675 region_position(ctx, region, &x, &y);
676 av_bprint_clear(&buf);
677 av_bprintf(&buf, "{\\an7}");
678 if (x > 0 || y > 0)
679 av_bprintf(&buf, "{\\pos(%d,%d)}", x, y);
680 }
681 if (region->is_ruby)
682 av_bprintf(&buf, "{\\fs%d}", char_height / 2);
683
684 for (int j = 0; j < region->char_count; j++) {
685 aribcc_caption_char_t *ch = &region->chars[j];
686
687 if (ctx->avctx->profile != AV_PROFILE_ARIB_PROFILE_C) {
688 if (ch->char_horizontal_spacing != char_horizontal_spacing) {
689 av_bprintf(&buf, "{\\fsp%d}", (region->is_ruby) ?
690 ch->char_horizontal_spacing / 2 :
691 ch->char_horizontal_spacing);
692 char_horizontal_spacing = ch->char_horizontal_spacing;
693 }
694 if (ch->char_width != char_width) {
695 av_bprintf(&buf, "{\\fscx%"PRId64"}",
696 av_rescale(ch->char_width, 100, ctx->font_size));
697 char_width = ch->char_width;
698 }
699 if (ch->char_height != char_height) {
700 av_bprintf(&buf, "{\\fscy%"PRId64"}",
701 av_rescale(ch->char_height, 100, ctx->font_size));
702 char_height = ch->char_height;
703 }
704 }
705 if (ch->style != charstyle) {
706 aribcc_charstyle_t diff = ch->style ^ charstyle;
707 if (diff & ARIBCC_CHARSTYLE_STROKE) {
708 if (charstyle & ARIBCC_CHARSTYLE_STROKE) {
709 if (ctx->force_stroke_text)
710 av_bprintf(&buf, "{\\bord%d}",
711 (int)(ctx->stroke_width * 4.0 / 3.0));
712 else
713 av_bprintf(&buf, "{\\bord0}");
714 } else
715 av_bprintf(&buf, "{\\bord3}");
716 }
717 if (diff & ARIBCC_CHARSTYLE_BOLD) {
718 if (charstyle & ARIBCC_CHARSTYLE_BOLD)
719 av_bprintf(&buf, "{\\b0}");
720 else
721 av_bprintf(&buf, "{\\b1}");
722 }
723 if (diff & ARIBCC_CHARSTYLE_ITALIC) {
724 if (charstyle & ARIBCC_CHARSTYLE_ITALIC)
725 av_bprintf(&buf, "{\\i0}");
726 else
727 av_bprintf(&buf, "{\\i1}");
728 }
729 if (diff & ARIBCC_CHARSTYLE_UNDERLINE) {
730 if (charstyle & ARIBCC_CHARSTYLE_UNDERLINE)
731 av_bprintf(&buf, "{\\u0}");
732 else
733 av_bprintf(&buf, "{\\u1}");
734 }
735 charstyle = ch->style;
736 }
737 if (ch->text_color != text_color) {
738 set_ass_color(&buf, 1, ch->text_color, text_color);
739 text_color = ch->text_color;
740 }
741 if (ch->stroke_color != stroke_color) {
742 set_ass_color(&buf, 3, ch->stroke_color, stroke_color);
743 stroke_color = ch->stroke_color;
744 }
745 if (ch->back_color != back_color) {
746 if (ctx->border_style == 4)
747 set_ass_color(&buf, 4, ch->back_color, back_color);
748 else
749 set_ass_color(&buf, 3, ch->back_color, back_color);
750 back_color = ch->back_color;
751 }
752 if (region->chars[j].type == ARIBCC_CHARTYPE_DRCS)
753 av_bprintf(&buf, "\xe3\x80\x93"); /* Geta Mark */
754 else
755 ff_ass_bprint_text_event(&buf, ch->u8str, strlen(ch->u8str), "", 0);
756 }
757
758 if (single_rect) {
759 ff_dlog(ctx, "ASS subtitle%s (%d,%d) %dx%d [region %d]\n",
760 (region->is_ruby) ? " (ruby)" : "",
761 region->x, region->y, region->width, region->height, i);
762 } else {
763 if (!av_bprint_is_complete(&buf)) {
764 ret = AVERROR(ENOMEM);
765 goto fail;
766 }
767 ff_dlog(ctx, "ASS subtitle%s (%d,%d) %dx%d [%d]: %s\n",
768 (region->is_ruby) ? " (ruby)" : "",
769 region->x, region->y, region->width, region->height,
770 rect_idx, buf.str);
771
772 ret = ff_ass_add_rect(sub, buf.str, ctx->readorder++, 0 , NULL, NULL);
773 if (ret != 0)
774 goto fail;
775 rect_idx++;
776 }
777 }
778 if (!emitted) {
779 /* Nothing to show: there is no region, or every region was skipped
780 as ruby. Only an explicit clear screen (CS) erases the previous
781 caption, for indefinite duration; a statement containing only
782 TIME also has no region. */
783 if (!(ctx->caption.flags & ARIBCC_CAPTIONFLAGS_CLEARSCREEN)) {
785 return 0;
786 }
787 ret = ff_ass_add_rect(sub, "", ctx->readorder++, 0, NULL, NULL);
788 if (ret < 0)
789 goto fail;
791 return 1;
792 }
793 if (single_rect) {
794 if (!av_bprint_is_complete(&buf)) {
795 ret = AVERROR(ENOMEM);
796 goto fail;
797 }
798 ff_dlog(ctx, "ASS subtitle: %s\n", buf.str);
799
800 ret = ff_ass_add_rect(sub, buf.str, ctx->readorder++, 0 , NULL, NULL);
801 if (ret != 0)
802 goto fail;
803 rect_idx++;
804 }
805
807 return rect_idx;
808
809fail:
810 if (sub->rects) {
811 for (int i = 0; i < sub->num_rects; i++) {
812 if (sub->rects[i]) {
813 av_freep(&sub->rects[i]->ass);
814 av_freep(&sub->rects[i]);
815 }
816 }
817 av_freep(&sub->rects);
818 }
819 sub->num_rects = 0;
821
822 return ret;
823}
824
826{
827 AVSubtitle *sub = ctx->sub;
829 int ret = 0, has_text = 0;
830 const char *text = ctx->caption.text ? ctx->caption.text : "";
831
832 /* The text leaves ruby out (except a placeholder for an unknown DRCS)
833 and may hold line breaks without any character, so decide on the
834 regions. */
835 for (int i = 0; i < ctx->caption.region_count && !has_text; i++)
836 has_text = !region_left_out(&ctx->caption.regions[i], 1);
837
838 if (!has_text) {
839 /* Nothing to show: there is no region, or only ruby. Only an
840 explicit clear screen (CS) erases the previous caption, with an
841 empty text; a statement containing only TIME also has no region. */
842 if (!(ctx->caption.flags & ARIBCC_CAPTIONFLAGS_CLEARSCREEN))
843 return 0;
844 text = "";
845 }
846
847 sub->rects = av_calloc(1, sizeof(*sub->rects));
848 if (!sub->rects) {
849 ret = AVERROR(ENOMEM);
850 goto fail;
851 }
852 sub->num_rects = 1;
853
854 sub->rects[0] = av_mallocz(sizeof(*sub->rects[0]));
855 if (!sub->rects[0]) {
856 ret = AVERROR(ENOMEM);
857 goto fail;
858 }
859 rect = sub->rects[0];
860
861 ff_dlog(ctx, "TEXT subtitle: %s\n", text);
862 rect->text = av_strdup(text);
863 if (!rect->text) {
864 ret = AVERROR(ENOMEM);
865 goto fail;
866 }
867
868 sub->format = 1; /* text */
869 rect->type = SUBTITLE_TEXT;
870
871 return 1;
872
873fail:
874 if (sub->rects) {
875 rect = sub->rects[0];
876 if (rect) {
877 av_freep(&rect->text);
878 av_freep(&rect);
879 }
880 av_freep(&sub->rects);
881 }
882 sub->num_rects = 0;
883
884 return ret;
885}
886
888 int *got_sub_ptr, const AVPacket *avpkt)
889{
891 int status;
892
893 ff_dlog(ctx, "ARIB caption packet pts=%"PRIx64":\n", avpkt->pts);
894 if (sub->num_rects) {
895 avpriv_request_sample(ctx, "Different Version of Segment asked Twice");
897 }
898 hex_dump_debug(ctx, avpkt->data, avpkt->size);
899
900 ctx->sub = sub;
901 ctx->avpkt = avpkt;
902 ctx->time_base = avctx->pkt_timebase;
903 if (ctx->time_base.num <= 0 || ctx->time_base.den <= 0) {
904 av_log(ctx, AV_LOG_VERBOSE, "No timebase set. assuming 90kHz.\n");
905 ctx->time_base = av_make_q(1, 90000);
906 }
907 if (avpkt->pts == AV_NOPTS_VALUE)
908 ctx->pts = ARIBCC_PTS_NOPTS;
909 else
910 ctx->pts = av_rescale_q(avpkt->pts, ctx->time_base, (AVRational){1, 1000});
911
912 status = aribcc_decoder_decode(ctx->decoder, avpkt->data, avpkt->size,
913 ctx->pts, &ctx->caption);
914 if (status == ARIBCC_DECODE_STATUS_ERROR) {
916 "aribcc_decoder_decode() returned with error.\n");
917 return AVERROR(EAGAIN);
918 }
919 if (status == ARIBCC_DECODE_STATUS_NO_CAPTION) {
920 ff_dlog(ctx, "No caption.\n");
921 return avpkt->size;
922 } else {
923 ff_dlog(ctx, "type=%02x, flags=%x, lang=%03x\n",
924 ctx->caption.type, ctx->caption.flags, ctx->caption.iso6392_language_code);
925 ff_dlog(ctx, "region count = %d, start=%d.%d, duration=%d.%d\n",
926 ctx->caption.region_count,
927 (int)(ctx->caption.pts / 1000), (int)(ctx->caption.pts % 1000),
928 (int)((ctx->caption.wait_duration == ARIBCC_DURATION_INDEFINITE) ?
929 -1 : ctx->caption.wait_duration / 1000),
930 (int)((ctx->caption.wait_duration == ARIBCC_DURATION_INDEFINITE) ?
931 0 : ctx->caption.wait_duration % 1000));
932 }
933
934 switch ((enum AVSubtitleType) ctx->subtitle_type) {
935 case SUBTITLE_TEXT:
937 break;
938
939 case SUBTITLE_ASS:
941 break;
942
943 case SUBTITLE_BITMAP:
945 break;
946
947 case SUBTITLE_NONE:
948 default:
949 status = 0;
950 }
951
952 if (status < 0) {
953 av_log(ctx, AV_LOG_ERROR, "Failed to set Subtitle: %s\n",
954 av_err2str(status));
955 aribcc_caption_cleanup(&ctx->caption);
956 return status;
957 }
958 if (status > 0) {
959 *got_sub_ptr = 1;
960 if (ctx->avpkt->pts != AV_NOPTS_VALUE)
961 sub->pts = av_rescale_q(ctx->avpkt->pts,
962 ctx->time_base, AV_TIME_BASE_Q);
963 if (ctx->caption.wait_duration == ARIBCC_DURATION_INDEFINITE)
964 sub->end_display_time = UINT32_MAX;
965 else
966 sub->end_display_time = (uint32_t)ctx->caption.wait_duration;
967 }
968
969 aribcc_caption_cleanup(&ctx->caption);
970 return avpkt->size;
971}
972
974{
976
977 if (ctx->decoder)
978 aribcc_decoder_flush(ctx->decoder);
979 if (ctx->renderer)
980 aribcc_renderer_flush(ctx->renderer);
981 if (!(avctx->flags2 & AV_CODEC_FLAG2_RO_FLUSH_NOOP))
982 ctx->readorder = 0;
983}
984
986{
988
989 av_freep(&ctx->clut);
990 if (ctx->renderer)
991 aribcc_renderer_free(ctx->renderer);
992 if (ctx->decoder)
993 aribcc_decoder_free(ctx->decoder);
994 if (ctx->context)
995 aribcc_context_free(ctx->context);
996
997 return 0;
998}
999
1001{
1003 aribcc_profile_t profile;
1004 int ret = 0;
1005
1006 ctx->avctx = avctx;
1007
1008 switch (avctx->profile) {
1010 profile = ARIBCC_PROFILE_A;
1011 /* assume 960x540 at initial state */
1012 ctx->plane_width = 960;
1013 ctx->plane_height = 540;
1014 ctx->font_size = 36;
1015 break;
1017 profile = ARIBCC_PROFILE_C;
1018 ctx->plane_width = 320;
1019 ctx->plane_height = 180;
1020 ctx->font_size = 16;
1021 break;
1022 default:
1023 av_log(avctx, AV_LOG_ERROR, "Unknown or unsupported profile set.\n");
1024 return AVERROR(EINVAL);
1025 }
1026 /* determine BorderStyle of ASS header */
1027 if (ctx->ignore_background)
1028 ctx->border_style = 1;
1029 else
1030 ctx->border_style = 4;
1031 ctx->charstyle = ARIBCC_CHARSTYLE_DEFAULT;
1032 if (ctx->force_stroke_text || ctx->ignore_background)
1033 ctx->charstyle |= ARIBCC_CHARSTYLE_STROKE;
1034
1035 if (!(ctx->context = aribcc_context_alloc())) {
1036 av_log(avctx, AV_LOG_ERROR, "Failed to alloc libaribcaption context.\n");
1037 return AVERROR_EXTERNAL;
1038 }
1039 aribcc_context_set_logcat_callback(ctx->context, logcat_callback, avctx);
1040 if (!(ctx->decoder = aribcc_decoder_alloc(ctx->context))) {
1041 av_log(avctx, AV_LOG_ERROR, "Failed to alloc libaribcaption decoder.\n");
1042 return AVERROR_EXTERNAL;
1043 }
1044 if (!aribcc_decoder_initialize(ctx->decoder,
1045 (enum aribcc_encoding_scheme_t) ctx->encoding_scheme,
1046 ARIBCC_CAPTIONTYPE_CAPTION,
1047 profile,
1048 ARIBCC_LANGUAGEID_FIRST)) {
1049 av_log(avctx, AV_LOG_ERROR, "Failed to initialize libaribcaption decoder.\n");
1050 return AVERROR_EXTERNAL;
1051 }
1052 aribcc_decoder_set_replace_msz_fullwidth_ascii(ctx->decoder,
1053 ctx->replace_msz_ascii);
1054 aribcc_decoder_set_replace_msz_fullwidth_japanese(ctx->decoder,
1055 ctx->replace_msz_japanese);
1056
1057 /* Similar behavior as ffmpeg tool to set canvas size */
1058 if (ctx->canvas_width > 0 && ctx->canvas_height > 0 &&
1059 (ctx->avctx->width == 0 || ctx->avctx->height == 0)) {
1060 ctx->avctx->width = ctx->canvas_width;
1061 ctx->avctx->height = ctx->canvas_height;
1062 }
1063
1064 switch ((enum AVSubtitleType) ctx->subtitle_type) {
1065 case SUBTITLE_ASS:
1067 if (ret != 0) {
1068 av_log(avctx, AV_LOG_ERROR, "Failed to set ASS header: %s\n",
1069 av_err2str(ret));
1070 return ret;
1071 }
1072 break;
1073
1074 case SUBTITLE_BITMAP:
1075 if(!(ctx->renderer = aribcc_renderer_alloc(ctx->context))) {
1076 av_log(avctx, AV_LOG_ERROR, "Failed to alloc libaribcaption renderer.\n");
1077 return AVERROR_EXTERNAL;
1078 }
1079 if(!aribcc_renderer_initialize(ctx->renderer,
1080 ARIBCC_CAPTIONTYPE_CAPTION,
1081 ARIBCC_FONTPROVIDER_TYPE_AUTO,
1082 ARIBCC_TEXTRENDERER_TYPE_AUTO)) {
1083 av_log(avctx, AV_LOG_ERROR, "Failed to initialize libaribcaption renderer.\n");
1084 return AVERROR_EXTERNAL;
1085 }
1087 ff_dlog(ctx, "canvas: %dx%d plane: %dx%d bitmap: %dx%d frame: %dx%d\n",
1088 ctx->avctx->width, ctx->avctx->height,
1089 ctx->plane_width, ctx->plane_height,
1090 ctx->bitmap_plane_width, ctx->bitmap_plane_height,
1091 ctx->frame_width, ctx->frame_height);
1092 if (!aribcc_renderer_set_frame_size(ctx->renderer,
1093 ctx->frame_width, ctx->frame_height)) {
1095 "aribcc_renderer_set_frame_size() returned with error.\n");
1096 return AVERROR_EXTERNAL;
1097 }
1098
1099 if (!(ctx->clut = av_mallocz(AVPALETTE_SIZE)))
1100 return AVERROR(ENOMEM);
1101
1102 aribcc_renderer_set_storage_policy(ctx->renderer, ARIBCC_CAPTION_STORAGE_POLICY_MINIMUM, 0);
1103 aribcc_renderer_set_replace_drcs(ctx->renderer, ctx->replace_drcs);
1104 aribcc_renderer_set_force_stroke_text(ctx->renderer, ctx->force_stroke_text);
1105 aribcc_renderer_set_force_no_background(ctx->renderer, ctx->ignore_background);
1106 aribcc_renderer_set_force_no_ruby(ctx->renderer, ctx->ignore_ruby);
1107 aribcc_renderer_set_stroke_width(ctx->renderer, ctx->stroke_width);
1108 aribcc_renderer_set_replace_msz_halfwidth_glyph(ctx->renderer,
1109 ctx->replace_msz_glyph);
1110 if (ctx->font) {
1111 int is_nomem = 0;
1112 size_t count = 0;
1113 const char **font_families = NULL;
1114 const char *fonts = ctx->font;
1115
1116 while (*fonts) {
1117 const char **ff = av_realloc_array(font_families, count + 1, sizeof(*font_families));
1118 if (!ff) {
1119 is_nomem = 1;
1120 break;
1121 } else {
1122 font_families = ff;
1123 ff[count++] = av_get_token(&fonts, ",");
1124 if (!ff[count - 1]) {
1125 is_nomem = 1;
1126 break;
1127 } else if (*fonts)
1128 fonts++;
1129 }
1130 }
1131 if (!is_nomem && count)
1132 aribcc_renderer_set_default_font_family(ctx->renderer, font_families, count, true);
1133 while (count)
1134 av_freep(&font_families[--count]);
1135 av_freep(&font_families);
1136 if (is_nomem)
1137 return AVERROR(ENOMEM);
1138 }
1139 break;
1140
1141 case SUBTITLE_TEXT:
1142 case SUBTITLE_NONE:
1143 default:
1144 /* do nothing */ ;
1145 }
1146
1147 ctx->readorder = 0;
1148
1149 return 0;
1150}
1151
1152#if !defined(ASS_SINGLE_RECT)
1153# define ASS_SINGLE_RECT 0
1154#endif
1155
1156#define OFFSET(x) offsetof(ARIBCaptionContext, x)
1157#define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
1158static const AVOption options[] = {
1159 { "sub_type", "subtitle rendering type",
1160 OFFSET(subtitle_type), AV_OPT_TYPE_INT,
1161 { .i64 = SUBTITLE_ASS }, SUBTITLE_NONE, SUBTITLE_ASS, SD, .unit = "type" },
1162 { "none", "do nothing", 0, AV_OPT_TYPE_CONST,
1163 { .i64 = SUBTITLE_NONE }, .flags = SD, .unit = "type" },
1164 { "bitmap", "bitmap rendering", 0, AV_OPT_TYPE_CONST,
1165 { .i64 = SUBTITLE_BITMAP }, .flags = SD, .unit = "type" },
1166 { "text", "plain text", 0, AV_OPT_TYPE_CONST,
1167 { .i64 = SUBTITLE_TEXT }, .flags = SD, .unit = "type" },
1168 { "ass", "formatted text", 0, AV_OPT_TYPE_CONST,
1169 { .i64 = SUBTITLE_ASS }, .flags = SD, .unit = "type" },
1170 { "caption_encoding", "encoding scheme of subtitle text",
1171 OFFSET(encoding_scheme), AV_OPT_TYPE_INT, { .i64 = ARIBCC_ENCODING_SCHEME_AUTO },
1172 ARIBCC_ENCODING_SCHEME_AUTO, ARIBCC_ENCODING_SCHEME_ABNT_NBR_15606_1_LATIN, SD, .unit = "encoding" },
1173 { "auto", "automatically detect encoding scheme", 0, AV_OPT_TYPE_CONST,
1174 { .i64 = ARIBCC_ENCODING_SCHEME_AUTO }, .flags = SD, .unit = "encoding" },
1175 { "jis", "8bit-char JIS encoding (Japanese ISDB captions)", 0, AV_OPT_TYPE_CONST,
1176 { .i64 = ARIBCC_ENCODING_SCHEME_ARIB_STD_B24_JIS }, .flags = SD, .unit = "encoding" },
1177 { "utf8", "UTF-8 encoding (Philippines ISDB-T captions)", 0, AV_OPT_TYPE_CONST,
1178 { .i64 = ARIBCC_ENCODING_SCHEME_ARIB_STD_B24_UTF8 }, .flags = SD, .unit = "encoding" },
1179 { "latin", "latin characters (SBTVD / ISDB-Tb captions used in South America)", 0, AV_OPT_TYPE_CONST,
1180 { .i64 = ARIBCC_ENCODING_SCHEME_ABNT_NBR_15606_1_LATIN }, .flags = SD, .unit = "encoding" },
1181 { "ass_single_rect", "workaround of ASS subtitle for players which can't handle multi-rectangle [ass]",
1182 OFFSET(ass_single_rect), AV_OPT_TYPE_BOOL, { .i64 = ASS_SINGLE_RECT }, 0, 1, SD },
1183 { "font", "comma-separated font family [ass, bitmap]",
1184 OFFSET(font), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
1185 { "force_outline_text", "always render characters with outline [(ass), bitmap]",
1186 OFFSET(force_stroke_text), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, SD },
1187 { "ignore_background", "ignore rendering caption background [(ass), bitmap]",
1188 OFFSET(ignore_background), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, SD },
1189 { "ignore_ruby", "ignore ruby-like characters [ass, bitmap]",
1190 OFFSET(ignore_ruby), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, SD },
1191 { "outline_width", "outline width of text [(ass), bitmap]",
1192 OFFSET(stroke_width), AV_OPT_TYPE_FLOAT, { .dbl = 1.5 }, 0.0, 3.0, SD },
1193 { "replace_drcs", "replace known DRCS [bitmap]",
1194 OFFSET(replace_drcs), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, SD },
1195 { "replace_msz_ascii", "replace MSZ fullwidth alphanumerics with halfwidth alphanumerics [ass, bitmap]",
1196 OFFSET(replace_msz_ascii), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, SD },
1197 { "replace_msz_japanese", "replace MSZ fullwidth Japanese with halfwidth [ass, bitmap]",
1198 OFFSET(replace_msz_japanese), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, SD },
1199 { "replace_msz_glyph", "replace MSZ characters with halfwidth glyphs [bitmap]",
1200 OFFSET(replace_msz_glyph), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, SD },
1201 {"canvas_size", "set input video size (WxH or abbreviation) [bitmap]",
1202 OFFSET(canvas_width), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, INT_MAX, SD },
1203 { NULL }
1204};
1205
1207 .class_name = "aribcaption decoder",
1208 .item_name = av_default_item_name,
1209 .option = options,
1210 .version = LIBAVUTIL_VERSION_INT,
1211};
1212
1214 .p.name = "libaribcaption",
1215 .p.long_name = NULL_IF_CONFIG_SMALL("ARIB STD-B24 caption decoder"),
1216 .p.type = AVMEDIA_TYPE_SUBTITLE,
1218 .priv_data_size = sizeof(ARIBCaptionContext),
1222 .flush = aribcaption_flush,
1223 .p.priv_class = &aribcaption_class,
1224 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
1225};
const FFCodec ff_libaribcaption_decoder
static AVFormatContext * ctx
void ff_ass_bprint_text_event(AVBPrint *buf, const char *p, int size, const char *linebreaks, int keep_ass_markup)
Escape a text subtitle using ASS syntax into an AVBPrint buffer.
Definition ass.c:173
int ff_ass_add_rect(AVSubtitle *sub, const char *dialog, int readorder, int layer, const char *style, const char *speaker)
Add an ASS dialog to a subtitle.
Definition ass.c:159
#define ASS_DEFAULT_ALIGNMENT
Definition ass.h:42
#define ASS_DEFAULT_COLOR
Definition ass.h:37
#define ASS_DEFAULT_BACK_COLOR
Definition ass.h:38
#define ASS_DEFAULT_BOLD
Definition ass.h:39
#define ASS_DEFAULT_UNDERLINE
Definition ass.h:41
#define ASS_DEFAULT_ITALIC
Definition ass.h:40
Libavcodec external API header.
char * av_asprintf(const char *fmt,...)
Definition avstring.c:115
Convenience header that includes libavutil's core.
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition bprint.c:121
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition bprint.c:68
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define SD
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define FF_CODEC_DECODE_SUB_CB(func)
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define abs(x)
#define AV_PROFILE_ARIB_PROFILE_A
Definition defs.h:205
#define AV_PROFILE_ARIB_PROFILE_C
Definition defs.h:206
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:613
int ret
Definition ffplay.c:391
#define fail
Definition test.h:479
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
Definition opt.h:270
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
#define AV_CODEC_FLAG2_RO_FLUSH_NOOP
Do not reset ASS ReadOrder field on flush (subtitles decoding)
Definition avcodec.h:376
AVSubtitleType
Definition avcodec.h:2060
@ SUBTITLE_BITMAP
A bitmap, pict will be set.
Definition avcodec.h:2063
@ SUBTITLE_ASS
Formatted text, the ass field must be set by the decoder and is authoritative.
Definition avcodec.h:2075
@ SUBTITLE_TEXT
Plain text, the text field must be set by the decoder and is authoritative.
Definition avcodec.h:2069
@ SUBTITLE_NONE
Definition avcodec.h:2061
@ AV_CODEC_ID_ARIB_CAPTION
Definition codec_id.h:597
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition bprint.h:218
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition bprint.c:234
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition bprint.c:226
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:247
static AVRational av_make_q(int num, int den)
Create an AVRational.
Definition rational.h:71
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:318
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:408
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
char * av_get_token(const char **buf, const char *term)
Unescape the given string until a non escaped terminating char, and return the token corresponding to...
Definition avstring.c:143
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int a
#define r
Definition input.c:42
#define b
Definition input.c:43
static int aribcaption_trans_bitmap_subtitle(ARIBCaptionContext *ctx)
aribcaption_trans_{bitmap|ass|text}_subtitle()
static int aribcaption_trans_ass_subtitle(ARIBCaptionContext *ctx)
#define ARIBC_ALPHA_DEFAULT_FRONT
#define ARIBC_BPRINT_SIZE_INIT
#define DEFAULT_FONT_ASS
static void estimate_video_frame_size(ARIBCaptionContext *ctx)
static int clut_find(ARIBCaptionContext *ctx, uint32_t rgba)
static void logcat_callback(aribcc_loglevel_t level, const char *message, void *userdata)
#define ARIBC_ALPHA_DEFAULT_BACK
static int aribcaption_trans_text_subtitle(ARIBCaptionContext *ctx)
#define ARIBCC_COLOR_DIFF_RGB(c1, c2)
static void set_ass_color(AVBPrint *buf, int color_num, aribcc_color_t new_color, aribcc_color_t old_color)
static av_cold int aribcaption_close(AVCodecContext *avctx)
static int region_skipped(const ARIBCaptionContext *ctx, const aribcc_caption_region_t *region)
static void region_position(const ARIBCaptionContext *ctx, const aribcc_caption_region_t *region, int *x, int *y)
static int set_ass_header(ARIBCaptionContext *ctx)
#define ARIBC_ALPHA_MAX_NUM
#define CLUT_A(c)
static void clut_add_region(ARIBCaptionContext *ctx, aribcc_caption_region_t *region)
#define CLUT_RGBA(r, g, b, a)
#define CLUT_R(c)
static void aribcaption_flush(AVCodecContext *avctx)
#define ASS_SINGLE_RECT
static void clut_reset(ARIBCaptionContext *ctx)
static uint8_t clut_find_nearlest_alpha(ARIBCaptionContext *ctx, uint8_t a)
static void clut_set_alpha(ARIBCaptionContext *ctx, uint8_t a)
static void clut_add(ARIBCaptionContext *ctx, uint32_t rgba)
#define ARIBCC_COLOR_DIFF_A(c1, c2)
static const AVClass aribcaption_class
#define CLUT_B(c)
#define ARIBCC_COLOR_TO_CLUT_RGBA(c, a)
static int clut_color_distance(uint32_t rgba1, uint32_t rgba2)
static int region_left_out(const aribcc_caption_region_t *region, int ignore_ruby)
#define OFFSET(x)
#define ARIBCC_COLOR_RGB(c)
#define ARIBC_BPRINT_SIZE_MAX
static uint8_t clut_pick_or_set(ARIBCaptionContext *ctx, int r, int g, int b, int a)
static int aribcaption_decode(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, const AVPacket *avpkt)
static av_cold int aribcaption_init(AVCodecContext *avctx)
#define CLUT_G(c)
av_cold void CBS_FUNC close(CodedBitstreamContext **ctx_ptr)
Close a context and free all internal state.
Definition cbs.c:118
common internal api header.
#define hex_dump_debug(class, buf, size)
Definition internal.h:39
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
int profile
Definition mxfenc.c:2393
#define av_strdup(s)
Definition ops_static.c:55
AVOptions.
#define AVPALETTE_COUNT
Definition pixfmt.h:33
#define AVPALETTE_SIZE
Definition pixfmt.h:32
uint8_t clut_alpha[ARIBC_ALPHA_MAX_NUM]
aribcc_decoder_t * decoder
aribcc_context_t * context
AVCodecContext * avctx
aribcc_renderer_t * renderer
aribcc_render_result_t render_result
aribcc_caption_t caption
const AVPacket * avpkt
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
int subtitle_header_size
Header containing style information for text subtitles.
Definition avcodec.h:1753
int flags2
AV_CODEC_FLAG2_*.
Definition avcodec.h:507
AVRational pkt_timebase
Timebase in which pkt_dts/pts and AVPacket.dts/pts are expressed.
Definition avcodec.h:554
int profile
profile
Definition avcodec.h:1646
uint8_t * subtitle_header
Definition avcodec.h:1754
void * priv_data
Definition avcodec.h:470
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:587
int size
Definition packet.h:611
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:603
uint8_t * data
Definition packet.h:610
Rational number (pair of numerator and denominator).
Definition rational.h:58
char * ass
0 terminated ASS/SSA compatible event line.
Definition avcodec.h:2104
uint8_t * data[4]
data+linesize for the bitmap of this subtitle.
Definition avcodec.h:2091
uint16_t format
Definition avcodec.h:2108
uint32_t end_display_time
Definition avcodec.h:2110
unsigned num_rects
Definition avcodec.h:2111
AVSubtitleRect ** rects
Definition avcodec.h:2112
int64_t pts
Same as packet pts, in AV_TIME_BASE.
Definition avcodec.h:2113
int w
Definition f_ebur128.c:79
int y
Definition f_ebur128.c:79
int h
Definition f_ebur128.c:79
int x
Definition f_ebur128.c:79
uint8_t level
Definition svq3.c:208
#define av_mallocz(s)
#define ff_dlog(a,...)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
const char * g
Definition vf_curves.c:128
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static double c[64]