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.

149 lines
4.5KB

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