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.

140 lines
3.9KB

  1. /*
  2. * Raw DTS-HD 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 "libavutil/dict.h"
  23. #include "avformat.h"
  24. #define AUPR_HDR 0x415550522D484452
  25. #define AUPRINFO 0x41555052494E464F
  26. #define BITSHVTB 0x4249545348565442
  27. #define BLACKOUT 0x424C41434B4F5554
  28. #define BRANCHPT 0x4252414E43485054
  29. #define BUILDVER 0x4255494C44564552
  30. #define CORESSMD 0x434F524553534D44
  31. #define DTSHDHDR 0x4454534844484452
  32. #define EXTSS_MD 0x45585453535f4d44
  33. #define FILEINFO 0x46494C45494E464F
  34. #define NAVI_TBL 0x4E4156492D54424C
  35. #define STRMDATA 0x5354524D44415441
  36. #define TIMECODE 0x54494D45434F4445
  37. typedef struct DTSHDDemuxContext {
  38. uint64_t data_end;
  39. } DTSHDDemuxContext;
  40. static int dtshd_probe(AVProbeData *p)
  41. {
  42. if (AV_RB64(p->buf) == DTSHDHDR)
  43. return AVPROBE_SCORE_MAX;
  44. return 0;
  45. }
  46. static int dtshd_read_header(AVFormatContext *s)
  47. {
  48. DTSHDDemuxContext *dtshd = s->priv_data;
  49. AVIOContext *pb = s->pb;
  50. uint64_t chunk_type, chunk_size;
  51. AVStream *st;
  52. int ret;
  53. char *value;
  54. st = avformat_new_stream(s, NULL);
  55. if (!st)
  56. return AVERROR(ENOMEM);
  57. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  58. st->codec->codec_id = AV_CODEC_ID_DTS;
  59. st->need_parsing = AVSTREAM_PARSE_FULL_RAW;
  60. while (!url_feof(pb)) {
  61. chunk_type = avio_rb64(pb);
  62. chunk_size = avio_rb64(pb);
  63. if (chunk_size < 4) {
  64. av_log(s, AV_LOG_ERROR, "chunk size too small\n");
  65. return AVERROR_INVALIDDATA;
  66. }
  67. if (chunk_size > ((uint64_t)1 << 61)) {
  68. av_log(s, AV_LOG_ERROR, "chunk size too big\n");
  69. return AVERROR_INVALIDDATA;
  70. }
  71. switch (chunk_type) {
  72. case STRMDATA:
  73. dtshd->data_end = chunk_size + avio_tell(pb);
  74. if (dtshd->data_end <= chunk_size)
  75. return AVERROR_INVALIDDATA;
  76. return 0;
  77. break;
  78. case FILEINFO:
  79. if (chunk_size > INT_MAX)
  80. goto skip;
  81. value = av_malloc(chunk_size);
  82. if (!value)
  83. goto skip;
  84. avio_read(pb, value, chunk_size);
  85. value[chunk_size - 1] = 0;
  86. av_dict_set(&s->metadata, "fileinfo", value,
  87. AV_DICT_DONT_STRDUP_VAL);
  88. break;
  89. default:
  90. skip:
  91. ret = avio_skip(pb, chunk_size);
  92. if (ret < 0)
  93. return ret;
  94. };
  95. }
  96. return AVERROR_EOF;
  97. }
  98. static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
  99. {
  100. DTSHDDemuxContext *dtshd = s->priv_data;
  101. int64_t size, left;
  102. int ret;
  103. left = dtshd->data_end - avio_tell(s->pb);
  104. size = FFMIN(left, 1024);
  105. if (size <= 0)
  106. return AVERROR_EOF;
  107. ret = av_get_packet(s->pb, pkt, size);
  108. if (ret < 0)
  109. return ret;
  110. pkt->stream_index = 0;
  111. return ret;
  112. }
  113. AVInputFormat ff_dtshd_demuxer = {
  114. .name = "dtshd",
  115. .long_name = NULL_IF_CONFIG_SMALL("raw DTS-HD"),
  116. .priv_data_size = sizeof(DTSHDDemuxContext),
  117. .read_probe = dtshd_probe,
  118. .read_header = dtshd_read_header,
  119. .read_packet = raw_read_packet,
  120. .flags = AVFMT_GENERIC_INDEX,
  121. .extensions = "dtshd",
  122. .raw_codec_id = AV_CODEC_ID_DTS,
  123. };