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.

121 lines
4.1KB

  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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "aacadtsdec.h"
  23. #include "put_bits.h"
  24. #include "get_bits.h"
  25. #include "mpeg4audio.h"
  26. #include "internal.h"
  27. typedef struct AACBSFContext {
  28. int first_frame_done;
  29. } AACBSFContext;
  30. /**
  31. * This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4
  32. * ADTS header and removes the ADTS header.
  33. */
  34. static int aac_adtstoasc_filter(AVBitStreamFilterContext *bsfc,
  35. AVCodecContext *avctx, const char *args,
  36. uint8_t **poutbuf, int *poutbuf_size,
  37. const uint8_t *buf, int buf_size,
  38. int keyframe)
  39. {
  40. GetBitContext gb;
  41. PutBitContext pb;
  42. AACADTSHeaderInfo hdr;
  43. AACBSFContext *ctx = bsfc->priv_data;
  44. init_get_bits(&gb, buf, AAC_ADTS_HEADER_SIZE*8);
  45. *poutbuf = (uint8_t*) buf;
  46. *poutbuf_size = buf_size;
  47. if (avctx->extradata)
  48. if (show_bits(&gb, 12) != 0xfff)
  49. return 0;
  50. if (avpriv_aac_parse_header(&gb, &hdr) < 0) {
  51. av_log(avctx, AV_LOG_ERROR, "Error parsing ADTS frame header!\n");
  52. return AVERROR_INVALIDDATA;
  53. }
  54. if (!hdr.crc_absent && hdr.num_aac_frames > 1) {
  55. avpriv_report_missing_feature(avctx,
  56. "Multiple RDBs per frame with CRC");
  57. return AVERROR_PATCHWELCOME;
  58. }
  59. buf += AAC_ADTS_HEADER_SIZE + 2*!hdr.crc_absent;
  60. buf_size -= AAC_ADTS_HEADER_SIZE + 2*!hdr.crc_absent;
  61. if (!ctx->first_frame_done) {
  62. int pce_size = 0;
  63. uint8_t pce_data[MAX_PCE_SIZE];
  64. if (!hdr.chan_config) {
  65. init_get_bits(&gb, buf, buf_size * 8);
  66. if (get_bits(&gb, 3) != 5) {
  67. avpriv_report_missing_feature(avctx,
  68. "PCE-based channel configuration "
  69. "without PCE as first syntax "
  70. "element");
  71. return AVERROR_PATCHWELCOME;
  72. }
  73. init_put_bits(&pb, pce_data, MAX_PCE_SIZE);
  74. pce_size = avpriv_copy_pce_data(&pb, &gb)/8;
  75. flush_put_bits(&pb);
  76. buf_size -= get_bits_count(&gb)/8;
  77. buf += get_bits_count(&gb)/8;
  78. }
  79. avctx->extradata_size = 2 + pce_size;
  80. avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  81. if (!avctx->extradata)
  82. return AVERROR(ENOMEM);
  83. init_put_bits(&pb, avctx->extradata, avctx->extradata_size);
  84. put_bits(&pb, 5, hdr.object_type);
  85. put_bits(&pb, 4, hdr.sampling_index);
  86. put_bits(&pb, 4, hdr.chan_config);
  87. put_bits(&pb, 1, 0); //frame length - 1024 samples
  88. put_bits(&pb, 1, 0); //does not depend on core coder
  89. put_bits(&pb, 1, 0); //is not extension
  90. flush_put_bits(&pb);
  91. if (pce_size) {
  92. memcpy(avctx->extradata + 2, pce_data, pce_size);
  93. }
  94. ctx->first_frame_done = 1;
  95. }
  96. *poutbuf = (uint8_t*) buf;
  97. *poutbuf_size = buf_size;
  98. return 0;
  99. }
  100. AVBitStreamFilter ff_aac_adtstoasc_bsf = {
  101. "aac_adtstoasc",
  102. sizeof(AACBSFContext),
  103. aac_adtstoasc_filter,
  104. };