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.

118 lines
3.3KB

  1. /*
  2. * Copyright (c) 2011 Justin Ruggles
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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_read_packet(AVFormatContext *s, AVPacket *pkt)
  33. {
  34. ADXDemuxerContext *c = s->priv_data;
  35. AVCodecParameters *par = s->streams[0]->codecpar;
  36. int ret, size;
  37. size = BLOCK_SIZE * par->channels;
  38. pkt->pos = avio_tell(s->pb);
  39. pkt->stream_index = 0;
  40. ret = av_get_packet(s->pb, pkt, size);
  41. if (ret != size) {
  42. av_packet_unref(pkt);
  43. return ret < 0 ? ret : AVERROR(EIO);
  44. }
  45. if (AV_RB16(pkt->data) & 0x8000) {
  46. av_packet_unref(pkt);
  47. return AVERROR_EOF;
  48. }
  49. pkt->size = size;
  50. pkt->duration = 1;
  51. pkt->pts = (pkt->pos - c->header_size) / size;
  52. return 0;
  53. }
  54. static int adx_read_header(AVFormatContext *s)
  55. {
  56. ADXDemuxerContext *c = s->priv_data;
  57. AVCodecParameters *par;
  58. AVStream *st = avformat_new_stream(s, NULL);
  59. if (!st)
  60. return AVERROR(ENOMEM);
  61. par = s->streams[0]->codecpar;
  62. if (avio_rb16(s->pb) != 0x8000)
  63. return AVERROR_INVALIDDATA;
  64. c->header_size = avio_rb16(s->pb) + 4;
  65. avio_seek(s->pb, -4, SEEK_CUR);
  66. par->extradata = av_mallocz(c->header_size + AV_INPUT_BUFFER_PADDING_SIZE);
  67. if (!par->extradata)
  68. return AVERROR(ENOMEM);
  69. if (avio_read(s->pb, par->extradata, c->header_size) < c->header_size) {
  70. av_freep(&par->extradata);
  71. return AVERROR(EIO);
  72. }
  73. par->extradata_size = c->header_size;
  74. if (par->extradata_size < 12) {
  75. av_log(s, AV_LOG_ERROR, "Invalid extradata size.\n");
  76. return AVERROR_INVALIDDATA;
  77. }
  78. par->channels = AV_RB8 (par->extradata + 7);
  79. par->sample_rate = AV_RB32(par->extradata + 8);
  80. if (par->channels <= 0) {
  81. av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", par->channels);
  82. return AVERROR_INVALIDDATA;
  83. }
  84. par->codec_type = AVMEDIA_TYPE_AUDIO;
  85. par->codec_id = s->iformat->raw_codec_id;
  86. avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, par->sample_rate);
  87. return 0;
  88. }
  89. AVInputFormat ff_adx_demuxer = {
  90. .name = "adx",
  91. .long_name = NULL_IF_CONFIG_SMALL("CRI ADX"),
  92. .priv_data_size = sizeof(ADXDemuxerContext),
  93. .read_header = adx_read_header,
  94. .read_packet = adx_read_packet,
  95. .extensions = "adx",
  96. .raw_codec_id = AV_CODEC_ID_ADPCM_ADX,
  97. .flags = AVFMT_GENERIC_INDEX,
  98. };