FFmpeg
Loading...
Searching...
No Matches
ftr.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "adts_header.h"
20#include "avcodec.h"
21#include "codec_internal.h"
22#include "get_bits.h"
23#include "decode.h"
24
26
27typedef struct FTRContext {
28 AVCodecContext *aac_avctx[64]; // wrapper context for AAC
33
35{
36 FTRContext *s = avctx->priv_data;
37 const AVCodec *codec;
38 int ret;
39
40 if (avctx->ch_layout.nb_channels > 64 ||
41 avctx->ch_layout.nb_channels <= 0)
42 return AVERROR(EINVAL);
43
44 s->packet = av_packet_alloc();
45 if (!s->packet)
46 return AVERROR(ENOMEM);
47
48 s->frame = av_frame_alloc();
49 if (!s->frame)
50 return AVERROR(ENOMEM);
51
52 s->nb_context = avctx->ch_layout.nb_channels;
53
55 if (!codec)
57
58 for (int i = 0; i < s->nb_context; i++) {
59 s->aac_avctx[i] = avcodec_alloc_context3(codec);
60 if (!s->aac_avctx[i])
61 return AVERROR(ENOMEM);
62 ret = avcodec_open2(s->aac_avctx[i], codec, NULL);
63 if (ret < 0)
64 return ret;
65 }
66
67 avctx->sample_fmt = s->aac_avctx[0]->sample_fmt;
69 return AVERROR(EINVAL);
70
71 return 0;
72}
73
75 int *got_frame, AVPacket *avpkt)
76{
77 FTRContext *s = avctx->priv_data;
79 int ret, ch_offset = 0;
80
81 ret = init_get_bits8(&gb, avpkt->data, avpkt->size);
82 if (ret < 0)
83 return ret;
84
85 frame->nb_samples = 0;
86
87 for (int i = 0; i < s->nb_context; i++) {
88 AVCodecContext *codec_avctx = s->aac_avctx[i];
89 GetBitContext gb2 = gb;
90 AACADTSHeaderInfo hdr_info;
91 int size;
92
93 if (get_bits_left(&gb) < 64)
95
96 memset(&hdr_info, 0, sizeof(hdr_info));
97
98 size = ff_adts_header_parse(&gb2, &hdr_info);
100 return AVERROR_INVALIDDATA;
101
102 if (size > s->packet->size) {
103 ret = av_grow_packet(s->packet, size - s->packet->size);
104 if (ret < 0)
105 return ret;
106 }
107
108 ret = av_packet_make_writable(s->packet);
109 if (ret < 0)
110 return ret;
111
112 memcpy(s->packet->data, avpkt->data + (get_bits_count(&gb) >> 3), size);
113 s->packet->size = size;
114
115 if (size > 12) {
116 uint8_t *buf = s->packet->data;
117
118 if (buf[3] & 0x20) {
119 int tmp = buf[8];
120 buf[ 9] = ~buf[9];
121 buf[11] = ~buf[11];
122 buf[12] = ~buf[12];
123 buf[ 8] = ~buf[10];
124 buf[10] = ~tmp;
125 }
126 }
127
128 ret = avcodec_send_packet(codec_avctx, s->packet);
129 if (ret < 0) {
130 av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
131 return ret;
132 }
133
134 ret = avcodec_receive_frame(codec_avctx, s->frame);
135 if (ret < 0)
136 return ret;
137
138 if (!avctx->sample_rate) {
139 avctx->sample_rate = codec_avctx->sample_rate;
140 } else {
141 if (avctx->sample_rate != codec_avctx->sample_rate)
142 return AVERROR_INVALIDDATA;
143 }
144
145 if (!frame->nb_samples) {
146 frame->nb_samples = s->frame->nb_samples;
147 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
148 return ret;
149 } else {
150 if (frame->nb_samples != s->frame->nb_samples)
151 return AVERROR_INVALIDDATA;
152 }
153
154 skip_bits_long(&gb, size * 8);
155
156 if (ch_offset + s->frame->ch_layout.nb_channels > avctx->ch_layout.nb_channels)
157 return AVERROR_INVALIDDATA;
158
159 if (avctx->sample_fmt != codec_avctx->sample_fmt)
160 return AVERROR_INVALIDDATA;
161
162 for (int ch = 0; ch < s->frame->ch_layout.nb_channels; ch++)
163 memcpy(frame->extended_data[ch_offset + ch],
164 s->frame->extended_data[ch],
165 av_get_bytes_per_sample(codec_avctx->sample_fmt) * s->frame->nb_samples);
166
167 ch_offset += s->frame->ch_layout.nb_channels;
168
169 if (ch_offset >= avctx->ch_layout.nb_channels)
170 break;
171 }
172
173 *got_frame = 1;
174
175 return get_bits_count(&gb) >> 3;
176}
177
179{
180 FTRContext *s = avctx->priv_data;
181
182 for (int i = 0; i < s->nb_context; i++)
183 avcodec_flush_buffers(s->aac_avctx[i]);
184}
185
187{
188 FTRContext *s = avctx->priv_data;
189
190 for (int i = 0; i < s->nb_context; i++)
191 avcodec_free_context(&s->aac_avctx[i]);
192 av_packet_free(&s->packet);
193 av_frame_free(&s->frame);
194
195 return 0;
196}
197
199 .p.name = "ftr",
200 .p.long_name = NULL_IF_CONFIG_SMALL("FTR Voice"),
201 .p.type = AVMEDIA_TYPE_AUDIO,
202 .p.id = AV_CODEC_ID_FTR,
203 .init = ftr_init,
205 .close = ftr_close,
206 .flush = ftr_flush,
207 .priv_data_size = sizeof(FTRContext),
208 .p.capabilities = AV_CODEC_CAP_DR1,
209 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
210};
int ff_adts_header_parse(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
Parse the ADTS frame header to the end of the variable header, which is the first 54 bits.
Definition adts_header.c:30
const FFCodec ff_ftr_decoder
Definition ftr.c:198
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define NULL
Definition coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
static int ftr_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition ftr.c:74
static av_cold int ftr_close(AVCodecContext *avctx)
Definition ftr.c:186
static av_cold void ftr_flush(AVCodecContext *avctx)
Definition ftr.c:178
static av_cold int ftr_init(AVCodecContext *avctx)
Definition ftr.c:34
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition avcodec.c:144
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition options.c:149
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition allcodecs.c:990
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition options.c:164
@ AV_CODEC_ID_FTR
Definition codec_id.h:553
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Alias for avcodec_receive_frame_flags(avctx, frame, 0).
Definition avcodec.c:720
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition decode.c:730
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal codec state / flush internal buffers.
Definition avcodec.c:389
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition packet.c:121
int av_packet_make_writable(AVPacket *pkt)
Create a writable reference for the data described by a given packet, avoiding data copy if possible.
Definition packet.c:516
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
Definition error.h:54
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition samplefmt.c:114
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition samplefmt.c:108
Macro definitions for various function/variable attributes.
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
int nb_channels
Number of channels in this layout.
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int sample_rate
samples per second
Definition avcodec.h:1040
void * priv_data
Definition avcodec.h:470
AVCodec.
Definition codec.h:175
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
int nb_context
Definition ftr.c:29
AVPacket * packet
Definition ftr.c:30
AVCodecContext * aac_avctx[64]
Definition ftr.c:28
AVFrame * frame
Definition ftr.c:31
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int size