FFmpeg
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 "adts_header.h"
23 #include "adts_parser.h"
24 #include "bsf.h"
25 #include "bsf_internal.h"
26 #include "put_bits.h"
27 #include "get_bits.h"
28 #include "mpeg4audio.h"
29 #include "mpeg4audio_copy_pce.h"
30 
31 typedef struct AACBSFContext {
34 
35 /**
36  * This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4
37  * ADTS header and removes the ADTS header.
38  */
40 {
41  AACBSFContext *ctx = bsfc->priv_data;
42 
43  GetBitContext gb;
44  PutBitContext pb;
46  int ret;
47 
48  ret = ff_bsf_get_packet_ref(bsfc, pkt);
49  if (ret < 0)
50  return ret;
51 
52  if (bsfc->par_in->extradata && pkt->size >= 2 && (AV_RB16(pkt->data) >> 4) != 0xfff)
53  return 0;
54 
56  goto packet_too_small;
57 
59 
60  if (ff_adts_header_parse(&gb, &hdr) < 0) {
61  av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
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 
74  if (pkt->size <= 0)
75  goto packet_too_small;
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, pkt->data, pkt->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 = ff_copy_pce_data(&pb, &gb) / 8;
95  flush_put_bits(&pb);
96  pkt->size -= get_bits_count(&gb)/8;
97  pkt->data += get_bits_count(&gb)/8;
98  }
99 
101  2 + pce_size);
102  if (!extradata) {
103  ret = AVERROR(ENOMEM);
104  goto fail;
105  }
106 
107  init_put_bits(&pb, extradata, 2 + pce_size);
108  put_bits(&pb, 5, hdr.object_type);
109  put_bits(&pb, 4, hdr.sampling_index);
110  put_bits(&pb, 4, hdr.chan_config);
111  put_bits(&pb, 1, 0); //frame length - 1024 samples
112  put_bits(&pb, 1, 0); //does not depend on core coder
113  put_bits(&pb, 1, 0); //is not extension
114  flush_put_bits(&pb);
115  if (pce_size) {
116  memcpy(extradata + 2, pce_data, pce_size);
117  }
118 
119  ctx->first_frame_done = 1;
120  }
121 
122  return 0;
123 
124 packet_too_small:
125  av_log(bsfc, AV_LOG_ERROR, "Input packet too small\n");
127 fail:
129  return ret;
130 }
131 
133 {
134  /* Validate the extradata if the stream is already MPEG-4 AudioSpecificConfig */
135  if (ctx->par_in->extradata) {
136  MPEG4AudioConfig mp4ac;
137  int ret = avpriv_mpeg4audio_get_config2(&mp4ac, ctx->par_in->extradata,
138  ctx->par_in->extradata_size, 1, ctx);
139  if (ret < 0) {
140  av_log(ctx, AV_LOG_ERROR, "Error parsing AudioSpecificConfig extradata!\n");
141  return ret;
142  }
143  }
144 
145  return 0;
146 }
147 
148 static const enum AVCodecID codec_ids[] = {
150 };
151 
153  .p.name = "aac_adtstoasc",
154  .p.codec_ids = codec_ids,
155  .priv_data_size = sizeof(AACBSFContext),
158 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:426
AVBSFContext::par_in
AVCodecParameters * par_in
Parameters of the input stream.
Definition: bsf.h:90
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
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
bsf_internal.h
AV_PKT_DATA_NEW_EXTRADATA
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:56
AVBitStreamFilter::name
const char * name
Definition: bsf.h:112
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:267
AV_AAC_ADTS_HEADER_SIZE
#define AV_AAC_ADTS_HEADER_SIZE
Definition: adts_parser.h:25
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:222
AVPacket::data
uint8_t * data
Definition: packet.h:515
ff_aac_adtstoasc_bsf
const FFBitStreamFilter ff_aac_adtstoasc_bsf
Definition: aac_adtstoasc_bsf.c:152
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
MAX_PCE_SIZE
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE.
Definition: mpeg4audio.h:117
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:515
AVBSFContext
The bitstream filter state.
Definition: bsf.h:68
aac_adtstoasc_init
static int aac_adtstoasc_init(AVBSFContext *ctx)
Definition: aac_adtstoasc_bsf.c:132
MPEG4AudioConfig
Definition: mpeg4audio.h:29
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:336
mpeg4audio.h
bsf.h
fail
#define fail()
Definition: checkasm.h:177
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:109
AACADTSHeaderInfo::chan_config
uint8_t chan_config
Definition: adts_header.h:35
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
avpriv_mpeg4audio_get_config2
int avpriv_mpeg4audio_get_config2(MPEG4AudioConfig *c, const uint8_t *buf, int size, int sync_extension, void *logctx)
Parse MPEG-4 systems extradata from a raw buffer to retrieve audio configuration.
Definition: mpeg4audio.c:165
aac_adtstoasc_filter
static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt)
This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4 ADTS header and removes the ADTS h...
Definition: aac_adtstoasc_bsf.c:39
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
ctx
AVFormatContext * ctx
Definition: movenc.c:48
ff_copy_pce_data
static int ff_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
Definition: mpeg4audio_copy_pce.h:38
get_bits.h
PutBitContext
Definition: put_bits.h:50
AACBSFContext
Definition: aac_adtstoasc_bsf.c:31
AACADTSHeaderInfo::num_aac_frames
uint8_t num_aac_frames
Definition: adts_header.h:36
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
FFBitStreamFilter
Definition: bsf_internal.h:27
AACADTSHeaderInfo::sampling_index
uint8_t sampling_index
Definition: adts_header.h:34
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:445
FFBitStreamFilter::p
AVBitStreamFilter p
The public AVBitStreamFilter.
Definition: bsf_internal.h:31
AVPacket::size
int size
Definition: packet.h:516
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
AACADTSHeaderInfo::object_type
uint8_t object_type
Definition: adts_header.h:33
AACBSFContext::first_frame_done
int first_frame_done
Definition: aac_adtstoasc_bsf.c:32
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:83
ret
ret
Definition: filter_design.txt:187
adts_parser.h
AACADTSHeaderInfo::crc_absent
uint8_t crc_absent
Definition: adts_header.h:32
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition: avpacket.c:231
codec_ids
static enum AVCodecID codec_ids[]
Definition: aac_adtstoasc_bsf.c:148
adts_header.h
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
AVPacket
This structure stores compressed data.
Definition: packet.h:492
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
ff_bsf_get_packet_ref
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:256
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
put_bits.h
mpeg4audio_copy_pce.h
AACADTSHeaderInfo
Definition: adts_header.h:28
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98