You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
5.5KB

  1. /*
  2. * LATM/LOAS muxer
  3. * Copyright (c) 2011 Kieran Kunhya <kieran@kunhya.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavcodec/get_bits.h"
  22. #include "libavcodec/put_bits.h"
  23. #include "libavcodec/avcodec.h"
  24. #include "libavcodec/mpeg4audio.h"
  25. #include "libavutil/opt.h"
  26. #include "avformat.h"
  27. #include "rawenc.h"
  28. typedef struct {
  29. int off;
  30. int channel_conf;
  31. int object_type;
  32. int counter;
  33. int mod;
  34. } LATMContext;
  35. static const AVOption options[] = {
  36. {"smc-interval", "StreamMuxConfig interval.",
  37. offsetof(LATMContext, mod), FF_OPT_TYPE_INT, {.dbl = 0x0014}, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM},
  38. {NULL},
  39. };
  40. static const AVClass latm_muxer_class = {
  41. .class_name = "LATM/LOAS muxer",
  42. .item_name = av_default_item_name,
  43. .option = options,
  44. .version = LIBAVUTIL_VERSION_INT,
  45. };
  46. static int latm_decode_extradata(LATMContext *ctx, uint8_t *buf, int size)
  47. {
  48. GetBitContext gb;
  49. MPEG4AudioConfig m4ac;
  50. init_get_bits(&gb, buf, size * 8);
  51. ctx->off = ff_mpeg4audio_get_config(&m4ac, buf, size);
  52. if (ctx->off < 0)
  53. return ctx->off;
  54. skip_bits_long(&gb, ctx->off);
  55. /* FIXME: are any formats not allowed in LATM? */
  56. if (m4ac.object_type > AOT_SBR && m4ac.object_type != AOT_ALS) {
  57. av_log(ctx, AV_LOG_ERROR, "Muxing MPEG-4 AOT %d in LATM is not supported\n", m4ac.object_type);
  58. return AVERROR_INVALIDDATA;
  59. }
  60. ctx->channel_conf = m4ac.chan_config;
  61. ctx->object_type = m4ac.object_type;
  62. return 0;
  63. }
  64. static int latm_write_header(AVFormatContext *s)
  65. {
  66. LATMContext *ctx = s->priv_data;
  67. AVCodecContext *avctx = s->streams[0]->codec;
  68. if (avctx->codec_id == CODEC_ID_AAC_LATM)
  69. return 0;
  70. if (avctx->extradata_size > 0 &&
  71. latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
  72. return AVERROR_INVALIDDATA;
  73. return 0;
  74. }
  75. static int latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
  76. {
  77. LATMContext *ctx = s->priv_data;
  78. AVCodecContext *avctx = s->streams[0]->codec;
  79. GetBitContext gb;
  80. int header_size;
  81. /* AudioMuxElement */
  82. put_bits(bs, 1, !!ctx->counter);
  83. if (!ctx->counter) {
  84. init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
  85. /* StreamMuxConfig */
  86. put_bits(bs, 1, 0); /* audioMuxVersion */
  87. put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
  88. put_bits(bs, 6, 0); /* numSubFrames */
  89. put_bits(bs, 4, 0); /* numProgram */
  90. put_bits(bs, 3, 0); /* numLayer */
  91. /* AudioSpecificConfig */
  92. if (ctx->object_type == AOT_ALS) {
  93. header_size = avctx->extradata_size-(ctx->off + 7) >> 3;
  94. ff_copy_bits(bs, &avctx->extradata[ctx->off], header_size);
  95. } else {
  96. ff_copy_bits(bs, avctx->extradata, ctx->off + 3);
  97. if (!ctx->channel_conf) {
  98. ff_copy_pce_data(bs, &gb);
  99. }
  100. }
  101. put_bits(bs, 3, 0); /* frameLengthType */
  102. put_bits(bs, 8, 0); /* latmBufferFullness */
  103. put_bits(bs, 1, 0); /* otherDataPresent */
  104. put_bits(bs, 1, 0); /* crcCheckPresent */
  105. }
  106. ctx->counter++;
  107. ctx->counter %= ctx->mod;
  108. return 0;
  109. }
  110. static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
  111. {
  112. AVIOContext *pb = s->pb;
  113. PutBitContext bs;
  114. int i, len;
  115. uint8_t loas_header[] = "\x56\xe0\x00";
  116. uint8_t *buf;
  117. if (s->streams[0]->codec->codec_id == CODEC_ID_AAC_LATM)
  118. return ff_raw_write_packet(s, pkt);
  119. if (pkt->size > 2 && pkt->data[0] == 0xff && (pkt->data[1] >> 4) == 0xf) {
  120. av_log(s, AV_LOG_ERROR, "ADTS header detected - ADTS will not be incorrectly muxed into LATM\n");
  121. return AVERROR_INVALIDDATA;
  122. }
  123. buf = av_malloc(pkt->size+1024);
  124. if (!buf)
  125. return AVERROR(ENOMEM);
  126. init_put_bits(&bs, buf, pkt->size+1024);
  127. latm_write_frame_header(s, &bs);
  128. /* PayloadLengthInfo() */
  129. for (i = 0; i <= pkt->size-255; i+=255)
  130. put_bits(&bs, 8, 255);
  131. put_bits(&bs, 8, pkt->size-i);
  132. /* The LATM payload is written unaligned */
  133. /* PayloadMux() */
  134. for (i = 0; i < pkt->size; i++)
  135. put_bits(&bs, 8, pkt->data[i]);
  136. align_put_bits(&bs);
  137. flush_put_bits(&bs);
  138. len = put_bits_count(&bs) >> 3;
  139. loas_header[1] |= (len >> 8) & 0x1f;
  140. loas_header[2] |= len & 0xff;
  141. avio_write(pb, loas_header, 3);
  142. avio_write(pb, buf, len);
  143. av_free(buf);
  144. return 0;
  145. }
  146. AVOutputFormat ff_latm_muxer = {
  147. .name = "latm",
  148. .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
  149. .mime_type = "audio/MP4A-LATM",
  150. .extensions = "latm,loas",
  151. .priv_data_size = sizeof(LATMContext),
  152. .audio_codec = CODEC_ID_AAC,
  153. .video_codec = CODEC_ID_NONE,
  154. .write_header = latm_write_header,
  155. .write_packet = latm_write_packet,
  156. .priv_class = &latm_muxer_class,
  157. };