FFmpeg
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 
25 typedef struct FTRContext {
26  AVCodecContext *aac_avctx[64]; // wrapper context for AAC
30 } FTRContext;
31 
32 static av_cold int ftr_init(AVCodecContext *avctx)
33 {
34  FTRContext *s = avctx->priv_data;
35  const AVCodec *codec;
36  int ret;
37 
38  if (avctx->ch_layout.nb_channels > 64 ||
39  avctx->ch_layout.nb_channels <= 0)
40  return AVERROR(EINVAL);
41 
42  s->packet = av_packet_alloc();
43  if (!s->packet)
44  return AVERROR(ENOMEM);
45 
46  s->frame = av_frame_alloc();
47  if (!s->frame)
48  return AVERROR(ENOMEM);
49 
50  s->nb_context = avctx->ch_layout.nb_channels;
51 
53  if (!codec)
54  return AVERROR_BUG;
55 
56  for (int i = 0; i < s->nb_context; i++) {
57  s->aac_avctx[i] = avcodec_alloc_context3(codec);
58  if (!s->aac_avctx[i])
59  return AVERROR(ENOMEM);
60  ret = avcodec_open2(s->aac_avctx[i], codec, NULL);
61  if (ret < 0)
62  return ret;
63  }
64 
65  avctx->sample_fmt = s->aac_avctx[0]->sample_fmt;
67  return AVERROR(EINVAL);
68 
69  return 0;
70 }
71 
73  int *got_frame, AVPacket *avpkt)
74 {
75  FTRContext *s = avctx->priv_data;
76  GetBitContext gb;
77  int ret, ch_offset = 0;
78 
79  ret = init_get_bits8(&gb, avpkt->data, avpkt->size);
80  if (ret < 0)
81  return ret;
82 
83  frame->nb_samples = 0;
84 
85  for (int i = 0; i < s->nb_context; i++) {
86  AVCodecContext *codec_avctx = s->aac_avctx[i];
87  GetBitContext gb2 = gb;
88  AACADTSHeaderInfo hdr_info;
89  int size;
90 
91  if (get_bits_left(&gb) < 64)
92  return AVERROR_INVALIDDATA;
93 
94  memset(&hdr_info, 0, sizeof(hdr_info));
95 
96  size = ff_adts_header_parse(&gb2, &hdr_info);
97  if (size <= 0 || size * 8 > get_bits_left(&gb))
98  return AVERROR_INVALIDDATA;
99 
100  if (size > s->packet->size) {
101  ret = av_grow_packet(s->packet, size - s->packet->size);
102  if (ret < 0)
103  return ret;
104  }
105 
106  ret = av_packet_make_writable(s->packet);
107  if (ret < 0)
108  return ret;
109 
110  memcpy(s->packet->data, avpkt->data + (get_bits_count(&gb) >> 3), size);
111  s->packet->size = size;
112 
113  if (size > 12) {
114  uint8_t *buf = s->packet->data;
115 
116  if (buf[3] & 0x20) {
117  int tmp = buf[8];
118  buf[ 9] = ~buf[9];
119  buf[11] = ~buf[11];
120  buf[12] = ~buf[12];
121  buf[ 8] = ~buf[10];
122  buf[10] = ~tmp;
123  }
124  }
125 
126  ret = avcodec_send_packet(codec_avctx, s->packet);
127  if (ret < 0) {
128  av_log(avctx, AV_LOG_ERROR, "Error submitting a packet for decoding\n");
129  return ret;
130  }
131 
132  ret = avcodec_receive_frame(codec_avctx, s->frame);
133  if (ret < 0)
134  return ret;
135 
136  if (!avctx->sample_rate) {
137  avctx->sample_rate = codec_avctx->sample_rate;
138  } else {
139  if (avctx->sample_rate != codec_avctx->sample_rate)
140  return AVERROR_INVALIDDATA;
141  }
142 
143  if (!frame->nb_samples) {
144  frame->nb_samples = s->frame->nb_samples;
145  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
146  return ret;
147  } else {
148  if (frame->nb_samples != s->frame->nb_samples)
149  return AVERROR_INVALIDDATA;
150  }
151 
152  skip_bits_long(&gb, size * 8);
153 
154  if (ch_offset + s->frame->ch_layout.nb_channels > avctx->ch_layout.nb_channels)
155  return AVERROR_INVALIDDATA;
156 
157  if (avctx->sample_fmt != codec_avctx->sample_fmt)
158  return AVERROR_INVALIDDATA;
159 
160  for (int ch = 0; ch < s->frame->ch_layout.nb_channels; ch++)
161  memcpy(frame->extended_data[ch_offset + ch],
162  s->frame->extended_data[ch],
163  av_get_bytes_per_sample(codec_avctx->sample_fmt) * s->frame->nb_samples);
164 
165  ch_offset += s->frame->ch_layout.nb_channels;
166 
167  if (ch_offset >= avctx->ch_layout.nb_channels)
168  break;
169  }
170 
171  *got_frame = 1;
172 
173  return get_bits_count(&gb) >> 3;
174 }
175 
176 static void ftr_flush(AVCodecContext *avctx)
177 {
178  FTRContext *s = avctx->priv_data;
179 
180  for (int i = 0; i < s->nb_context; i++)
181  avcodec_flush_buffers(s->aac_avctx[i]);
182 }
183 
184 static av_cold int ftr_close(AVCodecContext *avctx)
185 {
186  FTRContext *s = avctx->priv_data;
187 
188  for (int i = 0; i < s->nb_context; i++)
189  avcodec_free_context(&s->aac_avctx[i]);
190  av_packet_free(&s->packet);
191  av_frame_free(&s->frame);
192 
193  return 0;
194 }
195 
197  .p.name = "ftr",
198  .p.long_name = NULL_IF_CONFIG_SMALL("FTR Voice"),
199  .p.type = AVMEDIA_TYPE_AUDIO,
200  .p.id = AV_CODEC_ID_FTR,
201  .init = ftr_init,
203  .close = ftr_close,
204  .flush = ftr_flush,
205  .priv_data_size = sizeof(FTRContext),
206  .p.capabilities =
208  AV_CODEC_CAP_SUBFRAMES |
209 #endif
211  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
212 };
AVCodec
AVCodec.
Definition: codec.h:187
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
FTRContext
Definition: ftr.c:25
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1056
av_grow_packet
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: packet.c:121
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AVPacket::data
uint8_t * data
Definition: packet.h:539
FFCodec
Definition: codec_internal.h:127
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: packet.c:74
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1071
ff_adts_header_parse
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
GetBitContext
Definition: get_bits.h:108
ftr_close
static av_cold int ftr_close(AVCodecContext *avctx)
Definition: ftr.c:184
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
ftr_decode_frame
static int ftr_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: ftr.c:72
ftr_init
static av_cold int ftr_init(AVCodecContext *avctx)
Definition: ftr.c:32
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:149
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
s
#define s(width, name)
Definition: cbs_vp9.c:198
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
avcodec_receive_frame
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder or encoder (when the AV_CODEC_FLAG_RECON_FRAME flag is used...
Definition: avcodec.c:704
av_sample_fmt_is_planar
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:114
decode.h
get_bits.h
FTRContext::aac_avctx
AVCodecContext * aac_avctx[64]
Definition: ftr.c:26
NULL
#define NULL
Definition: coverity.c:32
avcodec_free_context
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
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:143
avcodec_find_decoder
const AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:1010
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:447
ftr_flush
static void ftr_flush(AVCodecContext *avctx)
Definition: ftr.c:176
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1692
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
codec_internal.h
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1063
size
int size
Definition: twinvq_data.h:10344
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: packet.c:63
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:727
FTRContext::packet
AVPacket * packet
Definition: ftr.c:28
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:108
FTRContext::nb_context
int nb_context
Definition: ftr.c:27
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
avcodec.h
ret
ret
Definition: filter_design.txt:187
avcodec_flush_buffers
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal codec state / flush internal buffers.
Definition: avcodec.c:365
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:451
ff_ftr_decoder
const FFCodec ff_ftr_decoder
Definition: ftr.c:196
av_packet_make_writable
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:511
FF_API_SUBFRAMES
#define FF_API_SUBFRAMES
Definition: version_major.h:41
adts_header.h
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVPacket
This structure stores compressed data.
Definition: packet.h:516
FTRContext::frame
AVFrame * frame
Definition: ftr.c:29
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:52
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CODEC_ID_FTR
@ AV_CODEC_ID_FTR
Definition: codec_id.h:545
AACADTSHeaderInfo
Definition: adts_header.h:35