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.

228 lines
7.0KB

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