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.

158 lines
4.6KB

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