bmv.c 3.9 KB

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