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.

89 lines
2.7KB

  1. /*
  2. * DVD subtitle decoding
  3. * Copyright (c) 2005 Fabrice Bellard
  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 <string.h>
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/mem.h"
  24. #include "avcodec.h"
  25. /* parser definition */
  26. typedef struct DVDSubParseContext {
  27. uint8_t *packet;
  28. int packet_len;
  29. int packet_index;
  30. } DVDSubParseContext;
  31. static av_cold int dvdsub_parse_init(AVCodecParserContext *s)
  32. {
  33. return 0;
  34. }
  35. static int dvdsub_parse(AVCodecParserContext *s,
  36. AVCodecContext *avctx,
  37. const uint8_t **poutbuf, int *poutbuf_size,
  38. const uint8_t *buf, int buf_size)
  39. {
  40. DVDSubParseContext *pc = s->priv_data;
  41. if (pc->packet_index == 0) {
  42. if (buf_size < 2)
  43. return 0;
  44. pc->packet_len = AV_RB16(buf);
  45. if (pc->packet_len == 0) /* HD-DVD subpicture packet */
  46. pc->packet_len = AV_RB32(buf+2);
  47. av_freep(&pc->packet);
  48. pc->packet = av_malloc(pc->packet_len);
  49. }
  50. if (pc->packet) {
  51. if (pc->packet_index + buf_size <= pc->packet_len) {
  52. memcpy(pc->packet + pc->packet_index, buf, buf_size);
  53. pc->packet_index += buf_size;
  54. if (pc->packet_index >= pc->packet_len) {
  55. *poutbuf = pc->packet;
  56. *poutbuf_size = pc->packet_len;
  57. pc->packet_index = 0;
  58. return buf_size;
  59. }
  60. } else {
  61. /* erroneous size */
  62. pc->packet_index = 0;
  63. }
  64. }
  65. *poutbuf = NULL;
  66. *poutbuf_size = 0;
  67. return buf_size;
  68. }
  69. static av_cold void dvdsub_parse_close(AVCodecParserContext *s)
  70. {
  71. DVDSubParseContext *pc = s->priv_data;
  72. av_freep(&pc->packet);
  73. }
  74. AVCodecParser ff_dvdsub_parser = {
  75. .codec_ids = { AV_CODEC_ID_DVD_SUBTITLE },
  76. .priv_data_size = sizeof(DVDSubParseContext),
  77. .parser_init = dvdsub_parse_init,
  78. .parser_parse = dvdsub_parse,
  79. .parser_close = dvdsub_parse_close,
  80. };