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.

216 lines
6.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 "avcodec.h"
  21. #include "mpegaudio.h"
  22. AVBitStreamFilter *first_bitstream_filter= NULL;
  23. void av_register_bitstream_filter(AVBitStreamFilter *bsf){
  24. bsf->next = first_bitstream_filter;
  25. first_bitstream_filter= bsf;
  26. }
  27. AVBitStreamFilterContext *av_bitstream_filter_init(const char *name){
  28. AVBitStreamFilter *bsf= first_bitstream_filter;
  29. while(bsf){
  30. if(!strcmp(name, bsf->name)){
  31. AVBitStreamFilterContext *bsfc= av_mallocz(sizeof(AVBitStreamFilterContext));
  32. bsfc->filter= bsf;
  33. bsfc->priv_data= av_mallocz(bsf->priv_data_size);
  34. return bsfc;
  35. }
  36. bsf= bsf->next;
  37. }
  38. return NULL;
  39. }
  40. void av_bitstream_filter_close(AVBitStreamFilterContext *bsfc){
  41. av_freep(&bsfc->priv_data);
  42. av_parser_close(bsfc->parser);
  43. av_free(bsfc);
  44. }
  45. int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,
  46. AVCodecContext *avctx, const char *args,
  47. uint8_t **poutbuf, int *poutbuf_size,
  48. const uint8_t *buf, int buf_size, int keyframe){
  49. *poutbuf= (uint8_t *) buf;
  50. *poutbuf_size= buf_size;
  51. return bsfc->filter->filter(bsfc, avctx, args, poutbuf, poutbuf_size, buf, buf_size, keyframe);
  52. }
  53. static int dump_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  54. uint8_t **poutbuf, int *poutbuf_size,
  55. const uint8_t *buf, int buf_size, int keyframe){
  56. int cmd= args ? *args : 0;
  57. /* cast to avoid warning about discarding qualifiers */
  58. if(avctx->extradata){
  59. if( (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER) && cmd=='a')
  60. ||(keyframe && (cmd=='k' || !cmd))
  61. ||(cmd=='e')
  62. /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
  63. int size= buf_size + avctx->extradata_size;
  64. *poutbuf_size= size;
  65. *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
  66. memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
  67. memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  68. return 1;
  69. }
  70. }
  71. return 0;
  72. }
  73. static int remove_extradata(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  74. uint8_t **poutbuf, int *poutbuf_size,
  75. const uint8_t *buf, int buf_size, int keyframe){
  76. int cmd= args ? *args : 0;
  77. AVCodecParserContext *s;
  78. if(!bsfc->parser){
  79. bsfc->parser= av_parser_init(avctx->codec_id);
  80. }
  81. s= bsfc->parser;
  82. if(s && s->parser->split){
  83. if( (((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)) && cmd=='a')
  84. ||(!keyframe && cmd=='k')
  85. ||(cmd=='e' || !cmd)
  86. ){
  87. int i= s->parser->split(avctx, buf, buf_size);
  88. buf += i;
  89. buf_size -= i;
  90. }
  91. }
  92. *poutbuf= (uint8_t *) buf;
  93. *poutbuf_size= buf_size;
  94. return 0;
  95. }
  96. static int noise(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  97. uint8_t **poutbuf, int *poutbuf_size,
  98. const uint8_t *buf, int buf_size, int keyframe){
  99. int amount= args ? atoi(args) : 10000;
  100. unsigned int *state= bsfc->priv_data;
  101. int i;
  102. *poutbuf= av_malloc(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  103. memcpy(*poutbuf, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
  104. for(i=0; i<buf_size; i++){
  105. (*state) += (*poutbuf)[i] + 1;
  106. if(*state % amount == 0)
  107. (*poutbuf)[i] = *state;
  108. }
  109. return 1;
  110. }
  111. static int mp3_header_compress(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args,
  112. uint8_t **poutbuf, int *poutbuf_size,
  113. const uint8_t *buf, int buf_size, int keyframe){
  114. uint32_t header, extraheader;
  115. int mode_extension, header_size;
  116. if(avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL){
  117. av_log(avctx, AV_LOG_ERROR, "not standards compliant\n");
  118. return -1;
  119. }
  120. header = AV_RB32(buf);
  121. mode_extension= (header>>4)&3;
  122. if(ff_mpa_check_header(header) < 0 || (header&0x60000) != 0x20000){
  123. output_unchanged:
  124. *poutbuf= (uint8_t *) buf;
  125. *poutbuf_size= buf_size;
  126. av_log(avctx, AV_LOG_INFO, "cannot compress %08X\n", header);
  127. return 0;
  128. }
  129. if(avctx->extradata_size == 0){
  130. avctx->extradata_size=15;
  131. avctx->extradata= av_malloc(avctx->extradata_size);
  132. strcpy(avctx->extradata, "FFCMP3 0.0");
  133. memcpy(avctx->extradata+11, buf, 4);
  134. }
  135. if(avctx->extradata_size != 15){
  136. av_log(avctx, AV_LOG_ERROR, "Extradata invalid\n");
  137. return -1;
  138. }
  139. extraheader = AV_RB32(avctx->extradata+11);
  140. if((extraheader&MP3_MASK) != (header&MP3_MASK))
  141. goto output_unchanged;
  142. header_size= (header&0x10000) ? 4 : 6;
  143. *poutbuf_size= buf_size - header_size;
  144. *poutbuf= av_malloc(buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
  145. memcpy(*poutbuf, buf + header_size, buf_size - header_size + FF_INPUT_BUFFER_PADDING_SIZE);
  146. if(avctx->channels==2){
  147. if((header & (3<<19)) != 3<<19){
  148. (*poutbuf)[1] &= 0x3F;
  149. (*poutbuf)[1] |= mode_extension<<6;
  150. FFSWAP(int, (*poutbuf)[1], (*poutbuf)[2]);
  151. }else{
  152. (*poutbuf)[1] &= 0x8F;
  153. (*poutbuf)[1] |= mode_extension<<4;
  154. }
  155. }
  156. return 1;
  157. }
  158. #ifdef CONFIG_DUMP_EXTRADATA_BSF
  159. AVBitStreamFilter dump_extradata_bsf={
  160. "dump_extra",
  161. 0,
  162. dump_extradata,
  163. };
  164. #endif
  165. #ifdef CONFIG_REMOVE_EXTRADATA_BSF
  166. AVBitStreamFilter remove_extradata_bsf={
  167. "remove_extra",
  168. 0,
  169. remove_extradata,
  170. };
  171. #endif
  172. #ifdef CONFIG_NOISE_BSF
  173. AVBitStreamFilter noise_bsf={
  174. "noise",
  175. sizeof(int),
  176. noise,
  177. };
  178. #endif
  179. #ifdef CONFIG_MP3_HEADER_COMPRESS_BSF
  180. AVBitStreamFilter mp3_header_compress_bsf={
  181. "mp3comp",
  182. 0,
  183. mp3_header_compress,
  184. };
  185. #endif