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.

140 lines
4.0KB

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