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.

122 lines
3.4KB

  1. /*
  2. * ADTS muxer.
  3. * Copyright (c) 2006 Baptiste Coudurier <baptiste.coudurier@smartjog.com>
  4. * Mans Rullgard <mru@inprovide.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "avformat.h"
  21. #include "bitstream.h"
  22. #define ADTS_HEADER_SIZE 7
  23. typedef struct {
  24. int write_adts;
  25. int objecttype;
  26. int sample_rate_index;
  27. int channel_conf;
  28. } ADTSContext;
  29. static int decode_extradata(ADTSContext *adts, uint8_t *buf, int size)
  30. {
  31. GetBitContext gb;
  32. init_get_bits(&gb, buf, size * 8);
  33. adts->objecttype = get_bits(&gb, 5) - 1;
  34. adts->sample_rate_index = get_bits(&gb, 4);
  35. adts->channel_conf = get_bits(&gb, 4);
  36. adts->write_adts = 1;
  37. return 0;
  38. }
  39. static int adts_write_header(AVFormatContext *s)
  40. {
  41. ADTSContext *adts = s->priv_data;
  42. AVCodecContext *avc = s->streams[0]->codec;
  43. if(avc->extradata_size > 0)
  44. decode_extradata(adts, avc->extradata, avc->extradata_size);
  45. return 0;
  46. }
  47. static int adts_write_frame_header(AVFormatContext *s, int size)
  48. {
  49. ADTSContext *ctx = s->priv_data;
  50. PutBitContext pb;
  51. uint8_t buf[ADTS_HEADER_SIZE];
  52. init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
  53. /* adts_fixed_header */
  54. put_bits(&pb, 12, 0xfff); /* syncword */
  55. put_bits(&pb, 1, 0); /* ID */
  56. put_bits(&pb, 2, 0); /* layer */
  57. put_bits(&pb, 1, 1); /* protection_absent */
  58. put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
  59. put_bits(&pb, 4, ctx->sample_rate_index);
  60. put_bits(&pb, 1, 0); /* private_bit */
  61. put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
  62. put_bits(&pb, 1, 0); /* original_copy */
  63. put_bits(&pb, 1, 0); /* home */
  64. /* adts_variable_header */
  65. put_bits(&pb, 1, 0); /* copyright_identification_bit */
  66. put_bits(&pb, 1, 0); /* copyright_identification_start */
  67. put_bits(&pb, 13, ADTS_HEADER_SIZE + size); /* aac_frame_length */
  68. put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
  69. put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
  70. flush_put_bits(&pb);
  71. put_buffer(&s->pb, buf, ADTS_HEADER_SIZE);
  72. return 0;
  73. }
  74. static int adts_write_trailer(AVFormatContext *s)
  75. {
  76. return 0;
  77. }
  78. static int adts_write_packet(AVFormatContext *s, AVPacket *pkt)
  79. {
  80. ADTSContext *adts = s->priv_data;
  81. ByteIOContext *pb = &s->pb;
  82. if (!pkt->size)
  83. return 0;
  84. if(adts->write_adts)
  85. adts_write_frame_header(s, pkt->size);
  86. put_buffer(pb, pkt->data, pkt->size);
  87. put_flush_packet(pb);
  88. return 0;
  89. }
  90. AVOutputFormat adts_muxer = {
  91. "adts",
  92. "ADTS AAC",
  93. "audio/aac",
  94. "aac",
  95. sizeof(ADTSContext),
  96. CODEC_ID_AAC,
  97. CODEC_ID_NONE,
  98. adts_write_header,
  99. adts_write_packet,
  100. adts_write_trailer,
  101. };