aac_adtstoasc_bsf.c 5.02 KB
Newer Older
1 2 3 4
/*
 * MPEG-2/4 AAC ADTS to MPEG-4 Audio Specific Configuration bitstream filter
 * Copyright (c) 2009 Alex Converse <alex.converse@gmail.com>
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22 23
#include "adts_header.h"
#include "adts_parser.h"
24
#include "avcodec.h"
25
#include "bsf.h"
26 27 28 29 30 31 32 33 34 35 36 37 38
#include "put_bits.h"
#include "get_bits.h"
#include "mpeg4audio.h"
#include "internal.h"

typedef struct AACBSFContext {
    int first_frame_done;
} AACBSFContext;

/**
 * This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4
 * ADTS header and removes the ADTS header.
 */
39
static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *out)
40
{
41 42
    AACBSFContext *ctx = bsfc->priv_data;

43 44 45
    GetBitContext gb;
    PutBitContext pb;
    AACADTSHeaderInfo hdr;
46 47
    AVPacket *in;
    int ret;
48

49 50 51
    ret = ff_bsf_get_packet(bsfc, &in);
    if (ret < 0)
        return ret;
52

53
    if (in->size < AV_AAC_ADTS_HEADER_SIZE)
54
        goto packet_too_small;
55

56
    init_get_bits(&gb, in->data, AV_AAC_ADTS_HEADER_SIZE * 8);
57

58 59
    if (bsfc->par_in->extradata && show_bits(&gb, 12) != 0xfff)
        goto finish;
60

61
    if (ff_adts_header_parse(&gb, &hdr) < 0) {
62 63 64
        av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
        ret = AVERROR_INVALIDDATA;
        goto fail;
65 66 67
    }

    if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
68
        avpriv_report_missing_feature(bsfc,
69
                                      "Multiple RDBs per frame with CRC");
70 71
        ret = AVERROR_PATCHWELCOME;
        goto fail;
72 73
    }

74
    in->size -= AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
75 76
    if (in->size <= 0)
        goto packet_too_small;
77
    in->data += AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
78 79 80 81

    if (!ctx->first_frame_done) {
        int            pce_size = 0;
        uint8_t        pce_data[MAX_PCE_SIZE];
82 83
        uint8_t       *extradata;

84
        if (!hdr.chan_config) {
85
            init_get_bits(&gb, in->data, in->size * 8);
86
            if (get_bits(&gb, 3) != 5) {
87
                avpriv_report_missing_feature(bsfc,
88 89 90
                                              "PCE-based channel configuration "
                                              "without PCE as first syntax "
                                              "element");
91 92
                ret = AVERROR_PATCHWELCOME;
                goto fail;
93 94
            }
            init_put_bits(&pb, pce_data, MAX_PCE_SIZE);
95
            pce_size = ff_copy_pce_data(&pb, &gb) / 8;
96
            flush_put_bits(&pb);
97 98
            in->size -= get_bits_count(&gb)/8;
            in->data += get_bits_count(&gb)/8;
99 100
        }

101 102 103 104 105 106 107 108
        extradata = av_packet_new_side_data(in, AV_PKT_DATA_NEW_EXTRADATA,
                                            2 + pce_size);
        if (!extradata) {
            ret = AVERROR(ENOMEM);
            goto fail;
        }

        init_put_bits(&pb, extradata, 2 + pce_size);
109 110 111 112 113 114 115 116
        put_bits(&pb, 5, hdr.object_type);
        put_bits(&pb, 4, hdr.sampling_index);
        put_bits(&pb, 4, hdr.chan_config);
        put_bits(&pb, 1, 0); //frame length - 1024 samples
        put_bits(&pb, 1, 0); //does not depend on core coder
        put_bits(&pb, 1, 0); //is not extension
        flush_put_bits(&pb);
        if (pce_size) {
117
            memcpy(extradata + 2, pce_data, pce_size);
118 119 120 121 122
        }

        ctx->first_frame_done = 1;
    }

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
finish:
    av_packet_move_ref(out, in);
    av_packet_free(&in);

    return 0;

packet_too_small:
    av_log(bsfc, AV_LOG_ERROR, "Input packet too small\n");
    ret = AVERROR_INVALIDDATA;
fail:
    av_packet_free(&in);
    return ret;
}

static int aac_adtstoasc_init(AVBSFContext *ctx)
{
139 140 141 142 143 144 145 146 147 148
    /* Validate the extradata if the stream is already MPEG-4 AudioSpecificConfig */
    if (ctx->par_in->extradata) {
        MPEG4AudioConfig mp4ac;
        int ret = avpriv_mpeg4audio_get_config(&mp4ac, ctx->par_in->extradata,
                                               ctx->par_in->extradata_size * 8, 1);
        if (ret < 0) {
            av_log(ctx, AV_LOG_ERROR, "Error parsing AudioSpecificConfig extradata!\n");
            return ret;
        }
    }
149 150 151 152

    return 0;
}

153 154 155 156 157 158 159 160 161 162
static const enum AVCodecID codec_ids[] = {
    AV_CODEC_ID_AAC, AV_CODEC_ID_NONE,
};

const AVBitStreamFilter ff_aac_adtstoasc_bsf = {
    .name           = "aac_adtstoasc",
    .priv_data_size = sizeof(AACBSFContext),
    .init           = aac_adtstoasc_init,
    .filter         = aac_adtstoasc_filter,
    .codec_ids      = codec_ids,
163
};