dvbsub_parser.c 5.0 KB

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