aac_adtstoasc_bsf.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * MPEG-2/4 AAC ADTS to MPEG-4 Audio Specific Configuration bitstream filter
  3. * Copyright (c) 2009 Alex Converse <alex.converse@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "adts_header.h"
  22. #include "adts_parser.h"
  23. #include "avcodec.h"
  24. #include "bsf.h"
  25. #include "put_bits.h"
  26. #include "get_bits.h"
  27. #include "mpeg4audio.h"
  28. #include "internal.h"
  29. typedef struct AACBSFContext {
  30. int first_frame_done;
  31. } AACBSFContext;
  32. /**
  33. * This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4
  34. * ADTS header and removes the ADTS header.
  35. */
  36. static int aac_adtstoasc_filter(AVBSFContext *bsfc, AVPacket *pkt)
  37. {
  38. AACBSFContext *ctx = bsfc->priv_data;
  39. GetBitContext gb;
  40. PutBitContext pb;
  41. AACADTSHeaderInfo hdr;
  42. int ret;
  43. ret = ff_bsf_get_packet_ref(bsfc, pkt);
  44. if (ret < 0)
  45. return ret;
  46. if (bsfc->par_in->extradata && pkt->size >= 2 && (AV_RB16(pkt->data) >> 4) != 0xfff)
  47. return 0;
  48. if (pkt->size < AV_AAC_ADTS_HEADER_SIZE)
  49. goto packet_too_small;
  50. init_get_bits(&gb, pkt->data, AV_AAC_ADTS_HEADER_SIZE * 8);
  51. if (ff_adts_header_parse(&gb, &hdr) < 0) {
  52. av_log(bsfc, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
  53. ret = AVERROR_INVALIDDATA;
  54. goto fail;
  55. }
  56. if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
  57. avpriv_report_missing_feature(bsfc,
  58. "Multiple RDBs per frame with CRC");
  59. ret = AVERROR_PATCHWELCOME;
  60. goto fail;
  61. }
  62. pkt->size -= AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
  63. if (pkt->size <= 0)
  64. goto packet_too_small;
  65. pkt->data += AV_AAC_ADTS_HEADER_SIZE + 2 * !hdr.crc_absent;
  66. if (!ctx->first_frame_done) {
  67. int pce_size = 0;
  68. uint8_t pce_data[MAX_PCE_SIZE];
  69. uint8_t *extradata;
  70. if (!hdr.chan_config) {
  71. init_get_bits(&gb, pkt->data, pkt->size * 8);
  72. if (get_bits(&gb, 3) != 5) {
  73. avpriv_report_missing_feature(bsfc,
  74. "PCE-based channel configuration "
  75. "without PCE as first syntax "
  76. "element");
  77. ret = AVERROR_PATCHWELCOME;
  78. goto fail;
  79. }
  80. init_put_bits(&pb, pce_data, MAX_PCE_SIZE);
  81. pce_size = ff_copy_pce_data(&pb, &gb) / 8;
  82. flush_put_bits(&pb);
  83. pkt->size -= get_bits_count(&gb)/8;
  84. pkt->data += get_bits_count(&gb)/8;
  85. }
  86. extradata = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
  87. 2 + pce_size);
  88. if (!extradata) {
  89. ret = AVERROR(ENOMEM);
  90. goto fail;
  91. }
  92. init_put_bits(&pb, extradata, 2 + pce_size);
  93. put_bits(&pb, 5, hdr.object_type);
  94. put_bits(&pb, 4, hdr.sampling_index);
  95. put_bits(&pb, 4, hdr.chan_config);
  96. put_bits(&pb, 1, 0); //frame length - 1024 samples
  97. put_bits(&pb, 1, 0); //does not depend on core coder
  98. put_bits(&pb, 1, 0); //is not extension
  99. flush_put_bits(&pb);
  100. if (pce_size) {
  101. memcpy(extradata + 2, pce_data, pce_size);
  102. }
  103. ctx->first_frame_done = 1;
  104. }
  105. return 0;
  106. packet_too_small:
  107. av_log(bsfc, AV_LOG_ERROR, "Input packet too small\n");
  108. ret = AVERROR_INVALIDDATA;
  109. fail:
  110. av_packet_unref(pkt);
  111. return ret;
  112. }
  113. static int aac_adtstoasc_init(AVBSFContext *ctx)
  114. {
  115. /* Validate the extradata if the stream is already MPEG-4 AudioSpecificConfig */
  116. if (ctx->par_in->extradata) {
  117. MPEG4AudioConfig mp4ac;
  118. int ret = avpriv_mpeg4audio_get_config(&mp4ac, ctx->par_in->extradata,
  119. ctx->par_in->extradata_size * 8, 1);
  120. if (ret < 0) {
  121. av_log(ctx, AV_LOG_ERROR, "Error parsing AudioSpecificConfig extradata!\n");
  122. return ret;
  123. }
  124. }
  125. return 0;
  126. }
  127. static const enum AVCodecID codec_ids[] = {
  128. AV_CODEC_ID_AAC, AV_CODEC_ID_NONE,
  129. };
  130. const AVBitStreamFilter ff_aac_adtstoasc_bsf = {
  131. .name = "aac_adtstoasc",
  132. .priv_data_size = sizeof(AACBSFContext),
  133. .init = aac_adtstoasc_init,
  134. .filter = aac_adtstoasc_filter,
  135. .codec_ids = codec_ids,
  136. };