adxdec.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "libavcodec/adx.h"
  26. #include "avformat.h"
  27. #include "internal.h"
  28. #define BLOCK_SIZE 18
  29. #define BLOCK_SAMPLES 32
  30. typedef struct ADXDemuxerContext {
  31. int header_size;
  32. } ADXDemuxerContext;
  33. static int adx_read_packet(AVFormatContext *s, AVPacket *pkt)
  34. {
  35. ADXDemuxerContext *c = s->priv_data;
  36. AVCodecContext *avctx = s->streams[0]->codec;
  37. int ret, size;
  38. if (avctx->channels <= 0) {
  39. av_log(s, AV_LOG_ERROR, "invalid number of channels %d\n", avctx->channels);
  40. return AVERROR_INVALIDDATA;
  41. }
  42. size = BLOCK_SIZE * avctx->channels;
  43. pkt->pos = avio_tell(s->pb);
  44. pkt->stream_index = 0;
  45. ret = av_get_packet(s->pb, pkt, size);
  46. if (ret != size) {
  47. av_free_packet(pkt);
  48. return ret < 0 ? ret : AVERROR(EIO);
  49. }
  50. if (AV_RB16(pkt->data) & 0x8000) {
  51. av_free_packet(pkt);
  52. return AVERROR_EOF;
  53. }
  54. pkt->size = size;
  55. pkt->duration = 1;
  56. pkt->pts = (pkt->pos - c->header_size) / size;
  57. return 0;
  58. }
  59. static int adx_read_header(AVFormatContext *s, AVFormatParameters *ap)
  60. {
  61. ADXDemuxerContext *c = s->priv_data;
  62. AVCodecContext *avctx;
  63. int ret;
  64. AVStream *st = avformat_new_stream(s, NULL);
  65. if (!st)
  66. return AVERROR(ENOMEM);
  67. avctx = s->streams[0]->codec;
  68. if (avio_rb16(s->pb) != 0x8000)
  69. return AVERROR_INVALIDDATA;
  70. c->header_size = avio_rb16(s->pb) + 4;
  71. avio_seek(s->pb, -4, SEEK_CUR);
  72. avctx->extradata = av_mallocz(c->header_size + FF_INPUT_BUFFER_PADDING_SIZE);
  73. if (!avctx->extradata)
  74. return AVERROR(ENOMEM);
  75. if (avio_read(s->pb, avctx->extradata, c->header_size) < c->header_size) {
  76. av_freep(&avctx->extradata);
  77. return AVERROR(EIO);
  78. }
  79. avctx->extradata_size = c->header_size;
  80. ret = avpriv_adx_decode_header(avctx, avctx->extradata,
  81. avctx->extradata_size, &c->header_size,
  82. NULL);
  83. if (ret)
  84. return ret;
  85. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  86. st->codec->codec_id = s->iformat->value;
  87. avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, avctx->sample_rate);
  88. return 0;
  89. }
  90. AVInputFormat ff_adx_demuxer = {
  91. .name = "adx",
  92. .long_name = NULL_IF_CONFIG_SMALL("CRI ADX"),
  93. .priv_data_size = sizeof(ADXDemuxerContext),
  94. .read_header = adx_read_header,
  95. .read_packet = adx_read_packet,
  96. .extensions = "adx",
  97. .value = CODEC_ID_ADPCM_ADX,
  98. .flags = AVFMT_GENERIC_INDEX,
  99. };