Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  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 "rawenc.h"
  29.  
  30. #define MAX_EXTRADATA_SIZE 1024
  31.  
  32. typedef struct LATMContext {
  33.     AVClass *av_class;
  34.     int off;
  35.     int channel_conf;
  36.     int object_type;
  37.     int counter;
  38.     int mod;
  39.     uint8_t buffer[0x1fff + MAX_EXTRADATA_SIZE + 1024];
  40. } LATMContext;
  41.  
  42. static const AVOption options[] = {
  43.     {"smc-interval", "StreamMuxConfig interval.",
  44.      offsetof(LATMContext, mod), AV_OPT_TYPE_INT, {.i64 = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
  45.     {NULL},
  46. };
  47.  
  48. static const AVClass latm_muxer_class = {
  49.     .class_name = "LATM/LOAS muxer",
  50.     .item_name  = av_default_item_name,
  51.     .option     = options,
  52.     .version    = LIBAVUTIL_VERSION_INT,
  53. };
  54.  
  55. static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
  56. {
  57.     MPEG4AudioConfig m4ac;
  58.  
  59.     if (size > MAX_EXTRADATA_SIZE) {
  60.         av_log(ctx, AV_LOG_ERROR, "Extradata is larger than currently supported.\n");
  61.         return AVERROR_INVALIDDATA;
  62.     }
  63.     ctx->off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
  64.     if (ctx->off < 0)
  65.         return ctx->off;
  66.  
  67.     if (ctx->object_type == AOT_ALS && (ctx->off & 7)) {
  68.         // as long as avpriv_mpeg4audio_get_config works correctly this is impossible
  69.         av_log(ctx, AV_LOG_ERROR, "BUG: ALS offset is not byte-aligned\n");
  70.         return AVERROR_INVALIDDATA;
  71.     }
  72.     /* FIXME: are any formats not allowed in LATM? */
  73.  
  74.     if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
  75.         av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
  76.         return AVERROR_INVALIDDATA;
  77.     }
  78.     ctx->channel_conf = m4ac.chan_config;
  79.     ctx->object_type  = m4ac.object_type;
  80.  
  81.     return 0;
  82. }
  83.  
  84. static int latm_write_header(AVFormatContext *s)
  85. {
  86.     LATMContext *ctx = s->priv_data;
  87.     AVCodecContext *avctx = s->streams[0]->codec;
  88.  
  89.     if (avctx->codec_id == AV_CODEC_ID_AAC_LATM)
  90.         return 0;
  91.  
  92.     if (avctx->extradata_size > 0 &&
  93.         latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
  94.         return AVERROR_INVALIDDATA;
  95.  
  96.     return 0;
  97. }
  98.  
  99. static void latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
  100. {
  101.     LATMContext *ctx = s->priv_data;
  102.     AVCodecContext *avctx = s->streams[0]->codec;
  103.     int header_size;
  104.  
  105.     /* AudioMuxElement */
  106.     put_bits(bs, 1, !!ctx->counter);
  107.  
  108.     if (!ctx->counter) {
  109.         /* StreamMuxConfig */
  110.         put_bits(bs, 1, 0); /* audioMuxVersion */
  111.         put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
  112.         put_bits(bs, 6, 0); /* numSubFrames */
  113.         put_bits(bs, 4, 0); /* numProgram */
  114.         put_bits(bs, 3, 0); /* numLayer */
  115.  
  116.         /* AudioSpecificConfig */
  117.         if (ctx->object_type == AOT_ALS) {
  118.             header_size = avctx->extradata_size-(ctx->off >> 3);
  119.             avpriv_copy_bits(bs, &avctx->extradata[ctx->off >> 3], header_size);
  120.         } else {
  121.             // + 3 assumes not scalable and dependsOnCoreCoder == 0,
  122.             // see decode_ga_specific_config in libavcodec/aacdec.c
  123.             avpriv_copy_bits(bs, avctx->extradata, ctx->off + 3);
  124.  
  125.             if (!ctx->channel_conf) {
  126.                 GetBitContext gb;
  127.                 int ret = init_get_bits8(&gb, avctx->extradata, avctx->extradata_size);
  128.                 av_assert0(ret >= 0); // extradata size has been checked already, so this should not fail
  129.                 skip_bits_long(&gb, ctx->off + 3);
  130.                 avpriv_copy_pce_data(bs, &gb);
  131.             }
  132.         }
  133.  
  134.         put_bits(bs, 3, 0); /* frameLengthType */
  135.         put_bits(bs, 8, 0xff); /* latmBufferFullness */
  136.  
  137.         put_bits(bs, 1, 0); /* otherDataPresent */
  138.         put_bits(bs, 1, 0); /* crcCheckPresent */
  139.     }
  140.  
  141.     ctx->counter++;
  142.     ctx->counter %= ctx->mod;
  143. }
  144.  
  145. static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
  146. {
  147.     LATMContext *ctx = s->priv_data;
  148.     AVIOContext *pb = s->pb;
  149.     PutBitContext bs;
  150.     int i, len;
  151.     uint8_t loas_header[] = "\x56\xe0\x00";
  152.  
  153.     if (s->streams[0]->codec->codec_id == AV_CODEC_ID_AAC_LATM)
  154.         return ff_raw_write_packet(s, pkt);
  155.  
  156.     if (pkt->size > 2 && pkt->data[0] == 0xff && (pkt->data[1] >> 4) == 0xf) {
  157.         av_log(s, AV_LOG_ERROR, "ADTS header detected - ADTS will not be incorrectly muxed into LATM\n");
  158.         return AVERROR_INVALIDDATA;
  159.     }
  160.  
  161.     if (!s->streams[0]->codec->extradata) {
  162.         if(pkt->size > 2 && pkt->data[0] == 0x56 && (pkt->data[1] >> 4) == 0xe &&
  163.             (AV_RB16(pkt->data + 1) & 0x1FFF) + 3 == pkt->size)
  164.             return ff_raw_write_packet(s, pkt);
  165.         else
  166.             return AVERROR_INVALIDDATA;
  167.     }
  168.  
  169.     if (pkt->size > 0x1fff)
  170.         goto too_large;
  171.  
  172.     init_put_bits(&bs, ctx->buffer, pkt->size+1024+MAX_EXTRADATA_SIZE);
  173.  
  174.     latm_write_frame_header(s, &bs);
  175.  
  176.     /* PayloadLengthInfo() */
  177.     for (i = 0; i <= pkt->size-255; i+=255)
  178.         put_bits(&bs, 8, 255);
  179.  
  180.     put_bits(&bs, 8, pkt->size-i);
  181.  
  182.     /* The LATM payload is written unaligned */
  183.  
  184.     /* PayloadMux() */
  185.     if (pkt->size && (pkt->data[0] & 0xe1) == 0x81) {
  186.         // Convert byte-aligned DSE to non-aligned.
  187.         // Due to the input format encoding we know that
  188.         // it is naturally byte-aligned in the input stream,
  189.         // so there are no padding bits to account for.
  190.         // To avoid having to add padding bits and rearrange
  191.         // the whole stream we just remove the byte-align flag.
  192.         // This allows us to remux our FATE AAC samples into latm
  193.         // files that are still playable with minimal effort.
  194.         put_bits(&bs, 8, pkt->data[0] & 0xfe);
  195.         avpriv_copy_bits(&bs, pkt->data + 1, 8*pkt->size - 8);
  196.     } else
  197.         avpriv_copy_bits(&bs, pkt->data, 8*pkt->size);
  198.  
  199.     avpriv_align_put_bits(&bs);
  200.     flush_put_bits(&bs);
  201.  
  202.     len = put_bits_count(&bs) >> 3;
  203.  
  204.     if (len > 0x1fff)
  205.         goto too_large;
  206.  
  207.     loas_header[1] |= (len >> 8) & 0x1f;
  208.     loas_header[2] |= len & 0xff;
  209.  
  210.     avio_write(pb, loas_header, 3);
  211.     avio_write(pb, ctx->buffer, len);
  212.  
  213.     return 0;
  214.  
  215. too_large:
  216.     av_log(s, AV_LOG_ERROR, "LATM packet size larger than maximum size 0x1fff\n");
  217.     return AVERROR_INVALIDDATA;
  218. }
  219.  
  220. AVOutputFormat ff_latm_muxer = {
  221.     .name           = "latm",
  222.     .long_name      = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
  223.     .mime_type      = "audio/MP4A-LATM",
  224.     .extensions     = "latm,loas",
  225.     .priv_data_size = sizeof(LATMContext),
  226.     .audio_codec    = AV_CODEC_ID_AAC,
  227.     .video_codec    = AV_CODEC_ID_NONE,
  228.     .write_header   = latm_write_header,
  229.     .write_packet   = latm_write_packet,
  230.     .priv_class     = &latm_muxer_class,
  231.     .flags          = AVFMT_NOTIMESTAMPS,
  232. };
  233.