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.

181 lines
4.8KB

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