• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
30 typedef struct AACBSFContext {
31     int first_frame_done;
32 } 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  */
aac_adtstoasc_filter(AVBSFContext * bsfc,AVPacket * pkt)38 static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt)
39 {
40     AACBSFContext *ctx = bsfc->priv_data;
41 
42     GetBitContext gb;
43     PutBitContext pb;
44     AACADTSHeaderInfo hdr;
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     init_get_bits(&gb, pkt->data, AV_AAC_ADTS_HEADER_SIZE * 8);
58 
59     if (ff_adts_header_parse(&gb, &hdr) < 0) {
60         av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
61         ret = AVERROR_INVALIDDATA;
62         goto fail;
63     }
64 
65     if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
66         avpriv_report_missing_feature(bsfc,
67                                       "Multiple RDBs per frame with CRC");
68         ret = AVERROR_PATCHWELCOME;
69         goto fail;
70     }
71 
72     pkt->size -= AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
73     if (pkt->size <= 0)
74         goto packet_too_small;
75     pkt->data += AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
76 
77     if (!ctx->first_frame_done) {
78         int            pce_size = 0;
79         uint8_t        pce_data[MAX_PCE_SIZE];
80         uint8_t       *extradata;
81 
82         if (!hdr.chan_config) {
83             init_get_bits(&gb, pkt->data, pkt->size * 8);
84             if (get_bits(&gb, 3) != 5) {
85                 avpriv_report_missing_feature(bsfc,
86                                               "PCE-based channel configuration "
87                                               "without PCE as first syntax "
88                                               "element");
89                 ret = AVERROR_PATCHWELCOME;
90                 goto fail;
91             }
92             init_put_bits(&pb, pce_data, MAX_PCE_SIZE);
93             pce_size = ff_copy_pce_data(&pb, &gb) / 8;
94             flush_put_bits(&pb);
95             pkt->size -= get_bits_count(&gb)/8;
96             pkt->data += get_bits_count(&gb)/8;
97         }
98 
99         extradata = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
100                                             2 + pce_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         ctx->first_frame_done = 1;
119     }
120 
121     return 0;
122 
123 packet_too_small:
124     av_log(bsfc, AV_LOG_ERROR, "Input packet too small\n");
125     ret = AVERROR_INVALIDDATA;
126 fail:
127     av_packet_unref(pkt);
128     return ret;
129 }
130 
aac_adtstoasc_init(AVBSFContext * ctx)131 static int aac_adtstoasc_init(AVBSFContext *ctx)
132 {
133     /* Validate the extradata if the stream is already MPEG-4 AudioSpecificConfig */
134     if (ctx->par_in->extradata) {
135         MPEG4AudioConfig mp4ac;
136         int ret = avpriv_mpeg4audio_get_config2(&mp4ac, ctx->par_in->extradata,
137                                                 ctx->par_in->extradata_size, 1, ctx);
138         if (ret < 0) {
139             av_log(ctx, AV_LOG_ERROR, "Error parsing AudioSpecificConfig extradata!\n");
140             return ret;
141         }
142     }
143 
144     return 0;
145 }
146 
147 static const enum AVCodecID codec_ids[] = {
148     AV_CODEC_ID_AAC, AV_CODEC_ID_NONE,
149 };
150 
151 const FFBitStreamFilter ff_aac_adtstoasc_bsf = {
152     .p.name         = "aac_adtstoasc",
153     .p.codec_ids    = codec_ids,
154     .priv_data_size = sizeof(AACBSFContext),
155     .init           = aac_adtstoasc_init,
156     .filter         = aac_adtstoasc_filter,
157 };
158