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