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.

189 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. //#define DEBUG
  25. //#define DEBUG_PACKET_CONTENTS
  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 len, buf_pos = 0;
  49. dprintf(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. #ifdef DEBUG_PACKET_CONTENTS
  52. int i;
  53. for (i=0; i < buf_size; i++)
  54. {
  55. av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
  56. if (i % 16 == 15)
  57. av_log(avctx, AV_LOG_INFO, "\n");
  58. }
  59. if (i % 16 != 0)
  60. av_log(avctx, AV_LOG_INFO, "\n");
  61. #endif
  62. *poutbuf = NULL;
  63. *poutbuf_size = 0;
  64. s->fetch_timestamp = 1;
  65. if (s->last_pts != s->pts && s->pts != AV_NOPTS_VALUE) /* Start of a new packet */
  66. {
  67. if (pc->packet_index != pc->packet_start)
  68. {
  69. dprintf(avctx, "Discarding %d bytes\n",
  70. pc->packet_index - pc->packet_start);
  71. }
  72. pc->packet_start = 0;
  73. pc->packet_index = 0;
  74. if (buf_size < 2 || buf[0] != 0x20 || buf[1] != 0x00) {
  75. dprintf(avctx, "Bad packet header\n");
  76. return -1;
  77. }
  78. buf_pos = 2;
  79. pc->in_packet = 1;
  80. } else {
  81. if (pc->packet_start != 0)
  82. {
  83. if (pc->packet_index != pc->packet_start)
  84. {
  85. memmove(pc->packet_buf, pc->packet_buf + pc->packet_start,
  86. pc->packet_index - pc->packet_start);
  87. pc->packet_index -= pc->packet_start;
  88. pc->packet_start = 0;
  89. } else {
  90. pc->packet_start = 0;
  91. pc->packet_index = 0;
  92. }
  93. }
  94. }
  95. if (buf_size - buf_pos + pc->packet_index > PARSE_BUF_SIZE)
  96. return -1;
  97. /* if not currently in a packet, discard data */
  98. if (pc->in_packet == 0)
  99. return buf_size;
  100. memcpy(pc->packet_buf + pc->packet_index, buf + buf_pos, buf_size - buf_pos);
  101. pc->packet_index += buf_size - buf_pos;
  102. p = pc->packet_buf;
  103. p_end = pc->packet_buf + pc->packet_index;
  104. while (p < p_end)
  105. {
  106. if (*p == 0x0f)
  107. {
  108. if (p + 6 <= p_end)
  109. {
  110. len = AV_RB16(p + 4);
  111. if (p + len + 6 <= p_end)
  112. {
  113. *poutbuf_size += len + 6;
  114. p += len + 6;
  115. } else
  116. break;
  117. } else
  118. break;
  119. } else if (*p == 0xff) {
  120. if (p + 1 < p_end)
  121. {
  122. dprintf(avctx, "Junk at end of packet\n");
  123. }
  124. pc->packet_index = p - pc->packet_buf;
  125. pc->in_packet = 0;
  126. break;
  127. } else {
  128. av_log(avctx, AV_LOG_ERROR, "Junk in packet\n");
  129. pc->packet_index = p - pc->packet_buf;
  130. pc->in_packet = 0;
  131. break;
  132. }
  133. }
  134. if (*poutbuf_size > 0)
  135. {
  136. *poutbuf = pc->packet_buf;
  137. pc->packet_start = *poutbuf_size;
  138. }
  139. if (s->pts == AV_NOPTS_VALUE)
  140. s->pts = s->last_pts;
  141. return buf_size;
  142. }
  143. static av_cold void dvbsub_parse_close(AVCodecParserContext *s)
  144. {
  145. DVBSubParseContext *pc = s->priv_data;
  146. av_freep(&pc->packet_buf);
  147. }
  148. AVCodecParser ff_dvbsub_parser = {
  149. { CODEC_ID_DVB_SUBTITLE },
  150. sizeof(DVBSubParseContext),
  151. dvbsub_parse_init,
  152. dvbsub_parse,
  153. dvbsub_parse_close,
  154. };