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.

191 lines
5.3KB

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