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.

180 lines
4.8KB

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