LCOV - code coverage report
Current view: top level - src/libavformat - adtsenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 58 82 70.7 %
Date: 2017-01-23 11:54:22 Functions: 5 5 100.0 %

          Line data    Source code
       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 FFmpeg.
       7             :  *
       8             :  * FFmpeg 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             :  * FFmpeg 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 FFmpeg; 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 "libavutil/opt.h"
      28             : #include "avformat.h"
      29             : #include "apetag.h"
      30             : #include "id3v2.h"
      31             : 
      32             : #define ADTS_HEADER_SIZE 7
      33             : 
      34             : typedef struct ADTSContext {
      35             :     AVClass *class;
      36             :     int write_adts;
      37             :     int objecttype;
      38             :     int sample_rate_index;
      39             :     int channel_conf;
      40             :     int pce_size;
      41             :     int apetag;
      42             :     int id3v2tag;
      43             :     uint8_t pce_data[MAX_PCE_SIZE];
      44             : } ADTSContext;
      45             : 
      46             : #define ADTS_MAX_FRAME_BYTES ((1 << 13) - 1)
      47             : 
      48          10 : static int adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, const uint8_t *buf, int size)
      49             : {
      50             :     GetBitContext gb;
      51             :     PutBitContext pb;
      52             :     MPEG4AudioConfig m4ac;
      53             :     int off;
      54             : 
      55          10 :     init_get_bits(&gb, buf, size * 8);
      56          10 :     off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
      57          10 :     if (off < 0)
      58           0 :         return off;
      59          10 :     skip_bits_long(&gb, off);
      60          10 :     adts->objecttype        = m4ac.object_type - 1;
      61          10 :     adts->sample_rate_index = m4ac.sampling_index;
      62          10 :     adts->channel_conf      = m4ac.chan_config;
      63             : 
      64          10 :     if (adts->objecttype > 3U) {
      65           0 :         av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
      66           0 :         return AVERROR_INVALIDDATA;
      67             :     }
      68          10 :     if (adts->sample_rate_index == 15) {
      69           0 :         av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
      70           0 :         return AVERROR_INVALIDDATA;
      71             :     }
      72          10 :     if (get_bits(&gb, 1)) {
      73           0 :         av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
      74           0 :         return AVERROR_INVALIDDATA;
      75             :     }
      76          10 :     if (get_bits(&gb, 1)) {
      77           0 :         av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
      78           0 :         return AVERROR_INVALIDDATA;
      79             :     }
      80          10 :     if (get_bits(&gb, 1)) {
      81           0 :         av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
      82           0 :         return AVERROR_INVALIDDATA;
      83             :     }
      84          10 :     if (!adts->channel_conf) {
      85           0 :         init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
      86             : 
      87           0 :         put_bits(&pb, 3, 5); //ID_PCE
      88           0 :         adts->pce_size = (avpriv_copy_pce_data(&pb, &gb) + 3) / 8;
      89           0 :         flush_put_bits(&pb);
      90             :     }
      91             : 
      92          10 :     adts->write_adts = 1;
      93             : 
      94          10 :     return 0;
      95             : }
      96             : 
      97          10 : static int adts_write_header(AVFormatContext *s)
      98             : {
      99          10 :     ADTSContext *adts = s->priv_data;
     100          10 :     AVCodecParameters *par = s->streams[0]->codecpar;
     101             : 
     102          10 :     if (adts->id3v2tag)
     103           0 :         ff_id3v2_write_simple(s, 4, ID3v2_DEFAULT_MAGIC);
     104          10 :     if (par->extradata_size > 0)
     105          10 :         return adts_decode_extradata(s, adts, par->extradata,
     106             :                                      par->extradata_size);
     107             : 
     108           0 :     return 0;
     109             : }
     110             : 
     111        3596 : static int adts_write_frame_header(ADTSContext *ctx,
     112             :                                    uint8_t *buf, int size, int pce_size)
     113             : {
     114             :     PutBitContext pb;
     115             : 
     116        3596 :     unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size;
     117        3596 :     if (full_frame_size > ADTS_MAX_FRAME_BYTES) {
     118           0 :         av_log(NULL, AV_LOG_ERROR, "ADTS frame size too large: %u (max %d)\n",
     119             :                full_frame_size, ADTS_MAX_FRAME_BYTES);
     120           0 :         return AVERROR_INVALIDDATA;
     121             :     }
     122             : 
     123        3596 :     init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
     124             : 
     125             :     /* adts_fixed_header */
     126        3596 :     put_bits(&pb, 12, 0xfff);   /* syncword */
     127        3596 :     put_bits(&pb, 1, 0);        /* ID */
     128        3596 :     put_bits(&pb, 2, 0);        /* layer */
     129        3596 :     put_bits(&pb, 1, 1);        /* protection_absent */
     130        3596 :     put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
     131        3596 :     put_bits(&pb, 4, ctx->sample_rate_index);
     132        3596 :     put_bits(&pb, 1, 0);        /* private_bit */
     133        3596 :     put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
     134        3596 :     put_bits(&pb, 1, 0);        /* original_copy */
     135        3596 :     put_bits(&pb, 1, 0);        /* home */
     136             : 
     137             :     /* adts_variable_header */
     138        3596 :     put_bits(&pb, 1, 0);        /* copyright_identification_bit */
     139        3596 :     put_bits(&pb, 1, 0);        /* copyright_identification_start */
     140        3596 :     put_bits(&pb, 13, full_frame_size); /* aac_frame_length */
     141        3596 :     put_bits(&pb, 11, 0x7ff);   /* adts_buffer_fullness */
     142        3596 :     put_bits(&pb, 2, 0);        /* number_of_raw_data_blocks_in_frame */
     143             : 
     144        3596 :     flush_put_bits(&pb);
     145             : 
     146        3596 :     return 0;
     147             : }
     148             : 
     149        3596 : static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
     150             : {
     151        3596 :     ADTSContext *adts = s->priv_data;
     152        3596 :     AVIOContext *pb = s->pb;
     153             :     uint8_t buf[ADTS_HEADER_SIZE];
     154             : 
     155        3596 :     if (!pkt->size)
     156           0 :         return 0;
     157        3596 :     if (adts->write_adts) {
     158        3596 :         int err = adts_write_frame_header(adts, buf, pkt->size,
     159             :                                              adts->pce_size);
     160        3596 :         if (err < 0)
     161           0 :             return err;
     162        3596 :         avio_write(pb, buf, ADTS_HEADER_SIZE);
     163        3596 :         if (adts->pce_size) {
     164           0 :             avio_write(pb, adts->pce_data, adts->pce_size);
     165           0 :             adts->pce_size = 0;
     166             :         }
     167             :     }
     168        3596 :     avio_write(pb, pkt->data, pkt->size);
     169             : 
     170        3596 :     return 0;
     171             : }
     172             : 
     173          10 : static int adts_write_trailer(AVFormatContext *s)
     174             : {
     175          10 :     ADTSContext *adts = s->priv_data;
     176             : 
     177          10 :     if (adts->apetag)
     178           0 :         ff_ape_write_tag(s);
     179             : 
     180          10 :     return 0;
     181             : }
     182             : 
     183             : #define ENC AV_OPT_FLAG_ENCODING_PARAM
     184             : #define OFFSET(obj) offsetof(ADTSContext, obj)
     185             : static const AVOption options[] = {
     186             :     { "write_id3v2",  "Enable ID3v2 tag writing", OFFSET(id3v2tag), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
     187             :     { "write_apetag", "Enable APE tag writing",   OFFSET(apetag),   AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, ENC},
     188             :     { NULL },
     189             : };
     190             : 
     191             : static const AVClass adts_muxer_class = {
     192             :     .class_name     = "ADTS muxer",
     193             :     .item_name      = av_default_item_name,
     194             :     .option         = options,
     195             :     .version        = LIBAVUTIL_VERSION_INT,
     196             : };
     197             : 
     198             : AVOutputFormat ff_adts_muxer = {
     199             :     .name              = "adts",
     200             :     .long_name         = NULL_IF_CONFIG_SMALL("ADTS AAC (Advanced Audio Coding)"),
     201             :     .mime_type         = "audio/aac",
     202             :     .extensions        = "aac,adts",
     203             :     .priv_data_size    = sizeof(ADTSContext),
     204             :     .audio_codec       = AV_CODEC_ID_AAC,
     205             :     .video_codec       = AV_CODEC_ID_NONE,
     206             :     .write_header      = adts_write_header,
     207             :     .write_packet      = adts_write_packet,
     208             :     .write_trailer     = adts_write_trailer,
     209             :     .priv_class        = &adts_muxer_class,
     210             :     .flags             = AVFMT_NOTIMESTAMPS,
     211             : };

Generated by: LCOV version 1.12