latmenc.c 7.0 KB

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