mp3_header_compress_bsf.c 2.8 KB

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