latmenc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. AVClass *av_class;
  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), AV_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 = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
  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->extradata_size > 0 &&
  69. latm_decode_extradata(ctx, avctx->extradata, avctx->extradata_size) < 0)
  70. return AVERROR_INVALIDDATA;
  71. return 0;
  72. }
  73. static int latm_write_frame_header(AVFormatContext *s, PutBitContext *bs)
  74. {
  75. LATMContext *ctx = s->priv_data;
  76. AVCodecContext *avctx = s->streams[0]->codec;
  77. GetBitContext gb;
  78. int header_size;
  79. /* AudioMuxElement */
  80. put_bits(bs, 1, !!ctx->counter);
  81. if (!ctx->counter) {
  82. init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
  83. /* StreamMuxConfig */
  84. put_bits(bs, 1, 0); /* audioMuxVersion */
  85. put_bits(bs, 1, 1); /* allStreamsSameTimeFraming */
  86. put_bits(bs, 6, 0); /* numSubFrames */
  87. put_bits(bs, 4, 0); /* numProgram */
  88. put_bits(bs, 3, 0); /* numLayer */
  89. /* AudioSpecificConfig */
  90. if (ctx->object_type == AOT_ALS) {
  91. header_size = avctx->extradata_size-(ctx->off + 7) >> 3;
  92. avpriv_copy_bits(bs, &avctx->extradata[ctx->off], header_size);
  93. } else {
  94. avpriv_copy_bits(bs, avctx->extradata, ctx->off + 3);
  95. if (!ctx->channel_conf) {
  96. avpriv_copy_pce_data(bs, &gb);
  97. }
  98. }
  99. put_bits(bs, 3, 0); /* frameLengthType */
  100. put_bits(bs, 8, 0xff); /* latmBufferFullness */
  101. put_bits(bs, 1, 0); /* otherDataPresent */
  102. put_bits(bs, 1, 0); /* crcCheckPresent */
  103. }
  104. ctx->counter++;
  105. ctx->counter %= ctx->mod;
  106. return 0;
  107. }
  108. static int latm_write_packet(AVFormatContext *s, AVPacket *pkt)
  109. {
  110. AVIOContext *pb = s->pb;
  111. PutBitContext bs;
  112. int i, len;
  113. uint8_t loas_header[] = "\x56\xe0\x00";
  114. uint8_t *buf;
  115. if (pkt->size > 2 && pkt->data[0] == 0xff && (pkt->data[1] >> 4) == 0xf) {
  116. av_log(s, AV_LOG_ERROR, "ADTS header detected - ADTS will not be incorrectly muxed into LATM\n");
  117. return AVERROR_INVALIDDATA;
  118. }
  119. buf = av_malloc(pkt->size+1024);
  120. if (!buf)
  121. return AVERROR(ENOMEM);
  122. init_put_bits(&bs, buf, pkt->size+1024);
  123. latm_write_frame_header(s, &bs);
  124. /* PayloadLengthInfo() */
  125. for (i = 0; i <= pkt->size-255; i+=255)
  126. put_bits(&bs, 8, 255);
  127. put_bits(&bs, 8, pkt->size-i);
  128. /* The LATM payload is written unaligned */
  129. /* PayloadMux() */
  130. for (i = 0; i < pkt->size; i++)
  131. put_bits(&bs, 8, pkt->data[i]);
  132. avpriv_align_put_bits(&bs);
  133. flush_put_bits(&bs);
  134. len = put_bits_count(&bs) >> 3;
  135. loas_header[1] |= (len >> 8) & 0x1f;
  136. loas_header[2] |= len & 0xff;
  137. avio_write(pb, loas_header, 3);
  138. avio_write(pb, buf, len);
  139. av_free(buf);
  140. return 0;
  141. }
  142. AVOutputFormat ff_latm_muxer = {
  143. .name = "latm",
  144. .long_name = NULL_IF_CONFIG_SMALL("LOAS/LATM"),
  145. .mime_type = "audio/MP4A-LATM",
  146. .extensions = "latm",
  147. .priv_data_size = sizeof(LATMContext),
  148. .audio_codec = CODEC_ID_AAC,
  149. .video_codec = CODEC_ID_NONE,
  150. .write_header = latm_write_header,
  151. .write_packet = latm_write_packet,
  152. .priv_class = &latm_muxer_class,
  153. };