FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
aac_adtstoasc_bsf.c
Go to the documentation of this file.
1 /*
2  * MPEG-2/4 AAC ADTS to MPEG-4 Audio Specific Configuration bitstream filter
3  * Copyright (c) 2009 Alex Converse <alex.converse@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "avcodec.h"
23 #include "aacadtsdec.h"
24 #include "bsf.h"
25 #include "put_bits.h"
26 #include "get_bits.h"
27 #include "mpeg4audio.h"
28 #include "internal.h"
29 
30 typedef struct AACBSFContext {
33 
34 /**
35  * This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4
36  * ADTS header and removes the ADTS header.
37  */
39 {
40  AACBSFContext *ctx = bsfc->priv_data;
41 
42  GetBitContext gb;
43  PutBitContext pb;
45  AVPacket *in;
46  int ret;
47 
48  ret = ff_bsf_get_packet(bsfc, &in);
49  if (ret < 0)
50  return ret;
51 
52  if (in->size < AAC_ADTS_HEADER_SIZE)
53  goto packet_too_small;
54 
56 
57  if (bsfc->par_in->extradata && show_bits(&gb, 12) != 0xfff)
58  goto finish;
59 
60  if (avpriv_aac_parse_header(&gb, &hdr) < 0) {
61  av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
62  ret = AVERROR_INVALIDDATA;
63  goto fail;
64  }
65 
66  if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
68  "Multiple RDBs per frame with CRC");
70  goto fail;
71  }
72 
73  in->size -= AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
74  if (in->size <= 0)
75  goto packet_too_small;
76  in->data += AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
77 
78  if (!ctx->first_frame_done) {
79  int pce_size = 0;
80  uint8_t pce_data[MAX_PCE_SIZE];
81  uint8_t *extradata;
82 
83  if (!hdr.chan_config) {
84  init_get_bits(&gb, in->data, in->size * 8);
85  if (get_bits(&gb, 3) != 5) {
87  "PCE-based channel configuration "
88  "without PCE as first syntax "
89  "element");
91  goto fail;
92  }
93  init_put_bits(&pb, pce_data, MAX_PCE_SIZE);
94  pce_size = avpriv_copy_pce_data(&pb, &gb)/8;
95  flush_put_bits(&pb);
96  in->size -= get_bits_count(&gb)/8;
97  in->data += get_bits_count(&gb)/8;
98  }
99 
100  extradata = av_mallocz(2 + pce_size + AV_INPUT_BUFFER_PADDING_SIZE);
101  if (!extradata) {
102  ret = AVERROR(ENOMEM);
103  goto fail;
104  }
105 
106  init_put_bits(&pb, extradata, 2 + pce_size);
107  put_bits(&pb, 5, hdr.object_type);
108  put_bits(&pb, 4, hdr.sampling_index);
109  put_bits(&pb, 4, hdr.chan_config);
110  put_bits(&pb, 1, 0); //frame length - 1024 samples
111  put_bits(&pb, 1, 0); //does not depend on core coder
112  put_bits(&pb, 1, 0); //is not extension
113  flush_put_bits(&pb);
114  if (pce_size) {
115  memcpy(extradata + 2, pce_data, pce_size);
116  }
117 
118  bsfc->par_out->extradata = extradata;
119  bsfc->par_out->extradata_size = 2 + pce_size;
120  ctx->first_frame_done = 1;
121  }
122 
123 finish:
124  av_packet_move_ref(out, in);
125  av_packet_free(&in);
126 
127  return 0;
128 
129 packet_too_small:
130  av_log(bsfc, AV_LOG_ERROR, "Input packet too small\n");
131  ret = AVERROR_INVALIDDATA;
132 fail:
133  av_packet_free(&in);
134  return ret;
135 }
136 
138 {
139  av_freep(&ctx->par_out->extradata);
140  ctx->par_out->extradata_size = 0;
141 
142  return 0;
143 }
144 
145 static const enum AVCodecID codec_ids[] = {
147 };
148 
150  .name = "aac_adtstoasc",
151  .priv_data_size = sizeof(AACBSFContext),
154  .codec_ids = codec_ids,
155 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVCodecParameters * par_out
Parameters of the output stream.
Definition: avcodec.h:5725
uint8_t object_type
Definition: aacadtsdec.h:36
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:206
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:247
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
The bitstream filter state.
Definition: avcodec.h:5694
int size
Definition: avcodec.h:1589
static int aac_adtstoasc_init(AVBSFContext *ctx)
void * priv_data
Opaque filter-specific private data.
Definition: avcodec.h:5715
static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *out)
This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4 ADTS header and removes the ADTS h...
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:62
uint8_t
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
Definition: cfhd.c:80
const AVBitStreamFilter ff_aac_adtstoasc_bsf
const char * name
Definition: avcodec.h:5741
static void finish(void)
Definition: movenc.c:338
uint8_t * data
Definition: avcodec.h:1588
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:617
bitstream reader API header.
#define av_log(a,...)
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:189
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
uint8_t sampling_index
Definition: aacadtsdec.h:37
#define fail()
Definition: checkasm.h:82
uint8_t chan_config
Definition: aacadtsdec.h:38
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3957
uint8_t num_aac_frames
Definition: aacadtsdec.h:39
AVFormatContext * ctx
Definition: movenc.c:48
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:282
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AAC_ADTS_HEADER_SIZE
Definition: aacadtsdec.h:29
Libavcodec external API header.
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
int avpriv_aac_parse_header(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
Parse AAC frame header.
Definition: aacadtsdec.c:29
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:406
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
static enum AVCodecID codec_ids[]
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
Called by the bitstream filters to get the next packet for filtering.
Definition: bsf.c:199
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE.
Definition: mpeg4audio.h:105
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:731
int avpriv_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
Definition: mpeg4audio.c:166
uint8_t crc_absent
Definition: aacadtsdec.h:35
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3953
FILE * out
Definition: movenc.c:54
#define av_freep(p)
This structure stores compressed data.
Definition: avcodec.h:1565
AVCodecParameters * par_in
Parameters of the input stream.
Definition: avcodec.h:5720
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
bitstream writer API