Libav
|
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 FFmpeg. 00007 * 00008 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 "avformat.h" 00027 #include "adts.h" 00028 00029 int ff_adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size) 00030 { 00031 GetBitContext gb; 00032 PutBitContext pb; 00033 00034 init_get_bits(&gb, buf, size * 8); 00035 adts->objecttype = get_bits(&gb, 5) - 1; 00036 adts->sample_rate_index = get_bits(&gb, 4); 00037 adts->channel_conf = get_bits(&gb, 4); 00038 00039 if (adts->objecttype > 3U) { 00040 av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1); 00041 return -1; 00042 } 00043 if (adts->sample_rate_index == 15) { 00044 av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n"); 00045 return -1; 00046 } 00047 if (get_bits(&gb, 1)) { 00048 av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n"); 00049 return -1; 00050 } 00051 if (get_bits(&gb, 1)) { 00052 av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n"); 00053 return -1; 00054 } 00055 if (get_bits(&gb, 1)) { 00056 av_log_missing_feature(s, "Signaled SBR or PS", 0); 00057 return -1; 00058 } 00059 if (!adts->channel_conf) { 00060 init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE); 00061 00062 put_bits(&pb, 3, 5); //ID_PCE 00063 adts->pce_size = (ff_copy_pce_data(&pb, &gb) + 3) / 8; 00064 flush_put_bits(&pb); 00065 } 00066 00067 adts->write_adts = 1; 00068 00069 return 0; 00070 } 00071 00072 static int adts_write_header(AVFormatContext *s) 00073 { 00074 ADTSContext *adts = s->priv_data; 00075 AVCodecContext *avc = s->streams[0]->codec; 00076 00077 if(avc->extradata_size > 0 && 00078 ff_adts_decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0) 00079 return -1; 00080 00081 return 0; 00082 } 00083 00084 int ff_adts_write_frame_header(ADTSContext *ctx, 00085 uint8_t *buf, int size, int pce_size) 00086 { 00087 PutBitContext pb; 00088 00089 init_put_bits(&pb, buf, ADTS_HEADER_SIZE); 00090 00091 /* adts_fixed_header */ 00092 put_bits(&pb, 12, 0xfff); /* syncword */ 00093 put_bits(&pb, 1, 0); /* ID */ 00094 put_bits(&pb, 2, 0); /* layer */ 00095 put_bits(&pb, 1, 1); /* protection_absent */ 00096 put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */ 00097 put_bits(&pb, 4, ctx->sample_rate_index); 00098 put_bits(&pb, 1, 0); /* private_bit */ 00099 put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */ 00100 put_bits(&pb, 1, 0); /* original_copy */ 00101 put_bits(&pb, 1, 0); /* home */ 00102 00103 /* adts_variable_header */ 00104 put_bits(&pb, 1, 0); /* copyright_identification_bit */ 00105 put_bits(&pb, 1, 0); /* copyright_identification_start */ 00106 put_bits(&pb, 13, ADTS_HEADER_SIZE + size + pce_size); /* aac_frame_length */ 00107 put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */ 00108 put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */ 00109 00110 flush_put_bits(&pb); 00111 00112 return 0; 00113 } 00114 00115 static int adts_write_packet(AVFormatContext *s, AVPacket *pkt) 00116 { 00117 ADTSContext *adts = s->priv_data; 00118 ByteIOContext *pb = s->pb; 00119 uint8_t buf[ADTS_HEADER_SIZE]; 00120 00121 if (!pkt->size) 00122 return 0; 00123 if(adts->write_adts) { 00124 ff_adts_write_frame_header(adts, buf, pkt->size, adts->pce_size); 00125 put_buffer(pb, buf, ADTS_HEADER_SIZE); 00126 if(adts->pce_size) { 00127 put_buffer(pb, adts->pce_data, adts->pce_size); 00128 adts->pce_size = 0; 00129 } 00130 } 00131 put_buffer(pb, pkt->data, pkt->size); 00132 put_flush_packet(pb); 00133 00134 return 0; 00135 } 00136 00137 AVOutputFormat adts_muxer = { 00138 "adts", 00139 NULL_IF_CONFIG_SMALL("ADTS AAC"), 00140 "audio/aac", 00141 "aac,adts", 00142 sizeof(ADTSContext), 00143 CODEC_ID_AAC, 00144 CODEC_ID_NONE, 00145 adts_write_header, 00146 adts_write_packet, 00147 };