/src/ffmpeg/libavformat/aptxdec.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * RAW aptX demuxer |
3 | | * |
4 | | * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org> |
5 | | * |
6 | | * This file is part of FFmpeg. |
7 | | * |
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. |
12 | | * |
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. |
17 | | * |
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 |
21 | | */ |
22 | | |
23 | | #include "config_components.h" |
24 | | |
25 | | #include "libavutil/opt.h" |
26 | | #include "avformat.h" |
27 | | #include "demux.h" |
28 | | |
29 | 93.8k | #define APTX_BLOCK_SIZE 4 |
30 | 49.7k | #define APTX_PACKET_SIZE (256*APTX_BLOCK_SIZE) |
31 | | |
32 | 77.1k | #define APTX_HD_BLOCK_SIZE 6 |
33 | 41.4k | #define APTX_HD_PACKET_SIZE (256*APTX_HD_BLOCK_SIZE) |
34 | | |
35 | | typedef struct AptXDemuxerContext { |
36 | | AVClass *class; |
37 | | int sample_rate; |
38 | | } AptXDemuxerContext; |
39 | | |
40 | | static AVStream *aptx_read_header_common(AVFormatContext *s) |
41 | 11.7k | { |
42 | 11.7k | AptXDemuxerContext *s1 = s->priv_data; |
43 | 11.7k | AVStream *st = avformat_new_stream(s, NULL); |
44 | 11.7k | if (!st) |
45 | 0 | return NULL; |
46 | 11.7k | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
47 | 11.7k | st->codecpar->format = AV_SAMPLE_FMT_S32P; |
48 | 11.7k | st->codecpar->ch_layout.nb_channels = 2; |
49 | 11.7k | st->codecpar->sample_rate = s1->sample_rate; |
50 | 11.7k | st->start_time = 0; |
51 | 11.7k | return st; |
52 | 11.7k | } |
53 | | |
54 | | static int aptx_read_header(AVFormatContext *s) |
55 | 5.82k | { |
56 | 5.82k | AVStream *st = aptx_read_header_common(s); |
57 | 5.82k | if (!st) |
58 | 0 | return AVERROR(ENOMEM); |
59 | 5.82k | st->codecpar->codec_id = AV_CODEC_ID_APTX; |
60 | 5.82k | st->codecpar->bits_per_coded_sample = 4; |
61 | 5.82k | st->codecpar->block_align = APTX_BLOCK_SIZE; |
62 | 5.82k | return 0; |
63 | 5.82k | } |
64 | | |
65 | | static int aptx_hd_read_header(AVFormatContext *s) |
66 | 5.94k | { |
67 | 5.94k | AVStream *st = aptx_read_header_common(s); |
68 | 5.94k | if (!st) |
69 | 0 | return AVERROR(ENOMEM); |
70 | 5.94k | st->codecpar->codec_id = AV_CODEC_ID_APTX_HD; |
71 | 5.94k | st->codecpar->bits_per_coded_sample = 6; |
72 | 5.94k | st->codecpar->block_align = APTX_HD_BLOCK_SIZE; |
73 | 5.94k | return 0; |
74 | 5.94k | } |
75 | | |
76 | | static int aptx_read_packet(AVFormatContext *s, AVPacket *pkt) |
77 | 49.7k | { |
78 | 49.7k | int ret = av_get_packet(s->pb, pkt, APTX_PACKET_SIZE); |
79 | 49.7k | if (ret >= 0 && !(ret % APTX_BLOCK_SIZE)) |
80 | 37.8k | pkt->flags &= ~AV_PKT_FLAG_CORRUPT; |
81 | 49.7k | return ret >= 0 ? 0 : ret; |
82 | 49.7k | } |
83 | | |
84 | | static int aptx_hd_read_packet(AVFormatContext *s, AVPacket *pkt) |
85 | 41.4k | { |
86 | 41.4k | int ret = av_get_packet(s->pb, pkt, APTX_HD_PACKET_SIZE); |
87 | 41.4k | if (ret >= 0 && !(ret % APTX_HD_BLOCK_SIZE)) |
88 | 29.2k | pkt->flags &= ~AV_PKT_FLAG_CORRUPT; |
89 | 41.4k | return ret >= 0 ? 0 : ret; |
90 | 41.4k | } |
91 | | |
92 | | static const AVOption aptx_options[] = { |
93 | | { "sample_rate", "", offsetof(AptXDemuxerContext, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, |
94 | | { NULL }, |
95 | | }; |
96 | | |
97 | | static const AVClass aptx_demuxer_class = { |
98 | | .class_name = "aptx (hd) demuxer", |
99 | | .item_name = av_default_item_name, |
100 | | .option = aptx_options, |
101 | | .version = LIBAVUTIL_VERSION_INT, |
102 | | }; |
103 | | |
104 | | #if CONFIG_APTX_DEMUXER |
105 | | const FFInputFormat ff_aptx_demuxer = { |
106 | | .p.name = "aptx", |
107 | | .p.long_name = NULL_IF_CONFIG_SMALL("raw aptX"), |
108 | | .p.extensions = "aptx", |
109 | | .p.flags = AVFMT_GENERIC_INDEX, |
110 | | .p.priv_class = &aptx_demuxer_class, |
111 | | .priv_data_size = sizeof(AptXDemuxerContext), |
112 | | .read_header = aptx_read_header, |
113 | | .read_packet = aptx_read_packet, |
114 | | }; |
115 | | #endif |
116 | | |
117 | | #if CONFIG_APTX_HD_DEMUXER |
118 | | const FFInputFormat ff_aptx_hd_demuxer = { |
119 | | .p.name = "aptx_hd", |
120 | | .p.long_name = NULL_IF_CONFIG_SMALL("raw aptX HD"), |
121 | | .p.extensions = "aptxhd", |
122 | | .p.flags = AVFMT_GENERIC_INDEX, |
123 | | .p.priv_class = &aptx_demuxer_class, |
124 | | .priv_data_size = sizeof(AptXDemuxerContext), |
125 | | .read_header = aptx_hd_read_header, |
126 | | .read_packet = aptx_hd_read_packet, |
127 | | }; |
128 | | #endif |