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.

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