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.

130 lines
3.7KB

  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/common.h"
  21. #include "libavutil/intreadwrite.h"
  22. #include "bsf.h"
  23. #include "bsf_internal.h"
  24. #include "mpegaudiodecheader.h"
  25. #include "mpegaudiodata.h"
  26. static int mp3_header_decompress(AVBSFContext *ctx, AVPacket *out)
  27. {
  28. AVPacket *in;
  29. uint32_t header;
  30. int sample_rate= ctx->par_in->sample_rate;
  31. int sample_rate_index=0;
  32. int lsf, mpeg25, bitrate_index, frame_size, ret;
  33. uint8_t *buf;
  34. int buf_size;
  35. ret = ff_bsf_get_packet(ctx, &in);
  36. if (ret < 0)
  37. return ret;
  38. buf = in->data;
  39. buf_size = in->size;
  40. header = AV_RB32(buf);
  41. if(ff_mpa_check_header(header) >= 0){
  42. av_packet_move_ref(out, in);
  43. av_packet_free(&in);
  44. return 0;
  45. }
  46. if(ctx->par_in->extradata_size != 15 || strcmp(ctx->par_in->extradata, "FFCMP3 0.0")){
  47. av_log(ctx, AV_LOG_ERROR, "Extradata invalid %d\n", ctx->par_in->extradata_size);
  48. ret = AVERROR(EINVAL);
  49. goto fail;
  50. }
  51. header= AV_RB32(ctx->par_in->extradata+11) & MP3_MASK;
  52. lsf = sample_rate < (24000+32000)/2;
  53. mpeg25 = sample_rate < (12000+16000)/2;
  54. sample_rate_index= (header>>10)&3;
  55. if (sample_rate_index == 3) {
  56. ret = AVERROR_INVALIDDATA;
  57. goto fail;
  58. }
  59. sample_rate= avpriv_mpa_freq_tab[sample_rate_index] >> (lsf + mpeg25); //in case sample rate is a little off
  60. for(bitrate_index=2; bitrate_index<30; bitrate_index++){
  61. frame_size = avpriv_mpa_bitrate_tab[lsf][2][bitrate_index>>1];
  62. frame_size = (frame_size * 144000) / (sample_rate << lsf) + (bitrate_index&1);
  63. if(frame_size == buf_size + 4)
  64. break;
  65. if(frame_size == buf_size + 6)
  66. break;
  67. }
  68. if(bitrate_index == 30){
  69. av_log(ctx, AV_LOG_ERROR, "Could not find bitrate_index.\n");
  70. ret = AVERROR(EINVAL);
  71. goto fail;
  72. }
  73. header |= (bitrate_index&1)<<9;
  74. header |= (bitrate_index>>1)<<12;
  75. header |= (frame_size == buf_size + 4)<<16; //FIXME actually set a correct crc instead of 0
  76. ret = av_new_packet(out, frame_size);
  77. if (ret < 0)
  78. goto fail;
  79. ret = av_packet_copy_props(out, in);
  80. if (ret < 0) {
  81. av_packet_unref(out);
  82. goto fail;
  83. }
  84. memcpy(out->data + frame_size - buf_size, buf, buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  85. if(ctx->par_in->channels==2){
  86. uint8_t *p= out->data + frame_size - buf_size;
  87. if(lsf){
  88. FFSWAP(int, p[1], p[2]);
  89. header |= (p[1] & 0xC0)>>2;
  90. p[1] &= 0x3F;
  91. }else{
  92. header |= p[1] & 0x30;
  93. p[1] &= 0xCF;
  94. }
  95. }
  96. AV_WB32(out->data, header);
  97. ret = 0;
  98. fail:
  99. av_packet_free(&in);
  100. return ret;
  101. }
  102. static const enum AVCodecID codec_ids[] = {
  103. AV_CODEC_ID_MP3, AV_CODEC_ID_NONE,
  104. };
  105. const AVBitStreamFilter ff_mp3_header_decompress_bsf = {
  106. .name = "mp3decomp",
  107. .filter = mp3_header_decompress,
  108. .codec_ids = codec_ids,
  109. };