Libav 0.7.1
|
00001 /* 00002 * ADTS muxer. 00003 * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com> 00004 * Mans Rullgard <mans@mansr.com> 00005 * 00006 * This file is part of Libav. 00007 * 00008 * Libav is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * Libav is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with Libav; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #include "libavcodec/get_bits.h" 00024 #include "libavcodec/put_bits.h" 00025 #include "libavcodec/avcodec.h" 00026 #include "libavcodec/mpeg4audio.h" 00027 #include "avformat.h" 00028 #include "adts.h" 00029 00030 int ff_adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size) 00031 { 00032 GetBitContext gb; 00033 PutBitContext pb; 00034 MPEG4AudioConfig m4ac; 00035 int off; 00036 00037 init_get_bits(&gb, buf, size * 8); 00038 off = ff_mpeg4audio_get_config(&m4ac, buf, size); 00039 if (off < 0) 00040 return off; 00041 skip_bits_long(&gb, off); 00042 adts->objecttype = m4ac.object_type - 1; 00043 adts->sample_rate_index = m4ac.sampling_index; 00044 adts->channel_conf = m4ac.chan_config; 00045 00046 if (adts->objecttype > 3U) { 00047 av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1); 00048 return -1; 00049 } 00050 if (adts->sample_rate_index == 15) { 00051 av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n"); 00052 return -1; 00053 } 00054 if (get_bits(&gb, 1)) { 00055 av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n"); 00056 return -1; 00057 } 00058 if (get_bits(&gb, 1)) { 00059 av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n"); 00060 return -1; 00061 } 00062 if (get_bits(&gb, 1)) { 00063 av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n"); 00064 return -1; 00065 } 00066 if (!adts->channel_conf) { 00067 init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE); 00068 00069 put_bits(&pb, 3, 5); //ID_PCE 00070 adts->pce_size = (ff_copy_pce_data(&pb, &gb) + 3) / 8; 00071 flush_put_bits(&pb); 00072 } 00073 00074 adts->write_adts = 1; 00075 00076 return 0; 00077 } 00078 00079 static int adts_write_header(AVFormatContext *s) 00080 { 00081 ADTSContext *adts = s->priv_data; 00082 AVCodecContext *avc = s->streams[0]->codec; 00083 00084 if (avc->extradata_size > 0 && 00085 ff_adts_decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0) 00086 return -1; 00087 00088 return 0; 00089 } 00090 00091 int ff_adts_write_frame_header(ADTSContext *ctx, 00092 uint8_t *buf, int size, int pce_size) 00093 { 00094 PutBitContext pb; 00095 00096 init_put_bits(&pb, buf, ADTS_HEADER_SIZE); 00097 00098 /* adts_fixed_header */ 00099 put_bits(&pb, 12, 0xfff); /* syncword */ 00100 put_bits(&pb, 1, 0); /* ID */ 00101 put_bits(&pb, 2, 0); /* layer */ 00102 put_bits(&pb, 1, 1); /* protection_absent */ 00103 put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */ 00104 put_bits(&pb, 4, ctx->sample_rate_index); 00105 put_bits(&pb, 1, 0); /* private_bit */ 00106 put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */ 00107 put_bits(&pb, 1, 0); /* original_copy */ 00108 put_bits(&pb, 1, 0); /* home */ 00109 00110 /* adts_variable_header */ 00111 put_bits(&pb, 1, 0); /* copyright_identification_bit */ 00112 put_bits(&pb, 1, 0); /* copyright_identification_start */ 00113 put_bits(&pb, 13, ADTS_HEADER_SIZE + size + pce_size); /* aac_frame_length */ 00114 put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */ 00115 put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */ 00116 00117 flush_put_bits(&pb); 00118 00119 return 0; 00120 } 00121 00122 static int adts_write_packet(AVFormatContext *s, AVPacket *pkt) 00123 { 00124 ADTSContext *adts = s->priv_data; 00125 AVIOContext *pb = s->pb; 00126 uint8_t buf[ADTS_HEADER_SIZE]; 00127 00128 if (!pkt->size) 00129 return 0; 00130 if (adts->write_adts) { 00131 ff_adts_write_frame_header(adts, buf, pkt->size, adts->pce_size); 00132 avio_write(pb, buf, ADTS_HEADER_SIZE); 00133 if (adts->pce_size) { 00134 avio_write(pb, adts->pce_data, adts->pce_size); 00135 adts->pce_size = 0; 00136 } 00137 } 00138 avio_write(pb, pkt->data, pkt->size); 00139 avio_flush(pb); 00140 00141 return 0; 00142 } 00143 00144 AVOutputFormat ff_adts_muxer = { 00145 "adts", 00146 NULL_IF_CONFIG_SMALL("ADTS AAC"), 00147 "audio/aac", 00148 "aac,adts", 00149 sizeof(ADTSContext), 00150 CODEC_ID_AAC, 00151 CODEC_ID_NONE, 00152 adts_write_header, 00153 adts_write_packet, 00154 };