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.

89 lines
2.8KB

  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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 "avcodec.h"
  23. #include "mpegaudiodecheader.h"
  24. static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  25. uint8_t **poutbuf, int *poutbuf_size,
  26. const uint8_t *buf, int buf_size, int keyframe){
  27. uint32_t header, extraheader;
  28. int mode_extension, header_size;
  29. if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  30. av_log(avctx, AV_LOG_ERROR, "not standards compliant\n");
  31. return -1;
  32. }
  33. header = AV_RB32(buf);
  34. mode_extension= (header>>4)&3;
  35. if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){
  36. output_unchanged:
  37. *poutbuf= (uint8_t *) buf;
  38. *poutbuf_size= buf_size;
  39. av_log(avctx, AV_LOG_INFO, "cannot compress %08X\n", header);
  40. return 0;
  41. }
  42. if(avctx->extradata_size == 0){
  43. avctx->extradata_size=15;
  44. avctx->extradata= av_malloc(avctx->extradata_size);
  45. strcpy(avctx->extradata, "FFCMP3 0.0");
  46. memcpy(avctx->extradata+11, buf, 4);
  47. }
  48. if(avctx->extradata_size != 15){
  49. av_log(avctx, AV_LOG_ERROR, "Extradata invalid\n");
  50. return -1;
  51. }
  52. extraheader = AV_RB32(avctx->extradata+11);
  53. if((extraheader&MP3_MASK) != (header&MP3_MASK))
  54. goto output_unchanged;
  55. header_size= (header&0x10000) ? 4 : 6;
  56. *poutbuf_size= buf_size - header_size;
  57. *poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
  58. memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
  59. if(avctx->channels==2){
  60. if((header & (3<<19)) != 3<<19){
  61. (*poutbuf)[1] &= 0x3F;
  62. (*poutbuf)[1] |= mode_extension<<6;
  63. FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);
  64. }else{
  65. (*poutbuf)[1] &= 0x8F;
  66. (*poutbuf)[1] |= mode_extension<<4;
  67. }
  68. }
  69. return 1;
  70. }
  71. AVBitStreamFilter ff_mp3_header_compress_bsf={
  72. "mp3comp",
  73. 0,
  74. mp3_header_compress,
  75. };