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.

137 lines
3.8KB

  1. /*
  2. * Copyright (c) 2011 Justin Ruggles
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * CRI ADX demuxer
  23. */
  24. #include "libavutil/intreadwrite.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #define BLOCK_SIZE 18
  28. #define BLOCK_SAMPLES 32
  29. typedef struct ADXDemuxerContext {
  30. int header_size;
  31. } ADXDemuxerContext;
  32. static int adx_probe(AVProbeData *p)
  33. {
  34. int offset;
  35. if (AV_RB16(p->buf) != 0x8000)
  36. return 0;
  37. offset = AV_RB16(&p->buf[2]);
  38. if ( offset < 8
  39. || offset > p->buf_size - 4
  40. || memcmp(p->buf + offset - 2, "(c)CRI", 6))
  41. return 0;
  42. return AVPROBE_SCORE_MAX * 3 / 4;
  43. }
  44. static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
  45. {
  46. ADXDemuxerContext *c = s->priv_data;
  47. AVCodecParameters *par = s->streams[0]->codecpar;
  48. int ret, size;
  49. if (par->channels <= 0) {
  50. av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->channels);
  51. return AVERROR_INVALIDDATA;
  52. }
  53. size = BLOCK_SIZE * par->channels;
  54. pkt->pos = avio_tell(s->pb);
  55. pkt->stream_index = 0;
  56. ret = av_get_packet(s->pb, pkt, size);
  57. if (ret != size) {
  58. av_packet_unref(pkt);
  59. return ret < 0 ? ret : AVERROR(EIO);
  60. }
  61. if (AV_RB16(pkt->data) & 0x8000) {
  62. av_packet_unref(pkt);
  63. return AVERROR_EOF;
  64. }
  65. pkt->size = size;
  66. pkt->duration = 1;
  67. pkt->pts = (pkt->pos - c->header_size) / size;
  68. return 0;
  69. }
  70. static int adx_read_header(AVFormatContext *s)
  71. {
  72. ADXDemuxerContext *c = s->priv_data;
  73. AVCodecParameters *par;
  74. AVStream *st = avformat_new_stream(s, NULL);
  75. if (!st)
  76. return AVERROR(ENOMEM);
  77. par = s->streams[0]->codecpar;
  78. if (avio_rb16(s->pb) != 0x8000)
  79. return AVERROR_INVALIDDATA;
  80. c->header_size = avio_rb16(s->pb) + 4;
  81. avio_seek(s->pb, -4, SEEK_CUR);
  82. if (ff_get_extradata(s, par, s->pb, c->header_size) < 0)
  83. return AVERROR(ENOMEM);
  84. if (par->extradata_size < 12) {
  85. av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
  86. return AVERROR_INVALIDDATA;
  87. }
  88. par->channels = AV_RB8 (par->extradata + 7);
  89. par->sample_rate = AV_RB32(par->extradata + 8);
  90. if (par->channels <= 0) {
  91. av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->channels);
  92. return AVERROR_INVALIDDATA;
  93. }
  94. if (par->sample_rate <= 0) {
  95. av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", par->sample_rate);
  96. return AVERROR_INVALIDDATA;
  97. }
  98. par->codec_type = AVMEDIA_TYPE_AUDIO;
  99. par->codec_id = s->iformat->raw_codec_id;
  100. par->bit_rate = par->sample_rate * par->channels * BLOCK_SIZE * 8LL / BLOCK_SAMPLES;
  101. avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, par->sample_rate);
  102. return 0;
  103. }
  104. AVInputFormat ff_adx_demuxer = {
  105. .name = "adx",
  106. .long_name = NULL_IF_CONFIG_SMALL("CRI ADX"),
  107. .read_probe = adx_probe,
  108. .priv_data_size = sizeof(ADXDemuxerContext),
  109. .read_header = adx_read_header,
  110. .read_packet = adx_read_packet,
  111. .extensions = "adx",
  112. .raw_codec_id = AV_CODEC_ID_ADPCM_ADX,
  113. .flags = AVFMT_GENERIC_INDEX,
  114. };