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.

207 lines
6.3KB

  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 "libavutil/opt.h"
  27. #include "avformat.h"
  28. #include "apetag.h"
  29. #define ADTS_HEADER_SIZE 7
  30. typedef struct {
  31. AVClass *class;
  32. int write_adts;
  33. int objecttype;
  34. int sample_rate_index;
  35. int channel_conf;
  36. int pce_size;
  37. int apetag;
  38. uint8_t pce_data[MAX_PCE_SIZE];
  39. } ADTSContext;
  40. #define ADTS_MAX_FRAME_BYTES ((1 << 13) - 1)
  41. static int adts_decode_extradata(AVFormatContext *s, ADTSContext *adts, uint8_t *buf, int size)
  42. {
  43. GetBitContext gb;
  44. PutBitContext pb;
  45. MPEG4AudioConfig m4ac;
  46. int off;
  47. init_get_bits(&gb, buf, size * 8);
  48. off = avpriv_mpeg4audio_get_config(&m4ac, buf, size * 8, 1);
  49. if (off < 0)
  50. return off;
  51. skip_bits_long(&gb, off);
  52. adts->objecttype = m4ac.object_type - 1;
  53. adts->sample_rate_index = m4ac.sampling_index;
  54. adts->channel_conf = m4ac.chan_config;
  55. if (adts->objecttype > 3U) {
  56. av_log(s, AV_LOG_ERROR, "MPEG-4 AOT %d is not allowed in ADTS\n", adts->objecttype+1);
  57. return -1;
  58. }
  59. if (adts->sample_rate_index == 15) {
  60. av_log(s, AV_LOG_ERROR, "Escape sample rate index illegal in ADTS\n");
  61. return -1;
  62. }
  63. if (get_bits(&gb, 1)) {
  64. av_log(s, AV_LOG_ERROR, "960/120 MDCT window is not allowed in ADTS\n");
  65. return -1;
  66. }
  67. if (get_bits(&gb, 1)) {
  68. av_log(s, AV_LOG_ERROR, "Scalable configurations are not allowed in ADTS\n");
  69. return -1;
  70. }
  71. if (get_bits(&gb, 1)) {
  72. av_log(s, AV_LOG_ERROR, "Extension flag is not allowed in ADTS\n");
  73. return -1;
  74. }
  75. if (!adts->channel_conf) {
  76. init_put_bits(&pb, adts->pce_data, MAX_PCE_SIZE);
  77. put_bits(&pb, 3, 5); //ID_PCE
  78. adts->pce_size = (avpriv_copy_pce_data(&pb, &gb) + 3) / 8;
  79. flush_put_bits(&pb);
  80. }
  81. adts->write_adts = 1;
  82. return 0;
  83. }
  84. static int adts_write_header(AVFormatContext *s)
  85. {
  86. ADTSContext *adts = s->priv_data;
  87. AVCodecContext *avc = s->streams[0]->codec;
  88. if (avc->extradata_size > 0 &&
  89. adts_decode_extradata(s, adts, avc->extradata, avc->extradata_size) < 0)
  90. return -1;
  91. return 0;
  92. }
  93. static int adts_write_frame_header(ADTSContext *ctx,
  94. uint8_t *buf, int size, int pce_size)
  95. {
  96. PutBitContext pb;
  97. unsigned full_frame_size = (unsigned)ADTS_HEADER_SIZE + size + pce_size;
  98. if (full_frame_size > ADTS_MAX_FRAME_BYTES) {
  99. av_log(NULL, AV_LOG_ERROR, "ADTS frame size too large: %u (max %d)\n",
  100. full_frame_size, ADTS_MAX_FRAME_BYTES);
  101. return AVERROR_INVALIDDATA;
  102. }
  103. init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
  104. /* adts_fixed_header */
  105. put_bits(&pb, 12, 0xfff); /* syncword */
  106. put_bits(&pb, 1, 0); /* ID */
  107. put_bits(&pb, 2, 0); /* layer */
  108. put_bits(&pb, 1, 1); /* protection_absent */
  109. put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
  110. put_bits(&pb, 4, ctx->sample_rate_index);
  111. put_bits(&pb, 1, 0); /* private_bit */
  112. put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
  113. put_bits(&pb, 1, 0); /* original_copy */
  114. put_bits(&pb, 1, 0); /* home */
  115. /* adts_variable_header */
  116. put_bits(&pb, 1, 0); /* copyright_identification_bit */
  117. put_bits(&pb, 1, 0); /* copyright_identification_start */
  118. put_bits(&pb, 13, full_frame_size); /* aac_frame_length */
  119. put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
  120. put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
  121. flush_put_bits(&pb);
  122. return 0;
  123. }
  124. static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
  125. {
  126. ADTSContext *adts = s->priv_data;
  127. AVIOContext *pb = s->pb;
  128. uint8_t buf[ADTS_HEADER_SIZE];
  129. if (!pkt->size)
  130. return 0;
  131. if (adts->write_adts) {
  132. int err = adts_write_frame_header(adts, buf, pkt->size,
  133. adts->pce_size);
  134. if (err < 0)
  135. return err;
  136. avio_write(pb, buf, ADTS_HEADER_SIZE);
  137. if (adts->pce_size) {
  138. avio_write(pb, adts->pce_data, adts->pce_size);
  139. adts->pce_size = 0;
  140. }
  141. }
  142. avio_write(pb, pkt->data, pkt->size);
  143. return 0;
  144. }
  145. static int adts_write_trailer(AVFormatContext *s)
  146. {
  147. ADTSContext *adts = s->priv_data;
  148. if (adts->apetag)
  149. ff_ape_write_tag(s);
  150. return 0;
  151. }
  152. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  153. #define OFFSET(obj) offsetof(ADTSContext, obj)
  154. static const AVOption options[] = {
  155. { "write_apetag", "Enable APE tag writing", OFFSET(apetag), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, ENC},
  156. { NULL },
  157. };
  158. static const AVClass adts_muxer_class = {
  159. .class_name = "ADTS muxer",
  160. .item_name = av_default_item_name,
  161. .option = options,
  162. .version = LIBAVUTIL_VERSION_INT,
  163. };
  164. AVOutputFormat ff_adts_muxer = {
  165. .name = "adts",
  166. .long_name = NULL_IF_CONFIG_SMALL("ADTS AAC (Advanced Audio Coding)"),
  167. .mime_type = "audio/aac",
  168. .extensions = "aac,adts",
  169. .priv_data_size = sizeof(ADTSContext),
  170. .audio_codec = AV_CODEC_ID_AAC,
  171. .video_codec = AV_CODEC_ID_NONE,
  172. .write_header = adts_write_header,
  173. .write_packet = adts_write_packet,
  174. .write_trailer = adts_write_trailer,
  175. .priv_class = &adts_muxer_class,
  176. .flags = AVFMT_NOTIMESTAMPS,
  177. };