/libavcodec/dvbsub_parser.c

http://github.com/FFmpeg/FFmpeg · C · 188 lines · 131 code · 34 blank · 23 comment · 38 complexity · 960050003efbe6627dce7ebe64942926 MD5 · raw file

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