adtsenc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include "adts.h"
  28. int ff_adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size)
  29. {
  30. GetBitContext gb;
  31. PutBitContext pb;
  32. MPEG4AudioConfig m4ac;
  33. int off;
  34. init_get_bits(&gb, buf, size * 8);
  35. off = ff_mpeg4audio_get_config(&m4ac, buf, size);
  36. if (off < 0)
  37. return off;
  38. skip_bits_long(&gb, off);
  39. adts->objecttype = m4ac.object_type - 1;
  40. adts->sample_rate_index = m4ac.sampling_index;
  41. adts->channel_conf = m4ac.chan_config;
  42. if (adts->objecttype > 3U) {
  43. av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
  44. return -1;
  45. }
  46. if (adts->sample_rate_index == 15) {
  47. av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
  48. return -1;
  49. }
  50. if (get_bits(&gb, 1)) {
  51. av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
  52. return -1;
  53. }
  54. if (get_bits(&gb, 1)) {
  55. av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
  56. return -1;
  57. }
  58. if (get_bits(&gb, 1)) {
  59. av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
  60. return -1;
  61. }
  62. if (!adts->channel_conf) {
  63. init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
  64. put_bits(&pb, 3, 5); //ID_PCE
  65. adts->pce_size = (ff_copy_pce_data(&pb, &gb) + 3) / 8;
  66. flush_put_bits(&pb);
  67. }
  68. adts->write_adts = 1;
  69. return 0;
  70. }
  71. static int adts_write_header(AVFormatContext *s)
  72. {
  73. ADTSContext *adts = s->priv_data;
  74. AVCodecContext *avc = s->streams[0]->codec;
  75. if (avc->extradata_size > 0 &&
  76. ff_adts_decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0)
  77. return -1;
  78. return 0;
  79. }
  80. int ff_adts_write_frame_header(ADTSContext *ctx,
  81. uint8_t *buf, int size, int pce_size)
  82. {
  83. PutBitContext pb;
  84. init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
  85. /* adts_fixed_header */
  86. put_bits(&pb, 12, 0xfff); /* syncword */
  87. put_bits(&pb, 1, 0); /* ID */
  88. put_bits(&pb, 2, 0); /* layer */
  89. put_bits(&pb, 1, 1); /* protection_absent */
  90. put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
  91. put_bits(&pb, 4, ctx->sample_rate_index);
  92. put_bits(&pb, 1, 0); /* private_bit */
  93. put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
  94. put_bits(&pb, 1, 0); /* original_copy */
  95. put_bits(&pb, 1, 0); /* home */
  96. /* adts_variable_header */
  97. put_bits(&pb, 1, 0); /* copyright_identification_bit */
  98. put_bits(&pb, 1, 0); /* copyright_identification_start */
  99. put_bits(&pb, 13, ADTS_HEADER_SIZE + size + pce_size); /* aac_frame_length */
  100. put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
  101. put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
  102. flush_put_bits(&pb);
  103. return 0;
  104. }
  105. static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
  106. {
  107. ADTSContext *adts = s->priv_data;
  108. AVIOContext *pb = s->pb;
  109. uint8_t buf[ADTS_HEADER_SIZE];
  110. if (!pkt->size)
  111. return 0;
  112. if (adts->write_adts) {
  113. ff_adts_write_frame_header(adts, buf, pkt->size, adts->pce_size);
  114. avio_write(pb, buf, ADTS_HEADER_SIZE);
  115. if (adts->pce_size) {
  116. avio_write(pb, adts->pce_data, adts->pce_size);
  117. adts->pce_size = 0;
  118. }
  119. }
  120. avio_write(pb, pkt->data, pkt->size);
  121. avio_flush(pb);
  122. return 0;
  123. }
  124. AVOutputFormat ff_adts_muxer = {
  125. .name = "adts",
  126. .long_name = NULL_IF_CONFIG_SMALL("ADTS AAC"),
  127. .mime_type = "audio/aac",
  128. .extensions = "aac,adts",
  129. .priv_data_size = sizeof(ADTSContext),
  130. .audio_codec = CODEC_ID_AAC,
  131. .video_codec = CODEC_ID_NONE,
  132. .write_header = adts_write_header,
  133. .write_packet = adts_write_packet,
  134. };