adtsenc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * ADTS muxer.
  3. * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
  4. * Mans Rullgard <mans@mansr.com>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "libavcodec/get_bits.h"
  23. #include "libavcodec/put_bits.h"
  24. #include "libavcodec/avcodec.h"
  25. #include "libavcodec/mpeg4audio.h"
  26. #include "avformat.h"
  27. #define ADTS_HEADER_SIZE 7
  28. typedef struct {
  29. int write_adts;
  30. int objecttype;
  31. int sample_rate_index;
  32. int channel_conf;
  33. int pce_size;
  34. uint8_t pce_data[MAX_PCE_SIZE];
  35. } ADTSContext;
  36. #define ADTS_MAX_FRAME_BYTES ((1 << 13) - 1)
  37. static int adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size)
  38. {
  39. GetBitContext gb;
  40. PutBitContext pb;
  41. MPEG4AudioConfig m4ac;
  42. int off;
  43. init_get_bits(&gb, buf, size * 8);
  44. off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
  45. if (off < 0)
  46. return off;
  47. skip_bits_long(&gb, off);
  48. adts->objecttype = m4ac.object_type - 1;
  49. adts->sample_rate_index = m4ac.sampling_index;
  50. adts->channel_conf = m4ac.chan_config;
  51. if (adts->objecttype > 3U) {
  52. av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
  53. return -1;
  54. }
  55. if (adts->sample_rate_index == 15) {
  56. av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
  57. return -1;
  58. }
  59. if (get_bits(&gb, 1)) {
  60. av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
  61. return -1;
  62. }
  63. if (get_bits(&gb, 1)) {
  64. av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
  65. return -1;
  66. }
  67. if (get_bits(&gb, 1)) {
  68. av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
  69. return -1;
  70. }
  71. if (!adts->channel_conf) {
  72. init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
  73. put_bits(&pb, 3, 5); //ID_PCE
  74. adts->pce_size = (avpriv_copy_pce_data(&pb, &gb) + 3) / 8;
  75. flush_put_bits(&pb);
  76. }
  77. adts->write_adts = 1;
  78. return 0;
  79. }
  80. static int adts_write_header(AVFormatContext *s)
  81. {
  82. ADTSContext *adts = s->priv_data;
  83. AVCodecContext *avc = s->streams[0]->codec;
  84. if (avc->extradata_size > 0 &&
  85. adts_decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0)
  86. return -1;
  87. return 0;
  88. }
  89. static int adts_write_frame_header(ADTSContext *ctx,
  90. uint8_t *buf, int size, int pce_size)
  91. {
  92. PutBitContext pb;
  93. unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size;
  94. if (full_frame_size > ADTS_MAX_FRAME_BYTES) {
  95. av_log(NULL, AV_LOG_ERROR, "ADTS frame size too large: %u (max %d)\n",
  96. full_frame_size, ADTS_MAX_FRAME_BYTES);
  97. return AVERROR_INVALIDDATA;
  98. }
  99. init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
  100. /* adts_fixed_header */
  101. put_bits(&pb, 12, 0xfff); /* syncword */
  102. put_bits(&pb, 1, 0); /* ID */
  103. put_bits(&pb, 2, 0); /* layer */
  104. put_bits(&pb, 1, 1); /* protection_absent */
  105. put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
  106. put_bits(&pb, 4, ctx->sample_rate_index);
  107. put_bits(&pb, 1, 0); /* private_bit */
  108. put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
  109. put_bits(&pb, 1, 0); /* original_copy */
  110. put_bits(&pb, 1, 0); /* home */
  111. /* adts_variable_header */
  112. put_bits(&pb, 1, 0); /* copyright_identification_bit */
  113. put_bits(&pb, 1, 0); /* copyright_identification_start */
  114. put_bits(&pb, 13, full_frame_size); /* aac_frame_length */
  115. put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
  116. put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
  117. flush_put_bits(&pb);
  118. return 0;
  119. }
  120. static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
  121. {
  122. ADTSContext *adts = s->priv_data;
  123. AVIOContext *pb = s->pb;
  124. uint8_t buf[ADTS_HEADER_SIZE];
  125. if (!pkt->size)
  126. return 0;
  127. if (adts->write_adts) {
  128. int err = adts_write_frame_header(adts, buf, pkt->size,
  129. adts->pce_size);
  130. if (err < 0)
  131. return err;
  132. avio_write(pb, buf, ADTS_HEADER_SIZE);
  133. if (adts->pce_size) {
  134. avio_write(pb, adts->pce_data, adts->pce_size);
  135. adts->pce_size = 0;
  136. }
  137. }
  138. avio_write(pb, pkt->data, pkt->size);
  139. avio_flush(pb);
  140. return 0;
  141. }
  142. AVOutputFormat ff_adts_muxer = {
  143. .name = "adts",
  144. .long_name = NULL_IF_CONFIG_SMALL("ADTS AAC (Advanced Audio Coding)"),
  145. .mime_type = "audio/aac",
  146. .extensions = "aac,adts",
  147. .priv_data_size = sizeof(ADTSContext),
  148. .audio_codec = AV_CODEC_ID_AAC,
  149. .video_codec = AV_CODEC_ID_NONE,
  150. .write_header = adts_write_header,
  151. .write_packet = adts_write_packet,
  152. };