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.

187 lines
4.9KB

  1. /*
  2. * DVB subtitle parser for Libav
  3. * Copyright (c) 2005 Ian Caulfield
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <inttypes.h>
  22. #include <string.h>
  23. #include "libavutil/intreadwrite.h"
  24. #include "libavutil/mem.h"
  25. #include "avcodec.h"
  26. #include "internal.h"
  27. /* Parser (mostly) copied from dvdsub.c */
  28. #define PARSE_BUF_SIZE (65536)
  29. /* parser definition */
  30. typedef struct DVBSubParseContext {
  31. uint8_t *packet_buf;
  32. int packet_start;
  33. int packet_index;
  34. int in_packet;
  35. } DVBSubParseContext;
  36. static av_cold int dvbsub_parse_init(AVCodecParserContext *s)
  37. {
  38. DVBSubParseContext *pc = s->priv_data;
  39. pc->packet_buf = av_malloc(PARSE_BUF_SIZE);
  40. return 0;
  41. }
  42. static int dvbsub_parse(AVCodecParserContext *s,
  43. AVCodecContext *avctx,
  44. const uint8_t **poutbuf, int *poutbuf_size,
  45. const uint8_t *buf, int buf_size)
  46. {
  47. DVBSubParseContext *pc = s->priv_data;
  48. uint8_t *p, *p_end;
  49. int i, len, buf_pos = 0;
  50. ff_dlog(avctx, "DVB parse packet pts=%"PRIx64", lpts=%"PRIx64", cpts=%"PRIx64":\n",
  51. s->pts, s->last_pts, s->cur_frame_pts[s->cur_frame_start_index]);
  52. for (i=0; i < buf_size; i++)
  53. {
  54. ff_dlog(avctx, "%02x ", buf[i]);
  55. if (i % 16 == 15)
  56. ff_dlog(avctx, "\n");
  57. }
  58. if (i % 16 != 0)
  59. ff_dlog(avctx, "\n");
  60. *poutbuf = NULL;
  61. *poutbuf_size = 0;
  62. s->fetch_timestamp = 1;
  63. if (s->last_pts != s->pts && s->pts != AV_NOPTS_VALUE) /* Start of a new packet */
  64. {
  65. if (pc->packet_index != pc->packet_start)
  66. {
  67. ff_dlog(avctx, "Discarding %d bytes\n",
  68. pc->packet_index - pc->packet_start);
  69. }
  70. pc->packet_start = 0;
  71. pc->packet_index = 0;
  72. if (buf_size < 2 || buf[0] != 0x20 || buf[1] != 0x00) {
  73. ff_dlog(avctx, "Bad packet header\n");
  74. return -1;
  75. }
  76. buf_pos = 2;
  77. pc->in_packet = 1;
  78. } else {
  79. if (pc->packet_start != 0)
  80. {
  81. if (pc->packet_index != pc->packet_start)
  82. {
  83. memmove(pc->packet_buf, pc->packet_buf + pc->packet_start,
  84. pc->packet_index - pc->packet_start);
  85. pc->packet_index -= pc->packet_start;
  86. pc->packet_start = 0;
  87. } else {
  88. pc->packet_start = 0;
  89. pc->packet_index = 0;
  90. }
  91. }
  92. }
  93. if (buf_size - buf_pos + pc->packet_index > PARSE_BUF_SIZE)
  94. return -1;
  95. /* if not currently in a packet, discard data */
  96. if (pc->in_packet == 0)
  97. return buf_size;
  98. memcpy(pc->packet_buf + pc->packet_index, buf + buf_pos, buf_size - buf_pos);
  99. pc->packet_index += buf_size - buf_pos;
  100. p = pc->packet_buf;
  101. p_end = pc->packet_buf + pc->packet_index;
  102. while (p < p_end)
  103. {
  104. if (*p == 0x0f)
  105. {
  106. if (p + 6 <= p_end)
  107. {
  108. len = AV_RB16(p + 4);
  109. if (p + len + 6 <= p_end)
  110. {
  111. *poutbuf_size += len + 6;
  112. p += len + 6;
  113. } else
  114. break;
  115. } else
  116. break;
  117. } else if (*p == 0xff) {
  118. if (p + 1 < p_end)
  119. {
  120. ff_dlog(avctx, "Junk at end of packet\n");
  121. }
  122. pc->packet_index = p - pc->packet_buf;
  123. pc->in_packet = 0;
  124. break;
  125. } else {
  126. av_log(avctx, AV_LOG_ERROR, "Junk in packet\n");
  127. pc->packet_index = p - pc->packet_buf;
  128. pc->in_packet = 0;
  129. break;
  130. }
  131. }
  132. if (*poutbuf_size > 0)
  133. {
  134. *poutbuf = pc->packet_buf;
  135. pc->packet_start = *poutbuf_size;
  136. }
  137. if (s->pts == AV_NOPTS_VALUE)
  138. s->pts = s->last_pts;
  139. return buf_size;
  140. }
  141. static av_cold void dvbsub_parse_close(AVCodecParserContext *s)
  142. {
  143. DVBSubParseContext *pc = s->priv_data;
  144. av_freep(&pc->packet_buf);
  145. }
  146. AVCodecParser ff_dvbsub_parser = {
  147. .codec_ids = { AV_CODEC_ID_DVB_SUBTITLE },
  148. .priv_data_size = sizeof(DVBSubParseContext),
  149. .parser_init = dvbsub_parse_init,
  150. .parser_parse = dvbsub_parse,
  151. .parser_close = dvbsub_parse_close,
  152. };