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.

70 lines
2.3KB

  1. /*
  2. * SSA/ASS common funtions
  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. #include "avcodec.h"
  22. #include "ass.h"
  23. void ff_ass_init(AVSubtitle *sub)
  24. {
  25. memset(sub, 0, sizeof(*sub));
  26. }
  27. static int ts_to_string(char *str, int strlen, int ts)
  28. {
  29. int h, m, s;
  30. h = ts/360000; ts -= 360000*h;
  31. m = ts/ 6000; ts -= 6000*m;
  32. s = ts/ 100; ts -= 100*s;
  33. return snprintf(str, strlen, "%d:%02d:%02d.%02d", h, m, s, ts);
  34. }
  35. int ff_ass_add_rect(AVSubtitle *sub, const char *dialog,
  36. int ts_start, int ts_end, int raw)
  37. {
  38. int len = 0, dlen, duration = ts_end - ts_start;
  39. char s_start[16], s_end[16], header[48] = {0};
  40. AVSubtitleRect **rects;
  41. if (!raw) {
  42. ts_to_string(s_start, sizeof(s_start), ts_start);
  43. ts_to_string(s_end, sizeof(s_end), ts_end );
  44. len = snprintf(header, sizeof(header), "Dialogue: 0,%s,%s,",
  45. s_start, s_end);
  46. }
  47. dlen = strcspn(dialog, "\n");
  48. dlen += dialog[dlen] == '\n';
  49. rects = av_realloc(sub->rects, (sub->num_rects+1) * sizeof(*sub->rects));
  50. if (!rects)
  51. return AVERROR(ENOMEM);
  52. sub->rects = rects;
  53. sub->end_display_time = FFMAX(sub->end_display_time, 10 * duration);
  54. rects[sub->num_rects] = av_mallocz(sizeof(*rects[0]));
  55. rects[sub->num_rects]->type = SUBTITLE_ASS;
  56. rects[sub->num_rects]->ass = av_malloc(len + dlen + 1);
  57. strcpy (rects[sub->num_rects]->ass , header);
  58. strncpy(rects[sub->num_rects]->ass + len, dialog, dlen);
  59. rects[sub->num_rects]->ass[len+dlen] = 0;
  60. sub->num_rects++;
  61. return dlen;
  62. }