4 * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org>
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "config_components.h"
25 #include "libavutil/opt.h"
28 #define APTX_BLOCK_SIZE 4
29 #define APTX_PACKET_SIZE (256*APTX_BLOCK_SIZE)
31 #define APTX_HD_BLOCK_SIZE 6
32 #define APTX_HD_PACKET_SIZE (256*APTX_HD_BLOCK_SIZE)
34 typedef struct AptXDemuxerContext {
39 static AVStream *aptx_read_header_common(AVFormatContext *s)
41 AptXDemuxerContext *s1 = s->priv_data;
42 AVStream *st = avformat_new_stream(s, NULL);
45 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
46 st->codecpar->format = AV_SAMPLE_FMT_S32P;
47 st->codecpar->ch_layout.nb_channels = 2;
48 st->codecpar->sample_rate = s1->sample_rate;
53 static int aptx_read_header(AVFormatContext *s)
55 AVStream *st = aptx_read_header_common(s);
57 return AVERROR(ENOMEM);
58 st->codecpar->codec_id = AV_CODEC_ID_APTX;
59 st->codecpar->bits_per_coded_sample = 4;
60 st->codecpar->block_align = APTX_BLOCK_SIZE;
64 static int aptx_hd_read_header(AVFormatContext *s)
66 AVStream *st = aptx_read_header_common(s);
68 return AVERROR(ENOMEM);
69 st->codecpar->codec_id = AV_CODEC_ID_APTX_HD;
70 st->codecpar->bits_per_coded_sample = 6;
71 st->codecpar->block_align = APTX_HD_BLOCK_SIZE;
75 static int aptx_read_packet(AVFormatContext *s, AVPacket *pkt)
77 int ret = av_get_packet(s->pb, pkt, APTX_PACKET_SIZE);
78 if (ret >= 0 && !(ret % APTX_BLOCK_SIZE))
79 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
80 return ret >= 0 ? 0 : ret;
83 static int aptx_hd_read_packet(AVFormatContext *s, AVPacket *pkt)
85 int ret = av_get_packet(s->pb, pkt, APTX_HD_PACKET_SIZE);
86 if (ret >= 0 && !(ret % APTX_HD_BLOCK_SIZE))
87 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
88 return ret >= 0 ? 0 : ret;
91 static const AVOption aptx_options[] = {
92 { "sample_rate", "", offsetof(AptXDemuxerContext, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
96 static const AVClass aptx_demuxer_class = {
97 .class_name = "aptx (hd) demuxer",
98 .item_name = av_default_item_name,
99 .option = aptx_options,
100 .version = LIBAVUTIL_VERSION_INT,
103 #if CONFIG_APTX_DEMUXER
104 const AVInputFormat ff_aptx_demuxer = {
106 .long_name = NULL_IF_CONFIG_SMALL("raw aptX"),
107 .extensions = "aptx",
108 .priv_data_size = sizeof(AptXDemuxerContext),
109 .read_header = aptx_read_header,
110 .read_packet = aptx_read_packet,
111 .flags = AVFMT_GENERIC_INDEX,
112 .priv_class = &aptx_demuxer_class,
116 #if CONFIG_APTX_HD_DEMUXER
117 const AVInputFormat ff_aptx_hd_demuxer = {
119 .long_name = NULL_IF_CONFIG_SMALL("raw aptX HD"),
120 .extensions = "aptxhd",
121 .priv_data_size = sizeof(AptXDemuxerContext),
122 .read_header = aptx_hd_read_header,
123 .read_packet = aptx_hd_read_packet,
124 .flags = AVFMT_GENERIC_INDEX,
125 .priv_class = &aptx_demuxer_class,