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.

102 lines
2.9KB

  1. /*
  2. * Copyright (c) 2012 Clément Bœsch
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avformat.h"
  21. #include "subtitles.h"
  22. AVPacket *ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q,
  23. const uint8_t *event, int len, int merge)
  24. {
  25. AVPacket *subs, *sub;
  26. if (merge && q->nb_subs > 0) {
  27. /* merge with previous event */
  28. int old_len;
  29. sub = &q->subs[q->nb_subs - 1];
  30. old_len = sub->size;
  31. if (av_grow_packet(sub, len) < 0)
  32. return NULL;
  33. memcpy(sub->data + old_len, event, len);
  34. } else {
  35. /* new event */
  36. if (q->nb_subs >= INT_MAX/sizeof(*q->subs) - 1)
  37. return NULL;
  38. subs = av_fast_realloc(q->subs, &q->allocated_size,
  39. (q->nb_subs + 1) * sizeof(*q->subs));
  40. if (!subs)
  41. return NULL;
  42. q->subs = subs;
  43. sub = &subs[q->nb_subs++];
  44. if (av_new_packet(sub, len) < 0)
  45. return NULL;
  46. sub->destruct = NULL;
  47. sub->flags |= AV_PKT_FLAG_KEY;
  48. sub->pts = sub->dts = 0;
  49. memcpy(sub->data, event, len);
  50. }
  51. return sub;
  52. }
  53. static int cmp_pkt_sub(const void *a, const void *b)
  54. {
  55. const AVPacket *s1 = a;
  56. const AVPacket *s2 = b;
  57. if (s1->pts == s2->pts) {
  58. if (s1->pos == s2->pos)
  59. return 0;
  60. return s1->pos > s2->pos ? 1 : -1;
  61. }
  62. return s1->pts > s2->pts ? 1 : -1;
  63. }
  64. void ff_subtitles_queue_finalize(FFDemuxSubtitlesQueue *q)
  65. {
  66. int i;
  67. qsort(q->subs, q->nb_subs, sizeof(*q->subs), cmp_pkt_sub);
  68. for (i = 0; i < q->nb_subs; i++)
  69. if (q->subs[i].duration == -1 && i < q->nb_subs - 1)
  70. q->subs[i].duration = q->subs[i + 1].pts - q->subs[i].pts;
  71. }
  72. int ff_subtitles_queue_read_packet(FFDemuxSubtitlesQueue *q, AVPacket *pkt)
  73. {
  74. AVPacket *sub = q->subs + q->current_sub_idx;
  75. if (q->current_sub_idx == q->nb_subs)
  76. return AVERROR_EOF;
  77. *pkt = *sub;
  78. pkt->dts = pkt->pts;
  79. q->current_sub_idx++;
  80. return 0;
  81. }
  82. void ff_subtitles_queue_clean(FFDemuxSubtitlesQueue *q)
  83. {
  84. int i;
  85. for (i = 0; i < q->nb_subs; i++)
  86. av_destruct_packet(&q->subs[i]);
  87. av_freep(&q->subs);
  88. q->nb_subs = q->allocated_size = q->current_sub_idx = 0;
  89. }