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.

122 lines
3.2KB

  1. /*
  2. * SDR2 demuxer
  3. * Copyright (c) 2014 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 "internal.h"
  24. static int sdr2_probe(AVProbeData *p)
  25. {
  26. if (AV_RL32(p->buf) != MKTAG('S', 'R', 'A', 1))
  27. return 0;
  28. return AVPROBE_SCORE_EXTENSION;
  29. }
  30. #define FIRST 0xA8
  31. static int sdr2_read_header(AVFormatContext *s)
  32. {
  33. AVStream *st, *ast;
  34. ast = avformat_new_stream(s, 0);
  35. if (!ast)
  36. return AVERROR(ENOMEM);
  37. st = avformat_new_stream(s, 0);
  38. if (!st)
  39. return AVERROR(ENOMEM);
  40. avio_skip(s->pb, 20);
  41. avpriv_set_pts_info(st, 64, 1, avio_rl32(s->pb));
  42. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  43. st->codec->width = avio_rl32(s->pb);
  44. st->codec->height = avio_rl32(s->pb);
  45. st->codec->codec_id = AV_CODEC_ID_H264;
  46. st->need_parsing = AVSTREAM_PARSE_FULL;
  47. ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  48. ast->codec->channels = 1;
  49. ast->codec->sample_rate = 8000;
  50. ast->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  51. avpriv_set_pts_info(ast, 64, 1, 8000);
  52. avio_seek(s->pb, FIRST, SEEK_SET);
  53. return 0;
  54. }
  55. static const uint8_t header[24] = {
  56. 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
  57. 0xa6, 0x80, 0xb0, 0x7e, 0x40, 0x00, 0x00, 0x00,
  58. 0x01, 0x68, 0xce, 0x38, 0x80, 0x00, 0x00, 0x00
  59. };
  60. static int sdr2_read_packet(AVFormatContext *s, AVPacket *pkt)
  61. {
  62. int64_t pos;
  63. unsigned next;
  64. int flags, ret = 0, is_video;
  65. pos = avio_tell(s->pb);
  66. flags = avio_rl32(s->pb);
  67. avio_skip(s->pb, 4);
  68. next = avio_rl32(s->pb);
  69. if (next <= 52)
  70. return AVERROR_INVALIDDATA;
  71. avio_skip(s->pb, 6);
  72. is_video = avio_rl32(s->pb);
  73. avio_skip(s->pb, 30);
  74. if (pos == FIRST) {
  75. if (av_new_packet(pkt, next - 52 + 24) < 0)
  76. return AVERROR(ENOMEM);
  77. memcpy(pkt->data, header, 24);
  78. ret = avio_read(s->pb, pkt->data + 24, next - 52);
  79. if (ret < 0) {
  80. av_free_packet(pkt);
  81. return ret;
  82. }
  83. av_shrink_packet(pkt, ret + 24);
  84. } else {
  85. ret = av_get_packet(s->pb, pkt, next - 52);
  86. }
  87. pkt->stream_index = !!is_video;
  88. pkt->pos = pos;
  89. if (flags & (1 << 12))
  90. pkt->flags |= AV_PKT_FLAG_KEY;
  91. return ret;
  92. }
  93. AVInputFormat ff_sdr2_demuxer = {
  94. .name = "sdr2",
  95. .long_name = NULL_IF_CONFIG_SMALL("SDR2"),
  96. .read_probe = sdr2_probe,
  97. .read_header = sdr2_read_header,
  98. .read_packet = sdr2_read_packet,
  99. .extensions = "sdr2",
  100. .flags = AVFMT_GENERIC_INDEX,
  101. };