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.

153 lines
4.6KB

  1. /*
  2. * LVF demuxer
  3. * Copyright (c) 2012 Paul B Mahol
  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/intreadwrite.h"
  22. #include "avformat.h"
  23. #include "riff.h"
  24. static int lvf_probe(AVProbeData *p)
  25. {
  26. if (AV_RL32(p->buf) != MKTAG('L', 'V', 'F', 'F'))
  27. return 0;
  28. if (!AV_RL32(p->buf + 16) || AV_RL32(p->buf + 16) > 256)
  29. return AVPROBE_SCORE_MAX / 8;
  30. return AVPROBE_SCORE_EXTENSION;
  31. }
  32. static int lvf_read_header(AVFormatContext *s)
  33. {
  34. AVStream *st;
  35. int64_t next_offset;
  36. unsigned size, nb_streams, id;
  37. avio_skip(s->pb, 16);
  38. nb_streams = avio_rl32(s->pb);
  39. if (!nb_streams)
  40. return AVERROR_INVALIDDATA;
  41. if (nb_streams > 2) {
  42. avpriv_request_sample(s, "%d streams", nb_streams);
  43. return AVERROR_PATCHWELCOME;
  44. }
  45. avio_skip(s->pb, 1012);
  46. while (!url_feof(s->pb)) {
  47. id = avio_rl32(s->pb);
  48. size = avio_rl32(s->pb);
  49. next_offset = avio_tell(s->pb) + size;
  50. switch (id) {
  51. case MKTAG('0', '0', 'f', 'm'):
  52. st = avformat_new_stream(s, 0);
  53. if (!st)
  54. return AVERROR(ENOMEM);
  55. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  56. avio_skip(s->pb, 4);
  57. st->codec->width = avio_rl32(s->pb);
  58. st->codec->height = avio_rl32(s->pb);
  59. avio_skip(s->pb, 4);
  60. st->codec->codec_tag = avio_rl32(s->pb);
  61. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags,
  62. st->codec->codec_tag);
  63. avpriv_set_pts_info(st, 32, 1, 1000);
  64. break;
  65. case MKTAG('0', '1', 'f', 'm'):
  66. st = avformat_new_stream(s, 0);
  67. if (!st)
  68. return AVERROR(ENOMEM);
  69. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  70. st->codec->codec_tag = avio_rl16(s->pb);
  71. st->codec->channels = avio_rl16(s->pb);
  72. st->codec->sample_rate = avio_rl16(s->pb);
  73. avio_skip(s->pb, 8);
  74. st->codec->bits_per_coded_sample = avio_r8(s->pb);
  75. st->codec->codec_id = ff_codec_get_id(ff_codec_wav_tags,
  76. st->codec->codec_tag);
  77. avpriv_set_pts_info(st, 32, 1, 1000);
  78. break;
  79. case 0:
  80. avio_seek(s->pb, 2048 + 8, SEEK_SET);
  81. return 0;
  82. default:
  83. avpriv_request_sample(s, "id %d", id);
  84. return AVERROR_PATCHWELCOME;
  85. }
  86. avio_seek(s->pb, next_offset, SEEK_SET);
  87. }
  88. return AVERROR_EOF;
  89. }
  90. static int lvf_read_packet(AVFormatContext *s, AVPacket *pkt)
  91. {
  92. unsigned size, flags, timestamp, id;
  93. int64_t pos;
  94. int ret, is_video = 0;
  95. pos = avio_tell(s->pb);
  96. while (!url_feof(s->pb)) {
  97. id = avio_rl32(s->pb);
  98. size = avio_rl32(s->pb);
  99. if (size == 0xFFFFFFFFu)
  100. return AVERROR_EOF;
  101. switch (id) {
  102. case MKTAG('0', '0', 'd', 'c'):
  103. is_video = 1;
  104. case MKTAG('0', '1', 'w', 'b'):
  105. if (size < 8)
  106. return AVERROR_INVALIDDATA;
  107. timestamp = avio_rl32(s->pb);
  108. flags = avio_rl32(s->pb);
  109. ret = av_get_packet(s->pb, pkt, size - 8);
  110. if (flags & (1 << 12))
  111. pkt->flags |= AV_PKT_FLAG_KEY;
  112. pkt->stream_index = is_video ? 0 : 1;
  113. pkt->pts = timestamp;
  114. pkt->pos = pos;
  115. return ret;
  116. default:
  117. ret = avio_skip(s->pb, size);
  118. }
  119. if (ret < 0)
  120. return ret;
  121. }
  122. return AVERROR_EOF;
  123. }
  124. AVInputFormat ff_lvf_demuxer = {
  125. .name = "lvf",
  126. .long_name = NULL_IF_CONFIG_SMALL("LVF"),
  127. .read_probe = lvf_probe,
  128. .read_header = lvf_read_header,
  129. .read_packet = lvf_read_packet,
  130. .extensions = "lvf",
  131. .flags = AVFMT_GENERIC_INDEX,
  132. };