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.

138 lines
3.8KB

  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 left;
  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->left = chunk_size;
  74. return 0;
  75. break;
  76. case FILEINFO:
  77. if (chunk_size > INT_MAX)
  78. goto skip;
  79. value = av_malloc(chunk_size);
  80. if (!value)
  81. goto skip;
  82. avio_read(pb, value, chunk_size);
  83. value[chunk_size - 1] = 0;
  84. av_dict_set(&s->metadata, "fileinfo", value,
  85. AV_DICT_DONT_STRDUP_VAL);
  86. break;
  87. default:
  88. skip:
  89. ret = avio_skip(pb, chunk_size);
  90. if (ret < 0)
  91. return ret;
  92. };
  93. }
  94. return AVERROR_EOF;
  95. }
  96. static int raw_read_packet(AVFormatContext *s, AVPacket *pkt)
  97. {
  98. DTSHDDemuxContext *dtshd = s->priv_data;
  99. uint64_t size;
  100. int ret;
  101. size = FFMIN(dtshd->left, 1024);
  102. if (size == 0)
  103. return AVERROR_EOF;
  104. ret = av_get_packet(s->pb, pkt, size);
  105. if (ret < 0)
  106. return ret;
  107. pkt->stream_index = 0;
  108. dtshd->left -= ret;
  109. return ret;
  110. }
  111. AVInputFormat ff_dtshd_demuxer = {
  112. .name = "dtshd",
  113. .long_name = NULL_IF_CONFIG_SMALL("raw DTS-HD"),
  114. .priv_data_size = sizeof(DTSHDDemuxContext),
  115. .read_probe = dtshd_probe,
  116. .read_header = dtshd_read_header,
  117. .read_packet = raw_read_packet,
  118. .flags = AVFMT_GENERIC_INDEX,
  119. .extensions = "dtshd",
  120. .raw_codec_id = AV_CODEC_ID_DTS,
  121. };