1/* 2 * ADTS muxer. 3 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com> 4 * Mans Rullgard <mans@mansr.com> 5 * 6 * This file is part of Libav. 7 * 8 * Libav is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2.1 of the License, or (at your option) any later version. 12 * 13 * Libav is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with Libav; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 */ 22 23#include "libavcodec/get_bits.h" 24#include "libavcodec/put_bits.h" 25#include "libavcodec/avcodec.h" 26#include "libavcodec/mpeg4audio.h" 27#include "avformat.h" 28#include "adts.h" 29 30#define ADTS_MAX_FRAME_BYTES ((1 << 13) - 1) 31 32int ff_adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size) 33{ 34 GetBitContext gb; 35 PutBitContext pb; 36 MPEG4AudioConfig m4ac; 37 int off; 38 39 init_get_bits(&gb, buf, size * 8); 40 off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1); 41 if (off < 0) 42 return off; 43 skip_bits_long(&gb, off); 44 adts->objecttype = m4ac.object_type - 1; 45 adts->sample_rate_index = m4ac.sampling_index; 46 adts->channel_conf = m4ac.chan_config; 47 48 if (adts->objecttype > 3U) { 49 av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1); 50 return -1; 51 } 52 if (adts->sample_rate_index == 15) { 53 av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n"); 54 return -1; 55 } 56 if (get_bits(&gb, 1)) { 57 av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n"); 58 return -1; 59 } 60 if (get_bits(&gb, 1)) { 61 av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n"); 62 return -1; 63 } 64 if (get_bits(&gb, 1)) { 65 av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n"); 66 return -1; 67 } 68 if (!adts->channel_conf) { 69 init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE); 70 71 put_bits(&pb, 3, 5); //ID_PCE 72 adts->pce_size = (avpriv_copy_pce_data(&pb, &gb) + 3) / 8; 73 flush_put_bits(&pb); 74 } 75 76 adts->write_adts = 1; 77 78 return 0; 79} 80 81static int adts_write_header(AVFormatContext *s) 82{ 83 ADTSContext *adts = s->priv_data; 84 AVCodecContext *avc = s->streams[0]->codec; 85 86 if (avc->extradata_size > 0 && 87 ff_adts_decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0) 88 return -1; 89 90 return 0; 91} 92 93int ff_adts_write_frame_header(ADTSContext *ctx, 94 uint8_t *buf, int size, int pce_size) 95{ 96 PutBitContext pb; 97 98 unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size; 99 if (full_frame_size > ADTS_MAX_FRAME_BYTES) { 100 av_log(NULL, AV_LOG_ERROR, "ADTS frame size too large: %u (max %d)\n", 101 full_frame_size, ADTS_MAX_FRAME_BYTES); 102 return AVERROR_INVALIDDATA; 103 } 104 105 init_put_bits(&pb, buf, ADTS_HEADER_SIZE); 106 107 /* adts_fixed_header */ 108 put_bits(&pb, 12, 0xfff); /* syncword */ 109 put_bits(&pb, 1, 0); /* ID */ 110 put_bits(&pb, 2, 0); /* layer */ 111 put_bits(&pb, 1, 1); /* protection_absent */ 112 put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */ 113 put_bits(&pb, 4, ctx->sample_rate_index); 114 put_bits(&pb, 1, 0); /* private_bit */ 115 put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */ 116 put_bits(&pb, 1, 0); /* original_copy */ 117 put_bits(&pb, 1, 0); /* home */ 118 119 /* adts_variable_header */ 120 put_bits(&pb, 1, 0); /* copyright_identification_bit */ 121 put_bits(&pb, 1, 0); /* copyright_identification_start */ 122 put_bits(&pb, 13, full_frame_size); /* aac_frame_length */ 123 put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */ 124 put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */ 125 126 flush_put_bits(&pb); 127 128 return 0; 129} 130 131static int adts_write_packet(AVFormatContext *s, AVPacket *pkt) 132{ 133 ADTSContext *adts = s->priv_data; 134 AVIOContext *pb = s->pb; 135 uint8_t buf[ADTS_HEADER_SIZE]; 136 137 if (!pkt->size) 138 return 0; 139 if (adts->write_adts) { 140 int err = ff_adts_write_frame_header(adts, buf, pkt->size, 141 adts->pce_size); 142 if (err < 0) 143 return err; 144 avio_write(pb, buf, ADTS_HEADER_SIZE); 145 if (adts->pce_size) { 146 avio_write(pb, adts->pce_data, adts->pce_size); 147 adts->pce_size = 0; 148 } 149 } 150 avio_write(pb, pkt->data, pkt->size); 151 avio_flush(pb); 152 153 return 0; 154} 155 156AVOutputFormat ff_adts_muxer = { 157 .name = "adts", 158 .long_name = NULL_IF_CONFIG_SMALL("ADTS AAC"), 159 .mime_type = "audio/aac", 160 .extensions = "aac,adts", 161 .priv_data_size = sizeof(ADTSContext), 162 .audio_codec = CODEC_ID_AAC, 163 .video_codec = CODEC_ID_NONE, 164 .write_header = adts_write_header, 165 .write_packet = adts_write_packet, 166}; 167