GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/aac_adtstoasc_bsf.c Lines: 32 65 49.2 %
Date: 2019-11-20 04:07:19 Branches: 15 36 41.7 %

Line Branch Exec Source
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 "avcodec.h"
25
#include "bsf.h"
26
#include "put_bits.h"
27
#include "get_bits.h"
28
#include "mpeg4audio.h"
29
#include "internal.h"
30
31
typedef struct AACBSFContext {
32
    int first_frame_done;
33
} 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
 */
39
206
static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt)
40
{
41
206
    AACBSFContext *ctx = bsfc->priv_data;
42
43
    GetBitContext gb;
44
    PutBitContext pb;
45
    AACADTSHeaderInfo hdr;
46
    int ret;
47
48
206
    ret = ff_bsf_get_packet_ref(bsfc, pkt);
49
206
    if (ret < 0)
50
86
        return ret;
51
52

120
    if (bsfc->par_in->extradata && pkt->size >= 2 && (AV_RB16(pkt->data) >> 4) != 0xfff)
53
        return 0;
54
55
120
    if (pkt->size < AV_AAC_ADTS_HEADER_SIZE)
56
        goto packet_too_small;
57
58
120
    init_get_bits(&gb, pkt->data, AV_AAC_ADTS_HEADER_SIZE * 8);
59
60
120
    if (ff_adts_header_parse(&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

120
    if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
67
        avpriv_report_missing_feature(bsfc,
68
                                      "Multiple RDBs per frame with CRC");
69
        ret = AVERROR_PATCHWELCOME;
70
        goto fail;
71
    }
72
73
120
    pkt->size -= AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
74
120
    if (pkt->size <= 0)
75
        goto packet_too_small;
76
120
    pkt->data += AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
77
78
120
    if (!ctx->first_frame_done) {
79
2
        int            pce_size = 0;
80
        uint8_t        pce_data[MAX_PCE_SIZE];
81
        uint8_t       *extradata;
82
83
2
        if (!hdr.chan_config) {
84
            init_get_bits(&gb, pkt->data, pkt->size * 8);
85
            if (get_bits(&gb, 3) != 5) {
86
                avpriv_report_missing_feature(bsfc,
87
                                              "PCE-based channel configuration "
88
                                              "without PCE as first syntax "
89
                                              "element");
90
                ret = AVERROR_PATCHWELCOME;
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
100
2
        extradata = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
101
                                            2 + pce_size);
102
2
        if (!extradata) {
103
            ret = AVERROR(ENOMEM);
104
            goto fail;
105
        }
106
107
2
        init_put_bits(&pb, extradata, 2 + pce_size);
108
2
        put_bits(&pb, 5, hdr.object_type);
109
2
        put_bits(&pb, 4, hdr.sampling_index);
110
2
        put_bits(&pb, 4, hdr.chan_config);
111
2
        put_bits(&pb, 1, 0); //frame length - 1024 samples
112
2
        put_bits(&pb, 1, 0); //does not depend on core coder
113
2
        put_bits(&pb, 1, 0); //is not extension
114
2
        flush_put_bits(&pb);
115
2
        if (pce_size) {
116
            memcpy(extradata + 2, pce_data, pce_size);
117
        }
118
119
2
        ctx->first_frame_done = 1;
120
    }
121
122
120
    return 0;
123
124
packet_too_small:
125
    av_log(bsfc, AV_LOG_ERROR, "Input packet too small\n");
126
    ret = AVERROR_INVALIDDATA;
127
fail:
128
    av_packet_unref(pkt);
129
    return ret;
130
}
131
132
2
static int aac_adtstoasc_init(AVBSFContext *ctx)
133
{
134
    /* Validate the extradata if the stream is already MPEG-4 AudioSpecificConfig */
135
2
    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
2
    return 0;
146
}
147
148
static const enum AVCodecID codec_ids[] = {
149
    AV_CODEC_ID_AAC, AV_CODEC_ID_NONE,
150
};
151
152
const AVBitStreamFilter ff_aac_adtstoasc_bsf = {
153
    .name           = "aac_adtstoasc",
154
    .priv_data_size = sizeof(AACBSFContext),
155
    .init           = aac_adtstoasc_init,
156
    .filter         = aac_adtstoasc_filter,
157
    .codec_ids      = codec_ids,
158
};