adxdec.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. size = BLOCK_SIZE * avctx->channels;
  39. pkt->pos = avio_tell(s->pb);
  40. pkt->stream_index = 0;
  41. ret = av_get_packet(s->pb, pkt, size);
  42. if (ret != size) {
  43. av_free_packet(pkt);
  44. return ret < 0 ? ret : AVERROR(EIO);
  45. }
  46. if (AV_RB16(pkt->data) & 0x8000) {
  47. av_free_packet(pkt);
  48. return AVERROR_EOF;
  49. }
  50. pkt->size = size;
  51. pkt->duration = 1;
  52. pkt->pts = (pkt->pos - c->header_size) / size;
  53. return 0;
  54. }
  55. static int adx_read_header(AVFormatContext *s)
  56. {
  57. ADXDemuxerContext *c = s->priv_data;
  58. AVCodecContext *avctx;
  59. int ret;
  60. AVStream *st = avformat_new_stream(s, NULL);
  61. if (!st)
  62. return AVERROR(ENOMEM);
  63. avctx = s->streams[0]->codec;
  64. if (avio_rb16(s->pb) != 0x8000)
  65. return AVERROR_INVALIDDATA;
  66. c->header_size = avio_rb16(s->pb) + 4;
  67. avio_seek(s->pb, -4, SEEK_CUR);
  68. avctx->extradata = av_mallocz(c->header_size + FF_INPUT_BUFFER_PADDING_SIZE);
  69. if (!avctx->extradata)
  70. return AVERROR(ENOMEM);
  71. if (avio_read(s->pb, avctx->extradata, c->header_size) < c->header_size) {
  72. av_freep(&avctx->extradata);
  73. return AVERROR(EIO);
  74. }
  75. avctx->extradata_size = c->header_size;
  76. ret = avpriv_adx_decode_header(avctx, avctx->extradata,
  77. avctx->extradata_size, &c->header_size,
  78. NULL);
  79. if (ret)
  80. return ret;
  81. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  82. st->codec->codec_id = s->iformat->raw_codec_id;
  83. avpriv_set_pts_info(st, 64, BLOCK_SAMPLES, avctx->sample_rate);
  84. return 0;
  85. }
  86. AVInputFormat ff_adx_demuxer = {
  87. .name = "adx",
  88. .long_name = NULL_IF_CONFIG_SMALL("CRI ADX"),
  89. .priv_data_size = sizeof(ADXDemuxerContext),
  90. .read_header = adx_read_header,
  91. .read_packet = adx_read_packet,
  92. .extensions = "adx",
  93. .raw_codec_id = AV_CODEC_ID_ADPCM_ADX,
  94. .flags = AVFMT_GENERIC_INDEX,
  95. };