LCOV - code coverage report
Current view: top level - libavformat - latmenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 71 102 69.6 %
Date: 2017-12-14 01:15:32 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * LATM/LOAS muxer
       3             :  * Copyright (c) 2011 Kieran Kunhya <kieran@kunhya.com>
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "libavcodec/get_bits.h"
      23             : #include "libavcodec/put_bits.h"
      24             : #include "libavcodec/avcodec.h"
      25             : #include "libavcodec/mpeg4audio.h"
      26             : #include "libavutil/opt.h"
      27             : #include "avformat.h"
      28             : #include "internal.h"
      29             : #include "rawenc.h"
      30             : 
      31             : #define MAX_EXTRADATA_SIZE 1024
      32             : 
      33             : typedef struct LATMContext {
      34             :     AVClass *av_class;
      35             :     int off;
      36             :     int channel_conf;
      37             :     int object_type;
      38             :     int counter;
      39             :     int mod;
      40             :     uint8_t buffer[0x1fff + MAX_EXTRADATA_SIZE + 1024];
      41             : } LATMContext;
      42             : 
      43             : static const AVOption options[] = {
      44             :     {"smc-interval", "StreamMuxConfig interval.",
      45             :      offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.i64 = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
      46             :     {NULL},
      47             : };
      48             : 
      49             : static const AVClass latm_muxer_class = {
      50             :     .class_name = "LATM/LOAS muxer",
      51             :     .item_name  = av_default_item_name,
      52             :     .option     = options,
      53             :     .version    = LIBAVUTIL_VERSION_INT,
      54             : };
      55             : 
      56           1 : static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
      57             : {
      58             :     MPEG4AudioConfig m4ac;
      59             : 
      60           1 :     if (size > MAX_EXTRADATA_SIZE) {
      61           0 :         av_log(ctx, AV_LOG_ERROR, "Extradata is larger than currently supported.\n");
      62           0 :         return AVERROR_INVALIDDATA;
      63             :     }
      64           1 :     ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
      65           1 :     if (ctx->off < 0)
      66           0 :         return ctx->off;
      67             : 
      68           1 :     if (ctx->object_type == AOT_ALS && (ctx->off & 7)) {
      69             :         // as long as avpriv_mpeg4audio_get_config works correctly this is impossible
      70           0 :         av_log(ctx, AV_LOG_ERROR, "BUG: ALS offset is not byte-aligned\n");
      71           0 :         return AVERROR_INVALIDDATA;
      72             :     }
      73             :     /* FIXME: are any formats not allowed in LATM? */
      74             : 
      75           1 :     if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
      76           0 :         av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
      77           0 :         return AVERROR_INVALIDDATA;
      78             :     }
      79           1 :     ctx->channel_conf = m4ac.chan_config;
      80           1 :     ctx->object_type  = m4ac.object_type;
      81             : 
      82           1 :     return 0;
      83             : }
      84             : 
      85           1 : static int latm_write_header(AVFormatContext *s)
      86             : {
      87           1 :     LATMContext *ctx = s->priv_data;
      88           1 :     AVCodecParameters *par = s->streams[0]->codecpar;
      89             : 
      90           1 :     if (par->codec_id == AV_CODEC_ID_AAC_LATM)
      91           0 :         return 0;
      92             : 
      93           2 :     if (par->extradata_size > 0 &&
      94           1 :         latm_decode_extradata(ctx, par->extradata, par->extradata_size) < 0)
      95           0 :         return AVERROR_INVALIDDATA;
      96             : 
      97           1 :     return 0;
      98             : }
      99             : 
     100         355 : static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
     101             : {
     102         355 :     LATMContext *ctx = s->priv_data;
     103         355 :     AVCodecParameters *par = s->streams[0]->codecpar;
     104             :     int header_size;
     105             : 
     106             :     /* AudioMuxElement */
     107         355 :     put_bits(bs, 1, !!ctx->counter);
     108             : 
     109         355 :     if (!ctx->counter) {
     110             :         /* StreamMuxConfig */
     111          18 :         put_bits(bs, 1, 0); /* audioMuxVersion */
     112          18 :         put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
     113          18 :         put_bits(bs, 6, 0); /* numSubFrames */
     114          18 :         put_bits(bs, 4, 0); /* numProgram */
     115          18 :         put_bits(bs, 3, 0); /* numLayer */
     116             : 
     117             :         /* AudioSpecificConfig */
     118          18 :         if (ctx->object_type == AOT_ALS) {
     119           0 :             header_size = par->extradata_size-(ctx->off >> 3);
     120           0 :             avpriv_copy_bits(bs, &par->extradata[ctx->off >> 3], header_size);
     121             :         } else {
     122             :             // + 3 assumes not scalable and dependsOnCoreCoder == 0,
     123             :             // see decode_ga_specific_config in libavcodec/aacdec.c
     124          18 :             avpriv_copy_bits(bs, par->extradata, ctx->off + 3);
     125             : 
     126          18 :             if (!ctx->channel_conf) {
     127             :                 GetBitContext gb;
     128          18 :                 int ret = init_get_bits8(&gb, par->extradata, par->extradata_size);
     129          18 :                 av_assert0(ret >= 0); // extradata size has been checked already, so this should not fail
     130          18 :                 skip_bits_long(&gb, ctx->off + 3);
     131          18 :                 ff_copy_pce_data(bs, &gb);
     132             :             }
     133             :         }
     134             : 
     135          18 :         put_bits(bs, 3, 0); /* frameLengthType */
     136          18 :         put_bits(bs, 8, 0xff); /* latmBufferFullness */
     137             : 
     138          18 :         put_bits(bs, 1, 0); /* otherDataPresent */
     139          18 :         put_bits(bs, 1, 0); /* crcCheckPresent */
     140             :     }
     141             : 
     142         355 :     ctx->counter++;
     143         355 :     ctx->counter %= ctx->mod;
     144         355 : }
     145             : 
     146         355 : static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
     147             : {
     148         355 :     LATMContext *ctx = s->priv_data;
     149         355 :     AVCodecParameters *par = s->streams[0]->codecpar;
     150         355 :     AVIOContext *pb = s->pb;
     151             :     PutBitContext bs;
     152             :     int i, len;
     153         355 :     uint8_t loas_header[] = "\x56\xe0\x00";
     154             : 
     155         355 :     if (par->codec_id == AV_CODEC_ID_AAC_LATM)
     156           0 :         return ff_raw_write_packet(s, pkt);
     157             : 
     158         355 :     if (!par->extradata) {
     159           0 :         if(pkt->size > 2 && pkt->data[0] == 0x56 && (pkt->data[1] >> 4) == 0xe &&
     160           0 :             (AV_RB16(pkt->data + 1) & 0x1FFF) + 3 == pkt->size)
     161           0 :             return ff_raw_write_packet(s, pkt);
     162             :         else {
     163             :             uint8_t *side_data;
     164           0 :             int side_data_size = 0, ret;
     165             : 
     166           0 :             side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
     167             :                                                 &side_data_size);
     168           0 :             if (side_data_size) {
     169           0 :                 if (latm_decode_extradata(ctx, side_data, side_data_size) < 0)
     170           0 :                     return AVERROR_INVALIDDATA;
     171           0 :                 ret = ff_alloc_extradata(par, side_data_size);
     172           0 :                 if (ret < 0)
     173           0 :                     return ret;
     174           0 :                 memcpy(par->extradata, side_data, side_data_size);
     175             :             }
     176             :         }
     177             :     }
     178             : 
     179         355 :     if (pkt->size > 0x1fff)
     180           0 :         goto too_large;
     181             : 
     182         355 :     init_put_bits(&bs, ctx->buffer, pkt->size+1024+MAX_EXTRADATA_SIZE);
     183             : 
     184         355 :     latm_write_frame_header(s, &bs);
     185             : 
     186             :     /* PayloadLengthInfo() */
     187         361 :     for (i = 0; i <= pkt->size-255; i+=255)
     188           6 :         put_bits(&bs, 8, 255);
     189             : 
     190         355 :     put_bits(&bs, 8, pkt->size-i);
     191             : 
     192             :     /* The LATM payload is written unaligned */
     193             : 
     194             :     /* PayloadMux() */
     195         355 :     if (pkt->size && (pkt->data[0] & 0xe1) == 0x81) {
     196             :         // Convert byte-aligned DSE to non-aligned.
     197             :         // Due to the input format encoding we know that
     198             :         // it is naturally byte-aligned in the input stream,
     199             :         // so there are no padding bits to account for.
     200             :         // To avoid having to add padding bits and rearrange
     201             :         // the whole stream we just remove the byte-align flag.
     202             :         // This allows us to remux our FATE AAC samples into latm
     203             :         // files that are still playable with minimal effort.
     204         355 :         put_bits(&bs, 8, pkt->data[0] & 0xfe);
     205         355 :         avpriv_copy_bits(&bs, pkt->data + 1, 8*pkt->size - 8);
     206             :     } else
     207           0 :         avpriv_copy_bits(&bs, pkt->data, 8*pkt->size);
     208             : 
     209         355 :     avpriv_align_put_bits(&bs);
     210         355 :     flush_put_bits(&bs);
     211             : 
     212         355 :     len = put_bits_count(&bs) >> 3;
     213             : 
     214         355 :     if (len > 0x1fff)
     215           0 :         goto too_large;
     216             : 
     217         355 :     loas_header[1] |= (len >> 8) & 0x1f;
     218         355 :     loas_header[2] |= len & 0xff;
     219             : 
     220         355 :     avio_write(pb, loas_header, 3);
     221         355 :     avio_write(pb, ctx->buffer, len);
     222             : 
     223         355 :     return 0;
     224             : 
     225           0 : too_large:
     226           0 :     av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
     227           0 :     return AVERROR_INVALIDDATA;
     228             : }
     229             : 
     230           1 : static int latm_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt)
     231             : {
     232           1 :     int ret = 1;
     233           1 :     AVStream *st = s->streams[pkt->stream_index];
     234             : 
     235           1 :     if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
     236           1 :         if (pkt->size > 2 && (AV_RB16(pkt->data) & 0xfff0) == 0xfff0)
     237           0 :             ret = ff_stream_add_bitstream_filter(st, "aac_adtstoasc", NULL);
     238             :     }
     239             : 
     240           1 :     return ret;
     241             : }
     242             : 
     243             : AVOutputFormat ff_latm_muxer = {
     244             :     .name           = "latm",
     245             :     .long_name      = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
     246             :     .mime_type      = "audio/MP4A-LATM",
     247             :     .extensions     = "latm,loas",
     248             :     .priv_data_size = sizeof(LATMContext),
     249             :     .audio_codec    = AV_CODEC_ID_AAC,
     250             :     .video_codec    = AV_CODEC_ID_NONE,
     251             :     .write_header   = latm_write_header,
     252             :     .write_packet   = latm_write_packet,
     253             :     .priv_class     = &latm_muxer_class,
     254             :     .check_bitstream= latm_check_bitstream,
     255             :     .flags          = AVFMT_NOTIMESTAMPS,
     256             : };

Generated by: LCOV version 1.13