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.

238 lines
7.4KB

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