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
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, err;
  67. while (c->get_next) {
  68. if (s->pb->eof_reached)
  69. return AVERROR_EOF;
  70. type = avio_r8(s->pb);
  71. if (type == BMV_NOP)
  72. continue;
  73. if (type == BMV_END)
  74. return AVERROR_EOF;
  75. c->size = avio_rl24(s->pb);
  76. if (!c->size)
  77. return AVERROR_INVALIDDATA;
  78. if ((err = av_reallocp(&c->packet, c->size + 1)) < 0)
  79. return err;
  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. };