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.

137 lines
3.9KB

  1. /*
  2. * Discworld II BMV demuxer
  3. * Copyright (c) 2011 Konstantin Shishkov.
  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 "avformat.h"
  22. enum BMVFlags {
  23. BMV_NOP = 0,
  24. BMV_END,
  25. BMV_DELTA,
  26. BMV_INTRA,
  27. BMV_AUDIO = 0x20,
  28. };
  29. typedef struct BMVContext {
  30. uint8_t *packet;
  31. int size;
  32. int get_next;
  33. int64_t audio_pos;
  34. } BMVContext;
  35. static int bmv_read_header(AVFormatContext *s, AVFormatParameters *ap)
  36. {
  37. AVStream *st, *ast;
  38. BMVContext *c = s->priv_data;
  39. st = avformat_new_stream(s, 0);
  40. if (!st)
  41. return AVERROR(ENOMEM);
  42. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  43. st->codec->codec_id = CODEC_ID_BMV_VIDEO;
  44. st->codec->width = 640;
  45. st->codec->height = 429;
  46. st->codec->pix_fmt = PIX_FMT_PAL8;
  47. av_set_pts_info(st, 16, 1, 12);
  48. ast = avformat_new_stream(s, 0);
  49. if (!ast)
  50. return AVERROR(ENOMEM);
  51. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  52. ast->codec->codec_id = CODEC_ID_BMV_AUDIO;
  53. ast->codec->channels = 2;
  54. ast->codec->sample_rate = 22050;
  55. av_set_pts_info(ast, 16, 1, 22050);
  56. c->get_next = 1;
  57. c->audio_pos = 0;
  58. return 0;
  59. }
  60. static int bmv_read_packet(AVFormatContext *s, AVPacket *pkt)
  61. {
  62. BMVContext *c = s->priv_data;
  63. int type;
  64. void *tmp;
  65. while (c->get_next) {
  66. if (s->pb->eof_reached)
  67. return AVERROR_EOF;
  68. type = avio_r8(s->pb);
  69. if (type == BMV_NOP)
  70. continue;
  71. if (type == BMV_END)
  72. return AVERROR_EOF;
  73. c->size = avio_rl24(s->pb);
  74. if (!c->size)
  75. return AVERROR_INVALIDDATA;
  76. tmp = av_realloc(c->packet, c->size + 1);
  77. if (!tmp)
  78. return AVERROR(ENOMEM);
  79. c->packet = tmp;
  80. c->packet[0] = type;
  81. if (avio_read(s->pb, c->packet + 1, c->size) != c->size)
  82. return AVERROR(EIO);
  83. if (type & BMV_AUDIO) {
  84. int audio_size = c->packet[1] * 65 + 1;
  85. if (audio_size >= c->size) {
  86. av_log(s, AV_LOG_ERROR, "Reported audio size %d is bigger than packet size (%d)\n",
  87. audio_size, c->size);
  88. return AVERROR_INVALIDDATA;
  89. }
  90. if (av_new_packet(pkt, audio_size) < 0)
  91. return AVERROR(ENOMEM);
  92. memcpy(pkt->data, c->packet + 1, pkt->size);
  93. pkt->stream_index = 1;
  94. pkt->pts = c->audio_pos;
  95. pkt->duration = c->packet[1] * 32;
  96. c->audio_pos += pkt->duration;
  97. c->get_next = 0;
  98. return pkt->size;
  99. } else
  100. break;
  101. }
  102. if (av_new_packet(pkt, c->size + 1) < 0)
  103. return AVERROR(ENOMEM);
  104. pkt->stream_index = 0;
  105. c->get_next = 1;
  106. memcpy(pkt->data, c->packet, pkt->size);
  107. return pkt->size;
  108. }
  109. static int bmv_read_close(AVFormatContext *s)
  110. {
  111. BMVContext *c = s->priv_data;
  112. av_freep(&c->packet);
  113. return 0;
  114. }
  115. AVInputFormat ff_bmv_demuxer = {
  116. .name = "bmv",
  117. .long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV"),
  118. .priv_data_size = sizeof(BMVContext),
  119. .read_header = bmv_read_header,
  120. .read_packet = bmv_read_packet,
  121. .read_close = bmv_read_close,
  122. .extensions = "bmv"
  123. };