FFmpeg
Loading...
Searching...
No Matches
aac_adtstoasc.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
24#include "libavcodec/bsf.h"
26#include "libavcodec/put_bits.h"
27#include "libavcodec/get_bits.h"
30
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{
42
45 int ret;
46
47 ret = ff_bsf_get_packet_ref(bsfc, pkt);
48 if (ret < 0)
49 return ret;
50
51 if (bsfc->par_in->extradata && pkt->size >= 2 && (AV_RB16(pkt->data) >> 4) != 0xfff)
52 return 0;
53
54 if (pkt->size < AV_AAC_ADTS_HEADER_SIZE)
55 goto packet_too_small;
56
57 if (ff_adts_header_parse_buf(pkt->data, &hdr) < 0) {
58 av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
60 goto fail;
61 }
62
63 if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
65 "Multiple RDBs per frame with CRC");
67 goto fail;
68 }
69
70 pkt->size -= AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
71 if (pkt->size <= 0)
72 goto packet_too_small;
73 pkt->data += AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
74
75 if (!ctx->first_frame_done) {
76 int pce_size = 0;
77 uint8_t pce_data[MAX_PCE_SIZE];
78 uint8_t *extradata;
79
80 if (!hdr.chan_config) {
82 init_get_bits(&gb, pkt->data, pkt->size * 8);
83 if (get_bits(&gb, 3) != 5) {
85 "PCE-based channel configuration "
86 "without PCE as first syntax "
87 "element");
89 goto fail;
90 }
91 init_put_bits(&pb, pce_data, MAX_PCE_SIZE);
92 pce_size = ff_copy_pce_data(&pb, &gb) / 8;
93 flush_put_bits(&pb);
94 pkt->size -= get_bits_count(&gb)/8;
95 pkt->data += get_bits_count(&gb)/8;
96 }
97
99 2 + pce_size);
100 if (!extradata) {
101 ret = AVERROR(ENOMEM);
102 goto fail;
103 }
104
105 init_put_bits(&pb, extradata, 2 + pce_size);
106 put_bits(&pb, 5, hdr.object_type);
107 put_bits(&pb, 4, hdr.sampling_index);
108 put_bits(&pb, 4, hdr.chan_config);
109 put_bits(&pb, 1, 0); //frame length - 1024 samples
110 put_bits(&pb, 1, 0); //does not depend on core coder
111 put_bits(&pb, 1, 0); //is not extension
112 flush_put_bits(&pb);
113 if (pce_size) {
114 memcpy(extradata + 2, pce_data, pce_size);
115 }
116
117 ctx->first_frame_done = 1;
118 }
119
120 return 0;
121
122packet_too_small:
123 av_log(bsfc, AV_LOG_ERROR, "Input packet too small\n");
125fail:
127 return ret;
128}
129
131{
132 /* Validate the extradata if the stream is already MPEG-4 AudioSpecificConfig */
133 if (ctx->par_in->extradata) {
134 MPEG4AudioConfig mp4ac;
135 int ret = avpriv_mpeg4audio_get_config2(&mp4ac, ctx->par_in->extradata,
136 ctx->par_in->extradata_size, 1, ctx);
137 if (ret < 0) {
138 av_log(ctx, AV_LOG_ERROR, "Error parsing AudioSpecificConfig extradata!\n");
139 return ret;
140 }
141 }
142
143 return 0;
144}
145
146static const enum AVCodecID codec_ids[] = {
148};
149
151 .p.name = "aac_adtstoasc",
152 .p.codec_ids = codec_ids,
153 .priv_data_size = sizeof(AACBSFContext),
156};
static int aac_adtstoasc_init(AVBSFContext *ctx)
static enum AVCodecID codec_ids[]
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...
int ff_adts_header_parse_buf(const uint8_t buf[AV_AAC_ADTS_HEADER_SIZE+AV_INPUT_BUFFER_PADDING_SIZE], AACADTSHeaderInfo *hdr)
Wrapper around ff_adts_header_parse() for users that don't already have a suitable GetBitContext.
Definition adts_header.c:76
#define AV_AAC_ADTS_HEADER_SIZE
Definition adts_parser.h:25
const FFBitStreamFilter ff_aac_adtstoasc_bsf
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition bsf.c:254
static AVPacket * pkt
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
bitstream reader API header.
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#define fail
Definition test.h:479
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
@ 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
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
#define AV_RB16(p)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
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:212
#define MAX_PCE_SIZE
Maximum size of a PCE including the 3-bit ID_PCE marker and the comment.
Definition mpeg4audio.h:119
static int ff_copy_pce_data(PutBitContext *pb, GetBitContext *gb)
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
uint8_t num_aac_frames
Definition adts_header.h:43
uint8_t sampling_index
Definition adts_header.h:41
The bitstream filter state.
Definition bsf.h:68
void * priv_data
Opaque filter-specific private data.
Definition bsf.h:83
AVCodecParameters * par_in
Parameters of the input stream.
Definition bsf.h:90
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
This structure stores compressed data.
Definition packet.h:580
#define av_log(a,...)
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
static AVFormatContext * ctx
Definition movenc.c:49