• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/adtsenc.c

Go to the documentation of this file.
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/bitstream.h"
00024 #include "libavcodec/internal.h"
00025 #include "avformat.h"
00026 
00027 #define ADTS_HEADER_SIZE 7
00028 
00029 typedef struct {
00030     int write_adts;
00031     int objecttype;
00032     int sample_rate_index;
00033     int channel_conf;
00034 } ADTSContext;
00035 
00036 static int decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size)
00037 {
00038     GetBitContext gb;
00039 
00040     init_get_bits(&gb, buf, size * 8);
00041     adts->objecttype = get_bits(&gb, 5) - 1;
00042     adts->sample_rate_index = get_bits(&gb, 4);
00043     adts->channel_conf = get_bits(&gb, 4);
00044 
00045     if (adts->objecttype > 3) {
00046         av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
00047         return -1;
00048     }
00049     if (adts->sample_rate_index == 15) {
00050         av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
00051         return -1;
00052     }
00053     if (adts->channel_conf == 0) {
00054         ff_log_missing_feature(s, "PCE based channel configuration", 0);
00055         return -1;
00056     }
00057 
00058     adts->write_adts = 1;
00059 
00060     return 0;
00061 }
00062 
00063 static int adts_write_header(AVFormatContext *s)
00064 {
00065     ADTSContext *adts = s->priv_data;
00066     AVCodecContext *avc = s->streams[0]->codec;
00067 
00068     if(avc->extradata_size > 0 &&
00069             decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0)
00070         return -1;
00071 
00072     return 0;
00073 }
00074 
00075 static int adts_write_frame_header(AVFormatContext *s, int size)
00076 {
00077     ADTSContext *ctx = s->priv_data;
00078     PutBitContext pb;
00079     uint8_t buf[ADTS_HEADER_SIZE];
00080 
00081     init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
00082 
00083     /* adts_fixed_header */
00084     put_bits(&pb, 12, 0xfff);   /* syncword */
00085     put_bits(&pb, 1, 0);        /* ID */
00086     put_bits(&pb, 2, 0);        /* layer */
00087     put_bits(&pb, 1, 1);        /* protection_absent */
00088     put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
00089     put_bits(&pb, 4, ctx->sample_rate_index);
00090     put_bits(&pb, 1, 0);        /* private_bit */
00091     put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
00092     put_bits(&pb, 1, 0);        /* original_copy */
00093     put_bits(&pb, 1, 0);        /* home */
00094 
00095     /* adts_variable_header */
00096     put_bits(&pb, 1, 0);        /* copyright_identification_bit */
00097     put_bits(&pb, 1, 0);        /* copyright_identification_start */
00098     put_bits(&pb, 13, ADTS_HEADER_SIZE + size); /* aac_frame_length */
00099     put_bits(&pb, 11, 0x7ff);   /* adts_buffer_fullness */
00100     put_bits(&pb, 2, 0);        /* number_of_raw_data_blocks_in_frame */
00101 
00102     flush_put_bits(&pb);
00103     put_buffer(s->pb, buf, ADTS_HEADER_SIZE);
00104 
00105     return 0;
00106 }
00107 
00108 static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
00109 {
00110     ADTSContext *adts = s->priv_data;
00111     ByteIOContext *pb = s->pb;
00112 
00113     if (!pkt->size)
00114         return 0;
00115     if(adts->write_adts)
00116         adts_write_frame_header(s, pkt->size);
00117     put_buffer(pb, pkt->data, pkt->size);
00118     put_flush_packet(pb);
00119 
00120     return 0;
00121 }
00122 
00123 AVOutputFormat adts_muxer = {
00124     "adts",
00125     NULL_IF_CONFIG_SMALL("ADTS AAC"),
00126     "audio/aac",
00127     "aac",
00128     sizeof(ADTSContext),
00129     CODEC_ID_AAC,
00130     CODEC_ID_NONE,
00131     adts_write_header,
00132     adts_write_packet,
00133 };

Generated on Sat Feb 16 2013 09:23:14 for ffmpeg by  doxygen 1.7.1