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.

119 lines
3.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/bitstream.h"
  23. #include "avformat.h"
  24. #define ADTS_HEADER_SIZE 7
  25. typedef struct {
  26. int write_adts;
  27. int objecttype;
  28. int sample_rate_index;
  29. int channel_conf;
  30. } ADTSContext;
  31. static int decode_extradata(ADTSContext *adts, uint8_t *buf, int size)
  32. {
  33. GetBitContext gb;
  34. init_get_bits(&gb, buf, size * 8);
  35. adts->objecttype = get_bits(&gb, 5) - 1;
  36. adts->sample_rate_index = get_bits(&gb, 4);
  37. adts->channel_conf = get_bits(&gb, 4);
  38. adts->write_adts = 1;
  39. return 0;
  40. }
  41. static int adts_write_header(AVFormatContext *s)
  42. {
  43. ADTSContext *adts = s->priv_data;
  44. AVCodecContext *avc = s->streams[0]->codec;
  45. if(avc->extradata_size > 0)
  46. decode_extradata(adts, avc->extradata, avc->extradata_size);
  47. return 0;
  48. }
  49. static int adts_write_frame_header(AVFormatContext *s, int size)
  50. {
  51. ADTSContext *ctx = s->priv_data;
  52. PutBitContext pb;
  53. uint8_t buf[ADTS_HEADER_SIZE];
  54. init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
  55. /* adts_fixed_header */
  56. put_bits(&pb, 12, 0xfff); /* syncword */
  57. put_bits(&pb, 1, 0); /* ID */
  58. put_bits(&pb, 2, 0); /* layer */
  59. put_bits(&pb, 1, 1); /* protection_absent */
  60. put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
  61. put_bits(&pb, 4, ctx->sample_rate_index);
  62. put_bits(&pb, 1, 0); /* private_bit */
  63. put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
  64. put_bits(&pb, 1, 0); /* original_copy */
  65. put_bits(&pb, 1, 0); /* home */
  66. /* adts_variable_header */
  67. put_bits(&pb, 1, 0); /* copyright_identification_bit */
  68. put_bits(&pb, 1, 0); /* copyright_identification_start */
  69. put_bits(&pb, 13, ADTS_HEADER_SIZE + size); /* aac_frame_length */
  70. put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
  71. put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
  72. flush_put_bits(&pb);
  73. put_buffer(s->pb, buf, ADTS_HEADER_SIZE);
  74. return 0;
  75. }
  76. static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
  77. {
  78. ADTSContext *adts = s->priv_data;
  79. ByteIOContext *pb = s->pb;
  80. if (!pkt->size)
  81. return 0;
  82. if(adts->write_adts)
  83. adts_write_frame_header(s, pkt->size);
  84. put_buffer(pb, pkt->data, pkt->size);
  85. put_flush_packet(pb);
  86. return 0;
  87. }
  88. AVOutputFormat adts_muxer = {
  89. "adts",
  90. NULL_IF_CONFIG_SMALL("ADTS AAC"),
  91. "audio/aac",
  92. "aac",
  93. sizeof(ADTSContext),
  94. CODEC_ID_AAC,
  95. CODEC_ID_NONE,
  96. adts_write_header,
  97. adts_write_packet,
  98. };